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 <https://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl-lib
))
28 (provide 'reftex-global
)
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'."
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
42 (with-current-buffer (reftex-get-file-buffer-force master
)
43 (message "Running etags to create TAGS file...")
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.")
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."
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
))
66 (mapconcat 'identity files
" "))))
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."
76 (let ((default (reftex-this-word)))
78 (setq regexp
(read-string (format "Search regexp in document [%s]: "
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
))))
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."
93 (let ((default (reftex-this-word)))
95 (setq from
(read-string (format "Replace regexp in document [%s]: "
97 (if (string= from
"") (setq from
(regexp-quote default
))))
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
))))
105 (defvar isearch-next-buffer-function
)
108 (defun reftex-find-duplicate-labels ()
109 "Produce a list of all duplicate labels in the document."
113 ;; Rescan the document to make sure
114 (reftex-access-scan-info t
)
116 (let ((master (reftex-TeX-master-file))
124 '(toc bof eof bib thebib label-numbers xr xr-doc
125 master-dir file-error bibview-cache appendix
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
))
134 (abbreviate-file-name (nth 3 x
)))
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
)
144 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
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
)
155 (when (and (car (car dlist
))
158 (insert (mapconcat 'identity
(car dlist
) "\n ") "\n"))
160 (goto-char (point-min))
162 (kill-buffer (current-buffer))
164 (message "Document does not contain duplicate labels."))))
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."
174 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
176 (setq from
(read-string (format "Replace label globally [%s]: "
178 (if (string= from
"") (setq from default
))
180 (setq to
(read-string (format "Replace label %s with: "
182 (reftex-query-replace-document
183 (concat "{" (regexp-quote from
) "}")
184 (format "{%s}" to
))))
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."
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? ")))
204 ;; Make the translation list
205 (let* ((re-core (concat "\\("
206 (mapconcat 'cdr reftex-typekey-to-prefix-alist
"\\|")
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.
244 (format "Replace %d items at %d places in %d files? "
245 (length translate-alist
) n
(length files
)))
247 (let ((inhibit-quit t
)) ;; Do not disturb...
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))
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
))
270 (error "No such file %s" file
))
275 (goto-char (point-min))
276 (while (re-search-forward search-re nil t
)
279 (setq label
(reftex-match-string 1)
280 cell
(assoc label translate-alist
)
281 match-data
(match-data)
282 macro
(reftex-what-macro 1)
284 (goto-char (or pos
(point)))
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]")
290 reftex-find-label-regexp-format
291 (regexp-quote label
)))))
292 ;; OK, we should replace it.
293 (set-match-data match-data
)
295 ((and test
(not cell
))
296 ;; We've got a problem
299 (reftex-highlight 1 (match-beginning 0) (match-end 0))
301 (or (y-or-n-p (format error-fmt label
))
303 (reftex-unhighlight 1)))
306 ((and (not test
) cell
)
308 (goto-char (match-beginning 1))
309 (delete-region (match-beginning 1) (match-end 1))
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
320 (let ((files (reftex-all-document-files))
323 (while (setq file
(pop files
))
324 (setq buffer
(reftex-get-buffer-visiting file
))
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."
333 (while (setq file
(pop files
))
334 (unless (file-exists-p file
)
336 (or (y-or-n-p (format "No such file %s. Continue? " file
))
338 (unless (file-writable-p file
)
340 (or (y-or-n-p (format "No write access to %s. Continue? " file
))
342 (when (and (setq buf
(reftex-get-buffer-visiting file
))
343 (with-current-buffer buf
346 (or (y-or-n-p (format "Buffer %s is read-only. Continue? "
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 ()
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 ()
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
)
372 (search-fun (isearch-search-fun-default)))
374 (funcall search-fun string bound noerror
)
377 (when isearch-next-buffer-function
378 (while (not (funcall search-fun string bound noerror
))
382 (funcall isearch-next-buffer-function
386 (error "Wrap forward"))
387 (switch-to-buffer nxt-buff
)
388 (goto-char (point-min))))
391 (funcall isearch-next-buffer-function
395 (error "Wrap backward"))
396 (switch-to-buffer nxt-buff
)
397 (goto-char (point-max))))))
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)))
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
)))
423 (find-file-noselect (car flist
))))))
425 ;; NB this is a global autoload - see reftex.el.
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."
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
443 (dolist (crt-buf (buffer-list))
444 (with-current-buffer crt-buf
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
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
480 ;; generated-autoload-file: "reftex-loaddefs.el"