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

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

[nongnu] elpa/multiple-cursors 9582c72 204/434: Add mc/insert-numbers


From: ELPA Syncer
Subject: [nongnu] elpa/multiple-cursors 9582c72 204/434: Add mc/insert-numbers
Date: Sat, 7 Aug 2021 09:20:26 -0400 (EDT)

branch: elpa/multiple-cursors
commit 9582c7220bb106ee8a803a44ffe31753ec480f4f
Author: Magnar Sveen <magnars@gmail.com>
Commit: Magnar Sveen <magnars@gmail.com>

    Add mc/insert-numbers
    
     - adds increasing numbers for each cursor, top to bottom
---
 README.md                       |  9 +++++---
 features/insert-numbers.feature | 13 ++++++++++++
 features/support/env.el         |  1 +
 mc-insert-numbers.el            | 47 +++++++++++++++++++++++++++++++++++++++++
 multiple-cursors-core.el        | 26 +++++++++++++----------
 multiple-cursors.el             |  6 ++++++
 6 files changed, 88 insertions(+), 14 deletions(-)

diff --git a/README.md b/README.md
index 7502326..fe873fc 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ You can [watch an intro to multiple-cursors at Emacs 
Rocks](http://emacsrocks.co
 
  - `set-rectangular-region-anchor`: Think of this one as `set-mark` except 
you're marking a rectangular region.
  - `mc/mark-sgml-tag-pair`: Mark the current opening and closing tag.
+ - `mc/insert-numbers`: Insert increasing numbers for each cursor, top to 
bottom.
 
 ## Tips and tricks
 
@@ -72,14 +73,16 @@ You can [watch an intro to multiple-cursors at Emacs 
Rocks](http://emacsrocks.co
 - Sometimes you end up with cursors outside of your view. You can
   scroll the screen to center on each cursor with `C-v` and `M-v`.
 
-- Try pressing `mc/mark-next-like-this` with no region selected. It will just 
add a cursor
-  on the next line.
+- Try pressing `mc/mark-next-like-this` with no region selected. It
+  will just add a cursor on the next line.
 
 - Try pressing `mc/mark-all-like-this-dwim` on a tagname in html-mode.
 
 - Notice that the number of cursors active can be seen in the modeline.
 
-- If you get out of multiple-cursors-mode and yank - it will yank only from 
the kill-ring of main cursor. To yank from the kill-rings of every cursor use 
yank-rectangle, normally found at C-x r y.
+- If you get out of multiple-cursors-mode and yank - it will yank only
+  from the kill-ring of main cursor. To yank from the kill-rings of
+  every cursor use yank-rectangle, normally found at C-x r y.
 
 - If you would like to keep the global bindings clean, and get custom 
keybindings
   when the region is active, you can try 
[region-bindings-mode](https://github.com/fgallina/region-bindings-mode).
diff --git a/features/insert-numbers.feature b/features/insert-numbers.feature
new file mode 100644
index 0000000..75f5665
--- /dev/null
+++ b/features/insert-numbers.feature
@@ -0,0 +1,13 @@
+Feature: Insert increasing numbers
+
+  Scenario: Three cursors, 0-1-2
+    Given I have cursors at "text" in "This text contains the word text thrice 
(text)"
+    When I press "H-0"
+    And I press "SPC"
+    Then I should see "This 0 text contains the word 1 text thrice (2 text)"
+
+  Scenario: Three cursors, 9-10-11
+    Given I have cursors at "text" in "This text contains the word text thrice 
(text)"
+    When I press "C-9 H-0"
+    And I press "SPC"
+    Then I should see "This 9 text contains the word 10 text thrice (11 text)"
diff --git a/features/support/env.el b/features/support/env.el
index 53edb3b..f6da1f5 100644
--- a/features/support/env.el
+++ b/features/support/env.el
@@ -25,6 +25,7 @@
  (global-set-key (kbd "M-!") 'mc/mark-all-like-this)
  (global-set-key (kbd "M-$") 'mc/mark-all-like-this-dwim)
  (global-set-key (kbd "M-#") 'mc/mark-all-in-region)
+ (global-set-key (kbd "H-0") 'mc/insert-numbers)
  (global-set-key (kbd "C-S-c C-S-c") 'mc/edit-lines)
  (global-set-key (kbd "H-SPC") 'set-rectangular-region-anchor)
  (switch-to-buffer
diff --git a/mc-insert-numbers.el b/mc-insert-numbers.el
new file mode 100644
index 0000000..d226c69
--- /dev/null
+++ b/mc-insert-numbers.el
@@ -0,0 +1,47 @@
+;;; mc-insert-numbers.el
+
+;; Copyright (C) 2012 Magnar Sveen
+
+;; Author: Magnar Sveen <magnars@gmail.com>
+;; Keywords: editing cursors
+
+;; 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This file contains functions for adding increasing integers to
+;; each cursor.
+
+;; Please see multiple-cursors.el for more commentary.
+
+;;; Code:
+
+(require 'multiple-cursors-core)
+
+;;;###autoload
+(defun mc/insert-numbers (arg)
+  (interactive "P")
+  (setq mc--insert-numbers-number (or arg 0))
+  (mc/for-each-cursor-ordered
+   (mc/execute-command-for-fake-cursor 'mc--insert-number-and-increase 
cursor)))
+
+(defvar mc--insert-numbers-number 0)
+
+(defun mc--insert-number-and-increase ()
+  (interactive)
+  (insert (number-to-string mc--insert-numbers-number))
+  (setq mc--insert-numbers-number (1+ mc--insert-numbers-number)))
+
+(provide 'mc-insert-numbers)
+;;; mc-insert-numbers.el ends here
diff --git a/multiple-cursors-core.el b/multiple-cursors-core.el
index eba2417..0069bcf 100644
--- a/multiple-cursors-core.el
+++ b/multiple-cursors-core.el
@@ -72,12 +72,12 @@
 
 (defmacro mc/for-each-cursor-ordered (&rest forms)
   "Runs the body for each cursor, fake and real, bound to the name cursor"
-  `(let ((real-cursor (mc/create-fake-cursor-at-point)))
+  `(let ((real-cursor-id (overlay-get (mc/create-fake-cursor-at-point) 
'mc-id)))
      (mapc #'(lambda (cursor)
                (when (mc/fake-cursor-p cursor)
                  ,@forms))
            (sort (overlays-in (point-min) (point-max)) 
'mc--compare-by-overlay-start))
-     (mc/pop-state-from-overlay real-cursor)))
+     (mc/pop-state-from-overlay (mc/cursor-with-id real-cursor-id))))
 
 (defmacro mc/save-window-scroll (&rest forms)
   "Saves and restores the window scroll position"
@@ -198,6 +198,17 @@ Saves the current state in the overlay to be restored 
later."
 
 (defvar mc--executing-command-for-fake-cursor nil)
 
+(defun mc/execute-command-for-fake-cursor (cmd cursor)
+  (let ((mc--executing-command-for-fake-cursor t)
+        (id (overlay-get cursor 'mc-id))
+        (annoying-arrows-mode nil)
+        (smooth-scroll-margin 0))
+    (mc/add-fake-cursor-to-undo-list
+     (mc/pop-state-from-overlay cursor)
+     (ignore-errors
+       (mc/execute-command cmd)
+       (mc/create-fake-cursor-at-point id)))))
+
 (defun mc/execute-command-for-all-fake-cursors (cmd)
   "Calls CMD interactively for each cursor.
 It works by moving point to the fake cursor, setting
@@ -208,15 +219,7 @@ cursor with updated info."
    (mc/save-window-scroll
     (mc/for-each-fake-cursor
      (save-excursion
-       (let ((mc--executing-command-for-fake-cursor t)
-             (id (overlay-get cursor 'mc-id))
-             (annoying-arrows-mode nil)
-             (smooth-scroll-margin 0))
-         (mc/add-fake-cursor-to-undo-list
-          (mc/pop-state-from-overlay cursor)
-          (ignore-errors
-            (mc/execute-command cmd)
-            (mc/create-fake-cursor-at-point id))))))))
+       (mc/execute-command-for-fake-cursor cmd cursor)))))
   (mc--reset-read-prompts))
 
 ;; Intercept some reading commands so you won't have to
@@ -561,6 +564,7 @@ for running commands with multiple cursors.")
                                      mc/mark-all-symbols-like-this-in-defun
                                      mc/mark-all-like-this-dwim
                                      mc/mark-sgml-tag-pair
+                                     mc/insert-numbers
                                      mc/cycle-forward
                                      mc/cycle-backward
                                      rrm/switch-to-multiple-cursors
diff --git a/multiple-cursors.el b/multiple-cursors.el
index 749b181..b618e3c 100644
--- a/multiple-cursors.el
+++ b/multiple-cursors.el
@@ -82,6 +82,7 @@
 
 ;;  - `set-rectangular-region-anchor`: Think of this one as `set-mark` except 
you're marking a rectangular region.
 ;;  - `mc/mark-sgml-tag-pair`: Mark the current opening and closing tag.
+;;  - `mc/insert-numbers`: Insert increasing numbers for each cursor, top to 
bottom.
 
 ;; ## Tips and tricks
 
@@ -99,6 +100,10 @@
 
 ;; - Notice that the number of cursors active can be seen in the modeline.
 
+;; - If you get out of multiple-cursors-mode and yank - it will yank only
+;; from the kill-ring of main cursor. To yank from the kill-rings of
+;; every cursor use yank-rectangle, normally found at C-x r y.
+
 ;; - If you would like to keep the global bindings clean, and get custom 
keybindings
 ;;   when the region is active, you can try 
[region-bindings-mode](https://github.com/fgallina/region-bindings-mode).
 
@@ -159,6 +164,7 @@
 (require 'mc-cycle-cursors)
 (require 'mc-mark-more)
 (require 'rectangular-region-mode)
+(require 'mc-insert-numbers)
 
 (provide 'multiple-cursors)
 



reply via email to

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