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