diff lisp/textmodes/reftex-parse.el @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents 5ade352e8d1c
children
line wrap: on
line diff
--- a/lisp/textmodes/reftex-parse.el	Sun Jan 15 23:02:10 2006 +0000
+++ b/lisp/textmodes/reftex-parse.el	Mon Jan 16 00:03:54 2006 +0000
@@ -1,8 +1,9 @@
 ;;; reftex-parse.el --- parser functions for RefTeX
-;; Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
+;; Copyright (c) 1997, 1998, 1999, 2000, 2003, 2004, 2005
+;;  Free Software Foundation, Inc.
 
 ;; Author: Carsten Dominik <dominik@science.uva.nl>
-;; Version: 4.18
+;; Version: VERSIONTAG
 ;;
 
 ;; This file is part of GNU Emacs.
@@ -19,8 +20,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -33,10 +34,10 @@
 (defmacro reftex-with-special-syntax (&rest body)
   `(let ((saved-syntax (syntax-table)))
      (unwind-protect
-	 (progn
-	   (set-syntax-table reftex-syntax-table)
-	   (let ((case-fold-search nil))
-	     ,@body))
+         (progn
+           (set-syntax-table reftex-syntax-table)
+           (let ((case-fold-search nil))
+             ,@body))
        (set-syntax-table saved-syntax))))
 
 (defun reftex-parse-one ()
@@ -68,12 +69,12 @@
 
   (let* ((old-list (symbol-value reftex-docstruct-symbol))
          (master (reftex-TeX-master-file))
-	 (true-master (file-truename master))
-	 (master-dir (file-name-as-directory (file-name-directory master)))
+         (true-master (file-truename master))
+         (master-dir (file-name-as-directory (file-name-directory master)))
          (file (or file (buffer-file-name)))
-	 (true-file (file-truename file))
-	 (bibview-cache (assq 'bibview-cache old-list))
-	 (index-tags (cdr (assq 'index-tags old-list)))
+         (true-file (file-truename file))
+         (bibview-cache (assq 'bibview-cache old-list))
+         (index-tags (cdr (assq 'index-tags old-list)))
          from-file appendix docstruct tmp)
 
     ;; Make sure replacement is really an option here
@@ -97,9 +98,9 @@
 
     ;; Find active toc entry and initialize section-numbers
     (setq reftex-active-toc (reftex-last-assoc-before-elt
-			     'toc (list 'bof from-file) old-list)
-	  appendix (reftex-last-assoc-before-elt
-		    'appendix (list 'bof from-file) old-list))
+                             'toc (list 'bof from-file) old-list)
+          appendix (reftex-last-assoc-before-elt
+                    'appendix (list 'bof from-file) old-list))
 
     (reftex-init-section-numbers reftex-active-toc appendix)
 
@@ -110,11 +111,11 @@
     (reftex-with-special-syntax
      (save-window-excursion
        (save-excursion
-	 (unwind-protect
-	     (setq docstruct
-		   (reftex-parse-from-file
-		    from-file docstruct master-dir))
-	   (reftex-kill-temporary-buffers)))))
+         (unwind-protect
+             (setq docstruct
+                   (reftex-parse-from-file
+                    from-file docstruct master-dir))
+           (reftex-kill-temporary-buffers)))))
 
     (message "Scanning document... done")
 
@@ -141,27 +142,27 @@
     (and index-tags (setq index-tags (sort index-tags 'string<)))
     (let ((index-tag-cell (assq 'index-tags docstruct)))
       (if index-tag-cell
-	  (setcdr index-tag-cell index-tags)
-	(push (cons 'index-tags index-tags) docstruct)))
+          (setcdr index-tag-cell index-tags)
+        (push (cons 'index-tags index-tags) docstruct)))
     (unless (assq 'xr docstruct)
       (let* ((allxr (reftex-all-assq 'xr-doc docstruct))
-	     (alist (mapcar
-		     (lambda (x)
-		       (if (setq tmp (reftex-locate-file (nth 2 x) "tex"
-							 master-dir))
-			   (cons (nth 1 x) tmp)
-			 (message "Can't find external document %s"
-				  (nth 2 x))
-			 nil))
-		     allxr))
-	     (alist (delq nil alist))
-	     (allprefix (delq nil (mapcar 'car alist)))
-	     (regexp (if allprefix
-			 (concat "\\`\\("
-				 (mapconcat 'identity allprefix "\\|")
-				 "\\)")
-		       "\\\\\\\\\\\\")))   ; this will never match
-	(push (list 'xr alist regexp) docstruct)))
+             (alist (mapcar
+                     (lambda (x) 
+                       (if (setq tmp (reftex-locate-file (nth 2 x) "tex"
+                                                         master-dir))
+                           (cons (nth 1 x) tmp)
+                         (message "Can't find external document %s"
+                                  (nth 2 x))
+                         nil))
+                     allxr))
+             (alist (delq nil alist))
+             (allprefix (delq nil (mapcar 'car alist)))
+             (regexp (if allprefix
+                         (concat "\\`\\(" 
+                                 (mapconcat 'identity allprefix "\\|")
+                                 "\\)")
+                       "\\\\\\\\\\\\")))   ; this will never match
+        (push (list 'xr alist regexp) docstruct)))
 
     (set reftex-docstruct-symbol docstruct)
     (put reftex-docstruct-symbol 'modified t)))
@@ -171,6 +172,7 @@
       reftex-everything-regexp
     reftex-everything-regexp-no-index))
 
+;;;###autoload
 (defun reftex-all-document-files (&optional relative)
   "Return a list of all files belonging to the current document.
 When RELATIVE is non-nil, give file names relative to directory
@@ -200,8 +202,8 @@
     (catch 'exit
       (setq file-found (reftex-locate-file file "tex" master-dir))
       (if (and (not file-found)
-	       (setq buf (reftex-get-buffer-visiting file)))
-	  (setq file-found (buffer-file-name buf)))
+               (setq buf (reftex-get-buffer-visiting file)))
+          (setq file-found (buffer-file-name buf)))
 
       (unless file-found
         (push (list 'file-error file) docstruct)
@@ -220,106 +222,106 @@
         (setq file (buffer-file-name))
         (push (list 'bof file) docstruct)
 
-	(reftex-with-special-syntax
-	 (save-excursion
-	   (save-restriction
-	     (widen)
-	     (goto-char 1)
+        (reftex-with-special-syntax
+         (save-excursion
+           (save-restriction
+             (widen)
+             (goto-char 1)
 
-	     (while (re-search-forward regexp nil t)
+             (while (re-search-forward regexp nil t)
 
-	       (cond
+               (cond
 
-		((match-end 1)
-		 ;; It is a label
-		 (push (reftex-label-info (reftex-match-string 1) file bound)
-		       docstruct))
+                ((match-end 1)
+                 ;; It is a label
+                 (push (reftex-label-info (reftex-match-string 1) file bound)
+                       docstruct))
 
-		((match-end 3)
-		 ;; It is a section
-		 (setq bound (point))
+                ((match-end 3)
+                 ;; It is a section
+                 (setq bound (point))
 
-		 ;; Insert in List
-		 (setq toc-entry (reftex-section-info file))
-		 (when toc-entry
-		   ;; It can happen that section info returns nil
-		   (setq level (nth 5 toc-entry))
-		   (setq highest-level (min highest-level level))
-		   (if (= level highest-level)
-		       (message
-			"Scanning %s %s ..."
-			(car (rassoc level reftex-section-levels-all))
-			(nth 6 toc-entry)))
+                 ;; Insert in List
+                 (setq toc-entry (reftex-section-info file))
+                 (when toc-entry
+                   ;; It can happen that section info returns nil
+                   (setq level (nth 5 toc-entry))
+                   (setq highest-level (min highest-level level))
+                   (if (= level highest-level)
+                       (message
+                        "Scanning %s %s ..."
+                        (car (rassoc level reftex-section-levels-all))
+                        (nth 6 toc-entry)))
 
-		   (push toc-entry docstruct)
-		   (setq reftex-active-toc toc-entry)))
+                   (push toc-entry docstruct)
+                   (setq reftex-active-toc toc-entry)))
 
-		((match-end 7)
-		 ;; It's an include or input
-		 (setq include-file (reftex-match-string 7))
-		 ;; Test if this file should be ignored
-		 (unless (delq nil (mapcar
-				    (lambda (x) (string-match x include-file))
-				    reftex-no-include-regexps))
-		   ;; Parse it
-		   (setq docstruct
-			 (reftex-parse-from-file
-			  include-file
-			  docstruct master-dir))))
+                ((match-end 7)
+                 ;; It's an include or input
+                 (setq include-file (reftex-match-string 7))
+                 ;; Test if this file should be ignored
+                 (unless (delq nil (mapcar 
+                                    (lambda (x) (string-match x include-file))
+                                    reftex-no-include-regexps))
+                   ;; Parse it
+                   (setq docstruct
+                         (reftex-parse-from-file
+                          include-file
+                          docstruct master-dir))))
 
-		((match-end 9)
-		 ;; Appendix starts here
-		 (reftex-init-section-numbers nil t)
-		 (push (cons 'appendix t) docstruct))
+                ((match-end 9)
+                 ;; Appendix starts here
+                 (reftex-init-section-numbers nil t)
+                 (push (cons 'appendix t) docstruct))
 
-		((match-end 10)
-		 ;; Index entry
-		 (when reftex-support-index
-		   (setq index-entry (reftex-index-info file))
-		   (when index-entry
-		     (add-to-list 'index-tags (nth 1 index-entry))
-		     (push index-entry docstruct))))
+                ((match-end 10)
+                 ;; Index entry
+                 (when reftex-support-index
+                   (setq index-entry (reftex-index-info file))
+                   (when index-entry
+                     (add-to-list 'index-tags (nth 1 index-entry))
+                     (push index-entry docstruct))))
 
-		((match-end 11)
-		 ;; A macro with label
-		 (save-excursion
-		   (let* ((mac (reftex-match-string 11))
-			  (label (progn (goto-char (match-end 11))
-					(save-match-data
-					  (reftex-no-props
-					   (reftex-nth-arg-wrapper
-					    mac)))))
-			  (typekey (nth 1 (assoc mac reftex-env-or-mac-alist)))
-			  (entry (progn (if typekey
-					    ;; A typing macro
-					    (goto-char (match-end 0))
-					  ;; A neutral macro
-					  (goto-char (match-end 11))
-					  (reftex-move-over-touching-args))
-					(reftex-label-info
-					 label file bound nil nil))))
-		     (push entry docstruct))))
-		(t (error "This should not happen (reftex-parse-from-file)")))
-	       )
+                ((match-end 11)
+                 ;; A macro with label
+                 (save-excursion
+                   (let* ((mac (reftex-match-string 11))
+                          (label (progn (goto-char (match-end 11))
+                                        (save-match-data
+                                          (reftex-no-props
+                                           (reftex-nth-arg-wrapper
+                                            mac)))))
+                          (typekey (nth 1 (assoc mac reftex-env-or-mac-alist)))
+                          (entry (progn (if typekey
+                                            ;; A typing macro
+                                            (goto-char (match-end 0))
+                                          ;; A neutral macro
+                                          (goto-char (match-end 11))
+                                          (reftex-move-over-touching-args))
+                                        (reftex-label-info
+                                         label file bound nil nil))))
+                     (push entry docstruct))))
+                (t (error "This should not happen (reftex-parse-from-file)")))
+               )
 
-	     ;; Find bibliography statement
-	     (when (setq tmp (reftex-locate-bibliography-files master-dir))
-	       (push (cons 'bib tmp) docstruct))
-
-	     (goto-char 1)
-	     (when (re-search-forward
-		    "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t)
-	       (push (cons 'thebib file) docstruct))
+             ;; Find bibliography statement
+             (when (setq tmp (reftex-locate-bibliography-files master-dir))
+               (push (cons 'bib tmp) docstruct))
 
-	     ;; Find external document specifications
-	     (goto-char 1)
-	     (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t)
-	       (push (list 'xr-doc (reftex-match-string 2)
-			   (reftex-match-string 3))
-		     docstruct))
+             (goto-char 1)
+             (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)
+               (push (list 'xr-doc (reftex-match-string 2)
+                           (reftex-match-string 3))
+                     docstruct))
 
-	     ;; End of file mark
-	     (push (list 'eof file) docstruct)))))
+             ;; End of file mark
+             (push (list 'eof file) docstruct)))))
 
       ;; Kill the scanned buffer
       (reftex-kill-temporary-buffers next-buf))
@@ -329,31 +331,31 @@
 
 (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))
       (if (re-search-forward
-	   (concat
-;	    "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\("
-	    "\\(^\\)[^%]*\\\\\\("
-	    (mapconcat 'identity reftex-bibliography-commands "\\|")
-	    "\\){[ \t]*\\([^}]+\\)") nil t)
-	  (setq files
-		(split-string (reftex-match-string 3)
-			      "[ \t\n\r]*,[ \t\n\r]*")))))
+           (concat
+;           "\\(\\`\\|[\n\r]\\)[^%]*\\\\\\("
+            "\\(^\\)[^%\n\r]*\\\\\\("
+            (mapconcat 'identity reftex-bibliography-commands "\\|")
+            "\\){[ \t]*\\([^}]+\\)") nil t)
+          (setq files 
+                (split-string (reftex-match-string 3)
+                              "[ \t\n\r]*,[ \t\n\r]*")))))
   (when files
-    (setq files
-	  (mapcar
-	   (lambda (x)
-	     (if (or (member x reftex-bibfile-ignore-list)
-		     (delq nil (mapcar (lambda (re) (string-match re x))
-				       reftex-bibfile-ignore-regexps)))
-		 ;; excluded file
-		 nil
-	       ;; find the file
-	       (reftex-locate-file x "bib" master-dir)))
-	   files))
+    (setq files 
+          (mapcar
+           (lambda (x)
+             (if (or (member x reftex-bibfile-ignore-list)
+                     (delq nil (mapcar (lambda (re) (string-match re x))
+                                       reftex-bibfile-ignore-regexps)))
+                 ;; excluded file
+                 nil
+               ;; find the file
+               (reftex-locate-file x "bib" master-dir)))
+           files))
     (delq nil files)))
 
 (defun reftex-replace-label-list-segment (old insert &optional entirely)
@@ -386,24 +388,24 @@
   ;; Carefull: This function expects the match-data to be still in place!
   (let* ((marker (set-marker (make-marker) (1- (match-beginning 3))))
          (macro (reftex-match-string 3))
-	 (prefix (save-match-data
-		   (if (string-match "begin{\\([^}]+\\)}" macro)
-		       (match-string 1 macro))))
-	 (level-exp (cdr (assoc macro reftex-section-levels-all)))
+         (prefix (save-match-data
+                   (if (string-match "begin{\\([^}]+\\)}" macro)
+                       (match-string 1 macro))))
+         (level-exp (cdr (assoc macro reftex-section-levels-all)))
          (level (if (symbolp level-exp)
-		    (save-match-data (funcall level-exp))
-		  level-exp))
-	 (star (= ?* (char-after (match-end 3))))
-	 (unnumbered (or star (< level 0)))
-	 (level (abs level))
+                    (save-match-data (funcall level-exp))
+                  level-exp))
+         (star (= ?* (char-after (match-end 3))))
+         (unnumbered (or star (< level 0)))
+         (level (abs level))
          (section-number (reftex-section-number level unnumbered))
-         (text1 (save-match-data
-		  (save-excursion
-		    (reftex-context-substring prefix))))
+         (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
@@ -412,7 +414,7 @@
            (make-string (* reftex-level-indent level) ?\ )
            (if (nth 1 reftex-label-menu-flags) ; section number flag
                (concat section-number " "))
-	   (if prefix (concat (capitalize prefix) ": ") "")
+           (if prefix (concat (capitalize prefix) ": ") "")
            text))
     (list 'toc "toc" text file marker level section-number
           literal (marker-position marker))))
@@ -425,12 +427,12 @@
    (reftex-support-index t)
    ((y-or-n-p "Turn on index support and rescan entire document? ")
     (setq reftex-support-index 'demanded
-	  current-prefix-arg '(16)))
+          current-prefix-arg '(16)))
    (t (if abort
-	  (error "No index support")
-	(message "No index support")
-	(ding)
-	(sit-for 1)))))
+          (error "No index support")
+        (message "No index support")
+        (ding)
+        (sit-for 1)))))
 
 (defun reftex-index-info-safe (file)
   (reftex-with-special-syntax
@@ -442,49 +444,49 @@
   ;; Carefull: This function expects the match-data to be still in place!
   (catch 'exit
     (let* ((macro (reftex-match-string 10))
-	   (bom (match-beginning 10))
-	   (boa (match-end 10))
-	   (entry (or (assoc macro reftex-index-macro-alist)
-		      (throw 'exit nil)))
-	   (exclude (nth 3 entry))
-	   ;; The following is a test if this match should be excluded
-	   (test-dummy (and (fboundp exclude)
-			    (funcall exclude)
-			    (throw 'exit nil)))
-	   (itag (nth 1 entry))
-	   (prefix (nth 2 entry))
-	   (index-tag
-	    (cond ((stringp itag) itag)
-		  ((integerp itag)
-		   (progn (goto-char boa)
-			  (or (reftex-nth-arg itag (nth 6 entry)) "idx")))
-		  (t "idx")))
-	   (arg (or (progn (goto-char boa)
-			   (reftex-nth-arg (nth 5 entry) (nth 6 entry)))
-		    ""))
-	   (end-of-args (progn (goto-char boa)
-			       (reftex-move-over-touching-args)
-			       (point)))
-	   (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point)))
-	   (begin-of-context
-	    (progn (goto-char bom)
-		   (skip-chars-backward "^ \t\r\n")
-		   (point)))
-	   (context (buffer-substring-no-properties
-		     begin-of-context end-of-context))
-	   (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
-			       (split-string key reftex-index-level-re)
-			       " ! ")))
+           (bom (match-beginning 10))
+           (boa (match-end 10))
+           (entry (or (assoc macro reftex-index-macro-alist)
+                      (throw 'exit nil)))
+           (exclude (nth 3 entry))
+           ;; The following is a test if this match should be excluded
+           (test-dummy (and (fboundp exclude)
+                            (funcall exclude)
+                            (throw 'exit nil)))
+           (itag (nth 1 entry))
+           (prefix (nth 2 entry))
+           (index-tag 
+            (cond ((stringp itag) itag)
+                  ((integerp itag)
+                   (progn (goto-char boa)
+                          (or (reftex-nth-arg itag (nth 6 entry)) "idx")))
+                  (t "idx")))
+           (arg (or (progn (goto-char boa)
+                           (reftex-nth-arg (nth 5 entry) (nth 6 entry)))
+                    ""))
+           (end-of-args (progn (goto-char boa)
+                               (reftex-move-over-touching-args)
+                               (point)))
+           (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point)))
+           (begin-of-context
+            (progn (goto-char bom)
+                   (skip-chars-backward "^ \t\r\n")
+                   (point)))
+           (context (buffer-substring-no-properties
+                     begin-of-context end-of-context))
+           (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 
+                               (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.
 
@@ -515,9 +517,9 @@
                    (match-string 1 (nth 7 reftex-active-toc)))
                "SECTION HEADING NOT FOUND")))
        (save-excursion
-	 (goto-char reftex-default-context-position)
-	 (unless (eq (string-to-char env) ?\\)
-	   (reftex-move-over-touching-args))
+         (goto-char reftex-default-context-position)
+         (unless (eq (string-to-char env) ?\\)
+           (reftex-move-over-touching-args))
          (reftex-context-substring))))
 
     ((stringp parse)
@@ -543,7 +545,7 @@
            (funcall parse env)
          (error (format "HOOK ERROR: %s" (cdr error-var))))))
     (t
-     "ILLEGAL VALUE OF PARSE"))))
+     "INVALID VALUE OF PARSE"))))
 
 (defun reftex-where-am-I ()
   ;; Return the docstruct entry above point.  Actually returns a cons
@@ -592,7 +594,7 @@
                          ((looking-at (reftex-make-regexp-allow-for-ctrl-m
                                        (nth 7 (car list))))
                           ;; Same title: remember, but keep looking
-			  (setq rtn-if-no-other (car list)))))
+                          (setq rtn-if-no-other (car list)))))
                     (pop list))
                   rtn1))
                ((match-end 7)
@@ -600,35 +602,35 @@
                 (car
                  (member (list 'eof (reftex-locate-file
                                      (reftex-match-string 7) "tex"
-				     (cdr (assq 'master-dir docstruct))))
+                                     (cdr (assq 'master-dir docstruct))))
                          docstruct)))
-	       ((match-end 9)
-		(assq 'appendix (symbol-value reftex-docstruct-symbol)))
-	       ((match-end 10)
-		;; Index entry
-		(when reftex-support-index
-		  (let* ((index-info (save-excursion
-				       (reftex-index-info-safe nil)))
-			 (list (member (list 'bof (buffer-file-name))
-				       docstruct))
-			 (endelt (car (member (list 'eof (buffer-file-name))
-					      list)))
-			 dist last-dist last (n 0))
-		    ;; 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)
-					  (nth 2 (car list))))
-			(incf n)
-			(setq dist (abs (- (point) (nth 4 (car list)))))
-			(if (or (not last-dist) (< dist last-dist))
-			    (setq last-dist dist last (car list))))
-		      (setq list (cdr list)))
-		    ;; We are sure if we have only one, or a zero distance
-		    (cond ((or (= n 1) (= dist 0)) last)
-			  ((> n 1) (setq cnt 2) last)
-			  (t nil)))))
-	       ((match-end 11)
+               ((match-end 9)
+                (assq 'appendix (symbol-value reftex-docstruct-symbol)))
+               ((match-end 10)
+                ;; Index entry
+                (when reftex-support-index
+                  (let* ((index-info (save-excursion 
+                                       (reftex-index-info-safe nil)))
+                         (list (member (list 'bof (buffer-file-name))
+                                       docstruct))
+                         (endelt (car (member (list 'eof (buffer-file-name))
+                                              list)))
+                         dist last-dist last (n 0))
+                    ;; 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) 
+                                          (nth 2 (car list))))
+                        (incf n)
+                        (setq dist (abs (- (point) (nth 4 (car list)))))
+                        (if (or (not last-dist) (< dist last-dist))
+                            (setq last-dist dist last (car list))))
+                      (setq list (cdr list)))
+                    ;; We are sure if we have only one, or a zero distance
+                    (cond ((or (= n 1) (equal dist 0)) last)
+                          ((> n 1) (setq cnt 2) last)
+                          (t nil)))))
+               ((match-end 11)
                 (save-excursion
                   (goto-char (match-end 11))
                   (assoc (reftex-no-props
@@ -640,88 +642,88 @@
     ;; Check if there was only a by-name match for the section.
     (when (and (not rtn) rtn-if-no-other)
       (setq rtn rtn-if-no-other
-	    cnt 2))
+            cnt 2))
     (cons rtn (eq cnt 1))))
 
 (defun reftex-notice-new (&optional n force)
   "Hook to handshake with RefTeX after something new has been inserted."
   ;; Add a new entry to the docstruct list.  If it is a section, renumber
   ;; the following sections.
-  ;; FIXME:  Put in a WHAT parameter
+  ;; FIXME:  Put in a WHAT parameter and search backward until one is found.
   ;; When N is given, go back that many matches of reftex-everything-regexp
   ;; When FORCE is non-nil, also insert if `reftex-where-am-I' was uncertain.
   (condition-case nil
       (catch 'exit
-	(unless reftex-mode (throw 'exit nil))
-	(reftex-access-scan-info)
-	(let* ((docstruct (symbol-value reftex-docstruct-symbol))
-	       here-I-am appendix tail entry star level
-	       section-number context)
+        (unless reftex-mode (throw 'exit nil))
+        (reftex-access-scan-info)
+        (let* ((docstruct (symbol-value reftex-docstruct-symbol))
+               here-I-am appendix tail entry star level
+               section-number context)
 
      (save-excursion
        (when (re-search-backward (reftex-everything-regexp) nil t (or n 1))
 
-	 ;; Find where we are
-	 (setq here-I-am (reftex-where-am-I))
-	 (or here-I-am (throw 'exit nil))
-	 (unless (or force (cdr here-I-am)) (throw 'exit nil))
-	 (setq tail (memq (car here-I-am) docstruct))
-	 (or tail (throw 'exit nil))
-	 (setq reftex-active-toc (reftex-last-assoc-before-elt
-				  'toc (car here-I-am) docstruct)
-	       appendix (reftex-last-assoc-before-elt
-			 'appendix (car here-I-am) docstruct))
+         ;; Find where we are
+         (setq here-I-am (reftex-where-am-I))
+         (or here-I-am (throw 'exit nil))
+         (unless (or force (cdr here-I-am)) (throw 'exit nil))
+         (setq tail (memq (car here-I-am) docstruct))
+         (or tail (throw 'exit nil))
+         (setq reftex-active-toc (reftex-last-assoc-before-elt
+                                  'toc (car here-I-am) docstruct)
+               appendix (reftex-last-assoc-before-elt
+                         'appendix (car here-I-am) docstruct))
 
-	 ;; Initialize section numbers
-	 (if (eq (car (car here-I-am)) 'appendix)
-	     (reftex-init-section-numbers nil t)
-	   (reftex-init-section-numbers reftex-active-toc appendix))
+         ;; Initialize section numbers
+         (if (eq (car (car here-I-am)) 'appendix)
+             (reftex-init-section-numbers nil t)
+           (reftex-init-section-numbers reftex-active-toc appendix))
 
-	 ;; Match the section command
-	 (when (re-search-forward (reftex-everything-regexp) nil t)
-	   (cond
-	    ((match-end 1)
-	     (push (reftex-label-info (reftex-match-string 1) buffer-file-name)
-		   (cdr tail)))
-
-	    ((match-end 3)
-	     (setq star (= ?* (char-after (match-end 3)))
-		   entry (reftex-section-info (buffer-file-name))
-		   level (nth 5 entry))
-	     ;; Insert the section info
-	     (push entry (cdr tail))
+         ;; Match the section command
+         (when (re-search-forward (reftex-everything-regexp) nil t)
+           (cond
+            ((match-end 1)
+             (push (reftex-label-info (reftex-match-string 1) buffer-file-name)
+                   (cdr tail)))
 
-	     ;; We are done unless we use section numbers
-	     (unless (nth 1 reftex-label-menu-flags) (throw 'exit nil))
+            ((match-end 3)
+             (setq star (= ?* (char-after (match-end 3)))
+                   entry (reftex-section-info (buffer-file-name))
+                   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))
 
-	     ;; Update the remaining toc items
-	     (setq tail (cdr tail))
-	     (while (and (setq tail (memq (assq 'toc (cdr tail)) tail))
-			 (setq entry (car tail))
-			 (>= (nth 5 entry) level))
-	       (setq star (string-match "\\*" (nth 6 entry))
-		     context (nth 2 entry)
-		     section-number
-		     (reftex-section-number (nth 5 entry) star))
-	       (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)"
-				   context)
-		 (when (and (not appendix)
-			    (>= (string-to-char (match-string 2)) ?A))
-		   ;; Just entered the appendex.  Get out.
-		   (throw 'exit nil))
+             ;; Update the remaining toc items
+             (setq tail (cdr tail))
+             (while (and (setq tail (memq (assq 'toc (cdr tail)) tail))
+                         (setq entry (car tail))
+                         (>= (nth 5 entry) level))
+               (setq star (string-match "\\*" (nth 6 entry))
+                     context (nth 2 entry)
+                     section-number
+                     (reftex-section-number (nth 5 entry) star))
+               (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)"
+                                   context)
+                 (when (and (not appendix)
+                            (>= (string-to-char (match-string 2)) ?A))
+                   ;; Just entered the appendex.  Get out.
+                   (throw 'exit nil))
 
-		 ;; Change the section number.
-		 (setf (nth 2 entry)
-		       (concat (match-string 1 context)
-			       section-number
-			       (match-string 3 context))))))
-	    ((match-end 10)
-	     ;; Index entry
-	     (and reftex-support-index
-		  (setq entry (reftex-index-info-safe buffer-file-name))
-		  ;; FIXME: (add-to-list 'index-tags (nth 1 index-entry))
-		  (push entry (cdr tail))))))))))
-
+                 ;; Change the section number.
+                 (setf (nth 2 entry)
+                       (concat (match-string 1 context)
+                               section-number
+                               (match-string 3 context))))))
+            ((match-end 10)
+             ;; Index entry
+             (and reftex-support-index
+                  (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))
   )
 
@@ -735,8 +737,8 @@
    ((memq (preceding-char) '(?\] ?\})))
    ;; Do a search
    ((and reftex-allow-detached-macro-args
-	 (re-search-backward
-	  "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t))
+         (re-search-backward
+          "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t))
     (goto-char (1+ (match-beginning 0)))
     t)
    (t nil)))
@@ -781,29 +783,29 @@
             (setq cnt 1 cnt-opt 0)
             ;; move back over any touching sexps
             (while (and (reftex-move-to-previous-arg bound)
-			(condition-case nil
-			    (progn (backward-sexp) t)
-			  (error nil)))
-	      (if (eq (following-char) ?\[) (incf cnt-opt))
+                        (condition-case nil
+                            (progn (backward-sexp) t)
+                          (error nil)))
+              (if (eq (following-char) ?\[) (incf cnt-opt))
               (incf cnt))
             (setq pos (point))
             (when (and (or (= (following-char) ?\[)
                            (= (following-char) ?\{))
                        (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t))
               (setq cmd (reftex-match-string 0))
-	      (when (looking-at "\\\\begin{[^}]*}")
-		(setq cmd (reftex-match-string 0)
-		      cnt (1- cnt)))
-	      ;; This does ignore optional arguments.  Very hard to fix.
-	      (when (setq entry (assoc cmd reftex-env-or-mac-alist))
-		(if (> cnt (or (nth 4 entry) 100))
-		    (setq cmd nil)))
+              (when (looking-at "\\\\begin{[^}]*}")
+                (setq cmd (reftex-match-string 0)
+                      cnt (1- cnt)))
+              ;; This does ignore optional arguments.  Very hard to fix.
+              (when (setq entry (assoc cmd reftex-env-or-mac-alist))
+                (if (> cnt (or (nth 4 entry) 100))
+                    (setq cmd nil)))
               (cond
-	       ((null cmd))
-	       ((eq t which)
-		(push (cons cmd (point)) cmd-list))
-	       ((or (eq 1 which) (member cmd which))
-		(throw 'exit (cons cmd (point))))))
+               ((null cmd))
+               ((eq t which)
+                (push (cons cmd (point)) cmd-list))
+               ((or (eq 1 which) (member cmd which))
+                (throw 'exit (cons cmd (point))))))
             (goto-char pos)))
         (nreverse cmd-list)))))
 
@@ -836,7 +838,7 @@
                      (match-beginning 2) (match-end 2)))
           (cond
            ((string= (match-string 1) "end")
-	    (push env end-list))
+            (push env end-list))
            ((equal env (car end-list))
             (setq end-list (cdr end-list)))
            ((eq t which)
@@ -866,25 +868,25 @@
       (let ((bound (or bound (save-excursion (re-search-backward
                                               reftex-section-regexp nil 1)
                                              (point))))
-	    (fun-list (if (listp which)
-			  (mapcar (lambda (x) (if (memq x which) x nil))
-				  reftex-special-env-parsers)
-			reftex-special-env-parsers))
+            (fun-list (if (listp which)
+                          (mapcar (lambda (x) (if (memq x which) x nil))
+                                  reftex-special-env-parsers)
+                        reftex-special-env-parsers))
             specials rtn)
-	;; Call all functions
-	(setq specials (mapcar
-			(lambda (fun)
-			  (save-excursion
-			    (setq rtn (and fun (funcall fun bound)))
-			    (if rtn (cons (symbol-name fun) rtn) nil)))
-			fun-list))
-	;; Delete the non-matches
-	(setq specials (delq nil specials))
-	;; Sort
-	(setq specials (sort specials (lambda (a b) (> (cdr a) (cdr b)))))
-	(if (eq which t)
-	    specials
-	  (car specials))))))
+        ;; Call all functions
+        (setq specials (mapcar 
+                        (lambda (fun)
+                          (save-excursion
+                            (setq rtn (and fun (funcall fun bound)))
+                            (if rtn (cons (symbol-name fun) rtn) nil)))
+                        fun-list))
+        ;; Delete the non-matches
+        (setq specials (delq nil specials))
+        ;; Sort
+        (setq specials (sort specials (lambda (a b) (> (cdr a) (cdr b)))))
+        (if (eq which t) 
+            specials
+          (car specials))))))
 
 (defsubst reftex-move-to-next-arg (&optional ignore)
   ;; Assuming that we are at the end of a macro name or a macro argument,
@@ -896,7 +898,7 @@
    ((memq (following-char) '(?\[ ?\{)))
    ;; Do a search
    ((and reftex-allow-detached-macro-args
-	 (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]"))
+         (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]"))
     (goto-char (1- (match-end 0)))
     t)
    (t nil)))
@@ -915,39 +917,39 @@
   (if (= n 1000)
       ;; Special case:  Skip all touching arguments
       (progn
-	(reftex-move-over-touching-args)
-	(reftex-context-substring))
+        (reftex-move-over-touching-args)
+        (reftex-context-substring))
 
     ;; 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) ?\{))
+            (incf cnt))
+          (when (< cnt n)
+            (unless (and (condition-case nil
+                             (or (forward-list 1) t)
+                           (error nil))
+                         (reftex-move-to-next-arg)
+                         (incf cnt))
+              (setq cnt 1000))))
 
-	(while (< cnt n)
-	  (while (and (member cnt opt-args)
-		      (eq (following-char) ?\{))
-	    (incf cnt))
-	  (when (< cnt n)
-	    (unless (and (condition-case nil
-			     (or (forward-list 1) t)
-			   (error nil))
-			 (reftex-move-to-next-arg)
-			 (incf cnt))
-	      (setq cnt 1000))))
-
-	(while (and (memq cnt opt-args)
-		    (eq (following-char) ?\{))
-	  (incf cnt)))
+        (while (and (memq cnt opt-args)
+                    (eq (following-char) ?\{))
+          (incf cnt)))
       (if (and (= n cnt)
-	       (> (skip-chars-forward "{\\[") 0))
-	  (reftex-context-substring)
-	nil))))
+               (> (skip-chars-forward "{\\[") 0))
+          (reftex-context-substring)
+        nil))))
 
 (defun reftex-move-over-touching-args ()
   (condition-case nil
       (while (memq (following-char) '(?\[ ?\{))
-	(forward-list 1))
-    (error nil)))
+        (forward-list 1))
+    (error nil)))  
 
 (defun reftex-context-substring (&optional to-end)
   ;; Return up to 150 chars from point
@@ -958,11 +960,11 @@
     (buffer-substring-no-properties
      (point)
      (min (+ (point) 150)
-	  (save-match-data
-	    ;; FIXME: THis is not perfect
-	    (if (re-search-forward "\\\\end{" nil t)
-		(match-beginning 0)
-	      (point-max))))))
+          (save-match-data
+            ;; FIXME: This is not perfect
+            (if (re-search-forward "\\\\end{" nil t)
+                (match-beginning 0)
+              (point-max))))))
    ((or (= (preceding-char) ?\{)
         (= (preceding-char) ?\[))
     ;; Inside a list - get only the list.
@@ -977,8 +979,8 @@
             (error (point-max))))))
    (t
     ;; no list - just grab 150 characters
-    (buffer-substring-no-properties (point)
-				    (min (+ (point) 150) (point-max))))))
+    (buffer-substring-no-properties (point) 
+                                    (min (+ (point) 150) (point-max))))))
 
 ;; Variable holding the vector with section numbers
 (defvar reftex-section-numbers (make-vector reftex-max-section-depth 0))
@@ -993,11 +995,11 @@
     (while (>= i 0)
       (if (> i level)
           (aset reftex-section-numbers i 0)
-	(setq number-string (or (car numbers) "0"))
-	(if (string-match "\\`[A-Z]\\'" number-string)
-	    (aset reftex-section-numbers i
-		  (- (string-to-char number-string) ?A -1))
-	    (aset reftex-section-numbers i (string-to-int number-string)))
+        (setq number-string (or (car numbers) "0"))
+        (if (string-match "\\`[A-Z]\\'" number-string)
+            (aset reftex-section-numbers i
+                  (- (string-to-char number-string) ?A -1))
+            (aset reftex-section-numbers i (string-to-number number-string)))
         (pop numbers))
       (decf i)))
   (put 'reftex-section-numbers 'appendix appendix))
@@ -1006,65 +1008,66 @@
   ;; Return a string with the current section number.
   ;; When LEVEL is non-nil, increase section numbers on that level.
   (let* ((depth (1- (length reftex-section-numbers))) idx n (string "")
-	 (appendix (get 'reftex-section-numbers 'appendix))
-	 (partspecial (and (not reftex-part-resets-chapter)
-			   (equal level 0))))
+         (appendix (get 'reftex-section-numbers 'appendix))
+         (partspecial (and (not reftex-part-resets-chapter)
+                           (equal level 0))))
     ;; partspecial means, this is a part statement.
     ;; Parts do not reset the chapter counter, and the part number is
     ;; not included in the numbering of other sectioning levels.
     (when level
       (when (and (> level -1) (not star))
-        (aset reftex-section-numbers
-	      level (1+ (aref reftex-section-numbers level))))
+        (aset reftex-section-numbers 
+              level (1+ (aref reftex-section-numbers level))))
       (setq idx (1+ level))
       (when (not star)
-	(while (<= idx depth)
-	  (if (or (not partspecial)
-		  (not (= idx 1)))
-	      (aset reftex-section-numbers idx 0))
-	  (incf idx))))
+        (while (<= idx depth)
+          (if (or (not partspecial)
+                  (not (= idx 1)))
+              (aset reftex-section-numbers idx 0))
+          (incf idx))))
     (if partspecial
-	(setq string (concat "Part " (reftex-roman-number
-				      (aref reftex-section-numbers 0))))
+        (setq string (concat "Part " (reftex-roman-number
+                                      (aref reftex-section-numbers 0))))
       (setq idx (if reftex-part-resets-chapter 0 1))
       (while (<= idx depth)
-	(setq n (aref reftex-section-numbers idx))
-	(if (not (and partspecial (not (equal string ""))))
-	    (setq string (concat string (if (not (string= string "")) "." "")
-				 (int-to-string n))))
-	(incf idx))
+        (setq n (aref reftex-section-numbers idx))
+        (if (not (and partspecial (not (equal string ""))))
+            (setq string (concat string (if (not (string= string "")) "." "")
+                                 (int-to-string n))))
+        (incf idx))
       (save-match-data
-	(if (string-match "\\`\\([@0]\\.\\)+" string)
-	    (setq string (replace-match "" nil nil string)))
-	(if (string-match "\\(\\.0\\)+\\'" string)
-	    (setq string (replace-match "" nil nil string)))
-	(if (and appendix
-		 (string-match "\\`[0-9]+" string))
-	    (setq string
-		  (concat
-		   (char-to-string
-		    (1- (+ ?A (string-to-int (match-string 0 string)))))
-		   (substring string (match-end 0))))))
+        (if (string-match "\\`\\([@0]\\.\\)+" string)
+            (setq string (replace-match "" nil nil string)))
+        (if (string-match "\\(\\.0\\)+\\'" string)
+            (setq string (replace-match "" nil nil string)))
+        (if (and appendix
+                 (string-match "\\`[0-9]+" string))
+            (setq string 
+                  (concat
+                   (char-to-string
+                    (1- (+ ?A (string-to-number (match-string 0 string)))))
+                   (substring string (match-end 0))))))
       (if star
-	  (concat (make-string (1- (length string)) ?\ ) "*")
-	string))))
+          (concat (make-string (1- (length string)) ?\ ) "*")
+        string))))
 
 (defun reftex-roman-number (n)
   ;; Return as a string the roman number equal to N.
   (let ((nrest n)
-	(string "")
-	(list '((1000 . "M") ( 900 . "CM") ( 500 . "D") ( 400 . "CD")
-		( 100 . "C") (  90 . "XC") (  50 . "L") (  40 . "XL")
-		(  10 . "X") (   9 . "IX") (   5 . "V") (   4 . "IV")
-		(   1 . "I")))
-	listel i s)
+        (string "")
+        (list '((1000 . "M") ( 900 . "CM") ( 500 . "D") ( 400 . "CD")
+                ( 100 . "C") (  90 . "XC") (  50 . "L") (  40 . "XL")
+                (  10 . "X") (   9 . "IX") (   5 . "V") (   4 . "IV")
+                (   1 . "I")))
+        listel i s)
     (while (>= nrest 1)
       (setq listel (pop list)
-	    i (car listel)
-	    s (cdr listel))
+            i (car listel)
+            s (cdr listel))
       (while (>= nrest i)
-	(setq string (concat string s)
-	      nrest (- nrest i))))
+        (setq string (concat string s)
+              nrest (- nrest i))))
     string))
 
+;;; arch-tag: 6a8168f7-abb9-4576-99dc-fcbc7ba901a3
 ;;; reftex-parse.el ends here