reftex-index.el 89 KB

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