1 ;;; reftex-global.el --- operations on entire documents with RefTeX
3 ;; Copyright (C) 1997-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: auctex-devel@gnu.org
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/>.
29 (eval-when-compile (require 'cl
))
30 (provide 'reftex-global
)
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'."
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
43 (with-current-buffer (reftex-get-file-buffer-force master
)
44 (message "Running etags to create TAGS file...")
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."
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
" "))))
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."
75 (let ((default (reftex-this-word)))
77 (setq regexp
(read-string (format "Search regexp in document [%s]: "
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."
91 (let ((default (reftex-this-word)))
93 (setq from
(read-string (format "Replace regexp in document [%s]: "
95 (if (string= from
"") (setq from
(regexp-quote default
))))
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
))))
103 (defvar isearch-next-buffer-function
)
105 (defun reftex-find-duplicate-labels ()
106 "Produce a list of all duplicate labels in the document."
110 ;; Rescan the document to make sure
111 (reftex-access-scan-info t
)
113 (let ((master (reftex-TeX-master-file))
121 '(toc bof eof bib thebib label-numbers xr xr-doc
122 master-dir file-error bibview-cache appendix
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
))
131 (abbreviate-file-name (nth 3 x
)))
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
)
141 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
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
)
152 (when (and (car (car dlist
))
155 (insert (mapconcat 'identity
(car dlist
) "\n ") "\n"))
157 (goto-char (point-min))
159 (kill-buffer (current-buffer))
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."
170 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
172 (setq from
(read-string (format "Replace label globally [%s]: "
174 (if (string= from
"") (setq from default
))
176 (setq to
(read-string (format "Replace label %s with: "
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."
193 ;; Resan 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? ")))
199 ;; Make the translation list
200 (let* ((re-core (concat "\\("
201 (mapconcat 'cdr reftex-typekey-to-prefix-alist
"\\|")
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 erros
234 (setq n
(reftex-translate
235 files search-re translate-alist error-fmt
'test
))
237 ;; Now the real thing.
239 (format "Replace %d items at %d places in %d files? "
240 (length translate-alist
) n
(length files
)))
242 (let ((inhibit-quit t
)) ;; Do not disturb...
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))
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-ALSIT.
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
))
265 (error "No such file %s" file
))
270 (goto-char (point-min))
271 (while (re-search-forward search-re nil t
)
274 (setq label
(reftex-match-string 1)
275 cell
(assoc label translate-alist
)
276 match-data
(match-data)
277 macro
(reftex-what-macro 1)
279 (goto-char (or pos
(point)))
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]")
285 reftex-find-label-regexp-format
286 (regexp-quote label
)))))
287 ;; OK, we should replace it.
288 (set-match-data match-data
)
290 ((and test
(not cell
))
291 ;; We've got a problem
294 (reftex-highlight 1 (match-beginning 0) (match-end 0))
296 (or (y-or-n-p (format error-fmt label
))
298 (reftex-unhighlight 1)))
301 ((and (not test
) cell
)
303 (goto-char (match-beginning 1))
304 (delete-region (match-beginning 1) (match-end 1))
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
314 (let ((files (reftex-all-document-files))
317 (while (setq file
(pop files
))
318 (setq buffer
(reftex-get-buffer-visiting file
))
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."
327 (while (setq file
(pop files
))
328 (unless (file-exists-p file
)
330 (or (y-or-n-p (format "No such file %s. Continue? " file
))
332 (unless (file-writable-p file
)
334 (or (y-or-n-p (format "No write access to %s. Continue? " file
))
336 (when (and (setq buf
(reftex-get-buffer-visiting file
))
337 (with-current-buffer buf
340 (or (y-or-n-p (format "Buffer %s is read-only. Continue? "
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 (if (not isearch-word
)
355 (funcall isearch-next-buffer-function
(current-buffer) t
)))
356 (goto-char (if isearch-forward
(point-min) (point-max))))
358 (defun reftex-isearch-push-state-function ()
360 (reftex-isearch-pop-state-function cmd
,(current-buffer))))
362 (defun reftex-isearch-pop-state-function (cmd buffer
)
363 (switch-to-buffer buffer
))
365 (defun reftex-isearch-isearch-search (string bound noerror
)
370 (if isearch-forward
'word-search-forward
'word-search-backward
))
372 (if isearch-forward
're-search-forward
're-search-backward
))
374 (if isearch-forward
'search-forward
'search-backward
)))))
376 (funcall search-fun string bound noerror
)
379 (when isearch-next-buffer-function
380 (while (not (funcall search-fun string bound noerror
))
384 (funcall isearch-next-buffer-function
388 (error "Wrap forward"))
389 (switch-to-buffer nxt-buff
)
390 (goto-char (point-min))))
393 (funcall isearch-next-buffer-function
397 (error "Wrap backward"))
398 (switch-to-buffer nxt-buff
)
399 (goto-char (point-max))))))
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.
411 (defun reftex-isearch-switch-to-next-file (crt-buf &optional wrapp
)
412 (reftex-access-scan-info)
413 (let ((cb (buffer-file-name crt-buf
))
414 (flist (reftex-all-document-files)))
417 (unless isearch-forward
418 (setq flist
(last flist
)))
419 (unless isearch-forward
420 (setq flist
(reverse flist
)))
421 (while (not (string= (car flist
) cb
))
422 (setq flist
(cdr flist
)))
423 (setq flist
(cdr flist
)))
425 (find-file-noselect (car flist
))))))
428 (defun reftex-isearch-minor-mode (&optional arg
)
429 "When on, isearch searches the whole document, not only the current file.
430 This minor mode allows isearch to search through all the files of
431 the current TeX document.
433 With no argument, this command toggles
434 `reftex-isearch-minor-mode'. With a prefix argument ARG, turn
435 `reftex-isearch-minor-mode' on if ARG is positive, otherwise turn it off."
437 (let ((old-reftex-isearch-minor-mode reftex-isearch-minor-mode
))
438 (setq reftex-isearch-minor-mode
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
444 (dolist (crt-buf (buffer-list))
445 (with-current-buffer crt-buf
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
))
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 modeline 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