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