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