Remove stuff about selection timeout, which is
[bpt/emacs.git] / lisp / textmodes / reftex-global.el
1 ;;; reftex-global.el - Operations on entire documents with RefTeX
2 ;; Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl>
5 ;; Version: 4.14
6 ;;
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 (eval-when-compile (require 'cl))
26 (provide 'reftex-global)
27 (require 'reftex)
28 ;;;
29
30 (defun reftex-create-tags-file ()
31 "Create TAGS file by running `etags' on the current document.
32 The TAGS file is also immediately visited with `visit-tags-table'."
33 (interactive)
34 (reftex-access-scan-info current-prefix-arg)
35 (let* ((master (reftex-TeX-master-file))
36 (files (reftex-all-document-files))
37 (cmd (format "etags %s" (mapconcat 'identity files " "))))
38 (save-excursion
39 (set-buffer (reftex-get-buffer-visiting master))
40 (message "Running etags to create TAGS file...")
41 (shell-command cmd)
42 (visit-tags-table "TAGS"))))
43
44 ;; History of grep commands.
45 (defvar reftex-grep-history nil)
46 (defvar reftex-grep-command "grep -n "
47 "Last grep command used in \\[reftex-grep-document]; default for next grep.")
48
49 (defun reftex-grep-document (grep-cmd)
50 "Run grep query through all files related to this document.
51 With prefix arg, force to rescan document.
52 No active TAGS table is required."
53
54 (interactive
55 (list (read-from-minibuffer "Run grep on document (like this): "
56 reftex-grep-command nil nil
57 'reftex-grep-history)))
58 (reftex-access-scan-info current-prefix-arg)
59 (let* ((files (reftex-all-document-files t))
60 (cmd (format
61 "%s %s" grep-cmd
62 (mapconcat 'identity files " "))))
63 (grep cmd)))
64
65 (defun reftex-search-document (&optional regexp)
66 "Regexp search through all files of the current document.
67 Starts always in the master file. Stops when a match is found.
68 To continue searching for next match, use command \\[tags-loop-continue].
69 No active TAGS table is required."
70 (interactive)
71 (let ((default (reftex-this-word)))
72 (unless regexp
73 (setq regexp (read-string (format "Search regexp in document [%s]: "
74 default))))
75 (if (string= regexp "") (setq regexp (regexp-quote default)))
76
77 (reftex-access-scan-info current-prefix-arg)
78 (tags-search regexp (list 'reftex-all-document-files))))
79
80 (defun reftex-query-replace-document (&optional from to delimited)
81 "Run a query-replace-regexp of FROM with TO over the entire document.
82 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
83 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
84 with the command \\[tags-loop-continue].
85 No active TAGS table is required."
86 (interactive)
87 (let ((default (reftex-this-word)))
88 (unless from
89 (setq from (read-string (format "Replace regexp in document [%s]: "
90 default)))
91 (if (string= from "") (setq from (regexp-quote default))))
92 (unless to
93 (setq to (read-string (format "Replace regexp %s with: " from))))
94 (reftex-access-scan-info current-prefix-arg)
95 (tags-query-replace from to (or delimited current-prefix-arg)
96 (list 'reftex-all-document-files))))
97
98 (defun reftex-find-duplicate-labels ()
99 "Produce a list of all duplicate labels in the document."
100
101 (interactive)
102
103 ;; Rescan the document to make sure
104 (reftex-access-scan-info t)
105
106 (let ((master (reftex-TeX-master-file))
107 (cnt 0)
108 (dlist
109 (mapcar
110 (lambda (x)
111 (let (x1)
112 (cond
113 ((memq (car x)
114 '(toc bof eof bib thebib label-numbers xr xr-doc
115 master-dir file-error bibview-cache appendix
116 is-multi index))
117 nil)
118 (t
119 (setq x1 (reftex-all-assoc-string
120 (car x) (symbol-value reftex-docstruct-symbol)))
121 (if (< 1 (length x1))
122 (append (list (car x))
123 (mapcar (lambda(x)
124 (abbreviate-file-name (nth 3 x)))
125 x1))
126 (list nil))))))
127 (reftex-uniquify-by-car (symbol-value reftex-docstruct-symbol)))))
128
129 (setq dlist (reftex-uniquify-by-car dlist))
130 (if (null dlist) (error "No duplicate labels in document"))
131 (switch-to-buffer-other-window "*Duplicate Labels*")
132 (set (make-local-variable 'TeX-master) master)
133 (erase-buffer)
134 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
135 (insert
136 " Move point to label and type `r' to run a query-replace on the label\n"
137 " and its references. Type `q' to exit this buffer.\n\n")
138 (insert " LABEL FILE\n")
139 (insert " -------------------------------------------------------------\n")
140 (use-local-map (make-sparse-keymap))
141 (local-set-key [?q] (lambda () "Kill this buffer." (interactive)
142 (kill-buffer (current-buffer)) (delete-window)))
143 (local-set-key [?r] 'reftex-change-label)
144 (while dlist
145 (when (and (car (car dlist))
146 (cdr (car dlist)))
147 (incf cnt)
148 (insert (mapconcat 'identity (car dlist) "\n ") "\n"))
149 (pop dlist))
150 (goto-char (point-min))
151 (when (= cnt 0)
152 (kill-buffer (current-buffer))
153 (delete-window)
154 (message "Document does not contain duplicate labels."))))
155
156 (defun reftex-change-label (&optional from to)
157 "Query replace FROM with TO in all \\label and \\ref commands.
158 Works on the entire multifile document.
159 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
160 with the command \\[tags-loop-continue].
161 No active TAGS table is required."
162 (interactive)
163 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
164 (unless from
165 (setq from (read-string (format "Replace label globally [%s]: "
166 default))))
167 (if (string= from "") (setq from default))
168 (unless to
169 (setq to (read-string (format "Replace label %s with: "
170 from))))
171 (reftex-query-replace-document
172 (concat "\\\\\\(label\\|[a-zA-Z]*ref\\){" (regexp-quote from) "}")
173 (format "\\\\\\1{%s}" to))))
174
175 (defun reftex-renumber-simple-labels ()
176 "Renumber all simple labels in the document to make them sequentially.
177 Simple labels are the ones created by RefTeX, consisting only of the
178 prefix and a number. After the command completes, all these labels will
179 have sequential numbers throughout the document. Any references to
180 the labels will be changed as well. For this, RefTeX looks at the
181 arguments of any macros which either start or end in the string `ref'.
182 This command should be used with care, in particular in multifile
183 documents. You should not use it if another document refers to this
184 one with the `xr' package."
185 (interactive)
186 ;; Resan the entire document
187 (reftex-access-scan-info 1)
188 ;; Get some insurance
189 (if (and (reftex-is-multi)
190 (not (yes-or-no-p "Replacing all simple labels in multiple files is risky. Continue? ")))
191 (error "Abort"))
192 ;; Make the translation list
193 (let* ((re-core (concat "\\("
194 (mapconcat 'cdr reftex-typekey-to-prefix-alist "\\|")
195 "\\)"))
196 (label-re (concat "\\`" re-core "\\([0-9]+\\)\\'"))
197 (search-re (concat "[{,]\\(" re-core "\\([0-9]+\\)\\)[,}]"))
198 (error-fmt "Undefined label or reference %s. Ignore and continue? ")
199 (label-numbers-alist (mapcar (lambda (x) (cons (cdr x) 0))
200 reftex-typekey-to-prefix-alist))
201 (files (reftex-all-document-files))
202 (list (symbol-value reftex-docstruct-symbol))
203 translate-alist n entry label new-label nr-cell changed-sequence)
204
205 (while (setq entry (pop list))
206 (when (and (stringp (car entry))
207 (string-match label-re (car entry)))
208 (setq label (car entry)
209 nr-cell (assoc (match-string 1 (car entry))
210 label-numbers-alist))
211 (if (assoc label translate-alist)
212 (error "Duplicate label %s" label))
213 (setq new-label (concat (match-string 1 (car entry))
214 (int-to-string (incf (cdr nr-cell)))))
215 (push (cons label new-label) translate-alist)
216 (or (string= label new-label) (setq changed-sequence t))))
217
218 (unless changed-sequence
219 (error "Simple labels are already in correct sequence"))
220
221 (reftex-ensure-write-access (reftex-all-document-files))
222
223 ;; Save all document buffers before this operation
224 (reftex-save-all-document-buffers)
225
226 ;; First test to check for erros
227 (setq n (reftex-translate
228 files search-re translate-alist error-fmt 'test))
229
230 ;; Now the real thing.
231 (if (yes-or-no-p
232 (format "Replace %d items at %d places in %d files? "
233 (length translate-alist) n (length files)))
234 (progn
235 (let ((inhibit-quit t)) ;; Do not disturb...
236 (reftex-translate
237 files search-re translate-alist error-fmt nil)
238 (setq quit-flag nil))
239 (if (and (reftex-is-multi)
240 (yes-or-no-p "Save entire document? "))
241 (reftex-save-all-document-buffers))
242 ;; Rescan again...
243 (reftex-access-scan-info 1)
244 (message "Done replacing simple labels."))
245 (message "No replacements done"))))
246
247 (defun reftex-translate (files search-re translate-alist error-fmt test)
248 ;; In FILES, look for SEARCH-RE and replace match 1 of it with
249 ;; its association in TRANSLATE-ALSIT.
250 ;; If we do not find an association and TEST is non-nil, query
251 ;; to ignore the problematic string.
252 ;; If TEST is nil, it is ignored without query.
253 ;; Return the number of replacements.
254 (let ((n 0) file label match-data buf macro pos cell)
255 (while (setq file (pop files))
256 (setq buf (reftex-get-file-buffer-force file))
257 (unless buf
258 (error "No such file %s" file))
259 (set-buffer buf)
260 (save-excursion
261 (save-restriction
262 (widen)
263 (goto-char (point-min))
264 (while (re-search-forward search-re nil t)
265 (backward-char)
266 (save-excursion
267 (setq label (reftex-match-string 1)
268 cell (assoc label translate-alist)
269 match-data (match-data)
270 macro (reftex-what-macro 1)
271 pos (cdr macro))
272 (goto-char (or pos (point)))
273 (when (and macro
274 (or (looking-at "\\\\ref")
275 (looking-at "\\\\[a-zA-Z]*ref\\(range\\)?[^a-zA-Z]")
276 (looking-at "\\\\ref[a-zA-Z]*[^a-zA-Z]")
277 (looking-at (format
278 reftex-find-label-regexp-format
279 (regexp-quote label)))))
280 ;; OK, we should replace it.
281 (set-match-data match-data)
282 (cond
283 ((and test (not cell))
284 ;; We've got a problem
285 (unwind-protect
286 (progn
287 (reftex-highlight 1 (match-beginning 0) (match-end 0))
288 (ding)
289 (or (y-or-n-p (format error-fmt label))
290 (error "Abort")))
291 (reftex-unhighlight 1)))
292 ((and test cell)
293 (incf n))
294 ((and (not test) cell)
295 ;; Replace
296 (goto-char (match-beginning 1))
297 (delete-region (match-beginning 1) (match-end 1))
298 (insert (cdr cell)))
299 (t nil))))))))
300 n))
301
302 (defun reftex-save-all-document-buffers ()
303 "Save all documents associated with the current document.
304 The function is useful after a global action like replacing or renumbering
305 labels."
306 (interactive)
307 (let ((files (reftex-all-document-files))
308 file buffer)
309 (save-excursion
310 (while (setq file (pop files))
311 (setq buffer (reftex-get-buffer-visiting file))
312 (when buffer
313 (set-buffer buffer)
314 (save-buffer))))))
315
316 (defun reftex-ensure-write-access (files)
317 "Make sure we have write access to all files in FILES.
318 Also checks if buffers visiting the files are in read-only mode."
319 (let (file buf)
320 (while (setq file (pop files))
321 (unless (file-exists-p file)
322 (ding)
323 (or (y-or-n-p (format "No such file %s. Continue? " file))
324 (error "Abort")))
325 (unless (file-writable-p file)
326 (ding)
327 (or (y-or-n-p (format "No write access to %s. Continue? " file))
328 (error "Abort")))
329 (when (and (setq buf (reftex-get-buffer-visiting file))
330 (save-excursion
331 (set-buffer buf)
332 buffer-read-only))
333 (ding)
334 (or (y-or-n-p (format "Buffer %s is read-only. Continue? "
335 (buffer-name buf)))
336 (error "Abort"))))))
337
338 ;;; reftex-global.el ends here