Spelling fixes.
[bpt/emacs.git] / lisp / textmodes / reftex-global.el
CommitLineData
3afbc435 1;;; reftex-global.el --- operations on entire documents with RefTeX
f2e3589a 2
73b0cd50 3;; Copyright (C) 1997-2011 Free Software Foundation, Inc.
3ba2590f 4
6fbeb429 5;; Author: Carsten Dominik <dominik@science.uva.nl>
ce545621 6;; Maintainer: auctex-devel@gnu.org
5d2a58e0 7;; Version: 4.31
bd78fa1d 8;; Package: reftex
3ba2590f
RS
9
10;; This file is part of GNU Emacs.
11
1fecc8fe 12;; GNU Emacs is free software: you can redistribute it and/or modify
3ba2590f 13;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
3ba2590f
RS
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
1fecc8fe 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1a9461d0 24
3afbc435
PJ
25;;; Commentary:
26
27;;; Code:
28
7c4d13cc 29(eval-when-compile (require 'cl))
1a9461d0
CD
30(provide 'reftex-global)
31(require 'reftex)
32;;;
33
34(defun reftex-create-tags-file ()
35 "Create TAGS file by running `etags' on the current document.
36The TAGS file is also immediately visited with `visit-tags-table'."
37 (interactive)
38 (reftex-access-scan-info current-prefix-arg)
39 (let* ((master (reftex-TeX-master-file))
40 (files (reftex-all-document-files))
773c795f
CD
41 (cmd (format "etags %s" (mapconcat 'shell-quote-argument
42 files " "))))
9a529312 43 (with-current-buffer (reftex-get-file-buffer-force master)
1a9461d0
CD
44 (message "Running etags to create TAGS file...")
45 (shell-command cmd)
46 (visit-tags-table "TAGS"))))
47
48;; History of grep commands.
49(defvar reftex-grep-history nil)
50(defvar reftex-grep-command "grep -n "
51 "Last grep command used in \\[reftex-grep-document]; default for next grep.")
52
53(defun reftex-grep-document (grep-cmd)
54 "Run grep query through all files related to this document.
55With prefix arg, force to rescan document.
56No active TAGS table is required."
57
58 (interactive
59 (list (read-from-minibuffer "Run grep on document (like this): "
60 reftex-grep-command nil nil
61 'reftex-grep-history)))
62 (reftex-access-scan-info current-prefix-arg)
63 (let* ((files (reftex-all-document-files t))
64 (cmd (format
65 "%s %s" grep-cmd
66 (mapconcat 'identity files " "))))
67 (grep cmd)))
68
69(defun reftex-search-document (&optional regexp)
70 "Regexp search through all files of the current document.
71Starts always in the master file. Stops when a match is found.
72To continue searching for next match, use command \\[tags-loop-continue].
73No active TAGS table is required."
74 (interactive)
75 (let ((default (reftex-this-word)))
76 (unless regexp
77 (setq regexp (read-string (format "Search regexp in document [%s]: "
78 default))))
79 (if (string= regexp "") (setq regexp (regexp-quote default)))
80
81 (reftex-access-scan-info current-prefix-arg)
82 (tags-search regexp (list 'reftex-all-document-files))))
83
84(defun reftex-query-replace-document (&optional from to delimited)
44fc58f2 85 "Do `query-replace-regexp' of FROM with TO over the entire document.
1a9461d0 86Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
44fc58f2 87If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
1a9461d0
CD
88with the command \\[tags-loop-continue].
89No active TAGS table is required."
90 (interactive)
91 (let ((default (reftex-this-word)))
92 (unless from
93 (setq from (read-string (format "Replace regexp in document [%s]: "
94 default)))
95 (if (string= from "") (setq from (regexp-quote default))))
96 (unless to
97 (setq to (read-string (format "Replace regexp %s with: " from))))
98 (reftex-access-scan-info current-prefix-arg)
99 (tags-query-replace from to (or delimited current-prefix-arg)
100 (list 'reftex-all-document-files))))
101
0c86715d
DN
102(defvar TeX-master)
103(defvar isearch-next-buffer-function)
7b07114a 104
1a9461d0
CD
105(defun reftex-find-duplicate-labels ()
106 "Produce a list of all duplicate labels in the document."
107
108 (interactive)
109
110 ;; Rescan the document to make sure
111 (reftex-access-scan-info t)
112
113 (let ((master (reftex-TeX-master-file))
3666daf6 114 (cnt 0)
1a9461d0
CD
115 (dlist
116 (mapcar
3666daf6
CD
117 (lambda (x)
118 (let (x1)
119 (cond
120 ((memq (car x)
121 '(toc bof eof bib thebib label-numbers xr xr-doc
122 master-dir file-error bibview-cache appendix
123 is-multi index))
124 nil)
125 (t
126 (setq x1 (reftex-all-assoc-string
127 (car x) (symbol-value reftex-docstruct-symbol)))
128 (if (< 1 (length x1))
129 (append (list (car x))
130 (mapcar (lambda(x)
131 (abbreviate-file-name (nth 3 x)))
132 x1))
133 (list nil))))))
1a9461d0
CD
134 (reftex-uniquify-by-car (symbol-value reftex-docstruct-symbol)))))
135
136 (setq dlist (reftex-uniquify-by-car dlist))
137 (if (null dlist) (error "No duplicate labels in document"))
138 (switch-to-buffer-other-window "*Duplicate Labels*")
139 (set (make-local-variable 'TeX-master) master)
140 (erase-buffer)
141 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
09e80d9f 142 (insert
1a9461d0
CD
143 " Move point to label and type `r' to run a query-replace on the label\n"
144 " and its references. Type `q' to exit this buffer.\n\n")
145 (insert " LABEL FILE\n")
146 (insert " -------------------------------------------------------------\n")
147 (use-local-map (make-sparse-keymap))
148 (local-set-key [?q] (lambda () "Kill this buffer." (interactive)
3666daf6 149 (kill-buffer (current-buffer)) (delete-window)))
1a9461d0
CD
150 (local-set-key [?r] 'reftex-change-label)
151 (while dlist
152 (when (and (car (car dlist))
153 (cdr (car dlist)))
3666daf6 154 (incf cnt)
1a9461d0
CD
155 (insert (mapconcat 'identity (car dlist) "\n ") "\n"))
156 (pop dlist))
157 (goto-char (point-min))
158 (when (= cnt 0)
159 (kill-buffer (current-buffer))
160 (delete-window)
161 (message "Document does not contain duplicate labels."))))
162
163(defun reftex-change-label (&optional from to)
3666daf6 164 "Run `query-replace-regexp' of FROM with TO in all macro arguments.
1a9461d0 165Works on the entire multifile document.
44fc58f2 166If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
1a9461d0
CD
167with the command \\[tags-loop-continue].
168No active TAGS table is required."
169 (interactive)
170 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
171 (unless from
172 (setq from (read-string (format "Replace label globally [%s]: "
173 default))))
174 (if (string= from "") (setq from default))
175 (unless to
176 (setq to (read-string (format "Replace label %s with: "
177 from))))
178 (reftex-query-replace-document
3666daf6
CD
179 (concat "{" (regexp-quote from) "}")
180 (format "{%s}" to))))
1a9461d0
CD
181
182(defun reftex-renumber-simple-labels ()
183 "Renumber all simple labels in the document to make them sequentially.
184Simple labels are the ones created by RefTeX, consisting only of the
185prefix and a number. After the command completes, all these labels will
186have sequential numbers throughout the document. Any references to
187the labels will be changed as well. For this, RefTeX looks at the
188arguments of any macros which either start or end in the string `ref'.
189This command should be used with care, in particular in multifile
190documents. You should not use it if another document refers to this
191one with the `xr' package."
192 (interactive)
09e80d9f 193 ;; Rescan the entire document
1a9461d0
CD
194 (reftex-access-scan-info 1)
195 ;; Get some insurance
196 (if (and (reftex-is-multi)
3666daf6 197 (not (yes-or-no-p "Replacing all simple labels in multiple files is risky. Continue? ")))
1a9461d0
CD
198 (error "Abort"))
199 ;; Make the translation list
09e80d9f
PE
200 (let* ((re-core (concat "\\("
201 (mapconcat 'cdr reftex-typekey-to-prefix-alist "\\|")
3666daf6
CD
202 "\\)"))
203 (label-re (concat "\\`" re-core "\\([0-9]+\\)\\'"))
204 (search-re (concat "[{,]\\(" re-core "\\([0-9]+\\)\\)[,}]"))
205 (error-fmt "Undefined label or reference %s. Ignore and continue? ")
206 (label-numbers-alist (mapcar (lambda (x) (cons (cdr x) 0))
207 reftex-typekey-to-prefix-alist))
208 (files (reftex-all-document-files))
209 (list (symbol-value reftex-docstruct-symbol))
210 translate-alist n entry label new-label nr-cell changed-sequence)
1a9461d0
CD
211
212 (while (setq entry (pop list))
213 (when (and (stringp (car entry))
3666daf6
CD
214 (string-match label-re (car entry)))
215 (setq label (car entry)
216 nr-cell (assoc (match-string 1 (car entry))
217 label-numbers-alist))
218 (if (assoc label translate-alist)
219 (error "Duplicate label %s" label))
220 (setq new-label (concat (match-string 1 (car entry))
221 (int-to-string (incf (cdr nr-cell)))))
222 (push (cons label new-label) translate-alist)
223 (or (string= label new-label) (setq changed-sequence t))))
1a9461d0
CD
224
225 (unless changed-sequence
226 (error "Simple labels are already in correct sequence"))
227
d8fb2015
CD
228 (reftex-ensure-write-access (reftex-all-document-files))
229
1a9461d0
CD
230 ;; Save all document buffers before this operation
231 (reftex-save-all-document-buffers)
232
4c36be58 233 ;; First test to check for errors.
09e80d9f 234 (setq n (reftex-translate
3666daf6 235 files search-re translate-alist error-fmt 'test))
1a9461d0
CD
236
237 ;; Now the real thing.
09e80d9f 238 (if (yes-or-no-p
3666daf6
CD
239 (format "Replace %d items at %d places in %d files? "
240 (length translate-alist) n (length files)))
241 (progn
242 (let ((inhibit-quit t)) ;; Do not disturb...
243 (reftex-translate
244 files search-re translate-alist error-fmt nil)
245 (setq quit-flag nil))
246 (if (and (reftex-is-multi)
247 (yes-or-no-p "Save entire document? "))
248 (reftex-save-all-document-buffers))
249 ;; Rescan again...
250 (reftex-access-scan-info 1)
251 (message "Done replacing simple labels."))
1a9461d0
CD
252 (message "No replacements done"))))
253
254(defun reftex-translate (files search-re translate-alist error-fmt test)
255 ;; In FILES, look for SEARCH-RE and replace match 1 of it with
0b381c7e 256 ;; its association in TRANSLATE-ALIST.
1a9461d0 257 ;; If we do not find an association and TEST is non-nil, query
09e80d9f 258 ;; to ignore the problematic string.
1a9461d0
CD
259 ;; If TEST is nil, it is ignored without query.
260 ;; Return the number of replacements.
261 (let ((n 0) file label match-data buf macro pos cell)
262 (while (setq file (pop files))
263 (setq buf (reftex-get-file-buffer-force file))
264 (unless buf
3666daf6 265 (error "No such file %s" file))
1a9461d0
CD
266 (set-buffer buf)
267 (save-excursion
3666daf6
CD
268 (save-restriction
269 (widen)
270 (goto-char (point-min))
271 (while (re-search-forward search-re nil t)
272 (backward-char)
273 (save-excursion
274 (setq label (reftex-match-string 1)
275 cell (assoc label translate-alist)
276 match-data (match-data)
277 macro (reftex-what-macro 1)
278 pos (cdr macro))
279 (goto-char (or pos (point)))
280 (when (and macro
281 (or (looking-at "\\\\ref")
282 (looking-at "\\\\[a-zA-Z]*ref\\(range\\)?[^a-zA-Z]")
283 (looking-at "\\\\ref[a-zA-Z]*[^a-zA-Z]")
09e80d9f 284 (looking-at (format
3666daf6
CD
285 reftex-find-label-regexp-format
286 (regexp-quote label)))))
287 ;; OK, we should replace it.
288 (set-match-data match-data)
289 (cond
290 ((and test (not cell))
291 ;; We've got a problem
292 (unwind-protect
293 (progn
294 (reftex-highlight 1 (match-beginning 0) (match-end 0))
295 (ding)
296 (or (y-or-n-p (format error-fmt label))
297 (error "Abort")))
298 (reftex-unhighlight 1)))
299 ((and test cell)
300 (incf n))
301 ((and (not test) cell)
302 ;; Replace
303 (goto-char (match-beginning 1))
304 (delete-region (match-beginning 1) (match-end 1))
305 (insert (cdr cell)))
306 (t nil))))))))
1a9461d0
CD
307 n))
308
309(defun reftex-save-all-document-buffers ()
310 "Save all documents associated with the current document.
311The function is useful after a global action like replacing or renumbering
312labels."
313 (interactive)
314 (let ((files (reftex-all-document-files))
3666daf6 315 file buffer)
9a529312 316 (save-current-buffer
1a9461d0 317 (while (setq file (pop files))
3666daf6
CD
318 (setq buffer (reftex-get-buffer-visiting file))
319 (when buffer
320 (set-buffer buffer)
321 (save-buffer))))))
1a9461d0 322
d8fb2015
CD
323(defun reftex-ensure-write-access (files)
324 "Make sure we have write access to all files in FILES.
325Also checks if buffers visiting the files are in read-only mode."
326 (let (file buf)
327 (while (setq file (pop files))
328 (unless (file-exists-p file)
3666daf6
CD
329 (ding)
330 (or (y-or-n-p (format "No such file %s. Continue? " file))
331 (error "Abort")))
d8fb2015 332 (unless (file-writable-p file)
3666daf6
CD
333 (ding)
334 (or (y-or-n-p (format "No write access to %s. Continue? " file))
335 (error "Abort")))
d8fb2015 336 (when (and (setq buf (reftex-get-buffer-visiting file))
9a529312 337 (with-current-buffer buf
3666daf6
CD
338 buffer-read-only))
339 (ding)
340 (or (y-or-n-p (format "Buffer %s is read-only. Continue? "
341 (buffer-name buf)))
342 (error "Abort"))))))
1a9461d0 343
fa862320
JL
344;;; Multi-file RefTeX Isearch
345
9a529312
SM
346;; `reftex-isearch-wrap-function', `reftex-isearch-push-state-function',
347;; `reftex-isearch-pop-state-function', `reftex-isearch-isearch-search'
348;; functions remain here only for backward-compatibility with Emacs 22
349;; and are obsolete since Emacs 23 that supports a single function
350;; variable `multi-isearch-next-buffer-function'.
fa862320 351
0072e19e
CD
352(defun reftex-isearch-wrap-function ()
353 (if (not isearch-word)
09e80d9f 354 (switch-to-buffer
0072e19e
CD
355 (funcall isearch-next-buffer-function (current-buffer) t)))
356 (goto-char (if isearch-forward (point-min) (point-max))))
357
358(defun reftex-isearch-push-state-function ()
359 `(lambda (cmd)
360 (reftex-isearch-pop-state-function cmd ,(current-buffer))))
361
362(defun reftex-isearch-pop-state-function (cmd buffer)
363 (switch-to-buffer buffer))
364
365(defun reftex-isearch-isearch-search (string bound noerror)
366 (let ((nxt-buff nil)
367 (search-fun
368 (cond
369 (isearch-word
370 (if isearch-forward 'word-search-forward 'word-search-backward))
371 (isearch-regexp
372 (if isearch-forward 're-search-forward 're-search-backward))
373 (t
374 (if isearch-forward 'search-forward 'search-backward)))))
375 (or
376 (funcall search-fun string bound noerror)
377 (unless bound
378 (condition-case nil
379 (when isearch-next-buffer-function
380 (while (not (funcall search-fun string bound noerror))
381 (cond
382 (isearch-forward
383 (setq nxt-buff
384 (funcall isearch-next-buffer-function
385 (current-buffer)))
386 (if (not nxt-buff)
387 (progn
388 (error "Wrap forward"))
389 (switch-to-buffer nxt-buff)
390 (goto-char (point-min))))
391 (t
392 (setq nxt-buff
393 (funcall isearch-next-buffer-function
394 (current-buffer)))
395 (if (not nxt-buff)
396 (progn
397 (error "Wrap backward"))
398 (switch-to-buffer nxt-buff)
399 (goto-char (point-max))))))
400 (point))
401 (error nil))))))
402
9a529312
SM
403;; This function is called when isearch reaches the end of a
404;; buffer. For reftex what we want to do is not wrap to the
405;; beginning, but switch to the next buffer in the logical order of
406;; the document. This function looks through list of files in the
407;; document (reftex-all-document-files), searches for the current
408;; buffer and switches to the next/previous one in the logical order
409;; of the document. If WRAPP is true then wrap the search to the
410;; beginning/end of the file list, depending of the search direction.
0072e19e
CD
411(defun reftex-isearch-switch-to-next-file (crt-buf &optional wrapp)
412 (reftex-access-scan-info)
b576c580
GM
413 (let ((cb (buffer-file-name crt-buf))
414 (flist (reftex-all-document-files)))
0072e19e
CD
415 (when flist
416 (if wrapp
417 (unless isearch-forward
418 (setq flist (last flist)))
419 (unless isearch-forward
b576c580 420 (setq flist (reverse flist)))
0072e19e
CD
421 (while (not (string= (car flist) cb))
422 (setq flist (cdr flist)))
423 (setq flist (cdr flist)))
424 (when flist
fa862320 425 (find-file-noselect (car flist))))))
0072e19e 426
0072e19e
CD
427;;;###autoload
428(defun reftex-isearch-minor-mode (&optional arg)
429 "When on, isearch searches the whole document, not only the current file.
430This minor mode allows isearch to search through all the files of
431the current TeX document.
432
433With no argument, this command toggles
434`reftex-isearch-minor-mode'. With a prefix argument ARG, turn
3ecd3a56 435`reftex-isearch-minor-mode' on if ARG is positive, otherwise turn it off."
0072e19e
CD
436 (interactive "P")
437 (let ((old-reftex-isearch-minor-mode reftex-isearch-minor-mode))
09e80d9f 438 (setq reftex-isearch-minor-mode
0072e19e
CD
439 (not (or (and (null arg) reftex-isearch-minor-mode)
440 (<= (prefix-numeric-value arg) 0))))
441 (unless (eq reftex-isearch-minor-mode old-reftex-isearch-minor-mode)
442 (if reftex-isearch-minor-mode
443 (progn
444 (dolist (crt-buf (buffer-list))
445 (with-current-buffer crt-buf
446 (when reftex-mode
fa862320
JL
447 (if (boundp 'multi-isearch-next-buffer-function)
448 (set (make-local-variable 'multi-isearch-next-buffer-function)
449 'reftex-isearch-switch-to-next-file)
450 (set (make-local-variable 'isearch-wrap-function)
451 'reftex-isearch-wrap-function)
452 (set (make-local-variable 'isearch-search-fun-function)
453 (lambda () 'reftex-isearch-isearch-search))
454 (set (make-local-variable 'isearch-push-state-function)
455 'reftex-isearch-push-state-function)
456 (set (make-local-variable 'isearch-next-buffer-function)
457 'reftex-isearch-switch-to-next-file))
0072e19e
CD
458 (setq reftex-isearch-minor-mode t))))
459 (add-hook 'reftex-mode-hook 'reftex-isearch-minor-mode))
460 (dolist (crt-buf (buffer-list))
461 (with-current-buffer crt-buf
462 (when reftex-mode
fa862320
JL
463 (if (boundp 'multi-isearch-next-buffer-function)
464 (kill-local-variable 'multi-isearch-next-buffer-function)
465 (kill-local-variable 'isearch-wrap-function)
466 (kill-local-variable 'isearch-search-fun-function)
467 (kill-local-variable 'isearch-push-state-function)
468 (kill-local-variable 'isearch-next-buffer-function))
0072e19e
CD
469 (setq reftex-isearch-minor-mode nil))))
470 (remove-hook 'reftex-mode-hook 'reftex-isearch-minor-mode)))
471 ;; Force modeline redisplay.
472 (set-buffer-modified-p (buffer-modified-p))))
473
09e80d9f 474(add-minor-mode 'reftex-isearch-minor-mode "/I" nil nil
0072e19e
CD
475 'reftex-isearch-minor-mode)
476
1a9461d0 477;;; reftex-global.el ends here