reftex-index.el 88 KB

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