reftex.el 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398
  1. ;;; reftex.el --- minor mode for doing \label, \ref, \cite, \index in LaTeX
  2. ;; Copyright (C) 1997-2000, 2003-2017 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <dominik@science.uva.nl>
  4. ;; Maintainer: auctex-devel@gnu.org
  5. ;; Keywords: tex
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; RefTeX is a minor mode with distinct support for \ref, \label, \cite,
  19. ;; and \index commands in (multi-file) LaTeX documents.
  20. ;; - A table of contents provides easy access to any part of a document.
  21. ;; - Labels are created semi-automatically.
  22. ;; - Definition context of labels is provided when creating a reference.
  23. ;; - Citations are simplified with efficient database lookup.
  24. ;; - Text phrases can be collected in a file, for later global indexing.
  25. ;; - The index preview buffer helps to check and edit index entries.
  26. ;;
  27. ;; There is an extensive Texinfo document describing RefTeX in detail.
  28. ;; One way to view this documentation is `M-x reftex-info RET'.
  29. ;;
  30. ;; The documentation in various formats is also available at
  31. ;;
  32. ;; http://www.gnu.org/software/auctex/manual/reftex.index.html
  33. ;;
  34. ;; RefTeX is bundled with Emacs and available as a plug-in package for
  35. ;; XEmacs 21.x. If you need to install it yourself, you can find a
  36. ;; distribution at
  37. ;;
  38. ;; http://www.gnu.org/software/auctex/reftex.html
  39. ;;
  40. ;; RefTeX was written by Carsten Dominik <dominik@science.uva.nl> with
  41. ;; contributions from Stephen Eglen. It is currently maintained by
  42. ;; the AUCTeX project.
  43. ;;; Code:
  44. (eval-when-compile (require 'cl-lib))
  45. ;; Stuff that needs to be there when we use defcustom
  46. (require 'custom)
  47. (require 'easymenu)
  48. (defvar reftex-tables-dirty t
  49. "Flag showing if tables need to be re-computed.")
  50. (eval-and-compile
  51. (defun reftex-set-dirty (symbol value)
  52. (setq reftex-tables-dirty t)
  53. (set symbol value)))
  54. ;; Configuration variables
  55. (require 'reftex-vars)
  56. ;;; Autoloads to ensure loading of support files when necessary
  57. (require 'reftex-loaddefs)
  58. ;; We autoload tons of functions from these files, but some have
  59. ;; a single function that needs to be globally autoloaded.
  60. ;; The alternative is to use a Makefile rule + distinct autoload
  61. ;; cookie (eg ;;;###reftex-autoload) for internal autoloads,
  62. ;; as eg calendar/ does. But that seemed like overkill for 4 functions.
  63. ;;;###autoload(autoload 'reftex-citation "reftex-cite" nil t)
  64. ;;;###autoload(autoload 'reftex-all-document-files "reftex-parse")
  65. ;;;###autoload(autoload 'reftex-isearch-minor-mode "reftex-global" nil t)
  66. ;;;###autoload(autoload 'reftex-index-phrases-mode "reftex-index" nil t)
  67. ;; Generated functions.
  68. (autoload 'reftex-varioref-vref "reftex-ref"
  69. "Make a varioref reference." t)
  70. (autoload 'reftex-fancyref-fref "reftex-ref"
  71. "Make a fancyref \\fref reference." t)
  72. (autoload 'reftex-fancyref-Fref "reftex-ref"
  73. "Make a fancyref \\Fref reference." t)
  74. ;;; =========================================================================
  75. ;;;
  76. ;;; Define the formal stuff for a minor mode named RefTeX.
  77. ;;;
  78. (defconst reftex-version emacs-version
  79. "Version string for RefTeX.")
  80. (defvar reftex-mode-map
  81. (let ((map (make-sparse-keymap)))
  82. ;; The default bindings in the mode map.
  83. (define-key map "\C-c=" 'reftex-toc)
  84. (define-key map "\C-c-" 'reftex-toc-recenter)
  85. (define-key map "\C-c(" 'reftex-label)
  86. (define-key map "\C-c)" 'reftex-reference)
  87. (define-key map "\C-c[" 'reftex-citation)
  88. (define-key map "\C-c<" 'reftex-index)
  89. (define-key map "\C-c>" 'reftex-display-index)
  90. (define-key map "\C-c/" 'reftex-index-selection-or-word)
  91. (define-key map "\C-c\\" 'reftex-index-phrase-selection-or-word)
  92. (define-key map "\C-c|" 'reftex-index-visit-phrases-buffer)
  93. (define-key map "\C-c&" 'reftex-view-crossref)
  94. ;; Bind `reftex-mouse-view-crossref' only when the key is still free
  95. (if (featurep 'xemacs)
  96. (unless (key-binding [(shift button2)])
  97. (define-key map [(shift button2)] 'reftex-mouse-view-crossref))
  98. (unless (key-binding [(shift mouse-2)])
  99. (define-key map [(shift mouse-2)] 'reftex-mouse-view-crossref)))
  100. ;; For most of these commands there are already bindings in place.
  101. ;; Setting `reftex-extra-bindings' really is only there to spare users
  102. ;; the hassle of defining bindings in the user space themselves. This
  103. ;; is why they violate the key binding recommendations.
  104. (when reftex-extra-bindings
  105. (define-key map "\C-ct" 'reftex-toc)
  106. (define-key map "\C-cl" 'reftex-label)
  107. (define-key map "\C-cr" 'reftex-reference)
  108. (define-key map "\C-cc" 'reftex-citation)
  109. (define-key map "\C-cv" 'reftex-view-crossref)
  110. (define-key map "\C-cg" 'reftex-grep-document)
  111. (define-key map "\C-cs" 'reftex-search-document))
  112. map)
  113. "Keymap for RefTeX mode.")
  114. (defvar reftex-mode-menu nil)
  115. (defvar reftex-syntax-table nil)
  116. (defvar reftex-syntax-table-for-bib nil)
  117. (defun reftex--prepare-syntax-tables ()
  118. (setq reftex-syntax-table (copy-syntax-table))
  119. (modify-syntax-entry ?\( "." reftex-syntax-table)
  120. (modify-syntax-entry ?\) "." reftex-syntax-table)
  121. (setq reftex-syntax-table-for-bib (copy-syntax-table))
  122. (modify-syntax-entry ?\' "." reftex-syntax-table-for-bib)
  123. (modify-syntax-entry ?\" "." reftex-syntax-table-for-bib)
  124. (modify-syntax-entry ?\[ "." reftex-syntax-table-for-bib)
  125. (modify-syntax-entry ?\] "." reftex-syntax-table-for-bib)
  126. (modify-syntax-entry ?\( "." reftex-syntax-table-for-bib)
  127. (modify-syntax-entry ?\) "." reftex-syntax-table-for-bib))
  128. (unless (and reftex-syntax-table reftex-syntax-table-for-bib)
  129. (reftex--prepare-syntax-tables))
  130. ;; The following definitions are out of place, but I need them here
  131. ;; to make the compilation of reftex-mode not complain.
  132. (defvar reftex-auto-view-crossref-timer nil
  133. "The timer used for auto-view-crossref.")
  134. (defvar reftex-toc-auto-recenter-timer nil
  135. "The idle timer used to recenter the toc window.")
  136. ;;;###autoload
  137. (defun turn-on-reftex ()
  138. "Turn on RefTeX mode."
  139. (reftex-mode t))
  140. (put 'reftex-mode :included '(memq major-mode '(latex-mode tex-mode)))
  141. (put 'reftex-mode :menu-tag "RefTeX Mode")
  142. ;;;###autoload
  143. (define-minor-mode reftex-mode
  144. "Minor mode with distinct support for \\label, \\ref and \\cite in LaTeX.
  145. \\<reftex-mode-map>A Table of Contents of the entire (multifile) document with browsing
  146. capabilities is available with `\\[reftex-toc]'.
  147. Labels can be created with `\\[reftex-label]' and referenced with `\\[reftex-reference]'.
  148. When referencing, you get a menu with all labels of a given type and
  149. context of the label definition. The selected label is inserted as a
  150. \\ref macro.
  151. Citations can be made with `\\[reftex-citation]' which will use a regular expression
  152. to pull out a *formatted* list of articles from your BibTeX
  153. database. The selected citation is inserted as a \\cite macro.
  154. Index entries can be made with `\\[reftex-index-selection-or-word]' which indexes the word at point
  155. or the current selection. More general index entries are created with
  156. `\\[reftex-index]'. `\\[reftex-display-index]' displays the compiled index.
  157. Most command have help available on the fly. This help is accessed by
  158. pressing `?' to any prompt mentioning this feature.
  159. Extensive documentation about RefTeX is available in Info format.
  160. You can view this information with `\\[reftex-info]'.
  161. \\{reftex-mode-map}
  162. Under X, these and other functions will also be available as `Ref' menu
  163. on the menu bar.
  164. ------------------------------------------------------------------------------"
  165. :lighter " Ref" :keymap reftex-mode-map
  166. (if reftex-mode
  167. (progn
  168. ;; Mode was turned on
  169. (easy-menu-add reftex-mode-menu)
  170. (and reftex-plug-into-AUCTeX
  171. (reftex-plug-into-AUCTeX))
  172. (unless (get 'reftex-auto-view-crossref 'initialized)
  173. (and reftex-auto-view-crossref
  174. (reftex-toggle-auto-view-crossref))
  175. (put 'reftex-auto-view-crossref 'initialized t))
  176. (unless (get 'reftex-auto-recenter-toc 'initialized)
  177. (and (eq reftex-auto-recenter-toc t)
  178. (reftex-toggle-auto-toc-recenter))
  179. (put 'reftex-auto-recenter-toc 'initialized t))
  180. ;; Prepare the special syntax tables.
  181. (reftex--prepare-syntax-tables)
  182. (run-hooks 'reftex-mode-hook))
  183. ;; Mode was turned off
  184. (easy-menu-remove reftex-mode-menu)))
  185. (defvar reftex-docstruct-symbol)
  186. (defun reftex-kill-buffer-hook ()
  187. "Save RefTeX's parse file for this buffer if the information has changed."
  188. ;; Save the parsing information if it was modified.
  189. ;; This function should be installed in `kill-buffer-hook'.
  190. ;; We are careful to make sure nothing goes wrong in this function.
  191. (when (and (boundp 'reftex-mode) reftex-mode
  192. (boundp 'reftex-save-parse-info) reftex-save-parse-info
  193. (boundp 'reftex-docstruct-symbol) reftex-docstruct-symbol
  194. (symbol-value reftex-docstruct-symbol)
  195. (get reftex-docstruct-symbol 'modified))
  196. ;; Write the file.
  197. (condition-case nil
  198. (reftex-access-parse-file 'write)
  199. (error nil))))
  200. (defun reftex-kill-emacs-hook ()
  201. "Call `reftex-kill-buffer-hook' on all buffers."
  202. ;; This function should be installed in `kill-emacs-hook'.
  203. (save-excursion
  204. (mapcar (lambda (buf)
  205. (set-buffer buf)
  206. (reftex-kill-buffer-hook))
  207. (buffer-list))))
  208. ;;; =========================================================================
  209. ;;;
  210. ;;; Silence warnings about variables in other packages.
  211. (defvar TeX-master)
  212. (defvar LaTeX-section-hook)
  213. (defvar LaTeX-label-function)
  214. (defvar tex-main-file)
  215. (defvar outline-minor-mode)
  216. (defvar font-lock-mode)
  217. (defvar font-lock-keywords)
  218. (defvar font-lock-fontify-region-function)
  219. ;;; =========================================================================
  220. ;;;
  221. ;;; Multibuffer Variables
  222. ;;;
  223. ;; Technical notes: These work as follows: We keep just one list
  224. ;; of labels for each master file - this can save a lot of memory.
  225. ;; `reftex-master-index-list' is an alist which connects the true file name
  226. ;; of each master file with the symbols holding the information on that
  227. ;; document. Each buffer has local variables which point to these symbols.
  228. ;; List of variables which handle the multifile stuff.
  229. ;; This list is used to tie, untie, and reset these symbols.
  230. (defconst reftex-multifile-symbols
  231. '(reftex-docstruct-symbol))
  232. ;; Alist connecting master file names with the corresponding lisp symbols.
  233. (defvar reftex-master-index-list nil)
  234. ;; Last index used for a master file.
  235. (defvar reftex-multifile-index 0)
  236. ;; Variable holding the symbol with the label list of the document.
  237. (defvar reftex-docstruct-symbol nil)
  238. (make-variable-buffer-local 'reftex-docstruct-symbol)
  239. (defun reftex-next-multifile-index ()
  240. ;; Return the next free index for multifile symbols.
  241. (cl-incf reftex-multifile-index))
  242. (defun reftex-tie-multifile-symbols ()
  243. "Tie the buffer-local symbols to globals connected with the master file.
  244. If the symbols for the current master file do not exist, they are created."
  245. (let* ((master (file-truename (reftex-TeX-master-file)))
  246. (index (assoc master reftex-master-index-list))
  247. (symlist reftex-multifile-symbols)
  248. symbol symname newflag)
  249. ;; Find the correct index.
  250. (if index
  251. ;; Symbols do exist
  252. (setq index (cdr index))
  253. ;; Get a new index and add info to the alist.
  254. (setq index (reftex-next-multifile-index)
  255. newflag t)
  256. (push (cons master index) reftex-master-index-list))
  257. ;; Get/create symbols and tie them.
  258. (while symlist
  259. (setq symbol (car symlist)
  260. symlist (cdr symlist)
  261. symname (symbol-name symbol))
  262. (set symbol (intern (concat symname "-" (int-to-string index))))
  263. (put (symbol-value symbol) :master-index index)
  264. ;; Initialize if new symbols.
  265. (when newflag
  266. (set (symbol-value symbol) nil)
  267. (put (symbol-value symbol) 'reftex-index-macros-style '(default))
  268. (put (symbol-value symbol) 'reftex-ref-style-list
  269. reftex-ref-style-default-list)))
  270. ;; Return t if the symbols did already exist, nil when we've made them.
  271. (not newflag)))
  272. (defun reftex-untie-multifile-symbols ()
  273. "Remove ties from multifile symbols, so that next use makes new ones."
  274. (let ((symlist reftex-multifile-symbols)
  275. (symbol nil))
  276. (while symlist
  277. (setq symbol (car symlist)
  278. symlist (cdr symlist))
  279. (set symbol nil))))
  280. (defun reftex-TeX-master-file ()
  281. ;; Return the name of the master file associated with the current buffer.
  282. ;; When AUCTeX is loaded, we will use it's more sophisticated method.
  283. ;; We also support the default TeX and LaTeX modes by checking for a
  284. ;; variable tex-main-file.
  285. (let
  286. ((master
  287. (cond
  288. ;; Test if we're in a subfile using the subfiles document
  289. ;; class, e.g., \documentclass[main.tex]{subfiles}. It's
  290. ;; argument is the main file, however it's not really the
  291. ;; master file in `TeX-master-file' or `tex-main-file's
  292. ;; sense. It should be used for references but not for
  293. ;; compilation, thus subfiles use a setting of
  294. ;; `TeX-master'/`tex-main-file' being themselves.
  295. ((save-excursion
  296. (goto-char (point-min))
  297. (re-search-forward
  298. "^[[:space:]]*\\\\documentclass\\[\\([^]]+\\)\\]{subfiles}"
  299. nil t))
  300. (match-string-no-properties 1))
  301. ;; AUCTeX is loaded. Use its mechanism.
  302. ((fboundp 'TeX-master-file)
  303. (condition-case nil
  304. (TeX-master-file t)
  305. (error (buffer-file-name))))
  306. ;; Emacs LaTeX mode
  307. ((fboundp 'tex-main-file) (tex-main-file))
  308. ;; Check the `TeX-master' variable.
  309. ((boundp 'TeX-master)
  310. (cond
  311. ((eq TeX-master t)
  312. (buffer-file-name))
  313. ((eq TeX-master 'shared)
  314. (setq TeX-master (read-file-name "Master file: "
  315. nil nil t nil)))
  316. (TeX-master)
  317. (t
  318. (setq TeX-master (read-file-name "Master file: "
  319. nil nil t nil)))))
  320. ;; Check the `tex-main-file' variable.
  321. ((boundp 'tex-main-file)
  322. ;; This is the variable from the default TeX modes.
  323. (cond
  324. ((stringp tex-main-file)
  325. ;; ok, this must be it
  326. tex-main-file)
  327. (t
  328. ;; In this case, the buffer is its own master.
  329. (buffer-file-name))))
  330. ;; We know nothing about master file. Assume this is a
  331. ;; master file.
  332. (t
  333. (buffer-file-name)))))
  334. (cond
  335. ((null master)
  336. (error "Need a filename for this buffer, please save it first"))
  337. ((or (file-exists-p (concat master ".tex"))
  338. (reftex-get-buffer-visiting (concat master ".tex")))
  339. ;; Ahh, an extra .tex was missing...
  340. (setq master (concat master ".tex")))
  341. ((or (file-exists-p master)
  342. (reftex-get-buffer-visiting master))
  343. ;; We either see the file, or have a buffer on it. OK.
  344. )
  345. (t
  346. ;; Use buffer file name.
  347. (setq master (buffer-file-name))))
  348. (expand-file-name master)))
  349. (defun reftex-is-multi ()
  350. ;; Tell if this is a multifile document. When not sure, say yes.
  351. (let ((entry (assq 'is-multi (symbol-value reftex-docstruct-symbol))))
  352. (if entry
  353. (nth 1 entry)
  354. t)))
  355. (defun reftex-set-cite-format (value)
  356. "Set the document-local value of `reftex-cite-format'.
  357. When such a value exists, it overwrites the setting given with
  358. `reftex-cite-format'. See the documentation of `reftex-cite-format'
  359. for possible values. This function should be used from AUCTeX style files."
  360. (unless reftex-docstruct-symbol
  361. (reftex-tie-multifile-symbols))
  362. (when (and reftex-docstruct-symbol
  363. (symbolp reftex-docstruct-symbol))
  364. (put reftex-docstruct-symbol 'reftex-cite-format value)))
  365. (defun reftex-get-cite-format ()
  366. ;; Return the current citation format. Either the document-local value in
  367. ;; reftex-cite-format-symbol, or the global value in reftex-cite-format.
  368. (if (and reftex-docstruct-symbol
  369. (symbolp reftex-docstruct-symbol)
  370. (get reftex-docstruct-symbol 'reftex-cite-format))
  371. (get reftex-docstruct-symbol 'reftex-cite-format)
  372. reftex-cite-format))
  373. (defun reftex-add-index-macros (entry-list)
  374. "Add index macro descriptions to `reftex-index-macros-style'.
  375. The format of ENTRY-LIST is exactly like `reftex-index-macros'. See there
  376. for details.
  377. This function makes it possible to support RefTeX from AUCTeX style files.
  378. The entries in ENTRY-LIST will be processed after the user settings in
  379. `reftex-index-entries', and before the defaults. Any changes made to
  380. `reftex-index-macros-style' will raise a flag to the effect that
  381. the label information is recompiled on next use."
  382. (unless reftex-docstruct-symbol
  383. (reftex-tie-multifile-symbols))
  384. (when (and reftex-docstruct-symbol
  385. (symbolp reftex-docstruct-symbol))
  386. (let ((list (get reftex-docstruct-symbol 'reftex-index-macros-style))
  387. entry changed)
  388. (while entry-list
  389. (setq entry (pop entry-list))
  390. ;; When it is a symbol, remove all other symbols
  391. (and (symbolp entry)
  392. (not (memq entry list))
  393. (setq list (reftex-remove-symbols-from-list list)))
  394. ;; Add to list unless already member
  395. (unless (member entry list)
  396. (setq reftex-tables-dirty t
  397. changed t)
  398. (push entry list)))
  399. (when changed
  400. (put reftex-docstruct-symbol 'reftex-index-macros-style list)))))
  401. (defun reftex-ref-style-activate (style)
  402. "Activate the referencing style STYLE."
  403. (reftex-ref-style-toggle style 'activate))
  404. (defun reftex-ref-style-toggle (style &optional action)
  405. "Activate or deactivate the referencing style STYLE.
  406. With the optional argument ACTION a certain action can be forced.
  407. The symbol `activate' will activate the style and `deactivate'
  408. will deactivate it."
  409. (unless reftex-docstruct-symbol
  410. (reftex-tie-multifile-symbols))
  411. (when (and reftex-docstruct-symbol
  412. (symbolp reftex-docstruct-symbol))
  413. (let ((list (get reftex-docstruct-symbol 'reftex-ref-style-list))
  414. changed)
  415. (cond ((eq action 'activate)
  416. (unless (member style list)
  417. (setq reftex-tables-dirty t
  418. changed t)
  419. (setq list (append list (list style)))))
  420. ((eq action 'deactivate)
  421. (when (member style list)
  422. (setq reftex-tables-dirty t
  423. changed t)
  424. (setq list (delete style list))))
  425. (t
  426. (if (member style list)
  427. (delete style list)
  428. (setq list (append list (list style))))
  429. (setq reftex-tables-dirty t
  430. changed t)))
  431. (when changed
  432. (put reftex-docstruct-symbol 'reftex-ref-style-list list)))))
  433. (defun reftex-ref-style-list ()
  434. "Return the list of referencing styles to be active at the moment."
  435. ;; Initialize the value of `reftex-ref-style-list' and tie it to the
  436. ;; docstruct symbol if necessary.
  437. (unless reftex-docstruct-symbol
  438. (reftex-tie-multifile-symbols))
  439. (if (and reftex-docstruct-symbol
  440. (symbolp reftex-docstruct-symbol)
  441. (get reftex-docstruct-symbol 'reftex-ref-style-list))
  442. (get reftex-docstruct-symbol 'reftex-ref-style-list)
  443. reftex-ref-style-default-list))
  444. ;;; =========================================================================
  445. ;;;
  446. ;;; Functions to compile the tables, reset the mode etc.
  447. ;; The following constants are derived from `reftex-label-alist'.
  448. ;; Prompt used for label type queries directed to the user.
  449. (defvar reftex-type-query-prompt nil)
  450. ;; Help string for label type queries.
  451. (defvar reftex-type-query-help nil)
  452. ;; Alist relating label type to reference format.
  453. (defvar reftex-typekey-to-format-alist nil)
  454. ;; Alist relating label type to label prefix.
  455. (defvar reftex-typekey-to-prefix-alist nil)
  456. ;; Alist relating environments or macros to label type and context regexp.
  457. (defvar reftex-env-or-mac-alist nil)
  458. ;; List of special environment parser functions
  459. (defvar reftex-special-env-parsers nil)
  460. ;; List of macros carrying a label.
  461. (defvar reftex-label-mac-list nil)
  462. ;; List of environments carrying a label.
  463. (defvar reftex-label-env-list nil)
  464. ;; List of all typekey letters in use.
  465. (defvar reftex-typekey-list nil)
  466. ;; Alist relating magic words to a label type.
  467. (defvar reftex-words-to-typekey-alist nil)
  468. ;; Alist relating label prefixes to a label type.
  469. (defvar reftex-prefix-to-typekey-alist nil)
  470. ;; The last list-of-labels entry used in a reference.
  471. (defvar reftex-last-used-reference (list nil nil nil nil))
  472. ;; Alist relating index macros to other info.
  473. (defvar reftex-key-to-index-macro-alist nil)
  474. ;; Prompt for index macro queries
  475. (defvar reftex-query-index-macro-prompt nil)
  476. ;; Help string for index macro queries
  477. (defvar reftex-query-index-macro-help nil)
  478. ;; The message when follow-mode is suspended
  479. (defvar reftex-no-follow-message
  480. "No follow-mode into unvisited file. Press SPC to visit it.")
  481. (defvar reftex-no-info-message
  482. "%s: info not available, use `\\[reftex-view-crossref]' to get it.")
  483. ;; Global variables used for communication between functions.
  484. (defvar reftex-default-context-position nil)
  485. (defvar reftex-location-start nil)
  486. (defvar reftex-call-back-to-this-buffer nil)
  487. (defvar reftex-select-return-marker (make-marker))
  488. (defvar reftex-active-toc nil)
  489. (defvar reftex-tex-path nil)
  490. (defvar reftex-bib-path nil)
  491. (defvar reftex-select-marked nil)
  492. (defvar reftex-last-follow-point nil)
  493. (defvar reftex-latex-syntax-table nil)
  494. (defvar reftex-prefix nil)
  495. (defvar reftex-section-levels-all nil)
  496. (defvar reftex-buffers-with-changed-invisibility nil)
  497. (defvar reftex-callback-fwd t)
  498. (defvar reftex-last-toc-master nil
  499. "Stores the name of the tex file that `reftex-toc' was last run on.")
  500. ;; Marker for return point from recursive edit
  501. (defvar reftex-recursive-edit-marker (make-marker))
  502. ;; List of buffers created temporarily for lookup, which should be killed.
  503. (defvar reftex-buffers-to-kill nil)
  504. ;; Regexp to find anything.
  505. (defvar reftex-section-regexp nil)
  506. (defvar reftex-section-or-include-regexp nil)
  507. (defvar reftex-index-macro-regexp nil)
  508. (defvar reftex-index-level-re nil)
  509. (defvar reftex-index-key-end-re nil)
  510. (defvar reftex-find-index-entry-regexp-format nil)
  511. (defvar reftex-everything-regexp nil)
  512. (defvar reftex-everything-regexp-no-index nil)
  513. (defvar reftex-index-re nil)
  514. (defvar reftex-find-citation-regexp-format
  515. "\\\\\\([a-zA-Z]*cite[*a-zA-Z]*\\*?\\|bibentry\\)\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\([^}]*,\\)?\\(%s\\)[},]")
  516. (defvar reftex-find-reference-format
  517. "\\\\\\(ref[a-zA-Z]*\\|[a-zA-Z]*ref\\(range\\)?\\)\\*?\\(\\[[^]]*\\]\\|{[^}]*}\\)*{\\(%s\\)}")
  518. (defvar reftex-macros-with-labels nil)
  519. (defvar reftex-macros-with-index nil)
  520. (defvar reftex-index-macro-alist nil)
  521. (defvar reftex-find-label-regexp-format nil)
  522. (defvar reftex-find-label-regexp-format2 nil)
  523. ;; Constants for making RefTeX open to Texinfo hooking
  524. (defvar reftex-section-pre-regexp "\\\\")
  525. ;; Including `\' as a character to be matched at the end of the regexp
  526. ;; will allow stuff like \begin{foo}\label{bar} to be matched. This
  527. ;; will make the parser to advance one char too much. Therefore
  528. ;; `reftex-parse-from-file' will step one char back if a section is
  529. ;; found.
  530. (defvar reftex-section-post-regexp "\\*?\\(\\[[^]]*\\]\\)?[[{ \t\r\n\\]")
  531. (defvar reftex-section-info-function 'reftex-section-info)
  532. (defvar reftex-memory nil
  533. "Memorizes old variable values to indicate changes in these variables.")
  534. ;; A list of all variables in the cache.
  535. ;; The cache is used to save the compiled versions of some variables.
  536. (defconst reftex-cache-variables
  537. '(reftex-memory ;; This MUST ALWAYS be the first!
  538. ;; Outline
  539. reftex-section-levels-all
  540. ;; Labels
  541. reftex-env-or-mac-alist
  542. reftex-special-env-parsers
  543. reftex-macros-with-labels
  544. reftex-label-mac-list
  545. reftex-label-env-list
  546. reftex-typekey-list
  547. reftex-typekey-to-format-alist
  548. reftex-typekey-to-prefix-alist
  549. reftex-words-to-typekey-alist
  550. reftex-prefix-to-typekey-alist
  551. reftex-type-query-prompt
  552. reftex-type-query-help
  553. ;; Index
  554. reftex-index-macro-alist
  555. reftex-macros-with-index
  556. reftex-query-index-macro-prompt
  557. reftex-query-index-macro-help
  558. reftex-key-to-index-macro-alist
  559. ;; Regular expressions
  560. reftex-section-regexp
  561. reftex-section-or-include-regexp
  562. reftex-index-re
  563. reftex-everything-regexp
  564. reftex-everything-regexp-no-index
  565. reftex-find-label-regexp-format
  566. reftex-find-label-regexp-format2
  567. reftex-find-index-entry-regexp-format
  568. ))
  569. (defun reftex-ensure-compiled-variables ()
  570. ;; Recompile the label alist when necessary
  571. (let* ((mem reftex-memory)
  572. (cache (get reftex-docstruct-symbol 'reftex-cache))
  573. (cmem (car cache))
  574. (alist reftex-label-alist)
  575. (levels (get reftex-docstruct-symbol 'reftex-section-levels))
  576. (style (get reftex-docstruct-symbol 'reftex-label-alist-style))
  577. (default reftex-default-label-alist-entries)
  578. (index reftex-index-macros)
  579. (istyle (get reftex-docstruct-symbol 'reftex-index-macros-style)))
  580. (cond
  581. (reftex-tables-dirty (reftex-compile-variables))
  582. ((and (eq alist (nth 0 mem))
  583. (eq levels (nth 1 mem))
  584. (eq style (nth 2 mem))
  585. (eq default (nth 3 mem))
  586. (eq index (nth 4 mem))
  587. (eq istyle (nth 5 mem)))) ;; everything is OK
  588. ((and (eq alist (nth 0 cmem))
  589. (eq levels (nth 1 cmem))
  590. (eq style (nth 2 cmem))
  591. (eq default (nth 2 cmem))
  592. (eq index (nth 4 cmem))
  593. (eq istyle (nth 5 cmem)))
  594. ;; restore the cache
  595. (message "Restoring cache")
  596. (mapcar (lambda (sym) (set sym (pop cache))) reftex-cache-variables))
  597. (t (reftex-compile-variables)))))
  598. (defun reftex-reset-mode ()
  599. "Reset RefTeX Mode.
  600. This will re-compile the configuration information and remove all
  601. current scanning information and the parse file to enforce a rescan
  602. on next use."
  603. (interactive)
  604. ;; Reset the file search path variables
  605. (dolist (prop '(status master-dir recursive-path rec-type))
  606. (put 'reftex-tex-path prop nil)
  607. (put 'reftex-bib-path prop nil))
  608. ;; Kill temporary buffers associated with RefTeX - just in case they
  609. ;; were not cleaned up properly
  610. (save-excursion
  611. (let ((buffer-list '("*RefTeX Help*" "*RefTeX Select*"
  612. "*Duplicate Labels*" "*toc*" " *RefTeX-scratch*"))
  613. buf)
  614. (while (setq buf (pop buffer-list))
  615. (if (get-buffer buf)
  616. (kill-buffer buf))))
  617. (reftex-erase-all-selection-and-index-buffers))
  618. ;; Make sure the current document will be rescanned soon.
  619. (reftex-reset-scanning-information)
  620. ;; Remove any parse info file
  621. (reftex-access-parse-file 'kill)
  622. ;; Plug functions into AUCTeX if the user option says so.
  623. (and reftex-plug-into-AUCTeX
  624. (reftex-plug-into-AUCTeX))
  625. (reftex-compile-variables))
  626. ;;;###autoload
  627. (defun reftex-reset-scanning-information ()
  628. "Reset the symbols containing information from buffer scanning.
  629. This enforces rescanning the buffer on next use."
  630. (if (string= reftex-last-toc-master (reftex-TeX-master-file))
  631. (reftex-erase-buffer "*toc*"))
  632. (let ((symlist reftex-multifile-symbols)
  633. symbol)
  634. (while symlist
  635. (setq symbol (car symlist)
  636. symlist (cdr symlist))
  637. (if (and (symbolp (symbol-value symbol))
  638. (not (null (symbol-value symbol))))
  639. (set (symbol-value symbol) nil)))))
  640. (defun reftex-erase-all-selection-and-index-buffers ()
  641. ;; Remove all selection buffers associated with current document.
  642. (mapc
  643. (lambda (type)
  644. (reftex-erase-buffer (reftex-make-selection-buffer-name type)))
  645. reftex-typekey-list)
  646. ;; Kill all index buffers
  647. (mapc
  648. (lambda (tag)
  649. (reftex-kill-buffer (reftex-make-index-buffer-name tag)))
  650. (cdr (assoc 'index-tags (symbol-value reftex-docstruct-symbol)))))
  651. (defun reftex-compile-variables ()
  652. ;; Compile the information in reftex-label-alist & Co.
  653. (message "Compiling label environment definitions...")
  654. ;; Update AUCTeX style information
  655. (when (and (featurep 'tex-site) (fboundp 'TeX-update-style))
  656. (condition-case nil (TeX-update-style) (error nil)))
  657. ;; Record that we have done this, and what we have used.
  658. (setq reftex-tables-dirty nil)
  659. (setq reftex-memory
  660. (list reftex-label-alist
  661. (get reftex-docstruct-symbol 'reftex-section-levels)
  662. (get reftex-docstruct-symbol 'reftex-label-alist-style)
  663. reftex-default-label-alist-entries
  664. reftex-index-macros
  665. (get reftex-docstruct-symbol 'reftex-index-macros-style)))
  666. ;; Compile information in reftex-label-alist
  667. (let ((all (reftex-uniquify-by-car
  668. (reftex-splice-symbols-into-list
  669. (append reftex-label-alist
  670. (get reftex-docstruct-symbol
  671. 'reftex-label-alist-style)
  672. reftex-default-label-alist-entries)
  673. reftex-label-alist-builtin)
  674. '(nil)))
  675. (all-index (reftex-uniquify-by-car
  676. (reftex-splice-symbols-into-list
  677. (append reftex-index-macros
  678. (get reftex-docstruct-symbol
  679. 'reftex-index-macros-style)
  680. '(default))
  681. reftex-index-macros-builtin)))
  682. entry env-or-mac typekeychar typekey prefix context word
  683. fmt reffmt labelfmt wordlist qh-list macros-with-labels
  684. nargs nlabel opt-args cell sum i
  685. macro verify repeat nindex tag key toc-level toc-levels)
  686. (setq reftex-words-to-typekey-alist nil
  687. reftex-prefix-to-typekey-alist
  688. '(("sec:" . "s") ("cha:" . "s") ("chap:" . "s"))
  689. reftex-typekey-list nil
  690. reftex-typekey-to-format-alist nil
  691. reftex-typekey-to-prefix-alist nil
  692. reftex-env-or-mac-alist nil
  693. reftex-label-env-list nil
  694. reftex-label-mac-list nil)
  695. (while all
  696. (catch 'next-entry
  697. (setq entry (car all)
  698. env-or-mac (car entry)
  699. entry (cdr entry)
  700. all (cdr all))
  701. (if (null env-or-mac)
  702. (setq env-or-mac ""))
  703. (if (stringp (car entry))
  704. ;; This is before version 2.00 - convert entry to new format
  705. ;; This is just to keep old users happy
  706. (setq entry (cons (string-to-char (car entry))
  707. (cons (concat (car entry) ":")
  708. (cdr entry)))))
  709. (setq typekeychar (nth 0 entry)
  710. typekey (if typekeychar (char-to-string typekeychar) nil)
  711. prefix (nth 1 entry)
  712. fmt (nth 2 entry)
  713. context (nth 3 entry)
  714. wordlist (nth 4 entry)
  715. toc-level (nth 5 entry))
  716. (if (stringp wordlist)
  717. ;; This is before version 2.04 - convert to new format
  718. (setq wordlist (nthcdr 4 entry)))
  719. (if (and (stringp fmt)
  720. (string-match "@" fmt))
  721. ;; Special syntax for specifying a label format
  722. (setq fmt (split-string fmt "@+"))
  723. (setq fmt (list "\\label{%s}" fmt)))
  724. (setq labelfmt (car fmt)
  725. reffmt (nth 1 fmt))
  726. ;; Note a new typekey
  727. (if typekey
  728. (cl-pushnew typekey reftex-typekey-list :test #'equal))
  729. (if (and typekey prefix
  730. (not (assoc prefix reftex-prefix-to-typekey-alist)))
  731. (cl-pushnew (cons prefix typekey) reftex-prefix-to-typekey-alist
  732. :test #'equal))
  733. (if (and typekey prefix
  734. (not (assoc typekey reftex-typekey-to-prefix-alist)))
  735. (cl-pushnew (cons typekey prefix) reftex-typekey-to-prefix-alist
  736. :test #'equal))
  737. ;; Check if this is a macro or environment
  738. (cond
  739. ((symbolp env-or-mac)
  740. ;; A special parser function
  741. (unless (fboundp env-or-mac)
  742. (message "Warning: %s does not seem to be a valid function"
  743. env-or-mac))
  744. (setq nargs nil nlabel nil opt-args nil)
  745. (cl-pushnew env-or-mac reftex-special-env-parsers)
  746. (setq env-or-mac (symbol-name env-or-mac)))
  747. ((string-match "\\`\\\\" env-or-mac)
  748. ;; It's a macro
  749. (let ((result (reftex-parse-args env-or-mac)))
  750. (setq env-or-mac (or (cl-first result) env-or-mac)
  751. nargs (cl-second result)
  752. nlabel (cl-third result)
  753. opt-args (cl-fourth result))
  754. (if nlabel (cl-pushnew env-or-mac macros-with-labels :test #'equal)))
  755. (if typekey (cl-pushnew env-or-mac reftex-label-mac-list :test #'equal)))
  756. (t
  757. ;; It's an environment
  758. (setq nargs nil nlabel nil opt-args nil)
  759. (cond ((string= env-or-mac "any"))
  760. ((string= env-or-mac ""))
  761. ((string= env-or-mac "section"))
  762. (t
  763. (cl-pushnew env-or-mac reftex-label-env-list :test #'equal)
  764. (if toc-level
  765. (let ((string (format "begin{%s}" env-or-mac)))
  766. (or (assoc string toc-levels)
  767. (push (cons string toc-level) toc-levels))))))))
  768. ;; Translate some special context cases
  769. (when (assq context reftex-default-context-regexps)
  770. (setq context
  771. (format
  772. (cdr (assq context reftex-default-context-regexps))
  773. (regexp-quote env-or-mac))))
  774. ;; See if this is the first format for this typekey
  775. (and reffmt
  776. (not (assoc typekey reftex-typekey-to-format-alist))
  777. (push (cons typekey reffmt) reftex-typekey-to-format-alist))
  778. ;; See if this is the first definition for this env-or-mac
  779. (and (not (string= env-or-mac "any"))
  780. (not (string= env-or-mac ""))
  781. (not (assoc env-or-mac reftex-env-or-mac-alist))
  782. (push (list env-or-mac typekey context labelfmt
  783. nargs nlabel opt-args)
  784. reftex-env-or-mac-alist))
  785. ;; Are the magic words regular expressions? Quote normal words.
  786. (if (eq (car wordlist) 'regexp)
  787. (setq wordlist (cdr wordlist))
  788. (setq wordlist (mapcar 'regexp-quote wordlist)))
  789. ;; Remember the first association of each word.
  790. (while (stringp (setq word (pop wordlist)))
  791. (or (assoc word reftex-words-to-typekey-alist)
  792. (push (cons word typekey) reftex-words-to-typekey-alist)))
  793. (cond
  794. ((string= "" env-or-mac) nil)
  795. ((setq cell (assoc typekey qh-list))
  796. (push env-or-mac (cdr cell)))
  797. (typekey
  798. (push (list typekey env-or-mac) qh-list)))))
  799. (setq reftex-typekey-to-prefix-alist
  800. (nreverse reftex-typekey-to-prefix-alist))
  801. ;; Prepare the typekey query prompt and help string.
  802. (setq qh-list
  803. (sort qh-list
  804. (lambda (x1 x2)
  805. (string< (downcase (car x1)) (downcase (car x2))))))
  806. (setq reftex-type-query-prompt
  807. (concat "Label type: ["
  808. (mapconcat (lambda(x) (format "%s" (car x)))
  809. qh-list "")
  810. "]"))
  811. ;; In the help string, we need to wrap lines...
  812. (setq reftex-type-query-help
  813. (concat
  814. "SELECT A LABEL TYPE:\n--------------------\n"
  815. (mapconcat
  816. (lambda(x)
  817. (setq sum 0)
  818. (format " [%s] %s"
  819. (car x)
  820. (mapconcat (lambda(env)
  821. (setq sum (+ sum (length env)))
  822. (if (< sum 60)
  823. env
  824. (setq sum 0)
  825. (concat "\n " env)))
  826. (cdr x) " ")))
  827. qh-list "\n")))
  828. ;; Convert magic words to regular expressions. We make regular expressions
  829. ;; which allow for some chars from the ref format to be in the buffer.
  830. ;; These characters will be seen and removed.
  831. (setq reftex-words-to-typekey-alist
  832. (mapcar
  833. (lambda (x)
  834. (setq word (car x)
  835. typekey (cdr x)
  836. fmt (cdr (assoc typekey reftex-typekey-to-format-alist)))
  837. (setq word (concat "\\W\\(" word "[ \t\n\r]*\\)\\("))
  838. (setq i 0)
  839. (while (and (< i 10) ; maximum number of format chars allowed
  840. (< i (length fmt))
  841. (not (member (aref fmt i) '(?%))))
  842. (setq word (concat word "\\|" (regexp-quote
  843. (substring fmt 0 (1+ i)))))
  844. (cl-incf i))
  845. (cons (concat word "\\)\\=") typekey))
  846. (nreverse reftex-words-to-typekey-alist)))
  847. ;; Parse the index macros
  848. (setq reftex-index-macro-alist nil
  849. reftex-key-to-index-macro-alist nil
  850. reftex-macros-with-index nil)
  851. (while all-index
  852. (setq entry (car all-index)
  853. macro (car entry)
  854. tag (nth 1 entry)
  855. key (nth 2 entry)
  856. prefix (or (nth 3 entry) "")
  857. verify (nth 4 entry)
  858. ;; For repeat, we need to be compatible with older code
  859. ;; This information used to be given only for the default macro,
  860. ;; but later we required to have it for *every* index macro
  861. repeat (cond ((> (length entry) 5) (nth 5 entry))
  862. ((and (eq key (car reftex-index-default-macro))
  863. (> (length reftex-index-default-macro) 2))
  864. ;; User has old setting - respect it
  865. (nth 2 reftex-index-default-macro))
  866. (t t))
  867. all-index (cdr all-index))
  868. (let ((result (reftex-parse-args macro)))
  869. (setq macro (or (cl-first result) macro)
  870. nargs (cl-second result)
  871. nindex (cl-third result)
  872. opt-args (cl-fourth result))
  873. (unless (member macro reftex-macros-with-index)
  874. ;; 0 1 2 3 4 5 6 7
  875. (push (list macro tag prefix verify nargs nindex opt-args repeat)
  876. reftex-index-macro-alist)
  877. (or (assoc key reftex-key-to-index-macro-alist)
  878. (push (list key macro) reftex-key-to-index-macro-alist))
  879. (push macro reftex-macros-with-index))))
  880. ;; Make the prompt and help string for index macros query
  881. (setq reftex-key-to-index-macro-alist
  882. (sort reftex-key-to-index-macro-alist
  883. (lambda (a b) (< (downcase (car a)) (downcase (car b))))))
  884. (setq reftex-query-index-macro-prompt
  885. (concat "Index macro: ["
  886. (mapconcat (lambda (x) (char-to-string (car x)))
  887. reftex-key-to-index-macro-alist "")
  888. "]"))
  889. (setq i 0
  890. reftex-query-index-macro-help
  891. (concat
  892. "SELECT A MACRO:\n---------------\n"
  893. (mapconcat
  894. (lambda(x)
  895. (format "[%c] %-20.20s%s" (car x) (nth 1 x)
  896. (if (= 0 (mod (cl-incf i) 3)) "\n" "")))
  897. reftex-key-to-index-macro-alist "")))
  898. ;; Make the full list of section levels
  899. (setq reftex-section-levels-all
  900. (append toc-levels
  901. (get reftex-docstruct-symbol 'reftex-section-levels)
  902. reftex-section-levels))
  903. ;; Calculate the regular expressions
  904. (let* (
  905. ; (wbol "\\(\\`\\|[\n\r]\\)[ \t]*")
  906. (wbol "\\(^\\)%?[ \t]*") ; Need to keep the empty group because
  907. ; match numbers are hard coded
  908. (label-re (concat "\\(?:"
  909. (mapconcat 'identity reftex-label-regexps "\\|")
  910. "\\)"))
  911. (include-re (concat wbol
  912. "\\\\\\("
  913. (mapconcat 'identity
  914. reftex-include-file-commands "\\|")
  915. "\\)[{ \t]+\\([^} \t\n\r]+\\)"))
  916. (section-re
  917. (concat wbol reftex-section-pre-regexp "\\("
  918. (mapconcat (lambda (x) (regexp-quote (car x)))
  919. reftex-section-levels-all "\\|")
  920. "\\)" reftex-section-post-regexp))
  921. (appendix-re (concat wbol "\\(\\\\appendix\\)"))
  922. (macro-re
  923. (if macros-with-labels
  924. (concat "\\("
  925. (mapconcat 'regexp-quote macros-with-labels "\\|")
  926. "\\)[[{]")
  927. ""))
  928. (index-re
  929. (concat "\\("
  930. (mapconcat 'regexp-quote reftex-macros-with-index "\\|")
  931. "\\)[[{]"))
  932. (find-index-re-format
  933. (concat "\\("
  934. (mapconcat 'regexp-quote reftex-macros-with-index "\\|")
  935. "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]"))
  936. (find-label-re-format
  937. (concat "\\("
  938. "label[[:space:]]*=[[:space:]]*"
  939. "\\|"
  940. (mapconcat 'regexp-quote (append '("\\label")
  941. macros-with-labels) "\\|")
  942. "\\)\\([[{][^]}]*[]}]\\)*[[{]\\(%s\\)[]}]"))
  943. (index-level-re
  944. (regexp-quote (nth 0 reftex-index-special-chars)))
  945. (index-key-end-re ;; ^]- not allowed
  946. (concat "[^" (nth 3 reftex-index-special-chars) "]"
  947. "[" (nth 1 reftex-index-special-chars)
  948. (nth 2 reftex-index-special-chars) "]"))
  949. )
  950. (setq reftex-section-regexp section-re
  951. reftex-section-or-include-regexp
  952. (concat section-re "\\|" include-re)
  953. reftex-everything-regexp
  954. (concat label-re "\\|" section-re "\\|" include-re
  955. "\\|" appendix-re
  956. "\\|" index-re
  957. (if macros-with-labels "\\|" "") macro-re)
  958. reftex-everything-regexp-no-index
  959. (concat label-re "\\|" section-re "\\|" include-re
  960. "\\|" appendix-re
  961. "\\|" "\\(\\\\6\\\\3\\\\1\\)" ; This is unlikely to match
  962. (if macros-with-labels "\\|" "") macro-re)
  963. reftex-index-re index-re
  964. reftex-index-level-re index-level-re
  965. reftex-index-key-end-re index-key-end-re
  966. reftex-macros-with-labels macros-with-labels
  967. reftex-find-index-entry-regexp-format find-index-re-format
  968. reftex-find-label-regexp-format find-label-re-format
  969. reftex-find-label-regexp-format2
  970. "\\([]} \t\n\r]\\)\\([[{]\\)\\(%s\\)[]}]")
  971. (message "Compiling label environment definitions...done")))
  972. (put reftex-docstruct-symbol 'reftex-cache
  973. (mapcar 'symbol-value reftex-cache-variables)))
  974. (defun reftex-parse-args (macro)
  975. ;; Return a list of macro name, nargs, arg-nr which is label and a list of
  976. ;; optional argument indices.
  977. (if (string-match "[[{]\\*?[]}]" macro)
  978. (progn
  979. (let ((must-match (substring macro 0 (match-beginning 0)))
  980. (args (substring macro (match-beginning 0)))
  981. opt-list nlabel (cnt 0))
  982. (while (string-match "\\`[[{]\\(\\*\\)?[]}]" args)
  983. (cl-incf cnt)
  984. (when (eq ?\[ (string-to-char args))
  985. (push cnt opt-list))
  986. (when (and (match-end 1)
  987. (not nlabel))
  988. (setq nlabel cnt))
  989. (setq args (substring args (match-end 0))))
  990. (list must-match cnt nlabel opt-list)))
  991. nil))
  992. ;;; =========================================================================
  993. ;;;
  994. ;;; Accessing the parse information
  995. (defun reftex-access-scan-info (&optional rescan file)
  996. "Ensure access to the scanning info for the current file."
  997. ;; When the multifile symbols are not yet tied,
  998. ;; tie them. When they are empty or RESCAN is non-nil, scan the document.
  999. ;; But, when RESCAN is -1, don't rescan even if docstruct is empty.
  1000. ;; When FILE is non-nil, parse only from that file.
  1001. ;; Error out in a buffer without a file.
  1002. (if (and reftex-mode
  1003. (not (buffer-file-name)))
  1004. (error "RefTeX works only in buffers visiting a file"))
  1005. ;; Make sure we have the symbols tied
  1006. (if (eq reftex-docstruct-symbol nil)
  1007. ;; Symbols are not yet tied: Tie them.
  1008. (reftex-tie-multifile-symbols))
  1009. (reftex-ensure-compiled-variables)
  1010. (when (or (null (symbol-value reftex-docstruct-symbol))
  1011. (member rescan '(t 1 (4) (16))))
  1012. ;; The docstruct will change: Remove selection buffers.
  1013. (save-excursion
  1014. (reftex-erase-buffer "*toc*")
  1015. (reftex-erase-all-selection-and-index-buffers)))
  1016. (if (and (null (symbol-value reftex-docstruct-symbol))
  1017. (not (member rescan '(t 1 (4) (16))))
  1018. reftex-save-parse-info)
  1019. ;; Try to read the stuff from a file
  1020. (reftex-access-parse-file 'read))
  1021. (cond
  1022. ((equal rescan -1)) ;; We are not allowed to scan.
  1023. ((not (symbol-value reftex-docstruct-symbol))
  1024. ;; Scan the whole document
  1025. (reftex-do-parse 1 file))
  1026. ((member rescan '(t 1 (4) (16)))
  1027. ;; Scan whatever was required by the caller.
  1028. (reftex-do-parse rescan file))))
  1029. (defun reftex-scanning-info-available-p ()
  1030. "Is the scanning info about the current document available?"
  1031. (unless reftex-docstruct-symbol
  1032. (reftex-tie-multifile-symbols))
  1033. (and (symbolp reftex-docstruct-symbol)
  1034. (symbol-value reftex-docstruct-symbol)
  1035. t))
  1036. (defun reftex-silence-toc-markers (list n)
  1037. ;; Set all toc markers in the first N entries in list to nil
  1038. (while (and list (> (cl-decf n) -1))
  1039. (and (eq (car (car list)) 'toc)
  1040. (markerp (nth 4 (car list)))
  1041. (set-marker (nth 4 (car list)) nil))
  1042. (pop list)))
  1043. (defun reftex-access-parse-file (action)
  1044. "Perform ACTION on the parse file (the .rel file).
  1045. Valid actions are: readable, restore, read, kill, write."
  1046. (let* ((list (symbol-value reftex-docstruct-symbol))
  1047. (docstruct-symbol reftex-docstruct-symbol)
  1048. (master (reftex-TeX-master-file))
  1049. (enable-local-variables nil)
  1050. (file (if (string-match "\\.[a-zA-Z]+\\'" master)
  1051. (concat (substring master 0 (match-beginning 0))
  1052. reftex-parse-file-extension)
  1053. (concat master reftex-parse-file-extension))))
  1054. (cond
  1055. ((eq action 'readable)
  1056. (file-readable-p file))
  1057. ((eq action 'restore)
  1058. (put reftex-docstruct-symbol 'modified nil)
  1059. (if (eq reftex-docstruct-symbol nil)
  1060. ;; Symbols are not yet tied: Tie them.
  1061. (reftex-tie-multifile-symbols))
  1062. (if (file-exists-p file)
  1063. ;; load the file and return t for success
  1064. (condition-case nil
  1065. (progn (load-file file) t)
  1066. (error (set reftex-docstruct-symbol nil)
  1067. (error "Error while loading file %s" file)))
  1068. ;; Throw an exception if the file does not exist
  1069. (error "No restore file %s" file)))
  1070. ((eq action 'read)
  1071. (put reftex-docstruct-symbol 'modified nil)
  1072. (if (file-exists-p file)
  1073. ;; load the file and return t for success
  1074. (condition-case nil
  1075. (progn
  1076. (load-file file)
  1077. (reftex-check-parse-consistency)
  1078. t)
  1079. (error (message "Error while restoring file %s" file)
  1080. (set reftex-docstruct-symbol nil)
  1081. nil))
  1082. ;; return nil for failure, but no exception
  1083. nil))
  1084. ((eq action 'kill)
  1085. ;; Remove the file
  1086. (when (and (file-exists-p file) (file-writable-p file))
  1087. (message "Unlinking file %s" file)
  1088. (delete-file file)))
  1089. (t
  1090. (put docstruct-symbol 'modified nil)
  1091. (save-excursion
  1092. (if (file-writable-p file)
  1093. (with-temp-file file
  1094. (message "Writing parse file %s" (abbreviate-file-name file))
  1095. (insert (format ";; RefTeX parse info file\n"))
  1096. (insert (format ";; File: %s\n" master))
  1097. (insert (format ";; User: %s (%s)\n\n"
  1098. (user-login-name) (user-full-name)))
  1099. (insert "(set reftex-docstruct-symbol '(\n\n")
  1100. (let ((standard-output (current-buffer)))
  1101. (mapc
  1102. (lambda (x)
  1103. (cond ((eq (car x) 'toc)
  1104. ;; A toc entry. Do not save the marker.
  1105. ;; Save the markers position at position 8
  1106. (print (list 'toc "toc" (nth 2 x) (nth 3 x)
  1107. nil (nth 5 x) (nth 6 x) (nth 7 x)
  1108. (or (and (markerp (nth 4 x))
  1109. (marker-position (nth 4 x)))
  1110. (nth 8 x)))))
  1111. ((and (not (eq t reftex-support-index))
  1112. (eq (car x) 'index))
  1113. ;; Don't save index entries
  1114. )
  1115. (t (print x))))
  1116. list))
  1117. (insert "))\n\n"))
  1118. (error "Cannot write to file %s" file)))
  1119. t))))
  1120. (defun reftex-check-parse-consistency ()
  1121. ;; Check if parse file is consistent, throw an error if not.
  1122. ;; Check if the master is the same: when moving a document, this will see it.
  1123. (let* ((real-master (reftex-TeX-master-file))
  1124. (parsed-master
  1125. (nth 1 (assq 'bof (symbol-value reftex-docstruct-symbol)))))
  1126. (unless (string= (file-truename real-master) (file-truename parsed-master))
  1127. (message "Master file name in load file is different: %s versus %s"
  1128. parsed-master real-master)
  1129. (error "Master file name error")))
  1130. ;; Check for the existence of all document files
  1131. ;;; (let* ((all (symbol-value reftex-docstruct-symbol)))
  1132. ;;; (while all
  1133. ;;; (when (and (eq (car (car all)) 'bof)
  1134. ;;; (not (file-regular-p (nth 1 (car all)))))
  1135. ;;; (message "File %s in saved parse info not available" (cdr (car all)))
  1136. ;;; (error "File not found"))
  1137. ;;; (setq all (cdr all))))
  1138. )
  1139. (defun reftex-select-external-document (xr-alist xr-index)
  1140. ;; Return index of an external document.
  1141. (let* ((len (length xr-alist)) (highest (1- (+ ?0 len)))
  1142. (prompt (format "[%c-%c] Select TAB: Read prefix with completion"
  1143. ?0 highest))
  1144. key prefix)
  1145. (cond
  1146. ((= len 1)
  1147. (message "No external documents available")
  1148. (ding) (sit-for 1) 0)
  1149. ((= len 2)
  1150. (- 1 xr-index))
  1151. (t
  1152. (save-excursion
  1153. (let* ((length (apply 'max (mapcar
  1154. (lambda(x) (length (car x))) xr-alist)))
  1155. (fmt (format " [%%c] %%-%ds %%s\n" length))
  1156. (n (1- ?0)))
  1157. (setq key
  1158. (reftex-select-with-char
  1159. prompt
  1160. (concat
  1161. "SELECT EXTERNAL DOCUMENT\n------------------------\n"
  1162. (mapconcat
  1163. (lambda (x)
  1164. (format fmt (cl-incf n) (or (car x) "")
  1165. (abbreviate-file-name (cdr x))))
  1166. xr-alist ""))
  1167. nil t))
  1168. (cond
  1169. ((and (>= key ?0) (<= key highest)) (- key ?0))
  1170. ((= key ?\C-i)
  1171. (setq prefix (completing-read "Prefix: " xr-alist nil t))
  1172. (- len (length (memq (assoc prefix xr-alist) xr-alist))))
  1173. (t (error "Invalid document selection [%c]" key)))))))))
  1174. ;;; =========================================================================
  1175. ;;;
  1176. ;;; Finding files
  1177. (defun reftex-locate-file (file type master-dir &optional die)
  1178. "Find FILE of type TYPE in MASTER-DIR or on the path associated with TYPE.
  1179. If the file does not have any of the valid extensions for TYPE,
  1180. try first the default extension and only then the naked file name.
  1181. When DIE is non-nil, throw an error if file not found."
  1182. (let* ((rec-values (if reftex-search-unrecursed-path-first '(nil t) '(t)))
  1183. (extensions (cdr (assoc type reftex-file-extensions)))
  1184. (def-ext (car extensions))
  1185. (ext-re (concat "\\("
  1186. (mapconcat 'regexp-quote extensions "\\|")
  1187. "\\)\\'"))
  1188. (files (if (string-match ext-re file)
  1189. (cons file nil)
  1190. (if reftex-try-all-extensions
  1191. (append (mapcar (lambda (x) (concat file x))
  1192. extensions)
  1193. (list file))
  1194. (list (concat file def-ext) file))))
  1195. path old-path file1 f fs)
  1196. (cond
  1197. ((file-name-absolute-p file)
  1198. (while (setq f (pop files))
  1199. (if (file-regular-p f)
  1200. (setq file1 f files nil))))
  1201. ((and reftex-use-external-file-finders
  1202. (assoc type reftex-external-file-finders))
  1203. (setq file1 (reftex-find-file-externally file type master-dir)))
  1204. (t
  1205. (while (and (null file1) rec-values)
  1206. (setq path (reftex-access-search-path
  1207. type (pop rec-values) master-dir file))
  1208. (setq fs files)
  1209. (while (and (null file1) (setq f (pop fs)))
  1210. (when (or (null old-path)
  1211. (not (eq old-path path)))
  1212. (setq old-path path
  1213. path (cons master-dir path))
  1214. (setq file1 (reftex-find-file-on-path f path master-dir)))))))
  1215. (cond (file1 file1)
  1216. (die (error "No such file: %s" file) nil)
  1217. (t (message "No such file: %s (ignored)" file) nil))))
  1218. (defun reftex-find-file-externally (file type &optional master-dir)
  1219. ;; Use external program to find FILE.
  1220. ;; The program is taken from `reftex-external-file-finders'.
  1221. ;; Interpret relative path definitions starting from MASTER-DIR.
  1222. (let ((default-directory (or master-dir default-directory))
  1223. (prg (cdr (assoc type reftex-external-file-finders)))
  1224. out)
  1225. (if (string-match "%f" prg)
  1226. (setq prg (replace-match file t t prg)))
  1227. (setq out (apply 'reftex-process-string (split-string prg)))
  1228. (if (string-match "[ \t\n]+\\'" out) ; chomp
  1229. (setq out (replace-match "" nil nil out)))
  1230. (cond ((equal out "") nil)
  1231. ((file-regular-p out) (expand-file-name out master-dir))
  1232. (t nil))))
  1233. (defun reftex-process-string (program &rest args)
  1234. "Execute PROGRAM with arguments ARGS and return its STDOUT as a string."
  1235. (let ((calling-dir default-directory)) ; remember default directory
  1236. (with-output-to-string
  1237. (with-current-buffer standard-output
  1238. (let ((default-directory calling-dir)) ; set default directory
  1239. (apply 'call-process program nil '(t nil) nil args))))))
  1240. (defun reftex-access-search-path (type &optional recurse master-dir file)
  1241. ;; Access path from environment variables. TYPE is either "tex" or "bib".
  1242. ;; When RECURSE is t, expand path elements ending in `//' recursively.
  1243. ;; Relative path elements are left as they are. However, relative recursive
  1244. ;; elements are expanded with MASTER-DIR as default directory.
  1245. ;; The expanded path is cached for the next search.
  1246. ;; FILE is just for the progress message.
  1247. ;; Returns the derived path.
  1248. (let* ((pathvar (intern (concat "reftex-" type "-path"))))
  1249. (when (null (get pathvar 'status))
  1250. ;; Get basic path
  1251. (set pathvar
  1252. (reftex-uniquify
  1253. (reftex-parse-colon-path
  1254. (mapconcat
  1255. (lambda(x)
  1256. (if (string-match "^!" x)
  1257. (apply 'reftex-process-string
  1258. (split-string (substring x 1)))
  1259. (or (getenv x) x)))
  1260. ;; For consistency, the next line should look like this:
  1261. ;; (cdr (assoc type reftex-path-environment))
  1262. ;; However, historically we have separate options for the
  1263. ;; environment variables, so we have to do this:
  1264. (symbol-value (intern (concat "reftex-" type
  1265. "path-environment-variables")))
  1266. path-separator))))
  1267. (put pathvar 'status 'split)
  1268. ;; Check if we have recursive elements
  1269. (let ((path (symbol-value pathvar)) dir rec)
  1270. (while (setq dir (pop path))
  1271. (when (string= (substring dir -2) "//")
  1272. (if (file-name-absolute-p dir)
  1273. (setq rec (or rec 'absolute))
  1274. (setq rec 'relative))))
  1275. (put pathvar 'rec-type rec)))
  1276. (if recurse
  1277. ;; Return the recursive expansion of the path
  1278. (cond
  1279. ((not (get pathvar 'rec-type))
  1280. ;; Path does not contain recursive elements - use simple path
  1281. (symbol-value pathvar))
  1282. ((or (not (get pathvar 'recursive-path))
  1283. (and (eq (get pathvar 'rec-type) 'relative)
  1284. (not (equal master-dir (get pathvar 'master-dir)))))
  1285. ;; Either: We don't have a recursive expansion yet.
  1286. ;; or: Relative recursive path elements need to be expanded
  1287. ;; relative to new default directory
  1288. (message "Expanding search path to find %s file: %s ..." type file)
  1289. (put pathvar 'recursive-path
  1290. (reftex-expand-path (symbol-value pathvar) master-dir))
  1291. (put pathvar 'master-dir master-dir)
  1292. (get pathvar 'recursive-path))
  1293. (t
  1294. ;; Recursive path computed earlier is still OK.
  1295. (get pathvar 'recursive-path)))
  1296. ;; The simple path was requested
  1297. (symbol-value pathvar))))
  1298. (defun reftex-find-file-on-path (file path &optional def-dir)
  1299. ;; Find FILE along the directory list PATH.
  1300. ;; DEF-DIR is the default directory for expanding relative path elements.
  1301. (catch 'exit
  1302. (when (file-name-absolute-p file)
  1303. (if (file-regular-p file)
  1304. (throw 'exit file)
  1305. (throw 'exit nil)))
  1306. (let* ((thepath path) file1 dir)
  1307. (while (setq dir (pop thepath))
  1308. (when (string= (substring dir -2) "//")
  1309. (setq dir (substring dir 0 -1)))
  1310. (setq file1 (expand-file-name file (expand-file-name dir def-dir)))
  1311. (if (file-regular-p file1)
  1312. (throw 'exit file1)))
  1313. ;; No such file
  1314. nil)))
  1315. (defun reftex-parse-colon-path (path)
  1316. ;; Like parse-colon-parse, but // or /~ are left alone.
  1317. ;; Trailing ! or !! will be converted into `//' (emTeX convention)
  1318. (mapcar
  1319. (lambda (dir)
  1320. (if (string-match "\\(//+\\|/*!+\\)\\'" dir)
  1321. (setq dir (replace-match "//" t t dir)))
  1322. (file-name-as-directory dir))
  1323. (delete "" (split-string path (concat path-separator "+")))))
  1324. (defun reftex-expand-path (path &optional default-dir)
  1325. ;; Expand parts of path ending in `//' recursively into directory list.
  1326. ;; Relative recursive path elements are expanded relative to DEFAULT-DIR.
  1327. (let (path1 dir recursive)
  1328. (while (setq dir (pop path))
  1329. (if (setq recursive (string= (substring dir -2) "//"))
  1330. (setq dir (substring dir 0 -1)))
  1331. (if (and recursive
  1332. (not (file-name-absolute-p dir)))
  1333. (setq dir (expand-file-name dir default-dir)))
  1334. (if recursive
  1335. ;; Expand recursively
  1336. (setq path1 (append (reftex-recursive-directory-list dir) path1))
  1337. ;; Keep unchanged
  1338. (push dir path1)))
  1339. (nreverse path1)))
  1340. (defun reftex-recursive-directory-list (dir)
  1341. ;; Return a list of all directories below DIR, including DIR itself
  1342. (let ((path (list dir)) path1 file files)
  1343. (while (setq dir (pop path))
  1344. (when (file-directory-p dir)
  1345. (setq files (nreverse (directory-files dir t "[^.]")))
  1346. (while (setq file (pop files))
  1347. (if (file-directory-p file)
  1348. (push (file-name-as-directory file) path)))
  1349. (push dir path1)))
  1350. path1))
  1351. ;;; =========================================================================
  1352. ;;;
  1353. ;;; Some generally useful functions
  1354. (defun reftex-typekey-check (typekey conf-variable &optional n)
  1355. ;; Check if CONF-VARIABLE is true or contains TYPEKEY
  1356. (and n (setq conf-variable (nth n conf-variable)))
  1357. (or (eq conf-variable t)
  1358. (and (stringp conf-variable)
  1359. (let ((case-fold-search nil))
  1360. (string-match (concat "[" conf-variable "]") typekey)))))
  1361. (defun reftex-check-recursive-edit ()
  1362. ;; Check if we are already in a recursive edit. Abort with helpful
  1363. ;; message if so.
  1364. (if (marker-position reftex-recursive-edit-marker)
  1365. (error
  1366. (substitute-command-keys
  1367. "In unfinished selection process. Finish, or abort with \\[abort-recursive-edit]"))))
  1368. (defun reftex-in-comment ()
  1369. "Return non-nil if point is in a comment."
  1370. (save-excursion
  1371. (save-match-data
  1372. (let ((pos (point)))
  1373. (beginning-of-line)
  1374. (re-search-forward
  1375. (or comment-start-skip
  1376. ;; The parser may open files in fundamental mode if
  1377. ;; `reftex-initialize-temporary-buffers' is nil, so here
  1378. ;; is a default suitable for plain TeX and LaTeX.
  1379. "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(%+[ \t]*\\)")
  1380. pos t)))))
  1381. (defun reftex-no-props (string)
  1382. ;; Return STRING with all text properties removed
  1383. (and (stringp string)
  1384. (set-text-properties 0 (length string) nil string))
  1385. string)
  1386. (defun reftex-match-string (n)
  1387. ;; Match string without properties
  1388. (when (match-beginning n)
  1389. (buffer-substring-no-properties (match-beginning n) (match-end n))))
  1390. (defun reftex-region-active-p ()
  1391. "Should we operate on an active region?"
  1392. (if (fboundp 'use-region-p)
  1393. (use-region-p)
  1394. ;; For XEmacs.
  1395. (region-active-p)))
  1396. (defun reftex-kill-buffer (buffer)
  1397. ;; Kill buffer if it exists.
  1398. (and (setq buffer (get-buffer buffer))
  1399. (kill-buffer buffer)))
  1400. (defun reftex-erase-buffer (&optional buffer)
  1401. ;; Erase BUFFER if it exists. BUFFER defaults to current buffer.
  1402. ;; This even erases read-only buffers.
  1403. (cond
  1404. ((null buffer)
  1405. ;; erase current buffer
  1406. (let ((buffer-read-only nil)) (erase-buffer)))
  1407. ((setq buffer (get-buffer buffer))
  1408. ;; buffer exists
  1409. (with-current-buffer buffer
  1410. (let ((inhibit-read-only t)) (erase-buffer))))))
  1411. (defun reftex-this-word (&optional class)
  1412. ;; Grab the word around point.
  1413. (setq class (or class "-a-zA-Z0-9:_/.*;|"))
  1414. (save-excursion
  1415. (buffer-substring-no-properties
  1416. (progn (skip-chars-backward class) (point))
  1417. (progn (skip-chars-forward class) (point)))))
  1418. (defun reftex-number (n unit &optional ending)
  1419. (if (and (integerp n) (stringp unit))
  1420. (format "%d %s%s" n unit (if (= n 1) "" (or ending "s")))
  1421. ""))
  1422. (defun reftex-all-assq (key list)
  1423. ;; Return a list of all associations of KEY in LIST. Comparison with eq.
  1424. (let (rtn)
  1425. (while (setq list (memq (assq key list) list))
  1426. (push (car list) rtn)
  1427. (pop list))
  1428. (nreverse rtn)))
  1429. (defun reftex-all-assoc-string (key list)
  1430. ;; Return a list of all associations of KEY in LIST. Comparison with string=.
  1431. (let (rtn)
  1432. (while list
  1433. (if (string= (car (car list)) key)
  1434. (push (car list) rtn))
  1435. (pop list))
  1436. (nreverse rtn)))
  1437. (defun reftex-last-assoc-before-elt (key elt list &optional exclusive)
  1438. ;; Find the last association of KEY in LIST before or at ELT
  1439. ;; ELT is found in LIST with equal, not eq.
  1440. ;; Returns nil when either KEY or elt are not found in LIST.
  1441. ;; When EXCLUSIVE is non-nil, ELT cannot be the return value.
  1442. ;; On success, returns the association.
  1443. (let* ((elt (car (member elt list))) (ex (not exclusive)) ass last-ass)
  1444. (while (and (setq ass (assoc key list))
  1445. (setq list (memq ass list))
  1446. (or ex (not (eq elt (car list))))
  1447. (memq elt list))
  1448. (setq last-ass ass
  1449. list (cdr list)))
  1450. last-ass))
  1451. (defun reftex-sublist-nth (list nth predicate &optional completion)
  1452. ;; Make a list of the NTH elements of all members of LIST which
  1453. ;; fulfill PREDICATE.
  1454. ;; When COMPLETION is non-nil, make all elements of the resulting
  1455. ;; list also a list, so that the result can be used for completion.
  1456. (let (rtn)
  1457. (while list
  1458. (if (funcall predicate (car list))
  1459. (push (if completion
  1460. (list (nth nth (car list)))
  1461. (nth nth (car list)))
  1462. rtn))
  1463. (setq list (cdr list)))
  1464. (nreverse rtn)))
  1465. (defun reftex-make-selection-buffer-name (type &optional index)
  1466. ;; Make unique name for a selection buffer.
  1467. (format " *RefTeX[%s][%d]*"
  1468. type (or index (get reftex-docstruct-symbol :master-index) 0)))
  1469. (defun reftex-make-index-buffer-name (tag &optional cnt)
  1470. ;; Make unique name for an index buffer.
  1471. (format "*Index[%s][%d]*"
  1472. tag (or cnt (get reftex-docstruct-symbol :master-index) 0)))
  1473. (defun reftex-truncate (string ncols &optional ellipses padding)
  1474. ;; Truncate STRING to NCOLS characters.
  1475. ;; When PADDING is non-nil, and string is shorter than NCOLS, fill with
  1476. ;; white space to NCOLS characters. When ELLIPSES is non-nil and the
  1477. ;; string needs to be truncated, replace last 3 characters by dots.
  1478. (setq string
  1479. (if (<= (length string) ncols)
  1480. string
  1481. (if ellipses
  1482. (concat (substring string 0 (- ncols 3)) "...")
  1483. (substring string 0 ncols))))
  1484. (if padding
  1485. (format (format "%%-%ds" ncols) string)
  1486. string))
  1487. (defun reftex-nearest-match (regexp &optional max-length)
  1488. ;; Find the nearest match of REGEXP. Set the match data.
  1489. ;; If POS is given, calculate distances relative to it.
  1490. ;; Return nil if there is no match.
  1491. (let ((pos (point))
  1492. (dist (or max-length (length regexp)))
  1493. match1 match2 match)
  1494. (goto-char (min (+ pos dist) (point-max)))
  1495. (when (re-search-backward regexp nil t)
  1496. (setq match1 (match-data)))
  1497. (goto-char (max (- pos dist) (point-min)))
  1498. (when (re-search-forward regexp nil t)
  1499. (setq match2 (match-data)))
  1500. (goto-char pos)
  1501. (setq match
  1502. (cond
  1503. ((not match1) match2)
  1504. ((not match2) match1)
  1505. ((< (abs (- pos (car match1))) (abs (- pos (car match2)))) match1)
  1506. (t match2)))
  1507. (if match (progn (set-match-data match) t) nil)))
  1508. (defun reftex-auto-mode-alist ()
  1509. ;; Return an `auto-mode-alist' with only the .gz (etc) thingies.
  1510. ;; Stolen from gnus nnheader.
  1511. (let ((alist auto-mode-alist)
  1512. out)
  1513. (while alist
  1514. (when (listp (cdr (car alist)))
  1515. (push (car alist) out))
  1516. (pop alist))
  1517. (nreverse out)))
  1518. (defun reftex-window-height ()
  1519. (if (fboundp 'window-displayed-height)
  1520. (window-displayed-height)
  1521. (window-height)))
  1522. (defun reftex-enlarge-to-fit (buf2 &optional keep-current)
  1523. ;; Enlarge other window displaying buffer to show whole buffer if possible.
  1524. ;; If KEEP-CURRENT in non-nil, current buffer must remain visible.
  1525. (let* ((win1 (selected-window))
  1526. (buf1 (current-buffer))
  1527. (win2 (get-buffer-window buf2))) ;; Only on current frame.
  1528. (when win2
  1529. (select-window win2)
  1530. (unless (and (pos-visible-in-window-p (point-min))
  1531. (pos-visible-in-window-p (point-max)))
  1532. (enlarge-window (1+ (- (count-lines (point-min) (point-max))
  1533. (reftex-window-height))))))
  1534. (cond
  1535. ((window-live-p win1) (select-window win1))
  1536. (keep-current
  1537. ;; we must have the old buffer!
  1538. (switch-to-buffer-other-window buf1)
  1539. (shrink-window (- (window-height) window-min-height))))))
  1540. (defun reftex-select-with-char (prompt help-string &optional delay-time scroll)
  1541. ;; Offer to select something with PROMPT and, after DELAY-TIME seconds,
  1542. ;; also with HELP-STRING.
  1543. ;; When SCROLL is non-nil, use SPC and DEL to scroll help window.
  1544. (let ((char ?\?))
  1545. (save-window-excursion
  1546. (catch 'exit
  1547. (message "%s (?=Help)" prompt)
  1548. (when (or (sit-for (or delay-time 0))
  1549. (= ?\? (setq char (read-char-exclusive))))
  1550. (reftex-kill-buffer "*RefTeX Select*")
  1551. (switch-to-buffer-other-window "*RefTeX Select*")
  1552. (insert help-string)
  1553. (goto-char 1)
  1554. (unless (and (pos-visible-in-window-p (point-min))
  1555. (pos-visible-in-window-p (point-max)))
  1556. (enlarge-window (1+ (- (count-lines (point-min) (point-max))
  1557. (reftex-window-height)))))
  1558. (setq truncate-lines t))
  1559. (if (and (pos-visible-in-window-p (point-min))
  1560. (pos-visible-in-window-p (point-max)))
  1561. nil
  1562. (setq prompt (concat prompt (if scroll " (SPC/DEL=Scroll)" ""))))
  1563. (message "%s" prompt)
  1564. (and (equal char ?\?) (setq char (read-char-exclusive)))
  1565. (while t
  1566. (cond ((equal char ?\C-g) (keyboard-quit))
  1567. ((equal char ?\?))
  1568. ((and scroll (equal char ?\ ))
  1569. (condition-case nil (scroll-up) (error nil))
  1570. (message "%s" prompt))
  1571. ((and scroll (equal char ?\C-? ))
  1572. (condition-case nil (scroll-down) (error nil))
  1573. (message "%s" prompt))
  1574. (t (message "")
  1575. (reftex-kill-buffer "*RefTeX Select*")
  1576. (throw 'exit char)))
  1577. (setq char (read-char-exclusive)))))))
  1578. (defun reftex-make-regexp-allow-for-ctrl-m (string)
  1579. ;; convert STRING into a regexp, allowing ^M for \n and vice versa
  1580. (let ((start -2))
  1581. (setq string (regexp-quote string))
  1582. (while (setq start (string-match "[\n\r]" string (+ 3 start)))
  1583. (setq string (replace-match "[\n\r]" nil t string)))
  1584. string))
  1585. (defun reftex-get-buffer-visiting (file)
  1586. ;; return a buffer visiting FILE
  1587. (cond
  1588. ((boundp 'find-file-compare-truenames) ; XEmacs
  1589. (let ((find-file-compare-truenames t))
  1590. (get-file-buffer file)))
  1591. ((fboundp 'find-buffer-visiting) ; Emacs
  1592. (find-buffer-visiting file))
  1593. (t (error "This should not happen (reftex-get-buffer-visiting)"))))
  1594. ;; Define `current-message' for compatibility with XEmacs prior to 20.4
  1595. (defvar message-stack)
  1596. (if (and (featurep 'xemacs)
  1597. (not (fboundp 'current-message)))
  1598. (defun current-message (&optional _frame)
  1599. (cdr (car message-stack))))
  1600. (defun reftex-visited-files (list)
  1601. ;; Takes a list of filenames and returns the buffers of those already visited
  1602. (delq nil (mapcar (lambda (x) (if (reftex-get-buffer-visiting x) x nil))
  1603. list)))
  1604. (defun reftex-get-file-buffer-force (file &optional mark-to-kill)
  1605. ;; Return a buffer visiting file. Make one, if necessary.
  1606. ;; If neither such a buffer nor the file exist, return nil.
  1607. ;; If MARK-TO-KILL is t and there is no live buffer, visit the file with
  1608. ;; initializations according to `reftex-initialize-temporary-buffers',
  1609. ;; and mark the buffer to be killed after use.
  1610. (let ((buf (reftex-get-buffer-visiting file)))
  1611. (cond (buf
  1612. ;; We have it already as a buffer - just return it
  1613. buf)
  1614. ((file-readable-p file)
  1615. ;; At least there is such a file and we can read it.
  1616. (if (or (not mark-to-kill)
  1617. (eq t reftex-initialize-temporary-buffers))
  1618. ;; Visit the file with full magic
  1619. (setq buf (find-file-noselect file))
  1620. ;; Else: Visit the file just briefly, without or
  1621. ;; with limited Magic
  1622. ;; The magic goes away
  1623. (cl-letf ((format-alist nil)
  1624. (auto-mode-alist (reftex-auto-mode-alist))
  1625. ((default-value 'major-mode) 'fundamental-mode)
  1626. (enable-local-variables nil)
  1627. (after-insert-file-functions nil))
  1628. (setq buf (find-file-noselect file)))
  1629. ;; Is there a hook to run?
  1630. (when (listp reftex-initialize-temporary-buffers)
  1631. (with-current-buffer buf
  1632. (run-hooks 'reftex-initialize-temporary-buffers))))
  1633. ;; Let's see if we got a license to kill :-|
  1634. (and mark-to-kill
  1635. (cl-pushnew buf reftex-buffers-to-kill))
  1636. ;; Return the new buffer
  1637. buf)
  1638. ;; If no such file exists, return nil
  1639. (t nil))))
  1640. (defun reftex-kill-temporary-buffers (&optional buffer)
  1641. ;; Kill all buffers in the list reftex-kill-temporary-buffers.
  1642. (cond
  1643. (buffer
  1644. (when (member buffer reftex-buffers-to-kill)
  1645. (kill-buffer buffer)
  1646. (setq reftex-buffers-to-kill
  1647. (delete buffer reftex-buffers-to-kill))))
  1648. (t
  1649. (while (setq buffer (pop reftex-buffers-to-kill))
  1650. (when (bufferp buffer)
  1651. (and (buffer-modified-p buffer)
  1652. (y-or-n-p (format "Save file %s? "
  1653. (buffer-file-name buffer)))
  1654. (with-current-buffer buffer
  1655. (save-buffer)))
  1656. (kill-buffer buffer))
  1657. (pop reftex-buffers-to-kill)))))
  1658. (defun reftex-splice-symbols-into-list (list alist)
  1659. ;; Splice the association in ALIST of any symbols in LIST into the list.
  1660. ;; Return new list.
  1661. (let (rtn tmp)
  1662. (while list
  1663. (while (and (not (null (car list))) ;; keep list elements nil
  1664. (symbolp (car list)))
  1665. (setq tmp (car list))
  1666. (cond
  1667. ((assoc tmp alist)
  1668. (setq list (append (nth 2 (assoc tmp alist)) (cdr list))))
  1669. (t
  1670. (error "Cannot treat symbol %s in reftex-label-alist"
  1671. (symbol-name tmp)))))
  1672. (push (pop list) rtn))
  1673. (nreverse rtn)))
  1674. (defun reftex-remove-symbols-from-list (list)
  1675. ;; Remove all symbols from list
  1676. (let (rtn)
  1677. (while list
  1678. (unless (symbolp (car list))
  1679. (push (car list) rtn))
  1680. (setq list (cdr list)))
  1681. (nreverse rtn)))
  1682. (defun reftex-uniquify (list &optional sort)
  1683. ;; Return a list of all strings in LIST, but each only once, keeping order
  1684. ;; unless SORT is set (faster!).
  1685. (setq list (copy-sequence list))
  1686. (if sort
  1687. (progn
  1688. (setq list (sort list 'string<))
  1689. (let ((p list))
  1690. (while (cdr p)
  1691. (if (string= (car p) (car (cdr p)))
  1692. (setcdr p (cdr (cdr p)))
  1693. (setq p (cdr p)))))
  1694. list)
  1695. (let ((p list) lst elt)
  1696. ;; push all sublists into lst in reverse(!) order
  1697. (while p
  1698. (push p lst)
  1699. (setq p (cdr p)))
  1700. ;; sort all sublists
  1701. (setq lst (sort lst (lambda (x1 x2) (string< (car x1) (car x2)))))
  1702. (while (cdr lst)
  1703. (setq elt (car (car lst)))
  1704. ;; for equal elements in the sorted sublist, replace the
  1705. ;; last(!) original list member with nil
  1706. (when (string= elt (car (cadr lst)))
  1707. (setcar (pop lst) nil)
  1708. (while (and (cdr lst) (string= elt (car (cadr lst))))
  1709. (setcar (pop lst) nil)))
  1710. (pop lst)))
  1711. ;; weed out all nils and return.
  1712. (delq nil list)))
  1713. (defun reftex-uniquify-by-car (alist &optional keep-list sort)
  1714. ;; Return a list of all elements in ALIST, but each car only once.
  1715. ;; Elements of KEEP-LIST are not removed even if duplicate.
  1716. ;; The order is kept unless SORT is set (faster!).
  1717. (setq keep-list (sort (copy-sequence keep-list) #'string<)
  1718. alist (copy-sequence alist))
  1719. (if sort
  1720. (let (lst elt)
  1721. (setq alist (sort alist (lambda(a b) (string< (car a) (car b)))))
  1722. (setq lst alist)
  1723. (while (cdr lst)
  1724. (setq elt (car (car lst)))
  1725. (when (string= elt (car (cadr lst)))
  1726. (while (and keep-list (string< (car keep-list) elt))
  1727. (pop keep-list))
  1728. (if (and keep-list (string= elt (car keep-list)))
  1729. (progn
  1730. (pop lst)
  1731. (while (and (cdr lst)
  1732. (string= elt (car (cadr lst))))
  1733. (pop lst)))
  1734. (setcdr lst (cdr (cdr lst)))
  1735. (while (and (cdr lst)
  1736. (string= elt (car (cadr lst))))
  1737. (setcdr lst (cdr (cdr lst))))))
  1738. (pop lst))
  1739. alist)
  1740. (let ((p alist) lst elt)
  1741. (while p
  1742. (push p lst)
  1743. (setq p (cdr p)))
  1744. (setq lst (sort lst (lambda(a b) (string< (car (car a))
  1745. (car (car b))))))
  1746. (while (cdr lst)
  1747. (setq elt (car (car (car lst))))
  1748. (when (string= elt (car (car (cadr lst))))
  1749. (while (and keep-list (string< (car keep-list) elt))
  1750. (pop keep-list))
  1751. (if (and keep-list (string= elt (car keep-list)))
  1752. (progn
  1753. (pop lst)
  1754. (while (and (cdr lst)
  1755. (string= elt (car (car (cadr lst)))))
  1756. (pop lst)))
  1757. (setcar (pop lst) nil)
  1758. (while (and (cdr lst)
  1759. (string= elt (car (car (cadr lst)))))
  1760. (setcar (pop lst) nil))))
  1761. (pop lst)))
  1762. (delq nil alist)))
  1763. (defun reftex-remove-if (predicate list)
  1764. "Nondestructively remove all items from LIST which satisfy PREDICATE."
  1765. (let (result)
  1766. (dolist (elt list (nreverse result))
  1767. (unless (funcall predicate elt)
  1768. (push elt result)))))
  1769. (defun reftex-abbreviate-title (string)
  1770. (reftex-convert-string string "[-~ \t\n\r,;]" nil t t
  1771. 5 40 nil 1 " " (nth 5 reftex-derive-label-parameters)))
  1772. (defun reftex-convert-string (string split-re invalid-re dot keep-fp
  1773. nwords maxchar invalid abbrev sep
  1774. ignore-words &optional downcase)
  1775. "Convert a string (a sentence) to something shorter.
  1776. SPLIT-RE is the regular expression used to split the string into words.
  1777. INVALID-RE matches characters which are invalid in the final string.
  1778. DOT t means add dots to abbreviated words.
  1779. KEEP-FP t means to keep a final punctuation when applicable.
  1780. NWORDS Number of words to use.
  1781. MAXCHAR Maximum number of characters in the final string.
  1782. INVALID nil: Throw away any words containing stuff matched with INVALID-RE.
  1783. t: Throw away only the matched part, not the whole word.
  1784. ABBREV nil: Never abbreviate words.
  1785. t: Always abbreviate words (see `reftex-abbrev-parameters').
  1786. not t and not nil: Abbreviate words if necessary to shorten
  1787. string below MAXCHAR.
  1788. SEP String separating different words in the output string.
  1789. IGNORE-WORDS List of words which should be removed from the string."
  1790. (let* ((words0 (split-string string (or split-re "[ \t\n\r]")))
  1791. (reftex-label-illegal-re (or invalid-re "\000"))
  1792. (abbrev-re (concat
  1793. "\\`\\("
  1794. (make-string (nth 0 reftex-abbrev-parameters) ?.)
  1795. "[" (nth 2 reftex-abbrev-parameters) "]*"
  1796. "\\)"
  1797. "[" (nth 3 reftex-abbrev-parameters) "]"
  1798. (make-string (1- (nth 1 reftex-abbrev-parameters)) ?.)))
  1799. words word)
  1800. ;; Remove words from the ignore list or with funny characters
  1801. (while (setq word (pop words0))
  1802. (if downcase (setq word (downcase word)))
  1803. (cond
  1804. ((member (downcase word) ignore-words))
  1805. ((string-match reftex-label-illegal-re word)
  1806. (when invalid
  1807. (while (string-match reftex-label-illegal-re word)
  1808. (setq word (replace-match "" nil nil word)))
  1809. (push word words)))
  1810. (t
  1811. (push word words))))
  1812. (setq words (nreverse words))
  1813. ;; Restrict number of words
  1814. (if (> (length words) nwords)
  1815. (setcdr (nthcdr (1- nwords) words) nil))
  1816. ;; First, try to use all words
  1817. (setq string (mapconcat 'identity words sep))
  1818. ;; Abbreviate words if enforced by user settings or string length
  1819. (if (or (eq t abbrev)
  1820. (and abbrev
  1821. (> (length string) maxchar)))
  1822. (setq words
  1823. (mapcar
  1824. (lambda (w) (if (string-match abbrev-re w)
  1825. (if dot
  1826. (concat (match-string 1 w) ".")
  1827. (match-string 1 w))
  1828. w))
  1829. words)
  1830. string (mapconcat 'identity words sep)))
  1831. ;; Shorten if still to long
  1832. (setq string
  1833. (if (> (length string) maxchar)
  1834. (substring string 0 maxchar)
  1835. string))
  1836. ;; Delete the final punctuation, if any
  1837. (if (and (not keep-fp) (string-match "\\s.+\\'" string))
  1838. (setq string (replace-match "" nil nil string)))
  1839. string))
  1840. (defun reftex-nicify-text (text)
  1841. ;; Make TEXT nice for inclusion as context into label menu.
  1842. ;; 1. remove line breaks and extra white space
  1843. (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text)
  1844. (setq text (replace-match " " nil t text)))
  1845. ;; 2. cut before the next `\end{' or `\item' or `\\'
  1846. (if (string-match "\\(\\\\end{\\|\\\\item\\|\\\\\\\\\\).*" text)
  1847. (setq text (replace-match "" nil t text)))
  1848. ;; 3. kill the embedded label
  1849. (if (string-match "\\\\label{[^}]*}" text)
  1850. (setq text (replace-match "" nil t text)))
  1851. ;; 4. remove leading garbage
  1852. (if (string-match "\\`[ }]+" text)
  1853. (setq text (replace-match "" nil t text)))
  1854. ;; 5. limit length
  1855. (cond
  1856. ((> (length text) 100) (substring text 0 100))
  1857. ((= (length text) 0) (make-string 1 ?\ ))
  1858. (t text)))
  1859. ;;; =========================================================================
  1860. ;;;
  1861. ;;; Fontification and Highlighting
  1862. (defun reftex-use-fonts ()
  1863. ;; Return t if we can and want to use fonts.
  1864. (and ; window-system
  1865. reftex-use-fonts
  1866. (featurep 'font-lock)))
  1867. (defun reftex-refontify ()
  1868. ;; Return t if we need to refontify context
  1869. (and (reftex-use-fonts)
  1870. (or (eq t reftex-refontify-context)
  1871. (and (eq 1 reftex-refontify-context)
  1872. ;; Test of we use the font-lock version of x-symbol
  1873. (and (featurep 'x-symbol-tex) (not (boundp 'x-symbol-mode)))))))
  1874. (defvar font-lock-defaults-computed)
  1875. (defun reftex-fontify-select-label-buffer (parent-buffer)
  1876. ;; Fontify the `*RefTeX Select*' buffer. Buffer is temporarily renamed to
  1877. ;; start with none-SPC char, because Font-Lock otherwise refuses operation.
  1878. (run-hook-with-args 'reftex-pre-refontification-functions
  1879. parent-buffer 'reftex-ref)
  1880. (let* ((oldname (buffer-name))
  1881. (newname (concat "Fontify-me-" oldname)))
  1882. (unwind-protect
  1883. (progn
  1884. ;; Rename buffer temporarily to start w/o space (because of font-lock)
  1885. (rename-buffer newname t)
  1886. (cond
  1887. ((fboundp 'font-lock-default-fontify-region)
  1888. ;; Good: we have the indirection functions
  1889. (set (make-local-variable 'font-lock-fontify-region-function)
  1890. 'reftex-select-font-lock-fontify-region)
  1891. (let ((major-mode 'latex-mode))
  1892. (font-lock-mode 1)))
  1893. ((fboundp 'font-lock-set-defaults-1)
  1894. ;; Looks like the XEmacs font-lock stuff.
  1895. ;; FIXME: this is still kind of a hack, but it works.
  1896. (set (make-local-variable 'font-lock-keywords) nil)
  1897. (let ((major-mode 'latex-mode)
  1898. (font-lock-defaults-computed nil))
  1899. (font-lock-set-defaults-1)
  1900. (reftex-select-font-lock-fontify-region (point-min) (point-max))))
  1901. (t
  1902. ;; Oops?
  1903. (message "Sorry: cannot refontify RefTeX Select buffer."))))
  1904. (rename-buffer oldname))))
  1905. (defun reftex-select-font-lock-fontify-region (beg end &optional _loudly)
  1906. ;; Fontify a region, but only lines starting with a dot.
  1907. (let ((func (if (fboundp 'font-lock-default-fontify-region)
  1908. 'font-lock-default-fontify-region
  1909. 'font-lock-fontify-region))
  1910. beg1 end1)
  1911. (goto-char beg)
  1912. (while (re-search-forward "^\\." end t)
  1913. (setq beg1 (point) end1 (progn (skip-chars-forward "^\n") (point)))
  1914. (funcall func beg1 end1 nil)
  1915. (goto-char end1))))
  1916. (defun reftex-select-font-lock-unfontify (&rest _ignore) t)
  1917. (defun reftex-verified-face (&rest faces)
  1918. ;; Return the first valid face in FACES, or nil if none is valid.
  1919. ;; Also, when finding a nil element in FACES, return nil. This
  1920. ;; function is just a safety net to catch name changes of builtin
  1921. ;; fonts. Currently it is only used for reftex-label-face.
  1922. (let (face)
  1923. (catch 'exit
  1924. (while (setq face (pop faces))
  1925. (if (featurep 'xemacs)
  1926. (if (find-face face) (throw 'exit face))
  1927. (if (facep face) (throw 'exit face)))))))
  1928. ;; Highlighting uses overlays. For XEmacs, we use extends.
  1929. (defalias 'reftex-make-overlay
  1930. (if (featurep 'xemacs) 'make-extent 'make-overlay))
  1931. (defalias 'reftex-overlay-put
  1932. (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
  1933. (defalias 'reftex-move-overlay
  1934. (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay))
  1935. (defalias 'reftex-delete-overlay
  1936. (if (featurep 'xemacs) 'detach-extent 'delete-overlay))
  1937. ;; We keep a vector with several different overlays to do our highlighting.
  1938. (defvar reftex-highlight-overlays [nil nil nil])
  1939. ;; Initialize the overlays
  1940. (aset reftex-highlight-overlays 0 (reftex-make-overlay 1 1))
  1941. (reftex-overlay-put (aref reftex-highlight-overlays 0)
  1942. 'face 'highlight)
  1943. (aset reftex-highlight-overlays 1 (reftex-make-overlay 1 1))
  1944. (reftex-overlay-put (aref reftex-highlight-overlays 1)
  1945. 'face reftex-cursor-selected-face)
  1946. (aset reftex-highlight-overlays 2 (reftex-make-overlay 1 1))
  1947. (reftex-overlay-put (aref reftex-highlight-overlays 2)
  1948. 'face reftex-cursor-selected-face)
  1949. ;; Two functions for activating and deactivation highlight overlays
  1950. (defun reftex-highlight (index begin end &optional buffer)
  1951. "Highlight a region with overlay INDEX."
  1952. (reftex-move-overlay (aref reftex-highlight-overlays index)
  1953. begin end (or buffer (current-buffer))))
  1954. (defun reftex-unhighlight (index)
  1955. "Detach overlay INDEX."
  1956. (reftex-delete-overlay (aref reftex-highlight-overlays index)))
  1957. (defun reftex-highlight-shall-die ()
  1958. ;; Function used in pre-command-hook to remove highlights.
  1959. (remove-hook 'pre-command-hook 'reftex-highlight-shall-die)
  1960. (reftex-unhighlight 0))
  1961. ;;; =========================================================================
  1962. ;;;
  1963. ;;; Keybindings
  1964. (defvar bibtex-mode-map)
  1965. ;; Bind `reftex-view-crossref-from-bibtex' in BibTeX mode map
  1966. (eval-after-load
  1967. "bibtex"
  1968. '(define-key bibtex-mode-map "\C-c&" 'reftex-view-crossref-from-bibtex))
  1969. ;;; =========================================================================
  1970. ;;;
  1971. ;;; Menu
  1972. ;; Define a menu for the menu bar if Emacs is running under X
  1973. (defvar reftex-isearch-minor-mode nil)
  1974. (make-variable-buffer-local 'reftex-isearch-minor-mode)
  1975. (easy-menu-define reftex-mode-menu reftex-mode-map
  1976. "Menu used in RefTeX mode"
  1977. `("Ref"
  1978. ["Table of Contents" reftex-toc t]
  1979. ["Recenter TOC" reftex-toc-recenter t]
  1980. "--"
  1981. ["\\label" reftex-label t]
  1982. ["\\ref" reftex-reference t]
  1983. ["\\cite" reftex-citation t]
  1984. ("\\index"
  1985. ["\\index" reftex-index t]
  1986. ["\\index{THIS}" reftex-index-selection-or-word t]
  1987. "--"
  1988. ["Add THIS to Index Phrases" reftex-index-phrase-selection-or-word t]
  1989. ["Visit Phrase Buffer" reftex-index-visit-phrases-buffer t]
  1990. ["Apply Phrases to Region" reftex-index-phrases-apply-to-region t]
  1991. "--"
  1992. ["Display the Index" reftex-display-index t])
  1993. "--"
  1994. ["View Crossref" reftex-view-crossref t]
  1995. "--"
  1996. ("Parse Document"
  1997. ["One File" reftex-parse-one reftex-enable-partial-scans]
  1998. ["Entire Document" reftex-parse-all t]
  1999. ["Save to File" (reftex-access-parse-file 'write)
  2000. (> (length (symbol-value reftex-docstruct-symbol)) 0)]
  2001. ["Restore from File" (reftex-access-parse-file 'restore) t])
  2002. ("Global Actions"
  2003. ["Search Whole Document" reftex-search-document t]
  2004. ["Search Again" tags-loop-continue t]
  2005. ["Replace in Document" reftex-query-replace-document t]
  2006. ["Grep on Document" reftex-grep-document t]
  2007. "--"
  2008. ["Goto Label" reftex-goto-label t]
  2009. ["Find Duplicate Labels" reftex-find-duplicate-labels t]
  2010. ["Change Label and Refs" reftex-change-label t]
  2011. ["Renumber Simple Labels" reftex-renumber-simple-labels t]
  2012. "--"
  2013. ["Create BibTeX File" reftex-create-bibtex-file t]
  2014. "--"
  2015. ["Create TAGS File" reftex-create-tags-file t]
  2016. "--"
  2017. ["Save Document" reftex-save-all-document-buffers t])
  2018. "--"
  2019. ("Options"
  2020. "PARSER"
  2021. ["Partial Scans"
  2022. (setq reftex-enable-partial-scans (not reftex-enable-partial-scans))
  2023. :style toggle :selected reftex-enable-partial-scans]
  2024. ["Auto-Save Parse Info"
  2025. (setq reftex-save-parse-info (not reftex-save-parse-info))
  2026. :style toggle :selected reftex-save-parse-info]
  2027. "--"
  2028. "TOC RECENTER"
  2029. ["Automatic Recenter" reftex-toggle-auto-toc-recenter
  2030. :style toggle :selected reftex-toc-auto-recenter-timer]
  2031. "--"
  2032. "CROSSREF INFO"
  2033. ["Automatic Info" reftex-toggle-auto-view-crossref
  2034. :style toggle :selected reftex-auto-view-crossref-timer]
  2035. ["...in Echo Area" (setq reftex-auto-view-crossref t)
  2036. :style radio :selected (eq reftex-auto-view-crossref t)]
  2037. ["...in Other Window" (setq reftex-auto-view-crossref 'window)
  2038. :style radio :selected (eq reftex-auto-view-crossref 'window)]
  2039. "--"
  2040. "MISC"
  2041. ["AUCTeX Interface" reftex-toggle-plug-into-AUCTeX
  2042. :style toggle :selected reftex-plug-into-AUCTeX]
  2043. ["isearch whole document" reftex-isearch-minor-mode
  2044. :style toggle :selected reftex-isearch-minor-mode])
  2045. ("Reference Style"
  2046. ,@(let (list item)
  2047. (dolist (elt reftex-ref-style-alist)
  2048. (setq elt (car elt)
  2049. item (vector
  2050. elt
  2051. `(reftex-ref-style-toggle ,elt)
  2052. :style 'toggle
  2053. :selected `(member ,elt (reftex-ref-style-list))))
  2054. (unless (member item list)
  2055. (setq list (append list (list item)))))
  2056. list))
  2057. ("Citation Style"
  2058. ,@(mapcar
  2059. (lambda (x)
  2060. (vector
  2061. (capitalize (symbol-name (car x)))
  2062. (list 'reftex-set-cite-format (list 'quote (car x)))
  2063. :style 'radio :selected
  2064. (list 'eq (list 'reftex-get-cite-format) (list 'quote (car x)))))
  2065. reftex-cite-format-builtin)
  2066. "--"
  2067. "Sort Database Matches"
  2068. ["Not" (setq reftex-sort-bibtex-matches nil)
  2069. :style radio :selected (eq reftex-sort-bibtex-matches nil)]
  2070. ["by Author" (setq reftex-sort-bibtex-matches 'author)
  2071. :style radio :selected (eq reftex-sort-bibtex-matches 'author)]
  2072. ["by Year" (setq reftex-sort-bibtex-matches 'year)
  2073. :style radio :selected (eq reftex-sort-bibtex-matches 'year)]
  2074. ["by Year, reversed" (setq reftex-sort-bibtex-matches 'reverse-year)
  2075. :style radio :selected (eq reftex-sort-bibtex-matches 'reverse-year)])
  2076. ("Index Style"
  2077. ,@(mapcar
  2078. (lambda (x)
  2079. (vector
  2080. (capitalize (symbol-name (car x)))
  2081. (list 'reftex-add-index-macros (list 'list (list 'quote (car x))))
  2082. :style 'radio :selected
  2083. (list 'memq (list 'quote (car x))
  2084. (list 'get 'reftex-docstruct-symbol
  2085. (list 'quote 'reftex-index-macros-style)))))
  2086. reftex-index-macros-builtin))
  2087. "--"
  2088. ["Reset RefTeX Mode" reftex-reset-mode t]
  2089. "--"
  2090. ("Customize"
  2091. ["Browse RefTeX Group" reftex-customize t]
  2092. "--"
  2093. ["Build Full Customize Menu" reftex-create-customize-menu
  2094. (fboundp 'customize-menu-create)])
  2095. ("Documentation"
  2096. ["Info" reftex-info t]
  2097. ["Commentary" reftex-show-commentary t])))
  2098. (defun reftex-customize ()
  2099. "Call the customize function with reftex as argument."
  2100. (interactive)
  2101. (customize-browse 'reftex))
  2102. (defun reftex-create-customize-menu ()
  2103. "Create a full customization menu for RefTeX, insert it into the menu."
  2104. (interactive)
  2105. (if (fboundp 'customize-menu-create)
  2106. (progn
  2107. (easy-menu-change
  2108. '("Ref") "Customize"
  2109. `(["Browse RefTeX group" reftex-customize t]
  2110. "--"
  2111. ,(customize-menu-create 'reftex)
  2112. ["Set" Custom-set t]
  2113. ["Save" Custom-save t]
  2114. ["Reset to Current" Custom-reset-current t]
  2115. ["Reset to Saved" Custom-reset-saved t]
  2116. ["Reset to Standard Settings" Custom-reset-standard t]))
  2117. (message "\"Ref\"-menu now contains full customization menu"))
  2118. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  2119. ;;; Misc
  2120. (defun reftex-show-commentary ()
  2121. "Use the finder to view the file documentation from `reftex.el'."
  2122. (interactive)
  2123. (finder-commentary "reftex.el"))
  2124. (defun reftex-info (&optional node)
  2125. "Read documentation for RefTeX in the info system.
  2126. With optional NODE, go directly to that node."
  2127. (interactive)
  2128. (info (format "(reftex)%s" (or node ""))))
  2129. (defun reftex-report-bug ()
  2130. "Report a bug in RefTeX.
  2131. Don't hesitate to report any problems or inaccurate documentation.
  2132. If you don't have setup sending mail from (X)Emacs, please copy the
  2133. output buffer into your mail program, as it gives us important
  2134. information about your RefTeX version and configuration."
  2135. (interactive)
  2136. (require 'reporter)
  2137. (defvar reporter-prompt-for-summary-p)
  2138. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  2139. (reporter-submit-bug-report
  2140. "bug-auctex@gnu.org, bug-gnu-emacs@gnu.org"
  2141. reftex-version
  2142. (list 'window-system
  2143. 'reftex-plug-into-AUCTeX)
  2144. nil nil
  2145. "Remember to cover the basics, that is, what you expected to happen and
  2146. what in fact did happen.
  2147. Check if the bug is reproducible with an up-to-date version of
  2148. RefTeX available from http://www.gnu.org/software/auctex/.
  2149. If the bug is triggered by a specific \(La)TeX file, you should try
  2150. to produce a minimal sample file showing the problem and include it
  2151. in your report.
  2152. Your bug report will be posted to the AUCTeX bug reporting list.
  2153. ------------------------------------------------------------------------")))
  2154. ;;; Install the kill-buffer and kill-emacs hooks ------------------------------
  2155. (add-hook 'kill-buffer-hook 'reftex-kill-buffer-hook)
  2156. (unless noninteractive
  2157. (add-hook 'kill-emacs-hook 'reftex-kill-emacs-hook))
  2158. ;;; Run Hook ------------------------------------------------------------------
  2159. (run-hooks 'reftex-load-hook)
  2160. ;;; That's it! ----------------------------------------------------------------
  2161. (setq reftex-tables-dirty t) ; in case this file is evaluated by hand
  2162. (provide 'reftex)
  2163. ;;; reftex.el ends here