Replace "Maintainer: FSF" with the emacs-devel mailing address
[bpt/emacs.git] / lisp / textmodes / reftex-index.el
CommitLineData
3afbc435 1;;; reftex-index.el --- index support with RefTeX
f2e3589a 2
ba318903 3;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
3ba2590f 4
6fbeb429 5;; Author: Carsten Dominik <dominik@science.uva.nl>
ce545621 6;; Maintainer: auctex-devel@gnu.org
3ba2590f
RS
7
8;; This file is part of GNU Emacs.
9
1fecc8fe 10;; GNU Emacs is free software: you can redistribute it and/or modify
3ba2590f 11;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
3ba2590f
RS
14
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.
19
20;; You should have received a copy of the GNU General Public License
1fecc8fe 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1a9461d0 22
3afbc435
PJ
23;;; Commentary:
24
25;;; Code:
26
7c4d13cc 27(eval-when-compile (require 'cl))
4f595e15
RA
28(declare-function texmathp "ext:texmathp" ())
29
1a9461d0 30(require 'reftex)
1a9461d0 31
3b919c9f 32;; START remove for XEmacs release
1a9461d0 33(defvar mark-active)
48ffe14f 34(defvar transient-mark-mode)
7b07114a 35(defvar TeX-master)
3b919c9f 36;; END remove for XEmacs release
f29263b3 37
7c4d13cc 38(defun reftex-index-selection-or-word (&optional arg phrase)
1a9461d0
CD
39 "Put selection or the word near point into the default index macro.
40This uses the information in `reftex-index-default-macro' to make an index
41entry. The phrase indexed is the current selection or the word near point.
42When called with one `C-u' prefix, let the user have a chance to edit the
43index entry. When called with 2 `C-u' as prefix, also ask for the index
44macro and other stuff.
45When called inside TeX math mode as determined by the `texmathp.el' library
46which 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
3666daf6 50 ;; check if we have an active selection
4f595e15 51 (active (reftex-region-active-p))
49275d55 52 (beg (if active
3666daf6 53 (region-beginning)
49275d55 54 (save-excursion
3666daf6
CD
55 (skip-syntax-backward "w\\") (point))))
56 (end (if active
57 (region-end)
49275d55 58 (save-excursion
3666daf6
CD
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)
1a9461d0 64
7c4d13cc 65 (if phrase
3666daf6
CD
66 (progn
67 (reftex-index-visit-phrases-buffer)
68 (reftex-index-new-phrase sel))
7c4d13cc
CD
69
70 (if (equal sel "")
3666daf6
CD
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)))))
49275d55 89
7c4d13cc 90(defun reftex-index (&optional char key tag sel no-insert)
49275d55 91 "Query for an index macro and insert it along with its arguments.
1a9461d0
CD
92The index macros available are those defined in `reftex-index-macro' or
93by a call to `reftex-add-index-macros', typically from an AUCTeX style file.
94RefteX provides completion for the index tag and the index key, and
95will prompt for other arguments."
96
97 (interactive)
98
99 ;; Ensure access to scanning info
100 (reftex-ensure-index-support t)
101 (reftex-access-scan-info current-prefix-arg)
102
103 ;; Find out which macro we are going to use
104 (let* ((char (or char
3666daf6
CD
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)
1a9461d0
CD
117
118 ;; Get the supported arguments
119 (if (stringp tag)
3666daf6 120 (setq tag1 tag)
1a9461d0
CD
121 (setq tag1 (or (reftex-index-complete-tag tag opt-args) "")))
122 (setq key (or key
3666daf6
CD
123 (reftex-index-complete-key
124 (if (string= tag1 "") "idx" tag1)
125 (member nindex opt-args))))
1a9461d0
CD
126
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)
3666daf6
CD
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 ": ")))))
1a9461d0 137 (unless (and opt (string= value ""))
3666daf6 138 (insert (if opt "[" "{") value (if opt "]" "}"))))
7c4d13cc 139 (and repeat (stringp sel) (insert sel))
1a9461d0 140 (and key reftex-plug-into-AUCTeX (fboundp 'LaTeX-add-index-entries)
3666daf6 141 (LaTeX-add-index-entries key))
1a9461d0
CD
142 (reftex-index-update-taglist tag1)
143 (reftex-notice-new)))
144
145(defun reftex-default-index ()
146 (cond ((null reftex-index-default-tag) nil)
3666daf6
CD
147 ((stringp reftex-index-default-tag) reftex-index-default-tag)
148 (t (or (get reftex-docstruct-symbol 'default-index-tag)
149 "idx"))))
1a9461d0
CD
150
151(defun reftex-update-default-index (tag &optional tag-list)
152 (if (and (not (equal tag ""))
3666daf6
CD
153 (stringp tag)
154 (eq reftex-index-default-tag 'last)
155 (or (null tag-list)
156 (member tag tag-list)))
1a9461d0
CD
157 (put reftex-docstruct-symbol 'default-index-tag tag)))
158
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)))
5b76833f
RF
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))))
1a9461d0
CD
179 (if (and default (equal tag "")) (setq tag default))
180 (reftex-update-default-index tag)
181 tag))
182
183(defun reftex-index-select-tag ()
184 ;; Have the user select an index tag.
185 ;; FIXME: should we cache tag-alist, prompt and help?
49275d55 186 (let* ((index-tags (cdr (assoc 'index-tags
3666daf6
CD
187 (symbol-value reftex-docstruct-symbol))))
188 (default (reftex-default-index)))
49275d55 189 (cond
1a9461d0
CD
190 ((null index-tags)
191 (error "No index tags available"))
192
193 ((= (length index-tags) 1)
194 ;; Just one index, use it
195 (car index-tags))
49275d55 196
1a9461d0
CD
197 ((> (length index-tags) 1)
198 ;; Several indices, ask.
199 (let* ((tags (copy-sequence index-tags))
3666daf6
CD
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)
216 tag
49275d55 217 (concat (substring tag 0 i)
3666daf6
CD
218 "[" (substring tag i (incf i)) "]"
219 (substring tag i)))
220 tag-alist)
221 (throw 'exit t)))
49275d55 222 (push (list (+ ?0 (incf cnt)) tag
3666daf6
CD
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)
236 "")
49275d55 237 (mapconcat (lambda(x)
3666daf6
CD
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)))))
1a9461d0
CD
250 (t (error "This should not happen (reftex-index-select-tag)")))))
251
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
3666daf6
CD
257 (symbol-value reftex-docstruct-symbol) 6
258 (lambda(x) (and (eq (car x) 'index)
259 (string= (nth 1 x) (or tag ""))))
260 t))
261 (prompt (concat "Index key" (if optional " (optional)" "") ": "))
262 (key (completing-read prompt table nil nil initial)))
1a9461d0
CD
263 key))
264
265(defun reftex-index-update-taglist (newtag)
49275d55 266 ;; add NEWTAG to the list of available index tags.
1a9461d0
CD
267 (let ((cell (assoc 'index-tags (symbol-value reftex-docstruct-symbol))))
268 (and newtag (cdr cell) (not (member newtag (cdr cell)))
3666daf6 269 (push newtag (cdr cell)))))
1a9461d0 270
e5bd0a28
SM
271(define-obsolete-variable-alias
272 'reftex-index-map 'reftex-index-mode-map "24.1")
4d789d84
SM
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)
279
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)
284
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)))
315
316 (loop for key across "0123456789" do
317 (define-key map (vector (list key)) 'digit-argument))
318 (define-key map "-" 'negative-argument)
319
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))))
325
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]))
373
374 map)
1a9461d0
CD
375 "Keymap used for *Index* buffers.")
376
377(defvar reftex-index-menu)
378
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.")
383
384(defvar reftex-index-return-marker (make-marker)
385 "Marker which makes it possible to return from index to old position.")
386
387(defvar reftex-index-restriction-indicator nil)
388(defvar reftex-index-restriction-data nil)
389
4d789d84 390(define-derived-mode reftex-index-mode fundamental-mode "RefTeX Index"
1a9461d0
CD
391 "Major mode for managing Index buffers for LaTeX files.
392This buffer was created with RefTeX.
393Press `?' for a summary of important key bindings, or check the menu.
394
395Here are all local bindings.
396
4d789d84 397\\{reftex-index-mode-map}"
1a9461d0
CD
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
3666daf6
CD
402 (list "---- " 'mode-line-buffer-identification
403 " " 'global-mode-string
404 " R<" 'reftex-index-restriction-indicator ">"
405 " -%-"))
1a9461d0 406 (setq truncate-lines t)
7ac7387b
CD
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))
1a9461d0 411 (make-local-variable 'reftex-last-follow-point)
4d789d84 412 (easy-menu-add reftex-index-menu reftex-index-mode-map)
1a9461d0 413 (add-hook 'post-command-hook 'reftex-index-post-command-hook nil t)
4d789d84 414 (add-hook 'pre-command-hook 'reftex-index-pre-command-hook nil t))
1a9461d0
CD
415
416(defconst reftex-index-help
417" AVAILABLE KEYS IN INDEX BUFFER
418 ==============================
419! A..Z Goto the section of entries starting with this letter.
420n / p next-entry / previous-entry
421SPC / TAB Show/Goto the corresponding entry in the LaTeX document.
422RET Goto the entry and hide the *Index* window (also on mouse-2).
423q / k Hide/Kill *Index* buffer.
424C-c = Switch to the TOC buffer.
425f / c Toggle follow mode / Toggle display of [c]ontext.
426g Refresh *Index* buffer.
427r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
428s Switch to a different index (for documents with multiple indices).
429e / C-k Edit/Kill the entry.
430* | @ Edit specific part of entry: [*]key [|]attribute [@]visual
431 With prefix: kill that part.
5829a569 432\( ) Toggle entry's beginning/end of page range property.
1a9461d0 433_ ^ Add/Remove parent key (to make this item a subitem).
1a9461d0
CD
434} / { Restrict Index to a single document section / Widen.
435< / > When restricted, move restriction to previous/next section.")
436
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
49275d55 444 ;; safer.
1a9461d0 445 (let* ((file (nth 3 data))
3666daf6
CD
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))))
274f1353 458 (t (message "%s" reftex-no-follow-message) nil))))
1a9461d0
CD
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))
464
f3c18bd0 465(defun reftex-display-index (&optional tag overriding-restriction redo
3666daf6 466 &rest locations)
1a9461d0
CD
467 "Display a buffer with an index compiled from the current document.
468When the document has multiple indices, first prompts for the correct one.
469When index support is turned off, offer to turn it on.
470With one or two `C-u' prefixes, rescan document first.
471With prefix 2, restrict index to current document section.
472With prefix 3, restrict index to region."
473
474 (interactive)
475
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))
480
481 (set-marker reftex-index-return-marker (point))
482 (setq reftex-last-follow-point 1)
483
484 ;; Determine the correct index to process
485 (let* ((docstruct (symbol-value reftex-docstruct-symbol))
3666daf6
CD
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
49275d55 492 (and (not redo)
3666daf6
CD
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
49275d55 501 (goto-char (+ (cdr what-macro)
3666daf6
CD
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)
1a9461d0
CD
508
509 (setq buffer-name (reftex-make-index-buffer-name index-tag))
510
511 ;; Goto the buffer and put it into the correct mode
49275d55 512
1a9461d0 513 (when (or restriction current-prefix-arg)
3666daf6 514 (reftex-kill-buffer buffer-name))
1a9461d0
CD
515
516 (if (get-buffer-window buffer-name)
3666daf6 517 (select-window (get-buffer-window buffer-name))
d63eb0e7 518 (switch-to-buffer buffer-name))
1a9461d0
CD
519
520 (or (eq major-mode 'reftex-index-mode) (reftex-index-mode))
521
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
3666daf6
CD
529 (setq reftex-index-restriction-indicator (car restriction)
530 reftex-index-restriction-data (cdr restriction))
f3c18bd0 531 (if (not redo)
3666daf6
CD
532 (setq reftex-index-restriction-indicator nil
533 reftex-index-restriction-data nil)))
1a9461d0
CD
534 (when (= (buffer-size) 0)
535 ;; buffer is empty - fill it
536 (message "Building %s buffer..." buffer-name)
537
538 (setq buffer-read-only nil)
539 (insert (format
540"INDEX <%s> on %s
541Restriction: <%s>
542SPC=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)))
548
549 (if (reftex-use-fonts)
550 (put-text-property 1 (point) 'face reftex-index-header-face))
551 (put-text-property 1 (point) 'intangible t)
552
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))))
561
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)
3666daf6
CD
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))
6c27f0f8 582 (index-face reftex-label-face)
3666daf6 583 sublist cell from to first-char)
1a9461d0
CD
584
585 ;; Make the sublist and sort it
586 (when bor
587 (setq all (or (memq bor all) all)))
588
589 (while (setq cell (pop all))
590 (if (eq cell eor)
3666daf6
CD
591 (setq all nil)
592 (and (eq (car cell) 'index)
593 (equal (nth 1 cell) tag)
594 (push cell sublist))))
1a9461d0 595 (setq sublist (sort (nreverse sublist)
3666daf6 596 (lambda (a b) (string< (nth 8 a) (nth 8 b)))))
1a9461d0
CD
597
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)
49275d55 602 (or (next-single-property-change (point) :data)
3666daf6 603 (point-max))))
1a9461d0
CD
604
605 ;; Walk through the list and insert all entries
606 (while (setq cell (pop sublist))
607 (unless update-one
3666daf6
CD
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)))
1a9461d0
CD
618
619 (setq from (point))
620 (insert indent (nth 7 cell))
621 (when font
3666daf6 622 (setq to (point))
49275d55 623 (put-text-property
3666daf6
CD
624 (- (point) (length (nth 7 cell))) to
625 'face index-face)
626 (goto-char to))
1a9461d0
CD
627
628 (when (or remark (nth 9 cell))
3666daf6
CD
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)))
1a9461d0
CD
634
635 (insert "\n")
636 (setq to (point))
637
638 (when context
3666daf6
CD
639 (insert context-indent (nth 2 cell) "\n")
640 (setq to (point)))
1a9461d0
CD
641 (put-text-property from to :data cell)
642 (when mouse-face
3666daf6
CD
643 (put-text-property from (1- to)
644 'mouse-face mouse-face))
1a9461d0
CD
645 (goto-char to))))
646
647
648(defun reftex-index-insert-new-letter (letter &optional font)
649 ;; Start a new section in the index
650 (let ((from (point)))
49275d55 651 (insert "\n" letter letter letter
3666daf6 652 "-----------------------------------------------------------------")
1a9461d0
CD
653 (when font
654 (put-text-property from (point) 'face reftex-index-section-face))
655 (insert "\n")))
656
657(defun reftex-get-restriction (arg docstruct)
c7015153 658 ;; Interpret the prefix ARG and derive index restriction specs.
1a9461d0 659 (let* ((beg (min (point) (or (condition-case nil (mark) (error nil))
3666daf6
CD
660 (point-max))))
661 (end (max (point) (or (condition-case nil (mark) (error nil))
662 (point-min))))
663 bor eor label here-I-am)
1a9461d0
CD
664 (cond
665 ((eq arg 2)
666 (setq here-I-am (car (reftex-where-am-I))
3666daf6
CD
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)))
1a9461d0
CD
673 ((eq arg 3)
674 (save-excursion
3666daf6
CD
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)))))
1a9461d0
CD
681 (t nil))
682 (if (and label (or bor eor))
3666daf6 683 (list label bor eor)
1a9461d0
CD
684 nil)))
685
686(defun reftex-index-pre-command-hook ()
687 ;; Used as pre command hook in *Index* buffer
688 (reftex-unhighlight 0)
689 (reftex-unhighlight 1))
690
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)
3666daf6
CD
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)))))
1a9461d0
CD
702 (if (integerp reftex-index-follow-mode)
703 ;; Remove delayed action
704 (setq reftex-index-follow-mode t)
705 (and reftex-index-follow-mode
3666daf6
CD
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)))))
1a9461d0
CD
712
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)))
722
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))
49275d55 728 (goto-char (or (next-single-property-change (point) :data)
3666daf6 729 (point)))
1a9461d0 730 (unless (get-text-property (point) :data)
49275d55 731 (goto-char (or (next-single-property-change (point) :data)
3666daf6 732 (point)))))
1a9461d0
CD
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)
3666daf6 738 (point)))
1a9461d0
CD
739 (unless (get-text-property (point) :data)
740 (goto-char (or (previous-single-property-change (point) :data)
3666daf6 741 (point)))))
1a9461d0
CD
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.
749Label 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.
785The 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
3666daf6
CD
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))
f3c18bd0 805 (reftex-display-index index-tag nil 'redo line)))
1a9461d0
CD
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)
3666daf6 812 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
1a9461d0
CD
813 (switch-to-buffer
814 (reftex-get-file-buffer-force reftex-last-index-file))
815 (setq current-prefix-arg '(16))
f3c18bd0 816 (reftex-display-index index-tag nil 'redo line)))
1a9461d0
CD
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))
3666daf6
CD
821 (index-tag reftex-index-tag)
822 (data (get-text-property (point) :data))
823 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
1a9461d0
CD
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
3666daf6 828 reftex-last-follow-point 1)
f3c18bd0 829 (reftex-display-index index-tag nil 'redo data line)))
1a9461d0
CD
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)
f3c18bd0 836 (reftex-display-index nil nil 'redo))
1a9461d0
CD
837
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))
3666daf6
CD
843 (docstruct (symbol-value reftex-docstruct-symbol))
844 bor eor)
1a9461d0 845 (if (and (not data) force)
3666daf6 846 (setq data (assq 'toc docstruct)))
1a9461d0
CD
847 (when data
848 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
3666daf6
CD
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) )))
1a9461d0
CD
853 (reftex-index-revert))
854
855(defun reftex-index-widen (&rest ignore)
856 "Show the unrestricted index (all entries)."
857 (interactive)
858 (setq reftex-index-restriction-indicator nil
3666daf6 859 reftex-index-restriction-data nil)
1a9461d0
CD
860 (reftex-index-revert)
861 (message "Index widened"))
862(defun reftex-index-restriction-forward (&rest ignore)
863 "Restrict to previous section.
864When index is currently unrestricted, restrict it to a section.
865When index is restricted, select the next section as restriction criterion."
866 (interactive)
867 (let* ((docstruct (symbol-value reftex-docstruct-symbol))
3666daf6 868 (bor (nth 1 reftex-index-restriction-data)))
1a9461d0 869 (if (or (not bor)
3666daf6
CD
870 (not (eq (car bor) 'toc)))
871 (reftex-index-restrict-to-section t)
1a9461d0 872 (setq reftex-index-restriction-indicator (nth 6 bor)
3666daf6 873 reftex-index-restriction-data
49275d55 874 (list bor
3666daf6
CD
875 (car (memq (assq 'toc (cdr (memq bor docstruct)))
876 docstruct))))
1a9461d0
CD
877 (reftex-index-revert))))
878(defun reftex-index-restriction-backward (&rest ignore)
879 "Restrict to next section.
880When index is currently unrestricted, restrict it to a section.
881When index is restricted, select the previous section as restriction criterion."
882 (interactive)
883 (let* ((docstruct (symbol-value reftex-docstruct-symbol))
3666daf6
CD
884 (eor (car reftex-index-restriction-data))
885 (bor (reftex-last-assoc-before-elt 'toc eor docstruct t)))
1a9461d0 886 (if (or (not bor)
3666daf6
CD
887 (not (eq (car bor) 'toc)))
888 (reftex-index-restrict-to-section t)
1a9461d0 889 (setq reftex-index-restriction-indicator (nth 6 bor)
3666daf6
CD
890 reftex-index-restriction-data
891 (list bor eor))
1a9461d0
CD
892 (reftex-index-revert))))
893
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.
da5ecfa9 899 ;; NO-REVISIT means don't visit files, just use live buffers.
1a9461d0
CD
900
901 (let* ((data (get-text-property (point) :data))
902 (index-window (selected-window))
903 show-window show-buffer match)
904
905 (unless data (error "Don't know which index entry to visit"))
49275d55 906
1a9461d0 907 (if (eq (car data) 'index)
3666daf6 908 (setq match (reftex-index-show-entry data no-revisit)))
1a9461d0
CD
909
910 (setq show-window (selected-window)
911 show-buffer (current-buffer))
912
913 (unless match
914 (select-window index-window)
915 (error "Cannot find location"))
916
917 (select-window index-window)
918
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))))
929
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))
3666daf6
CD
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)))
1a9461d0
CD
954 (list pre key attr actual post)))
955
1a9461d0
CD
956(defun reftex-index-edit ()
957 "Edit the index entry at point."
958 (interactive)
959 (let* ((data (get-text-property (point) :data))
3666daf6 960 old new)
1a9461d0
CD
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)))
965
966(defun reftex-index-toggle-range-beginning ()
967 "Toggle the page range start attribute `|('."
968 (interactive)
969 (let* ((data (get-text-property (point) :data))
3666daf6
CD
970 (bor (concat (nth 1 reftex-index-special-chars) "("))
971 new analyze attr)
1a9461d0
CD
972 (unless data (error "Don't know which index entry to edit"))
973 (setq analyze (reftex-index-analyze-entry data)
3666daf6 974 attr (nth 2 analyze))
1a9461d0
CD
975 (setf (nth 2 analyze) (if (string= attr bor) "" bor))
976 (setq new (apply 'concat analyze))
49275d55 977 (reftex-index-change-entry
1a9461d0 978 new (if (string= (nth 2 analyze) bor)
3666daf6
CD
979 "Entry is now START-OF-PAGE-RANGE"
980 "START-OF-PAGE-RANGE canceled"))))
1a9461d0
CD
981
982(defun reftex-index-toggle-range-end ()
983 "Toggle the page-range-end attribute `|)'."
984 (interactive)
985 (let* ((data (get-text-property (point) :data))
3666daf6
CD
986 (eor (concat (nth 1 reftex-index-special-chars) ")"))
987 new analyze attr)
1a9461d0
CD
988 (unless data (error "Don't know which index entry to edit"))
989 (setq analyze (reftex-index-analyze-entry data)
3666daf6 990 attr (nth 2 analyze))
1a9461d0
CD
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)
3666daf6
CD
995 "Entry is now END-OF-PAGE-RANGE"
996 "END-OF-PAGE-RANGE canceled"))))
1a9461d0
CD
997
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))
1002
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)
3666daf6 1007 "Attribute: "))
1a9461d0
CD
1008
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: "))
1013
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))
3666daf6 1017 new analyze opart npart)
1a9461d0
CD
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)
3666daf6 1021 opart (nth n analyze))
1a9461d0
CD
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)
3666daf6
CD
1027 (error "Not changed"))
1028 ((string= npart "")
1029 (if dont-allow-empty
5181ff9f 1030 (error "Invalid value")
3666daf6
CD
1031 (setf (nth n analyze) npart)))
1032 (t (setf (nth n analyze) (concat initial npart))))
1a9461d0
CD
1033 (setq new (apply 'concat analyze))
1034 ;; Change the entry and insert the changed version into the index.
49275d55 1035 (reftex-index-change-entry
1a9461d0 1036 new (if (string= npart "")
3666daf6
CD
1037 (format "Deleted: %s" opart)
1038 (format "New value is: %s" npart)))))
1a9461d0
CD
1039
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))
3666daf6
CD
1044 (docstruct (symbol-value reftex-docstruct-symbol))
1045 old new prefix key)
1a9461d0
CD
1046 (unless data (error "Don't know which index entry to change"))
1047 (setq old (nth 2 data)
3666daf6 1048 key (nth 6 data)
49275d55
JB
1049 prefix (completing-read
1050 "Prefix: "
1051 (reftex-sublist-nth
3666daf6
CD
1052 docstruct 6
1053 (lambda (x)
1054 (and (eq (car x) 'index)
1055 (string= (nth 1 x) reftex-index-tag))) t)))
49275d55 1056 (unless (string-match
3666daf6
CD
1057 (concat (regexp-quote (car reftex-index-special-chars)) "\\'")
1058 prefix)
1a9461d0
CD
1059 (setq prefix (concat prefix (car reftex-index-special-chars))))
1060 (if (string-match (regexp-quote key) old)
3666daf6 1061 (setq new (replace-match (concat prefix key) t t old))
1a9461d0
CD
1062 (error "Cannot construct new index key"))
1063 (reftex-index-change-entry new (format "Added prefix: %s" prefix))))
1064
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))
3666daf6 1069 old new prefix)
1a9461d0
CD
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) "]*"
3666daf6
CD
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))))
1a9461d0
CD
1080 (error "Entry is not a subitem"))
1081 (reftex-index-change-entry new (format "Removed prefix: %s" prefix))))
1082
1083(defun reftex-index-kill ()
1084 "FIXME: Not yet implemented"
1085 (interactive)
1086 (error "This function is currently not implemented"))
1087
1088(defun reftex-index-undo ()
1089 "FIXME: Not yet implemented"
1090 (interactive)
1091 (error "This function is currently not implemented"))
1092
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.
49275d55 1096
1a9461d0 1097 (let* ((data (get-text-property (point) :data))
3666daf6 1098 old beg end info)
1a9461d0
CD
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"))
9a529312 1104 (with-current-buffer (get-file-buffer (nth 3 data))
1a9461d0
CD
1105 (goto-char beg)
1106 (unless (looking-at (regexp-quote old))
3666daf6 1107 (error "This should not happen (reftex-index-change-entry)"))
1a9461d0
CD
1108 (delete-region beg end)
1109 (insert new)
1110 (goto-char (1- beg))
1111 (when (and (re-search-forward (reftex-everything-regexp) nil t)
3666daf6
CD
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))))
1a9461d0
CD
1116 (let ((buffer-read-only nil))
1117 (save-excursion
3666daf6
CD
1118 (reftex-insert-index (list data) reftex-index-tag t
1119 "EDITED")))
1a9461d0 1120 (setq reftex-last-follow-point 1)
5673af85 1121 (and message (message "%s" message))))
1a9461d0 1122
1a9461d0
CD
1123(defun reftex-index-goto-letter (char)
1124 "Go to the CHAR section in the index."
1125 (let ((pos (point))
3666daf6 1126 (case-fold-search nil))
e6ce8c42
GM
1127 (goto-char (point-min))
1128 (forward-line 2)
1a9461d0 1129 (if (re-search-forward (concat "^" (char-to-string char)) nil t)
3666daf6
CD
1130 (progn
1131 (beginning-of-line)
1132 (recenter 0)
1133 (reftex-index-next))
1a9461d0 1134 (goto-char pos)
7c4d13cc 1135 (if (eq char ?!)
3666daf6
CD
1136 (error "This <%s> index does not contain entries sorted before the letters"
1137 reftex-index-tag)
49275d55 1138 (error "This <%s> index does not contain entries starting with `%c'"
3666daf6 1139 reftex-index-tag char)))))
1a9461d0 1140
7c4d13cc
CD
1141
1142;;----------------------------------------------------------------------
1143;; The Index Phrases File
1144
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.
1166This gets refreshed in every phrases command.")
1167(defvar reftex-index-phrases-files nil
1168 "List of document files relevant for the phrases file.")
1169
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.")
e5bd0a28
SM
1174(define-obsolete-variable-alias
1175 'reftex-index-phrases-map 'reftex-index-phrases-mode-map "24.1")
4d789d84
SM
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)))
1193
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]))
1238
1239 map)
7c4d13cc 1240 "Keymap used for *toc* buffer.")
4f595e15
RA
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.")
7c4d13cc
CD
1246
1247(defun reftex-index-phrase-selection-or-word (arg)
1248 "Add current selection or word at point to the phrases buffer.
1249When you are in transient-mark-mode and the region is active, the
1250selection will be used - otherwise the word at point.
1251You 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)
49275d55 1257 (message "%s"
7c4d13cc 1258 (substitute-command-keys
3666daf6 1259 "Return to LaTeX with \\[reftex-index-phrases-save-and-return]"))))
7c4d13cc
CD
1260
1261(defun reftex-index-visit-phrases-buffer ()
1262 "Switch to the phrases buffer, initialize if empty."
1263 (interactive)
1264 (reftex-access-scan-info)
4f595e15 1265 (set-marker reftex-index-return-marker (point))
7c4d13cc 1266 (let* ((master (reftex-TeX-master-file))
3666daf6
CD
1267 (name (concat (file-name-sans-extension master)
1268 reftex-index-phrase-file-extension)))
7c4d13cc
CD
1269 (find-file name)
1270 (unless (eq major-mode 'reftex-index-phrases-mode)
1271 (reftex-index-phrases-mode))
1272 (if (= (buffer-size) 0)
3666daf6 1273 (reftex-index-initialize-phrases-buffer master))))
7c4d13cc
CD
1274
1275(defun reftex-index-initialize-phrases-buffer (&optional master)
1276 "Initialize the phrases buffer by creating the header.
1277If the buffer is non-empty, delete the old header first."
1278 (interactive)
1279 (let* ((case-fold-search t)
3666daf6
CD
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)
49275d55 1287
7c4d13cc 1288 (if master (set (make-local-variable 'TeX-master)
3666daf6 1289 (file-name-nondirectory master)))
7c4d13cc
CD
1290
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)
3666daf6 1295 (end-of-line))
7c4d13cc
CD
1296 (beginning-of-line 2)
1297 (if (looking-at reftex-index-phrases-comment-regexp)
3666daf6 1298 (beginning-of-line 2))
7c4d13cc 1299 (while (looking-at "^[ \t]*$")
49275d55
JB
1300 (beginning-of-line 2))
1301 (if (featurep 'xemacs)
a445370f
DN
1302 (zmacs-activate-region)
1303 (setq mark-active t))
ce5a3ac0 1304 (if (yes-or-no-p "Delete and rebuild header? ")
3666daf6 1305 (delete-region (point-min) (point))))
7c4d13cc
CD
1306
1307 ;; Insert the mode line
1308 (insert
1309 (format "%% -*- mode: reftex-index-phrases; TeX-master: \"%s\" -*-\n"
3666daf6 1310 (file-name-nondirectory (reftex-index-phrase-tex-master))))
7c4d13cc
CD
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)
3666daf6
CD
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))))
7c4d13cc 1321 (insert (format ">>>INDEX_MACRO_DEFINITION:\t%s\t%-20s\t%s\n"
3666daf6
CD
1322 (char-to-string key) (concat macro "{%s}")
1323 (if repeat "t" "nil"))))
7c4d13cc
CD
1324 (insert "%---------------------------------------------------------------------\n\n\n")))
1325
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)
3666daf6
CD
1330 (local-variable-p 'TeX-master (current-buffer))
1331 (stringp TeX-master))
7c4d13cc
CD
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")))
1336
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))))
1343
1344
1345(defvar reftex-index-phrases-menu)
7b07114a 1346(defvar reftex-index-phrases-marker)
7c4d13cc
CD
1347(defvar reftex-index-phrases-restrict-file nil)
1348;;;###autoload
4d789d84 1349(define-derived-mode reftex-index-phrases-mode fundamental-mode "Phrases"
7c4d13cc
CD
1350 "Major mode for managing the Index phrases of a LaTeX document.
1351This buffer was created with RefTeX.
1352
1353To 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.
1356
1357To index phrases use one of:
1358
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
1364
1365You can sort the phrases in this buffer with \\[reftex-index-sort-phrases].
1366To display information about the phrase at point, use \\[reftex-index-phrases-info].
1367
1368For more information see the RefTeX User Manual.
1369
1370Here are all local bindings.
1371
4d789d84 1372\\{reftex-index-phrases-mode-map}"
4f595e15 1373 :syntax-table reftex-index-phrases-syntax-table
49275d55 1374 (set (make-local-variable 'font-lock-defaults)
7c4d13cc 1375 reftex-index-phrases-font-lock-defaults)
4d789d84
SM
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)
7c4d13cc
CD
1379
1380;; Font Locking stuff
1381(let ((ss (if (featurep 'xemacs) 'secondary-selection ''secondary-selection)))
1382 (setq reftex-index-phrases-font-lock-keywords
49275d55 1383 (list
3666daf6
CD
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)))
7c4d13cc 1404 (setq reftex-index-phrases-font-lock-defaults
3666daf6
CD
1405 '((reftex-index-phrases-font-lock-keywords)
1406 nil t nil beginning-of-line))
49275d55 1407 (put 'reftex-index-phrases-mode 'font-lock-defaults
7c4d13cc
CD
1408 reftex-index-phrases-font-lock-defaults) ; XEmacs
1409 )
1410
7c4d13cc
CD
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)
3666daf6
CD
1419 (progn
1420 (goto-char (match-beginning 0))
f3c18bd0 1421 (reftex-index-this-phrase 'slave))
7c4d13cc
CD
1422 (error "No more phrase lines after point"))))
1423
f3c18bd0 1424(defun reftex-index-this-phrase (&optional slave)
7c4d13cc
CD
1425 "Index the phrase in the current line.
1426Does a global search and replace in the entire document. At each
1427match, the user will be asked to confirm the replacement."
1428 (interactive)
f3c18bd0 1429 (if (not slave) (reftex-index-phrases-parse-header t))
7c4d13cc
CD
1430 (save-excursion
1431 (beginning-of-line)
1432 (cond ((looking-at reftex-index-phrases-comment-regexp)
f3c18bd0 1433 (if (not slave) (error "Comment line")))
3666daf6 1434 ((looking-at "^[ \t]*$")
f3c18bd0 1435 (if (not slave) (error "Empty line")))
3666daf6 1436 ((looking-at reftex-index-phrases-macrodef-regexp)
f3c18bd0 1437 (if (not slave) (error "Macro definition line")))
3666daf6
CD
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)
5181ff9f 1454 (error "Invalid restriction file %s"
3666daf6
CD
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
49275d55
JB
1469 (reftex-query-index-phrase-globally
1470 files phrase macro-fmt
3666daf6 1471 index-key repeat as-words)
49275d55 1472 (message "%s replaced"
3666daf6
CD
1473 (reftex-number replace-count "occurrence"))))))
1474 (t (error "Cannot parse this line")))))
7c4d13cc
CD
1475
1476(defun reftex-index-all-phrases ()
1477 "Index all phrases in the phrases buffer.
1478Calls `reftex-index-this-phrase' on each line in the buffer."
1479 (interactive)
1480 (reftex-index-region-phrases (point-min) (point-max)))
1481
1482(defun reftex-index-remaining-phrases ()
1483 "Index all phrases in the phrases buffer.
1484Calls `reftex-index-this-phrase' on each line ay and below point in
1485the buffer."
1486 (interactive)
1487 (beginning-of-line)
1488 (reftex-index-region-phrases (point) (point-max)))
1489
1490(defun reftex-index-region-phrases (beg end)
1491 "Index all phrases in the phrases buffer.
1492Calls `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)
3666daf6 1497 (>= (point) end)))
f3c18bd0 1498 (save-excursion (reftex-index-this-phrase 'slave))
7c4d13cc
CD
1499 (beginning-of-line 2)))
1500
1501(defun reftex-index-phrases-parse-header (&optional get-files)
1502 "Parse the header of a phrases file to extract the macro definitions.
1503The definitions get stored in `reftex-index-phrases-macro-data'.
1504Also switches to the LaTeX document to find out which files belong to
1505the document and stores the list in `reftex-index-phrases-files'."
1506 (let* ((master (reftex-index-phrase-tex-master))
3666daf6 1507 buf)
7c4d13cc 1508 (if get-files
3666daf6
CD
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)
49275d55 1515 (setq reftex-index-phrases-files
3666daf6 1516 (reftex-all-document-files))))
7c4d13cc
CD
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)
3666daf6
CD
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))
7c4d13cc
CD
1527 ;; Reverse the list, so that the first macro is first
1528 (if (null reftex-index-phrases-macro-data)
3666daf6 1529 (error "No valid MACRO DEFINITION line in %s file (make sure to use TAB separators)" reftex-index-phrase-file-extension))
49275d55 1530 (setq reftex-index-phrases-macro-data
3666daf6 1531 (nreverse reftex-index-phrases-macro-data))
7c4d13cc
CD
1532 (goto-char (point-min)))))
1533
1534(defun reftex-index-phrases-apply-to-region (beg end)
1535 "Index all index phrases in the current region.
1536This works exactly like global indexing from the index phrases buffer,
1537but operation is restricted to the current region. This is useful if
1538you need to add/change text in an already indexed document and want to
1539index the new part without having to go over the unchanged parts again."
1540 (interactive "r")
1541 (let ((win-conf (current-window-configuration))
49275d55 1542 (reftex-index-phrases-restrict-file (buffer-file-name)))
7c4d13cc
CD
1543 (save-excursion
1544 (save-restriction
1545 (narrow-to-region beg end)
1546 (unwind-protect
3666daf6
CD
1547 (progn
1548 ;; Hide the region highlighting
a445370f
DN
1549 (if (featurep 'xemacs)
1550 (zmacs-deactivate-region)
1551 (deactivate-mark))
3666daf6
CD
1552 (delete-other-windows)
1553 (reftex-index-visit-phrases-buffer)
1554 (reftex-index-all-phrases))
1555 (set-window-configuration win-conf))))))
7c4d13cc
CD
1556
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
3666daf6
CD
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
49275d55 1566 (concat "^\\(\\S-*\\)\t\\(" (regexp-quote text)
3666daf6
CD
1567 "\\) *[\t\n]") nil t)
1568 (progn
1569 (goto-char (match-end 2))
1570 (error "Phrase is already in phrases buffer")))))
7c4d13cc
CD
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)))
1580
1581(defun reftex-index-find-next-conflict-phrase (&optional arg)
1582 "Find the next a phrase which is has conflicts in the phrase buffer.
1583The command helps to find possible conflicts in the phrase indexing process.
1584It searches downward from point for a phrase which is repeated elsewhere
1585in the buffer, or which is a subphrase of another phrase. If such a
1586phrase is found, the phrase info is displayed.
1587To check the whole buffer, start at the beginning and continue by calling
1588this function repeatedly."
1589 (interactive "P")
1590 (if (catch 'exit
3666daf6
CD
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)))
49275d55 1596 (if (save-excursion
3666daf6
CD
1597 (goto-char (point-min))
1598 (and (re-search-forward re nil t)
1599 (re-search-forward re nil t)))
1600 (throw 'exit t)))))
7c4d13cc 1601 (progn
3666daf6
CD
1602 (reftex-index-phrases-info)
1603 (message "Phrase with match conflict discovered"))
7c4d13cc
CD
1604 (goto-char (point-max))
1605 (error "No further problematic phrases found")))
1606
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) ""))
3666daf6
CD
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)
7c4d13cc
CD
1639 (setq files reftex-index-phrases-files)
1640 (save-excursion
3666daf6
CD
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))))
9a529312 1650 (save-current-buffer
3666daf6
CD
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)))))))))
7c4d13cc 1663 (with-output-to-temp-buffer "*Help*"
3666daf6
CD
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)))
1675 (t
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")))
49275d55 1689 (mapcar (lambda(x)
3666daf6
CD
1690 (princ (format " Line %4d: %s\n" (car x) (cdr x))))
1691 (nreverse superphrases))))))))
7c4d13cc
CD
1692
1693(defun reftex-index-phrases-set-macro-key ()
1694 "Change the macro key for the current line.
1695Prompts for a macro key and insert is at the beginning of the line.
1696If you reply with SPACE, the macro keyn will be removed, so that the
1697default macro will be used. If you reply with `RET', just prints
1698information 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)
3666daf6 1704 (looking-at "\t"))
7c4d13cc
CD
1705 (error "This is not a phrase line"))
1706 (let* ((nc (reftex-index-select-phrases-macro 0))
3666daf6
CD
1707 (macro-data (assoc nc reftex-index-phrases-macro-data))
1708 macro-fmt repeat)
7c4d13cc 1709 (cond (macro-data)
3666daf6
CD
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)))
7c4d13cc
CD
1720
1721 (setq macro-fmt (nth 1 macro-data)
3666daf6 1722 repeat (nth 2 macro-data))
7c4d13cc 1723 (if macro-data
3666daf6
CD
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")))))
7c4d13cc
CD
1730
1731(defun reftex-index-sort-phrases (&optional chars-first)
1732 "Sort the phrases lines in the buffer alphabetically.
1733Normally, this looks only at the phrases. With a prefix arg CHARS-FIRST,
1734it 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))
3666daf6
CD
1738 (progn (end-of-line) (point))))
1739 beg end)
7c4d13cc
CD
1740 (goto-char (point-min))
1741 ;; Find first and last phrase line in buffer
49275d55 1742 (setq beg
3666daf6
CD
1743 (and (re-search-forward reftex-index-phrases-phrase-regexp12 nil t)
1744 (match-beginning 0)))
7c4d13cc
CD
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)
3666daf6
CD
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")
537b04b9 1755 (let ((inhibit-quit t)) ;; make sure we do not lose lines
3666daf6
CD
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))
7c4d13cc
CD
1760 (error "Cannot find phrases lines to sort"))))
1761
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)
3666daf6
CD
1767 (progn
1768 ;; Extract macro char and phrase-or-key for a
49275d55
JB
1769 (setq ca (match-string 1 a)
1770 pa (downcase
3666daf6
CD
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
49275d55 1777 (setq cb (match-string 1 b)
3666daf6
CD
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'
49275d55 1785 ;; `chars-first' is bound locally in the calling function
3666daf6
CD
1786 (if chars-first
1787 (if (string= ca cb) p-p c-p)
1788 (if (string= pa pb) c-p p-p)))))
7c4d13cc
CD
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))))
1794
1795(defvar reftex-index-phrases-menu)
49275d55 1796(defun reftex-index-make-phrase-regexp (phrase &optional
3666daf6 1797 as-words allow-newline)
7c4d13cc
CD
1798 "Return a regexp matching PHRASE, even if distributed over lines.
1799With optional arg AS-WORDS, require word boundary at beginning and end.
1800With optional arg ALLOW-NEWLINE, allow single newline between words."
1801 (let* ((words (split-string phrase))
3666daf6
CD
1802 (space-re (if allow-newline
1803 "\\([ \t]*\\(\n[ \t]*\\)?\\|[ \t]\\)"
1804 "\\([ \t]+\\)")))
7c4d13cc 1805 (concat (if (and as-words (string-match "\\`\\w" (car words)))
f3c18bd0 1806 "\\(\\<\\|[`']\\)" "")
49275d55 1807 (mapconcat (lambda (w) (regexp-quote
f3c18bd0
CD
1808 (if reftex-index-phrases-case-fold-search
1809 (downcase w)
1810 w)))
3666daf6 1811 words space-re)
49275d55 1812 (if (and as-words
3666daf6 1813 (string-match "\\w\\'" (nth (1- (length words)) words)))
f3c18bd0 1814 "\\(\\>\\|'\\)" ""))))
7c4d13cc
CD
1815
1816(defun reftex-index-simplify-phrase (phrase)
1817 "Make phrase single spaces and single line."
1818 (mapconcat 'identity (split-string phrase) " "))
1819
1820(defun reftex-index-phrases-find-dup-re (phrase &optional sub)
1821 "Return a regexp which matches variations of PHRASE (with additional space).
1822When SUB ins non-nil, the regexp will also match when PHRASE is a subphrase
1823of another phrase. The regexp works lonly in the phrase buffer."
1824 (concat (if sub "^\\S-?\t\\([^\t\n]*" "^\\S-?\t")
3666daf6
CD
1825 (mapconcat 'regexp-quote (split-string phrase) " +")
1826 (if sub "[^\t\n]*\\)\\([\t\n]\\|$\\)" " *\\([\t\n]\\|$\\)")))
7c4d13cc
CD
1827
1828(defun reftex-index-make-replace-string (macro-fmt match index-key
3666daf6 1829 &optional repeat mathp)
7c4d13cc
CD
1830 "Return the string which can be used as replacement.
1831Treats the logical `and' for index phrases."
1832 (let ((index-keys (split-string (or index-key match)
3666daf6 1833 reftex-index-phrases-logical-and-regexp)))
7c4d13cc 1834 (concat
49275d55
JB
1835 (mapconcat (lambda (x)
1836 (format macro-fmt
3666daf6
CD
1837 (format (if mathp reftex-index-math-format "%s") x)))
1838 index-keys "")
7c4d13cc
CD
1839 (if repeat (reftex-index-simplify-phrase match) ""))))
1840
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))
3666daf6 1844 (file))
7c4d13cc
CD
1845 (unless files (error "No files"))
1846 (unwind-protect
3666daf6 1847 (progn
49275d55 1848 (switch-to-buffer-other-window (reftex-get-file-buffer-force
3666daf6
CD
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))))))
7c4d13cc
CD
1859 (reftex-unhighlight 0)
1860 (set-window-configuration win-conf))))
1861
1862(defconst reftex-index-phrases-help
1863 " Keys for query-index search
1864 ===========================
1865y Replace this match
1866n Skip this match
1867! Replace this and all further matches in this file
1868q / Q Skip match, start next file / start next phrase
1869o Use a different indexing macro for this match
18701 - 9 Select one of the multiple phrases
1871e Edit the replacement text
1872C-r Recursive edit.
1873s / S Save this buffer / Save all document buffers
1874C-g Abort"
1875 "The help string for indexing phrases.")
1876
1877(defvar replace-count)
1878(defun reftex-query-index-phrase (phrase macro-fmt &optional
3666daf6 1879 index-key repeat as-words)
7c4d13cc
CD
1880 "Search through buffer for PHRASE, and offer to replace it with an indexed
1881version. The index version is derived by applying `format' with MACRO-FMT
1882to INDEX-KEY or PHRASE. When REPEAT is non-nil, the PHRASE is inserted
1883again after the macro.
1884AS-WORDS means, the search for PHRASE should require word boundaries at
1885both ends."
1886 (let* ((re (reftex-index-make-phrase-regexp phrase as-words 'allow-newline))
3666daf6 1887 (case-fold-search reftex-index-phrases-case-fold-search)
49275d55 1888 (index-keys (split-string
3666daf6
CD
1889 (or index-key phrase)
1890 reftex-index-phrases-logical-or-regexp))
1891 (nkeys (length index-keys))
1892 (ckey (nth 0 index-keys))
49275d55 1893 (all-yes nil)
7b07114a
CD
1894 match rpl char (beg (make-marker)) (end (make-marker)) mathp)
1895 (move-marker beg 1)
1896 (move-marker end 1)
7c4d13cc 1897 (unwind-protect
3666daf6
CD
1898 (while (re-search-forward re nil t)
1899 (catch 'next-match
f3c18bd0
CD
1900 (if (reftex-in-comment)
1901 (throw 'next-match nil))
3666daf6
CD
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))))
7b07114a
CD
1909 (setq beg (move-marker beg (match-beginning 0))
1910 end (move-marker end (match-end 0)))
3666daf6
CD
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))
49275d55 1917 (setq rpl
3666daf6
CD
1918 (save-match-data
1919 (reftex-index-make-replace-string
1920 macro-fmt (match-string 0) ckey repeat mathp)))
49275d55 1921 (while
3666daf6
CD
1922 (not
1923 (catch 'loop
1924 (message "REPLACE: %s? (yn!qoe%s?)"
1925 rpl
49275d55 1926 (if (> nkeys 1)
3666daf6
CD
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))
b762841f 1958 ;; Select a different macro
3666daf6 1959 (let* ((nc (reftex-index-select-phrases-macro 2))
49275d55 1960 (macro-data
3666daf6
CD
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
49275d55 1978 (message "%s"
3666daf6
CD
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)))))
7c4d13cc 1997 (message "")
7b07114a
CD
1998 (move-marker beg nil)
1999 (move-marker end nil)
7c4d13cc
CD
2000 (setq all-yes nil)
2001 (reftex-unhighlight 0))))
2002
2003(defun reftex-index-phrase-match-is-indexed (beg end)
f3c18bd0 2004 ;; Check if match is in an argument of an index macro, or if an
3666daf6 2005 ;; index macro is directly attached to the match.
7c4d13cc
CD
2006 (save-excursion
2007 (goto-char end)
3666daf6 2008 (let* ((all-macros (reftex-what-macro t))
7b07114a 2009; (this-macro (car (car all-macros)))
3666daf6
CD
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))))))
db95369b 2029
7c4d13cc
CD
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) ?\ )
3666daf6
CD
2042 (string-match "\\S-" (buffer-substring bol (point))))
2043 (setq space1 (1- (point))))
7c4d13cc
CD
2044 (goto-char end)
2045 (skip-chars-forward "^ \n")
2046 (if (and (equal (following-char) ?\ )
3666daf6
CD
2047 (string-match "\\S-" (buffer-substring (point) eol)))
2048 (setq space2 (point)))
7c4d13cc
CD
2049 ;; Now check what we have and insert the newlines
2050 (if (<= (- eol bol) fill-column)
3666daf6
CD
2051 ;; Line is already short
2052 nil
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))))))))))
7c4d13cc
CD
2074
2075(defun reftex-index-phrases-replace-space (pos)
2076 "If there is a space at POS, replace it with a newline char.
2077Does not do a save-excursion."
2078 (when (equal (char-after pos) ?\ )
2079 (goto-char pos)
2080 (delete-char 1)
2081 (insert "\n")))
2082
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: ["
3666daf6
CD
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"))))
7c4d13cc
CD
2094 (reftex-select-with-char prompt help delay)))
2095
4f595e15 2096(provide 'reftex-index)
7c4d13cc 2097
1a9461d0 2098;;; reftex-index.el ends here