reftex-auc.el 8.9 KB

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