Mercurial > emacs
annotate lisp/textmodes/reftex-global.el @ 27004:99e734c7df9b
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Mon, 27 Dec 1999 12:27:32 +0000 |
parents | 489a5439b988 |
children | 083669ed376a |
rev | line source |
---|---|
25280 | 1 ;;; reftex-global.el - Operations on entire documents with RefTeX |
26910
489a5439b988
* reftex.el (reftex-compile-variables): respect new structure of
Carsten Dominik <dominik@science.uva.nl>
parents:
25806
diff
changeset
|
2 ;;; Version: 4.9 |
25280 | 3 ;;; |
4 ;;; See main file reftex.el for licensing information | |
5 | |
26910
489a5439b988
* reftex.el (reftex-compile-variables): respect new structure of
Carsten Dominik <dominik@science.uva.nl>
parents:
25806
diff
changeset
|
6 (eval-when-compile (require 'cl)) |
25280 | 7 (provide 'reftex-global) |
8 (require 'reftex) | |
9 ;;; | |
10 | |
11 (defun reftex-create-tags-file () | |
12 "Create TAGS file by running `etags' on the current document. | |
13 The TAGS file is also immediately visited with `visit-tags-table'." | |
14 (interactive) | |
15 (reftex-access-scan-info current-prefix-arg) | |
16 (let* ((master (reftex-TeX-master-file)) | |
17 (files (reftex-all-document-files)) | |
18 (cmd (format "etags %s" (mapconcat 'identity files " ")))) | |
19 (save-excursion | |
20 (set-buffer (reftex-get-buffer-visiting master)) | |
21 (message "Running etags to create TAGS file...") | |
22 (shell-command cmd) | |
23 (visit-tags-table "TAGS")))) | |
24 | |
25 ;; History of grep commands. | |
26 (defvar reftex-grep-history nil) | |
27 (defvar reftex-grep-command "grep -n " | |
28 "Last grep command used in \\[reftex-grep-document]; default for next grep.") | |
29 | |
30 (defun reftex-grep-document (grep-cmd) | |
31 "Run grep query through all files related to this document. | |
32 With prefix arg, force to rescan document. | |
33 No active TAGS table is required." | |
34 | |
35 (interactive | |
36 (list (read-from-minibuffer "Run grep on document (like this): " | |
37 reftex-grep-command nil nil | |
38 'reftex-grep-history))) | |
39 (reftex-access-scan-info current-prefix-arg) | |
40 (let* ((files (reftex-all-document-files t)) | |
41 (cmd (format | |
42 "%s %s" grep-cmd | |
43 (mapconcat 'identity files " ")))) | |
44 (grep cmd))) | |
45 | |
46 (defun reftex-search-document (&optional regexp) | |
47 "Regexp search through all files of the current document. | |
48 Starts always in the master file. Stops when a match is found. | |
49 To continue searching for next match, use command \\[tags-loop-continue]. | |
50 No active TAGS table is required." | |
51 (interactive) | |
52 (let ((default (reftex-this-word))) | |
53 (unless regexp | |
54 (setq regexp (read-string (format "Search regexp in document [%s]: " | |
55 default)))) | |
56 (if (string= regexp "") (setq regexp (regexp-quote default))) | |
57 | |
58 (reftex-access-scan-info current-prefix-arg) | |
59 (tags-search regexp (list 'reftex-all-document-files)))) | |
60 | |
61 (defun reftex-query-replace-document (&optional from to delimited) | |
62 "Run a query-replace-regexp of FROM with TO over the entire document. | |
63 Third arg DELIMITED (prefix arg) means replace only word-delimited matches. | |
64 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace | |
65 with the command \\[tags-loop-continue]. | |
66 No active TAGS table is required." | |
67 (interactive) | |
68 (let ((default (reftex-this-word))) | |
69 (unless from | |
70 (setq from (read-string (format "Replace regexp in document [%s]: " | |
71 default))) | |
72 (if (string= from "") (setq from (regexp-quote default)))) | |
73 (unless to | |
74 (setq to (read-string (format "Replace regexp %s with: " from)))) | |
75 (reftex-access-scan-info current-prefix-arg) | |
76 (tags-query-replace from to (or delimited current-prefix-arg) | |
77 (list 'reftex-all-document-files)))) | |
78 | |
79 (defun reftex-find-duplicate-labels () | |
80 "Produce a list of all duplicate labels in the document." | |
81 | |
82 (interactive) | |
83 | |
84 ;; Rescan the document to make sure | |
85 (reftex-access-scan-info t) | |
86 | |
87 (let ((master (reftex-TeX-master-file)) | |
88 (cnt 0) | |
89 (dlist | |
90 (mapcar | |
91 (lambda (x) | |
92 (let (x1) | |
93 (cond | |
94 ((memq (car x) | |
95 '(toc bof eof bib thebib label-numbers xr xr-doc | |
96 master-dir file-error bibview-cache appendix | |
97 is-multi index)) | |
98 nil) | |
99 (t | |
100 (setq x1 (reftex-all-assoc-string | |
101 (car x) (symbol-value reftex-docstruct-symbol))) | |
102 (if (< 1 (length x1)) | |
103 (append (list (car x)) | |
104 (mapcar (lambda(x) | |
105 (abbreviate-file-name (nth 3 x))) | |
106 x1)) | |
107 (list nil)))))) | |
108 (reftex-uniquify-by-car (symbol-value reftex-docstruct-symbol))))) | |
109 | |
110 (setq dlist (reftex-uniquify-by-car dlist)) | |
111 (if (null dlist) (error "No duplicate labels in document")) | |
112 (switch-to-buffer-other-window "*Duplicate Labels*") | |
113 (set (make-local-variable 'TeX-master) master) | |
114 (erase-buffer) | |
115 (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n") | |
116 (insert | |
117 " Move point to label and type `r' to run a query-replace on the label\n" | |
118 " and its references. Type `q' to exit this buffer.\n\n") | |
119 (insert " LABEL FILE\n") | |
120 (insert " -------------------------------------------------------------\n") | |
121 (use-local-map (make-sparse-keymap)) | |
122 (local-set-key [?q] (lambda () "Kill this buffer." (interactive) | |
123 (kill-buffer (current-buffer)) (delete-window))) | |
124 (local-set-key [?r] 'reftex-change-label) | |
125 (while dlist | |
126 (when (and (car (car dlist)) | |
127 (cdr (car dlist))) | |
128 (incf cnt) | |
129 (insert (mapconcat 'identity (car dlist) "\n ") "\n")) | |
130 (pop dlist)) | |
131 (goto-char (point-min)) | |
132 (when (= cnt 0) | |
133 (kill-buffer (current-buffer)) | |
134 (delete-window) | |
135 (message "Document does not contain duplicate labels.")))) | |
136 | |
137 (defun reftex-change-label (&optional from to) | |
138 "Query replace FROM with TO in all \\label and \\ref commands. | |
139 Works on the entire multifile document. | |
140 If you exit (\\[keyboard-quit] or ESC), you can resume the query replace | |
141 with the command \\[tags-loop-continue]. | |
142 No active TAGS table is required." | |
143 (interactive) | |
144 (let ((default (reftex-this-word "-a-zA-Z0-9_*.:"))) | |
145 (unless from | |
146 (setq from (read-string (format "Replace label globally [%s]: " | |
147 default)))) | |
148 (if (string= from "") (setq from default)) | |
149 (unless to | |
150 (setq to (read-string (format "Replace label %s with: " | |
151 from)))) | |
152 (reftex-query-replace-document | |
153 (concat "\\\\\\(label\\|[a-z]*ref\\){" (regexp-quote from) "}") | |
154 (format "\\\\\\1{%s}" to)))) | |
155 | |
156 (defun reftex-renumber-simple-labels () | |
157 "Renumber all simple labels in the document to make them sequentially. | |
158 Simple labels are the ones created by RefTeX, consisting only of the | |
159 prefix and a number. After the command completes, all these labels will | |
160 have sequential numbers throughout the document. Any references to | |
161 the labels will be changed as well. For this, RefTeX looks at the | |
162 arguments of any macros which either start or end in the string `ref'. | |
163 This command should be used with care, in particular in multifile | |
164 documents. You should not use it if another document refers to this | |
165 one with the `xr' package." | |
166 (interactive) | |
167 ;; Resan the entire document | |
168 (reftex-access-scan-info 1) | |
169 ;; Get some insurance | |
170 (if (and (reftex-is-multi) | |
171 (not (yes-or-no-p "Replacing all simple labels in multiple files is risky. Continue? "))) | |
172 (error "Abort")) | |
173 ;; Make the translation list | |
174 (let* ((re-core (concat "\\(" | |
175 (mapconcat 'cdr reftex-typekey-to-prefix-alist "\\|") | |
176 "\\)")) | |
177 (label-re (concat "\\`" re-core "\\([0-9]+\\)\\'")) | |
178 (search-re (concat "[{,]\\(" re-core "\\([0-9]+\\)\\)[,}]")) | |
179 (error-fmt "Undefined label or reference %s. Ignore and continue? ") | |
180 (label-numbers-alist (mapcar (lambda (x) (cons (cdr x) 0)) | |
181 reftex-typekey-to-prefix-alist)) | |
182 (files (reftex-all-document-files)) | |
183 (list (symbol-value reftex-docstruct-symbol)) | |
184 translate-alist n entry label new-label nr-cell changed-sequence) | |
185 | |
186 (while (setq entry (pop list)) | |
187 (when (and (stringp (car entry)) | |
188 (string-match label-re (car entry))) | |
189 (setq label (car entry) | |
190 nr-cell (assoc (match-string 1 (car entry)) | |
191 label-numbers-alist)) | |
192 (if (assoc label translate-alist) | |
193 (error "Duplicate label %s" label)) | |
194 (setq new-label (concat (match-string 1 (car entry)) | |
26910
489a5439b988
* reftex.el (reftex-compile-variables): respect new structure of
Carsten Dominik <dominik@science.uva.nl>
parents:
25806
diff
changeset
|
195 (int-to-string (incf (cdr nr-cell))))) |
25280 | 196 (push (cons label new-label) translate-alist) |
197 (or (string= label new-label) (setq changed-sequence t)))) | |
198 | |
199 (unless changed-sequence | |
200 (error "Simple labels are already in correct sequence")) | |
201 | |
202 ;; Save all document buffers before this operation | |
203 (reftex-save-all-document-buffers) | |
204 | |
205 ;; First test to check for erros | |
206 (setq n (reftex-translate | |
207 files search-re translate-alist error-fmt 'test)) | |
208 | |
209 ;; Now the real thing. | |
210 (if (yes-or-no-p | |
211 (format "Replace %d items at %d places in %d files? " | |
212 (length translate-alist) n (length files))) | |
213 (progn | |
214 (let ((inhibit-quit t)) ;; Do not disturb... | |
215 (reftex-translate | |
216 files search-re translate-alist error-fmt nil) | |
217 (setq quit-flag nil)) | |
218 (if (and (reftex-is-multi) | |
219 (yes-or-no-p "Save entire document? ")) | |
220 (reftex-save-all-document-buffers)) | |
221 ;; Rescan again... | |
222 (reftex-access-scan-info 1) | |
223 (message "Done replacing simple labels.")) | |
224 (message "No replacements done")))) | |
225 | |
226 (defun reftex-translate (files search-re translate-alist error-fmt test) | |
227 ;; In FILES, look for SEARCH-RE and replace match 1 of it with | |
228 ;; its association in TRANSLATE-ALSIT. | |
229 ;; If we do not find an association and TEST is non-nil, query | |
230 ;; to ignore the problematic string. | |
231 ;; If TEST is nil, it is ignored without query. | |
232 ;; Return the number of replacements. | |
233 (let ((n 0) file label match-data buf macro pos cell) | |
234 (while (setq file (pop files)) | |
235 (setq buf (reftex-get-file-buffer-force file)) | |
236 (unless buf | |
237 (error "No such file %s" file)) | |
238 (set-buffer buf) | |
239 (save-excursion | |
240 (save-restriction | |
241 (widen) | |
242 (goto-char (point-min)) | |
243 (while (re-search-forward search-re nil t) | |
244 (backward-char) | |
245 (save-excursion | |
246 (setq label (reftex-match-string 1) | |
247 cell (assoc label translate-alist) | |
248 match-data (match-data) | |
249 macro (reftex-what-macro 1) | |
250 pos (cdr macro)) | |
251 (goto-char (or pos (point))) | |
252 (when (and macro | |
253 (or (looking-at "\\\\ref") | |
254 (looking-at "\\\\[a-zA-Z]*ref\\(range\\)?[^a-zA-Z]") | |
255 (looking-at "\\\\ref[a-zA-Z]*[^a-zA-Z]") | |
256 (looking-at (format | |
257 reftex-find-label-regexp-format | |
258 (regexp-quote label))))) | |
259 ;; OK, we should replace it. | |
260 (set-match-data match-data) | |
261 (cond | |
262 ((and test (not cell)) | |
263 ;; We've got a problem | |
264 (unwind-protect | |
265 (progn | |
266 (reftex-highlight 1 (match-beginning 0) (match-end 0)) | |
267 (ding) | |
268 (or (y-or-n-p (format error-fmt label)) | |
269 (error "Abort"))) | |
270 (reftex-unhighlight 1))) | |
271 ((and test cell) | |
272 (incf n)) | |
273 ((and (not test) cell) | |
274 ;; Replace | |
275 (goto-char (match-beginning 1)) | |
276 (delete-region (match-beginning 1) (match-end 1)) | |
277 (insert (cdr cell))) | |
278 (t nil)))))))) | |
279 n)) | |
280 | |
281 (defun reftex-save-all-document-buffers () | |
282 "Save all documents associated with the current document. | |
283 The function is useful after a global action like replacing or renumbering | |
284 labels." | |
285 (interactive) | |
286 (let ((files (reftex-all-document-files)) | |
287 file buffer) | |
288 (save-excursion | |
289 (while (setq file (pop files)) | |
290 (setq buffer (reftex-get-buffer-visiting file)) | |
291 (when buffer | |
292 (set-buffer buffer) | |
293 (save-buffer)))))) | |
294 | |
295 | |
296 ;;; reftex-global.el ends here |