Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 171 lines (139 sloc) 6.335 kb
2bf67ff victorhge Merge changes in master and
victorhge authored
1 ;;; iedit-rect.el --- visible rectangle editing support based on Iedit.
2
3 ;; Copyright (C) 2010, 2011, 2012 Victor Ren
4
a089103 victorhge Fix typoes
victorhge authored
5 ;; Time-stamp: <2012-08-28 15:47:41 Victor Ren>
2bf67ff victorhge Merge changes in master and
victorhge authored
6 ;; Author: Victor Ren <victorhge@gmail.com>
7 ;; Keywords: occurrence region simultaneous rectangle refactoring
8 ;; Version: 0.97
9 ;; X-URL: http://www.emacswiki.org/emacs/Iedit
10 ;; Compatibility: GNU Emacs: 22.x, 23.x, 24.x
11
12 ;; This file is not part of GNU Emacs, but it is distributed under
13 ;; the same terms as GNU Emacs.
14
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Commentary:
29
30 ;; This package also provides rectangle support with *visible rectangle*
a089103 victorhge Fix typoes
victorhge authored
31 ;; highlighting, which is similar with cua-mode rectangle support. But it is
6f80f88 victorhge Differentiate lib keymap and occurrence keymap
victorhge authored
32 ;; lighter weight and uses iedit mechanisms.
2bf67ff victorhge Merge changes in master and
victorhge authored
33
34 ;; The code was developed and fully tested on Gnu Emacs 24.0.93, partially
35 ;; tested on Gnu Emacs 22. If you have any compatible problem, please let me
36 ;; know.
37
38 ;;; todo:
39 ;; - Add restrict function back
40
41 ;;; Code:
42
43 (eval-when-compile (require 'cl))
84896f8 victorhge *** empty log message ***
victorhge authored
44 (require 'rect) ;; kill-rectangle
2bf67ff victorhge Merge changes in master and
victorhge authored
45 (require 'iedit-lib)
46
47 (defvar iedit-rectangle-mode nil) ;; Name of the minor mode
48
49 (make-variable-buffer-local 'iedit-rectangle-mode)
50 (or (assq 'iedit-rectangle-mode minor-mode-alist)
51 (nconc minor-mode-alist
52 (list '(iedit-rectangle-mode iedit-rectangle-mode))))
53
54
55 ;;; Default key bindings:
56 (define-key global-map [C-return] 'iedit-rectangle-mode)
57
58 (defvar iedit-rectangle nil
59 "This buffer local variable which is the rectangle geometry if
60 current mode is iedit-rect. Otherwise it is nil.
61 \(car iedit-rectangle) is the top-left corner and
62 \(cadr iedit-rectangle) is the bottom-right corner" )
63
64 (make-variable-buffer-local 'iedit-rectangle)
65
66 ;;; Define Iedit rect mode map
67 (defvar iedit-rect-keymap
68 (let ((map (make-sparse-keymap)))
6f80f88 victorhge Differentiate lib keymap and occurrence keymap
victorhge authored
69 (set-keymap-parent map iedit-occurrence-keymap-default)
2bf67ff victorhge Merge changes in master and
victorhge authored
70 (define-key map (kbd "M-K") 'iedit-kill-rectangle)
71 map)
541eab7 victorhge Add document for iedit-rectangle-mode
victorhge authored
72 "Keymap used within overlays in Iedit-rect mode.")
2bf67ff victorhge Merge changes in master and
victorhge authored
73
74 (or (assq 'iedit-rectangle-mode minor-mode-map-alist)
75 (setq minor-mode-map-alist
6f80f88 victorhge Differentiate lib keymap and occurrence keymap
victorhge authored
76 (cons (cons 'iedit-rectangle-mode iedit-lib-keymap) minor-mode-map-alist)))
2bf67ff victorhge Merge changes in master and
victorhge authored
77
78
79 ;; Avoid to restore Iedit-rect mode when restoring desktop
80 (add-to-list 'desktop-minor-mode-handlers
81 '(iedit-rectangle-mode . nil))
82
83 ;;;###autoload
84 (defun iedit-rectangle-mode ()
541eab7 victorhge Add document for iedit-rectangle-mode
victorhge authored
85 "Toggle Iedit-rect mode.
86
87 When Iedit-rect mode is on, a rectangle is started with visible
a089103 victorhge Fix typoes
victorhge authored
88 rectangle highlighting. Rectangle editing support is based on
541eab7 victorhge Add document for iedit-rectangle-mode
victorhge authored
89 Iedit mechanism.
90
91 Commands:
92 \\{iedit-rect-keymap}"
2bf67ff victorhge Merge changes in master and
victorhge authored
93 (interactive)
94 (if iedit-rectangle-mode
95 (iedit-rectangle-done)
96 (iedit-barf-if-lib-active)
97 (if (iedit-region-active)
98 (let ((beg (region-beginning))
99 (end (region-end)))
100 (set-mark nil)
101 (iedit-rectangle-start beg end)))))
102
103 (defun iedit-rectangle-start (beg end)
104 "Start Iedit mode for the region as a rectangle."
105 (barf-if-buffer-read-only)
106 (setq iedit-occurrences-overlays nil)
107 (setq iedit-rectangle (list beg end))
108 (setq iedit-initial-string-local nil)
109 (save-excursion
110 (let ((beg-col (progn (goto-char beg) (current-column)))
111 (end-col (progn (goto-char end) (current-column))))
112 (when (< end-col beg-col)
113 (rotatef beg-col end-col))
114 (goto-char beg)
115 (loop do (progn
116 (push (iedit-make-occurrence-overlay
117 (progn
118 (move-to-column beg-col t)
119 (point))
120 (progn
121 (move-to-column end-col t)
6f80f88 victorhge Differentiate lib keymap and occurrence keymap
victorhge authored
122 (point))
123 iedit-rect-keymap)
2bf67ff victorhge Merge changes in master and
victorhge authored
124 iedit-occurrences-overlays)
125 (forward-line 1))
126 until (> (point) end))
127 (setq iedit-occurrences-overlays (nreverse iedit-occurrences-overlays))))
128 (setq iedit-rectangle-mode (propertize
541eab7 victorhge Add document for iedit-rectangle-mode
victorhge authored
129 (concat " Iedit-rect:" (number-to-string (length iedit-occurrences-overlays)))
2bf67ff victorhge Merge changes in master and
victorhge authored
130 'face 'font-lock-warning-face))
131 (force-mode-line-update)
132 (add-hook 'kbd-macro-termination-hook 'iedit-rectangle-done nil t)
133 (add-hook 'change-major-mode-hook 'iedit-rectangle-done nil t)
134 (add-hook 'iedit-aborting-hook 'iedit-rectangle-done nil t))
135
136 (defun iedit-rectangle-done ()
137 "Exit Iedit mode.
138 Save the current occurrence string locally and globally. Save
139 the initial string globally."
140 (when iedit-buffering
141 (iedit-stop-buffering))
1c410fc victorhge Fix iedit-number-occurrences with conjointed occurrences problem
victorhge authored
142 (iedit-cleanup)
2bf67ff victorhge Merge changes in master and
victorhge authored
143 (setq iedit-rectangle-mode nil)
144 (force-mode-line-update)
145 (remove-hook 'kbd-macro-termination-hook 'iedit-rectangle-done t)
146 (remove-hook 'change-major-mode-hook 'iedit-rectangle-done t)
147 (remove-hook 'iedit-aborting-hook 'iedit-rectangle-done t))
148
149 (defun iedit-kill-rectangle(&optional fill)
150 "Kill the rectangle.
151 The behavior is the same as `kill-rectangle' in rect mode."
152 (interactive "*P")
153 (or (and iedit-rectangle (iedit-same-column))
154 (error "Not a rectangle"))
155 (let ((inhibit-modification-hooks t)
156 (beg (overlay-start (car iedit-occurrences-overlays)))
157 (end (overlay-end (progn (iedit-last-occurrence)
158 (iedit-find-current-occurrence-overlay)))))
159 (kill-rectangle beg end fill)))
160
161 (provide 'iedit-rect)
162
163 ;;; iedit-rect.el ends here
164
165 ;; LocalWords: iedit el MERCHANTABILITY kbd isearch todo ert Lindberg Tassilo
166 ;; LocalWords: eval rect defgroup defcustom boolean defvar assq alist nconc
167 ;; LocalWords: substring cadr keymap defconst purecopy bkm defun princ prev
168 ;; LocalWords: iso lefttab backtab upcase downcase concat setq autoload arg
169 ;; LocalWords: refactoring propertize cond goto nreverse progn rotatef eq elp
170 ;; LocalWords: dolist pos unmatch args ov sReplace iedit's cdr quote'ed
Something went wrong with that request. Please try again.