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

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

[nongnu] elpa/evil-nerd-commenter a32e12d16d 166/235: add evilnc-imenu-c


From: ELPA Syncer
Subject: [nongnu] elpa/evil-nerd-commenter a32e12d16d 166/235: add evilnc-imenu-create-index-function
Date: Thu, 6 Jan 2022 02:59:45 -0500 (EST)

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

    add evilnc-imenu-create-index-function
---
 README.org             | 13 +++++++++-
 evil-nerd-commenter.el | 64 ++++++++++++++++++++++++++++++++++++++++++++++----
 pkg.sh                 |  2 +-
 3 files changed, 73 insertions(+), 6 deletions(-)

diff --git a/README.org b/README.org
index b3ce9a6546..507c7cd1c9 100644
--- a/README.org
+++ b/README.org
@@ -1,4 +1,4 @@
-* evil-nerd-commenter (v3.1.3)
+* evil-nerd-commenter (v3.2.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]]
 
@@ -138,6 +138,17 @@ For example:
 If a line is snippet wrapped HTML tags in HTML template, only the HTML syntax 
is used to comment out the line by default.
 
 But if you =(setq evilnc-comment-both-snippet-html t)=, snippet will be 
commented out with its own syntax at first. Then the wrapped html tag will be 
comment out using HTML syntax. This flag has effect on all above commands.  
[[http://web-mode.org/][Web-mode]] should be enabled to use this flag.
+*** Use imenu to list and jump to comments in current file
+Please setup `imenu-create-index-function' to 
`evilnc-imenu-create-index-function'.
+
+Setup on using =counsel-imenu= to list comments in current buffer,
+#+begin_src elisp
+(defun counsel-imenu-comments ()
+  (interactive)
+  (let* ((imenu-create-index-function 'evilnc-imenu-create-index-function))
+    (unless (featurep 'counsel) (require 'counsel))
+    (counsel-imenu)))
+#+end_src
 ** Examples
 *** Comment lines
 =C-u NUM M-x evilnc-comment-or-uncomment-lines=, comment/uncomment next NUM 
lines.
diff --git a/evil-nerd-commenter.el b/evil-nerd-commenter.el
index 51f7568c62..2e2e37a2c5 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: 3.1.3
+;; Version: 3.2.0
 ;; Keywords: commenter vim line evil
 ;;
 ;; This file is not part of GNU Emacs.
@@ -92,7 +92,16 @@
 ;;
 ;; You can assign other key instead of "c" to the text object by
 ;; customizing `evilnc-comment-text-object'.
-
+;;
+;; You can list of comments in current buffer through using imenu.
+;; by setup `imenu-create-index-function' to 
`evilnc-imenu-create-index-function',
+;;
+;;   (defun counsel-imenu-comments ()
+;;     (interactive)
+;;     (let* ((imenu-create-index-function 
'evilnc-imenu-create-index-function))
+;;       (unless (featurep 'counsel) (require 'counsel))
+;;       (counsel-imenu)))
+;;
 ;; For certain major modes, you need manual setup to override its original
 ;; keybindings,
 ;;
@@ -125,7 +134,10 @@ Please note it has NOT effect on evil text object!")
 `vic` to select inner object.
 `vac` to select outer object.")
 
-(defun evilnc--count-lines (beg end)
+(defvar evilnc-min-comment-length-for-imenu 8
+  "Minimum length of comment to display in imenu.")
+
+(defun  evilnc--count-lines (beg end)
   "Assume BEG is less than END."
   (let* ((rlt (count-lines beg end)))
     (save-excursion
@@ -653,7 +665,7 @@ Then we operate the expanded region.  NUM is ignored."
 (defun evilnc-version ()
   "The version number."
   (interactive)
-  (message "3.1.3"))
+  (message "3.2.0"))
 
 (defvar evil-normal-state-map)
 (defvar evil-visual-state-map)
@@ -697,6 +709,50 @@ If NO-EVIL-KEYBINDINGS is t, we don't define keybindings 
in EVIL."
          (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)))))
 
+;;;###autoload
+(defun evilnc-imenu-create-index-function ()
+  "Imenu function find comments."
+  (let* (beg
+         end
+         linenum
+         str
+         (searching t)
+         m
+         cands)
+    (save-excursion
+      (goto-char (point-min))
+      ;; learn this skill from etags-select
+      ;; use simple string search to speed up searching
+      (while searching
+        (setq beg (search-forward comment-start (point-max) t))
+        ;; OK, it's comment
+        (cond
+         ((not beg)
+          (setq searching nil))
+         (t
+          (setq beg (1+ beg))))
+
+        (when (and searching (evilnc-is-comment beg))
+          (setq linenum (line-number-at-pos beg) )
+          (cond
+           ((string= comment-end "")
+            (setq end (line-end-position)))
+           (t
+            (setq end (search-forward comment-end (point-max) t))))
+          (cond
+           ((and end (> end beg))
+            (setq str (buffer-substring-no-properties beg end))
+            (when (and (not (string-match-p "^[ \t]*$" str))
+                       (> (length str) evilnc-min-comment-length-for-imenu))
+              (setq m (make-marker))
+              (set-marker m beg)
+              (add-to-list 'cands
+                           (cons (format "%d:%s" linenum str) m)
+                           t)))
+           (t
+            (setq searching nil))))))
+    cands))
+
 ;; Attempt to define the operator on first load.
 ;; Will only work if evil has been loaded
 (eval-after-load 'evil
diff --git a/pkg.sh b/pkg.sh
index 8c58cdab3b..7d11662ade 100755
--- a/pkg.sh
+++ b/pkg.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 name=evil-nerd-commenter
-version=3.1.3
+version=3.2.0
 pkg=$name-$version
 mkdir $pkg
 cp *.el $pkg



reply via email to

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