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.
53 When allowed, do only a partial scan from FILE."
55 ;; Normalize the rescan argument
56 (setq rescan
(cond ((eq rescan t
) t
)
58 ((equal rescan
'(4)) t
)
59 ((equal rescan
'(16)) 1)
62 ;; Partial scans only when allowed
63 (unless reftex-enable-partial-scans
68 (let* ((old-list (symbol-value reftex-docstruct-symbol
))
69 (master (reftex-TeX-master-file))
70 (true-master (file-truename master
))
71 (master-dir (file-name-as-directory (file-name-directory master
)))
72 (file (or file
(buffer-file-name)))
73 (true-file (file-truename file
))
74 (bibview-cache (assq 'bibview-cache old-list
))
75 (index-tags (cdr (assq 'index-tags old-list
)))
76 from-file appendix docstruct tmp
)
78 ;; Make sure replacement is really an option here
79 (when (and (eq rescan t
)
80 (not (and (member (list 'bof file
) old-list
)
81 (member (list 'eof file
) old-list
))))
82 ;; Scan whole document because no such file section exists
84 (when (string= true-file true-master
)
85 ;; Scan whole document because this file is the master
88 ;; From which file do we start?
90 (cond ((eq rescan t
) (or file master
))
91 ((eq rescan
1) master
)
92 (t (error "This should not happen (reftex-do-parse)"))))
94 ;; Reset index-tags if we scan everything
95 (if (equal rescan
1) (setq index-tags nil
))
97 ;; Find active toc entry and initialize section-numbers
98 (setq reftex-active-toc
(reftex-last-assoc-before-elt
99 'toc
(list 'bof from-file
) old-list
)
100 appendix
(reftex-last-assoc-before-elt
101 'appendix
(list 'bof from-file
) old-list
))
103 (reftex-init-section-numbers reftex-active-toc appendix
)
106 (message "Scanning entire document...")
107 (message "Scanning document from %s..." from-file
))
109 (reftex-with-special-syntax
110 (save-window-excursion
114 (reftex-parse-from-file
115 from-file docstruct master-dir
))
116 (reftex-kill-temporary-buffers)))))
118 (message "Scanning document... done")
120 ;; Turn the list around.
121 (setq docstruct
(nreverse docstruct
))
124 (setq docstruct
(reftex-replace-label-list-segment
125 old-list docstruct
(eq rescan
1)))
127 ;; Add all missing information
128 (unless (assq 'label-numbers docstruct
)
129 (push (cons 'label-numbers nil
) docstruct
))
130 (unless (assq 'master-dir docstruct
)
131 (push (cons 'master-dir master-dir
) docstruct
))
132 (unless (assq 'bibview-cache docstruct
)
133 (push (cons 'bibview-cache
(cdr bibview-cache
)) docstruct
))
134 (let* ((bof1 (memq (assq 'bof docstruct
) docstruct
))
135 (bof2 (assq 'bof
(cdr bof1
)))
136 (is-multi (not (not (and bof1 bof2
))))
137 (entry (or (assq 'is-multi docstruct
)
138 (car (push (list 'is-multi is-multi
) docstruct
)))))
139 (setcdr entry
(cons is-multi nil
)))
140 (and index-tags
(setq index-tags
(sort index-tags
'string
<)))
141 (let ((index-tag-cell (assq 'index-tags docstruct
)))
143 (setcdr index-tag-cell index-tags
)
144 (push (cons 'index-tags index-tags
) docstruct
)))
145 (unless (assq 'xr docstruct
)
146 (let* ((allxr (reftex-all-assq 'xr-doc docstruct
))
149 (if (setq tmp
(reftex-locate-file (nth 2 x
) "tex"
152 (message "Can't find external document %s"
156 (alist (delq nil alist
))
157 (allprefix (delq nil
(mapcar 'car alist
)))
158 (regexp (if allprefix
160 (mapconcat 'identity allprefix
"\\|")
162 "\\\\\\\\\\\\"))) ; this will never match
163 (push (list 'xr alist regexp
) docstruct
)))
165 (set reftex-docstruct-symbol docstruct
)
166 (put reftex-docstruct-symbol
'modified t
)))
168 (defun reftex-everything-regexp ()
169 (if reftex-support-index
170 reftex-everything-regexp
171 reftex-everything-regexp-no-index
))
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
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
)))
182 (while (setq tmp
(assoc 'bof all
))
183 (setq file
(nth 1 tmp
)
184 all
(cdr (memq tmp all
)))
186 (string-match re file
)
187 (setq file
(substring file
(match-end 0))))
188 (push file file-list
))
189 (nreverse file-list
)))
191 ;; Bound in the caller, reftex-do-parse.
194 (defun reftex-parse-from-file (file docstruct master-dir
)
195 "Scan the buffer for labels and save them in a list."
196 (let ((regexp (reftex-everything-regexp))
198 file-found tmp include-file
201 toc-entry index-entry next-buf buf
)
204 (setq file-found
(reftex-locate-file file
"tex" master-dir
))
205 (if (and (not file-found
)
206 (setq buf
(reftex-get-buffer-visiting file
)))
207 (setq file-found
(buffer-file-name buf
)))
210 (push (list 'file-error file
) docstruct
)
215 (message "Scanning file %s" file
)
218 (reftex-get-file-buffer-force
220 (not (eq t reftex-keep-temporary-buffers
)))))
222 ;; Begin of file mark
223 (setq file
(buffer-file-name))
224 (push (list 'bof file
) docstruct
)
226 (reftex-with-special-syntax
232 (while (re-search-forward regexp nil t
)
238 (when (or (null reftex-label-ignored-macros-and-environments
)
239 ;; \label{} defs should always be honored,
240 ;; just no keyval style [label=foo] defs.
241 (string-equal "\label{" (substring (reftex-match-string 0) 0 7))
242 (if (and (fboundp 'TeX-current-macro
)
243 (fboundp 'LaTeX-current-environment
))
244 (not (or (member (save-match-data (TeX-current-macro))
245 reftex-label-ignored-macros-and-environments
)
246 (member (save-match-data (LaTeX-current-environment))
247 reftex-label-ignored-macros-and-environments
)))
249 (push (reftex-label-info (reftex-match-string 1) file bound
)
255 ;; Use the beginning as bound and not the end
256 ;; (i.e. (point)) because the section command might
257 ;; be the start of the current environment to be
258 ;; found by `reftex-label-info'.
259 (setq bound
(match-beginning 0))
260 ;; The section regexp matches a character at the end
261 ;; we are not interested in. Especially if it is the
262 ;; backslash of a following macro we want to find in
263 ;; the next parsing iteration.
264 (when (eq (char-before) ?
\\) (backward-char))
266 (setq toc-entry
(funcall reftex-section-info-function file
))
268 ;; It can happen that section info returns nil
269 (setq level
(nth 5 toc-entry
))
270 (setq highest-level
(min highest-level level
))
271 (if (= level highest-level
)
274 (car (rassoc level reftex-section-levels-all
))
277 (push toc-entry docstruct
)
278 (setq reftex-active-toc toc-entry
)))
281 ;; It's an include or input
282 (setq include-file
(reftex-match-string 7))
283 ;; Test if this file should be ignored
284 (unless (delq nil
(mapcar
285 (lambda (x) (string-match x include-file
))
286 reftex-no-include-regexps
))
289 (reftex-parse-from-file
291 docstruct master-dir
))))
294 ;; Appendix starts here
295 (reftex-init-section-numbers nil t
)
296 (push (cons 'appendix t
) docstruct
))
300 (when reftex-support-index
301 (setq index-entry
(reftex-index-info file
))
303 (add-to-list 'index-tags
(nth 1 index-entry
))
304 (push index-entry docstruct
))))
307 ;; A macro with label
309 (let* ((mac (reftex-match-string 11))
310 (label (progn (goto-char (match-end 11))
313 (reftex-nth-arg-wrapper
315 (typekey (nth 1 (assoc mac reftex-env-or-mac-alist
)))
316 (entry (progn (if typekey
318 (goto-char (match-end 0))
320 (goto-char (match-end 11))
321 (reftex-move-over-touching-args))
323 label file bound nil nil
))))
324 (push entry docstruct
))))
325 (t (error "This should not happen (reftex-parse-from-file)")))
328 ;; Find bibliography statement
329 (when (setq tmp
(reftex-locate-bibliography-files master-dir
))
330 (push (cons 'bib tmp
) docstruct
))
333 (when (re-search-forward
334 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t
)
335 (push (cons 'thebib file
) docstruct
))
337 ;; Find external document specifications
339 (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t
)
340 (push (list 'xr-doc
(reftex-match-string 2)
341 (reftex-match-string 3))
345 (push (list 'eof file
) docstruct
)))))
347 ;; Kill the scanned buffer
348 (reftex-kill-temporary-buffers next-buf
))
353 (defun reftex-using-biblatex-p ()
354 "Return non-nil iff we are using biblatex rather than bibtex."
355 (if (boundp 'TeX-active-styles
)
356 ;; the sophisticated AUCTeX way
357 (member "biblatex" TeX-active-styles
)
358 ;; poor-man's check...
360 (re-search-forward "^[^%]*\\\\usepackage.*{biblatex}" nil t
))))
362 (defun reftex-locate-bibliography-files (master-dir &optional files
)
363 "Scan buffer for bibliography macros and return file list."
366 (goto-char (point-min))
367 ;; when biblatex is used, multiple \bibliography or
368 ;; \addbibresource macros are allowed. With plain bibtex, only
369 ;; the first is used.
370 (let ((using-biblatex (reftex-using-biblatex-p))
375 ;; "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\("
376 "\\(^\\)[^%\n\r]*\\\\\\("
377 (mapconcat 'identity reftex-bibliography-commands
"\\|")
378 "\\)\\(\\[.+?\\]\\)?{[ \t]*\\([^}]+\\)") nil t
))
381 (split-string (reftex-match-string 4)
382 "[ \t\n\r]*,[ \t\n\r]*")))
383 (unless using-biblatex
384 (setq again nil
))))))
389 (if (or (member x reftex-bibfile-ignore-list
)
390 (delq nil
(mapcar (lambda (re) (string-match re x
))
391 reftex-bibfile-ignore-regexps
)))
395 (reftex-locate-file x
"bib" master-dir
)))
399 (defun reftex-replace-label-list-segment (old insert
&optional entirely
)
400 "Replace the segment in OLD which corresponds to INSERT.
401 Works with side effects, directly changes old.
402 If ENTIRELY is t, just return INSERT.
403 This function also makes sure the old toc markers do not point anywhere."
407 (reftex-silence-toc-markers old
(length old
))
410 (file (nth 1 (car insert
)))
411 (eof-list (member (list 'eof file
) old
))
412 (bof-list (member (list 'bof file
) old
))
414 (if (not (and bof-list eof-list
))
415 (error "Cannot splice")
417 (reftex-silence-toc-markers bof-list
(- (length bof-list
)
419 (setq n
(- (length old
) (length bof-list
)))
420 (setcdr (nthcdr n new
) (cdr insert
))
421 (setcdr (nthcdr (1- (length new
)) new
) (cdr eof-list
)))
424 (defun reftex-section-info (file)
425 "Return a section entry for the current match.
426 Careful: This function expects the match-data to be still in place!"
427 (let* ((marker (set-marker (make-marker) (1- (match-beginning 3))))
428 (macro (reftex-match-string 3))
429 (prefix (save-match-data
430 (if (string-match "begin{\\([^}]+\\)}" macro
)
431 (match-string 1 macro
))))
432 (level-exp (cdr (assoc macro reftex-section-levels-all
)))
433 (level (if (symbolp level-exp
)
434 (save-match-data (funcall level-exp
))
436 (star (= ?
* (char-after (match-end 3))))
437 (unnumbered (or star
(< level
0)))
439 (section-number (reftex-section-number level unnumbered
))
440 (text1 (save-match-data
442 (reftex-context-substring prefix
))))
443 (literal (buffer-substring-no-properties
444 (1- (match-beginning 3))
445 (min (point-max) (+ (match-end 0) (length text1
) 1))))
446 ;; Literal can be too short since text1 too short. No big problem.
447 (text (reftex-nicify-text text1
)))
449 ;; Add section number and indentation
452 (make-string (* reftex-level-indent level
) ?\
)
453 (if (nth 1 reftex-label-menu-flags
) ; section number flag
454 (concat section-number
" "))
455 (if prefix
(concat (capitalize prefix
) ": ") "")
457 (list 'toc
"toc" text file marker level section-number
458 literal
(marker-position marker
))))
460 (defun reftex-ensure-index-support (&optional abort
)
461 "When index support is turned off, ask to turn it on and
462 set the current prefix argument so that `reftex-access-scan-info'
463 will rescan the entire document."
465 (reftex-support-index t
)
466 ((y-or-n-p "Turn on index support and rescan entire document? ")
467 (setq reftex-support-index
'demanded
468 current-prefix-arg
'(16)))
470 (error "No index support")
471 (message "No index support")
475 (defun reftex-index-info-safe (file)
476 (reftex-with-special-syntax
477 (reftex-index-info file
)))
480 (defun reftex-index-info (file)
481 "Return an index entry for the current match.
482 Careful: This function expects the match-data to be still in place!"
484 (let* ((macro (reftex-match-string 10))
485 (bom (match-beginning 10))
487 (entry (or (assoc macro reftex-index-macro-alist
)
489 (exclude (nth 3 entry
))
490 ;; The following is a test if this match should be excluded
491 (test-dummy (and (fboundp exclude
)
495 (prefix (nth 2 entry
))
497 (cond ((stringp itag
) itag
)
499 (progn (goto-char boa
)
500 (or (reftex-nth-arg itag
(nth 6 entry
)) "idx")))
502 (arg (or (progn (goto-char boa
)
503 (reftex-nth-arg (nth 5 entry
) (nth 6 entry
)))
505 (end-of-args (progn (goto-char boa
)
506 (reftex-move-over-touching-args)
508 (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point)))
510 (progn (goto-char bom
)
511 (skip-chars-backward "^ \t\r\n")
513 (context (buffer-substring-no-properties
514 begin-of-context end-of-context
))
515 (key-end (if (string-match reftex-index-key-end-re arg
)
516 (1+ (match-beginning 0))))
517 (rawkey (substring arg
0 key-end
))
519 (key (if prefix
(concat prefix rawkey
) rawkey
))
520 (sortkey (downcase key
))
521 (showkey (mapconcat 'identity
522 (split-string key reftex-index-level-re
)
524 (goto-char end-of-args
)
525 ;; 0 1 2 3 4 5 6 7 8 9
526 (list 'index index-tag context file bom arg key showkey sortkey key-end
))))
528 (defun reftex-short-context (env parse
&optional bound derive
)
529 "Get about one line of useful context for the label definition at point."
532 (setq parse
(if derive
(cdr parse
) (car parse
))))
540 (reftex-context-substring)))
543 (if (string= env
"section")
544 ;; special treatment for section labels
546 (if (and (re-search-backward reftex-section-or-include-regexp
550 (goto-char (match-end 0))
551 (reftex-context-substring))
552 (if reftex-active-toc
554 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc
))
555 (match-string 1 (nth 7 reftex-active-toc
)))
556 "SECTION HEADING NOT FOUND")))
558 (goto-char reftex-default-context-position
)
559 (unless (eq (string-to-char env
) ?
\\)
560 (reftex-move-over-touching-args))
561 (reftex-context-substring))))
565 (if (re-search-backward parse bound t
)
567 (goto-char (match-end 0))
568 (reftex-context-substring))
569 "NO MATCH FOR CONTEXT REGEXP")))
573 (goto-char reftex-default-context-position
)
576 (nth 6 (assoc env reftex-env-or-mac-alist
))))
580 ;; A hook function. Call it.
582 (condition-case error-var
584 (error (format "HOOK ERROR: %s" (cdr error-var
))))))
586 "INVALID VALUE OF PARSE"))))
588 (defun reftex-where-am-I ()
589 "Return the docstruct entry above point.
590 Actually returns a cons cell in which the cdr is a flag indicating
591 if the information is exact (t) or approximate (nil)."
593 (let ((docstruct (symbol-value reftex-docstruct-symbol
))
594 (cnt 0) rtn rtn-if-no-other
599 (setq found
(re-search-backward (reftex-everything-regexp) nil t
))
605 (car (member (list 'bof
(buffer-file-name)) docstruct
))
607 (assq 'bof docstruct
) ;; for safety reasons
611 (assoc (reftex-match-string 1)
612 (symbol-value reftex-docstruct-symbol
)))
615 (goto-char (1- (match-beginning 3)))
616 (let* ((list (member (list 'bof
(buffer-file-name))
618 (endelt (car (member (list 'eof
(buffer-file-name))
621 (while (and list
(not (eq endelt
(car list
))))
622 (if (and (eq (car (car list
)) 'toc
)
623 (string= (buffer-file-name)
627 (or (and (markerp (nth 4 (car list
)))
628 (marker-position (nth 4 (car list
))))
630 ;; Fits with marker position or recorded position
631 (setq rtn1
(car list
) list nil
))
632 ((looking-at (reftex-make-regexp-allow-for-ctrl-m
634 ;; Same title: remember, but keep looking
635 (setq rtn-if-no-other
(car list
)))))
639 ;; Input or include...
641 (member (list 'eof
(reftex-locate-file
642 (reftex-match-string 7) "tex"
643 (cdr (assq 'master-dir docstruct
))))
646 (assq 'appendix
(symbol-value reftex-docstruct-symbol
)))
649 (when reftex-support-index
650 (let* ((index-info (save-excursion
651 (reftex-index-info-safe nil
)))
652 (list (member (list 'bof
(buffer-file-name))
654 (endelt (car (member (list 'eof
(buffer-file-name))
656 dist last-dist last
(n 0))
657 ;; Check all index entries with equal text
658 (while (and list
(not (eq endelt
(car list
))))
659 (when (and (eq (car (car list
)) 'index
)
660 (string= (nth 2 index-info
)
663 (setq dist
(abs (- (point) (nth 4 (car list
)))))
664 (if (or (not last-dist
) (< dist last-dist
))
665 (setq last-dist dist last
(car list
))))
666 (setq list
(cdr list
)))
667 ;; We are sure if we have only one, or a zero distance
668 (cond ((or (= n
1) (equal dist
0)) last
)
669 ((> n
1) (setq cnt
2) last
)
673 (goto-char (match-end 11))
674 (assoc (reftex-no-props
675 (reftex-nth-arg-wrapper
676 (reftex-match-string 11)))
677 (symbol-value reftex-docstruct-symbol
))))
679 (error "This should not happen (reftex-where-am-I)"))))))
680 ;; Check if there was only a by-name match for the section.
681 (when (and (not rtn
) rtn-if-no-other
)
682 (setq rtn rtn-if-no-other
684 (cons rtn
(eq cnt
1))))
686 (defun reftex-notice-new (&optional n force
)
687 "Hook to handshake with RefTeX after something new has been inserted."
688 ;; Add a new entry to the docstruct list. If it is a section, renumber
689 ;; the following sections.
690 ;; FIXME: Put in a WHAT parameter and search backward until one is found.
691 ;; When N is given, go back that many matches of reftex-everything-regexp
692 ;; When FORCE is non-nil, also insert if `reftex-where-am-I' was uncertain.
695 (unless reftex-mode
(throw 'exit nil
))
696 (reftex-access-scan-info)
697 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
698 here-I-am appendix tail entry star level
699 section-number context
)
702 (when (re-search-backward (reftex-everything-regexp) nil t
(or n
1))
705 (setq here-I-am
(reftex-where-am-I))
706 (or here-I-am
(throw 'exit nil
))
707 (unless (or force
(cdr here-I-am
)) (throw 'exit nil
))
708 (setq tail
(memq (car here-I-am
) docstruct
))
709 (or tail
(throw 'exit nil
))
710 (setq reftex-active-toc
(reftex-last-assoc-before-elt
711 'toc
(car here-I-am
) docstruct
)
712 appendix
(reftex-last-assoc-before-elt
713 'appendix
(car here-I-am
) docstruct
))
715 ;; Initialize section numbers
716 (if (eq (car (car here-I-am
)) 'appendix
)
717 (reftex-init-section-numbers nil t
)
718 (reftex-init-section-numbers reftex-active-toc appendix
))
720 ;; Match the section command
721 (when (re-search-forward (reftex-everything-regexp) nil t
)
724 (push (reftex-label-info (reftex-match-string 1) buffer-file-name
)
728 (setq star
(= ?
* (char-after (match-end 3)))
729 entry
(reftex-section-info (buffer-file-name))
731 ;; Insert the section info
732 (push entry
(cdr tail
))
734 ;; We are done unless we use section numbers
735 (unless (nth 1 reftex-label-menu-flags
) (throw 'exit nil
))
737 ;; Update the remaining toc items
738 (setq tail
(cdr tail
))
739 (while (and (setq tail
(memq (assq 'toc
(cdr tail
)) tail
))
740 (setq entry
(car tail
))
741 (>= (nth 5 entry
) level
))
742 (setq star
(string-match "\\*" (nth 6 entry
))
743 context
(nth 2 entry
)
745 (reftex-section-number (nth 5 entry
) star
))
746 (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)"
748 (when (and (not appendix
)
749 (>= (string-to-char (match-string 2)) ?A
))
750 ;; Just entered the appendix. Get out.
753 ;; Change the section number.
755 (concat (match-string 1 context
)
757 (match-string 3 context
))))))
760 (and reftex-support-index
761 (setq entry
(reftex-index-info-safe buffer-file-name
))
762 ;; FIXME: (add-to-list 'index-tags (nth 1 index-entry))
763 (push entry
(cdr tail
))))))))))
768 (defsubst reftex-move-to-previous-arg
(&optional bound
)
769 "Assuming that we are in front of a macro argument,
770 move backward to the closing parenthesis of the previous argument.
771 This function understands the splitting of macros over several lines
775 ((memq (preceding-char) '(?\
] ?\
})))
777 ((and reftex-allow-detached-macro-args
779 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t
))
780 (goto-char (1+ (match-beginning 0)))
784 (defun reftex-what-macro-safe (which &optional bound
)
785 "Call `reftex-what-macro' with special syntax table."
786 (reftex-with-special-syntax
787 (reftex-what-macro which bound
)))
789 (defun reftex-what-macro (which &optional bound
)
790 "Find out if point is within the arguments of any TeX-macro.
791 The return value is either (\"\\macro\" . (point)) or a list of them.
793 If WHICH is nil, immediately return nil.
794 If WHICH is 1, return innermost enclosing macro.
795 If WHICH is t, return list of all macros enclosing point.
796 If WHICH is a list of macros, look only for those macros and return the
797 name of the first macro in this list found to enclose point.
798 If the optional BOUND is an integer, bound backwards directed
799 searches to this point. If it is nil, limit to nearest \\section -
802 This function is pretty stable, but can be fooled if the text contains
803 things like \\macro{aa}{bb} where \\macro is defined to take only one
804 argument. As RefTeX cannot know this, the string \"bb\" would still be
805 considered an argument of macro \\macro."
806 (unless reftex-section-regexp
(reftex-compile-variables))
808 (if (null which
) (throw 'exit nil
))
809 (let ((bound (or bound
(save-excursion (re-search-backward
810 reftex-section-regexp nil
1)
812 pos cmd-list cmd cnt cnt-opt entry
)
815 (narrow-to-region (max (point-min) bound
) (point-max))
816 ;; move back out of the current parenthesis
817 (while (condition-case nil
818 (let ((forward-sexp-function nil
))
821 (setq cnt
1 cnt-opt
0)
822 ;; move back over any touching sexps
823 (while (and (reftex-move-to-previous-arg bound
)
825 (let ((forward-sexp-function nil
))
828 (if (eq (following-char) ?\
[) (incf cnt-opt
))
831 (when (and (or (= (following-char) ?\
[)
832 (= (following-char) ?\
{))
833 (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t
))
834 (setq cmd
(reftex-match-string 0))
835 (when (looking-at "\\\\begin{[^}]*}")
836 (setq cmd
(reftex-match-string 0)
838 ;; This does ignore optional arguments. Very hard to fix.
839 (when (setq entry
(assoc cmd reftex-env-or-mac-alist
))
840 (if (> cnt
(or (nth 4 entry
) 100))
845 (push (cons cmd
(point)) cmd-list
))
846 ((or (eq 1 which
) (member cmd which
))
847 (throw 'exit
(cons cmd
(point))))))
849 (nreverse cmd-list
)))))
851 (defun reftex-what-environment (which &optional bound
)
852 "Find out if point is inside a LaTeX environment.
853 The return value is (e.g.) either (\"equation\" . (point)) or a list of
856 If WHICH is nil, immediately return nil.
857 If WHICH is 1, return innermost enclosing environment.
858 If WHICH is t, return list of all environments enclosing point.
859 If WHICH is a list of environments, look only for those environments and
860 return the name of the first environment in this list found to enclose
863 If the optional BOUND is an integer, bound backwards directed searches to
864 this point. If it is nil, limit to nearest \\section - like statement."
865 (unless reftex-section-regexp
(reftex-compile-variables))
868 (if (null which
) (throw 'exit nil
))
869 (let ((bound (or bound
(save-excursion (re-search-backward
870 reftex-section-regexp nil
1)
872 env-list end-list env
)
873 (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}"
875 (setq env
(buffer-substring-no-properties
876 (match-beginning 2) (match-end 2)))
878 ((string= (match-string 1) "end")
880 ((equal env
(car end-list
))
881 (setq end-list
(cdr end-list
)))
883 (push (cons env
(point)) env-list
))
884 ((or (eq 1 which
) (member env which
))
885 (throw 'exit
(cons env
(point))))))
886 (nreverse env-list
)))))
888 (defun reftex-what-special-env (which &optional bound
)
889 "Run the special environment parsers and return the matches.
891 The return value is (e.g.) either (\"my-parser-function\" . (point))
894 If WHICH is nil, immediately return nil.
895 If WHICH is 1, return innermost enclosing environment.
896 If WHICH is t, return list of all environments enclosing point.
897 If WHICH is a list of environments, look only for those environments and
898 return the name of the first environment in this list found to enclose
900 (unless reftex-section-regexp
(reftex-compile-variables))
903 (if (null reftex-special-env-parsers
) (throw 'exit nil
))
904 (if (null which
) (throw 'exit nil
))
905 (let ((bound (or bound
(save-excursion (re-search-backward
906 reftex-section-regexp nil
1)
908 (fun-list (if (listp which
)
909 (mapcar (lambda (x) (if (memq x which
) x nil
))
910 reftex-special-env-parsers
)
911 reftex-special-env-parsers
))
913 ;; Call all functions
914 (setq specials
(mapcar
917 (setq rtn
(and fun
(funcall fun bound
)))
918 (if rtn
(cons (symbol-name fun
) rtn
) nil
)))
920 ;; Delete the non-matches
921 (setq specials
(delq nil specials
))
923 (setq specials
(sort specials
(lambda (a b
) (> (cdr a
) (cdr b
)))))
928 (defsubst reftex-move-to-next-arg
(&optional ignore
)
929 "Assuming that we are at the end of a macro name or a macro argument,
930 move forward to the opening parenthesis of the next argument.
931 This function understands the splitting of macros over several lines
935 ((memq (following-char) '(?\
[ ?\
{)))
937 ((and reftex-allow-detached-macro-args
938 (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]"))
939 (goto-char (1- (match-end 0)))
943 (defun reftex-nth-arg-wrapper (key)
944 (let ((entry (assoc key reftex-env-or-mac-alist
)))
945 (reftex-nth-arg (nth 5 entry
) (nth 6 entry
))))
947 (defun reftex-nth-arg (n &optional opt-args
)
948 "Return the Nth following {} or [] parentheses content.
949 OPT-ARGS is a list of argument numbers which are optional."
951 ;; If we are sitting at a macro start, skip to end of macro name.
952 (and (eq (following-char) ?
\\) (skip-chars-forward "a-zA-Z*\\\\"))
955 ;; Special case: Skip all touching arguments
957 (reftex-move-over-touching-args)
958 (reftex-context-substring))
960 ;; Do the real thing.
963 (when (reftex-move-to-next-arg)
966 (while (and (member cnt opt-args
)
967 (eq (following-char) ?\
{))
970 (unless (and (condition-case nil
971 (or (forward-list 1) t
)
973 (reftex-move-to-next-arg)
977 (while (and (memq cnt opt-args
)
978 (eq (following-char) ?\
{))
981 (> (skip-chars-forward "{\\[") 0))
982 (reftex-context-substring)
985 (defun reftex-move-over-touching-args ()
987 (while (memq (following-char) '(?\
[ ?\
{))
991 (defun reftex-context-substring (&optional to-end
)
992 "Return up to 150 chars from point.
993 When point is just after a { or [, limit string to matching parenthesis"
996 ;; Environment - find next \end
997 (buffer-substring-no-properties
1001 ;; FIXME: This is not perfect
1002 (if (re-search-forward "\\\\end{" nil t
)
1005 ((memq (preceding-char) '(?\
{ ?\
[))
1006 ;; Inside a list - get only the list.
1007 (buffer-substring-no-properties
1009 (min (+ (point) 150)
1012 (let ((forward-sexp-function nil
)) ;Unneeded fanciness.
1015 (error (point-max))))))
1017 ;; no list - just grab 150 characters
1018 (buffer-substring-no-properties (point)
1019 (min (+ (point) 150) (point-max))))))
1021 ;; Variable holding the vector with section numbers
1022 (defvar reftex-section-numbers
(make-vector reftex-max-section-depth
0))
1024 (defun reftex-init-section-numbers (&optional toc-entry appendix
)
1025 "Initialize the section numbers with zeros or with what is found in the TOC-ENTRY."
1026 (let* ((level (or (nth 5 toc-entry
) -
1))
1027 (numbers (nreverse (split-string (or (nth 6 toc-entry
) "") "\\.")))
1028 (depth (1- (length reftex-section-numbers
)))
1029 (i depth
) number-string
)
1032 (aset reftex-section-numbers i
0)
1033 (setq number-string
(or (car numbers
) "0"))
1034 (if (string-match "\\`[A-Z]\\'" number-string
)
1035 (aset reftex-section-numbers i
1036 (- (string-to-char number-string
) ?A -
1))
1037 (aset reftex-section-numbers i
(string-to-number number-string
)))
1040 (put 'reftex-section-numbers
'appendix appendix
))
1042 (defun reftex-section-number (&optional level star
)
1043 "Return a string with the current section number.
1044 When LEVEL is non-nil, increase section numbers on that level."
1045 (let* ((depth (1- (length reftex-section-numbers
))) idx n
(string "")
1046 (appendix (get 'reftex-section-numbers
'appendix
))
1047 (partspecial (and (not reftex-part-resets-chapter
)
1049 ;; partspecial means, this is a part statement.
1050 ;; Parts do not reset the chapter counter, and the part number is
1051 ;; not included in the numbering of other sectioning levels.
1053 (when (and (> level -
1) (not star
))
1054 (aset reftex-section-numbers
1055 level
(1+ (aref reftex-section-numbers level
))))
1056 (setq idx
(1+ level
))
1058 (while (<= idx depth
)
1059 (if (or (not partspecial
)
1061 (aset reftex-section-numbers idx
0))
1064 (setq string
(concat "Part " (reftex-roman-number
1065 (aref reftex-section-numbers
0))))
1066 (setq idx
(if reftex-part-resets-chapter
0 1))
1067 (while (<= idx depth
)
1068 (setq n
(aref reftex-section-numbers idx
))
1069 (if (not (and partspecial
(not (equal string
""))))
1070 (setq string
(concat string
(if (not (string= string
"")) "." "")
1071 (int-to-string n
))))
1074 (if (string-match "\\`\\([@0]\\.\\)+" string
)
1075 (setq string
(replace-match "" nil nil string
)))
1076 (if (string-match "\\(\\.0\\)+\\'" string
)
1077 (setq string
(replace-match "" nil nil string
)))
1079 (string-match "\\`[0-9]+" string
))
1083 (1- (+ ?A
(string-to-number (match-string 0 string
)))))
1084 (substring string
(match-end 0))))))
1086 (concat (make-string (1- (length string
)) ?\
) "*")
1089 (defun reftex-roman-number (n)
1090 "Return as a string the roman number equal to N."
1093 (list '((1000 .
"M") ( 900 .
"CM") ( 500 .
"D") ( 400 .
"CD")
1094 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
1095 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
1099 (setq listel
(pop list
)
1103 (setq string
(concat string s
)
1104 nrest
(- nrest i
))))
1107 (provide 'reftex-parse
)
1109 ;;; reftex-parse.el ends here