reftex-index.el 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. ;;; reftex-index.el --- index support with RefTeX
  2. ;; Copyright (C) 1997-2015 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <dominik@science.uva.nl>
  4. ;; Maintainer: auctex-devel@gnu.org
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (declare-function texmathp "ext:texmathp" ())
  20. (require 'reftex)
  21. ;; START remove for XEmacs release
  22. (defvar mark-active)
  23. (defvar transient-mark-mode)
  24. (defvar TeX-master)
  25. ;; END remove for XEmacs release
  26. ;;;###autoload
  27. (defun reftex-index-selection-or-word (&optional arg phrase)
  28. "Put selection or the word near point into the default index macro.
  29. This uses the information in `reftex-index-default-macro' to make an index
  30. entry. The phrase indexed is the current selection or the word near point.
  31. When called with one `C-u' prefix, let the user have a chance to edit the
  32. index entry. When called with 2 `C-u' as prefix, also ask for the index
  33. macro and other stuff.
  34. When called inside TeX math mode as determined by the `texmathp.el' library
  35. which is part of AUCTeX, the string is first processed with the
  36. `reftex-index-math-format', which see."
  37. (interactive "P")
  38. (let* ((use-default (not (equal arg '(16)))) ; check for double prefix
  39. ;; check if we have an active selection
  40. (active (reftex-region-active-p))
  41. (beg (if active
  42. (region-beginning)
  43. (save-excursion
  44. (skip-syntax-backward "w\\") (point))))
  45. (end (if active
  46. (region-end)
  47. (save-excursion
  48. (skip-syntax-forward "w\\") (point))))
  49. (sel (buffer-substring beg end))
  50. (mathp (condition-case nil (texmathp) (error nil)))
  51. (current-prefix-arg nil) ; we want to call reftex-index without prefix.
  52. key def-char def-tag full-entry)
  53. (if phrase
  54. (progn
  55. (reftex-index-visit-phrases-buffer)
  56. (reftex-index-new-phrase sel))
  57. (if (equal sel "")
  58. ;; Nothing selected, no word, so use full reftex-index command
  59. (reftex-index)
  60. ;; OK, we have something to index here.
  61. ;; Add the dollars when necessary
  62. (setq key (if mathp
  63. (format reftex-index-math-format sel)
  64. sel))
  65. ;; Get info from `reftex-index-default-macro'
  66. (setq def-char (if use-default (car reftex-index-default-macro)))
  67. (setq def-tag (if use-default (nth 1 reftex-index-default-macro)))
  68. ;; Does the user want to edit the entry?
  69. (setq full-entry (if arg
  70. (reftex-index-complete-key
  71. def-tag nil (cons key 0))
  72. key))
  73. ;; Delete what is in the buffer and make the index entry
  74. (delete-region beg end)
  75. (reftex-index def-char full-entry def-tag sel)))))
  76. ;;;###autoload
  77. (defun reftex-index (&optional char key tag sel no-insert)
  78. "Query for an index macro and insert it along with its arguments.
  79. The index macros available are those defined in `reftex-index-macro' or
  80. by a call to `reftex-add-index-macros', typically from an AUCTeX style file.
  81. RefteX provides completion for the index tag and the index key, and
  82. will prompt for other arguments."
  83. (interactive)
  84. ;; Ensure access to scanning info
  85. (reftex-ensure-index-support t)
  86. (reftex-access-scan-info current-prefix-arg)
  87. ;; Find out which macro we are going to use
  88. (let* ((char (or char
  89. (reftex-select-with-char reftex-query-index-macro-prompt
  90. reftex-query-index-macro-help)))
  91. (macro (nth 1 (assoc char reftex-key-to-index-macro-alist)))
  92. (entry (or (assoc macro reftex-index-macro-alist)
  93. (error "No index macro associated with %c" char)))
  94. (ntag (nth 1 entry))
  95. (tag (or tag (nth 1 entry)))
  96. (nargs (nth 4 entry))
  97. (nindex (nth 5 entry))
  98. (opt-args (nth 6 entry))
  99. (repeat (nth 7 entry))
  100. opt tag1 value)
  101. ;; Get the supported arguments
  102. (if (stringp tag)
  103. (setq tag1 tag)
  104. (setq tag1 (or (reftex-index-complete-tag tag opt-args) "")))
  105. (setq key (or key
  106. (reftex-index-complete-key
  107. (if (string= tag1 "") "idx" tag1)
  108. (member nindex opt-args))))
  109. ;; Insert the macro and ask for any additional args
  110. (insert macro)
  111. (loop for i from 1 to nargs do
  112. (setq opt (member i opt-args)
  113. value (cond ((= nindex i) key)
  114. ((equal ntag i) tag1)
  115. (t (read-string (concat "Macro arg nr. "
  116. (int-to-string i)
  117. (if opt " (optional)" "")
  118. ": ")))))
  119. (unless (and opt (string= value ""))
  120. (insert (if opt "[" "{") value (if opt "]" "}"))))
  121. (and repeat (stringp sel) (insert sel))
  122. (and key reftex-plug-into-AUCTeX (fboundp 'LaTeX-add-index-entries)
  123. (LaTeX-add-index-entries key))
  124. (reftex-index-update-taglist tag1)
  125. (reftex-notice-new)))
  126. (defun reftex-default-index ()
  127. (cond ((null reftex-index-default-tag) nil)
  128. ((stringp reftex-index-default-tag) reftex-index-default-tag)
  129. (t (or (get reftex-docstruct-symbol 'default-index-tag)
  130. "idx"))))
  131. (defun reftex-update-default-index (tag &optional tag-list)
  132. (if (and (not (equal tag ""))
  133. (stringp tag)
  134. (eq reftex-index-default-tag 'last)
  135. (or (null tag-list)
  136. (member tag tag-list)))
  137. (put reftex-docstruct-symbol 'default-index-tag tag)))
  138. ;;;###autoload
  139. (defun reftex-index-complete-tag (&optional itag opt-args)
  140. ;; Ask the user for a tag, completing on known tags.
  141. ;; ITAG is the argument number which contains the tag.
  142. ;; OPT-ARGS is a list of optional argument indices, as given by
  143. ;; `reftex-parse-args'.
  144. (let* ((opt (and (integerp itag) (member itag opt-args)))
  145. (index-tags (cdr (assq 'index-tags
  146. (symbol-value reftex-docstruct-symbol))))
  147. (default (reftex-default-index))
  148. (prompt (concat "Index tag"
  149. (if (or opt default)
  150. (format " (%s): "
  151. (concat
  152. (if opt "optional" "")
  153. (if default
  154. (concat (if opt ", " "")
  155. (format "default %s" default))
  156. "")))
  157. ": ")))
  158. (tag (completing-read prompt (mapcar 'list index-tags))))
  159. (if (and default (equal tag "")) (setq tag default))
  160. (reftex-update-default-index tag)
  161. tag))
  162. ;;;###autoload
  163. (defun reftex-index-select-tag ()
  164. ;; Have the user select an index tag.
  165. ;; FIXME: should we cache tag-alist, prompt and help?
  166. (let* ((index-tags (cdr (assoc 'index-tags
  167. (symbol-value reftex-docstruct-symbol))))
  168. (default (reftex-default-index)))
  169. (cond
  170. ((null index-tags)
  171. (error "No index tags available"))
  172. ((= (length index-tags) 1)
  173. ;; Just one index, use it
  174. (car index-tags))
  175. ((> (length index-tags) 1)
  176. ;; Several indices, ask.
  177. (let* ((tags (copy-sequence index-tags))
  178. (cnt 0)
  179. tag-alist i val len tag prompt help rpl)
  180. ;; Move idx and glo up in the list to ensure ?i and ?g shortcuts
  181. (if (member "glo" tags)
  182. (setq tags (cons "glo" (delete "glo" tags))))
  183. (if (member "idx" tags)
  184. (setq tags (cons "idx" (delete "idx" tags))))
  185. ;; Find unique shortcuts for each index.
  186. (while (setq tag (pop tags))
  187. (setq len (length tag)
  188. i -1
  189. val nil)
  190. (catch 'exit
  191. (while (and (< (incf i) len) (null val))
  192. (unless (assq (aref tag i) tag-alist)
  193. (push (list (aref tag i)
  194. tag
  195. (concat (substring tag 0 i)
  196. "[" (substring tag i (incf i)) "]"
  197. (substring tag i)))
  198. tag-alist)
  199. (throw 'exit t)))
  200. (push (list (+ ?0 (incf cnt)) tag
  201. (concat "[" (int-to-string cnt) "]:" tag))
  202. tag-alist)))
  203. (setq tag-alist (nreverse tag-alist))
  204. ;; Compute Prompt and Help strings
  205. (setq prompt
  206. (concat
  207. (format "Select Index%s: "
  208. (if default (format " (Default <%s>)" default) ""))
  209. (mapconcat (lambda(x) (nth 2 x)) tag-alist " ")))
  210. (setq help
  211. (concat "Select an Index\n===============\n"
  212. (if default
  213. (format "[^M] %s (the default)\n" default)
  214. "")
  215. (mapconcat (lambda(x)
  216. (apply 'format "[%c] %s" x))
  217. tag-alist "\n")))
  218. ;; Query the user for an index-tag
  219. (setq rpl (reftex-select-with-char prompt help 3 t))
  220. (message "")
  221. (if (and default (equal rpl ?\C-m))
  222. default
  223. (if (assq rpl tag-alist)
  224. (progn
  225. (reftex-update-default-index (nth 1 (assq rpl tag-alist)))
  226. (nth 1 (assq rpl tag-alist)))
  227. (error "No index tag associated with %c" rpl)))))
  228. (t (error "This should not happen (reftex-index-select-tag)")))))
  229. ;;;###autoload
  230. (defun reftex-index-complete-key (&optional tag optional initial)
  231. ;; Read an index key, with completion.
  232. ;; Restrict completion table on index tag TAG.
  233. ;; OPTIONAL indicates if the arg is optional.
  234. (let* ((table (reftex-sublist-nth
  235. (symbol-value reftex-docstruct-symbol) 6
  236. (lambda(x) (and (eq (car x) 'index)
  237. (string= (nth 1 x) (or tag ""))))
  238. t))
  239. (prompt (concat "Index key" (if optional " (optional)" "") ": "))
  240. (key (completing-read prompt table nil nil initial)))
  241. key))
  242. (defun reftex-index-update-taglist (newtag)
  243. ;; add NEWTAG to the list of available index tags.
  244. (let ((cell (assoc 'index-tags (symbol-value reftex-docstruct-symbol))))
  245. (and newtag (cdr cell) (not (member newtag (cdr cell)))
  246. (push newtag (cdr cell)))))
  247. (define-obsolete-variable-alias
  248. 'reftex-index-map 'reftex-index-mode-map "24.1")
  249. (defvar reftex-index-mode-map
  250. (let ((map (make-sparse-keymap)))
  251. ;; Index map
  252. (define-key map (if (featurep 'xemacs) [(button2)] [(mouse-2)])
  253. 'reftex-index-mouse-goto-line-and-hide)
  254. (define-key map [follow-link] 'mouse-face)
  255. (substitute-key-definition
  256. 'next-line 'reftex-index-next map global-map)
  257. (substitute-key-definition
  258. 'previous-line 'reftex-index-previous map global-map)
  259. (loop for x in
  260. '(("n" . reftex-index-next)
  261. ("p" . reftex-index-previous)
  262. ("?" . reftex-index-show-help)
  263. (" " . reftex-index-view-entry)
  264. ("\C-m" . reftex-index-goto-entry-and-hide)
  265. ("\C-i" . reftex-index-goto-entry)
  266. ("\C-k" . reftex-index-kill)
  267. ("r" . reftex-index-rescan)
  268. ("R" . reftex-index-Rescan)
  269. ("g" . revert-buffer)
  270. ("q" . reftex-index-quit)
  271. ("k" . reftex-index-quit-and-kill)
  272. ("f" . reftex-index-toggle-follow)
  273. ("s" . reftex-index-switch-index-tag)
  274. ("e" . reftex-index-edit)
  275. ("^" . reftex-index-level-up)
  276. ("_" . reftex-index-level-down)
  277. ("}" . reftex-index-restrict-to-section)
  278. ("{" . reftex-index-widen)
  279. (">" . reftex-index-restriction-forward)
  280. ("<" . reftex-index-restriction-backward)
  281. ("(" . reftex-index-toggle-range-beginning)
  282. (")" . reftex-index-toggle-range-end)
  283. ("|" . reftex-index-edit-attribute)
  284. ("@" . reftex-index-edit-visual)
  285. ("*" . reftex-index-edit-key)
  286. ("\C-c=". reftex-index-goto-toc)
  287. ("c" . reftex-index-toggle-context))
  288. do (define-key map (car x) (cdr x)))
  289. (loop for key across "0123456789" do
  290. (define-key map (vector (list key)) 'digit-argument))
  291. (define-key map "-" 'negative-argument)
  292. ;; The capital letters and the exclamation mark
  293. (loop for key across (concat "!" reftex-index-section-letters) do
  294. (define-key map (vector (list key))
  295. (list 'lambda '() '(interactive)
  296. (list 'reftex-index-goto-letter key))))
  297. (easy-menu-define reftex-index-menu map
  298. "Menu for Index buffer"
  299. '("Index"
  300. ["Goto section A-Z"
  301. (message "To go to a section, just press any of: !%s"
  302. reftex-index-section-letters) t]
  303. ["Show Entry" reftex-index-view-entry t]
  304. ["Go To Entry" reftex-index-goto-entry t]
  305. ["Exit & Go To Entry" reftex-index-goto-entry-and-hide t]
  306. ["Table of Contents" reftex-index-goto-toc t]
  307. ["Quit" reftex-index-quit t]
  308. "--"
  309. ("Update"
  310. ["Rebuilt *Index* Buffer" revert-buffer t]
  311. "--"
  312. ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans]
  313. ["Rescan Entire Document" reftex-index-Rescan t])
  314. ("Restrict"
  315. ["Restrict to section" reftex-index-restrict-to-section t]
  316. ["Widen" reftex-index-widen reftex-index-restriction-indicator]
  317. ["Next Section" reftex-index-restriction-forward
  318. reftex-index-restriction-indicator]
  319. ["Previous Section" reftex-index-restriction-backward
  320. reftex-index-restriction-indicator])
  321. ("Edit"
  322. ["Edit Entry" reftex-index-edit t]
  323. ["Edit Key" reftex-index-edit-key t]
  324. ["Edit Attribute" reftex-index-edit-attribute t]
  325. ["Edit Visual" reftex-index-edit-visual t]
  326. "--"
  327. ["Add Parentkey" reftex-index-level-down t]
  328. ["Remove Parentkey " reftex-index-level-up t]
  329. "--"
  330. ["Make Start-of-Range" reftex-index-toggle-range-beginning t]
  331. ["Make End-of-Range" reftex-index-toggle-range-end t]
  332. "--"
  333. ["Kill Entry" reftex-index-kill nil]
  334. "--"
  335. ["Undo" reftex-index-undo nil])
  336. ("Options"
  337. ["Context" reftex-index-toggle-context :style toggle
  338. :selected reftex-index-include-context]
  339. "--"
  340. ["Follow Mode" reftex-index-toggle-follow :style toggle
  341. :selected reftex-index-follow-mode])
  342. "--"
  343. ["Help" reftex-index-show-help t]))
  344. map)
  345. "Keymap used for *Index* buffers.")
  346. (defvar reftex-index-menu)
  347. (defvar reftex-last-index-file nil
  348. "Stores the file name from which `reftex-display-index' was called.")
  349. (defvar reftex-index-tag nil
  350. "Stores the tag of the index in an index buffer.")
  351. (defvar reftex-index-return-marker (make-marker)
  352. "Marker which makes it possible to return from index to old position.")
  353. (defvar reftex-index-restriction-indicator nil)
  354. (defvar reftex-index-restriction-data nil)
  355. (define-derived-mode reftex-index-mode fundamental-mode "RefTeX Index"
  356. "Major mode for managing Index buffers for LaTeX files.
  357. This buffer was created with RefTeX.
  358. Press `?' for a summary of important key bindings, or check the menu.
  359. Here are all local bindings.
  360. \\{reftex-index-mode-map}"
  361. (set (make-local-variable 'revert-buffer-function) 'reftex-index-revert)
  362. (set (make-local-variable 'reftex-index-restriction-data) nil)
  363. (set (make-local-variable 'reftex-index-restriction-indicator) nil)
  364. (setq mode-line-format
  365. (list "---- " 'mode-line-buffer-identification
  366. " " 'global-mode-string
  367. " R<" 'reftex-index-restriction-indicator ">"
  368. " -%-"))
  369. (setq truncate-lines t)
  370. (when (featurep 'xemacs)
  371. ;; XEmacs needs the call to make-local-hook
  372. (make-local-hook 'post-command-hook)
  373. (make-local-hook 'pre-command-hook))
  374. (make-local-variable 'reftex-last-follow-point)
  375. (easy-menu-add reftex-index-menu reftex-index-mode-map)
  376. (add-hook 'post-command-hook 'reftex-index-post-command-hook nil t)
  377. (add-hook 'pre-command-hook 'reftex-index-pre-command-hook nil t))
  378. (defconst reftex-index-help
  379. " AVAILABLE KEYS IN INDEX BUFFER
  380. ==============================
  381. ! A..Z Goto the section of entries starting with this letter.
  382. n / p next-entry / previous-entry
  383. SPC / TAB Show/Goto the corresponding entry in the LaTeX document.
  384. RET Goto the entry and hide the *Index* window (also on mouse-2).
  385. q / k Hide/Kill *Index* buffer.
  386. C-c = Switch to the TOC buffer.
  387. f / c Toggle follow mode / Toggle display of [c]ontext.
  388. g Refresh *Index* buffer.
  389. r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
  390. s Switch to a different index (for documents with multiple indices).
  391. e / C-k Edit/Kill the entry.
  392. * | @ Edit specific part of entry: [*]key [|]attribute [@]visual
  393. With prefix: kill that part.
  394. \( ) Toggle entry's beginning/end of page range property.
  395. _ ^ Add/Remove parent key (to make this item a subitem).
  396. } / { Restrict Index to a single document section / Widen.
  397. < / > When restricted, move restriction to previous/next section.")
  398. ;;;###autoload
  399. (defun reftex-index-show-entry (data &optional no-revisit)
  400. ;; Find an index entry associated with DATA and display it highlighted
  401. ;; in another window. NO-REVISIT means we are not allowed to visit
  402. ;; files for this.
  403. ;; Note: This function just looks for the nearest match of the
  404. ;; context string and may fail if the entry moved and an identical
  405. ;; entry is close to the old position. Frequent rescans make this
  406. ;; safer.
  407. (let* ((file (nth 3 data))
  408. (literal (nth 2 data))
  409. (pos (nth 4 data))
  410. (re (regexp-quote literal))
  411. (match
  412. (cond
  413. ((or (not no-revisit)
  414. (reftex-get-buffer-visiting file))
  415. (switch-to-buffer-other-window
  416. (reftex-get-file-buffer-force file nil))
  417. (goto-char (or pos (point-min)))
  418. (or (looking-at re)
  419. (reftex-nearest-match re (length literal))))
  420. (t (message "%s" reftex-no-follow-message) nil))))
  421. (when match
  422. (goto-char (match-beginning 0))
  423. (recenter '(4))
  424. (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
  425. match))
  426. ;;;###autoload
  427. (defun reftex-display-index (&optional tag overriding-restriction redo
  428. &rest locations)
  429. "Display a buffer with an index compiled from the current document.
  430. When the document has multiple indices, first prompts for the correct one.
  431. When index support is turned off, offer to turn it on.
  432. With one or two `C-u' prefixes, rescan document first.
  433. With prefix 2, restrict index to current document section.
  434. With prefix 3, restrict index to region."
  435. (interactive)
  436. ;; Ensure access to scanning info and rescan buffer if prefix are is '(4).
  437. (let ((current-prefix-arg current-prefix-arg))
  438. (reftex-ensure-index-support t)
  439. (reftex-access-scan-info current-prefix-arg))
  440. (set-marker reftex-index-return-marker (point))
  441. (setq reftex-last-follow-point 1)
  442. ;; Determine the correct index to process
  443. (let* ((docstruct (symbol-value reftex-docstruct-symbol))
  444. (docstruct-symbol reftex-docstruct-symbol)
  445. (index-tag (or tag (reftex-index-select-tag)))
  446. (master (reftex-TeX-master-file))
  447. (calling-file (buffer-file-name))
  448. (restriction
  449. (or overriding-restriction
  450. (and (not redo)
  451. (reftex-get-restriction current-prefix-arg docstruct))))
  452. (locations
  453. ;; See if we are on an index macro as initial position
  454. (or locations
  455. (let* ((what-macro (reftex-what-macro-safe 1))
  456. (macro (car what-macro))
  457. (here-I-am (when (member macro reftex-macros-with-index)
  458. (save-excursion
  459. (goto-char (+ (cdr what-macro)
  460. (length macro)))
  461. (reftex-move-over-touching-args)
  462. (reftex-where-am-I)))))
  463. (if (eq (car (car here-I-am)) 'index)
  464. (list (car here-I-am))))))
  465. buffer-name)
  466. (setq buffer-name (reftex-make-index-buffer-name index-tag))
  467. ;; Goto the buffer and put it into the correct mode
  468. (when (or restriction current-prefix-arg)
  469. (reftex-kill-buffer buffer-name))
  470. (if (get-buffer-window buffer-name)
  471. (select-window (get-buffer-window buffer-name))
  472. (switch-to-buffer buffer-name))
  473. (or (eq major-mode 'reftex-index-mode) (reftex-index-mode))
  474. ;; If the buffer is currently restricted, empty it to force update.
  475. (when reftex-index-restriction-data
  476. (reftex-erase-buffer))
  477. (set (make-local-variable 'reftex-last-index-file) calling-file)
  478. (set (make-local-variable 'reftex-index-tag) index-tag)
  479. (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol)
  480. (if restriction
  481. (setq reftex-index-restriction-indicator (car restriction)
  482. reftex-index-restriction-data (cdr restriction))
  483. (if (not redo)
  484. (setq reftex-index-restriction-indicator nil
  485. reftex-index-restriction-data nil)))
  486. (when (= (buffer-size) 0)
  487. ;; buffer is empty - fill it
  488. (message "Building %s buffer..." buffer-name)
  489. (setq buffer-read-only nil)
  490. (insert (format
  491. "INDEX <%s> on %s
  492. Restriction: <%s>
  493. SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help
  494. ------------------------------------------------------------------------------
  495. "
  496. index-tag (abbreviate-file-name master)
  497. (if (eq (car (car reftex-index-restriction-data)) 'toc)
  498. (nth 2 (car reftex-index-restriction-data))
  499. reftex-index-restriction-indicator)))
  500. (if (reftex-use-fonts)
  501. (put-text-property (point-min) (point)
  502. 'face reftex-index-header-face))
  503. (if (fboundp 'cursor-intangible-mode)
  504. (cursor-intangible-mode 1)
  505. ;; If `cursor-intangible' is not available, fallback on the old
  506. ;; intrusive `intangible' property.
  507. (put-text-property (point-min) (point) 'intangible t))
  508. (add-text-properties (point-min) (point)
  509. '(cursor-intangible t
  510. front-sticky (cursor-intangible)
  511. rear-nonsticky (cursor-intangible)))
  512. (reftex-insert-index docstruct index-tag)
  513. (goto-char (point-min))
  514. (run-hooks 'reftex-display-copied-context-hook)
  515. (message "Building %s buffer...done." buffer-name)
  516. (setq buffer-read-only t))
  517. (and locations (apply 'reftex-find-start-point (point) locations))
  518. (if reftex-index-restriction-indicator
  519. (message "Index restricted: <%s>" reftex-index-restriction-indicator))))
  520. (defun reftex-insert-index (docstruct tag &optional update-one remark)
  521. ;; Insert an index into the current buffer. Entries are from the
  522. ;; DOCSTRUCT.
  523. ;; TAG is the subindex to process.
  524. ;; UPDATE-ONE: When non-nil, delete the entry at point and replace
  525. ;; it with whatever the DOCSTRUCT contains.
  526. ;; REMARK can be a note to add to the entry.
  527. (let* ((all docstruct)
  528. (indent " ")
  529. (context reftex-index-include-context)
  530. (context-indent (concat indent " "))
  531. (section-chars (mapcar 'identity reftex-index-section-letters))
  532. (this-section-char 0)
  533. (font (reftex-use-fonts))
  534. (bor (car reftex-index-restriction-data))
  535. (eor (nth 1 reftex-index-restriction-data))
  536. (mouse-face
  537. (if (memq reftex-highlight-selection '(mouse both))
  538. reftex-mouse-selected-face
  539. nil))
  540. (index-face reftex-label-face)
  541. sublist cell from to first-char)
  542. ;; Make the sublist and sort it
  543. (when bor
  544. (setq all (or (memq bor all) all)))
  545. (while (setq cell (pop all))
  546. (if (eq cell eor)
  547. (setq all nil)
  548. (and (eq (car cell) 'index)
  549. (equal (nth 1 cell) tag)
  550. (push cell sublist))))
  551. (setq sublist (sort (nreverse sublist)
  552. (lambda (a b) (string< (nth 8 a) (nth 8 b)))))
  553. (when update-one
  554. ;; Delete the entry at place
  555. (and (bolp) (forward-char 1))
  556. (delete-region (previous-single-property-change (1+ (point)) :data)
  557. (or (next-single-property-change (point) :data)
  558. (point-max))))
  559. ;; Walk through the list and insert all entries
  560. (while (setq cell (pop sublist))
  561. (unless update-one
  562. (setq first-char (upcase (string-to-char (nth 6 cell))))
  563. (when (and (not (equal first-char this-section-char))
  564. (member first-char section-chars))
  565. ;; There is a new initial letter, so start a new section
  566. (reftex-index-insert-new-letter first-char font)
  567. (setq section-chars (delete first-char section-chars)
  568. this-section-char first-char))
  569. (when (= this-section-char 0)
  570. (setq this-section-char ?!)
  571. (reftex-index-insert-new-letter this-section-char font)))
  572. (setq from (point))
  573. (insert indent (nth 7 cell))
  574. (when font
  575. (setq to (point))
  576. (put-text-property
  577. (- (point) (length (nth 7 cell))) to
  578. 'face index-face)
  579. (goto-char to))
  580. (when (or remark (nth 9 cell))
  581. (and (< (current-column) 40)
  582. ;; FIXME: maybe this is too slow?
  583. (insert (make-string (max (- 40 (current-column)) 0) ?\ )))
  584. (and (nth 9 cell) (insert " " (substring (nth 5 cell) (nth 9 cell))))
  585. (and remark (insert " " remark)))
  586. (insert "\n")
  587. (setq to (point))
  588. (when context
  589. (insert context-indent (nth 2 cell) "\n")
  590. (setq to (point)))
  591. (put-text-property from to :data cell)
  592. (when mouse-face
  593. (put-text-property from (1- to)
  594. 'mouse-face mouse-face))
  595. (goto-char to))))
  596. (defun reftex-index-insert-new-letter (letter &optional font)
  597. ;; Start a new section in the index
  598. (let ((from (point)))
  599. (insert "\n" letter letter letter
  600. "-----------------------------------------------------------------")
  601. (when font
  602. (put-text-property from (point) 'face reftex-index-section-face))
  603. (insert "\n")))
  604. (defun reftex-get-restriction (arg docstruct)
  605. ;; Interpret the prefix ARG and derive index restriction specs.
  606. (let* ((beg (min (point) (or (condition-case nil (mark) (error nil))
  607. (point-max))))
  608. (end (max (point) (or (condition-case nil (mark) (error nil))
  609. (point-min))))
  610. bor eor label here-I-am)
  611. (cond
  612. ((eq arg 2)
  613. (setq here-I-am (car (reftex-where-am-I))
  614. bor (if (eq (car here-I-am) 'toc)
  615. here-I-am
  616. (reftex-last-assoc-before-elt
  617. 'toc here-I-am docstruct))
  618. eor (car (memq (assq 'toc (cdr (memq bor docstruct))) docstruct))
  619. label (nth 6 bor)))
  620. ((eq arg 3)
  621. (save-excursion
  622. (setq label "region")
  623. (goto-char beg)
  624. (setq bor (car (reftex-where-am-I)))
  625. (setq bor (nth 1 (memq bor docstruct)))
  626. (goto-char end)
  627. (setq eor (nth 1 (memq (car (reftex-where-am-I)) docstruct)))))
  628. (t nil))
  629. (if (and label (or bor eor))
  630. (list label bor eor)
  631. nil)))
  632. (defun reftex-index-pre-command-hook ()
  633. ;; Used as pre command hook in *Index* buffer
  634. (reftex-unhighlight 0)
  635. (reftex-unhighlight 1))
  636. (defun reftex-index-post-command-hook ()
  637. ;; Used in the post-command-hook for the *Index* buffer
  638. ;; FIXME: Lots of redundancy with reftex-toc-post-command-hook!
  639. (when (get-text-property (point) :data)
  640. (and (> (point) 1) ;FIXME: Is this point-min or do we care about narrowing?
  641. (not (get-text-property (point) 'cursor-intangible))
  642. (memq reftex-highlight-selection '(cursor both))
  643. (reftex-highlight 1
  644. (or (previous-single-property-change (1+ (point)) :data)
  645. (point-min))
  646. (or (next-single-property-change (point) :data)
  647. (point-max)))))
  648. (if (integerp reftex-index-follow-mode)
  649. ;; Remove delayed action
  650. (setq reftex-index-follow-mode t)
  651. (and reftex-index-follow-mode
  652. (not (equal reftex-last-follow-point (point)))
  653. ;; Show context in other window
  654. (setq reftex-last-follow-point (point))
  655. (condition-case nil
  656. (reftex-index-visit-location nil (not reftex-revisit-to-follow))
  657. (error t)))))
  658. (defun reftex-index-show-help ()
  659. "Show a summary of special key bindings."
  660. (interactive)
  661. (with-output-to-temp-buffer "*RefTeX Help*"
  662. (princ reftex-index-help))
  663. (reftex-enlarge-to-fit "*RefTeX Help*" t)
  664. ;; If follow mode is active, arrange to delay it one command
  665. (if reftex-index-follow-mode
  666. (setq reftex-index-follow-mode 1)))
  667. (defun reftex-index-next (&optional arg)
  668. "Move to next selectable item."
  669. (interactive "p")
  670. (setq reftex-callback-fwd t)
  671. (or (eobp) (forward-char 1))
  672. (goto-char (or (next-single-property-change (point) :data)
  673. (point)))
  674. (unless (get-text-property (point) :data)
  675. (goto-char (or (next-single-property-change (point) :data)
  676. (point)))))
  677. (defun reftex-index-previous (&optional arg)
  678. "Move to previous selectable item."
  679. (interactive "p")
  680. (setq reftex-callback-fwd nil)
  681. (goto-char (or (previous-single-property-change (point) :data)
  682. (point)))
  683. (unless (get-text-property (point) :data)
  684. (goto-char (or (previous-single-property-change (point) :data)
  685. (point)))))
  686. (defun reftex-index-toggle-follow ()
  687. "Toggle follow (other window follows with context)."
  688. (interactive)
  689. (setq reftex-last-follow-point -1)
  690. (setq reftex-index-follow-mode (not reftex-index-follow-mode)))
  691. (defun reftex-index-toggle-context ()
  692. "Toggle inclusion of label context in *Index* buffer.
  693. Label context is only displayed when the labels are there as well."
  694. (interactive)
  695. (setq reftex-index-include-context (not reftex-index-include-context))
  696. (reftex-index-revert))
  697. (defun reftex-index-view-entry ()
  698. "View document location in other window."
  699. (interactive)
  700. (reftex-index-visit-location))
  701. (defun reftex-index-goto-entry-and-hide ()
  702. "Go to document location in other window. Hide the *Index* window."
  703. (interactive)
  704. (reftex-index-visit-location 'hide))
  705. (defun reftex-index-goto-entry ()
  706. "Go to document location in other window. *Index* window stays."
  707. (interactive)
  708. (reftex-index-visit-location t))
  709. (defun reftex-index-mouse-goto-line-and-hide (ev)
  710. "Go to document location in other window. Hide the *Index* window."
  711. (interactive "e")
  712. (mouse-set-point ev)
  713. (reftex-index-visit-location 'hide))
  714. (defun reftex-index-quit ()
  715. "Hide the *Index* window and do not move point."
  716. (interactive)
  717. (or (one-window-p) (delete-window))
  718. (switch-to-buffer (marker-buffer reftex-index-return-marker))
  719. (goto-char (or (marker-position reftex-index-return-marker) (point))))
  720. (defun reftex-index-quit-and-kill ()
  721. "Kill the *Index* buffer."
  722. (interactive)
  723. (kill-buffer (current-buffer))
  724. (or (one-window-p) (delete-window))
  725. (switch-to-buffer (marker-buffer reftex-index-return-marker))
  726. (goto-char (or (marker-position reftex-index-return-marker) (point))))
  727. (defun reftex-index-goto-toc (&rest ignore)
  728. "Switch to the table of contents of the current document.
  729. The function will go to the section where the entry at point was defined."
  730. (interactive)
  731. (if (get-text-property (point) :data)
  732. (reftex-index-goto-entry)
  733. (switch-to-buffer (marker-buffer reftex-index-return-marker)))
  734. (delete-other-windows)
  735. (reftex-toc))
  736. (defun reftex-index-rescan (&rest ignore)
  737. "Regenerate the *Index* buffer after reparsing file of section at point."
  738. (interactive)
  739. (let ((index-tag reftex-index-tag))
  740. (if (and reftex-enable-partial-scans
  741. (null current-prefix-arg))
  742. (let* ((data (get-text-property (point) :data))
  743. (file (nth 3 data))
  744. (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
  745. (if (not file)
  746. (error "Don't know which file to rescan. Try `C-u r'")
  747. (switch-to-buffer (reftex-get-file-buffer-force file))
  748. (setq current-prefix-arg '(4))
  749. (reftex-display-index index-tag nil 'redo line)))
  750. (reftex-index-Rescan))
  751. (reftex-kill-temporary-buffers)))
  752. (defun reftex-index-Rescan (&rest ignore)
  753. "Regenerate the *Index* buffer after reparsing the entire document."
  754. (interactive)
  755. (let ((index-tag reftex-index-tag)
  756. (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
  757. (switch-to-buffer
  758. (reftex-get-file-buffer-force reftex-last-index-file))
  759. (setq current-prefix-arg '(16))
  760. (reftex-display-index index-tag nil 'redo line)))
  761. (defun reftex-index-revert (&rest ignore)
  762. "Regenerate the *Index* from the internal lists. No reparsing os done."
  763. (interactive)
  764. (let ((buf (current-buffer))
  765. (index-tag reftex-index-tag)
  766. (data (get-text-property (point) :data))
  767. (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
  768. (switch-to-buffer
  769. (reftex-get-file-buffer-force reftex-last-index-file))
  770. (reftex-erase-buffer buf)
  771. (setq current-prefix-arg nil
  772. reftex-last-follow-point 1)
  773. (reftex-display-index index-tag nil 'redo data line)))
  774. (defun reftex-index-switch-index-tag (&rest ignore)
  775. "Switch to a different index of the same document."
  776. (interactive)
  777. (switch-to-buffer
  778. (reftex-get-file-buffer-force reftex-last-index-file))
  779. (setq current-prefix-arg nil)
  780. (reftex-display-index nil nil 'redo))
  781. (defun reftex-index-restrict-to-section (&optional force)
  782. "Restrict index to entries defined in same document sect. as entry at point."
  783. ;; Optional FORCE means, even if point is not on an index entry.
  784. (interactive)
  785. (let* ((data (get-text-property (point) :data))
  786. (docstruct (symbol-value reftex-docstruct-symbol))
  787. bor eor)
  788. (if (and (not data) force)
  789. (setq data (assq 'toc docstruct)))
  790. (when data
  791. (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
  792. eor (car (memq (assq 'toc (cdr (memq bor docstruct)))
  793. docstruct))
  794. reftex-index-restriction-data (list bor eor)
  795. reftex-index-restriction-indicator (nth 6 bor) )))
  796. (reftex-index-revert))
  797. (defun reftex-index-widen (&rest ignore)
  798. "Show the unrestricted index (all entries)."
  799. (interactive)
  800. (setq reftex-index-restriction-indicator nil
  801. reftex-index-restriction-data nil)
  802. (reftex-index-revert)
  803. (message "Index widened"))
  804. (defun reftex-index-restriction-forward (&rest ignore)
  805. "Restrict to previous section.
  806. When index is currently unrestricted, restrict it to a section.
  807. When index is restricted, select the next section as restriction criterion."
  808. (interactive)
  809. (let* ((docstruct (symbol-value reftex-docstruct-symbol))
  810. (bor (nth 1 reftex-index-restriction-data)))
  811. (if (or (not bor)
  812. (not (eq (car bor) 'toc)))
  813. (reftex-index-restrict-to-section t)
  814. (setq reftex-index-restriction-indicator (nth 6 bor)
  815. reftex-index-restriction-data
  816. (list bor
  817. (car (memq (assq 'toc (cdr (memq bor docstruct)))
  818. docstruct))))
  819. (reftex-index-revert))))
  820. (defun reftex-index-restriction-backward (&rest ignore)
  821. "Restrict to next section.
  822. When index is currently unrestricted, restrict it to a section.
  823. When index is restricted, select the previous section as restriction criterion."
  824. (interactive)
  825. (let* ((docstruct (symbol-value reftex-docstruct-symbol))
  826. (eor (car reftex-index-restriction-data))
  827. (bor (reftex-last-assoc-before-elt 'toc eor docstruct t)))
  828. (if (or (not bor)
  829. (not (eq (car bor) 'toc)))
  830. (reftex-index-restrict-to-section t)
  831. (setq reftex-index-restriction-indicator (nth 6 bor)
  832. reftex-index-restriction-data
  833. (list bor eor))
  834. (reftex-index-revert))))
  835. (defun reftex-index-visit-location (&optional final no-revisit)
  836. ;; Visit the tex file corresponding to the index entry on the current line.
  837. ;; If FINAL is t, stay there
  838. ;; If FINAL is 'hide, hide the *Index* window.
  839. ;; Otherwise, move cursor back into *Index* window.
  840. ;; NO-REVISIT means don't visit files, just use live buffers.
  841. (let* ((data (get-text-property (point) :data))
  842. (index-window (selected-window))
  843. show-window show-buffer match)
  844. (unless data (error "Don't know which index entry to visit"))
  845. (if (eq (car data) 'index)
  846. (setq match (reftex-index-show-entry data no-revisit)))
  847. (setq show-window (selected-window)
  848. show-buffer (current-buffer))
  849. (unless match
  850. (select-window index-window)
  851. (error "Cannot find location"))
  852. (select-window index-window)
  853. ;; Use the `final' parameter to decide what to do next
  854. (cond
  855. ((eq final t)
  856. (reftex-unhighlight 0)
  857. (select-window show-window))
  858. ((eq final 'hide)
  859. (reftex-unhighlight 0)
  860. (or (one-window-p) (delete-window))
  861. (switch-to-buffer show-buffer))
  862. (t nil))))
  863. (defun reftex-index-analyze-entry (data)
  864. ;; This splits the index context so that key, attribute and visual
  865. ;; values are accessible individually.
  866. (interactive)
  867. (let* ((arg (nth 5 data))
  868. (context (nth 2 data))
  869. (sc reftex-index-special-chars)
  870. (boa (if (string-match (regexp-quote (concat "{" arg "}")) context)
  871. (1+ (match-beginning 0))
  872. (error "Something is wrong here")))
  873. (eoa (1- (match-end 0)))
  874. (boactual (if (string-match (concat "[^" (nth 3 sc) "]" (nth 2 sc))
  875. context boa)
  876. (1+ (match-beginning 0))
  877. eoa))
  878. (boattr (if (string-match (concat "[^" (nth 3 sc) "]" (nth 1 sc))
  879. context boa)
  880. (1+ (match-beginning 0))
  881. boactual))
  882. (pre (substring context 0 boa))
  883. (key (substring context boa boattr))
  884. (attr (substring context boattr boactual))
  885. (actual (substring context boactual eoa))
  886. (post (substring context eoa)))
  887. (list pre key attr actual post)))
  888. (defun reftex-index-edit ()
  889. "Edit the index entry at point."
  890. (interactive)
  891. (let* ((data (get-text-property (point) :data))
  892. old new)
  893. (unless data (error "Don't know which index entry to edit"))
  894. (reftex-index-view-entry)
  895. (setq old (nth 2 data) new (read-string "Edit: " old))
  896. (reftex-index-change-entry new)))
  897. (defun reftex-index-toggle-range-beginning ()
  898. "Toggle the page range start attribute `|('."
  899. (interactive)
  900. (let* ((data (get-text-property (point) :data))
  901. (bor (concat (nth 1 reftex-index-special-chars) "("))
  902. new analyze attr)
  903. (unless data (error "Don't know which index entry to edit"))
  904. (setq analyze (reftex-index-analyze-entry data)
  905. attr (nth 2 analyze))
  906. (setf (nth 2 analyze) (if (string= attr bor) "" bor))
  907. (setq new (apply 'concat analyze))
  908. (reftex-index-change-entry
  909. new (if (string= (nth 2 analyze) bor)
  910. "Entry is now START-OF-PAGE-RANGE"
  911. "START-OF-PAGE-RANGE canceled"))))
  912. (defun reftex-index-toggle-range-end ()
  913. "Toggle the page-range-end attribute `|)'."
  914. (interactive)
  915. (let* ((data (get-text-property (point) :data))
  916. (eor (concat (nth 1 reftex-index-special-chars) ")"))
  917. new analyze attr)
  918. (unless data (error "Don't know which index entry to edit"))
  919. (setq analyze (reftex-index-analyze-entry data)
  920. attr (nth 2 analyze))
  921. (setf (nth 2 analyze) (if (string= attr eor) "" eor))
  922. (setq new (apply 'concat analyze))
  923. (reftex-index-change-entry
  924. new (if (string= (nth 2 analyze) eor)
  925. "Entry is now END-OF-PAGE-RANGE"
  926. "END-OF-PAGE-RANGE canceled"))))
  927. (defun reftex-index-edit-key ()
  928. "Edit the KEY part of the index entry."
  929. (interactive)
  930. (reftex-index-edit-part nil 1 "" "Key: " t))
  931. (defun reftex-index-edit-attribute (&optional arg)
  932. "EDIT the ATTRIBUTE part of the entry. With arg: remove entire ATTRIBUTE."
  933. (interactive "P")
  934. (reftex-index-edit-part arg 2 (nth 1 reftex-index-special-chars)
  935. "Attribute: "))
  936. (defun reftex-index-edit-visual (&optional arg)
  937. "EDIT the VISUAL part of the entry. With arg: remove entire VISUAL string."
  938. (interactive "P")
  939. (reftex-index-edit-part arg 3 (nth 2 reftex-index-special-chars) "Visual: "))
  940. (defun reftex-index-edit-part (arg n initial prompt &optional dont-allow-empty)
  941. ;; This function does the work for all partial editing commands
  942. (let* ((data (get-text-property (point) :data))
  943. new analyze opart npart)
  944. (unless data (error "Don't know which index entry to edit"))
  945. ;; Analyze the whole context string
  946. (setq analyze (reftex-index-analyze-entry data)
  947. opart (nth n analyze))
  948. (and (> (length opart) 0) (setq opart (substring opart 1)))
  949. ;; Have the user editing the part
  950. (setq npart (if arg "" (read-string (concat prompt initial) opart)))
  951. ;; Tests:
  952. (cond ((string= npart opart)
  953. (error "Not changed"))
  954. ((string= npart "")
  955. (if dont-allow-empty
  956. (error "Invalid value")
  957. (setf (nth n analyze) npart)))
  958. (t (setf (nth n analyze) (concat initial npart))))
  959. (setq new (apply 'concat analyze))
  960. ;; Change the entry and insert the changed version into the index.
  961. (reftex-index-change-entry
  962. new (if (string= npart "")
  963. (format "Deleted: %s" opart)
  964. (format "New value is: %s" npart)))))
  965. (defun reftex-index-level-down ()
  966. "Make index entry a subitem of another entry."
  967. (interactive)
  968. (let* ((data (get-text-property (point) :data))
  969. (docstruct (symbol-value reftex-docstruct-symbol))
  970. old new prefix key)
  971. (unless data (error "Don't know which index entry to change"))
  972. (setq old (nth 2 data)
  973. key (nth 6 data)
  974. prefix (completing-read
  975. "Prefix: "
  976. (reftex-sublist-nth
  977. docstruct 6
  978. (lambda (x)
  979. (and (eq (car x) 'index)
  980. (string= (nth 1 x) reftex-index-tag))) t)))
  981. (unless (string-match
  982. (concat (regexp-quote (car reftex-index-special-chars)) "\\'")
  983. prefix)
  984. (setq prefix (concat prefix (car reftex-index-special-chars))))
  985. (if (string-match (regexp-quote key) old)
  986. (setq new (replace-match (concat prefix key) t t old))
  987. (error "Cannot construct new index key"))
  988. (reftex-index-change-entry new (format "Added prefix: %s" prefix))))
  989. (defun reftex-index-level-up ()
  990. "Remove the highest level of a hierarchical index entry."
  991. (interactive)
  992. (let* ((data (get-text-property (point) :data))
  993. old new prefix)
  994. (unless data (error "Don't know which entry to change"))
  995. (setq old (nth 2 data))
  996. (if (string-match (concat "{\\([^" (nth 0 reftex-index-special-chars) "]*"
  997. "[^" (nth 3 reftex-index-special-chars) "]"
  998. (regexp-quote (nth 0 reftex-index-special-chars))
  999. "\\)")
  1000. old)
  1001. (setq prefix (substring old (match-beginning 1) (match-end 1))
  1002. new (concat (substring old 0 (match-beginning 1))
  1003. (substring old (match-end 1))))
  1004. (error "Entry is not a subitem"))
  1005. (reftex-index-change-entry new (format "Removed prefix: %s" prefix))))
  1006. (defun reftex-index-kill ()
  1007. "FIXME: Not yet implemented"
  1008. (interactive)
  1009. (error "This function is currently not implemented"))
  1010. (defun reftex-index-undo ()
  1011. "FIXME: Not yet implemented"
  1012. (interactive)
  1013. (error "This function is currently not implemented"))
  1014. (defun reftex-index-change-entry (new &optional message)
  1015. ;; Change the full context string of the index entry at point to
  1016. ;; NEW. This actually edits the buffer where the entry is defined.
  1017. (let* ((data (get-text-property (point) :data))
  1018. old beg end info)
  1019. (unless data (error "Cannot change entry"))
  1020. (reftex-index-view-entry)
  1021. (setq beg (match-beginning 0) end (match-end 0))
  1022. (setq old (nth 2 data))
  1023. (and (equal old new) (error "Entry unchanged"))
  1024. (with-current-buffer (get-file-buffer (nth 3 data))
  1025. (goto-char beg)
  1026. (unless (looking-at (regexp-quote old))
  1027. (error "This should not happen (reftex-index-change-entry)"))
  1028. (delete-region beg end)
  1029. (insert new)
  1030. (goto-char (1- beg))
  1031. (when (and (re-search-forward (reftex-everything-regexp) nil t)
  1032. (match-end 10)
  1033. (< (abs (- (match-beginning 10) beg)) (length new))
  1034. (setq info (reftex-index-info-safe buffer-file-name)))
  1035. (setcdr data (cdr info))))
  1036. (let ((buffer-read-only nil))
  1037. (save-excursion
  1038. (reftex-insert-index (list data) reftex-index-tag t
  1039. "EDITED")))
  1040. (setq reftex-last-follow-point 1)
  1041. (and message (message "%s" message))))
  1042. (defun reftex-index-goto-letter (char)
  1043. "Go to the CHAR section in the index."
  1044. (let ((pos (point))
  1045. (case-fold-search nil))
  1046. (goto-char (point-min))
  1047. (forward-line 2)
  1048. (if (re-search-forward (concat "^" (char-to-string char)) nil t)
  1049. (progn
  1050. (beginning-of-line)
  1051. (recenter 0)
  1052. (reftex-index-next))
  1053. (goto-char pos)
  1054. (if (eq char ?!)
  1055. (error "This <%s> index does not contain entries sorted before the letters"
  1056. reftex-index-tag)
  1057. (error "This <%s> index does not contain entries starting with `%c'"
  1058. reftex-index-tag char)))))
  1059. ;;----------------------------------------------------------------------
  1060. ;; The Index Phrases File
  1061. ;; Some constants and variables
  1062. (defconst reftex-index-phrases-comment-regexp "^[ \t]*%.*"
  1063. "Regular expression to match comment lines in phrases buffer")
  1064. (defconst reftex-index-phrases-macrodef-regexp
  1065. "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\( *\t[ \t]*\\)\\([^\t]*[^ \t]\\)\\( *\t[ \t]*\\)\\(\\S-+\\)"
  1066. "Regular expression to match macro definition lines the phrases buffer.")
  1067. ;(defconst reftex-index-phrases-macrodef-regexp
  1068. ; "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\([ \t]*\\)\\([^\t]*[^ \t]\\)\\([ \t]*\\)\\(nil\\|t\\)[ \t]*$"
  1069. ; "Regular expression to match macro definition lines the phrases buffer.
  1070. ;This version would allow just spaces as separators.")
  1071. (defconst reftex-index-phrases-phrase-regexp1
  1072. "^\\(\\S-?\\)\\(\t\\)\\([^\t\n]*\\S-\\)\\([ \t]*\\)$"
  1073. "Regular expression matching phrases which have no separate index key.")
  1074. (defconst reftex-index-phrases-phrase-regexp2
  1075. "^\\(\\S-?\\)\\(\t\\)\\([^\t]*\\S-\\)\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)[ \t]*$"
  1076. "Regular expression matching phrases which have a separate index key.")
  1077. (defconst reftex-index-phrases-phrase-regexp12
  1078. "^\\(\\S-?\\)\\(\t\\)\\([^\n\t]*\\S-\\)\\(\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)\\)?[ \t]*$"
  1079. "Regular expression matching all types of phrase lines.")
  1080. (defvar reftex-index-phrases-macro-data nil
  1081. "Alist containing the information taken from the macro definition lines.
  1082. This gets refreshed in every phrases command.")
  1083. (defvar reftex-index-phrases-files nil
  1084. "List of document files relevant for the phrases file.")
  1085. (defvar reftex-index-phrases-font-lock-keywords nil
  1086. "Font lock keywords for reftex-index-phrases-mode.")
  1087. (defvar reftex-index-phrases-font-lock-defaults nil
  1088. "Font lock defaults for reftex-index-phrases-mode.")
  1089. (define-obsolete-variable-alias
  1090. 'reftex-index-phrases-map 'reftex-index-phrases-mode-map "24.1")
  1091. (defvar reftex-index-phrases-mode-map
  1092. (let ((map (make-sparse-keymap)))
  1093. ;; Keybindings and Menu for phrases buffer
  1094. (loop for x in
  1095. '(("\C-c\C-c" . reftex-index-phrases-save-and-return)
  1096. ("\C-c\C-x" . reftex-index-this-phrase)
  1097. ("\C-c\C-f" . reftex-index-next-phrase)
  1098. ("\C-c\C-r" . reftex-index-region-phrases)
  1099. ("\C-c\C-a" . reftex-index-all-phrases)
  1100. ("\C-c\C-d" . reftex-index-remaining-phrases)
  1101. ("\C-c\C-s" . reftex-index-sort-phrases)
  1102. ("\C-c\C-n" . reftex-index-new-phrase)
  1103. ("\C-c\C-m" . reftex-index-phrases-set-macro-key)
  1104. ("\C-c\C-i" . reftex-index-phrases-info)
  1105. ("\C-c\C-t" . reftex-index-find-next-conflict-phrase)
  1106. ("\C-i" . self-insert-command))
  1107. do (define-key map (car x) (cdr x)))
  1108. (easy-menu-define reftex-index-phrases-menu map
  1109. "Menu for Phrases buffer"
  1110. '("Phrases"
  1111. ["New Phrase" reftex-index-new-phrase t]
  1112. ["Set Phrase Macro" reftex-index-phrases-set-macro-key t]
  1113. ["Recreate File Header" reftex-index-initialize-phrases-buffer t]
  1114. "--"
  1115. ("Sort Phrases"
  1116. ["Sort" reftex-index-sort-phrases t]
  1117. "--"
  1118. "Sort Options"
  1119. ["by Search Phrase" (setq reftex-index-phrases-sort-prefers-entry nil)
  1120. :style radio :selected (not reftex-index-phrases-sort-prefers-entry)]
  1121. ["by Index Entry" (setq reftex-index-phrases-sort-prefers-entry t)
  1122. :style radio :selected reftex-index-phrases-sort-prefers-entry]
  1123. ["in Blocks" (setq reftex-index-phrases-sort-in-blocks
  1124. (not reftex-index-phrases-sort-in-blocks))
  1125. :style toggle :selected reftex-index-phrases-sort-in-blocks])
  1126. ["Describe Phrase" reftex-index-phrases-info t]
  1127. ["Next Phrase Conflict" reftex-index-find-next-conflict-phrase t]
  1128. "--"
  1129. ("Find and Index in Document"
  1130. ["Current Phrase" reftex-index-this-phrase t]
  1131. ["Next Phrase" reftex-index-next-phrase t]
  1132. ["Current and Following" reftex-index-remaining-phrases t]
  1133. ["Region Phrases" reftex-index-region-phrases t]
  1134. ["All Phrases" reftex-index-all-phrases t]
  1135. "--"
  1136. "Options"
  1137. ["Match Whole Words" (setq reftex-index-phrases-search-whole-words
  1138. (not reftex-index-phrases-search-whole-words))
  1139. :style toggle :selected reftex-index-phrases-search-whole-words]
  1140. ["Case Sensitive Search" (setq reftex-index-phrases-case-fold-search
  1141. (not reftex-index-phrases-case-fold-search))
  1142. :style toggle :selected (not
  1143. reftex-index-phrases-case-fold-search)]
  1144. ["Wrap Long Lines" (setq reftex-index-phrases-wrap-long-lines
  1145. (not reftex-index-phrases-wrap-long-lines))
  1146. :style toggle :selected reftex-index-phrases-wrap-long-lines]
  1147. ["Skip Indexed Matches" (setq reftex-index-phrases-skip-indexed-matches
  1148. (not reftex-index-phrases-skip-indexed-matches))
  1149. :style toggle :selected reftex-index-phrases-skip-indexed-matches])
  1150. "--"
  1151. ["Save and Return" reftex-index-phrases-save-and-return t]))
  1152. map)
  1153. "Keymap used for *toc* buffer.")
  1154. (defvar reftex-index-phrases-syntax-table
  1155. (let ((table (make-syntax-table)))
  1156. (modify-syntax-entry ?\" "." table)
  1157. table)
  1158. "Syntax table for RefTeX Index Phrases mode.")
  1159. ;;;###autoload
  1160. (defun reftex-index-phrase-selection-or-word (arg)
  1161. "Add current selection or word at point to the phrases buffer.
  1162. When you are in transient-mark-mode and the region is active, the
  1163. selection will be used - otherwise the word at point.
  1164. You get a chance to edit the entry in the phrases buffer - finish with
  1165. `C-c C-c'."
  1166. (interactive "P")
  1167. (set-marker reftex-index-return-marker (point))
  1168. (reftex-index-selection-or-word arg 'phrase)
  1169. (if (eq major-mode 'reftex-index-phrases-mode)
  1170. (message "%s"
  1171. (substitute-command-keys
  1172. "Return to LaTeX with \\[reftex-index-phrases-save-and-return]"))))
  1173. ;;;###autoload
  1174. (defun reftex-index-visit-phrases-buffer ()
  1175. "Switch to the phrases buffer, initialize if empty."
  1176. (interactive)
  1177. (reftex-access-scan-info)
  1178. (set-marker reftex-index-return-marker (point))
  1179. (let* ((master (reftex-TeX-master-file))
  1180. (name (concat (file-name-sans-extension master)
  1181. reftex-index-phrase-file-extension)))
  1182. (find-file name)
  1183. (unless (eq major-mode 'reftex-index-phrases-mode)
  1184. (reftex-index-phrases-mode))
  1185. (if (= (buffer-size) 0)
  1186. (reftex-index-initialize-phrases-buffer master))))
  1187. (defun reftex-index-initialize-phrases-buffer (&optional master)
  1188. "Initialize the phrases buffer by creating the header.
  1189. If the buffer is non-empty, delete the old header first."
  1190. (interactive)
  1191. (let* ((case-fold-search t)
  1192. (default-key (car reftex-index-default-macro))
  1193. (default-macro (nth 1 (assoc default-key
  1194. reftex-key-to-index-macro-alist)))
  1195. (macro-alist
  1196. (sort (copy-sequence reftex-index-macro-alist)
  1197. (lambda (a b) (equal (car a) default-macro))))
  1198. macro entry key repeat)
  1199. (if master (set (make-local-variable 'TeX-master)
  1200. (file-name-nondirectory master)))
  1201. (when (> (buffer-size) 0)
  1202. (goto-char 1)
  1203. (set-mark (point))
  1204. (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t)
  1205. (end-of-line))
  1206. (beginning-of-line 2)
  1207. (if (looking-at reftex-index-phrases-comment-regexp)
  1208. (beginning-of-line 2))
  1209. (while (looking-at "^[ \t]*$")
  1210. (beginning-of-line 2))
  1211. (if (featurep 'xemacs)
  1212. (zmacs-activate-region)
  1213. (setq mark-active t))
  1214. (if (yes-or-no-p "Delete and rebuild header? ")
  1215. (delete-region (point-min) (point))))
  1216. ;; Insert the mode line
  1217. (insert
  1218. (format "%% -*- mode: reftex-index-phrases; TeX-master: \"%s\" -*-\n"
  1219. (file-name-nondirectory (reftex-index-phrase-tex-master))))
  1220. ;; Insert the macro definitions
  1221. (insert "% Key Macro Format Repeat\n")
  1222. (insert "%---------------------------------------------------------------------\n")
  1223. (while (setq entry (pop macro-alist))
  1224. (setq macro (car entry)
  1225. repeat (nth 7 entry)
  1226. key (car (delq nil (mapcar (lambda (x) (if (equal (nth 1 x) macro)
  1227. (car x)
  1228. nil))
  1229. reftex-key-to-index-macro-alist))))
  1230. (insert (format ">>>INDEX_MACRO_DEFINITION:\t%s\t%-20s\t%s\n"
  1231. (char-to-string key) (concat macro "{%s}")
  1232. (if repeat "t" "nil"))))
  1233. (insert "%---------------------------------------------------------------------\n\n\n")))
  1234. (defvar TeX-master)
  1235. (defun reftex-index-phrase-tex-master (&optional dir)
  1236. "Return the name of the master file associated with a phrase buffer."
  1237. (if (and (boundp 'TeX-master)
  1238. (local-variable-p 'TeX-master (current-buffer))
  1239. (stringp TeX-master))
  1240. ;; We have a local variable which tells us which file to use
  1241. (expand-file-name TeX-master dir)
  1242. ;; have to guess
  1243. (concat (file-name-sans-extension (buffer-file-name)) ".tex")))
  1244. (defun reftex-index-phrases-save-and-return ()
  1245. "Return to where the `reftex-index-phrase-selection-or-word' was called."
  1246. (interactive)
  1247. (save-buffer)
  1248. (switch-to-buffer (marker-buffer reftex-index-return-marker))
  1249. (goto-char (or (marker-position reftex-index-return-marker) (point))))
  1250. (defvar reftex-index-phrases-menu)
  1251. (defvar reftex-index-phrases-marker)
  1252. (defvar reftex-index-phrases-restrict-file nil)
  1253. ;; NB this is a global autoload - see reftex.el.
  1254. ;;;###autoload
  1255. (define-derived-mode reftex-index-phrases-mode fundamental-mode "Phrases"
  1256. "Major mode for managing the Index phrases of a LaTeX document.
  1257. This buffer was created with RefTeX.
  1258. To insert new phrases, use
  1259. - `C-c \\' in the LaTeX document to copy selection or word
  1260. - `\\[reftex-index-new-phrase]' in the phrases buffer.
  1261. To index phrases use one of:
  1262. \\[reftex-index-this-phrase] index current phrase
  1263. \\[reftex-index-next-phrase] index next phrase (or N with prefix arg)
  1264. \\[reftex-index-all-phrases] index all phrases
  1265. \\[reftex-index-remaining-phrases] index current and following phrases
  1266. \\[reftex-index-region-phrases] index the phrases in the region
  1267. You can sort the phrases in this buffer with \\[reftex-index-sort-phrases].
  1268. To display information about the phrase at point, use \\[reftex-index-phrases-info].
  1269. For more information see the RefTeX User Manual.
  1270. Here are all local bindings.
  1271. \\{reftex-index-phrases-mode-map}"
  1272. :syntax-table reftex-index-phrases-syntax-table
  1273. (set (make-local-variable 'font-lock-defaults)
  1274. reftex-index-phrases-font-lock-defaults)
  1275. (easy-menu-add reftex-index-phrases-menu reftex-index-phrases-mode-map)
  1276. (set (make-local-variable 'reftex-index-phrases-marker) (make-marker)))
  1277. ;; (add-hook 'reftex-index-phrases-mode-hook 'turn-on-font-lock)
  1278. ;; Font Locking stuff
  1279. (let ((ss (if (featurep 'xemacs) 'secondary-selection ''secondary-selection)))
  1280. (setq reftex-index-phrases-font-lock-keywords
  1281. (list
  1282. (cons reftex-index-phrases-comment-regexp 'font-lock-comment-face)
  1283. (list reftex-index-phrases-macrodef-regexp
  1284. '(1 font-lock-type-face)
  1285. '(2 font-lock-keyword-face)
  1286. (list 3 ss)
  1287. '(4 font-lock-function-name-face)
  1288. (list 5 ss)
  1289. '(6 font-lock-string-face))
  1290. (list reftex-index-phrases-phrase-regexp1
  1291. '(1 font-lock-keyword-face)
  1292. (list 2 ss)
  1293. '(3 font-lock-string-face)
  1294. (list 4 ss))
  1295. (list reftex-index-phrases-phrase-regexp2
  1296. '(1 font-lock-keyword-face)
  1297. (list 2 ss)
  1298. '(3 font-lock-string-face)
  1299. (list 4 ss)
  1300. '(5 font-lock-function-name-face))
  1301. (cons "^\t$" ss)))
  1302. (setq reftex-index-phrases-font-lock-defaults
  1303. '((reftex-index-phrases-font-lock-keywords)
  1304. nil t nil beginning-of-line))
  1305. (put 'reftex-index-phrases-mode 'font-lock-defaults
  1306. reftex-index-phrases-font-lock-defaults) ; XEmacs
  1307. )
  1308. (defun reftex-index-next-phrase (&optional arg)
  1309. "Index the next ARG phrases in the phrases buffer."
  1310. (interactive "p")
  1311. (reftex-index-phrases-parse-header t)
  1312. (while (> arg 0)
  1313. (decf arg)
  1314. (end-of-line)
  1315. (if (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
  1316. (progn
  1317. (goto-char (match-beginning 0))
  1318. (reftex-index-this-phrase 'slave))
  1319. (error "No more phrase lines after point"))))
  1320. (defun reftex-index-this-phrase (&optional slave)
  1321. "Index the phrase in the current line.
  1322. Does a global search and replace in the entire document. At each
  1323. match, the user will be asked to confirm the replacement."
  1324. (interactive)
  1325. (if (not slave) (reftex-index-phrases-parse-header t))
  1326. (save-excursion
  1327. (beginning-of-line)
  1328. (cond ((looking-at reftex-index-phrases-comment-regexp)
  1329. (if (not slave) (error "Comment line")))
  1330. ((looking-at "^[ \t]*$")
  1331. (if (not slave) (error "Empty line")))
  1332. ((looking-at reftex-index-phrases-macrodef-regexp)
  1333. (if (not slave) (error "Macro definition line")))
  1334. ((looking-at reftex-index-phrases-phrase-regexp12)
  1335. ;; This is a phrase
  1336. (let* ((char (if (not (equal (match-string 1) ""))
  1337. (string-to-char (match-string 1))))
  1338. (phrase (match-string 3))
  1339. (index-key (match-string 6))
  1340. (macro-data (cdr (if (null char)
  1341. (car reftex-index-phrases-macro-data)
  1342. (assoc char reftex-index-phrases-macro-data))))
  1343. (macro-fmt (car macro-data))
  1344. (repeat (nth 1 macro-data))
  1345. (files
  1346. (cond ((and (stringp reftex-index-phrases-restrict-file)
  1347. (file-regular-p reftex-index-phrases-restrict-file))
  1348. (list reftex-index-phrases-restrict-file))
  1349. ((stringp reftex-index-phrases-restrict-file)
  1350. (error "Invalid restriction file %s"
  1351. reftex-index-phrases-restrict-file))
  1352. (t reftex-index-phrases-files)))
  1353. (as-words reftex-index-phrases-search-whole-words))
  1354. (unless macro-data
  1355. (error "No macro associated with key %c" char))
  1356. (unwind-protect
  1357. (let ((overlay-arrow-string "=>")
  1358. (overlay-arrow-position
  1359. reftex-index-phrases-marker)
  1360. (replace-count 0))
  1361. ;; Show the overlay arrow
  1362. (move-marker reftex-index-phrases-marker
  1363. (match-beginning 0) (current-buffer))
  1364. ;; Start the query-replace
  1365. (reftex-query-index-phrase-globally
  1366. files phrase macro-fmt
  1367. index-key repeat as-words)
  1368. (message "%s replaced"
  1369. (reftex-number replace-count "occurrence"))))))
  1370. (t (error "Cannot parse this line")))))
  1371. (defun reftex-index-all-phrases ()
  1372. "Index all phrases in the phrases buffer.
  1373. Calls `reftex-index-this-phrase' on each line in the buffer."
  1374. (interactive)
  1375. (reftex-index-region-phrases (point-min) (point-max)))
  1376. (defun reftex-index-remaining-phrases ()
  1377. "Index all phrases in the phrases buffer.
  1378. Calls `reftex-index-this-phrase' on each line ay and below point in
  1379. the buffer."
  1380. (interactive)
  1381. (beginning-of-line)
  1382. (reftex-index-region-phrases (point) (point-max)))
  1383. (defun reftex-index-region-phrases (beg end)
  1384. "Index all phrases in the phrases buffer.
  1385. Calls `reftex-index-this-phrase' on each line in the region."
  1386. (interactive "r")
  1387. (reftex-index-phrases-parse-header t)
  1388. (goto-char beg)
  1389. (while (not (or (eobp)
  1390. (>= (point) end)))
  1391. (save-excursion (reftex-index-this-phrase 'slave))
  1392. (beginning-of-line 2)))
  1393. (defun reftex-index-phrases-parse-header (&optional get-files)
  1394. "Parse the header of a phrases file to extract the macro definitions.
  1395. The definitions get stored in `reftex-index-phrases-macro-data'.
  1396. Also switches to the LaTeX document to find out which files belong to
  1397. the document and stores the list in `reftex-index-phrases-files'."
  1398. (let* ((master (reftex-index-phrase-tex-master))
  1399. buf)
  1400. (if get-files
  1401. ;; Get the file list
  1402. (save-excursion
  1403. (setq buf (reftex-get-file-buffer-force master))
  1404. (unless buf (error "Master file %s not found" master))
  1405. (set-buffer buf)
  1406. (reftex-access-scan-info)
  1407. (setq reftex-index-phrases-files
  1408. (reftex-all-document-files))))
  1409. ;; Parse the files header for macro definitions
  1410. (setq reftex-index-phrases-macro-data nil)
  1411. (save-excursion
  1412. (goto-char (point-min))
  1413. (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t)
  1414. (push (list
  1415. (string-to-char (match-string 2))
  1416. (match-string 4)
  1417. (equal (match-string 6) "t"))
  1418. reftex-index-phrases-macro-data))
  1419. ;; Reverse the list, so that the first macro is first
  1420. (if (null reftex-index-phrases-macro-data)
  1421. (error "No valid MACRO DEFINITION line in %s file (make sure to use TAB separators)" reftex-index-phrase-file-extension))
  1422. (setq reftex-index-phrases-macro-data
  1423. (nreverse reftex-index-phrases-macro-data))
  1424. (goto-char (point-min)))))
  1425. (defun reftex-index-phrases-apply-to-region (beg end)
  1426. "Index all index phrases in the current region.
  1427. This works exactly like global indexing from the index phrases buffer,
  1428. but operation is restricted to the current region. This is useful if
  1429. you need to add/change text in an already indexed document and want to
  1430. index the new part without having to go over the unchanged parts again."
  1431. (interactive "r")
  1432. (let ((win-conf (current-window-configuration))
  1433. (reftex-index-phrases-restrict-file (buffer-file-name)))
  1434. (save-excursion
  1435. (save-restriction
  1436. (narrow-to-region beg end)
  1437. (unwind-protect
  1438. (progn
  1439. ;; Hide the region highlighting
  1440. (if (featurep 'xemacs)
  1441. (zmacs-deactivate-region)
  1442. (deactivate-mark))
  1443. (delete-other-windows)
  1444. (reftex-index-visit-phrases-buffer)
  1445. (reftex-index-all-phrases))
  1446. (set-window-configuration win-conf))))))
  1447. (defun reftex-index-new-phrase (&optional text)
  1448. "Open a new line in the phrases buffer, insert TEXT."
  1449. (interactive)
  1450. (if (and text (stringp text))
  1451. (progn
  1452. ;; Check if the phrase is already in the buffer
  1453. (setq text (reftex-index-simplify-phrase text))
  1454. (goto-char (point-min))
  1455. (if (re-search-forward
  1456. (concat "^\\(\\S-*\\)\t\\(" (regexp-quote text)
  1457. "\\) *[\t\n]") nil t)
  1458. (progn
  1459. (goto-char (match-end 2))
  1460. (error "Phrase is already in phrases buffer")))))
  1461. ;; Add the new phrase line after the last in the buffer
  1462. (goto-char (point-max))
  1463. (if (re-search-backward reftex-index-phrases-phrase-regexp12 nil t)
  1464. (end-of-line))
  1465. (if (not (bolp))
  1466. (insert "\n"))
  1467. (insert "\t")
  1468. (if (and text (stringp text))
  1469. (insert text)))
  1470. (defun reftex-index-find-next-conflict-phrase (&optional arg)
  1471. "Find the next a phrase which is has conflicts in the phrase buffer.
  1472. The command helps to find possible conflicts in the phrase indexing process.
  1473. It searches downward from point for a phrase which is repeated elsewhere
  1474. in the buffer, or which is a subphrase of another phrase. If such a
  1475. phrase is found, the phrase info is displayed.
  1476. To check the whole buffer, start at the beginning and continue by calling
  1477. this function repeatedly."
  1478. (interactive "P")
  1479. (if (catch 'exit
  1480. (while (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
  1481. (goto-char (match-beginning 3))
  1482. (let* ((phrase (match-string 3))
  1483. (case-fold-search reftex-index-phrases-case-fold-search)
  1484. (re (reftex-index-phrases-find-dup-re phrase t)))
  1485. (if (save-excursion
  1486. (goto-char (point-min))
  1487. (and (re-search-forward re nil t)
  1488. (re-search-forward re nil t)))
  1489. (throw 'exit t)))))
  1490. (progn
  1491. (reftex-index-phrases-info)
  1492. (message "Phrase with match conflict discovered"))
  1493. (goto-char (point-max))
  1494. (error "No further problematic phrases found")))
  1495. (defun reftex-index-phrases-info ()
  1496. "Display information about the phrase at point."
  1497. (interactive)
  1498. (save-excursion
  1499. (beginning-of-line)
  1500. (unless (looking-at reftex-index-phrases-phrase-regexp12)
  1501. (error "Not a phrase line"))
  1502. (save-match-data (reftex-index-phrases-parse-header t))
  1503. (let* ((char (if (not (equal (match-string 1) ""))
  1504. (string-to-char (match-string 1))))
  1505. (phrase (match-string 3))
  1506. (index-key (match-string 6))
  1507. (index-keys (split-string
  1508. (or index-key phrase)
  1509. reftex-index-phrases-logical-or-regexp))
  1510. (macro-data (cdr (if (null char)
  1511. (car reftex-index-phrases-macro-data)
  1512. (assoc char reftex-index-phrases-macro-data))))
  1513. (macro-fmt (car macro-data))
  1514. (repeat (nth 1 macro-data))
  1515. (as-words reftex-index-phrases-search-whole-words)
  1516. (example (reftex-index-make-replace-string
  1517. macro-fmt (downcase phrase) (car index-keys) repeat))
  1518. (re (reftex-index-make-phrase-regexp phrase as-words t))
  1519. (re1 (reftex-index-phrases-find-dup-re phrase))
  1520. (re2 (reftex-index-phrases-find-dup-re phrase 'sub))
  1521. superphrases
  1522. (nmatches 0)
  1523. (ntimes1 0)
  1524. (ntimes2 0)
  1525. (case-fold-search reftex-index-phrases-case-fold-search)
  1526. file files buf)
  1527. (setq files reftex-index-phrases-files)
  1528. (save-excursion
  1529. (save-restriction
  1530. (widen)
  1531. (goto-char (point-min))
  1532. (while (re-search-forward re1 nil t)
  1533. (incf ntimes1))
  1534. (goto-char (point-min))
  1535. (while (re-search-forward re2 nil t)
  1536. (push (cons (count-lines 1 (point)) (match-string 1)) superphrases)
  1537. (incf ntimes2))))
  1538. (save-current-buffer
  1539. (while (setq file (pop files))
  1540. (setq buf (reftex-get-file-buffer-force file))
  1541. (when buf
  1542. (set-buffer buf)
  1543. (save-excursion
  1544. (save-restriction
  1545. (widen)
  1546. (goto-char (point-min))
  1547. (let ((case-fold-search reftex-index-phrases-case-fold-search))
  1548. (while (re-search-forward re nil t)
  1549. (or (reftex-in-comment)
  1550. (incf nmatches)))))))))
  1551. (with-output-to-temp-buffer "*Help*"
  1552. (princ (format " Phrase: %s\n" phrase))
  1553. (princ (format " Macro key: %s\n" char))
  1554. (princ (format " Macro format: %s\n" macro-fmt))
  1555. (princ (format " Repeat: %s\n" repeat))
  1556. (cond
  1557. (index-key
  1558. (let ((iks index-keys) (cnt 0) ik)
  1559. (while (setq ik (pop iks))
  1560. (princ (format "Index entry %d: %s\n" (incf cnt) ik)))))
  1561. (repeat
  1562. (princ (format " Index entry: %s\n" phrase)))
  1563. (t
  1564. (princ (format " Index key: <<Given by the match>>\n"))))
  1565. (princ (format " Example: %s\n" example))
  1566. (terpri)
  1567. (princ (format "Total matches: %s in %s\n"
  1568. (reftex-number nmatches "match" "es")
  1569. (reftex-number (length reftex-index-phrases-files)
  1570. "LaTeX file")))
  1571. (princ (format " Uniqueness: Phrase occurs %s in phrase buffer\n"
  1572. (reftex-number ntimes1 "time")))
  1573. (if (> ntimes2 1)
  1574. (progn
  1575. (princ (format " Superphrases: Phrase matches the following %s in the phrase buffer:\n"
  1576. (reftex-number ntimes2 "line")))
  1577. (mapcar (lambda(x)
  1578. (princ (format " Line %4d: %s\n" (car x) (cdr x))))
  1579. (nreverse superphrases))))))))
  1580. (defun reftex-index-phrases-set-macro-key ()
  1581. "Change the macro key for the current line.
  1582. Prompts for a macro key and insert is at the beginning of the line.
  1583. If you reply with SPACE, the macro keyn will be removed, so that the
  1584. default macro will be used. If you reply with `RET', just prints
  1585. information about the currently selected macro."
  1586. (interactive)
  1587. (reftex-index-phrases-parse-header)
  1588. (save-excursion
  1589. (beginning-of-line)
  1590. (unless (or (looking-at reftex-index-phrases-phrase-regexp12)
  1591. (looking-at "\t"))
  1592. (error "This is not a phrase line"))
  1593. (let* ((nc (reftex-index-select-phrases-macro 0))
  1594. (macro-data (assoc nc reftex-index-phrases-macro-data))
  1595. macro-fmt repeat)
  1596. (cond (macro-data)
  1597. ((equal nc ?\ )
  1598. (setq nc ""
  1599. macro-data (car reftex-index-phrases-macro-data)))
  1600. ((equal nc ?\C-m)
  1601. (setq nc (char-after (point)))
  1602. (if (equal nc ?\t)
  1603. (setq nc ""
  1604. macro-data (car reftex-index-phrases-macro-data))
  1605. (setq macro-data (assoc nc reftex-index-phrases-macro-data))))
  1606. (t (error "No macro associated with %c" nc)))
  1607. (setq macro-fmt (nth 1 macro-data)
  1608. repeat (nth 2 macro-data))
  1609. (if macro-data
  1610. (progn
  1611. (if (looking-at "[^\t]") (delete-char 1))
  1612. (insert nc)
  1613. (message "Line will use %s %s repeat" macro-fmt
  1614. (if repeat "with" "without")))
  1615. (error "Abort")))))
  1616. (defun reftex-index-sort-phrases (&optional chars-first)
  1617. "Sort the phrases lines in the buffer alphabetically.
  1618. Normally, this looks only at the phrases. With a prefix arg CHARS-FIRST,
  1619. it first compares the macro identifying chars and then the phrases."
  1620. (interactive "P")
  1621. ;; Remember the current line, so that we can return
  1622. (let ((line (buffer-substring (progn (beginning-of-line) (point))
  1623. (progn (end-of-line) (point))))
  1624. beg end)
  1625. (goto-char (point-min))
  1626. ;; Find first and last phrase line in buffer
  1627. (setq beg
  1628. (and (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
  1629. (match-beginning 0)))
  1630. (goto-char (point-max))
  1631. (setq end (re-search-backward reftex-index-phrases-phrase-regexp12 nil t))
  1632. (if end (setq end (progn (goto-char end) (end-of-line) (point))))
  1633. ;; Take the lines, sort them and re-insert.
  1634. (if (and beg end)
  1635. (progn
  1636. (message "Sorting lines...")
  1637. (let* ((lines (split-string (buffer-substring beg end) "\n"))
  1638. (lines1 (sort lines 'reftex-compare-phrase-lines)))
  1639. (message "Sorting lines...done")
  1640. (let ((inhibit-quit t)) ;; make sure we do not lose lines
  1641. (delete-region beg end)
  1642. (insert (mapconcat 'identity lines1 "\n"))))
  1643. (goto-char (point-max))
  1644. (re-search-backward (concat "^" (regexp-quote line) "$") nil t))
  1645. (error "Cannot find phrases lines to sort"))))
  1646. (defvar chars-first)
  1647. (defun reftex-compare-phrase-lines (a b)
  1648. "The comparison function used for sorting."
  1649. (let (ca cb pa pb c-p p-p)
  1650. (if (string-match reftex-index-phrases-phrase-regexp12 a)
  1651. (progn
  1652. ;; Extract macro char and phrase-or-key for a
  1653. (setq ca (match-string 1 a)
  1654. pa (downcase
  1655. (or (and reftex-index-phrases-sort-prefers-entry
  1656. (match-string 6 a))
  1657. (match-string 3 a))))
  1658. (if (string-match reftex-index-phrases-phrase-regexp12 b)
  1659. (progn
  1660. ;; Extract macro char and phrase-or-key for b
  1661. (setq cb (match-string 1 b)
  1662. pb (downcase
  1663. (or (and reftex-index-phrases-sort-prefers-entry
  1664. (match-string 6 b))
  1665. (match-string 3 b))))
  1666. (setq c-p (string< ca cb)
  1667. p-p (string< pa pb))
  1668. ;; Do the right comparison, based on the value of `chars-first'
  1669. ;; `chars-first' is bound locally in the calling function
  1670. (if chars-first
  1671. (if (string= ca cb) p-p c-p)
  1672. (if (string= pa pb) c-p p-p)))))
  1673. ;; If line a does not match, the answer we return determines
  1674. ;; if non-matching lines are collected at the beginning.
  1675. ;; When we return t here, non-matching lines form
  1676. ;; block separators for searches.
  1677. (not reftex-index-phrases-sort-in-blocks))))
  1678. (defvar reftex-index-phrases-menu)
  1679. (defun reftex-index-make-phrase-regexp (phrase &optional
  1680. as-words allow-newline)
  1681. "Return a regexp matching PHRASE, even if distributed over lines.
  1682. With optional arg AS-WORDS, require word boundary at beginning and end.
  1683. With optional arg ALLOW-NEWLINE, allow single newline between words."
  1684. (let* ((words (split-string phrase))
  1685. (space-re (if allow-newline
  1686. "\\([ \t]*\\(\n[ \t]*\\)?\\|[ \t]\\)"
  1687. "\\([ \t]+\\)")))
  1688. (concat (if (and as-words (string-match "\\`\\w" (car words)))
  1689. "\\(\\<\\|[`']\\)" "")
  1690. (mapconcat (lambda (w) (regexp-quote
  1691. (if reftex-index-phrases-case-fold-search
  1692. (downcase w)
  1693. w)))
  1694. words space-re)
  1695. (if (and as-words
  1696. (string-match "\\w\\'" (nth (1- (length words)) words)))
  1697. "\\(\\>\\|'\\)" ""))))
  1698. (defun reftex-index-simplify-phrase (phrase)
  1699. "Make phrase single spaces and single line."
  1700. (mapconcat 'identity (split-string phrase) " "))
  1701. (defun reftex-index-phrases-find-dup-re (phrase &optional sub)
  1702. "Return a regexp which matches variations of PHRASE (with additional space).
  1703. When SUB ins non-nil, the regexp will also match when PHRASE is a subphrase
  1704. of another phrase. The regexp works lonly in the phrase buffer."
  1705. (concat (if sub "^\\S-?\t\\([^\t\n]*" "^\\S-?\t")
  1706. (mapconcat 'regexp-quote (split-string phrase) " +")
  1707. (if sub "[^\t\n]*\\)\\([\t\n]\\|$\\)" " *\\([\t\n]\\|$\\)")))
  1708. (defun reftex-index-make-replace-string (macro-fmt match index-key
  1709. &optional repeat mathp)
  1710. "Return the string which can be used as replacement.
  1711. Treats the logical `and' for index phrases."
  1712. (let ((index-keys (split-string (or index-key match)
  1713. reftex-index-phrases-logical-and-regexp)))
  1714. (concat
  1715. (mapconcat (lambda (x)
  1716. (format macro-fmt
  1717. (format (if mathp reftex-index-math-format "%s") x)))
  1718. index-keys "")
  1719. (if repeat (reftex-index-simplify-phrase match) ""))))
  1720. (defun reftex-query-index-phrase-globally (files &rest args)
  1721. "Call `reftex-query-index-phrase' for all files in FILES."
  1722. (let ((win-conf (current-window-configuration))
  1723. (file))
  1724. (unless files (error "No files"))
  1725. (unwind-protect
  1726. (progn
  1727. (switch-to-buffer-other-window (reftex-get-file-buffer-force
  1728. (car files)))
  1729. (catch 'no-more-files
  1730. (while (setq file (pop files))
  1731. (switch-to-buffer (reftex-get-file-buffer-force file))
  1732. (save-excursion
  1733. (save-restriction
  1734. (unless (stringp reftex-index-phrases-restrict-file)
  1735. (widen))
  1736. (goto-char (point-min))
  1737. (apply 'reftex-query-index-phrase args))))))
  1738. (reftex-unhighlight 0)
  1739. (set-window-configuration win-conf))))
  1740. (defconst reftex-index-phrases-help
  1741. " Keys for query-index search
  1742. ===========================
  1743. y Replace this match
  1744. n Skip this match
  1745. ! Replace this and all further matches in this file
  1746. q / Q Skip match, start next file / start next phrase
  1747. o Use a different indexing macro for this match
  1748. 1 - 9 Select one of the multiple phrases
  1749. e Edit the replacement text
  1750. C-r Recursive edit.
  1751. s / S Save this buffer / Save all document buffers
  1752. C-g Abort"
  1753. "The help string for indexing phrases.")
  1754. (defvar replace-count)
  1755. (defun reftex-query-index-phrase (phrase macro-fmt &optional
  1756. index-key repeat as-words)
  1757. "Search through buffer for PHRASE, and offer to replace it with an indexed
  1758. version. The index version is derived by applying `format' with MACRO-FMT
  1759. to INDEX-KEY or PHRASE. When REPEAT is non-nil, the PHRASE is inserted
  1760. again after the macro.
  1761. AS-WORDS means, the search for PHRASE should require word boundaries at
  1762. both ends."
  1763. (let* ((re (reftex-index-make-phrase-regexp phrase as-words 'allow-newline))
  1764. (case-fold-search reftex-index-phrases-case-fold-search)
  1765. (index-keys (split-string
  1766. (or index-key phrase)
  1767. reftex-index-phrases-logical-or-regexp))
  1768. (nkeys (length index-keys))
  1769. (ckey (nth 0 index-keys))
  1770. (all-yes nil)
  1771. match rpl char (beg (make-marker)) (end (make-marker)) mathp)
  1772. (move-marker beg 1)
  1773. (move-marker end 1)
  1774. (unwind-protect
  1775. (while (re-search-forward re nil t)
  1776. (catch 'next-match
  1777. (if (reftex-in-comment)
  1778. (throw 'next-match nil))
  1779. (if (and (fboundp reftex-index-verify-function)
  1780. (not (funcall reftex-index-verify-function)))
  1781. (throw 'next-match nil))
  1782. (setq match (match-string 0))
  1783. (setq mathp
  1784. (save-match-data
  1785. (condition-case nil (texmathp) (error nil))))
  1786. (setq beg (move-marker beg (match-beginning 0))
  1787. end (move-marker end (match-end 0)))
  1788. (if (and reftex-index-phrases-skip-indexed-matches
  1789. (save-match-data
  1790. (reftex-index-phrase-match-is-indexed beg
  1791. end)))
  1792. (throw 'next-match nil))
  1793. (reftex-highlight 0 (match-beginning 0) (match-end 0))
  1794. (setq rpl
  1795. (save-match-data
  1796. (reftex-index-make-replace-string
  1797. macro-fmt (match-string 0) ckey repeat mathp)))
  1798. (while
  1799. (not
  1800. (catch 'loop
  1801. (message "REPLACE: %s? (yn!qoe%s?)"
  1802. rpl
  1803. (if (> nkeys 1)
  1804. (concat "1-" (int-to-string nkeys))
  1805. ""))
  1806. (setq char (if all-yes ?y (read-char-exclusive)))
  1807. (cond ((member char '(?y ?Y ?\ ))
  1808. ;; Yes!
  1809. (replace-match rpl t t)
  1810. (incf replace-count)
  1811. ;; See if we should insert newlines to shorten lines
  1812. (and reftex-index-phrases-wrap-long-lines
  1813. (reftex-index-phrases-fixup-line beg end))
  1814. (throw 'loop t))
  1815. ((member char '(?n ?N ?\C-h ?\C-?));; FIXME: DEL
  1816. ;; No
  1817. (throw 'loop t))
  1818. ((equal char ?!)
  1819. ;; Yes for all in this buffer
  1820. (setq all-yes t))
  1821. ((equal char ?q)
  1822. ;; Stop this one in this file
  1823. (goto-char (point-max))
  1824. (throw 'loop t))
  1825. ((equal char ?Q)
  1826. ;; Stop this one
  1827. (throw 'no-more-files t))
  1828. ((equal char ?s)
  1829. (save-buffer))
  1830. ((equal char ?S)
  1831. (reftex-save-all-document-buffers))
  1832. ((equal char ?\C-g)
  1833. (keyboard-quit))
  1834. ((member char '(?o ?O))
  1835. ;; Select a different macro
  1836. (let* ((nc (reftex-index-select-phrases-macro 2))
  1837. (macro-data
  1838. (cdr (assoc nc reftex-index-phrases-macro-data)))
  1839. (macro-fmt (car macro-data))
  1840. (repeat (nth 1 macro-data)))
  1841. (if macro-data
  1842. (setq rpl (save-match-data
  1843. (reftex-index-make-replace-string
  1844. macro-fmt match
  1845. ckey repeat mathp)))
  1846. (ding))))
  1847. ((equal char ?\?)
  1848. ;; Help
  1849. (with-output-to-temp-buffer "*Help*"
  1850. (princ reftex-index-phrases-help)))
  1851. ((equal char ?\C-r)
  1852. ;; Recursive edit
  1853. (save-match-data
  1854. (save-excursion
  1855. (message "%s"
  1856. (substitute-command-keys
  1857. "Recursive edit. Resume with \\[exit-recursive-edit]"))
  1858. (recursive-edit))))
  1859. ((equal char ?e)
  1860. (setq rpl (read-string "Edit: " rpl)))
  1861. ((equal char ?0)
  1862. (setq ckey (or index-key phrase)
  1863. rpl (save-match-data
  1864. (reftex-index-make-replace-string
  1865. macro-fmt match ckey repeat mathp))))
  1866. ((and (> char ?0)
  1867. (<= char (+ ?0 nkeys)))
  1868. (setq ckey (nth (1- (- char ?0)) index-keys)
  1869. rpl (save-match-data
  1870. (reftex-index-make-replace-string
  1871. macro-fmt match ckey repeat mathp))))
  1872. (t (ding)))
  1873. nil)))))
  1874. (message "")
  1875. (move-marker beg nil)
  1876. (move-marker end nil)
  1877. (setq all-yes nil)
  1878. (reftex-unhighlight 0))))
  1879. (defun reftex-index-phrase-match-is-indexed (beg end)
  1880. ;; Check if match is in an argument of an index macro, or if an
  1881. ;; index macro is directly attached to the match.
  1882. (save-excursion
  1883. (goto-char end)
  1884. (let* ((all-macros (reftex-what-macro t))
  1885. ; (this-macro (car (car all-macros)))
  1886. (before-macro
  1887. (and (> beg 2)
  1888. (goto-char (1- beg))
  1889. (memq (char-after (point)) '(?\] ?\}))
  1890. (car (reftex-what-macro 1))))
  1891. (after-macro
  1892. (and (goto-char end)
  1893. (looking-at "\\(\\\\[a-zA-Z]+\\*?\\)[[{]")
  1894. (match-string 1)))
  1895. macro)
  1896. (or (catch 'matched
  1897. (while (setq macro (pop all-macros))
  1898. (if (member (car macro) reftex-macros-with-index)
  1899. (throw 'matched t)))
  1900. nil)
  1901. (and before-macro
  1902. (member before-macro reftex-macros-with-index))
  1903. (and after-macro
  1904. (member after-macro reftex-macros-with-index))))))
  1905. (defun reftex-index-phrases-fixup-line (beg end)
  1906. "Insert newlines before BEG and/or after END to shorten line."
  1907. (let (bol eol space1 space2)
  1908. (save-excursion
  1909. ;; Find line boundaries and possible line breaks near BEG and END
  1910. (beginning-of-line)
  1911. (setq bol (point))
  1912. (end-of-line)
  1913. (setq eol (point))
  1914. (goto-char beg)
  1915. (skip-chars-backward "^ \n")
  1916. (if (and (equal (preceding-char) ?\ )
  1917. (string-match "\\S-" (buffer-substring bol (point))))
  1918. (setq space1 (1- (point))))
  1919. (goto-char end)
  1920. (skip-chars-forward "^ \n")
  1921. (if (and (equal (following-char) ?\ )
  1922. (string-match "\\S-" (buffer-substring (point) eol)))
  1923. (setq space2 (point)))
  1924. ;; Now check what we have and insert the newlines
  1925. (if (<= (- eol bol) fill-column)
  1926. ;; Line is already short
  1927. nil
  1928. (cond
  1929. ((and (not space1) (not space2))) ; No spaces available
  1930. ((not space2) ; Do space1
  1931. (reftex-index-phrases-replace-space space1))
  1932. ((not space1) ; Do space2
  1933. (reftex-index-phrases-replace-space space2))
  1934. (t ; We have both spaces
  1935. (let ((l1 (- space1 bol))
  1936. (l2 (- space2 space1))
  1937. (l3 (- eol space2)))
  1938. (if (> l2 fill-column)
  1939. ;; The central part alone is more than one line
  1940. (progn
  1941. (reftex-index-phrases-replace-space space1)
  1942. (reftex-index-phrases-replace-space space2))
  1943. (if (> (+ l1 l2) fill-column)
  1944. ;; Need to split beginning
  1945. (reftex-index-phrases-replace-space space1))
  1946. (if (> (+ l2 l3) fill-column)
  1947. ;; Need to split end
  1948. (reftex-index-phrases-replace-space space2))))))))))
  1949. (defun reftex-index-phrases-replace-space (pos)
  1950. "If there is a space at POS, replace it with a newline char.
  1951. Does not do a save-excursion."
  1952. (when (equal (char-after pos) ?\ )
  1953. (goto-char pos)
  1954. (delete-char 1)
  1955. (insert "\n")))
  1956. (defun reftex-index-select-phrases-macro (&optional delay)
  1957. "Offer a list of possible index macros and have the user select one."
  1958. (let* ((prompt (concat "Select macro: ["
  1959. (mapconcat (lambda (x) (char-to-string (car x)))
  1960. reftex-index-phrases-macro-data "")
  1961. "] "))
  1962. (help (concat "Select an indexing macro\n========================\n"
  1963. (mapconcat (lambda (x)
  1964. (format " [%c] %s"
  1965. (car x) (nth 1 x)))
  1966. reftex-index-phrases-macro-data "\n"))))
  1967. (reftex-select-with-char prompt help delay)))
  1968. (provide 'reftex-index)
  1969. ;;; reftex-index.el ends here
  1970. ;; Local Variables:
  1971. ;; generated-autoload-file: "reftex.el"
  1972. ;; End: