emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/evil-nerd-commenter 82069b7643 121/235: comment region res


From: ELPA Syncer
Subject: [nongnu] elpa/evil-nerd-commenter 82069b7643 121/235: comment region restricted in one line #50, v2.0
Date: Thu, 6 Jan 2022 02:59:40 -0500 (EST)

branch: elpa/evil-nerd-commenter
commit 82069b7643c7ef5549ccdd8b38214393fcb36c7c
Author: Chen Bin <chenbin.sh@gmail.com>
Commit: Chen Bin <chenbin.sh@gmail.com>

    comment region restricted in one line #50, v2.0
    
    - evilnc-comment-or-uncomment-lines enhancement
    - right comment syntax is used automatically
---
 README.org                      |   7 ++-
 evil-nerd-commenter-operator.el |   2 +-
 evil-nerd-commenter-pkg.el      |   2 +-
 evil-nerd-commenter.el          | 103 +++++++++++++++++++++++++++-------------
 pkg.sh                          |   2 +-
 5 files changed, 78 insertions(+), 38 deletions(-)

diff --git a/README.org b/README.org
index 5a6c619450..bce9059d56 100644
--- a/README.org
+++ b/README.org
@@ -1,4 +1,4 @@
-* evil-nerd-commenter (v1.5.15)
+* evil-nerd-commenter (v2.0)
 
 This program can be used *independently WITHOUT 
[[http://www.emacswiki.org/Evil][evil-mode]]!*
 
@@ -70,6 +70,11 @@ comment/uncomment lines. This command supports negative 
arguments.
 
 The hotkey is ",ci" in evil-mode and "M-;" in all modes. "M" means ALT key.
 
+If a region selected, the region is expand to make sure the region contain
+whole lines. Then we comment/uncomment the expanded region. NUM is ignored.
+
+If the region is insided of ONE line, we comment/uncomment that region. In 
this case, CORRECT comment syntax will be used for C++/Java/Javascript.
+
 This may be the *only command* you need to learn!
 *** evilnc-quick-comment-or-uncomment-to-the-line
 comment/uncomment from current line to the user-specified line. You can input 
the rightest digit(s) to specify the line number if you want to type less.
diff --git a/evil-nerd-commenter-operator.el b/evil-nerd-commenter-operator.el
index 97eaf4dadb..28c14c3615 100644
--- a/evil-nerd-commenter-operator.el
+++ b/evil-nerd-commenter-operator.el
@@ -4,7 +4,7 @@
 
 ;; Author: Chen Bin <chenbin.sh@gmail.com>
 ;; URL: http://github.com/redguardtoo/evil-nerd-commenter
-;; Version: 1.5.15
+;; Version: 2.0
 ;; Keywords: commenter vim line evil
 ;;
 ;; This file is not part of GNU Emacs.
diff --git a/evil-nerd-commenter-pkg.el b/evil-nerd-commenter-pkg.el
index bbad4fa911..9b81bdac81 100644
--- a/evil-nerd-commenter-pkg.el
+++ b/evil-nerd-commenter-pkg.el
@@ -1,2 +1,2 @@
-(define-package "evil-nerd-commenter" "1.5.15"
+(define-package "evil-nerd-commenter" "2.0"
                 "Comment/uncomment lines efficiently. Like Nerd Commenter in 
Vim")
diff --git a/evil-nerd-commenter.el b/evil-nerd-commenter.el
index ac79535b10..a5d33cbb23 100644
--- a/evil-nerd-commenter.el
+++ b/evil-nerd-commenter.el
@@ -4,7 +4,7 @@
 
 ;; Author: Chen Bin <chenbin.sh@gmail.com>
 ;; URL: http://github.com/redguardtoo/evil-nerd-commenter
-;; Version: 1.5.15
+;; Version: 2.0
 ;; Keywords: commenter vim line evil
 ;;
 ;; This file is not part of GNU Emacs.
@@ -121,35 +121,69 @@
         (setq comment-start-skip "^\\(\\s*\\)\\(dnl\\|#\\) +"))))
 
 (defun evilnc--operation-on-lines-or-region (fn &optional NUM)
-  (if (not (region-active-p))
-      (let ((b (line-beginning-position)) e)
-        (save-excursion
-          (forward-line (- NUM 1))
-          (setq e (line-end-position))
-          )
-        (funcall fn b e))
-    ;; expand selected region
-    (progn
+  (cond
+   ((not (region-active-p))
+    (let ((b (line-beginning-position)) e)
       (save-excursion
-        (let ((b (region-beginning))
-              (e (region-end))
-              )
-          ;; another work around for evil-visual-line bug:
-          ;; in evil-mode, if we use hot key V `M-x evil-visual-line` to 
select line
-          ;; the (line-beginning-position) of the line which is after the last 
selected
-          ;; line is always (region-end)! Don't know why.
-          (if (and (> e b)
-                     (save-excursion (goto-char e) (= e 
(line-beginning-position)))
-                     (boundp 'evil-state) (eq evil-state 'visual))
-              (setq e (1- e)))
-
-          (goto-char b)
-          (setq b (line-beginning-position))
-          (goto-char e)
-          (setq e (line-end-position))
-          (funcall fn b e)
-          ))
-      )))
+        (forward-line (- NUM 1))
+        (setq e (line-end-position))
+        )
+      (funcall fn b e)))
+   ((and (<= (line-beginning-position) (region-beginning))
+          (<= (region-end) (line-end-position)))
+    ;; Select region inside ONE line
+    (cond
+     ;; Well, looks current comment syntax is NOT fit for comment out a region.
+     ;; So we also need hack the comment-start and comment-end
+     ((and (string= "" comment-end)
+           (member major-mode '(java-mode
+                                javascript-mode
+                                js-mode
+                                js2-mode
+                                js3-mode
+                                c++-mode
+                                objc-mode)))
+      (let ((comment-start-old comment-start)
+            (comment-end-old comment-end)
+            (comment-start-skip-old comment-start-skip)
+            (comment-end-skip-old comment-end-skip))
+
+        ;; use C comment syntax temporarily
+        (setq comment-start "/* ")
+        (setq comment-end " */")
+        (setq comment-start-skip "\\(//+\\|/\\*+\\)\\s *")
+        (setq comment-end-skip "[      ]*\\(\\s>\\|\\*+/\\)")
+
+        (funcall fn (region-beginning) (region-end))
+
+        ;; Restore the original comment syntax
+        (setq comment-start comment-start-old)
+        (setq comment-end comment-end-old)
+        (setq comment-start-skip comment-start-skip-old)
+        (setq comment-end-skip comment-end-skip-old)))
+     ;; just comment out the region
+     (t (funcall fn (region-beginning) (region-end)))))
+   (t
+    ;; selected region spans MORE than one line
+    (save-excursion
+      (let ((b (region-beginning))
+            (e (region-end)))
+        ;; Another work around for evil-visual-line bug:
+        ;; In evil-mode, if we use hotkey V or `M-x evil-visual-line` to 
select line,
+        ;; the (line-beginning-position) of the line which is after the last 
selected
+        ;; line is always (region-end)! Don't know why.
+        (if (and (> e b)
+                 (save-excursion (goto-char e) (= e (line-beginning-position)))
+                 (boundp 'evil-state) (eq evil-state 'visual))
+            (setq e (1- e)))
+
+        (goto-char b)
+        (setq b (line-beginning-position))
+        (goto-char e)
+        (setq e (line-end-position))
+        (funcall fn b e)
+        )))
+   ))
 
 (defun evilnc--get-one-paragraph-region ()
   (let (b e)
@@ -299,12 +333,11 @@
       (web-mode-uncomment (/ (+ beg end) 2))
       )
      (t
-      (when (not (region-active-p))
+      (unless (region-active-p)
         (push-mark beg t t)
         (goto-char end))
       (web-mode-comment (/ (+ beg end) 2)))
-     )
-    )
+     ))
     (t
      (evilnc--working-on-region beg end 'comment-or-uncomment-region))
     ))
