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