Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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