@@ -428,7 +461,9 @@ or 'C-u 3 M-x 
evilnc-quick-comment-or-uncomment-to-the-line' to comment to the l
    Case 1: If no region selected, comment/uncomment on current line. if NUM>1, 
comment/uncomment
    extra N-1 lines from next line
    Case 2: If a region selected, the region is expand to make sure the region 
contain
-   whole lines. Then we comment/uncomment the expanded region. NUM is ignored."
+   whole lines. Then we comment/uncomment the expanded region. NUM is ignored.
+   Case 3: If a region in ONE line is selected, we comment/uncomment that 
region.
+   In this case, CORRECT comment syntax will be used for C++/Java/Javascript."
   (interactive "p")
   ;; donot move the cursor
   ;; support negative number
@@ -510,7 +545,7 @@ or 'C-u 3 M-x 
evilnc-quick-comment-or-uncomment-to-the-line' to comment to the l
 ;;;###autoload
 (defun evilnc-version ()
   (interactive)
-  (message "1.5.15"))
+  (message "2.0"))
 
 ;;;###autoload
 (defun evilnc-default-hotkeys ()
diff --git a/pkg.sh b/pkg.sh
index a99963e1ab..50ea6366e4 100755
--- a/pkg.sh
+++ b/pkg.sh
@@ -1,5 +1,5 @@
 #!/bin/bash
-pkg=evil-nerd-commenter-1.5.15
+pkg=evil-nerd-commenter-2.0
 mkdir $pkg
 cp README.org $pkg
 cp *.el $pkg



reply via email to

[Prev in Thread] Current Thread [Next in Thread]