]> code.delx.au - gnu-emacs/blobdiff - lisp/textmodes/reftex-parse.el
Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[gnu-emacs] / lisp / textmodes / reftex-parse.el
index 12bf8b70462bd930719d2f5f14894d23b68ca6d2..a86b10e21cc1a4bc1b788fa01432f025b289e078 100644 (file)
@@ -1,12 +1,9 @@
 ;;; reftex-parse.el --- parser functions for RefTeX
 
-;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-;;   2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+;; Copyright (C) 1997-2013 Free Software Foundation, Inc.
 
 ;; Author: Carsten Dominik <dominik@science.uva.nl>
 ;; Maintainer: auctex-devel@gnu.org
-;; Version: 4.31
-;; Package: reftex
 
 ;; This file is part of GNU Emacs.
 
@@ -28,7 +25,7 @@
 ;;; Code:
 
 (eval-when-compile (require 'cl))
-(provide 'reftex-parse)
+
 (require 'reftex)
 
 (defmacro reftex-with-special-syntax (&rest body)
     (unless (assq 'xr docstruct)
       (let* ((allxr (reftex-all-assq 'xr-doc docstruct))
              (alist (mapcar
-                     (lambda (x) 
+                     (lambda (x)
                        (if (setq tmp (reftex-locate-file (nth 2 x) "tex"
                                                          master-dir))
                            (cons (nth 1 x) tmp)
              (alist (delq nil alist))
              (allprefix (delq nil (mapcar 'car alist)))
              (regexp (if allprefix
-                         (concat "\\`\\(" 
+                         (concat "\\`\\("
                                  (mapconcat 'identity allprefix "\\|")
                                  "\\)")
                        "\\\\\\\\\\\\")))   ; this will never match
@@ -190,6 +187,9 @@ of master file."
       (push file file-list))
     (nreverse file-list)))
 
+;; Bound in the caller, reftex-do-parse.
+(defvar index-tags)
+
 (defun reftex-parse-from-file (file docstruct master-dir)
   ;; Scan the buffer for labels and save them in a list.
   (let ((regexp (reftex-everything-regexp))
@@ -239,10 +239,19 @@ of master file."
 
                 ((match-end 3)
                  ;; It is a section
-                 (setq bound (point))
 
+                ;; Use the beginning as bound and not the end
+                ;; (i.e. (point)) because the section command might
+                ;; be the start of the current environment to be
+                ;; found by `reftex-label-info'.
+                 (setq bound (match-beginning 0))
+                ;; The section regexp matches a character at the end
+                ;; we are not interested in.  Especially if it is the
+                ;; backslash of a following macro we want to find in
+                ;; the next parsing iteration.
+                (when (eq (char-before) ?\\) (backward-char))
                  ;; Insert in List
-                 (setq toc-entry (reftex-section-info file))
+                 (setq toc-entry (funcall reftex-section-info-function file))
                  (when toc-entry
                    ;; It can happen that section info returns nil
                    (setq level (nth 5 toc-entry))
@@ -260,7 +269,7 @@ of master file."
                  ;; It's an include or input
                  (setq include-file (reftex-match-string 7))
                  ;; Test if this file should be ignored
-                 (unless (delq nil (mapcar 
+                 (unless (delq nil (mapcar
                                     (lambda (x) (string-match x include-file))
                                     reftex-no-include-regexps))
                    ;; Parse it
@@ -309,10 +318,10 @@ of master file."
                (push (cons 'bib tmp) docstruct))
 
              (goto-char 1)
-             (when (re-search-forward 
+             (when (re-search-forward
                     "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t)
                (push (cons 'thebib file) docstruct))
-                   
+
              ;; Find external document specifications
              (goto-char 1)
              (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t)
@@ -331,7 +340,7 @@ of master file."
 
 (defun reftex-locate-bibliography-files (master-dir &optional files)
   ;; Scan buffer for bibliography macro and return file list.
-  
+
   (unless files
     (save-excursion
       (goto-char (point-min))
@@ -341,11 +350,11 @@ of master file."
             "\\(^\\)[^%\n\r]*\\\\\\("
             (mapconcat 'identity reftex-bibliography-commands "\\|")
             "\\){[ \t]*\\([^}]+\\)") nil t)
-          (setq files 
+          (setq files
                 (split-string (reftex-match-string 3)
                               "[ \t\n\r]*,[ \t\n\r]*")))))
   (when files
-    (setq files 
+    (setq files
           (mapcar
            (lambda (x)
              (if (or (member x reftex-bibfile-ignore-list)
@@ -399,13 +408,13 @@ of master file."
          (unnumbered (or star (< level 0)))
          (level (abs level))
          (section-number (reftex-section-number level unnumbered))
-         (text1 (save-match-data 
+         (text1 (save-match-data
                   (save-excursion
                     (reftex-context-substring prefix))))
          (literal (buffer-substring-no-properties
                    (1- (match-beginning 3))
                    (min (point-max) (+ (match-end 0) (length text1) 1))))
-         ;; Literal can be too short since text1 too short. No big problem. 
+         ;; Literal can be too short since text1 too short. No big problem.
          (text (reftex-nicify-text text1)))
 
     ;; Add section number and indentation
@@ -441,7 +450,7 @@ of master file."
 (defvar test-dummy)
 (defun reftex-index-info (file)
   ;; Return an index entry for the current match.
-  ;; Carefull: This function expects the match-data to be still in place!
+  ;; Careful: This function expects the match-data to be still in place!
   (catch 'exit
     (let* ((macro (reftex-match-string 10))
            (bom (match-beginning 10))
@@ -455,7 +464,7 @@ of master file."
                             (throw 'exit nil)))
            (itag (nth 1 entry))
            (prefix (nth 2 entry))
-           (index-tag 
+           (index-tag
             (cond ((stringp itag) itag)
                   ((integerp itag)
                    (progn (goto-char boa)
@@ -477,16 +486,16 @@ of master file."
            (key-end (if (string-match reftex-index-key-end-re arg)
                         (1+ (match-beginning 0))))
            (rawkey (substring arg 0 key-end))
-                              
+
            (key (if prefix (concat prefix rawkey) rawkey))
            (sortkey (downcase key))
-           (showkey (mapconcat 'identity 
+           (showkey (mapconcat 'identity
                                (split-string key reftex-index-level-re)
                                " ! ")))
       (goto-char end-of-args)
       ;;       0        1       2      3   4   5  6      7       8      9
       (list 'index index-tag context file bom arg key showkey sortkey key-end))))
-  
+
 (defun reftex-short-context (env parse &optional bound derive)
   ;; Get about one line of useful context for the label definition at point.
 
@@ -609,7 +618,7 @@ of master file."
                ((match-end 10)
                 ;; Index entry
                 (when reftex-support-index
-                  (let* ((index-info (save-excursion 
+                  (let* ((index-info (save-excursion
                                        (reftex-index-info-safe nil)))
                          (list (member (list 'bof (buffer-file-name))
                                        docstruct))
@@ -619,7 +628,7 @@ of master file."
                     ;; Check all index entries with equal text
                     (while (and list (not (eq endelt (car list))))
                       (when (and (eq (car (car list)) 'index)
-                                 (string= (nth 2 index-info) 
+                                 (string= (nth 2 index-info)
                                           (nth 2 (car list))))
                         (incf n)
                         (setq dist (abs (- (point) (nth 4 (car list)))))
@@ -692,7 +701,7 @@ of master file."
                    level (nth 5 entry))
              ;; Insert the section info
              (push entry (cdr tail))
-             
+
              ;; We are done unless we use section numbers
              (unless (nth 1 reftex-label-menu-flags) (throw 'exit nil))
 
@@ -709,7 +718,7 @@ of master file."
                                    context)
                  (when (and (not appendix)
                             (>= (string-to-char (match-string 2)) ?A))
-                   ;; Just entered the appendex.  Get out.
+                   ;; Just entered the appendix.  Get out.
                    (throw 'exit nil))
 
                  ;; Change the section number.
@@ -723,7 +732,7 @@ of master file."
                   (setq entry (reftex-index-info-safe buffer-file-name))
                   ;; FIXME: (add-to-list 'index-tags (nth 1 index-entry))
                   (push entry (cdr tail))))))))))
-            
+
     (error nil))
   )
 
@@ -876,7 +885,7 @@ of master file."
                         reftex-special-env-parsers))
             specials rtn)
         ;; Call all functions
-        (setq specials (mapcar 
+        (setq specials (mapcar
                         (lambda (fun)
                           (save-excursion
                             (setq rtn (and fun (funcall fun bound)))
@@ -886,7 +895,7 @@ of master file."
         (setq specials (delq nil specials))
         ;; Sort
         (setq specials (sort specials (lambda (a b) (> (cdr a) (cdr b)))))
-        (if (eq which t) 
+        (if (eq which t)
             specials
           (car specials))))))
 
@@ -924,9 +933,9 @@ of master file."
 
     ;; Do the real thing.
     (let ((cnt 1))
-      
+
       (when (reftex-move-to-next-arg)
-        
+
         (while (< cnt n)
           (while (and (member cnt opt-args)
                       (eq (following-char) ?\{))
@@ -951,7 +960,7 @@ of master file."
   (condition-case nil
       (while (memq (following-char) '(?\[ ?\{))
         (forward-list 1))
-    (error nil)))  
+    (error nil)))
 
 (defun reftex-context-substring (&optional to-end)
   ;; Return up to 150 chars from point
@@ -980,7 +989,7 @@ of master file."
             (error (point-max))))))
    (t
     ;; no list - just grab 150 characters
-    (buffer-substring-no-properties (point) 
+    (buffer-substring-no-properties (point)
                                     (min (+ (point) 150) (point-max))))))
 
 ;; Variable holding the vector with section numbers
@@ -1017,7 +1026,7 @@ of master file."
     ;; not included in the numbering of other sectioning levels.
     (when level
       (when (and (> level -1) (not star))
-        (aset reftex-section-numbers 
+        (aset reftex-section-numbers
               level (1+ (aref reftex-section-numbers level))))
       (setq idx (1+ level))
       (when (not star)
@@ -1043,7 +1052,7 @@ of master file."
             (setq string (replace-match "" nil nil string)))
         (if (and appendix
                  (string-match "\\`[0-9]+" string))
-            (setq string 
+            (setq string
                   (concat
                    (char-to-string
                     (1- (+ ?A (string-to-number (match-string 0 string)))))
@@ -1070,4 +1079,6 @@ of master file."
               nrest (- nrest i))))
     string))
 
+(provide 'reftex-parse)
+
 ;;; reftex-parse.el ends here