Trim whitespace in lm-keywords-list
[emacs.git] / lisp / textmodes / reftex-parse.el
blobffabb134c83a4f78aa3c9db777551ddd8e530bf3
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
29 (require 'reftex)
31 (defmacro reftex-with-special-syntax (&rest body)
32 `(let ((saved-syntax (syntax-table)))
33 (unwind-protect
34 (progn
35 (set-syntax-table reftex-syntax-table)
36 (let ((case-fold-search nil))
37 ,@body))
38 (set-syntax-table saved-syntax))))
40 (defun reftex-parse-one ()
41 "Re-parse this file."
42 (interactive)
43 (let ((reftex-enable-partial-scans t))
44 (reftex-access-scan-info '(4))))
46 (defun reftex-parse-all ()
47 "Re-parse entire document."
48 (interactive)
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)
57 ((eq rescan 1) 1)
58 ((equal rescan '(4)) t)
59 ((equal rescan '(16)) 1)
60 (t 1)))
62 ;; Partial scans only when allowed
63 (unless reftex-enable-partial-scans
64 (setq rescan 1))
66 ;; Do the scanning.
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
83 (setq rescan 1))
84 (when (string= true-file true-master)
85 ;; Scan whole document because this file is the master
86 (setq rescan 1))
88 ;; From which file do we start?
89 (setq from-file
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)
105 (if (eq rescan 1)
106 (message "Scanning entire document...")
107 (message "Scanning document from %s..." from-file))
109 (reftex-with-special-syntax
110 (save-window-excursion
111 (save-excursion
112 (unwind-protect
113 (setq docstruct
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))
123 ;; Set or insert
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)))
142 (if index-tag-cell
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))
147 (alist (mapcar
148 (lambda (x)
149 (if (setq tmp (reftex-locate-file (nth 2 x) "tex"
150 master-dir))
151 (cons (nth 1 x) tmp)
152 (message "Can't find external document %s"
153 (nth 2 x))
154 nil))
155 allxr))
156 (alist (delq nil alist))
157 (allprefix (delq nil (mapcar 'car alist)))
158 (regexp (if allprefix
159 (concat "\\`\\("
160 (mapconcat 'identity allprefix "\\|")
161 "\\)")
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))
173 ;;;###autoload
174 (defun reftex-all-document-files (&optional relative)
175 "Return a list of all files belonging to the current document.
176 When RELATIVE is non-nil, give file names relative to directory
177 of master file."
178 (let* ((all (symbol-value reftex-docstruct-symbol))
179 (master-dir (file-name-directory (reftex-TeX-master-file)))
180 (re (concat "\\`" (regexp-quote master-dir)))
181 file-list tmp file)
182 (while (setq tmp (assoc 'bof all))
183 (setq file (nth 1 tmp)
184 all (cdr (memq tmp all)))
185 (and relative
186 (string-match re file)
187 (setq file (substring file (match-end 0))))
188 (push file file-list))
189 (nreverse file-list)))
191 ;; Bound in the caller, reftex-do-parse.
192 (defvar index-tags)
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))
197 (bound 0)
198 file-found tmp include-file
199 (level 1)
200 (highest-level 100)
201 toc-entry index-entry next-buf buf)
203 (catch 'exit
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)))
209 (unless file-found
210 (push (list 'file-error file) docstruct)
211 (throw 'exit nil))
213 (save-excursion
215 (message "Scanning file %s" file)
216 (set-buffer
217 (setq next-buf
218 (reftex-get-file-buffer-force
219 file-found
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
227 (save-excursion
228 (save-restriction
229 (widen)
230 (goto-char 1)
232 (while (re-search-forward regexp nil t)
234 (cond
236 ((match-end 1)
237 ;; It is a label
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)
250 docstruct)))
252 ((match-end 3)
253 ;; It is a section
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))
265 ;; Insert in List
266 (setq toc-entry (funcall reftex-section-info-function file))
267 (when toc-entry
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)
272 (message
273 "Scanning %s %s ..."
274 (car (rassoc level reftex-section-levels-all))
275 (nth 6 toc-entry)))
277 (push toc-entry docstruct)
278 (setq reftex-active-toc toc-entry)))
280 ((match-end 7)
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))
287 ;; Parse it
288 (setq docstruct
289 (reftex-parse-from-file
290 include-file
291 docstruct master-dir))))
293 ((match-end 9)
294 ;; Appendix starts here
295 (reftex-init-section-numbers nil t)
296 (push (cons 'appendix t) docstruct))
298 ((match-end 10)
299 ;; Index entry
300 (when reftex-support-index
301 (setq index-entry (reftex-index-info file))
302 (when index-entry
303 (add-to-list 'index-tags (nth 1 index-entry))
304 (push index-entry docstruct))))
306 ((match-end 11)
307 ;; A macro with label
308 (save-excursion
309 (let* ((mac (reftex-match-string 11))
310 (label (progn (goto-char (match-end 11))
311 (save-match-data
312 (reftex-no-props
313 (reftex-nth-arg-wrapper
314 mac)))))
315 (typekey (nth 1 (assoc mac reftex-env-or-mac-alist)))
316 (entry (progn (if typekey
317 ;; A typing macro
318 (goto-char (match-end 0))
319 ;; A neutral macro
320 (goto-char (match-end 11))
321 (reftex-move-over-touching-args))
322 (reftex-label-info
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))
332 (goto-char 1)
333 (when (re-search-forward
334 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t)
335 (push (cons 'thebib file) docstruct))
337 ;; Find external document specifications
338 (goto-char 1)
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))
342 docstruct))
344 ;; End of file mark
345 (push (list 'eof file) docstruct)))))
347 ;; Kill the scanned buffer
348 (reftex-kill-temporary-buffers next-buf))
350 ;; Return the list
351 docstruct))
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...
359 (save-excursion
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."
364 (unless files
365 (save-excursion
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))
371 (again t))
372 (while (and again
373 (re-search-forward
374 (concat
375 ;; "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\("
376 "\\(^\\)[^%\n\r]*\\\\\\("
377 (mapconcat 'identity reftex-bibliography-commands "\\|")
378 "\\)\\(\\[.+?\\]\\)?{[ \t]*\\([^}]+\\)") nil t))
379 (setq files
380 (append files
381 (split-string (reftex-match-string 4)
382 "[ \t\n\r]*,[ \t\n\r]*")))
383 (unless using-biblatex
384 (setq again nil))))))
385 (when files
386 (setq files
387 (mapcar
388 (lambda (x)
389 (if (or (member x reftex-bibfile-ignore-list)
390 (delq nil (mapcar (lambda (re) (string-match re x))
391 reftex-bibfile-ignore-regexps)))
392 ;; excluded file
394 ;; find the file
395 (reftex-locate-file x "bib" master-dir)))
396 files))
397 (delq nil files)))
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."
405 (cond
406 (entirely
407 (reftex-silence-toc-markers old (length old))
408 insert)
409 (t (let* ((new 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")
416 ;; Splice
417 (reftex-silence-toc-markers bof-list (- (length bof-list)
418 (length eof-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)))
422 new))))
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))
435 level-exp))
436 (star (= ?* (char-after (match-end 3))))
437 (unnumbered (or star (< level 0)))
438 (level (abs level))
439 (section-number (reftex-section-number level unnumbered))
440 (text1 (save-match-data
441 (save-excursion
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
450 (setq text
451 (concat
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) ": ") "")
456 text))
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."
464 (cond
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)))
469 (t (if abort
470 (error "No index support")
471 (message "No index support")
472 (ding)
473 (sit-for 1)))))
475 (defun reftex-index-info-safe (file)
476 (reftex-with-special-syntax
477 (reftex-index-info file)))
479 (defvar test-dummy)
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!"
483 (catch 'exit
484 (let* ((macro (reftex-match-string 10))
485 (bom (match-beginning 10))
486 (boa (match-end 10))
487 (entry (or (assoc macro reftex-index-macro-alist)
488 (throw 'exit nil)))
489 (exclude (nth 3 entry))
490 ;; The following is a test if this match should be excluded
491 (test-dummy (and (fboundp exclude)
492 (funcall exclude)
493 (throw 'exit nil)))
494 (itag (nth 1 entry))
495 (prefix (nth 2 entry))
496 (index-tag
497 (cond ((stringp itag) itag)
498 ((integerp itag)
499 (progn (goto-char boa)
500 (or (reftex-nth-arg itag (nth 6 entry)) "idx")))
501 (t "idx")))
502 (arg (or (progn (goto-char boa)
503 (reftex-nth-arg (nth 5 entry) (nth 6 entry)))
504 ""))
505 (end-of-args (progn (goto-char boa)
506 (reftex-move-over-touching-args)
507 (point)))
508 (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point)))
509 (begin-of-context
510 (progn (goto-char bom)
511 (skip-chars-backward "^ \t\r\n")
512 (point)))
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)
523 " ! ")))
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."
531 (if (consp parse)
532 (setq parse (if derive (cdr parse) (car parse))))
534 (reftex-nicify-text
536 (cond
538 ((null parse)
539 (save-excursion
540 (reftex-context-substring)))
542 ((eq parse t)
543 (if (string= env "section")
544 ;; special treatment for section labels
545 (save-excursion
546 (if (and (re-search-backward reftex-section-or-include-regexp
547 (point-min) t)
548 (match-end 2))
549 (progn
550 (goto-char (match-end 0))
551 (reftex-context-substring))
552 (if reftex-active-toc
553 (progn
554 (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc))
555 (match-string 1 (nth 7 reftex-active-toc)))
556 "SECTION HEADING NOT FOUND")))
557 (save-excursion
558 (goto-char reftex-default-context-position)
559 (unless (eq (string-to-char env) ?\\)
560 (reftex-move-over-touching-args))
561 (reftex-context-substring))))
563 ((stringp parse)
564 (save-excursion
565 (if (re-search-backward parse bound t)
566 (progn
567 (goto-char (match-end 0))
568 (reftex-context-substring))
569 "NO MATCH FOR CONTEXT REGEXP")))
571 ((integerp parse)
572 (or (save-excursion
573 (goto-char reftex-default-context-position)
574 (reftex-nth-arg
575 parse
576 (nth 6 (assoc env reftex-env-or-mac-alist))))
577 ""))
579 ((fboundp parse)
580 ;; A hook function. Call it.
581 (save-excursion
582 (condition-case error-var
583 (funcall parse env)
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
595 found)
596 (save-excursion
597 (while (not rtn)
598 (incf cnt)
599 (setq found (re-search-backward (reftex-everything-regexp) nil t))
600 (setq rtn
601 (cond
602 ((not found)
603 ;; no match
605 (car (member (list 'bof (buffer-file-name)) docstruct))
606 (not (setq cnt 2))
607 (assq 'bof docstruct) ;; for safety reasons
608 'corrupted))
609 ((match-end 1)
610 ;; Label
611 (assoc (reftex-match-string 1)
612 (symbol-value reftex-docstruct-symbol)))
613 ((match-end 3)
614 ;; Section
615 (goto-char (1- (match-beginning 3)))
616 (let* ((list (member (list 'bof (buffer-file-name))
617 docstruct))
618 (endelt (car (member (list 'eof (buffer-file-name))
619 list)))
620 rtn1)
621 (while (and list (not (eq endelt (car list))))
622 (if (and (eq (car (car list)) 'toc)
623 (string= (buffer-file-name)
624 (nth 3 (car list))))
625 (cond
626 ((equal (point)
627 (or (and (markerp (nth 4 (car list)))
628 (marker-position (nth 4 (car list))))
629 (nth 8 (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
633 (nth 7 (car list))))
634 ;; Same title: remember, but keep looking
635 (setq rtn-if-no-other (car list)))))
636 (pop list))
637 rtn1))
638 ((match-end 7)
639 ;; Input or include...
640 (car
641 (member (list 'eof (reftex-locate-file
642 (reftex-match-string 7) "tex"
643 (cdr (assq 'master-dir docstruct))))
644 docstruct)))
645 ((match-end 9)
646 (assq 'appendix (symbol-value reftex-docstruct-symbol)))
647 ((match-end 10)
648 ;; Index entry
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))
653 docstruct))
654 (endelt (car (member (list 'eof (buffer-file-name))
655 list)))
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)
661 (nth 2 (car list))))
662 (incf n)
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)
670 (t nil)))))
671 ((match-end 11)
672 (save-excursion
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
683 cnt 2))
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.
693 (condition-case nil
694 (catch 'exit
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)
701 (save-excursion
702 (when (re-search-backward (reftex-everything-regexp) nil t (or n 1))
704 ;; Find where we are
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)
722 (cond
723 ((match-end 1)
724 (push (reftex-label-info (reftex-match-string 1) buffer-file-name)
725 (cdr tail)))
727 ((match-end 3)
728 (setq star (= ?* (char-after (match-end 3)))
729 entry (reftex-section-info (buffer-file-name))
730 level (nth 5 entry))
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)
744 section-number
745 (reftex-section-number (nth 5 entry) star))
746 (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)"
747 context)
748 (when (and (not appendix)
749 (>= (string-to-char (match-string 2)) ?A))
750 ;; Just entered the appendix. Get out.
751 (throw 'exit nil))
753 ;; Change the section number.
754 (setf (nth 2 entry)
755 (concat (match-string 1 context)
756 section-number
757 (match-string 3 context))))))
758 ((match-end 10)
759 ;; Index entry
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))))))))))
765 (error nil))
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
772 in TeX."
773 (cond
774 ;; Just to be quick:
775 ((memq (preceding-char) '(?\] ?\})))
776 ;; Do a search
777 ((and reftex-allow-detached-macro-args
778 (re-search-backward
779 "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t))
780 (goto-char (1+ (match-beginning 0)))
782 (t nil)))
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 -
800 like statement.
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))
807 (catch 'exit
808 (if (null which) (throw 'exit nil))
809 (let ((bound (or bound (save-excursion (re-search-backward
810 reftex-section-regexp nil 1)
811 (point))))
812 pos cmd-list cmd cnt cnt-opt entry)
813 (save-restriction
814 (save-excursion
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))
819 (up-list -1) t)
820 (error nil))
821 (setq cnt 1 cnt-opt 0)
822 ;; move back over any touching sexps
823 (while (and (reftex-move-to-previous-arg bound)
824 (condition-case nil
825 (let ((forward-sexp-function nil))
826 (backward-sexp) t)
827 (error nil)))
828 (if (eq (following-char) ?\[) (incf cnt-opt))
829 (incf cnt))
830 (setq pos (point))
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)
837 cnt (1- cnt)))
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))
841 (setq cmd nil)))
842 (cond
843 ((null cmd))
844 ((eq t which)
845 (push (cons cmd (point)) cmd-list))
846 ((or (eq 1 which) (member cmd which))
847 (throw 'exit (cons cmd (point))))))
848 (goto-char pos)))
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
854 them.
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
861 point.
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))
866 (catch 'exit
867 (save-excursion
868 (if (null which) (throw 'exit nil))
869 (let ((bound (or bound (save-excursion (re-search-backward
870 reftex-section-regexp nil 1)
871 (point))))
872 env-list end-list env)
873 (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}"
874 bound t)
875 (setq env (buffer-substring-no-properties
876 (match-beginning 2) (match-end 2)))
877 (cond
878 ((string= (match-string 1) "end")
879 (push env end-list))
880 ((equal env (car end-list))
881 (setq end-list (cdr end-list)))
882 ((eq t which)
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))
892 or a list of them.
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
899 point."
900 (unless reftex-section-regexp (reftex-compile-variables))
901 (catch 'exit
902 (save-excursion
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)
907 (point))))
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))
912 specials rtn)
913 ;; Call all functions
914 (setq specials (mapcar
915 (lambda (fun)
916 (save-excursion
917 (setq rtn (and fun (funcall fun bound)))
918 (if rtn (cons (symbol-name fun) rtn) nil)))
919 fun-list))
920 ;; Delete the non-matches
921 (setq specials (delq nil specials))
922 ;; Sort
923 (setq specials (sort specials (lambda (a b) (> (cdr a) (cdr b)))))
924 (if (eq which t)
925 specials
926 (car specials))))))
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
932 in TeX."
933 (cond
934 ;; Just to be quick:
935 ((memq (following-char) '(?\[ ?\{)))
936 ;; Do a search
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)))
941 (t nil)))
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*\\\\"))
954 (if (= n 1000)
955 ;; Special case: Skip all touching arguments
956 (progn
957 (reftex-move-over-touching-args)
958 (reftex-context-substring))
960 ;; Do the real thing.
961 (let ((cnt 1))
963 (when (reftex-move-to-next-arg)
965 (while (< cnt n)
966 (while (and (member cnt opt-args)
967 (eq (following-char) ?\{))
968 (incf cnt))
969 (when (< cnt n)
970 (unless (and (condition-case nil
971 (or (forward-list 1) t)
972 (error nil))
973 (reftex-move-to-next-arg)
974 (incf cnt))
975 (setq cnt 1000))))
977 (while (and (memq cnt opt-args)
978 (eq (following-char) ?\{))
979 (incf cnt)))
980 (if (and (= n cnt)
981 (> (skip-chars-forward "{\\[") 0))
982 (reftex-context-substring)
983 nil))))
985 (defun reftex-move-over-touching-args ()
986 (condition-case nil
987 (while (memq (following-char) '(?\[ ?\{))
988 (forward-list 1))
989 (error nil)))
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"
994 (cond
995 (to-end
996 ;; Environment - find next \end
997 (buffer-substring-no-properties
998 (point)
999 (min (+ (point) 150)
1000 (save-match-data
1001 ;; FIXME: This is not perfect
1002 (if (re-search-forward "\\\\end{" nil t)
1003 (match-beginning 0)
1004 (point-max))))))
1005 ((memq (preceding-char) '(?\{ ?\[))
1006 ;; Inside a list - get only the list.
1007 (buffer-substring-no-properties
1008 (point)
1009 (min (+ (point) 150)
1010 (point-max)
1011 (condition-case nil
1012 (let ((forward-sexp-function nil)) ;Unneeded fanciness.
1013 (up-list 1)
1014 (1- (point)))
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)
1030 (while (>= i 0)
1031 (if (> i level)
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)))
1038 (pop numbers))
1039 (decf i)))
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)
1048 (equal level 0))))
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.
1052 (when level
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))
1057 (when (not star)
1058 (while (<= idx depth)
1059 (if (or (not partspecial)
1060 (not (= idx 1)))
1061 (aset reftex-section-numbers idx 0))
1062 (incf idx))))
1063 (if partspecial
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))))
1072 (incf idx))
1073 (save-match-data
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)))
1078 (if (and appendix
1079 (string-match "\\`[0-9]+" string))
1080 (setq string
1081 (concat
1082 (char-to-string
1083 (1- (+ ?A (string-to-number (match-string 0 string)))))
1084 (substring string (match-end 0))))))
1085 (if star
1086 (concat (make-string (1- (length string)) ?\ ) "*")
1087 string))))
1089 (defun reftex-roman-number (n)
1090 "Return as a string the roman number equal to N."
1091 (let ((nrest n)
1092 (string "")
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")
1096 ( 1 . "I")))
1097 listel i s)
1098 (while (>= nrest 1)
1099 (setq listel (pop list)
1100 i (car listel)
1101 s (cdr listel))
1102 (while (>= nrest i)
1103 (setq string (concat string s)
1104 nrest (- nrest i))))
1105 string))
1107 (provide 'reftex-parse)
1109 ;;; reftex-parse.el ends here