1;;; reftex-auc.el --- RefTeX's interface to AUCTeX
2
3;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4;;   2006, 2007 Free Software Foundation, Inc.
5
6;; Author: Carsten Dominik <dominik@science.uva.nl>
7;; Maintainer: auctex-devel@gnu.org
8;; Version: 4.31
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
14;; the Free Software Foundation; either version 2, or (at your option)
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
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
26
27;;; Commentary:
28
29;;; Code:
30
31(eval-when-compile (require 'cl))
32(provide 'reftex-auc)
33(require 'reftex)
34;;;
35
36(defun reftex-plug-flag (which)
37  ;; Tell if a certain flag is set in reftex-plug-into-AUCTeX
38  (or (eq t reftex-plug-into-AUCTeX)
39      (and (listp reftex-plug-into-AUCTeX)
40           (nth which reftex-plug-into-AUCTeX))))
41
42(defun reftex-arg-label (optional &optional prompt definition)
43  "Use `reftex-label', `reftex-reference' or AUCTeX's code to insert label arg.
44What is being used depends upon `reftex-plug-into-AUCTeX'."
45  (let (label)
46    (cond
47     ((and definition (reftex-plug-flag 1))
48      ;; Create a new label, with a temporary brace for `reftex-what-macro'
49      (unwind-protect
50          (progn (insert "{") (setq label (or (reftex-label nil t) "")))
51        (delete-backward-char 1)))
52     ((and (not definition) (reftex-plug-flag 2))
53      ;; Reference a label with RefTeX
54      (setq label (reftex-reference nil t)))
55     (t
56      ;; AUCTeX's default mechanism
57      (setq label (completing-read (TeX-argument-prompt optional prompt "Key")
58                                   (LaTeX-label-list)))))
59    (if (and definition (not (string-equal "" label)))
60        (LaTeX-add-labels label))
61    (TeX-argument-insert label optional)))
62
63(defun reftex-arg-cite (optional &optional prompt definition)
64  "Use `reftex-citation' or AUCTeX's code to insert a cite-key macro argument.
65What is being used depends upon `reftex-plug-into-AUCTeX'."
66  (let (items)
67    (cond
68     ((and (not definition) (reftex-plug-flag 3))
69      (setq items (list (or (reftex-citation t) ""))))
70     (t
71      (setq prompt (concat (if optional "(Optional) " "")
72			   (if prompt prompt "Add key")
73			   " (default none): "))
74      (setq items (multi-prompt "," t prompt (LaTeX-bibitem-list)))))
75    (apply 'LaTeX-add-bibitems items)
76    (TeX-argument-insert (mapconcat 'identity items ",") optional)))
77
78
79(defun reftex-arg-index-tag (optional &optional prompt &rest args)
80  "Prompt for an index tag with completion.
81This is the name of an index, not the entry."
82  (let (tag taglist)
83    (setq prompt (concat (if optional "(Optional) " "")
84			 (if prompt prompt "Index tag")
85			 " (default none): "))
86    (if (and reftex-support-index (reftex-plug-flag 4))
87        ;; Use RefTeX completion
88        (progn
89          (reftex-access-scan-info nil)
90          (setq taglist
91                (cdr (assoc 'index-tags
92                            (symbol-value reftex-docstruct-symbol)))
93                tag (completing-read prompt (mapcar 'list taglist))))
94      ;; Just ask like AUCTeX does.
95      (setq tag (read-string prompt)))
96    (TeX-argument-insert tag optional)))
97
98(defun reftex-arg-index (optional &optional prompt &rest args)
99  "Prompt for an index entry completing with known entries.
100Completion is specific for just one index, if the macro or a tag
101argument identify one of multiple indices."
102  (let* (tag key)
103    (if (and reftex-support-index (reftex-plug-flag 4))
104        (progn
105          (reftex-access-scan-info nil)
106          (setq tag (reftex-what-index-tag)
107                key (reftex-index-complete-key (or tag "idx"))))
108      (setq key (completing-read (TeX-argument-prompt optional prompt "Key")
109                                 (LaTeX-index-entry-list))))
110    (unless (string-equal "" key)
111      (LaTeX-add-index-entries key))
112    (TeX-argument-insert key optional)))
113
114(defun reftex-what-index-tag ()
115  ;; Look backward to find out what index the macro at point belongs to
116  (let ((macro (save-excursion
117                 (and (re-search-backward "\\\\[a-zA-Z*]+" nil t)
118                      (match-string 0))))
119        tag entry)
120    (when (and macro
121               (setq entry (assoc macro reftex-index-macro-alist)))
122      (setq tag (nth 1 entry))
123      (cond
124       ((stringp tag) tag)
125       ((integerp tag)
126        (save-excursion
127          (goto-char (match-end 1))
128          (or (reftex-nth-arg tag (nth 6 entry)) "idx")))
129       (t "idx")))))
130
131(defvar LaTeX-label-function)
132(defun reftex-plug-into-AUCTeX ()
133  ;; Replace AUCTeX functions with RefTeX functions.
134  ;; Which functions are replaced is controlled by the variable
135  ;; `reftex-plug-into-AUCTeX'.
136
137  (if (reftex-plug-flag 0)
138      (setq LaTeX-label-function 'reftex-label)
139    (setq LaTeX-label-function nil))
140
141  (and (or (reftex-plug-flag 1) (reftex-plug-flag 2))
142       (fboundp 'TeX-arg-label)
143       (fset 'TeX-arg-label 'reftex-arg-label))
144
145  (and (reftex-plug-flag 3)
146       (fboundp 'TeX-arg-cite)
147       (fset 'TeX-arg-cite 'reftex-arg-cite))
148
149  (and (reftex-plug-flag 4)
150       (fboundp 'TeX-arg-index-tag)
151       (fset 'TeX-arg-index-tag 'reftex-arg-index-tag))
152  (and (reftex-plug-flag 4)
153       (fboundp 'TeX-arg-index)
154       (fset 'TeX-arg-index 'reftex-arg-index)))
155
156(defun reftex-toggle-plug-into-AUCTeX ()
157  "Toggle Interface between AUCTeX and RefTeX on and off."
158  (interactive)
159  (unless (and (featurep 'tex-site) (featurep 'latex))
160    (error "AUCTeX's LaTeX mode does not seem to be loaded"))
161  (setq reftex-plug-into-AUCTeX (not reftex-plug-into-AUCTeX))
162  (reftex-plug-into-AUCTeX)
163  (if reftex-plug-into-AUCTeX
164      (message "RefTeX has been plugged into AUCTeX.")
165    (message "RefTeX no longer interacts with AUCTeX.")))
166
167(defun reftex-add-label-environments (entry-list)
168  "Add label environment descriptions to `reftex-label-alist-style'.
169The format of ENTRY-LIST is exactly like `reftex-label-alist'.  See there
170for details.
171This function makes it possible to support RefTeX from AUCTeX style files.
172The entries in ENTRY-LIST will be processed after the user settings in
173`reftex-label-alist', and before the defaults (specified in
174`reftex-default-label-alist-entries').  Any changes made to
175`reftex-label-alist-style' will raise a flag to the effect that
176the label information is recompiled on next use."
177  (unless reftex-docstruct-symbol
178    (reftex-tie-multifile-symbols))
179  (when (and reftex-docstruct-symbol
180             (symbolp reftex-docstruct-symbol))
181    (let ((list (get reftex-docstruct-symbol 'reftex-label-alist-style))
182          entry changed)
183      (while entry-list
184        (setq entry (pop entry-list))
185        (unless (member entry list)
186          (setq reftex-tables-dirty t
187                changed t)
188          (push entry list)))
189      (when changed
190        (put reftex-docstruct-symbol 'reftex-label-alist-style list)))))
191(defalias 'reftex-add-to-label-alist 'reftex-add-label-environments)
192
193(defun reftex-add-section-levels (entry-list)
194  "Add entries to the value of `reftex-section-levels'.
195The added values are kept local to the current document.  The format
196of ENTRY-LIST is a list of cons cells (\"MACRONAME\" . LEVEL).  See
197`reftex-section-levels' for an example."
198  (unless reftex-docstruct-symbol
199    (reftex-tie-multifile-symbols))
200  (when (and reftex-docstruct-symbol
201             (symbolp reftex-docstruct-symbol))
202    (let ((list (get reftex-docstruct-symbol 'reftex-section-levels))
203          entry changed)
204      (while entry-list
205        (setq entry (pop entry-list))
206        (unless (member entry list)
207          (setq reftex-tables-dirty t
208                changed t)
209          (push entry list)))
210      (when changed
211        (put reftex-docstruct-symbol 'reftex-section-levels list)))))
212
213(defun reftex-notice-new-section ()
214  (reftex-notice-new 1 'force))
215
216;;; arch-tag: 4a798e68-3405-421c-a09b-0269aac64ab4
217;;; reftex-auc.el ends here
218