Use run-mode-hooks for major mode hooks.
[bpt/emacs.git] / lisp / textmodes / reftex-index.el
index 5b2d62c..5bdd09c 100644 (file)
@@ -1,18 +1,19 @@
 ;;; reftex-index.el --- index support with RefTeX
 
 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-;;   2006, 2007 Free Software Foundation, Inc.
+;;   2006, 2007, 2008, 2009, 2010, 2011 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.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,9 +21,7 @@
 ;; GNU General Public License for more details.
 
 ;; 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., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -35,7 +34,6 @@
 
 ;; START remove for XEmacs release
 (defvar mark-active)
-(defvar zmacs-regions)
 (defvar transient-mark-mode)
 (defvar TeX-master)
 ;; END remove for XEmacs release
@@ -58,13 +56,13 @@ which is part of AUCTeX, the string is first processed with the
          (active (if (featurep 'xemacs)
                      (and zmacs-regions (region-exists-p))  ; XEmacs
                    (and transient-mark-mode mark-active)))  ; Emacs
-         (beg (if active 
+         (beg (if active
                   (region-beginning)
-                (save-excursion 
+                (save-excursion
                   (skip-syntax-backward "w\\") (point))))
          (end (if active
                   (region-end)
-                (save-excursion 
+                (save-excursion
                   (skip-syntax-forward "w\\") (point))))
          (sel (buffer-substring beg end))
          (mathp (condition-case nil (texmathp) (error nil)))
@@ -95,9 +93,9 @@ which is part of AUCTeX, the string is first processed with the
         ;; Delete what is in the buffer and make the index entry
         (delete-region beg end)
         (reftex-index def-char full-entry def-tag sel)))))
-  
+
 (defun reftex-index (&optional char key tag sel no-insert)
-  "Query for an index macro and insert it along with its argments.
+  "Query for an index macro and insert it along with its arguments.
 The index macros available are those defined in `reftex-index-macro' or
 by a call to `reftex-add-index-macros', typically from an AUCTeX style file.
 RefteX provides completion for the index tag and the index key, and
@@ -192,17 +190,17 @@ will prompt for other arguments."
 (defun reftex-index-select-tag ()
   ;; Have the user select an index tag.
   ;; FIXME: should we cache tag-alist, prompt and help?
-  (let* ((index-tags (cdr (assoc 'index-tags 
+  (let* ((index-tags (cdr (assoc 'index-tags
                                  (symbol-value reftex-docstruct-symbol))))
          (default (reftex-default-index)))
-    (cond 
+    (cond
      ((null index-tags)
       (error "No index tags available"))
 
      ((= (length index-tags) 1)
       ;; Just one index, use it
       (car index-tags))
-          
+
      ((> (length index-tags) 1)
       ;; Several indices, ask.
       (let* ((tags (copy-sequence index-tags))
@@ -223,12 +221,12 @@ will prompt for other arguments."
               (unless (assq (aref tag i) tag-alist)
                 (push (list (aref tag i)
                             tag
-                            (concat (substring tag 0 i) 
+                            (concat (substring tag 0 i)
                                     "[" (substring tag i (incf i)) "]"
                                     (substring tag i)))
                       tag-alist)
                 (throw 'exit t)))
-            (push (list (+ ?0 (incf cnt)) tag 
+            (push (list (+ ?0 (incf cnt)) tag
                         (concat "[" (int-to-string cnt) "]:" tag))
                   tag-alist)))
         (setq tag-alist (nreverse tag-alist))
@@ -243,7 +241,7 @@ will prompt for other arguments."
                       (if default
                           (format "[^M]  %s (the default)\n" default)
                         "")
-                      (mapconcat (lambda(x) 
+                      (mapconcat (lambda(x)
                                    (apply 'format "[%c]   %s" x))
                                  tag-alist "\n")))
         ;; Query the user for an index-tag
@@ -272,13 +270,116 @@ will prompt for other arguments."
     key))
 
 (defun reftex-index-update-taglist (newtag)
-  ;; add NEWTAG to the list of available index tags. 
+  ;; add NEWTAG to the list of available index tags.
   (let ((cell (assoc 'index-tags (symbol-value reftex-docstruct-symbol))))
     (and newtag (cdr cell) (not (member newtag (cdr cell)))
          (push newtag (cdr cell)))))
 
-(defvar reftex-index-map (make-sparse-keymap)
+(defvar reftex-index-mode-map
+  (let ((map (make-sparse-keymap)))
+    ;; Index map
+    (define-key map (if (featurep 'xemacs) [(button2)] [(mouse-2)])
+      'reftex-index-mouse-goto-line-and-hide)
+    (define-key map [follow-link] 'mouse-face)
+
+    (substitute-key-definition
+     'next-line 'reftex-index-next map global-map)
+    (substitute-key-definition
+     'previous-line 'reftex-index-previous map global-map)
+
+    (loop for x in
+          '(("n"    . reftex-index-next)
+            ("p"    . reftex-index-previous)
+            ("?"    . reftex-index-show-help)
+            (" "    . reftex-index-view-entry)
+            ("\C-m" . reftex-index-goto-entry-and-hide)
+            ("\C-i" . reftex-index-goto-entry)
+            ("\C-k" . reftex-index-kill)
+            ("r"    . reftex-index-rescan)
+            ("R"    . reftex-index-Rescan)
+            ("g"    . revert-buffer)
+            ("q"    . reftex-index-quit)
+            ("k"    . reftex-index-quit-and-kill)
+            ("f"    . reftex-index-toggle-follow)
+            ("s"    . reftex-index-switch-index-tag)
+            ("e"    . reftex-index-edit)
+            ("^"    . reftex-index-level-up)
+            ("_"    . reftex-index-level-down)
+            ("}"    . reftex-index-restrict-to-section)
+            ("{"    . reftex-index-widen)
+            (">"    . reftex-index-restriction-forward)
+            ("<"    . reftex-index-restriction-backward)
+            ("("    . reftex-index-toggle-range-beginning)
+            (")"    . reftex-index-toggle-range-end)
+            ("|"    . reftex-index-edit-attribute)
+            ("@"    . reftex-index-edit-visual)
+            ("*"    . reftex-index-edit-key)
+            ("\C-c=". reftex-index-goto-toc)
+            ("c"    . reftex-index-toggle-context))
+          do (define-key map (car x) (cdr x)))
+
+    (loop for key across "0123456789" do
+          (define-key map (vector (list key)) 'digit-argument))
+    (define-key map "-" 'negative-argument)
+
+    ;; The capital letters and the exclamation mark
+    (loop for key across (concat "!" reftex-index-section-letters) do
+          (define-key map (vector (list key))
+            (list 'lambda '() '(interactive)
+                  (list 'reftex-index-goto-letter key))))
+
+    (easy-menu-define reftex-index-menu map
+      "Menu for Index buffer"
+      '("Index"
+        ["Goto section A-Z"
+         (message "To go to a section, just press any of: !%s"
+                  reftex-index-section-letters) t]
+        ["Show Entry" reftex-index-view-entry t]
+        ["Go To Entry" reftex-index-goto-entry t]
+        ["Exit & Go To Entry" reftex-index-goto-entry-and-hide t]
+        ["Table of Contents" reftex-index-goto-toc t]
+        ["Quit" reftex-index-quit t]
+        "--"
+        ("Update"
+         ["Rebuilt *Index* Buffer" revert-buffer t]
+         "--"
+         ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans]
+         ["Rescan Entire Document" reftex-index-Rescan t])
+        ("Restrict"
+         ["Restrict to section" reftex-index-restrict-to-section t]
+         ["Widen" reftex-index-widen reftex-index-restriction-indicator]
+         ["Next Section" reftex-index-restriction-forward
+          reftex-index-restriction-indicator]
+         ["Previous Section" reftex-index-restriction-backward
+          reftex-index-restriction-indicator])
+        ("Edit"
+         ["Edit Entry" reftex-index-edit t]
+         ["Edit Key" reftex-index-edit-key t]
+         ["Edit Attribute" reftex-index-edit-attribute t]
+         ["Edit Visual" reftex-index-edit-visual t]
+         "--"
+         ["Add Parentkey" reftex-index-level-down t]
+         ["Remove Parentkey " reftex-index-level-up t]
+         "--"
+         ["Make Start-of-Range" reftex-index-toggle-range-beginning t]
+         ["Make End-of-Range" reftex-index-toggle-range-end t]
+         "--"
+         ["Kill Entry" reftex-index-kill nil]
+         "--"
+         ["Undo" reftex-index-undo nil])
+        ("Options"
+         ["Context" reftex-index-toggle-context :style toggle
+          :selected reftex-index-include-context]
+         "--"
+         ["Follow Mode" reftex-index-toggle-follow :style toggle
+          :selected reftex-index-follow-mode])
+        "--"
+        ["Help" reftex-index-show-help t]))
+
+    map)
   "Keymap used for *Index* buffers.")
+(define-obsolete-variable-alias
+  'reftex-index-map 'reftex-index-mode-map "24.1")
 
 (defvar reftex-index-menu)
 
@@ -293,19 +394,14 @@ will prompt for other arguments."
 (defvar reftex-index-restriction-indicator nil)
 (defvar reftex-index-restriction-data nil)
 
-(defun reftex-index-mode ()
+(define-derived-mode reftex-index-mode fundamental-mode "RefTeX Index"
   "Major mode for managing Index buffers for LaTeX files.
 This buffer was created with RefTeX.
 Press `?' for a summary of important key bindings, or check the menu.
 
 Here are all local bindings.
 
-\\{reftex-index-map}"
-  (interactive)
-  (kill-all-local-variables)
-  (setq major-mode 'reftex-index-mode
-        mode-name "RefTeX Index")
-  (use-local-map reftex-index-map)
+\\{reftex-index-mode-map}"
   (set (make-local-variable 'revert-buffer-function) 'reftex-index-revert)
   (set (make-local-variable 'reftex-index-restriction-data) nil)
   (set (make-local-variable 'reftex-index-restriction-indicator) nil)
@@ -320,10 +416,9 @@ Here are all local bindings.
     (make-local-hook 'post-command-hook)
     (make-local-hook 'pre-command-hook))
   (make-local-variable 'reftex-last-follow-point)
-  (easy-menu-add reftex-index-menu reftex-index-map)
+  (easy-menu-add reftex-index-menu reftex-index-mode-map)
   (add-hook 'post-command-hook 'reftex-index-post-command-hook nil t)
-  (add-hook 'pre-command-hook  'reftex-index-pre-command-hook nil t)
-  (run-hooks 'reftex-index-mode-hook))
+  (add-hook 'pre-command-hook  'reftex-index-pre-command-hook nil t))
 
 (defconst reftex-index-help
 "                      AVAILABLE KEYS IN INDEX BUFFER
@@ -353,7 +448,7 @@ _ ^        Add/Remove parent key (to make this item a subitem).
   ;; Note:  This function just looks for the nearest match of the
   ;; context string and may fail if the entry moved and an identical
   ;; entry is close to the old position.  Frequent rescans make this
-  ;; safer. 
+  ;; safer.
   (let* ((file (nth 3 data))
          (literal (nth 2 data))
          (pos (nth 4 data))
@@ -401,7 +496,7 @@ With prefix 3, restrict index to region."
          (calling-file (buffer-file-name))
          (restriction
           (or overriding-restriction
-              (and (not redo) 
+              (and (not redo)
                    (reftex-get-restriction current-prefix-arg docstruct))))
          (locations
           ;; See if we are on an index macro as initial position
@@ -410,7 +505,7 @@ With prefix 3, restrict index to region."
                      (macro (car what-macro))
                      (here-I-am (when (member macro reftex-macros-with-index)
                                   (save-excursion
-                                    (goto-char (+ (cdr what-macro) 
+                                    (goto-char (+ (cdr what-macro)
                                                   (length macro)))
                                     (reftex-move-over-touching-args)
                                     (reftex-where-am-I)))))
@@ -421,14 +516,13 @@ With prefix 3, restrict index to region."
     (setq buffer-name (reftex-make-index-buffer-name index-tag))
 
     ;; Goto the buffer and put it into the correct mode
-                      
+
     (when (or restriction current-prefix-arg)
          (reftex-kill-buffer buffer-name))
 
     (if (get-buffer-window buffer-name)
         (select-window (get-buffer-window buffer-name))
-      (let ((default-major-mode 'reftex-index-mode))
-        (switch-to-buffer buffer-name)))
+      (switch-to-buffer buffer-name))
 
     (or (eq major-mode 'reftex-index-mode) (reftex-index-mode))
 
@@ -514,7 +608,7 @@ SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help
       ;; Delete the entry at place
       (and (bolp) (forward-char 1))
       (delete-region (previous-single-property-change (1+ (point)) :data)
-                     (or (next-single-property-change (point) :data) 
+                     (or (next-single-property-change (point) :data)
                          (point-max))))
 
     ;; Walk through the list and insert all entries
@@ -535,7 +629,7 @@ SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help
       (insert indent (nth 7 cell))
       (when font
         (setq to (point))
-        (put-text-property 
+        (put-text-property
          (- (point) (length (nth 7 cell))) to
          'face index-face)
         (goto-char to))
@@ -563,7 +657,7 @@ SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help
 (defun reftex-index-insert-new-letter (letter &optional font)
   ;; Start a new section in the index
   (let ((from (point)))
-    (insert "\n" letter letter letter 
+    (insert "\n" letter letter letter
             "-----------------------------------------------------------------")
     (when font
       (put-text-property from (point) 'face reftex-index-section-face))
@@ -640,10 +734,10 @@ SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help
   (interactive "p")
   (setq reftex-callback-fwd t)
   (or (eobp) (forward-char 1))
-  (goto-char (or (next-single-property-change (point) :data) 
+  (goto-char (or (next-single-property-change (point) :data)
                  (point)))
   (unless (get-text-property (point) :data)
-    (goto-char (or (next-single-property-change (point) :data) 
+    (goto-char (or (next-single-property-change (point) :data)
                    (point)))))
 (defun reftex-index-previous (&optional arg)
   "Move to previous selectable item."
@@ -786,7 +880,7 @@ When index is restricted, select the next section as restriction criterion."
         (reftex-index-restrict-to-section t)
       (setq reftex-index-restriction-indicator (nth 6 bor)
             reftex-index-restriction-data
-            (list bor 
+            (list bor
                   (car (memq (assq 'toc (cdr (memq bor docstruct)))
                              docstruct))))
       (reftex-index-revert))))
@@ -818,7 +912,7 @@ When index is restricted, select the previous section as restriction criterion."
          show-window show-buffer match)
 
     (unless data (error "Don't know which index entry to visit"))
-    
+
     (if (eq (car data) 'index)
         (setq match (reftex-index-show-entry data no-revisit)))
 
@@ -889,7 +983,7 @@ When index is restricted, select the previous section as restriction criterion."
           attr (nth 2 analyze))
     (setf (nth 2 analyze) (if (string= attr bor) "" bor))
     (setq new (apply 'concat analyze))
-    (reftex-index-change-entry 
+    (reftex-index-change-entry
      new (if (string= (nth 2 analyze) bor)
              "Entry is now START-OF-PAGE-RANGE"
            "START-OF-PAGE-RANGE canceled"))))
@@ -947,7 +1041,7 @@ When index is restricted, select the previous section as restriction criterion."
           (t (setf (nth n analyze) (concat initial npart))))
     (setq new (apply 'concat analyze))
     ;; Change the entry and insert the changed version into the index.
-    (reftex-index-change-entry 
+    (reftex-index-change-entry
      new (if (string= npart "")
              (format "Deleted: %s" opart)
            (format "New value is: %s" npart)))))
@@ -961,14 +1055,14 @@ When index is restricted, select the previous section as restriction criterion."
     (unless data (error "Don't know which index entry to change"))
     (setq old (nth 2 data)
           key (nth 6 data)
-          prefix (completing-read 
-                  "Prefix: " 
-                  (reftex-sublist-nth 
+          prefix (completing-read
+                  "Prefix: "
+                  (reftex-sublist-nth
                    docstruct 6
                    (lambda (x)
                      (and (eq (car x) 'index)
                           (string= (nth 1 x) reftex-index-tag))) t)))
-    (unless (string-match 
+    (unless (string-match
              (concat (regexp-quote (car reftex-index-special-chars)) "\\'")
              prefix)
       (setq prefix (concat prefix (car reftex-index-special-chars))))
@@ -1008,7 +1102,7 @@ When index is restricted, select the previous section as restriction criterion."
 (defun reftex-index-change-entry (new &optional message)
   ;; Change the full context string of the index entry at point to
   ;; NEW.  This actually edits the buffer where the entry is defined.
-  
+
   (let* ((data (get-text-property (point) :data))
          old beg end info)
     (unless data (error "Cannot change entry"))
@@ -1016,8 +1110,7 @@ When index is restricted, select the previous section as restriction criterion."
     (setq beg (match-beginning 0) end (match-end 0))
     (setq old (nth 2 data))
     (and (equal old new) (error "Entry unchanged"))
-    (save-excursion
-      (set-buffer (get-file-buffer (nth 3 data)))
+    (with-current-buffer (get-file-buffer (nth 3 data))
       (goto-char beg)
       (unless (looking-at (regexp-quote old))
         (error "This should not happen (reftex-index-change-entry)"))
@@ -1036,62 +1129,12 @@ When index is restricted, select the previous section as restriction criterion."
     (setq reftex-last-follow-point 1)
     (and message (message "%s" message))))
 
-;; Index map
-(define-key reftex-index-map (if (featurep 'xemacs) [(button2)] [(mouse-2)])
-  'reftex-index-mouse-goto-line-and-hide)
-(define-key reftex-index-map [follow-link] 'mouse-face)
-
-(substitute-key-definition
- 'next-line 'reftex-index-next reftex-index-map global-map)
-(substitute-key-definition
- 'previous-line 'reftex-index-previous reftex-index-map global-map)
-
-(loop for x in
-      '(("n"    . reftex-index-next)
-        ("p"    . reftex-index-previous)
-        ("?"    . reftex-index-show-help)
-        (" "    . reftex-index-view-entry)
-        ("\C-m" . reftex-index-goto-entry-and-hide)
-        ("\C-i" . reftex-index-goto-entry)
-        ("\C-k" . reftex-index-kill)
-        ("r"    . reftex-index-rescan)
-        ("R"    . reftex-index-Rescan)
-        ("g"    . revert-buffer)
-        ("q"    . reftex-index-quit)
-        ("k"    . reftex-index-quit-and-kill)
-        ("f"    . reftex-index-toggle-follow)
-        ("s"    . reftex-index-switch-index-tag)
-        ("e"    . reftex-index-edit)
-        ("^"    . reftex-index-level-up)
-        ("_"    . reftex-index-level-down)
-        ("}"    . reftex-index-restrict-to-section)
-        ("{"    . reftex-index-widen)
-        (">"    . reftex-index-restriction-forward)
-        ("<"    . reftex-index-restriction-backward)
-        ("("    . reftex-index-toggle-range-beginning)
-        (")"    . reftex-index-toggle-range-end)
-        ("|"    . reftex-index-edit-attribute)
-        ("@"    . reftex-index-edit-visual)
-        ("*"    . reftex-index-edit-key)
-        ("\C-c=". reftex-index-goto-toc)
-        ("c"    . reftex-index-toggle-context))
-      do (define-key reftex-index-map (car x) (cdr x)))
-
-(loop for key across "0123456789" do
-      (define-key reftex-index-map (vector (list key)) 'digit-argument))
-(define-key reftex-index-map "-" 'negative-argument)
-
-;; The capital letters and the exclamation mark
-(loop for key across (concat "!" reftex-index-section-letters) do
-      (define-key reftex-index-map (vector (list key))
-        (list 'lambda '() '(interactive)
-              (list 'reftex-index-goto-letter key))))
-
 (defun reftex-index-goto-letter (char)
   "Go to the CHAR section in the index."
   (let ((pos (point))
         (case-fold-search nil))
-    (goto-line 3)
+    (goto-char (point-min))
+    (forward-line 2)
     (if (re-search-forward (concat "^" (char-to-string char)) nil t)
         (progn
           (beginning-of-line)
@@ -1101,58 +1144,9 @@ When index is restricted, select the previous section as restriction criterion."
       (if (eq char ?!)
           (error "This <%s> index does not contain entries sorted before the letters"
                  reftex-index-tag)
-        (error "This <%s> index does not contain entries starting with `%c'" 
+        (error "This <%s> index does not contain entries starting with `%c'"
                reftex-index-tag char)))))
 
-(easy-menu-define 
- reftex-index-menu reftex-index-map
- "Menu for Index buffer"
- `("Index"
-   ["Goto section A-Z" 
-    (message "To go to a section, just press any of: !%s"
-             reftex-index-section-letters) t]
-   ["Show Entry" reftex-index-view-entry t]
-   ["Go To Entry" reftex-index-goto-entry t]
-   ["Exit & Go To Entry" reftex-index-goto-entry-and-hide t]
-   ["Table of Contents" reftex-index-goto-toc t]
-   ["Quit" reftex-index-quit t]
-   "--"
-   ("Update"
-    ["Rebuilt *Index* Buffer" revert-buffer t]
-    "--"
-    ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans]
-    ["Rescan Entire Document" reftex-index-Rescan t])
-   ("Restrict"
-    ["Restrict to section" reftex-index-restrict-to-section t]
-    ["Widen" reftex-index-widen reftex-index-restriction-indicator]
-    ["Next Section" reftex-index-restriction-forward
-     reftex-index-restriction-indicator]
-    ["Previous Section" reftex-index-restriction-backward
-     reftex-index-restriction-indicator])
-   ("Edit"
-    ["Edit Entry" reftex-index-edit t]
-    ["Edit Key" reftex-index-edit-key t]
-    ["Edit Attribute" reftex-index-edit-attribute t]
-    ["Edit Visual" reftex-index-edit-visual t]
-    "--"
-    ["Add Parentkey" reftex-index-level-down t]
-    ["Remove Parentkey " reftex-index-level-up t]
-    "--"
-    ["Make Start-of-Range" reftex-index-toggle-range-beginning t]
-    ["Make End-of-Range" reftex-index-toggle-range-end t]
-    "--"
-    ["Kill Entry" reftex-index-kill nil]
-    "--"
-    ["Undo" reftex-index-undo nil])
-   ("Options"
-    ["Context" reftex-index-toggle-context :style toggle
-     :selected reftex-index-include-context]
-    "--"
-    ["Follow Mode" reftex-index-toggle-follow :style toggle 
-     :selected reftex-index-follow-mode])
-   "--"
-   ["Help" reftex-index-show-help t]))
-
 
 ;;----------------------------------------------------------------------
 ;; The Index Phrases File
@@ -1186,8 +1180,73 @@ This gets refreshed in every phrases command.")
   "Font lock keywords for reftex-index-phrases-mode.")
 (defvar reftex-index-phrases-font-lock-defaults nil
   "Font lock defaults for reftex-index-phrases-mode.")
-(defvar reftex-index-phrases-map (make-sparse-keymap)
+(defvar reftex-index-phrases-mode-map
+  (let ((map (make-sparse-keymap)))
+    ;; Keybindings and Menu for phrases buffer
+    (loop for x in
+          '(("\C-c\C-c" . reftex-index-phrases-save-and-return)
+            ("\C-c\C-x" . reftex-index-this-phrase)
+            ("\C-c\C-f" . reftex-index-next-phrase)
+            ("\C-c\C-r" . reftex-index-region-phrases)
+            ("\C-c\C-a" . reftex-index-all-phrases)
+            ("\C-c\C-d" . reftex-index-remaining-phrases)
+            ("\C-c\C-s" . reftex-index-sort-phrases)
+            ("\C-c\C-n" . reftex-index-new-phrase)
+            ("\C-c\C-m" . reftex-index-phrases-set-macro-key)
+            ("\C-c\C-i" . reftex-index-phrases-info)
+            ("\C-c\C-t" . reftex-index-find-next-conflict-phrase)
+            ("\C-i"     . self-insert-command))
+          do (define-key map (car x) (cdr x)))
+
+    (easy-menu-define reftex-index-phrases-menu map
+      "Menu for Phrases buffer"
+      '("Phrases"
+        ["New Phrase" reftex-index-new-phrase t]
+        ["Set Phrase Macro" reftex-index-phrases-set-macro-key t]
+        ["Recreate File Header" reftex-index-initialize-phrases-buffer t]
+        "--"
+        ("Sort Phrases"
+         ["Sort" reftex-index-sort-phrases t]
+         "--"
+         "Sort Options"
+         ["by Search Phrase" (setq reftex-index-phrases-sort-prefers-entry nil)
+          :style radio :selected (not reftex-index-phrases-sort-prefers-entry)]
+         ["by Index Entry" (setq reftex-index-phrases-sort-prefers-entry t)
+          :style radio :selected reftex-index-phrases-sort-prefers-entry]
+         ["in Blocks" (setq reftex-index-phrases-sort-in-blocks
+                            (not reftex-index-phrases-sort-in-blocks))
+          :style toggle :selected reftex-index-phrases-sort-in-blocks])
+        ["Describe Phrase" reftex-index-phrases-info t]
+        ["Next Phrase Conflict" reftex-index-find-next-conflict-phrase t]
+        "--"
+        ("Find and Index in Document"
+         ["Current Phrase" reftex-index-this-phrase t]
+         ["Next Phrase" reftex-index-next-phrase t]
+         ["Current and Following" reftex-index-remaining-phrases t]
+         ["Region Phrases" reftex-index-region-phrases t]
+         ["All Phrases" reftex-index-all-phrases t]
+         "--"
+         "Options"
+         ["Match Whole Words" (setq reftex-index-phrases-search-whole-words
+                                    (not reftex-index-phrases-search-whole-words))
+          :style toggle :selected reftex-index-phrases-search-whole-words]
+         ["Case Sensitive Search" (setq reftex-index-phrases-case-fold-search
+                                        (not  reftex-index-phrases-case-fold-search))
+          :style toggle :selected (not
+                                   reftex-index-phrases-case-fold-search)]
+         ["Wrap Long Lines" (setq reftex-index-phrases-wrap-long-lines
+                                  (not reftex-index-phrases-wrap-long-lines))
+          :style toggle :selected reftex-index-phrases-wrap-long-lines]
+         ["Skip Indexed Matches" (setq reftex-index-phrases-skip-indexed-matches
+                                       (not reftex-index-phrases-skip-indexed-matches))
+          :style toggle :selected reftex-index-phrases-skip-indexed-matches])
+        "--"
+        ["Save and Return" reftex-index-phrases-save-and-return t]))
+
+    map)
   "Keymap used for *toc* buffer.")
+(define-obsolete-variable-alias
+  'reftex-index-phrases-map 'reftex-index-phrases-mode-map "24.1")
 
 
 (defun reftex-index-phrase-selection-or-word (arg)
@@ -1200,7 +1259,7 @@ You get a chance to edit the entry in the phrases buffer - finish with
   (set-marker reftex-index-return-marker (point))
   (reftex-index-selection-or-word arg 'phrase)
   (if (eq major-mode 'reftex-index-phrases-mode)
-      (message "%s" 
+      (message "%s"
        (substitute-command-keys
         "Return to LaTeX with \\[reftex-index-phrases-save-and-return]"))))
 
@@ -1229,7 +1288,7 @@ If the buffer is non-empty, delete the old header first."
           (sort (copy-sequence reftex-index-macro-alist)
                 (lambda (a b) (equal (car a) default-macro))))
          macro entry key repeat)
-    
+
     (if master (set (make-local-variable 'TeX-master)
                     (file-name-nondirectory master)))
 
@@ -1242,9 +1301,10 @@ If the buffer is non-empty, delete the old header first."
       (if (looking-at reftex-index-phrases-comment-regexp)
           (beginning-of-line 2))
       (while (looking-at "^[ \t]*$")
-          (beginning-of-line 2))          
-      (cond ((fboundp 'zmacs-activate-region) (zmacs-activate-region))
-            ((boundp 'make-active) (setq mark-active t)))
+          (beginning-of-line 2))
+      (if (featurep 'xemacs)
+         (zmacs-activate-region)
+       (setq mark-active t))
       (if (yes-or-no-p "Delete and rebuild header? ")
           (delete-region (point-min) (point))))
 
@@ -1290,7 +1350,7 @@ If the buffer is non-empty, delete the old header first."
 (defvar reftex-index-phrases-marker)
 (defvar reftex-index-phrases-restrict-file nil)
 ;;;###autoload
-(defun reftex-index-phrases-mode ()
+(define-derived-mode reftex-index-phrases-mode fundamental-mode "Phrases"
   "Major mode for managing the Index phrases of a LaTeX document.
 This buffer was created with RefTeX.
 
@@ -1313,23 +1373,17 @@ For more information see the RefTeX User Manual.
 
 Here are all local bindings.
 
-\\{reftex-index-phrases-map}"
-  (interactive)
-  (kill-all-local-variables)
-  (setq major-mode 'reftex-index-phrases-mode
-        mode-name "Phrases")
-  (use-local-map reftex-index-phrases-map)
-  (set (make-local-variable 'font-lock-defaults) 
+\\{reftex-index-phrases-mode-map}"
+  (set (make-local-variable 'font-lock-defaults)
        reftex-index-phrases-font-lock-defaults)
-  (easy-menu-add reftex-index-phrases-menu reftex-index-phrases-map)
-  (set (make-local-variable 'reftex-index-phrases-marker) (make-marker))
-  (run-hooks 'reftex-index-phrases-mode-hook))
-(add-hook 'reftex-index-phrases-mode-hook 'turn-on-font-lock)
+  (easy-menu-add reftex-index-phrases-menu reftex-index-phrases-mode-map)
+  (set (make-local-variable 'reftex-index-phrases-marker) (make-marker)))
+;; (add-hook 'reftex-index-phrases-mode-hook 'turn-on-font-lock)
 
 ;; Font Locking stuff
 (let ((ss (if (featurep 'xemacs) 'secondary-selection ''secondary-selection)))
   (setq reftex-index-phrases-font-lock-keywords
-        (list 
+        (list
          (cons reftex-index-phrases-comment-regexp 'font-lock-comment-face)
          (list reftex-index-phrases-macrodef-regexp
                '(1 font-lock-type-face)
@@ -1353,7 +1407,7 @@ Here are all local bindings.
   (setq reftex-index-phrases-font-lock-defaults
         '((reftex-index-phrases-font-lock-keywords)
           nil t nil beginning-of-line))
-  (put 'reftex-index-phrases-mode 'font-lock-defaults 
+  (put 'reftex-index-phrases-mode 'font-lock-defaults
        reftex-index-phrases-font-lock-defaults) ; XEmacs
   )
 
@@ -1415,10 +1469,10 @@ match, the user will be asked to confirm the replacement."
                    (move-marker reftex-index-phrases-marker
                                 (match-beginning 0) (current-buffer))
                    ;; Start the query-replace
-                   (reftex-query-index-phrase-globally 
-                    files phrase macro-fmt 
+                   (reftex-query-index-phrase-globally
+                    files phrase macro-fmt
                     index-key repeat as-words)
-                   (message "%s replaced" 
+                   (message "%s replaced"
                             (reftex-number replace-count "occurrence"))))))
           (t (error "Cannot parse this line")))))
 
@@ -1461,7 +1515,7 @@ the document and stores the list in `reftex-index-phrases-files'."
           (unless buf (error "Master file %s not found" master))
           (set-buffer buf)
           (reftex-access-scan-info)
-          (setq reftex-index-phrases-files 
+          (setq reftex-index-phrases-files
                 (reftex-all-document-files))))
     ;; Parse the files header for macro definitions
     (setq reftex-index-phrases-macro-data nil)
@@ -1476,7 +1530,7 @@ the document and stores the list in `reftex-index-phrases-files'."
       ;; Reverse the list, so that the first macro is first
       (if (null reftex-index-phrases-macro-data)
           (error "No valid MACRO DEFINITION line in %s file (make sure to use TAB separators)" reftex-index-phrase-file-extension))
-      (setq reftex-index-phrases-macro-data 
+      (setq reftex-index-phrases-macro-data
             (nreverse reftex-index-phrases-macro-data))
       (goto-char (point-min)))))
 
@@ -1488,15 +1542,16 @@ you need to add/change text in an already indexed document and want to
 index the new part without having to go over the unchanged parts again."
   (interactive "r")
   (let ((win-conf (current-window-configuration))
-        (reftex-index-phrases-restrict-file (buffer-file-name)))        
+        (reftex-index-phrases-restrict-file (buffer-file-name)))
   (save-excursion
     (save-restriction
       (narrow-to-region beg end)
       (unwind-protect
           (progn
             ;; Hide the region highlighting
-            (cond ((fboundp 'zmacs-deactivate-region) (zmacs-deactivate-region))
-                  ((fboundp 'deactivate-mark) (deactivate-mark)))
+            (if (featurep 'xemacs)
+               (zmacs-deactivate-region)
+             (deactivate-mark))
             (delete-other-windows)
             (reftex-index-visit-phrases-buffer)
             (reftex-index-all-phrases))
@@ -1511,7 +1566,7 @@ index the new part without having to go over the unchanged parts again."
         (setq text (reftex-index-simplify-phrase text))
         (goto-char (point-min))
         (if (re-search-forward
-             (concat "^\\(\\S-*\\)\t\\(" (regexp-quote text) 
+             (concat "^\\(\\S-*\\)\t\\(" (regexp-quote text)
                      "\\) *[\t\n]") nil t)
             (progn
               (goto-char (match-end 2))
@@ -1541,7 +1596,7 @@ this function repeatedly."
           (let* ((phrase (match-string 3))
                  (case-fold-search reftex-index-phrases-case-fold-search)
                  (re (reftex-index-phrases-find-dup-re phrase t)))
-            (if (save-excursion 
+            (if (save-excursion
                   (goto-char (point-min))
                   (and (re-search-forward re nil t)
                        (re-search-forward re nil t)))
@@ -1595,7 +1650,7 @@ this function repeatedly."
           (while (re-search-forward re2 nil t)
             (push (cons (count-lines 1 (point)) (match-string 1)) superphrases)
             (incf ntimes2))))
-      (save-excursion
+      (save-current-buffer
         (while (setq file (pop files))
           (setq buf (reftex-get-file-buffer-force file))
           (when buf
@@ -1634,7 +1689,7 @@ this function repeatedly."
             (progn
               (princ (format " Superphrases:  Phrase matches the following %s in the phrase buffer:\n"
                              (reftex-number ntimes2 "line")))
-              (mapcar (lambda(x) 
+              (mapcar (lambda(x)
                         (princ (format "                Line %4d:  %s\n" (car x) (cdr x))))
                       (nreverse superphrases))))))))
 
@@ -1687,7 +1742,7 @@ it first compares the macro identifying chars and then the phrases."
         beg end)
     (goto-char (point-min))
     ;; Find first and last phrase line in buffer
-    (setq beg 
+    (setq beg
           (and (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
                (match-beginning 0)))
     (goto-char (point-max))
@@ -1700,7 +1755,7 @@ it first compares the macro identifying chars and then the phrases."
           (let* ((lines (split-string (buffer-substring beg end) "\n"))
                  (lines1 (sort lines 'reftex-compare-phrase-lines)))
             (message "Sorting lines...done")
-            (let ((inhibit-quit t))  ;; make sure we do not loose lines
+            (let ((inhibit-quit t))  ;; make sure we do not lose lines
               (delete-region beg end)
               (insert (mapconcat 'identity lines1 "\n"))))
           (goto-char (point-max))
@@ -1714,15 +1769,15 @@ it first compares the macro identifying chars and then the phrases."
     (if (string-match reftex-index-phrases-phrase-regexp12 a)
         (progn
           ;; Extract macro char and phrase-or-key for a
-          (setq ca (match-string 1 a) 
-                pa (downcase 
+          (setq ca (match-string 1 a)
+                pa (downcase
                     (or (and reftex-index-phrases-sort-prefers-entry
                              (match-string 6 a))
                         (match-string 3 a))))
           (if (string-match reftex-index-phrases-phrase-regexp12 b)
               (progn
                 ;; Extract macro char and phrase-or-key for b
-                (setq cb (match-string 1 b) 
+                (setq cb (match-string 1 b)
                       pb (downcase
                           (or (and reftex-index-phrases-sort-prefers-entry
                                    (match-string 6 b))
@@ -1730,7 +1785,7 @@ it first compares the macro identifying chars and then the phrases."
                 (setq c-p (string< ca cb)
                       p-p (string< pa pb))
                 ;; Do the right comparison, based on the value of `chars-first'
-                ;; `chars-first' is bound locally in the calling function               
+                ;; `chars-first' is bound locally in the calling function
                 (if chars-first
                     (if (string= ca cb) p-p c-p)
                   (if (string= pa pb) c-p p-p)))))
@@ -1741,7 +1796,7 @@ it first compares the macro identifying chars and then the phrases."
       (not reftex-index-phrases-sort-in-blocks))))
 
 (defvar reftex-index-phrases-menu)
-(defun reftex-index-make-phrase-regexp (phrase &optional 
+(defun reftex-index-make-phrase-regexp (phrase &optional
                                                as-words allow-newline)
   "Return a regexp matching PHRASE, even if distributed over lines.
 With optional arg AS-WORDS, require word boundary at beginning and end.
@@ -1752,12 +1807,12 @@ With optional arg ALLOW-NEWLINE, allow single newline between words."
                      "\\([ \t]+\\)")))
     (concat (if (and as-words (string-match "\\`\\w" (car words)))
                 "\\(\\<\\|[`']\\)" "")
-            (mapconcat (lambda (w) (regexp-quote 
+            (mapconcat (lambda (w) (regexp-quote
                                     (if reftex-index-phrases-case-fold-search
                                         (downcase w)
                                       w)))
                        words space-re)
-            (if (and as-words 
+            (if (and as-words
                      (string-match "\\w\\'" (nth (1- (length words)) words)))
                 "\\(\\>\\|'\\)" ""))))
 
@@ -1780,8 +1835,8 @@ Treats the logical `and' for index phrases."
   (let ((index-keys (split-string (or index-key match)
                                   reftex-index-phrases-logical-and-regexp)))
     (concat
-     (mapconcat (lambda (x) 
-                  (format macro-fmt 
+     (mapconcat (lambda (x)
+                  (format macro-fmt
                           (format (if mathp reftex-index-math-format "%s") x)))
                 index-keys "")
    (if repeat (reftex-index-simplify-phrase match) ""))))
@@ -1793,7 +1848,7 @@ Treats the logical `and' for index phrases."
     (unless files (error "No files"))
     (unwind-protect
         (progn
-          (switch-to-buffer-other-window (reftex-get-file-buffer-force 
+          (switch-to-buffer-other-window (reftex-get-file-buffer-force
                                           (car files)))
           (catch 'no-more-files
             (while (setq file (pop files))
@@ -1833,12 +1888,12 @@ AS-WORDS means, the search for PHRASE should require word boundaries at
 both ends."
   (let* ((re (reftex-index-make-phrase-regexp phrase as-words 'allow-newline))
          (case-fold-search reftex-index-phrases-case-fold-search)
-         (index-keys (split-string 
+         (index-keys (split-string
                       (or index-key phrase)
                       reftex-index-phrases-logical-or-regexp))
          (nkeys (length index-keys))
          (ckey (nth 0 index-keys))
-         (all-yes nil) 
+         (all-yes nil)
          match rpl char (beg (make-marker)) (end (make-marker)) mathp)
     (move-marker beg 1)
     (move-marker end 1)
@@ -1862,16 +1917,16 @@ both ends."
                                                              end)))
                 (throw 'next-match nil))
             (reftex-highlight 0 (match-beginning 0) (match-end 0))
-            (setq rpl 
+            (setq rpl
                   (save-match-data
                     (reftex-index-make-replace-string
                      macro-fmt (match-string 0) ckey repeat mathp)))
-            (while 
+            (while
                 (not
                  (catch 'loop
                    (message "REPLACE: %s?   (yn!qoe%s?)"
                             rpl
-                            (if (> nkeys 1) 
+                            (if (> nkeys 1)
                                 (concat "1-" (int-to-string nkeys))
                               ""))
                    (setq char (if all-yes ?y (read-char-exclusive)))
@@ -1905,7 +1960,7 @@ both ends."
                          ((member char '(?o ?O))
                           ;; Select a differnt macro
                           (let* ((nc (reftex-index-select-phrases-macro 2))
-                                 (macro-data 
+                                 (macro-data
                                   (cdr (assoc nc reftex-index-phrases-macro-data)))
                                  (macro-fmt (car macro-data))
                                  (repeat (nth 1 macro-data)))
@@ -1923,7 +1978,7 @@ both ends."
                           ;; Recursive edit
                           (save-match-data
                             (save-excursion
-                              (message "%s" 
+                              (message "%s"
                                (substitute-command-keys
                                 "Recursive edit.  Resume with \\[exit-recursive-edit]"))
                               (recursive-edit))))
@@ -2041,69 +2096,5 @@ Does not do a save-excursion."
                                   reftex-index-phrases-macro-data "\n"))))
     (reftex-select-with-char prompt help delay)))
 
-;; Keybindings and Menu for phrases buffer
-
-(loop for x in
-      '(("\C-c\C-c" . reftex-index-phrases-save-and-return)
-        ("\C-c\C-x" . reftex-index-this-phrase)
-        ("\C-c\C-f" . reftex-index-next-phrase)
-        ("\C-c\C-r" . reftex-index-region-phrases)
-        ("\C-c\C-a" . reftex-index-all-phrases)
-        ("\C-c\C-d" . reftex-index-remaining-phrases)
-        ("\C-c\C-s" . reftex-index-sort-phrases)
-        ("\C-c\C-n" . reftex-index-new-phrase)
-        ("\C-c\C-m" . reftex-index-phrases-set-macro-key)
-        ("\C-c\C-i" . reftex-index-phrases-info)
-        ("\C-c\C-t" . reftex-index-find-next-conflict-phrase)
-        ("\C-i"     . self-insert-command))
-      do (define-key reftex-index-phrases-map (car x) (cdr x)))
-
-(easy-menu-define 
- reftex-index-phrases-menu reftex-index-phrases-map
- "Menu for Phrases buffer"
- '("Phrases"
-   ["New Phrase" reftex-index-new-phrase t]
-   ["Set Phrase Macro" reftex-index-phrases-set-macro-key t]
-   ["Recreate File Header" reftex-index-initialize-phrases-buffer t]
-   "--"
-   ("Sort Phrases"
-    ["Sort" reftex-index-sort-phrases t]
-    "--"
-    "Sort Options"
-    ["by Search Phrase" (setq reftex-index-phrases-sort-prefers-entry nil)
-     :style radio :selected (not reftex-index-phrases-sort-prefers-entry)]
-    ["by Index Entry" (setq reftex-index-phrases-sort-prefers-entry t)
-     :style radio :selected reftex-index-phrases-sort-prefers-entry]
-    ["in Blocks" (setq reftex-index-phrases-sort-in-blocks
-                          (not reftex-index-phrases-sort-in-blocks))
-     :style toggle :selected reftex-index-phrases-sort-in-blocks])
-   ["Describe Phrase" reftex-index-phrases-info t]
-   ["Next Phrase Conflict" reftex-index-find-next-conflict-phrase t]
-   "--"
-   ("Find and Index in Document"
-    ["Current Phrase" reftex-index-this-phrase t]
-    ["Next Phrase" reftex-index-next-phrase t]
-    ["Current and Following" reftex-index-remaining-phrases t]
-    ["Region Phrases" reftex-index-region-phrases t]
-    ["All Phrases" reftex-index-all-phrases t]
-    "--"
-    "Options"
-    ["Match Whole Words" (setq reftex-index-phrases-search-whole-words
-                          (not reftex-index-phrases-search-whole-words))
-     :style toggle :selected reftex-index-phrases-search-whole-words]
-    ["Case Sensitive Search" (setq reftex-index-phrases-case-fold-search
-                                  (not  reftex-index-phrases-case-fold-search))
-     :style toggle :selected (not
-                              reftex-index-phrases-case-fold-search)]
-    ["Wrap Long Lines" (setq reftex-index-phrases-wrap-long-lines
-                             (not reftex-index-phrases-wrap-long-lines))
-    :style toggle :selected reftex-index-phrases-wrap-long-lines]
-    ["Skip Indexed Matches" (setq reftex-index-phrases-skip-indexed-matches
-                                  (not reftex-index-phrases-skip-indexed-matches))
-     :style toggle :selected reftex-index-phrases-skip-indexed-matches])
-   "--"
-   ["Save and Return" reftex-index-phrases-save-and-return t]))
-
-
-;;; arch-tag: 4b2362af-c156-42c1-8932-ea2823e205c1
+
 ;;; reftex-index.el ends here