Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 1136 lines (1007 sloc) 47.941 kB
8219304 @victorhge Fix problem when iedit-stop-buffering is called when current point is…
authored
1 ;;; iedit.el --- Edit multiple regions in one buffer simultaneously.
e9a9485 updates
anybody authored
2
02946e0 @victorhge Update contributors and spell check
authored
3 ;; Copyright (C) 2010, 2011, 2012 Victor Ren
e9a9485 updates
anybody authored
4
62d2a5a @victorhge Fix iedit-kill-rectangle problem
authored
5 ;; Time-stamp: <2012-03-04 11:39:18 Victor Ren>
e9a9485 updates
anybody authored
6 ;; Author: Victor Ren <victorhge@gmail.com>
7 ;; Keywords: occurrence region replace simultaneous
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
8 ;; Version: 0.95
799a0d1 @victorhge Bump to version 0.94
authored
9 ;; X-URL: http://www.emacswiki.org/emacs/Iedit
5ad28b5 updates
anybody authored
10 ;; Compatibility: GNU Emacs: 22.x, 23.x, 24.x
e9a9485 updates
anybody authored
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
16ed986 @victorhge Update document
authored
30 ;; This package is an Emacs minor mode and allows you to edit one occurrence of
31 ;; some text in a buffer or region, and simultaneously have other occurrences
32 ;; edited in the same way.
33 ;;
d2a6e16 @victorhge Update commentary with rectangle support
authored
34 ;; Normal scenario of iedit-mode is like:
e9a9485 updates
anybody authored
35 ;;
16ed986 @victorhge Update document
authored
36 ;; - Highlight certain contents - by press C-;
37 ;; All occurrences of a symbol, string or a rectangle in the buffer or a
38 ;; region may be highlighted corresponding to current mark, point and prefix
3ead6c6 @victorhge run checkdoc
authored
39 ;; argument. Refer to the document of `iedit-mode' for details.
16ed986 @victorhge Update document
authored
40 ;;
41 ;; - Edit one of the occurrences
42 ;; The change is applied to other occurrences simultaneously.
e9a9485 updates
anybody authored
43 ;;
44 ;; - Finish - by pressing C-; again
16ed986 @victorhge Update document
authored
45 ;;
46 ;; This package also provides rectangle support with *visible rectangle*
47 ;; highlighting, which is similar with cua mode rectangle support.
e9a9485 updates
anybody authored
48
16ed986 @victorhge Update document
authored
49 ;; You can also use Iedit mode as a quick way to temporarily show only the
50 ;; buffer lines that match the current text being edited. This gives you the
51 ;; effect of a temporary 'keep-lines' or 'occur'. To get this effect, hit `C-'
52 ;; when in Iedit mode - it toggles hiding non-matching lines.
e9a9485 updates
anybody authored
53
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
54 ;; Renaming refactoring is convenient in iedit mode
62d2a5a @victorhge Fix iedit-kill-rectangle problem
authored
55 ;;
56 ;; - The symbol under point is selected as occurrence by default and only
57 ;; complete symbols are matched
16ed986 @victorhge Update document
authored
58 ;; - With digit prefix argument 0, only symbols in current function are matched
59 ;; - Restricting symbols in current region can be done by pressing C-; again
60 ;; - Last renaming refactoring is remembered and can be applied to other buffers
62d2a5a @victorhge Fix iedit-kill-rectangle problem
authored
61 ;; - later
16ed986 @victorhge Update document
authored
62
63 ;; There are also some other facilities you may never think about. Refer to the
64 ;; document of function `iedit-mode' (C-h f iedit-mode RET) for more details.
e9a9485 updates
anybody authored
65
a9bda4d @victorhge Refine hotkeys
authored
66 ;;; Default key bindings:
e9a9485 updates
anybody authored
67 ;;
68 ;; (define-key global-map (kbd "C-;") 'iedit-mode)
69 ;; (define-key isearch-mode-map (kbd "C-;") 'iedit-mode)
a9bda4d @victorhge Refine hotkeys
authored
70 ;; (define-key esc-map (kbd "C-;") 'iedit-execute-last-modification)
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
71 ;; (define-key help-map (kbd "C-;") 'iedit-mode-function)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
72 ;; (define-key global-map [C-return] 'iedit-rectangle-mode)
e9a9485 updates
anybody authored
73
74 ;;; todo:
9b3aaa8 @victorhge Fix occurrence from history does not exist problem
authored
75 ;; - Add more easy access keys for whole occurrence
e9a9485 updates
anybody authored
76
ec6785b @victorhge host on git
authored
77 ;;; Contributors
78 ;; Adam Lindberg <eproxus@gmail.com> added a case sensitivity option that can be toggled.
02946e0 @victorhge Update contributors and spell check
authored
79
80 ;; Tassilo Horn <tassilo@member.fsf.org> added an option to match only complete
81 ;; words, not inside words
82
3bca7e2 @victorhge Fix iedit toggle case-sensitive problem when getting occurrence from …
authored
83 ;; Le Wang <l26wang@gmail.com> proposed to match only complete symbols, not
e99e65f @victorhge Fix current point move problem when call iedit-replace-occurrences.
authored
84 ;; inside symbols, contributed rectangle support
ec6785b @victorhge host on git
authored
85
e9a9485 updates
anybody authored
86 ;;; Code:
87
88 (eval-when-compile (require 'cl))
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
89 (require 'rect) ;; kill rectangle
e9a9485 updates
anybody authored
90
91 (defgroup iedit nil
92 "Edit multiple regions with the same content simultaneously."
93 :prefix "iedit-"
94 :group 'replace
95 :group 'convenience)
96
97 (defcustom iedit-occurrence-face 'highlight
98 "*Face used for the occurrences' default values."
99 :type 'face
100 :group 'iedit)
101
fc71952 @victorhge Boolean `t'
authored
102 (defcustom iedit-current-symbol-default t
f808bf1 @victorhge Merge from lewang/master and update comments
authored
103 "If no-nil, use current symbol by default for the occurrence."
e9a9485 updates
anybody authored
104 :type 'boolean
105 :group 'iedit)
106
fc71952 @victorhge Boolean `t'
authored
107 (defcustom iedit-case-sensitive-default t
3ead6c6 @victorhge run checkdoc
authored
108 "If no-nil, matching is case sensitive."
5ad28b5 updates
anybody authored
109 :type 'boolean
110 :group 'iedit)
111
f808bf1 @victorhge Merge from lewang/master and update comments
authored
112 (defcustom iedit-only-at-symbol-boundaries t
113 "If no-nil, matches have to start and end at symbol boundaries.
3ead6c6 @victorhge run checkdoc
authored
114 For example, when invoking `iedit-mode' on the \"in\" in the
da0a247 @tsdh Add option to match only complete words, not inside words
tsdh authored
115 sentence \"The king in the castle...\", the \"king\" is not
116 edited."
117 :type 'boolean
118 :group 'iedit)
119
e9a9485 updates
anybody authored
120 (defcustom iedit-unmatched-lines-invisible-default nil
3ead6c6 @victorhge run checkdoc
authored
121 "If no-nil, hide lines that do not cover any occurrences by default."
122
e9a9485 updates
anybody authored
123 :type 'boolean
124 :group 'iedit)
125
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
126 (defcustom iedit-transtient-mark-sensitive t
127 "If no-nil, `iedit-mode' is sensitive to the Transient Mark mode."
128 :type 'boolean
129 :group 'iedit)
130
e9a9485 updates
anybody authored
131 (defvar iedit-mode-hook nil
132 "Function(s) to call after starting up an iedit.")
133
134 (defvar iedit-mode-end-hook nil
135 "Function(s) to call after terminating an iedit.")
136
137 (defvar iedit-mode nil) ;; Name of the minor mode
138
139 (make-variable-buffer-local 'iedit-mode)
140
141 (or (assq 'iedit-mode minor-mode-alist)
142 (nconc minor-mode-alist
9bc57d2 @victorhge make iedit-skipped-modification-once buffer local
authored
143 (list '(iedit-mode iedit-mode))))
e9a9485 updates
anybody authored
144
a9bda4d @victorhge Refine hotkeys
authored
145 (define-key global-map (kbd "C-;") 'iedit-mode)
146 (define-key isearch-mode-map (kbd "C-;") 'iedit-mode)
147 (define-key esc-map (kbd "C-;") 'iedit-execute-last-modification)
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
148 (define-key help-map (kbd "C-;") 'iedit-mode-function)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
149 (define-key global-map [C-return] 'iedit-rectangle-mode)
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
150
a9bda4d @victorhge Refine hotkeys
authored
151 (defvar iedit-last-initial-string-global nil
152 "This is a global variable which is the last initial occurrence string.")
153
154 (defvar iedit-initial-string-local nil
3ead6c6 @victorhge run checkdoc
authored
155 "This is buffer local variable which is the initial string to start iedit mode.")
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
156
e9a9485 updates
anybody authored
157 (defvar iedit-occurrences-overlays nil
9a891d7 updates
anybody authored
158 "The occurrences slot contains a list of overlays used to
159 indicate the position of each occurrence. In addition, the
160 occurrence overlay is used to provide a different face
161 configurable via `iedit-occurrence-face'.")
e9a9485 updates
anybody authored
162
a9bda4d @victorhge Refine hotkeys
authored
163 (defvar iedit-case-sensitive-local iedit-case-sensitive-default
5ad28b5 updates
anybody authored
164 "This is buffer local variable. If no-nil, matching is case
3ead6c6 @victorhge run checkdoc
authored
165 sensitive.")
5ad28b5 updates
anybody authored
166
a9bda4d @victorhge Refine hotkeys
authored
167 (defvar iedit-case-sensitive-global iedit-case-sensitive-default
3ead6c6 @victorhge run checkdoc
authored
168 "This is global variable.
169 If no-nil, matching is case sensitive.")
a9bda4d @victorhge Refine hotkeys
authored
170
e9a9485 updates
anybody authored
171 (defvar iedit-unmatched-lines-invisible nil
9a891d7 updates
anybody authored
172 "This is buffer local variable which indicates whether
173 unmatched lines are hided.")
e9a9485 updates
anybody authored
174
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
175 (defvar iedit-last-occurrence-local nil
e9a9485 updates
anybody authored
176 "This is buffer local variable which is the occurrence when
9a891d7 updates
anybody authored
177 iedit mode is turned off last time.")
178
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
179 (defvar iedit-last-occurrence-global nil
180 "This is global variable which is the occurrence when
181 iedit mode is turned off last time.")
182
183 (defvar iedit-only-complete-symbol-local nil
0edc86f @victorhge Fix the last occurrence problem when it is a complete symbol.
authored
184 "This is buffer local variable which indicates the occurrence
185 only matches complete symbol.")
186
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
187 (defvar iedit-only-complete-symbol-global nil
188 "This is global variable which indicates the last global occurrence
189 only matches complete symbol.")
190
9a891d7 updates
anybody authored
191 (defvar iedit-forward-success t
039c0e2 @victorhge Postpone iedit-done after commands are excuted.
authored
192 "This is buffer local variable which indicates the moving
9a891d7 updates
anybody authored
193 forward or backward successful")
e9a9485 updates
anybody authored
194
c8dac6b @victorhge exit iedit mode when change is not inside of occurrence.
authored
195 (defvar iedit-before-modification-string ""
196 "This is buffer local variable which is the buffer substring that is going to be changed.")
d4b9fd5 @victorhge don't call hooks when there is only text properites change
authored
197
c53a885 @victorhge Add bufferring modification functionalities
authored
198 (defvar iedit-before-modification-undo-list nil
199 "This is buffer local variable which is the buffer undo list before modification.")
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
200
039c0e2 @victorhge Postpone iedit-done after commands are excuted.
authored
201 ;; `iedit-occurrence-update' gets called twice when change==0 and occurrence
a75c446 fix bug with autopair and iedit-rect
Le Wang authored
202 ;; is zero-width (beg==end)
039c0e2 @victorhge Postpone iedit-done after commands are excuted.
authored
203 ;; -- for front and back insertion.
a75c446 fix bug with autopair and iedit-rect
Le Wang authored
204 (defvar iedit-skipped-modification-once nil
3097129 @victorhge Remove a limitation of empty occurrence string
authored
205 "Variable used to skip first modification hook run when
3ead6c6 @victorhge run checkdoc
authored
206 insertion against a zero-width occurrence.")
039c0e2 @victorhge Postpone iedit-done after commands are excuted.
authored
207
208 (defvar iedit-aborting nil
3ead6c6 @victorhge run checkdoc
authored
209 "This is buffer local variable which indicates iedit mode is aborting.")
039c0e2 @victorhge Postpone iedit-done after commands are excuted.
authored
210
c53a885 @victorhge Add bufferring modification functionalities
authored
211 (defvar iedit-buffering nil
212 "This is buffer local variable which indicates iedit-mode is
3097129 @victorhge Remove a limitation of empty occurrence string
authored
213 buffering, which means the modification to the current occurrence
214 is not applied to other occurrences when it is true.")
c53a885 @victorhge Add bufferring modification functionalities
authored
215
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
216 (defvar iedit-rectangle nil
87053a3 @victorhge Remove two buffer local variables for rectangle.
authored
217 "This buffer local variable which is the rectangle geometry if
3ead6c6 @victorhge run checkdoc
authored
218 current mode is iedit-rect. Otherwise it is nil.
219 \(car iedit-rectangle) is the top-left corner and
220 \(cadr iedit-rectangle) is the bottom-right corner" )
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
221
a9bda4d @victorhge Refine hotkeys
authored
222 (defvar iedit-current-keymap nil
223 "The current keymap, `iedit-occurrence-keymap' or `iedit-rect-keymap'.")
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
224
00abe88 @victorhge Hide line-feed between occurrence
authored
225 (defvar iedit-occurrence-context-lines 1
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
226 "The number of lines before or after the occurrence.")
227
e9a9485 updates
anybody authored
228 (make-variable-buffer-local 'iedit-occurrences-overlays)
229 (make-variable-buffer-local 'iedit-unmatched-lines-invisible)
a9bda4d @victorhge Refine hotkeys
authored
230 (make-variable-buffer-local 'iedit-case-sensitive-local)
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
231 (make-variable-buffer-local 'iedit-last-occurrence-local)
232 (make-variable-buffer-local 'iedit-only-complete-symbol-local)
9a891d7 updates
anybody authored
233 (make-variable-buffer-local 'iedit-forward-success)
c8dac6b @victorhge exit iedit mode when change is not inside of occurrence.
authored
234 (make-variable-buffer-local 'iedit-before-modification-string)
c53a885 @victorhge Add bufferring modification functionalities
authored
235 (make-variable-buffer-local 'iedit-before-modification-undo-list)
9bc57d2 @victorhge make iedit-skipped-modification-once buffer local
authored
236 (make-variable-buffer-local 'iedit-skipped-modification-once)
039c0e2 @victorhge Postpone iedit-done after commands are excuted.
authored
237 (make-variable-buffer-local 'iedit-aborting)
c53a885 @victorhge Add bufferring modification functionalities
authored
238 (make-variable-buffer-local 'iedit-buffering)
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
239 (make-variable-buffer-local 'iedit-rectangle)
240 (make-variable-buffer-local 'iedit-current-keymap)
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
241 (make-variable-buffer-local 'iedit-occurrence-context-lines)
e9a9485 updates
anybody authored
242
243 (defconst iedit-occurrence-overlay-name 'iedit-occurrence-overlay-name)
244 (defconst iedit-invisible-overlay-name 'iedit-invisible-overlay-name)
5ad28b5 updates
anybody authored
245
246 ;;; Define iedit help map.
247 (eval-when-compile (require 'help-macro))
248
249 (defvar iedit-help-map
250 (let ((map (make-sparse-keymap)))
251 (define-key map (char-to-string help-char) 'iedit-help-for-help)
252 (define-key map [help] 'iedit-help-for-help)
253 (define-key map [f1] 'iedit-help-for-help)
254 (define-key map "?" 'iedit-help-for-help)
255 (define-key map "b" 'iedit-describe-bindings)
256 (define-key map "k" 'iedit-describe-key)
257 (define-key map "m" 'iedit-describe-mode)
258 (define-key map "q" 'help-quit)
259 map)
260 "Keymap for characters following the Help key for iedit mode.")
261
9bc57d2 @victorhge make iedit-skipped-modification-once buffer local
authored
262 (make-help-screen
263 iedit-help-for-help-internal
264 (purecopy "Type a help option: [bkm] or ?")
265 "You have typed %THIS-KEY%, the help character. Type a Help option:
5ad28b5 updates
anybody authored
266 \(Type \\<help-map>\\[help-quit] to exit the Help command.)
267
268 b Display all Iedit key bindings.
269 k KEYS Display full documentation of Iedit key sequence.
270 m Display documentation of Iedit mode.
271
272 You can't type here other help keys available in the global help map,
273 but outside of this help window when you type them in Iedit mode,
274 they exit Iedit mode before displaying global help."
9bc57d2 @victorhge make iedit-skipped-modification-once buffer local
authored
275 iedit-help-map)
5ad28b5 updates
anybody authored
276
277 (defun iedit-help-for-help ()
278 "Display Iedit help menu."
279 (interactive)
280 (let (same-window-buffer-names same-window-regexps)
281 (iedit-help-for-help-internal)))
282
283 (defun iedit-describe-bindings ()
284 "Show a list of all keys defined in Iedit mode, and their definitions.
285 This is like `describe-bindings', but displays only Iedit keys."
286 (interactive)
a9bda4d @victorhge Refine hotkeys
authored
287 (let (same-window-buffer-names
288 same-window-regexps
289 (keymap (substitute-command-keys "\\{iedit-current-keymap}")))
5ad28b5 updates
anybody authored
290 (with-help-window "*Help*"
291 (with-current-buffer standard-output
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
292 (princ "Iedit Mode Bindings: ")
a9bda4d @victorhge Refine hotkeys
authored
293 (princ keymap)))))
5ad28b5 updates
anybody authored
294
295 (defun iedit-describe-key ()
00abe88 @victorhge Hide line-feed between occurrence
authored
296 "Display documentation of the function invoked by iedit mode key."
5ad28b5 updates
anybody authored
297 (interactive)
298 (let (same-window-buffer-names same-window-regexps)
299 (call-interactively 'describe-key)))
300
301 (defun iedit-describe-mode ()
302 "Display documentation of iedit mode."
303 (interactive)
304 (let (same-window-buffer-names same-window-regexps)
305 (describe-function 'iedit-mode)))
306
307 ;;; Define iedit mode map
1d070b7 move init code for iedit-mode-map inside defvar
Le Wang authored
308 (defvar iedit-mode-map
652087d @victorhge fix warning: the function `make-sparse-key-map' is not known to be
authored
309 (let ((map (make-sparse-keymap)))
1d070b7 move init code for iedit-mode-map inside defvar
Le Wang authored
310 ;; Default key bindings
652087d @victorhge fix warning: the function `make-sparse-key-map' is not known to be
authored
311 (define-key map (kbd "TAB") 'iedit-next-occurrence)
312 (define-key map (kbd "<S-tab>") 'iedit-prev-occurrence)
313 (define-key map (kbd "<S-iso-lefttab>") 'iedit-prev-occurrence)
314 (define-key map (kbd "<backtab>") 'iedit-prev-occurrence)
315 (define-key map (kbd "C-'") 'iedit-toggle-unmatched-lines-visible)
316 (define-key map (char-to-string help-char) iedit-help-map)
317 (define-key map [help] iedit-help-map)
318 (define-key map [f1] iedit-help-map)
1d070b7 move init code for iedit-mode-map inside defvar
Le Wang authored
319 map)
e9a9485 updates
anybody authored
320 "Keymap used while iedit mode is enabled.")
321
a9bda4d @victorhge Refine hotkeys
authored
322 (defvar iedit-occurrence-keymap
da47784 @victorhge Add iedit mode easy access keys
authored
323 (let ((map (make-sparse-keymap)))
324 (set-keymap-parent map iedit-mode-map)
a9bda4d @victorhge Refine hotkeys
authored
325 (define-key map (kbd "M-U") 'iedit-upcase-occurrences)
326 (define-key map (kbd "M-L") 'iedit-downcase-occurrences)
327 (define-key map (kbd "M-R") 'iedit-replace-occurrences)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
328 (define-key map (kbd "M-SPC") 'iedit-blank-occurrences)
a9bda4d @victorhge Refine hotkeys
authored
329 (define-key map (kbd "M-C") 'iedit-toggle-case-sensitive)
c53a885 @victorhge Add bufferring modification functionalities
authored
330 (define-key map (kbd "M-D") 'iedit-delete-occurrences)
78f606d @victorhge Add iedit-number-occurrences function
authored
331 (define-key map (kbd "M-N") 'iedit-number-occurrences)
a9bda4d @victorhge Refine hotkeys
authored
332 (define-key map (kbd "M-;") 'iedit-apply-global-modification)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
333 (define-key map (kbd "M-B") 'iedit-toggle-buffering)
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
334 (define-key map (kbd "M-<") 'iedit-first-occurrence)
335 (define-key map (kbd "M->") 'iedit-last-occurrence)
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
336 (define-key map (kbd "M-H") 'iedit-restrict-function)
7d18e5d @victorhge Add a help command C-? for occurrences
authored
337 (define-key map (kbd "C-?") 'iedit-help-for-occurrences)
da47784 @victorhge Add iedit mode easy access keys
authored
338 map)
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
339 "Keymap used within overlays in iedit mode.")
340
a9bda4d @victorhge Refine hotkeys
authored
341 (defvar iedit-rect-keymap
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
342 (let ((map (make-sparse-keymap)))
a9bda4d @victorhge Refine hotkeys
authored
343 (set-keymap-parent map iedit-occurrence-keymap)
344 (define-key map (kbd "M-K") 'iedit-kill-rectangle)
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
345 map)
346 "Keymap used within overlays in iedit-RECT mode.")
347
7d18e5d @victorhge Add a help command C-? for occurrences
authored
348 (defun iedit-help-for-occurrences ()
a9bda4d @victorhge Refine hotkeys
authored
349 "Display `iedit-occurrence-keymap' or `iedit-rect-keymap'."
7d18e5d @victorhge Add a help command C-? for occurrences
authored
350 (interactive)
a9bda4d @victorhge Refine hotkeys
authored
351 (message (concat (substitute-command-keys "\\[iedit-upcase-occurrences]") "/"
352 (substitute-command-keys "\\[iedit-downcase-occurrences]") ":up/downcase "
353 (substitute-command-keys "\\[iedit-replace-occurrences]") ":replace "
354 (substitute-command-keys "\\[iedit-blank-occurrences]") ":blank "
355 (substitute-command-keys "\\[iedit-delete-occurrences]") ":delete "
356 (substitute-command-keys "\\[iedit-number-occurrences]") ":number "
357 (substitute-command-keys "\\[iedit-toggle-case-sensitive]") ":case "
358 (substitute-command-keys "\\[iedit-apply-global-modification]") ":redo "
359 (substitute-command-keys "\\[iedit-toggle-buffering]") ":buffering "
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
360 (substitute-command-keys "\\[iedit-first-occurrence]") "/"
361 (substitute-command-keys "\\[iedit-last-occurrence]") ":first/last "
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
362 (substitute-command-keys "\\[iedit-restrict-function]") ":restrict "
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
363 (if iedit-rectangle
3097129 @victorhge Remove a limitation of empty occurrence string
authored
364 (concat
365 (substitute-command-keys "\\[iedit-kill-rectangle]") ":kill")))))
7d18e5d @victorhge Add a help command C-? for occurrences
authored
366
e9a9485 updates
anybody authored
367 (or (assq 'iedit-mode minor-mode-map-alist)
368 (setq minor-mode-map-alist
5ad28b5 updates
anybody authored
369 (cons (cons 'iedit-mode iedit-mode-map) minor-mode-map-alist)))
e9a9485 updates
anybody authored
370
371 ;;;###autoload
372 (defun iedit-mode (&optional arg)
373 "Toggle iedit mode.
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
374 This command behaves differently, depending on the mark, point,
375 prefix argument and variable `iedit-transtient-mark-sensitive'.
3097129 @victorhge Remove a limitation of empty occurrence string
authored
376
d872cb7 @victorhge Update comments for restricting
authored
377 If iedit mode is off, turn iedit mode on.
e9a9485 updates
anybody authored
378
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
379 When iedit mode is turned on, all the occurrences of the current
380 region are highlighted. If one occurrence is modified, the
381 change are propagated to all other occurrences simultaneously.
382
383 If region is not active, the current symbol (returns from
384 `current-word') is used as the occurrence by default. The
385 occurrences of the current symbol, but not include occurrences
386 that are part of other symbols, are highlighted. With digit
387 prefix argument 0, only symbols in current function are matched.
388 This is good for renaming refactoring in programming. If you
389 still want to match all the occurrences, even though they are
390 parts of other symbols, you may have to mark the symbol first.
e9a9485 updates
anybody authored
391
392 You can also switch to iedit mode from isearch mode directly. The
393 current search string is used as occurrence. All occurrences of
394 the current search string are highlighted.
395
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
396 With an universal prefix argument, the occurrence when iedit is
397 turned off last time in current buffer is used as occurrence.
398 This is intended to recover last iedit which is turned off by
399 mistake. If region active, `iedit-mode' is limited within the
400 current region.
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
401
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
402 With repeated universal prefix argument, the occurrence when
403 iedit is turned off last time (might be in other buffer) is used
404 as occurrence. If region active, `iedit-mode' is limited within
405 the current region.
e9a9485 updates
anybody authored
406
d872cb7 @victorhge Update comments for restricting
authored
407 If iedit mode is on and region is active, iedit mode is
408 restricted in the region, e.g. the occurrences outside of the region
409 is excluded.
410
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
411 If iedit mode is on and region is active, with an universal
d872cb7 @victorhge Update comments for restricting
authored
412 prefix argument, iedit mode is restricted outside of the region,
413 e.g. the occurrences in the region is excluded.
414
415 Turn off iedit mode in other situations.
416
e9a9485 updates
anybody authored
417 Commands:
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
418 \\{iedit-current-keymap}"
e08ad59 @victorhge Make Iedit mode available for read only buffer
authored
419 (interactive "P")
e9a9485 updates
anybody authored
420 (if iedit-mode
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
421 (iedit-mode-on-action arg)
422 (let (occurrence
423 complete-symbol
424 (beg (point-min))
425 (end (point-max)))
426 (when (and arg (iedit-region-active))
427 (setq beg (region-beginning))
428 (setq end (region-end)))
ec765ee implement interactive string-rectangle mode with universal argument a…
Le Wang authored
429 (cond ((and arg
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
430 (= 4 (prefix-numeric-value arg))
431 iedit-last-occurrence-local)
432 (setq occurrence iedit-last-occurrence-local)
433 (setq complete-symbol iedit-only-complete-symbol-local))
434 ((and arg
435 (= 16 (prefix-numeric-value arg))
436 iedit-last-initial-string-global)
437 (setq occurrence iedit-last-initial-string-global)
438 (setq complete-symbol iedit-only-complete-symbol-global))
ec765ee implement interactive string-rectangle mode with universal argument a…
Le Wang authored
439 ((and arg
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
440 (= 0 (prefix-numeric-value arg))
441 iedit-current-symbol-default
442 (current-word t))
443 (setq occurrence (current-word))
444 (when iedit-only-at-symbol-boundaries
445 (setq complete-symbol t))
446 (save-excursion
447 (mark-defun)
448 (setq beg (region-beginning))
449 (setq end (region-end))))
450 ((iedit-region-active)
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
451 (setq occurrence (buffer-substring-no-properties
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
452 (mark) (point))))
e9a9485 updates
anybody authored
453 ((and isearch-mode (not (string= isearch-string "")))
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
454 (setq occurrence (buffer-substring-no-properties
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
455 (point) isearch-other-end))
e9a9485 updates
anybody authored
456 (isearch-exit))
f808bf1 @victorhge Merge from lewang/master and update comments
authored
457 ((and iedit-current-symbol-default (current-word t))
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
458 (setq occurrence (current-word))
f808bf1 @victorhge Merge from lewang/master and update comments
authored
459 (when iedit-only-at-symbol-boundaries
3bca7e2 @victorhge Fix iedit toggle case-sensitive problem when getting occurrence from …
authored
460 (setq complete-symbol t)))
3ead6c6 @victorhge run checkdoc
authored
461 (t (error "No candidate of the occurrence, cannot enable iedit mode")))
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
462 (setq iedit-only-complete-symbol-local complete-symbol)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
463 (deactivate-mark t)
464 (setq iedit-case-sensitive-local iedit-case-sensitive-default)
465 (iedit-start occurrence beg end))))
466
467 ;;;###autoload
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
468 (defun iedit-mode-function ()
469 "Toggle `iedit-mode' on currenct function."
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
470 (interactive)
471 (iedit-mode 0))
472
473 ;;;###autoload
474 (defun iedit-rectangle-mode ()
475 "Toggle iedit-RECT mode."
476 (interactive)
477 (if iedit-mode
478 (iedit-mode-on-action)
479 (if (iedit-region-active)
480 (let ((beg (region-beginning))
481 (end (region-end)))
482 (deactivate-mark t)
483 (iedit-rectangle-start beg end)))))
484
485 (defun iedit-mode-on-action (&optional arg)
486 "Turn off iedit-mode or restrict it in a region."
487 (if (iedit-region-active)
488 ;; Restrict iedit-mode
489 (let ((beg (region-beginning))
490 (end (region-end)))
491 (if (null (iedit-find-overlay beg end 'iedit-occurrence-overlay-name arg))
492 (iedit-done)
493 (iedit-restrict-region beg end arg)
494 (iedit-first-occurrence)))
495 (iedit-done)))
e9a9485 updates
anybody authored
496
90ef2d5 @victorhge Add digit prefix argment 0 to enable restricting match in a function
authored
497 (defun iedit-start (occurrence-exp beg end)
3ead6c6 @victorhge run checkdoc
authored
498 "Start an iedit for the OCCURRENCE-EXP in the current buffer."
e9a9485 updates
anybody authored
499 (setq iedit-unmatched-lines-invisible iedit-unmatched-lines-invisible-default)
9b3aaa8 @victorhge Fix occurrence from history does not exist problem
authored
500 (setq iedit-aborting nil)
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
501 (setq iedit-rectangle nil)
a9bda4d @victorhge Refine hotkeys
authored
502 (setq iedit-current-keymap iedit-occurrence-keymap)
90ef2d5 @victorhge Add digit prefix argment 0 to enable restricting match in a function
authored
503 (iedit-refresh occurrence-exp beg end)
84c02ea @victorhge Fix toggle case-sensitive problem when occurrence is empty string.
authored
504 (run-hooks 'iedit-mode-hook)
505 (add-hook 'kbd-macro-termination-hook 'iedit-done))
506
e568677 @victorhge Add new functionality: restrict iedit-mode in region
authored
507 (defun iedit-refresh (occurrence-exp beg end)
84c02ea @victorhge Fix toggle case-sensitive problem when occurrence is empty string.
authored
508 "Refresh iedit-mode."
509 (setq iedit-occurrences-overlays nil)
a9bda4d @victorhge Refine hotkeys
authored
510 (setq iedit-initial-string-local occurrence-exp)
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
511 (setq occurrence-exp (regexp-quote occurrence-exp))
512 (when iedit-only-complete-symbol-local
3bca7e2 @victorhge Fix iedit toggle case-sensitive problem when getting occurrence from …
authored
513 (setq occurrence-exp (concat "\\_<" occurrence-exp "\\_>")))
e9a9485 updates
anybody authored
514 ;; Find and record each occurrence's markers and add the overlay to the occurrences
5ad28b5 updates
anybody authored
515 (let ((counter 0)
a9bda4d @victorhge Refine hotkeys
authored
516 (case-fold-search (not iedit-case-sensitive-local)))
f808bf1 @victorhge Merge from lewang/master and update comments
authored
517 (save-excursion
e568677 @victorhge Add new functionality: restrict iedit-mode in region
authored
518 (goto-char beg)
519 (while (re-search-forward occurrence-exp end t)
f808bf1 @victorhge Merge from lewang/master and update comments
authored
520 (push (iedit-make-occurrence-overlay (match-beginning 0) (match-end 0))
521 iedit-occurrences-overlays)
9b3aaa8 @victorhge Fix occurrence from history does not exist problem
authored
522 (setq counter (1+ counter)))
523 (if (= 0 counter)
84c02ea @victorhge Fix toggle case-sensitive problem when occurrence is empty string.
authored
524 (error "0 matches for \"%s\"" (iedit-printable occurrence-exp))
9b3aaa8 @victorhge Fix occurrence from history does not exist problem
authored
525 (setq iedit-occurrences-overlays (nreverse iedit-occurrences-overlays))
526 (if iedit-unmatched-lines-invisible
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
527 (iedit-hide-unmatched-lines iedit-occurrence-context-lines)))
84c02ea @victorhge Fix toggle case-sensitive problem when occurrence is empty string.
authored
528 (message "%d matches for \"%s\"" counter (iedit-printable occurrence-exp))
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
529 (setq iedit-mode (propertize (concat " Iedit:" (number-to-string counter))
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
530 'face 'font-lock-warning-face))
531 (force-mode-line-update))))
84c02ea @victorhge Fix toggle case-sensitive problem when occurrence is empty string.
authored
532
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
533 (defun iedit-rectangle-start (beg end)
5ab9809 @victorhge Update documents
authored
534 "Start an iedit for the region as a rectangle."
e08ad59 @victorhge Make Iedit mode available for read only buffer
authored
535 (barf-if-buffer-read-only)
ec765ee implement interactive string-rectangle mode with universal argument a…
Le Wang authored
536 (setq iedit-occurrences-overlays nil)
87053a3 @victorhge Remove two buffer local variables for rectangle.
authored
537 (setq iedit-rectangle (list beg end))
a9bda4d @victorhge Refine hotkeys
authored
538 (setq iedit-initial-string-local nil)
539 (setq iedit-current-keymap iedit-rect-keymap)
c53a885 @victorhge Add bufferring modification functionalities
authored
540 (save-excursion
541 (let ((beg-col (progn (goto-char beg) (current-column)))
542 (end-col (progn (goto-char end) (current-column))))
543 (when (< end-col beg-col)
544 (rotatef beg-col end-col))
545 (goto-char beg)
546 (loop do (progn
547 (push (iedit-make-occurrence-overlay
548 (progn
549 (move-to-column beg-col t)
550 (point))
551 (progn
552 (move-to-column end-col t)
553 (point)))
554 iedit-occurrences-overlays)
555 (forward-line 1))
556 until (> (point) end))
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
557 (setq iedit-occurrences-overlays (nreverse iedit-occurrences-overlays))))
558 (setq iedit-mode (propertize
559 (concat " Iedit-RECT:" (number-to-string (length iedit-occurrences-overlays)))
560 'face 'font-lock-warning-face))
561 (force-mode-line-update)
562 (run-hooks 'iedit-mode-hook)
563 (add-hook 'kbd-macro-termination-hook 'iedit-done))
ec765ee implement interactive string-rectangle mode with universal argument a…
Le Wang authored
564
e9a9485 updates
anybody authored
565 (defun iedit-done ()
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
566 "Exit iedit mode.
567 Save the current occurrence string locally and globally. Save
568 the initial string globally."
c53a885 @victorhge Add bufferring modification functionalities
authored
569 (if iedit-buffering
570 (iedit-stop-buffering))
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
571 (when (null iedit-rectangle)
572 (setq iedit-last-occurrence-local (iedit-current-occurrence-string))
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
573 (setq iedit-last-occurrence-global iedit-last-occurrence-local)
574 (setq iedit-only-complete-symbol-global iedit-only-complete-symbol-local)
575 (setq iedit-last-initial-string-global iedit-initial-string-local)
576 (setq iedit-case-sensitive-global iedit-case-sensitive-local))
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
577 (remove-overlays nil nil iedit-occurrence-overlay-name t)
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
578 (iedit-show-all)
e9a9485 updates
anybody authored
579 (setq iedit-occurrences-overlays nil)
c53a885 @victorhge Add bufferring modification functionalities
authored
580 (setq iedit-aborting nil)
581 (setq iedit-before-modification-string "")
582 (setq iedit-before-modification-undo-list nil)
e9a9485 updates
anybody authored
583 (setq iedit-mode nil)
584 (force-mode-line-update)
84c02ea @victorhge Fix toggle case-sensitive problem when occurrence is empty string.
authored
585 (remove-hook 'kbd-macro-termination-hook 'iedit-done)
e9a9485 updates
anybody authored
586 (run-hooks 'iedit-mode-end-hook))
587
a9bda4d @victorhge Refine hotkeys
authored
588 (defun iedit-execute-last-modification (&optional arg)
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
589 "Apply last modification in iedit mode to the current buffer or an active region."
590 (interactive "*P")
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
591 (or (and iedit-last-initial-string-global
592 (not (string= iedit-last-initial-string-global iedit-last-occurrence-global)))
3ead6c6 @victorhge run checkdoc
authored
593 (error "No modification available"))
a9bda4d @victorhge Refine hotkeys
authored
594 (let ((occurrence-exp (regexp-quote iedit-last-initial-string-global))
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
595 (replacement iedit-last-occurrence-global)
a9bda4d @victorhge Refine hotkeys
authored
596 (case-fold-search (not iedit-case-sensitive-global))
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
597 beg end)
a9bda4d @victorhge Refine hotkeys
authored
598 (when case-fold-search
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
599 (setq occurrence-exp (downcase occurrence-exp))
600 (setq replacement (downcase replacement)))
a9bda4d @victorhge Refine hotkeys
authored
601 (if iedit-only-complete-symbol-global
602 (setq occurrence-exp (concat "\\_<" occurrence-exp "\\_>")))
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
603 (when (iedit-region-active)
a9bda4d @victorhge Refine hotkeys
authored
604 (setq beg (region-beginning))
605 (setq end (region-end)))
606 (perform-replace occurrence-exp replacement t t nil nil nil beg end)))
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
607
e9a9485 updates
anybody authored
608 (defun iedit-make-occurrence-overlay (begin end)
609 "Create an overlay for an occurrence in iedit mode.
610 Add the properties for the overlay: a face used to display a
611 occurrence's default value, and modification hooks to update
612 occurrences if the user starts typing."
613 (let ((occurrence (make-overlay begin end (current-buffer) nil t)))
614 (overlay-put occurrence iedit-occurrence-overlay-name t)
615 (overlay-put occurrence 'face iedit-occurrence-face)
a9bda4d @victorhge Refine hotkeys
authored
616 (overlay-put occurrence 'keymap iedit-current-keymap)
e9a9485 updates
anybody authored
617 (overlay-put occurrence 'insert-in-front-hooks '(iedit-occurrence-update))
618 (overlay-put occurrence 'insert-behind-hooks '(iedit-occurrence-update))
619 (overlay-put occurrence 'modification-hooks '(iedit-occurrence-update))
620 occurrence))
621
622 (defun iedit-make-unmatched-lines-overlay (begin end)
623 "Create an overlay for lines between two occurrences in iedit mode."
624 (let ((unmatched-lines-overlay (make-overlay begin end (current-buffer) nil t)))
625 (overlay-put unmatched-lines-overlay iedit-invisible-overlay-name t)
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
626 (overlay-put unmatched-lines-overlay 'invisible 'iedit-invisible-overlay-name)
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
627 ;; (overlay-put unmatched-lines-overlay 'intangible t)
e9a9485 updates
anybody authored
628 unmatched-lines-overlay))
629
6e49225 @victorhge Add iedit-toggle-case-sensitive
authored
630 (defun iedit-reset-aborting ()
3ead6c6 @victorhge run checkdoc
authored
631 "Turning `iedit-mode' off and reset iedit-aborting.
5ab9809 @victorhge Update documents
authored
632
633 This is added to `post-command-hook when aborting iedit-mode is
634 decided. `iedit-done' is postponed after the current command is
635 executed for avoiding iedit-occurrence-update is called for a
636 removed overlay."
6e49225 @victorhge Add iedit-toggle-case-sensitive
authored
637 (iedit-done)
638 (remove-hook 'post-command-hook 'iedit-reset-aborting t)
639 (setq iedit-aborting nil))
640
3097129 @victorhge Remove a limitation of empty occurrence string
authored
641 ;; There are two ways to update all occurrence. One is to redefine all key
642 ;; stroke map for overlay, the other is to figure out three basic modification
643 ;; in the modification hook. This function chooses the latter.
e9a9485 updates
anybody authored
644 (defun iedit-occurrence-update (occurrence after beg end &optional change)
645 "Update all occurrences.
9a891d7 updates
anybody authored
646 This modification hook is triggered when a user edits any
c8dac6b @victorhge exit iedit mode when change is not inside of occurrence.
authored
647 occurrence and is responsible for updating all other occurrences.
5ab9809 @victorhge Update documents
authored
648 Current supported edits are insertion, yank, deletion and
649 replacement. If this modification is going out of the
650 occurrence, it will exit iedit mode."
039c0e2 @victorhge Postpone iedit-done after commands are excuted.
authored
651 (when (and (not iedit-aborting )
0edc86f @victorhge Fix the last occurrence problem when it is a complete symbol.
authored
652 (not undo-in-progress)) ; undo will do all the update
c8dac6b @victorhge exit iedit mode when change is not inside of occurrence.
authored
653 ;; before modification
13ecba1 Merge branch 'upstream-master'
Le Wang authored
654 (if (null after)
c8dac6b @victorhge exit iedit mode when change is not inside of occurrence.
authored
655 (if (or (< beg (overlay-start occurrence))
656 (> end (overlay-end occurrence)))
0edc86f @victorhge Fix the last occurrence problem when it is a complete symbol.
authored
657 (progn (setq iedit-aborting t) ; abort iedit-mode
658 (add-hook 'post-command-hook 'iedit-reset-aborting nil t))
17ce973 @victorhge Clean up code
authored
659 (setq iedit-before-modification-string
660 (buffer-substring-no-properties beg end)))
20c1075 remove conditions on setting `iedit-before-modification-string'.
Le Wang authored
661 ;; after modification
c53a885 @victorhge Add bufferring modification functionalities
authored
662 (when (not iedit-buffering)
9bc57d2 @victorhge make iedit-skipped-modification-once buffer local
authored
663 ;; Check if we are inserting into zero-width occurrence. If so, then
664 ;; TWO modification hooks will be called -- "insert-in-front-hooks" and
665 ;; "insert-behind-hooks". We need to run just once.
c53a885 @victorhge Add bufferring modification functionalities
authored
666 (if (and (= beg (overlay-start occurrence))
667 (= end (overlay-end occurrence))
668 (= change 0)
669 (not iedit-skipped-modification-once))
670 (setq iedit-skipped-modification-once t)
671 (setq iedit-skipped-modification-once nil)
672 (when (or (eq 0 change) ;; insertion
673 (eq beg end) ;; deletion
674 (not (string= iedit-before-modification-string
675 (buffer-substring-no-properties beg end))))
676 (let ((inhibit-modification-hooks t) ; todo: extract this as a function
677 (offset (- beg (overlay-start occurrence)))
678 (value (buffer-substring-no-properties beg end)))
679 (save-excursion
680 ;; insertion or yank
681 (if (eq 0 change)
682 (dolist (another-occurrence (remove occurrence iedit-occurrences-overlays))
683 (progn
684 (goto-char (+ (overlay-start another-occurrence) offset))
685 (insert-and-inherit value)))
686 ;; deletion
b81b9d6 @victorhge Clean code.
authored
687 (dolist (another-occurrence (remove occurrence iedit-occurrences-overlays))
c53a885 @victorhge Add bufferring modification functionalities
authored
688 (let* ((beginning (+ (overlay-start another-occurrence) offset))
689 (ending (+ beginning change)))
690 (delete-region beginning ending)
691 (unless (eq beg end) ;; replacement
692 (goto-char beginning)
693 (insert-and-inherit value)))))))))))))
c8dac6b @victorhge exit iedit mode when change is not inside of occurrence.
authored
694
e9a9485 updates
anybody authored
695 (defun iedit-next-occurrence ()
9a891d7 updates
anybody authored
696 "Move forward to the next occurrence in the `iedit'.
ec6785b @victorhge host on git
authored
697 If the point is already in the last occurrences, you are asked to type
9a891d7 updates
anybody authored
698 another `iedit-next-occurrence', it starts again from the
699 beginning of the buffer."
e9a9485 updates
anybody authored
700 (interactive)
9a891d7 updates
anybody authored
701 (let ((pos (point))
702 (in-occurrence (get-char-property (point) 'iedit-occurrence-overlay-name)))
703 (when in-occurrence
704 (setq pos (next-single-char-property-change pos 'iedit-occurrence-overlay-name)))
705 (setq pos (next-single-char-property-change pos 'iedit-occurrence-overlay-name))
706 (if (/= pos (point-max))
707 (setq iedit-forward-success t)
708 (if (and iedit-forward-success in-occurrence)
709 (progn (message "This is the last occurrence.")
710 (setq iedit-forward-success nil))
da0a247 @tsdh Add option to match only complete words, not inside words
tsdh authored
711 (progn
9a891d7 updates
anybody authored
712 (if (get-char-property (point-min) 'iedit-occurrence-overlay-name)
713 (setq pos (point-min))
3bca7e2 @victorhge Fix iedit toggle case-sensitive problem when getting occurrence from …
authored
714 (setq pos (next-single-char-property-change
715 (point-min)
716 'iedit-occurrence-overlay-name)))
9a891d7 updates
anybody authored
717 (setq iedit-forward-success t)
718 (message "Located the first occurrence."))))
719 (when iedit-forward-success
720 (goto-char pos))))
e9a9485 updates
anybody authored
721
722 (defun iedit-prev-occurrence ()
9a891d7 updates
anybody authored
723 "Move backward to the previous occurrence in the `iedit'.
ec6785b @victorhge host on git
authored
724 If the point is already in the first occurrences, you are asked to type
9a891d7 updates
anybody authored
725 another `iedit-prev-occurrence', it starts again from the end of
726 the buffer."
e9a9485 updates
anybody authored
727 (interactive)
9a891d7 updates
anybody authored
728 (let ((pos (point))
729 (in-occurrence (get-char-property (point) 'iedit-occurrence-overlay-name)))
730 (when in-occurrence
731 (setq pos (previous-single-char-property-change pos 'iedit-occurrence-overlay-name)))
732 (setq pos (previous-single-char-property-change pos 'iedit-occurrence-overlay-name))
5ad28b5 updates
anybody authored
733 ;; At the start of the first occurrence
9a891d7 updates
anybody authored
734 (if (or (and (eq pos (point-min))
735 (not (get-char-property (point-min) 'iedit-occurrence-overlay-name)))
da0a247 @tsdh Add option to match only complete words, not inside words
tsdh authored
736 (and (eq (point) (point-min))
9a891d7 updates
anybody authored
737 in-occurrence))
738 (if (and iedit-forward-success in-occurrence)
739 (progn (message "This is the first occurrence.")
740 (setq iedit-forward-success nil))
da0a247 @tsdh Add option to match only complete words, not inside words
tsdh authored
741 (progn
9a891d7 updates
anybody authored
742 (setq pos (previous-single-char-property-change (point-max) 'iedit-occurrence-overlay-name))
743 (if (not (get-char-property (- (point-max) 1) 'iedit-occurrence-overlay-name))
744 (setq pos (previous-single-char-property-change pos 'iedit-occurrence-overlay-name)))
745 (setq iedit-forward-success t)
746 (message "Located the last occurrence.")))
747 (setq iedit-forward-success t))
748 (when iedit-forward-success
749 (goto-char pos))))
e9a9485 updates
anybody authored
750
e568677 @victorhge Add new functionality: restrict iedit-mode in region
authored
751 (defun iedit-first-occurrence ()
752 "Move to the first occurrence."
753 (interactive)
754 (let ((pos (if (get-char-property (point-min) 'iedit-occurrence-overlay-name)
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
755 (point-min)
756 (next-single-char-property-change
757 (point-min) 'iedit-occurrence-overlay-name))))
e568677 @victorhge Add new functionality: restrict iedit-mode in region
authored
758 (setq iedit-forward-success t)
759 (goto-char pos)
760 (message "Located the first occurrence.")))
761
762 (defun iedit-last-occurrence ()
763 "Move to the last occurrence."
764 (interactive)
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
765 (let ((pos (previous-single-char-property-change (point-max) 'iedit-occurrence-overlay-name)))
766 (if (not (get-char-property (- (point-max) 1) 'iedit-occurrence-overlay-name))
767 (setq pos (previous-single-char-property-change pos 'iedit-occurrence-overlay-name)))
768 (setq iedit-forward-success t)
769 (goto-char pos)
770 (message "Located the last occurrence.")))
e568677 @victorhge Add new functionality: restrict iedit-mode in region
authored
771
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
772 (defun iedit-toggle-unmatched-lines-visible (&optional arg)
773 "Toggle whether to display unmatched lines.
774 A prefix ARG specifies how many lines before and after the
799a0d1 @victorhge Bump to version 0.94
authored
775 occurrences are not hided; negative is treated the same as zero.
e9a9485 updates
anybody authored
776
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
777 If no prefix argument, the prefix argument last time or default
778 value of `iedit-occurrence-context-lines' is used for this time."
779 (interactive "P")
780 (if (null arg)
00abe88 @victorhge Hide line-feed between occurrence
authored
781 ;; toggle visible
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
782 (progn (setq iedit-unmatched-lines-invisible (not iedit-unmatched-lines-invisible))
783 (if iedit-unmatched-lines-invisible
00abe88 @victorhge Hide line-feed between occurrence
authored
784 (iedit-hide-unmatched-lines iedit-occurrence-context-lines)
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
785 (iedit-show-all)))
00abe88 @victorhge Hide line-feed between occurrence
authored
786 ;; reset invisible lines
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
787 (setq arg (prefix-numeric-value arg))
788 (if (< arg 0)
789 (setq arg 0))
790 (unless (and iedit-unmatched-lines-invisible
791 (= arg iedit-occurrence-context-lines))
792 (when iedit-unmatched-lines-invisible
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
793 (remove-overlays nil nil iedit-invisible-overlay-name t))
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
794 (setq iedit-occurrence-context-lines arg)
795 (setq iedit-unmatched-lines-invisible t)
796 (iedit-hide-unmatched-lines iedit-occurrence-context-lines))))
797
798 (defun iedit-show-all()
799 "Show hided lines."
800 (setq line-move-ignore-invisible nil)
00abe88 @victorhge Hide line-feed between occurrence
authored
801 (remove-from-invisibility-spec '(iedit-invisible-overlay-name . t))
802 (remove-overlays nil nil iedit-invisible-overlay-name t))
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
803
804 (defun iedit-hide-unmatched-lines (context-lines)
6e49225 @victorhge Add iedit-toggle-case-sensitive
authored
805 "Hide unmatched lines using invisible overlay."
00abe88 @victorhge Hide line-feed between occurrence
authored
806 (let ((prev-occurrence-end 1)
6e49225 @victorhge Add iedit-toggle-case-sensitive
authored
807 (unmatched-lines nil))
808 (save-excursion
809 (dolist (overlay iedit-occurrences-overlays)
810 (goto-char (overlay-start overlay))
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
811 (forward-line (- context-lines))
6e49225 @victorhge Add iedit-toggle-case-sensitive
authored
812 (let ((line-beginning (line-beginning-position)))
00abe88 @victorhge Hide line-feed between occurrence
authored
813 (if (> line-beginning prev-occurrence-end)
814 (push (list prev-occurrence-end (1- line-beginning)) unmatched-lines)))
6e49225 @victorhge Add iedit-toggle-case-sensitive
authored
815 (goto-char (overlay-end overlay))
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
816 (forward-line context-lines)
6e49225 @victorhge Add iedit-toggle-case-sensitive
authored
817 (setq prev-occurrence-end (line-end-position)))
818 (if (< prev-occurrence-end (point-max))
00abe88 @victorhge Hide line-feed between occurrence
authored
819 (push (list prev-occurrence-end (point-max)) unmatched-lines))
6e49225 @victorhge Add iedit-toggle-case-sensitive
authored
820 (when unmatched-lines
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
821 (set (make-local-variable 'line-move-ignore-invisible) t)
822 (add-to-invisibility-spec '(iedit-invisible-overlay-name . t))
6e49225 @victorhge Add iedit-toggle-case-sensitive
authored
823 (dolist (unmatch unmatched-lines)
824 (iedit-make-unmatched-lines-overlay (car unmatch) (cadr unmatch)))))))
825
a9bda4d @victorhge Refine hotkeys
authored
826 ;;;; functions for overlay keymap
e99e65f @victorhge Fix current point move problem when call iedit-replace-occurrences.
authored
827 (defun iedit-apply-on-occurrences (function &rest args)
da47784 @victorhge Add iedit mode easy access keys
authored
828 "Call function for each occurrence."
829 (let* ((ov (car iedit-occurrences-overlays))
830 (beg (overlay-start ov))
831 (end (overlay-end ov)))
3097129 @victorhge Remove a limitation of empty occurrence string
authored
832 (let ((inhibit-modification-hooks t))
833 (save-excursion
834 (dolist (occurrence iedit-occurrences-overlays)
835 (apply function (overlay-start occurrence) (overlay-end occurrence) args))))))
da47784 @victorhge Add iedit mode easy access keys
authored
836
837 (defun iedit-upcase-occurrences ()
838 "Covert occurrences to upper case."
e08ad59 @victorhge Make Iedit mode available for read only buffer
authored
839 (interactive "*")
e99e65f @victorhge Fix current point move problem when call iedit-replace-occurrences.
authored
840 (iedit-apply-on-occurrences 'upcase-region))
da47784 @victorhge Add iedit mode easy access keys
authored
841
842 (defun iedit-downcase-occurrences()
843 "Covert occurrences to lower case."
e08ad59 @victorhge Make Iedit mode available for read only buffer
authored
844 (interactive "*")
e99e65f @victorhge Fix current point move problem when call iedit-replace-occurrences.
authored
845 (iedit-apply-on-occurrences 'downcase-region))
da47784 @victorhge Add iedit mode easy access keys
authored
846
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
847 (defun iedit-apply-global-modification ()
a9bda4d @victorhge Refine hotkeys
authored
848 "Apply last global modification."
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
849 (interactive "*")
a9bda4d @victorhge Refine hotkeys
authored
850 (if (and iedit-last-initial-string-global
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
851 (string= iedit-initial-string-local iedit-last-initial-string-global)
852 (not (string= iedit-last-initial-string-global iedit-last-occurrence-global)))
a9bda4d @victorhge Refine hotkeys
authored
853 (iedit-replace-occurrences iedit-last-occurrence-global)
854 (message "No global modification available.")))
855
856 (defun iedit-replace-occurrences(to-string)
857 "Replace occurrences with STRING.
858 This function preserves case."
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
859 (interactive "*sReplace with: ")
e99e65f @victorhge Fix current point move problem when call iedit-replace-occurrences.
authored
860 (let* ((ov (iedit-find-current-occurrence-overlay))
a9bda4d @victorhge Refine hotkeys
authored
861 (offset (- (point) (overlay-start ov)))
62d2a5a @victorhge Fix iedit-kill-rectangle problem
authored
862 (from-string (downcase (buffer-substring-no-properties
863 (overlay-start ov)
864 (overlay-end ov)))))
e99e65f @victorhge Fix current point move problem when call iedit-replace-occurrences.
authored
865 (iedit-apply-on-occurrences
a9bda4d @victorhge Refine hotkeys
authored
866 (lambda (beg end from-string to-string)
867 (goto-char beg)
868 (search-forward from-string end)
869 (replace-match to-string nil))
870 from-string to-string)
e99e65f @victorhge Fix current point move problem when call iedit-replace-occurrences.
authored
871 (goto-char (+ (overlay-start ov) offset))))
da47784 @victorhge Add iedit mode easy access keys
authored
872
a9bda4d @victorhge Refine hotkeys
authored
873 (defun iedit-blank-occurrences()
da47784 @victorhge Add iedit mode easy access keys
authored
874 "Replace occurrences with blank spaces."
e08ad59 @victorhge Make Iedit mode available for read only buffer
authored
875 (interactive "*")
78f606d @victorhge Add iedit-number-occurrences function
authored
876 (let* ((ov (car iedit-occurrences-overlays))
877 (count (- (overlay-end ov) (overlay-start ov))))
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
878 (iedit-replace-occurrences (make-string count 32))))
da47784 @victorhge Add iedit mode easy access keys
authored
879
880 (defun iedit-delete-occurrences()
881 "Delete occurrences."
e08ad59 @victorhge Make Iedit mode available for read only buffer
authored
882 (interactive "*")
e99e65f @victorhge Fix current point move problem when call iedit-replace-occurrences.
authored
883 (iedit-apply-on-occurrences 'delete-region))
da47784 @victorhge Add iedit mode easy access keys
authored
884
78f606d @victorhge Add iedit-number-occurrences function
authored
885 (defun iedit-toggle-case-sensitive ()
886 "Toggle case-sensitive matching occurrences."
887 (interactive)
a9bda4d @victorhge Refine hotkeys
authored
888 (setq iedit-case-sensitive-local (not iedit-case-sensitive-local))
84c02ea @victorhge Fix toggle case-sensitive problem when occurrence is empty string.
authored
889 (if iedit-buffering
890 (iedit-stop-buffering))
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
891 (setq iedit-last-occurrence-local (iedit-current-occurrence-string))
892 (when iedit-last-occurrence-local
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
893 (remove-overlays nil nil iedit-occurrence-overlay-name t)
e568677 @victorhge Add new functionality: restrict iedit-mode in region
authored
894 (iedit-show-all)
b58ba72 @victorhge Add global variables and functions to make it possible to reapply las…
authored
895 (iedit-refresh iedit-last-occurrence-local (point-min) (point-max))))
78f606d @victorhge Add iedit-number-occurrences function
authored
896
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
897 ;; todo: add cancel buffering
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
898 (defun iedit-toggle-buffering ()
899 "Toggle buffering.
3ead6c6 @victorhge run checkdoc
authored
900 This is intended to improve iedit's response time. If the number
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
901 of occurrences are huge, iedit might be slow to update all the
902 occurrences for each key stoke. When buffering is on,
903 modification is only applied to the current occurrence and will
904 be applied to other occurrences when buffering is off."
905 (interactive "*")
906 (if iedit-buffering
907 (iedit-stop-buffering)
8219304 @victorhge Fix problem when iedit-stop-buffering is called when current point is…
authored
908 (iedit-start-buffering))
909 (message (concat "Iedit-mode buffering "
910 (if iedit-buffering
911 "started."
912 "stopped."))))
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
913
c53a885 @victorhge Add bufferring modification functionalities
authored
914 (defun iedit-start-buffering ()
915 "Start buffering."
916 (setq iedit-buffering t)
84c02ea @victorhge Fix toggle case-sensitive problem when occurrence is empty string.
authored
917 (setq iedit-before-modification-string (iedit-current-occurrence-string))
918 (setq iedit-before-modification-undo-list buffer-undo-list)
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
919 (setq iedit-mode (propertize
920 (concat " Iedit-B:" (number-to-string (length iedit-occurrences-overlays)))
921 'face 'font-lock-warning-face))
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
922 (force-mode-line-update))
c53a885 @victorhge Add bufferring modification functionalities
authored
923
924 (defun iedit-stop-buffering ()
8219304 @victorhge Fix problem when iedit-stop-buffering is called when current point is…
authored
925 "Stop buffering and apply the modification to other occurrences.
926 If current point is not at any occurrence, the buffered
927 modification is not going to be applied to other occurrences."
928 (let ((ov (iedit-find-current-occurrence-overlay)))
929 (when ov
930 (let* ((beg (overlay-start ov))
931 (end (overlay-end ov))
932 (modified-string (buffer-substring-no-properties beg end))
933 (offset (- (point) beg))) ;; delete-region moves cursor
934 (when (not (string= iedit-before-modification-string modified-string))
935 (save-excursion
936 ;; Rollback the current modification and buffer-undo-list. This is to
937 ;; avoid the inconsistency if user undoes modifications
938 (delete-region beg end)
939 (goto-char beg)
940 (insert-and-inherit iedit-before-modification-string)
941 (setq buffer-undo-list iedit-before-modification-undo-list)
942 (dolist (occurrence iedit-occurrences-overlays) ; todo:extract as a function
943 (let ((beginning (overlay-start occurrence))
944 (ending (overlay-end occurrence)))
945 (delete-region beginning ending)
946 (unless (eq beg end) ;; replacement
947 (goto-char beginning)
948 (insert-and-inherit modified-string)))))
949 (goto-char (+ (overlay-start ov) offset))))))
c53a885 @victorhge Add bufferring modification functionalities
authored
950 (setq iedit-buffering nil)
3bca7e2 @victorhge Fix iedit toggle case-sensitive problem when getting occurrence from …
authored
951 (setq iedit-mode (propertize
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
952 (concat (if iedit-rectangle " Iedit-RECT:" " Iedit:")
953 (number-to-string (length iedit-occurrences-overlays)))
3bca7e2 @victorhge Fix iedit toggle case-sensitive problem when getting occurrence from …
authored
954 'face 'font-lock-warning-face))
c53a885 @victorhge Add bufferring modification functionalities
authored
955 (force-mode-line-update)
8219304 @victorhge Fix problem when iedit-stop-buffering is called when current point is…
authored
956 (setq iedit-before-modification-undo-list nil))
c53a885 @victorhge Add bufferring modification functionalities
authored
957
a8cbb68 @victorhge Add iedit-kill-rectangle to iedit-rect mode and adjust keymap help
authored
958 (defvar iedit-number-line-counter 1
3ead6c6 @victorhge run checkdoc
authored
959 "Occurrence number for 'iedit-number-occurrences.")
78f606d @victorhge Add iedit-number-occurrences function
authored
960
0d4027f @victorhge Move save-excursion out of dolist
authored
961 (defun iedit-default-line-number-format (start-at)
78f606d @victorhge Add iedit-number-occurrences function
authored
962 (concat "%"
963 (int-to-string
964 (length (int-to-string
965 (1- (+ (length iedit-occurrences-overlays) start-at)))))
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
966 "d "))
78f606d @victorhge Add iedit-number-occurrences function
authored
967
968 (defun iedit-number-occurrences (start-at &optional format)
969 "Insert numbers in front of the occurrences.
970 START-AT, if non-nil, should be a number from which to begin
971 counting. FORMAT, if non-nil, should be a format string to pass
972 to `format' along with the line count. When called interactively
973 with a prefix argument, prompt for START-AT and FORMAT."
974 (interactive
975 (if current-prefix-arg
976 (let* ((start-at (read-number "Number to count from: " 1)))
977 (list start-at
978 (read-string "Format string: "
0d4027f @victorhge Move save-excursion out of dolist
authored
979 (iedit-default-line-number-format
78f606d @victorhge Add iedit-number-occurrences function
authored
980 start-at))))
981 (list 1 nil)))
982 (unless format
0d4027f @victorhge Move save-excursion out of dolist
authored
983 (setq format (iedit-default-line-number-format start-at)))
78f606d @victorhge Add iedit-number-occurrences function
authored
984 (let ((iedit-number-line-counter start-at))
985 (iedit-apply-on-occurrences
986 (lambda (beg _end format-string)
987 (goto-char beg)
988 (insert (format format-string iedit-number-line-counter))
989 (setq iedit-number-line-counter
990 (1+ iedit-number-line-counter))) format)))
991
3bca7e2 @victorhge Fix iedit toggle case-sensitive problem when getting occurrence from …
authored
992 (defun iedit-kill-rectangle(&optional fill)
993 "Kill the rectangle.
994 The behavior is the same as `kill-rectangle' in rect mode."
995 (interactive "*P")
62d2a5a @victorhge Fix iedit-kill-rectangle problem
authored
996 (or (iedit-same-column)
997 (error "Not a rectangle"))
998 (let ((inhibit-modification-hooks t)
999 (beg (overlay-start (car iedit-occurrences-overlays)))
1000 (end (overlay-end (progn (iedit-last-occurrence)
1001 (iedit-find-current-occurrence-overlay)))))
1002 (kill-rectangle beg end fill)))
3bca7e2 @victorhge Fix iedit toggle case-sensitive problem when getting occurrence from …
authored
1003
8559638 @victorhge Fix C-u C-u iedit-last-inital-string-global problem
authored
1004 (defun iedit-restrict-function(&optional arg)
1005 "Restricting iedit mode in current function."
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
1006 (interactive "P")
1007 (save-excursion
1008 (mark-defun)
1009 (iedit-restrict-region (region-beginning) (region-end) arg)))
1010
5ab9809 @victorhge Update documents
authored
1011 ;;; help functions
c53a885 @victorhge Add bufferring modification functionalities
authored
1012 (defun iedit-find-current-occurrence-overlay ()
5ab9809 @victorhge Update documents
authored
1013 "Return the current occurrence overlay at point or point - 1.
a9bda4d @victorhge Refine hotkeys
authored
1014 This function is supposed to be called in overlay keymap."
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
1015 (or (iedit-find-overlay-at-point (point) 'iedit-occurrence-overlay-name)
1016 (iedit-find-overlay-at-point (1- (point)) 'iedit-occurrence-overlay-name)))
c53a885 @victorhge Add bufferring modification functionalities
authored
1017
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
1018 (defun iedit-find-overlay-at-point (point property)
c53a885 @victorhge Add bufferring modification functionalities
authored
1019 "Return the overlay with PROPERTY at POINT."
1020 (let ((overlays (overlays-at point))
1021 found)
1022 (while (and overlays (not found))
1023 (let ((overlay (car overlays)))
1024 (if (overlay-get overlay property)
1025 (setq found overlay)
1026 (setq overlays (cdr overlays)))))
1027 found))
1028
62d2a5a @victorhge Fix iedit-kill-rectangle problem
authored
1029 (defun iedit-same-column ()
1030 "Return t if all occurrences are at the same column."
1031 (save-excursion
1032 (let ((column (progn (goto-char (overlay-start (car iedit-occurrences-overlays)))
1033 (current-column)))
1034 (overlays (cdr iedit-occurrences-overlays))
1035 (same t))
1036 (while (and overlays same)
1037 (let ((overlay (car overlays)))
1038 (if (/= (progn (goto-char (overlay-start overlay))
1039 (current-column))
1040 column)
1041 (setq same nil)
1042 (setq overlays (cdr overlays)))))
1043 same)))
1044
e568677 @victorhge Add new functionality: restrict iedit-mode in region
authored
1045 ;; This function might be called out of any occurrence
5ab9809 @victorhge Update documents
authored
1046 (defun iedit-current-occurrence-string ()
e568677 @victorhge Add new functionality: restrict iedit-mode in region
authored
1047 "Return current occurrence string.
1048 Return nil if occurrence string is empty string."
1049 (let* ((ov (or (iedit-find-current-occurrence-overlay)
1050 (car iedit-occurrences-overlays)))
5ab9809 @victorhge Update documents
authored
1051 (beg (overlay-start ov))
1052 (end (overlay-end ov)))
1053 (if (and ov (/= beg end))
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
1054 (buffer-substring-no-properties beg end)
5ab9809 @victorhge Update documents
authored
1055 nil)))
1056
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
1057 (defun iedit-find-overlay (beg end property &optional exclusive)
1058 "Return a overlay with property in region, or out of the region if EXCLUSIVE is not nil."
1059 (if exclusive
1060 (or (iedit-find-overlay-in-region (point-min) beg property)
1061 (iedit-find-overlay-in-region end (point-max) property))
1062 (iedit-find-overlay-in-region beg end property)))
1063
e568677 @victorhge Add new functionality: restrict iedit-mode in region
authored
1064 (defun iedit-find-overlay-in-region (beg end property)
1065 "Return a overlay with property in region."
1066 (let ((overlays (overlays-in beg end))
1067 found)
1068 (while (and overlays (not found))
1069 (let ((overlay (car overlays)))
1070 (if (and (overlay-get overlay property)
1071 (>= (overlay-start overlay) beg)
1072 (<= (overlay-end overlay) end))
1073 (setq found overlay)
1074 (setq overlays (cdr overlays)))))
1075 found))
1076
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
1077 (defun iedit-restrict-region (beg end &optional inclusive)
3ead6c6 @victorhge run checkdoc
authored
1078 "Restricting iedit mode in a region."
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
1079 (when iedit-buffering
1080 (iedit-stop-buffering))
1081 (setq iedit-last-occurrence-local (iedit-current-occurrence-string))
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
1082 (deactivate-mark t)
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
1083 (iedit-show-all)
90ef2d5 @victorhge Add digit prefix argment 0 to enable restricting match in a function
authored
1084 (iedit-cleanup-occurrences-overlays beg end inclusive)
bd8c116 @victorhge Add new occurrence function iedit-restrict-defun
authored
1085 (if iedit-unmatched-lines-invisible
1086 (iedit-hide-unmatched-lines iedit-occurrence-context-lines))
1087 (setq iedit-mode (propertize
1088 (concat " Iedit:" (number-to-string
1089 (length iedit-occurrences-overlays)))
1090 'face 'font-lock-warning-face))
1091 (force-mode-line-update))
1092
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
1093 (defun iedit-cleanup-occurrences-overlays (beg end &optional inclusive)
a9bda4d @victorhge Refine hotkeys
authored
1094 "Remove deleted overlays from list `iedit-occurrences-overlays'."
61689f6 @victorhge Add arg handle for iedit-mode to exclude some occurrences.
authored
1095 (if inclusive
1096 (remove-overlays beg end iedit-occurrence-overlay-name t)
1097 (remove-overlays (point-min) beg iedit-occurrence-overlay-name t)
1098 (remove-overlays end (point-max) iedit-occurrence-overlay-name t))
1099 (let (overlays)
1100 (dolist (overlay iedit-occurrences-overlays)
1101 (if (overlay-buffer overlay)
1102 (push overlay overlays)))
1103 (setq iedit-occurrences-overlays (nreverse overlays))))
1104
5ab9809 @victorhge Update documents
authored
1105 (defun iedit-printable (string)
4e31d50 @victorhge Add a prefix arg handling to iedit-toggle-unmatched-lines-visible
authored
1106 "Return a omitted substring that is not longer than 50.
1107 STRING is already `regexp-quote'ed"
5ab9809 @victorhge Update documents
authored
1108 (let ((first-newline-index (string-match "$" string))
1109 (length (length string)))
1110 (if (and first-newline-index
1111 (/= first-newline-index length))
1112 (if (< first-newline-index 50)
1113 (concat (substring string 0 first-newline-index) "...")
1114 (concat (substring string 0 50) "..."))
1115 (if (> length 50)
1116 (concat (substring string 0 50) "...")
1117 string))))
1118
ac2faa2 @victorhge Add iedit-mode-defun and global key binding
authored
1119 (defun iedit-region-active ()
1120 "Return t if mark is active and not empty region."
1121 (and (if iedit-transtient-mark-sensitive
1122 transient-mark-mode
1123 t)
1124 mark-active (not (equal (mark) (point)))))
1125
e9a9485 updates
anybody authored
1126 (provide 'iedit)
1127
1128 ;;; iedit.el ends here
a9bda4d @victorhge Refine hotkeys
authored
1129
1130 ;; LocalWords: iedit el MERCHANTABILITY kbd isearch todo ert Lindberg Tassilo
1131 ;; LocalWords: eval rect defgroup defcustom boolean defvar assq alist nconc
1132 ;; LocalWords: substring cadr keymap defconst purecopy bkm defun princ prev
1133 ;; LocalWords: iso lefttab backtab upcase downcase concat setq autoload arg
1134 ;; LocalWords: refactoring propertize cond goto nreverse progn rotatef eq elp
1135 ;; LocalWords: dolist pos unmatch args ov sReplace iedit's cdr quote'ed
Something went wrong with that request. Please try again.