1;;; reftex-auc.el --- RefTeX's interface to AUCTeX
2
3;; Copyright (C) 1997-2021 Free Software Foundation, Inc.
4
5;; Author: Carsten Dominik <dominik@science.uva.nl>
6;; Maintainer: auctex-devel@gnu.org
7
8;; This file is part of GNU Emacs.
9
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.
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
21;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
22
23;;; Commentary:
24
25;;; Code:
26
27(eval-when-compile (require 'cl-lib))
28
29(require 'reftex)
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))
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))
43
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))))
49
50;;;###autoload
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
59          (progn (insert "{") (setq label (or (reftex-label nil t) "")))
60        (delete-char -1)))
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")
67                                   (LaTeX-label-list)))))
68    (if (and definition (not (string-equal "" label)))
69        (LaTeX-add-labels label))
70    (TeX-argument-insert label optional)))
71
72;;;###autoload
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))
79      (setq items (or (reftex-citation t) (list ""))))
80     (t
81      (setq prompt (concat (if optional "(Optional) " "")
82			   (if prompt prompt "Add key")
83			   " (default none): "))
84      (setq items (multi-prompt "," t prompt (LaTeX-bibitem-list)))))
85    (apply 'LaTeX-add-bibitems items)
86    (TeX-argument-insert (mapconcat 'identity items reftex-cite-key-separator)
87			 optional)))
88
89
90;;;###autoload
91(defun reftex-arg-index-tag (optional &optional prompt &rest args)
92  "Prompt for an index tag with completion.
93This is the name of an index, not the entry."
94  (let (tag taglist)
95    (setq prompt (concat (if optional "(Optional) " "")
96			 (if prompt prompt "Index tag")
97			 " (default none): "))
98    (if (and reftex-support-index (reftex-plug-flag 4))
99        ;; Use RefTeX completion
100        (progn
101          (reftex-access-scan-info nil)
102          (setq taglist
103                (cdr (assoc 'index-tags
104                            (symbol-value reftex-docstruct-symbol)))
105                tag (completing-read prompt (mapcar 'list taglist))))
106      ;; Just ask like AUCTeX does.
107      (setq tag (read-string prompt)))
108    (TeX-argument-insert tag optional)))
109
110;;;###autoload
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))
117        (progn
118          (reftex-access-scan-info nil)
119          (setq tag (reftex-what-index-tag)
120                key (reftex-index-complete-key (or tag "idx"))))
121      (setq key (completing-read (TeX-argument-prompt optional prompt "Key")
122                                 (LaTeX-index-entry-list))))
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
130                 (and (re-search-backward "\\\\[a-zA-Z*]+" nil t)
131                      (match-string 0))))
132        tag entry)
133    (when (and macro
134               (setq entry (assoc macro reftex-index-macro-alist)))
135      (setq tag (nth 1 entry))
136      (cond
137       ((stringp tag) tag)
138       ((integerp tag)
139        (save-excursion
140          (goto-char (match-end 0))
141          (or (reftex-nth-arg tag (nth 6 entry)) "idx")))
142       (t "idx")))))
143
144(defvar LaTeX-label-function)
145;;;###autoload
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'.
150
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))
162
163  (and (reftex-plug-flag 4)
164       (fboundp 'TeX-arg-index-tag)
165       (fset 'TeX-arg-index-tag 'reftex-arg-index-tag))
166  (and (reftex-plug-flag 4)
167       (fboundp 'TeX-arg-index)
168       (fset 'TeX-arg-index 'reftex-arg-index)))
169
170;;;###autoload
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
182;;;###autoload
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
196             (symbolp reftex-docstruct-symbol))
197    (let ((list (get reftex-docstruct-symbol 'reftex-label-alist-style))
198          entry changed)
199      (while entry-list
200        (setq entry (pop entry-list))
201        (unless (member entry list)
202          (setq reftex-tables-dirty t
203                changed t)
204          (push entry list)))
205      (when changed
206        (put reftex-docstruct-symbol 'reftex-label-alist-style list)))))
207;;;###autoload
208(defalias 'reftex-add-to-label-alist 'reftex-add-label-environments)
209
210;;;###autoload
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
219             (symbolp reftex-docstruct-symbol))
220    (let ((list (get reftex-docstruct-symbol 'reftex-section-levels))
221          entry changed)
222      (while entry-list
223        (setq entry (pop entry-list))
224        (unless (member entry list)
225          (setq reftex-tables-dirty t
226                changed t)
227          (push entry list)))
228      (when changed
229        (put reftex-docstruct-symbol 'reftex-section-levels list)))))
230
231;;;###autoload
232(defun reftex-notice-new-section ()
233  (reftex-notice-new 1 'force))
234
235(provide 'reftex-auc)
236
237;;; reftex-auc.el ends here
238
239;; Local Variables:
240;; generated-autoload-file: "reftex-loaddefs.el"
241;; End:
242