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