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