update mc
[emacs.git] / .emacs.d / elisp / multiple-cursors / rectangular-region-mode.el
1 ;;; rectangular-region-mode.el
2
3 ;; Copyright (C) 2012 Magnar Sveen
4
5 ;; Author: Magnar Sveen <magnars@gmail.com>
6 ;; Keywords: editing cursors
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; (global-set-key (kbd "H-SPC") 'set-rectangular-region-anchor)
24
25 ;; Think of this one as `set-mark` except you're marking a rectangular region. It is
26 ;; an exceedingly quick way of adding multiple cursors to multiple lines.
27
28 ;;; Code:
29
30 (require 'multiple-cursors-core)
31
32 (defvar rrm/anchor (make-marker)
33 "The position in the buffer that anchors the rectangular region.")
34
35 (defvar rectangular-region-mode-map (make-sparse-keymap)
36 "Keymap for rectangular region is mainly for rebinding C-g")
37
38 (define-key rectangular-region-mode-map (kbd "C-g") 'rrm/keyboard-quit)
39 (define-key rectangular-region-mode-map (kbd "<return>") 'rrm/switch-to-multiple-cursors)
40
41 (defun rrm/keyboard-quit ()
42 "Exit rectangular-region-mode."
43 (interactive)
44 (rectangular-region-mode 0)
45 (rrm/remove-rectangular-region-overlays)
46 (deactivate-mark))
47
48 ;; Bind this to a key (for instance H-SPC) to start rectangular-region-mode
49 ;;;###autoload
50 (defun set-rectangular-region-anchor ()
51 "Anchors the rectangular region at point.
52
53 Think of this one as `set-mark' except you're marking a rectangular region. It is
54 an exceedingly quick way of adding multiple cursors to multiple lines."
55 (interactive)
56 (set-marker rrm/anchor (point))
57 (push-mark (point))
58 (rectangular-region-mode 1))
59
60 (defun rrm/remove-rectangular-region-overlays ()
61 "Remove all rectangular-region overlays."
62 (mc/remove-fake-cursors)
63 (mapc #'(lambda (o)
64 (when (eq (overlay-get o 'type) 'additional-region)
65 (delete-overlay o)))
66 (overlays-in (point-min) (point-max))))
67
68 (defun rrm/repaint ()
69 "Start from the anchor and draw a rectangle between it and point."
70 (if (not rectangular-region-mode)
71 (remove-hook 'post-command-hook 'rrm/repaint t)
72 ;; else
73 (rrm/remove-rectangular-region-overlays)
74 (let* ((annoying-arrows-mode nil)
75 (point-column (current-column))
76 (point-line (line-number-at-pos))
77 (anchor-column (save-excursion (goto-char rrm/anchor) (current-column)))
78 (anchor-line (save-excursion (goto-char rrm/anchor) (line-number-at-pos)))
79 (left-column (if (< point-column anchor-column) point-column anchor-column))
80 (right-column (if (> point-column anchor-column) point-column anchor-column))
81 (navigation-step (if (< point-line anchor-line) 1 -1)))
82 (move-to-column anchor-column)
83 (set-mark (point))
84 (move-to-column point-column)
85 (mc/save-excursion
86 (while (not (= anchor-line (line-number-at-pos)))
87 (forward-line navigation-step)
88 (move-to-column anchor-column)
89 (when (= anchor-column (current-column))
90 (set-mark (point))
91 (move-to-column point-column)
92 (when (= point-column (current-column))
93 (mc/create-fake-cursor-at-point))))))))
94
95 (defun rrm/switch-to-multiple-cursors (&rest forms)
96 "Switch from rectangular-region-mode to multiple-cursors-mode."
97 (interactive)
98 (rectangular-region-mode 0)
99 (multiple-cursors-mode 1))
100
101 (defadvice er/expand-region (before switch-from-rrm-to-mc activate)
102 (when rectangular-region-mode
103 (rrm/switch-to-multiple-cursors)))
104
105 (defadvice kill-ring-save (before switch-from-rrm-to-mc activate)
106 (when rectangular-region-mode
107 (rrm/switch-to-multiple-cursors)))
108
109 (define-minor-mode rectangular-region-mode
110 "A mode for creating a rectangular region to edit"
111 nil " rr" rectangular-region-mode-map
112 (if rectangular-region-mode
113 (progn
114 (add-hook 'after-change-functions 'rrm/switch-to-multiple-cursors t t)
115 (add-hook 'post-command-hook 'rrm/repaint t t))
116 (remove-hook 'after-change-functions 'rrm/switch-to-multiple-cursors t)
117 (remove-hook 'post-command-hook 'rrm/repaint t)
118 (set-marker rrm/anchor nil)))
119
120 (provide 'rectangular-region-mode)
121
122 ;;; rectangular-region-mode.el ends here