browse-url-firefox: update for firefox 36's removal of -remote
[emacs.git] / lisp / textmodes / reftex-index.el
blobb5a3e05867e54d79d30b6ec9b32472f0564dcfd2
1 ;;; reftex-index.el --- index support with RefTeX
3 ;; Copyright (C) 1997-2015 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: auctex-devel@gnu.org
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
28 (declare-function texmathp "ext:texmathp" ())
30 (require 'reftex)
32 ;; START remove for XEmacs release
33 (defvar mark-active)
34 (defvar transient-mark-mode)
35 (defvar TeX-master)
36 ;; END remove for XEmacs release
38 (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."
48 (interactive "P")
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))
52 (beg (if active
53 (region-beginning)
54 (save-excursion
55 (skip-syntax-backward "w\\") (point))))
56 (end (if active
57 (region-end)
58 (save-excursion
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)
65 (if phrase
66 (progn
67 (reftex-index-visit-phrases-buffer)
68 (reftex-index-new-phrase sel))
70 (if (equal sel "")
71 ;; Nothing selected, no word, so use full reftex-index command
72 (reftex-index)
73 ;; OK, we have something to index here.
74 ;; Add the dollars when necessary
75 (setq key (if mathp
76 (format reftex-index-math-format sel)
77 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))
85 key))
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."
97 (interactive)
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)))
110 (ntag (nth 1 entry))
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))
116 opt tag1 value)
118 ;; Get the supported arguments
119 (if (stringp tag)
120 (setq tag1 tag)
121 (setq tag1 (or (reftex-index-complete-tag tag opt-args) "")))
122 (setq key (or key
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
128 (insert macro)
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. "
134 (int-to-string i)
135 (if opt " (optional)" "")
136 ": ")))))
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)
149 "idx"))))
151 (defun reftex-update-default-index (tag &optional tag-list)
152 (if (and (not (equal tag ""))
153 (stringp tag)
154 (eq reftex-index-default-tag 'last)
155 (or (null tag-list)
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"
169 (if (or opt default)
170 (format " (%s): "
171 (concat
172 (if opt "optional" "")
173 (if default
174 (concat (if opt ", " "")
175 (format "default %s" default))
176 "")))
177 ": ")))
178 (tag (completing-read prompt (mapcar 'list index-tags))))
179 (if (and default (equal tag "")) (setq tag default))
180 (reftex-update-default-index tag)
181 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)))
189 (cond
190 ((null index-tags)
191 (error "No index tags available"))
193 ((= (length index-tags) 1)
194 ;; Just one index, use it
195 (car index-tags))
197 ((> (length index-tags) 1)
198 ;; Several indices, ask.
199 (let* ((tags (copy-sequence index-tags))
200 (cnt 0)
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)
210 i -1
211 val nil)
212 (catch 'exit
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)) "]"
219 (substring tag i)))
220 tag-alist)
221 (throw 'exit t)))
222 (push (list (+ ?0 (incf cnt)) tag
223 (concat "[" (int-to-string cnt) "]:" tag))
224 tag-alist)))
225 (setq tag-alist (nreverse tag-alist))
226 ;; Compute Prompt and Help strings
227 (setq prompt
228 (concat
229 (format "Select Index%s: "
230 (if default (format " (Default <%s>)" default) ""))
231 (mapconcat (lambda(x) (nth 2 x)) tag-alist " ")))
232 (setq help
233 (concat "Select an Index\n===============\n"
234 (if default
235 (format "[^M] %s (the default)\n" default)
237 (mapconcat (lambda(x)
238 (apply 'format "[%c] %s" x))
239 tag-alist "\n")))
240 ;; Query the user for an index-tag
241 (setq rpl (reftex-select-with-char prompt help 3 t))
242 (message "")
243 (if (and default (equal rpl ?\C-m))
244 default
245 (if (assq rpl tag-alist)
246 (progn
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)))
263 key))
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)))
275 ;; Index map
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)
285 (loop for x in
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"
328 '("Index"
329 ["Goto section A-Z"
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]
337 "--"
338 ("Update"
339 ["Rebuilt *Index* Buffer" revert-buffer t]
340 "--"
341 ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans]
342 ["Rescan Entire Document" reftex-index-Rescan t])
343 ("Restrict"
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])
350 ("Edit"
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]
355 "--"
356 ["Add Parentkey" reftex-index-level-down t]
357 ["Remove Parentkey " reftex-index-level-up t]
358 "--"
359 ["Make Start-of-Range" reftex-index-toggle-range-beginning t]
360 ["Make End-of-Range" reftex-index-toggle-range-end t]
361 "--"
362 ["Kill Entry" reftex-index-kill nil]
363 "--"
364 ["Undo" reftex-index-undo nil])
365 ("Options"
366 ["Context" reftex-index-toggle-context :style toggle
367 :selected reftex-index-include-context]
368 "--"
369 ["Follow Mode" reftex-index-toggle-follow :style toggle
370 :selected reftex-index-follow-mode])
371 "--"
372 ["Help" reftex-index-show-help t]))
374 map)
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 ">"
405 " -%-"))
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
440 ;; files for this.
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
444 ;; safer.
445 (let* ((file (nth 3 data))
446 (literal (nth 2 data))
447 (pos (nth 4 data))
448 (re (regexp-quote literal))
449 (match
450 (cond
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)))
456 (or (looking-at re)
457 (reftex-nearest-match re (length literal))))
458 (t (message "%s" reftex-no-follow-message) nil))))
459 (when match
460 (goto-char (match-beginning 0))
461 (recenter '(4))
462 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
463 match))
465 (defun reftex-display-index (&optional tag overriding-restriction redo
466 &rest locations)
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."
474 (interactive)
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))
490 (restriction
491 (or overriding-restriction
492 (and (not redo)
493 (reftex-get-restriction current-prefix-arg docstruct))))
494 (locations
495 ;; See if we are on an index macro as initial position
496 (or locations
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)
500 (save-excursion
501 (goto-char (+ (cdr what-macro)
502 (length 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))))))
507 buffer-name)
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)
528 (if restriction
529 (setq reftex-index-restriction-indicator (car restriction)
530 reftex-index-restriction-data (cdr restriction))
531 (if (not redo)
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)
539 (insert (format
540 "INDEX <%s> on %s
541 Restriction: <%s>
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
564 ;; DOCSTRUCT.
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)
570 (indent " ")
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))
578 (mouse-face
579 (if (memq reftex-highlight-selection '(mouse both))
580 reftex-mouse-selected-face
581 nil))
582 (index-face reftex-label-face)
583 sublist cell from to first-char)
585 ;; Make the sublist and sort it
586 (when bor
587 (setq all (or (memq bor all) all)))
589 (while (setq cell (pop all))
590 (if (eq cell eor)
591 (setq all nil)
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)))))
598 (when update-one
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)
603 (point-max))))
605 ;; Walk through the list and insert all entries
606 (while (setq cell (pop sublist))
607 (unless update-one
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)))
619 (setq from (point))
620 (insert indent (nth 7 cell))
621 (when font
622 (setq to (point))
623 (put-text-property
624 (- (point) (length (nth 7 cell))) to
625 'face index-face)
626 (goto-char 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)))
635 (insert "\n")
636 (setq to (point))
638 (when context
639 (insert context-indent (nth 2 cell) "\n")
640 (setq to (point)))
641 (put-text-property from to :data cell)
642 (when mouse-face
643 (put-text-property from (1- to)
644 'mouse-face mouse-face))
645 (goto-char to))))
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 "-----------------------------------------------------------------")
653 (when font
654 (put-text-property from (point) 'face reftex-index-section-face))
655 (insert "\n")))
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))
660 (point-max))))
661 (end (max (point) (or (condition-case nil (mark) (error nil))
662 (point-min))))
663 bor eor label here-I-am)
664 (cond
665 ((eq arg 2)
666 (setq here-I-am (car (reftex-where-am-I))
667 bor (if (eq (car here-I-am) 'toc)
668 here-I-am
669 (reftex-last-assoc-before-elt
670 'toc here-I-am docstruct))
671 eor (car (memq (assq 'toc (cdr (memq bor docstruct))) docstruct))
672 label (nth 6 bor)))
673 ((eq arg 3)
674 (save-excursion
675 (setq label "region")
676 (goto-char beg)
677 (setq bor (car (reftex-where-am-I)))
678 (setq bor (nth 1 (memq bor docstruct)))
679 (goto-char end)
680 (setq eor (nth 1 (memq (car (reftex-where-am-I)) docstruct)))))
681 (t nil))
682 (if (and label (or bor eor))
683 (list label bor eor)
684 nil)))
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)
694 (and (> (point) 1)
695 (not (get-text-property (point) 'intangible))
696 (memq reftex-highlight-selection '(cursor both))
697 (reftex-highlight 1
698 (or (previous-single-property-change (1+ (point)) :data)
699 (point-min))
700 (or (next-single-property-change (point) :data)
701 (point-max)))))
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))
709 (condition-case nil
710 (reftex-index-visit-location nil (not reftex-revisit-to-follow))
711 (error t)))))
713 (defun reftex-index-show-help ()
714 "Show a summary of special key bindings."
715 (interactive)
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."
725 (interactive "p")
726 (setq reftex-callback-fwd t)
727 (or (eobp) (forward-char 1))
728 (goto-char (or (next-single-property-change (point) :data)
729 (point)))
730 (unless (get-text-property (point) :data)
731 (goto-char (or (next-single-property-change (point) :data)
732 (point)))))
733 (defun reftex-index-previous (&optional arg)
734 "Move to previous selectable item."
735 (interactive "p")
736 (setq reftex-callback-fwd nil)
737 (goto-char (or (previous-single-property-change (point) :data)
738 (point)))
739 (unless (get-text-property (point) :data)
740 (goto-char (or (previous-single-property-change (point) :data)
741 (point)))))
742 (defun reftex-index-toggle-follow ()
743 "Toggle follow (other window follows with context)."
744 (interactive)
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."
750 (interactive)
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."
755 (interactive)
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."
759 (interactive)
760 (reftex-index-visit-location 'hide))
761 (defun reftex-index-goto-entry ()
762 "Go to document location in other window. *Index* window stays."
763 (interactive)
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."
767 (interactive "e")
768 (mouse-set-point ev)
769 (reftex-index-visit-location 'hide))
770 (defun reftex-index-quit ()
771 "Hide the *Index* window and do not move point."
772 (interactive)
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."
778 (interactive)
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."
786 (interactive)
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)
791 (reftex-toc))
792 (defun reftex-index-rescan (&rest ignore)
793 "Regenerate the *Index* buffer after reparsing file of section at point."
794 (interactive)
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))
799 (file (nth 3 data))
800 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
801 (if (not file)
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."
810 (interactive)
811 (let ((index-tag reftex-index-tag)
812 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
813 (switch-to-buffer
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."
819 (interactive)
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))))
824 (switch-to-buffer
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."
832 (interactive)
833 (switch-to-buffer
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.
841 (interactive)
842 (let* ((data (get-text-property (point) :data))
843 (docstruct (symbol-value reftex-docstruct-symbol))
844 bor eor)
845 (if (and (not data) force)
846 (setq data (assq 'toc docstruct)))
847 (when data
848 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
849 eor (car (memq (assq 'toc (cdr (memq bor docstruct)))
850 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)."
857 (interactive)
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."
866 (interactive)
867 (let* ((docstruct (symbol-value reftex-docstruct-symbol))
868 (bor (nth 1 reftex-index-restriction-data)))
869 (if (or (not bor)
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
874 (list bor
875 (car (memq (assq 'toc (cdr (memq bor docstruct)))
876 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."
882 (interactive)
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)))
886 (if (or (not bor)
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
891 (list bor eor))
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))
913 (unless match
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
920 (cond
921 ((eq final t)
922 (reftex-unhighlight 0)
923 (select-window show-window))
924 ((eq final 'hide)
925 (reftex-unhighlight 0)
926 (or (one-window-p) (delete-window))
927 (switch-to-buffer show-buffer))
928 (t nil))))
930 (defun reftex-index-analyze-entry (data)
931 ;; This splits the index context so that key, attribute and visual
932 ;; values are accessible individually.
933 (interactive)
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))
942 context boa)
943 (1+ (match-beginning 0))
944 eoa))
945 (boattr (if (string-match (concat "[^" (nth 3 sc) "]" (nth 1 sc))
946 context boa)
947 (1+ (match-beginning 0))
948 boactual))
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."
958 (interactive)
959 (let* ((data (get-text-property (point) :data))
960 old new)
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 `|('."
968 (interactive)
969 (let* ((data (get-text-property (point) :data))
970 (bor (concat (nth 1 reftex-index-special-chars) "("))
971 new analyze attr)
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 `|)'."
984 (interactive)
985 (let* ((data (get-text-property (point) :data))
986 (eor (concat (nth 1 reftex-index-special-chars) ")"))
987 new analyze attr)
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."
1000 (interactive)
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."
1005 (interactive "P")
1006 (reftex-index-edit-part arg 2 (nth 1 reftex-index-special-chars)
1007 "Attribute: "))
1009 (defun reftex-index-edit-visual (&optional arg)
1010 "EDIT the VISUAL part of the entry. With arg: remove entire VISUAL string."
1011 (interactive "P")
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)))
1025 ;; Tests:
1026 (cond ((string= npart opart)
1027 (error "Not changed"))
1028 ((string= npart "")
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."
1042 (interactive)
1043 (let* ((data (get-text-property (point) :data))
1044 (docstruct (symbol-value reftex-docstruct-symbol))
1045 old new prefix key)
1046 (unless data (error "Don't know which index entry to change"))
1047 (setq old (nth 2 data)
1048 key (nth 6 data)
1049 prefix (completing-read
1050 "Prefix: "
1051 (reftex-sublist-nth
1052 docstruct 6
1053 (lambda (x)
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)) "\\'")
1058 prefix)
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."
1067 (interactive)
1068 (let* ((data (get-text-property (point) :data))
1069 old new prefix)
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))
1075 "\\)")
1076 old)
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"
1085 (interactive)
1086 (error "This function is currently not implemented"))
1088 (defun reftex-index-undo ()
1089 "FIXME: Not yet implemented"
1090 (interactive)
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))
1098 old beg end info)
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))
1105 (goto-char beg)
1106 (unless (looking-at (regexp-quote old))
1107 (error "This should not happen (reftex-index-change-entry)"))
1108 (delete-region beg end)
1109 (insert new)
1110 (goto-char (1- beg))
1111 (when (and (re-search-forward (reftex-everything-regexp) nil t)
1112 (match-end 10)
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))
1117 (save-excursion
1118 (reftex-insert-index (list data) reftex-index-tag t
1119 "EDITED")))
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."
1125 (let ((pos (point))
1126 (case-fold-search nil))
1127 (goto-char (point-min))
1128 (forward-line 2)
1129 (if (re-search-forward (concat "^" (char-to-string char)) nil t)
1130 (progn
1131 (beginning-of-line)
1132 (recenter 0)
1133 (reftex-index-next))
1134 (goto-char pos)
1135 (if (eq char ?!)
1136 (error "This <%s> index does not contain entries sorted before the letters"
1137 reftex-index-tag)
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
1179 (loop for x in
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"
1196 '("Phrases"
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]
1200 "--"
1201 ("Sort Phrases"
1202 ["Sort" reftex-index-sort-phrases t]
1203 "--"
1204 "Sort Options"
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]
1214 "--"
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]
1221 "--"
1222 "Options"
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])
1236 "--"
1237 ["Save and Return" reftex-index-phrases-save-and-return t]))
1239 map)
1240 "Keymap used for *toc* buffer.")
1241 (defvar reftex-index-phrases-syntax-table
1242 (let ((table (make-syntax-table)))
1243 (modify-syntax-entry ?\" "." table)
1244 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
1252 `C-c C-c'."
1253 (interactive "P")
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)
1257 (message "%s"
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."
1263 (interactive)
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)))
1269 (find-file name)
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."
1278 (interactive)
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)))
1283 (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)
1292 (goto-char 1)
1293 (set-mark (point))
1294 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t)
1295 (end-of-line))
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
1308 (insert
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)
1318 (car x)
1319 nil))
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")))
1326 (defvar TeX-master)
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)
1334 ;; have to guess
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."
1339 (interactive)
1340 (save-buffer)
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)
1348 ;;;###autoload
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
1383 (list
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)
1388 (list 3 ss)
1389 '(4 font-lock-function-name-face)
1390 (list 5 ss)
1391 '(6 font-lock-string-face))
1392 (list reftex-index-phrases-phrase-regexp1
1393 '(1 font-lock-keyword-face)
1394 (list 2 ss)
1395 '(3 font-lock-string-face)
1396 (list 4 ss))
1397 (list reftex-index-phrases-phrase-regexp2
1398 '(1 font-lock-keyword-face)
1399 (list 2 ss)
1400 '(3 font-lock-string-face)
1401 (list 4 ss)
1402 '(5 font-lock-function-name-face))
1403 (cons "^\t$" ss)))
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."
1413 (interactive "p")
1414 (reftex-index-phrases-parse-header t)
1415 (while (> arg 0)
1416 (decf arg)
1417 (end-of-line)
1418 (if (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
1419 (progn
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."
1428 (interactive)
1429 (if (not slave) (reftex-index-phrases-parse-header t))
1430 (save-excursion
1431 (beginning-of-line)
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)
1439 ;; This is a phrase
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))
1449 (files
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))
1458 (unless macro-data
1459 (error "No macro associated with key %c" char))
1460 (unwind-protect
1461 (let ((overlay-arrow-string "=>")
1462 (overlay-arrow-position
1463 reftex-index-phrases-marker)
1464 (replace-count 0))
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."
1479 (interactive)
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
1485 the buffer."
1486 (interactive)
1487 (beginning-of-line)
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."
1493 (interactive "r")
1494 (reftex-index-phrases-parse-header t)
1495 (goto-char beg)
1496 (while (not (or (eobp)
1497 (>= (point) end)))
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))
1507 buf)
1508 (if get-files
1509 ;; Get the file list
1510 (save-excursion
1511 (setq buf (reftex-get-file-buffer-force master))
1512 (unless buf (error "Master file %s not found" master))
1513 (set-buffer buf)
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)
1519 (save-excursion
1520 (goto-char (point-min))
1521 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t)
1522 (push (list
1523 (string-to-char (match-string 2))
1524 (match-string 4)
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."
1540 (interactive "r")
1541 (let ((win-conf (current-window-configuration))
1542 (reftex-index-phrases-restrict-file (buffer-file-name)))
1543 (save-excursion
1544 (save-restriction
1545 (narrow-to-region beg end)
1546 (unwind-protect
1547 (progn
1548 ;; Hide the region highlighting
1549 (if (featurep 'xemacs)
1550 (zmacs-deactivate-region)
1551 (deactivate-mark))
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."
1559 (interactive)
1560 (if (and text (stringp text))
1561 (progn
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)
1568 (progn
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)
1574 (end-of-line))
1575 (if (not (bolp))
1576 (insert "\n"))
1577 (insert "\t")
1578 (if (and text (stringp text))
1579 (insert 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."
1589 (interactive "P")
1590 (if (catch 'exit
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)))
1596 (if (save-excursion
1597 (goto-char (point-min))
1598 (and (re-search-forward re nil t)
1599 (re-search-forward re nil t)))
1600 (throw 'exit t)))))
1601 (progn
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."
1609 (interactive)
1610 (save-excursion
1611 (beginning-of-line)
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))
1633 superphrases
1634 (nmatches 0)
1635 (ntimes1 0)
1636 (ntimes2 0)
1637 (case-fold-search reftex-index-phrases-case-fold-search)
1638 file files buf)
1639 (setq files reftex-index-phrases-files)
1640 (save-excursion
1641 (save-restriction
1642 (widen)
1643 (goto-char (point-min))
1644 (while (re-search-forward re1 nil t)
1645 (incf ntimes1))
1646 (goto-char (point-min))
1647 (while (re-search-forward re2 nil t)
1648 (push (cons (count-lines 1 (point)) (match-string 1)) superphrases)
1649 (incf ntimes2))))
1650 (save-current-buffer
1651 (while (setq file (pop files))
1652 (setq buf (reftex-get-file-buffer-force file))
1653 (when buf
1654 (set-buffer buf)
1655 (save-excursion
1656 (save-restriction
1657 (widen)
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))
1668 (cond
1669 (index-key
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)))))
1673 (repeat
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))
1678 (terpri)
1679 (princ (format "Total matches: %s in %s\n"
1680 (reftex-number nmatches "match" "es")
1681 (reftex-number (length reftex-index-phrases-files)
1682 "LaTeX file")))
1683 (princ (format " Uniqueness: Phrase occurs %s in phrase buffer\n"
1684 (reftex-number ntimes1 "time")))
1685 (if (> ntimes2 1)
1686 (progn
1687 (princ (format " Superphrases: Phrase matches the following %s in the phrase buffer:\n"
1688 (reftex-number ntimes2 "line")))
1689 (mapcar (lambda(x)
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."
1699 (interactive)
1700 (reftex-index-phrases-parse-header)
1701 (save-excursion
1702 (beginning-of-line)
1703 (unless (or (looking-at reftex-index-phrases-phrase-regexp12)
1704 (looking-at "\t"))
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))
1708 macro-fmt repeat)
1709 (cond (macro-data)
1710 ((equal nc ?\ )
1711 (setq nc ""
1712 macro-data (car reftex-index-phrases-macro-data)))
1713 ((equal nc ?\C-m)
1714 (setq nc (char-after (point)))
1715 (if (equal nc ?\t)
1716 (setq nc ""
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))
1723 (if macro-data
1724 (progn
1725 (if (looking-at "[^\t]") (delete-char 1))
1726 (insert nc)
1727 (message "Line will use %s %s repeat" macro-fmt
1728 (if repeat "with" "without")))
1729 (error "Abort")))))
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."
1735 (interactive "P")
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))))
1739 beg end)
1740 (goto-char (point-min))
1741 ;; Find first and last phrase line in buffer
1742 (setq beg
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.
1749 (if (and beg end)
1750 (progn
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)
1767 (progn
1768 ;; Extract macro char and phrase-or-key for a
1769 (setq ca (match-string 1 a)
1770 pa (downcase
1771 (or (and reftex-index-phrases-sort-prefers-entry
1772 (match-string 6 a))
1773 (match-string 3 a))))
1774 (if (string-match reftex-index-phrases-phrase-regexp12 b)
1775 (progn
1776 ;; Extract macro char and phrase-or-key for b
1777 (setq cb (match-string 1 b)
1778 pb (downcase
1779 (or (and reftex-index-phrases-sort-prefers-entry
1780 (match-string 6 b))
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
1786 (if chars-first
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]\\)"
1804 "\\([ \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
1809 (downcase w)
1810 w)))
1811 words space-re)
1812 (if (and as-words
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)))
1834 (concat
1835 (mapconcat (lambda (x)
1836 (format macro-fmt
1837 (format (if mathp reftex-index-math-format "%s") x)))
1838 index-keys "")
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))
1844 (file))
1845 (unless files (error "No files"))
1846 (unwind-protect
1847 (progn
1848 (switch-to-buffer-other-window (reftex-get-file-buffer-force
1849 (car files)))
1850 (catch 'no-more-files
1851 (while (setq file (pop files))
1852 (switch-to-buffer (reftex-get-file-buffer-force file))
1853 (save-excursion
1854 (save-restriction
1855 (unless (stringp reftex-index-phrases-restrict-file)
1856 (widen))
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
1866 n Skip 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
1872 C-r Recursive edit.
1873 s / S Save this buffer / Save all document buffers
1874 C-g Abort"
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
1885 both ends."
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))
1893 (all-yes nil)
1894 match rpl char (beg (make-marker)) (end (make-marker)) mathp)
1895 (move-marker beg 1)
1896 (move-marker end 1)
1897 (unwind-protect
1898 (while (re-search-forward re nil t)
1899 (catch 'next-match
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))
1906 (setq mathp
1907 (save-match-data
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
1912 (save-match-data
1913 (reftex-index-phrase-match-is-indexed beg
1914 end)))
1915 (throw 'next-match nil))
1916 (reftex-highlight 0 (match-beginning 0) (match-end 0))
1917 (setq rpl
1918 (save-match-data
1919 (reftex-index-make-replace-string
1920 macro-fmt (match-string 0) ckey repeat mathp)))
1921 (while
1922 (not
1923 (catch 'loop
1924 (message "REPLACE: %s? (yn!qoe%s?)"
1926 (if (> nkeys 1)
1927 (concat "1-" (int-to-string nkeys))
1928 ""))
1929 (setq char (if all-yes ?y (read-char-exclusive)))
1930 (cond ((member char '(?y ?Y ?\ ))
1931 ;; Yes!
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))
1937 (throw 'loop t))
1938 ((member char '(?n ?N ?\C-h ?\C-?));; FIXME: DEL
1939 ;; No
1940 (throw 'loop t))
1941 ((equal char ?!)
1942 ;; Yes for all in this buffer
1943 (setq all-yes t))
1944 ((equal char ?q)
1945 ;; Stop this one in this file
1946 (goto-char (point-max))
1947 (throw 'loop t))
1948 ((equal char ?Q)
1949 ;; Stop this one
1950 (throw 'no-more-files t))
1951 ((equal char ?s)
1952 (save-buffer))
1953 ((equal char ?S)
1954 (reftex-save-all-document-buffers))
1955 ((equal char ?\C-g)
1956 (keyboard-quit))
1957 ((member char '(?o ?O))
1958 ;; Select a different macro
1959 (let* ((nc (reftex-index-select-phrases-macro 2))
1960 (macro-data
1961 (cdr (assoc nc reftex-index-phrases-macro-data)))
1962 (macro-fmt (car macro-data))
1963 (repeat (nth 1 macro-data)))
1964 (if macro-data
1965 (setq rpl (save-match-data
1966 (reftex-index-make-replace-string
1967 macro-fmt match
1968 ckey repeat mathp)))
1969 (ding))))
1970 ((equal char ?\?)
1971 ;; Help
1972 (with-output-to-temp-buffer "*Help*"
1973 (princ reftex-index-phrases-help)))
1974 ((equal char ?\C-r)
1975 ;; Recursive edit
1976 (save-match-data
1977 (save-excursion
1978 (message "%s"
1979 (substitute-command-keys
1980 "Recursive edit. Resume with \\[exit-recursive-edit]"))
1981 (recursive-edit))))
1982 ((equal char ?e)
1983 (setq rpl (read-string "Edit: " rpl)))
1984 ((equal char ?0)
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))))
1989 ((and (> char ?0)
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))))
1995 (t (ding)))
1996 nil)))))
1997 (message "")
1998 (move-marker beg nil)
1999 (move-marker end nil)
2000 (setq all-yes 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.
2006 (save-excursion
2007 (goto-char end)
2008 (let* ((all-macros (reftex-what-macro t))
2009 ; (this-macro (car (car all-macros)))
2010 (before-macro
2011 (and (> beg 2)
2012 (goto-char (1- beg))
2013 (memq (char-after (point)) '(?\] ?\}))
2014 (car (reftex-what-macro 1))))
2015 (after-macro
2016 (and (goto-char end)
2017 (looking-at "\\(\\\\[a-zA-Z]+\\*?\\)[[{]")
2018 (match-string 1)))
2019 macro)
2020 (or (catch 'matched
2021 (while (setq macro (pop all-macros))
2022 (if (member (car macro) reftex-macros-with-index)
2023 (throw 'matched t)))
2024 nil)
2025 (and before-macro
2026 (member before-macro reftex-macros-with-index))
2027 (and after-macro
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)
2033 (save-excursion
2034 ;; Find line boundaries and possible line breaks near BEG and END
2035 (beginning-of-line)
2036 (setq bol (point))
2037 (end-of-line)
2038 (setq eol (point))
2039 (goto-char beg)
2040 (skip-chars-backward "^ \n")
2041 (if (and (equal (preceding-char) ?\ )
2042 (string-match "\\S-" (buffer-substring bol (point))))
2043 (setq space1 (1- (point))))
2044 (goto-char end)
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
2053 (cond
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
2065 (progn
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) ?\ )
2079 (goto-char pos)
2080 (delete-char 1)
2081 (insert "\n")))
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 "")
2088 "] "))
2089 (help (concat "Select an indexing macro\n========================\n"
2090 (mapconcat (lambda (x)
2091 (format " [%c] %s"
2092 (car x) (nth 1 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