Mercurial > emacs
annotate lisp/textmodes/reftex-parse.el @ 26581:b679911f6f28
*** empty log message ***
author | Dave Love <fx@gnu.org> |
---|---|
date | Wed, 24 Nov 1999 20:03:27 +0000 |
parents | 8b032ca83ce6 |
children | 489a5439b988 |
rev | line source |
---|---|
25280 | 1 ;;; reftex-parse.el - Parser Functions for RefTeX |
25803
8b032ca83ce6
(reftex-parse-from-file): Scan for multiple thebibliography
Carsten Dominik <dominik@science.uva.nl>
parents:
25280
diff
changeset
|
2 ;;; Version: 4.6 |
25280 | 3 ;;; |
4 ;;; See main file reftex.el for licensing information | |
5 | |
6 (provide 'reftex-parse) | |
7 (require 'reftex) | |
8 | |
9 (defmacro reftex-with-special-syntax (&rest body) | |
10 `(let ((saved-syntax (syntax-table))) | |
11 (unwind-protect | |
12 (progn | |
13 (set-syntax-table reftex-syntax-table) | |
14 ,@body) | |
15 (set-syntax-table saved-syntax)))) | |
16 | |
17 (defun reftex-parse-one () | |
18 "Re-parse this file." | |
19 (interactive) | |
20 (let ((reftex-enable-partial-scans t)) | |
21 (reftex-access-scan-info '(4)))) | |
22 | |
23 (defun reftex-parse-all () | |
24 "Re-parse entire document." | |
25 (interactive) | |
26 (reftex-access-scan-info '(16))) | |
27 | |
28 (defun reftex-do-parse (rescan &optional file) | |
29 "Do a document rescan. When allowed, do only a partial scan from FILE." | |
30 | |
31 ;; Normalize the rescan argument | |
32 (setq rescan (cond ((eq rescan t) t) | |
33 ((eq rescan 1) 1) | |
34 ((equal rescan '(4)) t) | |
35 ((equal rescan '(16)) 1) | |
36 (t 1))) | |
37 | |
38 ;; Partial scans only when allowed | |
39 (unless reftex-enable-partial-scans | |
40 (setq rescan 1)) | |
41 | |
42 ;; Do the scanning. | |
43 | |
44 (let* ((old-list (symbol-value reftex-docstruct-symbol)) | |
45 (master (reftex-TeX-master-file)) | |
46 (true-master (file-truename master)) | |
47 (master-dir (file-name-as-directory (file-name-directory master))) | |
48 (file (or file (buffer-file-name))) | |
49 (true-file (file-truename file)) | |
50 (bibview-cache (assq 'bibview-cache old-list)) | |
51 (index-tags (cdr (assq 'index-tags old-list))) | |
52 from-file appendix docstruct tmp) | |
53 | |
54 ;; Make sure replacement is really an option here | |
55 (when (and (eq rescan t) | |
56 (not (and (member (list 'bof file) old-list) | |
57 (member (list 'eof file) old-list)))) | |
58 ;; Scan whole document because no such file section exists | |
59 (setq rescan 1)) | |
60 (when (string= true-file true-master) | |
61 ;; Scan whole document because this file is the master | |
62 (setq rescan 1)) | |
63 | |
64 ;; From which file do we start? | |
65 (setq from-file | |
66 (cond ((eq rescan t) (or file master)) | |
67 ((eq rescan 1) master) | |
68 (t (error "This should not happen (reftex-do-parse)")))) | |
69 | |
70 ;; Reset index-tags if we scan everything | |
71 (if (equal rescan 1) (setq index-tags nil)) | |
72 | |
73 ;; Find active toc entry and initialize section-numbers | |
74 (setq reftex-active-toc (reftex-last-assoc-before-elt | |
75 'toc (list 'bof from-file) old-list) | |
76 appendix (reftex-last-assoc-before-elt | |
77 'appendix (list 'bof from-file) old-list)) | |
78 | |
79 (reftex-init-section-numbers reftex-active-toc appendix) | |
80 | |
81 (if (eq rescan 1) | |
82 (message "Scanning entire document...") | |
83 (message "Scanning document from %s..." from-file)) | |
84 | |
85 (reftex-with-special-syntax | |
86 (save-window-excursion | |
87 (save-excursion | |
88 (unwind-protect | |
89 (setq docstruct | |
90 (reftex-parse-from-file | |
91 from-file docstruct master-dir)) | |
92 (reftex-kill-temporary-buffers))))) | |
93 | |
94 (message "Scanning document... done") | |
95 | |
96 ;; Turn the list around. | |
97 (setq docstruct (nreverse docstruct)) | |
98 | |
99 ;; Set or insert | |
100 (setq docstruct (reftex-replace-label-list-segment | |
101 old-list docstruct (eq rescan 1))) | |
102 | |
103 ;; Add all missing information | |
104 (unless (assq 'label-numbers docstruct) | |
105 (push (cons 'label-numbers nil) docstruct)) | |
106 (unless (assq 'master-dir docstruct) | |
107 (push (cons 'master-dir master-dir) docstruct)) | |
108 (unless (assq 'bibview-cache docstruct) | |
109 (push (cons 'bibview-cache (cdr bibview-cache)) docstruct)) | |
110 (let* ((bof1 (memq (assq 'bof docstruct) docstruct)) | |
111 (bof2 (assq 'bof (cdr bof1))) | |
112 (is-multi (not (not (and bof1 bof2)))) | |
113 (entry (or (assq 'is-multi docstruct) | |
114 (car (push (list 'is-multi is-multi) docstruct))))) | |
115 (setcdr entry (cons is-multi nil))) | |
116 (and index-tags (setq index-tags (sort index-tags 'string<))) | |
117 (let ((index-tag-cell (assq 'index-tags docstruct))) | |
118 (if index-tag-cell | |
119 (setcdr index-tag-cell index-tags) | |
120 (push (cons 'index-tags index-tags) docstruct))) | |
121 (unless (assq 'xr docstruct) | |
122 (let* ((allxr (reftex-all-assq 'xr-doc docstruct)) | |
123 (alist (mapcar | |
124 (lambda (x) | |
125 (if (setq tmp (reftex-locate-file (nth 2 x) "tex" | |
126 master-dir)) | |
127 (cons (nth 1 x) tmp) | |
128 (message "Can't find external document %s" | |
129 (nth 2 x)) | |
130 nil)) | |
131 allxr)) | |
132 (alist (delq nil alist)) | |
133 (allprefix (delq nil (mapcar 'car alist))) | |
134 (regexp (if allprefix | |
135 (concat "\\`\\(" | |
136 (mapconcat 'identity allprefix "\\|") | |
137 "\\)") | |
138 "\\\\\\\\\\\\"))) ; this will never match | |
139 (push (list 'xr alist regexp) docstruct))) | |
140 | |
141 (set reftex-docstruct-symbol docstruct) | |
142 (put reftex-docstruct-symbol 'modified t))) | |
143 | |
144 (defun reftex-everything-regexp () | |
145 (if reftex-support-index | |
146 reftex-everything-regexp | |
147 reftex-everything-regexp-no-index)) | |
148 | |
149 (defun reftex-all-document-files (&optional relative) | |
150 "Return a list of all files belonging to the current document. | |
151 When RELATIVE is non-nil, give file names relative to directory | |
152 of master file." | |
153 (let* ((all (symbol-value reftex-docstruct-symbol)) | |
154 (master-dir (file-name-directory (reftex-TeX-master-file))) | |
155 (re (concat "\\`" (regexp-quote master-dir))) | |
156 file-list tmp file) | |
157 (while (setq tmp (assoc 'bof all)) | |
158 (setq file (nth 1 tmp) | |
159 all (cdr (memq tmp all))) | |
160 (and relative | |
161 (string-match re file) | |
162 (setq file (substring file (match-end 0)))) | |
163 (push file file-list)) | |
164 (nreverse file-list))) | |
165 | |
166 (defun reftex-parse-from-file (file docstruct master-dir) | |
167 ;; Scan the buffer for labels and save them in a list. | |
168 (let ((regexp (reftex-everything-regexp)) | |
169 (bound 0) | |
170 file-found tmp include-file | |
171 (level 1) | |
172 (highest-level 100) | |
173 toc-entry index-entry next-buf buf) | |
174 | |
175 (catch 'exit | |
176 (setq file-found (reftex-locate-file file "tex" master-dir)) | |
177 (if (and (not file-found) | |
178 (setq buf (reftex-get-buffer-visiting file))) | |
179 (setq file-found (buffer-file-name buf))) | |
180 | |
181 (unless file-found | |
182 (push (list 'file-error file) docstruct) | |
183 (throw 'exit nil)) | |
184 | |
185 (save-excursion | |
186 | |
187 (message "Scanning file %s" file) | |
188 (set-buffer | |
189 (setq next-buf | |
190 (reftex-get-file-buffer-force | |
191 file-found | |
192 (not (eq t reftex-keep-temporary-buffers))))) | |
193 | |
194 ;; Begin of file mark | |
195 (setq file (buffer-file-name)) | |
196 (push (list 'bof file) docstruct) | |
197 | |
198 (reftex-with-special-syntax | |
199 (save-excursion | |
200 (save-restriction | |
201 (widen) | |
202 (goto-char 1) | |
203 | |
204 (while (re-search-forward regexp nil t) | |
205 | |
206 (cond | |
207 | |
208 ((match-end 1) | |
209 ;; It is a label | |
210 (push (reftex-label-info (reftex-match-string 1) file bound) | |
211 docstruct)) | |
212 | |
213 ((match-end 3) | |
214 ;; It is a section | |
215 (setq bound (point)) | |
216 | |
217 ;; Insert in List | |
218 (setq toc-entry (reftex-section-info file)) | |
219 (setq level (nth 5 toc-entry)) | |
220 (setq highest-level (min highest-level level)) | |
221 (if (= level highest-level) | |
222 (message | |
223 "Scanning %s %s ..." | |
224 (car (rassoc level reftex-section-levels-all)) | |
225 (nth 6 toc-entry))) | |
226 | |
227 (push toc-entry docstruct) | |
228 (setq reftex-active-toc toc-entry)) | |
229 | |
230 ((match-end 7) | |
231 ;; It's an include or input | |
232 (setq include-file (reftex-match-string 7)) | |
233 ;; Test if this file should be ignored | |
234 (unless (delq nil (mapcar | |
235 (lambda (x) (string-match x include-file)) | |
236 reftex-no-include-regexps)) | |
237 ;; Parse it | |
238 (setq docstruct | |
239 (reftex-parse-from-file | |
240 include-file | |
241 docstruct master-dir)))) | |
242 | |
243 ((match-end 9) | |
244 ;; Appendix starts here | |
245 (reftex-init-section-numbers nil t) | |
246 (push (cons 'appendix t) docstruct)) | |
247 | |
248 ((match-end 10) | |
249 ;; Index entry | |
250 (when reftex-support-index | |
251 (setq index-entry (reftex-index-info file)) | |
252 (when index-entry | |
253 (add-to-list 'index-tags (nth 1 index-entry)) | |
254 (push index-entry docstruct)))) | |
255 | |
256 ((match-end 11) | |
257 ;; A macro with label | |
258 (save-excursion | |
259 (let* ((mac (reftex-match-string 11)) | |
260 (label (progn (goto-char (match-end 11)) | |
261 (save-match-data | |
262 (reftex-no-props | |
263 (reftex-nth-arg-wrapper | |
264 mac))))) | |
265 (typekey (nth 1 (assoc mac reftex-env-or-mac-alist))) | |
266 (entry (progn (if typekey | |
267 ;; A typing macro | |
268 (goto-char (match-end 0)) | |
269 ;; A neutral macro | |
270 (goto-char (match-end 11)) | |
271 (reftex-move-over-touching-args)) | |
272 (reftex-label-info | |
273 label file bound nil nil)))) | |
274 (push entry docstruct)))) | |
275 (t (error "This should not happen (reftex-parse-from-file)"))) | |
276 ) | |
277 | |
278 ;; Find bibliography statement | |
279 (when (setq tmp (reftex-locate-bibliography-files master-dir)) | |
280 (push (cons 'bib tmp) docstruct)) | |
281 | |
282 (goto-char 1) | |
283 (when (re-search-forward | |
284 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t) | |
285 (push (cons 'thebib file) docstruct)) | |
286 | |
287 ;; Find external document specifications | |
288 (goto-char 1) | |
289 (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t) | |
290 (push (list 'xr-doc (reftex-match-string 2) | |
291 (reftex-match-string 3)) | |
292 docstruct)) | |
293 | |
294 ;; End of file mark | |
295 (push (list 'eof file) docstruct))))) | |
296 | |
297 ;; Kill the scanned buffer | |
298 (reftex-kill-temporary-buffers next-buf)) | |
299 | |
300 ;; Return the list | |
301 docstruct)) | |
302 | |
303 (defun reftex-locate-bibliography-files (master-dir &optional files) | |
304 ;; Scan buffer for bibliography macro and return file list. | |
305 | |
306 (unless files | |
307 (save-excursion | |
308 (goto-char (point-min)) | |
309 (if (re-search-forward | |
310 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\bibliography{[ \t]*\\([^}]+\\)" nil t) | |
311 (setq files | |
312 (split-string (reftex-match-string 2) | |
313 "[ \t\n\r]*,[ \t\n\r]*"))))) | |
314 (when files | |
315 (setq files | |
316 (mapcar | |
317 (lambda (x) | |
318 (if (or (member x reftex-bibfile-ignore-list) | |
319 (delq nil (mapcar (lambda (re) (string-match re x)) | |
320 reftex-bibfile-ignore-regexps))) | |
321 ;; excluded file | |
322 nil | |
323 ;; find the file | |
324 (reftex-locate-file x "bib" master-dir))) | |
325 files)) | |
326 (delq nil files))) | |
327 | |
328 (defun reftex-replace-label-list-segment (old insert &optional entirely) | |
329 ;; Replace the segment in OLD which corresponds to INSERT. | |
330 ;; Works with side effects, directly changes old. | |
331 ;; If entirely is t, just return INSERT. | |
332 ;; This function also makes sure the old toc markers do not point anywhere. | |
333 | |
334 (cond | |
335 (entirely | |
336 (reftex-silence-toc-markers old (length old)) | |
337 insert) | |
338 (t (let* ((new old) | |
339 (file (nth 1 (car insert))) | |
340 (eof-list (member (list 'eof file) old)) | |
341 (bof-list (member (list 'bof file) old)) | |
342 n) | |
343 (if (not (and bof-list eof-list)) | |
344 (error "Cannot splice") | |
345 ;; Splice | |
346 (reftex-silence-toc-markers bof-list (- (length bof-list) | |
347 (length eof-list))) | |
348 (setq n (- (length old) (length bof-list))) | |
349 (setcdr (nthcdr n new) (cdr insert)) | |
350 (setcdr (nthcdr (1- (length new)) new) (cdr eof-list))) | |
351 new)))) | |
352 | |
353 (defun reftex-section-info (file) | |
354 ;; Return a section entry for the current match. | |
355 ;; Carefull: This function expects the match-data to be still in place! | |
356 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3)))) | |
357 (macro (reftex-match-string 3)) | |
25803
8b032ca83ce6
(reftex-parse-from-file): Scan for multiple thebibliography
Carsten Dominik <dominik@science.uva.nl>
parents:
25280
diff
changeset
|
358 (level-exp (cdr (assoc macro reftex-section-levels-all))) |
8b032ca83ce6
(reftex-parse-from-file): Scan for multiple thebibliography
Carsten Dominik <dominik@science.uva.nl>
parents:
25280
diff
changeset
|
359 (level (if (symbolp level-exp) |
8b032ca83ce6
(reftex-parse-from-file): Scan for multiple thebibliography
Carsten Dominik <dominik@science.uva.nl>
parents:
25280
diff
changeset
|
360 (save-match-data (funcall level-exp)) |
8b032ca83ce6
(reftex-parse-from-file): Scan for multiple thebibliography
Carsten Dominik <dominik@science.uva.nl>
parents:
25280
diff
changeset
|
361 level-exp)) |
8b032ca83ce6
(reftex-parse-from-file): Scan for multiple thebibliography
Carsten Dominik <dominik@science.uva.nl>
parents:
25280
diff
changeset
|
362 (star (= ?* (char-after (match-end 3)))) |
25280 | 363 (unnumbered (or star (< level 0))) |
364 (level (abs level)) | |
365 (section-number (reftex-section-number level unnumbered)) | |
366 (text1 (save-match-data (save-excursion (reftex-context-substring)))) | |
367 (literal (buffer-substring-no-properties | |
368 (1- (match-beginning 3)) | |
369 (min (point-max) (+ (match-end 0) (length text1) 1)))) | |
370 ;; Literal can be too short since text1 too short. No big problem. | |
371 (text (reftex-nicify-text text1))) | |
372 | |
373 ;; Add section number and indentation | |
374 (setq text | |
375 (concat | |
376 (make-string (* reftex-level-indent level) ?\ ) | |
377 (if (nth 1 reftex-label-menu-flags) ; section number flag | |
378 (concat section-number " ")) | |
379 text)) | |
380 (list 'toc "toc" text file marker level section-number | |
381 literal (marker-position marker)))) | |
382 | |
383 (defun reftex-ensure-index-support (&optional abort) | |
384 ;; When index support is turned off, ask to turn it on and | |
385 ;; set the current prefix argument so that `reftex-access-scan-info' | |
386 ;; will rescan the entire document. | |
387 (cond | |
388 (reftex-support-index t) | |
389 ((y-or-n-p "Turn on index support and rescan entire document? ") | |
390 (setq reftex-support-index 'demanded | |
391 current-prefix-arg '(16))) | |
392 (t (if abort | |
393 (error "No index support") | |
394 (message "No index support") | |
395 (ding) | |
396 (sit-for 1))))) | |
397 | |
398 (defun reftex-index-info-safe (file) | |
399 (reftex-with-special-syntax | |
400 (reftex-index-info file))) | |
401 | |
402 (defvar test-dummy) | |
403 (defun reftex-index-info (file) | |
404 ;; Return an index entry for the current match. | |
405 ;; Carefull: This function expects the match-data to be still in place! | |
406 (catch 'exit | |
407 (let* ((macro (reftex-match-string 10)) | |
408 (bom (match-beginning 10)) | |
409 (boa (match-end 10)) | |
410 (entry (or (assoc macro reftex-index-macro-alist) | |
411 (throw 'exit nil))) | |
412 (exclude (nth 3 entry)) | |
413 ;; The following is a test if this match should be excluded | |
414 (test-dummy (and (fboundp exclude) | |
415 (funcall exclude) | |
416 (throw 'exit nil))) | |
417 (itag (nth 1 entry)) | |
418 (prefix (nth 2 entry)) | |
419 (index-tag | |
420 (cond ((stringp itag) itag) | |
421 ((integerp itag) | |
422 (progn (goto-char boa) | |
423 (or (reftex-nth-arg itag (nth 6 entry)) "idx"))) | |
424 (t "idx"))) | |
425 (arg (or (progn (goto-char boa) | |
426 (reftex-nth-arg (nth 5 entry) (nth 6 entry))) | |
427 "")) | |
428 (end-of-args (progn (goto-char boa) | |
429 (reftex-move-over-touching-args) | |
430 (point))) | |
431 (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point))) | |
432 (begin-of-context | |
433 (progn (goto-char bom) | |
434 (skip-chars-backward "^ \t\r\n") | |
435 (point))) | |
436 (context (buffer-substring-no-properties | |
437 begin-of-context end-of-context)) | |
438 (key-end (if (string-match reftex-index-key-end-re arg) | |
439 (1+ (match-beginning 0)))) | |
440 (rawkey (substring arg 0 key-end)) | |
441 | |
442 (key (if prefix (concat prefix rawkey) rawkey)) | |
443 (sortkey (downcase key)) | |
444 (showkey (mapconcat 'identity | |
445 (split-string key reftex-index-level-re) | |
446 " ! "))) | |
447 (goto-char end-of-args) | |
448 ;; 0 1 2 3 4 5 6 7 8 9 | |
449 (list 'index index-tag context file bom arg key showkey sortkey key-end)))) | |
450 | |
451 (defun reftex-short-context (env parse &optional bound derive) | |
452 ;; Get about one line of useful context for the label definition at point. | |
453 | |
454 (if (consp parse) | |
455 (setq parse (if derive (cdr parse) (car parse)))) | |
456 | |
457 (reftex-nicify-text | |
458 | |
459 (cond | |
460 | |
461 ((null parse) | |
462 (save-excursion | |
463 (reftex-context-substring))) | |
464 | |
465 ((eq parse t) | |
466 (if (string= env "section") | |
467 ;; special treatment for section labels | |
468 (save-excursion | |
469 (if (and (re-search-backward reftex-section-or-include-regexp | |
470 (point-min) t) | |
471 (match-end 2)) | |
472 (progn | |
473 (goto-char (match-end 0)) | |
474 (reftex-context-substring)) | |
475 (if reftex-active-toc | |
476 (progn | |
477 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc)) | |
478 (match-string 1 (nth 7 reftex-active-toc))) | |
479 "SECTION HEADING NOT FOUND"))) | |
480 (save-excursion | |
481 (goto-char reftex-default-context-position) | |
482 (unless (eq (string-to-char env) ?\\) | |
483 (reftex-move-over-touching-args)) | |
484 (reftex-context-substring)))) | |
485 | |
486 ((stringp parse) | |
487 (save-excursion | |
488 (if (re-search-backward parse bound t) | |
489 (progn | |
490 (goto-char (match-end 0)) | |
491 (reftex-context-substring)) | |
492 "NO MATCH FOR CONTEXT REGEXP"))) | |
493 | |
494 ((integerp parse) | |
495 (or (save-excursion | |
496 (goto-char reftex-default-context-position) | |
497 (reftex-nth-arg | |
498 parse | |
499 (nth 6 (assoc env reftex-env-or-mac-alist)))) | |
500 "")) | |
501 | |
502 ((fboundp parse) | |
503 ;; A hook function. Call it. | |
504 (save-excursion | |
505 (condition-case error-var | |
506 (funcall parse env) | |
507 (error (format "HOOK ERROR: %s" (cdr error-var)))))) | |
508 (t | |
509 "ILLEGAL VALUE OF PARSE")))) | |
510 | |
511 (defun reftex-where-am-I () | |
512 ;; Return the docstruct entry above point. Actually returns a cons | |
513 ;; cell in which the cdr is a flag indicating if the information is | |
514 ;; exact (t) or approximate (nil). | |
515 | |
516 (let ((docstruct (symbol-value reftex-docstruct-symbol)) | |
517 (cnt 0) rtn | |
518 found) | |
519 (save-excursion | |
520 (while (not rtn) | |
521 (incf cnt) | |
522 (setq found (re-search-backward (reftex-everything-regexp) nil t)) | |
523 (setq rtn | |
524 (cond | |
525 ((not found) | |
526 ;; no match | |
527 (or | |
528 (car (member (list 'bof (buffer-file-name)) docstruct)) | |
529 (not (setq cnt 2)) | |
530 (assq 'bof docstruct) ;; for safety reasons | |
531 'corrupted)) | |
532 ((match-end 1) | |
533 ;; Label | |
534 (assoc (reftex-match-string 1) | |
535 (symbol-value reftex-docstruct-symbol))) | |
536 ((match-end 3) | |
537 ;; Section | |
538 (goto-char (1- (match-beginning 3))) | |
539 (let* ((list (member (list 'bof (buffer-file-name)) | |
540 docstruct)) | |
541 (endelt (car (member (list 'eof (buffer-file-name)) | |
542 list))) | |
543 rtn1) | |
544 (while (and list (not (eq endelt (car list)))) | |
545 (if (and (eq (car (car list)) 'toc) | |
546 (string= (buffer-file-name) | |
547 (nth 3 (car list)))) | |
548 (cond | |
549 ((equal (point) | |
550 (or (and (markerp (nth 4 (car list))) | |
551 (marker-position (nth 4 (car list)))) | |
552 (nth 8 (car list)))) | |
553 ;; Fits with marker position or recorded position | |
554 (setq rtn1 (car list) list nil)) | |
555 ((looking-at (reftex-make-regexp-allow-for-ctrl-m | |
556 (nth 7 (car list)))) | |
557 ;; Same title | |
558 (setq rtn1 (car list) list nil cnt 2)))) | |
559 (pop list)) | |
560 rtn1)) | |
561 ((match-end 7) | |
562 ;; Input or include... | |
563 (car | |
564 (member (list 'eof (reftex-locate-file | |
565 (reftex-match-string 7) "tex" | |
566 (cdr (assq 'master-dir docstruct)))) | |
567 docstruct))) | |
568 ((match-end 9) | |
569 (assq 'appendix (symbol-value reftex-docstruct-symbol))) | |
570 ((match-end 10) | |
571 ;; Index entry | |
572 (when reftex-support-index | |
573 (let* ((index-info (save-excursion | |
574 (reftex-index-info-safe nil))) | |
575 (list (member (list 'bof (buffer-file-name)) | |
576 docstruct)) | |
577 (endelt (car (member (list 'eof (buffer-file-name)) | |
578 list))) | |
579 dist last-dist last (n 0)) | |
580 ;; Check all index entries with equal text | |
581 (while (and list (not (eq endelt (car list)))) | |
582 (when (and (eq (car (car list)) 'index) | |
583 (string= (nth 2 index-info) | |
584 (nth 2 (car list)))) | |
585 (incf n) | |
586 (setq dist (abs (- (point) (nth 4 (car list))))) | |
587 (if (or (not last-dist) (< dist last-dist)) | |
588 (setq last-dist dist last (car list)))) | |
589 (setq list (cdr list))) | |
590 ;; We are sure if we have only one, or a zero distance | |
591 (cond ((or (= n 1) (= dist 0)) last) | |
592 ((> n 1) (setq cnt 2) last) | |
593 (t nil))))) | |
594 ((match-end 11) | |
595 (save-excursion | |
596 (goto-char (match-end 11)) | |
597 (assoc (reftex-no-props | |
598 (reftex-nth-arg-wrapper | |
599 (reftex-match-string 11))) | |
600 (symbol-value reftex-docstruct-symbol)))) | |
601 (t | |
602 (error "This should not happen (reftex-where-am-I)")))))) | |
603 (cons rtn (eq cnt 1)))) | |
604 | |
605 (defun reftex-notice-new (&optional n force) | |
606 "Hook to handshake with RefTeX after something new has been inserted." | |
607 ;; Add a new entry to the docstruct list. If it is a section, renumber | |
608 ;; the following sections. | |
609 ;; FIXME: Put in a WHAT parameter | |
610 ;; When N is given, go back that many matches of reftex-everything-regexp | |
611 ;; When FORCE is non-nil, also insert if `reftex-where-am-I' was uncertain. | |
612 (condition-case nil | |
613 (catch 'exit | |
614 (unless reftex-mode (throw 'exit nil)) | |
615 (reftex-access-scan-info) | |
616 (let* ((docstruct (symbol-value reftex-docstruct-symbol)) | |
617 here-I-am appendix tail entry star level | |
618 section-number context) | |
619 | |
620 (save-excursion | |
621 (when (re-search-backward (reftex-everything-regexp) nil t (or n 1)) | |
622 | |
623 ;; Find where we are | |
624 (setq here-I-am (reftex-where-am-I)) | |
625 (or here-I-am (throw 'exit nil)) | |
626 (unless (or force (cdr here-I-am)) (throw 'exit nil)) | |
627 (setq tail (memq (car here-I-am) docstruct)) | |
628 (or tail (throw 'exit nil)) | |
629 (setq reftex-active-toc (reftex-last-assoc-before-elt | |
630 'toc (car here-I-am) docstruct) | |
631 appendix (reftex-last-assoc-before-elt | |
632 'appendix (car here-I-am) docstruct)) | |
633 | |
634 ;; Initialize section numbers | |
635 (if (eq (car (car here-I-am)) 'appendix) | |
636 (reftex-init-section-numbers nil t) | |
637 (reftex-init-section-numbers reftex-active-toc appendix)) | |
638 | |
639 ;; Match the section command | |
640 (when (re-search-forward (reftex-everything-regexp) nil t) | |
641 (cond | |
642 ((match-end 1) | |
643 (push (reftex-label-info (reftex-match-string 1) buffer-file-name) | |
644 (cdr tail))) | |
645 | |
646 ((match-end 3) | |
647 (setq star (= ?* (char-after (match-end 3))) | |
648 entry (reftex-section-info (buffer-file-name)) | |
649 level (nth 5 entry)) | |
650 ;; Insert the section info | |
651 (push entry (cdr tail)) | |
652 | |
653 ;; We are done unless we use section numbers | |
654 (unless (nth 1 reftex-label-menu-flags) (throw 'exit nil)) | |
655 | |
656 ;; Update the remaining toc items | |
657 (setq tail (cdr tail)) | |
658 (while (and (setq tail (memq (assq 'toc (cdr tail)) tail)) | |
659 (setq entry (car tail)) | |
660 (>= (nth 5 entry) level)) | |
661 (setq star (string-match "\\*" (nth 6 entry)) | |
662 context (nth 2 entry) | |
663 section-number | |
664 (reftex-section-number (nth 5 entry) star)) | |
665 (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)" | |
666 context) | |
667 (when (and (not appendix) | |
668 (>= (string-to-char (match-string 2)) ?A)) | |
669 ;; Just entered the appendex. Get out. | |
670 (throw 'exit nil)) | |
671 | |
672 ;; Change the section number. | |
673 (setf (nth 2 entry) | |
674 (concat (match-string 1 context) | |
675 section-number | |
676 (match-string 3 context)))))) | |
677 ((match-end 10) | |
678 ;; Index entry | |
679 (and reftex-support-index | |
680 (setq entry (reftex-index-info-safe buffer-file-name)) | |
681 ;; FIXME: (add-to-list 'index-tags (nth 1 index-entry)) | |
682 (push entry (cdr tail)))))))))) | |
683 | |
684 (error nil)) | |
685 ) | |
686 | |
687 (defsubst reftex-move-to-previous-arg (&optional bound) | |
688 ;; Assuming that we are in front of a macro argument, | |
689 ;; move backward to the closing parenthesis of the previous argument. | |
690 ;; This function understands the splitting of macros over several lines | |
691 ;; in TeX. | |
692 (cond | |
693 ;; Just to be quick: | |
694 ((memq (preceding-char) '(?\] ?\}))) | |
695 ;; Do a search | |
696 ((and reftex-allow-detached-macro-args | |
697 (re-search-backward | |
698 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t)) | |
699 (goto-char (1+ (match-beginning 0))) | |
700 t) | |
701 (t nil))) | |
702 | |
703 (defun reftex-what-macro-safe (which &optional bound) | |
704 ;; reftex-what-macro with special syntax table. | |
705 (reftex-with-special-syntax | |
706 (reftex-what-macro which bound))) | |
707 | |
708 (defun reftex-what-macro (which &optional bound) | |
709 ;; Find out if point is within the arguments of any TeX-macro. | |
710 ;; The return value is either ("\\macro" . (point)) or a list of them. | |
711 | |
712 ;; If WHICH is nil, immediately return nil. | |
713 ;; If WHICH is 1, return innermost enclosing macro. | |
714 ;; If WHICH is t, return list of all macros enclosing point. | |
715 ;; If WHICH is a list of macros, look only for those macros and return the | |
716 ;; name of the first macro in this list found to enclose point. | |
717 ;; If the optional BOUND is an integer, bound backwards directed | |
718 ;; searches to this point. If it is nil, limit to nearest \section - | |
719 ;; like statement. | |
720 | |
721 ;; This function is pretty stable, but can be fooled if the text contains | |
722 ;; things like \macro{aa}{bb} where \macro is defined to take only one | |
723 ;; argument. As RefTeX cannot know this, the string "bb" would still be | |
724 ;; considered an argument of macro \macro. | |
725 | |
726 (unless reftex-section-regexp (reftex-compile-variables)) | |
727 (catch 'exit | |
728 (if (null which) (throw 'exit nil)) | |
729 (let ((bound (or bound (save-excursion (re-search-backward | |
730 reftex-section-regexp nil 1) | |
731 (point)))) | |
732 pos cmd-list cmd cnt cnt-opt entry) | |
733 (save-restriction | |
734 (save-excursion | |
735 (narrow-to-region (max 1 bound) (point-max)) | |
736 ;; move back out of the current parenthesis | |
737 (while (condition-case nil | |
738 (progn (up-list -1) t) | |
739 (error nil)) | |
740 (setq cnt 1 cnt-opt 0) | |
741 ;; move back over any touching sexps | |
742 (while (and (reftex-move-to-previous-arg bound) | |
743 (condition-case nil | |
744 (progn (backward-sexp) t) | |
745 (error nil))) | |
746 (if (eq (following-char) ?\[) (incf cnt-opt)) | |
747 (incf cnt)) | |
748 (setq pos (point)) | |
749 (when (and (or (= (following-char) ?\[) | |
750 (= (following-char) ?\{)) | |
751 (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t)) | |
752 (setq cmd (reftex-match-string 0)) | |
753 (when (looking-at "\\\\begin{[^}]*}") | |
754 (setq cmd (reftex-match-string 0) | |
755 cnt (1- cnt))) | |
756 ;; This does ignore optional arguments. Very hard to fix. | |
757 (when (setq entry (assoc cmd reftex-env-or-mac-alist)) | |
758 (if (> cnt (or (nth 4 entry) 100)) | |
759 (setq cmd nil))) | |
760 (cond | |
761 ((null cmd)) | |
762 ((eq t which) | |
763 (push (cons cmd (point)) cmd-list)) | |
764 ((or (eq 1 which) (member cmd which)) | |
765 (throw 'exit (cons cmd (point)))))) | |
766 (goto-char pos))) | |
767 (nreverse cmd-list))))) | |
768 | |
769 (defun reftex-what-environment (which &optional bound) | |
770 ;; Find out if point is inside a LaTeX environment. | |
771 ;; The return value is (e.g.) either ("equation" . (point)) or a list of | |
772 ;; them. | |
773 | |
774 ;; If WHICH is nil, immediately return nil. | |
775 ;; If WHICH is 1, return innermost enclosing environment. | |
776 ;; If WHICH is t, return list of all environments enclosing point. | |
777 ;; If WHICH is a list of environments, look only for those environments and | |
778 ;; return the name of the first environment in this list found to enclose | |
779 ;; point. | |
780 | |
781 ;; If the optional BOUND is an integer, bound backwards directed searches to | |
782 ;; this point. If it is nil, limit to nearest \section - like statement. | |
783 | |
784 (unless reftex-section-regexp (reftex-compile-variables)) | |
785 (catch 'exit | |
786 (save-excursion | |
787 (if (null which) (throw 'exit nil)) | |
788 (let ((bound (or bound (save-excursion (re-search-backward | |
789 reftex-section-regexp nil 1) | |
790 (point)))) | |
791 env-list end-list env) | |
792 (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}" | |
793 bound t) | |
794 (setq env (buffer-substring-no-properties | |
795 (match-beginning 2) (match-end 2))) | |
796 (cond | |
797 ((string= (match-string 1) "end") | |
798 (push env end-list)) | |
799 ((equal env (car end-list)) | |
800 (setq end-list (cdr end-list))) | |
801 ((eq t which) | |
802 (push (cons env (point)) env-list)) | |
803 ((or (eq 1 which) (member env which)) | |
804 (throw 'exit (cons env (point)))))) | |
805 (nreverse env-list))))) | |
806 | |
807 (defun reftex-what-special-env (which &optional bound) | |
808 ;; Run the special environment parsers and return the matches. | |
809 ;; | |
810 ;; The return value is (e.g.) either ("my-parser-function" . (point)) | |
811 ;; or a list of them. | |
812 | |
813 ;; If WHICH is nil, immediately return nil. | |
814 ;; If WHICH is 1, return innermost enclosing environment. | |
815 ;; If WHICH is t, return list of all environments enclosing point. | |
816 ;; If WHICH is a list of environments, look only for those environments and | |
817 ;; return the name of the first environment in this list found to enclose | |
818 ;; point. | |
819 | |
820 (unless reftex-section-regexp (reftex-compile-variables)) | |
821 (catch 'exit | |
822 (save-excursion | |
823 (if (null reftex-special-env-parsers) (throw 'exit nil)) | |
824 (if (null which) (throw 'exit nil)) | |
825 (let ((bound (or bound (save-excursion (re-search-backward | |
826 reftex-section-regexp nil 1) | |
827 (point)))) | |
828 (fun-list (if (listp which) | |
829 (mapcar (lambda (x) (if (memq x which) x nil)) | |
830 reftex-special-env-parsers) | |
831 reftex-special-env-parsers)) | |
832 specials rtn) | |
833 ;; Call all functions | |
834 (setq specials (mapcar | |
835 (lambda (fun) | |
836 (save-excursion | |
837 (setq rtn (and fun (funcall fun bound))) | |
838 (if rtn (cons (symbol-name fun) rtn) nil))) | |
839 fun-list)) | |
840 ;; Delete the non-matches | |
841 (setq specials (delq nil specials)) | |
842 ;; Sort | |
843 (setq specials (sort specials (lambda (a b) (> (cdr a) (cdr b))))) | |
844 (if (eq which t) | |
845 specials | |
846 (car specials)))))) | |
847 | |
848 (defsubst reftex-move-to-next-arg (&optional ignore) | |
849 ;; Assuming that we are at the end of a macro name or a macro argument, | |
850 ;; move forward to the opening parenthesis of the next argument. | |
851 ;; This function understands the splitting of macros over several lines | |
852 ;; in TeX. | |
853 (cond | |
854 ;; Just to be quick: | |
855 ((memq (following-char) '(?\[ ?\{))) | |
856 ;; Do a search | |
857 ((and reftex-allow-detached-macro-args | |
858 (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]")) | |
859 (goto-char (1- (match-end 0))) | |
860 t) | |
861 (t nil))) | |
862 | |
863 (defun reftex-nth-arg-wrapper (key) | |
864 (let ((entry (assoc key reftex-env-or-mac-alist))) | |
865 (reftex-nth-arg (nth 5 entry) (nth 6 entry)))) | |
866 | |
867 (defun reftex-nth-arg (n &optional opt-args) | |
868 ;; Return the nth following {} or [] parentheses content. | |
869 ;; OPT-ARGS is a list of argument numbers which are optional. | |
870 | |
871 ;; If we are sitting at a macro start, skip to end of macro name. | |
872 (and (eq (following-char) ?\\) (skip-chars-forward "a-zA-Z*\\\\")) | |
873 | |
874 (if (= n 1000) | |
875 ;; Special case: Skip all touching arguments | |
876 (progn | |
877 (reftex-move-over-touching-args) | |
878 (reftex-context-substring)) | |
879 | |
880 ;; Do the real thing. | |
881 (let ((cnt 1)) | |
882 | |
883 (when (reftex-move-to-next-arg) | |
884 | |
885 (while (< cnt n) | |
886 (while (and (member cnt opt-args) | |
887 (eq (following-char) ?\{)) | |
888 (incf cnt)) | |
889 (when (< cnt n) | |
890 (unless (and (condition-case nil | |
891 (or (forward-list 1) t) | |
892 (error nil)) | |
893 (reftex-move-to-next-arg) | |
894 (incf cnt)) | |
895 (setq cnt 1000)))) | |
896 | |
897 (while (and (memq cnt opt-args) | |
898 (eq (following-char) ?\{)) | |
899 (incf cnt))) | |
900 (if (and (= n cnt) | |
901 (> (skip-chars-forward "{\\[") 0)) | |
902 (reftex-context-substring) | |
903 nil)))) | |
904 | |
905 (defun reftex-move-over-touching-args () | |
906 (condition-case nil | |
907 (while (memq (following-char) '(?\[ ?\{)) | |
908 (forward-list 1)) | |
909 (error nil))) | |
910 | |
911 (defun reftex-context-substring () | |
912 ;; Return up to 150 chars from point | |
913 ;; When point is just after a { or [, limit string to matching parenthesis | |
914 (cond | |
915 ((or (= (preceding-char) ?\{) | |
916 (= (preceding-char) ?\[)) | |
917 ;; Inside a list - get only the list. | |
918 (buffer-substring-no-properties | |
919 (point) | |
920 (min (+ (point) 150) | |
921 (point-max) | |
922 (condition-case nil | |
923 (progn | |
924 (up-list 1) | |
925 (1- (point))) | |
926 (error (point-max)))))) | |
927 (t | |
928 ;; no list - just grab 150 characters | |
929 (buffer-substring-no-properties (point) | |
930 (min (+ (point) 150) (point-max)))))) | |
931 | |
932 ;; Variable holding the vector with section numbers | |
25803
8b032ca83ce6
(reftex-parse-from-file): Scan for multiple thebibliography
Carsten Dominik <dominik@science.uva.nl>
parents:
25280
diff
changeset
|
933 (defvar reftex-section-numbers (make-vector reftex-max-section-depth 0)) |
25280 | 934 |
935 (defun reftex-init-section-numbers (&optional toc-entry appendix) | |
936 ;; Initialize the section numbers with zeros or with what is found | |
937 ;; in the toc entry. | |
938 (let* ((level (or (nth 5 toc-entry) -1)) | |
939 (numbers (nreverse (split-string (or (nth 6 toc-entry) "") "\\."))) | |
940 (depth (1- (length reftex-section-numbers))) | |
941 (i depth) number-string) | |
942 (while (>= i 0) | |
943 (if (> i level) | |
944 (aset reftex-section-numbers i 0) | |
945 (setq number-string (or (car numbers) "0")) | |
946 (if (string-match "\\`[A-Z]\\'" number-string) | |
947 (aset reftex-section-numbers i | |
948 (- (string-to-char number-string) ?A -1)) | |
949 (aset reftex-section-numbers i (string-to-int number-string))) | |
950 (pop numbers)) | |
951 (decf i))) | |
952 (put 'reftex-section-numbers 'appendix appendix)) | |
953 | |
954 (defun reftex-section-number (&optional level star) | |
955 ;; Return a string with the current section number. | |
956 ;; When LEVEL is non-nil, increase section numbers on that level. | |
957 (let* ((depth (1- (length reftex-section-numbers))) idx n (string "") | |
958 (appendix (get 'reftex-section-numbers 'appendix))) | |
959 (when level | |
960 (when (and (> level -1) (not star)) | |
961 (aset reftex-section-numbers | |
962 level (1+ (aref reftex-section-numbers level)))) | |
963 (setq idx (1+ level)) | |
964 (when (not star) | |
965 (while (<= idx depth) | |
966 (aset reftex-section-numbers idx 0) | |
967 (incf idx)))) | |
968 (setq idx 0) | |
969 (while (<= idx depth) | |
970 (setq n (aref reftex-section-numbers idx)) | |
971 (setq string (concat string (if (not (string= string "")) "." "") | |
972 (int-to-string n))) | |
973 (incf idx)) | |
974 (save-match-data | |
975 (if (string-match "\\`\\([@0]\\.\\)+" string) | |
976 (setq string (replace-match "" nil nil string))) | |
977 (if (string-match "\\(\\.0\\)+\\'" string) | |
978 (setq string (replace-match "" nil nil string))) | |
979 (if (and appendix | |
980 (string-match "\\`[0-9]+" string)) | |
981 (setq string | |
982 (concat | |
983 (char-to-string | |
984 (1- (+ ?A (string-to-int (match-string 0 string))))) | |
985 (substring string (match-end 0)))))) | |
986 (if star | |
987 (concat (make-string (1- (length string)) ?\ ) "*") | |
988 string))) | |
989 | |
990 ;;; reftex-parse.el ends here |