search-commands.lisp 8.71 KB
Newer Older
1
;;; -*- Mode: Lisp; Package: CLIMACS-COMMANDS -*-
2 3 4 5 6 7 8 9 10

;;;  (c) copyright 2004-2005 by
;;;           Robert Strandh (strandh@labri.fr)
;;;  (c) copyright 2004-2005 by
;;;           Elliott Johnson (ejohnson@fasl.info)
;;;  (c) copyright 2005 by
;;;           Matthieu Villeneuve (matthieu.villeneuve@free.fr)
;;;  (c) copyright 2005 by
;;;           Aleksandar Bakic (a_bakic@yahoo.com)
11 12
;;;  (c) copyright 2007 by
;;;           Troels Henriksen (athas@sigkill.dk)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

;;; 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.

29
;;; Search commands for Climacs.
30

31
(in-package :climacs-commands)
32

33 34 35 36
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 
;;; Multiple query replace

37
(make-command-table 'multiple-query-replace-drei-table :errorp nil)
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

(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)
63 64
    ((view 'view :prompt "View with Query Repace strings"))
  (unless (member view (views *esa-instance*))
65
    (beep)
66
    (display-message "~A not an existing buffer" (name view))
67
    (return-from com-multiple-query-replace-from-buffer nil))
68 69
  (let* ((buffer (buffer view))
         (contents (buffer-substring buffer 0 (1- (size buffer))))
70 71 72 73 74
	 (strings (loop with length = (length contents)
			with index = 0
			with start = 0
			while (< index length)
			do (loop until (>= index length)
75 76
				 while (whitespacep (syntax buffer)
                                                    (char contents index))
77 78 79
				 do (incf index))
			   (setf start index)
			   (loop until (>= index length)
80 81
				 until (whitespacep (syntax buffer)
                                                    (char contents index))
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
				 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
108
      (let* ((point (point)) 
109 110
	     (found (multiple-query-replace-find-next-match point re search-strings)))
	(when found
111
	  (setf (query-replace-state (current-window))
112
		(make-instance 'query-replace-state
113 114 115
                 :string1 found
                 :string2 (cdr (assoc found strings :test #'string=)))
		(query-replace-mode (current-window))
116 117
		t)
	  (display-message "Replace ~A with ~A: "
118 119
			   (string1 (query-replace-state (current-window)))
			   (string2 (query-replace-state (current-window))))
120
	  (simple-command-loop 'multiple-query-replace-drei-table
121 122
			       (query-replace-mode (current-window))
			       ((setf (query-replace-mode (current-window)) nil))))))
123 124 125 126
    (display-message "Replaced ~D occurrence~:P" occurrences)))

(define-command (com-multiple-query-replace-replace
		 :name t
127
		 :command-table multiple-query-replace-drei-table)
128
    ()
129
  (declare (special strings occurrences re))
130 131
  (let* ((point (point (current-view))) 
	 (state (query-replace-state (current-window)))
132 133
	 (string1 (string1 state))
	 (string1-length (length string1)))
134
    (backward-object point string1-length)
135
    (replace-one-string point string1-length (string2 state) (no-upper-p string1))
136 137 138 139 140
    (incf occurrences)
    (let ((found (multiple-query-replace-find-next-match
		  point
		  re
		  (mapcar #'car strings))))
141 142
      (cond ((null found) (setf (query-replace-mode (current-window)) nil))
	    (t (setf (query-replace-state (current-window))
143 144 145 146
		     (make-instance 'query-replace-state
			:string1 found
			:string2 (cdr (assoc found strings :test #'string=))))
	       (display-message "Replace ~A with ~A: "
147 148
				(string1 (query-replace-state (current-window)))
				(string2 (query-replace-state (current-window)))))))))
149

150 151 152

(define-command (com-multiple-query-replace-replace-and-quit
		 :name t
153
		 :command-table multiple-query-replace-drei-table)
154 155
    ()
  (declare (special strings occurrences))
156 157
  (let* ((point (point))
	 (state (query-replace-state (current-window)))
158 159 160 161 162
	 (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)
163
    (setf (query-replace-mode (current-window)) nil)))
164 165 166

(define-command (com-multiple-query-replace-replace-all
		 :name t
167
		 :command-table multiple-query-replace-drei-table)
168 169
    ()
  (declare (special strings occurrences re))
170
  (let* ((point (point)) 
171
	 (found nil))
172
    (loop for state = (query-replace-state (current-window))
173 174 175 176 177 178 179 180 181 182 183 184 185
	  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
186
	  do (setf (query-replace-state (current-window))
187 188 189
		   (make-instance 'query-replace-state
		      :string1 found
		      :string2 (cdr (assoc found strings :test #'string=))))
190
	  finally (setf (query-replace-state (current-window)) nil))))
191

192 193
(define-command (com-multiple-query-replace-skip
		 :name t
194
		 :command-table multiple-query-replace-drei-table)
195 196
    ()
  (declare (special strings re))
197
  (let* ((point (point))
198 199 200 201
	 (found (multiple-query-replace-find-next-match
		 point
		 re
		 (mapcar #'car strings))))
202 203
    (cond ((null found) (setf (query-replace-mode (current-window)) nil))
	    (t (setf (query-replace-state (current-window))
204 205 206 207
		     (make-instance 'query-replace-state
			:string1 found
			:string2 (cdr (assoc found strings :test #'string=))))
	       (display-message "Replace ~A with ~A: "
208 209
				(string1 (query-replace-state (current-window)))
				(string2 (query-replace-state (current-window))))))))
210 211

(defun multiple-query-replace-set-key (gesture command)
212
  (add-command-to-command-table command 'multiple-query-replace-drei-table
213 214 215 216 217 218 219 220 221 222
				: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)
223
(multiple-query-replace-set-key '(#\.) 'com-multiple-query-replace-replace-and-quit)
224
(multiple-query-replace-set-key '(#\!) 'com-multiple-query-replace-replace-all)