reftex-auc.el 8.6 KB

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