Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

766 lines
31 KiB

  1. ;;; iedit.el --- Edit multiple regions in the same way simultaneously.
  2. ;; Copyright (C) 2010, 2011, 2012 Victor Ren
  3. ;; Time-stamp: <2016-09-20 00:04:51 Victor Ren>
  4. ;; Author: Victor Ren <victorhge@gmail.com>
  5. ;; Keywords: occurrence region simultaneous refactoring
  6. ;; Version: 0.9.9
  7. ;; X-URL: http://www.emacswiki.org/emacs/Iedit
  8. ;; https://github.com/victorhge/iedit
  9. ;; Compatibility: GNU Emacs: 22.x, 23.x, 24.x
  10. ;; This file is not part of GNU Emacs, but it is distributed under
  11. ;; the same terms as GNU Emacs.
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;; Commentary:
  23. ;; This package is an Emacs minor mode and allows you to edit one occurrence of
  24. ;; some text in a buffer (possibly narrowed) or region, and simultaneously have
  25. ;; other occurrences edited in the same way.
  26. ;;
  27. ;; Normal scenario of iedit-mode is like:
  28. ;;
  29. ;; - Highlight certain contents - by press C-; (The default key binding)
  30. ;; All occurrences of a symbol, string in the buffer or a region may be
  31. ;; highlighted corresponding to current mark, point and prefix argument.
  32. ;; Refer to the document of `iedit-mode' for details.
  33. ;;
  34. ;; - Edit one of the occurrences
  35. ;; The change is applied to other occurrences simultaneously.
  36. ;;
  37. ;; - Finish - by pressing C-; again
  38. ;;
  39. ;; You can also use Iedit mode as a quick way to temporarily show only the
  40. ;; buffer lines that match the current text being edited. This gives you the
  41. ;; effect of a temporary `keep-lines' or `occur'. To get this effect, hit C-'
  42. ;; when in Iedit mode - it toggles hiding non-matching lines.
  43. ;;
  44. ;; Renaming refactoring is convenient in Iedit mode
  45. ;;
  46. ;; - The symbol under point is selected as occurrence by default and only
  47. ;; complete symbols are matched
  48. ;; - With digit prefix argument 0, only symbols in current function are matched
  49. ;; - Restricting symbols in current region can be done by pressing C-; again
  50. ;; - Last renaming refactoring is remembered and can be applied to other buffers
  51. ;; later
  52. ;;
  53. ;; There are also some other facilities you may never think about. Refer to the
  54. ;; document of function `iedit-mode' (C-h f iedit-mode RET) for more details.
  55. ;; The code was developed and fully tested on Gnu Emacs 24.0.93, partially
  56. ;; tested on Gnu Emacs 22. If you have any compatible problem, please let me
  57. ;; know.
  58. ;;; Contributors
  59. ;; Adam Lindberg <eproxus@gmail.com> added a case sensitivity option that can be toggled.
  60. ;; Tassilo Horn <tassilo@member.fsf.org> added an option to match only complete
  61. ;; words, not inside words
  62. ;; Le Wang <l26wang@gmail.com> proposed to match only complete symbols, not
  63. ;; inside symbols, contributed rectangle support
  64. ;;; Code:
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'sgml-mode))
  68. (require 'iedit-lib)
  69. (defcustom iedit-toggle-key-default (kbd "C-;")
  70. "If no-nil, the key is inserted into global-map,
  71. isearch-mode-map, esc-map and help-map."
  72. :type 'vector
  73. :group 'iedit)
  74. (defvar iedit-mode-hook nil
  75. "Function(s) to call after starting up an iedit.")
  76. (defvar iedit-mode-end-hook nil
  77. "Function(s) to call after terminating an iedit.")
  78. (defvar iedit-mode nil) ;; Name of the minor mode
  79. (defvar iedit-use-symbol-boundaries t
  80. "If no-nil, matches have to start and end at symbol boundaries. Otherwise,
  81. matches starts and end at word bondaries.")
  82. (defvar iedit-occurrence-type-local 'symbol
  83. "This is buffer local variable which indicates the occurrence
  84. type. It might be (symbol word other).")
  85. (defvar iedit-occurrence-type-global 'symbol
  86. "This is global variable which indicates the last global occurrence
  87. type. It might be (symbol word other).")
  88. (defvar iedit-last-occurrence-local nil
  89. "This is buffer local variable which is the occurrence when
  90. Iedit mode is turned off last time.")
  91. (defvar iedit-last-occurrence-global nil
  92. "This is global variable which is the occurrence when
  93. Iedit mode is turned off last time.")
  94. (defvar iedit-last-initial-string-global nil
  95. "This is a global variable which is the last initial occurrence string.")
  96. (defvar iedit-initial-string-local nil
  97. "This is buffer local variable which is the initial string to start Iedit mode.")
  98. (defvar iedit-initial-region nil
  99. "This is buffer local variable which is the initial region
  100. where Iedit mode is started from.")
  101. (defvar iedit-num-lines-to-expand-up 0
  102. "This is a global variable indicating how many lines up from
  103. point should be included in the replacement region.")
  104. (defvar iedit-num-lines-to-expand-down 0
  105. "This is a global variable indicating how many lines down from
  106. point should be included in the replacement region.")
  107. (defvar iedit-default-occurrence-local nil
  108. "This is a function which returns a string as occurrence candidate.
  109. It is called in `iedit-default-occurrence'. This buffer local
  110. varialbe can be configured in some modes. An example of how to
  111. use this variable:
  112. (add-hook 'perl-mode-hook
  113. '(lambda ()
  114. (setq iedit-default-occurrence-local
  115. '(lambda ()
  116. (let* ((bound (bounds-of-thing-at-point 'symbol))
  117. (prefix-char (char-after (1- (car bound)))))
  118. (if (memq prefix-char '(?$ ?% ?@ ?*))
  119. (progn
  120. (setq iedit-occurrence-type-local 'regexp)
  121. (concat (regexp-quote (buffer-substring-no-properties (1- (car bound)) (cdr bound))) \"\\\\_>\"))
  122. (buffer-substring-no-properties (car bound) (cdr bound))))))))
  123. '$%@*' will be included in the occurrences in perl mode.")
  124. (make-variable-buffer-local 'iedit-mode)
  125. (make-variable-buffer-local 'iedit-use-symbol-boundaries)
  126. (make-variable-buffer-local 'iedit-occurrence-type-local)
  127. (make-variable-buffer-local 'iedit-last-occurrence-local)
  128. (make-variable-buffer-local 'iedit-initial-string-local)
  129. (make-variable-buffer-local 'iedit-initial-region)
  130. (make-variable-buffer-local 'iedit-default-occurrence-local)
  131. (or (assq 'iedit-mode minor-mode-alist)
  132. (nconc minor-mode-alist
  133. (list '(iedit-mode iedit-mode))))
  134. ;;; Define iedit help map.
  135. (eval-when-compile (require 'help-macro))
  136. (defvar iedit-help-map
  137. (let ((map (make-sparse-keymap)))
  138. (define-key map (vector (event-convert-list `(,help-char))) 'iedit-help-for-help)
  139. (define-key map [help] 'iedit-help-for-help)
  140. (define-key map [f1] 'iedit-help-for-help)
  141. (define-key map "?" 'iedit-help-for-help)
  142. (define-key map "b" 'iedit-describe-bindings)
  143. (define-key map "k" 'iedit-describe-key)
  144. (define-key map "m" 'iedit-describe-mode)
  145. (define-key map "q" 'help-quit)
  146. map)
  147. "Keymap for characters following the Help key for Iedit mode.")
  148. (make-help-screen
  149. iedit-help-for-help-internal
  150. (purecopy "Type a help option: [bkm] or ?")
  151. "You have typed %THIS-KEY%, the help character. Type a Help option:
  152. \(Type \\<help-map>\\[help-quit] to exit the Help command.)
  153. b Display all Iedit key bindings.
  154. k KEYS Display full documentation of Iedit key sequence.
  155. m Display documentation of Iedit mode.
  156. You can't type here other help keys available in the global help map,
  157. but outside of this help window when you type them in Iedit mode,
  158. they exit Iedit mode before displaying global help."
  159. iedit-help-map)
  160. (defun iedit-help-for-help ()
  161. "Display Iedit help menu."
  162. (interactive)
  163. (let (same-window-buffer-names same-window-regexps)
  164. (iedit-help-for-help-internal)))
  165. (defun iedit-describe-bindings ()
  166. "Show a list of all keys defined in Iedit mode, and their definitions.
  167. This is like `describe-bindings', but displays only Iedit keys."
  168. (interactive)
  169. (let (same-window-buffer-names
  170. same-window-regexps
  171. (keymap (substitute-command-keys "\\{iedit-mode-keymap}\\{iedit-mode-occurrence-keymap}")))
  172. (with-help-window "*Help*"
  173. (with-current-buffer standard-output
  174. (princ "Iedit Mode Bindings: ")
  175. (princ keymap)))))
  176. (defun iedit-describe-key ()
  177. "Display documentation of the function invoked by Iedit mode key."
  178. (interactive)
  179. (let (same-window-buffer-names same-window-regexps)
  180. (call-interactively 'describe-key)))
  181. (defun iedit-describe-mode ()
  182. "Display documentation of Iedit mode."
  183. (interactive)
  184. (let (same-window-buffer-names same-window-regexps)
  185. (describe-function 'iedit-mode)))
  186. ;;; Default key bindings:
  187. (when (and iedit-toggle-key-default (null (where-is-internal 'iedit-mode)))
  188. (let ((key-def (lookup-key (current-global-map) iedit-toggle-key-default)))
  189. (if key-def
  190. (display-warning 'iedit (format "Iedit default key %S is occupied by %s."
  191. (key-description iedit-toggle-key-default)
  192. key-def)
  193. :warning)
  194. (define-key global-map iedit-toggle-key-default 'iedit-mode)
  195. (define-key isearch-mode-map iedit-toggle-key-default 'iedit-mode-from-isearch)
  196. (define-key esc-map iedit-toggle-key-default 'iedit-execute-last-modification)
  197. (define-key help-map iedit-toggle-key-default 'iedit-mode-toggle-on-function)
  198. (message "Iedit default key binding is %s" (key-description iedit-toggle-key-default)))))
  199. ;; Avoid to restore Iedit mode when restoring desktop
  200. (add-to-list 'desktop-minor-mode-handlers
  201. '(iedit-mode . nil))
  202. ;;; Define iedit help map.
  203. (eval-when-compile (require 'help-macro))
  204. (defvar iedit-mode-occurrence-keymap
  205. (let ((map (make-sparse-keymap)))
  206. (set-keymap-parent map iedit-occurrence-keymap-default)
  207. (define-key map (kbd "M-H") 'iedit-restrict-function)
  208. (define-key map (kbd "M-I") 'iedit-restrict-current-line)
  209. (define-key map (kbd "M-{") 'iedit-expand-up-a-line)
  210. (define-key map (kbd "M-}") 'iedit-expand-down-a-line)
  211. (define-key map (kbd "M-p") 'iedit-expand-up-to-occurrence)
  212. (define-key map (kbd "M-n") 'iedit-expand-down-to-occurrence)
  213. (define-key map (kbd "M-G") 'iedit-apply-global-modification)
  214. (define-key map (kbd "M-C") 'iedit-toggle-case-sensitive)
  215. map)
  216. "Keymap used within overlays in Iedit mode.")
  217. (defvar iedit-mode-keymap
  218. (let ((map (make-sparse-keymap)))
  219. (set-keymap-parent map iedit-lib-keymap)
  220. (define-key map (vector (event-convert-list `(,help-char))) iedit-help-map)
  221. (define-key map [help] iedit-help-map)
  222. (define-key map [f1] iedit-help-map)
  223. (define-key map (kbd "M-;") 'iedit-toggle-selection)
  224. map)
  225. "Keymap used while Iedit mode is enabled.")
  226. ;;; Define Iedit mode map
  227. (or (assq 'iedit-mode minor-mode-map-alist)
  228. (setq minor-mode-map-alist
  229. (cons (cons 'iedit-mode iedit-mode-keymap) minor-mode-map-alist)))
  230. ;;;###autoload
  231. (defun iedit-mode (&optional arg)
  232. "Toggle Iedit mode.
  233. This command behaves differently, depending on the mark, point,
  234. prefix argument and variable `iedit-transient-mark-sensitive'.
  235. If Iedit mode is off, turn Iedit mode on.
  236. When Iedit mode is turned on, all the occurrences of the current
  237. region in the buffer (possibly narrowed) or a region are
  238. highlighted. If one occurrence is modified, the change are
  239. propagated to all other occurrences simultaneously.
  240. If region is not active, `iedit-default-occurrence' is called to
  241. get an occurrence candidate, according to the thing at point. It
  242. might be url, email address, markup tag or current symbol(or
  243. word) .
  244. In the above two situations, with digit prefix argument 0, only
  245. occurrences in current function are matched. This is good for
  246. renaming refactoring in programming.
  247. You can also switch to Iedit mode from isearch mode directly. The
  248. current search string is used as occurrence. All occurrences of
  249. the current search string are highlighted.
  250. With an universal prefix argument, the occurrence when Iedit mode
  251. is turned off last time in current buffer is used as occurrence.
  252. This is intended to recover last Iedit mode which is turned off.
  253. If region active, Iedit mode is limited within the current
  254. region.
  255. With repeated universal prefix argument, the occurrence when
  256. Iedit mode is turned off last time (might be in other buffer) is
  257. used as occurrence. If region active, Iedit mode is limited
  258. within the current region.
  259. With digital prefix argument 1, Iedit mode is limited on the
  260. current symbol or the active region, which means just one
  261. instance is highlighted. This behavior serves as a start point
  262. of incremental selection work flow.
  263. If Iedit mode is on and region is active, Iedit mode is
  264. restricted in the region, e.g. the occurrences outside of the
  265. region is excluded.
  266. If Iedit mode is on and region is active, with an universal
  267. prefix argument, Iedit mode is restricted outside of the region,
  268. e.g. the occurrences in the region is excluded.
  269. Turn off Iedit mode in other situations.
  270. Commands:
  271. \\{iedit-mode-keymap}
  272. Keymap used within overlays:
  273. \\{iedit-mode-occurrence-keymap}"
  274. (interactive "P")
  275. (if iedit-mode
  276. (iedit-mode-on-action arg)
  277. (iedit-barf-if-lib-active)
  278. (let (occurrence
  279. (beg (if (eq major-mode 'occur-edit-mode) ; skip the first occurrence
  280. (next-single-char-property-change 1 'read-only)
  281. (point-min)))
  282. (end (point-max)))
  283. ;; Get the occurrence and iedit-occurrence-type-local
  284. (cond ((and arg
  285. (= 4 (prefix-numeric-value arg))
  286. iedit-last-occurrence-local)
  287. (setq occurrence iedit-last-occurrence-local))
  288. ((and arg
  289. (= 16 (prefix-numeric-value arg))
  290. iedit-last-initial-string-global)
  291. (setq occurrence iedit-last-initial-string-global)
  292. (setq iedit-occurrence-type-local iedit-occurrence-type-global))
  293. ((iedit-region-active)
  294. (setq occurrence (buffer-substring-no-properties
  295. (mark) (point)))
  296. (setq iedit-occurrence-type-local 'selection))
  297. (t (setq occurrence (iedit-default-occurrence))
  298. (unless occurrence
  299. (error "No candidate of the occurrence, cannot enable Iedit mode"))))
  300. ;; Get the scope
  301. (when arg
  302. (cond ((= 0 (prefix-numeric-value arg))
  303. (save-excursion
  304. (mark-defun)
  305. (setq beg (region-beginning))
  306. (setq end (region-end))))
  307. ((and (= 1 (prefix-numeric-value arg))
  308. (not (iedit-region-active)))
  309. (let ((region (bounds-of-thing-at-point 'symbol)))
  310. (setq beg (car region))
  311. (setq end (cdr region))))
  312. ((iedit-region-active)
  313. (setq beg (region-beginning))
  314. (setq end (region-end)))))
  315. (setq mark-active nil)
  316. (run-hooks 'deactivate-mark-hook)
  317. (setq iedit-initial-string-local occurrence)
  318. (iedit-start (iedit-regexp-quote occurrence) beg end)
  319. (unless iedit-occurrences-overlays
  320. ;; (message "No matches found for %s" (iedit-regexp-quote occurrence))
  321. (iedit-done)))))
  322. (defun iedit-mode-from-isearch (regexp)
  323. "Start Iedit mode using last search string as the regexp."
  324. (interactive
  325. (let ((regexp (cond
  326. ((functionp isearch-regexp-function)
  327. (funcall isearch-regexp-function isearch-string))
  328. (isearch-regexp-function (word-search-regexp isearch-string))
  329. (isearch-regexp isearch-string)
  330. (t (regexp-quote isearch-string)))))
  331. (list regexp)))
  332. (or isearch-success
  333. (error "No match" ))
  334. (if (or isearch-regexp isearch-regexp-function)
  335. nil
  336. (setq iedit-initial-string-local isearch-string))
  337. (let ((iedit-case-sensitive (not isearch-case-fold-search)))
  338. (isearch-exit)
  339. (setq mark-active nil)
  340. (run-hooks 'deactivate-mark-hook)
  341. (when iedit-mode
  342. (iedit-cleanup))
  343. (iedit-start regexp (point-min) (point-max))
  344. ;; TODO: reconsider how to avoid the loop in iedit-same-length
  345. (cond ((not iedit-occurrences-overlays)
  346. (message "No matches found for %s" regexp)
  347. (iedit-done))
  348. ((not (iedit-same-length))
  349. (message "Matches are not the same length.")
  350. (iedit-done)))))
  351. (defun iedit-start (occurrence-regexp beg end)
  352. "Start Iedit mode for the `occurrence-regexp' in the current buffer."
  353. ;; enforce skip modification once, errors may happen to cause this to be
  354. ;; unset.
  355. (setq iedit-skip-modification-once t)
  356. (setq iedit-initial-region (list beg end))
  357. (let ((counter 0))
  358. (if (eq iedit-occurrence-type-local 'markup-tag)
  359. (progn
  360. (setq iedit-occurrence-keymap iedit-occurrence-keymap-default)
  361. (iedit-make-markers-overlays iedit-occurrences-overlays)
  362. (setq counter 2))
  363. (setq iedit-occurrence-keymap iedit-mode-occurrence-keymap)
  364. (setq counter (iedit-make-occurrences-overlays occurrence-regexp beg end)))
  365. (message "%d matches for \"%s\""
  366. counter
  367. (iedit-printable occurrence-regexp))
  368. (setq iedit-mode
  369. (propertize
  370. (concat " Iedit:" (number-to-string counter))
  371. 'face
  372. 'font-lock-warning-face))
  373. (force-mode-line-update))
  374. (run-hooks 'iedit-mode-hook)
  375. (add-hook 'before-revert-hook 'iedit-done nil t)
  376. (add-hook 'kbd-macro-termination-hook 'iedit-done nil t)
  377. (add-hook 'change-major-mode-hook 'iedit-done nil t)
  378. (add-hook 'iedit-aborting-hook 'iedit-done nil t))
  379. (defun iedit-default-occurrence()
  380. "This function returns a string as occurrence candidate.
  381. The candidate depends on the thing at point."
  382. (let (occurrence-str)
  383. (cond
  384. ((thing-at-point 'url)
  385. (setq occurrence-str (thing-at-point 'url))
  386. (setq iedit-occurrence-type-local 'url))
  387. ((thing-at-point 'email)
  388. (setq occurrence-str (thing-at-point 'email))
  389. (setq iedit-occurrence-type-local 'email))
  390. (iedit-default-occurrence-local
  391. (setq occurrence-str (funcall iedit-default-occurrence-local)))
  392. ;; Try to mark sgml pair anyway
  393. ((and (not (bound-and-true-p sgml-electric-tag-pair-mode))
  394. (setq occurrence-str (iedit-mark-sgml-pair)))
  395. (setq iedit-occurrence-type-local 'markup-tag))
  396. ((and iedit-use-symbol-boundaries ;option
  397. (thing-at-point 'symbol))
  398. (setq occurrence-str (thing-at-point 'symbol))
  399. (setq iedit-occurrence-type-local 'symbol))
  400. ((thing-at-point 'word)
  401. (setq occurrence-str (thing-at-point 'word))
  402. (setq iedit-occurrence-type-local 'word)))
  403. occurrence-str))
  404. (defun iedit-regexp-quote (exp)
  405. "Return a regexp string."
  406. (cl-case iedit-occurrence-type-local
  407. ('symbol (concat "\\_<" (regexp-quote exp) "\\_>"))
  408. ('word (concat "\\<" (regexp-quote exp) "\\>"))
  409. ('regexp exp)
  410. ( t (regexp-quote exp))))
  411. (defun iedit-mark-sgml-pair ()
  412. "Check if the cursor is on a markup tag.
  413. If the cursor is on a markup tag, the postion of the opening and
  414. closing markup tags are saved in `iedit-occurrence-overlays'
  415. temporarily.
  416. The code is adpated from
  417. `sgml-electric-tag-pair-before-change-function'.
  418. Return the tag if succeeded, nil if failed."
  419. (condition-case err
  420. (save-excursion
  421. (skip-chars-backward "[:alnum:]-_.:")
  422. (if (or (eq (char-before) ?<)
  423. (and (eq (char-before) ?/)
  424. (eq (char-before (1- (point))) ?<)))
  425. (let* ((endp (eq (char-before) ?/))
  426. (cl-start (point))
  427. (cl-end (progn (skip-chars-forward "[:alnum:]-_.:") (point)))
  428. (match
  429. (if endp
  430. (when (sgml-skip-tag-backward 1) (forward-char 1) t)
  431. (with-syntax-table sgml-tag-syntax-table
  432. (up-list -1)
  433. (when (sgml-skip-tag-forward 1)
  434. (backward-sexp 1)
  435. (forward-char 2)
  436. t)))))
  437. (when (and match
  438. (/= cl-end cl-start)
  439. (equal (buffer-substring cl-start cl-end)
  440. (buffer-substring (point)
  441. (save-excursion
  442. (skip-chars-forward "[:alnum:]-_.:")
  443. (point))))
  444. (or (not endp) (eq (char-after cl-end) ?>)))
  445. (push (cons cl-start cl-end) iedit-occurrences-overlays)
  446. (push (cons (point) (+ (point) (- cl-end cl-start))) iedit-occurrences-overlays)
  447. (buffer-substring cl-start cl-end)))))
  448. (error nil)))
  449. (defun iedit-done ()
  450. "Exit Iedit mode.
  451. Save the current occurrence string locally and globally. Save
  452. the initial string globally."
  453. (when iedit-buffering
  454. (iedit-stop-buffering))
  455. (setq iedit-last-occurrence-local (iedit-current-occurrence-string))
  456. (setq iedit-occurrence-type-global iedit-occurrence-type-local)
  457. (setq iedit-last-occurrence-global iedit-last-occurrence-local)
  458. (setq iedit-last-initial-string-global iedit-initial-string-local)
  459. (if iedit-last-occurrence-local
  460. (kill-new iedit-last-occurrence-local)) ; Make occurrence the latest kill in the kill ring.
  461. (setq iedit-num-lines-to-expand-up 0)
  462. (setq iedit-num-lines-to-expand-down 0)
  463. (iedit-cleanup)
  464. (setq iedit-initial-string-local nil)
  465. (setq iedit-mode nil)
  466. (force-mode-line-update)
  467. (remove-hook 'before-revert-hook 'iedit-done t)
  468. (remove-hook 'kbd-macro-termination-hook 'iedit-done t)
  469. (remove-hook 'change-major-mode-hook 'iedit-done t)
  470. (remove-hook 'iedit-aborting-hook 'iedit-done t)
  471. (run-hooks 'iedit-mode-end-hook))
  472. (defun iedit-mode-on-action (&optional arg)
  473. "Turn off Iedit mode or restrict it in a region if region is active."
  474. (if (iedit-region-active)
  475. (iedit-restrict-region (region-beginning) (region-end) arg)
  476. (iedit-done)))
  477. ;;;###autoload
  478. (defun iedit-mode-toggle-on-function ()
  479. "Toggle Iedit mode on current function."
  480. (interactive)
  481. (iedit-mode 0))
  482. (defun iedit-execute-last-modification (&optional arg)
  483. "Apply last modification in Iedit mode to the current buffer or an active region."
  484. (interactive "*P")
  485. (or (and iedit-last-initial-string-global
  486. (not (string= iedit-last-initial-string-global iedit-last-occurrence-global)))
  487. (error "No modification available"))
  488. (let ((occurrence-exp (regexp-quote iedit-last-initial-string-global))
  489. (replacement iedit-last-occurrence-global)
  490. (case-fold-search (not iedit-case-sensitive))
  491. beg end)
  492. (when case-fold-search
  493. (setq occurrence-exp (downcase occurrence-exp))
  494. (setq replacement (downcase replacement)))
  495. ;; `iedit-regexp-quote' depends on iedit-occurrence-type-local
  496. (setq iedit-occurrence-type-local iedit-occurrence-type-global)
  497. (setq occurrence-exp (iedit-regexp-quote occurrence-exp))
  498. (when (iedit-region-active)
  499. (setq beg (region-beginning))
  500. (setq end (region-end)))
  501. (perform-replace occurrence-exp replacement t t nil nil nil beg end)))
  502. (defun iedit-apply-global-modification ()
  503. "Apply last global modification."
  504. (interactive "*")
  505. (if (and iedit-last-initial-string-global
  506. (string= iedit-initial-string-local iedit-last-initial-string-global)
  507. (not (string= iedit-last-initial-string-global iedit-last-occurrence-global)))
  508. (iedit-replace-occurrences iedit-last-occurrence-global)
  509. (message "No global modification available.")))
  510. (defun iedit-toggle-selection ()
  511. "Select or deselect the occurrence under point."
  512. (interactive)
  513. (iedit-barf-if-buffering)
  514. (let ((ov (iedit-find-current-occurrence-overlay)))
  515. (if ov
  516. (iedit-restrict-region (overlay-start ov) (overlay-end ov) t)
  517. (let ((current-occurrence-string (iedit-current-occurrence-string)))
  518. (when (not (null current-occurrence-string))
  519. (save-excursion
  520. (goto-char (if (> (point) (length current-occurrence-string))
  521. ( - (point) (length current-occurrence-string))
  522. (point-min)))
  523. (iedit-add-next-occurrence-overlay
  524. (iedit-regexp-quote current-occurrence-string)))
  525. (setq iedit-mode (propertize
  526. (concat " Iedit:" (number-to-string
  527. (length iedit-occurrences-overlays)))
  528. 'face 'font-lock-warning-face))
  529. (force-mode-line-update))))))
  530. (defun iedit-restrict-function(&optional arg)
  531. "Restricting Iedit mode in current function."
  532. (interactive "P")
  533. (save-excursion
  534. (mark-defun)
  535. (iedit-restrict-region (region-beginning) (region-end) arg))
  536. (message "Restricted in current function, %d matches."
  537. (length iedit-occurrences-overlays)))
  538. (defun iedit-restrict-current-line ()
  539. "Restrict Iedit mode to current line."
  540. (interactive)
  541. (iedit-restrict-region (iedit-char-at-bol) (iedit-char-at-eol))
  542. (setq iedit-num-lines-to-expand-up 0
  543. iedit-num-lines-to-expand-down 0)
  544. (message "Restricted to current line, %d match%s."
  545. (length iedit-occurrences-overlays)
  546. (if (= 1 (length iedit-occurrences-overlays)) "" "es")))
  547. (defun iedit-expand-by-a-line (where amount)
  548. "Expands the top or bottom of the search region upwards or
  549. downwards by `amount' lines. The region being acted upon is
  550. controlled with `where' ('top to act on the top, anything else
  551. for the bottom). With a prefix, collapses the top or bottom of
  552. the search region by `amount' lines."
  553. (interactive "P")
  554. (let ((occurrence (iedit-current-occurrence-string)))
  555. (iedit-cleanup)
  556. (if (eq where 'top)
  557. (setq iedit-num-lines-to-expand-up
  558. (max 0 (+ amount iedit-num-lines-to-expand-up)))
  559. (setq iedit-num-lines-to-expand-down
  560. (max 0 (+ amount iedit-num-lines-to-expand-down))))
  561. (iedit-start (iedit-regexp-quote occurrence)
  562. (iedit-char-at-bol (- iedit-num-lines-to-expand-up))
  563. (iedit-char-at-eol iedit-num-lines-to-expand-down))
  564. (message "Now looking -%d/+%d lines around current line, %d match%s."
  565. iedit-num-lines-to-expand-up
  566. iedit-num-lines-to-expand-down
  567. (length iedit-occurrences-overlays)
  568. (if (= 1 (length iedit-occurrences-overlays)) "" "es"))))
  569. (defun iedit-expand-up-a-line (&optional arg)
  570. "After start iedit-mode only on current symbol or the active
  571. region, this function expands the search region upwards by one
  572. line. With a prefix, bring the top of the region back down one
  573. line."
  574. (interactive "P")
  575. (iedit-expand-by-a-line 'top
  576. (if arg -1 1)))
  577. (defun iedit-expand-down-a-line (&optional arg)
  578. "After start iedit-mode only on current symbol or the active
  579. region, this function expands the search region downwards by one
  580. line. With a prefix, bring the bottom of the region back up one
  581. line."
  582. (interactive "P")
  583. (iedit-expand-by-a-line 'bottom
  584. (if arg -1 1)))
  585. (defun iedit-expand-down-to-occurrence (&optional arg)
  586. "Expand the search region downwards until reaching a new occurrence.
  587. If no such occurrence can be found, throw an error. With a
  588. prefix, bring the bottom of the region back up one occurrence."
  589. (interactive "P")
  590. (if arg
  591. (progn (iedit-restrict-region
  592. (iedit-first-occurrence)
  593. (1- (iedit-last-occurrence)))
  594. (when iedit-mode
  595. (goto-char (iedit-last-occurrence))))
  596. (iedit-expand-to-occurrence t)))
  597. (defun iedit-expand-up-to-occurrence (&optional arg)
  598. "Expand the search region upwards until reaching a new occurrence.
  599. If no such occurrence can be found, throw an error. With a
  600. prefix, bring the top of the region back down one occurrence."
  601. (interactive "P")
  602. (if arg
  603. (progn (iedit-restrict-region
  604. (+ (iedit-occurrence-string-length) (iedit-first-occurrence))
  605. (+ (iedit-occurrence-string-length) (iedit-last-occurrence)))
  606. (when iedit-mode
  607. (goto-char (iedit-first-occurrence))))
  608. (iedit-expand-to-occurrence nil)))
  609. (defun iedit-expand-to-occurrence (forward)
  610. "Expand to next or previous occurrence."
  611. (let ((pos (iedit-add-occurrence-overlay
  612. (iedit-regexp-quote (iedit-current-occurrence-string))
  613. (if forward
  614. (1+ (iedit-last-occurrence))
  615. (iedit-first-occurrence))
  616. forward)))
  617. (when pos
  618. (goto-char pos)
  619. (setq iedit-mode (propertize
  620. (concat " Iedit:" (number-to-string
  621. (length iedit-occurrences-overlays)))
  622. 'face 'font-lock-warning-face))
  623. (force-mode-line-update))))
  624. (defun iedit-restrict-region (beg end &optional inclusive)
  625. "Restricting Iedit mode in a region."
  626. (if (null (iedit-find-overlay beg end 'iedit-occurrence-overlay-name inclusive))
  627. (iedit-done)
  628. (when iedit-buffering
  629. (iedit-stop-buffering))
  630. (setq iedit-last-occurrence-local (iedit-current-occurrence-string))
  631. (setq mark-active nil)
  632. (run-hooks 'deactivate-mark-hook)
  633. (iedit-show-all)
  634. (iedit-cleanup-occurrences-overlays beg end inclusive)
  635. (if iedit-unmatched-lines-invisible
  636. (iedit-hide-unmatched-lines iedit-occurrence-context-lines))
  637. (setq iedit-mode (propertize
  638. (concat " Iedit:" (number-to-string
  639. (length iedit-occurrences-overlays)))
  640. 'face 'font-lock-warning-face))
  641. (force-mode-line-update)))
  642. (defun iedit-toggle-case-sensitive ()
  643. "Toggle case-sensitive matching occurrences. "
  644. (interactive)
  645. (setq iedit-case-sensitive (not iedit-case-sensitive))
  646. (if iedit-buffering
  647. (iedit-stop-buffering))
  648. (setq iedit-last-occurrence-local (iedit-current-occurrence-string))
  649. (when iedit-last-occurrence-local
  650. (remove-overlays nil nil iedit-occurrence-overlay-name t)
  651. (iedit-show-all)
  652. (let* ((occurrence-regexp (iedit-regexp-quote iedit-last-occurrence-local))
  653. (begin (car iedit-initial-region))
  654. (end (cadr iedit-initial-region))
  655. (counter (iedit-make-occurrences-overlays occurrence-regexp begin end)))
  656. (message "iedit %s. %d matches for \"%s\""
  657. (if iedit-case-sensitive
  658. "is case sensitive"
  659. "ignores case")
  660. counter
  661. (iedit-printable occurrence-regexp))
  662. (setq iedit-mode
  663. (propertize
  664. (concat " Iedit:" (number-to-string counter))
  665. 'face
  666. 'font-lock-warning-face))
  667. (force-mode-line-update))))
  668. (provide 'iedit)
  669. ;;; iedit.el ends here
  670. ;; LocalWords: iedit el MERCHANTABILITY kbd isearch todo ert Lindberg Tassilo
  671. ;; LocalWords: eval defgroup defcustom boolean defvar assq alist nconc
  672. ;; LocalWords: substring cadr keymap defconst purecopy bkm defun princ prev
  673. ;; LocalWords: iso lefttab backtab upcase downcase concat setq autoload arg
  674. ;; LocalWords: refactoring propertize cond goto nreverse progn rotatef eq elp
  675. ;; LocalWords: dolist pos unmatch args ov sReplace iedit's cdr quote'ed