Drop some `quotes' in configure.in's final summary
[emacs.git] / lisp / textmodes / reftex-global.el
blobd836bbb6cb824ef2ddf2f72b0eda0890ac43a3c6
1 ;;; reftex-global.el --- operations on entire documents with RefTeX
3 ;; Copyright (C) 1997-2012 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Version: 4.31
8 ;; Package: reftex
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (provide 'reftex-global)
31 (require 'reftex)
32 ;;;
34 (defun reftex-create-tags-file ()
35 "Create TAGS file by running `etags' on the current document.
36 The 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))
41 (cmd (format "etags %s" (mapconcat 'shell-quote-argument
42 files " "))))
43 (with-current-buffer (reftex-get-file-buffer-force master)
44 (message "Running etags to create TAGS file...")
45 (shell-command cmd)
46 (visit-tags-table "TAGS"))))
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.")
53 (defun reftex-grep-document (grep-cmd)
54 "Run grep query through all files related to this document.
55 With prefix arg, force to rescan document.
56 No active TAGS table is required."
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)))
69 (defun reftex-search-document (&optional regexp)
70 "Regexp search through all files of the current document.
71 Starts always in the master file. Stops when a match is found.
72 To continue searching for next match, use command \\[tags-loop-continue].
73 No 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)))
81 (reftex-access-scan-info current-prefix-arg)
82 (tags-search regexp (list 'reftex-all-document-files))))
84 (defun reftex-query-replace-document (&optional from to delimited)
85 "Do `query-replace-regexp' of FROM with TO over the entire document.
86 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
87 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
88 with the command \\[tags-loop-continue].
89 No 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))))
102 (defvar TeX-master)
103 (defvar isearch-next-buffer-function)
105 (defun reftex-find-duplicate-labels ()
106 "Produce a list of all duplicate labels in the document."
108 (interactive)
110 ;; Rescan the document to make sure
111 (reftex-access-scan-info t)
113 (let ((master (reftex-TeX-master-file))
114 (cnt 0)
115 (dlist
116 (mapcar
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)
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))))))
134 (reftex-uniquify-by-car (symbol-value reftex-docstruct-symbol)))))
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")
142 (insert
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)
149 (kill-buffer (current-buffer)) (delete-window)))
150 (local-set-key [?r] 'reftex-change-label)
151 (while dlist
152 (when (and (car (car dlist))
153 (cdr (car dlist)))
154 (incf cnt)
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."))))
163 (defun reftex-change-label (&optional from to)
164 "Run `query-replace-regexp' of FROM with TO in all macro arguments.
165 Works on the entire multifile document.
166 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
167 with the command \\[tags-loop-continue].
168 No 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
179 (concat "{" (regexp-quote from) "}")
180 (format "{%s}" to))))
182 (defun reftex-renumber-simple-labels ()
183 "Renumber all simple labels in the document to make them sequentially.
184 Simple labels are the ones created by RefTeX, consisting only of the
185 prefix and a number. After the command completes, all these labels will
186 have sequential numbers throughout the document. Any references to
187 the labels will be changed as well. For this, RefTeX looks at the
188 arguments of any macros which either start or end in the string `ref'.
189 This command should be used with care, in particular in multifile
190 documents. You should not use it if another document refers to this
191 one with the `xr' package."
192 (interactive)
193 ;; Rescan the entire document
194 (reftex-access-scan-info 1)
195 ;; Get some insurance
196 (if (and (reftex-is-multi)
197 (not (yes-or-no-p "Replacing all simple labels in multiple files is risky. Continue? ")))
198 (error "Abort"))
199 ;; Make the translation list
200 (let* ((re-core (concat "\\("
201 (mapconcat 'cdr reftex-typekey-to-prefix-alist "\\|")
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)
212 (while (setq entry (pop list))
213 (when (and (stringp (car entry))
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))))
225 (unless changed-sequence
226 (error "Simple labels are already in correct sequence"))
228 (reftex-ensure-write-access (reftex-all-document-files))
230 ;; Save all document buffers before this operation
231 (reftex-save-all-document-buffers)
233 ;; First test to check for errors.
234 (setq n (reftex-translate
235 files search-re translate-alist error-fmt 'test))
237 ;; Now the real thing.
238 (if (yes-or-no-p
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."))
252 (message "No replacements done"))))
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
256 ;; its association in TRANSLATE-ALIST.
257 ;; If we do not find an association and TEST is non-nil, query
258 ;; to ignore the problematic string.
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
265 (error "No such file %s" file))
266 (set-buffer buf)
267 (save-excursion
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]")
284 (looking-at (format
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))))))))
309 (defun reftex-save-all-document-buffers ()
310 "Save all documents associated with the current document.
311 The function is useful after a global action like replacing or renumbering
312 labels."
313 (interactive)
314 (let ((files (reftex-all-document-files))
315 file buffer)
316 (save-current-buffer
317 (while (setq file (pop files))
318 (setq buffer (reftex-get-buffer-visiting file))
319 (when buffer
320 (set-buffer buffer)
321 (save-buffer))))))
323 (defun reftex-ensure-write-access (files)
324 "Make sure we have write access to all files in FILES.
325 Also 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)
329 (ding)
330 (or (y-or-n-p (format "No such file %s. Continue? " file))
331 (error "Abort")))
332 (unless (file-writable-p file)
333 (ding)
334 (or (y-or-n-p (format "No write access to %s. Continue? " file))
335 (error "Abort")))
336 (when (and (setq buf (reftex-get-buffer-visiting file))
337 (with-current-buffer buf
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"))))))
344 ;;; Multi-file RefTeX Isearch
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'.
352 (defun reftex-isearch-wrap-function ()
353 (switch-to-buffer
354 (funcall isearch-next-buffer-function (current-buffer) t))
355 (goto-char (if isearch-forward (point-min) (point-max))))
357 (defun reftex-isearch-push-state-function ()
358 `(lambda (cmd)
359 (reftex-isearch-pop-state-function cmd ,(current-buffer))))
361 (defun reftex-isearch-pop-state-function (cmd buffer)
362 (switch-to-buffer buffer))
364 (defun reftex-isearch-isearch-search (string bound noerror)
365 (let ((nxt-buff nil)
366 (search-fun (isearch-search-fun-default)))
368 (funcall search-fun string bound noerror)
369 (unless bound
370 (condition-case nil
371 (when isearch-next-buffer-function
372 (while (not (funcall search-fun string bound noerror))
373 (cond
374 (isearch-forward
375 (setq nxt-buff
376 (funcall isearch-next-buffer-function
377 (current-buffer)))
378 (if (not nxt-buff)
379 (progn
380 (error "Wrap forward"))
381 (switch-to-buffer nxt-buff)
382 (goto-char (point-min))))
384 (setq nxt-buff
385 (funcall isearch-next-buffer-function
386 (current-buffer)))
387 (if (not nxt-buff)
388 (progn
389 (error "Wrap backward"))
390 (switch-to-buffer nxt-buff)
391 (goto-char (point-max))))))
392 (point))
393 (error nil))))))
395 ;; This function is called when isearch reaches the end of a
396 ;; buffer. For reftex what we want to do is not wrap to the
397 ;; beginning, but switch to the next buffer in the logical order of
398 ;; the document. This function looks through list of files in the
399 ;; document (reftex-all-document-files), searches for the current
400 ;; buffer and switches to the next/previous one in the logical order
401 ;; of the document. If WRAPP is true then wrap the search to the
402 ;; beginning/end of the file list, depending of the search direction.
403 (defun reftex-isearch-switch-to-next-file (crt-buf &optional wrapp)
404 (reftex-access-scan-info)
405 (let ((cb (buffer-file-name crt-buf))
406 (flist (reftex-all-document-files)))
407 (when flist
408 (if wrapp
409 (unless isearch-forward
410 (setq flist (last flist)))
411 (unless isearch-forward
412 (setq flist (reverse flist)))
413 (while (not (string= (car flist) cb))
414 (setq flist (cdr flist)))
415 (setq flist (cdr flist)))
416 (when flist
417 (find-file-noselect (car flist))))))
419 ;;;###autoload
420 (defun reftex-isearch-minor-mode (&optional arg)
421 "When on, isearch searches the whole document, not only the current file.
422 This minor mode allows isearch to search through all the files of
423 the current TeX document.
425 With no argument, this command toggles
426 `reftex-isearch-minor-mode'. With a prefix argument ARG, turn
427 `reftex-isearch-minor-mode' on if ARG is positive, otherwise turn it off."
428 (interactive "P")
429 (let ((old-reftex-isearch-minor-mode reftex-isearch-minor-mode))
430 (setq reftex-isearch-minor-mode
431 (not (or (and (null arg) reftex-isearch-minor-mode)
432 (<= (prefix-numeric-value arg) 0))))
433 (unless (eq reftex-isearch-minor-mode old-reftex-isearch-minor-mode)
434 (if reftex-isearch-minor-mode
435 (progn
436 (dolist (crt-buf (buffer-list))
437 (with-current-buffer crt-buf
438 (when reftex-mode
439 (if (boundp 'multi-isearch-next-buffer-function)
440 (set (make-local-variable 'multi-isearch-next-buffer-function)
441 'reftex-isearch-switch-to-next-file)
442 (set (make-local-variable 'isearch-wrap-function)
443 'reftex-isearch-wrap-function)
444 (set (make-local-variable 'isearch-search-fun-function)
445 (lambda () 'reftex-isearch-isearch-search))
446 (set (make-local-variable 'isearch-push-state-function)
447 'reftex-isearch-push-state-function)
448 (set (make-local-variable 'isearch-next-buffer-function)
449 'reftex-isearch-switch-to-next-file))
450 (setq reftex-isearch-minor-mode t))))
451 (add-hook 'reftex-mode-hook 'reftex-isearch-minor-mode))
452 (dolist (crt-buf (buffer-list))
453 (with-current-buffer crt-buf
454 (when reftex-mode
455 (if (boundp 'multi-isearch-next-buffer-function)
456 (kill-local-variable 'multi-isearch-next-buffer-function)
457 (kill-local-variable 'isearch-wrap-function)
458 (kill-local-variable 'isearch-search-fun-function)
459 (kill-local-variable 'isearch-push-state-function)
460 (kill-local-variable 'isearch-next-buffer-function))
461 (setq reftex-isearch-minor-mode nil))))
462 (remove-hook 'reftex-mode-hook 'reftex-isearch-minor-mode)))
463 ;; Force mode line redisplay.
464 (set-buffer-modified-p (buffer-modified-p))))
466 (add-minor-mode 'reftex-isearch-minor-mode "/I" nil nil
467 'reftex-isearch-minor-mode)
469 ;;; reftex-global.el ends here