1 ;;; reftex-global.el - Operations on entire documents with RefTeX
2 ;; Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 (eval-when-compile (require 'cl))
26 (provide 'reftex-global)
30 (defun reftex-create-tags-file ()
31 "Create TAGS file by running `etags' on the current document.
32 The TAGS file is also immediately visited with `visit-tags-table'."
34 (reftex-access-scan-info current-prefix-arg)
35 (let* ((master (reftex-TeX-master-file))
36 (files (reftex-all-document-files))
37 (cmd (format "etags %s" (mapconcat 'identity files " "))))
39 (set-buffer (reftex-get-buffer-visiting master))
40 (message "Running etags to create TAGS file...")
42 (visit-tags-table "TAGS"))))
44 ;; History of grep commands.
45 (defvar reftex-grep-history nil)
46 (defvar reftex-grep-command "grep -n "
47 "Last grep command used in \\[reftex-grep-document]; default for next grep.")
49 (defun reftex-grep-document (grep-cmd)
50 "Run grep query through all files related to this document.
51 With prefix arg, force to rescan document.
52 No active TAGS table is required."
55 (list (read-from-minibuffer "Run grep on document (like this): "
56 reftex-grep-command nil nil
57 'reftex-grep-history)))
58 (reftex-access-scan-info current-prefix-arg)
59 (let* ((files (reftex-all-document-files t))
62 (mapconcat 'identity files " "))))
65 (defun reftex-search-document (&optional regexp)
66 "Regexp search through all files of the current document.
67 Starts always in the master file. Stops when a match is found.
68 To continue searching for next match, use command \\[tags-loop-continue].
69 No active TAGS table is required."
71 (let ((default (reftex-this-word)))
73 (setq regexp (read-string (format "Search regexp in document [%s]: "
75 (if (string= regexp "") (setq regexp (regexp-quote default)))
77 (reftex-access-scan-info current-prefix-arg)
78 (tags-search regexp (list 'reftex-all-document-files))))
80 (defun reftex-query-replace-document (&optional from to delimited)
81 "Run a query-replace-regexp of FROM with TO over the entire document.
82 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
83 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
84 with the command \\[tags-loop-continue].
85 No active TAGS table is required."
87 (let ((default (reftex-this-word)))
89 (setq from (read-string (format "Replace regexp in document [%s]: "
91 (if (string= from "") (setq from (regexp-quote default))))
93 (setq to (read-string (format "Replace regexp %s with: " from))))
94 (reftex-access-scan-info current-prefix-arg)
95 (tags-query-replace from to (or delimited current-prefix-arg)
96 (list 'reftex-all-document-files))))
98 (defun reftex-find-duplicate-labels ()
99 "Produce a list of all duplicate labels in the document."
103 ;; Rescan the document to make sure
104 (reftex-access-scan-info t)
106 (let ((master (reftex-TeX-master-file))
114 '(toc bof eof bib thebib label-numbers xr xr-doc
115 master-dir file-error bibview-cache appendix
119 (setq x1 (reftex-all-assoc-string
120 (car x) (symbol-value reftex-docstruct-symbol)))
121 (if (< 1 (length x1))
122 (append (list (car x))
124 (abbreviate-file-name (nth 3 x)))
127 (reftex-uniquify-by-car (symbol-value reftex-docstruct-symbol)))))
129 (setq dlist (reftex-uniquify-by-car dlist))
130 (if (null dlist) (error "No duplicate labels in document"))
131 (switch-to-buffer-other-window "*Duplicate Labels*")
132 (set (make-local-variable 'TeX-master) master)
134 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n")
136 " Move point to label and type `r' to run a query-replace on the label\n"
137 " and its references. Type `q' to exit this buffer.\n\n")
138 (insert " LABEL FILE\n")
139 (insert " -------------------------------------------------------------\n")
140 (use-local-map (make-sparse-keymap))
141 (local-set-key [?q] (lambda () "Kill this buffer." (interactive)
142 (kill-buffer (current-buffer)) (delete-window)))
143 (local-set-key [?r] 'reftex-change-label)
145 (when (and (car (car dlist))
148 (insert (mapconcat 'identity (car dlist) "\n ") "\n"))
150 (goto-char (point-min))
152 (kill-buffer (current-buffer))
154 (message "Document does not contain duplicate labels."))))
156 (defun reftex-change-label (&optional from to)
157 "Query replace FROM with TO in all \\label and \\ref commands.
158 Works on the entire multifile document.
159 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace
160 with the command \\[tags-loop-continue].
161 No active TAGS table is required."
163 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:")))
165 (setq from (read-string (format "Replace label globally [%s]: "
167 (if (string= from "") (setq from default))
169 (setq to (read-string (format "Replace label %s with: "
171 (reftex-query-replace-document
172 (concat "\\\\\\(label\\|[a-zA-Z]*ref\\){" (regexp-quote from) "}")
173 (format "\\\\\\1{%s}" to))))
175 (defun reftex-renumber-simple-labels ()
176 "Renumber all simple labels in the document to make them sequentially.
177 Simple labels are the ones created by RefTeX, consisting only of the
178 prefix and a number. After the command completes, all these labels will
179 have sequential numbers throughout the document. Any references to
180 the labels will be changed as well. For this, RefTeX looks at the
181 arguments of any macros which either start or end in the string `ref'.
182 This command should be used with care, in particular in multifile
183 documents. You should not use it if another document refers to this
184 one with the `xr' package."
186 ;; Resan the entire document
187 (reftex-access-scan-info 1)
188 ;; Get some insurance
189 (if (and (reftex-is-multi)
190 (not (yes-or-no-p "Replacing all simple labels in multiple files is risky. Continue? ")))
192 ;; Make the translation list
193 (let* ((re-core (concat "\\("
194 (mapconcat 'cdr reftex-typekey-to-prefix-alist "\\|")
196 (label-re (concat "\\`" re-core "\\([0-9]+\\)\\'"))
197 (search-re (concat "[{,]\\(" re-core "\\([0-9]+\\)\\)[,}]"))
198 (error-fmt "Undefined label or reference %s. Ignore and continue? ")
199 (label-numbers-alist (mapcar (lambda (x) (cons (cdr x) 0))
200 reftex-typekey-to-prefix-alist))
201 (files (reftex-all-document-files))
202 (list (symbol-value reftex-docstruct-symbol))
203 translate-alist n entry label new-label nr-cell changed-sequence)
205 (while (setq entry (pop list))
206 (when (and (stringp (car entry))
207 (string-match label-re (car entry)))
208 (setq label (car entry)
209 nr-cell (assoc (match-string 1 (car entry))
210 label-numbers-alist))
211 (if (assoc label translate-alist)
212 (error "Duplicate label %s" label))
213 (setq new-label (concat (match-string 1 (car entry))
214 (int-to-string (incf (cdr nr-cell)))))
215 (push (cons label new-label) translate-alist)
216 (or (string= label new-label) (setq changed-sequence t))))
218 (unless changed-sequence
219 (error "Simple labels are already in correct sequence"))
221 (reftex-ensure-write-access (reftex-all-document-files))
223 ;; Save all document buffers before this operation
224 (reftex-save-all-document-buffers)
226 ;; First test to check for erros
227 (setq n (reftex-translate
228 files search-re translate-alist error-fmt 'test))
230 ;; Now the real thing.
232 (format "Replace %d items at %d places in %d files? "
233 (length translate-alist) n (length files)))
235 (let ((inhibit-quit t)) ;; Do not disturb...
237 files search-re translate-alist error-fmt nil)
238 (setq quit-flag nil))
239 (if (and (reftex-is-multi)
240 (yes-or-no-p "Save entire document? "))
241 (reftex-save-all-document-buffers))
243 (reftex-access-scan-info 1)
244 (message "Done replacing simple labels."))
245 (message "No replacements done"))))
247 (defun reftex-translate (files search-re translate-alist error-fmt test)
248 ;; In FILES, look for SEARCH-RE and replace match 1 of it with
249 ;; its association in TRANSLATE-ALSIT.
250 ;; If we do not find an association and TEST is non-nil, query
251 ;; to ignore the problematic string.
252 ;; If TEST is nil, it is ignored without query.
253 ;; Return the number of replacements.
254 (let ((n 0) file label match-data buf macro pos cell)
255 (while (setq file (pop files))
256 (setq buf (reftex-get-file-buffer-force file))
258 (error "No such file %s" file))
263 (goto-char (point-min))
264 (while (re-search-forward search-re nil t)
267 (setq label (reftex-match-string 1)
268 cell (assoc label translate-alist)
269 match-data (match-data)
270 macro (reftex-what-macro 1)
272 (goto-char (or pos (point)))
274 (or (looking-at "\\\\ref")
275 (looking-at "\\\\[a-zA-Z]*ref\\(range\\)?[^a-zA-Z]")
276 (looking-at "\\\\ref[a-zA-Z]*[^a-zA-Z]")
278 reftex-find-label-regexp-format
279 (regexp-quote label)))))
280 ;; OK, we should replace it.
281 (set-match-data match-data)
283 ((and test (not cell))
284 ;; We've got a problem
287 (reftex-highlight 1 (match-beginning 0) (match-end 0))
289 (or (y-or-n-p (format error-fmt label))
291 (reftex-unhighlight 1)))
294 ((and (not test) cell)
296 (goto-char (match-beginning 1))
297 (delete-region (match-beginning 1) (match-end 1))
302 (defun reftex-save-all-document-buffers ()
303 "Save all documents associated with the current document.
304 The function is useful after a global action like replacing or renumbering
307 (let ((files (reftex-all-document-files))
310 (while (setq file (pop files))
311 (setq buffer (reftex-get-buffer-visiting file))
316 (defun reftex-ensure-write-access (files)
317 "Make sure we have write access to all files in FILES.
318 Also checks if buffers visiting the files are in read-only mode."
320 (while (setq file (pop files))
321 (unless (file-exists-p file)
323 (or (y-or-n-p (format "No such file %s. Continue? " file))
325 (unless (file-writable-p file)
327 (or (y-or-n-p (format "No write access to %s. Continue? " file))
329 (when (and (setq buf (reftex-get-buffer-visiting file))
334 (or (y-or-n-p (format "Buffer %s is read-only. Continue? "
338 ;;; reftex-global.el ends here