Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / textmodes / reftex-auc.el
blobce69a64f4c0b5a8e183238b86415684aebe88704
1 ;;; reftex-auc.el --- RefTeX's interface to AUCTeX
3 ;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: auctex-devel@gnu.org
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
29 (require 'reftex)
31 (declare-function TeX-argument-prompt "ext:tex"
32 (optional prompt default &optional complete))
33 (declare-function TeX-argument-insert "ext:tex"
34 (name optional &optional prefix))
35 (declare-function LaTeX-add-labels "ext:tex" (&rest entries) t)
36 (declare-function LaTeX-add-index-entries "ext:tex" (&rest entries) t)
37 (declare-function LaTeX-bibitem-list "ext:tex" () t)
38 (declare-function LaTeX-index-entry-list "ext:tex" () t)
39 (declare-function LaTeX-label-list "ext:tex" () t)
40 (declare-function multi-prompt "ext:multi-prompt"
41 (separator unique prompt table &optional
42 mp-predicate require-match initial history))
44 (defun reftex-plug-flag (which)
45 ;; Tell if a certain flag is set in reftex-plug-into-AUCTeX
46 (or (eq t reftex-plug-into-AUCTeX)
47 (and (listp reftex-plug-into-AUCTeX)
48 (nth which reftex-plug-into-AUCTeX))))
50 (defun reftex-arg-label (optional &optional prompt definition)
51 "Use `reftex-label', `reftex-reference' or AUCTeX's code to insert label arg.
52 What is being used depends upon `reftex-plug-into-AUCTeX'."
53 (let (label)
54 (cond
55 ((and definition (reftex-plug-flag 1))
56 ;; Create a new label, with a temporary brace for `reftex-what-macro'
57 (unwind-protect
58 (progn (insert "{") (setq label (or (reftex-label nil t) "")))
59 (delete-char -1)))
60 ((and (not definition) (reftex-plug-flag 2))
61 ;; Reference a label with RefTeX
62 (setq label (reftex-reference nil t)))
64 ;; AUCTeX's default mechanism
65 (setq label (completing-read (TeX-argument-prompt optional prompt "Key")
66 (LaTeX-label-list)))))
67 (if (and definition (not (string-equal "" label)))
68 (LaTeX-add-labels label))
69 (TeX-argument-insert label optional)))
71 (defun reftex-arg-cite (optional &optional prompt definition)
72 "Use `reftex-citation' or AUCTeX's code to insert a cite-key macro argument.
73 What is being used depends upon `reftex-plug-into-AUCTeX'."
74 (let (items)
75 (cond
76 ((and (not definition) (reftex-plug-flag 3))
77 (setq items (or (reftex-citation t) (list ""))))
79 (setq prompt (concat (if optional "(Optional) " "")
80 (if prompt prompt "Add key")
81 " (default none): "))
82 (setq items (multi-prompt "," t prompt (LaTeX-bibitem-list)))))
83 (apply 'LaTeX-add-bibitems items)
84 (TeX-argument-insert (mapconcat 'identity items reftex-cite-key-separator)
85 optional)))
88 (defun reftex-arg-index-tag (optional &optional prompt &rest args)
89 "Prompt for an index tag with completion.
90 This is the name of an index, not the entry."
91 (let (tag taglist)
92 (setq prompt (concat (if optional "(Optional) " "")
93 (if prompt prompt "Index tag")
94 " (default none): "))
95 (if (and reftex-support-index (reftex-plug-flag 4))
96 ;; Use RefTeX completion
97 (progn
98 (reftex-access-scan-info nil)
99 (setq taglist
100 (cdr (assoc 'index-tags
101 (symbol-value reftex-docstruct-symbol)))
102 tag (completing-read prompt (mapcar 'list taglist))))
103 ;; Just ask like AUCTeX does.
104 (setq tag (read-string prompt)))
105 (TeX-argument-insert tag optional)))
107 (defun reftex-arg-index (optional &optional prompt &rest args)
108 "Prompt for an index entry completing with known entries.
109 Completion is specific for just one index, if the macro or a tag
110 argument identify one of multiple indices."
111 (let* (tag key)
112 (if (and reftex-support-index (reftex-plug-flag 4))
113 (progn
114 (reftex-access-scan-info nil)
115 (setq tag (reftex-what-index-tag)
116 key (reftex-index-complete-key (or tag "idx"))))
117 (setq key (completing-read (TeX-argument-prompt optional prompt "Key")
118 (LaTeX-index-entry-list))))
119 (unless (string-equal "" key)
120 (LaTeX-add-index-entries key))
121 (TeX-argument-insert key optional)))
123 (defun reftex-what-index-tag ()
124 ;; Look backward to find out what index the macro at point belongs to
125 (let ((macro (save-excursion
126 (and (re-search-backward "\\\\[a-zA-Z*]+" nil t)
127 (match-string 0))))
128 tag entry)
129 (when (and macro
130 (setq entry (assoc macro reftex-index-macro-alist)))
131 (setq tag (nth 1 entry))
132 (cond
133 ((stringp tag) tag)
134 ((integerp tag)
135 (save-excursion
136 (goto-char (match-end 1))
137 (or (reftex-nth-arg tag (nth 6 entry)) "idx")))
138 (t "idx")))))
140 (defvar LaTeX-label-function)
141 (defun reftex-plug-into-AUCTeX ()
142 ;; Replace AUCTeX functions with RefTeX functions.
143 ;; Which functions are replaced is controlled by the variable
144 ;; `reftex-plug-into-AUCTeX'.
146 (if (reftex-plug-flag 0)
147 (setq LaTeX-label-function 'reftex-label)
148 (setq LaTeX-label-function nil))
150 (and (or (reftex-plug-flag 1) (reftex-plug-flag 2))
151 (fboundp 'TeX-arg-label)
152 (fset 'TeX-arg-label 'reftex-arg-label))
154 (and (reftex-plug-flag 3)
155 (fboundp 'TeX-arg-cite)
156 (fset 'TeX-arg-cite 'reftex-arg-cite))
158 (and (reftex-plug-flag 4)
159 (fboundp 'TeX-arg-index-tag)
160 (fset 'TeX-arg-index-tag 'reftex-arg-index-tag))
161 (and (reftex-plug-flag 4)
162 (fboundp 'TeX-arg-index)
163 (fset 'TeX-arg-index 'reftex-arg-index)))
165 (defun reftex-toggle-plug-into-AUCTeX ()
166 "Toggle Interface between AUCTeX and RefTeX on and off."
167 (interactive)
168 (unless (and (featurep 'tex-site) (featurep 'latex))
169 (error "AUCTeX's LaTeX mode does not seem to be loaded"))
170 (setq reftex-plug-into-AUCTeX (not reftex-plug-into-AUCTeX))
171 (reftex-plug-into-AUCTeX)
172 (if reftex-plug-into-AUCTeX
173 (message "RefTeX has been plugged into AUCTeX.")
174 (message "RefTeX no longer interacts with AUCTeX.")))
176 (defun reftex-add-label-environments (entry-list)
177 "Add label environment descriptions to `reftex-label-alist-style'.
178 The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there
179 for details.
180 This function makes it possible to support RefTeX from AUCTeX style files.
181 The entries in ENTRY-LIST will be processed after the user settings in
182 `reftex-label-alist', and before the defaults (specified in
183 `reftex-default-label-alist-entries'). Any changes made to
184 `reftex-label-alist-style' will raise a flag to the effect that
185 the label information is recompiled on next use."
186 (unless reftex-docstruct-symbol
187 (reftex-tie-multifile-symbols))
188 (when (and reftex-docstruct-symbol
189 (symbolp reftex-docstruct-symbol))
190 (let ((list (get reftex-docstruct-symbol 'reftex-label-alist-style))
191 entry changed)
192 (while entry-list
193 (setq entry (pop entry-list))
194 (unless (member entry list)
195 (setq reftex-tables-dirty t
196 changed t)
197 (push entry list)))
198 (when changed
199 (put reftex-docstruct-symbol 'reftex-label-alist-style list)))))
200 (defalias 'reftex-add-to-label-alist 'reftex-add-label-environments)
202 (defun reftex-add-section-levels (entry-list)
203 "Add entries to the value of `reftex-section-levels'.
204 The added values are kept local to the current document. The format
205 of ENTRY-LIST is a list of cons cells (\"MACRONAME\" . LEVEL). See
206 `reftex-section-levels' for an example."
207 (unless reftex-docstruct-symbol
208 (reftex-tie-multifile-symbols))
209 (when (and reftex-docstruct-symbol
210 (symbolp reftex-docstruct-symbol))
211 (let ((list (get reftex-docstruct-symbol 'reftex-section-levels))
212 entry changed)
213 (while entry-list
214 (setq entry (pop entry-list))
215 (unless (member entry list)
216 (setq reftex-tables-dirty t
217 changed t)
218 (push entry list)))
219 (when changed
220 (put reftex-docstruct-symbol 'reftex-section-levels list)))))
222 (defun reftex-notice-new-section ()
223 (reftex-notice-new 1 'force))
225 (provide 'reftex-auc)
227 ;;; reftex-auc.el ends here