1 ;;; reftex-parse.el --- parser functions for RefTeX
3 ;; Copyright (C) 1997-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: auctex-devel@gnu.org
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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
31 (defmacro reftex-with-special-syntax
(&rest body
)
32 `(let ((saved-syntax (syntax-table)))
35 (set-syntax-table reftex-syntax-table
)
36 (let ((case-fold-search nil
))
38 (set-syntax-table saved-syntax
))))
40 (defun reftex-parse-one ()
43 (let ((reftex-enable-partial-scans t
))
44 (reftex-access-scan-info '(4))))
46 (defun reftex-parse-all ()
47 "Re-parse entire document."
49 (reftex-access-scan-info '(16)))
51 (defun reftex-do-parse (rescan &optional file
)
52 "Do a document rescan. When allowed, do only a partial scan from FILE."
54 ;; Normalize the rescan argument
55 (setq rescan
(cond ((eq rescan t
) t
)
57 ((equal rescan
'(4)) t
)
58 ((equal rescan
'(16)) 1)
61 ;; Partial scans only when allowed
62 (unless reftex-enable-partial-scans
67 (let* ((old-list (symbol-value reftex-docstruct-symbol
))
68 (master (reftex-TeX-master-file))
69 (true-master (file-truename master
))
70 (master-dir (file-name-as-directory (file-name-directory master
)))
71 (file (or file
(buffer-file-name)))
72 (true-file (file-truename file
))
73 (bibview-cache (assq 'bibview-cache old-list
))
74 (index-tags (cdr (assq 'index-tags old-list
)))
75 from-file appendix docstruct tmp
)
77 ;; Make sure replacement is really an option here
78 (when (and (eq rescan t
)
79 (not (and (member (list 'bof file
) old-list
)
80 (member (list 'eof file
) old-list
))))
81 ;; Scan whole document because no such file section exists
83 (when (string= true-file true-master
)
84 ;; Scan whole document because this file is the master
87 ;; From which file do we start?
89 (cond ((eq rescan t
) (or file master
))
90 ((eq rescan
1) master
)
91 (t (error "This should not happen (reftex-do-parse)"))))
93 ;; Reset index-tags if we scan everything
94 (if (equal rescan
1) (setq index-tags nil
))
96 ;; Find active toc entry and initialize section-numbers
97 (setq reftex-active-toc
(reftex-last-assoc-before-elt
98 'toc
(list 'bof from-file
) old-list
)
99 appendix
(reftex-last-assoc-before-elt
100 'appendix
(list 'bof from-file
) old-list
))
102 (reftex-init-section-numbers reftex-active-toc appendix
)
105 (message "Scanning entire document...")
106 (message "Scanning document from %s..." from-file
))
108 (reftex-with-special-syntax
109 (save-window-excursion
113 (reftex-parse-from-file
114 from-file docstruct master-dir
))
115 (reftex-kill-temporary-buffers)))))
117 (message "Scanning document... done")
119 ;; Turn the list around.
120 (setq docstruct
(nreverse docstruct
))
123 (setq docstruct
(reftex-replace-label-list-segment
124 old-list docstruct
(eq rescan
1)))
126 ;; Add all missing information
127 (unless (assq 'label-numbers docstruct
)
128 (push (cons 'label-numbers nil
) docstruct
))
129 (unless (assq 'master-dir docstruct
)
130 (push (cons 'master-dir master-dir
) docstruct
))
131 (unless (assq 'bibview-cache docstruct
)
132 (push (cons 'bibview-cache
(cdr bibview-cache
)) docstruct
))
133 (let* ((bof1 (memq (assq 'bof docstruct
) docstruct
))
134 (bof2 (assq 'bof
(cdr bof1
)))
135 (is-multi (not (not (and bof1 bof2
))))
136 (entry (or (assq 'is-multi docstruct
)
137 (car (push (list 'is-multi is-multi
) docstruct
)))))
138 (setcdr entry
(cons is-multi nil
)))
139 (and index-tags
(setq index-tags
(sort index-tags
'string
<)))
140 (let ((index-tag-cell (assq 'index-tags docstruct
)))
142 (setcdr index-tag-cell index-tags
)
143 (push (cons 'index-tags index-tags
) docstruct
)))
144 (unless (assq 'xr docstruct
)
145 (let* ((allxr (reftex-all-assq 'xr-doc docstruct
))
148 (if (setq tmp
(reftex-locate-file (nth 2 x
) "tex"
151 (message "Can't find external document %s"
155 (alist (delq nil alist
))
156 (allprefix (delq nil
(mapcar 'car alist
)))
157 (regexp (if allprefix
159 (mapconcat 'identity allprefix
"\\|")
161 "\\\\\\\\\\\\"))) ; this will never match
162 (push (list 'xr alist regexp
) docstruct
)))
164 (set reftex-docstruct-symbol docstruct
)
165 (put reftex-docstruct-symbol
'modified t
)))
167 (defun reftex-everything-regexp ()
168 (if reftex-support-index
169 reftex-everything-regexp
170 reftex-everything-regexp-no-index
))
173 (defun reftex-all-document-files (&optional relative
)
174 "Return a list of all files belonging to the current document.
175 When RELATIVE is non-nil, give file names relative to directory
177 (let* ((all (symbol-value reftex-docstruct-symbol
))
178 (master-dir (file-name-directory (reftex-TeX-master-file)))
179 (re (concat "\\`" (regexp-quote master-dir
)))
181 (while (setq tmp
(assoc 'bof all
))
182 (setq file
(nth 1 tmp
)
183 all
(cdr (memq tmp all
)))
185 (string-match re file
)
186 (setq file
(substring file
(match-end 0))))
187 (push file file-list
))
188 (nreverse file-list
)))
190 ;; Bound in the caller, reftex-do-parse.
193 (defun reftex-parse-from-file (file docstruct master-dir
)
194 ;; Scan the buffer for labels and save them in a list.
195 (let ((regexp (reftex-everything-regexp))
197 file-found tmp include-file
200 toc-entry index-entry next-buf buf
)
203 (setq file-found
(reftex-locate-file file
"tex" master-dir
))
204 (if (and (not file-found
)
205 (setq buf
(reftex-get-buffer-visiting file
)))
206 (setq file-found
(buffer-file-name buf
)))
209 (push (list 'file-error file
) docstruct
)
214 (message "Scanning file %s" file
)
217 (reftex-get-file-buffer-force
219 (not (eq t reftex-keep-temporary-buffers
)))))
221 ;; Begin of file mark
222 (setq file
(buffer-file-name))
223 (push (list 'bof file
) docstruct
)
225 (reftex-with-special-syntax
231 (while (re-search-forward regexp nil t
)
237 (when (or (null reftex-label-ignored-macros-and-environments
)
238 ;; \label{} defs should always be honored,
239 ;; just no keyval style [label=foo] defs.
240 (string-equal "\label{" (substring (reftex-match-string 0) 0 7))
241 (not (fboundp 'TeX-current-macro
))
242 (not (fboundp 'LaTeX-current-environment
))
243 (not (or (member (save-match-data (TeX-current-macro))
244 reftex-label-ignored-macros-and-environments
)
245 (member (save-match-data (LaTeX-current-environment))
246 reftex-label-ignored-macros-and-environments
))))
247 (push (reftex-label-info (reftex-match-string 1) file bound
)
253 ;; Use the beginning as bound and not the end
254 ;; (i.e. (point)) because the section command might
255 ;; be the start of the current environment to be
256 ;; found by `reftex-label-info'.
257 (setq bound
(match-beginning 0))
258 ;; The section regexp matches a character at the end
259 ;; we are not interested in. Especially if it is the
260 ;; backslash of a following macro we want to find in
261 ;; the next parsing iteration.
262 (when (eq (char-before) ?
\\) (backward-char))
264 (setq toc-entry
(funcall reftex-section-info-function file
))
266 ;; It can happen that section info returns nil
267 (setq level
(nth 5 toc-entry
))
268 (setq highest-level
(min highest-level level
))
269 (if (= level highest-level
)
272 (car (rassoc level reftex-section-levels-all
))
275 (push toc-entry docstruct
)
276 (setq reftex-active-toc toc-entry
)))
279 ;; It's an include or input
280 (setq include-file
(reftex-match-string 7))
281 ;; Test if this file should be ignored
282 (unless (delq nil
(mapcar
283 (lambda (x) (string-match x include-file
))
284 reftex-no-include-regexps
))
287 (reftex-parse-from-file
289 docstruct master-dir
))))
292 ;; Appendix starts here
293 (reftex-init-section-numbers nil t
)
294 (push (cons 'appendix t
) docstruct
))
298 (when reftex-support-index
299 (setq index-entry
(reftex-index-info file
))
301 (add-to-list 'index-tags
(nth 1 index-entry
))
302 (push index-entry docstruct
))))
305 ;; A macro with label
307 (let* ((mac (reftex-match-string 11))
308 (label (progn (goto-char (match-end 11))
311 (reftex-nth-arg-wrapper
313 (typekey (nth 1 (assoc mac reftex-env-or-mac-alist
)))
314 (entry (progn (if typekey
316 (goto-char (match-end 0))
318 (goto-char (match-end 11))
319 (reftex-move-over-touching-args))
321 label file bound nil nil
))))
322 (push entry docstruct
))))
323 (t (error "This should not happen (reftex-parse-from-file)")))
326 ;; Find bibliography statement
327 (when (setq tmp
(reftex-locate-bibliography-files master-dir
))
328 (push (cons 'bib tmp
) docstruct
))
331 (when (re-search-forward
332 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t
)
333 (push (cons 'thebib file
) docstruct
))
335 ;; Find external document specifications
337 (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t
)
338 (push (list 'xr-doc
(reftex-match-string 2)
339 (reftex-match-string 3))
343 (push (list 'eof file
) docstruct
)))))
345 ;; Kill the scanned buffer
346 (reftex-kill-temporary-buffers next-buf
))
351 (defun reftex-locate-bibliography-files (master-dir &optional files
)
352 ;; Scan buffer for bibliography macro and return file list.
356 (goto-char (point-min))
357 (if (re-search-forward
359 ; "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\("
360 "\\(^\\)[^%\n\r]*\\\\\\("
361 (mapconcat 'identity reftex-bibliography-commands
"\\|")
362 "\\){[ \t]*\\([^}]+\\)") nil t
)
364 (split-string (reftex-match-string 3)
365 "[ \t\n\r]*,[ \t\n\r]*")))))
370 (if (or (member x reftex-bibfile-ignore-list
)
371 (delq nil
(mapcar (lambda (re) (string-match re x
))
372 reftex-bibfile-ignore-regexps
)))
376 (reftex-locate-file x
"bib" master-dir
)))
380 (defun reftex-replace-label-list-segment (old insert
&optional entirely
)
381 ;; Replace the segment in OLD which corresponds to INSERT.
382 ;; Works with side effects, directly changes old.
383 ;; If entirely is t, just return INSERT.
384 ;; This function also makes sure the old toc markers do not point anywhere.
388 (reftex-silence-toc-markers old
(length old
))
391 (file (nth 1 (car insert
)))
392 (eof-list (member (list 'eof file
) old
))
393 (bof-list (member (list 'bof file
) old
))
395 (if (not (and bof-list eof-list
))
396 (error "Cannot splice")
398 (reftex-silence-toc-markers bof-list
(- (length bof-list
)
400 (setq n
(- (length old
) (length bof-list
)))
401 (setcdr (nthcdr n new
) (cdr insert
))
402 (setcdr (nthcdr (1- (length new
)) new
) (cdr eof-list
)))
405 (defun reftex-section-info (file)
406 ;; Return a section entry for the current match.
407 ;; Careful: This function expects the match-data to be still in place!
408 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3))))
409 (macro (reftex-match-string 3))
410 (prefix (save-match-data
411 (if (string-match "begin{\\([^}]+\\)}" macro
)
412 (match-string 1 macro
))))
413 (level-exp (cdr (assoc macro reftex-section-levels-all
)))
414 (level (if (symbolp level-exp
)
415 (save-match-data (funcall level-exp
))
417 (star (= ?
* (char-after (match-end 3))))
418 (unnumbered (or star
(< level
0)))
420 (section-number (reftex-section-number level unnumbered
))
421 (text1 (save-match-data
423 (reftex-context-substring prefix
))))
424 (literal (buffer-substring-no-properties
425 (1- (match-beginning 3))
426 (min (point-max) (+ (match-end 0) (length text1
) 1))))
427 ;; Literal can be too short since text1 too short. No big problem.
428 (text (reftex-nicify-text text1
)))
430 ;; Add section number and indentation
433 (make-string (* reftex-level-indent level
) ?\
)
434 (if (nth 1 reftex-label-menu-flags
) ; section number flag
435 (concat section-number
" "))
436 (if prefix
(concat (capitalize prefix
) ": ") "")
438 (list 'toc
"toc" text file marker level section-number
439 literal
(marker-position marker
))))
441 (defun reftex-ensure-index-support (&optional abort
)
442 ;; When index support is turned off, ask to turn it on and
443 ;; set the current prefix argument so that `reftex-access-scan-info'
444 ;; will rescan the entire document.
446 (reftex-support-index t
)
447 ((y-or-n-p "Turn on index support and rescan entire document? ")
448 (setq reftex-support-index
'demanded
449 current-prefix-arg
'(16)))
451 (error "No index support")
452 (message "No index support")
456 (defun reftex-index-info-safe (file)
457 (reftex-with-special-syntax
458 (reftex-index-info file
)))
461 (defun reftex-index-info (file)
462 ;; Return an index entry for the current match.
463 ;; Careful: This function expects the match-data to be still in place!
465 (let* ((macro (reftex-match-string 10))
466 (bom (match-beginning 10))
468 (entry (or (assoc macro reftex-index-macro-alist
)
470 (exclude (nth 3 entry
))
471 ;; The following is a test if this match should be excluded
472 (test-dummy (and (fboundp exclude
)
476 (prefix (nth 2 entry
))
478 (cond ((stringp itag
) itag
)
480 (progn (goto-char boa
)
481 (or (reftex-nth-arg itag
(nth 6 entry
)) "idx")))
483 (arg (or (progn (goto-char boa
)
484 (reftex-nth-arg (nth 5 entry
) (nth 6 entry
)))
486 (end-of-args (progn (goto-char boa
)
487 (reftex-move-over-touching-args)
489 (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point)))
491 (progn (goto-char bom
)
492 (skip-chars-backward "^ \t\r\n")
494 (context (buffer-substring-no-properties
495 begin-of-context end-of-context
))
496 (key-end (if (string-match reftex-index-key-end-re arg
)
497 (1+ (match-beginning 0))))
498 (rawkey (substring arg
0 key-end
))
500 (key (if prefix
(concat prefix rawkey
) rawkey
))
501 (sortkey (downcase key
))
502 (showkey (mapconcat 'identity
503 (split-string key reftex-index-level-re
)
505 (goto-char end-of-args
)
506 ;; 0 1 2 3 4 5 6 7 8 9
507 (list 'index index-tag context file bom arg key showkey sortkey key-end
))))
509 (defun reftex-short-context (env parse
&optional bound derive
)
510 ;; Get about one line of useful context for the label definition at point.
513 (setq parse
(if derive
(cdr parse
) (car parse
))))
521 (reftex-context-substring)))
524 (if (string= env
"section")
525 ;; special treatment for section labels
527 (if (and (re-search-backward reftex-section-or-include-regexp
531 (goto-char (match-end 0))
532 (reftex-context-substring))
533 (if reftex-active-toc
535 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc
))
536 (match-string 1 (nth 7 reftex-active-toc
)))
537 "SECTION HEADING NOT FOUND")))
539 (goto-char reftex-default-context-position
)
540 (unless (eq (string-to-char env
) ?
\\)
541 (reftex-move-over-touching-args))
542 (reftex-context-substring))))
546 (if (re-search-backward parse bound t
)
548 (goto-char (match-end 0))
549 (reftex-context-substring))
550 "NO MATCH FOR CONTEXT REGEXP")))
554 (goto-char reftex-default-context-position
)
557 (nth 6 (assoc env reftex-env-or-mac-alist
))))
561 ;; A hook function. Call it.
563 (condition-case error-var
565 (error (format "HOOK ERROR: %s" (cdr error-var
))))))
567 "INVALID VALUE OF PARSE"))))
569 (defun reftex-where-am-I ()
570 ;; Return the docstruct entry above point. Actually returns a cons
571 ;; cell in which the cdr is a flag indicating if the information is
572 ;; exact (t) or approximate (nil).
574 (let ((docstruct (symbol-value reftex-docstruct-symbol
))
575 (cnt 0) rtn rtn-if-no-other
580 (setq found
(re-search-backward (reftex-everything-regexp) nil t
))
586 (car (member (list 'bof
(buffer-file-name)) docstruct
))
588 (assq 'bof docstruct
) ;; for safety reasons
592 (assoc (reftex-match-string 1)
593 (symbol-value reftex-docstruct-symbol
)))
596 (goto-char (1- (match-beginning 3)))
597 (let* ((list (member (list 'bof
(buffer-file-name))
599 (endelt (car (member (list 'eof
(buffer-file-name))
602 (while (and list
(not (eq endelt
(car list
))))
603 (if (and (eq (car (car list
)) 'toc
)
604 (string= (buffer-file-name)
608 (or (and (markerp (nth 4 (car list
)))
609 (marker-position (nth 4 (car list
))))
611 ;; Fits with marker position or recorded position
612 (setq rtn1
(car list
) list nil
))
613 ((looking-at (reftex-make-regexp-allow-for-ctrl-m
615 ;; Same title: remember, but keep looking
616 (setq rtn-if-no-other
(car list
)))))
620 ;; Input or include...
622 (member (list 'eof
(reftex-locate-file
623 (reftex-match-string 7) "tex"
624 (cdr (assq 'master-dir docstruct
))))
627 (assq 'appendix
(symbol-value reftex-docstruct-symbol
)))
630 (when reftex-support-index
631 (let* ((index-info (save-excursion
632 (reftex-index-info-safe nil
)))
633 (list (member (list 'bof
(buffer-file-name))
635 (endelt (car (member (list 'eof
(buffer-file-name))
637 dist last-dist last
(n 0))
638 ;; Check all index entries with equal text
639 (while (and list
(not (eq endelt
(car list
))))
640 (when (and (eq (car (car list
)) 'index
)
641 (string= (nth 2 index-info
)
644 (setq dist
(abs (- (point) (nth 4 (car list
)))))
645 (if (or (not last-dist
) (< dist last-dist
))
646 (setq last-dist dist last
(car list
))))
647 (setq list
(cdr list
)))
648 ;; We are sure if we have only one, or a zero distance
649 (cond ((or (= n
1) (equal dist
0)) last
)
650 ((> n
1) (setq cnt
2) last
)
654 (goto-char (match-end 11))
655 (assoc (reftex-no-props
656 (reftex-nth-arg-wrapper
657 (reftex-match-string 11)))
658 (symbol-value reftex-docstruct-symbol
))))
660 (error "This should not happen (reftex-where-am-I)"))))))
661 ;; Check if there was only a by-name match for the section.
662 (when (and (not rtn
) rtn-if-no-other
)
663 (setq rtn rtn-if-no-other
665 (cons rtn
(eq cnt
1))))
667 (defun reftex-notice-new (&optional n force
)
668 "Hook to handshake with RefTeX after something new has been inserted."
669 ;; Add a new entry to the docstruct list. If it is a section, renumber
670 ;; the following sections.
671 ;; FIXME: Put in a WHAT parameter and search backward until one is found.
672 ;; When N is given, go back that many matches of reftex-everything-regexp
673 ;; When FORCE is non-nil, also insert if `reftex-where-am-I' was uncertain.
676 (unless reftex-mode
(throw 'exit nil
))
677 (reftex-access-scan-info)
678 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
679 here-I-am appendix tail entry star level
680 section-number context
)
683 (when (re-search-backward (reftex-everything-regexp) nil t
(or n
1))
686 (setq here-I-am
(reftex-where-am-I))
687 (or here-I-am
(throw 'exit nil
))
688 (unless (or force
(cdr here-I-am
)) (throw 'exit nil
))
689 (setq tail
(memq (car here-I-am
) docstruct
))
690 (or tail
(throw 'exit nil
))
691 (setq reftex-active-toc
(reftex-last-assoc-before-elt
692 'toc
(car here-I-am
) docstruct
)
693 appendix
(reftex-last-assoc-before-elt
694 'appendix
(car here-I-am
) docstruct
))
696 ;; Initialize section numbers
697 (if (eq (car (car here-I-am
)) 'appendix
)
698 (reftex-init-section-numbers nil t
)
699 (reftex-init-section-numbers reftex-active-toc appendix
))
701 ;; Match the section command
702 (when (re-search-forward (reftex-everything-regexp) nil t
)
705 (push (reftex-label-info (reftex-match-string 1) buffer-file-name
)
709 (setq star
(= ?
* (char-after (match-end 3)))
710 entry
(reftex-section-info (buffer-file-name))
712 ;; Insert the section info
713 (push entry
(cdr tail
))
715 ;; We are done unless we use section numbers
716 (unless (nth 1 reftex-label-menu-flags
) (throw 'exit nil
))
718 ;; Update the remaining toc items
719 (setq tail
(cdr tail
))
720 (while (and (setq tail
(memq (assq 'toc
(cdr tail
)) tail
))
721 (setq entry
(car tail
))
722 (>= (nth 5 entry
) level
))
723 (setq star
(string-match "\\*" (nth 6 entry
))
724 context
(nth 2 entry
)
726 (reftex-section-number (nth 5 entry
) star
))
727 (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)"
729 (when (and (not appendix
)
730 (>= (string-to-char (match-string 2)) ?A
))
731 ;; Just entered the appendix. Get out.
734 ;; Change the section number.
736 (concat (match-string 1 context
)
738 (match-string 3 context
))))))
741 (and reftex-support-index
742 (setq entry
(reftex-index-info-safe buffer-file-name
))
743 ;; FIXME: (add-to-list 'index-tags (nth 1 index-entry))
744 (push entry
(cdr tail
))))))))))
749 (defsubst reftex-move-to-previous-arg
(&optional bound
)
750 ;; Assuming that we are in front of a macro argument,
751 ;; move backward to the closing parenthesis of the previous argument.
752 ;; This function understands the splitting of macros over several lines
756 ((memq (preceding-char) '(?\
] ?\
})))
758 ((and reftex-allow-detached-macro-args
760 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t
))
761 (goto-char (1+ (match-beginning 0)))
765 (defun reftex-what-macro-safe (which &optional bound
)
766 ;; reftex-what-macro with special syntax table.
767 (reftex-with-special-syntax
768 (reftex-what-macro which bound
)))
770 (defun reftex-what-macro (which &optional bound
)
771 ;; Find out if point is within the arguments of any TeX-macro.
772 ;; The return value is either ("\\macro" . (point)) or a list of them.
774 ;; If WHICH is nil, immediately return nil.
775 ;; If WHICH is 1, return innermost enclosing macro.
776 ;; If WHICH is t, return list of all macros enclosing point.
777 ;; If WHICH is a list of macros, look only for those macros and return the
778 ;; name of the first macro in this list found to enclose point.
779 ;; If the optional BOUND is an integer, bound backwards directed
780 ;; searches to this point. If it is nil, limit to nearest \section -
783 ;; This function is pretty stable, but can be fooled if the text contains
784 ;; things like \macro{aa}{bb} where \macro is defined to take only one
785 ;; argument. As RefTeX cannot know this, the string "bb" would still be
786 ;; considered an argument of macro \macro.
788 (unless reftex-section-regexp
(reftex-compile-variables))
790 (if (null which
) (throw 'exit nil
))
791 (let ((bound (or bound
(save-excursion (re-search-backward
792 reftex-section-regexp nil
1)
794 pos cmd-list cmd cnt cnt-opt entry
)
797 (narrow-to-region (max (point-min) bound
) (point-max))
798 ;; move back out of the current parenthesis
799 (while (condition-case nil
800 (let ((forward-sexp-function nil
))
803 (setq cnt
1 cnt-opt
0)
804 ;; move back over any touching sexps
805 (while (and (reftex-move-to-previous-arg bound
)
807 (let ((forward-sexp-function nil
))
810 (if (eq (following-char) ?\
[) (incf cnt-opt
))
813 (when (and (or (= (following-char) ?\
[)
814 (= (following-char) ?\
{))
815 (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t
))
816 (setq cmd
(reftex-match-string 0))
817 (when (looking-at "\\\\begin{[^}]*}")
818 (setq cmd
(reftex-match-string 0)
820 ;; This does ignore optional arguments. Very hard to fix.
821 (when (setq entry
(assoc cmd reftex-env-or-mac-alist
))
822 (if (> cnt
(or (nth 4 entry
) 100))
827 (push (cons cmd
(point)) cmd-list
))
828 ((or (eq 1 which
) (member cmd which
))
829 (throw 'exit
(cons cmd
(point))))))
831 (nreverse cmd-list
)))))
833 (defun reftex-what-environment (which &optional bound
)
834 ;; Find out if point is inside a LaTeX environment.
835 ;; The return value is (e.g.) either ("equation" . (point)) or a list of
838 ;; If WHICH is nil, immediately return nil.
839 ;; If WHICH is 1, return innermost enclosing environment.
840 ;; If WHICH is t, return list of all environments enclosing point.
841 ;; If WHICH is a list of environments, look only for those environments and
842 ;; return the name of the first environment in this list found to enclose
845 ;; If the optional BOUND is an integer, bound backwards directed searches to
846 ;; this point. If it is nil, limit to nearest \section - like statement.
848 (unless reftex-section-regexp
(reftex-compile-variables))
851 (if (null which
) (throw 'exit nil
))
852 (let ((bound (or bound
(save-excursion (re-search-backward
853 reftex-section-regexp nil
1)
855 env-list end-list env
)
856 (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}"
858 (setq env
(buffer-substring-no-properties
859 (match-beginning 2) (match-end 2)))
861 ((string= (match-string 1) "end")
863 ((equal env
(car end-list
))
864 (setq end-list
(cdr end-list
)))
866 (push (cons env
(point)) env-list
))
867 ((or (eq 1 which
) (member env which
))
868 (throw 'exit
(cons env
(point))))))
869 (nreverse env-list
)))))
871 (defun reftex-what-special-env (which &optional bound
)
872 ;; Run the special environment parsers and return the matches.
874 ;; The return value is (e.g.) either ("my-parser-function" . (point))
875 ;; or a list of them.
877 ;; If WHICH is nil, immediately return nil.
878 ;; If WHICH is 1, return innermost enclosing environment.
879 ;; If WHICH is t, return list of all environments enclosing point.
880 ;; If WHICH is a list of environments, look only for those environments and
881 ;; return the name of the first environment in this list found to enclose
884 (unless reftex-section-regexp
(reftex-compile-variables))
887 (if (null reftex-special-env-parsers
) (throw 'exit nil
))
888 (if (null which
) (throw 'exit nil
))
889 (let ((bound (or bound
(save-excursion (re-search-backward
890 reftex-section-regexp nil
1)
892 (fun-list (if (listp which
)
893 (mapcar (lambda (x) (if (memq x which
) x nil
))
894 reftex-special-env-parsers
)
895 reftex-special-env-parsers
))
897 ;; Call all functions
898 (setq specials
(mapcar
901 (setq rtn
(and fun
(funcall fun bound
)))
902 (if rtn
(cons (symbol-name fun
) rtn
) nil
)))
904 ;; Delete the non-matches
905 (setq specials
(delq nil specials
))
907 (setq specials
(sort specials
(lambda (a b
) (> (cdr a
) (cdr b
)))))
912 (defsubst reftex-move-to-next-arg
(&optional ignore
)
913 ;; Assuming that we are at the end of a macro name or a macro argument,
914 ;; move forward to the opening parenthesis of the next argument.
915 ;; This function understands the splitting of macros over several lines
919 ((memq (following-char) '(?\
[ ?\
{)))
921 ((and reftex-allow-detached-macro-args
922 (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]"))
923 (goto-char (1- (match-end 0)))
927 (defun reftex-nth-arg-wrapper (key)
928 (let ((entry (assoc key reftex-env-or-mac-alist
)))
929 (reftex-nth-arg (nth 5 entry
) (nth 6 entry
))))
931 (defun reftex-nth-arg (n &optional opt-args
)
932 ;; Return the nth following {} or [] parentheses content.
933 ;; OPT-ARGS is a list of argument numbers which are optional.
935 ;; If we are sitting at a macro start, skip to end of macro name.
936 (and (eq (following-char) ?
\\) (skip-chars-forward "a-zA-Z*\\\\"))
939 ;; Special case: Skip all touching arguments
941 (reftex-move-over-touching-args)
942 (reftex-context-substring))
944 ;; Do the real thing.
947 (when (reftex-move-to-next-arg)
950 (while (and (member cnt opt-args
)
951 (eq (following-char) ?\
{))
954 (unless (and (condition-case nil
955 (or (forward-list 1) t
)
957 (reftex-move-to-next-arg)
961 (while (and (memq cnt opt-args
)
962 (eq (following-char) ?\
{))
965 (> (skip-chars-forward "{\\[") 0))
966 (reftex-context-substring)
969 (defun reftex-move-over-touching-args ()
971 (while (memq (following-char) '(?\
[ ?\
{))
975 (defun reftex-context-substring (&optional to-end
)
976 ;; Return up to 150 chars from point
977 ;; When point is just after a { or [, limit string to matching parenthesis
980 ;; Environment - find next \end
981 (buffer-substring-no-properties
985 ;; FIXME: This is not perfect
986 (if (re-search-forward "\\\\end{" nil t
)
989 ((memq (preceding-char) '(?\
{ ?\
[))
990 ;; Inside a list - get only the list.
991 (buffer-substring-no-properties
996 (let ((forward-sexp-function nil
)) ;Unneeded fanciness.
999 (error (point-max))))))
1001 ;; no list - just grab 150 characters
1002 (buffer-substring-no-properties (point)
1003 (min (+ (point) 150) (point-max))))))
1005 ;; Variable holding the vector with section numbers
1006 (defvar reftex-section-numbers
(make-vector reftex-max-section-depth
0))
1008 (defun reftex-init-section-numbers (&optional toc-entry appendix
)
1009 ;; Initialize the section numbers with zeros or with what is found
1010 ;; in the toc entry.
1011 (let* ((level (or (nth 5 toc-entry
) -
1))
1012 (numbers (nreverse (split-string (or (nth 6 toc-entry
) "") "\\.")))
1013 (depth (1- (length reftex-section-numbers
)))
1014 (i depth
) number-string
)
1017 (aset reftex-section-numbers i
0)
1018 (setq number-string
(or (car numbers
) "0"))
1019 (if (string-match "\\`[A-Z]\\'" number-string
)
1020 (aset reftex-section-numbers i
1021 (- (string-to-char number-string
) ?A -
1))
1022 (aset reftex-section-numbers i
(string-to-number number-string
)))
1025 (put 'reftex-section-numbers
'appendix appendix
))
1027 (defun reftex-section-number (&optional level star
)
1028 ;; Return a string with the current section number.
1029 ;; When LEVEL is non-nil, increase section numbers on that level.
1030 (let* ((depth (1- (length reftex-section-numbers
))) idx n
(string "")
1031 (appendix (get 'reftex-section-numbers
'appendix
))
1032 (partspecial (and (not reftex-part-resets-chapter
)
1034 ;; partspecial means, this is a part statement.
1035 ;; Parts do not reset the chapter counter, and the part number is
1036 ;; not included in the numbering of other sectioning levels.
1038 (when (and (> level -
1) (not star
))
1039 (aset reftex-section-numbers
1040 level
(1+ (aref reftex-section-numbers level
))))
1041 (setq idx
(1+ level
))
1043 (while (<= idx depth
)
1044 (if (or (not partspecial
)
1046 (aset reftex-section-numbers idx
0))
1049 (setq string
(concat "Part " (reftex-roman-number
1050 (aref reftex-section-numbers
0))))
1051 (setq idx
(if reftex-part-resets-chapter
0 1))
1052 (while (<= idx depth
)
1053 (setq n
(aref reftex-section-numbers idx
))
1054 (if (not (and partspecial
(not (equal string
""))))
1055 (setq string
(concat string
(if (not (string= string
"")) "." "")
1056 (int-to-string n
))))
1059 (if (string-match "\\`\\([@0]\\.\\)+" string
)
1060 (setq string
(replace-match "" nil nil string
)))
1061 (if (string-match "\\(\\.0\\)+\\'" string
)
1062 (setq string
(replace-match "" nil nil string
)))
1064 (string-match "\\`[0-9]+" string
))
1068 (1- (+ ?A
(string-to-number (match-string 0 string
)))))
1069 (substring string
(match-end 0))))))
1071 (concat (make-string (1- (length string
)) ?\
) "*")
1074 (defun reftex-roman-number (n)
1075 ;; Return as a string the roman number equal to N.
1078 (list '((1000 .
"M") ( 900 .
"CM") ( 500 .
"D") ( 400 .
"CD")
1079 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
1080 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
1084 (setq listel
(pop list
)
1088 (setq string
(concat string s
)
1089 nrest
(- nrest i
))))
1092 (provide 'reftex-parse
)
1094 ;;; reftex-parse.el ends here