-
Notifications
You must be signed in to change notification settings - Fork 2
/
search-commands.lisp
224 lines (206 loc) · 8.71 KB
/
search-commands.lisp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
;;; -*- Mode: Lisp; Package: CLIMACS-COMMANDS -*-
;;; (c) copyright 2004-2005 by
;;; Robert Strandh ([email protected])
;;; (c) copyright 2004-2005 by
;;; Elliott Johnson ([email protected])
;;; (c) copyright 2005 by
;;; Matthieu Villeneuve ([email protected])
;;; (c) copyright 2005 by
;;; Aleksandar Bakic ([email protected])
;;; (c) copyright 2007 by
;;; Troels Henriksen ([email protected])
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library 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
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
;;; Search commands for Climacs.
(in-package :climacs-commands)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Multiple query replace
(make-command-table 'multiple-query-replace-drei-table :errorp nil)
(defun multiple-query-replace-find-next-match (mark re list)
(multiple-value-bind (foundp start)
(re-search-forward mark re)
(when foundp
(loop with buffer = (buffer mark)
for string in list
when (buffer-looking-at buffer start string)
do (return string)))))
(define-command (com-multiple-query-replace :name t :command-table search-table) ()
"Prompts for pairs of strings, replacing the first with the second.
Entering an empty search string stops the prompting."
(let ((strings
(loop for string1 = (accept 'string :prompt "Multiple Query Replace")
until (string= string1 "")
for string2
= (accept 'string
:prompt (format nil
"Replace ~A with"
string1))
collecting (cons string1 string2))))
(multiple-query-replace strings)))
(define-command (com-multiple-query-replace-from-buffer :name t :command-table search-table)
((view 'view :prompt "View with Query Repace strings"))
(unless (member view (views *esa-instance*))
(beep)
(display-message "~A not an existing buffer" (name view))
(return-from com-multiple-query-replace-from-buffer nil))
(let* ((buffer (buffer view))
(contents (buffer-substring buffer 0 (1- (size buffer))))
(strings (loop with length = (length contents)
with index = 0
with start = 0
while (< index length)
do (loop until (>= index length)
while (whitespacep (syntax buffer)
(char contents index))
do (incf index))
(setf start index)
(loop until (>= index length)
until (whitespacep (syntax buffer)
(char contents index))
do (incf index))
until (= start index)
collecting (string-trim '(#\Space #\Tab #\Newline)
(subseq contents start index)))))
(unless (evenp (length strings))
(beep)
(display-message "Uneven number of strings in ~A" (name buffer))
(return-from com-multiple-query-replace-from-buffer nil))
(multiple-query-replace (loop for (string1 string2) on strings by #'cddr
collect (cons string1 string2)))))
(define-command (com-query-exchange :name t :command-table search-table) ()
"Prompts for two strings to exchange for one another."
(let* ((string1 (accept 'string :prompt "Query Exchange"))
(string2 (accept 'string :prompt (format nil
"Exchange ~A and"
string1))))
(multiple-query-replace (list (cons string1 string2) (cons string2 string1)))))
(defun multiple-query-replace (strings)
(declare (special strings))
(let* ((occurrences 0)
(search-strings (mapcar #'car strings))
(re (format nil "~{~A~^|~}" search-strings)))
(declare (special occurrences re))
(when strings
(let* ((point (point))
(found (multiple-query-replace-find-next-match point re search-strings)))
(when found
(setf (query-replace-state (current-window))
(make-instance 'query-replace-state
:string1 found
:string2 (cdr (assoc found strings :test #'string=)))
(query-replace-mode (current-window))
t)
(display-message "Replace ~A with ~A: "
(string1 (query-replace-state (current-window)))
(string2 (query-replace-state (current-window))))
(simple-command-loop 'multiple-query-replace-drei-table
(query-replace-mode (current-window))
((setf (query-replace-mode (current-window)) nil))))))
(display-message "Replaced ~D occurrence~:P" occurrences)))
(define-command (com-multiple-query-replace-replace
:name t
:command-table multiple-query-replace-drei-table)
()
(declare (special strings occurrences re))
(let* ((point (point (current-view)))
(state (query-replace-state (current-window)))
(string1 (string1 state))
(string1-length (length string1)))
(backward-object point string1-length)
(replace-one-string point string1-length (string2 state) (no-upper-p string1))
(incf occurrences)
(let ((found (multiple-query-replace-find-next-match
point
re
(mapcar #'car strings))))
(cond ((null found) (setf (query-replace-mode (current-window)) nil))
(t (setf (query-replace-state (current-window))
(make-instance 'query-replace-state
:string1 found
:string2 (cdr (assoc found strings :test #'string=))))
(display-message "Replace ~A with ~A: "
(string1 (query-replace-state (current-window)))
(string2 (query-replace-state (current-window)))))))))
(define-command (com-multiple-query-replace-replace-and-quit
:name t
:command-table multiple-query-replace-drei-table)
()
(declare (special strings occurrences))
(let* ((point (point))
(state (query-replace-state (current-window)))
(string1 (string1 state))
(string1-length (length string1)))
(backward-object point string1-length)
(replace-one-string point string1-length (string2 state) (no-upper-p string1))
(incf occurrences)
(setf (query-replace-mode (current-window)) nil)))
(define-command (com-multiple-query-replace-replace-all
:name t
:command-table multiple-query-replace-drei-table)
()
(declare (special strings occurrences re))
(let* ((point (point))
(found nil))
(loop for state = (query-replace-state (current-window))
for string1 = (string1 state)
for string1-length = (length string1)
do (backward-object point string1-length)
(replace-one-string point
string1-length
(string2 state)
(no-upper-p string1))
(incf occurrences)
(setf found (multiple-query-replace-find-next-match
point
re
(mapcar #'car strings)))
while found
do (setf (query-replace-state (current-window))
(make-instance 'query-replace-state
:string1 found
:string2 (cdr (assoc found strings :test #'string=))))
finally (setf (query-replace-state (current-window)) nil))))
(define-command (com-multiple-query-replace-skip
:name t
:command-table multiple-query-replace-drei-table)
()
(declare (special strings re))
(let* ((point (point))
(found (multiple-query-replace-find-next-match
point
re
(mapcar #'car strings))))
(cond ((null found) (setf (query-replace-mode (current-window)) nil))
(t (setf (query-replace-state (current-window))
(make-instance 'query-replace-state
:string1 found
:string2 (cdr (assoc found strings :test #'string=))))
(display-message "Replace ~A with ~A: "
(string1 (query-replace-state (current-window)))
(string2 (query-replace-state (current-window))))))))
(defun multiple-query-replace-set-key (gesture command)
(add-command-to-command-table command 'multiple-query-replace-drei-table
:keystroke gesture
:errorp nil))
(multiple-query-replace-set-key '(#\Newline) 'com-query-replace-exit)
(multiple-query-replace-set-key '(#\Space) 'com-multiple-query-replace-replace)
(multiple-query-replace-set-key '(#\Backspace) 'com-multiple-query-replace-skip)
(multiple-query-replace-set-key '(#\Rubout) 'com-multiple-query-replace-skip)
(multiple-query-replace-set-key '(#\q) 'com-query-replace-exit)
(multiple-query-replace-set-key '(#\y) 'com-multiple-query-replace-replace)
(multiple-query-replace-set-key '(#\n) 'com-multiple-query-replace-skip)
(multiple-query-replace-set-key '(#\.) 'com-multiple-query-replace-replace-and-quit)
(multiple-query-replace-set-key '(#\!) 'com-multiple-query-replace-replace-all)