Merge from emacs-24; up to 2014-05-29T17:16:00Z!dmantipov@yandex.ru
[bpt/emacs.git] / lisp / textmodes / reftex-auc.el
CommitLineData
818226ac 1;;; reftex-auc.el --- RefTeX's interface to AUCTeX
f2e3589a 2
ba318903 3;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
3ba2590f 4
6fbeb429 5;; Author: Carsten Dominik <dominik@science.uva.nl>
ce545621 6;; Maintainer: auctex-devel@gnu.org
3ba2590f
RS
7
8;; This file is part of GNU Emacs.
9
1fecc8fe 10;; GNU Emacs is free software: you can redistribute it and/or modify
3ba2590f 11;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
3ba2590f
RS
14
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.
19
20;; You should have received a copy of the GNU General Public License
1fecc8fe 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1a9461d0 22
3afbc435
PJ
23;;; Commentary:
24
25;;; Code:
26
7c4d13cc 27(eval-when-compile (require 'cl))
4f595e15 28
1a9461d0 29(require 'reftex)
4f595e15
RA
30
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))
f29263b3 35(declare-function LaTeX-add-labels "ext:tex" (&rest entries) t)
4f595e15 36(declare-function LaTeX-add-index-entries "ext:tex" (&rest entries) t)
f29263b3
DN
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)
4f595e15
RA
40(declare-function multi-prompt "ext:multi-prompt"
41 (separator unique prompt table &optional
42 mp-predicate require-match initial history))
f29263b3 43
1a9461d0
CD
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)
3666daf6 48 (nth which reftex-plug-into-AUCTeX))))
1a9461d0 49
41116c5a 50;;;###autoload
1a9461d0
CD
51(defun reftex-arg-label (optional &optional prompt definition)
52 "Use `reftex-label', `reftex-reference' or AUCTeX's code to insert label arg.
53What is being used depends upon `reftex-plug-into-AUCTeX'."
54 (let (label)
55 (cond
56 ((and definition (reftex-plug-flag 1))
57 ;; Create a new label, with a temporary brace for `reftex-what-macro'
58 (unwind-protect
3666daf6 59 (progn (insert "{") (setq label (or (reftex-label nil t) "")))
d355a0b7 60 (delete-char -1)))
1a9461d0
CD
61 ((and (not definition) (reftex-plug-flag 2))
62 ;; Reference a label with RefTeX
63 (setq label (reftex-reference nil t)))
64 (t
65 ;; AUCTeX's default mechanism
66 (setq label (completing-read (TeX-argument-prompt optional prompt "Key")
3666daf6 67 (LaTeX-label-list)))))
1a9461d0 68 (if (and definition (not (string-equal "" label)))
3666daf6 69 (LaTeX-add-labels label))
1a9461d0
CD
70 (TeX-argument-insert label optional)))
71
41116c5a 72;;;###autoload
1a9461d0
CD
73(defun reftex-arg-cite (optional &optional prompt definition)
74 "Use `reftex-citation' or AUCTeX's code to insert a cite-key macro argument.
75What is being used depends upon `reftex-plug-into-AUCTeX'."
76 (let (items)
77 (cond
78 ((and (not definition) (reftex-plug-flag 3))
4f595e15 79 (setq items (or (reftex-citation t) (list ""))))
1a9461d0
CD
80 (t
81 (setq prompt (concat (if optional "(Optional) " "")
5b76833f
RF
82 (if prompt prompt "Add key")
83 " (default none): "))
1a9461d0
CD
84 (setq items (multi-prompt "," t prompt (LaTeX-bibitem-list)))))
85 (apply 'LaTeX-add-bibitems items)
4f595e15
RA
86 (TeX-argument-insert (mapconcat 'identity items reftex-cite-key-separator)
87 optional)))
1a9461d0
CD
88
89
41116c5a 90;;;###autoload
1a9461d0 91(defun reftex-arg-index-tag (optional &optional prompt &rest args)
ceb4c4d3 92 "Prompt for an index tag with completion.
1a9461d0
CD
93This is the name of an index, not the entry."
94 (let (tag taglist)
95 (setq prompt (concat (if optional "(Optional) " "")
5b76833f
RF
96 (if prompt prompt "Index tag")
97 " (default none): "))
1a9461d0 98 (if (and reftex-support-index (reftex-plug-flag 4))
3666daf6
CD
99 ;; Use RefTeX completion
100 (progn
101 (reftex-access-scan-info nil)
ceb4c4d3
TTN
102 (setq taglist
103 (cdr (assoc 'index-tags
3666daf6
CD
104 (symbol-value reftex-docstruct-symbol)))
105 tag (completing-read prompt (mapcar 'list taglist))))
1a9461d0
CD
106 ;; Just ask like AUCTeX does.
107 (setq tag (read-string prompt)))
108 (TeX-argument-insert tag optional)))
109
41116c5a 110;;;###autoload
1a9461d0
CD
111(defun reftex-arg-index (optional &optional prompt &rest args)
112 "Prompt for an index entry completing with known entries.
113Completion is specific for just one index, if the macro or a tag
114argument identify one of multiple indices."
115 (let* (tag key)
116 (if (and reftex-support-index (reftex-plug-flag 4))
3666daf6
CD
117 (progn
118 (reftex-access-scan-info nil)
119 (setq tag (reftex-what-index-tag)
120 key (reftex-index-complete-key (or tag "idx"))))
1a9461d0 121 (setq key (completing-read (TeX-argument-prompt optional prompt "Key")
3666daf6 122 (LaTeX-index-entry-list))))
1a9461d0
CD
123 (unless (string-equal "" key)
124 (LaTeX-add-index-entries key))
125 (TeX-argument-insert key optional)))
126
127(defun reftex-what-index-tag ()
128 ;; Look backward to find out what index the macro at point belongs to
129 (let ((macro (save-excursion
3666daf6
CD
130 (and (re-search-backward "\\\\[a-zA-Z*]+" nil t)
131 (match-string 0))))
132 tag entry)
1a9461d0 133 (when (and macro
3666daf6 134 (setq entry (assoc macro reftex-index-macro-alist)))
1a9461d0
CD
135 (setq tag (nth 1 entry))
136 (cond
137 ((stringp tag) tag)
138 ((integerp tag)
3666daf6
CD
139 (save-excursion
140 (goto-char (match-end 1))
141 (or (reftex-nth-arg tag (nth 6 entry)) "idx")))
1a9461d0
CD
142 (t "idx")))))
143
144(defvar LaTeX-label-function)
41116c5a 145;;;###autoload
1a9461d0
CD
146(defun reftex-plug-into-AUCTeX ()
147 ;; Replace AUCTeX functions with RefTeX functions.
148 ;; Which functions are replaced is controlled by the variable
149 ;; `reftex-plug-into-AUCTeX'.
ceb4c4d3 150
1a9461d0
CD
151 (if (reftex-plug-flag 0)
152 (setq LaTeX-label-function 'reftex-label)
153 (setq LaTeX-label-function nil))
154
155 (and (or (reftex-plug-flag 1) (reftex-plug-flag 2))
156 (fboundp 'TeX-arg-label)
157 (fset 'TeX-arg-label 'reftex-arg-label))
158
159 (and (reftex-plug-flag 3)
160 (fboundp 'TeX-arg-cite)
161 (fset 'TeX-arg-cite 'reftex-arg-cite))
ceb4c4d3
TTN
162
163 (and (reftex-plug-flag 4)
1a9461d0
CD
164 (fboundp 'TeX-arg-index-tag)
165 (fset 'TeX-arg-index-tag 'reftex-arg-index-tag))
ceb4c4d3 166 (and (reftex-plug-flag 4)
1a9461d0
CD
167 (fboundp 'TeX-arg-index)
168 (fset 'TeX-arg-index 'reftex-arg-index)))
169
41116c5a 170;;;###autoload
1a9461d0
CD
171(defun reftex-toggle-plug-into-AUCTeX ()
172 "Toggle Interface between AUCTeX and RefTeX on and off."
173 (interactive)
174 (unless (and (featurep 'tex-site) (featurep 'latex))
175 (error "AUCTeX's LaTeX mode does not seem to be loaded"))
176 (setq reftex-plug-into-AUCTeX (not reftex-plug-into-AUCTeX))
177 (reftex-plug-into-AUCTeX)
178 (if reftex-plug-into-AUCTeX
179 (message "RefTeX has been plugged into AUCTeX.")
180 (message "RefTeX no longer interacts with AUCTeX.")))
181
41116c5a 182;;;###autoload
1a9461d0
CD
183(defun reftex-add-label-environments (entry-list)
184 "Add label environment descriptions to `reftex-label-alist-style'.
185The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there
186for details.
187This function makes it possible to support RefTeX from AUCTeX style files.
188The entries in ENTRY-LIST will be processed after the user settings in
189`reftex-label-alist', and before the defaults (specified in
190`reftex-default-label-alist-entries'). Any changes made to
191`reftex-label-alist-style' will raise a flag to the effect that
192the label information is recompiled on next use."
193 (unless reftex-docstruct-symbol
194 (reftex-tie-multifile-symbols))
195 (when (and reftex-docstruct-symbol
3666daf6 196 (symbolp reftex-docstruct-symbol))
1a9461d0 197 (let ((list (get reftex-docstruct-symbol 'reftex-label-alist-style))
3666daf6 198 entry changed)
1a9461d0 199 (while entry-list
3666daf6
CD
200 (setq entry (pop entry-list))
201 (unless (member entry list)
202 (setq reftex-tables-dirty t
203 changed t)
204 (push entry list)))
1a9461d0 205 (when changed
3666daf6 206 (put reftex-docstruct-symbol 'reftex-label-alist-style list)))))
41116c5a 207;;;###autoload
1a9461d0
CD
208(defalias 'reftex-add-to-label-alist 'reftex-add-label-environments)
209
41116c5a 210;;;###autoload
1a9461d0
CD
211(defun reftex-add-section-levels (entry-list)
212 "Add entries to the value of `reftex-section-levels'.
213The added values are kept local to the current document. The format
214of ENTRY-LIST is a list of cons cells (\"MACRONAME\" . LEVEL). See
215`reftex-section-levels' for an example."
216 (unless reftex-docstruct-symbol
217 (reftex-tie-multifile-symbols))
218 (when (and reftex-docstruct-symbol
3666daf6 219 (symbolp reftex-docstruct-symbol))
1a9461d0 220 (let ((list (get reftex-docstruct-symbol 'reftex-section-levels))
3666daf6 221 entry changed)
1a9461d0 222 (while entry-list
3666daf6
CD
223 (setq entry (pop entry-list))
224 (unless (member entry list)
225 (setq reftex-tables-dirty t
226 changed t)
227 (push entry list)))
1a9461d0 228 (when changed
3666daf6 229 (put reftex-docstruct-symbol 'reftex-section-levels list)))))
1a9461d0 230
41116c5a 231;;;###autoload
1a9461d0
CD
232(defun reftex-notice-new-section ()
233 (reftex-notice-new 1 'force))
234
4f595e15
RA
235(provide 'reftex-auc)
236
1a9461d0 237;;; reftex-auc.el ends here
41116c5a
GM
238
239;; Local Variables:
240;; generated-autoload-file: "reftex.el"
241;; End: