-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal-replace.el
116 lines (93 loc) · 3.66 KB
/
global-replace.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
;;; global-replace.el -- replace commands for emacs acting on several open buffers
;; Copyright (C) 2013 Sebastian Riese
;; Author: Sebastian Riese <sebastian.riese.mail@web.de>
;; This file is distributed under the terms of the GNU General Public
;; License as published by the Free Software Foundation, either
;; version 3 of the license, or (at your option) any later version.
;; It is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;; You should have received a copy of the GNU General Public License
;; along with global-replace.el. If not, see
;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Provide the ability to do replace operations on all open buffers,
;; that are not read-only and point to a file.
;; This file relies on the interals of replace.el from the Emacs core and
;; was written against GNU Emacs 23.3.
;;; Code:
(defmacro global-replace-for-buffer-query (&rest body)
`(save-window-excursion
(dolist (buf (buffer-list))
(set-buffer buf)
(if (and (not (null buffer-file-name))
(not buffer-read-only))
(progn (switch-to-buffer buf)
(save-excursion
(goto-char (point-min))
,@body))))))
(defmacro global-replace-for-buffer (&rest body)
`(save-excursion
(dolist (buf (buffer-list))
(set-buffer buf)
(if (and (not (null buffer-file-name))
(not buffer-read-only))
(save-excursion
(goto-char (point-min))
,@body)))))
(defun global-query-replace (from-string to-string &optional delimited)
"Iterate over all writable buffers which point to a file. In each of
them do `query-replace'"
(interactive
(let ((common
(query-replace-read-args
(concat "Replace"
(if current-prefix-arg " word" "")
" string"
(if (and transient-mark-mode mark-active) " in region" ""))
nil)))
(list (nth 0 common) (nth 1 common) (nth 2 common))))
(global-replace-for-buffer-query
(perform-replace from-string to-string t nil delimited)))
(defun global-replace-string (from-string to-string &optional delimited)
"Iterate over all writable buffers which point to a file. In each of the
do `replace-string'"
(interactive
(let ((common
(query-replace-read-args
(concat "Replace"
(if current-prefix-arg " word" "")
" string")
nil)))
(list (nth 0 common) (nth 1 common) (nth 2 common))))
(global-replace-for-buffer
(perform-replace from-string to-string nil nil delimited)))
(defun global-query-replace-regexp (from-string to-string &optional delimited)
"Iterate over all writable buffers which point to a file. In each of
them do `query-replace-regexp'"
(interactive
(let ((common
(query-replace-read-args
(concat "Replace"
(if current-prefix-arg " word" "")
" regexp")
nil)))
(list (nth 0 common) (nth 1 common) (nth 2 common))))
(global-replace-for-buffer-query
(perform-replace from-string to-string t t delimited)))
(defun global-replace-regexp (from-string to-string &optional delimited)
"Iterate over all writable buffers which point to a file. In each of the
do `replace-regexp'"
(interactive
(let ((common
(query-replace-read-args
(concat "Replace"
(if current-prefix-arg " word" "")
" regexp")
nil)))
(list (nth 0 common) (nth 1 common) (nth 2 common))))
(global-replace-for-buffer
(perform-replace from-string to-string nil t delimited)))
(provide 'global-replace)
;;; global-replace.el ends here