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

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

[nongnu] elpa/evil-nerd-commenter 9e7e969719 226/235: try stay put when


From: ELPA Syncer
Subject: [nongnu] elpa/evil-nerd-commenter 9e7e969719 226/235: try stay put when comment in evil visual line state
Date: Thu, 6 Jan 2022 02:59:50 -0500 (EST)

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

    try stay put when comment in evil visual line state
---
 evil-nerd-commenter.el | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/evil-nerd-commenter.el b/evil-nerd-commenter.el
index f7c9965b7d..cf8424a04f 100644
--- a/evil-nerd-commenter.el
+++ b/evil-nerd-commenter.el
@@ -629,6 +629,20 @@ to comment to the line 6453"
                "Empty line(s) will be commented"
              "Empty line(s) will NOT be commented")))
 
+(defun evilnc-visual-line-p ()
+  "In Evil visual line mode."
+  (and (fboundp 'evil-visual-type) (eq (evil-visual-type) 'line)))
+
+(defun evilnc-guess-position-at-point ()
+  "Guess current position when evil visual line state is on or off."
+  (cond
+   ((and (evilnc-visual-line-p)
+         (eq (point) (nth 1 (evil-visual-range))))
+    ;; In evil visual line state, point is beginning or end visual range
+    (1- (point)))
+   (t
+    (point))))
+
 ;;;###autoload
 (defun evilnc-comment-or-uncomment-lines (&optional num)
   "Comment or uncomment NUM lines.  NUM could be negative.
@@ -643,7 +657,7 @@ Case 3: If a region inside of ONE line is selected,
 we comment/uncomment that region.
 CORRECT comment syntax will be used for C++/Java/Javascript."
   (interactive "p")
-  (let* ((orig-pos (point)))
+  (let* ((orig-pos (evilnc-guess-position-at-point)))
     ;; donot move the cursor
     ;; support negative number
     (cond
@@ -655,10 +669,14 @@ CORRECT comment syntax will be used for 
C++/Java/Javascript."
         (when (< num 0)
           (evilnc--forward-line (1+ num))
           (setq num (- 0 num)))
-        (evilnc--operation-on-lines-or-region '(lambda (b e)
-                                                 
(evilnc--fix-buggy-major-modes)
-                                                 
(evilnc-comment-or-uncomment-region b e))
-                                              num))
+        (evilnc--operation-on-lines-or-region
+         (lambda (b e)
+           (setq orig-pos (point))
+           (evilnc--fix-buggy-major-modes)
+           ;; when comment in evil visual state, the cursor may be rogue
+           (when (evilnc-visual-line-p) (evil-normal-state))
+           (evilnc-comment-or-uncomment-region b e))
+         num))
       (goto-char orig-pos)))))
 
 ;;;###autoload



reply via email to

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