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