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