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