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