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/>.
27 (eval-when-compile (require 'cl
))
28 (declare-function texmathp
"ext:texmathp" ())
32 ;; START remove for XEmacs release
34 (defvar transient-mark-mode
)
36 ;; END remove for XEmacs release
38 (defun reftex-index-selection-or-word (&optional arg phrase
)
39 "Put selection or the word near point into the default index macro.
40 This uses the information in `reftex-index-default-macro' to make an index
41 entry. The phrase indexed is the current selection or the word near point.
42 When called with one `C-u' prefix, let the user have a chance to edit the
43 index entry. When called with 2 `C-u' as prefix, also ask for the index
44 macro and other stuff.
45 When called inside TeX math mode as determined by the `texmathp.el' library
46 which is part of AUCTeX, the string is first processed with the
47 `reftex-index-math-format', which see."
49 (let* ((use-default (not (equal arg
'(16)))) ; check for double prefix
50 ;; check if we have an active selection
51 (active (reftex-region-active-p))
55 (skip-syntax-backward "w\\") (point))))
59 (skip-syntax-forward "w\\") (point))))
60 (sel (buffer-substring beg end
))
61 (mathp (condition-case nil
(texmathp) (error nil
)))
62 (current-prefix-arg nil
) ; we want to call reftex-index without prefix.
63 key def-char def-tag full-entry
)
67 (reftex-index-visit-phrases-buffer)
68 (reftex-index-new-phrase sel
))
71 ;; Nothing selected, no word, so use full reftex-index command
73 ;; OK, we have something to index here.
74 ;; Add the dollars when necessary
76 (format reftex-index-math-format sel
)
78 ;; Get info from `reftex-index-default-macro'
79 (setq def-char
(if use-default
(car reftex-index-default-macro
)))
80 (setq def-tag
(if use-default
(nth 1 reftex-index-default-macro
)))
81 ;; Does the user want to edit the entry?
82 (setq full-entry
(if arg
83 (reftex-index-complete-key
84 def-tag nil
(cons key
0))
86 ;; Delete what is in the buffer and make the index entry
87 (delete-region beg end
)
88 (reftex-index def-char full-entry def-tag sel
)))))
90 (defun reftex-index (&optional char key tag sel no-insert
)
91 "Query for an index macro and insert it along with its arguments.
92 The index macros available are those defined in `reftex-index-macro' or
93 by a call to `reftex-add-index-macros', typically from an AUCTeX style file.
94 RefteX provides completion for the index tag and the index key, and
95 will prompt for other arguments."
99 ;; Ensure access to scanning info
100 (reftex-ensure-index-support t
)
101 (reftex-access-scan-info current-prefix-arg
)
103 ;; Find out which macro we are going to use
104 (let* ((char (or char
105 (reftex-select-with-char reftex-query-index-macro-prompt
106 reftex-query-index-macro-help
)))
107 (macro (nth 1 (assoc char reftex-key-to-index-macro-alist
)))
108 (entry (or (assoc macro reftex-index-macro-alist
)
109 (error "No index macro associated with %c" char
)))
111 (tag (or tag
(nth 1 entry
)))
112 (nargs (nth 4 entry
))
113 (nindex (nth 5 entry
))
114 (opt-args (nth 6 entry
))
115 (repeat (nth 7 entry
))
118 ;; Get the supported arguments
121 (setq tag1
(or (reftex-index-complete-tag tag opt-args
) "")))
123 (reftex-index-complete-key
124 (if (string= tag1
"") "idx" tag1
)
125 (member nindex opt-args
))))
127 ;; Insert the macro and ask for any additional args
129 (loop for i from
1 to nargs do
130 (setq opt
(member i opt-args
)
131 value
(cond ((= nindex i
) key
)
132 ((equal ntag i
) tag1
)
133 (t (read-string (concat "Macro arg nr. "
135 (if opt
" (optional)" "")
137 (unless (and opt
(string= value
""))
138 (insert (if opt
"[" "{") value
(if opt
"]" "}"))))
139 (and repeat
(stringp sel
) (insert sel
))
140 (and key reftex-plug-into-AUCTeX
(fboundp 'LaTeX-add-index-entries
)
141 (LaTeX-add-index-entries key
))
142 (reftex-index-update-taglist tag1
)
143 (reftex-notice-new)))
145 (defun reftex-default-index ()
146 (cond ((null reftex-index-default-tag
) nil
)
147 ((stringp reftex-index-default-tag
) reftex-index-default-tag
)
148 (t (or (get reftex-docstruct-symbol
'default-index-tag
)
151 (defun reftex-update-default-index (tag &optional tag-list
)
152 (if (and (not (equal tag
""))
154 (eq reftex-index-default-tag
'last
)
156 (member tag tag-list
)))
157 (put reftex-docstruct-symbol
'default-index-tag tag
)))
159 (defun reftex-index-complete-tag (&optional itag opt-args
)
160 ;; Ask the user for a tag, completing on known tags.
161 ;; ITAG is the argument number which contains the tag.
162 ;; OPT-ARGS is a list of optional argument indices, as given by
163 ;; `reftex-parse-args'.
164 (let* ((opt (and (integerp itag
) (member itag opt-args
)))
165 (index-tags (cdr (assq 'index-tags
166 (symbol-value reftex-docstruct-symbol
))))
167 (default (reftex-default-index))
168 (prompt (concat "Index tag"
172 (if opt
"optional" "")
174 (concat (if opt
", " "")
175 (format "default %s" default
))
178 (tag (completing-read prompt
(mapcar 'list index-tags
))))
179 (if (and default
(equal tag
"")) (setq tag default
))
180 (reftex-update-default-index tag
)
183 (defun reftex-index-select-tag ()
184 ;; Have the user select an index tag.
185 ;; FIXME: should we cache tag-alist, prompt and help?
186 (let* ((index-tags (cdr (assoc 'index-tags
187 (symbol-value reftex-docstruct-symbol
))))
188 (default (reftex-default-index)))
191 (error "No index tags available"))
193 ((= (length index-tags
) 1)
194 ;; Just one index, use it
197 ((> (length index-tags
) 1)
198 ;; Several indices, ask.
199 (let* ((tags (copy-sequence index-tags
))
201 tag-alist i val len tag prompt help rpl
)
202 ;; Move idx and glo up in the list to ensure ?i and ?g shortcuts
203 (if (member "glo" tags
)
204 (setq tags
(cons "glo" (delete "glo" tags
))))
205 (if (member "idx" tags
)
206 (setq tags
(cons "idx" (delete "idx" tags
))))
207 ;; Find unique shortcuts for each index.
208 (while (setq tag
(pop tags
))
209 (setq len
(length tag
)
213 (while (and (< (incf i
) len
) (null val
))
214 (unless (assq (aref tag i
) tag-alist
)
215 (push (list (aref tag i
)
217 (concat (substring tag
0 i
)
218 "[" (substring tag i
(incf i
)) "]"
222 (push (list (+ ?
0 (incf cnt
)) tag
223 (concat "[" (int-to-string cnt
) "]:" tag
))
225 (setq tag-alist
(nreverse tag-alist
))
226 ;; Compute Prompt and Help strings
229 (format "Select Index%s: "
230 (if default
(format " (Default <%s>)" default
) ""))
231 (mapconcat (lambda(x) (nth 2 x
)) tag-alist
" ")))
233 (concat "Select an Index\n===============\n"
235 (format "[^M] %s (the default)\n" default
)
237 (mapconcat (lambda(x)
238 (apply 'format
"[%c] %s" x
))
240 ;; Query the user for an index-tag
241 (setq rpl
(reftex-select-with-char prompt help
3 t
))
243 (if (and default
(equal rpl ?\C-m
))
245 (if (assq rpl tag-alist
)
247 (reftex-update-default-index (nth 1 (assq rpl tag-alist
)))
248 (nth 1 (assq rpl tag-alist
)))
249 (error "No index tag associated with %c" rpl
)))))
250 (t (error "This should not happen (reftex-index-select-tag)")))))
252 (defun reftex-index-complete-key (&optional tag optional initial
)
253 ;; Read an index key, with completion.
254 ;; Restrict completion table on index tag TAG.
255 ;; OPTIONAL indicates if the arg is optional.
256 (let* ((table (reftex-sublist-nth
257 (symbol-value reftex-docstruct-symbol
) 6
258 (lambda(x) (and (eq (car x
) 'index
)
259 (string= (nth 1 x
) (or tag
""))))
261 (prompt (concat "Index key" (if optional
" (optional)" "") ": "))
262 (key (completing-read prompt table nil nil initial
)))
265 (defun reftex-index-update-taglist (newtag)
266 ;; add NEWTAG to the list of available index tags.
267 (let ((cell (assoc 'index-tags
(symbol-value reftex-docstruct-symbol
))))
268 (and newtag
(cdr cell
) (not (member newtag
(cdr cell
)))
269 (push newtag
(cdr cell
)))))
271 (define-obsolete-variable-alias
272 'reftex-index-map
'reftex-index-mode-map
"24.1")
273 (defvar reftex-index-mode-map
274 (let ((map (make-sparse-keymap)))
276 (define-key map
(if (featurep 'xemacs
) [(button2)] [(mouse-2)])
277 'reftex-index-mouse-goto-line-and-hide
)
278 (define-key map
[follow-link
] 'mouse-face
)
280 (substitute-key-definition
281 'next-line
'reftex-index-next map global-map
)
282 (substitute-key-definition
283 'previous-line
'reftex-index-previous map global-map
)
286 '(("n" . reftex-index-next
)
287 ("p" . reftex-index-previous
)
288 ("?" . reftex-index-show-help
)
289 (" " . reftex-index-view-entry
)
290 ("\C-m" . reftex-index-goto-entry-and-hide
)
291 ("\C-i" . reftex-index-goto-entry
)
292 ("\C-k" . reftex-index-kill
)
293 ("r" . reftex-index-rescan
)
294 ("R" . reftex-index-Rescan
)
295 ("g" . revert-buffer
)
296 ("q" . reftex-index-quit
)
297 ("k" . reftex-index-quit-and-kill
)
298 ("f" . reftex-index-toggle-follow
)
299 ("s" . reftex-index-switch-index-tag
)
300 ("e" . reftex-index-edit
)
301 ("^" . reftex-index-level-up
)
302 ("_" . reftex-index-level-down
)
303 ("}" . reftex-index-restrict-to-section
)
304 ("{" . reftex-index-widen
)
305 (">" . reftex-index-restriction-forward
)
306 ("<" . reftex-index-restriction-backward
)
307 ("(" . reftex-index-toggle-range-beginning
)
308 (")" . reftex-index-toggle-range-end
)
309 ("|" . reftex-index-edit-attribute
)
310 ("@" . reftex-index-edit-visual
)
311 ("*" . reftex-index-edit-key
)
312 ("\C-c=". reftex-index-goto-toc
)
313 ("c" . reftex-index-toggle-context
))
314 do
(define-key map
(car x
) (cdr x
)))
316 (loop for key across
"0123456789" do
317 (define-key map
(vector (list key
)) 'digit-argument
))
318 (define-key map
"-" 'negative-argument
)
320 ;; The capital letters and the exclamation mark
321 (loop for key across
(concat "!" reftex-index-section-letters
) do
322 (define-key map
(vector (list key
))
323 (list 'lambda
'() '(interactive)
324 (list 'reftex-index-goto-letter key
))))
326 (easy-menu-define reftex-index-menu map
327 "Menu for Index buffer"
330 (message "To go to a section, just press any of: !%s"
331 reftex-index-section-letters
) t
]
332 ["Show Entry" reftex-index-view-entry t
]
333 ["Go To Entry" reftex-index-goto-entry t
]
334 ["Exit & Go To Entry" reftex-index-goto-entry-and-hide t
]
335 ["Table of Contents" reftex-index-goto-toc t
]
336 ["Quit" reftex-index-quit t
]
339 ["Rebuilt *Index* Buffer" revert-buffer t
]
341 ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans
]
342 ["Rescan Entire Document" reftex-index-Rescan t
])
344 ["Restrict to section" reftex-index-restrict-to-section t
]
345 ["Widen" reftex-index-widen reftex-index-restriction-indicator
]
346 ["Next Section" reftex-index-restriction-forward
347 reftex-index-restriction-indicator
]
348 ["Previous Section" reftex-index-restriction-backward
349 reftex-index-restriction-indicator
])
351 ["Edit Entry" reftex-index-edit t
]
352 ["Edit Key" reftex-index-edit-key t
]
353 ["Edit Attribute" reftex-index-edit-attribute t
]
354 ["Edit Visual" reftex-index-edit-visual t
]
356 ["Add Parentkey" reftex-index-level-down t
]
357 ["Remove Parentkey " reftex-index-level-up t
]
359 ["Make Start-of-Range" reftex-index-toggle-range-beginning t
]
360 ["Make End-of-Range" reftex-index-toggle-range-end t
]
362 ["Kill Entry" reftex-index-kill nil
]
364 ["Undo" reftex-index-undo nil
])
366 ["Context" reftex-index-toggle-context
:style toggle
367 :selected reftex-index-include-context
]
369 ["Follow Mode" reftex-index-toggle-follow
:style toggle
370 :selected reftex-index-follow-mode
])
372 ["Help" reftex-index-show-help t
]))
375 "Keymap used for *Index* buffers.")
377 (defvar reftex-index-menu
)
379 (defvar reftex-last-index-file nil
380 "Stores the file name from which `reftex-display-index' was called.")
381 (defvar reftex-index-tag nil
382 "Stores the tag of the index in an index buffer.")
384 (defvar reftex-index-return-marker
(make-marker)
385 "Marker which makes it possible to return from index to old position.")
387 (defvar reftex-index-restriction-indicator nil
)
388 (defvar reftex-index-restriction-data nil
)
390 (define-derived-mode reftex-index-mode fundamental-mode
"RefTeX Index"
391 "Major mode for managing Index buffers for LaTeX files.
392 This buffer was created with RefTeX.
393 Press `?' for a summary of important key bindings, or check the menu.
395 Here are all local bindings.
397 \\{reftex-index-mode-map}"
398 (set (make-local-variable 'revert-buffer-function
) 'reftex-index-revert
)
399 (set (make-local-variable 'reftex-index-restriction-data
) nil
)
400 (set (make-local-variable 'reftex-index-restriction-indicator
) nil
)
401 (setq mode-line-format
402 (list "---- " 'mode-line-buffer-identification
403 " " 'global-mode-string
404 " R<" 'reftex-index-restriction-indicator
">"
406 (setq truncate-lines t
)
407 (when (featurep 'xemacs
)
408 ;; XEmacs needs the call to make-local-hook
409 (make-local-hook 'post-command-hook
)
410 (make-local-hook 'pre-command-hook
))
411 (make-local-variable 'reftex-last-follow-point
)
412 (easy-menu-add reftex-index-menu reftex-index-mode-map
)
413 (add-hook 'post-command-hook
'reftex-index-post-command-hook nil t
)
414 (add-hook 'pre-command-hook
'reftex-index-pre-command-hook nil t
))
416 (defconst reftex-index-help
417 " AVAILABLE KEYS IN INDEX BUFFER
418 ==============================
419 ! A..Z Goto the section of entries starting with this letter.
420 n / p next-entry / previous-entry
421 SPC / TAB Show/Goto the corresponding entry in the LaTeX document.
422 RET Goto the entry and hide the *Index* window (also on mouse-2).
423 q / k Hide/Kill *Index* buffer.
424 C-c = Switch to the TOC buffer.
425 f / c Toggle follow mode / Toggle display of [c]ontext.
426 g Refresh *Index* buffer.
427 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
428 s Switch to a different index (for documents with multiple indices).
429 e / C-k Edit/Kill the entry.
430 * | @ Edit specific part of entry: [*]key [|]attribute [@]visual
431 With prefix: kill that part.
432 \( ) Toggle entry's beginning/end of page range property.
433 _ ^ Add/Remove parent key (to make this item a subitem).
434 } / { Restrict Index to a single document section / Widen.
435 < / > When restricted, move restriction to previous/next section.")
437 (defun reftex-index-show-entry (data &optional no-revisit
)
438 ;; Find an index entry associated with DATA and display it highlighted
439 ;; in another window. NO-REVISIT means we are not allowed to visit
441 ;; Note: This function just looks for the nearest match of the
442 ;; context string and may fail if the entry moved and an identical
443 ;; entry is close to the old position. Frequent rescans make this
445 (let* ((file (nth 3 data
))
446 (literal (nth 2 data
))
448 (re (regexp-quote literal
))
451 ((or (not no-revisit
)
452 (reftex-get-buffer-visiting file
))
453 (switch-to-buffer-other-window
454 (reftex-get-file-buffer-force file nil
))
455 (goto-char (or pos
(point-min)))
457 (reftex-nearest-match re
(length literal
))))
458 (t (message "%s" reftex-no-follow-message
) nil
))))
460 (goto-char (match-beginning 0))
462 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
465 (defun reftex-display-index (&optional tag overriding-restriction redo
467 "Display a buffer with an index compiled from the current document.
468 When the document has multiple indices, first prompts for the correct one.
469 When index support is turned off, offer to turn it on.
470 With one or two `C-u' prefixes, rescan document first.
471 With prefix 2, restrict index to current document section.
472 With prefix 3, restrict index to region."
476 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4).
477 (let ((current-prefix-arg current-prefix-arg
))
478 (reftex-ensure-index-support t
)
479 (reftex-access-scan-info current-prefix-arg
))
481 (set-marker reftex-index-return-marker
(point))
482 (setq reftex-last-follow-point
1)
484 ;; Determine the correct index to process
485 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
486 (docstruct-symbol reftex-docstruct-symbol
)
487 (index-tag (or tag
(reftex-index-select-tag)))
488 (master (reftex-TeX-master-file))
489 (calling-file (buffer-file-name))
491 (or overriding-restriction
493 (reftex-get-restriction current-prefix-arg docstruct
))))
495 ;; See if we are on an index macro as initial position
497 (let* ((what-macro (reftex-what-macro-safe 1))
498 (macro (car what-macro
))
499 (here-I-am (when (member macro reftex-macros-with-index
)
501 (goto-char (+ (cdr what-macro
)
503 (reftex-move-over-touching-args)
504 (reftex-where-am-I)))))
505 (if (eq (car (car here-I-am
)) 'index
)
506 (list (car here-I-am
))))))
509 (setq buffer-name
(reftex-make-index-buffer-name index-tag
))
511 ;; Goto the buffer and put it into the correct mode
513 (when (or restriction current-prefix-arg
)
514 (reftex-kill-buffer buffer-name
))
516 (if (get-buffer-window buffer-name
)
517 (select-window (get-buffer-window buffer-name
))
518 (switch-to-buffer buffer-name
))
520 (or (eq major-mode
'reftex-index-mode
) (reftex-index-mode))
522 ;; If the buffer is currently restricted, empty it to force update.
523 (when reftex-index-restriction-data
524 (reftex-erase-buffer))
525 (set (make-local-variable 'reftex-last-index-file
) calling-file
)
526 (set (make-local-variable 'reftex-index-tag
) index-tag
)
527 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
529 (setq reftex-index-restriction-indicator
(car restriction
)
530 reftex-index-restriction-data
(cdr restriction
))
532 (setq reftex-index-restriction-indicator nil
533 reftex-index-restriction-data nil
)))
534 (when (= (buffer-size) 0)
535 ;; buffer is empty - fill it
536 (message "Building %s buffer..." buffer-name
)
538 (setq buffer-read-only nil
)
542 SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help
543 ------------------------------------------------------------------------------
544 " index-tag
(abbreviate-file-name master
)
545 (if (eq (car (car reftex-index-restriction-data
)) 'toc
)
546 (nth 2 (car reftex-index-restriction-data
))
547 reftex-index-restriction-indicator
)))
549 (if (reftex-use-fonts)
550 (put-text-property 1 (point) 'face reftex-index-header-face
))
551 (put-text-property 1 (point) 'intangible t
)
553 (reftex-insert-index docstruct index-tag
)
554 (goto-char (point-min))
555 (run-hooks 'reftex-display-copied-context-hook
)
556 (message "Building %s buffer...done." buffer-name
)
557 (setq buffer-read-only t
))
558 (and locations
(apply 'reftex-find-start-point
(point) locations
))
559 (if reftex-index-restriction-indicator
560 (message "Index restricted: <%s>" reftex-index-restriction-indicator
))))
562 (defun reftex-insert-index (docstruct tag
&optional update-one remark
)
563 ;; Insert an index into the current buffer. Entries are from the
565 ;; TAG is the subindex to process.
566 ;; UPDATE-ONE: When non-nil, delete the entry at point and replace
567 ;; it with whatever the DOCSTRUCT contains.
568 ;; REMARK can be a note to add to the entry.
569 (let* ((all docstruct
)
571 (context reftex-index-include-context
)
572 (context-indent (concat indent
" "))
573 (section-chars (mapcar 'identity reftex-index-section-letters
))
574 (this-section-char 0)
575 (font (reftex-use-fonts))
576 (bor (car reftex-index-restriction-data
))
577 (eor (nth 1 reftex-index-restriction-data
))
579 (if (memq reftex-highlight-selection
'(mouse both
))
580 reftex-mouse-selected-face
582 (index-face reftex-label-face
)
583 sublist cell from to first-char
)
585 ;; Make the sublist and sort it
587 (setq all
(or (memq bor all
) all
)))
589 (while (setq cell
(pop all
))
592 (and (eq (car cell
) 'index
)
593 (equal (nth 1 cell
) tag
)
594 (push cell sublist
))))
595 (setq sublist
(sort (nreverse sublist
)
596 (lambda (a b
) (string< (nth 8 a
) (nth 8 b
)))))
599 ;; Delete the entry at place
600 (and (bolp) (forward-char 1))
601 (delete-region (previous-single-property-change (1+ (point)) :data
)
602 (or (next-single-property-change (point) :data
)
605 ;; Walk through the list and insert all entries
606 (while (setq cell
(pop sublist
))
608 (setq first-char
(upcase (string-to-char (nth 6 cell
))))
609 (when (and (not (equal first-char this-section-char
))
610 (member first-char section-chars
))
611 ;; There is a new initial letter, so start a new section
612 (reftex-index-insert-new-letter first-char font
)
613 (setq section-chars
(delete first-char section-chars
)
614 this-section-char first-char
))
615 (when (= this-section-char
0)
616 (setq this-section-char ?
!)
617 (reftex-index-insert-new-letter this-section-char font
)))
620 (insert indent
(nth 7 cell
))
624 (- (point) (length (nth 7 cell
))) to
628 (when (or remark
(nth 9 cell
))
629 (and (< (current-column) 40)
630 ;; FIXME: maybe this is too slow?
631 (insert (make-string (max (- 40 (current-column)) 0) ?\
)))
632 (and (nth 9 cell
) (insert " " (substring (nth 5 cell
) (nth 9 cell
))))
633 (and remark
(insert " " remark
)))
639 (insert context-indent
(nth 2 cell
) "\n")
641 (put-text-property from to
:data cell
)
643 (put-text-property from
(1- to
)
644 'mouse-face mouse-face
))
648 (defun reftex-index-insert-new-letter (letter &optional font
)
649 ;; Start a new section in the index
650 (let ((from (point)))
651 (insert "\n" letter letter letter
652 "-----------------------------------------------------------------")
654 (put-text-property from
(point) 'face reftex-index-section-face
))
657 (defun reftex-get-restriction (arg docstruct
)
658 ;; Interpret the prefix ARG and derive index restriction specs.
659 (let* ((beg (min (point) (or (condition-case nil
(mark) (error nil
))
661 (end (max (point) (or (condition-case nil
(mark) (error nil
))
663 bor eor label here-I-am
)
666 (setq here-I-am
(car (reftex-where-am-I))
667 bor
(if (eq (car here-I-am
) 'toc
)
669 (reftex-last-assoc-before-elt
670 'toc here-I-am docstruct
))
671 eor
(car (memq (assq 'toc
(cdr (memq bor docstruct
))) docstruct
))
675 (setq label
"region")
677 (setq bor
(car (reftex-where-am-I)))
678 (setq bor
(nth 1 (memq bor docstruct
)))
680 (setq eor
(nth 1 (memq (car (reftex-where-am-I)) docstruct
)))))
682 (if (and label
(or bor eor
))
686 (defun reftex-index-pre-command-hook ()
687 ;; Used as pre command hook in *Index* buffer
688 (reftex-unhighlight 0)
689 (reftex-unhighlight 1))
691 (defun reftex-index-post-command-hook ()
692 ;; Used in the post-command-hook for the *Index* buffer
693 (when (get-text-property (point) :data
)
695 (not (get-text-property (point) 'intangible
))
696 (memq reftex-highlight-selection
'(cursor both
))
698 (or (previous-single-property-change (1+ (point)) :data
)
700 (or (next-single-property-change (point) :data
)
702 (if (integerp reftex-index-follow-mode
)
703 ;; Remove delayed action
704 (setq reftex-index-follow-mode t
)
705 (and reftex-index-follow-mode
706 (not (equal reftex-last-follow-point
(point)))
707 ;; Show context in other window
708 (setq reftex-last-follow-point
(point))
710 (reftex-index-visit-location nil
(not reftex-revisit-to-follow
))
713 (defun reftex-index-show-help ()
714 "Show a summary of special key bindings."
716 (with-output-to-temp-buffer "*RefTeX Help*"
717 (princ reftex-index-help
))
718 (reftex-enlarge-to-fit "*RefTeX Help*" t
)
719 ;; If follow mode is active, arrange to delay it one command
720 (if reftex-index-follow-mode
721 (setq reftex-index-follow-mode
1)))
723 (defun reftex-index-next (&optional arg
)
724 "Move to next selectable item."
726 (setq reftex-callback-fwd t
)
727 (or (eobp) (forward-char 1))
728 (goto-char (or (next-single-property-change (point) :data
)
730 (unless (get-text-property (point) :data
)
731 (goto-char (or (next-single-property-change (point) :data
)
733 (defun reftex-index-previous (&optional arg
)
734 "Move to previous selectable item."
736 (setq reftex-callback-fwd nil
)
737 (goto-char (or (previous-single-property-change (point) :data
)
739 (unless (get-text-property (point) :data
)
740 (goto-char (or (previous-single-property-change (point) :data
)
742 (defun reftex-index-toggle-follow ()
743 "Toggle follow (other window follows with context)."
745 (setq reftex-last-follow-point -
1)
746 (setq reftex-index-follow-mode
(not reftex-index-follow-mode
)))
747 (defun reftex-index-toggle-context ()
748 "Toggle inclusion of label context in *Index* buffer.
749 Label context is only displayed when the labels are there as well."
751 (setq reftex-index-include-context
(not reftex-index-include-context
))
752 (reftex-index-revert))
753 (defun reftex-index-view-entry ()
754 "View document location in other window."
756 (reftex-index-visit-location))
757 (defun reftex-index-goto-entry-and-hide ()
758 "Go to document location in other window. Hide the *Index* window."
760 (reftex-index-visit-location 'hide
))
761 (defun reftex-index-goto-entry ()
762 "Go to document location in other window. *Index* window stays."
764 (reftex-index-visit-location t
))
765 (defun reftex-index-mouse-goto-line-and-hide (ev)
766 "Go to document location in other window. Hide the *Index* window."
769 (reftex-index-visit-location 'hide
))
770 (defun reftex-index-quit ()
771 "Hide the *Index* window and do not move point."
773 (or (one-window-p) (delete-window))
774 (switch-to-buffer (marker-buffer reftex-index-return-marker
))
775 (goto-char (or (marker-position reftex-index-return-marker
) (point))))
776 (defun reftex-index-quit-and-kill ()
777 "Kill the *Index* buffer."
779 (kill-buffer (current-buffer))
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-goto-toc (&rest ignore
)
784 "Switch to the table of contents of the current document.
785 The function will go to the section where the entry at point was defined."
787 (if (get-text-property (point) :data
)
788 (reftex-index-goto-entry)
789 (switch-to-buffer (marker-buffer reftex-index-return-marker
)))
790 (delete-other-windows)
792 (defun reftex-index-rescan (&rest ignore
)
793 "Regenerate the *Index* buffer after reparsing file of section at point."
795 (let ((index-tag reftex-index-tag
))
796 (if (and reftex-enable-partial-scans
797 (null current-prefix-arg
))
798 (let* ((data (get-text-property (point) :data
))
800 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
802 (error "Don't know which file to rescan. Try `C-u r'")
803 (switch-to-buffer (reftex-get-file-buffer-force file
))
804 (setq current-prefix-arg
'(4))
805 (reftex-display-index index-tag nil
'redo line
)))
806 (reftex-index-Rescan))
807 (reftex-kill-temporary-buffers)))
808 (defun reftex-index-Rescan (&rest ignore
)
809 "Regenerate the *Index* buffer after reparsing the entire document."
811 (let ((index-tag reftex-index-tag
)
812 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
814 (reftex-get-file-buffer-force reftex-last-index-file
))
815 (setq current-prefix-arg
'(16))
816 (reftex-display-index index-tag nil
'redo line
)))
817 (defun reftex-index-revert (&rest ignore
)
818 "Regenerate the *Index* from the internal lists. No reparsing os done."
820 (let ((buf (current-buffer))
821 (index-tag reftex-index-tag
)
822 (data (get-text-property (point) :data
))
823 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
825 (reftex-get-file-buffer-force reftex-last-index-file
))
826 (reftex-erase-buffer buf
)
827 (setq current-prefix-arg nil
828 reftex-last-follow-point
1)
829 (reftex-display-index index-tag nil
'redo data line
)))
830 (defun reftex-index-switch-index-tag (&rest ignore
)
831 "Switch to a different index of the same document."
834 (reftex-get-file-buffer-force reftex-last-index-file
))
835 (setq current-prefix-arg nil
)
836 (reftex-display-index nil nil
'redo
))
838 (defun reftex-index-restrict-to-section (&optional force
)
839 "Restrict index to entries defined in same document sect. as entry at point."
840 ;; Optional FORCE means, even if point is not on an index entry.
842 (let* ((data (get-text-property (point) :data
))
843 (docstruct (symbol-value reftex-docstruct-symbol
))
845 (if (and (not data
) force
)
846 (setq data
(assq 'toc docstruct
)))
848 (setq bor
(reftex-last-assoc-before-elt 'toc data docstruct
)
849 eor
(car (memq (assq 'toc
(cdr (memq bor docstruct
)))
851 reftex-index-restriction-data
(list bor eor
)
852 reftex-index-restriction-indicator
(nth 6 bor
) )))
853 (reftex-index-revert))
855 (defun reftex-index-widen (&rest ignore
)
856 "Show the unrestricted index (all entries)."
858 (setq reftex-index-restriction-indicator nil
859 reftex-index-restriction-data nil
)
860 (reftex-index-revert)
861 (message "Index widened"))
862 (defun reftex-index-restriction-forward (&rest ignore
)
863 "Restrict to previous section.
864 When index is currently unrestricted, restrict it to a section.
865 When index is restricted, select the next section as restriction criterion."
867 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
868 (bor (nth 1 reftex-index-restriction-data
)))
870 (not (eq (car bor
) 'toc
)))
871 (reftex-index-restrict-to-section t
)
872 (setq reftex-index-restriction-indicator
(nth 6 bor
)
873 reftex-index-restriction-data
875 (car (memq (assq 'toc
(cdr (memq bor docstruct
)))
877 (reftex-index-revert))))
878 (defun reftex-index-restriction-backward (&rest ignore
)
879 "Restrict to next section.
880 When index is currently unrestricted, restrict it to a section.
881 When index is restricted, select the previous section as restriction criterion."
883 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
884 (eor (car reftex-index-restriction-data
))
885 (bor (reftex-last-assoc-before-elt 'toc eor docstruct t
)))
887 (not (eq (car bor
) 'toc
)))
888 (reftex-index-restrict-to-section t
)
889 (setq reftex-index-restriction-indicator
(nth 6 bor
)
890 reftex-index-restriction-data
892 (reftex-index-revert))))
894 (defun reftex-index-visit-location (&optional final no-revisit
)
895 ;; Visit the tex file corresponding to the index entry on the current line.
896 ;; If FINAL is t, stay there
897 ;; If FINAL is 'hide, hide the *Index* window.
898 ;; Otherwise, move cursor back into *Index* window.
899 ;; NO-REVISIT means don't visit files, just use live buffers.
901 (let* ((data (get-text-property (point) :data
))
902 (index-window (selected-window))
903 show-window show-buffer match
)
905 (unless data
(error "Don't know which index entry to visit"))
907 (if (eq (car data
) 'index
)
908 (setq match
(reftex-index-show-entry data no-revisit
)))
910 (setq show-window
(selected-window)
911 show-buffer
(current-buffer))
914 (select-window index-window
)
915 (error "Cannot find location"))
917 (select-window index-window
)
919 ;; Use the `final' parameter to decide what to do next
922 (reftex-unhighlight 0)
923 (select-window show-window
))
925 (reftex-unhighlight 0)
926 (or (one-window-p) (delete-window))
927 (switch-to-buffer show-buffer
))
930 (defun reftex-index-analyze-entry (data)
931 ;; This splits the index context so that key, attribute and visual
932 ;; values are accessible individually.
934 (let* ((arg (nth 5 data
))
935 (context (nth 2 data
))
936 (sc reftex-index-special-chars
)
937 (boa (if (string-match (regexp-quote (concat "{" arg
"}")) context
)
938 (1+ (match-beginning 0))
939 (error "Something is wrong here")))
940 (eoa (1- (match-end 0)))
941 (boactual (if (string-match (concat "[^" (nth 3 sc
) "]" (nth 2 sc
))
943 (1+ (match-beginning 0))
945 (boattr (if (string-match (concat "[^" (nth 3 sc
) "]" (nth 1 sc
))
947 (1+ (match-beginning 0))
949 (pre (substring context
0 boa
))
950 (key (substring context boa boattr
))
951 (attr (substring context boattr boactual
))
952 (actual (substring context boactual eoa
))
953 (post (substring context eoa
)))
954 (list pre key attr actual post
)))
956 (defun reftex-index-edit ()
957 "Edit the index entry at point."
959 (let* ((data (get-text-property (point) :data
))
961 (unless data
(error "Don't know which index entry to edit"))
962 (reftex-index-view-entry)
963 (setq old
(nth 2 data
) new
(read-string "Edit: " old
))
964 (reftex-index-change-entry new
)))
966 (defun reftex-index-toggle-range-beginning ()
967 "Toggle the page range start attribute `|('."
969 (let* ((data (get-text-property (point) :data
))
970 (bor (concat (nth 1 reftex-index-special-chars
) "("))
972 (unless data
(error "Don't know which index entry to edit"))
973 (setq analyze
(reftex-index-analyze-entry data
)
974 attr
(nth 2 analyze
))
975 (setf (nth 2 analyze
) (if (string= attr bor
) "" bor
))
976 (setq new
(apply 'concat analyze
))
977 (reftex-index-change-entry
978 new
(if (string= (nth 2 analyze
) bor
)
979 "Entry is now START-OF-PAGE-RANGE"
980 "START-OF-PAGE-RANGE canceled"))))
982 (defun reftex-index-toggle-range-end ()
983 "Toggle the page-range-end attribute `|)'."
985 (let* ((data (get-text-property (point) :data
))
986 (eor (concat (nth 1 reftex-index-special-chars
) ")"))
988 (unless data
(error "Don't know which index entry to edit"))
989 (setq analyze
(reftex-index-analyze-entry data
)
990 attr
(nth 2 analyze
))
991 (setf (nth 2 analyze
) (if (string= attr eor
) "" eor
))
992 (setq new
(apply 'concat analyze
))
993 (reftex-index-change-entry
994 new
(if (string= (nth 2 analyze
) eor
)
995 "Entry is now END-OF-PAGE-RANGE"
996 "END-OF-PAGE-RANGE canceled"))))
998 (defun reftex-index-edit-key ()
999 "Edit the KEY part of the index entry."
1001 (reftex-index-edit-part nil
1 "" "Key: " t
))
1003 (defun reftex-index-edit-attribute (&optional arg
)
1004 "EDIT the ATTRIBUTE part of the entry. With arg: remove entire ATTRIBUTE."
1006 (reftex-index-edit-part arg
2 (nth 1 reftex-index-special-chars
)
1009 (defun reftex-index-edit-visual (&optional arg
)
1010 "EDIT the VISUAL part of the entry. With arg: remove entire VISUAL string."
1012 (reftex-index-edit-part arg
3 (nth 2 reftex-index-special-chars
) "Visual: "))
1014 (defun reftex-index-edit-part (arg n initial prompt
&optional dont-allow-empty
)
1015 ;; This function does the work for all partial editing commands
1016 (let* ((data (get-text-property (point) :data
))
1017 new analyze opart npart
)
1018 (unless data
(error "Don't know which index entry to edit"))
1019 ;; Analyze the whole context string
1020 (setq analyze
(reftex-index-analyze-entry data
)
1021 opart
(nth n analyze
))
1022 (and (> (length opart
) 0) (setq opart
(substring opart
1)))
1023 ;; Have the user editing the part
1024 (setq npart
(if arg
"" (read-string (concat prompt initial
) opart
)))
1026 (cond ((string= npart opart
)
1027 (error "Not changed"))
1029 (if dont-allow-empty
1030 (error "Invalid value")
1031 (setf (nth n analyze
) npart
)))
1032 (t (setf (nth n analyze
) (concat initial npart
))))
1033 (setq new
(apply 'concat analyze
))
1034 ;; Change the entry and insert the changed version into the index.
1035 (reftex-index-change-entry
1036 new
(if (string= npart
"")
1037 (format "Deleted: %s" opart
)
1038 (format "New value is: %s" npart
)))))
1040 (defun reftex-index-level-down ()
1041 "Make index entry a subitem of another entry."
1043 (let* ((data (get-text-property (point) :data
))
1044 (docstruct (symbol-value reftex-docstruct-symbol
))
1046 (unless data
(error "Don't know which index entry to change"))
1047 (setq old
(nth 2 data
)
1049 prefix
(completing-read
1054 (and (eq (car x
) 'index
)
1055 (string= (nth 1 x
) reftex-index-tag
))) t
)))
1056 (unless (string-match
1057 (concat (regexp-quote (car reftex-index-special-chars
)) "\\'")
1059 (setq prefix
(concat prefix
(car reftex-index-special-chars
))))
1060 (if (string-match (regexp-quote key
) old
)
1061 (setq new
(replace-match (concat prefix key
) t t old
))
1062 (error "Cannot construct new index key"))
1063 (reftex-index-change-entry new
(format "Added prefix: %s" prefix
))))
1065 (defun reftex-index-level-up ()
1066 "Remove the highest level of a hierarchical index entry."
1068 (let* ((data (get-text-property (point) :data
))
1070 (unless data
(error "Don't know which entry to change"))
1071 (setq old
(nth 2 data
))
1072 (if (string-match (concat "{\\([^" (nth 0 reftex-index-special-chars
) "]*"
1073 "[^" (nth 3 reftex-index-special-chars
) "]"
1074 (regexp-quote (nth 0 reftex-index-special-chars
))
1077 (setq prefix
(substring old
(match-beginning 1) (match-end 1))
1078 new
(concat (substring old
0 (match-beginning 1))
1079 (substring old
(match-end 1))))
1080 (error "Entry is not a subitem"))
1081 (reftex-index-change-entry new
(format "Removed prefix: %s" prefix
))))
1083 (defun reftex-index-kill ()
1084 "FIXME: Not yet implemented"
1086 (error "This function is currently not implemented"))
1088 (defun reftex-index-undo ()
1089 "FIXME: Not yet implemented"
1091 (error "This function is currently not implemented"))
1093 (defun reftex-index-change-entry (new &optional message
)
1094 ;; Change the full context string of the index entry at point to
1095 ;; NEW. This actually edits the buffer where the entry is defined.
1097 (let* ((data (get-text-property (point) :data
))
1099 (unless data
(error "Cannot change entry"))
1100 (reftex-index-view-entry)
1101 (setq beg
(match-beginning 0) end
(match-end 0))
1102 (setq old
(nth 2 data
))
1103 (and (equal old new
) (error "Entry unchanged"))
1104 (with-current-buffer (get-file-buffer (nth 3 data
))
1106 (unless (looking-at (regexp-quote old
))
1107 (error "This should not happen (reftex-index-change-entry)"))
1108 (delete-region beg end
)
1110 (goto-char (1- beg
))
1111 (when (and (re-search-forward (reftex-everything-regexp) nil t
)
1113 (< (abs (- (match-beginning 10) beg
)) (length new
))
1114 (setq info
(reftex-index-info-safe buffer-file-name
)))
1115 (setcdr data
(cdr info
))))
1116 (let ((buffer-read-only nil
))
1118 (reftex-insert-index (list data
) reftex-index-tag t
1120 (setq reftex-last-follow-point
1)
1121 (and message
(message "%s" message
))))
1123 (defun reftex-index-goto-letter (char)
1124 "Go to the CHAR section in the index."
1126 (case-fold-search nil
))
1127 (goto-char (point-min))
1129 (if (re-search-forward (concat "^" (char-to-string char
)) nil t
)
1133 (reftex-index-next))
1136 (error "This <%s> index does not contain entries sorted before the letters"
1138 (error "This <%s> index does not contain entries starting with `%c'"
1139 reftex-index-tag char
)))))
1142 ;;----------------------------------------------------------------------
1143 ;; The Index Phrases File
1145 ;; Some constants and variables
1146 (defconst reftex-index-phrases-comment-regexp
"^[ \t]*%.*"
1147 "Regular expression to match comment lines in phrases buffer")
1148 (defconst reftex-index-phrases-macrodef-regexp
1149 "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\( *\t[ \t]*\\)\\([^\t]*[^ \t]\\)\\( *\t[ \t]*\\)\\(\\S-+\\)"
1150 "Regular expression to match macro definition lines the phrases buffer.")
1151 ;(defconst reftex-index-phrases-macrodef-regexp
1152 ; "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\([ \t]*\\)\\([^\t]*[^ \t]\\)\\([ \t]*\\)\\(nil\\|t\\)[ \t]*$"
1153 ; "Regular expression to match macro definition lines the phrases buffer.
1154 ;This version would allow just spaces as separators.")
1155 (defconst reftex-index-phrases-phrase-regexp1
1156 "^\\(\\S-?\\)\\(\t\\)\\([^\t\n]*\\S-\\)\\([ \t]*\\)$"
1157 "Regular expression matching phrases which have no separate index key.")
1158 (defconst reftex-index-phrases-phrase-regexp2
1159 "^\\(\\S-?\\)\\(\t\\)\\([^\t]*\\S-\\)\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)[ \t]*$"
1160 "Regular expression matching phrases which have a separate index key.")
1161 (defconst reftex-index-phrases-phrase-regexp12
1162 "^\\(\\S-?\\)\\(\t\\)\\([^\n\t]*\\S-\\)\\(\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)\\)?[ \t]*$"
1163 "Regular expression matching all types of phrase lines.")
1164 (defvar reftex-index-phrases-macro-data nil
1165 "Alist containing the information taken from the macro definition lines.
1166 This gets refreshed in every phrases command.")
1167 (defvar reftex-index-phrases-files nil
1168 "List of document files relevant for the phrases file.")
1170 (defvar reftex-index-phrases-font-lock-keywords nil
1171 "Font lock keywords for reftex-index-phrases-mode.")
1172 (defvar reftex-index-phrases-font-lock-defaults nil
1173 "Font lock defaults for reftex-index-phrases-mode.")
1174 (define-obsolete-variable-alias
1175 'reftex-index-phrases-map
'reftex-index-phrases-mode-map
"24.1")
1176 (defvar reftex-index-phrases-mode-map
1177 (let ((map (make-sparse-keymap)))
1178 ;; Keybindings and Menu for phrases buffer
1180 '(("\C-c\C-c" . reftex-index-phrases-save-and-return
)
1181 ("\C-c\C-x" . reftex-index-this-phrase
)
1182 ("\C-c\C-f" . reftex-index-next-phrase
)
1183 ("\C-c\C-r" . reftex-index-region-phrases
)
1184 ("\C-c\C-a" . reftex-index-all-phrases
)
1185 ("\C-c\C-d" . reftex-index-remaining-phrases
)
1186 ("\C-c\C-s" . reftex-index-sort-phrases
)
1187 ("\C-c\C-n" . reftex-index-new-phrase
)
1188 ("\C-c\C-m" . reftex-index-phrases-set-macro-key
)
1189 ("\C-c\C-i" . reftex-index-phrases-info
)
1190 ("\C-c\C-t" . reftex-index-find-next-conflict-phrase
)
1191 ("\C-i" . self-insert-command
))
1192 do
(define-key map
(car x
) (cdr x
)))
1194 (easy-menu-define reftex-index-phrases-menu map
1195 "Menu for Phrases buffer"
1197 ["New Phrase" reftex-index-new-phrase t
]
1198 ["Set Phrase Macro" reftex-index-phrases-set-macro-key t
]
1199 ["Recreate File Header" reftex-index-initialize-phrases-buffer t
]
1202 ["Sort" reftex-index-sort-phrases t
]
1205 ["by Search Phrase" (setq reftex-index-phrases-sort-prefers-entry nil
)
1206 :style radio
:selected
(not reftex-index-phrases-sort-prefers-entry
)]
1207 ["by Index Entry" (setq reftex-index-phrases-sort-prefers-entry t
)
1208 :style radio
:selected reftex-index-phrases-sort-prefers-entry
]
1209 ["in Blocks" (setq reftex-index-phrases-sort-in-blocks
1210 (not reftex-index-phrases-sort-in-blocks
))
1211 :style toggle
:selected reftex-index-phrases-sort-in-blocks
])
1212 ["Describe Phrase" reftex-index-phrases-info t
]
1213 ["Next Phrase Conflict" reftex-index-find-next-conflict-phrase t
]
1215 ("Find and Index in Document"
1216 ["Current Phrase" reftex-index-this-phrase t
]
1217 ["Next Phrase" reftex-index-next-phrase t
]
1218 ["Current and Following" reftex-index-remaining-phrases t
]
1219 ["Region Phrases" reftex-index-region-phrases t
]
1220 ["All Phrases" reftex-index-all-phrases t
]
1223 ["Match Whole Words" (setq reftex-index-phrases-search-whole-words
1224 (not reftex-index-phrases-search-whole-words
))
1225 :style toggle
:selected reftex-index-phrases-search-whole-words
]
1226 ["Case Sensitive Search" (setq reftex-index-phrases-case-fold-search
1227 (not reftex-index-phrases-case-fold-search
))
1228 :style toggle
:selected
(not
1229 reftex-index-phrases-case-fold-search
)]
1230 ["Wrap Long Lines" (setq reftex-index-phrases-wrap-long-lines
1231 (not reftex-index-phrases-wrap-long-lines
))
1232 :style toggle
:selected reftex-index-phrases-wrap-long-lines
]
1233 ["Skip Indexed Matches" (setq reftex-index-phrases-skip-indexed-matches
1234 (not reftex-index-phrases-skip-indexed-matches
))
1235 :style toggle
:selected reftex-index-phrases-skip-indexed-matches
])
1237 ["Save and Return" reftex-index-phrases-save-and-return t
]))
1240 "Keymap used for *toc* buffer.")
1241 (defvar reftex-index-phrases-syntax-table
1242 (let ((table (make-syntax-table)))
1243 (modify-syntax-entry ?
\" "." table
)
1245 "Syntax table for RefTeX Index Phrases mode.")
1247 (defun reftex-index-phrase-selection-or-word (arg)
1248 "Add current selection or word at point to the phrases buffer.
1249 When you are in transient-mark-mode and the region is active, the
1250 selection will be used - otherwise the word at point.
1251 You get a chance to edit the entry in the phrases buffer - finish with
1254 (set-marker reftex-index-return-marker
(point))
1255 (reftex-index-selection-or-word arg
'phrase
)
1256 (if (eq major-mode
'reftex-index-phrases-mode
)
1258 (substitute-command-keys
1259 "Return to LaTeX with \\[reftex-index-phrases-save-and-return]"))))
1261 (defun reftex-index-visit-phrases-buffer ()
1262 "Switch to the phrases buffer, initialize if empty."
1264 (reftex-access-scan-info)
1265 (set-marker reftex-index-return-marker
(point))
1266 (let* ((master (reftex-TeX-master-file))
1267 (name (concat (file-name-sans-extension master
)
1268 reftex-index-phrase-file-extension
)))
1270 (unless (eq major-mode
'reftex-index-phrases-mode
)
1271 (reftex-index-phrases-mode))
1272 (if (= (buffer-size) 0)
1273 (reftex-index-initialize-phrases-buffer master
))))
1275 (defun reftex-index-initialize-phrases-buffer (&optional master
)
1276 "Initialize the phrases buffer by creating the header.
1277 If the buffer is non-empty, delete the old header first."
1279 (let* ((case-fold-search t
)
1280 (default-key (car reftex-index-default-macro
))
1281 (default-macro (nth 1 (assoc default-key
1282 reftex-key-to-index-macro-alist
)))
1284 (sort (copy-sequence reftex-index-macro-alist
)
1285 (lambda (a b
) (equal (car a
) default-macro
))))
1286 macro entry key repeat
)
1288 (if master
(set (make-local-variable 'TeX-master
)
1289 (file-name-nondirectory master
)))
1291 (when (> (buffer-size) 0)
1294 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t
)
1296 (beginning-of-line 2)
1297 (if (looking-at reftex-index-phrases-comment-regexp
)
1298 (beginning-of-line 2))
1299 (while (looking-at "^[ \t]*$")
1300 (beginning-of-line 2))
1301 (if (featurep 'xemacs
)
1302 (zmacs-activate-region)
1303 (setq mark-active t
))
1304 (if (yes-or-no-p "Delete and rebuild header? ")
1305 (delete-region (point-min) (point))))
1307 ;; Insert the mode line
1309 (format "%% -*- mode: reftex-index-phrases; TeX-master: \"%s\" -*-\n"
1310 (file-name-nondirectory (reftex-index-phrase-tex-master))))
1311 ;; Insert the macro definitions
1312 (insert "% Key Macro Format Repeat\n")
1313 (insert "%---------------------------------------------------------------------\n")
1314 (while (setq entry
(pop macro-alist
))
1315 (setq macro
(car entry
)
1316 repeat
(nth 7 entry
)
1317 key
(car (delq nil
(mapcar (lambda (x) (if (equal (nth 1 x
) macro
)
1320 reftex-key-to-index-macro-alist
))))
1321 (insert (format ">>>INDEX_MACRO_DEFINITION:\t%s\t%-20s\t%s\n"
1322 (char-to-string key
) (concat macro
"{%s}")
1323 (if repeat
"t" "nil"))))
1324 (insert "%---------------------------------------------------------------------\n\n\n")))
1327 (defun reftex-index-phrase-tex-master (&optional dir
)
1328 "Return the name of the master file associated with a phrase buffer."
1329 (if (and (boundp 'TeX-master
)
1330 (local-variable-p 'TeX-master
(current-buffer))
1331 (stringp TeX-master
))
1332 ;; We have a local variable which tells us which file to use
1333 (expand-file-name TeX-master dir
)
1335 (concat (file-name-sans-extension (buffer-file-name)) ".tex")))
1337 (defun reftex-index-phrases-save-and-return ()
1338 "Return to where the `reftex-index-phrase-selection-or-word' was called."
1341 (switch-to-buffer (marker-buffer reftex-index-return-marker
))
1342 (goto-char (or (marker-position reftex-index-return-marker
) (point))))
1345 (defvar reftex-index-phrases-menu
)
1346 (defvar reftex-index-phrases-marker
)
1347 (defvar reftex-index-phrases-restrict-file nil
)
1349 (define-derived-mode reftex-index-phrases-mode fundamental-mode
"Phrases"
1350 "Major mode for managing the Index phrases of a LaTeX document.
1351 This buffer was created with RefTeX.
1353 To insert new phrases, use
1354 - `C-c \\' in the LaTeX document to copy selection or word
1355 - `\\[reftex-index-new-phrase]' in the phrases buffer.
1357 To index phrases use one of:
1359 \\[reftex-index-this-phrase] index current phrase
1360 \\[reftex-index-next-phrase] index next phrase (or N with prefix arg)
1361 \\[reftex-index-all-phrases] index all phrases
1362 \\[reftex-index-remaining-phrases] index current and following phrases
1363 \\[reftex-index-region-phrases] index the phrases in the region
1365 You can sort the phrases in this buffer with \\[reftex-index-sort-phrases].
1366 To display information about the phrase at point, use \\[reftex-index-phrases-info].
1368 For more information see the RefTeX User Manual.
1370 Here are all local bindings.
1372 \\{reftex-index-phrases-mode-map}"
1373 :syntax-table reftex-index-phrases-syntax-table
1374 (set (make-local-variable 'font-lock-defaults
)
1375 reftex-index-phrases-font-lock-defaults
)
1376 (easy-menu-add reftex-index-phrases-menu reftex-index-phrases-mode-map
)
1377 (set (make-local-variable 'reftex-index-phrases-marker
) (make-marker)))
1378 ;; (add-hook 'reftex-index-phrases-mode-hook 'turn-on-font-lock)
1380 ;; Font Locking stuff
1381 (let ((ss (if (featurep 'xemacs
) 'secondary-selection
''secondary-selection
)))
1382 (setq reftex-index-phrases-font-lock-keywords
1384 (cons reftex-index-phrases-comment-regexp
'font-lock-comment-face
)
1385 (list reftex-index-phrases-macrodef-regexp
1386 '(1 font-lock-type-face
)
1387 '(2 font-lock-keyword-face
)
1389 '(4 font-lock-function-name-face
)
1391 '(6 font-lock-string-face
))
1392 (list reftex-index-phrases-phrase-regexp1
1393 '(1 font-lock-keyword-face
)
1395 '(3 font-lock-string-face
)
1397 (list reftex-index-phrases-phrase-regexp2
1398 '(1 font-lock-keyword-face
)
1400 '(3 font-lock-string-face
)
1402 '(5 font-lock-function-name-face
))
1404 (setq reftex-index-phrases-font-lock-defaults
1405 '((reftex-index-phrases-font-lock-keywords)
1406 nil t nil beginning-of-line
))
1407 (put 'reftex-index-phrases-mode
'font-lock-defaults
1408 reftex-index-phrases-font-lock-defaults
) ; XEmacs
1411 (defun reftex-index-next-phrase (&optional arg
)
1412 "Index the next ARG phrases in the phrases buffer."
1414 (reftex-index-phrases-parse-header t
)
1418 (if (re-search-forward reftex-index-phrases-phrase-regexp12 nil t
)
1420 (goto-char (match-beginning 0))
1421 (reftex-index-this-phrase 'slave
))
1422 (error "No more phrase lines after point"))))
1424 (defun reftex-index-this-phrase (&optional slave
)
1425 "Index the phrase in the current line.
1426 Does a global search and replace in the entire document. At each
1427 match, the user will be asked to confirm the replacement."
1429 (if (not slave
) (reftex-index-phrases-parse-header t
))
1432 (cond ((looking-at reftex-index-phrases-comment-regexp
)
1433 (if (not slave
) (error "Comment line")))
1434 ((looking-at "^[ \t]*$")
1435 (if (not slave
) (error "Empty line")))
1436 ((looking-at reftex-index-phrases-macrodef-regexp
)
1437 (if (not slave
) (error "Macro definition line")))
1438 ((looking-at reftex-index-phrases-phrase-regexp12
)
1440 (let* ((char (if (not (equal (match-string 1) ""))
1441 (string-to-char (match-string 1))))
1442 (phrase (match-string 3))
1443 (index-key (match-string 6))
1444 (macro-data (cdr (if (null char
)
1445 (car reftex-index-phrases-macro-data
)
1446 (assoc char reftex-index-phrases-macro-data
))))
1447 (macro-fmt (car macro-data
))
1448 (repeat (nth 1 macro-data
))
1450 (cond ((and (stringp reftex-index-phrases-restrict-file
)
1451 (file-regular-p reftex-index-phrases-restrict-file
))
1452 (list reftex-index-phrases-restrict-file
))
1453 ((stringp reftex-index-phrases-restrict-file
)
1454 (error "Invalid restriction file %s"
1455 reftex-index-phrases-restrict-file
))
1456 (t reftex-index-phrases-files
)))
1457 (as-words reftex-index-phrases-search-whole-words
))
1459 (error "No macro associated with key %c" char
))
1461 (let ((overlay-arrow-string "=>")
1462 (overlay-arrow-position
1463 reftex-index-phrases-marker
)
1465 ;; Show the overlay arrow
1466 (move-marker reftex-index-phrases-marker
1467 (match-beginning 0) (current-buffer))
1468 ;; Start the query-replace
1469 (reftex-query-index-phrase-globally
1470 files phrase macro-fmt
1471 index-key repeat as-words
)
1472 (message "%s replaced"
1473 (reftex-number replace-count
"occurrence"))))))
1474 (t (error "Cannot parse this line")))))
1476 (defun reftex-index-all-phrases ()
1477 "Index all phrases in the phrases buffer.
1478 Calls `reftex-index-this-phrase' on each line in the buffer."
1480 (reftex-index-region-phrases (point-min) (point-max)))
1482 (defun reftex-index-remaining-phrases ()
1483 "Index all phrases in the phrases buffer.
1484 Calls `reftex-index-this-phrase' on each line ay and below point in
1488 (reftex-index-region-phrases (point) (point-max)))
1490 (defun reftex-index-region-phrases (beg end
)
1491 "Index all phrases in the phrases buffer.
1492 Calls `reftex-index-this-phrase' on each line in the region."
1494 (reftex-index-phrases-parse-header t
)
1496 (while (not (or (eobp)
1498 (save-excursion (reftex-index-this-phrase 'slave
))
1499 (beginning-of-line 2)))
1501 (defun reftex-index-phrases-parse-header (&optional get-files
)
1502 "Parse the header of a phrases file to extract the macro definitions.
1503 The definitions get stored in `reftex-index-phrases-macro-data'.
1504 Also switches to the LaTeX document to find out which files belong to
1505 the document and stores the list in `reftex-index-phrases-files'."
1506 (let* ((master (reftex-index-phrase-tex-master))
1509 ;; Get the file list
1511 (setq buf
(reftex-get-file-buffer-force master
))
1512 (unless buf
(error "Master file %s not found" master
))
1514 (reftex-access-scan-info)
1515 (setq reftex-index-phrases-files
1516 (reftex-all-document-files))))
1517 ;; Parse the files header for macro definitions
1518 (setq reftex-index-phrases-macro-data nil
)
1520 (goto-char (point-min))
1521 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t
)
1523 (string-to-char (match-string 2))
1525 (equal (match-string 6) "t"))
1526 reftex-index-phrases-macro-data
))
1527 ;; Reverse the list, so that the first macro is first
1528 (if (null reftex-index-phrases-macro-data
)
1529 (error "No valid MACRO DEFINITION line in %s file (make sure to use TAB separators)" reftex-index-phrase-file-extension
))
1530 (setq reftex-index-phrases-macro-data
1531 (nreverse reftex-index-phrases-macro-data
))
1532 (goto-char (point-min)))))
1534 (defun reftex-index-phrases-apply-to-region (beg end
)
1535 "Index all index phrases in the current region.
1536 This works exactly like global indexing from the index phrases buffer,
1537 but operation is restricted to the current region. This is useful if
1538 you need to add/change text in an already indexed document and want to
1539 index the new part without having to go over the unchanged parts again."
1541 (let ((win-conf (current-window-configuration))
1542 (reftex-index-phrases-restrict-file (buffer-file-name)))
1545 (narrow-to-region beg end
)
1548 ;; Hide the region highlighting
1549 (if (featurep 'xemacs
)
1550 (zmacs-deactivate-region)
1552 (delete-other-windows)
1553 (reftex-index-visit-phrases-buffer)
1554 (reftex-index-all-phrases))
1555 (set-window-configuration win-conf
))))))
1557 (defun reftex-index-new-phrase (&optional text
)
1558 "Open a new line in the phrases buffer, insert TEXT."
1560 (if (and text
(stringp text
))
1562 ;; Check if the phrase is already in the buffer
1563 (setq text
(reftex-index-simplify-phrase text
))
1564 (goto-char (point-min))
1565 (if (re-search-forward
1566 (concat "^\\(\\S-*\\)\t\\(" (regexp-quote text
)
1567 "\\) *[\t\n]") nil t
)
1569 (goto-char (match-end 2))
1570 (error "Phrase is already in phrases buffer")))))
1571 ;; Add the new phrase line after the last in the buffer
1572 (goto-char (point-max))
1573 (if (re-search-backward reftex-index-phrases-phrase-regexp12 nil t
)
1578 (if (and text
(stringp text
))
1581 (defun reftex-index-find-next-conflict-phrase (&optional arg
)
1582 "Find the next a phrase which is has conflicts in the phrase buffer.
1583 The command helps to find possible conflicts in the phrase indexing process.
1584 It searches downward from point for a phrase which is repeated elsewhere
1585 in the buffer, or which is a subphrase of another phrase. If such a
1586 phrase is found, the phrase info is displayed.
1587 To check the whole buffer, start at the beginning and continue by calling
1588 this function repeatedly."
1591 (while (re-search-forward reftex-index-phrases-phrase-regexp12 nil t
)
1592 (goto-char (match-beginning 3))
1593 (let* ((phrase (match-string 3))
1594 (case-fold-search reftex-index-phrases-case-fold-search
)
1595 (re (reftex-index-phrases-find-dup-re phrase t
)))
1597 (goto-char (point-min))
1598 (and (re-search-forward re nil t
)
1599 (re-search-forward re nil t
)))
1602 (reftex-index-phrases-info)
1603 (message "Phrase with match conflict discovered"))
1604 (goto-char (point-max))
1605 (error "No further problematic phrases found")))
1607 (defun reftex-index-phrases-info ()
1608 "Display information about the phrase at point."
1612 (unless (looking-at reftex-index-phrases-phrase-regexp12
)
1613 (error "Not a phrase line"))
1614 (save-match-data (reftex-index-phrases-parse-header t
))
1615 (let* ((char (if (not (equal (match-string 1) ""))
1616 (string-to-char (match-string 1))))
1617 (phrase (match-string 3))
1618 (index-key (match-string 6))
1619 (index-keys (split-string
1620 (or index-key phrase
)
1621 reftex-index-phrases-logical-or-regexp
))
1622 (macro-data (cdr (if (null char
)
1623 (car reftex-index-phrases-macro-data
)
1624 (assoc char reftex-index-phrases-macro-data
))))
1625 (macro-fmt (car macro-data
))
1626 (repeat (nth 1 macro-data
))
1627 (as-words reftex-index-phrases-search-whole-words
)
1628 (example (reftex-index-make-replace-string
1629 macro-fmt
(downcase phrase
) (car index-keys
) repeat
))
1630 (re (reftex-index-make-phrase-regexp phrase as-words t
))
1631 (re1 (reftex-index-phrases-find-dup-re phrase
))
1632 (re2 (reftex-index-phrases-find-dup-re phrase
'sub
))
1637 (case-fold-search reftex-index-phrases-case-fold-search
)
1639 (setq files reftex-index-phrases-files
)
1643 (goto-char (point-min))
1644 (while (re-search-forward re1 nil t
)
1646 (goto-char (point-min))
1647 (while (re-search-forward re2 nil t
)
1648 (push (cons (count-lines 1 (point)) (match-string 1)) superphrases
)
1650 (save-current-buffer
1651 (while (setq file
(pop files
))
1652 (setq buf
(reftex-get-file-buffer-force file
))
1658 (goto-char (point-min))
1659 (let ((case-fold-search reftex-index-phrases-case-fold-search
))
1660 (while (re-search-forward re nil t
)
1661 (or (reftex-in-comment)
1662 (incf nmatches
)))))))))
1663 (with-output-to-temp-buffer "*Help*"
1664 (princ (format " Phrase: %s\n" phrase
))
1665 (princ (format " Macro key: %s\n" char
))
1666 (princ (format " Macro format: %s\n" macro-fmt
))
1667 (princ (format " Repeat: %s\n" repeat
))
1670 (let ((iks index-keys
) (cnt 0) ik
)
1671 (while (setq ik
(pop iks
))
1672 (princ (format "Index entry %d: %s\n" (incf cnt
) ik
)))))
1674 (princ (format " Index entry: %s\n" phrase
)))
1676 (princ (format " Index key: <<Given by the match>>\n"))))
1677 (princ (format " Example: %s\n" example
))
1679 (princ (format "Total matches: %s in %s\n"
1680 (reftex-number nmatches
"match" "es")
1681 (reftex-number (length reftex-index-phrases-files
)
1683 (princ (format " Uniqueness: Phrase occurs %s in phrase buffer\n"
1684 (reftex-number ntimes1
"time")))
1687 (princ (format " Superphrases: Phrase matches the following %s in the phrase buffer:\n"
1688 (reftex-number ntimes2
"line")))
1690 (princ (format " Line %4d: %s\n" (car x
) (cdr x
))))
1691 (nreverse superphrases
))))))))
1693 (defun reftex-index-phrases-set-macro-key ()
1694 "Change the macro key for the current line.
1695 Prompts for a macro key and insert is at the beginning of the line.
1696 If you reply with SPACE, the macro keyn will be removed, so that the
1697 default macro will be used. If you reply with `RET', just prints
1698 information about the currently selected macro."
1700 (reftex-index-phrases-parse-header)
1703 (unless (or (looking-at reftex-index-phrases-phrase-regexp12
)
1705 (error "This is not a phrase line"))
1706 (let* ((nc (reftex-index-select-phrases-macro 0))
1707 (macro-data (assoc nc reftex-index-phrases-macro-data
))
1712 macro-data
(car reftex-index-phrases-macro-data
)))
1714 (setq nc
(char-after (point)))
1717 macro-data
(car reftex-index-phrases-macro-data
))
1718 (setq macro-data
(assoc nc reftex-index-phrases-macro-data
))))
1719 (t (error "No macro associated with %c" nc
)))
1721 (setq macro-fmt
(nth 1 macro-data
)
1722 repeat
(nth 2 macro-data
))
1725 (if (looking-at "[^\t]") (delete-char 1))
1727 (message "Line will use %s %s repeat" macro-fmt
1728 (if repeat
"with" "without")))
1731 (defun reftex-index-sort-phrases (&optional chars-first
)
1732 "Sort the phrases lines in the buffer alphabetically.
1733 Normally, this looks only at the phrases. With a prefix arg CHARS-FIRST,
1734 it first compares the macro identifying chars and then the phrases."
1736 ;; Remember the current line, so that we can return
1737 (let ((line (buffer-substring (progn (beginning-of-line) (point))
1738 (progn (end-of-line) (point))))
1740 (goto-char (point-min))
1741 ;; Find first and last phrase line in buffer
1743 (and (re-search-forward reftex-index-phrases-phrase-regexp12 nil t
)
1744 (match-beginning 0)))
1745 (goto-char (point-max))
1746 (setq end
(re-search-backward reftex-index-phrases-phrase-regexp12 nil t
))
1747 (if end
(setq end
(progn (goto-char end
) (end-of-line) (point))))
1748 ;; Take the lines, sort them and re-insert.
1751 (message "Sorting lines...")
1752 (let* ((lines (split-string (buffer-substring beg end
) "\n"))
1753 (lines1 (sort lines
'reftex-compare-phrase-lines
)))
1754 (message "Sorting lines...done")
1755 (let ((inhibit-quit t
)) ;; make sure we do not lose lines
1756 (delete-region beg end
)
1757 (insert (mapconcat 'identity lines1
"\n"))))
1758 (goto-char (point-max))
1759 (re-search-backward (concat "^" (regexp-quote line
) "$") nil t
))
1760 (error "Cannot find phrases lines to sort"))))
1762 (defvar chars-first
)
1763 (defun reftex-compare-phrase-lines (a b
)
1764 "The comparison function used for sorting."
1765 (let (ca cb pa pb c-p p-p
)
1766 (if (string-match reftex-index-phrases-phrase-regexp12 a
)
1768 ;; Extract macro char and phrase-or-key for a
1769 (setq ca
(match-string 1 a
)
1771 (or (and reftex-index-phrases-sort-prefers-entry
1773 (match-string 3 a
))))
1774 (if (string-match reftex-index-phrases-phrase-regexp12 b
)
1776 ;; Extract macro char and phrase-or-key for b
1777 (setq cb
(match-string 1 b
)
1779 (or (and reftex-index-phrases-sort-prefers-entry
1781 (match-string 3 b
))))
1782 (setq c-p
(string< ca cb
)
1783 p-p
(string< pa pb
))
1784 ;; Do the right comparison, based on the value of `chars-first'
1785 ;; `chars-first' is bound locally in the calling function
1787 (if (string= ca cb
) p-p c-p
)
1788 (if (string= pa pb
) c-p p-p
)))))
1789 ;; If line a does not match, the answer we return determines
1790 ;; if non-matching lines are collected at the beginning.
1791 ;; When we return t here, non-matching lines form
1792 ;; block separators for searches.
1793 (not reftex-index-phrases-sort-in-blocks
))))
1795 (defvar reftex-index-phrases-menu
)
1796 (defun reftex-index-make-phrase-regexp (phrase &optional
1797 as-words allow-newline
)
1798 "Return a regexp matching PHRASE, even if distributed over lines.
1799 With optional arg AS-WORDS, require word boundary at beginning and end.
1800 With optional arg ALLOW-NEWLINE, allow single newline between words."
1801 (let* ((words (split-string phrase
))
1802 (space-re (if allow-newline
1803 "\\([ \t]*\\(\n[ \t]*\\)?\\|[ \t]\\)"
1805 (concat (if (and as-words
(string-match "\\`\\w" (car words
)))
1806 "\\(\\<\\|[`']\\)" "")
1807 (mapconcat (lambda (w) (regexp-quote
1808 (if reftex-index-phrases-case-fold-search
1813 (string-match "\\w\\'" (nth (1- (length words
)) words
)))
1814 "\\(\\>\\|'\\)" ""))))
1816 (defun reftex-index-simplify-phrase (phrase)
1817 "Make phrase single spaces and single line."
1818 (mapconcat 'identity
(split-string phrase
) " "))
1820 (defun reftex-index-phrases-find-dup-re (phrase &optional sub
)
1821 "Return a regexp which matches variations of PHRASE (with additional space).
1822 When SUB ins non-nil, the regexp will also match when PHRASE is a subphrase
1823 of another phrase. The regexp works lonly in the phrase buffer."
1824 (concat (if sub
"^\\S-?\t\\([^\t\n]*" "^\\S-?\t")
1825 (mapconcat 'regexp-quote
(split-string phrase
) " +")
1826 (if sub
"[^\t\n]*\\)\\([\t\n]\\|$\\)" " *\\([\t\n]\\|$\\)")))
1828 (defun reftex-index-make-replace-string (macro-fmt match index-key
1829 &optional repeat mathp
)
1830 "Return the string which can be used as replacement.
1831 Treats the logical `and' for index phrases."
1832 (let ((index-keys (split-string (or index-key match
)
1833 reftex-index-phrases-logical-and-regexp
)))
1835 (mapconcat (lambda (x)
1837 (format (if mathp reftex-index-math-format
"%s") x
)))
1839 (if repeat
(reftex-index-simplify-phrase match
) ""))))
1841 (defun reftex-query-index-phrase-globally (files &rest args
)
1842 "Call `reftex-query-index-phrase' for all files in FILES."
1843 (let ((win-conf (current-window-configuration))
1845 (unless files
(error "No files"))
1848 (switch-to-buffer-other-window (reftex-get-file-buffer-force
1850 (catch 'no-more-files
1851 (while (setq file
(pop files
))
1852 (switch-to-buffer (reftex-get-file-buffer-force file
))
1855 (unless (stringp reftex-index-phrases-restrict-file
)
1857 (goto-char (point-min))
1858 (apply 'reftex-query-index-phrase args
))))))
1859 (reftex-unhighlight 0)
1860 (set-window-configuration win-conf
))))
1862 (defconst reftex-index-phrases-help
1863 " Keys for query-index search
1864 ===========================
1865 y Replace this match
1867 ! Replace this and all further matches in this file
1868 q / Q Skip match, start next file / start next phrase
1869 o Use a different indexing macro for this match
1870 1 - 9 Select one of the multiple phrases
1871 e Edit the replacement text
1873 s / S Save this buffer / Save all document buffers
1875 "The help string for indexing phrases.")
1877 (defvar replace-count
)
1878 (defun reftex-query-index-phrase (phrase macro-fmt
&optional
1879 index-key repeat as-words
)
1880 "Search through buffer for PHRASE, and offer to replace it with an indexed
1881 version. The index version is derived by applying `format' with MACRO-FMT
1882 to INDEX-KEY or PHRASE. When REPEAT is non-nil, the PHRASE is inserted
1883 again after the macro.
1884 AS-WORDS means, the search for PHRASE should require word boundaries at
1886 (let* ((re (reftex-index-make-phrase-regexp phrase as-words
'allow-newline
))
1887 (case-fold-search reftex-index-phrases-case-fold-search
)
1888 (index-keys (split-string
1889 (or index-key phrase
)
1890 reftex-index-phrases-logical-or-regexp
))
1891 (nkeys (length index-keys
))
1892 (ckey (nth 0 index-keys
))
1894 match rpl char
(beg (make-marker)) (end (make-marker)) mathp
)
1898 (while (re-search-forward re nil t
)
1900 (if (reftex-in-comment)
1901 (throw 'next-match nil
))
1902 (if (and (fboundp reftex-index-verify-function
)
1903 (not (funcall reftex-index-verify-function
)))
1904 (throw 'next-match nil
))
1905 (setq match
(match-string 0))
1908 (condition-case nil
(texmathp) (error nil
))))
1909 (setq beg
(move-marker beg
(match-beginning 0))
1910 end
(move-marker end
(match-end 0)))
1911 (if (and reftex-index-phrases-skip-indexed-matches
1913 (reftex-index-phrase-match-is-indexed beg
1915 (throw 'next-match nil
))
1916 (reftex-highlight 0 (match-beginning 0) (match-end 0))
1919 (reftex-index-make-replace-string
1920 macro-fmt
(match-string 0) ckey repeat mathp
)))
1924 (message "REPLACE: %s? (yn!qoe%s?)"
1927 (concat "1-" (int-to-string nkeys
))
1929 (setq char
(if all-yes ?y
(read-char-exclusive)))
1930 (cond ((member char
'(?y ?Y ?\
))
1932 (replace-match rpl t t
)
1933 (incf replace-count
)
1934 ;; See if we should insert newlines to shorten lines
1935 (and reftex-index-phrases-wrap-long-lines
1936 (reftex-index-phrases-fixup-line beg end
))
1938 ((member char
'(?n ?N ?\C-h ?\C-?
));; FIXME: DEL
1942 ;; Yes for all in this buffer
1945 ;; Stop this one in this file
1946 (goto-char (point-max))
1950 (throw 'no-more-files t
))
1954 (reftex-save-all-document-buffers))
1957 ((member char
'(?o ?O
))
1958 ;; Select a different macro
1959 (let* ((nc (reftex-index-select-phrases-macro 2))
1961 (cdr (assoc nc reftex-index-phrases-macro-data
)))
1962 (macro-fmt (car macro-data
))
1963 (repeat (nth 1 macro-data
)))
1965 (setq rpl
(save-match-data
1966 (reftex-index-make-replace-string
1968 ckey repeat mathp
)))
1972 (with-output-to-temp-buffer "*Help*"
1973 (princ reftex-index-phrases-help
)))
1979 (substitute-command-keys
1980 "Recursive edit. Resume with \\[exit-recursive-edit]"))
1983 (setq rpl
(read-string "Edit: " rpl
)))
1985 (setq ckey
(or index-key phrase
)
1986 rpl
(save-match-data
1987 (reftex-index-make-replace-string
1988 macro-fmt match ckey repeat mathp
))))
1990 (<= char
(+ ?
0 nkeys
)))
1991 (setq ckey
(nth (1- (- char ?
0)) index-keys
)
1992 rpl
(save-match-data
1993 (reftex-index-make-replace-string
1994 macro-fmt match ckey repeat mathp
))))
1998 (move-marker beg nil
)
1999 (move-marker end nil
)
2001 (reftex-unhighlight 0))))
2003 (defun reftex-index-phrase-match-is-indexed (beg end
)
2004 ;; Check if match is in an argument of an index macro, or if an
2005 ;; index macro is directly attached to the match.
2008 (let* ((all-macros (reftex-what-macro t
))
2009 ; (this-macro (car (car all-macros)))
2012 (goto-char (1- beg
))
2013 (memq (char-after (point)) '(?\
] ?\
}))
2014 (car (reftex-what-macro 1))))
2016 (and (goto-char end
)
2017 (looking-at "\\(\\\\[a-zA-Z]+\\*?\\)[[{]")
2021 (while (setq macro
(pop all-macros
))
2022 (if (member (car macro
) reftex-macros-with-index
)
2023 (throw 'matched t
)))
2026 (member before-macro reftex-macros-with-index
))
2028 (member after-macro reftex-macros-with-index
))))))
2030 (defun reftex-index-phrases-fixup-line (beg end
)
2031 "Insert newlines before BEG and/or after END to shorten line."
2032 (let (bol eol space1 space2
)
2034 ;; Find line boundaries and possible line breaks near BEG and END
2040 (skip-chars-backward "^ \n")
2041 (if (and (equal (preceding-char) ?\
)
2042 (string-match "\\S-" (buffer-substring bol
(point))))
2043 (setq space1
(1- (point))))
2045 (skip-chars-forward "^ \n")
2046 (if (and (equal (following-char) ?\
)
2047 (string-match "\\S-" (buffer-substring (point) eol
)))
2048 (setq space2
(point)))
2049 ;; Now check what we have and insert the newlines
2050 (if (<= (- eol bol
) fill-column
)
2051 ;; Line is already short
2054 ((and (not space1
) (not space2
))) ; No spaces available
2055 ((not space2
) ; Do space1
2056 (reftex-index-phrases-replace-space space1
))
2057 ((not space1
) ; Do space2
2058 (reftex-index-phrases-replace-space space2
))
2059 (t ; We have both spaces
2060 (let ((l1 (- space1 bol
))
2061 (l2 (- space2 space1
))
2062 (l3 (- eol space2
)))
2063 (if (> l2 fill-column
)
2064 ;; The central part alone is more than one line
2066 (reftex-index-phrases-replace-space space1
)
2067 (reftex-index-phrases-replace-space space2
))
2068 (if (> (+ l1 l2
) fill-column
)
2069 ;; Need to split beginning
2070 (reftex-index-phrases-replace-space space1
))
2071 (if (> (+ l2 l3
) fill-column
)
2072 ;; Need to split end
2073 (reftex-index-phrases-replace-space space2
))))))))))
2075 (defun reftex-index-phrases-replace-space (pos)
2076 "If there is a space at POS, replace it with a newline char.
2077 Does not do a save-excursion."
2078 (when (equal (char-after pos
) ?\
)
2083 (defun reftex-index-select-phrases-macro (&optional delay
)
2084 "Offer a list of possible index macros and have the user select one."
2085 (let* ((prompt (concat "Select macro: ["
2086 (mapconcat (lambda (x) (char-to-string (car x
)))
2087 reftex-index-phrases-macro-data
"")
2089 (help (concat "Select an indexing macro\n========================\n"
2090 (mapconcat (lambda (x)
2093 reftex-index-phrases-macro-data
"\n"))))
2094 (reftex-select-with-char prompt help delay
)))
2096 (provide 'reftex-index
)
2098 ;;; reftex-index.el ends here