1;;; reftex-index.el --- index support with RefTeX 2 3;; Copyright (C) 1997-2021 Free Software Foundation, Inc. 4 5;; Author: Carsten Dominik <dominik@science.uva.nl> 6;; Maintainer: auctex-devel@gnu.org 7 8;; This file is part of GNU Emacs. 9 10;; GNU Emacs is free software: you can redistribute it and/or modify 11;; it under the terms of the GNU General Public License as published by 12;; the Free Software Foundation, either version 3 of the License, or 13;; (at your option) any later version. 14 15;; GNU Emacs is distributed in the hope that it will be useful, 16;; but WITHOUT ANY WARRANTY; without even the implied warranty of 17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18;; GNU General Public License for more details. 19 20;; You should have received a copy of the GNU General Public License 21;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 22 23;;; Commentary: 24 25;;; Code: 26 27(eval-when-compile (require 'cl-lib)) 28(declare-function texmathp "ext:texmathp" ()) 29 30(require 'reftex) 31 32;; START remove for XEmacs release 33(defvar mark-active) 34(defvar transient-mark-mode) 35(defvar TeX-master) 36;; END remove for XEmacs release 37 38;;;###autoload 39(defun reftex-index-selection-or-word (&optional arg phrase) 40 "Put selection or the word near point into the default index macro. 41This uses the information in `reftex-index-default-macro' to make an index 42entry. The phrase indexed is the current selection or the word near point. 43When called with one `C-u' prefix, let the user have a chance to edit the 44index entry. When called with 2 `C-u' as prefix, also ask for the index 45macro and other stuff. 46When called inside TeX math mode as determined by the `texmathp.el' library 47which is part of AUCTeX, the string is first processed with the 48`reftex-index-math-format', which see." 49 (interactive "P") 50 (let* ((use-default (not (equal arg '(16)))) ; check for double prefix 51 ;; check if we have an active selection 52 (active (reftex-region-active-p)) 53 (beg (if active 54 (region-beginning) 55 (save-excursion 56 (skip-syntax-backward "w\\") (point)))) 57 (end (if active 58 (region-end) 59 (save-excursion 60 (skip-syntax-forward "w\\") (point)))) 61 (sel (buffer-substring beg end)) 62 (mathp (condition-case nil (texmathp) (error nil))) 63 (current-prefix-arg nil) ; we want to call reftex-index without prefix. 64 key def-char def-tag full-entry) 65 66 (if phrase 67 (progn 68 (reftex-index-visit-phrases-buffer) 69 (reftex-index-new-phrase sel)) 70 71 (if (equal sel "") 72 ;; Nothing selected, no word, so use full reftex-index command 73 (reftex-index) 74 ;; OK, we have something to index here. 75 ;; Add the dollars when necessary 76 (setq key (if mathp 77 (format reftex-index-math-format sel) 78 sel)) 79 ;; Get info from `reftex-index-default-macro' 80 (setq def-char (if use-default (car reftex-index-default-macro))) 81 (setq def-tag (if use-default (nth 1 reftex-index-default-macro))) 82 ;; Does the user want to edit the entry? 83 (setq full-entry (if arg 84 (reftex-index-complete-key 85 def-tag nil (cons key 0)) 86 key)) 87 ;; Delete what is in the buffer and make the index entry 88 (delete-region beg end) 89 (reftex-index def-char full-entry def-tag sel))))) 90 91;;;###autoload 92(defun reftex-index (&optional char key tag sel no-insert) 93 "Query for an index macro and insert it along with its arguments. 94The index macros available are those defined in `reftex-index-macro' or 95by a call to `reftex-add-index-macros', typically from an AUCTeX style file. 96RefteX provides completion for the index tag and the index key, and 97will prompt for other arguments." 98 99 (interactive) 100 101 ;; Ensure access to scanning info 102 (reftex-ensure-index-support t) 103 (reftex-access-scan-info current-prefix-arg) 104 105 ;; Find out which macro we are going to use 106 (let* ((char (or char 107 (reftex-select-with-char reftex-query-index-macro-prompt 108 reftex-query-index-macro-help))) 109 (macro (nth 1 (assoc char reftex-key-to-index-macro-alist))) 110 (entry (or (assoc macro reftex-index-macro-alist) 111 (error "No index macro associated with %c" char))) 112 (ntag (nth 1 entry)) 113 (tag (or tag (nth 1 entry))) 114 (nargs (nth 4 entry)) 115 (nindex (nth 5 entry)) 116 (opt-args (nth 6 entry)) 117 (repeat (nth 7 entry)) 118 opt tag1 value) 119 120 ;; Get the supported arguments 121 (if (stringp tag) 122 (setq tag1 tag) 123 (setq tag1 (or (reftex-index-complete-tag tag opt-args) ""))) 124 (setq key (or key 125 (reftex-index-complete-key 126 (if (string= tag1 "") "idx" tag1) 127 (member nindex opt-args)))) 128 129 ;; Insert the macro and ask for any additional args 130 (insert macro) 131 (cl-loop for i from 1 to nargs do 132 (setq opt (member i opt-args) 133 value (cond ((= nindex i) key) 134 ((equal ntag i) tag1) 135 (t (read-string (concat "Macro arg nr. " 136 (int-to-string i) 137 (if opt " (optional)" "") 138 ": "))))) 139 (unless (and opt (string= value "")) 140 (insert (if opt "[" "{") value (if opt "]" "}")))) 141 (and repeat (stringp sel) (insert sel)) 142 (and key reftex-plug-into-AUCTeX (fboundp 'LaTeX-add-index-entries) 143 (LaTeX-add-index-entries key)) 144 (reftex-index-update-taglist tag1) 145 (reftex-notice-new))) 146 147(defun reftex-default-index () 148 (cond ((null reftex-index-default-tag) nil) 149 ((stringp reftex-index-default-tag) reftex-index-default-tag) 150 (t (or (get reftex-docstruct-symbol 'default-index-tag) 151 "idx")))) 152 153(defun reftex-update-default-index (tag &optional tag-list) 154 (if (and (not (equal tag "")) 155 (stringp tag) 156 (eq reftex-index-default-tag 'last) 157 (or (null tag-list) 158 (member tag tag-list))) 159 (put reftex-docstruct-symbol 'default-index-tag tag))) 160 161;;;###autoload 162(defun reftex-index-complete-tag (&optional itag opt-args) 163 ;; Ask the user for a tag, completing on known tags. 164 ;; ITAG is the argument number which contains the tag. 165 ;; OPT-ARGS is a list of optional argument indices, as given by 166 ;; `reftex-parse-args'. 167 (let* ((opt (and (integerp itag) (member itag opt-args))) 168 (index-tags (cdr (assq 'index-tags 169 (symbol-value reftex-docstruct-symbol)))) 170 (default (reftex-default-index)) 171 (prompt (concat "Index tag" 172 (if (or opt default) 173 (format " (%s): " 174 (concat 175 (if opt "optional" "") 176 (if default 177 (concat (if opt ", " "") 178 (format "default %s" default)) 179 ""))) 180 ": "))) 181 (tag (completing-read prompt (mapcar 'list index-tags)))) 182 (if (and default (equal tag "")) (setq tag default)) 183 (reftex-update-default-index tag) 184 tag)) 185 186;;;###autoload 187(defun reftex-index-select-tag () 188 ;; Have the user select an index tag. 189 ;; FIXME: should we cache tag-alist, prompt and help? 190 (let* ((index-tags (cdr (assoc 'index-tags 191 (symbol-value reftex-docstruct-symbol)))) 192 (default (reftex-default-index))) 193 (cond 194 ((null index-tags) 195 (error "No index tags available")) 196 197 ((= (length index-tags) 1) 198 ;; Just one index, use it 199 (car index-tags)) 200 201 ((> (length index-tags) 1) 202 ;; Several indices, ask. 203 (let* ((tags (copy-sequence index-tags)) 204 (cnt 0) 205 tag-alist i val len tag prompt help rpl) 206 ;; Move idx and glo up in the list to ensure ?i and ?g shortcuts 207 (if (member "glo" tags) 208 (setq tags (cons "glo" (delete "glo" tags)))) 209 (if (member "idx" tags) 210 (setq tags (cons "idx" (delete "idx" tags)))) 211 ;; Find unique shortcuts for each index. 212 (while (setq tag (pop tags)) 213 (setq len (length tag) 214 i -1 215 val nil) 216 (catch 'exit 217 (while (and (< (cl-incf i) len) (null val)) 218 (unless (assq (aref tag i) tag-alist) 219 (push (list (aref tag i) 220 tag 221 (concat (substring tag 0 i) 222 "[" (substring tag i (cl-incf i)) "]" 223 (substring tag i))) 224 tag-alist) 225 (throw 'exit t))) 226 (push (list (+ ?0 (cl-incf cnt)) tag 227 (concat "[" (int-to-string cnt) "]:" tag)) 228 tag-alist))) 229 (setq tag-alist (nreverse tag-alist)) 230 ;; Compute Prompt and Help strings 231 (setq prompt 232 (concat 233 (format "Select Index%s: " 234 (if default (format " (Default <%s>)" default) "")) 235 (mapconcat (lambda(x) (nth 2 x)) tag-alist " "))) 236 (setq help 237 (concat "Select an Index\n===============\n" 238 (if default 239 (format "[^M] %s (the default)\n" default) 240 "") 241 (mapconcat (lambda(x) 242 (apply 'format "[%c] %s" x)) 243 tag-alist "\n"))) 244 ;; Query the user for an index-tag 245 (setq rpl (reftex-select-with-char prompt help 3 t)) 246 (message "") 247 (if (and default (equal rpl ?\C-m)) 248 default 249 (if (assq rpl tag-alist) 250 (progn 251 (reftex-update-default-index (nth 1 (assq rpl tag-alist))) 252 (nth 1 (assq rpl tag-alist))) 253 (error "No index tag associated with %c" rpl))))) 254 (t (error "This should not happen (reftex-index-select-tag)"))))) 255 256;;;###autoload 257(defun reftex-index-complete-key (&optional tag optional initial) 258 ;; Read an index key, with completion. 259 ;; Restrict completion table on index tag TAG. 260 ;; OPTIONAL indicates if the arg is optional. 261 (let* ((table (reftex-sublist-nth 262 (symbol-value reftex-docstruct-symbol) 6 263 (lambda(x) (and (eq (car x) 'index) 264 (string= (nth 1 x) (or tag "")))) 265 t)) 266 (prompt (concat "Index key" (if optional " (optional)" "") ": ")) 267 (key (completing-read prompt table nil nil initial))) 268 key)) 269 270(defun reftex-index-update-taglist (newtag) 271 ;; add NEWTAG to the list of available index tags. 272 (let ((cell (assoc 'index-tags (symbol-value reftex-docstruct-symbol)))) 273 (and newtag (cdr cell) (not (member newtag (cdr cell))) 274 (push newtag (cdr cell))))) 275 276(define-obsolete-variable-alias 277 'reftex-index-map 'reftex-index-mode-map "24.1") 278(defvar reftex-index-mode-map 279 (let ((map (make-sparse-keymap))) 280 ;; Index map 281 (define-key map (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 282 'reftex-index-mouse-goto-line-and-hide) 283 (define-key map [follow-link] 'mouse-face) 284 285 (substitute-key-definition 286 'next-line 'reftex-index-next map global-map) 287 (substitute-key-definition 288 'previous-line 'reftex-index-previous map global-map) 289 290 (define-key map "n" 'reftex-index-next) 291 (define-key map "p" 'reftex-index-previous) 292 (define-key map "?" 'reftex-index-show-help) 293 (define-key map " " 'reftex-index-view-entry) 294 (define-key map "\C-m" 'reftex-index-goto-entry-and-hide) 295 (define-key map "\C-i" 'reftex-index-goto-entry) 296 (define-key map "\C-k" 'reftex-index-kill) 297 (define-key map "r" 'reftex-index-rescan) 298 (define-key map "R" 'reftex-index-Rescan) 299 (define-key map "g" 'revert-buffer) 300 (define-key map "q" 'reftex-index-quit) 301 (define-key map "k" 'reftex-index-quit-and-kill) 302 (define-key map "f" 'reftex-index-toggle-follow) 303 (define-key map "s" 'reftex-index-switch-index-tag) 304 (define-key map "e" 'reftex-index-edit) 305 (define-key map "^" 'reftex-index-level-up) 306 (define-key map "_" 'reftex-index-level-down) 307 (define-key map "}" 'reftex-index-restrict-to-section) 308 (define-key map "{" 'reftex-index-widen) 309 (define-key map ">" 'reftex-index-restriction-forward) 310 (define-key map "<" 'reftex-index-restriction-backward) 311 (define-key map "(" 'reftex-index-toggle-range-beginning) 312 (define-key map ")" 'reftex-index-toggle-range-end) 313 (define-key map "|" 'reftex-index-edit-attribute) 314 (define-key map "@" 'reftex-index-edit-visual) 315 (define-key map "*" 'reftex-index-edit-key) 316 (define-key map "\C-c=" 'reftex-index-goto-toc) 317 (define-key map "c" 'reftex-index-toggle-context) 318 319 ;; The capital letters and the exclamation mark 320 (cl-loop for key across (concat "!" reftex-index-section-letters) do 321 (define-key map (vector (list key)) 322 (list 'lambda '() '(interactive) 323 (list 'reftex-index-goto-letter key)))) 324 325 (easy-menu-define reftex-index-menu map 326 "Menu for Index buffer" 327 '("Index" 328 ["Goto section A-Z" 329 (message "To go to a section, just press any of: !%s" 330 reftex-index-section-letters) t] 331 ["Show Entry" reftex-index-view-entry t] 332 ["Go To Entry" reftex-index-goto-entry t] 333 ["Exit & Go To Entry" reftex-index-goto-entry-and-hide t] 334 ["Table of Contents" reftex-index-goto-toc t] 335 ["Quit" reftex-index-quit t] 336 "--" 337 ("Update" 338 ["Rebuilt *Index* Buffer" revert-buffer t] 339 "--" 340 ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans] 341 ["Rescan Entire Document" reftex-index-Rescan t]) 342 ("Restrict" 343 ["Restrict to section" reftex-index-restrict-to-section t] 344 ["Widen" reftex-index-widen reftex-index-restriction-indicator] 345 ["Next Section" reftex-index-restriction-forward 346 reftex-index-restriction-indicator] 347 ["Previous Section" reftex-index-restriction-backward 348 reftex-index-restriction-indicator]) 349 ("Edit" 350 ["Edit Entry" reftex-index-edit t] 351 ["Edit Key" reftex-index-edit-key t] 352 ["Edit Attribute" reftex-index-edit-attribute t] 353 ["Edit Visual" reftex-index-edit-visual t] 354 "--" 355 ["Add Parentkey" reftex-index-level-down t] 356 ["Remove Parentkey " reftex-index-level-up t] 357 "--" 358 ["Make Start-of-Range" reftex-index-toggle-range-beginning t] 359 ["Make End-of-Range" reftex-index-toggle-range-end t] 360 "--" 361 ["Kill Entry" reftex-index-kill nil] 362 "--" 363 ["Undo" reftex-index-undo nil]) 364 ("Options" 365 ["Context" reftex-index-toggle-context :style toggle 366 :selected reftex-index-include-context] 367 "--" 368 ["Follow Mode" reftex-index-toggle-follow :style toggle 369 :selected reftex-index-follow-mode]) 370 "--" 371 ["Help" reftex-index-show-help t])) 372 373 map) 374 "Keymap used for *Index* buffers.") 375 376(defvar reftex-index-menu) 377 378(defvar reftex-last-index-file nil 379 "Stores the file name from which `reftex-display-index' was called.") 380(defvar reftex-index-tag nil 381 "Stores the tag of the index in an index buffer.") 382 383(defvar reftex-index-return-marker (make-marker) 384 "Marker which makes it possible to return from index to old position.") 385 386(defvar reftex-index-restriction-indicator nil) 387(defvar reftex-index-restriction-data nil) 388 389(define-derived-mode reftex-index-mode special-mode "RefTeX Index" 390 "Major mode for managing Index buffers for LaTeX files. 391This buffer was created with RefTeX. 392Press `?' for a summary of important key bindings, or check the menu. 393 394Here are all local bindings. 395 396\\{reftex-index-mode-map}" 397 (set (make-local-variable 'revert-buffer-function) 'reftex-index-revert) 398 (set (make-local-variable 'reftex-index-restriction-data) nil) 399 (set (make-local-variable 'reftex-index-restriction-indicator) nil) 400 (setq mode-line-format 401 (list "---- " 'mode-line-buffer-identification 402 " " 'global-mode-string 403 " R<" 'reftex-index-restriction-indicator ">" 404 " -%-")) 405 (setq truncate-lines t) 406 (when (featurep 'xemacs) 407 ;; XEmacs needs the call to make-local-hook 408 (make-local-hook 'post-command-hook) 409 (make-local-hook 'pre-command-hook)) 410 (make-local-variable 'reftex-last-follow-point) 411 (easy-menu-add reftex-index-menu reftex-index-mode-map) 412 (add-hook 'post-command-hook 'reftex-index-post-command-hook nil t) 413 (add-hook 'pre-command-hook 'reftex-index-pre-command-hook nil t)) 414 415(defconst reftex-index-help 416" AVAILABLE KEYS IN INDEX BUFFER 417 ============================== 418! A..Z Goto the section of entries starting with this letter. 419n / p next-entry / previous-entry 420SPC / TAB Show/Goto the corresponding entry in the LaTeX document. 421RET Goto the entry and hide the *Index* window (also on mouse-2). 422q / k Hide/Kill *Index* buffer. 423C-c = Switch to the TOC buffer. 424f / c Toggle follow mode / Toggle display of [c]ontext. 425g Refresh *Index* buffer. 426r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document. 427s Switch to a different index (for documents with multiple indices). 428e / C-k Edit/Kill the entry. 429* | @ Edit specific part of entry: [*]key [|]attribute [@]visual 430 With prefix: kill that part. 431\( ) Toggle entry's beginning/end of page range property. 432_ ^ Add/Remove parent key (to make this item a subitem). 433} / { Restrict Index to a single document section / Widen. 434< / > When restricted, move restriction to previous/next section.") 435 436;;;###autoload 437(defun reftex-index-show-entry (data &optional no-revisit) 438 ;; Find an index entry associated with DATA and display it highlighted 439 ;; in another window. NO-REVISIT means we are not allowed to visit 440 ;; files for this. 441 ;; Note: This function just looks for the nearest match of the 442 ;; context string and may fail if the entry moved and an identical 443 ;; entry is close to the old position. Frequent rescans make this 444 ;; safer. 445 (let* ((file (nth 3 data)) 446 (literal (nth 2 data)) 447 (pos (nth 4 data)) 448 (re (regexp-quote literal)) 449 (match 450 (cond 451 ((or (not no-revisit) 452 (reftex-get-buffer-visiting file)) 453 (switch-to-buffer-other-window 454 (reftex-get-file-buffer-force file nil)) 455 (goto-char (or pos (point-min))) 456 (or (looking-at re) 457 (reftex-nearest-match re (length literal)))) 458 (t (message "%s" reftex-no-follow-message) nil)))) 459 (when match 460 (goto-char (match-beginning 0)) 461 (recenter '(4)) 462 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer))) 463 match)) 464 465;;;###autoload 466(defun reftex-display-index (&optional tag overriding-restriction redo 467 &rest locations) 468 "Display a buffer with an index compiled from the current document. 469When the document has multiple indices, first prompts for the correct one. 470When index support is turned off, offer to turn it on. 471With one or two `C-u' prefixes, rescan document first. 472With prefix 2, restrict index to current document section. 473With prefix 3, restrict index to region." 474 475 (interactive) 476 477 ;; Ensure access to scanning info and rescan buffer if prefix arg is '(4). 478 (let ((current-prefix-arg current-prefix-arg)) 479 (reftex-ensure-index-support t) 480 (reftex-access-scan-info current-prefix-arg)) 481 482 (set-marker reftex-index-return-marker (point)) 483 (setq reftex-last-follow-point 1) 484 485 ;; Determine the correct index to process 486 (let* ((docstruct (symbol-value reftex-docstruct-symbol)) 487 (docstruct-symbol reftex-docstruct-symbol) 488 (index-tag (or tag (reftex-index-select-tag))) 489 (master (reftex-TeX-master-file)) 490 (calling-file (buffer-file-name)) 491 (restriction 492 (or overriding-restriction 493 (and (not redo) 494 (reftex-get-restriction current-prefix-arg docstruct)))) 495 (locations 496 ;; See if we are on an index macro as initial position 497 (or locations 498 (let* ((what-macro (reftex-what-macro-safe 1)) 499 (macro (car what-macro)) 500 (here-I-am (when (member macro reftex-macros-with-index) 501 (save-excursion 502 (goto-char (+ (cdr what-macro) 503 (length macro))) 504 (reftex-move-over-touching-args) 505 (reftex-where-am-I))))) 506 (if (eq (car (car here-I-am)) 'index) 507 (list (car here-I-am)))))) 508 buffer-name) 509 510 (setq buffer-name (reftex-make-index-buffer-name index-tag)) 511 512 ;; Goto the buffer and put it into the correct mode 513 514 (when (or restriction current-prefix-arg) 515 (reftex-kill-buffer buffer-name)) 516 517 (if (get-buffer-window buffer-name) 518 (select-window (get-buffer-window buffer-name)) 519 (switch-to-buffer buffer-name)) 520 521 (or (eq major-mode 'reftex-index-mode) (reftex-index-mode)) 522 523 ;; If the buffer is currently restricted, empty it to force update. 524 (when reftex-index-restriction-data 525 (reftex-erase-buffer)) 526 (set (make-local-variable 'reftex-last-index-file) calling-file) 527 (set (make-local-variable 'reftex-index-tag) index-tag) 528 (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol) 529 (if restriction 530 (setq reftex-index-restriction-indicator (car restriction) 531 reftex-index-restriction-data (cdr restriction)) 532 (if (not redo) 533 (setq reftex-index-restriction-indicator nil 534 reftex-index-restriction-data nil))) 535 (when (= (buffer-size) 0) 536 ;; buffer is empty - fill it 537 (message "Building %s buffer..." buffer-name) 538 539 (setq buffer-read-only nil) 540 (insert (format 541 "INDEX <%s> on %s 542Restriction: <%s> 543SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help 544------------------------------------------------------------------------------ 545" 546 index-tag (abbreviate-file-name master) 547 (if (eq (car (car reftex-index-restriction-data)) 'toc) 548 (nth 2 (car reftex-index-restriction-data)) 549 reftex-index-restriction-indicator))) 550 551 (if (reftex-use-fonts) 552 (put-text-property (point-min) (point) 553 'face reftex-index-header-face)) 554 (if (fboundp 'cursor-intangible-mode) 555 (cursor-intangible-mode 1) 556 ;; If `cursor-intangible' is not available, fallback on the old 557 ;; intrusive `intangible' property. 558 (put-text-property (point-min) (point) 'intangible t)) 559 (add-text-properties (point-min) (point) 560 '(cursor-intangible t 561 front-sticky (cursor-intangible) 562 rear-nonsticky (cursor-intangible))) 563 564 (reftex-insert-index docstruct index-tag) 565 (goto-char (point-min)) 566 (run-hooks 'reftex-display-copied-context-hook) 567 (message "Building %s buffer...done." buffer-name) 568 (setq buffer-read-only t)) 569 (and locations (apply 'reftex-find-start-point (point) locations)) 570 (if reftex-index-restriction-indicator 571 (message "Index restricted: <%s>" reftex-index-restriction-indicator)))) 572 573(defun reftex-insert-index (docstruct tag &optional update-one remark) 574 ;; Insert an index into the current buffer. Entries are from the 575 ;; DOCSTRUCT. 576 ;; TAG is the subindex to process. 577 ;; UPDATE-ONE: When non-nil, delete the entry at point and replace 578 ;; it with whatever the DOCSTRUCT contains. 579 ;; REMARK can be a note to add to the entry. 580 (let* ((all docstruct) 581 (indent " ") 582 (context reftex-index-include-context) 583 (context-indent (concat indent " ")) 584 (section-chars (mapcar 'identity reftex-index-section-letters)) 585 (this-section-char 0) 586 (font (reftex-use-fonts)) 587 (bor (car reftex-index-restriction-data)) 588 (eor (nth 1 reftex-index-restriction-data)) 589 (mouse-face 590 (if (memq reftex-highlight-selection '(mouse both)) 591 reftex-mouse-selected-face 592 nil)) 593 (index-face reftex-label-face) 594 sublist cell from to first-char) 595 596 ;; Make the sublist and sort it 597 (when bor 598 (setq all (or (memq bor all) all))) 599 600 (while (setq cell (pop all)) 601 (if (eq cell eor) 602 (setq all nil) 603 (and (eq (car cell) 'index) 604 (equal (nth 1 cell) tag) 605 (push cell sublist)))) 606 (setq sublist (sort (nreverse sublist) 607 (lambda (a b) (string< (nth 8 a) (nth 8 b))))) 608 609 (when update-one 610 ;; Delete the entry at place 611 (and (bolp) (forward-char 1)) 612 (delete-region (previous-single-property-change (1+ (point)) :data) 613 (or (next-single-property-change (point) :data) 614 (point-max)))) 615 616 ;; Walk through the list and insert all entries 617 (while (setq cell (pop sublist)) 618 (unless update-one 619 (setq first-char (upcase (string-to-char (nth 6 cell)))) 620 (when (and (not (equal first-char this-section-char)) 621 (member first-char section-chars)) 622 ;; There is a new initial letter, so start a new section 623 (reftex-index-insert-new-letter first-char font) 624 (setq section-chars (delete first-char section-chars) 625 this-section-char first-char)) 626 (when (= this-section-char 0) 627 (setq this-section-char ?!) 628 (reftex-index-insert-new-letter this-section-char font))) 629 630 (setq from (point)) 631 (insert indent (nth 7 cell)) 632 (when font 633 (setq to (point)) 634 (put-text-property 635 (- (point) (length (nth 7 cell))) to 636 'face index-face) 637 (goto-char to)) 638 639 (when (or remark (nth 9 cell)) 640 (and (< (current-column) 40) 641 ;; FIXME: maybe this is too slow? 642 (insert (make-string (max (- 40 (current-column)) 0) ?\ ))) 643 (and (nth 9 cell) (insert " " (substring (nth 5 cell) (nth 9 cell)))) 644 (and remark (insert " " remark))) 645 646 (insert "\n") 647 (setq to (point)) 648 649 (when context 650 (insert context-indent (nth 2 cell) "\n") 651 (setq to (point))) 652 (put-text-property from to :data cell) 653 (when mouse-face 654 (put-text-property from (1- to) 655 'mouse-face mouse-face)) 656 (goto-char to)))) 657 658 659(defun reftex-index-insert-new-letter (letter &optional font) 660 ;; Start a new section in the index 661 (let ((from (point))) 662 (insert "\n" letter letter letter 663 "-----------------------------------------------------------------") 664 (when font 665 (put-text-property from (point) 'face reftex-index-section-face)) 666 (insert "\n"))) 667 668(defun reftex-get-restriction (arg docstruct) 669 ;; Interpret the prefix ARG and derive index restriction specs. 670 (let* ((beg (min (point) (or (condition-case nil (mark) (error nil)) 671 (point-max)))) 672 (end (max (point) (or (condition-case nil (mark) (error nil)) 673 (point-min)))) 674 bor eor label here-I-am) 675 (cond 676 ((eq arg 2) 677 (setq here-I-am (car (reftex-where-am-I)) 678 bor (if (eq (car here-I-am) 'toc) 679 here-I-am 680 (reftex-last-assoc-before-elt 681 'toc here-I-am docstruct)) 682 eor (car (memq (assq 'toc (cdr (memq bor docstruct))) docstruct)) 683 label (nth 6 bor))) 684 ((eq arg 3) 685 (save-excursion 686 (setq label "region") 687 (goto-char beg) 688 (setq bor (car (reftex-where-am-I))) 689 (setq bor (nth 1 (memq bor docstruct))) 690 (goto-char end) 691 (setq eor (nth 1 (memq (car (reftex-where-am-I)) docstruct))))) 692 (t nil)) 693 (if (and label (or bor eor)) 694 (list label bor eor) 695 nil))) 696 697(defun reftex-index-pre-command-hook () 698 ;; Used as pre command hook in *Index* buffer 699 (reftex-unhighlight 0) 700 (reftex-unhighlight 1)) 701 702(defun reftex-index-post-command-hook () 703 ;; Used in the post-command-hook for the *Index* buffer 704 ;; FIXME: Lots of redundancy with reftex-toc-post-command-hook! 705 (when (get-text-property (point) :data) 706 (and (> (point) 1) ;FIXME: Is this point-min or do we care about narrowing? 707 (not (get-text-property (point) 'cursor-intangible)) 708 (memq reftex-highlight-selection '(cursor both)) 709 (reftex-highlight 1 710 (or (previous-single-property-change (1+ (point)) :data) 711 (point-min)) 712 (or (next-single-property-change (point) :data) 713 (point-max))))) 714 (if (integerp reftex-index-follow-mode) 715 ;; Remove delayed action 716 (setq reftex-index-follow-mode t) 717 (and reftex-index-follow-mode 718 (not (equal reftex-last-follow-point (point))) 719 ;; Show context in other window 720 (setq reftex-last-follow-point (point)) 721 (condition-case nil 722 (reftex-index-visit-location nil (not reftex-revisit-to-follow)) 723 (error t))))) 724 725(defun reftex-index-show-help () 726 "Show a summary of special key bindings." 727 (interactive) 728 (with-output-to-temp-buffer "*RefTeX Help*" 729 (princ reftex-index-help)) 730 (reftex-enlarge-to-fit "*RefTeX Help*" t) 731 ;; If follow mode is active, arrange to delay it one command 732 (if reftex-index-follow-mode 733 (setq reftex-index-follow-mode 1))) 734 735(defun reftex-index-next (&optional arg) 736 "Move to next selectable item." 737 (interactive "p") 738 (setq reftex-callback-fwd t) 739 (or (eobp) (forward-char 1)) 740 (goto-char (or (next-single-property-change (point) :data) 741 (point))) 742 (unless (get-text-property (point) :data) 743 (goto-char (or (next-single-property-change (point) :data) 744 (point))))) 745(defun reftex-index-previous (&optional arg) 746 "Move to previous selectable item." 747 (interactive "p") 748 (setq reftex-callback-fwd nil) 749 (goto-char (or (previous-single-property-change (point) :data) 750 (point))) 751 (unless (get-text-property (point) :data) 752 (goto-char (or (previous-single-property-change (point) :data) 753 (point))))) 754(defun reftex-index-toggle-follow () 755 "Toggle follow (other window follows with context)." 756 (interactive) 757 (setq reftex-last-follow-point -1) 758 (setq reftex-index-follow-mode (not reftex-index-follow-mode))) 759(defun reftex-index-toggle-context () 760 "Toggle inclusion of label context in *Index* buffer. 761Label context is only displayed when the labels are there as well." 762 (interactive) 763 (setq reftex-index-include-context (not reftex-index-include-context)) 764 (reftex-index-revert)) 765(defun reftex-index-view-entry () 766 "View document location in other window." 767 (interactive) 768 (reftex-index-visit-location)) 769(defun reftex-index-goto-entry-and-hide () 770 "Go to document location in other window. Hide the *Index* window." 771 (interactive) 772 (reftex-index-visit-location 'hide)) 773(defun reftex-index-goto-entry () 774 "Go to document location in other window. *Index* window stays." 775 (interactive) 776 (reftex-index-visit-location t)) 777(defun reftex-index-mouse-goto-line-and-hide (ev) 778 "Go to document location in other window. Hide the *Index* window." 779 (interactive "e") 780 (mouse-set-point ev) 781 (reftex-index-visit-location 'hide)) 782(defun reftex-index-quit () 783 "Hide the *Index* window and do not move point." 784 (interactive) 785 (or (one-window-p) (delete-window)) 786 (switch-to-buffer (marker-buffer reftex-index-return-marker)) 787 (goto-char (or (marker-position reftex-index-return-marker) (point)))) 788(defun reftex-index-quit-and-kill () 789 "Kill the *Index* buffer." 790 (interactive) 791 (kill-buffer (current-buffer)) 792 (or (one-window-p) (delete-window)) 793 (switch-to-buffer (marker-buffer reftex-index-return-marker)) 794 (goto-char (or (marker-position reftex-index-return-marker) (point)))) 795(defun reftex-index-goto-toc (&rest ignore) 796 "Switch to the table of contents of the current document. 797The function will go to the section where the entry at point was defined." 798 (interactive) 799 (if (get-text-property (point) :data) 800 (reftex-index-goto-entry) 801 (switch-to-buffer (marker-buffer reftex-index-return-marker))) 802 (delete-other-windows) 803 (reftex-toc)) 804(defun reftex-index-rescan (&rest ignore) 805 "Regenerate the *Index* buffer after reparsing file of section at point." 806 (interactive) 807 (let ((index-tag reftex-index-tag)) 808 (if (and reftex-enable-partial-scans 809 (null current-prefix-arg)) 810 (let* ((data (get-text-property (point) :data)) 811 (file (nth 3 data)) 812 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) 813 (if (not file) 814 (error "Don't know which file to rescan. Try `C-u r'") 815 (switch-to-buffer (reftex-get-file-buffer-force file)) 816 (setq current-prefix-arg '(4)) 817 (reftex-display-index index-tag nil 'redo line))) 818 (reftex-index-Rescan)) 819 (reftex-kill-temporary-buffers))) 820(defun reftex-index-Rescan (&rest ignore) 821 "Regenerate the *Index* buffer after reparsing the entire document." 822 (interactive) 823 (let ((index-tag reftex-index-tag) 824 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) 825 (switch-to-buffer 826 (reftex-get-file-buffer-force reftex-last-index-file)) 827 (setq current-prefix-arg '(16)) 828 (reftex-display-index index-tag nil 'redo line))) 829(defun reftex-index-revert (&rest ignore) 830 "Regenerate the *Index* from the internal lists. No reparsing os done." 831 (interactive) 832 (let ((buf (current-buffer)) 833 (index-tag reftex-index-tag) 834 (data (get-text-property (point) :data)) 835 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) 836 (switch-to-buffer 837 (reftex-get-file-buffer-force reftex-last-index-file)) 838 (reftex-erase-buffer buf) 839 (setq current-prefix-arg nil 840 reftex-last-follow-point 1) 841 (reftex-display-index index-tag nil 'redo data line))) 842(defun reftex-index-switch-index-tag (&rest ignore) 843 "Switch to a different index of the same document." 844 (interactive) 845 (switch-to-buffer 846 (reftex-get-file-buffer-force reftex-last-index-file)) 847 (setq current-prefix-arg nil) 848 (reftex-display-index nil nil 'redo)) 849 850(defun reftex-index-restrict-to-section (&optional force) 851 "Restrict index to entries defined in same document sect. as entry at point." 852 ;; Optional FORCE means, even if point is not on an index entry. 853 (interactive) 854 (let* ((data (get-text-property (point) :data)) 855 (docstruct (symbol-value reftex-docstruct-symbol)) 856 bor eor) 857 (if (and (not data) force) 858 (setq data (assq 'toc docstruct))) 859 (when data 860 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct) 861 eor (car (memq (assq 'toc (cdr (memq bor docstruct))) 862 docstruct)) 863 reftex-index-restriction-data (list bor eor) 864 reftex-index-restriction-indicator (nth 6 bor) ))) 865 (reftex-index-revert)) 866 867(defun reftex-index-widen (&rest ignore) 868 "Show the unrestricted index (all entries)." 869 (interactive) 870 (setq reftex-index-restriction-indicator nil 871 reftex-index-restriction-data nil) 872 (reftex-index-revert) 873 (message "Index widened")) 874(defun reftex-index-restriction-forward (&rest ignore) 875 "Restrict to previous section. 876When index is currently unrestricted, restrict it to a section. 877When index is restricted, select the next section as restriction criterion." 878 (interactive) 879 (let* ((docstruct (symbol-value reftex-docstruct-symbol)) 880 (bor (nth 1 reftex-index-restriction-data))) 881 (if (or (not bor) 882 (not (eq (car bor) 'toc))) 883 (reftex-index-restrict-to-section t) 884 (setq reftex-index-restriction-indicator (nth 6 bor) 885 reftex-index-restriction-data 886 (list bor 887 (car (memq (assq 'toc (cdr (memq bor docstruct))) 888 docstruct)))) 889 (reftex-index-revert)))) 890(defun reftex-index-restriction-backward (&rest ignore) 891 "Restrict to next section. 892When index is currently unrestricted, restrict it to a section. 893When index is restricted, select the previous section as restriction criterion." 894 (interactive) 895 (let* ((docstruct (symbol-value reftex-docstruct-symbol)) 896 (eor (car reftex-index-restriction-data)) 897 (bor (reftex-last-assoc-before-elt 'toc eor docstruct t))) 898 (if (or (not bor) 899 (not (eq (car bor) 'toc))) 900 (reftex-index-restrict-to-section t) 901 (setq reftex-index-restriction-indicator (nth 6 bor) 902 reftex-index-restriction-data 903 (list bor eor)) 904 (reftex-index-revert)))) 905 906(defun reftex-index-visit-location (&optional final no-revisit) 907 ;; Visit the tex file corresponding to the index entry on the current line. 908 ;; If FINAL is t, stay there 909 ;; If FINAL is 'hide, hide the *Index* window. 910 ;; Otherwise, move cursor back into *Index* window. 911 ;; NO-REVISIT means don't visit files, just use live buffers. 912 913 (let* ((data (get-text-property (point) :data)) 914 (index-window (selected-window)) 915 show-window show-buffer match) 916 917 (unless data (error "Don't know which index entry to visit")) 918 919 (if (eq (car data) 'index) 920 (setq match (reftex-index-show-entry data no-revisit))) 921 922 (setq show-window (selected-window) 923 show-buffer (current-buffer)) 924 925 (unless match 926 (select-window index-window) 927 (error "Cannot find location")) 928 929 (select-window index-window) 930 931 ;; Use the `final' parameter to decide what to do next 932 (cond 933 ((eq final t) 934 (reftex-unhighlight 0) 935 (select-window show-window)) 936 ((eq final 'hide) 937 (reftex-unhighlight 0) 938 (or (one-window-p) (delete-window)) 939 (switch-to-buffer show-buffer)) 940 (t nil)))) 941 942(defun reftex-index-analyze-entry (data) 943 ;; This splits the index context so that key, attribute and visual 944 ;; values are accessible individually. 945 (interactive) 946 (let* ((arg (nth 5 data)) 947 (context (nth 2 data)) 948 (sc reftex-index-special-chars) 949 (boa (if (string-match (regexp-quote (concat "{" arg "}")) context) 950 (1+ (match-beginning 0)) 951 (error "Something is wrong here"))) 952 (eoa (1- (match-end 0))) 953 (boactual (if (string-match (concat "[^" (nth 3 sc) "]" (nth 2 sc)) 954 context boa) 955 (1+ (match-beginning 0)) 956 eoa)) 957 (boattr (if (string-match (concat "[^" (nth 3 sc) "]" (nth 1 sc)) 958 context boa) 959 (1+ (match-beginning 0)) 960 boactual)) 961 (pre (substring context 0 boa)) 962 (key (substring context boa boattr)) 963 (attr (substring context boattr boactual)) 964 (actual (substring context boactual eoa)) 965 (post (substring context eoa))) 966 (list pre key attr actual post))) 967 968(defun reftex-index-edit () 969 "Edit the index entry at point." 970 (interactive) 971 (let* ((data (get-text-property (point) :data)) 972 old new) 973 (unless data (error "Don't know which index entry to edit")) 974 (reftex-index-view-entry) 975 (setq old (nth 2 data) new (read-string "Edit: " old)) 976 (reftex-index-change-entry new))) 977 978(defun reftex-index-toggle-range-beginning () 979 "Toggle the page range start attribute `|('." 980 (interactive) 981 (let* ((data (get-text-property (point) :data)) 982 (bor (concat (nth 1 reftex-index-special-chars) "(")) 983 new analyze attr) 984 (unless data (error "Don't know which index entry to edit")) 985 (setq analyze (reftex-index-analyze-entry data) 986 attr (nth 2 analyze)) 987 (setf (nth 2 analyze) (if (string= attr bor) "" bor)) 988 (setq new (apply 'concat analyze)) 989 (reftex-index-change-entry 990 new (if (string= (nth 2 analyze) bor) 991 "Entry is now START-OF-PAGE-RANGE" 992 "START-OF-PAGE-RANGE canceled")))) 993 994(defun reftex-index-toggle-range-end () 995 "Toggle the page-range-end attribute `|)'." 996 (interactive) 997 (let* ((data (get-text-property (point) :data)) 998 (eor (concat (nth 1 reftex-index-special-chars) ")")) 999 new analyze attr) 1000 (unless data (error "Don't know which index entry to edit")) 1001 (setq analyze (reftex-index-analyze-entry data) 1002 attr (nth 2 analyze)) 1003 (setf (nth 2 analyze) (if (string= attr eor) "" eor)) 1004 (setq new (apply 'concat analyze)) 1005 (reftex-index-change-entry 1006 new (if (string= (nth 2 analyze) eor) 1007 "Entry is now END-OF-PAGE-RANGE" 1008 "END-OF-PAGE-RANGE canceled")))) 1009 1010(defun reftex-index-edit-key () 1011 "Edit the KEY part of the index entry." 1012 (interactive) 1013 (reftex-index-edit-part nil 1 "" "Key: " t)) 1014 1015(defun reftex-index-edit-attribute (&optional arg) 1016 "EDIT the ATTRIBUTE part of the entry. With arg: remove entire ATTRIBUTE." 1017 (interactive "P") 1018 (reftex-index-edit-part arg 2 (nth 1 reftex-index-special-chars) 1019 "Attribute: ")) 1020 1021(defun reftex-index-edit-visual (&optional arg) 1022 "EDIT the VISUAL part of the entry. With arg: remove entire VISUAL string." 1023 (interactive "P") 1024 (reftex-index-edit-part arg 3 (nth 2 reftex-index-special-chars) "Visual: ")) 1025 1026(defun reftex-index-edit-part (arg n initial prompt &optional dont-allow-empty) 1027 ;; This function does the work for all partial editing commands 1028 (let* ((data (get-text-property (point) :data)) 1029 new analyze opart npart) 1030 (unless data (error "Don't know which index entry to edit")) 1031 ;; Analyze the whole context string 1032 (setq analyze (reftex-index-analyze-entry data) 1033 opart (nth n analyze)) 1034 (and (> (length opart) 0) (setq opart (substring opart 1))) 1035 ;; Have the user editing the part 1036 (setq npart (if arg "" (read-string (concat prompt initial) opart))) 1037 ;; Tests: 1038 (cond ((string= npart opart) 1039 (error "Not changed")) 1040 ((string= npart "") 1041 (if dont-allow-empty 1042 (error "Invalid value") 1043 (setf (nth n analyze) npart))) 1044 (t (setf (nth n analyze) (concat initial npart)))) 1045 (setq new (apply 'concat analyze)) 1046 ;; Change the entry and insert the changed version into the index. 1047 (reftex-index-change-entry 1048 new (if (string= npart "") 1049 (format "Deleted: %s" opart) 1050 (format "New value is: %s" npart))))) 1051 1052(defun reftex-index-level-down () 1053 "Make index entry a subitem of another entry." 1054 (interactive) 1055 (let* ((data (get-text-property (point) :data)) 1056 (docstruct (symbol-value reftex-docstruct-symbol)) 1057 old new prefix key) 1058 (unless data (error "Don't know which index entry to change")) 1059 (setq old (nth 2 data) 1060 key (nth 6 data) 1061 prefix (completing-read 1062 "Prefix: " 1063 (reftex-sublist-nth 1064 docstruct 6 1065 (lambda (x) 1066 (and (eq (car x) 'index) 1067 (string= (nth 1 x) reftex-index-tag))) t))) 1068 (unless (string-match 1069 (concat (regexp-quote (car reftex-index-special-chars)) "\\'") 1070 prefix) 1071 (setq prefix (concat prefix (car reftex-index-special-chars)))) 1072 (if (string-match (regexp-quote key) old) 1073 (setq new (replace-match (concat prefix key) t t old)) 1074 (error "Cannot construct new index key")) 1075 (reftex-index-change-entry new (format "Added prefix: %s" prefix)))) 1076 1077(defun reftex-index-level-up () 1078 "Remove the highest level of a hierarchical index entry." 1079 (interactive) 1080 (let* ((data (get-text-property (point) :data)) 1081 old new prefix) 1082 (unless data (error "Don't know which entry to change")) 1083 (setq old (nth 2 data)) 1084 (if (string-match (concat "{\\([^" (nth 0 reftex-index-special-chars) "]*" 1085 "[^" (nth 3 reftex-index-special-chars) "]" 1086 (regexp-quote (nth 0 reftex-index-special-chars)) 1087 "\\)") 1088 old) 1089 (setq prefix (substring old (match-beginning 1) (match-end 1)) 1090 new (concat (substring old 0 (match-beginning 1)) 1091 (substring old (match-end 1)))) 1092 (error "Entry is not a subitem")) 1093 (reftex-index-change-entry new (format "Removed prefix: %s" prefix)))) 1094 1095(defun reftex-index-kill () 1096 "FIXME: Not yet implemented." 1097 (interactive) 1098 (error "This function is currently not implemented")) 1099 1100(defun reftex-index-undo () 1101 "FIXME: Not yet implemented." 1102 (interactive) 1103 (error "This function is currently not implemented")) 1104 1105(defun reftex-index-change-entry (new &optional message) 1106 ;; Change the full context string of the index entry at point to 1107 ;; NEW. This actually edits the buffer where the entry is defined. 1108 1109 (let* ((data (get-text-property (point) :data)) 1110 old beg end info) 1111 (unless data (error "Cannot change entry")) 1112 (reftex-index-view-entry) 1113 (setq beg (match-beginning 0) end (match-end 0)) 1114 (setq old (nth 2 data)) 1115 (and (equal old new) (error "Entry unchanged")) 1116 (with-current-buffer (get-file-buffer (nth 3 data)) 1117 (goto-char beg) 1118 (unless (looking-at (regexp-quote old)) 1119 (error "This should not happen (reftex-index-change-entry)")) 1120 (delete-region beg end) 1121 (insert new) 1122 (goto-char (1- beg)) 1123 (when (and (re-search-forward (reftex-everything-regexp) nil t) 1124 (match-end 10) 1125 (< (abs (- (match-beginning 10) beg)) (length new)) 1126 (setq info (reftex-index-info-safe buffer-file-name))) 1127 (setcdr data (cdr info)))) 1128 (let ((buffer-read-only nil)) 1129 (save-excursion 1130 (reftex-insert-index (list data) reftex-index-tag t 1131 "EDITED"))) 1132 (setq reftex-last-follow-point 1) 1133 (and message (message "%s" message)))) 1134 1135(defun reftex-index-goto-letter (char) 1136 "Go to the CHAR section in the index." 1137 (let ((pos (point)) 1138 (case-fold-search nil)) 1139 (goto-char (point-min)) 1140 (forward-line 2) 1141 (if (re-search-forward (concat "^" (char-to-string char)) nil t) 1142 (progn 1143 (beginning-of-line) 1144 (recenter 0) 1145 (reftex-index-next)) 1146 (goto-char pos) 1147 (if (eq char ?!) 1148 (error "This <%s> index does not contain entries sorted before the letters" 1149 reftex-index-tag) 1150 (error "This <%s> index does not contain entries starting with `%c'" 1151 reftex-index-tag char))))) 1152 1153 1154;;---------------------------------------------------------------------- 1155;; The Index Phrases File 1156 1157;; Some constants and variables 1158(defconst reftex-index-phrases-comment-regexp "^[ \t]*%.*" 1159 "Regular expression to match comment lines in phrases buffer") 1160(defconst reftex-index-phrases-macrodef-regexp 1161 "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\( *\t[ \t]*\\)\\([^\t]*[^ \t]\\)\\( *\t[ \t]*\\)\\(\\S-+\\)" 1162 "Regular expression to match macro definition lines the phrases buffer.") 1163;(defconst reftex-index-phrases-macrodef-regexp 1164; "^\\(>>>INDEX_MACRO_DEFINITION:\\)[ \t]+\\(\\S-\\)\\([ \t]*\\)\\([^\t]*[^ \t]\\)\\([ \t]*\\)\\(nil\\|t\\)[ \t]*$" 1165; "Regular expression to match macro definition lines the phrases buffer. 1166;This version would allow just spaces as separators.") 1167(defconst reftex-index-phrases-phrase-regexp1 1168 "^\\(\\S-?\\)\\(\t\\)\\([^\t\n]*\\S-\\)\\([ \t]*\\)$" 1169 "Regular expression matching phrases which have no separate index key.") 1170(defconst reftex-index-phrases-phrase-regexp2 1171 "^\\(\\S-?\\)\\(\t\\)\\([^\t]*\\S-\\)\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)[ \t]*$" 1172 "Regular expression matching phrases which have a separate index key.") 1173(defconst reftex-index-phrases-phrase-regexp12 1174 "^\\(\\S-?\\)\\(\t\\)\\([^\n\t]*\\S-\\)\\(\\(\t[ \t]*\\)\\([^\n\t]*\\S-\\)\\)?[ \t]*$" 1175 "Regular expression matching all types of phrase lines.") 1176(defvar reftex-index-phrases-macro-data nil 1177 "Alist containing the information taken from the macro definition lines. 1178This gets refreshed in every phrases command.") 1179(defvar reftex-index-phrases-files nil 1180 "List of document files relevant for the phrases file.") 1181 1182(defvar reftex-index-phrases-font-lock-keywords nil 1183 "Font lock keywords for reftex-index-phrases-mode.") 1184(defvar reftex-index-phrases-font-lock-defaults nil 1185 "Font lock defaults for reftex-index-phrases-mode.") 1186(define-obsolete-variable-alias 1187 'reftex-index-phrases-map 'reftex-index-phrases-mode-map "24.1") 1188(defvar reftex-index-phrases-mode-map 1189 (let ((map (make-sparse-keymap))) 1190 ;; Keybindings and Menu for phrases buffer 1191 (define-key map "\C-c\C-c" 'reftex-index-phrases-save-and-return) 1192 (define-key map "\C-c\C-x" 'reftex-index-this-phrase) 1193 (define-key map "\C-c\C-f" 'reftex-index-next-phrase) 1194 (define-key map "\C-c\C-r" 'reftex-index-region-phrases) 1195 (define-key map "\C-c\C-a" 'reftex-index-all-phrases) 1196 (define-key map "\C-c\C-d" 'reftex-index-remaining-phrases) 1197 (define-key map "\C-c\C-s" 'reftex-index-sort-phrases) 1198 (define-key map "\C-c\C-n" 'reftex-index-new-phrase) 1199 (define-key map "\C-c\C-m" 'reftex-index-phrases-set-macro-key) 1200 (define-key map "\C-c\C-i" 'reftex-index-phrases-info) 1201 (define-key map "\C-c\C-t" 'reftex-index-find-next-conflict-phrase) 1202 (define-key map "\C-i" 'self-insert-command) 1203 1204 (easy-menu-define reftex-index-phrases-menu map 1205 "Menu for Phrases buffer" 1206 '("Phrases" 1207 ["New Phrase" reftex-index-new-phrase t] 1208 ["Set Phrase Macro" reftex-index-phrases-set-macro-key t] 1209 ["Recreate File Header" reftex-index-initialize-phrases-buffer t] 1210 "--" 1211 ("Sort Phrases" 1212 ["Sort" reftex-index-sort-phrases t] 1213 "--" 1214 "Sort Options" 1215 ["by Search Phrase" (setq reftex-index-phrases-sort-prefers-entry nil) 1216 :style radio :selected (not reftex-index-phrases-sort-prefers-entry)] 1217 ["by Index Entry" (setq reftex-index-phrases-sort-prefers-entry t) 1218 :style radio :selected reftex-index-phrases-sort-prefers-entry] 1219 ["in Blocks" (setq reftex-index-phrases-sort-in-blocks 1220 (not reftex-index-phrases-sort-in-blocks)) 1221 :style toggle :selected reftex-index-phrases-sort-in-blocks]) 1222 ["Describe Phrase" reftex-index-phrases-info t] 1223 ["Next Phrase Conflict" reftex-index-find-next-conflict-phrase t] 1224 "--" 1225 ("Find and Index in Document" 1226 ["Current Phrase" reftex-index-this-phrase t] 1227 ["Next Phrase" reftex-index-next-phrase t] 1228 ["Current and Following" reftex-index-remaining-phrases t] 1229 ["Region Phrases" reftex-index-region-phrases t] 1230 ["All Phrases" reftex-index-all-phrases t] 1231 "--" 1232 "Options" 1233 ["Match Whole Words" (setq reftex-index-phrases-search-whole-words 1234 (not reftex-index-phrases-search-whole-words)) 1235 :style toggle :selected reftex-index-phrases-search-whole-words] 1236 ["Case Sensitive Search" (setq reftex-index-phrases-case-fold-search 1237 (not reftex-index-phrases-case-fold-search)) 1238 :style toggle :selected (not 1239 reftex-index-phrases-case-fold-search)] 1240 ["Wrap Long Lines" (setq reftex-index-phrases-wrap-long-lines 1241 (not reftex-index-phrases-wrap-long-lines)) 1242 :style toggle :selected reftex-index-phrases-wrap-long-lines] 1243 ["Skip Indexed Matches" (setq reftex-index-phrases-skip-indexed-matches 1244 (not reftex-index-phrases-skip-indexed-matches)) 1245 :style toggle :selected reftex-index-phrases-skip-indexed-matches]) 1246 "--" 1247 ["Save and Return" reftex-index-phrases-save-and-return t])) 1248 1249 map) 1250 "Keymap used for index phrases buffer.") 1251(defvar reftex-index-phrases-syntax-table 1252 (let ((table (make-syntax-table))) 1253 (modify-syntax-entry ?\" "." table) 1254 table) 1255 "Syntax table for RefTeX Index Phrases mode.") 1256 1257;;;###autoload 1258(defun reftex-index-phrase-selection-or-word (arg) 1259 "Add current selection or word at point to the phrases buffer. 1260When you are in transient-mark-mode and the region is active, the 1261selection will be used - otherwise the word at point. 1262You get a chance to edit the entry in the phrases buffer - finish with 1263`C-c C-c'." 1264 (interactive "P") 1265 (set-marker reftex-index-return-marker (point)) 1266 (reftex-index-selection-or-word arg 'phrase) 1267 (if (eq major-mode 'reftex-index-phrases-mode) 1268 (message "%s" 1269 (substitute-command-keys 1270 "Return to LaTeX with \\[reftex-index-phrases-save-and-return]")))) 1271 1272;;;###autoload 1273(defun reftex-index-visit-phrases-buffer () 1274 "Switch to the phrases buffer, initialize if empty." 1275 (interactive) 1276 (reftex-access-scan-info) 1277 (set-marker reftex-index-return-marker (point)) 1278 (let* ((master (reftex-TeX-master-file)) 1279 (name (concat (file-name-sans-extension master) 1280 reftex-index-phrase-file-extension))) 1281 (find-file name) 1282 (unless (eq major-mode 'reftex-index-phrases-mode) 1283 (reftex-index-phrases-mode)) 1284 (if (= (buffer-size) 0) 1285 (reftex-index-initialize-phrases-buffer master)))) 1286 1287(defun reftex-index-initialize-phrases-buffer (&optional master) 1288 "Initialize the phrases buffer by creating the header. 1289If the buffer is non-empty, delete the old header first." 1290 (interactive) 1291 (let* ((case-fold-search t) 1292 (default-key (car reftex-index-default-macro)) 1293 (default-macro (nth 1 (assoc default-key 1294 reftex-key-to-index-macro-alist))) 1295 (macro-alist 1296 (sort (copy-sequence reftex-index-macro-alist) 1297 (lambda (a b) (equal (car a) default-macro)))) 1298 macro entry key repeat) 1299 1300 (if master (set (make-local-variable 'TeX-master) 1301 (file-name-nondirectory master))) 1302 1303 (when (> (buffer-size) 0) 1304 (goto-char 1) 1305 (set-mark (point)) 1306 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t) 1307 (end-of-line)) 1308 (beginning-of-line 2) 1309 (if (looking-at reftex-index-phrases-comment-regexp) 1310 (beginning-of-line 2)) 1311 (while (looking-at "^[ \t]*$") 1312 (beginning-of-line 2)) 1313 (if (featurep 'xemacs) 1314 (zmacs-activate-region) 1315 (setq mark-active t)) 1316 (if (yes-or-no-p "Delete and rebuild header? ") 1317 (delete-region (point-min) (point)))) 1318 1319 ;; Insert the mode line 1320 (insert 1321 (format "%% -*- mode: reftex-index-phrases; TeX-master: \"%s\" -*-\n" 1322 (file-name-nondirectory (reftex-index-phrase-tex-master)))) 1323 ;; Insert the macro definitions 1324 (insert "% Key Macro Format Repeat\n") 1325 (insert "%---------------------------------------------------------------------\n") 1326 (while (setq entry (pop macro-alist)) 1327 (setq macro (car entry) 1328 repeat (nth 7 entry) 1329 key (car (delq nil (mapcar (lambda (x) (if (equal (nth 1 x) macro) 1330 (car x) 1331 nil)) 1332 reftex-key-to-index-macro-alist)))) 1333 (insert (format ">>>INDEX_MACRO_DEFINITION:\t%s\t%-20s\t%s\n" 1334 (char-to-string key) (concat macro "{%s}") 1335 (if repeat "t" "nil")))) 1336 (insert "%---------------------------------------------------------------------\n\n\n"))) 1337 1338(defvar TeX-master) 1339(defun reftex-index-phrase-tex-master (&optional dir) 1340 "Return the name of the master file associated with a phrase buffer." 1341 (if (and (boundp 'TeX-master) 1342 (local-variable-p 'TeX-master (current-buffer)) 1343 (stringp TeX-master)) 1344 ;; We have a local variable which tells us which file to use 1345 (expand-file-name TeX-master dir) 1346 ;; have to guess 1347 (concat (file-name-sans-extension (buffer-file-name)) ".tex"))) 1348 1349(defun reftex-index-phrases-save-and-return () 1350 "Return to where the `reftex-index-phrase-selection-or-word' was called." 1351 (interactive) 1352 (save-buffer) 1353 (switch-to-buffer (marker-buffer reftex-index-return-marker)) 1354 (goto-char (or (marker-position reftex-index-return-marker) (point)))) 1355 1356 1357(defvar reftex-index-phrases-menu) 1358(defvar reftex-index-phrases-marker) 1359(defvar reftex-index-phrases-restrict-file nil) 1360;; NB this is a global autoload - see reftex.el. 1361;;;###autoload 1362(define-derived-mode reftex-index-phrases-mode fundamental-mode "Phrases" 1363 "Major mode for managing the Index phrases of a LaTeX document. 1364This buffer was created with RefTeX. 1365 1366To insert new phrases, use 1367 - `C-c \\' in the LaTeX document to copy selection or word 1368 - `\\[reftex-index-new-phrase]' in the phrases buffer. 1369 1370To index phrases use one of: 1371 1372\\[reftex-index-this-phrase] index current phrase 1373\\[reftex-index-next-phrase] index next phrase (or N with prefix arg) 1374\\[reftex-index-all-phrases] index all phrases 1375\\[reftex-index-remaining-phrases] index current and following phrases 1376\\[reftex-index-region-phrases] index the phrases in the region 1377 1378You can sort the phrases in this buffer with \\[reftex-index-sort-phrases]. 1379To display information about the phrase at point, use \\[reftex-index-phrases-info]. 1380 1381For more information see the RefTeX User Manual. 1382 1383Here are all local bindings. 1384 1385\\{reftex-index-phrases-mode-map}" 1386 :syntax-table reftex-index-phrases-syntax-table 1387 (set (make-local-variable 'font-lock-defaults) 1388 reftex-index-phrases-font-lock-defaults) 1389 (easy-menu-add reftex-index-phrases-menu reftex-index-phrases-mode-map) 1390 (set (make-local-variable 'reftex-index-phrases-marker) (make-marker))) 1391;; (add-hook 'reftex-index-phrases-mode-hook 'turn-on-font-lock) 1392 1393;; Font Locking stuff 1394(let ((ss (if (featurep 'xemacs) 'secondary-selection ''secondary-selection))) 1395 (setq reftex-index-phrases-font-lock-keywords 1396 (list 1397 (cons reftex-index-phrases-comment-regexp 'font-lock-comment-face) 1398 (list reftex-index-phrases-macrodef-regexp 1399 '(1 font-lock-type-face) 1400 '(2 font-lock-keyword-face) 1401 (list 3 ss) 1402 '(4 font-lock-function-name-face) 1403 (list 5 ss) 1404 '(6 font-lock-string-face)) 1405 (list reftex-index-phrases-phrase-regexp1 1406 '(1 font-lock-keyword-face) 1407 (list 2 ss) 1408 '(3 font-lock-string-face) 1409 (list 4 ss)) 1410 (list reftex-index-phrases-phrase-regexp2 1411 '(1 font-lock-keyword-face) 1412 (list 2 ss) 1413 '(3 font-lock-string-face) 1414 (list 4 ss) 1415 '(5 font-lock-function-name-face)) 1416 (cons "^\t$" ss))) 1417 (setq reftex-index-phrases-font-lock-defaults 1418 '((reftex-index-phrases-font-lock-keywords) 1419 nil t nil beginning-of-line)) 1420 (put 'reftex-index-phrases-mode 'font-lock-defaults 1421 reftex-index-phrases-font-lock-defaults) ; XEmacs 1422 ) 1423 1424(defun reftex-index-next-phrase (&optional arg) 1425 "Index the next ARG phrases in the phrases buffer." 1426 (interactive "p") 1427 (reftex-index-phrases-parse-header t) 1428 (while (> arg 0) 1429 (cl-decf arg) 1430 (end-of-line) 1431 (if (re-search-forward reftex-index-phrases-phrase-regexp12 nil t) 1432 (progn 1433 (goto-char (match-beginning 0)) 1434 (reftex-index-this-phrase 'slave)) 1435 (error "No more phrase lines after point")))) 1436 1437(defun reftex-index-this-phrase (&optional slave) 1438 "Index the phrase in the current line. 1439Does a global search and replace in the entire document. At each 1440match, the user will be asked to confirm the replacement." 1441 (interactive) 1442 (if (not slave) (reftex-index-phrases-parse-header t)) 1443 (save-excursion 1444 (beginning-of-line) 1445 (cond ((looking-at reftex-index-phrases-comment-regexp) 1446 (if (not slave) (error "Comment line"))) 1447 ((looking-at "^[ \t]*$") 1448 (if (not slave) (error "Empty line"))) 1449 ((looking-at reftex-index-phrases-macrodef-regexp) 1450 (if (not slave) (error "Macro definition line"))) 1451 ((looking-at reftex-index-phrases-phrase-regexp12) 1452 ;; This is a phrase 1453 (let* ((char (if (not (equal (match-string 1) "")) 1454 (string-to-char (match-string 1)))) 1455 (phrase (match-string 3)) 1456 (index-key (match-string 6)) 1457 (macro-data (cdr (if (null char) 1458 (car reftex-index-phrases-macro-data) 1459 (assoc char reftex-index-phrases-macro-data)))) 1460 (macro-fmt (car macro-data)) 1461 (repeat (nth 1 macro-data)) 1462 (files 1463 (cond ((and (stringp reftex-index-phrases-restrict-file) 1464 (file-regular-p reftex-index-phrases-restrict-file)) 1465 (list reftex-index-phrases-restrict-file)) 1466 ((stringp reftex-index-phrases-restrict-file) 1467 (error "Invalid restriction file %s" 1468 reftex-index-phrases-restrict-file)) 1469 (t reftex-index-phrases-files))) 1470 (as-words reftex-index-phrases-search-whole-words)) 1471 (unless macro-data 1472 (error "No macro associated with key %c" char)) 1473 (unwind-protect 1474 (let ((overlay-arrow-string "=>") 1475 (overlay-arrow-position 1476 reftex-index-phrases-marker) 1477 (replace-count 0)) 1478 ;; Show the overlay arrow 1479 (move-marker reftex-index-phrases-marker 1480 (match-beginning 0) (current-buffer)) 1481 ;; Start the query-replace 1482 (reftex-query-index-phrase-globally 1483 files phrase macro-fmt 1484 index-key repeat as-words) 1485 (message "%s replaced" 1486 (reftex-number replace-count "occurrence")))))) 1487 (t (error "Cannot parse this line"))))) 1488 1489(defun reftex-index-all-phrases () 1490 "Index all phrases in the phrases buffer. 1491Calls `reftex-index-this-phrase' on each line in the buffer." 1492 (interactive) 1493 (reftex-index-region-phrases (point-min) (point-max))) 1494 1495(defun reftex-index-remaining-phrases () 1496 "Index all phrases in the phrases buffer. 1497Calls `reftex-index-this-phrase' on each line ay and below point in 1498the buffer." 1499 (interactive) 1500 (beginning-of-line) 1501 (reftex-index-region-phrases (point) (point-max))) 1502 1503(defun reftex-index-region-phrases (beg end) 1504 "Index all phrases in the phrases buffer. 1505Calls `reftex-index-this-phrase' on each line in the region." 1506 (interactive "r") 1507 (reftex-index-phrases-parse-header t) 1508 (goto-char beg) 1509 (while (not (or (eobp) 1510 (>= (point) end))) 1511 (save-excursion (reftex-index-this-phrase 'slave)) 1512 (beginning-of-line 2))) 1513 1514(defun reftex-index-phrases-parse-header (&optional get-files) 1515 "Parse the header of a phrases file to extract the macro definitions. 1516The definitions get stored in `reftex-index-phrases-macro-data'. 1517Also switches to the LaTeX document to find out which files belong to 1518the document and stores the list in `reftex-index-phrases-files'." 1519 (let* ((master (reftex-index-phrase-tex-master)) 1520 buf) 1521 (if get-files 1522 ;; Get the file list 1523 (save-excursion 1524 (setq buf (reftex-get-file-buffer-force master)) 1525 (unless buf (error "Master file %s not found" master)) 1526 (set-buffer buf) 1527 (reftex-access-scan-info) 1528 (setq reftex-index-phrases-files 1529 (reftex-all-document-files)))) 1530 ;; Parse the files header for macro definitions 1531 (setq reftex-index-phrases-macro-data nil) 1532 (save-excursion 1533 (goto-char (point-min)) 1534 (while (re-search-forward reftex-index-phrases-macrodef-regexp nil t) 1535 (push (list 1536 (string-to-char (match-string 2)) 1537 (match-string 4) 1538 (equal (match-string 6) "t")) 1539 reftex-index-phrases-macro-data)) 1540 ;; Reverse the list, so that the first macro is first 1541 (if (null reftex-index-phrases-macro-data) 1542 (error "No valid MACRO DEFINITION line in %s file (make sure to use TAB separators)" reftex-index-phrase-file-extension)) 1543 (setq reftex-index-phrases-macro-data 1544 (nreverse reftex-index-phrases-macro-data)) 1545 (goto-char (point-min))))) 1546 1547(defun reftex-index-phrases-apply-to-region (beg end) 1548 "Index all index phrases in the current region. 1549This works exactly like global indexing from the index phrases buffer, 1550but operation is restricted to the current region. This is useful if 1551you need to add/change text in an already indexed document and want to 1552index the new part without having to go over the unchanged parts again." 1553 (interactive "r") 1554 (let ((win-conf (current-window-configuration)) 1555 (reftex-index-phrases-restrict-file (buffer-file-name))) 1556 (save-excursion 1557 (save-restriction 1558 (narrow-to-region beg end) 1559 (unwind-protect 1560 (progn 1561 ;; Hide the region highlighting 1562 (if (featurep 'xemacs) 1563 (zmacs-deactivate-region) 1564 (deactivate-mark)) 1565 (delete-other-windows) 1566 (reftex-index-visit-phrases-buffer) 1567 (reftex-index-all-phrases)) 1568 (set-window-configuration win-conf)))))) 1569 1570(defun reftex-index-new-phrase (&optional text) 1571 "Open a new line in the phrases buffer, insert TEXT." 1572 (interactive) 1573 (if (and text (stringp text)) 1574 (progn 1575 ;; Check if the phrase is already in the buffer 1576 (setq text (reftex-index-simplify-phrase text)) 1577 (goto-char (point-min)) 1578 (if (re-search-forward 1579 (concat "^\\(\\S-*\\)\t\\(" (regexp-quote text) 1580 "\\) *[\t\n]") nil t) 1581 (progn 1582 (goto-char (match-end 2)) 1583 (error "Phrase is already in phrases buffer"))))) 1584 ;; Add the new phrase line after the last in the buffer 1585 (goto-char (point-max)) 1586 (if (re-search-backward reftex-index-phrases-phrase-regexp12 nil t) 1587 (end-of-line)) 1588 (if (not (bolp)) 1589 (insert "\n")) 1590 (insert "\t") 1591 (if (and text (stringp text)) 1592 (insert text))) 1593 1594(defun reftex-index-find-next-conflict-phrase (&optional arg) 1595 "Find the next a phrase which is has conflicts in the phrase buffer. 1596The command helps to find possible conflicts in the phrase indexing process. 1597It searches downward from point for a phrase which is repeated elsewhere 1598in the buffer, or which is a subphrase of another phrase. If such a 1599phrase is found, the phrase info is displayed. 1600To check the whole buffer, start at the beginning and continue by calling 1601this function repeatedly." 1602 (interactive "P") 1603 (if (catch 'exit 1604 (while (re-search-forward reftex-index-phrases-phrase-regexp12 nil t) 1605 (goto-char (match-beginning 3)) 1606 (let* ((phrase (match-string 3)) 1607 (case-fold-search reftex-index-phrases-case-fold-search) 1608 (re (reftex-index-phrases-find-dup-re phrase t))) 1609 (if (save-excursion 1610 (goto-char (point-min)) 1611 (and (re-search-forward re nil t) 1612 (re-search-forward re nil t))) 1613 (throw 'exit t))))) 1614 (progn 1615 (reftex-index-phrases-info) 1616 (message "Phrase with match conflict discovered")) 1617 (goto-char (point-max)) 1618 (error "No further problematic phrases found"))) 1619 1620(defun reftex-index-phrases-info () 1621 "Display information about the phrase at point." 1622 (interactive) 1623 (save-excursion 1624 (beginning-of-line) 1625 (unless (looking-at reftex-index-phrases-phrase-regexp12) 1626 (error "Not a phrase line")) 1627 (save-match-data (reftex-index-phrases-parse-header t)) 1628 (let* ((char (if (not (equal (match-string 1) "")) 1629 (string-to-char (match-string 1)))) 1630 (phrase (match-string 3)) 1631 (index-key (match-string 6)) 1632 (index-keys (split-string 1633 (or index-key phrase) 1634 reftex-index-phrases-logical-or-regexp)) 1635 (macro-data (cdr (if (null char) 1636 (car reftex-index-phrases-macro-data) 1637 (assoc char reftex-index-phrases-macro-data)))) 1638 (macro-fmt (car macro-data)) 1639 (repeat (nth 1 macro-data)) 1640 (as-words reftex-index-phrases-search-whole-words) 1641 (example (reftex-index-make-replace-string 1642 macro-fmt (downcase phrase) (car index-keys) repeat)) 1643 (re (reftex-index-make-phrase-regexp phrase as-words t)) 1644 (re1 (reftex-index-phrases-find-dup-re phrase)) 1645 (re2 (reftex-index-phrases-find-dup-re phrase 'sub)) 1646 superphrases 1647 (nmatches 0) 1648 (ntimes1 0) 1649 (ntimes2 0) 1650 (case-fold-search reftex-index-phrases-case-fold-search) 1651 file files buf) 1652 (setq files reftex-index-phrases-files) 1653 (save-excursion 1654 (save-restriction 1655 (widen) 1656 (goto-char (point-min)) 1657 (while (re-search-forward re1 nil t) 1658 (cl-incf ntimes1)) 1659 (goto-char (point-min)) 1660 (while (re-search-forward re2 nil t) 1661 (push (cons (count-lines 1 (point)) (match-string 1)) superphrases) 1662 (cl-incf ntimes2)))) 1663 (save-current-buffer 1664 (while (setq file (pop files)) 1665 (setq buf (reftex-get-file-buffer-force file)) 1666 (when buf 1667 (set-buffer buf) 1668 (save-excursion 1669 (save-restriction 1670 (widen) 1671 (goto-char (point-min)) 1672 (let ((case-fold-search reftex-index-phrases-case-fold-search)) 1673 (while (re-search-forward re nil t) 1674 (or (reftex-in-comment) 1675 (cl-incf nmatches))))))))) 1676 (with-output-to-temp-buffer "*Help*" 1677 (princ (format " Phrase: %s\n" phrase)) 1678 (princ (format " Macro key: %s\n" char)) 1679 (princ (format " Macro format: %s\n" macro-fmt)) 1680 (princ (format " Repeat: %s\n" repeat)) 1681 (cond 1682 (index-key 1683 (let ((iks index-keys) (cnt 0) ik) 1684 (while (setq ik (pop iks)) 1685 (princ (format "Index entry %d: %s\n" (cl-incf cnt) ik))))) 1686 (repeat 1687 (princ (format " Index entry: %s\n" phrase))) 1688 (t 1689 (princ (format " Index key: <<Given by the match>>\n")))) 1690 (princ (format " Example: %s\n" example)) 1691 (terpri) 1692 (princ (format "Total matches: %s in %s\n" 1693 (reftex-number nmatches "match" "es") 1694 (reftex-number (length reftex-index-phrases-files) 1695 "LaTeX file"))) 1696 (princ (format " Uniqueness: Phrase occurs %s in phrase buffer\n" 1697 (reftex-number ntimes1 "time"))) 1698 (if (> ntimes2 1) 1699 (progn 1700 (princ (format " Superphrases: Phrase matches the following %s in the phrase buffer:\n" 1701 (reftex-number ntimes2 "line"))) 1702 (mapcar (lambda(x) 1703 (princ (format " Line %4d: %s\n" (car x) (cdr x)))) 1704 (nreverse superphrases)))))))) 1705 1706(defun reftex-index-phrases-set-macro-key () 1707 "Change the macro key for the current line. 1708Prompts for a macro key and insert is at the beginning of the line. 1709If you reply with SPACE, the macro keyn will be removed, so that the 1710default macro will be used. If you reply with `RET', just prints 1711information about the currently selected macro." 1712 (interactive) 1713 (reftex-index-phrases-parse-header) 1714 (save-excursion 1715 (beginning-of-line) 1716 (unless (or (looking-at reftex-index-phrases-phrase-regexp12) 1717 (looking-at "\t")) 1718 (error "This is not a phrase line")) 1719 (let* ((nc (reftex-index-select-phrases-macro 0)) 1720 (macro-data (assoc nc reftex-index-phrases-macro-data)) 1721 macro-fmt repeat) 1722 (cond (macro-data) 1723 ((equal nc ?\ ) 1724 (setq nc "" 1725 macro-data (car reftex-index-phrases-macro-data))) 1726 ((equal nc ?\C-m) 1727 (setq nc (char-after (point))) 1728 (if (equal nc ?\t) 1729 (setq nc "" 1730 macro-data (car reftex-index-phrases-macro-data)) 1731 (setq macro-data (assoc nc reftex-index-phrases-macro-data)))) 1732 (t (error "No macro associated with %c" nc))) 1733 1734 (setq macro-fmt (nth 1 macro-data) 1735 repeat (nth 2 macro-data)) 1736 (if macro-data 1737 (progn 1738 (if (looking-at "[^\t]") (delete-char 1)) 1739 (insert nc) 1740 (message "Line will use %s %s repeat" macro-fmt 1741 (if repeat "with" "without"))) 1742 (error "Abort"))))) 1743 1744(defun reftex-index-sort-phrases (&optional chars-first) 1745 "Sort the phrases lines in the buffer alphabetically. 1746Normally, this looks only at the phrases. With a prefix arg CHARS-FIRST, 1747it first compares the macro identifying chars and then the phrases." 1748 (interactive "P") 1749 ;; Remember the current line, so that we can return 1750 (let ((line (buffer-substring (progn (beginning-of-line) (point)) 1751 (progn (end-of-line) (point)))) 1752 beg end) 1753 (goto-char (point-min)) 1754 ;; Find first and last phrase line in buffer 1755 (setq beg 1756 (and (re-search-forward reftex-index-phrases-phrase-regexp12 nil t) 1757 (match-beginning 0))) 1758 (goto-char (point-max)) 1759 (setq end (re-search-backward reftex-index-phrases-phrase-regexp12 nil t)) 1760 (if end (setq end (progn (goto-char end) (end-of-line) (point)))) 1761 ;; Take the lines, sort them and re-insert. 1762 (if (and beg end) 1763 (progn 1764 (message "Sorting lines...") 1765 (let* ((lines (split-string (buffer-substring beg end) "\n")) 1766 (lines1 (sort lines 'reftex-compare-phrase-lines))) 1767 (message "Sorting lines...done") 1768 (let ((inhibit-quit t)) ;; make sure we do not lose lines 1769 (delete-region beg end) 1770 (insert (mapconcat 'identity lines1 "\n")))) 1771 (goto-char (point-max)) 1772 (re-search-backward (concat "^" (regexp-quote line) "$") nil t)) 1773 (error "Cannot find phrases lines to sort")))) 1774 1775(defvar chars-first) 1776(defun reftex-compare-phrase-lines (a b) 1777 "The comparison function used for sorting." 1778 (let (ca cb pa pb c-p p-p) 1779 (if (string-match reftex-index-phrases-phrase-regexp12 a) 1780 (progn 1781 ;; Extract macro char and phrase-or-key for a 1782 (setq ca (match-string 1 a) 1783 pa (downcase 1784 (or (and reftex-index-phrases-sort-prefers-entry 1785 (match-string 6 a)) 1786 (match-string 3 a)))) 1787 (if (string-match reftex-index-phrases-phrase-regexp12 b) 1788 (progn 1789 ;; Extract macro char and phrase-or-key for b 1790 (setq cb (match-string 1 b) 1791 pb (downcase 1792 (or (and reftex-index-phrases-sort-prefers-entry 1793 (match-string 6 b)) 1794 (match-string 3 b)))) 1795 (setq c-p (string< ca cb) 1796 p-p (string< pa pb)) 1797 ;; Do the right comparison, based on the value of `chars-first' 1798 ;; `chars-first' is bound locally in the calling function 1799 (if chars-first 1800 (if (string= ca cb) p-p c-p) 1801 (if (string= pa pb) c-p p-p))))) 1802 ;; If line a does not match, the answer we return determines 1803 ;; if non-matching lines are collected at the beginning. 1804 ;; When we return t here, non-matching lines form 1805 ;; block separators for searches. 1806 (not reftex-index-phrases-sort-in-blocks)))) 1807 1808(defvar reftex-index-phrases-menu) 1809(defun reftex-index-make-phrase-regexp (phrase &optional 1810 as-words allow-newline) 1811 "Return a regexp matching PHRASE, even if distributed over lines. 1812With optional arg AS-WORDS, require word boundary at beginning and end. 1813With optional arg ALLOW-NEWLINE, allow single newline between words." 1814 (let* ((words (split-string phrase)) 1815 (space-re (if allow-newline 1816 "\\([ \t]*\\(\n[ \t]*\\)?\\|[ \t]\\)" 1817 "\\([ \t]+\\)"))) 1818 (concat (if (and as-words (string-match "\\`\\w" (car words))) 1819 "\\(\\<\\|[`']\\)" "") 1820 (mapconcat (lambda (w) (regexp-quote 1821 (if reftex-index-phrases-case-fold-search 1822 (downcase w) 1823 w))) 1824 words space-re) 1825 (if (and as-words 1826 (string-match "\\w\\'" (nth (1- (length words)) words))) 1827 "\\(\\>\\|'\\)" "")))) 1828 1829(defun reftex-index-simplify-phrase (phrase) 1830 "Make phrase single spaces and single line." 1831 (mapconcat 'identity (split-string phrase) " ")) 1832 1833(defun reftex-index-phrases-find-dup-re (phrase &optional sub) 1834 "Return a regexp which matches variations of PHRASE (with additional space). 1835When SUB ins non-nil, the regexp will also match when PHRASE is a subphrase 1836of another phrase. The regexp works lonly in the phrase buffer." 1837 (concat (if sub "^\\S-?\t\\([^\t\n]*" "^\\S-?\t") 1838 (mapconcat 'regexp-quote (split-string phrase) " +") 1839 (if sub "[^\t\n]*\\)\\([\t\n]\\|$\\)" " *\\([\t\n]\\|$\\)"))) 1840 1841(defun reftex-index-make-replace-string (macro-fmt match index-key 1842 &optional repeat mathp) 1843 "Return the string which can be used as replacement. 1844Treats the logical `and' for index phrases." 1845 (let ((index-keys (split-string (or index-key match) 1846 reftex-index-phrases-logical-and-regexp))) 1847 (concat 1848 (mapconcat (lambda (x) 1849 (format macro-fmt 1850 (format (if mathp reftex-index-math-format "%s") x))) 1851 index-keys "") 1852 (if repeat (reftex-index-simplify-phrase match) "")))) 1853 1854(defun reftex-query-index-phrase-globally (files &rest args) 1855 "Call `reftex-query-index-phrase' for all files in FILES." 1856 (let ((win-conf (current-window-configuration)) 1857 (file)) 1858 (unless files (error "No files")) 1859 (unwind-protect 1860 (progn 1861 (switch-to-buffer-other-window (reftex-get-file-buffer-force 1862 (car files))) 1863 (catch 'no-more-files 1864 (while (setq file (pop files)) 1865 (switch-to-buffer (reftex-get-file-buffer-force file)) 1866 (save-excursion 1867 (save-restriction 1868 (unless (stringp reftex-index-phrases-restrict-file) 1869 (widen)) 1870 (goto-char (point-min)) 1871 (apply 'reftex-query-index-phrase args)))))) 1872 (reftex-unhighlight 0) 1873 (set-window-configuration win-conf)))) 1874 1875(defconst reftex-index-phrases-help 1876 " Keys for query-index search 1877 =========================== 1878y Replace this match 1879n Skip this match 1880! Replace this and all further matches in this file 1881q / Q Skip match, start next file / start next phrase 1882o Use a different indexing macro for this match 18831 - 9 Select one of the multiple phrases 1884e Edit the replacement text 1885C-r Recursive edit. 1886s / S Save this buffer / Save all document buffers 1887C-g Abort" 1888 "The help string for indexing phrases.") 1889 1890(defvar replace-count) 1891(defun reftex-query-index-phrase (phrase macro-fmt &optional 1892 index-key repeat as-words) 1893 "Search through buffer for PHRASE, and offer to replace it with an indexed 1894version. The index version is derived by applying `format' with MACRO-FMT 1895to INDEX-KEY or PHRASE. When REPEAT is non-nil, the PHRASE is inserted 1896again after the macro. 1897AS-WORDS means, the search for PHRASE should require word boundaries at 1898both ends." 1899 (let* ((re (reftex-index-make-phrase-regexp phrase as-words 'allow-newline)) 1900 (case-fold-search reftex-index-phrases-case-fold-search) 1901 (index-keys (split-string 1902 (or index-key phrase) 1903 reftex-index-phrases-logical-or-regexp)) 1904 (nkeys (length index-keys)) 1905 (ckey (nth 0 index-keys)) 1906 (all-yes nil) 1907 match rpl char (beg (make-marker)) (end (make-marker)) mathp) 1908 (move-marker beg 1) 1909 (move-marker end 1) 1910 (unwind-protect 1911 (while (re-search-forward re nil t) 1912 (catch 'next-match 1913 (if (reftex-in-comment) 1914 (throw 'next-match nil)) 1915 (if (and (fboundp reftex-index-verify-function) 1916 (not (funcall reftex-index-verify-function))) 1917 (throw 'next-match nil)) 1918 (setq match (match-string 0)) 1919 (setq mathp 1920 (save-match-data 1921 (condition-case nil (texmathp) (error nil)))) 1922 (setq beg (move-marker beg (match-beginning 0)) 1923 end (move-marker end (match-end 0))) 1924 (if (and reftex-index-phrases-skip-indexed-matches 1925 (save-match-data 1926 (reftex-index-phrase-match-is-indexed beg 1927 end))) 1928 (throw 'next-match nil)) 1929 (reftex-highlight 0 (match-beginning 0) (match-end 0)) 1930 (setq rpl 1931 (save-match-data 1932 (reftex-index-make-replace-string 1933 macro-fmt (match-string 0) ckey repeat mathp))) 1934 (while 1935 (not 1936 (catch 'loop 1937 (message "REPLACE: %s? (yn!qoe%s?)" 1938 rpl 1939 (if (> nkeys 1) 1940 (concat "1-" (int-to-string nkeys)) 1941 "")) 1942 (setq char (if all-yes ?y (read-char-exclusive))) 1943 (cond ((member char '(?y ?Y ?\ )) 1944 ;; Yes! 1945 (replace-match rpl t t) 1946 (cl-incf replace-count) 1947 ;; See if we should insert newlines to shorten lines 1948 (and reftex-index-phrases-wrap-long-lines 1949 (reftex-index-phrases-fixup-line beg end)) 1950 (throw 'loop t)) 1951 ((member char '(?n ?N ?\C-h ?\C-?));; FIXME: DEL 1952 ;; No 1953 (throw 'loop t)) 1954 ((equal char ?!) 1955 ;; Yes for all in this buffer 1956 (setq all-yes t)) 1957 ((equal char ?q) 1958 ;; Stop this one in this file 1959 (goto-char (point-max)) 1960 (throw 'loop t)) 1961 ((equal char ?Q) 1962 ;; Stop this one 1963 (throw 'no-more-files t)) 1964 ((equal char ?s) 1965 (save-buffer)) 1966 ((equal char ?S) 1967 (reftex-save-all-document-buffers)) 1968 ((equal char ?\C-g) 1969 (keyboard-quit)) 1970 ((member char '(?o ?O)) 1971 ;; Select a different macro 1972 (let* ((nc (reftex-index-select-phrases-macro 2)) 1973 (macro-data 1974 (cdr (assoc nc reftex-index-phrases-macro-data))) 1975 (macro-fmt (car macro-data)) 1976 (repeat (nth 1 macro-data))) 1977 (if macro-data 1978 (setq rpl (save-match-data 1979 (reftex-index-make-replace-string 1980 macro-fmt match 1981 ckey repeat mathp))) 1982 (ding)))) 1983 ((equal char ?\?) 1984 ;; Help 1985 (with-output-to-temp-buffer "*Help*" 1986 (princ reftex-index-phrases-help))) 1987 ((equal char ?\C-r) 1988 ;; Recursive edit 1989 (save-match-data 1990 (save-excursion 1991 (message "%s" 1992 (substitute-command-keys 1993 "Recursive edit. Resume with \\[exit-recursive-edit]")) 1994 (recursive-edit)))) 1995 ((equal char ?e) 1996 (setq rpl (read-string "Edit: " rpl))) 1997 ((equal char ?0) 1998 (setq ckey (or index-key phrase) 1999 rpl (save-match-data 2000 (reftex-index-make-replace-string 2001 macro-fmt match ckey repeat mathp)))) 2002 ((and (> char ?0) 2003 (<= char (+ ?0 nkeys))) 2004 (setq ckey (nth (1- (- char ?0)) index-keys) 2005 rpl (save-match-data 2006 (reftex-index-make-replace-string 2007 macro-fmt match ckey repeat mathp)))) 2008 (t (ding))) 2009 nil))))) 2010 (message "") 2011 (move-marker beg nil) 2012 (move-marker end nil) 2013 (setq all-yes nil) 2014 (reftex-unhighlight 0)))) 2015 2016(defun reftex-index-phrase-match-is-indexed (beg end) 2017 ;; Check if match is in an argument of an index macro, or if an 2018 ;; index macro is directly attached to the match. 2019 (save-excursion 2020 (goto-char end) 2021 (let* ((all-macros (reftex-what-macro t)) 2022; (this-macro (car (car all-macros))) 2023 (before-macro 2024 (and (> beg 2) 2025 (goto-char (1- beg)) 2026 (memq (char-after (point)) '(?\] ?\})) 2027 (car (reftex-what-macro 1)))) 2028 (after-macro 2029 (and (goto-char end) 2030 (looking-at "\\(\\\\[a-zA-Z]+\\*?\\)[[{]") 2031 (match-string 1))) 2032 macro) 2033 (or (catch 'matched 2034 (while (setq macro (pop all-macros)) 2035 (if (member (car macro) reftex-macros-with-index) 2036 (throw 'matched t))) 2037 nil) 2038 (and before-macro 2039 (member before-macro reftex-macros-with-index)) 2040 (and after-macro 2041 (member after-macro reftex-macros-with-index)))))) 2042 2043(defun reftex-index-phrases-fixup-line (beg end) 2044 "Insert newlines before BEG and/or after END to shorten line." 2045 (let (bol eol space1 space2) 2046 (save-excursion 2047 ;; Find line boundaries and possible line breaks near BEG and END 2048 (beginning-of-line) 2049 (setq bol (point)) 2050 (end-of-line) 2051 (setq eol (point)) 2052 (goto-char beg) 2053 (skip-chars-backward "^ \n") 2054 (if (and (equal (preceding-char) ?\ ) 2055 (string-match "\\S-" (buffer-substring bol (point)))) 2056 (setq space1 (1- (point)))) 2057 (goto-char end) 2058 (skip-chars-forward "^ \n") 2059 (if (and (equal (following-char) ?\ ) 2060 (string-match "\\S-" (buffer-substring (point) eol))) 2061 (setq space2 (point))) 2062 ;; Now check what we have and insert the newlines 2063 (if (<= (- eol bol) fill-column) 2064 ;; Line is already short 2065 nil 2066 (cond 2067 ((and (not space1) (not space2))) ; No spaces available 2068 ((not space2) ; Do space1 2069 (reftex-index-phrases-replace-space space1)) 2070 ((not space1) ; Do space2 2071 (reftex-index-phrases-replace-space space2)) 2072 (t ; We have both spaces 2073 (let ((l1 (- space1 bol)) 2074 (l2 (- space2 space1)) 2075 (l3 (- eol space2))) 2076 (if (> l2 fill-column) 2077 ;; The central part alone is more than one line 2078 (progn 2079 (reftex-index-phrases-replace-space space1) 2080 (reftex-index-phrases-replace-space space2)) 2081 (if (> (+ l1 l2) fill-column) 2082 ;; Need to split beginning 2083 (reftex-index-phrases-replace-space space1)) 2084 (if (> (+ l2 l3) fill-column) 2085 ;; Need to split end 2086 (reftex-index-phrases-replace-space space2)))))))))) 2087 2088(defun reftex-index-phrases-replace-space (pos) 2089 "If there is a space at POS, replace it with a newline char. 2090Does not do a save-excursion." 2091 (when (equal (char-after pos) ?\ ) 2092 (goto-char pos) 2093 (delete-char 1) 2094 (insert "\n"))) 2095 2096(defun reftex-index-select-phrases-macro (&optional delay) 2097 "Offer a list of possible index macros and have the user select one." 2098 (let* ((prompt (concat "Select macro: [" 2099 (mapconcat (lambda (x) (char-to-string (car x))) 2100 reftex-index-phrases-macro-data "") 2101 "] ")) 2102 (help (concat "Select an indexing macro\n========================\n" 2103 (mapconcat (lambda (x) 2104 (format " [%c] %s" 2105 (car x) (nth 1 x))) 2106 reftex-index-phrases-macro-data "\n")))) 2107 (reftex-select-with-char prompt help delay))) 2108 2109(provide 'reftex-index) 2110 2111;;; reftex-index.el ends here 2112 2113;; Local Variables: 2114;; generated-autoload-file: "reftex-loaddefs.el" 2115;; End: 2116