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

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

[nongnu] elpa/iedit e575cbf569 256/301: Allow adding a incremented numbe


From: ELPA Syncer
Subject: [nongnu] elpa/iedit e575cbf569 256/301: Allow adding a incremented number in each occurence
Date: Mon, 10 Jan 2022 22:59:08 -0500 (EST)

branch: elpa/iedit
commit e575cbf5695ecea6df75a1d9f65fe3a588485c88
Author: Thierry Volpiatto <thierry.volpiatto@gmail.com>
Commit: Victor <victorhge@gmail.com>

    Allow adding a incremented number in each occurence
    
    At any place.
    
    Added a new user variable that allow configuring the default format string
    used by `iedit-increment-occurences`.
    Additionally allow editing the format string when calling
    `iedit-increment-occurences` with a prefix arg.
---
 iedit-lib.el | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/iedit-lib.el b/iedit-lib.el
index f448ebd47e..236a3f5a29 100644
--- a/iedit-lib.el
+++ b/iedit-lib.el
@@ -99,6 +99,12 @@ the traverse of the long `iedit-occurrences-overlays' list."
   :type 'integer
   :group 'iedit)
 
+(defcustom iedit-increment-format-string "%03d"
+  "Format string used to format incremented numbers.
+This is used by `iedit-increment-occurences'."
+  :type 'string
+  :group 'iedit)
+
 (defvar iedit-occurrences-overlays nil
   "The occurrences slot contains a list of overlays used to
 indicate the position of each editable occurrence.  In addition, the
@@ -214,6 +220,7 @@ occurrence.")
     (define-key map (kbd "M-SPC") 'iedit-blank-occurrences)
     (define-key map (kbd "M-D") 'iedit-delete-occurrences)
     (define-key map (kbd "M-N") 'iedit-number-occurrences)
+    (define-key map (kbd "M-V") 'iedit-increment-occurences)
     (define-key map (kbd "M-B") 'iedit-toggle-buffering)
     (define-key map (kbd "M-<") 'iedit-goto-first-occurrence)
     (define-key map (kbd "M->") 'iedit-goto-last-occurrence)
@@ -689,14 +696,35 @@ value of `iedit-occurrence-context-lines' is used for 
this time."
   (iedit-barf-if-buffering)
   (iedit-apply-on-occurrences 'upcase-region))
 
-
-
 (defun iedit-downcase-occurrences()
   "Covert occurrences to lower case."
   (interactive "*")
   (iedit-barf-if-buffering)
   (iedit-apply-on-occurrences 'downcase-region))
 
+(defun iedit-increment-occurences (&optional arg)
+  "Replace placeholder \"\\#\" by incremented number in each occurrence.
+Called with a prefix arg, allow editing the format string used, which
+default to `iedit-increment-format-string'."
+  (interactive "*P")
+  (iedit-barf-if-buffering)
+  (let ((inhibit-modification-hooks t)
+        (fmt-str (if arg
+                     (read-string
+                      (format "Format incremented numbers (default '%s'): "
+                              iedit-increment-format-string)
+                      nil nil iedit-increment-format-string)
+                   iedit-increment-format-string)))
+    (save-excursion
+      (cl-loop for occurrence in (reverse iedit-occurrences-overlays)
+               for counter from 1
+               for beg = (overlay-start occurrence)
+               for end = (overlay-end occurrence)
+               do (progn
+                    (goto-char beg)
+                    (when (re-search-forward "\\\\#" end t)
+                      (replace-match (format fmt-str counter) t)))))))
+
 ;;; Don't downcase from-string to allow case freedom!
 (defun iedit-replace-occurrences(&optional to-string)
   "Replace occurrences with STRING."



reply via email to

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