1 ;;; reftex-index.el --- index support with RefTeX
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <dominik@science.uva.nl>
7 ;; Maintainer: auctex-devel@gnu.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (eval-when-compile (require 'cl))
31 (provide 'reftex-index)
35 ;; START remove for XEmacs release
37 (defvar transient-mark-mode)
39 ;; END remove for XEmacs release
41 (declare-function texmathp "ext:texmathp" ())
43 (defun reftex-index-selection-or-word (&optional arg phrase)
44 "Put selection or the word near point into the default index macro.
45 This uses the information in `reftex-index-default-macro' to make an index
46 entry. The phrase indexed is the current selection or the word near point.
47 When called with one `C-u' prefix, let the user have a chance to edit the
48 index entry. When called with 2 `C-u' as prefix, also ask for the index
49 macro and other stuff.
50 When called inside TeX math mode as determined by the `texmathp.el' library
51 which is part of AUCTeX, the string is first processed with the
52 `reftex-index-math-format', which see."
54 (let* ((use-default (not (equal arg '(16)))) ; check for double prefix
55 ;; check if we have an active selection
56 (active (if (featurep 'xemacs)
57 (and zmacs-regions (region-exists-p)) ; XEmacs
58 (and transient-mark-mode mark-active))) ; Emacs
62 (skip-syntax-backward "w\\") (point))))
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)
74 (reftex-index-visit-phrases-buffer)
75 (reftex-index-new-phrase sel))
78 ;; Nothing selected, no word, so use full reftex-index command
80 ;; OK, we have something to index here.
81 ;; Add the dollars when necessary
83 (format reftex-index-math-format 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))
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)))))
97 (defun reftex-index (&optional char key tag sel no-insert)
98 "Query for an index macro and insert it along with its arguments.
99 The index macros available are those defined in `reftex-index-macro' or
100 by a call to `reftex-add-index-macros', typically from an AUCTeX style file.
101 RefteX provides completion for the index tag and the index key, and
102 will prompt for other arguments."
106 ;; Ensure access to scanning info
107 (reftex-ensure-index-support t)
108 (reftex-access-scan-info current-prefix-arg)
110 ;; Find out which macro we are going to use
111 (let* ((char (or char
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)))
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))
125 ;; Get the supported arguments
128 (setq tag1 (or (reftex-index-complete-tag tag opt-args) "")))
130 (reftex-index-complete-key
131 (if (string= tag1 "") "idx" tag1)
132 (member nindex opt-args))))
134 ;; Insert the macro and ask for any additional args
136 (loop for i from 1 to nargs do
137 (setq opt (member i opt-args)
138 value (cond ((= nindex i) key)
139 ((equal ntag i) tag1)
140 (t (read-string (concat "Macro arg nr. "
142 (if opt " (optional)" "")
144 (unless (and opt (string= value ""))
145 (insert (if opt "[" "{") value (if opt "]" "}"))))
146 (and repeat (stringp sel) (insert sel))
147 (and key reftex-plug-into-AUCTeX (fboundp 'LaTeX-add-index-entries)
148 (LaTeX-add-index-entries key))
149 (reftex-index-update-taglist tag1)
150 (reftex-notice-new)))
152 (defun reftex-default-index ()
153 (cond ((null reftex-index-default-tag) nil)
154 ((stringp reftex-index-default-tag) reftex-index-default-tag)
155 (t (or (get reftex-docstruct-symbol 'default-index-tag)
158 (defun reftex-update-default-index (tag &optional tag-list)
159 (if (and (not (equal tag ""))
161 (eq reftex-index-default-tag 'last)
163 (member tag tag-list)))
164 (put reftex-docstruct-symbol 'default-index-tag tag)))
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)))
172 (index-tags (cdr (assq 'index-tags
173 (symbol-value reftex-docstruct-symbol))))
174 (default (reftex-default-index))
175 (prompt (concat "Index tag"
179 (if opt "optional" "")
181 (concat (if opt ", " "")
182 (format "default %s" default))
185 (tag (completing-read prompt (mapcar 'list index-tags))))
186 (if (and default (equal tag "")) (setq tag default))
187 (reftex-update-default-index tag)
190 (defun reftex-index-select-tag ()
191 ;; Have the user select an index tag.
192 ;; FIXME: should we cache tag-alist, prompt and help?
193 (let* ((index-tags (cdr (assoc 'index-tags
194 (symbol-value reftex-docstruct-symbol))))
195 (default (reftex-default-index)))
198 (error "No index tags available"))
200 ((= (length index-tags) 1)
201 ;; Just one index, use it
204 ((> (length index-tags) 1)
205 ;; Several indices, ask.
206 (let* ((tags (copy-sequence index-tags))
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)
220 (while (and (< (incf i) len) (null val))
221 (unless (assq (aref tag i) tag-alist)
222 (push (list (aref tag i)
224 (concat (substring tag 0 i)
225 "[" (substring tag i (incf i)) "]"
229 (push (list (+ ?0 (incf cnt)) tag
230 (concat "[" (int-to-string cnt) "]:" tag))
232 (setq tag-alist (nreverse tag-alist))
233 ;; Compute Prompt and Help strings
236 (format "Select Index%s: "
237 (if default (format " (Default <%s>)" default) ""))
238 (mapconcat (lambda(x) (nth 2 x)) tag-alist " ")))
240 (concat "Select an Index\n===============\n"
242 (format "[^M] %s (the default)\n" default)
244 (mapconcat (lambda(x)
245 (apply 'format "[%c] %s" x))
247 ;; Query the user for an index-tag
248 (setq rpl (reftex-select-with-char prompt help 3 t))
250 (if (and default (equal rpl ?\C-m))
252 (if (assq rpl tag-alist)
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)))))
257 (t (error "This should not happen (reftex-index-select-tag)")))))
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
264 (symbol-value reftex-docstruct-symbol) 6
265 (lambda(x) (and (eq (car x) 'index)
266 (string= (nth 1 x) (or tag ""))))
268 (prompt (concat "Index key" (if optional " (optional)" "") ": "))
269 (key (completing-read prompt table nil nil initial)))
272 (defun reftex-index-update-taglist (newtag)
273 ;; add NEWTAG to the list of available index tags.
274 (let ((cell (assoc 'index-tags (symbol-value reftex-docstruct-symbol))))
275 (and newtag (cdr cell) (not (member newtag (cdr cell)))
276 (push newtag (cdr cell)))))
278 (defvar reftex-index-mode-map
279 (let ((map (make-sparse-keymap)))
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)
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)
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)))
321 (loop for key across "0123456789" do
322 (define-key map (vector (list key)) 'digit-argument))
323 (define-key map "-" 'negative-argument)
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))))
331 (easy-menu-define reftex-index-menu map
332 "Menu for Index buffer"
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]
344 ["Rebuilt *Index* Buffer" revert-buffer t]
346 ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans]
347 ["Rescan Entire Document" reftex-index-Rescan t])
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])
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]
361 ["Add Parentkey" reftex-index-level-down t]
362 ["Remove Parentkey " reftex-index-level-up t]
364 ["Make Start-of-Range" reftex-index-toggle-range-beginning t]
365 ["Make End-of-Range" reftex-index-toggle-range-end t]
367 ["Kill Entry" reftex-index-kill nil]
369 ["Undo" reftex-index-undo nil])
371 ["Context" reftex-index-toggle-context :style toggle
372 :selected reftex-index-include-context]
374 ["Follow Mode" reftex-index-toggle-follow :style toggle
375 :selected reftex-index-follow-mode])
377 ["Help" reftex-index-show-help t]))
380 "Keymap used for *Index* buffers.")
381 (define-obsolete-variable-alias
382 'reftex-index-map 'reftex-index-mode-map "24.1")
384 (defvar reftex-index-menu)
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.")
391 (defvar reftex-index-return-marker (make-marker)
392 "Marker which makes it possible to return from index to old position.")
394 (defvar reftex-index-restriction-indicator nil)
395 (defvar reftex-index-restriction-data nil)
397 (define-derived-mode reftex-index-mode fundamental-mode "RefTeX Index"
398 "Major mode for managing Index buffers for LaTeX files.
399 This buffer was created with RefTeX.
400 Press `?' for a summary of important key bindings, or check the menu.
402 Here are all local bindings.
404 \\{reftex-index-mode-map}"
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
409 (list "---- " 'mode-line-buffer-identification
410 " " 'global-mode-string
411 " R<" 'reftex-index-restriction-indicator ">"
413 (setq truncate-lines t)
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))
418 (make-local-variable 'reftex-last-follow-point)
419 (easy-menu-add reftex-index-menu reftex-index-mode-map)
420 (add-hook 'post-command-hook 'reftex-index-post-command-hook nil t)
421 (add-hook 'pre-command-hook 'reftex-index-pre-command-hook nil t))
423 (defconst reftex-index-help
424 " AVAILABLE KEYS IN INDEX BUFFER
425 ==============================
426 ! A..Z Goto the section of entries starting with this letter.
427 n / p next-entry / previous-entry
428 SPC / TAB Show/Goto the corresponding entry in the LaTeX document.
429 RET Goto the entry and hide the *Index* window (also on mouse-2).
430 q / k Hide/Kill *Index* buffer.
431 C-c = Switch to the TOC buffer.
432 f / c Toggle follow mode / Toggle display of [c]ontext.
433 g Refresh *Index* buffer.
434 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
435 s Switch to a different index (for documents with multiple indices).
436 e / C-k Edit/Kill the entry.
437 * | @ Edit specific part of entry: [*]key [|]attribute [@]visual
438 With prefix: kill that part.
439 \( ) Toggle entry's beginning/end of page range property.
440 _ ^ Add/Remove parent key (to make this item a subitem).
441 } / { Restrict Index to a single document section / Widen.
442 < / > When restricted, move restriction to previous/next section.")
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
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
452 (let* ((file (nth 3 data))
453 (literal (nth 2 data))
455 (re (regexp-quote literal))
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)))
464 (reftex-nearest-match re (length literal))))
465 (t (message "%s" reftex-no-follow-message) nil))))
467 (goto-char (match-beginning 0))
469 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
472 (defun reftex-display-index (&optional tag overriding-restriction redo
474 "Display a buffer with an index compiled from the current document.
475 When the document has multiple indices, first prompts for the correct one.
476 When index support is turned off, offer to turn it on.
477 With one or two `C-u' prefixes, rescan document first.
478 With prefix 2, restrict index to current document section.
479 With prefix 3, restrict index to region."
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))
488 (set-marker reftex-index-return-marker (point))
489 (setq reftex-last-follow-point 1)
491 ;; Determine the correct index to process
492 (let* ((docstruct (symbol-value reftex-docstruct-symbol))
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))
498 (or overriding-restriction
500 (reftex-get-restriction current-prefix-arg docstruct))))
502 ;; See if we are on an index macro as initial position
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)
508 (goto-char (+ (cdr what-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))))))
516 (setq buffer-name (reftex-make-index-buffer-name index-tag))
518 ;; Goto the buffer and put it into the correct mode
520 (when (or restriction current-prefix-arg)
521 (reftex-kill-buffer buffer-name))
523 (if (get-buffer-window buffer-name)
524 (select-window (get-buffer-window buffer-name))
525 (switch-to-buffer buffer-name))
527 (or (eq major-mode 'reftex-index-mode) (reftex-index-mode))
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)
536 (setq reftex-index-restriction-indicator (car restriction)
537 reftex-index-restriction-data (cdr restriction))
539 (setq reftex-index-restriction-indicator nil
540 reftex-index-restriction-data nil)))
541 (when (= (buffer-size) 0)
542 ;; buffer is empty - fill it
543 (message "Building %s buffer..." buffer-name)
545 (setq buffer-read-only nil)
549 SPC=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)))
556 (if (reftex-use-fonts)
557 (put-text-property 1 (point) 'face reftex-index-header-face))
558 (put-text-property 1 (point) 'intangible t)
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))))
569 (defun reftex-insert-index (docstruct tag &optional update-one remark)
570 ;; Insert an index into the current buffer. Entries are from the
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)
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))
586 (if (memq reftex-highlight-selection '(mouse both))
587 reftex-mouse-selected-face
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)
594 ;; Make the sublist and sort it
596 (setq all (or (memq bor all) all)))
598 (while (setq cell (pop all))
601 (and (eq (car cell) 'index)
602 (equal (nth 1 cell) tag)
603 (push cell sublist))))
604 (setq sublist (sort (nreverse sublist)
605 (lambda (a b) (string< (nth 8 a) (nth 8 b)))))
608 ;; Delete the entry at place
609 (and (bolp) (forward-char 1))
610 (delete-region (previous-single-property-change (1+ (point)) :data)
611 (or (next-single-property-change (point) :data)
614 ;; Walk through the list and insert all entries
615 (while (setq cell (pop sublist))
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)))
629 (insert indent (nth 7 cell))
633 (- (point) (length (nth 7 cell))) to
637 (when (or remark (nth 9 cell))
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)))
648 (insert context-indent (nth 2 cell) "\n")
650 (put-text-property from to :data cell)
652 (put-text-property from (1- to)
653 'mouse-face mouse-face))
657 (defun reftex-index-insert-new-letter (letter &optional font)
658 ;; Start a new section in the index
659 (let ((from (point)))
660 (insert "\n" letter letter letter
661 "-----------------------------------------------------------------")
663 (put-text-property from (point) 'face reftex-index-section-face))
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))
670 (end (max (point) (or (condition-case nil (mark) (error nil))
672 bor eor label here-I-am)
675 (setq here-I-am (car (reftex-where-am-I))
676 bor (if (eq (car here-I-am) 'toc)
678 (reftex-last-assoc-before-elt
679 'toc here-I-am docstruct))
680 eor (car (memq (assq 'toc (cdr (memq bor docstruct))) docstruct))
684 (setq label "region")
686 (setq bor (car (reftex-where-am-I)))
687 (setq bor (nth 1 (memq bor docstruct)))
689 (setq eor (nth 1 (memq (car (reftex-where-am-I)) docstruct)))))
691 (if (and label (or bor eor))
695 (defun reftex-index-pre-command-hook ()
696 ;; Used as pre command hook in *Index* buffer
697 (reftex-unhighlight 0)
698 (reftex-unhighlight 1))
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)
704 (not (get-text-property (point) 'intangible))
705 (memq reftex-highlight-selection '(cursor both))
707 (or (previous-single-property-change (1+ (point)) :data)
709 (or (next-single-property-change (point) :data)
711 (if (integerp reftex-index-follow-mode)
712 ;; Remove delayed action
713 (setq reftex-index-follow-mode t)
714 (and reftex-index-follow-mode
715 (not (equal reftex-last-follow-point (point)))
716 ;; Show context in other window
717 (setq reftex-last-follow-point (point))
719 (reftex-index-visit-location nil (not reftex-revisit-to-follow))
722 (defun reftex-index-show-help ()
723 "Show a summary of special key bindings."
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)))
732 (defun reftex-index-next (&optional arg)
733 "Move to next selectable item."
735 (setq reftex-callback-fwd t)
736 (or (eobp) (forward-char 1))
737 (goto-char (or (next-single-property-change (point) :data)
739 (unless (get-text-property (point) :data)
740 (goto-char (or (next-single-property-change (point) :data)
742 (defun reftex-index-previous (&optional arg)
743 "Move to previous selectable item."
745 (setq reftex-callback-fwd nil)
746 (goto-char (or (previous-single-property-change (point) :data)
748 (unless (get-text-property (point) :data)
749 (goto-char (or (previous-single-property-change (point) :data)
751 (defun reftex-index-toggle-follow ()
752 "Toggle follow (other window follows with context)."
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.
758 Label context is only displayed when the labels are there as well."
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."
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."
769 (reftex-index-visit-location 'hide))
770 (defun reftex-index-goto-entry ()
771 "Go to document location in other window. *Index* window stays."
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."
778 (reftex-index-visit-location 'hide))
779 (defun reftex-index-quit ()
780 "Hide the *Index* window and do not move point."
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."
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.
794 The function will go to the section where the entry at point was defined."
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)
801 (defun reftex-index-rescan (&rest ignore)
802 "Regenerate the *Index* buffer after reparsing file of section at point."
804 (let ((index-tag reftex-index-tag))
805 (if (and reftex-enable-partial-scans
806 (null current-prefix-arg))
807 (let* ((data (get-text-property (point) :data))
809 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
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))
814 (reftex-display-index index-tag nil 'redo line)))
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."
820 (let ((index-tag reftex-index-tag)
821 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
823 (reftex-get-file-buffer-force reftex-last-index-file))
824 (setq current-prefix-arg '(16))
825 (reftex-display-index index-tag nil 'redo line)))
826 (defun reftex-index-revert (&rest ignore)
827 "Regenerate the *Index* from the internal lists. No reparsing os done."
829 (let ((buf (current-buffer))
830 (index-tag reftex-index-tag)
831 (data (get-text-property (point) :data))
832 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
834 (reftex-get-file-buffer-force reftex-last-index-file))
835 (reftex-erase-buffer buf)
836 (setq current-prefix-arg nil
837 reftex-last-follow-point 1)
838 (reftex-display-index index-tag nil 'redo data line)))
839 (defun reftex-index-switch-index-tag (&rest ignore)
840 "Switch to a different index of the same document."
843 (reftex-get-file-buffer-force reftex-last-index-file))
844 (setq current-prefix-arg nil)
845 (reftex-display-index nil nil 'redo))
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.
851 (let* ((data (get-text-property (point) :data))
852 (docstruct (symbol-value reftex-docstruct-symbol))
854 (if (and (not data) force)
855 (setq data (assq 'toc docstruct)))
857 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
858 eor (car (memq (assq 'toc (cdr (memq bor docstruct)))
860 reftex-index-restriction-data (list bor eor)
861 reftex-index-restriction-indicator (nth 6 bor) )))
862 (reftex-index-revert))
864 (defun reftex-index-widen (&rest ignore)
865 "Show the unrestricted index (all entries)."
867 (setq reftex-index-restriction-indicator nil
868 reftex-index-restriction-data nil)
869 (reftex-index-revert)
870 (message "Index widened"))
871 (defun reftex-index-restriction-forward (&rest ignore)
872 "Restrict to previous section.
873 When index is currently unrestricted, restrict it to a section.
874 When index is restricted, select the next section as restriction criterion."
876 (let* ((docstruct (symbol-value reftex-docstruct-symbol))
877 (bor (nth 1 reftex-index-restriction-data)))
879 (not (eq (car bor) 'toc)))
880 (reftex-index-restrict-to-section t)
881 (setq reftex-index-restriction-indicator (nth 6 bor)
882 reftex-index-restriction-data
884 (car (memq (assq 'toc (cdr (memq bor docstruct)))
886 (reftex-index-revert))))
887 (defun reftex-index-restriction-backward (&rest ignore)
888 "Restrict to next section.
889 When index is currently unrestricted, restrict it to a section.
890 When index is restricted, select the previous section as restriction criterion."
892 (let* ((docstruct (symbol-value reftex-docstruct-symbol))
893 (eor (car reftex-index-restriction-data))
894 (bor (reftex-last-assoc-before-elt 'toc eor docstruct t)))
896 (not (eq (car bor) 'toc)))
897 (reftex-index-restrict-to-section t)
898 (setq reftex-index-restriction-indicator (nth 6 bor)
899 reftex-index-restriction-data
901 (reftex-index-revert))))
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.
910 (let* ((data (get-text-property (point) :data))
911 (index-window (selected-window))
912 show-window show-buffer match)
914 (unless data (error "Don't know which index entry to visit"))
916 (if (eq (car data) 'index)
917 (setq match (reftex-index-show-entry data no-revisit)))
919 (setq show-window (selected-window)
920 show-buffer (current-buffer))
923 (select-window index-window)
924 (error "Cannot find location"))
926 (select-window index-window)
928 ;; Use the `final' parameter to decide what to do next
931 (reftex-unhighlight 0)
932 (select-window show-window))
934 (reftex-unhighlight 0)
935 (or (one-window-p) (delete-window))
936 (switch-to-buffer show-buffer))
939 (defun reftex-index-analyze-entry (data)
940 ;; This splits the index context so that key, attribute and visual
941 ;; values are accessible individually.
943 (let* ((arg (nth 5 data))
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))
952 (1+ (match-beginning 0))
954 (boattr (if (string-match (concat "[^" (nth 3 sc) "]" (nth 1 sc))
956 (1+ (match-beginning 0))
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)))
963 (list pre key attr actual post)))
965 (defun reftex-index-edit ()
966 "Edit the index entry at point."
968 (let* ((data (get-text-property (point) :data))
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)))
975 (defun reftex-index-toggle-range-beginning ()
976 "Toggle the page range start attribute `|('."
978 (let* ((data (get-text-property (point) :data))
979 (bor (concat (nth 1 reftex-index-special-chars) "("))
981 (unless data (error "Don't know which index entry to edit"))
982 (setq analyze (reftex-index-analyze-entry data)
983 attr (nth 2 analyze))
984 (setf (nth 2 analyze) (if (string= attr bor) "" bor))
985 (setq new (apply 'concat analyze))
986 (reftex-index-change-entry
987 new (if (string= (nth 2 analyze) bor)
988 "Entry is now START-OF-PAGE-RANGE"
989 "START-OF-PAGE-RANGE canceled"))))
991 (defun reftex-index-toggle-range-end ()
992 "Toggle the page-range-end attribute `|)'."
994 (let* ((data (get-text-property (point) :data))
995 (eor (concat (nth 1 reftex-index-special-chars) ")"))
997 (unless data (error "Don't know which index entry to edit"))
998 (setq analyze (reftex-index-analyze-entry data)
999 attr (nth 2 analyze))
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)
1004 "Entry is now END-OF-PAGE-RANGE"
1005 "END-OF-PAGE-RANGE canceled"))))
1007 (defun reftex-index-edit-key ()
1008 "Edit the KEY part of the index entry."
1010 (reftex-index-edit-part nil 1 "" "Key: " t))
1012 (defun reftex-index-edit-attribute (&optional arg)
1013 "EDIT the ATTRIBUTE part of the entry. With arg: remove entire ATTRIBUTE."
1015 (reftex-index-edit-part arg 2 (nth 1 reftex-index-special-chars)
1018 (defun reftex-index-edit-visual (&optional arg)
1019 "EDIT the VISUAL part of the entry. With arg: remove entire VISUAL string."
1021 (reftex-index-edit-part arg 3 (nth 2 reftex-index-special-chars) "Visual: "))
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))
1026 new analyze opart npart)
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)
1030 opart (nth n analyze))
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)))
1035 (cond ((string= npart opart)
1036 (error "Not changed"))
1038 (if dont-allow-empty
1039 (error "Invalid value")
1040 (setf (nth n analyze) npart)))
1041 (t (setf (nth n analyze) (concat initial npart))))
1042 (setq new (apply 'concat analyze))
1043 ;; Change the entry and insert the changed version into the index.
1044 (reftex-index-change-entry
1045 new (if (string= npart "")
1046 (format "Deleted: %s" opart)
1047 (format "New value is: %s" npart)))))
1049 (defun reftex-index-level-down ()
1050 "Make index entry a subitem of another entry."
1052 (let* ((data (get-text-property (point) :data))
1053 (docstruct (symbol-value reftex-docstruct-symbol))
1055 (unless data (error "Don't know which index entry to change"))
1056 (setq old (nth 2 data)
1058 prefix (completing-read
1063 (and (eq (car x) 'index)
1064 (string= (nth 1 x) reftex-index-tag))) t)))
1065 (unless (string-match
1066 (concat (regexp-quote (car reftex-index-special-chars)) "\\'")
1068 (setq prefix (concat prefix (car reftex-index-special-chars))))
1069 (if (string-match (regexp-quote key) old)
1070 (setq new (replace-match (concat prefix key) t t old))
1071 (error "Cannot construct new index key"))
1072 (reftex-index-change-entry new (format "Added prefix: %s" prefix))))
1074 (defun reftex-index-level-up ()
1075 "Remove the highest level of a hierarchical index entry."
1077 (let* ((data (get-text-property (point) :data))
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) "]*"
1082 "[^" (nth 3 reftex-index-special-chars) "]"
1083 (regexp-quote (nth 0 reftex-index-special-chars))
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))))
1089 (error "Entry is not a subitem"))
1090 (reftex-index-change-entry new (format "Removed prefix: %s" prefix))))
1092 (defun reftex-index-kill ()
1093 "FIXME: Not yet implemented"
1095 (error "This function is currently not implemented"))
1097 (defun reftex-index-undo ()
1098 "FIXME: Not yet implemented"
1100 (error "This function is currently not implemented"))
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.
1106 (let* ((data (get-text-property (point) :data))
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"))
1113 (with-current-buffer (get-file-buffer (nth 3 data))
1115 (unless (looking-at (regexp-quote old))
1116 (error "This should not happen (reftex-index-change-entry)"))
1117 (delete-region beg end)
1119 (goto-char (1- beg))
1120 (when (and (re-search-forward (reftex-everything-regexp) nil t)
1122 (< (abs (- (match-beginning 10) beg)) (length new))
1123 (setq info (reftex-index-info-safe buffer-file-name)))
1124 (setcdr data (cdr info))))
1125 (let ((buffer-read-only nil))
1127 (reftex-insert-index (list data) reftex-index-tag t
1129 (setq reftex-last-follow-point 1)
1130 (and message (message "%s" message))))
1132 (defun reftex-index-goto-letter (char)
1133 "Go to the CHAR section in the index."
1135 (case-fold-search nil))
1136 (goto-char (point-min))
1138 (if (re-search-forward (concat "^" (char-to-string char)) nil t)
1142 (reftex-index-next))
1145 (error "This <%s> index does not contain entries sorted before the letters"
1147 (error "This <%s> index does not contain entries starting with `%c'"
1148 reftex-index-tag char)))))
1151 ;;----------------------------------------------------------------------
1152 ;; The Index Phrases File
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.
1175 This gets refreshed in every phrases command.")
1176 (defvar reftex-index-phrases-files nil
1177 "List of document files relevant for the phrases file.")
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-mode-map
1184 (let ((map (make-sparse-keymap)))
1185 ;; Keybindings and Menu for phrases buffer
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)))
1201 (easy-menu-define reftex-index-phrases-menu map
1202 "Menu for Phrases buffer"
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]
1209 ["Sort" reftex-index-sort-phrases t]
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]
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]
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])
1244 ["Save and Return" reftex-index-phrases-save-and-return t]))
1247 "Keymap used for *toc* buffer.")
1248 (define-obsolete-variable-alias
1249 'reftex-index-phrases-map 'reftex-index-phrases-mode-map "24.1")
1252 (defun reftex-index-phrase-selection-or-word (arg)
1253 "Add current selection or word at point to the phrases buffer.
1254 When you are in transient-mark-mode and the region is active, the
1255 selection will be used - otherwise the word at point.
1256 You get a chance to edit the entry in the phrases buffer - finish with
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)
1263 (substitute-command-keys
1264 "Return to LaTeX with \\[reftex-index-phrases-save-and-return]"))))
1266 (defun reftex-index-visit-phrases-buffer ()
1267 "Switch to the phrases buffer, initialize if empty."
1269 (reftex-access-scan-info)
1270 (let* ((master (reftex-TeX-master-file))
1271 (name (concat (file-name-sans-extension master)
1272 reftex-index-phrase-file-extension)))
1274 (unless (eq major-mode 'reftex-index-phrases-mode)
1275 (reftex-index-phrases-mode))
1276 (if (= (buffer-size) 0)
1277 (reftex-index-initialize-phrases-buffer master))))
1279 (defun reftex-index-initialize-phrases-buffer (&optional master)
1280 "Initialize the phrases buffer by creating the header.
1281 If the buffer is non-empty, delete the old header first."
1283 (let* ((case-fold-search t)
1284 (default-key (car reftex-index-default-macro))
1285 (default-macro (nth 1 (assoc default-key
1286 reftex-key-to-index-macro-alist)))
1288 (sort (copy-sequence reftex-index-macro-alist)
1289 (lambda (a b) (equal (car a) default-macro))))
1290 macro entry key repeat)
1292 (if master (set (make-local-variable 'TeX-master)
1293 (file-name-nondirectory master)))
1295 (when (> (buffer-size) 0)
1298 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t)
1300 (beginning-of-line 2)
1301 (if (looking-at reftex-index-phrases-comment-regexp)
1302 (beginning-of-line 2))
1303 (while (looking-at "^[ \t]*$")
1304 (beginning-of-line 2))
1305 (if (featurep 'xemacs)
1306 (zmacs-activate-region)
1307 (setq mark-active t))
1308 (if (yes-or-no-p "Delete and rebuild header? ")
1309 (delete-region (point-min) (point))))
1311 ;; Insert the mode line
1313 (format "%% -*- mode: reftex-index-phrases; TeX-master: \"%s\" -*-\n"
1314 (file-name-nondirectory (reftex-index-phrase-tex-master))))
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)
1320 repeat (nth 7 entry)
1321 key (car (delq nil (mapcar (lambda (x) (if (equal (nth 1 x) macro)
1324 reftex-key-to-index-macro-alist))))
1325 (insert (format ">>>INDEX_MACRO_DEFINITION:\t%s\t%-20s\t%s\n"
1326 (char-to-string key) (concat macro "{%s}")
1327 (if repeat "t" "nil"))))
1328 (insert "%---------------------------------------------------------------------\n\n\n")))
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)
1334 (local-variable-p 'TeX-master (current-buffer))
1335 (stringp TeX-master))
1336 ;; We have a local variable which tells us which file to use
1337 (expand-file-name TeX-master dir)
1339 (concat (file-name-sans-extension (buffer-file-name)) ".tex")))
1341 (defun reftex-index-phrases-save-and-return ()
1342 "Return to where the `reftex-index-phrase-selection-or-word' was called."
1345 (switch-to-buffer (marker-buffer reftex-index-return-marker))
1346 (goto-char (or (marker-position reftex-index-return-marker) (point))))
1349 (defvar reftex-index-phrases-menu)
1350 (defvar reftex-index-phrases-marker)
1351 (defvar reftex-index-phrases-restrict-file nil)
1353 (define-derived-mode reftex-index-phrases-mode fundamental-mode "Phrases"
1354 "Major mode for managing the Index phrases of a LaTeX document.
1355 This buffer was created with RefTeX.
1357 To 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.
1361 To index phrases use one of:
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
1369 You can sort the phrases in this buffer with \\[reftex-index-sort-phrases].
1370 To display information about the phrase at point, use \\[reftex-index-phrases-info].
1372 For more information see the RefTeX User Manual.
1374 Here are all local bindings.
1376 \\{reftex-index-phrases-mode-map}"
1377 (set (make-local-variable 'font-lock-defaults)
1378 reftex-index-phrases-font-lock-defaults)
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)
1383 ;; Font Locking stuff
1384 (let ((ss (if (featurep 'xemacs) 'secondary-selection ''secondary-selection)))
1385 (setq reftex-index-phrases-font-lock-keywords
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)
1392 '(4 font-lock-function-name-face)
1394 '(6 font-lock-string-face))
1395 (list reftex-index-phrases-phrase-regexp1
1396 '(1 font-lock-keyword-face)
1398 '(3 font-lock-string-face)
1400 (list reftex-index-phrases-phrase-regexp2
1401 '(1 font-lock-keyword-face)
1403 '(3 font-lock-string-face)
1405 '(5 font-lock-function-name-face))
1407 (setq reftex-index-phrases-font-lock-defaults
1408 '((reftex-index-phrases-font-lock-keywords)
1409 nil t nil beginning-of-line))
1410 (put 'reftex-index-phrases-mode 'font-lock-defaults
1411 reftex-index-phrases-font-lock-defaults) ; XEmacs
1414 (defun reftex-index-next-phrase (&optional arg)
1415 "Index the next ARG phrases in the phrases buffer."
1417 (reftex-index-phrases-parse-header t)
1421 (if (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
1423 (goto-char (match-beginning 0))
1424 (reftex-index-this-phrase 'slave))
1425 (error "No more phrase lines after point"))))
1427 (defun reftex-index-this-phrase (&optional slave)
1428 "Index the phrase in the current line.
1429 Does a global search and replace in the entire document. At each
1430 match, the user will be asked to confirm the replacement."
1432 (if (not slave) (reftex-index-phrases-parse-header t))
1435 (cond ((looking-at reftex-index-phrases-comment-regexp)
1436 (if (not slave) (error "Comment line")))
1437 ((looking-at "^[ \t]*$")
1438 (if (not slave) (error "Empty line")))
1439 ((looking-at reftex-index-phrases-macrodef-regexp)
1440 (if (not slave) (error "Macro definition line")))
1441 ((looking-at reftex-index-phrases-phrase-regexp12)
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))
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)
1457 (error "Invalid restriction file %s"
1458 reftex-index-phrases-restrict-file))
1459 (t reftex-index-phrases-files)))
1460 (as-words reftex-index-phrases-search-whole-words))
1462 (error "No macro associated with key %c" char))
1464 (let ((overlay-arrow-string "=>")
1465 (overlay-arrow-position
1466 reftex-index-phrases-marker)
1468 ;; Show the overlay arrow
1469 (move-marker reftex-index-phrases-marker
1470 (match-beginning 0) (current-buffer))
1471 ;; Start the query-replace
1472 (reftex-query-index-phrase-globally
1473 files phrase macro-fmt
1474 index-key repeat as-words)
1475 (message "%s replaced"
1476 (reftex-number replace-count "occurrence"))))))
1477 (t (error "Cannot parse this line")))))
1479 (defun reftex-index-all-phrases ()
1480 "Index all phrases in the phrases buffer.
1481 Calls `reftex-index-this-phrase' on each line in the buffer."
1483 (reftex-index-region-phrases (point-min) (point-max)))
1485 (defun reftex-index-remaining-phrases ()
1486 "Index all phrases in the phrases buffer.
1487 Calls `reftex-index-this-phrase' on each line ay and below point in
1491 (reftex-index-region-phrases (point) (point-max)))
1493 (defun reftex-index-region-phrases (beg end)
1494 "Index all phrases in the phrases buffer.
1495 Calls `reftex-index-this-phrase' on each line in the region."
1497 (reftex-index-phrases-parse-header t)
1499 (while (not (or (eobp)
1501 (save-excursion (reftex-index-this-phrase 'slave))
1502 (beginning-of-line 2)))
1504 (defun reftex-index-phrases-parse-header (&optional get-files)
1505 "Parse the header of a phrases file to extract the macro definitions.
1506 The definitions get stored in `reftex-index-phrases-macro-data'.
1507 Also switches to the LaTeX document to find out which files belong to
1508 the document and stores the list in `reftex-index-phrases-files'."
1509 (let* ((master (reftex-index-phrase-tex-master))
1512 ;; Get the file list
1514 (setq buf (reftex-get-file-buffer-force master))
1515 (unless buf (error "Master file %s not found" master))
1517 (reftex-access-scan-info)
1518 (setq reftex-index-phrases-files
1519 (reftex-all-document-files))))
1520 ;; Parse the files header for macro definitions
1521 (setq reftex-index-phrases-macro-data nil)
1523 (goto-char (point-min))
1524 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t)
1526 (string-to-char (match-string 2))
1528 (equal (match-string 6) "t"))
1529 reftex-index-phrases-macro-data))
1530 ;; Reverse the list, so that the first macro is first
1531 (if (null reftex-index-phrases-macro-data)
1532 (error "No valid MACRO DEFINITION line in %s file (make sure to use TAB separators)" reftex-index-phrase-file-extension))
1533 (setq reftex-index-phrases-macro-data
1534 (nreverse reftex-index-phrases-macro-data))
1535 (goto-char (point-min)))))
1537 (defun reftex-index-phrases-apply-to-region (beg end)
1538 "Index all index phrases in the current region.
1539 This works exactly like global indexing from the index phrases buffer,
1540 but operation is restricted to the current region. This is useful if
1541 you need to add/change text in an already indexed document and want to
1542 index the new part without having to go over the unchanged parts again."
1544 (let ((win-conf (current-window-configuration))
1545 (reftex-index-phrases-restrict-file (buffer-file-name)))
1548 (narrow-to-region beg end)
1551 ;; Hide the region highlighting
1552 (if (featurep 'xemacs)
1553 (zmacs-deactivate-region)
1555 (delete-other-windows)
1556 (reftex-index-visit-phrases-buffer)
1557 (reftex-index-all-phrases))
1558 (set-window-configuration win-conf))))))
1560 (defun reftex-index-new-phrase (&optional text)
1561 "Open a new line in the phrases buffer, insert TEXT."
1563 (if (and text (stringp text))
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
1569 (concat "^\\(\\S-*\\)\t\\(" (regexp-quote text)
1570 "\\) *[\t\n]") nil t)
1572 (goto-char (match-end 2))
1573 (error "Phrase is already in phrases buffer")))))
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)
1581 (if (and text (stringp text))
1584 (defun reftex-index-find-next-conflict-phrase (&optional arg)
1585 "Find the next a phrase which is has conflicts in the phrase buffer.
1586 The command helps to find possible conflicts in the phrase indexing process.
1587 It searches downward from point for a phrase which is repeated elsewhere
1588 in the buffer, or which is a subphrase of another phrase. If such a
1589 phrase is found, the phrase info is displayed.
1590 To check the whole buffer, start at the beginning and continue by calling
1591 this function repeatedly."
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)))
1600 (goto-char (point-min))
1601 (and (re-search-forward re nil t)
1602 (re-search-forward re nil t)))
1605 (reftex-index-phrases-info)
1606 (message "Phrase with match conflict discovered"))
1607 (goto-char (point-max))
1608 (error "No further problematic phrases found")))
1610 (defun reftex-index-phrases-info ()
1611 "Display information about the phrase at point."
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) ""))
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))
1640 (case-fold-search reftex-index-phrases-case-fold-search)
1642 (setq files reftex-index-phrases-files)
1646 (goto-char (point-min))
1647 (while (re-search-forward re1 nil t)
1649 (goto-char (point-min))
1650 (while (re-search-forward re2 nil t)
1651 (push (cons (count-lines 1 (point)) (match-string 1)) superphrases)
1653 (save-current-buffer
1654 (while (setq file (pop files))
1655 (setq buf (reftex-get-file-buffer-force file))
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)))))))))
1666 (with-output-to-temp-buffer "*Help*"
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))
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)))))
1677 (princ (format " Index entry: %s\n" phrase)))
1679 (princ (format " Index key: <<Given by the match>>\n"))))
1680 (princ (format " Example: %s\n" example))
1682 (princ (format "Total matches: %s in %s\n"
1683 (reftex-number nmatches "match" "es")
1684 (reftex-number (length reftex-index-phrases-files)
1686 (princ (format " Uniqueness: Phrase occurs %s in phrase buffer\n"
1687 (reftex-number ntimes1 "time")))
1690 (princ (format " Superphrases: Phrase matches the following %s in the phrase buffer:\n"
1691 (reftex-number ntimes2 "line")))
1693 (princ (format " Line %4d: %s\n" (car x) (cdr x))))
1694 (nreverse superphrases))))))))
1696 (defun reftex-index-phrases-set-macro-key ()
1697 "Change the macro key for the current line.
1698 Prompts for a macro key and insert is at the beginning of the line.
1699 If you reply with SPACE, the macro keyn will be removed, so that the
1700 default macro will be used. If you reply with `RET', just prints
1701 information about the currently selected macro."
1703 (reftex-index-phrases-parse-header)
1706 (unless (or (looking-at reftex-index-phrases-phrase-regexp12)
1708 (error "This is not a phrase line"))
1709 (let* ((nc (reftex-index-select-phrases-macro 0))
1710 (macro-data (assoc nc reftex-index-phrases-macro-data))
1715 macro-data (car reftex-index-phrases-macro-data)))
1717 (setq nc (char-after (point)))
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)))
1724 (setq macro-fmt (nth 1 macro-data)
1725 repeat (nth 2 macro-data))
1728 (if (looking-at "[^\t]") (delete-char 1))
1730 (message "Line will use %s %s repeat" macro-fmt
1731 (if repeat "with" "without")))
1734 (defun reftex-index-sort-phrases (&optional chars-first)
1735 "Sort the phrases lines in the buffer alphabetically.
1736 Normally, this looks only at the phrases. With a prefix arg CHARS-FIRST,
1737 it first compares the macro identifying chars and then the phrases."
1739 ;; Remember the current line, so that we can return
1740 (let ((line (buffer-substring (progn (beginning-of-line) (point))
1741 (progn (end-of-line) (point))))
1743 (goto-char (point-min))
1744 ;; Find first and last phrase line in buffer
1746 (and (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
1747 (match-beginning 0)))
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.
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")
1758 (let ((inhibit-quit t)) ;; make sure we do not lose lines
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))
1763 (error "Cannot find phrases lines to sort"))))
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)
1771 ;; Extract macro char and phrase-or-key for a
1772 (setq ca (match-string 1 a)
1774 (or (and reftex-index-phrases-sort-prefers-entry
1776 (match-string 3 a))))
1777 (if (string-match reftex-index-phrases-phrase-regexp12 b)
1779 ;; Extract macro char and phrase-or-key for b
1780 (setq cb (match-string 1 b)
1782 (or (and reftex-index-phrases-sort-prefers-entry
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'
1788 ;; `chars-first' is bound locally in the calling function
1790 (if (string= ca cb) p-p c-p)
1791 (if (string= pa pb) c-p p-p)))))
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))))
1798 (defvar reftex-index-phrases-menu)
1799 (defun reftex-index-make-phrase-regexp (phrase &optional
1800 as-words allow-newline)
1801 "Return a regexp matching PHRASE, even if distributed over lines.
1802 With optional arg AS-WORDS, require word boundary at beginning and end.
1803 With optional arg ALLOW-NEWLINE, allow single newline between words."
1804 (let* ((words (split-string phrase))
1805 (space-re (if allow-newline
1806 "\\([ \t]*\\(\n[ \t]*\\)?\\|[ \t]\\)"
1808 (concat (if (and as-words (string-match "\\`\\w" (car words)))
1809 "\\(\\<\\|[`']\\)" "")
1810 (mapconcat (lambda (w) (regexp-quote
1811 (if reftex-index-phrases-case-fold-search
1816 (string-match "\\w\\'" (nth (1- (length words)) words)))
1817 "\\(\\>\\|'\\)" ""))))
1819 (defun reftex-index-simplify-phrase (phrase)
1820 "Make phrase single spaces and single line."
1821 (mapconcat 'identity (split-string phrase) " "))
1823 (defun reftex-index-phrases-find-dup-re (phrase &optional sub)
1824 "Return a regexp which matches variations of PHRASE (with additional space).
1825 When SUB ins non-nil, the regexp will also match when PHRASE is a subphrase
1826 of another phrase. The regexp works lonly in the phrase buffer."
1827 (concat (if sub "^\\S-?\t\\([^\t\n]*" "^\\S-?\t")
1828 (mapconcat 'regexp-quote (split-string phrase) " +")
1829 (if sub "[^\t\n]*\\)\\([\t\n]\\|$\\)" " *\\([\t\n]\\|$\\)")))
1831 (defun reftex-index-make-replace-string (macro-fmt match index-key
1832 &optional repeat mathp)
1833 "Return the string which can be used as replacement.
1834 Treats the logical `and' for index phrases."
1835 (let ((index-keys (split-string (or index-key match)
1836 reftex-index-phrases-logical-and-regexp)))
1838 (mapconcat (lambda (x)
1840 (format (if mathp reftex-index-math-format "%s") x)))
1842 (if repeat (reftex-index-simplify-phrase match) ""))))
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))
1848 (unless files (error "No files"))
1851 (switch-to-buffer-other-window (reftex-get-file-buffer-force
1853 (catch 'no-more-files
1854 (while (setq file (pop files))
1855 (switch-to-buffer (reftex-get-file-buffer-force file))
1858 (unless (stringp reftex-index-phrases-restrict-file)
1860 (goto-char (point-min))
1861 (apply 'reftex-query-index-phrase args))))))
1862 (reftex-unhighlight 0)
1863 (set-window-configuration win-conf))))
1865 (defconst reftex-index-phrases-help
1866 " Keys for query-index search
1867 ===========================
1868 y Replace this match
1870 ! Replace this and all further matches in this file
1871 q / Q Skip match, start next file / start next phrase
1872 o Use a different indexing macro for this match
1873 1 - 9 Select one of the multiple phrases
1874 e Edit the replacement text
1876 s / S Save this buffer / Save all document buffers
1878 "The help string for indexing phrases.")
1880 (defvar replace-count)
1881 (defun reftex-query-index-phrase (phrase macro-fmt &optional
1882 index-key repeat as-words)
1883 "Search through buffer for PHRASE, and offer to replace it with an indexed
1884 version. The index version is derived by applying `format' with MACRO-FMT
1885 to INDEX-KEY or PHRASE. When REPEAT is non-nil, the PHRASE is inserted
1886 again after the macro.
1887 AS-WORDS means, the search for PHRASE should require word boundaries at
1889 (let* ((re (reftex-index-make-phrase-regexp phrase as-words 'allow-newline))
1890 (case-fold-search reftex-index-phrases-case-fold-search)
1891 (index-keys (split-string
1892 (or index-key phrase)
1893 reftex-index-phrases-logical-or-regexp))
1894 (nkeys (length index-keys))
1895 (ckey (nth 0 index-keys))
1897 match rpl char (beg (make-marker)) (end (make-marker)) mathp)
1901 (while (re-search-forward re nil t)
1903 (if (reftex-in-comment)
1904 (throw 'next-match nil))
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))
1911 (condition-case nil (texmathp) (error nil))))
1912 (setq beg (move-marker beg (match-beginning 0))
1913 end (move-marker end (match-end 0)))
1914 (if (and reftex-index-phrases-skip-indexed-matches
1916 (reftex-index-phrase-match-is-indexed beg
1918 (throw 'next-match nil))
1919 (reftex-highlight 0 (match-beginning 0) (match-end 0))
1922 (reftex-index-make-replace-string
1923 macro-fmt (match-string 0) ckey repeat mathp)))
1927 (message "REPLACE: %s? (yn!qoe%s?)"
1930 (concat "1-" (int-to-string nkeys))
1932 (setq char (if all-yes ?y (read-char-exclusive)))
1933 (cond ((member char '(?y ?Y ?\ ))
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))
1941 ((member char '(?n ?N ?\C-h ?\C-?));; FIXME: DEL
1945 ;; Yes for all in this buffer
1948 ;; Stop this one in this file
1949 (goto-char (point-max))
1953 (throw 'no-more-files t))
1957 (reftex-save-all-document-buffers))
1960 ((member char '(?o ?O))
1961 ;; Select a differnt macro
1962 (let* ((nc (reftex-index-select-phrases-macro 2))
1964 (cdr (assoc nc reftex-index-phrases-macro-data)))
1965 (macro-fmt (car macro-data))
1966 (repeat (nth 1 macro-data)))
1968 (setq rpl (save-match-data
1969 (reftex-index-make-replace-string
1971 ckey repeat mathp)))
1975 (with-output-to-temp-buffer "*Help*"
1976 (princ reftex-index-phrases-help)))
1982 (substitute-command-keys
1983 "Recursive edit. Resume with \\[exit-recursive-edit]"))
1986 (setq rpl (read-string "Edit: " rpl)))
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))))
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))))
2001 (move-marker beg nil)
2002 (move-marker end nil)
2004 (reftex-unhighlight 0))))
2006 (defun reftex-index-phrase-match-is-indexed (beg end)
2007 ;; Check if match is in an argument of an index macro, or if an
2008 ;; index macro is directly attached to the match.
2011 (let* ((all-macros (reftex-what-macro t))
2012 ; (this-macro (car (car all-macros)))
2015 (goto-char (1- beg))
2016 (memq (char-after (point)) '(?\] ?\}))
2017 (car (reftex-what-macro 1))))
2019 (and (goto-char end)
2020 (looking-at "\\(\\\\[a-zA-Z]+\\*?\\)[[{]")
2024 (while (setq macro (pop all-macros))
2025 (if (member (car macro) reftex-macros-with-index)
2026 (throw 'matched t)))
2029 (member before-macro reftex-macros-with-index))
2031 (member after-macro reftex-macros-with-index))))))
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)
2037 ;; Find line boundaries and possible line breaks near BEG and END
2043 (skip-chars-backward "^ \n")
2044 (if (and (equal (preceding-char) ?\ )
2045 (string-match "\\S-" (buffer-substring bol (point))))
2046 (setq space1 (1- (point))))
2048 (skip-chars-forward "^ \n")
2049 (if (and (equal (following-char) ?\ )
2050 (string-match "\\S-" (buffer-substring (point) eol)))
2051 (setq space2 (point)))
2052 ;; Now check what we have and insert the newlines
2053 (if (<= (- eol bol) fill-column)
2054 ;; Line is already short
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
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))))))))))
2078 (defun reftex-index-phrases-replace-space (pos)
2079 "If there is a space at POS, replace it with a newline char.
2080 Does not do a save-excursion."
2081 (when (equal (char-after pos) ?\ )
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: ["
2089 (mapconcat (lambda (x) (char-to-string (car x)))
2090 reftex-index-phrases-macro-data "")
2092 (help (concat "Select an indexing macro\n========================\n"
2093 (mapconcat (lambda (x)
2096 reftex-index-phrases-macro-data "\n"))))
2097 (reftex-select-with-char prompt help delay)))
2100 ;;; reftex-index.el ends here