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