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

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

[nongnu] elpa/evil-nerd-commenter a61f920a66 146/235: commenter text obj


From: ELPA Syncer
Subject: [nongnu] elpa/evil-nerd-commenter a61f920a66 146/235: commenter text objext finished
Date: Thu, 6 Jan 2022 02:59:43 -0500 (EST)

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

    commenter text objext finished
---
 README.org                      |  12 +++-
 evil-nerd-commenter-operator.el | 119 ++++++++++++++++++++++++++++++++++------
 evil-nerd-commenter-sdk.el      |  79 ++++++++++++++++++++++++++
 evil-nerd-commenter.el          |  45 +++++++++------
 pkg.sh                          |   2 +-
 5 files changed, 219 insertions(+), 38 deletions(-)

diff --git a/README.org b/README.org
index e8642eaae8..f6ccde85f9 100644
--- a/README.org
+++ b/README.org
@@ -1,4 +1,4 @@
-* evil-nerd-commenter (v2.3.3)
+* evil-nerd-commenter (v3.0.0)
 
 
[[http://melpa.org/#/evil-nerd-commenter][file:http://melpa.org/packages/evil-nerd-commenter-badge.svg]]
 
[[http://stable.melpa.org/#/evil-nerd-commenter][file:http://stable.melpa.org/packages/evil-nerd-commenter-badge.svg]]
 
@@ -167,7 +167,15 @@ The first line is production code. The second line is your 
debug code. You want
 All you need to is =M-x evilnc-toggle-invert-comment-line-by-line= then =C-u 2 
evilnc-comment-or-uncomment-lines=. The first command turn on some flag, so the 
behavior of (un)commenting is different.
 * Evil usage
 If you use [[http://emacswiki.org/emacs/Evil][Evil]], you can use 
[[http://vimdoc.sourceforge.net/htmldoc/motion.html#text-objects][text objects 
and motions]]. But if you only *deals with lines*, I suggest using 
=evilnc-comment-or-uncomment-lines= instead.
-**  evilnc-comment-operator
+** commenter text object "c"
+We defined commenter text object "c" which can have *multi-lines*.
+
+Press =vac= to select outer object (comment with limiters).
+
+Press =vic= to select inner object (comment without limiter).
+
+You can assign other key instead of "c" to the text object by customizing 
=evilnc-comment-text-object=.
+** evilnc-comment-operator
 =evilnc-comment-operator= acts much like the delete/change operator. Takes a 
motion or text object and comments it out, yanking its content in the process.
 
 Example 1: ",,," to comment out the current line.
diff --git a/evil-nerd-commenter-operator.el b/evil-nerd-commenter-operator.el
index 19075e6f1e..eaae761706 100644
--- a/evil-nerd-commenter-operator.el
+++ b/evil-nerd-commenter-operator.el
@@ -1,39 +1,52 @@
 ;;; evil-nerd-commenter-operator.el --- Provides an evil operator for 
evil-nerd-commenter
 
-;; Copyright (C) 2013-2015, Chen Bin
+;; Copyright (C) 2013-2017, Chen Bin
 
 ;; Author: Chen Bin <chenbin.sh@gmail.com>
-;; URL: http://github.com/redguardtoo/evil-nerd-commenter
-;; Version: 2.3.3
-;; Keywords: commenter vim line evil
-;;
+
 ;; This file is not part of GNU Emacs.
 
 ;;; License:
 
-;; This file is part of evil-nerd-commenter
-;;
-;; evil-nerd-commenter is free software: you can redistribute it and/or
-;; modify it under the terms of the GNU General Public License as published
-;; by the Free Software Foundation, either version 3 of the License, or
-;; (at your option) any later version.
-;;
-;; evil-nerd-commenter is distributed in the hope that it will be useful,
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+
+;; This program is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 ;; GNU General Public License for more details.
-;;
+
 ;; You should have received a copy of the GNU General Public License
-;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+;; along with this program; if not, write to the Free Software
+;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 ;;; Commentary:
 
-;;
 ;; Provides operators for evil-mode.
 
 ;;; Code:
 
 (require 'evil)
+(require 'evil-nerd-commenter-sdk)
+
+(defvar evilnc-c-style-comment-modes
+  '(awk-mode
+    c++-mode
+    c-mode
+    css-mode
+    dart-mode
+    ess-mode
+    go-mode
+    java-mode
+    javascript-mode
+    js-mode
+    js2-mode
+    perl-mode
+    php-mode
+    swift-mode
+    web-mode))
 
 (evil-define-operator evilnc-comment-operator (beg end type)
   "Comments text from BEG to END with TYPE."
@@ -84,5 +97,77 @@
       ;; or else beg end will be screwed up
       (comment-region beg end))))
 
+
+(defun evilnc-get-comment-bounds ()
+  (let* ((b (point))
+         (e (point))
+         rlt)
+    ;; extend begin
+    (while (evilnc-is-comment (- b 1))
+      (setq b (- b 1)))
+    ;; extend end
+    (while (evilnc-is-comment (+ e 1))
+      (setq e (+ e 1)))
+
+    (if (< b e) (setq rlt (cons b e)))
+    rlt))
+
+(defun evilnc-ajusted-comment-end (b e)
+  (let* ((next-end-char (evilnc-get-char (- e 2)))
+         (end-char (evilnc-get-char (- e 1))))
+    ;; avoid selecting CR/LF
+    (while (and (< b e)
+                (memq (evilnc-get-char (- e 1)) '(10 13)))
+      (setq e (- e 1)))
+
+    ;; avoid selecting comment limiter
+    (cond
+     ((and (memq major-mode evilnc-c-style-comment-modes)
+           (= end-char 47) ; "/" => 47
+           (= next-end-char 42)) ; "*" => 42
+      ;; avoid selecting the ending comment limiter "*/"
+      (setq e (- e 2))
+      (while (and (> e b)
+                  (= (evilnc-get-char (- e 1)) 42))
+        (setq e (- e 1))))
+     (t
+      ;; other languages we can safely use font face
+      (while (and (> e b)
+                  (evilnc-is-comment-delimiter (- e 1)))
+        (setq e (- e 1)))))
+    e))
+
+;;;###autoload
+(evil-define-text-object evilnc-inner-comment (&optional count begin end type)
+  "An inner comment text object."
+  (let* ((bounds (evilnc-get-comment-bounds)))
+    (cond
+     (bounds
+      (let* ((b (save-excursion
+                  (goto-char (car bounds))
+                  (forward-word 1)
+                  (forward-word -1)
+                  (point)))
+             (e (save-excursion
+                  (goto-char (cdr bounds))
+                  (goto-char (evilnc-ajusted-comment-end b 
(line-end-position)))
+                  (point))))
+
+        (evil-range b e 'block :expanded t)))
+     (t
+      (error "Not inside a comment.")))))
+
+;;;###autoload
+(evil-define-text-object evilnc-outer-commenter (&optional count begin end 
type)
+  "An outer comment text object."
+  (let* ((bounds (evilnc-get-comment-bounds)))
+    (cond
+     (bounds
+      (let* ((b (car bounds))
+             (e (cdr bounds)))
+        (evil-range b e 'exclusive :expanded t)))
+     (t
+      (error "Not inside a comment.")))))
+
 (provide 'evil-nerd-commenter-operator)
 ;;; evil-nerd-commenter-operator.el ends here
diff --git a/evil-nerd-commenter-sdk.el b/evil-nerd-commenter-sdk.el
new file mode 100644
index 0000000000..4ae8f6ff95
--- /dev/null
+++ b/evil-nerd-commenter-sdk.el
@@ -0,0 +1,79 @@
+;;; evil-nerd-commenter-sdk.el --- SDK used by other files
+
+;; Copyright (C) 2017 Chen Bin
+
+;; Author: Chen Bin <chenin DOT sh AT gmail DOT com>
+
+;;; License:
+
+;; This file is not part of GNU Emacs.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; if not, write to the Free Software
+;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+;;; Commentary:
+;; SDK used internally
+
+;;; Code:
+
+(defun evilnc--check-fonts (fonts-under-cursor fonts-list)
+  (delq nil
+        (mapcar #'(lambda (f)
+                    ;; learn this trick from flyspell
+                    (member f fonts-list))
+                fonts-under-cursor)))
+
+(defun evilnc-web-mode-is-comment (&optional pos)
+  "Check whether the code at POS is comment.
+`web-mode' removes its API, so create our own."
+  (unless pos (setq pos (point)))
+  (not (null (or (eq (get-text-property pos 'tag-type) 'comment)
+                 (eq (get-text-property pos 'block-token) 'comment)
+                 (eq (get-text-property pos 'part-token) 'comment)))))
+
+(defun evilnc-is-comment (pos)
+  "Check whether the code at POS is comment by comparing font face."
+  (let* ((fontfaces (if (> pos 0) (get-text-property pos 'face))))
+    (if (not (listp fontfaces))
+        (setf fontfaces (list fontfaces)))
+    (cond
+     ((or (< pos (point-min)) (> pos (point-max)))
+      nil)
+     ((and (not fontfaces)
+           ;; character under cursor is SPACE or TAB
+           (member (evilnc-get-char pos) '(32 9)))
+      t)
+     ((string= major-mode "web-mode")
+      (evilnc-web-mode-is-comment pos))
+     (t
+      (evilnc--check-fonts fontfaces
+                           '(font-lock-comment-face
+                             font-lock-comment-delimiter-face))))))
+
+(defun evilnc-get-char (pos)
+  (save-excursion
+    (goto-char pos)
+    (following-char)))
+
+(defun evilnc-is-comment-delimiter (pos)
+  (let* ((fontfaces (if (> pos 0) (get-text-property pos 'face))))
+    (if (not (listp fontfaces))
+        (setf fontfaces (list fontfaces)))
+    (and fontfaces
+         (evilnc--check-fonts fontfaces
+                              '(font-lock-comment-delimiter-face)))))
+
+(provide 'evil-nerd-commenter-sdk)
+;;; evil-nerd-commenter-sdk.el ends here
+
diff --git a/evil-nerd-commenter.el b/evil-nerd-commenter.el
index 0944857dce..96797ef56f 100644
--- a/evil-nerd-commenter.el
+++ b/evil-nerd-commenter.el
@@ -1,10 +1,10 @@
 ;;; evil-nerd-commenter.el --- Comment/uncomment lines efficiently. Like Nerd 
Commenter in Vim
 
-;; Copyright (C) 2013-2016, Chen Bin
+;; Copyright (C) 2013-2017, Chen Bin
 
 ;; Author: Chen Bin <chenbin.sh@gmail.com>
 ;; URL: http://github.com/redguardtoo/evil-nerd-commenter
-;; Version: 2.3.3
+;; Version: 3.0.0
 ;; Keywords: commenter vim line evil
 ;;
 ;; This file is not part of GNU Emacs.
@@ -83,6 +83,12 @@
 ;;   - Place the commented out text above original text
 ;;   - Or place the original text above commented out text
 ;;
+;; We defined comment text object "c" which can have multi-lines.
+;; Press "vac" to select outer object (comment with limiters).
+;; Press "vic" to select inner object (comment without limiter).
+;; You can assign other key instead of "c" to the text object by
+;; customizing `evilnc-comment-text-object'.
+
 ;; For certain major modes, you need manual setup to override its original
 ;; keybindings,
 ;;
@@ -94,6 +100,8 @@
 ;;
 ;;; Code:
 
+(require 'evil-nerd-commenter-sdk)
+
 (autoload 'count-lines "simple")
 
 (defvar evilnc-original-above-comment-when-copy-and-comment nil
@@ -108,6 +116,11 @@ Please note it has NOT effect on evil text object!")
   "Comment both embedded snippet and HTML tag if they are mixed in one line.
 `web-mode' required.")
 
+(defvar evilnc-comment-text-object "c"
+  "The comment object.
+`vic` to select inner object.
+`vac` to select outer objectselect outer object.")
+
 (defun evilnc--count-lines (beg end)
   "Assume BEG is less than END."
   (let* ((rlt (count-lines beg end)))
@@ -126,14 +139,6 @@ Please note it has NOT effect on evil text object!")
       (re-search-forward "[\n\C-m]" nil 'end (1- line-num))
       (forward-line (1- line-num)))))
 
-(defun evilnc--web-mode-is-comment (&optional pos)
-  "Check whether the code at POS is comment.
-`web-mode' removes its API, so create our own."
-  (unless pos (setq pos (point)))
-  (not (null (or (eq (get-text-property pos 'tag-type) 'comment)
-                 (eq (get-text-property pos 'block-token) 'comment)
-                 (eq (get-text-property pos 'part-token) 'comment)))))
-
 (defun evilnc--fix-buggy-major-modes ()
   "Fix major modes whose comment regex is buggy.
 See http://lists.gnu.org/archive/html/bug-gnu-emacs/2013-03/msg00891.html.";
@@ -142,7 +147,7 @@ See 
http://lists.gnu.org/archive/html/bug-gnu-emacs/2013-03/msg00891.html.";
     ;; since comment-use-syntax is nil in autoconf.el, the comment-start-skip 
need
     ;; make sure its first parenthesized expression match the string exactly 
before
     ;; the "dnl", check the comment-start-skip in lisp-mode for sample.
-    ;; See code in (defun comment-search-forward) from emacs 24.2.3.3:
+    ;; See code in (defun comment-search-forward) from emacs 24.2:
     ;; (if (not comment-use-syntax)
     ;;     (if (re-search-forward comment-start-skip limit noerror)
     ;;     (or (match-end 1) (match-beginning 0)))
@@ -354,12 +359,12 @@ Code snippets embedded in Org-mode is identified and 
right `major-mode' is used.
                      (goto-char beg)
                      (goto-char (line-end-position))
                      (re-search-backward "^\\|[^[:space:]]")
-                     (evilnc--web-mode-is-comment))
-                   (evilnc--web-mode-is-comment (/ (+ beg end) 2))
+                     (evilnc-web-mode-is-comment))
+                   (evilnc-web-mode-is-comment (/ (+ beg end) 2))
                    (save-excursion
                      (goto-char end)
                      (back-to-indentation)
-                     (evilnc--web-mode-is-comment)))))
+                     (evilnc-web-mode-is-comment)))))
     rlt))
 
 (defun evilnc--web-mode-do-current-line ()
@@ -449,8 +454,7 @@ If UNITS is 16, line 16, line 116, and line 216 are good 
candidates."
       (setq r (* r 10))
       (setq l (- l 1)))
     (if (>= (mod cur-line-num r) UNITS)
-        (setq UNITS (+ UNITS r))
-      )
+        (setq UNITS (+ UNITS r)))
     (setq dst-line-num (+ cur-line-num (- UNITS (mod cur-line-num r))))))
 
 ;; ==== below this line are public commands
@@ -670,7 +674,7 @@ Then we operate the expanded region.  NUM is ignored."
 (defun evilnc-version ()
   "The version number."
   (interactive)
-  (message "2.3.3"))
+  (message "3.0.0"))
 
 ;;;###autoload
 (defun evilnc-default-hotkeys (&optional no-evil-keybindings)
@@ -699,10 +703,15 @@ If NO-EVIL-KEYBINDINGS is t, we don't define keybindings 
in evil-mode."
     ;; Install operator for evil text objects
     (eval-after-load 'evil-nerd-commenter-operator
       '(progn
+         ;; operator to comment at text objects
          (define-key evil-normal-state-map ",." 
'evilnc-copy-and-comment-operator)
          (define-key evil-visual-state-map ",." 
'evilnc-copy-and-comment-operator)
          (define-key evil-normal-state-map ",," 'evilnc-comment-operator)
-         (define-key evil-visual-state-map ",," 'evilnc-comment-operator)))))
+         (define-key evil-visual-state-map ",," 'evilnc-comment-operator)
+
+         ;; comment itself is text object
+         (define-key evil-inner-text-objects-map evilnc-comment-text-object 
'evilnc-inner-comment)
+         (define-key evil-outer-text-objects-map evilnc-comment-text-object 
'evilnc-outer-commenter)))))
 
 ;; Attempt to define the operator on first load.
 ;; Will only work if evil has been loaded
diff --git a/pkg.sh b/pkg.sh
index 3cea9135c8..4eb24d8bbe 100755
--- a/pkg.sh
+++ b/pkg.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 name=evil-nerd-commenter
-version=2.3.3
+version=3.0.0
 pkg=$name-$version
 mkdir $pkg
 cp *.el $pkg



reply via email to

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