reftex-auc.el 8.75 KB
Newer Older
David Kastrup's avatar
David Kastrup committed
1
;;; reftex-auc.el --- RefTeX's interface to AUCTeX
Glenn Morris's avatar
Glenn Morris committed
2 3

;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
5

Carsten Dominik's avatar
Carsten Dominik committed
6
;; Author: Carsten Dominik <dominik@science.uva.nl>
7
;; Maintainer: auctex-devel@gnu.org
Carsten Dominik's avatar
Carsten Dominik committed
8
;; Version: 4.31
Richard M. Stallman's avatar
Richard M. Stallman committed
9 10 11

;; This file is part of GNU Emacs.

12
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
13
;; it under the terms of the GNU General Public License as published by
14 15
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
16 17 18 19 20 21 22

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
23
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Carsten Dominik's avatar
Carsten Dominik committed
24

25 26 27 28
;;; Commentary:

;;; Code:

29
(eval-when-compile (require 'cl))
Carsten Dominik's avatar
Carsten Dominik committed
30 31 32 33
(provide 'reftex-auc)
(require 'reftex)
;;;

34 35 36 37 38 39 40 41 42 43 44 45
(declare-function TeX-argument-insert "ext:tex" (name optional &optional prefix))
(declare-function TeX-argument-prompt "ext:tex" (optional prompt default &optional complete))
(declare-function multi-prompt "ext:multi-prompt" 
		  (separator
		   unique prompt table
		   &optional mp-predicate require-match initial history))
(declare-function LaTeX-add-index-entries "ext:tex" (&rest entries) t)
(declare-function LaTeX-add-labels "ext:tex" (&rest entries) t)
(declare-function LaTeX-bibitem-list "ext:tex" () t)
(declare-function LaTeX-index-entry-list "ext:tex" () t)
(declare-function LaTeX-label-list "ext:tex" () t)

Carsten Dominik's avatar
Carsten Dominik committed
46 47 48 49
(defun reftex-plug-flag (which)
  ;; Tell if a certain flag is set in reftex-plug-into-AUCTeX
  (or (eq t reftex-plug-into-AUCTeX)
      (and (listp reftex-plug-into-AUCTeX)
Carsten Dominik's avatar
Carsten Dominik committed
50
           (nth which reftex-plug-into-AUCTeX))))
Carsten Dominik's avatar
Carsten Dominik committed
51 52 53 54 55 56 57 58 59

(defun reftex-arg-label (optional &optional prompt definition)
  "Use `reftex-label', `reftex-reference' or AUCTeX's code to insert label arg.
What is being used depends upon `reftex-plug-into-AUCTeX'."
  (let (label)
    (cond
     ((and definition (reftex-plug-flag 1))
      ;; Create a new label, with a temporary brace for `reftex-what-macro'
      (unwind-protect
Carsten Dominik's avatar
Carsten Dominik committed
60
          (progn (insert "{") (setq label (or (reftex-label nil t) "")))
61
        (delete-char -1)))
Carsten Dominik's avatar
Carsten Dominik committed
62 63 64 65 66 67
     ((and (not definition) (reftex-plug-flag 2))
      ;; Reference a label with RefTeX
      (setq label (reftex-reference nil t)))
     (t
      ;; AUCTeX's default mechanism
      (setq label (completing-read (TeX-argument-prompt optional prompt "Key")
Carsten Dominik's avatar
Carsten Dominik committed
68
                                   (LaTeX-label-list)))))
Carsten Dominik's avatar
Carsten Dominik committed
69
    (if (and definition (not (string-equal "" label)))
Carsten Dominik's avatar
Carsten Dominik committed
70
        (LaTeX-add-labels label))
Carsten Dominik's avatar
Carsten Dominik committed
71 72 73 74 75 76 77 78 79 80 81
    (TeX-argument-insert label optional)))

(defun reftex-arg-cite (optional &optional prompt definition)
  "Use `reftex-citation' or AUCTeX's code to insert a cite-key macro argument.
What is being used depends upon `reftex-plug-into-AUCTeX'."
  (let (items)
    (cond
     ((and (not definition) (reftex-plug-flag 3))
      (setq items (list (or (reftex-citation t) ""))))
     (t
      (setq prompt (concat (if optional "(Optional) " "")
82 83
			   (if prompt prompt "Add key")
			   " (default none): "))
Carsten Dominik's avatar
Carsten Dominik committed
84 85 86 87 88 89
      (setq items (multi-prompt "," t prompt (LaTeX-bibitem-list)))))
    (apply 'LaTeX-add-bibitems items)
    (TeX-argument-insert (mapconcat 'identity items ",") optional)))


(defun reftex-arg-index-tag (optional &optional prompt &rest args)
90
  "Prompt for an index tag with completion.
Carsten Dominik's avatar
Carsten Dominik committed
91 92 93
This is the name of an index, not the entry."
  (let (tag taglist)
    (setq prompt (concat (if optional "(Optional) " "")
94 95
			 (if prompt prompt "Index tag")
			 " (default none): "))
Carsten Dominik's avatar
Carsten Dominik committed
96
    (if (and reftex-support-index (reftex-plug-flag 4))
Carsten Dominik's avatar
Carsten Dominik committed
97 98 99
        ;; Use RefTeX completion
        (progn
          (reftex-access-scan-info nil)
100 101
          (setq taglist
                (cdr (assoc 'index-tags
Carsten Dominik's avatar
Carsten Dominik committed
102 103
                            (symbol-value reftex-docstruct-symbol)))
                tag (completing-read prompt (mapcar 'list taglist))))
Carsten Dominik's avatar
Carsten Dominik committed
104 105 106 107 108 109 110 111 112 113
      ;; Just ask like AUCTeX does.
      (setq tag (read-string prompt)))
    (TeX-argument-insert tag optional)))

(defun reftex-arg-index (optional &optional prompt &rest args)
  "Prompt for an index entry completing with known entries.
Completion is specific for just one index, if the macro or a tag
argument identify one of multiple indices."
  (let* (tag key)
    (if (and reftex-support-index (reftex-plug-flag 4))
Carsten Dominik's avatar
Carsten Dominik committed
114 115 116 117
        (progn
          (reftex-access-scan-info nil)
          (setq tag (reftex-what-index-tag)
                key (reftex-index-complete-key (or tag "idx"))))
Carsten Dominik's avatar
Carsten Dominik committed
118
      (setq key (completing-read (TeX-argument-prompt optional prompt "Key")
Carsten Dominik's avatar
Carsten Dominik committed
119
                                 (LaTeX-index-entry-list))))
Carsten Dominik's avatar
Carsten Dominik committed
120 121 122 123 124 125 126
    (unless (string-equal "" key)
      (LaTeX-add-index-entries key))
    (TeX-argument-insert key optional)))

(defun reftex-what-index-tag ()
  ;; Look backward to find out what index the macro at point belongs to
  (let ((macro (save-excursion
Carsten Dominik's avatar
Carsten Dominik committed
127 128 129
                 (and (re-search-backward "\\\\[a-zA-Z*]+" nil t)
                      (match-string 0))))
        tag entry)
Carsten Dominik's avatar
Carsten Dominik committed
130
    (when (and macro
Carsten Dominik's avatar
Carsten Dominik committed
131
               (setq entry (assoc macro reftex-index-macro-alist)))
Carsten Dominik's avatar
Carsten Dominik committed
132 133 134 135
      (setq tag (nth 1 entry))
      (cond
       ((stringp tag) tag)
       ((integerp tag)
Carsten Dominik's avatar
Carsten Dominik committed
136 137 138
        (save-excursion
          (goto-char (match-end 1))
          (or (reftex-nth-arg tag (nth 6 entry)) "idx")))
Carsten Dominik's avatar
Carsten Dominik committed
139 140 141 142 143 144 145
       (t "idx")))))

(defvar LaTeX-label-function)
(defun reftex-plug-into-AUCTeX ()
  ;; Replace AUCTeX functions with RefTeX functions.
  ;; Which functions are replaced is controlled by the variable
  ;; `reftex-plug-into-AUCTeX'.
146

Carsten Dominik's avatar
Carsten Dominik committed
147 148 149 150 151 152 153 154 155 156 157
  (if (reftex-plug-flag 0)
      (setq LaTeX-label-function 'reftex-label)
    (setq LaTeX-label-function nil))

  (and (or (reftex-plug-flag 1) (reftex-plug-flag 2))
       (fboundp 'TeX-arg-label)
       (fset 'TeX-arg-label 'reftex-arg-label))

  (and (reftex-plug-flag 3)
       (fboundp 'TeX-arg-cite)
       (fset 'TeX-arg-cite 'reftex-arg-cite))
158 159

  (and (reftex-plug-flag 4)
Carsten Dominik's avatar
Carsten Dominik committed
160 161
       (fboundp 'TeX-arg-index-tag)
       (fset 'TeX-arg-index-tag 'reftex-arg-index-tag))
162
  (and (reftex-plug-flag 4)
Carsten Dominik's avatar
Carsten Dominik committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
       (fboundp 'TeX-arg-index)
       (fset 'TeX-arg-index 'reftex-arg-index)))

(defun reftex-toggle-plug-into-AUCTeX ()
  "Toggle Interface between AUCTeX and RefTeX on and off."
  (interactive)
  (unless (and (featurep 'tex-site) (featurep 'latex))
    (error "AUCTeX's LaTeX mode does not seem to be loaded"))
  (setq reftex-plug-into-AUCTeX (not reftex-plug-into-AUCTeX))
  (reftex-plug-into-AUCTeX)
  (if reftex-plug-into-AUCTeX
      (message "RefTeX has been plugged into AUCTeX.")
    (message "RefTeX no longer interacts with AUCTeX.")))

(defun reftex-add-label-environments (entry-list)
  "Add label environment descriptions to `reftex-label-alist-style'.
The format of ENTRY-LIST is exactly like `reftex-label-alist'.  See there
for details.
This function makes it possible to support RefTeX from AUCTeX style files.
The entries in ENTRY-LIST will be processed after the user settings in
`reftex-label-alist', and before the defaults (specified in
`reftex-default-label-alist-entries').  Any changes made to
`reftex-label-alist-style' will raise a flag to the effect that
the label information is recompiled on next use."
  (unless reftex-docstruct-symbol
    (reftex-tie-multifile-symbols))
  (when (and reftex-docstruct-symbol
Carsten Dominik's avatar
Carsten Dominik committed
190
             (symbolp reftex-docstruct-symbol))
Carsten Dominik's avatar
Carsten Dominik committed
191
    (let ((list (get reftex-docstruct-symbol 'reftex-label-alist-style))
Carsten Dominik's avatar
Carsten Dominik committed
192
          entry changed)
Carsten Dominik's avatar
Carsten Dominik committed
193
      (while entry-list
Carsten Dominik's avatar
Carsten Dominik committed
194 195 196 197 198
        (setq entry (pop entry-list))
        (unless (member entry list)
          (setq reftex-tables-dirty t
                changed t)
          (push entry list)))
Carsten Dominik's avatar
Carsten Dominik committed
199
      (when changed
Carsten Dominik's avatar
Carsten Dominik committed
200
        (put reftex-docstruct-symbol 'reftex-label-alist-style list)))))
Carsten Dominik's avatar
Carsten Dominik committed
201 202 203 204 205 206 207 208 209 210
(defalias 'reftex-add-to-label-alist 'reftex-add-label-environments)

(defun reftex-add-section-levels (entry-list)
  "Add entries to the value of `reftex-section-levels'.
The added values are kept local to the current document.  The format
of ENTRY-LIST is a list of cons cells (\"MACRONAME\" . LEVEL).  See
`reftex-section-levels' for an example."
  (unless reftex-docstruct-symbol
    (reftex-tie-multifile-symbols))
  (when (and reftex-docstruct-symbol
Carsten Dominik's avatar
Carsten Dominik committed
211
             (symbolp reftex-docstruct-symbol))
Carsten Dominik's avatar
Carsten Dominik committed
212
    (let ((list (get reftex-docstruct-symbol 'reftex-section-levels))
Carsten Dominik's avatar
Carsten Dominik committed
213
          entry changed)
Carsten Dominik's avatar
Carsten Dominik committed
214
      (while entry-list
Carsten Dominik's avatar
Carsten Dominik committed
215 216 217 218 219
        (setq entry (pop entry-list))
        (unless (member entry list)
          (setq reftex-tables-dirty t
                changed t)
          (push entry list)))
Carsten Dominik's avatar
Carsten Dominik committed
220
      (when changed
Carsten Dominik's avatar
Carsten Dominik committed
221
        (put reftex-docstruct-symbol 'reftex-section-levels list)))))
Carsten Dominik's avatar
Carsten Dominik committed
222 223 224 225

(defun reftex-notice-new-section ()
  (reftex-notice-new 1 'force))

226
;; arch-tag: 4a798e68-3405-421c-a09b-0269aac64ab4
Carsten Dominik's avatar
Carsten Dominik committed
227
;;; reftex-auc.el ends here