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