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, | |
12dc447f | 4 | ;; 2006, 2007, 2008 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 |
3ba2590f RS |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
5a9dffec | 14 | ;; the Free Software Foundation; either version 3, or (at your option) |
3ba2590f RS |
15 | ;; any later version. |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
27e81652 TTN |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
1a9461d0 | 26 | |
3afbc435 PJ |
27 | ;;; Commentary: |
28 | ||
29 | ;;; Code: | |
30 | ||
7c4d13cc | 31 | (eval-when-compile (require 'cl)) |
1a9461d0 CD |
32 | (provide 'reftex-auc) |
33 | (require 'reftex) | |
34 | ;;; | |
35 | ||
f29263b3 DN |
36 | (declare-function TeX-argument-insert "ext:tex" (name optional &optional prefix)) |
37 | (declare-function TeX-argument-prompt "ext:tex" (optional prompt default &optional complete)) | |
38 | (declare-function multi-prompt "ext:multi-prompt" | |
39 | (separator | |
40 | unique prompt table | |
41 | &optional mp-predicate require-match initial history)) | |
42 | (declare-function LaTeX-add-index-entries "ext:tex" (&rest entries) t) | |
43 | (declare-function LaTeX-add-labels "ext:tex" (&rest entries) t) | |
44 | (declare-function LaTeX-bibitem-list "ext:tex" () t) | |
45 | (declare-function LaTeX-index-entry-list "ext:tex" () t) | |
46 | (declare-function LaTeX-label-list "ext:tex" () t) | |
47 | ||
1a9461d0 CD |
48 | (defun reftex-plug-flag (which) |
49 | ;; Tell if a certain flag is set in reftex-plug-into-AUCTeX | |
50 | (or (eq t reftex-plug-into-AUCTeX) | |
51 | (and (listp reftex-plug-into-AUCTeX) | |
3666daf6 | 52 | (nth which reftex-plug-into-AUCTeX)))) |
1a9461d0 CD |
53 | |
54 | (defun reftex-arg-label (optional &optional prompt definition) | |
55 | "Use `reftex-label', `reftex-reference' or AUCTeX's code to insert label arg. | |
56 | What is being used depends upon `reftex-plug-into-AUCTeX'." | |
57 | (let (label) | |
58 | (cond | |
59 | ((and definition (reftex-plug-flag 1)) | |
60 | ;; Create a new label, with a temporary brace for `reftex-what-macro' | |
61 | (unwind-protect | |
3666daf6 CD |
62 | (progn (insert "{") (setq label (or (reftex-label nil t) ""))) |
63 | (delete-backward-char 1))) | |
1a9461d0 CD |
64 | ((and (not definition) (reftex-plug-flag 2)) |
65 | ;; Reference a label with RefTeX | |
66 | (setq label (reftex-reference nil t))) | |
67 | (t | |
68 | ;; AUCTeX's default mechanism | |
69 | (setq label (completing-read (TeX-argument-prompt optional prompt "Key") | |
3666daf6 | 70 | (LaTeX-label-list))))) |
1a9461d0 | 71 | (if (and definition (not (string-equal "" label))) |
3666daf6 | 72 | (LaTeX-add-labels label)) |
1a9461d0 CD |
73 | (TeX-argument-insert label optional))) |
74 | ||
75 | (defun reftex-arg-cite (optional &optional prompt definition) | |
76 | "Use `reftex-citation' or AUCTeX's code to insert a cite-key macro argument. | |
77 | What is being used depends upon `reftex-plug-into-AUCTeX'." | |
78 | (let (items) | |
79 | (cond | |
80 | ((and (not definition) (reftex-plug-flag 3)) | |
81 | (setq items (list (or (reftex-citation t) "")))) | |
82 | (t | |
83 | (setq prompt (concat (if optional "(Optional) " "") | |
5b76833f RF |
84 | (if prompt prompt "Add key") |
85 | " (default none): ")) | |
1a9461d0 CD |
86 | (setq items (multi-prompt "," t prompt (LaTeX-bibitem-list))))) |
87 | (apply 'LaTeX-add-bibitems items) | |
88 | (TeX-argument-insert (mapconcat 'identity items ",") optional))) | |
89 | ||
90 | ||
91 | (defun reftex-arg-index-tag (optional &optional prompt &rest args) | |
ceb4c4d3 | 92 | "Prompt for an index tag with completion. |
1a9461d0 CD |
93 | This 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 | ||
110 | (defun reftex-arg-index (optional &optional prompt &rest args) | |
111 | "Prompt for an index entry completing with known entries. | |
112 | Completion is specific for just one index, if the macro or a tag | |
113 | argument identify one of multiple indices." | |
114 | (let* (tag key) | |
115 | (if (and reftex-support-index (reftex-plug-flag 4)) | |
3666daf6 CD |
116 | (progn |
117 | (reftex-access-scan-info nil) | |
118 | (setq tag (reftex-what-index-tag) | |
119 | key (reftex-index-complete-key (or tag "idx")))) | |
1a9461d0 | 120 | (setq key (completing-read (TeX-argument-prompt optional prompt "Key") |
3666daf6 | 121 | (LaTeX-index-entry-list)))) |
1a9461d0 CD |
122 | (unless (string-equal "" key) |
123 | (LaTeX-add-index-entries key)) | |
124 | (TeX-argument-insert key optional))) | |
125 | ||
126 | (defun reftex-what-index-tag () | |
127 | ;; Look backward to find out what index the macro at point belongs to | |
128 | (let ((macro (save-excursion | |
3666daf6 CD |
129 | (and (re-search-backward "\\\\[a-zA-Z*]+" nil t) |
130 | (match-string 0)))) | |
131 | tag entry) | |
1a9461d0 | 132 | (when (and macro |
3666daf6 | 133 | (setq entry (assoc macro reftex-index-macro-alist))) |
1a9461d0 CD |
134 | (setq tag (nth 1 entry)) |
135 | (cond | |
136 | ((stringp tag) tag) | |
137 | ((integerp tag) | |
3666daf6 CD |
138 | (save-excursion |
139 | (goto-char (match-end 1)) | |
140 | (or (reftex-nth-arg tag (nth 6 entry)) "idx"))) | |
1a9461d0 CD |
141 | (t "idx"))))) |
142 | ||
143 | (defvar LaTeX-label-function) | |
144 | (defun reftex-plug-into-AUCTeX () | |
145 | ;; Replace AUCTeX functions with RefTeX functions. | |
146 | ;; Which functions are replaced is controlled by the variable | |
147 | ;; `reftex-plug-into-AUCTeX'. | |
ceb4c4d3 | 148 | |
1a9461d0 CD |
149 | (if (reftex-plug-flag 0) |
150 | (setq LaTeX-label-function 'reftex-label) | |
151 | (setq LaTeX-label-function nil)) | |
152 | ||
153 | (and (or (reftex-plug-flag 1) (reftex-plug-flag 2)) | |
154 | (fboundp 'TeX-arg-label) | |
155 | (fset 'TeX-arg-label 'reftex-arg-label)) | |
156 | ||
157 | (and (reftex-plug-flag 3) | |
158 | (fboundp 'TeX-arg-cite) | |
159 | (fset 'TeX-arg-cite 'reftex-arg-cite)) | |
ceb4c4d3 TTN |
160 | |
161 | (and (reftex-plug-flag 4) | |
1a9461d0 CD |
162 | (fboundp 'TeX-arg-index-tag) |
163 | (fset 'TeX-arg-index-tag 'reftex-arg-index-tag)) | |
ceb4c4d3 | 164 | (and (reftex-plug-flag 4) |
1a9461d0 CD |
165 | (fboundp 'TeX-arg-index) |
166 | (fset 'TeX-arg-index 'reftex-arg-index))) | |
167 | ||
168 | (defun reftex-toggle-plug-into-AUCTeX () | |
169 | "Toggle Interface between AUCTeX and RefTeX on and off." | |
170 | (interactive) | |
171 | (unless (and (featurep 'tex-site) (featurep 'latex)) | |
172 | (error "AUCTeX's LaTeX mode does not seem to be loaded")) | |
173 | (setq reftex-plug-into-AUCTeX (not reftex-plug-into-AUCTeX)) | |
174 | (reftex-plug-into-AUCTeX) | |
175 | (if reftex-plug-into-AUCTeX | |
176 | (message "RefTeX has been plugged into AUCTeX.") | |
177 | (message "RefTeX no longer interacts with AUCTeX."))) | |
178 | ||
179 | (defun reftex-add-label-environments (entry-list) | |
180 | "Add label environment descriptions to `reftex-label-alist-style'. | |
181 | The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there | |
182 | for details. | |
183 | This function makes it possible to support RefTeX from AUCTeX style files. | |
184 | The entries in ENTRY-LIST will be processed after the user settings in | |
185 | `reftex-label-alist', and before the defaults (specified in | |
186 | `reftex-default-label-alist-entries'). Any changes made to | |
187 | `reftex-label-alist-style' will raise a flag to the effect that | |
188 | the label information is recompiled on next use." | |
189 | (unless reftex-docstruct-symbol | |
190 | (reftex-tie-multifile-symbols)) | |
191 | (when (and reftex-docstruct-symbol | |
3666daf6 | 192 | (symbolp reftex-docstruct-symbol)) |
1a9461d0 | 193 | (let ((list (get reftex-docstruct-symbol 'reftex-label-alist-style)) |
3666daf6 | 194 | entry changed) |
1a9461d0 | 195 | (while entry-list |
3666daf6 CD |
196 | (setq entry (pop entry-list)) |
197 | (unless (member entry list) | |
198 | (setq reftex-tables-dirty t | |
199 | changed t) | |
200 | (push entry list))) | |
1a9461d0 | 201 | (when changed |
3666daf6 | 202 | (put reftex-docstruct-symbol 'reftex-label-alist-style list))))) |
1a9461d0 CD |
203 | (defalias 'reftex-add-to-label-alist 'reftex-add-label-environments) |
204 | ||
205 | (defun reftex-add-section-levels (entry-list) | |
206 | "Add entries to the value of `reftex-section-levels'. | |
207 | The added values are kept local to the current document. The format | |
208 | of ENTRY-LIST is a list of cons cells (\"MACRONAME\" . LEVEL). See | |
209 | `reftex-section-levels' for an example." | |
210 | (unless reftex-docstruct-symbol | |
211 | (reftex-tie-multifile-symbols)) | |
212 | (when (and reftex-docstruct-symbol | |
3666daf6 | 213 | (symbolp reftex-docstruct-symbol)) |
1a9461d0 | 214 | (let ((list (get reftex-docstruct-symbol 'reftex-section-levels)) |
3666daf6 | 215 | entry changed) |
1a9461d0 | 216 | (while entry-list |
3666daf6 CD |
217 | (setq entry (pop entry-list)) |
218 | (unless (member entry list) | |
219 | (setq reftex-tables-dirty t | |
220 | changed t) | |
221 | (push entry list))) | |
1a9461d0 | 222 | (when changed |
3666daf6 | 223 | (put reftex-docstruct-symbol 'reftex-section-levels list))))) |
1a9461d0 CD |
224 | |
225 | (defun reftex-notice-new-section () | |
226 | (reftex-notice-new 1 'force)) | |
227 | ||
cbee283d | 228 | ;; arch-tag: 4a798e68-3405-421c-a09b-0269aac64ab4 |
1a9461d0 | 229 | ;;; reftex-auc.el ends here |