1 ;;; reftex-parse.el --- parser functions for RefTeX
3 ;; Copyright (C) 1997-2014 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
))))
41 (defun reftex-parse-one ()
44 (let ((reftex-enable-partial-scans t
))
45 (reftex-access-scan-info '(4))))
48 (defun reftex-parse-all ()
49 "Re-parse entire document."
51 (reftex-access-scan-info '(16)))
53 (defvar reftex--index-tags
)
56 (defun reftex-do-parse (rescan &optional file
)
57 "Do a document rescan.
58 When allowed, do only a partial scan from FILE."
60 ;; Normalize the rescan argument
61 (setq rescan
(cond ((eq rescan t
) t
)
63 ((equal rescan
'(4)) t
)
64 ((equal rescan
'(16)) 1)
67 ;; Partial scans only when allowed
68 (unless reftex-enable-partial-scans
73 (let* ((old-list (symbol-value reftex-docstruct-symbol
))
74 (master (reftex-TeX-master-file))
75 (true-master (file-truename master
))
76 (master-dir (file-name-as-directory (file-name-directory master
)))
77 (file (or file
(buffer-file-name)))
78 (true-file (file-truename file
))
79 (bibview-cache (assq 'bibview-cache old-list
))
80 (reftex--index-tags (cdr (assq 'index-tags old-list
)))
81 from-file appendix docstruct tmp
)
83 ;; Make sure replacement is really an option here
84 (when (and (eq rescan t
)
85 (not (and (member (list 'bof file
) old-list
)
86 (member (list 'eof file
) old-list
))))
87 ;; Scan whole document because no such file section exists
89 (when (string= true-file true-master
)
90 ;; Scan whole document because this file is the master
93 ;; From which file do we start?
95 (cond ((eq rescan t
) (or file master
))
96 ((eq rescan
1) master
)
97 (t (error "This should not happen (reftex-do-parse)"))))
99 ;; Reset index-tags if we scan everything
100 (if (equal rescan
1) (setq reftex--index-tags nil
))
102 ;; Find active toc entry and initialize section-numbers
103 (setq reftex-active-toc
(reftex-last-assoc-before-elt
104 'toc
(list 'bof from-file
) old-list
)
105 appendix
(reftex-last-assoc-before-elt
106 'appendix
(list 'bof from-file
) old-list
))
108 (reftex-init-section-numbers reftex-active-toc appendix
)
111 (message "Scanning entire document...")
112 (message "Scanning document from %s..." from-file
))
114 (reftex-with-special-syntax
115 (save-window-excursion
119 (reftex-parse-from-file
120 from-file docstruct master-dir
))
121 (reftex-kill-temporary-buffers)))))
123 (message "Scanning document... done")
125 ;; Turn the list around.
126 (setq docstruct
(nreverse docstruct
))
129 (setq docstruct
(reftex-replace-label-list-segment
130 old-list docstruct
(eq rescan
1)))
132 ;; Add all missing information
133 (unless (assq 'label-numbers docstruct
)
134 (push (cons 'label-numbers nil
) docstruct
))
135 (unless (assq 'master-dir docstruct
)
136 (push (cons 'master-dir master-dir
) docstruct
))
137 (unless (assq 'bibview-cache docstruct
)
138 (push (cons 'bibview-cache
(cdr bibview-cache
)) docstruct
))
139 (let* ((bof1 (memq (assq 'bof docstruct
) docstruct
))
140 (bof2 (assq 'bof
(cdr bof1
)))
141 (is-multi (not (not (and bof1 bof2
))))
142 (entry (or (assq 'is-multi docstruct
)
143 (car (push (list 'is-multi is-multi
) docstruct
)))))
144 (setcdr entry
(cons is-multi nil
)))
145 (and reftex--index-tags
146 (setq reftex--index-tags
(sort reftex--index-tags
'string
<)))
147 (let ((index-tag-cell (assq 'index-tags docstruct
)))
149 (setcdr index-tag-cell reftex--index-tags
)
150 (push (cons 'index-tags reftex--index-tags
) docstruct
)))
151 (unless (assq 'xr docstruct
)
152 (let* ((allxr (reftex-all-assq 'xr-doc docstruct
))
155 (if (setq tmp
(reftex-locate-file (nth 2 x
) "tex"
158 (message "Can't find external document %s"
162 (alist (delq nil alist
))
163 (allprefix (delq nil
(mapcar 'car alist
)))
164 (regexp (if allprefix
166 (mapconcat 'identity allprefix
"\\|")
168 "\\\\\\\\\\\\"))) ; this will never match
169 (push (list 'xr alist regexp
) docstruct
)))
171 (set reftex-docstruct-symbol docstruct
)
172 (put reftex-docstruct-symbol
'modified t
)))
175 (defun reftex-everything-regexp ()
176 (if reftex-support-index
177 reftex-everything-regexp
178 reftex-everything-regexp-no-index
))
180 ;; NB this is a global autoload - see reftex.el.
182 (defun reftex-all-document-files (&optional relative
)
183 "Return a list of all files belonging to the current document.
184 When RELATIVE is non-nil, give file names relative to directory
186 (let* ((all (symbol-value reftex-docstruct-symbol
))
187 (master-dir (file-name-directory (reftex-TeX-master-file)))
188 (re (concat "\\`" (regexp-quote master-dir
)))
190 (while (setq tmp
(assoc 'bof all
))
191 (setq file
(nth 1 tmp
)
192 all
(cdr (memq tmp all
)))
194 (string-match re file
)
195 (setq file
(substring file
(match-end 0))))
196 (push file file-list
))
197 (nreverse file-list
)))
199 ;; Bound in the caller, reftex-do-parse.
200 (defun reftex-parse-from-file (file docstruct master-dir
)
201 "Scan the buffer for labels and save them in a list."
202 (let ((regexp (reftex-everything-regexp))
204 file-found tmp include-file
207 toc-entry index-entry next-buf buf
)
210 (setq file-found
(reftex-locate-file file
"tex" master-dir
))
211 (if (and (not file-found
)
212 (setq buf
(reftex-get-buffer-visiting file
)))
213 (setq file-found
(buffer-file-name buf
)))
216 (push (list 'file-error file
) docstruct
)
221 (message "Scanning file %s" file
)
224 (reftex-get-file-buffer-force
226 (not (eq t reftex-keep-temporary-buffers
)))))
228 ;; Begin of file mark
229 (setq file
(buffer-file-name))
230 (push (list 'bof file
) docstruct
)
232 (reftex-with-special-syntax
238 (while (re-search-forward regexp nil t
)
244 (when (or (null reftex-label-ignored-macros-and-environments
)
245 ;; \label{} defs should always be honored,
246 ;; just no keyval style [label=foo] defs.
247 (string-equal "\label{" (substring (reftex-match-string 0) 0 7))
248 (if (and (fboundp 'TeX-current-macro
)
249 (fboundp 'LaTeX-current-environment
))
250 (not (or (member (save-match-data (TeX-current-macro))
251 reftex-label-ignored-macros-and-environments
)
252 (member (save-match-data (LaTeX-current-environment))
253 reftex-label-ignored-macros-and-environments
)))
255 (push (reftex-label-info (reftex-match-string 1) file bound
)
261 ;; Use the beginning as bound and not the end
262 ;; (i.e. (point)) because the section command might
263 ;; be the start of the current environment to be
264 ;; found by `reftex-label-info'.
265 (setq bound
(match-beginning 0))
266 ;; The section regexp matches a character at the end
267 ;; we are not interested in. Especially if it is the
268 ;; backslash of a following macro we want to find in
269 ;; the next parsing iteration.
270 (when (eq (char-before) ?
\\) (backward-char))
272 (setq toc-entry
(funcall reftex-section-info-function file
))
274 ;; It can happen that section info returns nil
275 (setq level
(nth 5 toc-entry
))
276 (setq highest-level
(min highest-level level
))
277 (if (= level highest-level
)
280 (car (rassoc level reftex-section-levels-all
))
283 (push toc-entry docstruct
)
284 (setq reftex-active-toc toc-entry
)))
287 ;; It's an include or input
288 (setq include-file
(reftex-match-string 7))
289 ;; Test if this file should be ignored
290 (unless (delq nil
(mapcar
291 (lambda (x) (string-match x include-file
))
292 reftex-no-include-regexps
))
295 (reftex-parse-from-file
297 docstruct master-dir
))))
300 ;; Appendix starts here
301 (reftex-init-section-numbers nil t
)
302 (push (cons 'appendix t
) docstruct
))
306 (when reftex-support-index
307 (setq index-entry
(reftex-index-info file
))
309 (add-to-list 'reftex--index-tags
(nth 1 index-entry
))
310 (push index-entry docstruct
))))
313 ;; A macro with label
315 (let* ((mac (reftex-match-string 11))
316 (label (progn (goto-char (match-end 11))
319 (reftex-nth-arg-wrapper
321 (typekey (nth 1 (assoc mac reftex-env-or-mac-alist
)))
322 (entry (progn (if typekey
324 (goto-char (match-end 0))
326 (goto-char (match-end 11))
327 (reftex-move-over-touching-args))
329 label file bound nil nil
))))
330 (push entry docstruct
))))
331 (t (error "This should not happen (reftex-parse-from-file)")))
334 ;; Find bibliography statement
335 (when (setq tmp
(reftex-locate-bibliography-files master-dir
))
336 (push (cons 'bib tmp
) docstruct
))
339 (when (re-search-forward
340 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t
)
341 (push (cons 'thebib file
) docstruct
))
343 ;; Find external document specifications
345 (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t
)
346 (push (list 'xr-doc
(reftex-match-string 2)
347 (reftex-match-string 3))
351 (push (list 'eof file
) docstruct
)))))
353 ;; Kill the scanned buffer
354 (reftex-kill-temporary-buffers next-buf
))
359 (defun reftex-using-biblatex-p ()
360 "Return non-nil iff we are using biblatex rather than bibtex."
361 (if (boundp 'TeX-active-styles
)
362 ;; the sophisticated AUCTeX way
363 (member "biblatex" TeX-active-styles
)
364 ;; poor-man's check...
366 (re-search-forward "^[^%]*\\\\usepackage.*{biblatex}" nil t
))))
369 (defun reftex-locate-bibliography-files (master-dir &optional files
)
370 "Scan buffer for bibliography macros and return file list."
373 (goto-char (point-min))
374 ;; when biblatex is used, multiple \bibliography or
375 ;; \addbibresource macros are allowed. With plain bibtex, only
376 ;; the first is used.
377 (let ((using-biblatex (reftex-using-biblatex-p))
382 ;; "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\("
383 "\\(^\\)[^%\n\r]*\\\\\\("
384 (mapconcat 'identity reftex-bibliography-commands
"\\|")
385 "\\)\\(\\[.+?\\]\\)?{[ \t]*\\([^}]+\\)") nil t
))
388 (split-string (reftex-match-string 4)
389 "[ \t\n\r]*,[ \t\n\r]*")))
390 (unless using-biblatex
391 (setq again nil
))))))
396 (if (or (member x reftex-bibfile-ignore-list
)
397 (delq nil
(mapcar (lambda (re) (string-match re x
))
398 reftex-bibfile-ignore-regexps
)))
402 (reftex-locate-file x
"bib" master-dir
)))
406 (defun reftex-replace-label-list-segment (old insert
&optional entirely
)
407 "Replace the segment in OLD which corresponds to INSERT.
408 Works with side effects, directly changes old.
409 If ENTIRELY is t, just return INSERT.
410 This function also makes sure the old toc markers do not point anywhere."
414 (reftex-silence-toc-markers old
(length old
))
417 (file (nth 1 (car insert
)))
418 (eof-list (member (list 'eof file
) old
))
419 (bof-list (member (list 'bof file
) old
))
421 (if (not (and bof-list eof-list
))
422 (error "Cannot splice")
424 (reftex-silence-toc-markers bof-list
(- (length bof-list
)
426 (setq n
(- (length old
) (length bof-list
)))
427 (setcdr (nthcdr n new
) (cdr insert
))
428 (setcdr (nthcdr (1- (length new
)) new
) (cdr eof-list
)))
432 (defun reftex-section-info (file)
433 "Return a section entry for the current match.
434 Careful: This function expects the match-data to be still in place!"
435 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3))))
436 (macro (reftex-match-string 3))
437 (prefix (save-match-data
438 (if (string-match "begin{\\([^}]+\\)}" macro
)
439 (match-string 1 macro
))))
440 (level-exp (cdr (assoc macro reftex-section-levels-all
)))
441 (level (if (symbolp level-exp
)
442 (save-match-data (funcall level-exp
))
444 (star (= ?
* (char-after (match-end 3))))
445 (unnumbered (or star
(< level
0)))
447 (section-number (reftex-section-number level unnumbered
))
448 (text1 (save-match-data
450 (reftex-context-substring prefix
))))
451 (literal (buffer-substring-no-properties
452 (1- (match-beginning 3))
453 (min (point-max) (+ (match-end 0) (length text1
) 1))))
454 ;; Literal can be too short since text1 too short. No big problem.
455 (text (reftex-nicify-text text1
)))
457 ;; Add section number and indentation
460 (make-string (* reftex-level-indent level
) ?\
)
461 (if (nth 1 reftex-label-menu-flags
) ; section number flag
462 (concat section-number
" "))
463 (if prefix
(concat (capitalize prefix
) ": ") "")
465 (list 'toc
"toc" text file marker level section-number
466 literal
(marker-position marker
))))
469 (defun reftex-ensure-index-support (&optional abort
)
470 "When index support is turned off, ask to turn it on and
471 set the current prefix argument so that `reftex-access-scan-info'
472 will rescan the entire document."
474 (reftex-support-index t
)
475 ((y-or-n-p "Turn on index support and rescan entire document? ")
476 (setq reftex-support-index
'demanded
477 current-prefix-arg
'(16)))
479 (error "No index support")
480 (message "No index support")
485 (defun reftex-index-info-safe (file)
486 (reftex-with-special-syntax
487 (reftex-index-info file
)))
491 (defun reftex-index-info (file)
492 "Return an index entry for the current match.
493 Careful: This function expects the match-data to be still in place!"
495 (let* ((macro (reftex-match-string 10))
496 (bom (match-beginning 10))
498 (entry (or (assoc macro reftex-index-macro-alist
)
500 (exclude (nth 3 entry
))
501 ;; The following is a test if this match should be excluded
502 (test-dummy (and (fboundp exclude
)
506 (prefix (nth 2 entry
))
508 (cond ((stringp itag
) itag
)
510 (progn (goto-char boa
)
511 (or (reftex-nth-arg itag
(nth 6 entry
)) "idx")))
513 (arg (or (progn (goto-char boa
)
514 (reftex-nth-arg (nth 5 entry
) (nth 6 entry
)))
516 (end-of-args (progn (goto-char boa
)
517 (reftex-move-over-touching-args)
519 (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point)))
521 (progn (goto-char bom
)
522 (skip-chars-backward "^ \t\r\n")
524 (context (buffer-substring-no-properties
525 begin-of-context end-of-context
))
526 (key-end (if (string-match reftex-index-key-end-re arg
)
527 (1+ (match-beginning 0))))
528 (rawkey (substring arg
0 key-end
))
530 (key (if prefix
(concat prefix rawkey
) rawkey
))
531 (sortkey (downcase key
))
532 (showkey (mapconcat 'identity
533 (split-string key reftex-index-level-re
)
535 (goto-char end-of-args
)
536 ;; 0 1 2 3 4 5 6 7 8 9
537 (list 'index index-tag context file bom arg key showkey sortkey key-end
))))
540 (defun reftex-short-context (env parse
&optional bound derive
)
541 "Get about one line of useful context for the label definition at point."
544 (setq parse
(if derive
(cdr parse
) (car parse
))))
552 (reftex-context-substring)))
555 (if (string= env
"section")
556 ;; special treatment for section labels
558 (if (and (re-search-backward reftex-section-or-include-regexp
562 (goto-char (match-end 0))
563 (reftex-context-substring))
564 (if reftex-active-toc
566 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc
))
567 (match-string 1 (nth 7 reftex-active-toc
)))
568 "SECTION HEADING NOT FOUND")))
570 (goto-char reftex-default-context-position
)
571 (unless (eq (string-to-char env
) ?
\\)
572 (reftex-move-over-touching-args))
573 (reftex-context-substring))))
577 (if (re-search-backward parse bound t
)
579 (goto-char (match-end 0))
580 (reftex-context-substring))
581 "NO MATCH FOR CONTEXT REGEXP")))
585 (goto-char reftex-default-context-position
)
588 (nth 6 (assoc env reftex-env-or-mac-alist
))))
592 ;; A hook function. Call it.
594 (condition-case error-var
596 (error (format "HOOK ERROR: %s" (cdr error-var
))))))
598 "INVALID VALUE OF PARSE"))))
601 (defun reftex-where-am-I ()
602 "Return the docstruct entry above point.
603 Actually returns a cons cell in which the cdr is a flag indicating
604 if the information is exact (t) or approximate (nil)."
606 (let ((docstruct (symbol-value reftex-docstruct-symbol
))
607 (cnt 0) rtn rtn-if-no-other
612 (setq found
(re-search-backward (reftex-everything-regexp) nil t
))
618 (car (member (list 'bof
(buffer-file-name)) docstruct
))
620 (assq 'bof docstruct
) ;; for safety reasons
624 (assoc (reftex-match-string 1)
625 (symbol-value reftex-docstruct-symbol
)))
628 (goto-char (1- (match-beginning 3)))
629 (let* ((list (member (list 'bof
(buffer-file-name))
631 (endelt (car (member (list 'eof
(buffer-file-name))
634 (while (and list
(not (eq endelt
(car list
))))
635 (if (and (eq (car (car list
)) 'toc
)
636 (string= (buffer-file-name)
640 (or (and (markerp (nth 4 (car list
)))
641 (marker-position (nth 4 (car list
))))
643 ;; Fits with marker position or recorded position
644 (setq rtn1
(car list
) list nil
))
645 ((looking-at (reftex-make-regexp-allow-for-ctrl-m
647 ;; Same title: remember, but keep looking
648 (setq rtn-if-no-other
(car list
)))))
652 ;; Input or include...
654 (member (list 'eof
(reftex-locate-file
655 (reftex-match-string 7) "tex"
656 (cdr (assq 'master-dir docstruct
))))
659 (assq 'appendix
(symbol-value reftex-docstruct-symbol
)))
662 (when reftex-support-index
663 (let* ((index-info (save-excursion
664 (reftex-index-info-safe nil
)))
665 (list (member (list 'bof
(buffer-file-name))
667 (endelt (car (member (list 'eof
(buffer-file-name))
669 dist last-dist last
(n 0))
670 ;; Check all index entries with equal text
671 (while (and list
(not (eq endelt
(car list
))))
672 (when (and (eq (car (car list
)) 'index
)
673 (string= (nth 2 index-info
)
676 (setq dist
(abs (- (point) (nth 4 (car list
)))))
677 (if (or (not last-dist
) (< dist last-dist
))
678 (setq last-dist dist last
(car list
))))
679 (setq list
(cdr list
)))
680 ;; We are sure if we have only one, or a zero distance
681 (cond ((or (= n
1) (equal dist
0)) last
)
682 ((> n
1) (setq cnt
2) last
)
686 (goto-char (match-end 11))
687 (assoc (reftex-no-props
688 (reftex-nth-arg-wrapper
689 (reftex-match-string 11)))
690 (symbol-value reftex-docstruct-symbol
))))
692 (error "This should not happen (reftex-where-am-I)"))))))
693 ;; Check if there was only a by-name match for the section.
694 (when (and (not rtn
) rtn-if-no-other
)
695 (setq rtn rtn-if-no-other
697 (cons rtn
(eq cnt
1))))
700 (defun reftex-notice-new (&optional n force
)
701 "Hook to handshake with RefTeX after something new has been inserted."
702 ;; Add a new entry to the docstruct list. If it is a section, renumber
703 ;; the following sections.
704 ;; FIXME: Put in a WHAT parameter and search backward until one is found.
705 ;; When N is given, go back that many matches of reftex-everything-regexp
706 ;; When FORCE is non-nil, also insert if `reftex-where-am-I' was uncertain.
709 (unless reftex-mode
(throw 'exit nil
))
710 (reftex-access-scan-info)
711 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
712 here-I-am appendix tail entry star level
713 section-number context
)
716 (when (re-search-backward (reftex-everything-regexp) nil t
(or n
1))
719 (setq here-I-am
(reftex-where-am-I))
720 (or here-I-am
(throw 'exit nil
))
721 (unless (or force
(cdr here-I-am
)) (throw 'exit nil
))
722 (setq tail
(memq (car here-I-am
) docstruct
))
723 (or tail
(throw 'exit nil
))
724 (setq reftex-active-toc
(reftex-last-assoc-before-elt
725 'toc
(car here-I-am
) docstruct
)
726 appendix
(reftex-last-assoc-before-elt
727 'appendix
(car here-I-am
) docstruct
))
729 ;; Initialize section numbers
730 (if (eq (car (car here-I-am
)) 'appendix
)
731 (reftex-init-section-numbers nil t
)
732 (reftex-init-section-numbers reftex-active-toc appendix
))
734 ;; Match the section command
735 (when (re-search-forward (reftex-everything-regexp) nil t
)
738 (push (reftex-label-info (reftex-match-string 1) buffer-file-name
)
742 (setq star
(= ?
* (char-after (match-end 3)))
743 entry
(reftex-section-info (buffer-file-name))
745 ;; Insert the section info
746 (push entry
(cdr tail
))
748 ;; We are done unless we use section numbers
749 (unless (nth 1 reftex-label-menu-flags
) (throw 'exit nil
))
751 ;; Update the remaining toc items
752 (setq tail
(cdr tail
))
753 (while (and (setq tail
(memq (assq 'toc
(cdr tail
)) tail
))
754 (setq entry
(car tail
))
755 (>= (nth 5 entry
) level
))
756 (setq star
(string-match "\\*" (nth 6 entry
))
757 context
(nth 2 entry
)
759 (reftex-section-number (nth 5 entry
) star
))
760 (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)"
762 (when (and (not appendix
)
763 (>= (string-to-char (match-string 2)) ?A
))
764 ;; Just entered the appendix. Get out.
767 ;; Change the section number.
769 (concat (match-string 1 context
)
771 (match-string 3 context
))))))
774 (and reftex-support-index
775 (setq entry
(reftex-index-info-safe buffer-file-name
))
776 ;; FIXME: (add-to-list 'reftex--index-tags (nth 1 index-entry))
777 (push entry
(cdr tail
))))))))))
782 (defsubst reftex-move-to-previous-arg
(&optional bound
)
783 "Assuming that we are in front of a macro argument,
784 move backward to the closing parenthesis of the previous argument.
785 This function understands the splitting of macros over several lines
789 ((memq (preceding-char) '(?\
] ?\
})))
791 ((and reftex-allow-detached-macro-args
793 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t
))
794 (goto-char (1+ (match-beginning 0)))
799 (defun reftex-what-macro-safe (which &optional bound
)
800 "Call `reftex-what-macro' with special syntax table."
801 (reftex-with-special-syntax
802 (reftex-what-macro which bound
)))
805 (defun reftex-what-macro (which &optional bound
)
806 "Find out if point is within the arguments of any TeX-macro.
807 The return value is either (\"\\macro\" . (point)) or a list of them.
809 If WHICH is nil, immediately return nil.
810 If WHICH is 1, return innermost enclosing macro.
811 If WHICH is t, return list of all macros enclosing point.
812 If WHICH is a list of macros, look only for those macros and return the
813 name of the first macro in this list found to enclose point.
814 If the optional BOUND is an integer, bound backwards directed
815 searches to this point. If it is nil, limit to nearest \\section -
818 This function is pretty stable, but can be fooled if the text contains
819 things like \\macro{aa}{bb} where \\macro is defined to take only one
820 argument. As RefTeX cannot know this, the string \"bb\" would still be
821 considered an argument of macro \\macro."
822 (unless reftex-section-regexp
(reftex-compile-variables))
824 (if (null which
) (throw 'exit nil
))
825 (let ((bound (or bound
(save-excursion (re-search-backward
826 reftex-section-regexp nil
1)
828 pos cmd-list cmd cnt cnt-opt entry
)
831 (narrow-to-region (max (point-min) bound
) (point-max))
832 ;; move back out of the current parenthesis
833 (while (condition-case nil
834 (let ((forward-sexp-function nil
))
837 (setq cnt
1 cnt-opt
0)
838 ;; move back over any touching sexps
839 (while (and (reftex-move-to-previous-arg bound
)
841 (let ((forward-sexp-function nil
))
844 (if (eq (following-char) ?\
[) (incf cnt-opt
))
847 (when (and (or (= (following-char) ?\
[)
848 (= (following-char) ?\
{))
849 (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t
))
850 (setq cmd
(reftex-match-string 0))
851 (when (looking-at "\\\\begin{[^}]*}")
852 (setq cmd
(reftex-match-string 0)
854 ;; This does ignore optional arguments. Very hard to fix.
855 (when (setq entry
(assoc cmd reftex-env-or-mac-alist
))
856 (if (> cnt
(or (nth 4 entry
) 100))
861 (push (cons cmd
(point)) cmd-list
))
862 ((or (eq 1 which
) (member cmd which
))
863 (throw 'exit
(cons cmd
(point))))))
865 (nreverse cmd-list
)))))
868 (defun reftex-what-environment (which &optional bound
)
869 "Find out if point is inside a LaTeX environment.
870 The return value is (e.g.) either (\"equation\" . (point)) or a list of
873 If WHICH is nil, immediately return nil.
874 If WHICH is 1, return innermost enclosing environment.
875 If WHICH is t, return list of all environments enclosing point.
876 If WHICH is a list of environments, look only for those environments and
877 return the name of the first environment in this list found to enclose
880 If the optional BOUND is an integer, bound backwards directed searches to
881 this point. If it is nil, limit to nearest \\section - like statement."
882 (unless reftex-section-regexp
(reftex-compile-variables))
885 (if (null which
) (throw 'exit nil
))
886 (let ((bound (or bound
(save-excursion (re-search-backward
887 reftex-section-regexp nil
1)
889 env-list end-list env
)
890 (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}"
892 (setq env
(buffer-substring-no-properties
893 (match-beginning 2) (match-end 2)))
895 ((string= (match-string 1) "end")
897 ((equal env
(car end-list
))
898 (setq end-list
(cdr end-list
)))
900 (push (cons env
(point)) env-list
))
901 ((or (eq 1 which
) (member env which
))
902 (throw 'exit
(cons env
(point))))))
903 (nreverse env-list
)))))
906 (defun reftex-what-special-env (which &optional bound
)
907 "Run the special environment parsers and return the matches.
909 The return value is (e.g.) either (\"my-parser-function\" . (point))
912 If WHICH is nil, immediately return nil.
913 If WHICH is 1, return innermost enclosing environment.
914 If WHICH is t, return list of all environments enclosing point.
915 If WHICH is a list of environments, look only for those environments and
916 return the name of the first environment in this list found to enclose
918 (unless reftex-section-regexp
(reftex-compile-variables))
921 (if (null reftex-special-env-parsers
) (throw 'exit nil
))
922 (if (null which
) (throw 'exit nil
))
923 (let ((bound (or bound
(save-excursion (re-search-backward
924 reftex-section-regexp nil
1)
926 (fun-list (if (listp which
)
927 (mapcar (lambda (x) (if (memq x which
) x nil
))
928 reftex-special-env-parsers
)
929 reftex-special-env-parsers
))
931 ;; Call all functions
932 (setq specials
(mapcar
935 (setq rtn
(and fun
(funcall fun bound
)))
936 (if rtn
(cons (symbol-name fun
) rtn
) nil
)))
938 ;; Delete the non-matches
939 (setq specials
(delq nil specials
))
941 (setq specials
(sort specials
(lambda (a b
) (> (cdr a
) (cdr b
)))))
946 (defsubst reftex-move-to-next-arg
(&optional _ignore
)
947 "Assuming that we are at the end of a macro name or a macro argument,
948 move forward to the opening parenthesis of the next argument.
949 This function understands the splitting of macros over several lines
953 ((memq (following-char) '(?\
[ ?\
{)))
955 ((and reftex-allow-detached-macro-args
956 (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]"))
957 (goto-char (1- (match-end 0)))
961 (defun reftex-nth-arg-wrapper (key)
962 (let ((entry (assoc key reftex-env-or-mac-alist
)))
963 (reftex-nth-arg (nth 5 entry
) (nth 6 entry
))))
966 (defun reftex-nth-arg (n &optional opt-args
)
967 "Return the Nth following {} or [] parentheses content.
968 OPT-ARGS is a list of argument numbers which are optional."
970 ;; If we are sitting at a macro start, skip to end of macro name.
971 (and (eq (following-char) ?
\\) (skip-chars-forward "a-zA-Z*\\\\"))
974 ;; Special case: Skip all touching arguments
976 (reftex-move-over-touching-args)
977 (reftex-context-substring))
979 ;; Do the real thing.
982 (when (reftex-move-to-next-arg)
985 (while (and (member cnt opt-args
)
986 (eq (following-char) ?\
{))
989 (unless (and (condition-case nil
990 (or (forward-list 1) t
)
992 (reftex-move-to-next-arg)
996 (while (and (memq cnt opt-args
)
997 (eq (following-char) ?\
{))
1000 (> (skip-chars-forward "{\\[") 0))
1001 (reftex-context-substring)
1005 (defun reftex-move-over-touching-args ()
1007 (while (memq (following-char) '(?\
[ ?\
{))
1011 (defun reftex-context-substring (&optional to-end
)
1012 "Return up to 150 chars from point.
1013 When point is just after a { or [, limit string to matching parenthesis"
1016 ;; Environment - find next \end
1017 (buffer-substring-no-properties
1019 (min (+ (point) 150)
1021 ;; FIXME: This is not perfect
1022 (if (re-search-forward "\\\\end{" nil t
)
1025 ((memq (preceding-char) '(?\
{ ?\
[))
1026 ;; Inside a list - get only the list.
1027 (buffer-substring-no-properties
1029 (min (+ (point) 150)
1032 (let ((forward-sexp-function nil
)) ;Unneeded fanciness.
1035 (error (point-max))))))
1037 ;; no list - just grab 150 characters
1038 (buffer-substring-no-properties (point)
1039 (min (+ (point) 150) (point-max))))))
1041 ;; Variable holding the vector with section numbers
1042 (defvar reftex-section-numbers
(make-vector reftex-max-section-depth
0))
1045 (defun reftex-init-section-numbers (&optional toc-entry appendix
)
1046 "Initialize the section numbers with zeros or with what is found in the TOC-ENTRY."
1047 (let* ((level (or (nth 5 toc-entry
) -
1))
1048 (numbers (nreverse (split-string (or (nth 6 toc-entry
) "") "\\.")))
1049 (depth (1- (length reftex-section-numbers
)))
1050 (i depth
) number-string
)
1053 (aset reftex-section-numbers i
0)
1054 (setq number-string
(or (car numbers
) "0"))
1055 (if (string-match "\\`[A-Z]\\'" number-string
)
1056 (aset reftex-section-numbers i
1057 (- (string-to-char number-string
) ?A -
1))
1058 (aset reftex-section-numbers i
(string-to-number number-string
)))
1061 (put 'reftex-section-numbers
'appendix appendix
))
1064 (defun reftex-section-number (&optional level star
)
1065 "Return a string with the current section number.
1066 When LEVEL is non-nil, increase section numbers on that level."
1067 (let* ((depth (1- (length reftex-section-numbers
))) idx n
(string "")
1068 (appendix (get 'reftex-section-numbers
'appendix
))
1069 (partspecial (and (not reftex-part-resets-chapter
)
1071 ;; partspecial means, this is a part statement.
1072 ;; Parts do not reset the chapter counter, and the part number is
1073 ;; not included in the numbering of other sectioning levels.
1075 (when (and (> level -
1) (not star
))
1076 (aset reftex-section-numbers
1077 level
(1+ (aref reftex-section-numbers level
))))
1078 (setq idx
(1+ level
))
1080 (while (<= idx depth
)
1081 (if (or (not partspecial
)
1083 (aset reftex-section-numbers idx
0))
1086 (setq string
(concat "Part " (reftex-roman-number
1087 (aref reftex-section-numbers
0))))
1088 (setq idx
(if reftex-part-resets-chapter
0 1))
1089 (while (<= idx depth
)
1090 (setq n
(aref reftex-section-numbers idx
))
1091 (if (not (and partspecial
(not (equal string
""))))
1092 (setq string
(concat string
(if (not (string= string
"")) "." "")
1093 (int-to-string n
))))
1096 (if (string-match "\\`\\([@0]\\.\\)+" string
)
1097 (setq string
(replace-match "" nil nil string
)))
1098 (if (string-match "\\(\\.0\\)+\\'" string
)
1099 (setq string
(replace-match "" nil nil string
)))
1101 (string-match "\\`[0-9]+" string
))
1105 (1- (+ ?A
(string-to-number (match-string 0 string
)))))
1106 (substring string
(match-end 0))))))
1108 (concat (make-string (1- (length string
)) ?\
) "*")
1111 (defun reftex-roman-number (n)
1112 "Return as a string the roman number equal to N."
1115 (list '((1000 .
"M") ( 900 .
"CM") ( 500 .
"D") ( 400 .
"CD")
1116 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
1117 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
1121 (setq listel
(pop list
)
1125 (setq string
(concat string s
)
1126 nrest
(- nrest i
))))
1129 (provide 'reftex-parse
)
1131 ;;; reftex-parse.el ends here
1134 ;; generated-autoload-file: "reftex.el"