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