1 ;;; reftex-index.el --- index support with RefTeX
3 ;; Copyright (C) 1997-2017 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-lib
))
28 (declare-function texmathp
"ext:texmathp" ())
32 ;; START remove for XEmacs release
34 (defvar transient-mark-mode
)
36 ;; END remove for XEmacs release
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."
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))
56 (skip-syntax-backward "w\\") (point))))
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
)
68 (reftex-index-visit-phrases-buffer)
69 (reftex-index-new-phrase sel
))
72 ;; Nothing selected, no word, so use full reftex-index command
74 ;; OK, we have something to index here.
75 ;; Add the dollars when necessary
77 (format reftex-index-math-format 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))
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
)))))
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."
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
)))
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
))
120 ;; Get the supported arguments
123 (setq tag1
(or (reftex-index-complete-tag tag opt-args
) "")))
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
131 (cl-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. "
137 (if opt
" (optional)" "")
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
)
153 (defun reftex-update-default-index (tag &optional tag-list
)
154 (if (and (not (equal tag
""))
156 (eq reftex-index-default-tag
'last
)
158 (member tag tag-list
)))
159 (put reftex-docstruct-symbol
'default-index-tag tag
)))
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"
175 (if opt
"optional" "")
177 (concat (if opt
", " "")
178 (format "default %s" default
))
181 (tag (completing-read prompt
(mapcar 'list index-tags
))))
182 (if (and default
(equal tag
"")) (setq tag default
))
183 (reftex-update-default-index tag
)
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)))
195 (error "No index tags available"))
197 ((= (length index-tags
) 1)
198 ;; Just one index, use it
201 ((> (length index-tags
) 1)
202 ;; Several indices, ask.
203 (let* ((tags (copy-sequence index-tags
))
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
)
217 (while (and (< (cl-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
(cl-incf i
)) "]"
226 (push (list (+ ?
0 (cl-incf cnt
)) tag
227 (concat "[" (int-to-string cnt
) "]:" tag
))
229 (setq tag-alist
(nreverse tag-alist
))
230 ;; Compute Prompt and Help strings
233 (format "Select Index%s: "
234 (if default
(format " (Default <%s>)" default
) ""))
235 (mapconcat (lambda(x) (nth 2 x
)) tag-alist
" ")))
237 (concat "Select an Index\n===============\n"
239 (format "[^M] %s (the default)\n" default
)
241 (mapconcat (lambda(x)
242 (apply 'format
"[%c] %s" x
))
244 ;; Query the user for an index-tag
245 (setq rpl
(reftex-select-with-char prompt help
3 t
))
247 (if (and default
(equal rpl ?\C-m
))
249 (if (assq rpl tag-alist
)
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)")))))
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
)))
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)))
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 (define-key map
"n" 'reftex-index-next
)
291 (define-key map
"p" 'reftex-index-previous
)
292 (define-key map
"?" 'reftex-index-show-help
)
293 (define-key map
" " 'reftex-index-view-entry
)
294 (define-key map
"\C-m" 'reftex-index-goto-entry-and-hide
)
295 (define-key map
"\C-i" 'reftex-index-goto-entry
)
296 (define-key map
"\C-k" 'reftex-index-kill
)
297 (define-key map
"r" 'reftex-index-rescan
)
298 (define-key map
"R" 'reftex-index-Rescan
)
299 (define-key map
"g" 'revert-buffer
)
300 (define-key map
"q" 'reftex-index-quit
)
301 (define-key map
"k" 'reftex-index-quit-and-kill
)
302 (define-key map
"f" 'reftex-index-toggle-follow
)
303 (define-key map
"s" 'reftex-index-switch-index-tag
)
304 (define-key map
"e" 'reftex-index-edit
)
305 (define-key map
"^" 'reftex-index-level-up
)
306 (define-key map
"_" 'reftex-index-level-down
)
307 (define-key map
"}" 'reftex-index-restrict-to-section
)
308 (define-key map
"{" 'reftex-index-widen
)
309 (define-key map
">" 'reftex-index-restriction-forward
)
310 (define-key map
"<" 'reftex-index-restriction-backward
)
311 (define-key map
"(" 'reftex-index-toggle-range-beginning
)
312 (define-key map
")" 'reftex-index-toggle-range-end
)
313 (define-key map
"|" 'reftex-index-edit-attribute
)
314 (define-key map
"@" 'reftex-index-edit-visual
)
315 (define-key map
"*" 'reftex-index-edit-key
)
316 (define-key map
"\C-c=" 'reftex-index-goto-toc
)
317 (define-key map
"c" 'reftex-index-toggle-context
)
319 ;; The capital letters and the exclamation mark
320 (cl-loop for key across
(concat "!" reftex-index-section-letters
) do
321 (define-key map
(vector (list key
))
322 (list 'lambda
'() '(interactive)
323 (list 'reftex-index-goto-letter key
))))
325 (easy-menu-define reftex-index-menu map
326 "Menu for Index buffer"
329 (message "To go to a section, just press any of: !%s"
330 reftex-index-section-letters
) t
]
331 ["Show Entry" reftex-index-view-entry t
]
332 ["Go To Entry" reftex-index-goto-entry t
]
333 ["Exit & Go To Entry" reftex-index-goto-entry-and-hide t
]
334 ["Table of Contents" reftex-index-goto-toc t
]
335 ["Quit" reftex-index-quit t
]
338 ["Rebuilt *Index* Buffer" revert-buffer t
]
340 ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans
]
341 ["Rescan Entire Document" reftex-index-Rescan t
])
343 ["Restrict to section" reftex-index-restrict-to-section t
]
344 ["Widen" reftex-index-widen reftex-index-restriction-indicator
]
345 ["Next Section" reftex-index-restriction-forward
346 reftex-index-restriction-indicator
]
347 ["Previous Section" reftex-index-restriction-backward
348 reftex-index-restriction-indicator
])
350 ["Edit Entry" reftex-index-edit t
]
351 ["Edit Key" reftex-index-edit-key t
]
352 ["Edit Attribute" reftex-index-edit-attribute t
]
353 ["Edit Visual" reftex-index-edit-visual t
]
355 ["Add Parentkey" reftex-index-level-down t
]
356 ["Remove Parentkey " reftex-index-level-up t
]
358 ["Make Start-of-Range" reftex-index-toggle-range-beginning t
]
359 ["Make End-of-Range" reftex-index-toggle-range-end t
]
361 ["Kill Entry" reftex-index-kill nil
]
363 ["Undo" reftex-index-undo nil
])
365 ["Context" reftex-index-toggle-context
:style toggle
366 :selected reftex-index-include-context
]
368 ["Follow Mode" reftex-index-toggle-follow
:style toggle
369 :selected reftex-index-follow-mode
])
371 ["Help" reftex-index-show-help t
]))
374 "Keymap used for *Index* buffers.")
376 (defvar reftex-index-menu
)
378 (defvar reftex-last-index-file nil
379 "Stores the file name from which `reftex-display-index' was called.")
380 (defvar reftex-index-tag nil
381 "Stores the tag of the index in an index buffer.")
383 (defvar reftex-index-return-marker
(make-marker)
384 "Marker which makes it possible to return from index to old position.")
386 (defvar reftex-index-restriction-indicator nil
)
387 (defvar reftex-index-restriction-data nil
)
389 (define-derived-mode reftex-index-mode special-mode
"RefTeX Index"
390 "Major mode for managing Index buffers for LaTeX files.
391 This buffer was created with RefTeX.
392 Press `?' for a summary of important key bindings, or check the menu.
394 Here are all local bindings.
396 \\{reftex-index-mode-map}"
397 (set (make-local-variable 'revert-buffer-function
) 'reftex-index-revert
)
398 (set (make-local-variable 'reftex-index-restriction-data
) nil
)
399 (set (make-local-variable 'reftex-index-restriction-indicator
) nil
)
400 (setq mode-line-format
401 (list "---- " 'mode-line-buffer-identification
402 " " 'global-mode-string
403 " R<" 'reftex-index-restriction-indicator
">"
405 (setq truncate-lines t
)
406 (when (featurep 'xemacs
)
407 ;; XEmacs needs the call to make-local-hook
408 (make-local-hook 'post-command-hook
)
409 (make-local-hook 'pre-command-hook
))
410 (make-local-variable 'reftex-last-follow-point
)
411 (easy-menu-add reftex-index-menu reftex-index-mode-map
)
412 (add-hook 'post-command-hook
'reftex-index-post-command-hook nil t
)
413 (add-hook 'pre-command-hook
'reftex-index-pre-command-hook nil t
))
415 (defconst reftex-index-help
416 " AVAILABLE KEYS IN INDEX BUFFER
417 ==============================
418 ! A..Z Goto the section of entries starting with this letter.
419 n / p next-entry / previous-entry
420 SPC / TAB Show/Goto the corresponding entry in the LaTeX document.
421 RET Goto the entry and hide the *Index* window (also on mouse-2).
422 q / k Hide/Kill *Index* buffer.
423 C-c = Switch to the TOC buffer.
424 f / c Toggle follow mode / Toggle display of [c]ontext.
425 g Refresh *Index* buffer.
426 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
427 s Switch to a different index (for documents with multiple indices).
428 e / C-k Edit/Kill the entry.
429 * | @ Edit specific part of entry: [*]key [|]attribute [@]visual
430 With prefix: kill that part.
431 \( ) Toggle entry's beginning/end of page range property.
432 _ ^ Add/Remove parent key (to make this item a subitem).
433 } / { Restrict Index to a single document section / Widen.
434 < / > 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)))
466 (defun reftex-display-index (&optional tag overriding-restriction redo
468 "Display a buffer with an index compiled from the current document.
469 When the document has multiple indices, first prompts for the correct one.
470 When index support is turned off, offer to turn it on.
471 With one or two `C-u' prefixes, rescan document first.
472 With prefix 2, restrict index to current document section.
473 With prefix 3, restrict index to region."
477 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4).
478 (let ((current-prefix-arg current-prefix-arg
))
479 (reftex-ensure-index-support t
)
480 (reftex-access-scan-info current-prefix-arg
))
482 (set-marker reftex-index-return-marker
(point))
483 (setq reftex-last-follow-point
1)
485 ;; Determine the correct index to process
486 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
487 (docstruct-symbol reftex-docstruct-symbol
)
488 (index-tag (or tag
(reftex-index-select-tag)))
489 (master (reftex-TeX-master-file))
490 (calling-file (buffer-file-name))
492 (or overriding-restriction
494 (reftex-get-restriction current-prefix-arg docstruct
))))
496 ;; See if we are on an index macro as initial position
498 (let* ((what-macro (reftex-what-macro-safe 1))
499 (macro (car what-macro
))
500 (here-I-am (when (member macro reftex-macros-with-index
)
502 (goto-char (+ (cdr what-macro
)
504 (reftex-move-over-touching-args)
505 (reftex-where-am-I)))))
506 (if (eq (car (car here-I-am
)) 'index
)
507 (list (car here-I-am
))))))
510 (setq buffer-name
(reftex-make-index-buffer-name index-tag
))
512 ;; Goto the buffer and put it into the correct mode
514 (when (or restriction current-prefix-arg
)
515 (reftex-kill-buffer buffer-name
))
517 (if (get-buffer-window buffer-name
)
518 (select-window (get-buffer-window buffer-name
))
519 (switch-to-buffer buffer-name
))
521 (or (eq major-mode
'reftex-index-mode
) (reftex-index-mode))
523 ;; If the buffer is currently restricted, empty it to force update.
524 (when reftex-index-restriction-data
525 (reftex-erase-buffer))
526 (set (make-local-variable 'reftex-last-index-file
) calling-file
)
527 (set (make-local-variable 'reftex-index-tag
) index-tag
)
528 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
530 (setq reftex-index-restriction-indicator
(car restriction
)
531 reftex-index-restriction-data
(cdr restriction
))
533 (setq reftex-index-restriction-indicator nil
534 reftex-index-restriction-data nil
)))
535 (when (= (buffer-size) 0)
536 ;; buffer is empty - fill it
537 (message "Building %s buffer..." buffer-name
)
539 (setq buffer-read-only nil
)
543 SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help
544 ------------------------------------------------------------------------------
546 index-tag
(abbreviate-file-name master
)
547 (if (eq (car (car reftex-index-restriction-data
)) 'toc
)
548 (nth 2 (car reftex-index-restriction-data
))
549 reftex-index-restriction-indicator
)))
551 (if (reftex-use-fonts)
552 (put-text-property (point-min) (point)
553 'face reftex-index-header-face
))
554 (if (fboundp 'cursor-intangible-mode
)
555 (cursor-intangible-mode 1)
556 ;; If `cursor-intangible' is not available, fallback on the old
557 ;; intrusive `intangible' property.
558 (put-text-property (point-min) (point) 'intangible t
))
559 (add-text-properties (point-min) (point)
560 '(cursor-intangible t
561 front-sticky
(cursor-intangible)
562 rear-nonsticky
(cursor-intangible)))
564 (reftex-insert-index docstruct index-tag
)
565 (goto-char (point-min))
566 (run-hooks 'reftex-display-copied-context-hook
)
567 (message "Building %s buffer...done." buffer-name
)
568 (setq buffer-read-only t
))
569 (and locations
(apply 'reftex-find-start-point
(point) locations
))
570 (if reftex-index-restriction-indicator
571 (message "Index restricted: <%s>" reftex-index-restriction-indicator
))))
573 (defun reftex-insert-index (docstruct tag
&optional update-one remark
)
574 ;; Insert an index into the current buffer. Entries are from the
576 ;; TAG is the subindex to process.
577 ;; UPDATE-ONE: When non-nil, delete the entry at point and replace
578 ;; it with whatever the DOCSTRUCT contains.
579 ;; REMARK can be a note to add to the entry.
580 (let* ((all docstruct
)
582 (context reftex-index-include-context
)
583 (context-indent (concat indent
" "))
584 (section-chars (mapcar 'identity reftex-index-section-letters
))
585 (this-section-char 0)
586 (font (reftex-use-fonts))
587 (bor (car reftex-index-restriction-data
))
588 (eor (nth 1 reftex-index-restriction-data
))
590 (if (memq reftex-highlight-selection
'(mouse both
))
591 reftex-mouse-selected-face
593 (index-face reftex-label-face
)
594 sublist cell from to first-char
)
596 ;; Make the sublist and sort it
598 (setq all
(or (memq bor all
) all
)))
600 (while (setq cell
(pop all
))
603 (and (eq (car cell
) 'index
)
604 (equal (nth 1 cell
) tag
)
605 (push cell sublist
))))
606 (setq sublist
(sort (nreverse sublist
)
607 (lambda (a b
) (string< (nth 8 a
) (nth 8 b
)))))
610 ;; Delete the entry at place
611 (and (bolp) (forward-char 1))
612 (delete-region (previous-single-property-change (1+ (point)) :data
)
613 (or (next-single-property-change (point) :data
)
616 ;; Walk through the list and insert all entries
617 (while (setq cell
(pop sublist
))
619 (setq first-char
(upcase (string-to-char (nth 6 cell
))))
620 (when (and (not (equal first-char this-section-char
))
621 (member first-char section-chars
))
622 ;; There is a new initial letter, so start a new section
623 (reftex-index-insert-new-letter first-char font
)
624 (setq section-chars
(delete first-char section-chars
)
625 this-section-char first-char
))
626 (when (= this-section-char
0)
627 (setq this-section-char ?
!)
628 (reftex-index-insert-new-letter this-section-char font
)))
631 (insert indent
(nth 7 cell
))
635 (- (point) (length (nth 7 cell
))) to
639 (when (or remark
(nth 9 cell
))
640 (and (< (current-column) 40)
641 ;; FIXME: maybe this is too slow?
642 (insert (make-string (max (- 40 (current-column)) 0) ?\
)))
643 (and (nth 9 cell
) (insert " " (substring (nth 5 cell
) (nth 9 cell
))))
644 (and remark
(insert " " remark
)))
650 (insert context-indent
(nth 2 cell
) "\n")
652 (put-text-property from to
:data cell
)
654 (put-text-property from
(1- to
)
655 'mouse-face mouse-face
))
659 (defun reftex-index-insert-new-letter (letter &optional font
)
660 ;; Start a new section in the index
661 (let ((from (point)))
662 (insert "\n" letter letter letter
663 "-----------------------------------------------------------------")
665 (put-text-property from
(point) 'face reftex-index-section-face
))
668 (defun reftex-get-restriction (arg docstruct
)
669 ;; Interpret the prefix ARG and derive index restriction specs.
670 (let* ((beg (min (point) (or (condition-case nil
(mark) (error nil
))
672 (end (max (point) (or (condition-case nil
(mark) (error nil
))
674 bor eor label here-I-am
)
677 (setq here-I-am
(car (reftex-where-am-I))
678 bor
(if (eq (car here-I-am
) 'toc
)
680 (reftex-last-assoc-before-elt
681 'toc here-I-am docstruct
))
682 eor
(car (memq (assq 'toc
(cdr (memq bor docstruct
))) docstruct
))
686 (setq label
"region")
688 (setq bor
(car (reftex-where-am-I)))
689 (setq bor
(nth 1 (memq bor docstruct
)))
691 (setq eor
(nth 1 (memq (car (reftex-where-am-I)) docstruct
)))))
693 (if (and label
(or bor eor
))
697 (defun reftex-index-pre-command-hook ()
698 ;; Used as pre command hook in *Index* buffer
699 (reftex-unhighlight 0)
700 (reftex-unhighlight 1))
702 (defun reftex-index-post-command-hook ()
703 ;; Used in the post-command-hook for the *Index* buffer
704 ;; FIXME: Lots of redundancy with reftex-toc-post-command-hook!
705 (when (get-text-property (point) :data
)
706 (and (> (point) 1) ;FIXME: Is this point-min or do we care about narrowing?
707 (not (get-text-property (point) 'cursor-intangible
))
708 (memq reftex-highlight-selection
'(cursor both
))
710 (or (previous-single-property-change (1+ (point)) :data
)
712 (or (next-single-property-change (point) :data
)
714 (if (integerp reftex-index-follow-mode
)
715 ;; Remove delayed action
716 (setq reftex-index-follow-mode t
)
717 (and reftex-index-follow-mode
718 (not (equal reftex-last-follow-point
(point)))
719 ;; Show context in other window
720 (setq reftex-last-follow-point
(point))
722 (reftex-index-visit-location nil
(not reftex-revisit-to-follow
))
725 (defun reftex-index-show-help ()
726 "Show a summary of special key bindings."
728 (with-output-to-temp-buffer "*RefTeX Help*"
729 (princ reftex-index-help
))
730 (reftex-enlarge-to-fit "*RefTeX Help*" t
)
731 ;; If follow mode is active, arrange to delay it one command
732 (if reftex-index-follow-mode
733 (setq reftex-index-follow-mode
1)))
735 (defun reftex-index-next (&optional arg
)
736 "Move to next selectable item."
738 (setq reftex-callback-fwd t
)
739 (or (eobp) (forward-char 1))
740 (goto-char (or (next-single-property-change (point) :data
)
742 (unless (get-text-property (point) :data
)
743 (goto-char (or (next-single-property-change (point) :data
)
745 (defun reftex-index-previous (&optional arg
)
746 "Move to previous selectable item."
748 (setq reftex-callback-fwd nil
)
749 (goto-char (or (previous-single-property-change (point) :data
)
751 (unless (get-text-property (point) :data
)
752 (goto-char (or (previous-single-property-change (point) :data
)
754 (defun reftex-index-toggle-follow ()
755 "Toggle follow (other window follows with context)."
757 (setq reftex-last-follow-point -
1)
758 (setq reftex-index-follow-mode
(not reftex-index-follow-mode
)))
759 (defun reftex-index-toggle-context ()
760 "Toggle inclusion of label context in *Index* buffer.
761 Label context is only displayed when the labels are there as well."
763 (setq reftex-index-include-context
(not reftex-index-include-context
))
764 (reftex-index-revert))
765 (defun reftex-index-view-entry ()
766 "View document location in other window."
768 (reftex-index-visit-location))
769 (defun reftex-index-goto-entry-and-hide ()
770 "Go to document location in other window. Hide the *Index* window."
772 (reftex-index-visit-location 'hide
))
773 (defun reftex-index-goto-entry ()
774 "Go to document location in other window. *Index* window stays."
776 (reftex-index-visit-location t
))
777 (defun reftex-index-mouse-goto-line-and-hide (ev)
778 "Go to document location in other window. Hide the *Index* window."
781 (reftex-index-visit-location 'hide
))
782 (defun reftex-index-quit ()
783 "Hide the *Index* window and do not move point."
785 (or (one-window-p) (delete-window))
786 (switch-to-buffer (marker-buffer reftex-index-return-marker
))
787 (goto-char (or (marker-position reftex-index-return-marker
) (point))))
788 (defun reftex-index-quit-and-kill ()
789 "Kill the *Index* buffer."
791 (kill-buffer (current-buffer))
792 (or (one-window-p) (delete-window))
793 (switch-to-buffer (marker-buffer reftex-index-return-marker
))
794 (goto-char (or (marker-position reftex-index-return-marker
) (point))))
795 (defun reftex-index-goto-toc (&rest ignore
)
796 "Switch to the table of contents of the current document.
797 The function will go to the section where the entry at point was defined."
799 (if (get-text-property (point) :data
)
800 (reftex-index-goto-entry)
801 (switch-to-buffer (marker-buffer reftex-index-return-marker
)))
802 (delete-other-windows)
804 (defun reftex-index-rescan (&rest ignore
)
805 "Regenerate the *Index* buffer after reparsing file of section at point."
807 (let ((index-tag reftex-index-tag
))
808 (if (and reftex-enable-partial-scans
809 (null current-prefix-arg
))
810 (let* ((data (get-text-property (point) :data
))
812 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
814 (error "Don't know which file to rescan. Try `C-u r'")
815 (switch-to-buffer (reftex-get-file-buffer-force file
))
816 (setq current-prefix-arg
'(4))
817 (reftex-display-index index-tag nil
'redo line
)))
818 (reftex-index-Rescan))
819 (reftex-kill-temporary-buffers)))
820 (defun reftex-index-Rescan (&rest ignore
)
821 "Regenerate the *Index* buffer after reparsing the entire document."
823 (let ((index-tag reftex-index-tag
)
824 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
826 (reftex-get-file-buffer-force reftex-last-index-file
))
827 (setq current-prefix-arg
'(16))
828 (reftex-display-index index-tag nil
'redo line
)))
829 (defun reftex-index-revert (&rest ignore
)
830 "Regenerate the *Index* from the internal lists. No reparsing os done."
832 (let ((buf (current-buffer))
833 (index-tag reftex-index-tag
)
834 (data (get-text-property (point) :data
))
835 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
837 (reftex-get-file-buffer-force reftex-last-index-file
))
838 (reftex-erase-buffer buf
)
839 (setq current-prefix-arg nil
840 reftex-last-follow-point
1)
841 (reftex-display-index index-tag nil
'redo data line
)))
842 (defun reftex-index-switch-index-tag (&rest ignore
)
843 "Switch to a different index of the same document."
846 (reftex-get-file-buffer-force reftex-last-index-file
))
847 (setq current-prefix-arg nil
)
848 (reftex-display-index nil nil
'redo
))
850 (defun reftex-index-restrict-to-section (&optional force
)
851 "Restrict index to entries defined in same document sect. as entry at point."
852 ;; Optional FORCE means, even if point is not on an index entry.
854 (let* ((data (get-text-property (point) :data
))
855 (docstruct (symbol-value reftex-docstruct-symbol
))
857 (if (and (not data
) force
)
858 (setq data
(assq 'toc docstruct
)))
860 (setq bor
(reftex-last-assoc-before-elt 'toc data docstruct
)
861 eor
(car (memq (assq 'toc
(cdr (memq bor docstruct
)))
863 reftex-index-restriction-data
(list bor eor
)
864 reftex-index-restriction-indicator
(nth 6 bor
) )))
865 (reftex-index-revert))
867 (defun reftex-index-widen (&rest ignore
)
868 "Show the unrestricted index (all entries)."
870 (setq reftex-index-restriction-indicator nil
871 reftex-index-restriction-data nil
)
872 (reftex-index-revert)
873 (message "Index widened"))
874 (defun reftex-index-restriction-forward (&rest ignore
)
875 "Restrict to previous section.
876 When index is currently unrestricted, restrict it to a section.
877 When index is restricted, select the next section as restriction criterion."
879 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
880 (bor (nth 1 reftex-index-restriction-data
)))
882 (not (eq (car bor
) 'toc
)))
883 (reftex-index-restrict-to-section t
)
884 (setq reftex-index-restriction-indicator
(nth 6 bor
)
885 reftex-index-restriction-data
887 (car (memq (assq 'toc
(cdr (memq bor docstruct
)))
889 (reftex-index-revert))))
890 (defun reftex-index-restriction-backward (&rest ignore
)
891 "Restrict to next section.
892 When index is currently unrestricted, restrict it to a section.
893 When index is restricted, select the previous section as restriction criterion."
895 (let* ((docstruct (symbol-value reftex-docstruct-symbol
))
896 (eor (car reftex-index-restriction-data
))
897 (bor (reftex-last-assoc-before-elt 'toc eor docstruct t
)))
899 (not (eq (car bor
) 'toc
)))
900 (reftex-index-restrict-to-section t
)
901 (setq reftex-index-restriction-indicator
(nth 6 bor
)
902 reftex-index-restriction-data
904 (reftex-index-revert))))
906 (defun reftex-index-visit-location (&optional final no-revisit
)
907 ;; Visit the tex file corresponding to the index entry on the current line.
908 ;; If FINAL is t, stay there
909 ;; If FINAL is 'hide, hide the *Index* window.
910 ;; Otherwise, move cursor back into *Index* window.
911 ;; NO-REVISIT means don't visit files, just use live buffers.
913 (let* ((data (get-text-property (point) :data
))
914 (index-window (selected-window))
915 show-window show-buffer match
)
917 (unless data
(error "Don't know which index entry to visit"))
919 (if (eq (car data
) 'index
)
920 (setq match
(reftex-index-show-entry data no-revisit
)))
922 (setq show-window
(selected-window)
923 show-buffer
(current-buffer))
926 (select-window index-window
)
927 (error "Cannot find location"))
929 (select-window index-window
)
931 ;; Use the `final' parameter to decide what to do next
934 (reftex-unhighlight 0)
935 (select-window show-window
))
937 (reftex-unhighlight 0)
938 (or (one-window-p) (delete-window))
939 (switch-to-buffer show-buffer
))
942 (defun reftex-index-analyze-entry (data)
943 ;; This splits the index context so that key, attribute and visual
944 ;; values are accessible individually.
946 (let* ((arg (nth 5 data
))
947 (context (nth 2 data
))
948 (sc reftex-index-special-chars
)
949 (boa (if (string-match (regexp-quote (concat "{" arg
"}")) context
)
950 (1+ (match-beginning 0))
951 (error "Something is wrong here")))
952 (eoa (1- (match-end 0)))
953 (boactual (if (string-match (concat "[^" (nth 3 sc
) "]" (nth 2 sc
))
955 (1+ (match-beginning 0))
957 (boattr (if (string-match (concat "[^" (nth 3 sc
) "]" (nth 1 sc
))
959 (1+ (match-beginning 0))
961 (pre (substring context
0 boa
))
962 (key (substring context boa boattr
))
963 (attr (substring context boattr boactual
))
964 (actual (substring context boactual eoa
))
965 (post (substring context eoa
)))
966 (list pre key attr actual post
)))
968 (defun reftex-index-edit ()
969 "Edit the index entry at point."
971 (let* ((data (get-text-property (point) :data
))
973 (unless data
(error "Don't know which index entry to edit"))
974 (reftex-index-view-entry)
975 (setq old
(nth 2 data
) new
(read-string "Edit: " old
))
976 (reftex-index-change-entry new
)))
978 (defun reftex-index-toggle-range-beginning ()
979 "Toggle the page range start attribute `|('."
981 (let* ((data (get-text-property (point) :data
))
982 (bor (concat (nth 1 reftex-index-special-chars
) "("))
984 (unless data
(error "Don't know which index entry to edit"))
985 (setq analyze
(reftex-index-analyze-entry data
)
986 attr
(nth 2 analyze
))
987 (setf (nth 2 analyze
) (if (string= attr bor
) "" bor
))
988 (setq new
(apply 'concat analyze
))
989 (reftex-index-change-entry
990 new
(if (string= (nth 2 analyze
) bor
)
991 "Entry is now START-OF-PAGE-RANGE"
992 "START-OF-PAGE-RANGE canceled"))))
994 (defun reftex-index-toggle-range-end ()
995 "Toggle the page-range-end attribute `|)'."
997 (let* ((data (get-text-property (point) :data
))
998 (eor (concat (nth 1 reftex-index-special-chars
) ")"))
1000 (unless data
(error "Don't know which index entry to edit"))
1001 (setq analyze
(reftex-index-analyze-entry data
)
1002 attr
(nth 2 analyze
))
1003 (setf (nth 2 analyze
) (if (string= attr eor
) "" eor
))
1004 (setq new
(apply 'concat analyze
))
1005 (reftex-index-change-entry
1006 new
(if (string= (nth 2 analyze
) eor
)
1007 "Entry is now END-OF-PAGE-RANGE"
1008 "END-OF-PAGE-RANGE canceled"))))
1010 (defun reftex-index-edit-key ()
1011 "Edit the KEY part of the index entry."
1013 (reftex-index-edit-part nil
1 "" "Key: " t
))
1015 (defun reftex-index-edit-attribute (&optional arg
)
1016 "EDIT the ATTRIBUTE part of the entry. With arg: remove entire ATTRIBUTE."
1018 (reftex-index-edit-part arg
2 (nth 1 reftex-index-special-chars
)
1021 (defun reftex-index-edit-visual (&optional arg
)
1022 "EDIT the VISUAL part of the entry. With arg: remove entire VISUAL string."
1024 (reftex-index-edit-part arg
3 (nth 2 reftex-index-special-chars
) "Visual: "))
1026 (defun reftex-index-edit-part (arg n initial prompt
&optional dont-allow-empty
)
1027 ;; This function does the work for all partial editing commands
1028 (let* ((data (get-text-property (point) :data
))
1029 new analyze opart npart
)
1030 (unless data
(error "Don't know which index entry to edit"))
1031 ;; Analyze the whole context string
1032 (setq analyze
(reftex-index-analyze-entry data
)
1033 opart
(nth n analyze
))
1034 (and (> (length opart
) 0) (setq opart
(substring opart
1)))
1035 ;; Have the user editing the part
1036 (setq npart
(if arg
"" (read-string (concat prompt initial
) opart
)))
1038 (cond ((string= npart opart
)
1039 (error "Not changed"))
1041 (if dont-allow-empty
1042 (error "Invalid value")
1043 (setf (nth n analyze
) npart
)))
1044 (t (setf (nth n analyze
) (concat initial npart
))))
1045 (setq new
(apply 'concat analyze
))
1046 ;; Change the entry and insert the changed version into the index.
1047 (reftex-index-change-entry
1048 new
(if (string= npart
"")
1049 (format "Deleted: %s" opart
)
1050 (format "New value is: %s" npart
)))))
1052 (defun reftex-index-level-down ()
1053 "Make index entry a subitem of another entry."
1055 (let* ((data (get-text-property (point) :data
))
1056 (docstruct (symbol-value reftex-docstruct-symbol
))
1058 (unless data
(error "Don't know which index entry to change"))
1059 (setq old
(nth 2 data
)
1061 prefix
(completing-read
1066 (and (eq (car x
) 'index
)
1067 (string= (nth 1 x
) reftex-index-tag
))) t
)))
1068 (unless (string-match
1069 (concat (regexp-quote (car reftex-index-special-chars
)) "\\'")
1071 (setq prefix
(concat prefix
(car reftex-index-special-chars
))))
1072 (if (string-match (regexp-quote key
) old
)
1073 (setq new
(replace-match (concat prefix key
) t t old
))
1074 (error "Cannot construct new index key"))
1075 (reftex-index-change-entry new
(format "Added prefix: %s" prefix
))))
1077 (defun reftex-index-level-up ()
1078 "Remove the highest level of a hierarchical index entry."
1080 (let* ((data (get-text-property (point) :data
))
1082 (unless data
(error "Don't know which entry to change"))
1083 (setq old
(nth 2 data
))
1084 (if (string-match (concat "{\\([^" (nth 0 reftex-index-special-chars
) "]*"
1085 "[^" (nth 3 reftex-index-special-chars
) "]"
1086 (regexp-quote (nth 0 reftex-index-special-chars
))
1089 (setq prefix
(substring old
(match-beginning 1) (match-end 1))
1090 new
(concat (substring old
0 (match-beginning 1))
1091 (substring old
(match-end 1))))
1092 (error "Entry is not a subitem"))
1093 (reftex-index-change-entry new
(format "Removed prefix: %s" prefix
))))
1095 (defun reftex-index-kill ()
1096 "FIXME: Not yet implemented"
1098 (error "This function is currently not implemented"))
1100 (defun reftex-index-undo ()
1101 "FIXME: Not yet implemented"
1103 (error "This function is currently not implemented"))
1105 (defun reftex-index-change-entry (new &optional message
)
1106 ;; Change the full context string of the index entry at point to
1107 ;; NEW. This actually edits the buffer where the entry is defined.
1109 (let* ((data (get-text-property (point) :data
))
1111 (unless data
(error "Cannot change entry"))
1112 (reftex-index-view-entry)
1113 (setq beg
(match-beginning 0) end
(match-end 0))
1114 (setq old
(nth 2 data
))
1115 (and (equal old new
) (error "Entry unchanged"))
1116 (with-current-buffer (get-file-buffer (nth 3 data
))
1118 (unless (looking-at (regexp-quote old
))
1119 (error "This should not happen (reftex-index-change-entry)"))
1120 (delete-region beg end
)
1122 (goto-char (1- beg
))
1123 (when (and (re-search-forward (reftex-everything-regexp) nil t
)
1125 (< (abs (- (match-beginning 10) beg
)) (length new
))
1126 (setq info
(reftex-index-info-safe buffer-file-name
)))
1127 (setcdr data
(cdr info
))))
1128 (let ((buffer-read-only nil
))
1130 (reftex-insert-index (list data
) reftex-index-tag t
1132 (setq reftex-last-follow-point
1)
1133 (and message
(message "%s" message
))))
1135 (defun reftex-index-goto-letter (char)
1136 "Go to the CHAR section in the index."
1138 (case-fold-search nil
))
1139 (goto-char (point-min))
1141 (if (re-search-forward (concat "^" (char-to-string char
)) nil t
)
1145 (reftex-index-next))
1148 (error "This <%s> index does not contain entries sorted before the letters"
1150 (error "This <%s> index does not contain entries starting with `%c'"
1151 reftex-index-tag char
)))))
1154 ;;----------------------------------------------------------------------
1155 ;; The Index Phrases File
1157 ;; Some constants and variables
1158 (defconst reftex-index-phrases-comment-regexp
"^[ \t]*%.*"
1159 "Regular expression to match comment lines in phrases buffer")
1160 (defconst reftex-index-phrases-macrodef-regexp
1161 "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\( *\t[ \t]*\\)\\([^\t]*[^ \t]\\)\\( *\t[ \t]*\\)\\(\\S-+\\)"
1162 "Regular expression to match macro definition lines the phrases buffer.")
1163 ;(defconst reftex-index-phrases-macrodef-regexp
1164 ; "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\([ \t]*\\)\\([^\t]*[^ \t]\\)\\([ \t]*\\)\\(nil\\|t\\)[ \t]*$"
1165 ; "Regular expression to match macro definition lines the phrases buffer.
1166 ;This version would allow just spaces as separators.")
1167 (defconst reftex-index-phrases-phrase-regexp1
1168 "^\\(\\S-?\\)\\(\t\\)\\([^\t\n]*\\S-\\)\\([ \t]*\\)$"
1169 "Regular expression matching phrases which have no separate index key.")
1170 (defconst reftex-index-phrases-phrase-regexp2
1171 "^\\(\\S-?\\)\\(\t\\)\\([^\t]*\\S-\\)\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)[ \t]*$"
1172 "Regular expression matching phrases which have a separate index key.")
1173 (defconst reftex-index-phrases-phrase-regexp12
1174 "^\\(\\S-?\\)\\(\t\\)\\([^\n\t]*\\S-\\)\\(\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)\\)?[ \t]*$"
1175 "Regular expression matching all types of phrase lines.")
1176 (defvar reftex-index-phrases-macro-data nil
1177 "Alist containing the information taken from the macro definition lines.
1178 This gets refreshed in every phrases command.")
1179 (defvar reftex-index-phrases-files nil
1180 "List of document files relevant for the phrases file.")
1182 (defvar reftex-index-phrases-font-lock-keywords nil
1183 "Font lock keywords for reftex-index-phrases-mode.")
1184 (defvar reftex-index-phrases-font-lock-defaults nil
1185 "Font lock defaults for reftex-index-phrases-mode.")
1186 (define-obsolete-variable-alias
1187 'reftex-index-phrases-map
'reftex-index-phrases-mode-map
"24.1")
1188 (defvar reftex-index-phrases-mode-map
1189 (let ((map (make-sparse-keymap)))
1190 ;; Keybindings and Menu for phrases buffer
1191 (define-key map
"\C-c\C-c" 'reftex-index-phrases-save-and-return
)
1192 (define-key map
"\C-c\C-x" 'reftex-index-this-phrase
)
1193 (define-key map
"\C-c\C-f" 'reftex-index-next-phrase
)
1194 (define-key map
"\C-c\C-r" 'reftex-index-region-phrases
)
1195 (define-key map
"\C-c\C-a" 'reftex-index-all-phrases
)
1196 (define-key map
"\C-c\C-d" 'reftex-index-remaining-phrases
)
1197 (define-key map
"\C-c\C-s" 'reftex-index-sort-phrases
)
1198 (define-key map
"\C-c\C-n" 'reftex-index-new-phrase
)
1199 (define-key map
"\C-c\C-m" 'reftex-index-phrases-set-macro-key
)
1200 (define-key map
"\C-c\C-i" 'reftex-index-phrases-info
)
1201 (define-key map
"\C-c\C-t" 'reftex-index-find-next-conflict-phrase
)
1202 (define-key map
"\C-i" 'self-insert-command
)
1204 (easy-menu-define reftex-index-phrases-menu map
1205 "Menu for Phrases buffer"
1207 ["New Phrase" reftex-index-new-phrase t
]
1208 ["Set Phrase Macro" reftex-index-phrases-set-macro-key t
]
1209 ["Recreate File Header" reftex-index-initialize-phrases-buffer t
]
1212 ["Sort" reftex-index-sort-phrases t
]
1215 ["by Search Phrase" (setq reftex-index-phrases-sort-prefers-entry nil
)
1216 :style radio
:selected
(not reftex-index-phrases-sort-prefers-entry
)]
1217 ["by Index Entry" (setq reftex-index-phrases-sort-prefers-entry t
)
1218 :style radio
:selected reftex-index-phrases-sort-prefers-entry
]
1219 ["in Blocks" (setq reftex-index-phrases-sort-in-blocks
1220 (not reftex-index-phrases-sort-in-blocks
))
1221 :style toggle
:selected reftex-index-phrases-sort-in-blocks
])
1222 ["Describe Phrase" reftex-index-phrases-info t
]
1223 ["Next Phrase Conflict" reftex-index-find-next-conflict-phrase t
]
1225 ("Find and Index in Document"
1226 ["Current Phrase" reftex-index-this-phrase t
]
1227 ["Next Phrase" reftex-index-next-phrase t
]
1228 ["Current and Following" reftex-index-remaining-phrases t
]
1229 ["Region Phrases" reftex-index-region-phrases t
]
1230 ["All Phrases" reftex-index-all-phrases t
]
1233 ["Match Whole Words" (setq reftex-index-phrases-search-whole-words
1234 (not reftex-index-phrases-search-whole-words
))
1235 :style toggle
:selected reftex-index-phrases-search-whole-words
]
1236 ["Case Sensitive Search" (setq reftex-index-phrases-case-fold-search
1237 (not reftex-index-phrases-case-fold-search
))
1238 :style toggle
:selected
(not
1239 reftex-index-phrases-case-fold-search
)]
1240 ["Wrap Long Lines" (setq reftex-index-phrases-wrap-long-lines
1241 (not reftex-index-phrases-wrap-long-lines
))
1242 :style toggle
:selected reftex-index-phrases-wrap-long-lines
]
1243 ["Skip Indexed Matches" (setq reftex-index-phrases-skip-indexed-matches
1244 (not reftex-index-phrases-skip-indexed-matches
))
1245 :style toggle
:selected reftex-index-phrases-skip-indexed-matches
])
1247 ["Save and Return" reftex-index-phrases-save-and-return t
]))
1250 "Keymap used for index phrases buffer.")
1251 (defvar reftex-index-phrases-syntax-table
1252 (let ((table (make-syntax-table)))
1253 (modify-syntax-entry ?
\" "." table
)
1255 "Syntax table for RefTeX Index Phrases mode.")
1258 (defun reftex-index-phrase-selection-or-word (arg)
1259 "Add current selection or word at point to the phrases buffer.
1260 When you are in transient-mark-mode and the region is active, the
1261 selection will be used - otherwise the word at point.
1262 You get a chance to edit the entry in the phrases buffer - finish with
1265 (set-marker reftex-index-return-marker
(point))
1266 (reftex-index-selection-or-word arg
'phrase
)
1267 (if (eq major-mode
'reftex-index-phrases-mode
)
1269 (substitute-command-keys
1270 "Return to LaTeX with \\[reftex-index-phrases-save-and-return]"))))
1273 (defun reftex-index-visit-phrases-buffer ()
1274 "Switch to the phrases buffer, initialize if empty."
1276 (reftex-access-scan-info)
1277 (set-marker reftex-index-return-marker
(point))
1278 (let* ((master (reftex-TeX-master-file))
1279 (name (concat (file-name-sans-extension master
)
1280 reftex-index-phrase-file-extension
)))
1282 (unless (eq major-mode
'reftex-index-phrases-mode
)
1283 (reftex-index-phrases-mode))
1284 (if (= (buffer-size) 0)
1285 (reftex-index-initialize-phrases-buffer master
))))
1287 (defun reftex-index-initialize-phrases-buffer (&optional master
)
1288 "Initialize the phrases buffer by creating the header.
1289 If the buffer is non-empty, delete the old header first."
1291 (let* ((case-fold-search t
)
1292 (default-key (car reftex-index-default-macro
))
1293 (default-macro (nth 1 (assoc default-key
1294 reftex-key-to-index-macro-alist
)))
1296 (sort (copy-sequence reftex-index-macro-alist
)
1297 (lambda (a b
) (equal (car a
) default-macro
))))
1298 macro entry key repeat
)
1300 (if master
(set (make-local-variable 'TeX-master
)
1301 (file-name-nondirectory master
)))
1303 (when (> (buffer-size) 0)
1306 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t
)
1308 (beginning-of-line 2)
1309 (if (looking-at reftex-index-phrases-comment-regexp
)
1310 (beginning-of-line 2))
1311 (while (looking-at "^[ \t]*$")
1312 (beginning-of-line 2))
1313 (if (featurep 'xemacs
)
1314 (zmacs-activate-region)
1315 (setq mark-active t
))
1316 (if (yes-or-no-p "Delete and rebuild header? ")
1317 (delete-region (point-min) (point))))
1319 ;; Insert the mode line
1321 (format "%% -*- mode: reftex-index-phrases; TeX-master: \"%s\" -*-\n"
1322 (file-name-nondirectory (reftex-index-phrase-tex-master))))
1323 ;; Insert the macro definitions
1324 (insert "% Key Macro Format Repeat\n")
1325 (insert "%---------------------------------------------------------------------\n")
1326 (while (setq entry
(pop macro-alist
))
1327 (setq macro
(car entry
)
1328 repeat
(nth 7 entry
)
1329 key
(car (delq nil
(mapcar (lambda (x) (if (equal (nth 1 x
) macro
)
1332 reftex-key-to-index-macro-alist
))))
1333 (insert (format ">>>INDEX_MACRO_DEFINITION:\t%s\t%-20s\t%s\n"
1334 (char-to-string key
) (concat macro
"{%s}")
1335 (if repeat
"t" "nil"))))
1336 (insert "%---------------------------------------------------------------------\n\n\n")))
1339 (defun reftex-index-phrase-tex-master (&optional dir
)
1340 "Return the name of the master file associated with a phrase buffer."
1341 (if (and (boundp 'TeX-master
)
1342 (local-variable-p 'TeX-master
(current-buffer))
1343 (stringp TeX-master
))
1344 ;; We have a local variable which tells us which file to use
1345 (expand-file-name TeX-master dir
)
1347 (concat (file-name-sans-extension (buffer-file-name)) ".tex")))
1349 (defun reftex-index-phrases-save-and-return ()
1350 "Return to where the `reftex-index-phrase-selection-or-word' was called."
1353 (switch-to-buffer (marker-buffer reftex-index-return-marker
))
1354 (goto-char (or (marker-position reftex-index-return-marker
) (point))))
1357 (defvar reftex-index-phrases-menu
)
1358 (defvar reftex-index-phrases-marker
)
1359 (defvar reftex-index-phrases-restrict-file nil
)
1360 ;; NB this is a global autoload - see reftex.el.
1362 (define-derived-mode reftex-index-phrases-mode fundamental-mode
"Phrases"
1363 "Major mode for managing the Index phrases of a LaTeX document.
1364 This buffer was created with RefTeX.
1366 To insert new phrases, use
1367 - `C-c \\' in the LaTeX document to copy selection or word
1368 - `\\[reftex-index-new-phrase]' in the phrases buffer.
1370 To index phrases use one of:
1372 \\[reftex-index-this-phrase] index current phrase
1373 \\[reftex-index-next-phrase] index next phrase (or N with prefix arg)
1374 \\[reftex-index-all-phrases] index all phrases
1375 \\[reftex-index-remaining-phrases] index current and following phrases
1376 \\[reftex-index-region-phrases] index the phrases in the region
1378 You can sort the phrases in this buffer with \\[reftex-index-sort-phrases].
1379 To display information about the phrase at point, use \\[reftex-index-phrases-info].
1381 For more information see the RefTeX User Manual.
1383 Here are all local bindings.
1385 \\{reftex-index-phrases-mode-map}"
1386 :syntax-table reftex-index-phrases-syntax-table
1387 (set (make-local-variable 'font-lock-defaults
)
1388 reftex-index-phrases-font-lock-defaults
)
1389 (easy-menu-add reftex-index-phrases-menu reftex-index-phrases-mode-map
)
1390 (set (make-local-variable 'reftex-index-phrases-marker
) (make-marker)))
1391 ;; (add-hook 'reftex-index-phrases-mode-hook 'turn-on-font-lock)
1393 ;; Font Locking stuff
1394 (let ((ss (if (featurep 'xemacs
) 'secondary-selection
''secondary-selection
)))
1395 (setq reftex-index-phrases-font-lock-keywords
1397 (cons reftex-index-phrases-comment-regexp
'font-lock-comment-face
)
1398 (list reftex-index-phrases-macrodef-regexp
1399 '(1 font-lock-type-face
)
1400 '(2 font-lock-keyword-face
)
1402 '(4 font-lock-function-name-face
)
1404 '(6 font-lock-string-face
))
1405 (list reftex-index-phrases-phrase-regexp1
1406 '(1 font-lock-keyword-face
)
1408 '(3 font-lock-string-face
)
1410 (list reftex-index-phrases-phrase-regexp2
1411 '(1 font-lock-keyword-face
)
1413 '(3 font-lock-string-face
)
1415 '(5 font-lock-function-name-face
))
1417 (setq reftex-index-phrases-font-lock-defaults
1418 '((reftex-index-phrases-font-lock-keywords)
1419 nil t nil beginning-of-line
))
1420 (put 'reftex-index-phrases-mode
'font-lock-defaults
1421 reftex-index-phrases-font-lock-defaults
) ; XEmacs
1424 (defun reftex-index-next-phrase (&optional arg
)
1425 "Index the next ARG phrases in the phrases buffer."
1427 (reftex-index-phrases-parse-header t
)
1431 (if (re-search-forward reftex-index-phrases-phrase-regexp12 nil t
)
1433 (goto-char (match-beginning 0))
1434 (reftex-index-this-phrase 'slave
))
1435 (error "No more phrase lines after point"))))
1437 (defun reftex-index-this-phrase (&optional slave
)
1438 "Index the phrase in the current line.
1439 Does a global search and replace in the entire document. At each
1440 match, the user will be asked to confirm the replacement."
1442 (if (not slave
) (reftex-index-phrases-parse-header t
))
1445 (cond ((looking-at reftex-index-phrases-comment-regexp
)
1446 (if (not slave
) (error "Comment line")))
1447 ((looking-at "^[ \t]*$")
1448 (if (not slave
) (error "Empty line")))
1449 ((looking-at reftex-index-phrases-macrodef-regexp
)
1450 (if (not slave
) (error "Macro definition line")))
1451 ((looking-at reftex-index-phrases-phrase-regexp12
)
1453 (let* ((char (if (not (equal (match-string 1) ""))
1454 (string-to-char (match-string 1))))
1455 (phrase (match-string 3))
1456 (index-key (match-string 6))
1457 (macro-data (cdr (if (null char
)
1458 (car reftex-index-phrases-macro-data
)
1459 (assoc char reftex-index-phrases-macro-data
))))
1460 (macro-fmt (car macro-data
))
1461 (repeat (nth 1 macro-data
))
1463 (cond ((and (stringp reftex-index-phrases-restrict-file
)
1464 (file-regular-p reftex-index-phrases-restrict-file
))
1465 (list reftex-index-phrases-restrict-file
))
1466 ((stringp reftex-index-phrases-restrict-file
)
1467 (error "Invalid restriction file %s"
1468 reftex-index-phrases-restrict-file
))
1469 (t reftex-index-phrases-files
)))
1470 (as-words reftex-index-phrases-search-whole-words
))
1472 (error "No macro associated with key %c" char
))
1474 (let ((overlay-arrow-string "=>")
1475 (overlay-arrow-position
1476 reftex-index-phrases-marker
)
1478 ;; Show the overlay arrow
1479 (move-marker reftex-index-phrases-marker
1480 (match-beginning 0) (current-buffer))
1481 ;; Start the query-replace
1482 (reftex-query-index-phrase-globally
1483 files phrase macro-fmt
1484 index-key repeat as-words
)
1485 (message "%s replaced"
1486 (reftex-number replace-count
"occurrence"))))))
1487 (t (error "Cannot parse this line")))))
1489 (defun reftex-index-all-phrases ()
1490 "Index all phrases in the phrases buffer.
1491 Calls `reftex-index-this-phrase' on each line in the buffer."
1493 (reftex-index-region-phrases (point-min) (point-max)))
1495 (defun reftex-index-remaining-phrases ()
1496 "Index all phrases in the phrases buffer.
1497 Calls `reftex-index-this-phrase' on each line ay and below point in
1501 (reftex-index-region-phrases (point) (point-max)))
1503 (defun reftex-index-region-phrases (beg end
)
1504 "Index all phrases in the phrases buffer.
1505 Calls `reftex-index-this-phrase' on each line in the region."
1507 (reftex-index-phrases-parse-header t
)
1509 (while (not (or (eobp)
1511 (save-excursion (reftex-index-this-phrase 'slave
))
1512 (beginning-of-line 2)))
1514 (defun reftex-index-phrases-parse-header (&optional get-files
)
1515 "Parse the header of a phrases file to extract the macro definitions.
1516 The definitions get stored in `reftex-index-phrases-macro-data'.
1517 Also switches to the LaTeX document to find out which files belong to
1518 the document and stores the list in `reftex-index-phrases-files'."
1519 (let* ((master (reftex-index-phrase-tex-master))
1522 ;; Get the file list
1524 (setq buf
(reftex-get-file-buffer-force master
))
1525 (unless buf
(error "Master file %s not found" master
))
1527 (reftex-access-scan-info)
1528 (setq reftex-index-phrases-files
1529 (reftex-all-document-files))))
1530 ;; Parse the files header for macro definitions
1531 (setq reftex-index-phrases-macro-data nil
)
1533 (goto-char (point-min))
1534 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t
)
1536 (string-to-char (match-string 2))
1538 (equal (match-string 6) "t"))
1539 reftex-index-phrases-macro-data
))
1540 ;; Reverse the list, so that the first macro is first
1541 (if (null reftex-index-phrases-macro-data
)
1542 (error "No valid MACRO DEFINITION line in %s file (make sure to use TAB separators)" reftex-index-phrase-file-extension
))
1543 (setq reftex-index-phrases-macro-data
1544 (nreverse reftex-index-phrases-macro-data
))
1545 (goto-char (point-min)))))
1547 (defun reftex-index-phrases-apply-to-region (beg end
)
1548 "Index all index phrases in the current region.
1549 This works exactly like global indexing from the index phrases buffer,
1550 but operation is restricted to the current region. This is useful if
1551 you need to add/change text in an already indexed document and want to
1552 index the new part without having to go over the unchanged parts again."
1554 (let ((win-conf (current-window-configuration))
1555 (reftex-index-phrases-restrict-file (buffer-file-name)))
1558 (narrow-to-region beg end
)
1561 ;; Hide the region highlighting
1562 (if (featurep 'xemacs
)
1563 (zmacs-deactivate-region)
1565 (delete-other-windows)
1566 (reftex-index-visit-phrases-buffer)
1567 (reftex-index-all-phrases))
1568 (set-window-configuration win-conf
))))))
1570 (defun reftex-index-new-phrase (&optional text
)
1571 "Open a new line in the phrases buffer, insert TEXT."
1573 (if (and text
(stringp text
))
1575 ;; Check if the phrase is already in the buffer
1576 (setq text
(reftex-index-simplify-phrase text
))
1577 (goto-char (point-min))
1578 (if (re-search-forward
1579 (concat "^\\(\\S-*\\)\t\\(" (regexp-quote text
)
1580 "\\) *[\t\n]") nil t
)
1582 (goto-char (match-end 2))
1583 (error "Phrase is already in phrases buffer")))))
1584 ;; Add the new phrase line after the last in the buffer
1585 (goto-char (point-max))
1586 (if (re-search-backward reftex-index-phrases-phrase-regexp12 nil t
)
1591 (if (and text
(stringp text
))
1594 (defun reftex-index-find-next-conflict-phrase (&optional arg
)
1595 "Find the next a phrase which is has conflicts in the phrase buffer.
1596 The command helps to find possible conflicts in the phrase indexing process.
1597 It searches downward from point for a phrase which is repeated elsewhere
1598 in the buffer, or which is a subphrase of another phrase. If such a
1599 phrase is found, the phrase info is displayed.
1600 To check the whole buffer, start at the beginning and continue by calling
1601 this function repeatedly."
1604 (while (re-search-forward reftex-index-phrases-phrase-regexp12 nil t
)
1605 (goto-char (match-beginning 3))
1606 (let* ((phrase (match-string 3))
1607 (case-fold-search reftex-index-phrases-case-fold-search
)
1608 (re (reftex-index-phrases-find-dup-re phrase t
)))
1610 (goto-char (point-min))
1611 (and (re-search-forward re nil t
)
1612 (re-search-forward re nil t
)))
1615 (reftex-index-phrases-info)
1616 (message "Phrase with match conflict discovered"))
1617 (goto-char (point-max))
1618 (error "No further problematic phrases found")))
1620 (defun reftex-index-phrases-info ()
1621 "Display information about the phrase at point."
1625 (unless (looking-at reftex-index-phrases-phrase-regexp12
)
1626 (error "Not a phrase line"))
1627 (save-match-data (reftex-index-phrases-parse-header t
))
1628 (let* ((char (if (not (equal (match-string 1) ""))
1629 (string-to-char (match-string 1))))
1630 (phrase (match-string 3))
1631 (index-key (match-string 6))
1632 (index-keys (split-string
1633 (or index-key phrase
)
1634 reftex-index-phrases-logical-or-regexp
))
1635 (macro-data (cdr (if (null char
)
1636 (car reftex-index-phrases-macro-data
)
1637 (assoc char reftex-index-phrases-macro-data
))))
1638 (macro-fmt (car macro-data
))
1639 (repeat (nth 1 macro-data
))
1640 (as-words reftex-index-phrases-search-whole-words
)
1641 (example (reftex-index-make-replace-string
1642 macro-fmt
(downcase phrase
) (car index-keys
) repeat
))
1643 (re (reftex-index-make-phrase-regexp phrase as-words t
))
1644 (re1 (reftex-index-phrases-find-dup-re phrase
))
1645 (re2 (reftex-index-phrases-find-dup-re phrase
'sub
))
1650 (case-fold-search reftex-index-phrases-case-fold-search
)
1652 (setq files reftex-index-phrases-files
)
1656 (goto-char (point-min))
1657 (while (re-search-forward re1 nil t
)
1659 (goto-char (point-min))
1660 (while (re-search-forward re2 nil t
)
1661 (push (cons (count-lines 1 (point)) (match-string 1)) superphrases
)
1662 (cl-incf ntimes2
))))
1663 (save-current-buffer
1664 (while (setq file
(pop files
))
1665 (setq buf
(reftex-get-file-buffer-force file
))
1671 (goto-char (point-min))
1672 (let ((case-fold-search reftex-index-phrases-case-fold-search
))
1673 (while (re-search-forward re nil t
)
1674 (or (reftex-in-comment)
1675 (cl-incf nmatches
)))))))))
1676 (with-output-to-temp-buffer "*Help*"
1677 (princ (format " Phrase: %s\n" phrase
))
1678 (princ (format " Macro key: %s\n" char
))
1679 (princ (format " Macro format: %s\n" macro-fmt
))
1680 (princ (format " Repeat: %s\n" repeat
))
1683 (let ((iks index-keys
) (cnt 0) ik
)
1684 (while (setq ik
(pop iks
))
1685 (princ (format "Index entry %d: %s\n" (cl-incf cnt
) ik
)))))
1687 (princ (format " Index entry: %s\n" phrase
)))
1689 (princ (format " Index key: <<Given by the match>>\n"))))
1690 (princ (format " Example: %s\n" example
))
1692 (princ (format "Total matches: %s in %s\n"
1693 (reftex-number nmatches
"match" "es")
1694 (reftex-number (length reftex-index-phrases-files
)
1696 (princ (format " Uniqueness: Phrase occurs %s in phrase buffer\n"
1697 (reftex-number ntimes1
"time")))
1700 (princ (format " Superphrases: Phrase matches the following %s in the phrase buffer:\n"
1701 (reftex-number ntimes2
"line")))
1703 (princ (format " Line %4d: %s\n" (car x
) (cdr x
))))
1704 (nreverse superphrases
))))))))
1706 (defun reftex-index-phrases-set-macro-key ()
1707 "Change the macro key for the current line.
1708 Prompts for a macro key and insert is at the beginning of the line.
1709 If you reply with SPACE, the macro keyn will be removed, so that the
1710 default macro will be used. If you reply with `RET', just prints
1711 information about the currently selected macro."
1713 (reftex-index-phrases-parse-header)
1716 (unless (or (looking-at reftex-index-phrases-phrase-regexp12
)
1718 (error "This is not a phrase line"))
1719 (let* ((nc (reftex-index-select-phrases-macro 0))
1720 (macro-data (assoc nc reftex-index-phrases-macro-data
))
1725 macro-data
(car reftex-index-phrases-macro-data
)))
1727 (setq nc
(char-after (point)))
1730 macro-data
(car reftex-index-phrases-macro-data
))
1731 (setq macro-data
(assoc nc reftex-index-phrases-macro-data
))))
1732 (t (error "No macro associated with %c" nc
)))
1734 (setq macro-fmt
(nth 1 macro-data
)
1735 repeat
(nth 2 macro-data
))
1738 (if (looking-at "[^\t]") (delete-char 1))
1740 (message "Line will use %s %s repeat" macro-fmt
1741 (if repeat
"with" "without")))
1744 (defun reftex-index-sort-phrases (&optional chars-first
)
1745 "Sort the phrases lines in the buffer alphabetically.
1746 Normally, this looks only at the phrases. With a prefix arg CHARS-FIRST,
1747 it first compares the macro identifying chars and then the phrases."
1749 ;; Remember the current line, so that we can return
1750 (let ((line (buffer-substring (progn (beginning-of-line) (point))
1751 (progn (end-of-line) (point))))
1753 (goto-char (point-min))
1754 ;; Find first and last phrase line in buffer
1756 (and (re-search-forward reftex-index-phrases-phrase-regexp12 nil t
)
1757 (match-beginning 0)))
1758 (goto-char (point-max))
1759 (setq end
(re-search-backward reftex-index-phrases-phrase-regexp12 nil t
))
1760 (if end
(setq end
(progn (goto-char end
) (end-of-line) (point))))
1761 ;; Take the lines, sort them and re-insert.
1764 (message "Sorting lines...")
1765 (let* ((lines (split-string (buffer-substring beg end
) "\n"))
1766 (lines1 (sort lines
'reftex-compare-phrase-lines
)))
1767 (message "Sorting lines...done")
1768 (let ((inhibit-quit t
)) ;; make sure we do not lose lines
1769 (delete-region beg end
)
1770 (insert (mapconcat 'identity lines1
"\n"))))
1771 (goto-char (point-max))
1772 (re-search-backward (concat "^" (regexp-quote line
) "$") nil t
))
1773 (error "Cannot find phrases lines to sort"))))
1775 (defvar chars-first
)
1776 (defun reftex-compare-phrase-lines (a b
)
1777 "The comparison function used for sorting."
1778 (let (ca cb pa pb c-p p-p
)
1779 (if (string-match reftex-index-phrases-phrase-regexp12 a
)
1781 ;; Extract macro char and phrase-or-key for a
1782 (setq ca
(match-string 1 a
)
1784 (or (and reftex-index-phrases-sort-prefers-entry
1786 (match-string 3 a
))))
1787 (if (string-match reftex-index-phrases-phrase-regexp12 b
)
1789 ;; Extract macro char and phrase-or-key for b
1790 (setq cb
(match-string 1 b
)
1792 (or (and reftex-index-phrases-sort-prefers-entry
1794 (match-string 3 b
))))
1795 (setq c-p
(string< ca cb
)
1796 p-p
(string< pa pb
))
1797 ;; Do the right comparison, based on the value of `chars-first'
1798 ;; `chars-first' is bound locally in the calling function
1800 (if (string= ca cb
) p-p c-p
)
1801 (if (string= pa pb
) c-p p-p
)))))
1802 ;; If line a does not match, the answer we return determines
1803 ;; if non-matching lines are collected at the beginning.
1804 ;; When we return t here, non-matching lines form
1805 ;; block separators for searches.
1806 (not reftex-index-phrases-sort-in-blocks
))))
1808 (defvar reftex-index-phrases-menu
)
1809 (defun reftex-index-make-phrase-regexp (phrase &optional
1810 as-words allow-newline
)
1811 "Return a regexp matching PHRASE, even if distributed over lines.
1812 With optional arg AS-WORDS, require word boundary at beginning and end.
1813 With optional arg ALLOW-NEWLINE, allow single newline between words."
1814 (let* ((words (split-string phrase
))
1815 (space-re (if allow-newline
1816 "\\([ \t]*\\(\n[ \t]*\\)?\\|[ \t]\\)"
1818 (concat (if (and as-words
(string-match "\\`\\w" (car words
)))
1819 "\\(\\<\\|[`']\\)" "")
1820 (mapconcat (lambda (w) (regexp-quote
1821 (if reftex-index-phrases-case-fold-search
1826 (string-match "\\w\\'" (nth (1- (length words
)) words
)))
1827 "\\(\\>\\|'\\)" ""))))
1829 (defun reftex-index-simplify-phrase (phrase)
1830 "Make phrase single spaces and single line."
1831 (mapconcat 'identity
(split-string phrase
) " "))
1833 (defun reftex-index-phrases-find-dup-re (phrase &optional sub
)
1834 "Return a regexp which matches variations of PHRASE (with additional space).
1835 When SUB ins non-nil, the regexp will also match when PHRASE is a subphrase
1836 of another phrase. The regexp works lonly in the phrase buffer."
1837 (concat (if sub
"^\\S-?\t\\([^\t\n]*" "^\\S-?\t")
1838 (mapconcat 'regexp-quote
(split-string phrase
) " +")
1839 (if sub
"[^\t\n]*\\)\\([\t\n]\\|$\\)" " *\\([\t\n]\\|$\\)")))
1841 (defun reftex-index-make-replace-string (macro-fmt match index-key
1842 &optional repeat mathp
)
1843 "Return the string which can be used as replacement.
1844 Treats the logical `and' for index phrases."
1845 (let ((index-keys (split-string (or index-key match
)
1846 reftex-index-phrases-logical-and-regexp
)))
1848 (mapconcat (lambda (x)
1850 (format (if mathp reftex-index-math-format
"%s") x
)))
1852 (if repeat
(reftex-index-simplify-phrase match
) ""))))
1854 (defun reftex-query-index-phrase-globally (files &rest args
)
1855 "Call `reftex-query-index-phrase' for all files in FILES."
1856 (let ((win-conf (current-window-configuration))
1858 (unless files
(error "No files"))
1861 (switch-to-buffer-other-window (reftex-get-file-buffer-force
1863 (catch 'no-more-files
1864 (while (setq file
(pop files
))
1865 (switch-to-buffer (reftex-get-file-buffer-force file
))
1868 (unless (stringp reftex-index-phrases-restrict-file
)
1870 (goto-char (point-min))
1871 (apply 'reftex-query-index-phrase args
))))))
1872 (reftex-unhighlight 0)
1873 (set-window-configuration win-conf
))))
1875 (defconst reftex-index-phrases-help
1876 " Keys for query-index search
1877 ===========================
1878 y Replace this match
1880 ! Replace this and all further matches in this file
1881 q / Q Skip match, start next file / start next phrase
1882 o Use a different indexing macro for this match
1883 1 - 9 Select one of the multiple phrases
1884 e Edit the replacement text
1886 s / S Save this buffer / Save all document buffers
1888 "The help string for indexing phrases.")
1890 (defvar replace-count
)
1891 (defun reftex-query-index-phrase (phrase macro-fmt
&optional
1892 index-key repeat as-words
)
1893 "Search through buffer for PHRASE, and offer to replace it with an indexed
1894 version. The index version is derived by applying `format' with MACRO-FMT
1895 to INDEX-KEY or PHRASE. When REPEAT is non-nil, the PHRASE is inserted
1896 again after the macro.
1897 AS-WORDS means, the search for PHRASE should require word boundaries at
1899 (let* ((re (reftex-index-make-phrase-regexp phrase as-words
'allow-newline
))
1900 (case-fold-search reftex-index-phrases-case-fold-search
)
1901 (index-keys (split-string
1902 (or index-key phrase
)
1903 reftex-index-phrases-logical-or-regexp
))
1904 (nkeys (length index-keys
))
1905 (ckey (nth 0 index-keys
))
1907 match rpl char
(beg (make-marker)) (end (make-marker)) mathp
)
1911 (while (re-search-forward re nil t
)
1913 (if (reftex-in-comment)
1914 (throw 'next-match nil
))
1915 (if (and (fboundp reftex-index-verify-function
)
1916 (not (funcall reftex-index-verify-function
)))
1917 (throw 'next-match nil
))
1918 (setq match
(match-string 0))
1921 (condition-case nil
(texmathp) (error nil
))))
1922 (setq beg
(move-marker beg
(match-beginning 0))
1923 end
(move-marker end
(match-end 0)))
1924 (if (and reftex-index-phrases-skip-indexed-matches
1926 (reftex-index-phrase-match-is-indexed beg
1928 (throw 'next-match nil
))
1929 (reftex-highlight 0 (match-beginning 0) (match-end 0))
1932 (reftex-index-make-replace-string
1933 macro-fmt
(match-string 0) ckey repeat mathp
)))
1937 (message "REPLACE: %s? (yn!qoe%s?)"
1940 (concat "1-" (int-to-string nkeys
))
1942 (setq char
(if all-yes ?y
(read-char-exclusive)))
1943 (cond ((member char
'(?y ?Y ?\
))
1945 (replace-match rpl t t
)
1946 (cl-incf replace-count
)
1947 ;; See if we should insert newlines to shorten lines
1948 (and reftex-index-phrases-wrap-long-lines
1949 (reftex-index-phrases-fixup-line beg end
))
1951 ((member char
'(?n ?N ?\C-h ?\C-?
));; FIXME: DEL
1955 ;; Yes for all in this buffer
1958 ;; Stop this one in this file
1959 (goto-char (point-max))
1963 (throw 'no-more-files t
))
1967 (reftex-save-all-document-buffers))
1970 ((member char
'(?o ?O
))
1971 ;; Select a different macro
1972 (let* ((nc (reftex-index-select-phrases-macro 2))
1974 (cdr (assoc nc reftex-index-phrases-macro-data
)))
1975 (macro-fmt (car macro-data
))
1976 (repeat (nth 1 macro-data
)))
1978 (setq rpl
(save-match-data
1979 (reftex-index-make-replace-string
1981 ckey repeat mathp
)))
1985 (with-output-to-temp-buffer "*Help*"
1986 (princ reftex-index-phrases-help
)))
1992 (substitute-command-keys
1993 "Recursive edit. Resume with \\[exit-recursive-edit]"))
1996 (setq rpl
(read-string "Edit: " rpl
)))
1998 (setq ckey
(or index-key phrase
)
1999 rpl
(save-match-data
2000 (reftex-index-make-replace-string
2001 macro-fmt match ckey repeat mathp
))))
2003 (<= char
(+ ?
0 nkeys
)))
2004 (setq ckey
(nth (1- (- char ?
0)) index-keys
)
2005 rpl
(save-match-data
2006 (reftex-index-make-replace-string
2007 macro-fmt match ckey repeat mathp
))))
2011 (move-marker beg nil
)
2012 (move-marker end nil
)
2014 (reftex-unhighlight 0))))
2016 (defun reftex-index-phrase-match-is-indexed (beg end
)
2017 ;; Check if match is in an argument of an index macro, or if an
2018 ;; index macro is directly attached to the match.
2021 (let* ((all-macros (reftex-what-macro t
))
2022 ; (this-macro (car (car all-macros)))
2025 (goto-char (1- beg
))
2026 (memq (char-after (point)) '(?\
] ?\
}))
2027 (car (reftex-what-macro 1))))
2029 (and (goto-char end
)
2030 (looking-at "\\(\\\\[a-zA-Z]+\\*?\\)[[{]")
2034 (while (setq macro
(pop all-macros
))
2035 (if (member (car macro
) reftex-macros-with-index
)
2036 (throw 'matched t
)))
2039 (member before-macro reftex-macros-with-index
))
2041 (member after-macro reftex-macros-with-index
))))))
2043 (defun reftex-index-phrases-fixup-line (beg end
)
2044 "Insert newlines before BEG and/or after END to shorten line."
2045 (let (bol eol space1 space2
)
2047 ;; Find line boundaries and possible line breaks near BEG and END
2053 (skip-chars-backward "^ \n")
2054 (if (and (equal (preceding-char) ?\
)
2055 (string-match "\\S-" (buffer-substring bol
(point))))
2056 (setq space1
(1- (point))))
2058 (skip-chars-forward "^ \n")
2059 (if (and (equal (following-char) ?\
)
2060 (string-match "\\S-" (buffer-substring (point) eol
)))
2061 (setq space2
(point)))
2062 ;; Now check what we have and insert the newlines
2063 (if (<= (- eol bol
) fill-column
)
2064 ;; Line is already short
2067 ((and (not space1
) (not space2
))) ; No spaces available
2068 ((not space2
) ; Do space1
2069 (reftex-index-phrases-replace-space space1
))
2070 ((not space1
) ; Do space2
2071 (reftex-index-phrases-replace-space space2
))
2072 (t ; We have both spaces
2073 (let ((l1 (- space1 bol
))
2074 (l2 (- space2 space1
))
2075 (l3 (- eol space2
)))
2076 (if (> l2 fill-column
)
2077 ;; The central part alone is more than one line
2079 (reftex-index-phrases-replace-space space1
)
2080 (reftex-index-phrases-replace-space space2
))
2081 (if (> (+ l1 l2
) fill-column
)
2082 ;; Need to split beginning
2083 (reftex-index-phrases-replace-space space1
))
2084 (if (> (+ l2 l3
) fill-column
)
2085 ;; Need to split end
2086 (reftex-index-phrases-replace-space space2
))))))))))
2088 (defun reftex-index-phrases-replace-space (pos)
2089 "If there is a space at POS, replace it with a newline char.
2090 Does not do a save-excursion."
2091 (when (equal (char-after pos
) ?\
)
2096 (defun reftex-index-select-phrases-macro (&optional delay
)
2097 "Offer a list of possible index macros and have the user select one."
2098 (let* ((prompt (concat "Select macro: ["
2099 (mapconcat (lambda (x) (char-to-string (car x
)))
2100 reftex-index-phrases-macro-data
"")
2102 (help (concat "Select an indexing macro\n========================\n"
2103 (mapconcat (lambda (x)
2106 reftex-index-phrases-macro-data
"\n"))))
2107 (reftex-select-with-char prompt help delay
)))
2109 (provide 'reftex-index
)
2111 ;;; reftex-index.el ends here
2114 ;; generated-autoload-file: "reftex-loaddefs.el"