Add mc/insert-numbers

- adds increasing numbers for each cursor, top to bottom
This commit is contained in:
Magnar Sveen 2013-01-23 12:05:18 +01:00
parent a6984a1141
commit 9582c7220b
6 changed files with 88 additions and 14 deletions

View File

@ -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).

View File

@ -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)"

View File

@ -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

47
mc-insert-numbers.el Normal file
View File

@ -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

View File

@ -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

View File

@ -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)