Commit | Line | Data |
---|---|---|
818226ac | 1 | ;;; reftex-auc.el --- RefTeX's interface to AUCTeX |
f2e3589a GM |
2 | |
3 | ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, | |
114f9c96 | 4 | ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
3ba2590f | 5 | |
6fbeb429 | 6 | ;; Author: Carsten Dominik <dominik@science.uva.nl> |
ce545621 | 7 | ;; Maintainer: auctex-devel@gnu.org |
5d2a58e0 | 8 | ;; Version: 4.31 |
bd78fa1d | 9 | ;; Package: reftex |
3ba2590f RS |
10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
1fecc8fe | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
3ba2590f | 14 | ;; it under the terms of the GNU General Public License as published by |
1fecc8fe GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
3ba2590f RS |
17 | |
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. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
1fecc8fe | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
1a9461d0 | 25 | |
3afbc435 PJ |
26 | ;;; Commentary: |
27 | ||
28 | ;;; Code: | |
29 | ||
7c4d13cc | 30 | (eval-when-compile (require 'cl)) |
1a9461d0 CD |
31 | (provide 'reftex-auc) |
32 | (require 'reftex) | |
33 | ;;; | |
34 | ||
f29263b3 DN |
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) | |
46 | ||
1a9461d0 CD |
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) | |
3666daf6 | 51 | (nth which reftex-plug-into-AUCTeX)))) |
1a9461d0 CD |
52 | |
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 | |
3666daf6 | 61 | (progn (insert "{") (setq label (or (reftex-label nil t) ""))) |
d355a0b7 | 62 | (delete-char -1))) |
1a9461d0 CD |
63 | ((and (not definition) (reftex-plug-flag 2)) |
64 | ;; Reference a label with RefTeX | |
65 | (setq label (reftex-reference nil t))) | |
66 | (t | |
67 | ;; AUCTeX's default mechanism | |
68 | (setq label (completing-read (TeX-argument-prompt optional prompt "Key") | |
3666daf6 | 69 | (LaTeX-label-list))))) |
1a9461d0 | 70 | (if (and definition (not (string-equal "" label))) |
3666daf6 | 71 | (LaTeX-add-labels label)) |
1a9461d0 CD |
72 | (TeX-argument-insert label optional))) |
73 | ||
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) "")))) | |
81 | (t | |
82 | (setq prompt (concat (if optional "(Optional) " "") | |
5b76833f RF |
83 | (if prompt prompt "Add key") |
84 | " (default none): ")) | |
1a9461d0 CD |
85 | (setq items (multi-prompt "," t prompt (LaTeX-bibitem-list))))) |
86 | (apply 'LaTeX-add-bibitems items) | |
87 | (TeX-argument-insert (mapconcat 'identity items ",") optional))) | |
88 | ||
89 | ||
90 | (defun reftex-arg-index-tag (optional &optional prompt &rest args) | |
ceb4c4d3 | 91 | "Prompt for an index tag with completion. |
1a9461d0 CD |
92 | This is the name of an index, not the entry." |
93 | (let (tag taglist) | |
94 | (setq prompt (concat (if optional "(Optional) " "") | |
5b76833f RF |
95 | (if prompt prompt "Index tag") |
96 | " (default none): ")) | |
1a9461d0 | 97 | (if (and reftex-support-index (reftex-plug-flag 4)) |
3666daf6 CD |
98 | ;; Use RefTeX completion |
99 | (progn | |
100 | (reftex-access-scan-info nil) | |
ceb4c4d3 TTN |
101 | (setq taglist |
102 | (cdr (assoc 'index-tags | |
3666daf6 CD |
103 | (symbol-value reftex-docstruct-symbol))) |
104 | tag (completing-read prompt (mapcar 'list taglist)))) | |
1a9461d0 CD |
105 | ;; Just ask like AUCTeX does. |
106 | (setq tag (read-string prompt))) | |
107 | (TeX-argument-insert tag optional))) | |
108 | ||
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)) | |
3666daf6 CD |
115 | (progn |
116 | (reftex-access-scan-info nil) | |
117 | (setq tag (reftex-what-index-tag) | |
118 | key (reftex-index-complete-key (or tag "idx")))) | |
1a9461d0 | 119 | (setq key (completing-read (TeX-argument-prompt optional prompt "Key") |
3666daf6 | 120 | (LaTeX-index-entry-list)))) |
1a9461d0 CD |
121 | (unless (string-equal "" key) |
122 | (LaTeX-add-index-entries key)) | |
123 | (TeX-argument-insert key optional))) | |
124 | ||
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 | |
3666daf6 CD |
128 | (and (re-search-backward "\\\\[a-zA-Z*]+" nil t) |
129 | (match-string 0)))) | |
130 | tag entry) | |
1a9461d0 | 131 | (when (and macro |
3666daf6 | 132 | (setq entry (assoc macro reftex-index-macro-alist))) |
1a9461d0 CD |
133 | (setq tag (nth 1 entry)) |
134 | (cond | |
135 | ((stringp tag) tag) | |
136 | ((integerp tag) | |
3666daf6 CD |
137 | (save-excursion |
138 | (goto-char (match-end 1)) | |
139 | (or (reftex-nth-arg tag (nth 6 entry)) "idx"))) | |
1a9461d0 CD |
140 | (t "idx"))))) |
141 | ||
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'. | |
ceb4c4d3 | 147 | |
1a9461d0 CD |
148 | (if (reftex-plug-flag 0) |
149 | (setq LaTeX-label-function 'reftex-label) | |
150 | (setq LaTeX-label-function nil)) | |
151 | ||
152 | (and (or (reftex-plug-flag 1) (reftex-plug-flag 2)) | |
153 | (fboundp 'TeX-arg-label) | |
154 | (fset 'TeX-arg-label 'reftex-arg-label)) | |
155 | ||
156 | (and (reftex-plug-flag 3) | |
157 | (fboundp 'TeX-arg-cite) | |
158 | (fset 'TeX-arg-cite 'reftex-arg-cite)) | |
ceb4c4d3 TTN |
159 | |
160 | (and (reftex-plug-flag 4) | |
1a9461d0 CD |
161 | (fboundp 'TeX-arg-index-tag) |
162 | (fset 'TeX-arg-index-tag 'reftex-arg-index-tag)) | |
ceb4c4d3 | 163 | (and (reftex-plug-flag 4) |
1a9461d0 CD |
164 | (fboundp 'TeX-arg-index) |
165 | (fset 'TeX-arg-index 'reftex-arg-index))) | |
166 | ||
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."))) | |
177 | ||
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 | |
3666daf6 | 191 | (symbolp reftex-docstruct-symbol)) |
1a9461d0 | 192 | (let ((list (get reftex-docstruct-symbol 'reftex-label-alist-style)) |
3666daf6 | 193 | entry changed) |
1a9461d0 | 194 | (while entry-list |
3666daf6 CD |
195 | (setq entry (pop entry-list)) |
196 | (unless (member entry list) | |
197 | (setq reftex-tables-dirty t | |
198 | changed t) | |
199 | (push entry list))) | |
1a9461d0 | 200 | (when changed |
3666daf6 | 201 | (put reftex-docstruct-symbol 'reftex-label-alist-style list))))) |
1a9461d0 CD |
202 | (defalias 'reftex-add-to-label-alist 'reftex-add-label-environments) |
203 | ||
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 | |
3666daf6 | 212 | (symbolp reftex-docstruct-symbol)) |
1a9461d0 | 213 | (let ((list (get reftex-docstruct-symbol 'reftex-section-levels)) |
3666daf6 | 214 | entry changed) |
1a9461d0 | 215 | (while entry-list |
3666daf6 CD |
216 | (setq entry (pop entry-list)) |
217 | (unless (member entry list) | |
218 | (setq reftex-tables-dirty t | |
219 | changed t) | |
220 | (push entry list))) | |
1a9461d0 | 221 | (when changed |
3666daf6 | 222 | (put reftex-docstruct-symbol 'reftex-section-levels list))))) |
1a9461d0 CD |
223 | |
224 | (defun reftex-notice-new-section () | |
225 | (reftex-notice-new 1 'force)) | |
226 | ||
cbee283d | 227 | ;; arch-tag: 4a798e68-3405-421c-a09b-0269aac64ab4 |
1a9461d0 | 228 | ;;; reftex-auc.el ends here |