Commit | Line | Data |
---|---|---|
3afbc435 | 1 | ;;; reftex-index.el --- index support with RefTeX |
f2e3589a | 2 | |
acaf905b | 3 | ;; Copyright (C) 1997-2012 Free Software Foundation, Inc. |
3ba2590f | 4 | |
6fbeb429 | 5 | ;; Author: Carsten Dominik <dominik@science.uva.nl> |
ce545621 | 6 | ;; Maintainer: auctex-devel@gnu.org |
5d2a58e0 | 7 | ;; Version: 4.31 |
bd78fa1d | 8 | ;; Package: reftex |
3ba2590f RS |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
1fecc8fe | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
3ba2590f | 13 | ;; it under the terms of the GNU General Public License as published by |
1fecc8fe GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
3ba2590f RS |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
1fecc8fe | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
1a9461d0 | 24 | |
3afbc435 PJ |
25 | ;;; Commentary: |
26 | ||
27 | ;;; Code: | |
28 | ||
7c4d13cc | 29 | (eval-when-compile (require 'cl)) |
1a9461d0 CD |
30 | (provide 'reftex-index) |
31 | (require 'reftex) | |
32 | ;;; | |
33 | ||
3b919c9f | 34 | ;; START remove for XEmacs release |
1a9461d0 | 35 | (defvar mark-active) |
48ffe14f | 36 | (defvar transient-mark-mode) |
7b07114a | 37 | (defvar TeX-master) |
3b919c9f | 38 | ;; END remove for XEmacs release |
f29263b3 DN |
39 | |
40 | (declare-function texmathp "ext:texmathp" ()) | |
41 | ||
7c4d13cc | 42 | (defun reftex-index-selection-or-word (&optional arg phrase) |
1a9461d0 CD |
43 | "Put selection or the word near point into the default index macro. |
44 | This uses the information in `reftex-index-default-macro' to make an index | |
45 | entry. The phrase indexed is the current selection or the word near point. | |
46 | When called with one `C-u' prefix, let the user have a chance to edit the | |
47 | index entry. When called with 2 `C-u' as prefix, also ask for the index | |
48 | macro and other stuff. | |
49 | When called inside TeX math mode as determined by the `texmathp.el' library | |
50 | which is part of AUCTeX, the string is first processed with the | |
51 | `reftex-index-math-format', which see." | |
52 | (interactive "P") | |
53 | (let* ((use-default (not (equal arg '(16)))) ; check for double prefix | |
3666daf6 | 54 | ;; check if we have an active selection |
4ea0e84a | 55 | (active (if (featurep 'xemacs) |
3666daf6 CD |
56 | (and zmacs-regions (region-exists-p)) ; XEmacs |
57 | (and transient-mark-mode mark-active))) ; Emacs | |
49275d55 | 58 | (beg (if active |
3666daf6 | 59 | (region-beginning) |
49275d55 | 60 | (save-excursion |
3666daf6 CD |
61 | (skip-syntax-backward "w\\") (point)))) |
62 | (end (if active | |
63 | (region-end) | |
49275d55 | 64 | (save-excursion |
3666daf6 CD |
65 | (skip-syntax-forward "w\\") (point)))) |
66 | (sel (buffer-substring beg end)) | |
67 | (mathp (condition-case nil (texmathp) (error nil))) | |
68 | (current-prefix-arg nil) ; we want to call reftex-index without prefix. | |
69 | key def-char def-tag full-entry) | |
1a9461d0 | 70 | |
7c4d13cc | 71 | (if phrase |
3666daf6 CD |
72 | (progn |
73 | (reftex-index-visit-phrases-buffer) | |
74 | (reftex-index-new-phrase sel)) | |
7c4d13cc CD |
75 | |
76 | (if (equal sel "") | |
3666daf6 CD |
77 | ;; Nothing selected, no word, so use full reftex-index command |
78 | (reftex-index) | |
79 | ;; OK, we have something to index here. | |
80 | ;; Add the dollars when necessary | |
81 | (setq key (if mathp | |
82 | (format reftex-index-math-format sel) | |
83 | sel)) | |
84 | ;; Get info from `reftex-index-default-macro' | |
85 | (setq def-char (if use-default (car reftex-index-default-macro))) | |
86 | (setq def-tag (if use-default (nth 1 reftex-index-default-macro))) | |
87 | ;; Does the user want to edit the entry? | |
88 | (setq full-entry (if arg | |
89 | (reftex-index-complete-key | |
90 | def-tag nil (cons key 0)) | |
91 | key)) | |
92 | ;; Delete what is in the buffer and make the index entry | |
93 | (delete-region beg end) | |
94 | (reftex-index def-char full-entry def-tag sel))))) | |
49275d55 | 95 | |
7c4d13cc | 96 | (defun reftex-index (&optional char key tag sel no-insert) |
49275d55 | 97 | "Query for an index macro and insert it along with its arguments. |
1a9461d0 CD |
98 | The index macros available are those defined in `reftex-index-macro' or |
99 | by a call to `reftex-add-index-macros', typically from an AUCTeX style file. | |
100 | RefteX provides completion for the index tag and the index key, and | |
101 | will prompt for other arguments." | |
102 | ||
103 | (interactive) | |
104 | ||
105 | ;; Ensure access to scanning info | |
106 | (reftex-ensure-index-support t) | |
107 | (reftex-access-scan-info current-prefix-arg) | |
108 | ||
109 | ;; Find out which macro we are going to use | |
110 | (let* ((char (or char | |
3666daf6 CD |
111 | (reftex-select-with-char reftex-query-index-macro-prompt |
112 | reftex-query-index-macro-help))) | |
113 | (macro (nth 1 (assoc char reftex-key-to-index-macro-alist))) | |
114 | (entry (or (assoc macro reftex-index-macro-alist) | |
115 | (error "No index macro associated with %c" char))) | |
116 | (ntag (nth 1 entry)) | |
117 | (tag (or tag (nth 1 entry))) | |
118 | (nargs (nth 4 entry)) | |
119 | (nindex (nth 5 entry)) | |
120 | (opt-args (nth 6 entry)) | |
121 | (repeat (nth 7 entry)) | |
122 | opt tag1 value) | |
1a9461d0 CD |
123 | |
124 | ;; Get the supported arguments | |
125 | (if (stringp tag) | |
3666daf6 | 126 | (setq tag1 tag) |
1a9461d0 CD |
127 | (setq tag1 (or (reftex-index-complete-tag tag opt-args) ""))) |
128 | (setq key (or key | |
3666daf6 CD |
129 | (reftex-index-complete-key |
130 | (if (string= tag1 "") "idx" tag1) | |
131 | (member nindex opt-args)))) | |
1a9461d0 CD |
132 | |
133 | ;; Insert the macro and ask for any additional args | |
134 | (insert macro) | |
135 | (loop for i from 1 to nargs do | |
136 | (setq opt (member i opt-args) | |
3666daf6 CD |
137 | value (cond ((= nindex i) key) |
138 | ((equal ntag i) tag1) | |
139 | (t (read-string (concat "Macro arg nr. " | |
140 | (int-to-string i) | |
141 | (if opt " (optional)" "") | |
142 | ": "))))) | |
1a9461d0 | 143 | (unless (and opt (string= value "")) |
3666daf6 | 144 | (insert (if opt "[" "{") value (if opt "]" "}")))) |
7c4d13cc | 145 | (and repeat (stringp sel) (insert sel)) |
1a9461d0 | 146 | (and key reftex-plug-into-AUCTeX (fboundp 'LaTeX-add-index-entries) |
3666daf6 | 147 | (LaTeX-add-index-entries key)) |
1a9461d0 CD |
148 | (reftex-index-update-taglist tag1) |
149 | (reftex-notice-new))) | |
150 | ||
151 | (defun reftex-default-index () | |
152 | (cond ((null reftex-index-default-tag) nil) | |
3666daf6 CD |
153 | ((stringp reftex-index-default-tag) reftex-index-default-tag) |
154 | (t (or (get reftex-docstruct-symbol 'default-index-tag) | |
155 | "idx")))) | |
1a9461d0 CD |
156 | |
157 | (defun reftex-update-default-index (tag &optional tag-list) | |
158 | (if (and (not (equal tag "")) | |
3666daf6 CD |
159 | (stringp tag) |
160 | (eq reftex-index-default-tag 'last) | |
161 | (or (null tag-list) | |
162 | (member tag tag-list))) | |
1a9461d0 CD |
163 | (put reftex-docstruct-symbol 'default-index-tag tag))) |
164 | ||
165 | (defun reftex-index-complete-tag (&optional itag opt-args) | |
166 | ;; Ask the user for a tag, completing on known tags. | |
167 | ;; ITAG is the argument number which contains the tag. | |
168 | ;; OPT-ARGS is a list of optional argument indices, as given by | |
169 | ;; `reftex-parse-args'. | |
170 | (let* ((opt (and (integerp itag) (member itag opt-args))) | |
5b76833f RF |
171 | (index-tags (cdr (assq 'index-tags |
172 | (symbol-value reftex-docstruct-symbol)))) | |
173 | (default (reftex-default-index)) | |
174 | (prompt (concat "Index tag" | |
175 | (if (or opt default) | |
176 | (format " (%s): " | |
177 | (concat | |
178 | (if opt "optional" "") | |
179 | (if default | |
180 | (concat (if opt ", " "") | |
181 | (format "default %s" default)) | |
182 | ""))) | |
183 | ": "))) | |
184 | (tag (completing-read prompt (mapcar 'list index-tags)))) | |
1a9461d0 CD |
185 | (if (and default (equal tag "")) (setq tag default)) |
186 | (reftex-update-default-index tag) | |
187 | tag)) | |
188 | ||
189 | (defun reftex-index-select-tag () | |
190 | ;; Have the user select an index tag. | |
191 | ;; FIXME: should we cache tag-alist, prompt and help? | |
49275d55 | 192 | (let* ((index-tags (cdr (assoc 'index-tags |
3666daf6 CD |
193 | (symbol-value reftex-docstruct-symbol)))) |
194 | (default (reftex-default-index))) | |
49275d55 | 195 | (cond |
1a9461d0 CD |
196 | ((null index-tags) |
197 | (error "No index tags available")) | |
198 | ||
199 | ((= (length index-tags) 1) | |
200 | ;; Just one index, use it | |
201 | (car index-tags)) | |
49275d55 | 202 | |
1a9461d0 CD |
203 | ((> (length index-tags) 1) |
204 | ;; Several indices, ask. | |
205 | (let* ((tags (copy-sequence index-tags)) | |
3666daf6 CD |
206 | (cnt 0) |
207 | tag-alist i val len tag prompt help rpl) | |
208 | ;; Move idx and glo up in the list to ensure ?i and ?g shortcuts | |
209 | (if (member "glo" tags) | |
210 | (setq tags (cons "glo" (delete "glo" tags)))) | |
211 | (if (member "idx" tags) | |
212 | (setq tags (cons "idx" (delete "idx" tags)))) | |
213 | ;; Find unique shortcuts for each index. | |
214 | (while (setq tag (pop tags)) | |
215 | (setq len (length tag) | |
216 | i -1 | |
217 | val nil) | |
218 | (catch 'exit | |
219 | (while (and (< (incf i) len) (null val)) | |
220 | (unless (assq (aref tag i) tag-alist) | |
221 | (push (list (aref tag i) | |
222 | tag | |
49275d55 | 223 | (concat (substring tag 0 i) |
3666daf6 CD |
224 | "[" (substring tag i (incf i)) "]" |
225 | (substring tag i))) | |
226 | tag-alist) | |
227 | (throw 'exit t))) | |
49275d55 | 228 | (push (list (+ ?0 (incf cnt)) tag |
3666daf6 CD |
229 | (concat "[" (int-to-string cnt) "]:" tag)) |
230 | tag-alist))) | |
231 | (setq tag-alist (nreverse tag-alist)) | |
232 | ;; Compute Prompt and Help strings | |
233 | (setq prompt | |
234 | (concat | |
235 | (format "Select Index%s: " | |
236 | (if default (format " (Default <%s>)" default) "")) | |
237 | (mapconcat (lambda(x) (nth 2 x)) tag-alist " "))) | |
238 | (setq help | |
239 | (concat "Select an Index\n===============\n" | |
240 | (if default | |
241 | (format "[^M] %s (the default)\n" default) | |
242 | "") | |
49275d55 | 243 | (mapconcat (lambda(x) |
3666daf6 CD |
244 | (apply 'format "[%c] %s" x)) |
245 | tag-alist "\n"))) | |
246 | ;; Query the user for an index-tag | |
247 | (setq rpl (reftex-select-with-char prompt help 3 t)) | |
248 | (message "") | |
249 | (if (and default (equal rpl ?\C-m)) | |
250 | default | |
251 | (if (assq rpl tag-alist) | |
252 | (progn | |
253 | (reftex-update-default-index (nth 1 (assq rpl tag-alist))) | |
254 | (nth 1 (assq rpl tag-alist))) | |
255 | (error "No index tag associated with %c" rpl))))) | |
1a9461d0 CD |
256 | (t (error "This should not happen (reftex-index-select-tag)"))))) |
257 | ||
258 | (defun reftex-index-complete-key (&optional tag optional initial) | |
259 | ;; Read an index key, with completion. | |
260 | ;; Restrict completion table on index tag TAG. | |
261 | ;; OPTIONAL indicates if the arg is optional. | |
262 | (let* ((table (reftex-sublist-nth | |
3666daf6 CD |
263 | (symbol-value reftex-docstruct-symbol) 6 |
264 | (lambda(x) (and (eq (car x) 'index) | |
265 | (string= (nth 1 x) (or tag "")))) | |
266 | t)) | |
267 | (prompt (concat "Index key" (if optional " (optional)" "") ": ")) | |
268 | (key (completing-read prompt table nil nil initial))) | |
1a9461d0 CD |
269 | key)) |
270 | ||
271 | (defun reftex-index-update-taglist (newtag) | |
49275d55 | 272 | ;; add NEWTAG to the list of available index tags. |
1a9461d0 CD |
273 | (let ((cell (assoc 'index-tags (symbol-value reftex-docstruct-symbol)))) |
274 | (and newtag (cdr cell) (not (member newtag (cdr cell))) | |
3666daf6 | 275 | (push newtag (cdr cell))))) |
1a9461d0 | 276 | |
e5bd0a28 SM |
277 | (define-obsolete-variable-alias |
278 | 'reftex-index-map 'reftex-index-mode-map "24.1") | |
4d789d84 SM |
279 | (defvar reftex-index-mode-map |
280 | (let ((map (make-sparse-keymap))) | |
281 | ;; Index map | |
282 | (define-key map (if (featurep 'xemacs) [(button2)] [(mouse-2)]) | |
283 | 'reftex-index-mouse-goto-line-and-hide) | |
284 | (define-key map [follow-link] 'mouse-face) | |
285 | ||
286 | (substitute-key-definition | |
287 | 'next-line 'reftex-index-next map global-map) | |
288 | (substitute-key-definition | |
289 | 'previous-line 'reftex-index-previous map global-map) | |
290 | ||
291 | (loop for x in | |
292 | '(("n" . reftex-index-next) | |
293 | ("p" . reftex-index-previous) | |
294 | ("?" . reftex-index-show-help) | |
295 | (" " . reftex-index-view-entry) | |
296 | ("\C-m" . reftex-index-goto-entry-and-hide) | |
297 | ("\C-i" . reftex-index-goto-entry) | |
298 | ("\C-k" . reftex-index-kill) | |
299 | ("r" . reftex-index-rescan) | |
300 | ("R" . reftex-index-Rescan) | |
301 | ("g" . revert-buffer) | |
302 | ("q" . reftex-index-quit) | |
303 | ("k" . reftex-index-quit-and-kill) | |
304 | ("f" . reftex-index-toggle-follow) | |
305 | ("s" . reftex-index-switch-index-tag) | |
306 | ("e" . reftex-index-edit) | |
307 | ("^" . reftex-index-level-up) | |
308 | ("_" . reftex-index-level-down) | |
309 | ("}" . reftex-index-restrict-to-section) | |
310 | ("{" . reftex-index-widen) | |
311 | (">" . reftex-index-restriction-forward) | |
312 | ("<" . reftex-index-restriction-backward) | |
313 | ("(" . reftex-index-toggle-range-beginning) | |
314 | (")" . reftex-index-toggle-range-end) | |
315 | ("|" . reftex-index-edit-attribute) | |
316 | ("@" . reftex-index-edit-visual) | |
317 | ("*" . reftex-index-edit-key) | |
318 | ("\C-c=". reftex-index-goto-toc) | |
319 | ("c" . reftex-index-toggle-context)) | |
320 | do (define-key map (car x) (cdr x))) | |
321 | ||
322 | (loop for key across "0123456789" do | |
323 | (define-key map (vector (list key)) 'digit-argument)) | |
324 | (define-key map "-" 'negative-argument) | |
325 | ||
326 | ;; The capital letters and the exclamation mark | |
327 | (loop for key across (concat "!" reftex-index-section-letters) do | |
328 | (define-key map (vector (list key)) | |
329 | (list 'lambda '() '(interactive) | |
330 | (list 'reftex-index-goto-letter key)))) | |
331 | ||
332 | (easy-menu-define reftex-index-menu map | |
333 | "Menu for Index buffer" | |
334 | '("Index" | |
335 | ["Goto section A-Z" | |
336 | (message "To go to a section, just press any of: !%s" | |
337 | reftex-index-section-letters) t] | |
338 | ["Show Entry" reftex-index-view-entry t] | |
339 | ["Go To Entry" reftex-index-goto-entry t] | |
340 | ["Exit & Go To Entry" reftex-index-goto-entry-and-hide t] | |
341 | ["Table of Contents" reftex-index-goto-toc t] | |
342 | ["Quit" reftex-index-quit t] | |
343 | "--" | |
344 | ("Update" | |
345 | ["Rebuilt *Index* Buffer" revert-buffer t] | |
346 | "--" | |
347 | ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans] | |
348 | ["Rescan Entire Document" reftex-index-Rescan t]) | |
349 | ("Restrict" | |
350 | ["Restrict to section" reftex-index-restrict-to-section t] | |
351 | ["Widen" reftex-index-widen reftex-index-restriction-indicator] | |
352 | ["Next Section" reftex-index-restriction-forward | |
353 | reftex-index-restriction-indicator] | |
354 | ["Previous Section" reftex-index-restriction-backward | |
355 | reftex-index-restriction-indicator]) | |
356 | ("Edit" | |
357 | ["Edit Entry" reftex-index-edit t] | |
358 | ["Edit Key" reftex-index-edit-key t] | |
359 | ["Edit Attribute" reftex-index-edit-attribute t] | |
360 | ["Edit Visual" reftex-index-edit-visual t] | |
361 | "--" | |
362 | ["Add Parentkey" reftex-index-level-down t] | |
363 | ["Remove Parentkey " reftex-index-level-up t] | |
364 | "--" | |
365 | ["Make Start-of-Range" reftex-index-toggle-range-beginning t] | |
366 | ["Make End-of-Range" reftex-index-toggle-range-end t] | |
367 | "--" | |
368 | ["Kill Entry" reftex-index-kill nil] | |
369 | "--" | |
370 | ["Undo" reftex-index-undo nil]) | |
371 | ("Options" | |
372 | ["Context" reftex-index-toggle-context :style toggle | |
373 | :selected reftex-index-include-context] | |
374 | "--" | |
375 | ["Follow Mode" reftex-index-toggle-follow :style toggle | |
376 | :selected reftex-index-follow-mode]) | |
377 | "--" | |
378 | ["Help" reftex-index-show-help t])) | |
379 | ||
380 | map) | |
1a9461d0 CD |
381 | "Keymap used for *Index* buffers.") |
382 | ||
383 | (defvar reftex-index-menu) | |
384 | ||
385 | (defvar reftex-last-index-file nil | |
386 | "Stores the file name from which `reftex-display-index' was called.") | |
387 | (defvar reftex-index-tag nil | |
388 | "Stores the tag of the index in an index buffer.") | |
389 | ||
390 | (defvar reftex-index-return-marker (make-marker) | |
391 | "Marker which makes it possible to return from index to old position.") | |
392 | ||
393 | (defvar reftex-index-restriction-indicator nil) | |
394 | (defvar reftex-index-restriction-data nil) | |
395 | ||
4d789d84 | 396 | (define-derived-mode reftex-index-mode fundamental-mode "RefTeX Index" |
1a9461d0 CD |
397 | "Major mode for managing Index buffers for LaTeX files. |
398 | This buffer was created with RefTeX. | |
399 | Press `?' for a summary of important key bindings, or check the menu. | |
400 | ||
401 | Here are all local bindings. | |
402 | ||
4d789d84 | 403 | \\{reftex-index-mode-map}" |
1a9461d0 CD |
404 | (set (make-local-variable 'revert-buffer-function) 'reftex-index-revert) |
405 | (set (make-local-variable 'reftex-index-restriction-data) nil) | |
406 | (set (make-local-variable 'reftex-index-restriction-indicator) nil) | |
407 | (setq mode-line-format | |
3666daf6 CD |
408 | (list "---- " 'mode-line-buffer-identification |
409 | " " 'global-mode-string | |
410 | " R<" 'reftex-index-restriction-indicator ">" | |
411 | " -%-")) | |
1a9461d0 | 412 | (setq truncate-lines t) |
7ac7387b CD |
413 | (when (featurep 'xemacs) |
414 | ;; XEmacs needs the call to make-local-hook | |
415 | (make-local-hook 'post-command-hook) | |
416 | (make-local-hook 'pre-command-hook)) | |
1a9461d0 | 417 | (make-local-variable 'reftex-last-follow-point) |
4d789d84 | 418 | (easy-menu-add reftex-index-menu reftex-index-mode-map) |
1a9461d0 | 419 | (add-hook 'post-command-hook 'reftex-index-post-command-hook nil t) |
4d789d84 | 420 | (add-hook 'pre-command-hook 'reftex-index-pre-command-hook nil t)) |
1a9461d0 CD |
421 | |
422 | (defconst reftex-index-help | |
423 | " AVAILABLE KEYS IN INDEX BUFFER | |
424 | ============================== | |
425 | ! A..Z Goto the section of entries starting with this letter. | |
426 | n / p next-entry / previous-entry | |
427 | SPC / TAB Show/Goto the corresponding entry in the LaTeX document. | |
428 | RET Goto the entry and hide the *Index* window (also on mouse-2). | |
429 | q / k Hide/Kill *Index* buffer. | |
430 | C-c = Switch to the TOC buffer. | |
431 | f / c Toggle follow mode / Toggle display of [c]ontext. | |
432 | g Refresh *Index* buffer. | |
433 | r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document. | |
434 | s Switch to a different index (for documents with multiple indices). | |
435 | e / C-k Edit/Kill the entry. | |
436 | * | @ Edit specific part of entry: [*]key [|]attribute [@]visual | |
437 | With prefix: kill that part. | |
5829a569 | 438 | \( ) Toggle entry's beginning/end of page range property. |
1a9461d0 | 439 | _ ^ Add/Remove parent key (to make this item a subitem). |
1a9461d0 CD |
440 | } / { Restrict Index to a single document section / Widen. |
441 | < / > When restricted, move restriction to previous/next section.") | |
442 | ||
443 | (defun reftex-index-show-entry (data &optional no-revisit) | |
444 | ;; Find an index entry associated with DATA and display it highlighted | |
445 | ;; in another window. NO-REVISIT means we are not allowed to visit | |
446 | ;; files for this. | |
447 | ;; Note: This function just looks for the nearest match of the | |
448 | ;; context string and may fail if the entry moved and an identical | |
449 | ;; entry is close to the old position. Frequent rescans make this | |
49275d55 | 450 | ;; safer. |
1a9461d0 | 451 | (let* ((file (nth 3 data)) |
3666daf6 CD |
452 | (literal (nth 2 data)) |
453 | (pos (nth 4 data)) | |
454 | (re (regexp-quote literal)) | |
455 | (match | |
456 | (cond | |
457 | ((or (not no-revisit) | |
458 | (reftex-get-buffer-visiting file)) | |
459 | (switch-to-buffer-other-window | |
460 | (reftex-get-file-buffer-force file nil)) | |
461 | (goto-char (or pos (point-min))) | |
462 | (or (looking-at re) | |
463 | (reftex-nearest-match re (length literal)))) | |
274f1353 | 464 | (t (message "%s" reftex-no-follow-message) nil)))) |
1a9461d0 CD |
465 | (when match |
466 | (goto-char (match-beginning 0)) | |
467 | (recenter '(4)) | |
468 | (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer))) | |
469 | match)) | |
470 | ||
f3c18bd0 | 471 | (defun reftex-display-index (&optional tag overriding-restriction redo |
3666daf6 | 472 | &rest locations) |
1a9461d0 CD |
473 | "Display a buffer with an index compiled from the current document. |
474 | When the document has multiple indices, first prompts for the correct one. | |
475 | When index support is turned off, offer to turn it on. | |
476 | With one or two `C-u' prefixes, rescan document first. | |
477 | With prefix 2, restrict index to current document section. | |
478 | With prefix 3, restrict index to region." | |
479 | ||
480 | (interactive) | |
481 | ||
482 | ;; Ensure access to scanning info and rescan buffer if prefix are is '(4). | |
483 | (let ((current-prefix-arg current-prefix-arg)) | |
484 | (reftex-ensure-index-support t) | |
485 | (reftex-access-scan-info current-prefix-arg)) | |
486 | ||
487 | (set-marker reftex-index-return-marker (point)) | |
488 | (setq reftex-last-follow-point 1) | |
489 | ||
490 | ;; Determine the correct index to process | |
491 | (let* ((docstruct (symbol-value reftex-docstruct-symbol)) | |
3666daf6 CD |
492 | (docstruct-symbol reftex-docstruct-symbol) |
493 | (index-tag (or tag (reftex-index-select-tag))) | |
494 | (master (reftex-TeX-master-file)) | |
495 | (calling-file (buffer-file-name)) | |
496 | (restriction | |
497 | (or overriding-restriction | |
49275d55 | 498 | (and (not redo) |
3666daf6 CD |
499 | (reftex-get-restriction current-prefix-arg docstruct)))) |
500 | (locations | |
501 | ;; See if we are on an index macro as initial position | |
502 | (or locations | |
503 | (let* ((what-macro (reftex-what-macro-safe 1)) | |
504 | (macro (car what-macro)) | |
505 | (here-I-am (when (member macro reftex-macros-with-index) | |
506 | (save-excursion | |
49275d55 | 507 | (goto-char (+ (cdr what-macro) |
3666daf6 CD |
508 | (length macro))) |
509 | (reftex-move-over-touching-args) | |
510 | (reftex-where-am-I))))) | |
511 | (if (eq (car (car here-I-am)) 'index) | |
512 | (list (car here-I-am)))))) | |
513 | buffer-name) | |
1a9461d0 CD |
514 | |
515 | (setq buffer-name (reftex-make-index-buffer-name index-tag)) | |
516 | ||
517 | ;; Goto the buffer and put it into the correct mode | |
49275d55 | 518 | |
1a9461d0 | 519 | (when (or restriction current-prefix-arg) |
3666daf6 | 520 | (reftex-kill-buffer buffer-name)) |
1a9461d0 CD |
521 | |
522 | (if (get-buffer-window buffer-name) | |
3666daf6 | 523 | (select-window (get-buffer-window buffer-name)) |
d63eb0e7 | 524 | (switch-to-buffer buffer-name)) |
1a9461d0 CD |
525 | |
526 | (or (eq major-mode 'reftex-index-mode) (reftex-index-mode)) | |
527 | ||
528 | ;; If the buffer is currently restricted, empty it to force update. | |
529 | (when reftex-index-restriction-data | |
530 | (reftex-erase-buffer)) | |
531 | (set (make-local-variable 'reftex-last-index-file) calling-file) | |
532 | (set (make-local-variable 'reftex-index-tag) index-tag) | |
533 | (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol) | |
534 | (if restriction | |
3666daf6 CD |
535 | (setq reftex-index-restriction-indicator (car restriction) |
536 | reftex-index-restriction-data (cdr restriction)) | |
f3c18bd0 | 537 | (if (not redo) |
3666daf6 CD |
538 | (setq reftex-index-restriction-indicator nil |
539 | reftex-index-restriction-data nil))) | |
1a9461d0 CD |
540 | (when (= (buffer-size) 0) |
541 | ;; buffer is empty - fill it | |
542 | (message "Building %s buffer..." buffer-name) | |
543 | ||
544 | (setq buffer-read-only nil) | |
545 | (insert (format | |
546 | "INDEX <%s> on %s | |
547 | Restriction: <%s> | |
548 | SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help | |
549 | ------------------------------------------------------------------------------ | |
550 | " index-tag (abbreviate-file-name master) | |
551 | (if (eq (car (car reftex-index-restriction-data)) 'toc) | |
552 | (nth 2 (car reftex-index-restriction-data)) | |
553 | reftex-index-restriction-indicator))) | |
554 | ||
555 | (if (reftex-use-fonts) | |
556 | (put-text-property 1 (point) 'face reftex-index-header-face)) | |
557 | (put-text-property 1 (point) 'intangible t) | |
558 | ||
559 | (reftex-insert-index docstruct index-tag) | |
560 | (goto-char (point-min)) | |
561 | (run-hooks 'reftex-display-copied-context-hook) | |
562 | (message "Building %s buffer...done." buffer-name) | |
563 | (setq buffer-read-only t)) | |
564 | (and locations (apply 'reftex-find-start-point (point) locations)) | |
565 | (if reftex-index-restriction-indicator | |
566 | (message "Index restricted: <%s>" reftex-index-restriction-indicator)))) | |
567 | ||
568 | (defun reftex-insert-index (docstruct tag &optional update-one remark) | |
569 | ;; Insert an index into the current buffer. Entries are from the | |
570 | ;; DOCSTRUCT. | |
571 | ;; TAG is the subindex to process. | |
572 | ;; UPDATE-ONE: When non-nil, delete the entry at point and replace | |
573 | ;; it with whatever the DOCSTRUCT contains. | |
574 | ;; REMARK can be a note to add to the entry. | |
575 | (let* ((all docstruct) | |
3666daf6 CD |
576 | (indent " ") |
577 | (context reftex-index-include-context) | |
578 | (context-indent (concat indent " ")) | |
579 | (section-chars (mapcar 'identity reftex-index-section-letters)) | |
580 | (this-section-char 0) | |
581 | (font (reftex-use-fonts)) | |
582 | (bor (car reftex-index-restriction-data)) | |
583 | (eor (nth 1 reftex-index-restriction-data)) | |
584 | (mouse-face | |
585 | (if (memq reftex-highlight-selection '(mouse both)) | |
586 | reftex-mouse-selected-face | |
587 | nil)) | |
6c27f0f8 | 588 | (index-face reftex-label-face) |
3666daf6 | 589 | sublist cell from to first-char) |
1a9461d0 CD |
590 | |
591 | ;; Make the sublist and sort it | |
592 | (when bor | |
593 | (setq all (or (memq bor all) all))) | |
594 | ||
595 | (while (setq cell (pop all)) | |
596 | (if (eq cell eor) | |
3666daf6 CD |
597 | (setq all nil) |
598 | (and (eq (car cell) 'index) | |
599 | (equal (nth 1 cell) tag) | |
600 | (push cell sublist)))) | |
1a9461d0 | 601 | (setq sublist (sort (nreverse sublist) |
3666daf6 | 602 | (lambda (a b) (string< (nth 8 a) (nth 8 b))))) |
1a9461d0 CD |
603 | |
604 | (when update-one | |
605 | ;; Delete the entry at place | |
606 | (and (bolp) (forward-char 1)) | |
607 | (delete-region (previous-single-property-change (1+ (point)) :data) | |
49275d55 | 608 | (or (next-single-property-change (point) :data) |
3666daf6 | 609 | (point-max)))) |
1a9461d0 CD |
610 | |
611 | ;; Walk through the list and insert all entries | |
612 | (while (setq cell (pop sublist)) | |
613 | (unless update-one | |
3666daf6 CD |
614 | (setq first-char (upcase (string-to-char (nth 6 cell)))) |
615 | (when (and (not (equal first-char this-section-char)) | |
616 | (member first-char section-chars)) | |
617 | ;; There is a new initial letter, so start a new section | |
618 | (reftex-index-insert-new-letter first-char font) | |
619 | (setq section-chars (delete first-char section-chars) | |
620 | this-section-char first-char)) | |
621 | (when (= this-section-char 0) | |
622 | (setq this-section-char ?!) | |
623 | (reftex-index-insert-new-letter this-section-char font))) | |
1a9461d0 CD |
624 | |
625 | (setq from (point)) | |
626 | (insert indent (nth 7 cell)) | |
627 | (when font | |
3666daf6 | 628 | (setq to (point)) |
49275d55 | 629 | (put-text-property |
3666daf6 CD |
630 | (- (point) (length (nth 7 cell))) to |
631 | 'face index-face) | |
632 | (goto-char to)) | |
1a9461d0 CD |
633 | |
634 | (when (or remark (nth 9 cell)) | |
3666daf6 CD |
635 | (and (< (current-column) 40) |
636 | ;; FIXME: maybe this is too slow? | |
637 | (insert (make-string (max (- 40 (current-column)) 0) ?\ ))) | |
638 | (and (nth 9 cell) (insert " " (substring (nth 5 cell) (nth 9 cell)))) | |
639 | (and remark (insert " " remark))) | |
1a9461d0 CD |
640 | |
641 | (insert "\n") | |
642 | (setq to (point)) | |
643 | ||
644 | (when context | |
3666daf6 CD |
645 | (insert context-indent (nth 2 cell) "\n") |
646 | (setq to (point))) | |
1a9461d0 CD |
647 | (put-text-property from to :data cell) |
648 | (when mouse-face | |
3666daf6 CD |
649 | (put-text-property from (1- to) |
650 | 'mouse-face mouse-face)) | |
1a9461d0 CD |
651 | (goto-char to)))) |
652 | ||
653 | ||
654 | (defun reftex-index-insert-new-letter (letter &optional font) | |
655 | ;; Start a new section in the index | |
656 | (let ((from (point))) | |
49275d55 | 657 | (insert "\n" letter letter letter |
3666daf6 | 658 | "-----------------------------------------------------------------") |
1a9461d0 CD |
659 | (when font |
660 | (put-text-property from (point) 'face reftex-index-section-face)) | |
661 | (insert "\n"))) | |
662 | ||
663 | (defun reftex-get-restriction (arg docstruct) | |
c7015153 | 664 | ;; Interpret the prefix ARG and derive index restriction specs. |
1a9461d0 | 665 | (let* ((beg (min (point) (or (condition-case nil (mark) (error nil)) |
3666daf6 CD |
666 | (point-max)))) |
667 | (end (max (point) (or (condition-case nil (mark) (error nil)) | |
668 | (point-min)))) | |
669 | bor eor label here-I-am) | |
1a9461d0 CD |
670 | (cond |
671 | ((eq arg 2) | |
672 | (setq here-I-am (car (reftex-where-am-I)) | |
3666daf6 CD |
673 | bor (if (eq (car here-I-am) 'toc) |
674 | here-I-am | |
675 | (reftex-last-assoc-before-elt | |
676 | 'toc here-I-am docstruct)) | |
677 | eor (car (memq (assq 'toc (cdr (memq bor docstruct))) docstruct)) | |
678 | label (nth 6 bor))) | |
1a9461d0 CD |
679 | ((eq arg 3) |
680 | (save-excursion | |
3666daf6 CD |
681 | (setq label "region") |
682 | (goto-char beg) | |
683 | (setq bor (car (reftex-where-am-I))) | |
684 | (setq bor (nth 1 (memq bor docstruct))) | |
685 | (goto-char end) | |
686 | (setq eor (nth 1 (memq (car (reftex-where-am-I)) docstruct))))) | |
1a9461d0 CD |
687 | (t nil)) |
688 | (if (and label (or bor eor)) | |
3666daf6 | 689 | (list label bor eor) |
1a9461d0 CD |
690 | nil))) |
691 | ||
692 | (defun reftex-index-pre-command-hook () | |
693 | ;; Used as pre command hook in *Index* buffer | |
694 | (reftex-unhighlight 0) | |
695 | (reftex-unhighlight 1)) | |
696 | ||
697 | (defun reftex-index-post-command-hook () | |
698 | ;; Used in the post-command-hook for the *Index* buffer | |
699 | (when (get-text-property (point) :data) | |
700 | (and (> (point) 1) | |
3666daf6 CD |
701 | (not (get-text-property (point) 'intangible)) |
702 | (memq reftex-highlight-selection '(cursor both)) | |
703 | (reftex-highlight 1 | |
704 | (or (previous-single-property-change (1+ (point)) :data) | |
705 | (point-min)) | |
706 | (or (next-single-property-change (point) :data) | |
707 | (point-max))))) | |
1a9461d0 CD |
708 | (if (integerp reftex-index-follow-mode) |
709 | ;; Remove delayed action | |
710 | (setq reftex-index-follow-mode t) | |
711 | (and reftex-index-follow-mode | |
3666daf6 CD |
712 | (not (equal reftex-last-follow-point (point))) |
713 | ;; Show context in other window | |
714 | (setq reftex-last-follow-point (point)) | |
715 | (condition-case nil | |
716 | (reftex-index-visit-location nil (not reftex-revisit-to-follow)) | |
717 | (error t))))) | |
1a9461d0 CD |
718 | |
719 | (defun reftex-index-show-help () | |
720 | "Show a summary of special key bindings." | |
721 | (interactive) | |
722 | (with-output-to-temp-buffer "*RefTeX Help*" | |
723 | (princ reftex-index-help)) | |
724 | (reftex-enlarge-to-fit "*RefTeX Help*" t) | |
725 | ;; If follow mode is active, arrange to delay it one command | |
726 | (if reftex-index-follow-mode | |
727 | (setq reftex-index-follow-mode 1))) | |
728 | ||
729 | (defun reftex-index-next (&optional arg) | |
730 | "Move to next selectable item." | |
731 | (interactive "p") | |
732 | (setq reftex-callback-fwd t) | |
733 | (or (eobp) (forward-char 1)) | |
49275d55 | 734 | (goto-char (or (next-single-property-change (point) :data) |
3666daf6 | 735 | (point))) |
1a9461d0 | 736 | (unless (get-text-property (point) :data) |
49275d55 | 737 | (goto-char (or (next-single-property-change (point) :data) |
3666daf6 | 738 | (point))))) |
1a9461d0 CD |
739 | (defun reftex-index-previous (&optional arg) |
740 | "Move to previous selectable item." | |
741 | (interactive "p") | |
742 | (setq reftex-callback-fwd nil) | |
743 | (goto-char (or (previous-single-property-change (point) :data) | |
3666daf6 | 744 | (point))) |
1a9461d0 CD |
745 | (unless (get-text-property (point) :data) |
746 | (goto-char (or (previous-single-property-change (point) :data) | |
3666daf6 | 747 | (point))))) |
1a9461d0 CD |
748 | (defun reftex-index-toggle-follow () |
749 | "Toggle follow (other window follows with context)." | |
750 | (interactive) | |
751 | (setq reftex-last-follow-point -1) | |
752 | (setq reftex-index-follow-mode (not reftex-index-follow-mode))) | |
753 | (defun reftex-index-toggle-context () | |
754 | "Toggle inclusion of label context in *Index* buffer. | |
755 | Label context is only displayed when the labels are there as well." | |
756 | (interactive) | |
757 | (setq reftex-index-include-context (not reftex-index-include-context)) | |
758 | (reftex-index-revert)) | |
759 | (defun reftex-index-view-entry () | |
760 | "View document location in other window." | |
761 | (interactive) | |
762 | (reftex-index-visit-location)) | |
763 | (defun reftex-index-goto-entry-and-hide () | |
764 | "Go to document location in other window. Hide the *Index* window." | |
765 | (interactive) | |
766 | (reftex-index-visit-location 'hide)) | |
767 | (defun reftex-index-goto-entry () | |
768 | "Go to document location in other window. *Index* window stays." | |
769 | (interactive) | |
770 | (reftex-index-visit-location t)) | |
771 | (defun reftex-index-mouse-goto-line-and-hide (ev) | |
772 | "Go to document location in other window. Hide the *Index* window." | |
773 | (interactive "e") | |
774 | (mouse-set-point ev) | |
775 | (reftex-index-visit-location 'hide)) | |
776 | (defun reftex-index-quit () | |
777 | "Hide the *Index* window and do not move point." | |
778 | (interactive) | |
779 | (or (one-window-p) (delete-window)) | |
780 | (switch-to-buffer (marker-buffer reftex-index-return-marker)) | |
781 | (goto-char (or (marker-position reftex-index-return-marker) (point)))) | |
782 | (defun reftex-index-quit-and-kill () | |
783 | "Kill the *Index* buffer." | |
784 | (interactive) | |
785 | (kill-buffer (current-buffer)) | |
786 | (or (one-window-p) (delete-window)) | |
787 | (switch-to-buffer (marker-buffer reftex-index-return-marker)) | |
788 | (goto-char (or (marker-position reftex-index-return-marker) (point)))) | |
789 | (defun reftex-index-goto-toc (&rest ignore) | |
790 | "Switch to the table of contents of the current document. | |
791 | The function will go to the section where the entry at point was defined." | |
792 | (interactive) | |
793 | (if (get-text-property (point) :data) | |
794 | (reftex-index-goto-entry) | |
795 | (switch-to-buffer (marker-buffer reftex-index-return-marker))) | |
796 | (delete-other-windows) | |
797 | (reftex-toc)) | |
798 | (defun reftex-index-rescan (&rest ignore) | |
799 | "Regenerate the *Index* buffer after reparsing file of section at point." | |
800 | (interactive) | |
801 | (let ((index-tag reftex-index-tag)) | |
802 | (if (and reftex-enable-partial-scans | |
3666daf6 CD |
803 | (null current-prefix-arg)) |
804 | (let* ((data (get-text-property (point) :data)) | |
805 | (file (nth 3 data)) | |
806 | (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) | |
807 | (if (not file) | |
808 | (error "Don't know which file to rescan. Try `C-u r'") | |
809 | (switch-to-buffer (reftex-get-file-buffer-force file)) | |
810 | (setq current-prefix-arg '(4)) | |
f3c18bd0 | 811 | (reftex-display-index index-tag nil 'redo line))) |
1a9461d0 CD |
812 | (reftex-index-Rescan)) |
813 | (reftex-kill-temporary-buffers))) | |
814 | (defun reftex-index-Rescan (&rest ignore) | |
815 | "Regenerate the *Index* buffer after reparsing the entire document." | |
816 | (interactive) | |
817 | (let ((index-tag reftex-index-tag) | |
3666daf6 | 818 | (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) |
1a9461d0 CD |
819 | (switch-to-buffer |
820 | (reftex-get-file-buffer-force reftex-last-index-file)) | |
821 | (setq current-prefix-arg '(16)) | |
f3c18bd0 | 822 | (reftex-display-index index-tag nil 'redo line))) |
1a9461d0 CD |
823 | (defun reftex-index-revert (&rest ignore) |
824 | "Regenerate the *Index* from the internal lists. No reparsing os done." | |
825 | (interactive) | |
826 | (let ((buf (current-buffer)) | |
3666daf6 CD |
827 | (index-tag reftex-index-tag) |
828 | (data (get-text-property (point) :data)) | |
829 | (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) | |
1a9461d0 CD |
830 | (switch-to-buffer |
831 | (reftex-get-file-buffer-force reftex-last-index-file)) | |
832 | (reftex-erase-buffer buf) | |
833 | (setq current-prefix-arg nil | |
3666daf6 | 834 | reftex-last-follow-point 1) |
f3c18bd0 | 835 | (reftex-display-index index-tag nil 'redo data line))) |
1a9461d0 CD |
836 | (defun reftex-index-switch-index-tag (&rest ignore) |
837 | "Switch to a different index of the same document." | |
838 | (interactive) | |
839 | (switch-to-buffer | |
840 | (reftex-get-file-buffer-force reftex-last-index-file)) | |
841 | (setq current-prefix-arg nil) | |
f3c18bd0 | 842 | (reftex-display-index nil nil 'redo)) |
1a9461d0 CD |
843 | |
844 | (defun reftex-index-restrict-to-section (&optional force) | |
845 | "Restrict index to entries defined in same document sect. as entry at point." | |
846 | ;; Optional FORCE means, even if point is not on an index entry. | |
847 | (interactive) | |
848 | (let* ((data (get-text-property (point) :data)) | |
3666daf6 CD |
849 | (docstruct (symbol-value reftex-docstruct-symbol)) |
850 | bor eor) | |
1a9461d0 | 851 | (if (and (not data) force) |
3666daf6 | 852 | (setq data (assq 'toc docstruct))) |
1a9461d0 CD |
853 | (when data |
854 | (setq bor (reftex-last-assoc-before-elt 'toc data docstruct) | |
3666daf6 CD |
855 | eor (car (memq (assq 'toc (cdr (memq bor docstruct))) |
856 | docstruct)) | |
857 | reftex-index-restriction-data (list bor eor) | |
858 | reftex-index-restriction-indicator (nth 6 bor) ))) | |
1a9461d0 CD |
859 | (reftex-index-revert)) |
860 | ||
861 | (defun reftex-index-widen (&rest ignore) | |
862 | "Show the unrestricted index (all entries)." | |
863 | (interactive) | |
864 | (setq reftex-index-restriction-indicator nil | |
3666daf6 | 865 | reftex-index-restriction-data nil) |
1a9461d0 CD |
866 | (reftex-index-revert) |
867 | (message "Index widened")) | |
868 | (defun reftex-index-restriction-forward (&rest ignore) | |
869 | "Restrict to previous section. | |
870 | When index is currently unrestricted, restrict it to a section. | |
871 | When index is restricted, select the next section as restriction criterion." | |
872 | (interactive) | |
873 | (let* ((docstruct (symbol-value reftex-docstruct-symbol)) | |
3666daf6 | 874 | (bor (nth 1 reftex-index-restriction-data))) |
1a9461d0 | 875 | (if (or (not bor) |
3666daf6 CD |
876 | (not (eq (car bor) 'toc))) |
877 | (reftex-index-restrict-to-section t) | |
1a9461d0 | 878 | (setq reftex-index-restriction-indicator (nth 6 bor) |
3666daf6 | 879 | reftex-index-restriction-data |
49275d55 | 880 | (list bor |
3666daf6 CD |
881 | (car (memq (assq 'toc (cdr (memq bor docstruct))) |
882 | docstruct)))) | |
1a9461d0 CD |
883 | (reftex-index-revert)))) |
884 | (defun reftex-index-restriction-backward (&rest ignore) | |
885 | "Restrict to next section. | |
886 | When index is currently unrestricted, restrict it to a section. | |
887 | When index is restricted, select the previous section as restriction criterion." | |
888 | (interactive) | |
889 | (let* ((docstruct (symbol-value reftex-docstruct-symbol)) | |
3666daf6 CD |
890 | (eor (car reftex-index-restriction-data)) |
891 | (bor (reftex-last-assoc-before-elt 'toc eor docstruct t))) | |
1a9461d0 | 892 | (if (or (not bor) |
3666daf6 CD |
893 | (not (eq (car bor) 'toc))) |
894 | (reftex-index-restrict-to-section t) | |
1a9461d0 | 895 | (setq reftex-index-restriction-indicator (nth 6 bor) |
3666daf6 CD |
896 | reftex-index-restriction-data |
897 | (list bor eor)) | |
1a9461d0 CD |
898 | (reftex-index-revert)))) |
899 | ||
900 | (defun reftex-index-visit-location (&optional final no-revisit) | |
901 | ;; Visit the tex file corresponding to the index entry on the current line. | |
902 | ;; If FINAL is t, stay there | |
903 | ;; If FINAL is 'hide, hide the *Index* window. | |
904 | ;; Otherwise, move cursor back into *Index* window. | |
905 | ;; NO-REVISIT means don't visit files, just use live biffers. | |
906 | ||
907 | (let* ((data (get-text-property (point) :data)) | |
908 | (index-window (selected-window)) | |
909 | show-window show-buffer match) | |
910 | ||
911 | (unless data (error "Don't know which index entry to visit")) | |
49275d55 | 912 | |
1a9461d0 | 913 | (if (eq (car data) 'index) |
3666daf6 | 914 | (setq match (reftex-index-show-entry data no-revisit))) |
1a9461d0 CD |
915 | |
916 | (setq show-window (selected-window) | |
917 | show-buffer (current-buffer)) | |
918 | ||
919 | (unless match | |
920 | (select-window index-window) | |
921 | (error "Cannot find location")) | |
922 | ||
923 | (select-window index-window) | |
924 | ||
925 | ;; Use the `final' parameter to decide what to do next | |
926 | (cond | |
927 | ((eq final t) | |
928 | (reftex-unhighlight 0) | |
929 | (select-window show-window)) | |
930 | ((eq final 'hide) | |
931 | (reftex-unhighlight 0) | |
932 | (or (one-window-p) (delete-window)) | |
933 | (switch-to-buffer show-buffer)) | |
934 | (t nil)))) | |
935 | ||
936 | (defun reftex-index-analyze-entry (data) | |
937 | ;; This splits the index context so that key, attribute and visual | |
938 | ;; values are accessible individually. | |
939 | (interactive) | |
940 | (let* ((arg (nth 5 data)) | |
3666daf6 CD |
941 | (context (nth 2 data)) |
942 | (sc reftex-index-special-chars) | |
943 | (boa (if (string-match (regexp-quote (concat "{" arg "}")) context) | |
944 | (1+ (match-beginning 0)) | |
945 | (error "Something is wrong here"))) | |
946 | (eoa (1- (match-end 0))) | |
947 | (boactual (if (string-match (concat "[^" (nth 3 sc) "]" (nth 2 sc)) | |
948 | context boa) | |
949 | (1+ (match-beginning 0)) | |
950 | eoa)) | |
951 | (boattr (if (string-match (concat "[^" (nth 3 sc) "]" (nth 1 sc)) | |
952 | context boa) | |
953 | (1+ (match-beginning 0)) | |
954 | boactual)) | |
955 | (pre (substring context 0 boa)) | |
956 | (key (substring context boa boattr)) | |
957 | (attr (substring context boattr boactual)) | |
958 | (actual (substring context boactual eoa)) | |
959 | (post (substring context eoa))) | |
1a9461d0 CD |
960 | (list pre key attr actual post))) |
961 | ||
1a9461d0 CD |
962 | (defun reftex-index-edit () |
963 | "Edit the index entry at point." | |
964 | (interactive) | |
965 | (let* ((data (get-text-property (point) :data)) | |
3666daf6 | 966 | old new) |
1a9461d0 CD |
967 | (unless data (error "Don't know which index entry to edit")) |
968 | (reftex-index-view-entry) | |
969 | (setq old (nth 2 data) new (read-string "Edit: " old)) | |
970 | (reftex-index-change-entry new))) | |
971 | ||
972 | (defun reftex-index-toggle-range-beginning () | |
973 | "Toggle the page range start attribute `|('." | |
974 | (interactive) | |
975 | (let* ((data (get-text-property (point) :data)) | |
3666daf6 CD |
976 | (bor (concat (nth 1 reftex-index-special-chars) "(")) |
977 | new analyze attr) | |
1a9461d0 CD |
978 | (unless data (error "Don't know which index entry to edit")) |
979 | (setq analyze (reftex-index-analyze-entry data) | |
3666daf6 | 980 | attr (nth 2 analyze)) |
1a9461d0 CD |
981 | (setf (nth 2 analyze) (if (string= attr bor) "" bor)) |
982 | (setq new (apply 'concat analyze)) | |
49275d55 | 983 | (reftex-index-change-entry |
1a9461d0 | 984 | new (if (string= (nth 2 analyze) bor) |
3666daf6 CD |
985 | "Entry is now START-OF-PAGE-RANGE" |
986 | "START-OF-PAGE-RANGE canceled")))) | |
1a9461d0 CD |
987 | |
988 | (defun reftex-index-toggle-range-end () | |
989 | "Toggle the page-range-end attribute `|)'." | |
990 | (interactive) | |
991 | (let* ((data (get-text-property (point) :data)) | |
3666daf6 CD |
992 | (eor (concat (nth 1 reftex-index-special-chars) ")")) |
993 | new analyze attr) | |
1a9461d0 CD |
994 | (unless data (error "Don't know which index entry to edit")) |
995 | (setq analyze (reftex-index-analyze-entry data) | |
3666daf6 | 996 | attr (nth 2 analyze)) |
1a9461d0 CD |
997 | (setf (nth 2 analyze) (if (string= attr eor) "" eor)) |
998 | (setq new (apply 'concat analyze)) | |
999 | (reftex-index-change-entry | |
1000 | new (if (string= (nth 2 analyze) eor) | |
3666daf6 CD |
1001 | "Entry is now END-OF-PAGE-RANGE" |
1002 | "END-OF-PAGE-RANGE canceled")))) | |
1a9461d0 CD |
1003 | |
1004 | (defun reftex-index-edit-key () | |
1005 | "Edit the KEY part of the index entry." | |
1006 | (interactive) | |
1007 | (reftex-index-edit-part nil 1 "" "Key: " t)) | |
1008 | ||
1009 | (defun reftex-index-edit-attribute (&optional arg) | |
1010 | "EDIT the ATTRIBUTE part of the entry. With arg: remove entire ATTRIBUTE." | |
1011 | (interactive "P") | |
1012 | (reftex-index-edit-part arg 2 (nth 1 reftex-index-special-chars) | |
3666daf6 | 1013 | "Attribute: ")) |
1a9461d0 CD |
1014 | |
1015 | (defun reftex-index-edit-visual (&optional arg) | |
1016 | "EDIT the VISUAL part of the entry. With arg: remove entire VISUAL string." | |
1017 | (interactive "P") | |
1018 | (reftex-index-edit-part arg 3 (nth 2 reftex-index-special-chars) "Visual: ")) | |
1019 | ||
1020 | (defun reftex-index-edit-part (arg n initial prompt &optional dont-allow-empty) | |
1021 | ;; This function does the work for all partial editing commands | |
1022 | (let* ((data (get-text-property (point) :data)) | |
3666daf6 | 1023 | new analyze opart npart) |
1a9461d0 CD |
1024 | (unless data (error "Don't know which index entry to edit")) |
1025 | ;; Analyze the whole context string | |
1026 | (setq analyze (reftex-index-analyze-entry data) | |
3666daf6 | 1027 | opart (nth n analyze)) |
1a9461d0 CD |
1028 | (and (> (length opart) 0) (setq opart (substring opart 1))) |
1029 | ;; Have the user editing the part | |
1030 | (setq npart (if arg "" (read-string (concat prompt initial) opart))) | |
1031 | ;; Tests: | |
1032 | (cond ((string= npart opart) | |
3666daf6 CD |
1033 | (error "Not changed")) |
1034 | ((string= npart "") | |
1035 | (if dont-allow-empty | |
5181ff9f | 1036 | (error "Invalid value") |
3666daf6 CD |
1037 | (setf (nth n analyze) npart))) |
1038 | (t (setf (nth n analyze) (concat initial npart)))) | |
1a9461d0 CD |
1039 | (setq new (apply 'concat analyze)) |
1040 | ;; Change the entry and insert the changed version into the index. | |
49275d55 | 1041 | (reftex-index-change-entry |
1a9461d0 | 1042 | new (if (string= npart "") |
3666daf6 CD |
1043 | (format "Deleted: %s" opart) |
1044 | (format "New value is: %s" npart))))) | |
1a9461d0 CD |
1045 | |
1046 | (defun reftex-index-level-down () | |
1047 | "Make index entry a subitem of another entry." | |
1048 | (interactive) | |
1049 | (let* ((data (get-text-property (point) :data)) | |
3666daf6 CD |
1050 | (docstruct (symbol-value reftex-docstruct-symbol)) |
1051 | old new prefix key) | |
1a9461d0 CD |
1052 | (unless data (error "Don't know which index entry to change")) |
1053 | (setq old (nth 2 data) | |
3666daf6 | 1054 | key (nth 6 data) |
49275d55 JB |
1055 | prefix (completing-read |
1056 | "Prefix: " | |
1057 | (reftex-sublist-nth | |
3666daf6 CD |
1058 | docstruct 6 |
1059 | (lambda (x) | |
1060 | (and (eq (car x) 'index) | |
1061 | (string= (nth 1 x) reftex-index-tag))) t))) | |
49275d55 | 1062 | (unless (string-match |
3666daf6 CD |
1063 | (concat (regexp-quote (car reftex-index-special-chars)) "\\'") |
1064 | prefix) | |
1a9461d0 CD |
1065 | (setq prefix (concat prefix (car reftex-index-special-chars)))) |
1066 | (if (string-match (regexp-quote key) old) | |
3666daf6 | 1067 | (setq new (replace-match (concat prefix key) t t old)) |
1a9461d0 CD |
1068 | (error "Cannot construct new index key")) |
1069 | (reftex-index-change-entry new (format "Added prefix: %s" prefix)))) | |
1070 | ||
1071 | (defun reftex-index-level-up () | |
1072 | "Remove the highest level of a hierarchical index entry." | |
1073 | (interactive) | |
1074 | (let* ((data (get-text-property (point) :data)) | |
3666daf6 | 1075 | old new prefix) |
1a9461d0 CD |
1076 | (unless data (error "Don't know which entry to change")) |
1077 | (setq old (nth 2 data)) | |
1078 | (if (string-match (concat "{\\([^" (nth 0 reftex-index-special-chars) "]*" | |
3666daf6 CD |
1079 | "[^" (nth 3 reftex-index-special-chars) "]" |
1080 | (regexp-quote (nth 0 reftex-index-special-chars)) | |
1081 | "\\)") | |
1082 | old) | |
1083 | (setq prefix (substring old (match-beginning 1) (match-end 1)) | |
1084 | new (concat (substring old 0 (match-beginning 1)) | |
1085 | (substring old (match-end 1)))) | |
1a9461d0 CD |
1086 | (error "Entry is not a subitem")) |
1087 | (reftex-index-change-entry new (format "Removed prefix: %s" prefix)))) | |
1088 | ||
1089 | (defun reftex-index-kill () | |
1090 | "FIXME: Not yet implemented" | |
1091 | (interactive) | |
1092 | (error "This function is currently not implemented")) | |
1093 | ||
1094 | (defun reftex-index-undo () | |
1095 | "FIXME: Not yet implemented" | |
1096 | (interactive) | |
1097 | (error "This function is currently not implemented")) | |
1098 | ||
1099 | (defun reftex-index-change-entry (new &optional message) | |
1100 | ;; Change the full context string of the index entry at point to | |
1101 | ;; NEW. This actually edits the buffer where the entry is defined. | |
49275d55 | 1102 | |
1a9461d0 | 1103 | (let* ((data (get-text-property (point) :data)) |
3666daf6 | 1104 | old beg end info) |
1a9461d0 CD |
1105 | (unless data (error "Cannot change entry")) |
1106 | (reftex-index-view-entry) | |
1107 | (setq beg (match-beginning 0) end (match-end 0)) | |
1108 | (setq old (nth 2 data)) | |
1109 | (and (equal old new) (error "Entry unchanged")) | |
9a529312 | 1110 | (with-current-buffer (get-file-buffer (nth 3 data)) |
1a9461d0 CD |
1111 | (goto-char beg) |
1112 | (unless (looking-at (regexp-quote old)) | |
3666daf6 | 1113 | (error "This should not happen (reftex-index-change-entry)")) |
1a9461d0 CD |
1114 | (delete-region beg end) |
1115 | (insert new) | |
1116 | (goto-char (1- beg)) | |
1117 | (when (and (re-search-forward (reftex-everything-regexp) nil t) | |
3666daf6 CD |
1118 | (match-end 10) |
1119 | (< (abs (- (match-beginning 10) beg)) (length new)) | |
1120 | (setq info (reftex-index-info-safe buffer-file-name))) | |
1121 | (setcdr data (cdr info)))) | |
1a9461d0 CD |
1122 | (let ((buffer-read-only nil)) |
1123 | (save-excursion | |
3666daf6 CD |
1124 | (reftex-insert-index (list data) reftex-index-tag t |
1125 | "EDITED"))) | |
1a9461d0 | 1126 | (setq reftex-last-follow-point 1) |
5673af85 | 1127 | (and message (message "%s" message)))) |
1a9461d0 | 1128 | |
1a9461d0 CD |
1129 | (defun reftex-index-goto-letter (char) |
1130 | "Go to the CHAR section in the index." | |
1131 | (let ((pos (point)) | |
3666daf6 | 1132 | (case-fold-search nil)) |
e6ce8c42 GM |
1133 | (goto-char (point-min)) |
1134 | (forward-line 2) | |
1a9461d0 | 1135 | (if (re-search-forward (concat "^" (char-to-string char)) nil t) |
3666daf6 CD |
1136 | (progn |
1137 | (beginning-of-line) | |
1138 | (recenter 0) | |
1139 | (reftex-index-next)) | |
1a9461d0 | 1140 | (goto-char pos) |
7c4d13cc | 1141 | (if (eq char ?!) |
3666daf6 CD |
1142 | (error "This <%s> index does not contain entries sorted before the letters" |
1143 | reftex-index-tag) | |
49275d55 | 1144 | (error "This <%s> index does not contain entries starting with `%c'" |
3666daf6 | 1145 | reftex-index-tag char))))) |
1a9461d0 | 1146 | |
7c4d13cc CD |
1147 | |
1148 | ;;---------------------------------------------------------------------- | |
1149 | ;; The Index Phrases File | |
1150 | ||
1151 | ;; Some constants and variables | |
1152 | (defconst reftex-index-phrases-comment-regexp "^[ \t]*%.*" | |
1153 | "Regular expression to match comment lines in phrases buffer") | |
1154 | (defconst reftex-index-phrases-macrodef-regexp | |
1155 | "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\( *\t[ \t]*\\)\\([^\t]*[^ \t]\\)\\( *\t[ \t]*\\)\\(\\S-+\\)" | |
1156 | "Regular expression to match macro definition lines the phrases buffer.") | |
1157 | ;(defconst reftex-index-phrases-macrodef-regexp | |
1158 | ; "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\([ \t]*\\)\\([^\t]*[^ \t]\\)\\([ \t]*\\)\\(nil\\|t\\)[ \t]*$" | |
1159 | ; "Regular expression to match macro definition lines the phrases buffer. | |
1160 | ;This version would allow just spaces as separators.") | |
1161 | (defconst reftex-index-phrases-phrase-regexp1 | |
1162 | "^\\(\\S-?\\)\\(\t\\)\\([^\t\n]*\\S-\\)\\([ \t]*\\)$" | |
1163 | "Regular expression matching phrases which have no separate index key.") | |
1164 | (defconst reftex-index-phrases-phrase-regexp2 | |
1165 | "^\\(\\S-?\\)\\(\t\\)\\([^\t]*\\S-\\)\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)[ \t]*$" | |
1166 | "Regular expression matching phrases which have a separate index key.") | |
1167 | (defconst reftex-index-phrases-phrase-regexp12 | |
1168 | "^\\(\\S-?\\)\\(\t\\)\\([^\n\t]*\\S-\\)\\(\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)\\)?[ \t]*$" | |
1169 | "Regular expression matching all types of phrase lines.") | |
1170 | (defvar reftex-index-phrases-macro-data nil | |
1171 | "Alist containing the information taken from the macro definition lines. | |
1172 | This gets refreshed in every phrases command.") | |
1173 | (defvar reftex-index-phrases-files nil | |
1174 | "List of document files relevant for the phrases file.") | |
1175 | ||
1176 | (defvar reftex-index-phrases-font-lock-keywords nil | |
1177 | "Font lock keywords for reftex-index-phrases-mode.") | |
1178 | (defvar reftex-index-phrases-font-lock-defaults nil | |
1179 | "Font lock defaults for reftex-index-phrases-mode.") | |
e5bd0a28 SM |
1180 | (define-obsolete-variable-alias |
1181 | 'reftex-index-phrases-map 'reftex-index-phrases-mode-map "24.1") | |
4d789d84 SM |
1182 | (defvar reftex-index-phrases-mode-map |
1183 | (let ((map (make-sparse-keymap))) | |
1184 | ;; Keybindings and Menu for phrases buffer | |
1185 | (loop for x in | |
1186 | '(("\C-c\C-c" . reftex-index-phrases-save-and-return) | |
1187 | ("\C-c\C-x" . reftex-index-this-phrase) | |
1188 | ("\C-c\C-f" . reftex-index-next-phrase) | |
1189 | ("\C-c\C-r" . reftex-index-region-phrases) | |
1190 | ("\C-c\C-a" . reftex-index-all-phrases) | |
1191 | ("\C-c\C-d" . reftex-index-remaining-phrases) | |
1192 | ("\C-c\C-s" . reftex-index-sort-phrases) | |
1193 | ("\C-c\C-n" . reftex-index-new-phrase) | |
1194 | ("\C-c\C-m" . reftex-index-phrases-set-macro-key) | |
1195 | ("\C-c\C-i" . reftex-index-phrases-info) | |
1196 | ("\C-c\C-t" . reftex-index-find-next-conflict-phrase) | |
1197 | ("\C-i" . self-insert-command)) | |
1198 | do (define-key map (car x) (cdr x))) | |
1199 | ||
1200 | (easy-menu-define reftex-index-phrases-menu map | |
1201 | "Menu for Phrases buffer" | |
1202 | '("Phrases" | |
1203 | ["New Phrase" reftex-index-new-phrase t] | |
1204 | ["Set Phrase Macro" reftex-index-phrases-set-macro-key t] | |
1205 | ["Recreate File Header" reftex-index-initialize-phrases-buffer t] | |
1206 | "--" | |
1207 | ("Sort Phrases" | |
1208 | ["Sort" reftex-index-sort-phrases t] | |
1209 | "--" | |
1210 | "Sort Options" | |
1211 | ["by Search Phrase" (setq reftex-index-phrases-sort-prefers-entry nil) | |
1212 | :style radio :selected (not reftex-index-phrases-sort-prefers-entry)] | |
1213 | ["by Index Entry" (setq reftex-index-phrases-sort-prefers-entry t) | |
1214 | :style radio :selected reftex-index-phrases-sort-prefers-entry] | |
1215 | ["in Blocks" (setq reftex-index-phrases-sort-in-blocks | |
1216 | (not reftex-index-phrases-sort-in-blocks)) | |
1217 | :style toggle :selected reftex-index-phrases-sort-in-blocks]) | |
1218 | ["Describe Phrase" reftex-index-phrases-info t] | |
1219 | ["Next Phrase Conflict" reftex-index-find-next-conflict-phrase t] | |
1220 | "--" | |
1221 | ("Find and Index in Document" | |
1222 | ["Current Phrase" reftex-index-this-phrase t] | |
1223 | ["Next Phrase" reftex-index-next-phrase t] | |
1224 | ["Current and Following" reftex-index-remaining-phrases t] | |
1225 | ["Region Phrases" reftex-index-region-phrases t] | |
1226 | ["All Phrases" reftex-index-all-phrases t] | |
1227 | "--" | |
1228 | "Options" | |
1229 | ["Match Whole Words" (setq reftex-index-phrases-search-whole-words | |
1230 | (not reftex-index-phrases-search-whole-words)) | |
1231 | :style toggle :selected reftex-index-phrases-search-whole-words] | |
1232 | ["Case Sensitive Search" (setq reftex-index-phrases-case-fold-search | |
1233 | (not reftex-index-phrases-case-fold-search)) | |
1234 | :style toggle :selected (not | |
1235 | reftex-index-phrases-case-fold-search)] | |
1236 | ["Wrap Long Lines" (setq reftex-index-phrases-wrap-long-lines | |
1237 | (not reftex-index-phrases-wrap-long-lines)) | |
1238 | :style toggle :selected reftex-index-phrases-wrap-long-lines] | |
1239 | ["Skip Indexed Matches" (setq reftex-index-phrases-skip-indexed-matches | |
1240 | (not reftex-index-phrases-skip-indexed-matches)) | |
1241 | :style toggle :selected reftex-index-phrases-skip-indexed-matches]) | |
1242 | "--" | |
1243 | ["Save and Return" reftex-index-phrases-save-and-return t])) | |
1244 | ||
1245 | map) | |
7c4d13cc CD |
1246 | "Keymap used for *toc* buffer.") |
1247 | ||
1248 | ||
1249 | (defun reftex-index-phrase-selection-or-word (arg) | |
1250 | "Add current selection or word at point to the phrases buffer. | |
1251 | When you are in transient-mark-mode and the region is active, the | |
1252 | selection will be used - otherwise the word at point. | |
1253 | You get a chance to edit the entry in the phrases buffer - finish with | |
1254 | `C-c C-c'." | |
1255 | (interactive "P") | |
1256 | (set-marker reftex-index-return-marker (point)) | |
1257 | (reftex-index-selection-or-word arg 'phrase) | |
1258 | (if (eq major-mode 'reftex-index-phrases-mode) | |
49275d55 | 1259 | (message "%s" |
7c4d13cc | 1260 | (substitute-command-keys |
3666daf6 | 1261 | "Return to LaTeX with \\[reftex-index-phrases-save-and-return]")))) |
7c4d13cc CD |
1262 | |
1263 | (defun reftex-index-visit-phrases-buffer () | |
1264 | "Switch to the phrases buffer, initialize if empty." | |
1265 | (interactive) | |
1266 | (reftex-access-scan-info) | |
1267 | (let* ((master (reftex-TeX-master-file)) | |
3666daf6 CD |
1268 | (name (concat (file-name-sans-extension master) |
1269 | reftex-index-phrase-file-extension))) | |
7c4d13cc CD |
1270 | (find-file name) |
1271 | (unless (eq major-mode 'reftex-index-phrases-mode) | |
1272 | (reftex-index-phrases-mode)) | |
1273 | (if (= (buffer-size) 0) | |
3666daf6 | 1274 | (reftex-index-initialize-phrases-buffer master)))) |
7c4d13cc CD |
1275 | |
1276 | (defun reftex-index-initialize-phrases-buffer (&optional master) | |
1277 | "Initialize the phrases buffer by creating the header. | |
1278 | If the buffer is non-empty, delete the old header first." | |
1279 | (interactive) | |
1280 | (let* ((case-fold-search t) | |
3666daf6 CD |
1281 | (default-key (car reftex-index-default-macro)) |
1282 | (default-macro (nth 1 (assoc default-key | |
1283 | reftex-key-to-index-macro-alist))) | |
1284 | (macro-alist | |
1285 | (sort (copy-sequence reftex-index-macro-alist) | |
1286 | (lambda (a b) (equal (car a) default-macro)))) | |
1287 | macro entry key repeat) | |
49275d55 | 1288 | |
7c4d13cc | 1289 | (if master (set (make-local-variable 'TeX-master) |
3666daf6 | 1290 | (file-name-nondirectory master))) |
7c4d13cc CD |
1291 | |
1292 | (when (> (buffer-size) 0) | |
1293 | (goto-char 1) | |
1294 | (set-mark (point)) | |
1295 | (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t) | |
3666daf6 | 1296 | (end-of-line)) |
7c4d13cc CD |
1297 | (beginning-of-line 2) |
1298 | (if (looking-at reftex-index-phrases-comment-regexp) | |
3666daf6 | 1299 | (beginning-of-line 2)) |
7c4d13cc | 1300 | (while (looking-at "^[ \t]*$") |
49275d55 JB |
1301 | (beginning-of-line 2)) |
1302 | (if (featurep 'xemacs) | |
a445370f DN |
1303 | (zmacs-activate-region) |
1304 | (setq mark-active t)) | |
ce5a3ac0 | 1305 | (if (yes-or-no-p "Delete and rebuild header? ") |
3666daf6 | 1306 | (delete-region (point-min) (point)))) |
7c4d13cc CD |
1307 | |
1308 | ;; Insert the mode line | |
1309 | (insert | |
1310 | (format "%% -*- mode: reftex-index-phrases; TeX-master: \"%s\" -*-\n" | |
3666daf6 | 1311 | (file-name-nondirectory (reftex-index-phrase-tex-master)))) |
7c4d13cc CD |
1312 | ;; Insert the macro definitions |
1313 | (insert "% Key Macro Format Repeat\n") | |
1314 | (insert "%---------------------------------------------------------------------\n") | |
1315 | (while (setq entry (pop macro-alist)) | |
1316 | (setq macro (car entry) | |
3666daf6 CD |
1317 | repeat (nth 7 entry) |
1318 | key (car (delq nil (mapcar (lambda (x) (if (equal (nth 1 x) macro) | |
1319 | (car x) | |
1320 | nil)) | |
1321 | reftex-key-to-index-macro-alist)))) | |
7c4d13cc | 1322 | (insert (format ">>>INDEX_MACRO_DEFINITION:\t%s\t%-20s\t%s\n" |
3666daf6 CD |
1323 | (char-to-string key) (concat macro "{%s}") |
1324 | (if repeat "t" "nil")))) | |
7c4d13cc CD |
1325 | (insert "%---------------------------------------------------------------------\n\n\n"))) |
1326 | ||
1327 | (defvar TeX-master) | |
1328 | (defun reftex-index-phrase-tex-master (&optional dir) | |
1329 | "Return the name of the master file associated with a phrase buffer." | |
1330 | (if (and (boundp 'TeX-master) | |
3666daf6 CD |
1331 | (local-variable-p 'TeX-master (current-buffer)) |
1332 | (stringp TeX-master)) | |
7c4d13cc CD |
1333 | ;; We have a local variable which tells us which file to use |
1334 | (expand-file-name TeX-master dir) | |
1335 | ;; have to guess | |
1336 | (concat (file-name-sans-extension (buffer-file-name)) ".tex"))) | |
1337 | ||
1338 | (defun reftex-index-phrases-save-and-return () | |
1339 | "Return to where the `reftex-index-phrase-selection-or-word' was called." | |
1340 | (interactive) | |
1341 | (save-buffer) | |
1342 | (switch-to-buffer (marker-buffer reftex-index-return-marker)) | |
1343 | (goto-char (or (marker-position reftex-index-return-marker) (point)))) | |
1344 | ||
1345 | ||
1346 | (defvar reftex-index-phrases-menu) | |
7b07114a | 1347 | (defvar reftex-index-phrases-marker) |
7c4d13cc CD |
1348 | (defvar reftex-index-phrases-restrict-file nil) |
1349 | ;;;###autoload | |
4d789d84 | 1350 | (define-derived-mode reftex-index-phrases-mode fundamental-mode "Phrases" |
7c4d13cc CD |
1351 | "Major mode for managing the Index phrases of a LaTeX document. |
1352 | This buffer was created with RefTeX. | |
1353 | ||
1354 | To insert new phrases, use | |
1355 | - `C-c \\' in the LaTeX document to copy selection or word | |
1356 | - `\\[reftex-index-new-phrase]' in the phrases buffer. | |
1357 | ||
1358 | To index phrases use one of: | |
1359 | ||
1360 | \\[reftex-index-this-phrase] index current phrase | |
1361 | \\[reftex-index-next-phrase] index next phrase (or N with prefix arg) | |
1362 | \\[reftex-index-all-phrases] index all phrases | |
1363 | \\[reftex-index-remaining-phrases] index current and following phrases | |
1364 | \\[reftex-index-region-phrases] index the phrases in the region | |
1365 | ||
1366 | You can sort the phrases in this buffer with \\[reftex-index-sort-phrases]. | |
1367 | To display information about the phrase at point, use \\[reftex-index-phrases-info]. | |
1368 | ||
1369 | For more information see the RefTeX User Manual. | |
1370 | ||
1371 | Here are all local bindings. | |
1372 | ||
4d789d84 | 1373 | \\{reftex-index-phrases-mode-map}" |
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. |
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) | |
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. | |
1478 | Calls `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. | |
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))) | |
1489 | ||
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) | |
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. | |
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)) | |
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. | |
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)) | |
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. | |
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 | |
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. | |
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) | |
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. | |
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)) | |
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. |
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)) | |
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). | |
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") | |
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. |
1831 | Treats 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 | =========================== | |
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.") | |
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 |
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)) | |
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. | |
2077 | Does 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 | ||
7c4d13cc | 2096 | |
1a9461d0 | 2097 | ;;; reftex-index.el ends here |