Mercurial > emacs
changeset 25280:9b601931b795
Initial revision
author | Carsten Dominik <dominik@science.uva.nl> |
---|---|
date | Mon, 16 Aug 1999 07:42:41 +0000 |
parents | 03cb8fb8ab28 |
children | 26430a2408d5 |
files | lisp/textmodes/reftex-auc.el lisp/textmodes/reftex-cite.el lisp/textmodes/reftex-global.el lisp/textmodes/reftex-index.el lisp/textmodes/reftex-parse.el lisp/textmodes/reftex-ref.el lisp/textmodes/reftex-sel.el lisp/textmodes/reftex-toc.el lisp/textmodes/reftex-vars.el lisp/textmodes/reftex-vcr.el |
diffstat | 10 files changed, 7648 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-auc.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,190 @@ +;;; reftex-auc.el - RefTeX's interface to AUC TeX +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-auc) +(require 'reftex) +;;; + +(defun reftex-plug-flag (which) + ;; Tell if a certain flag is set in reftex-plug-into-AUCTeX + (or (eq t reftex-plug-into-AUCTeX) + (and (listp reftex-plug-into-AUCTeX) + (nth which reftex-plug-into-AUCTeX)))) + +(defun reftex-arg-label (optional &optional prompt definition) + "Use `reftex-label', `reftex-reference' or AUCTeX's code to insert label arg. +What is being used depends upon `reftex-plug-into-AUCTeX'." + (let (label) + (cond + ((and definition (reftex-plug-flag 1)) + ;; Create a new label, with a temporary brace for `reftex-what-macro' + (unwind-protect + (progn (insert "{") (setq label (or (reftex-label nil t) ""))) + (delete-backward-char 1))) + ((and (not definition) (reftex-plug-flag 2)) + ;; Reference a label with RefTeX + (setq label (reftex-reference nil t))) + (t + ;; AUCTeX's default mechanism + (setq label (completing-read (TeX-argument-prompt optional prompt "Key") + (LaTeX-label-list))))) + (if (and definition (not (string-equal "" label))) + (LaTeX-add-labels label)) + (TeX-argument-insert label optional))) + +(defun reftex-arg-cite (optional &optional prompt definition) + "Use `reftex-citation' or AUCTeX's code to insert a cite-key macro argument. +What is being used depends upon `reftex-plug-into-AUCTeX'." + (let (items) + (cond + ((and (not definition) (reftex-plug-flag 3)) + (setq items (list (or (reftex-citation t) "")))) + (t + (setq prompt (concat (if optional "(Optional) " "") + (if prompt prompt "Add key") + ": (default none) ")) + (setq items (multi-prompt "," t prompt (LaTeX-bibitem-list))))) + (apply 'LaTeX-add-bibitems items) + (TeX-argument-insert (mapconcat 'identity items ",") optional))) + + +(defun reftex-arg-index-tag (optional &optional prompt &rest args) + "Prompt for an index tag with completion. +This is the name of an index, not the entry." + (let (tag taglist) + (setq prompt (concat (if optional "(Optional) " "") + (if prompt prompt "Index tag") + ": (default none) ")) + (if (and reftex-support-index (reftex-plug-flag 4)) + ;; Use RefTeX completion + (progn + (reftex-access-scan-info nil) + (setq taglist + (cdr (assoc 'index-tags + (symbol-value reftex-docstruct-symbol))) + tag (completing-read prompt (mapcar 'list taglist)))) + ;; Just ask like AUCTeX does. + (setq tag (read-string prompt))) + (TeX-argument-insert tag optional))) + +(defun reftex-arg-index (optional &optional prompt &rest args) + "Prompt for an index entry completing with known entries. +Completion is specific for just one index, if the macro or a tag +argument identify one of multiple indices." + (let* (tag key) + (if (and reftex-support-index (reftex-plug-flag 4)) + (progn + (reftex-access-scan-info nil) + (setq tag (reftex-what-index-tag) + key (reftex-index-complete-key (or tag "idx")))) + (setq key (completing-read (TeX-argument-prompt optional prompt "Key") + (LaTeX-index-entry-list)))) + (unless (string-equal "" key) + (LaTeX-add-index-entries key)) + (TeX-argument-insert key optional))) + +(defun reftex-what-index-tag () + ;; Look backward to find out what index the macro at point belongs to + (let ((macro (save-excursion + (and (re-search-backward "\\\\[a-zA-Z*]+" nil t) + (match-string 0)))) + tag entry) + (when (and macro + (setq entry (assoc macro reftex-index-macro-alist))) + (setq tag (nth 1 entry)) + (cond + ((stringp tag) tag) + ((integerp tag) + (save-excursion + (goto-char (match-end 1)) + (or (reftex-nth-arg tag (nth 6 entry)) "idx"))) + (t "idx"))))) + +(defvar LaTeX-label-function) +(defun reftex-plug-into-AUCTeX () + ;; Replace AUCTeX functions with RefTeX functions. + ;; Which functions are replaced is controlled by the variable + ;; `reftex-plug-into-AUCTeX'. + + (if (reftex-plug-flag 0) + (setq LaTeX-label-function 'reftex-label) + (setq LaTeX-label-function nil)) + + (and (or (reftex-plug-flag 1) (reftex-plug-flag 2)) + (fboundp 'TeX-arg-label) + (fset 'TeX-arg-label 'reftex-arg-label)) + + (and (reftex-plug-flag 3) + (fboundp 'TeX-arg-cite) + (fset 'TeX-arg-cite 'reftex-arg-cite)) + + (and (reftex-plug-flag 4) + (fboundp 'TeX-arg-index-tag) + (fset 'TeX-arg-index-tag 'reftex-arg-index-tag)) + (and (reftex-plug-flag 4) + (fboundp 'TeX-arg-index) + (fset 'TeX-arg-index 'reftex-arg-index))) + +(defun reftex-toggle-plug-into-AUCTeX () + "Toggle Interface between AUCTeX and RefTeX on and off." + (interactive) + (unless (and (featurep 'tex-site) (featurep 'latex)) + (error "AUCTeX's LaTeX mode does not seem to be loaded")) + (setq reftex-plug-into-AUCTeX (not reftex-plug-into-AUCTeX)) + (reftex-plug-into-AUCTeX) + (if reftex-plug-into-AUCTeX + (message "RefTeX has been plugged into AUCTeX.") + (message "RefTeX no longer interacts with AUCTeX."))) + +(defun reftex-add-label-environments (entry-list) + "Add label environment descriptions to `reftex-label-alist-style'. +The format of ENTRY-LIST is exactly like `reftex-label-alist'. See there +for details. +This function makes it possible to support RefTeX from AUCTeX style files. +The entries in ENTRY-LIST will be processed after the user settings in +`reftex-label-alist', and before the defaults (specified in +`reftex-default-label-alist-entries'). Any changes made to +`reftex-label-alist-style' will raise a flag to the effect that +the label information is recompiled on next use." + (unless reftex-docstruct-symbol + (reftex-tie-multifile-symbols)) + (when (and reftex-docstruct-symbol + (symbolp reftex-docstruct-symbol)) + (let ((list (get reftex-docstruct-symbol 'reftex-label-alist-style)) + entry changed) + (while entry-list + (setq entry (pop entry-list)) + (unless (member entry list) + (setq reftex-tables-dirty t + changed t) + (push entry list))) + (when changed + (put reftex-docstruct-symbol 'reftex-label-alist-style list))))) +(defalias 'reftex-add-to-label-alist 'reftex-add-label-environments) + +(defun reftex-add-section-levels (entry-list) + "Add entries to the value of `reftex-section-levels'. +The added values are kept local to the current document. The format +of ENTRY-LIST is a list of cons cells (\"MACRONAME\" . LEVEL). See +`reftex-section-levels' for an example." + (unless reftex-docstruct-symbol + (reftex-tie-multifile-symbols)) + (when (and reftex-docstruct-symbol + (symbolp reftex-docstruct-symbol)) + (let ((list (get reftex-docstruct-symbol 'reftex-section-levels)) + entry changed) + (while entry-list + (setq entry (pop entry-list)) + (unless (member entry list) + (setq reftex-tables-dirty t + changed t) + (push entry list))) + (when changed + (put reftex-docstruct-symbol 'reftex-section-levels list))))) + +(defun reftex-notice-new-section () + (reftex-notice-new 1 'force)) + +;;; reftex-auc.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-cite.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,928 @@ +;;; reftex-cite.el - Creating citations with RefTeX +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-cite) +(require 'reftex) +;;; + +;; Variables and constants + +;; The history list of regular expressions used for citations +(defvar reftex-cite-regexp-hist nil) + +;; Prompt and help string for citation selection +(defconst reftex-citation-prompt + "Select: [n]ext [p]revious [r]estrict [ ]full_entry [q]uit RET [?]Help+more") + +(defconst reftex-citation-help + " n / p Go to next/previous entry (Cursor motion works as well). + g / r Start over with new regexp / Refine with additional regexp. + SPC Show full database entry in other window. + f Toggle follow mode: Other window will follow with full db entry. + . Show insertion point. + q Quit without inserting \\cite macro into buffer. + TAB Enter citation key with completion. + RET Accept current entry (also on mouse-2) and create \\cite macro. + m / u Mark/Unmark the entry. + a / A Put all (marked) entries into one/many \\cite commands.") + +;; Find bibtex files + +(defun reftex-default-bibliography () + ;; Return the expanded value of `reftex-default-bibliography'. + ;; The expanded value is cached. + (unless (eq (get 'reftex-default-bibliography :reftex-raw) + reftex-default-bibliography) + (put 'reftex-default-bibliography :reftex-expanded + (reftex-locate-bibliography-files + default-directory reftex-default-bibliography)) + (put 'reftex-default-bibliography :reftex-raw + reftex-default-bibliography)) + (get 'reftex-default-bibliography :reftex-expanded)) + +(defun reftex-get-bibfile-list () + ;; Return list of bibfiles for current document. + ;; When using the chapterbib or bibunits package you should either + ;; use the same database files everywhere, or separate parts using + ;; different databases into different files (included into the mater file). + ;; Then this function will return the applicable database files. + + ;; Ensure access to scanning info + (reftex-access-scan-info) + (or + ;; Try inside this file (and its includes) + (cdr (reftex-last-assoc-before-elt + 'bib (list 'eof (buffer-file-name)) + (member (list 'bof (buffer-file-name)) + (symbol-value reftex-docstruct-symbol)))) + ;; Try after the beginning of this file + (cdr (assq 'bib (member (list 'bof (buffer-file-name)) + (symbol-value reftex-docstruct-symbol)))) + ;; Anywhere in the entire document + (cdr (assq 'bib (symbol-value reftex-docstruct-symbol))) + (error "\\bibliography statement missing or .bib files not found"))) + +;; Find a certain reference in any of the BibTeX files. + +(defun reftex-pop-to-bibtex-entry (key file-list &optional mark-to-kill + highlight item return) + ;; Find BibTeX KEY in any file in FILE-LIST in another window. + ;; If MARK-TO-KILL is non-nil, mark new buffer to kill. + ;; If HIGHLIGHT is non-nil, highlight the match. + ;; If ITEM in non-nil, search for bibitem instead of database entry. + ;; If RETURN is non-nil, just return the entry. + + (let* ((re + (if item + (concat "\\\\bibitem\\(\\[[^]]*\\]\\)?{" (regexp-quote key) "}") + (concat "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*" (regexp-quote key) + "[, \t\r\n}]"))) + (buffer-conf (current-buffer)) + file buf) + + (catch 'exit + (while file-list + (setq file (car file-list) + file-list (cdr file-list)) + (unless (setq buf (reftex-get-file-buffer-force file mark-to-kill)) + (error "No such file %s" file)) + (set-buffer buf) + (widen) + (goto-char (point-min)) + (when (re-search-forward re nil t) + (goto-char (match-beginning 0)) + (when return + ;; Just return the relevant entry + (if item (goto-char (match-end 0))) + (setq return (buffer-substring + (point) (reftex-end-of-bib-entry item))) + (set-buffer buffer-conf) + (throw 'exit return)) + (switch-to-buffer-other-window buf) + (recenter 0) + (if highlight + (reftex-highlight 0 (match-beginning 0) (match-end 0))) + (throw 'exit (selected-window)))) + (set-buffer buffer-conf) + (if item + (error "No \\bibitem with citation key %s" key) + (error "No BibTeX entry with citation key %s" key))))) + +(defun reftex-end-of-bib-entry (item) + (save-excursion + (condition-case nil + (if item + (progn (end-of-line) + (re-search-forward + "\\\\bibitem\\|\\end{thebibliography}") + (1- (match-beginning 0))) + (progn (forward-list 1) (point))) + (error (min (point-max) (+ 300 (point))))))) + +;; Parse bibtex buffers + +(defun reftex-extract-bib-entries (buffers) + ;; Extract bib entries which match regexps from BUFFERS. + ;; BUFFERS is a list of buffers or file names. + ;; Return list with entries." + (let* (re-list first-re rest-re + (buffer-list (if (listp buffers) buffers (list buffers))) + found-list entry buffer1 buffer alist + key-point start-point end-point) + + ;; Read a regexp, completing on known citation keys. + (setq re-list + (split-string + (completing-read + "RegExp [ && RegExp...]: " + (if reftex-mode + (if (fboundp 'LaTeX-bibitem-list) + (LaTeX-bibitem-list) + (cdr (assoc 'bibview-cache + (symbol-value reftex-docstruct-symbol)))) + nil) + nil nil nil 'reftex-cite-regexp-hist) + "[ \t]*&&[ \t]*")) + + (setq first-re (car re-list) ; We'll use the first re to find things, + rest-re (cdr re-list)) ; the others to narrow down. + (if (string-match "\\`[ \t]*\\'" (or first-re "")) + (error "Empty regular expression")) + + (save-excursion + (save-window-excursion + + ;; Walk through all bibtex files + (while buffer-list + (setq buffer (car buffer-list) + buffer-list (cdr buffer-list)) + (if (and (bufferp buffer) + (buffer-live-p buffer)) + (setq buffer1 buffer) + (setq buffer1 (reftex-get-file-buffer-force + buffer (not reftex-keep-temporary-buffers)))) + (if (not buffer1) + (message "No such BibTeX file %s (ignored)" buffer) + (message "Scanning bibliography database %s" buffer1)) + + (set-buffer buffer1) + (save-excursion + (goto-char (point-min)) + (while (re-search-forward first-re nil t) + (catch 'search-again + (setq key-point (point)) + (unless (re-search-backward + "\\(\\`\\|[\n\r]\\)[ \t]*@\\([a-zA-Z]+\\)[ \t\n\r]*[{(]" nil t) + (throw 'search-again nil)) + (setq start-point (point)) + (goto-char (match-end 0)) + (condition-case nil + (up-list 1) + (error (goto-char key-point) + (throw 'search-again nil))) + (setq end-point (point)) + + ;; Ignore @string, @comment and @c entries or things + ;; outside entries + (when (or (string= (downcase (match-string 2)) "string") + (string= (downcase (match-string 2)) "comment") + (string= (downcase (match-string 2)) "c") + (< (point) key-point)) ; this means match not in {} + (goto-char key-point) + (throw 'search-again nil)) + + ;; Well, we have got a match + (setq entry (concat + (buffer-substring start-point (point)) "\n")) + + ;; Check if other regexp match as well + (setq re-list rest-re) + (while re-list + (unless (string-match (car re-list) entry) + ;; nope - move on + (throw 'search-again nil)) + (pop re-list)) + + (setq alist (reftex-parse-bibtex-entry + nil start-point end-point)) + (push (cons "&entry" entry) alist) + + ;; check for crossref entries + (if (assoc "crossref" alist) + (setq alist + (append + alist (reftex-get-crossref-alist alist)))) + + ;; format the entry + (push (cons "&formatted" (reftex-format-bib-entry alist)) + alist) + + ;; make key the first element + (push (reftex-get-bib-field "&key" alist) alist) + + ;; add it to the list + (push alist found-list)))) + (reftex-kill-temporary-buffers)))) + (setq found-list (nreverse found-list)) + + ;; Sorting + (cond + ((eq 'author reftex-sort-bibtex-matches) + (sort found-list 'reftex-bib-sort-author)) + ((eq 'year reftex-sort-bibtex-matches) + (sort found-list 'reftex-bib-sort-year)) + ((eq 'reverse-year reftex-sort-bibtex-matches) + (sort found-list 'reftex-bib-sort-year-reverse)) + (t found-list)))) + +(defun reftex-bib-sort-author (e1 e2) + (let ((al1 (reftex-get-bib-names "author" e1)) + (al2 (reftex-get-bib-names "author" e2))) + (while (and al1 al2 (string= (car al1) (car al2))) + (pop al1) + (pop al2)) + (if (and (stringp (car al1)) + (stringp (car al2))) + (string< (car al1) (car al2)) + (not (stringp (car al1)))))) + +(defun reftex-bib-sort-year (e1 e2) + (< (string-to-int (cdr (assoc "year" e1))) + (string-to-int (cdr (assoc "year" e2))))) + +(defun reftex-bib-sort-year-reverse (e1 e2) + (> (string-to-int (or (cdr (assoc "year" e1)) "0")) + (string-to-int (or (cdr (assoc "year" e2)) "0")))) + +(defun reftex-get-crossref-alist (entry) + ;; return the alist from a crossref entry + (let ((crkey (cdr (assoc "crossref" entry))) + start) + (save-excursion + (save-restriction + (widen) + (if (re-search-forward + (concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey) + "[ \t\n\r]*,") nil t) + (progn + (setq start (match-beginning 0)) + (condition-case nil + (up-list 1) + (error nil)) + (reftex-parse-bibtex-entry nil start (point))) + nil))))) + +;; Parse the thebibliography environment +(defun reftex-extract-bib-entries-from-thebibliography (file) + ;; Extract bib-entries from the \begin{thebibliography} environment. + ;; Parsing is not as good as for the BibTeX database stuff. + ;; The environment should be located in file FILE. + + (let* (start end buf entries re re-list) + (unless file + (error "Need file name to find thebibliography environment")) + (setq buf (reftex-get-file-buffer-force + file (not reftex-keep-temporary-buffers))) + (unless buf + (error "No such file %s" file)) + (message "Scanning thebibliography environment in %s" file) + + (save-excursion + (set-buffer buf) + (save-restriction + (widen) + (goto-char (point-min)) + (if (re-search-forward + "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t) + (progn + (beginning-of-line 2) + (setq start (point)))) + (if (re-search-forward + "\\(\\`\\|[\n\r]\\)[ \t]*\\\\end{thebibliography}" nil t) + (progn + (beginning-of-line 1) + (setq end (point)))) + (when (and start end) + (setq entries + (mapcar 'reftex-parse-bibitem + (delete "" + (split-string + (buffer-substring-no-properties start end) + "[ \t\n\r]*\\\\bibitem\\(\\[[^]]*]\\)*"))))))) + (unless entries + (error "No bibitems found")) + + (setq re-list (split-string + (read-string "RegExp [ && RegExp...]: " + nil 'reftex-cite-regexp-hist) + "[ \t]*&&[ \t]*")) + (if (string-match "\\`[ \t]*\\'" (car re-list)) + (error "Empty regular expression")) + + (while (and (setq re (pop re-list)) entries) + (setq entries + (delq nil (mapcar + (lambda (x) + (if (string-match re (cdr (assoc "&entry" x))) + x nil)) + entries)))) + (setq entries + (mapcar + (lambda (x) + (push (cons "&formatted" (reftex-format-bibitem x)) x) + (push (reftex-get-bib-field "&key" x) x) + x) + entries)) + + entries)) + +;; Parse and format individual entries + +(defun reftex-get-bib-names (field entry) + ;; Return a list with the author or editor names in ENTRY + (let ((names (reftex-get-bib-field field entry))) + (if (equal "" names) + (setq names (reftex-get-bib-field "editor" entry))) + (while (string-match "\\band\\b[ \t]*" names) + (setq names (replace-match "\n" nil t names))) + (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" names) + (setq names (replace-match "" nil t names))) + (while (string-match "^[ \t]+\\|[ \t]+$" names) + (setq names (replace-match "" nil t names))) + (while (string-match "[ \t][ \t]+" names) + (setq names (replace-match " " nil t names))) + (split-string names "\n"))) + +(defun reftex-parse-bibtex-entry (entry &optional from to) + (let (alist key start field) + (save-excursion + (save-restriction + (if entry + (progn + (set-buffer (get-buffer-create " *RefTeX-scratch*")) + (fundamental-mode) + (erase-buffer) + (insert entry)) + (widen) + (narrow-to-region from to)) + (goto-char (point-min)) + + (if (re-search-forward + "@\\(\\w+\\)[ \t\n\r]*[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t) + (setq alist + (list + (cons "&type" (downcase (reftex-match-string 1))) + (cons "&key" (reftex-match-string 2))))) + (while (re-search-forward "\\(\\w+\\)[ \t\n\r]*=[ \t\n\r]*" nil t) + (setq key (downcase (reftex-match-string 1))) + (cond + ((= (following-char) ?{) + (forward-char 1) + (setq start (point)) + (condition-case nil + (up-list 1) + (error nil))) + ((= (following-char) ?\") + (forward-char 1) + (setq start (point)) + (while (and (search-forward "\"" nil t) + (= ?\\ (char-after (- (point) 2)))))) + (t + (setq start (point)) + (re-search-forward "[ \t]*[\n\r,}]" nil 1))) + (setq field (buffer-substring-no-properties start (1- (point)))) + ;; remove extra whitespace + (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field) + (setq field (replace-match " " nil t field))) + ;; remove leading garbage + (if (string-match "^[ \t{]+" field) + (setq field (replace-match "" nil t field))) + ;; remove trailing garbage + (if (string-match "[ \t}]+$" field) + (setq field (replace-match "" nil t field))) + (push (cons key field) alist)))) + alist)) + +(defun reftex-get-bib-field (fieldname entry &optional format) + ;; Extract the field FIELDNAME from an ENTRY + (let ((cell (assoc fieldname entry))) + (if cell + (if format + (format format (cdr cell)) + (cdr cell)) + ""))) + +(defun reftex-format-bib-entry (entry) + ;; Format a BibTeX ENTRY so that it is nice to look at + (let* + ((auth-list (reftex-get-bib-names "author" entry)) + (authors (mapconcat 'identity auth-list ", ")) + (year (reftex-get-bib-field "year" entry)) + (title (reftex-get-bib-field "title" entry)) + (type (reftex-get-bib-field "&type" entry)) + (key (reftex-get-bib-field "&key" entry)) + (extra + (cond + ((equal type "article") + (concat (reftex-get-bib-field "journal" entry) " " + (reftex-get-bib-field "volume" entry) ", " + (reftex-get-bib-field "pages" entry))) + ((equal type "book") + (concat "book (" (reftex-get-bib-field "publisher" entry) ")")) + ((equal type "phdthesis") + (concat "PhD: " (reftex-get-bib-field "school" entry))) + ((equal type "mastersthesis") + (concat "Master: " (reftex-get-bib-field "school" entry))) + ((equal type "inbook") + (concat "Chap: " (reftex-get-bib-field "chapter" entry) + ", pp. " (reftex-get-bib-field "pages" entry))) + ((or (equal type "conference") + (equal type "incollection") + (equal type "inproceedings")) + (reftex-get-bib-field "booktitle" entry "in: %s")) + (t "")))) + (setq authors (reftex-truncate authors 30 t t)) + (when (reftex-use-fonts) + (put-text-property 0 (length key) 'face + (reftex-verified-face reftex-label-face + 'font-lock-constant-face + 'font-lock-reference-face) + key) + (put-text-property 0 (length authors) 'face reftex-bib-author-face + authors) + (put-text-property 0 (length year) 'face reftex-bib-year-face + year) + (put-text-property 0 (length title) 'face reftex-bib-title-face + title) + (put-text-property 0 (length extra) 'face reftex-bib-extra-face + extra)) + (concat key "\n " authors " " year " " extra "\n " title "\n\n"))) + +(defun reftex-parse-bibitem (item) + ;; Parse a \bibitem entry + (let ((key "") (text "")) + (when (string-match "\\`{\\([^}]+\\)}\\([\001-\255]*\\)" item) + (setq key (match-string 1 item) + text (match-string 2 item))) + ;; Clean up the text a little bit + (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) + (setq text (replace-match " " nil t text))) + (if (string-match "\\`[ \t]+" text) + (setq text (replace-match "" nil t text))) + (list + (cons "&key" key) + (cons "&text" text) + (cons "&entry" (concat key " " text))))) + +(defun reftex-format-bibitem (item) + ;; Format a \bibitem entry so that it is (relatively) nice to look at. + (let ((text (reftex-get-bib-field "&text" item)) + (key (reftex-get-bib-field "&key" item)) + (lines nil)) + + ;; Wrap the text into several lines. + (while (and (> (length text) 70) + (string-match " " (substring text 60))) + (push (substring text 0 (+ 60 (match-beginning 0))) lines) + (setq text (substring text (+ 61 (match-beginning 0))))) + (push text lines) + (setq text (mapconcat 'identity (nreverse lines) "\n ")) + + (when (reftex-use-fonts) + (put-text-property 0 (length text) 'face reftex-bib-author-face text)) + (concat key "\n " text "\n\n"))) + +;; Make a citation + +;;;###autoload +(defun reftex-citation (&optional no-insert) + "Make a citation using BibTeX database files. +After prompting for a regular expression, scans the buffers with +bibtex entries (taken from the \\bibliography command) and offers the +matching entries for selection. The selected entry is formated according +to `reftex-cite-format' and inserted into the buffer. + +If NO-INSERT is non-nil, nothing is inserted, only the selected key returned. + +When called with one or two `C-u' prefixes, first rescans the document. +When called with a numeric prefix, make that many citations. When +called with point inside the braces of a `\cite' command, it will +add another key, ignoring the value of `reftex-cite-format'. + +The regular expression uses an expanded syntax: && is interpreted as `and'. +Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'. +While entering the regexp, completion on knows citation keys is possible. +`=' is a good regular expression to match all entries in all files." + + (interactive) + + ;; check for recursive edit + (reftex-check-recursive-edit) + + ;; This function may also be called outside reftex-mode. + ;; Thus look for the scanning info only if in reftex-mode. + + (when reftex-mode + (reftex-access-scan-info current-prefix-arg)) + + ;; Call reftex-do-citation, but protected + (unwind-protect + (reftex-do-citation current-prefix-arg no-insert) + (reftex-kill-temporary-buffers))) + +(defun reftex-do-citation (&optional arg no-insert) + ;; This really does the work of reftex-citation. + + (let* ((format (reftex-figure-out-cite-format arg no-insert)) + (docstruct-symbol reftex-docstruct-symbol) + (selected-entries (reftex-offer-bib-menu)) + (insert-entries selected-entries) + entry string cite-view) + + (unless selected-entries (error "Quit")) + + (if (stringp selected-entries) + ;; Nonexistent entry + (setq selected-entries nil + insert-entries (list (list selected-entries + (cons "&key" selected-entries)))) + ;; It makes sense to compute the cite-view strings. + (setq cite-view t)) + + (when (eq (car selected-entries) 'concat) + ;; All keys go into a single command - we need to trick a little + (pop selected-entries) + (let ((concat-keys (mapconcat 'car selected-entries ","))) + (setq insert-entries + (list (list concat-keys (cons "&key" concat-keys)))))) + + (unless no-insert + + ;; We shall insert this into the buffer... + (message "Formatting...") + + (while (setq entry (pop insert-entries)) + ;; Format the citation and insert it + (setq string (if reftex-format-cite-function + (funcall reftex-format-cite-function + (reftex-get-bib-field "&key" entry) + format) + (reftex-format-citation entry format))) + (insert string)) + + ;; Reposition cursor? + (when (string-match "\\?" string) + (search-backward "?") + (delete-char 1)) + + ;; Tell AUCTeX + (when (and reftex-mode + (fboundp 'LaTeX-add-bibitems) + reftex-plug-into-AUCTeX) + (apply 'LaTeX-add-bibitems (mapcar 'car selected-entries))) + + ;; Produce the cite-view strings + (when (and reftex-mode reftex-cache-cite-echo cite-view) + (mapcar (lambda (entry) + (reftex-make-cite-echo-string entry docstruct-symbol)) + selected-entries)) + + (message "")) + + (set-marker reftex-select-return-marker nil) + (reftex-kill-buffer "*RefTeX Select*") + + ;; Check if the prefix arg was numeric, and call recursively + (when (integerp arg) + (if (> arg 1) + (progn + (skip-chars-backward "}") + (decf arg) + (reftex-do-citation arg)) + (forward-char 1))) + + ;; Return the citation key + (car (car selected-entries)))) + +(defun reftex-figure-out-cite-format (arg no-insert) + ;; Check if there is already a cite command at point and change cite format + ;; in order to only add another reference in the same cite command. + (let ((macro (car (reftex-what-macro 1))) + (cite-format-value (reftex-get-cite-format)) + key format) + (cond + (no-insert + ;; Format does not really matter because nothing will be inserted. + (setq format "%l")) + + ((and (stringp macro) + (string-match "\\`\\\\cite\\|cite\\'" macro)) + ;; We are already inside a cite macro + (if (or (not arg) (not (listp arg))) + (setq format + (concat + (if (member (preceding-char) '(?\{ ?,)) "" ",") + "%l" + (if (member (following-char) '(?\} ?,)) "" ","))) + (setq format "%l"))) + (t + ;; Figure out the correct format + (setq format + (if (and (symbolp cite-format-value) + (assq cite-format-value reftex-cite-format-builtin)) + (nth 2 (assq cite-format-value reftex-cite-format-builtin)) + cite-format-value)) + (when (listp format) + (setq key + (reftex-select-with-char + "" (concat "SELECT A CITATION FORMAT\n\n" + (mapconcat + (lambda (x) + (format "[%c] %s %s" (car x) + (if (> (car x) 31) " " "") + (cdr x))) + format "\n")))) + (if (assq key format) + (setq format (cdr (assq key format))) + (error "No citation format associated with key `%c'" key))))) + format)) + +(defvar reftex-select-bib-map) +(defun reftex-offer-bib-menu () + ;; Offer bib menu and return list of selected items + + (let (found-list rtn key data selected-entries) + (while + (not + (catch 'done + ;; Scan bibtex files + (setq found-list + (cond + ((assq 'bib (symbol-value reftex-docstruct-symbol)) + ;; using BibTeX database files. + (reftex-extract-bib-entries (reftex-get-bibfile-list))) + ((assq 'thebib (symbol-value reftex-docstruct-symbol)) + ;; using thebibliography environment. + (reftex-extract-bib-entries-from-thebibliography + (cdr (assq 'thebib (symbol-value reftex-docstruct-symbol))))) + (reftex-default-bibliography + (message "Using default bibliography") + (reftex-extract-bib-entries (reftex-default-bibliography))) + (t (error "No valid bibliography in this document, and no default available")))) + + (unless found-list + (error "Sorry, no matches found")) + + ;; Remember where we came from + (setq reftex-call-back-to-this-buffer (current-buffer)) + (set-marker reftex-select-return-marker (point)) + + ;; Offer selection + (save-window-excursion + (delete-other-windows) + (let ((default-major-mode 'reftex-select-bib-mode)) + (reftex-kill-buffer "*RefTeX Select*") + (switch-to-buffer-other-window "*RefTeX Select*") + (unless (eq major-mode 'reftex-select-bib-mode) + (reftex-select-bib-mode)) + (let ((buffer-read-only nil)) + (erase-buffer) + (reftex-insert-bib-matches found-list))) + (setq buffer-read-only t) + (if (= 0 (buffer-size)) + (error "No matches found")) + (setq truncate-lines t) + (goto-char 1) + (while t + (setq rtn + (reftex-select-item + reftex-citation-prompt + reftex-citation-help + reftex-select-bib-map + nil + 'reftex-bibtex-selection-callback nil)) + (setq key (car rtn) + data (nth 1 rtn)) + (unless key (throw 'done t)) + (cond + ((eq key ?g) + ;; Start over + (throw 'done nil)) + ((eq key ?r) + ;; Restrict with new regular expression + (setq found-list (reftex-restrict-bib-matches found-list)) + (let ((buffer-read-only nil)) + (erase-buffer) + (reftex-insert-bib-matches found-list)) + (goto-char 1)) + ((eq key ?A) + ;; Take all (marked) + (setq selected-entries + (if reftex-select-marked + (mapcar 'car (nreverse reftex-select-marked)) + found-list)) + (throw 'done t)) + ((eq key ?a) + ;; Take all (marked), and push the symbol 'concat + (setq selected-entries + (cons 'concat + (if reftex-select-marked + (mapcar 'car (nreverse reftex-select-marked)) + found-list))) + (throw 'done t)) + ((or (eq key ?\C-m) + (eq key 'return)) + ;; Take selected + (setq selected-entries + (if reftex-select-marked + (cons 'concat + (mapcar 'car (nreverse reftex-select-marked))) + (if data (list data) nil))) + (throw 'done t)) + ((stringp key) + ;; Got this one with completion + (setq selected-entries key) + (throw 'done t)) + (t + (ding)))))))) + selected-entries)) + +(defun reftex-restrict-bib-matches (found-list) + ;; Limit FOUND-LIST with more regular expressions + (let ((re-list (split-string (read-string + "RegExp [ && RegExp...]: " + nil 'reftex-cite-regexp-hist) + "[ \t]*&&[ \t]*")) + (found-list-r found-list) + re) + (while (setq re (pop re-list)) + (setq found-list-r + (delq nil + (mapcar + (lambda (x) + (if (string-match + re (cdr (assoc "&entry" x))) + x + nil)) + found-list-r)))) + (if found-list-r + found-list-r + (ding) + found-list))) + +(defun reftex-insert-bib-matches (list) + ;; Insert the bib matches and number them correctly + (let ((mouse-face + (if (memq reftex-highlight-selection '(mouse both)) + reftex-mouse-selected-face + nil)) + tmp len) + (mapcar + (lambda (x) + (setq tmp (cdr (assoc "&formatted" x)) + len (length tmp)) + (put-text-property 0 len :data x tmp) + (put-text-property 0 (1- len) 'mouse-face mouse-face tmp) + (insert tmp)) + list)) + (run-hooks 'reftex-display-copied-context-hook)) + +(defun reftex-format-names (namelist n) + (let (last (len (length namelist))) + (cond + ((< len 1) "") + ((= 1 len) (car namelist)) + ((> len n) (concat (car namelist) (nth 2 reftex-cite-punctuation))) + (t + (setq n (min len n) + last (nth (1- n) namelist)) + (setcdr (nthcdr (- n 2) namelist) nil) + (concat + (mapconcat 'identity namelist (nth 0 reftex-cite-punctuation)) + (nth 1 reftex-cite-punctuation) + last))))) + +(defun reftex-format-citation (entry format) + ;; Format a citation from the info in the BibTeX ENTRY + + (unless (stringp format) (setq format "\\cite{%l}")) + + (if (and reftex-comment-citations + (string-match "%l" reftex-cite-comment-format)) + (error "reftex-cite-comment-format contains illegal %%l")) + + (while (string-match + "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)" + format) + (let ((n (string-to-int (match-string 4 format))) + (l (string-to-char (match-string 5 format))) + rpl b e) + (save-match-data + (setq rpl + (cond + ((= l ?l) (concat + (reftex-get-bib-field "&key" entry) + (if reftex-comment-citations + reftex-cite-comment-format + ""))) + ((= l ?a) (reftex-format-names + (reftex-get-bib-names "author" entry) + (or n 2))) + ((= l ?A) (car (reftex-get-bib-names "author" entry))) + ((= l ?b) (reftex-get-bib-field "booktitle" entry "in: %s")) + ((= l ?B) (reftex-abbreviate-title + (reftex-get-bib-field "booktitle" entry "in: %s"))) + ((= l ?c) (reftex-get-bib-field "chapter" entry)) + ((= l ?d) (reftex-get-bib-field "edition" entry)) + ((= l ?e) (reftex-format-names + (reftex-get-bib-names "editor" entry) + (or n 2))) + ((= l ?E) (car (reftex-get-bib-names "editor" entry))) + ((= l ?h) (reftex-get-bib-field "howpublished" entry)) + ((= l ?i) (reftex-get-bib-field "institution" entry)) + ((= l ?j) (reftex-get-bib-field "journal" entry)) + ((= l ?k) (reftex-get-bib-field "key" entry)) + ((= l ?m) (reftex-get-bib-field "month" entry)) + ((= l ?n) (reftex-get-bib-field "number" entry)) + ((= l ?o) (reftex-get-bib-field "organization" entry)) + ((= l ?p) (reftex-get-bib-field "pages" entry)) + ((= l ?P) (car (split-string + (reftex-get-bib-field "pages" entry) + "[- .]+"))) + ((= l ?s) (reftex-get-bib-field "school" entry)) + ((= l ?u) (reftex-get-bib-field "publisher" entry)) + ((= l ?r) (reftex-get-bib-field "address" entry)) + ((= l ?t) (reftex-get-bib-field "title" entry)) + ((= l ?T) (reftex-abbreviate-title + (reftex-get-bib-field "title" entry))) + ((= l ?v) (reftex-get-bib-field "volume" entry)) + ((= l ?y) (reftex-get-bib-field "year" entry))))) + + (if (string= rpl "") + (setq b (match-beginning 2) e (match-end 2)) + (setq b (match-beginning 3) e (match-end 3))) + (setq format (concat (substring format 0 b) rpl (substring format e))))) + (while (string-match "%%" format) + (setq format (replace-match "%" t t format))) + (while (string-match "[ ,.;:]*%<" format) + (setq format (replace-match "" t t format))) + format) + +(defun reftex-make-cite-echo-string (entry docstruct-symbol) + ;; Format a bibtex entry for the echo area and cache the result. + (let* ((key (reftex-get-bib-field "&key" entry)) + (string + (let* ((reftex-cite-punctuation '(" " " & " " etal."))) + (reftex-format-citation entry reftex-cite-view-format))) + (cache (assq 'bibview-cache (symbol-value docstruct-symbol))) + (cache-entry (assoc key (cdr cache)))) + (unless cache + ;; This docstruct has no cache - make one. + (set docstruct-symbol (cons (cons 'bibview-cache nil) + (symbol-value docstruct-symbol)))) + (when reftex-cache-cite-echo + (setq key (copy-sequence key)) + (set-text-properties 0 (length key) nil key) + (set-text-properties 0 (length string) nil string) + (if cache-entry + (unless (string= (cdr cache-entry) string) + (setcdr cache-entry string) + (put reftex-docstruct-symbol 'modified t)) + (push (cons key string) (cdr cache)) + (put reftex-docstruct-symbol 'modified t))) + string)) + +(defun reftex-bibtex-selection-callback (data ignore no-revisit) + ;; Callback function to be called from the BibTeX selection, in + ;; order to display context. This function is relatively slow and not + ;; recommended for follow mode. It works OK for individual lookups. + (let ((win (selected-window)) + (key (reftex-get-bib-field "&key" data)) + bibfile-list item tmp) + + (catch 'exit + (save-excursion + (set-buffer reftex-call-back-to-this-buffer) + (cond + ((assq 'bib (symbol-value reftex-docstruct-symbol)) + (setq bibfile-list (reftex-get-bibfile-list))) + ((setq tmp (assq 'thebib (symbol-value reftex-docstruct-symbol))) + (setq bibfile-list (list (cdr tmp)) + item t)) + (reftex-default-bibliography + (setq bibfile-list (reftex-default-bibliography))) + (t (ding) (throw 'exit)))) + + (when no-revisit + (setq bibfile-list (reftex-visited-files bibfile-list))) + + (condition-case nil + (reftex-pop-to-bibtex-entry + key bibfile-list (not reftex-keep-temporary-buffers) t item) + (error (ding)))) + + (select-window win))) + +;;; reftex-cite.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-global.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,295 @@ +;;; reftex-global.el - Operations on entire documents with RefTeX +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-global) +(require 'reftex) +;;; + +(defun reftex-create-tags-file () + "Create TAGS file by running `etags' on the current document. +The TAGS file is also immediately visited with `visit-tags-table'." + (interactive) + (reftex-access-scan-info current-prefix-arg) + (let* ((master (reftex-TeX-master-file)) + (files (reftex-all-document-files)) + (cmd (format "etags %s" (mapconcat 'identity files " ")))) + (save-excursion + (set-buffer (reftex-get-buffer-visiting master)) + (message "Running etags to create TAGS file...") + (shell-command cmd) + (visit-tags-table "TAGS")))) + +;; History of grep commands. +(defvar reftex-grep-history nil) +(defvar reftex-grep-command "grep -n " + "Last grep command used in \\[reftex-grep-document]; default for next grep.") + +(defun reftex-grep-document (grep-cmd) + "Run grep query through all files related to this document. +With prefix arg, force to rescan document. +No active TAGS table is required." + + (interactive + (list (read-from-minibuffer "Run grep on document (like this): " + reftex-grep-command nil nil + 'reftex-grep-history))) + (reftex-access-scan-info current-prefix-arg) + (let* ((files (reftex-all-document-files t)) + (cmd (format + "%s %s" grep-cmd + (mapconcat 'identity files " ")))) + (grep cmd))) + +(defun reftex-search-document (&optional regexp) + "Regexp search through all files of the current document. +Starts always in the master file. Stops when a match is found. +To continue searching for next match, use command \\[tags-loop-continue]. +No active TAGS table is required." + (interactive) + (let ((default (reftex-this-word))) + (unless regexp + (setq regexp (read-string (format "Search regexp in document [%s]: " + default)))) + (if (string= regexp "") (setq regexp (regexp-quote default))) + + (reftex-access-scan-info current-prefix-arg) + (tags-search regexp (list 'reftex-all-document-files)))) + +(defun reftex-query-replace-document (&optional from to delimited) + "Run a query-replace-regexp of FROM with TO over the entire document. +Third arg DELIMITED (prefix arg) means replace only word-delimited matches. +If you exit (\\[keyboard-quit] or ESC), you can resume the query replace +with the command \\[tags-loop-continue]. +No active TAGS table is required." + (interactive) + (let ((default (reftex-this-word))) + (unless from + (setq from (read-string (format "Replace regexp in document [%s]: " + default))) + (if (string= from "") (setq from (regexp-quote default)))) + (unless to + (setq to (read-string (format "Replace regexp %s with: " from)))) + (reftex-access-scan-info current-prefix-arg) + (tags-query-replace from to (or delimited current-prefix-arg) + (list 'reftex-all-document-files)))) + +(defun reftex-find-duplicate-labels () + "Produce a list of all duplicate labels in the document." + + (interactive) + + ;; Rescan the document to make sure + (reftex-access-scan-info t) + + (let ((master (reftex-TeX-master-file)) + (cnt 0) + (dlist + (mapcar + (lambda (x) + (let (x1) + (cond + ((memq (car x) + '(toc bof eof bib thebib label-numbers xr xr-doc + master-dir file-error bibview-cache appendix + is-multi index)) + nil) + (t + (setq x1 (reftex-all-assoc-string + (car x) (symbol-value reftex-docstruct-symbol))) + (if (< 1 (length x1)) + (append (list (car x)) + (mapcar (lambda(x) + (abbreviate-file-name (nth 3 x))) + x1)) + (list nil)))))) + (reftex-uniquify-by-car (symbol-value reftex-docstruct-symbol))))) + + (setq dlist (reftex-uniquify-by-car dlist)) + (if (null dlist) (error "No duplicate labels in document")) + (switch-to-buffer-other-window "*Duplicate Labels*") + (set (make-local-variable 'TeX-master) master) + (erase-buffer) + (insert " MULTIPLE LABELS IN CURRENT DOCUMENT:\n") + (insert + " Move point to label and type `r' to run a query-replace on the label\n" + " and its references. Type `q' to exit this buffer.\n\n") + (insert " LABEL FILE\n") + (insert " -------------------------------------------------------------\n") + (use-local-map (make-sparse-keymap)) + (local-set-key [?q] (lambda () "Kill this buffer." (interactive) + (kill-buffer (current-buffer)) (delete-window))) + (local-set-key [?r] 'reftex-change-label) + (while dlist + (when (and (car (car dlist)) + (cdr (car dlist))) + (incf cnt) + (insert (mapconcat 'identity (car dlist) "\n ") "\n")) + (pop dlist)) + (goto-char (point-min)) + (when (= cnt 0) + (kill-buffer (current-buffer)) + (delete-window) + (message "Document does not contain duplicate labels.")))) + +(defun reftex-change-label (&optional from to) + "Query replace FROM with TO in all \\label and \\ref commands. +Works on the entire multifile document. +If you exit (\\[keyboard-quit] or ESC), you can resume the query replace +with the command \\[tags-loop-continue]. +No active TAGS table is required." + (interactive) + (let ((default (reftex-this-word "-a-zA-Z0-9_*.:"))) + (unless from + (setq from (read-string (format "Replace label globally [%s]: " + default)))) + (if (string= from "") (setq from default)) + (unless to + (setq to (read-string (format "Replace label %s with: " + from)))) + (reftex-query-replace-document + (concat "\\\\\\(label\\|[a-z]*ref\\){" (regexp-quote from) "}") + (format "\\\\\\1{%s}" to)))) + +(defun reftex-renumber-simple-labels () + "Renumber all simple labels in the document to make them sequentially. +Simple labels are the ones created by RefTeX, consisting only of the +prefix and a number. After the command completes, all these labels will +have sequential numbers throughout the document. Any references to +the labels will be changed as well. For this, RefTeX looks at the +arguments of any macros which either start or end in the string `ref'. +This command should be used with care, in particular in multifile +documents. You should not use it if another document refers to this +one with the `xr' package." + (interactive) + ;; Resan the entire document + (reftex-access-scan-info 1) + ;; Get some insurance + (if (and (reftex-is-multi) + (not (yes-or-no-p "Replacing all simple labels in multiple files is risky. Continue? "))) + (error "Abort")) + ;; Make the translation list + (let* ((re-core (concat "\\(" + (mapconcat 'cdr reftex-typekey-to-prefix-alist "\\|") + "\\)")) + (label-re (concat "\\`" re-core "\\([0-9]+\\)\\'")) + (search-re (concat "[{,]\\(" re-core "\\([0-9]+\\)\\)[,}]")) + (error-fmt "Undefined label or reference %s. Ignore and continue? ") + (label-numbers-alist (mapcar (lambda (x) (cons (cdr x) 0)) + reftex-typekey-to-prefix-alist)) + (files (reftex-all-document-files)) + (list (symbol-value reftex-docstruct-symbol)) + translate-alist n entry label new-label nr-cell changed-sequence) + + (while (setq entry (pop list)) + (when (and (stringp (car entry)) + (string-match label-re (car entry))) + (setq label (car entry) + nr-cell (assoc (match-string 1 (car entry)) + label-numbers-alist)) + (if (assoc label translate-alist) + (error "Duplicate label %s" label)) + (setq new-label (concat (match-string 1 (car entry)) + (incf (cdr nr-cell)))) + (push (cons label new-label) translate-alist) + (or (string= label new-label) (setq changed-sequence t)))) + + (unless changed-sequence + (error "Simple labels are already in correct sequence")) + + ;; Save all document buffers before this operation + (reftex-save-all-document-buffers) + + ;; First test to check for erros + (setq n (reftex-translate + files search-re translate-alist error-fmt 'test)) + + ;; Now the real thing. + (if (yes-or-no-p + (format "Replace %d items at %d places in %d files? " + (length translate-alist) n (length files))) + (progn + (let ((inhibit-quit t)) ;; Do not disturb... + (reftex-translate + files search-re translate-alist error-fmt nil) + (setq quit-flag nil)) + (if (and (reftex-is-multi) + (yes-or-no-p "Save entire document? ")) + (reftex-save-all-document-buffers)) + ;; Rescan again... + (reftex-access-scan-info 1) + (message "Done replacing simple labels.")) + (message "No replacements done")))) + +(defun reftex-translate (files search-re translate-alist error-fmt test) + ;; In FILES, look for SEARCH-RE and replace match 1 of it with + ;; its association in TRANSLATE-ALSIT. + ;; If we do not find an association and TEST is non-nil, query + ;; to ignore the problematic string. + ;; If TEST is nil, it is ignored without query. + ;; Return the number of replacements. + (let ((n 0) file label match-data buf macro pos cell) + (while (setq file (pop files)) + (setq buf (reftex-get-file-buffer-force file)) + (unless buf + (error "No such file %s" file)) + (set-buffer buf) + (save-excursion + (save-restriction + (widen) + (goto-char (point-min)) + (while (re-search-forward search-re nil t) + (backward-char) + (save-excursion + (setq label (reftex-match-string 1) + cell (assoc label translate-alist) + match-data (match-data) + macro (reftex-what-macro 1) + pos (cdr macro)) + (goto-char (or pos (point))) + (when (and macro + (or (looking-at "\\\\ref") + (looking-at "\\\\[a-zA-Z]*ref\\(range\\)?[^a-zA-Z]") + (looking-at "\\\\ref[a-zA-Z]*[^a-zA-Z]") + (looking-at (format + reftex-find-label-regexp-format + (regexp-quote label))))) + ;; OK, we should replace it. + (set-match-data match-data) + (cond + ((and test (not cell)) + ;; We've got a problem + (unwind-protect + (progn + (reftex-highlight 1 (match-beginning 0) (match-end 0)) + (ding) + (or (y-or-n-p (format error-fmt label)) + (error "Abort"))) + (reftex-unhighlight 1))) + ((and test cell) + (incf n)) + ((and (not test) cell) + ;; Replace + (goto-char (match-beginning 1)) + (delete-region (match-beginning 1) (match-end 1)) + (insert (cdr cell))) + (t nil)))))))) + n)) + +(defun reftex-save-all-document-buffers () + "Save all documents associated with the current document. +The function is useful after a global action like replacing or renumbering +labels." + (interactive) + (let ((files (reftex-all-document-files)) + file buffer) + (save-excursion + (while (setq file (pop files)) + (setq buffer (reftex-get-buffer-visiting file)) + (when buffer + (set-buffer buffer) + (save-buffer)))))) + + +;;; reftex-global.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-index.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,1219 @@ +;;; reftex-index.el - Index support with RefTeX +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-index) +(require 'reftex) +;;; + +(defvar mark-active) +(defvar zmacs-regions) +(defun reftex-index-selection-or-word (&optional arg) + "Put selection or the word near point into the default index macro. +This uses the information in `reftex-index-default-macro' to make an index +entry. The phrase indexed is the current selection or the word near point. +When called with one `C-u' prefix, let the user have a chance to edit the +index entry. When called with 2 `C-u' as prefix, also ask for the index +macro and other stuff. +When called inside TeX math mode as determined by the `texmathp.el' library +which is part of AUCTeX, the string is first processed with the +`reftex-index-math-format', which see." + (interactive "P") + (let* ((use-default (not (equal arg '(16)))) ; check for double prefix + ;; check if we have an active selection + (active (if (boundp 'zmacs-regions) + (and zmacs-regions (region-exists-p)) ; XEmacs + (and transient-mark-mode mark-active))) ; Emacs + (beg (if active + (region-beginning) + (save-excursion + (skip-syntax-backward "w\\") (point)))) + (end (if active + (region-end) + (save-excursion + (skip-syntax-forward "w\\") (point)))) + (sel (buffer-substring beg end)) + (mathp (condition-case nil (texmathp) (error nil))) + (current-prefix-arg nil) ; we want to call reftex-index without prefix. + key def-char def-tag full-entry repeat-word) + + (if (equal sel "") + ;; Nothing selecte, no word, so use full reftex-index command + (reftex-index) + ;; OK, we have something to index here. + ;; Add the dollars when necessary + (setq key (if mathp + (format reftex-index-math-format sel) + sel)) + ;; Get info from `reftex-index-default-macro' + (setq def-char (if use-default (car reftex-index-default-macro))) + (setq def-tag (if use-default (nth 1 reftex-index-default-macro))) + ;; Does the user want to edit the entry? + (setq full-entry (if arg + (reftex-index-complete-key + def-tag nil (cons key 0)) + key)) + ;; Do we neet to repeat the word outside the macro? + (setq repeat-word (if use-default + (nth 2 reftex-index-default-macro) + (y-or-n-p "Repeat phrase outside macro? "))) + ;; Delete what is in the buffer and make the index entry + (delete-region beg end) + (reftex-index def-char full-entry def-tag (if repeat-word sel nil))))) + +(defun reftex-index (&optional char key tag postfix no-insert) + "Query for an index macro and insert it along with its argments. +The index macros available are those defined in `reftex-index-macro' or +by a call to `reftex-add-index-macros', typically from an AUCTeX style file. +RefteX provides completion for the index tag and the index key, and +will prompt for other arguments." + + (interactive) + + ;; Ensure access to scanning info + (reftex-ensure-index-support t) + (reftex-access-scan-info current-prefix-arg) + + ;; Find out which macro we are going to use + (let* ((char (or char + (reftex-select-with-char reftex-query-index-macro-prompt + reftex-query-index-macro-help))) + (macro (nth 1 (assoc char reftex-key-to-index-macro-alist))) + (entry (or (assoc macro reftex-index-macro-alist) + (error "No index macro associated with %c" char))) + (ntag (nth 1 entry)) + (tag (or tag (nth 1 entry))) + (nargs (nth 4 entry)) + (nindex (nth 5 entry)) + (opt-args (nth 6 entry)) + opt tag1 value) + + ;; Get the supported arguments + (if (stringp tag) + (setq tag1 tag) + (setq tag1 (or (reftex-index-complete-tag tag opt-args) ""))) + (setq key (or key + (reftex-index-complete-key + (if (string= tag1 "") "idx" tag1) + (member nindex opt-args)))) + + ;; Insert the macro and ask for any additional args + (insert macro) + (loop for i from 1 to nargs do + (setq opt (member i opt-args) + value (cond ((= nindex i) key) + ((equal ntag i) tag1) + (t (read-string (concat "Macro arg nr. " + (int-to-string i) + (if opt " (optional)" "") + ": "))))) + (unless (and opt (string= value "")) + (insert (if opt "[" "{") value (if opt "]" "}")))) + (and (stringp postfix) (insert postfix)) + (and key reftex-plug-into-AUCTeX (fboundp 'LaTeX-add-index-entries) + (LaTeX-add-index-entries key)) + (reftex-index-update-taglist tag1) + (reftex-notice-new))) + +(defun reftex-default-index () + (cond ((null reftex-index-default-tag) nil) + ((stringp reftex-index-default-tag) reftex-index-default-tag) + (t (or (get reftex-docstruct-symbol 'default-index-tag) + "idx")))) + +(defun reftex-update-default-index (tag &optional tag-list) + (if (and (not (equal tag "")) + (stringp tag) + (eq reftex-index-default-tag 'last) + (or (null tag-list) + (member tag tag-list))) + (put reftex-docstruct-symbol 'default-index-tag tag))) + +(defun reftex-index-complete-tag (&optional itag opt-args) + ;; Ask the user for a tag, completing on known tags. + ;; ITAG is the argument number which contains the tag. + ;; OPT-ARGS is a list of optional argument indices, as given by + ;; `reftex-parse-args'. + (let* ((opt (and (integerp itag) (member itag opt-args))) + (index-tags (cdr (assq 'index-tags + (symbol-value reftex-docstruct-symbol)))) + (default (reftex-default-index)) + (prompt (concat "Index tag" + (if default (format " (default: %s)" default) "") + (if opt " (optional)" "") ": ")) + (tag (completing-read prompt (mapcar 'list index-tags)))) + (if (and default (equal tag "")) (setq tag default)) + (reftex-update-default-index tag) + tag)) + +(defun reftex-index-select-tag () + ;; Have the user select an index tag. + ;; FIXME: should we cache tag-alist, prompt and help? + (let* ((index-tags (cdr (assoc 'index-tags + (symbol-value reftex-docstruct-symbol)))) + (default (reftex-default-index))) + (cond + ((null index-tags) + (error "No index tags available")) + + ((= (length index-tags) 1) + ;; Just one index, use it + (car index-tags)) + + ((> (length index-tags) 1) + ;; Several indices, ask. + (let* ((tags (copy-sequence index-tags)) + (cnt 0) + tag-alist i val len tag prompt help rpl) + ;; Move idx and glo up in the list to ensure ?i and ?g shortcuts + (if (member "glo" tags) + (setq tags (cons "glo" (delete "glo" tags)))) + (if (member "idx" tags) + (setq tags (cons "idx" (delete "idx" tags)))) + ;; Find unique shortcuts for each index. + (while (setq tag (pop tags)) + (setq len (length tag) + i -1 + val nil) + (catch 'exit + (while (and (< (incf i) len) (null val)) + (unless (assq (aref tag i) tag-alist) + (push (list (aref tag i) + tag + (concat (substring tag 0 i) + "[" (substring tag i (incf i)) "]" + (substring tag i))) + tag-alist) + (throw 'exit t))) + (push (list (+ ?0 (incf cnt)) tag + (concat "[" (int-to-string cnt) "]:" tag)) + tag-alist))) + (setq tag-alist (nreverse tag-alist)) + ;; Compute Prompt and Help strings + (setq prompt + (concat + (format "Select Index%s: " + (if default (format " (Default <%s>)" default) "")) + (mapconcat (lambda(x) (nth 2 x)) tag-alist " "))) + (setq help + (concat "Select an Index\n===============\n" + (if default + (format "[^M] %s (the default)\n" default) + "") + (mapconcat (lambda(x) + (apply 'format "[%c] %s" x)) + tag-alist "\n"))) + ;; Query the user for an index-tag + (setq rpl (reftex-select-with-char prompt help 3 t)) + (message "") + (if (and default (equal rpl ?\C-m)) + default + (if (assq rpl tag-alist) + (progn + (reftex-update-default-index (nth 1 (assq rpl tag-alist))) + (nth 1 (assq rpl tag-alist))) + (error "No index tag associated with %c" rpl))))) + (t (error "This should not happen (reftex-index-select-tag)"))))) + +(defun reftex-index-complete-key (&optional tag optional initial) + ;; Read an index key, with completion. + ;; Restrict completion table on index tag TAG. + ;; OPTIONAL indicates if the arg is optional. + (let* ((table (reftex-sublist-nth + (symbol-value reftex-docstruct-symbol) 6 + (lambda(x) (and (eq (car x) 'index) + (string= (nth 1 x) (or tag "")))) + t)) + (prompt (concat "Index key" (if optional " (optional)" "") ": ")) + (key (completing-read prompt table nil nil initial))) + key)) + +(defun reftex-index-update-taglist (newtag) + ;; add NEWTAG to the list of available index tags. + (let ((cell (assoc 'index-tags (symbol-value reftex-docstruct-symbol)))) + (and newtag (cdr cell) (not (member newtag (cdr cell))) + (push newtag (cdr cell))))) + +(defvar reftex-last-index-file) +(defun reftex-index-globally (&optional data call-file) + "Index a word with a global search and replace. +This works very much like `reftex-query-replace-document', but the +defaults for the search and replace strings are derived from +local context. +When there is an index entry, we try to index similar words. The word +to search for is either a word in direct contact with the index macro +(like `\\index{WORD}WORD' or `WORD\\index{WORD}') or the index key. +The replacement text is the index macro with all its arguments and the +attached word. +When there is no index entry at point, we search for the word near point +and propose to index it like this: `\\index{word}word'. +You get a chance to edit the search and replacement strings. +DATA can be a docstruct entry describing an index entry, and then the +defaults will be derived from it. +CALL-FILE may be the file from where to call the global search command." + (interactive) + (let* ((call-file (cond (call-file call-file) + (reftex-mode (buffer-file-name)) + ((eq major-mode 'reftex-index-mode) + reftex-last-index-file) + (t (error "Need a call file here")))) + (pos (point)) + (data (cond + (data data) + ((and reftex-mode + (save-excursion + (forward-char 20) + (re-search-backward reftex-everything-regexp nil t) + (< (count-lines (min pos (point)) (max pos (point))) + 2))) + (reftex-index-info (buffer-file-name))) + (t nil))) + (ksep (car reftex-index-special-chars)) + (words-include-escapes t) + (case-replace nil) + (case-fold-search t) + word rpl start analyze-list pre key attr actual post) + + ;; Find the word and construct the replacement string + (if (and data (eq (car data) 'index)) + ;; OK, we have an index entry + (progn + (setq analyze-list (reftex-index-analyze-entry data) + pre (car analyze-list) + key (nth 1 analyze-list) + attr (nth 2 analyze-list) + actual (nth 3 analyze-list) + post (nth 4 analyze-list)) + (when (string-match (concat "\\<\\(\\sw+\\)" reftex-index-re) pre) + (setq word (match-string 1 pre) + pre (concat "<<<1>>>" (substring pre (match-end 1))) + rpl (concat pre key attr actual post))) + (when (string-match "}\\(\\sw+\\)\\>[^}]*\\'" post) + (setq word (match-string 1 post) + post (concat (substring post 0 (match-beginning 1)) + "<<<1>>>") + rpl (concat pre key attr actual post))) + (when (and (not word) key) + (if (string-match (concat ".*" (regexp-quote ksep)) key) + (setq word (substring key (match-end 0))) + (setq word key)) + (setq rpl (concat pre key attr actual post)))) + ;; No index entry, just use local word. + (setq word (save-excursion + (buffer-substring-no-properties + (progn (skip-syntax-backward "w") (point)) + (progn (skip-syntax-forward "w") (point)))) + rpl (concat "\\index{" word "}<<<1>>>"))) + ;; Quote what is necessary + (setq word (regexp-quote (downcase word))) + (setq start 0) + (while (setq start (string-match "\\\\" rpl start)) + (setq rpl (replace-match "\\\\" t t rpl) + start (+ 2 start))) + ;; We used <<<1>>> instead of \1 to avoid the quoting. Fix this now. + (if (string-match "<<<1>>>" rpl) + (setq rpl (replace-match "\\1" t t rpl))) + + ;; Give the user a chance to edit the strings + (setq word (read-string "Search: " + (if word (format "\\<\\(%s\\)\\>" word))) + rpl (read-string "Replace with: " rpl)) + + ;; Execute the command + (save-excursion + (switch-to-buffer (get-file-buffer call-file)) + (condition-case nil + (reftex-query-replace-document word rpl) + (error nil))))) + +(defvar reftex-index-map (make-sparse-keymap) + "Keymap used for *Index* buffers.") + +(defvar reftex-index-menu) + +(defvar reftex-last-index-file nil + "Stores the file name from which `reftex-display-index' was called.") +(defvar reftex-index-tag nil + "Stores the tag of the index in an index buffer.") + +(defvar reftex-index-return-marker (make-marker) + "Marker which makes it possible to return from index to old position.") + +(defvar reftex-index-restriction-indicator nil) +(defvar reftex-index-restriction-data nil) + +(defun reftex-index-mode () + "Major mode for managing Index buffers for LaTeX files. +This buffer was created with RefTeX. +Press `?' for a summary of important key bindings, or check the menu. + +Here are all local bindings. + +\\{reftex-index-map}" + (interactive) + (kill-all-local-variables) + (setq major-mode 'reftex-index-mode + mode-name "RefTeX Index") + (use-local-map reftex-index-map) + (set (make-local-variable 'revert-buffer-function) 'reftex-index-revert) + (set (make-local-variable 'reftex-index-restriction-data) nil) + (set (make-local-variable 'reftex-index-restriction-indicator) nil) + (setq mode-line-format + (list "---- " 'mode-line-buffer-identification + " " 'global-mode-string + " R<" 'reftex-index-restriction-indicator ">" + " -%-")) + (setq truncate-lines t) + (make-local-hook 'post-command-hook) + (make-local-hook 'pre-command-hook) + (make-local-variable 'reftex-last-follow-point) + (easy-menu-add reftex-index-menu reftex-index-map) + (add-hook 'post-command-hook 'reftex-index-post-command-hook nil t) + (add-hook 'pre-command-hook 'reftex-index-pre-command-hook nil t) + (run-hooks 'reftex-index-mode-hook)) + +(defconst reftex-index-help +" AVAILABLE KEYS IN INDEX BUFFER + ============================== +! A..Z Goto the section of entries starting with this letter. +n / p next-entry / previous-entry +SPC / TAB Show/Goto the corresponding entry in the LaTeX document. +RET Goto the entry and hide the *Index* window (also on mouse-2). +q / k Hide/Kill *Index* buffer. +C-c = Switch to the TOC buffer. +f / c Toggle follow mode / Toggle display of [c]ontext. +g Refresh *Index* buffer. +r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document. +s Switch to a different index (for documents with multiple indices). +e / C-k Edit/Kill the entry. +* | @ Edit specific part of entry: [*]key [|]attribute [@]visual + With prefix: kill that part. +( ) Toggle entry's beginning/end of page range property. +_ ^ Add/Remove parent key (to make this item a subitem). +& Index the same word everywhere in the document. +} / { Restrict Index to a single document section / Widen. +< / > When restricted, move restriction to previous/next section.") + +(defun reftex-index-show-entry (data &optional no-revisit) + ;; Find an index entry associated with DATA and display it highlighted + ;; in another window. NO-REVISIT means we are not allowed to visit + ;; files for this. + ;; Note: This function just looks for the nearest match of the + ;; context string and may fail if the entry moved and an identical + ;; entry is close to the old position. Frequent rescans make this + ;; safer. + (let* ((file (nth 3 data)) + (literal (nth 2 data)) + (pos (nth 4 data)) + (re (regexp-quote literal)) + (match + (cond + ((or (not no-revisit) + (reftex-get-buffer-visiting file)) + (switch-to-buffer-other-window + (reftex-get-file-buffer-force file nil)) + (goto-char (or pos (point-min))) + (or (looking-at re) + (reftex-nearest-match re (length literal)))) + (t (message reftex-no-follow-message) nil)))) + (when match + (goto-char (match-beginning 0)) + (recenter '(4)) + (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer))) + match)) + +(defun reftex-display-index (&optional tag overriding-restriction + &rest locations) + "Display a buffer with an index compiled from the current document. +When the document has multiple indices, first prompts for the correct one. +When index support is turned off, offer to turn it on. +With one or two `C-u' prefixes, rescan document first. +With prefix 2, restrict index to current document section. +With prefix 3, restrict index to region." + + (interactive) + + ;; Ensure access to scanning info and rescan buffer if prefix are is '(4). + (let ((current-prefix-arg current-prefix-arg)) + (reftex-ensure-index-support t) + (reftex-access-scan-info current-prefix-arg)) + + (set-marker reftex-index-return-marker (point)) + (setq reftex-last-follow-point 1) + + ;; Determine the correct index to process + (let* ((docstruct (symbol-value reftex-docstruct-symbol)) + (docstruct-symbol reftex-docstruct-symbol) + (index-tag (or tag (reftex-index-select-tag))) + (master (reftex-TeX-master-file)) + (calling-file (buffer-file-name)) + (restriction + (or overriding-restriction + (and (interactive-p) + (reftex-get-restriction current-prefix-arg docstruct)))) + (locations + ;; See if we are on an index macro as initial position + (or locations + (let* ((what-macro (reftex-what-macro-safe 1)) + (macro (car what-macro)) + (here-I-am (when (member macro reftex-macros-with-index) + (save-excursion + (goto-char (+ (cdr what-macro) + (length macro))) + (reftex-move-over-touching-args) + (reftex-where-am-I))))) + (if (eq (car (car here-I-am)) 'index) + (list (car here-I-am)))))) + buffer-name) + + (setq buffer-name (reftex-make-index-buffer-name index-tag)) + + ;; Goto the buffer and put it into the correct mode + + (when (or restriction current-prefix-arg) + (reftex-kill-buffer buffer-name)) + + (if (get-buffer-window buffer-name) + (select-window (get-buffer-window buffer-name)) + (let ((default-major-mode 'reftex-index-mode)) + (switch-to-buffer buffer-name))) + + (or (eq major-mode 'reftex-index-mode) (reftex-index-mode)) + + ;; If the buffer is currently restricted, empty it to force update. + (when reftex-index-restriction-data + (reftex-erase-buffer)) + (set (make-local-variable 'reftex-last-index-file) calling-file) + (set (make-local-variable 'reftex-index-tag) index-tag) + (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol) + (if restriction + (setq reftex-index-restriction-indicator (car restriction) + reftex-index-restriction-data (cdr restriction)) + (if (interactive-p) + (setq reftex-index-restriction-indicator nil + reftex-index-restriction-data nil))) + (when (= (buffer-size) 0) + ;; buffer is empty - fill it + (message "Building %s buffer..." buffer-name) + + (setq buffer-read-only nil) + (insert (format +"INDEX <%s> on %s +Restriction: <%s> +SPC=view TAB=goto RET=goto+hide [e]dit [q]uit [r]escan [f]ollow [?]Help +------------------------------------------------------------------------------ +" index-tag (abbreviate-file-name master) +(if (eq (car (car reftex-index-restriction-data)) 'toc) + (nth 2 (car reftex-index-restriction-data)) + reftex-index-restriction-indicator))) + + (if (reftex-use-fonts) + (put-text-property 1 (point) 'face reftex-index-header-face)) + (put-text-property 1 (point) 'intangible t) + + (reftex-insert-index docstruct index-tag) + (goto-char (point-min)) + (run-hooks 'reftex-display-copied-context-hook) + (message "Building %s buffer...done." buffer-name) + (setq buffer-read-only t)) + (and locations (apply 'reftex-find-start-point (point) locations)) + (if reftex-index-restriction-indicator + (message "Index restricted: <%s>" reftex-index-restriction-indicator)))) + +(defun reftex-insert-index (docstruct tag &optional update-one remark) + ;; Insert an index into the current buffer. Entries are from the + ;; DOCSTRUCT. + ;; TAG is the subindex to process. + ;; UPDATE-ONE: When non-nil, delete the entry at point and replace + ;; it with whatever the DOCSTRUCT contains. + ;; REMARK can be a note to add to the entry. + (let* ((all docstruct) + (indent " ") + (context reftex-index-include-context) + (context-indent (concat indent " ")) + (section-chars (mapcar 'identity reftex-index-section-letters)) + (this-section-char 0) + (font (reftex-use-fonts)) + (bor (car reftex-index-restriction-data)) + (eor (nth 1 reftex-index-restriction-data)) + (mouse-face + (if (memq reftex-highlight-selection '(mouse both)) + reftex-mouse-selected-face + nil)) + (index-face (reftex-verified-face reftex-label-face + 'font-lock-constant-face + 'font-lock-reference-face)) + sublist cell from to first-char) + + ;; Make the sublist and sort it + (when bor + (setq all (or (memq bor all) all))) + + (while (setq cell (pop all)) + (if (eq cell eor) + (setq all nil) + (and (eq (car cell) 'index) + (equal (nth 1 cell) tag) + (push cell sublist)))) + (setq sublist (sort (nreverse sublist) + (lambda (a b) (string< (nth 8 a) (nth 8 b))))) + + (when update-one + ;; Delete the entry at place + (and (bolp) (forward-char 1)) + (delete-region (previous-single-property-change (1+ (point)) :data) + (or (next-single-property-change (point) :data) + (point-max)))) + + ;; Walk through the list and insert all entries + (while (setq cell (pop sublist)) + (unless update-one + (setq first-char (upcase (string-to-char (nth 6 cell)))) + (when (and (not (equal first-char this-section-char)) + (member first-char section-chars)) + ;; There is a new initial letter, so start a new section + (reftex-index-insert-new-letter first-char font) + (setq section-chars (delete first-char section-chars) + this-section-char first-char)) + (when (= this-section-char 0) + (setq this-section-char ?!) + (reftex-index-insert-new-letter this-section-char font))) + + (setq from (point)) + (insert indent (nth 7 cell)) + (when font + (setq to (point)) + (put-text-property + (- (point) (length (nth 7 cell))) to + 'face index-face) + (goto-char to)) + + (when (or remark (nth 9 cell)) + (and (< (current-column) 40) + ;; FIXME: maybe this is too slow? + (insert (make-string (max (- 40 (current-column)) 0) ?\ ))) + (and (nth 9 cell) (insert " " (substring (nth 5 cell) (nth 9 cell)))) + (and remark (insert " " remark))) + + (insert "\n") + (setq to (point)) + + (when context + (insert context-indent (nth 2 cell) "\n") + (setq to (point))) + (put-text-property from to :data cell) + (when mouse-face + (put-text-property from (1- to) + 'mouse-face mouse-face)) + (goto-char to)))) + + +(defun reftex-index-insert-new-letter (letter &optional font) + ;; Start a new section in the index + (let ((from (point))) + (insert "\n" letter letter letter + "-----------------------------------------------------------------") + (when font + (put-text-property from (point) 'face reftex-index-section-face)) + (insert "\n"))) + +(defun reftex-get-restriction (arg docstruct) + ;; Interprete the prefix ARG and derive index restriction specs. + (let* ((beg (min (point) (or (condition-case nil (mark) (error nil)) + (point-max)))) + (end (max (point) (or (condition-case nil (mark) (error nil)) + (point-min)))) + bor eor label here-I-am) + (cond + ((eq arg 2) + (setq here-I-am (car (reftex-where-am-I)) + bor (if (eq (car here-I-am) 'toc) + here-I-am + (reftex-last-assoc-before-elt + 'toc here-I-am docstruct)) + eor (car (memq (assq 'toc (cdr (memq bor docstruct))) docstruct)) + label (nth 6 bor))) + ((eq arg 3) + (save-excursion + (setq label "region") + (goto-char beg) + (setq bor (car (reftex-where-am-I))) + (setq bor (nth 1 (memq bor docstruct))) + (goto-char end) + (setq eor (nth 1 (memq (car (reftex-where-am-I)) docstruct))))) + (t nil)) + (if (and label (or bor eor)) + (list label bor eor) + nil))) + +(defun reftex-index-pre-command-hook () + ;; Used as pre command hook in *Index* buffer + (reftex-unhighlight 0) + (reftex-unhighlight 1)) + +(defun reftex-index-post-command-hook () + ;; Used in the post-command-hook for the *Index* buffer + (when (get-text-property (point) :data) + (and (> (point) 1) + (not (get-text-property (point) 'intangible)) + (memq reftex-highlight-selection '(cursor both)) + (reftex-highlight 1 + (or (previous-single-property-change (1+ (point)) :data) + (point-min)) + (or (next-single-property-change (point) :data) + (point-max))))) + (if (integerp reftex-index-follow-mode) + ;; Remove delayed action + (setq reftex-index-follow-mode t) + (and reftex-index-follow-mode + (not (equal reftex-last-follow-point (point))) + ;; Show context in other window + (setq reftex-last-follow-point (point)) + (condition-case nil + (reftex-index-visit-location nil (not reftex-revisit-to-follow)) + (error t))))) + +(defun reftex-index-show-help () + "Show a summary of special key bindings." + (interactive) + (with-output-to-temp-buffer "*RefTeX Help*" + (princ reftex-index-help)) + (reftex-enlarge-to-fit "*RefTeX Help*" t) + ;; If follow mode is active, arrange to delay it one command + (if reftex-index-follow-mode + (setq reftex-index-follow-mode 1))) + +(defun reftex-index-next (&optional arg) + "Move to next selectable item." + (interactive "p") + (setq reftex-callback-fwd t) + (or (eobp) (forward-char 1)) + (goto-char (or (next-single-property-change (point) :data) + (point))) + (unless (get-text-property (point) :data) + (goto-char (or (next-single-property-change (point) :data) + (point))))) +(defun reftex-index-previous (&optional arg) + "Move to previous selectable item." + (interactive "p") + (setq reftex-callback-fwd nil) + (goto-char (or (previous-single-property-change (point) :data) + (point))) + (unless (get-text-property (point) :data) + (goto-char (or (previous-single-property-change (point) :data) + (point))))) +(defun reftex-index-toggle-follow () + "Toggle follow (other window follows with context)." + (interactive) + (setq reftex-last-follow-point -1) + (setq reftex-index-follow-mode (not reftex-index-follow-mode))) +(defun reftex-index-toggle-context () + "Toggle inclusion of label context in *Index* buffer. +Label context is only displayed when the labels are there as well." + (interactive) + (setq reftex-index-include-context (not reftex-index-include-context)) + (reftex-index-revert)) +(defun reftex-index-view-entry () + "View document location in other window." + (interactive) + (reftex-index-visit-location)) +(defun reftex-index-goto-entry-and-hide () + "Go to document location in other window. Hide the *Index* window." + (interactive) + (reftex-index-visit-location 'hide)) +(defun reftex-index-goto-entry () + "Go to document location in other window. *Index* window stays." + (interactive) + (reftex-index-visit-location t)) +(defun reftex-index-mouse-goto-line-and-hide (ev) + "Go to document location in other window. Hide the *Index* window." + (interactive "e") + (mouse-set-point ev) + (reftex-index-visit-location 'hide)) +(defun reftex-index-quit () + "Hide the *Index* window and do not move point." + (interactive) + (or (one-window-p) (delete-window)) + (switch-to-buffer (marker-buffer reftex-index-return-marker)) + (goto-char (or (marker-position reftex-index-return-marker) (point)))) +(defun reftex-index-quit-and-kill () + "Kill the *Index* buffer." + (interactive) + (kill-buffer (current-buffer)) + (or (one-window-p) (delete-window)) + (switch-to-buffer (marker-buffer reftex-index-return-marker)) + (goto-char (or (marker-position reftex-index-return-marker) (point)))) +(defun reftex-index-goto-toc (&rest ignore) + "Switch to the table of contents of the current document. +The function will go to the section where the entry at point was defined." + (interactive) + (if (get-text-property (point) :data) + (reftex-index-goto-entry) + (switch-to-buffer (marker-buffer reftex-index-return-marker))) + (delete-other-windows) + (reftex-toc)) +(defun reftex-index-rescan (&rest ignore) + "Regenerate the *Index* buffer after reparsing file of section at point." + (interactive) + (let ((index-tag reftex-index-tag)) + (if (and reftex-enable-partial-scans + (null current-prefix-arg)) + (let* ((data (get-text-property (point) :data)) + (file (nth 3 data)) + (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) + (if (not file) + (error "Don't know which file to rescan. Try `C-u r'") + (switch-to-buffer (reftex-get-file-buffer-force file)) + (setq current-prefix-arg '(4)) + (reftex-display-index index-tag nil line))) + (reftex-index-Rescan)) + (reftex-kill-temporary-buffers))) +(defun reftex-index-Rescan (&rest ignore) + "Regenerate the *Index* buffer after reparsing the entire document." + (interactive) + (let ((index-tag reftex-index-tag) + (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) + (switch-to-buffer + (reftex-get-file-buffer-force reftex-last-index-file)) + (setq current-prefix-arg '(16)) + (reftex-display-index index-tag nil line))) +(defun reftex-index-revert (&rest ignore) + "Regenerate the *Index* from the internal lists. No reparsing os done." + (interactive) + (let ((buf (current-buffer)) + (index-tag reftex-index-tag) + (data (get-text-property (point) :data)) + (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) + (switch-to-buffer + (reftex-get-file-buffer-force reftex-last-index-file)) + (reftex-erase-buffer buf) + (setq current-prefix-arg nil + reftex-last-follow-point 1) + (reftex-display-index index-tag nil data line))) +(defun reftex-index-switch-index-tag (&rest ignore) + "Switch to a different index of the same document." + (interactive) + (switch-to-buffer + (reftex-get-file-buffer-force reftex-last-index-file)) + (setq current-prefix-arg nil) + (reftex-display-index)) + +(defun reftex-index-restrict-to-section (&optional force) + "Restrict index to entries defined in same document sect. as entry at point." + ;; Optional FORCE means, even if point is not on an index entry. + (interactive) + (let* ((data (get-text-property (point) :data)) + (docstruct (symbol-value reftex-docstruct-symbol)) + bor eor) + (if (and (not data) force) + (setq data (assq 'toc docstruct))) + (when data + (setq bor (reftex-last-assoc-before-elt 'toc data docstruct) + eor (car (memq (assq 'toc (cdr (memq bor docstruct))) + docstruct)) + reftex-index-restriction-data (list bor eor) + reftex-index-restriction-indicator (nth 6 bor) ))) + (reftex-index-revert)) + +(defun reftex-index-widen (&rest ignore) + "Show the unrestricted index (all entries)." + (interactive) + (setq reftex-index-restriction-indicator nil + reftex-index-restriction-data nil) + (reftex-index-revert) + (message "Index widened")) +(defun reftex-index-restriction-forward (&rest ignore) + "Restrict to previous section. +When index is currently unrestricted, restrict it to a section. +When index is restricted, select the next section as restriction criterion." + (interactive) + (let* ((docstruct (symbol-value reftex-docstruct-symbol)) + (bor (nth 1 reftex-index-restriction-data))) + (if (or (not bor) + (not (eq (car bor) 'toc))) + (reftex-index-restrict-to-section t) + (setq reftex-index-restriction-indicator (nth 6 bor) + reftex-index-restriction-data + (list bor + (car (memq (assq 'toc (cdr (memq bor docstruct))) + docstruct)))) + (reftex-index-revert)))) +(defun reftex-index-restriction-backward (&rest ignore) + "Restrict to next section. +When index is currently unrestricted, restrict it to a section. +When index is restricted, select the previous section as restriction criterion." + (interactive) + (let* ((docstruct (symbol-value reftex-docstruct-symbol)) + (eor (car reftex-index-restriction-data)) + (bor (reftex-last-assoc-before-elt 'toc eor docstruct t))) + (if (or (not bor) + (not (eq (car bor) 'toc))) + (reftex-index-restrict-to-section t) + (setq reftex-index-restriction-indicator (nth 6 bor) + reftex-index-restriction-data + (list bor eor)) + (reftex-index-revert)))) + +(defun reftex-index-visit-location (&optional final no-revisit) + ;; Visit the tex file corresponding to the index entry on the current line. + ;; If FINAL is t, stay there + ;; If FINAL is 'hide, hide the *Index* window. + ;; Otherwise, move cursor back into *Index* window. + ;; NO-REVISIT means don't visit files, just use live biffers. + + (let* ((data (get-text-property (point) :data)) + (index-window (selected-window)) + show-window show-buffer match) + + (unless data (error "Don't know which index entry to visit")) + + (if (eq (car data) 'index) + (setq match (reftex-index-show-entry data no-revisit))) + + (setq show-window (selected-window) + show-buffer (current-buffer)) + + (unless match + (select-window index-window) + (error "Cannot find location")) + + (select-window index-window) + + ;; Use the `final' parameter to decide what to do next + (cond + ((eq final t) + (reftex-unhighlight 0) + (select-window show-window)) + ((eq final 'hide) + (reftex-unhighlight 0) + (or (one-window-p) (delete-window)) + (switch-to-buffer show-buffer)) + (t nil)))) + +(defun reftex-index-analyze-entry (data) + ;; This splits the index context so that key, attribute and visual + ;; values are accessible individually. + (interactive) + (let* ((arg (nth 5 data)) + (context (nth 2 data)) + (sc reftex-index-special-chars) + (boa (if (string-match (regexp-quote (concat "{" arg "}")) context) + (1+ (match-beginning 0)) + (error "Something is wrong here"))) + (eoa (1- (match-end 0))) + (boactual (if (string-match (concat "[^" (nth 3 sc) "]" (nth 2 sc)) + context boa) + (1+ (match-beginning 0)) + eoa)) + (boattr (if (string-match (concat "[^" (nth 3 sc) "]" (nth 1 sc)) + context boa) + (1+ (match-beginning 0)) + boactual)) + (pre (substring context 0 boa)) + (key (substring context boa boattr)) + (attr (substring context boattr boactual)) + (actual (substring context boactual eoa)) + (post (substring context eoa))) + (list pre key attr actual post))) + +(defun reftex-index-globalize (&optional arg) + "Globalize the current index entry. +This starts a global search and replace to index the same word +at other places in the document. After this function completes, you +need to rescan the document with `r' or `C-u r' in order to get the +entries into the index buffer. +Defaults for the search and replace strings are derived from +the current entry. See the command `reftex-index-globally'." + (interactive) + (let* ((data (get-text-property (point) :data)) + (buf (current-buffer))) + (unless data + (error "No index entry at point")) + (reftex-index-globally data) + (switch-to-buffer buf))) + +(defun reftex-index-edit () + "Edit the index entry at point." + (interactive) + (let* ((data (get-text-property (point) :data)) + old new) + (unless data (error "Don't know which index entry to edit")) + (reftex-index-view-entry) + (setq old (nth 2 data) new (read-string "Edit: " old)) + (reftex-index-change-entry new))) + +(defun reftex-index-toggle-range-beginning () + "Toggle the page range start attribute `|('." + (interactive) + (let* ((data (get-text-property (point) :data)) + (bor (concat (nth 1 reftex-index-special-chars) "(")) + new analyze attr) + (unless data (error "Don't know which index entry to edit")) + (setq analyze (reftex-index-analyze-entry data) + attr (nth 2 analyze)) + (setf (nth 2 analyze) (if (string= attr bor) "" bor)) + (setq new (apply 'concat analyze)) + (reftex-index-change-entry + new (if (string= (nth 2 analyze) bor) + "Entry is now START-OF-PAGE-RANGE" + "START-OF-PAGE-RANGE canceled")))) + +(defun reftex-index-toggle-range-end () + "Toggle the page-range-end attribute `|)'." + (interactive) + (let* ((data (get-text-property (point) :data)) + (eor (concat (nth 1 reftex-index-special-chars) "(")) + new analyze attr) + (unless data (error "Don't know which index entry to edit")) + (setq analyze (reftex-index-analyze-entry data) + attr (nth 2 analyze)) + (setf (nth 2 analyze) (if (string= attr eor) "" eor)) + (setq new (apply 'concat analyze)) + (reftex-index-change-entry + new (if (string= (nth 2 analyze) eor) + "Entry is now END-OF-PAGE-RANGE" + "END-OF-PAGE-RANGE canceled")))) + +(defun reftex-index-edit-key () + "Edit the KEY part of the index entry." + (interactive) + (reftex-index-edit-part nil 1 "" "Key: " t)) + +(defun reftex-index-edit-attribute (&optional arg) + "EDIT the ATTRIBUTE part of the entry. With arg: remove entire ATTRIBUTE." + (interactive "P") + (reftex-index-edit-part arg 2 (nth 1 reftex-index-special-chars) + "Attribute: ")) + +(defun reftex-index-edit-visual (&optional arg) + "EDIT the VISUAL part of the entry. With arg: remove entire VISUAL string." + (interactive "P") + (reftex-index-edit-part arg 3 (nth 2 reftex-index-special-chars) "Visual: ")) + +(defun reftex-index-edit-part (arg n initial prompt &optional dont-allow-empty) + ;; This function does the work for all partial editing commands + (let* ((data (get-text-property (point) :data)) + new analyze opart npart) + (unless data (error "Don't know which index entry to edit")) + ;; Analyze the whole context string + (setq analyze (reftex-index-analyze-entry data) + opart (nth n analyze)) + (and (> (length opart) 0) (setq opart (substring opart 1))) + ;; Have the user editing the part + (setq npart (if arg "" (read-string (concat prompt initial) opart))) + ;; Tests: + (cond ((string= npart opart) + (error "Not changed")) + ((string= npart "") + (if dont-allow-empty + (error "Illegal value") + (setf (nth n analyze) npart))) + (t (setf (nth n analyze) (concat initial npart)))) + (setq new (apply 'concat analyze)) + ;; Change the entry and insert the changed version into the index. + (reftex-index-change-entry + new (if (string= npart "") + (format "Deleted: %s" opart) + (format "New value is: %s" npart))))) + +(defun reftex-index-level-down () + "Make index entry a subitem of another entry." + (interactive) + (let* ((data (get-text-property (point) :data)) + (docstruct (symbol-value reftex-docstruct-symbol)) + old new prefix key) + (unless data (error "Don't know which index entry to change")) + (setq old (nth 2 data) + key (nth 6 data) + prefix (completing-read + "Prefix: " + (reftex-sublist-nth + docstruct 6 + (lambda (x) + (and (eq (car x) 'index) + (string= (nth 1 x) reftex-index-tag))) t))) + (unless (string-match + (concat (regexp-quote (car reftex-index-special-chars)) "\\'") + prefix) + (setq prefix (concat prefix (car reftex-index-special-chars)))) + (if (string-match (regexp-quote key) old) + (setq new (replace-match (concat prefix key) t t old)) + (error "Cannot construct new index key")) + (reftex-index-change-entry new (format "Added prefix: %s" prefix)))) + +(defun reftex-index-level-up () + "Remove the highest level of a hierarchical index entry." + (interactive) + (let* ((data (get-text-property (point) :data)) + old new prefix) + (unless data (error "Don't know which entry to change")) + (setq old (nth 2 data)) + (if (string-match (concat "{\\([^" (nth 0 reftex-index-special-chars) "]*" + "[^" (nth 3 reftex-index-special-chars) "]" + (regexp-quote (nth 0 reftex-index-special-chars)) + "\\)") + old) + (setq prefix (substring old (match-beginning 1) (match-end 1)) + new (concat (substring old 0 (match-beginning 1)) + (substring old (match-end 1)))) + (error "Entry is not a subitem")) + (reftex-index-change-entry new (format "Removed prefix: %s" prefix)))) + +(defun reftex-index-kill () + "FIXME: Not yet implemented" + (interactive) + (error "This function is currently not implemented")) + +(defun reftex-index-undo () + "FIXME: Not yet implemented" + (interactive) + (error "This function is currently not implemented")) + +(defun reftex-index-change-entry (new &optional message) + ;; Change the full context string of the index entry at point to + ;; NEW. This actually edits the buffer where the entry is defined. + + (let* ((data (get-text-property (point) :data)) + old beg end info) + (unless data (error "Cannot change entry")) + (reftex-index-view-entry) + (setq beg (match-beginning 0) end (match-end 0)) + (setq old (nth 2 data)) + (and (equal old new) (error "Entry unchanged")) + (save-excursion + (set-buffer (get-file-buffer (nth 3 data))) + (goto-char beg) + (unless (looking-at (regexp-quote old)) + (error "This should not happen (reftex-index-change-entry)")) + (delete-region beg end) + (insert new) + (goto-char (1- beg)) + (when (and (re-search-forward (reftex-everything-regexp) nil t) + (match-end 10) + (< (abs (- (match-beginning 10) beg)) (length new)) + (setq info (reftex-index-info-safe buffer-file-name))) + (setcdr data (cdr info)))) + (let ((buffer-read-only nil)) + (save-excursion + (reftex-insert-index (list data) reftex-index-tag t + "EDITED"))) + (setq reftex-last-follow-point 1) + (and message (message message)))) + +;; Index map +(define-key reftex-index-map (if (featurep 'xemacs) [(button2)] [(mouse-2)]) + 'reftex-index-mouse-goto-line-and-hide) + +(substitute-key-definition + 'next-line 'reftex-index-next reftex-index-map global-map) +(substitute-key-definition + 'previous-line 'reftex-index-previous reftex-index-map global-map) + +(loop for x in + '(("n" . reftex-index-next) + ("p" . reftex-index-previous) + ("?" . reftex-index-show-help) + (" " . reftex-index-view-entry) + ("\C-m" . reftex-index-goto-entry-and-hide) + ("\C-i" . reftex-index-goto-entry) + ("\C-k" . reftex-index-kill) + ("r" . reftex-index-rescan) + ("R" . reftex-index-Rescan) + ("g" . revert-buffer) + ("q" . reftex-index-quit) + ("k" . reftex-index-quit-and-kill) + ("f" . reftex-index-toggle-follow) + ("s" . reftex-index-switch-index-tag) + ("e" . reftex-index-edit) + ("^" . reftex-index-level-up) + ("_" . reftex-index-level-down) + ("}" . reftex-index-restrict-to-section) + ("{" . reftex-index-widen) + (">" . reftex-index-restriction-forward) + ("<" . reftex-index-restriction-backward) + ("(" . reftex-index-toggle-range-beginning) + (")" . reftex-index-toggle-range-end) + ("|" . reftex-index-edit-attribute) + ("@" . reftex-index-edit-visual) + ("*" . reftex-index-edit-key) + ("&" . reftex-index-globalize) + ("\C-c=". reftex-index-goto-toc) + ("c" . reftex-index-toggle-context)) + do (define-key reftex-index-map (car x) (cdr x))) + +(loop for key across "0123456789" do + (define-key reftex-index-map (vector (list key)) 'digit-argument)) +(define-key reftex-index-map "-" 'negative-argument) + +;; The capital letters and the exclamation mark +(loop for key across (concat "!" reftex-index-section-letters) do + (define-key reftex-index-map (vector (list key)) + (list 'lambda '() '(interactive) + (list 'reftex-index-goto-letter key)))) + +(defun reftex-index-goto-letter (char) + "Go to the CHAR section in the index." + (let ((pos (point)) + (case-fold-search nil)) + (goto-line 3) + (if (re-search-forward (concat "^" (char-to-string char)) nil t) + (progn + (beginning-of-line) + (recenter 0) + (reftex-index-next)) + (goto-char pos) + (error "This <%s> index does not contain entries starting with `%c'" + reftex-index-tag char)))) + +(easy-menu-define + reftex-index-menu reftex-index-map + "Menu for Index buffer" + `("Index" + ["Goto section A-Z" + (message "To go to a section, just press any of: !%s" + reftex-index-section-letters) t] + ["Show Entry" reftex-index-view-entry t] + ["Go To Entry" reftex-index-goto-entry t] + ["Exit & Go To Entry" reftex-index-goto-entry-and-hide t] + ["Table of Contents" reftex-index-goto-toc t] + ["Quit" reftex-index-quit t] + "--" + ("Update" + ["Rebuilt *Index* Buffer" revert-buffer t] + "--" + ["Rescan One File" reftex-index-rescan reftex-enable-partial-scans] + ["Rescan Entire Document" reftex-index-Rescan t]) + ("Restrict" + ["Restrict to section" reftex-index-restrict-to-section t] + ["Widen" reftex-index-widen reftex-index-restriction-indicator] + ["Next Section" reftex-index-restriction-forward + reftex-index-restriction-indicator] + ["Previous Section" reftex-index-restriction-backward + reftex-index-restriction-indicator]) + ("Edit" + ["Edit Entry" reftex-index-edit t] + ["Edit Key" reftex-index-edit-key t] + ["Edit Attribute" reftex-index-edit-attribute t] + ["Edit Visual" reftex-index-edit-visual t] + "--" + ["Add Parentkey" reftex-index-level-down t] + ["Remove Parentkey " reftex-index-level-up t] + "--" + ["Make Start-of-Range" reftex-index-toggle-range-beginning t] + ["Make End-of-Range" reftex-index-toggle-range-end t] + "--" + ["Globalize" reftex-index-globalize t] + ["Kill Entry" reftex-index-kill nil] + "--" + ["Undo" reftex-index-undo nil]) + ("Options" + ["Context" reftex-index-toggle-context :style toggle + :selected reftex-index-include-context] + "--" + ["Follow Mode" reftex-index-toggle-follow :style toggle + :selected reftex-index-follow-mode]) + "--" + ["Help" reftex-index-show-help t])) + +;;; reftex-index.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-parse.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,987 @@ +;;; reftex-parse.el - Parser Functions for RefTeX +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-parse) +(require 'reftex) + +(defmacro reftex-with-special-syntax (&rest body) + `(let ((saved-syntax (syntax-table))) + (unwind-protect + (progn + (set-syntax-table reftex-syntax-table) + ,@body) + (set-syntax-table saved-syntax)))) + +(defun reftex-parse-one () + "Re-parse this file." + (interactive) + (let ((reftex-enable-partial-scans t)) + (reftex-access-scan-info '(4)))) + +(defun reftex-parse-all () + "Re-parse entire document." + (interactive) + (reftex-access-scan-info '(16))) + +(defun reftex-do-parse (rescan &optional file) + "Do a document rescan. When allowed, do only a partial scan from FILE." + + ;; Normalize the rescan argument + (setq rescan (cond ((eq rescan t) t) + ((eq rescan 1) 1) + ((equal rescan '(4)) t) + ((equal rescan '(16)) 1) + (t 1))) + + ;; Partial scans only when allowed + (unless reftex-enable-partial-scans + (setq rescan 1)) + + ;; Do the scanning. + + (let* ((old-list (symbol-value reftex-docstruct-symbol)) + (master (reftex-TeX-master-file)) + (true-master (file-truename master)) + (master-dir (file-name-as-directory (file-name-directory master))) + (file (or file (buffer-file-name))) + (true-file (file-truename file)) + (bibview-cache (assq 'bibview-cache old-list)) + (index-tags (cdr (assq 'index-tags old-list))) + from-file appendix docstruct tmp) + + ;; Make sure replacement is really an option here + (when (and (eq rescan t) + (not (and (member (list 'bof file) old-list) + (member (list 'eof file) old-list)))) + ;; Scan whole document because no such file section exists + (setq rescan 1)) + (when (string= true-file true-master) + ;; Scan whole document because this file is the master + (setq rescan 1)) + + ;; From which file do we start? + (setq from-file + (cond ((eq rescan t) (or file master)) + ((eq rescan 1) master) + (t (error "This should not happen (reftex-do-parse)")))) + + ;; Reset index-tags if we scan everything + (if (equal rescan 1) (setq index-tags nil)) + + ;; Find active toc entry and initialize section-numbers + (setq reftex-active-toc (reftex-last-assoc-before-elt + 'toc (list 'bof from-file) old-list) + appendix (reftex-last-assoc-before-elt + 'appendix (list 'bof from-file) old-list)) + + (reftex-init-section-numbers reftex-active-toc appendix) + + (if (eq rescan 1) + (message "Scanning entire document...") + (message "Scanning document from %s..." from-file)) + + (reftex-with-special-syntax + (save-window-excursion + (save-excursion + (unwind-protect + (setq docstruct + (reftex-parse-from-file + from-file docstruct master-dir)) + (reftex-kill-temporary-buffers))))) + + (message "Scanning document... done") + + ;; Turn the list around. + (setq docstruct (nreverse docstruct)) + + ;; Set or insert + (setq docstruct (reftex-replace-label-list-segment + old-list docstruct (eq rescan 1))) + + ;; Add all missing information + (unless (assq 'label-numbers docstruct) + (push (cons 'label-numbers nil) docstruct)) + (unless (assq 'master-dir docstruct) + (push (cons 'master-dir master-dir) docstruct)) + (unless (assq 'bibview-cache docstruct) + (push (cons 'bibview-cache (cdr bibview-cache)) docstruct)) + (let* ((bof1 (memq (assq 'bof docstruct) docstruct)) + (bof2 (assq 'bof (cdr bof1))) + (is-multi (not (not (and bof1 bof2)))) + (entry (or (assq 'is-multi docstruct) + (car (push (list 'is-multi is-multi) docstruct))))) + (setcdr entry (cons is-multi nil))) + (and index-tags (setq index-tags (sort index-tags 'string<))) + (let ((index-tag-cell (assq 'index-tags docstruct))) + (if index-tag-cell + (setcdr index-tag-cell index-tags) + (push (cons 'index-tags index-tags) docstruct))) + (unless (assq 'xr docstruct) + (let* ((allxr (reftex-all-assq 'xr-doc docstruct)) + (alist (mapcar + (lambda (x) + (if (setq tmp (reftex-locate-file (nth 2 x) "tex" + master-dir)) + (cons (nth 1 x) tmp) + (message "Can't find external document %s" + (nth 2 x)) + nil)) + allxr)) + (alist (delq nil alist)) + (allprefix (delq nil (mapcar 'car alist))) + (regexp (if allprefix + (concat "\\`\\(" + (mapconcat 'identity allprefix "\\|") + "\\)") + "\\\\\\\\\\\\"))) ; this will never match + (push (list 'xr alist regexp) docstruct))) + + (set reftex-docstruct-symbol docstruct) + (put reftex-docstruct-symbol 'modified t))) + +(defun reftex-everything-regexp () + (if reftex-support-index + reftex-everything-regexp + reftex-everything-regexp-no-index)) + +(defun reftex-all-document-files (&optional relative) + "Return a list of all files belonging to the current document. +When RELATIVE is non-nil, give file names relative to directory +of master file." + (let* ((all (symbol-value reftex-docstruct-symbol)) + (master-dir (file-name-directory (reftex-TeX-master-file))) + (re (concat "\\`" (regexp-quote master-dir))) + file-list tmp file) + (while (setq tmp (assoc 'bof all)) + (setq file (nth 1 tmp) + all (cdr (memq tmp all))) + (and relative + (string-match re file) + (setq file (substring file (match-end 0)))) + (push file file-list)) + (nreverse file-list))) + +(defun reftex-parse-from-file (file docstruct master-dir) + ;; Scan the buffer for labels and save them in a list. + (let ((regexp (reftex-everything-regexp)) + (bound 0) + file-found tmp include-file + (level 1) + (highest-level 100) + toc-entry index-entry next-buf buf) + + (catch 'exit + (setq file-found (reftex-locate-file file "tex" master-dir)) + (if (and (not file-found) + (setq buf (reftex-get-buffer-visiting file))) + (setq file-found (buffer-file-name buf))) + + (unless file-found + (push (list 'file-error file) docstruct) + (throw 'exit nil)) + + (save-excursion + + (message "Scanning file %s" file) + (set-buffer + (setq next-buf + (reftex-get-file-buffer-force + file-found + (not (eq t reftex-keep-temporary-buffers))))) + + ;; Begin of file mark + (setq file (buffer-file-name)) + (push (list 'bof file) docstruct) + + (reftex-with-special-syntax + (save-excursion + (save-restriction + (widen) + (goto-char 1) + + (while (re-search-forward regexp nil t) + + (cond + + ((match-end 1) + ;; It is a label + (push (reftex-label-info (reftex-match-string 1) file bound) + docstruct)) + + ((match-end 3) + ;; It is a section + (setq bound (point)) + + ;; Insert in List + (setq toc-entry (reftex-section-info file)) + (setq level (nth 5 toc-entry)) + (setq highest-level (min highest-level level)) + (if (= level highest-level) + (message + "Scanning %s %s ..." + (car (rassoc level reftex-section-levels-all)) + (nth 6 toc-entry))) + + (push toc-entry docstruct) + (setq reftex-active-toc toc-entry)) + + ((match-end 7) + ;; It's an include or input + (setq include-file (reftex-match-string 7)) + ;; Test if this file should be ignored + (unless (delq nil (mapcar + (lambda (x) (string-match x include-file)) + reftex-no-include-regexps)) + ;; Parse it + (setq docstruct + (reftex-parse-from-file + include-file + docstruct master-dir)))) + + ((match-end 9) + ;; Appendix starts here + (reftex-init-section-numbers nil t) + (push (cons 'appendix t) docstruct)) + + ((match-end 10) + ;; Index entry + (when reftex-support-index + (setq index-entry (reftex-index-info file)) + (when index-entry + (add-to-list 'index-tags (nth 1 index-entry)) + (push index-entry docstruct)))) + + ((match-end 11) + ;; A macro with label + (save-excursion + (let* ((mac (reftex-match-string 11)) + (label (progn (goto-char (match-end 11)) + (save-match-data + (reftex-no-props + (reftex-nth-arg-wrapper + mac))))) + (typekey (nth 1 (assoc mac reftex-env-or-mac-alist))) + (entry (progn (if typekey + ;; A typing macro + (goto-char (match-end 0)) + ;; A neutral macro + (goto-char (match-end 11)) + (reftex-move-over-touching-args)) + (reftex-label-info + label file bound nil nil)))) + (push entry docstruct)))) + (t (error "This should not happen (reftex-parse-from-file)"))) + ) + + ;; Find bibliography statement + (when (setq tmp (reftex-locate-bibliography-files master-dir)) + (push (cons 'bib tmp) docstruct)) + + (goto-char 1) + (when (re-search-forward + "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t) + (push (cons 'thebib file) docstruct)) + + ;; Find external document specifications + (goto-char 1) + (while (re-search-forward "[\n\r][ \t]*\\\\externaldocument\\(\\[\\([^]]*\\)\\]\\)?{\\([^}]+\\)}" nil t) + (push (list 'xr-doc (reftex-match-string 2) + (reftex-match-string 3)) + docstruct)) + + ;; End of file mark + (push (list 'eof file) docstruct))))) + + ;; Kill the scanned buffer + (reftex-kill-temporary-buffers next-buf)) + + ;; Return the list + docstruct)) + +(defun reftex-locate-bibliography-files (master-dir &optional files) + ;; Scan buffer for bibliography macro and return file list. + + (unless files + (save-excursion + (goto-char (point-min)) + (if (re-search-forward + "\\(\\`\\|[\n\r]\\)[ \t]*\\\\bibliography{[ \t]*\\([^}]+\\)" nil t) + (setq files + (split-string (reftex-match-string 2) + "[ \t\n\r]*,[ \t\n\r]*"))))) + (when files + (setq files + (mapcar + (lambda (x) + (if (or (member x reftex-bibfile-ignore-list) + (delq nil (mapcar (lambda (re) (string-match re x)) + reftex-bibfile-ignore-regexps))) + ;; excluded file + nil + ;; find the file + (reftex-locate-file x "bib" master-dir))) + files)) + (delq nil files))) + +(defun reftex-replace-label-list-segment (old insert &optional entirely) + ;; Replace the segment in OLD which corresponds to INSERT. + ;; Works with side effects, directly changes old. + ;; If entirely is t, just return INSERT. + ;; This function also makes sure the old toc markers do not point anywhere. + + (cond + (entirely + (reftex-silence-toc-markers old (length old)) + insert) + (t (let* ((new old) + (file (nth 1 (car insert))) + (eof-list (member (list 'eof file) old)) + (bof-list (member (list 'bof file) old)) + n) + (if (not (and bof-list eof-list)) + (error "Cannot splice") + ;; Splice + (reftex-silence-toc-markers bof-list (- (length bof-list) + (length eof-list))) + (setq n (- (length old) (length bof-list))) + (setcdr (nthcdr n new) (cdr insert)) + (setcdr (nthcdr (1- (length new)) new) (cdr eof-list))) + new)))) + +(defun reftex-section-info (file) + ;; Return a section entry for the current match. + ;; Carefull: This function expects the match-data to be still in place! + (let* ((marker (set-marker (make-marker) (1- (match-beginning 3)))) + (macro (reftex-match-string 3)) + (level (cdr (assoc macro reftex-section-levels-all))) + (star (= ?* (char-after (match-end 3)))) + (unnumbered (or star (< level 0))) + (level (abs level)) + (section-number (reftex-section-number level unnumbered)) + (text1 (save-match-data (save-excursion (reftex-context-substring)))) + (literal (buffer-substring-no-properties + (1- (match-beginning 3)) + (min (point-max) (+ (match-end 0) (length text1) 1)))) + ;; Literal can be too short since text1 too short. No big problem. + (text (reftex-nicify-text text1))) + + ;; Add section number and indentation + (setq text + (concat + (make-string (* reftex-level-indent level) ?\ ) + (if (nth 1 reftex-label-menu-flags) ; section number flag + (concat section-number " ")) + text)) + (list 'toc "toc" text file marker level section-number + literal (marker-position marker)))) + +(defun reftex-ensure-index-support (&optional abort) + ;; When index support is turned off, ask to turn it on and + ;; set the current prefix argument so that `reftex-access-scan-info' + ;; will rescan the entire document. + (cond + (reftex-support-index t) + ((y-or-n-p "Turn on index support and rescan entire document? ") + (setq reftex-support-index 'demanded + current-prefix-arg '(16))) + (t (if abort + (error "No index support") + (message "No index support") + (ding) + (sit-for 1))))) + +(defun reftex-index-info-safe (file) + (reftex-with-special-syntax + (reftex-index-info file))) + +(defvar test-dummy) +(defun reftex-index-info (file) + ;; Return an index entry for the current match. + ;; Carefull: This function expects the match-data to be still in place! + (catch 'exit + (let* ((macro (reftex-match-string 10)) + (bom (match-beginning 10)) + (boa (match-end 10)) + (entry (or (assoc macro reftex-index-macro-alist) + (throw 'exit nil))) + (exclude (nth 3 entry)) + ;; The following is a test if this match should be excluded + (test-dummy (and (fboundp exclude) + (funcall exclude) + (throw 'exit nil))) + (itag (nth 1 entry)) + (prefix (nth 2 entry)) + (index-tag + (cond ((stringp itag) itag) + ((integerp itag) + (progn (goto-char boa) + (or (reftex-nth-arg itag (nth 6 entry)) "idx"))) + (t "idx"))) + (arg (or (progn (goto-char boa) + (reftex-nth-arg (nth 5 entry) (nth 6 entry))) + "")) + (end-of-args (progn (goto-char boa) + (reftex-move-over-touching-args) + (point))) + (end-of-context (progn (skip-chars-forward "^ \t\n\r") (point))) + (begin-of-context + (progn (goto-char bom) + (skip-chars-backward "^ \t\r\n") + (point))) + (context (buffer-substring-no-properties + begin-of-context end-of-context)) + (key-end (if (string-match reftex-index-key-end-re arg) + (1+ (match-beginning 0)))) + (rawkey (substring arg 0 key-end)) + + (key (if prefix (concat prefix rawkey) rawkey)) + (sortkey (downcase key)) + (showkey (mapconcat 'identity + (split-string key reftex-index-level-re) + " ! "))) + (goto-char end-of-args) + ;; 0 1 2 3 4 5 6 7 8 9 + (list 'index index-tag context file bom arg key showkey sortkey key-end)))) + +(defun reftex-short-context (env parse &optional bound derive) + ;; Get about one line of useful context for the label definition at point. + + (if (consp parse) + (setq parse (if derive (cdr parse) (car parse)))) + + (reftex-nicify-text + + (cond + + ((null parse) + (save-excursion + (reftex-context-substring))) + + ((eq parse t) + (if (string= env "section") + ;; special treatment for section labels + (save-excursion + (if (and (re-search-backward reftex-section-or-include-regexp + (point-min) t) + (match-end 2)) + (progn + (goto-char (match-end 0)) + (reftex-context-substring)) + (if reftex-active-toc + (progn + (string-match "{\\([^}]*\\)" (nth 7 reftex-active-toc)) + (match-string 1 (nth 7 reftex-active-toc))) + "SECTION HEADING NOT FOUND"))) + (save-excursion + (goto-char reftex-default-context-position) + (unless (eq (string-to-char env) ?\\) + (reftex-move-over-touching-args)) + (reftex-context-substring)))) + + ((stringp parse) + (save-excursion + (if (re-search-backward parse bound t) + (progn + (goto-char (match-end 0)) + (reftex-context-substring)) + "NO MATCH FOR CONTEXT REGEXP"))) + + ((integerp parse) + (or (save-excursion + (goto-char reftex-default-context-position) + (reftex-nth-arg + parse + (nth 6 (assoc env reftex-env-or-mac-alist)))) + "")) + + ((fboundp parse) + ;; A hook function. Call it. + (save-excursion + (condition-case error-var + (funcall parse env) + (error (format "HOOK ERROR: %s" (cdr error-var)))))) + (t + "ILLEGAL VALUE OF PARSE")))) + +(defun reftex-where-am-I () + ;; Return the docstruct entry above point. Actually returns a cons + ;; cell in which the cdr is a flag indicating if the information is + ;; exact (t) or approximate (nil). + + (let ((docstruct (symbol-value reftex-docstruct-symbol)) + (cnt 0) rtn + found) + (save-excursion + (while (not rtn) + (incf cnt) + (setq found (re-search-backward (reftex-everything-regexp) nil t)) + (setq rtn + (cond + ((not found) + ;; no match + (or + (car (member (list 'bof (buffer-file-name)) docstruct)) + (not (setq cnt 2)) + (assq 'bof docstruct) ;; for safety reasons + 'corrupted)) + ((match-end 1) + ;; Label + (assoc (reftex-match-string 1) + (symbol-value reftex-docstruct-symbol))) + ((match-end 3) + ;; Section + (goto-char (1- (match-beginning 3))) + (let* ((list (member (list 'bof (buffer-file-name)) + docstruct)) + (endelt (car (member (list 'eof (buffer-file-name)) + list))) + rtn1) + (while (and list (not (eq endelt (car list)))) + (if (and (eq (car (car list)) 'toc) + (string= (buffer-file-name) + (nth 3 (car list)))) + (cond + ((equal (point) + (or (and (markerp (nth 4 (car list))) + (marker-position (nth 4 (car list)))) + (nth 8 (car list)))) + ;; Fits with marker position or recorded position + (setq rtn1 (car list) list nil)) + ((looking-at (reftex-make-regexp-allow-for-ctrl-m + (nth 7 (car list)))) + ;; Same title + (setq rtn1 (car list) list nil cnt 2)))) + (pop list)) + rtn1)) + ((match-end 7) + ;; Input or include... + (car + (member (list 'eof (reftex-locate-file + (reftex-match-string 7) "tex" + (cdr (assq 'master-dir docstruct)))) + docstruct))) + ((match-end 9) + (assq 'appendix (symbol-value reftex-docstruct-symbol))) + ((match-end 10) + ;; Index entry + (when reftex-support-index + (let* ((index-info (save-excursion + (reftex-index-info-safe nil))) + (list (member (list 'bof (buffer-file-name)) + docstruct)) + (endelt (car (member (list 'eof (buffer-file-name)) + list))) + dist last-dist last (n 0)) + ;; Check all index entries with equal text + (while (and list (not (eq endelt (car list)))) + (when (and (eq (car (car list)) 'index) + (string= (nth 2 index-info) + (nth 2 (car list)))) + (incf n) + (setq dist (abs (- (point) (nth 4 (car list))))) + (if (or (not last-dist) (< dist last-dist)) + (setq last-dist dist last (car list)))) + (setq list (cdr list))) + ;; We are sure if we have only one, or a zero distance + (cond ((or (= n 1) (= dist 0)) last) + ((> n 1) (setq cnt 2) last) + (t nil))))) + ((match-end 11) + (save-excursion + (goto-char (match-end 11)) + (assoc (reftex-no-props + (reftex-nth-arg-wrapper + (reftex-match-string 11))) + (symbol-value reftex-docstruct-symbol)))) + (t + (error "This should not happen (reftex-where-am-I)")))))) + (cons rtn (eq cnt 1)))) + +(defun reftex-notice-new (&optional n force) + "Hook to handshake with RefTeX after something new has been inserted." + ;; Add a new entry to the docstruct list. If it is a section, renumber + ;; the following sections. + ;; FIXME: Put in a WHAT parameter + ;; When N is given, go back that many matches of reftex-everything-regexp + ;; When FORCE is non-nil, also insert if `reftex-where-am-I' was uncertain. + (condition-case nil + (catch 'exit + (unless reftex-mode (throw 'exit nil)) + (reftex-access-scan-info) + (let* ((docstruct (symbol-value reftex-docstruct-symbol)) + here-I-am appendix tail entry star level + section-number context) + + (save-excursion + (when (re-search-backward (reftex-everything-regexp) nil t (or n 1)) + + ;; Find where we are + (setq here-I-am (reftex-where-am-I)) + (or here-I-am (throw 'exit nil)) + (unless (or force (cdr here-I-am)) (throw 'exit nil)) + (setq tail (memq (car here-I-am) docstruct)) + (or tail (throw 'exit nil)) + (setq reftex-active-toc (reftex-last-assoc-before-elt + 'toc (car here-I-am) docstruct) + appendix (reftex-last-assoc-before-elt + 'appendix (car here-I-am) docstruct)) + + ;; Initialize section numbers + (if (eq (car (car here-I-am)) 'appendix) + (reftex-init-section-numbers nil t) + (reftex-init-section-numbers reftex-active-toc appendix)) + + ;; Match the section command + (when (re-search-forward (reftex-everything-regexp) nil t) + (cond + ((match-end 1) + (push (reftex-label-info (reftex-match-string 1) buffer-file-name) + (cdr tail))) + + ((match-end 3) + (setq star (= ?* (char-after (match-end 3))) + entry (reftex-section-info (buffer-file-name)) + level (nth 5 entry)) + ;; Insert the section info + (push entry (cdr tail)) + + ;; We are done unless we use section numbers + (unless (nth 1 reftex-label-menu-flags) (throw 'exit nil)) + + ;; Update the remaining toc items + (setq tail (cdr tail)) + (while (and (setq tail (memq (assq 'toc (cdr tail)) tail)) + (setq entry (car tail)) + (>= (nth 5 entry) level)) + (setq star (string-match "\\*" (nth 6 entry)) + context (nth 2 entry) + section-number + (reftex-section-number (nth 5 entry) star)) + (when (string-match "\\`\\([ \t]*\\)\\([.0-9A-Z]+\\)\\(.*\\)" + context) + (when (and (not appendix) + (>= (string-to-char (match-string 2)) ?A)) + ;; Just entered the appendex. Get out. + (throw 'exit nil)) + + ;; Change the section number. + (setf (nth 2 entry) + (concat (match-string 1 context) + section-number + (match-string 3 context)))))) + ((match-end 10) + ;; Index entry + (and reftex-support-index + (setq entry (reftex-index-info-safe buffer-file-name)) + ;; FIXME: (add-to-list 'index-tags (nth 1 index-entry)) + (push entry (cdr tail)))))))))) + + (error nil)) + ) + +(defsubst reftex-move-to-previous-arg (&optional bound) + ;; Assuming that we are in front of a macro argument, + ;; move backward to the closing parenthesis of the previous argument. + ;; This function understands the splitting of macros over several lines + ;; in TeX. + (cond + ;; Just to be quick: + ((memq (preceding-char) '(?\] ?\}))) + ;; Do a search + ((and reftex-allow-detached-macro-args + (re-search-backward + "[]}][ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*\\=" bound t)) + (goto-char (1+ (match-beginning 0))) + t) + (t nil))) + +(defun reftex-what-macro-safe (which &optional bound) + ;; reftex-what-macro with special syntax table. + (reftex-with-special-syntax + (reftex-what-macro which bound))) + +(defun reftex-what-macro (which &optional bound) + ;; Find out if point is within the arguments of any TeX-macro. + ;; The return value is either ("\\macro" . (point)) or a list of them. + + ;; If WHICH is nil, immediately return nil. + ;; If WHICH is 1, return innermost enclosing macro. + ;; If WHICH is t, return list of all macros enclosing point. + ;; If WHICH is a list of macros, look only for those macros and return the + ;; name of the first macro in this list found to enclose point. + ;; If the optional BOUND is an integer, bound backwards directed + ;; searches to this point. If it is nil, limit to nearest \section - + ;; like statement. + + ;; This function is pretty stable, but can be fooled if the text contains + ;; things like \macro{aa}{bb} where \macro is defined to take only one + ;; argument. As RefTeX cannot know this, the string "bb" would still be + ;; considered an argument of macro \macro. + + (unless reftex-section-regexp (reftex-compile-variables)) + (catch 'exit + (if (null which) (throw 'exit nil)) + (let ((bound (or bound (save-excursion (re-search-backward + reftex-section-regexp nil 1) + (point)))) + pos cmd-list cmd cnt cnt-opt entry) + (save-restriction + (save-excursion + (narrow-to-region (max 1 bound) (point-max)) + ;; move back out of the current parenthesis + (while (condition-case nil + (progn (up-list -1) t) + (error nil)) + (setq cnt 1 cnt-opt 0) + ;; move back over any touching sexps + (while (and (reftex-move-to-previous-arg bound) + (condition-case nil + (progn (backward-sexp) t) + (error nil))) + (if (eq (following-char) ?\[) (incf cnt-opt)) + (incf cnt)) + (setq pos (point)) + (when (and (or (= (following-char) ?\[) + (= (following-char) ?\{)) + (re-search-backward "\\\\[*a-zA-Z]+\\=" nil t)) + (setq cmd (reftex-match-string 0)) + (when (looking-at "\\\\begin{[^}]*}") + (setq cmd (reftex-match-string 0) + cnt (1- cnt))) + ;; This does ignore optional arguments. Very hard to fix. + (when (setq entry (assoc cmd reftex-env-or-mac-alist)) + (if (> cnt (or (nth 4 entry) 100)) + (setq cmd nil))) + (cond + ((null cmd)) + ((eq t which) + (push (cons cmd (point)) cmd-list)) + ((or (eq 1 which) (member cmd which)) + (throw 'exit (cons cmd (point)))))) + (goto-char pos))) + (nreverse cmd-list))))) + +(defun reftex-what-environment (which &optional bound) + ;; Find out if point is inside a LaTeX environment. + ;; The return value is (e.g.) either ("equation" . (point)) or a list of + ;; them. + + ;; If WHICH is nil, immediately return nil. + ;; If WHICH is 1, return innermost enclosing environment. + ;; If WHICH is t, return list of all environments enclosing point. + ;; If WHICH is a list of environments, look only for those environments and + ;; return the name of the first environment in this list found to enclose + ;; point. + + ;; If the optional BOUND is an integer, bound backwards directed searches to + ;; this point. If it is nil, limit to nearest \section - like statement. + + (unless reftex-section-regexp (reftex-compile-variables)) + (catch 'exit + (save-excursion + (if (null which) (throw 'exit nil)) + (let ((bound (or bound (save-excursion (re-search-backward + reftex-section-regexp nil 1) + (point)))) + env-list end-list env) + (while (re-search-backward "\\\\\\(begin\\|end\\){\\([^}]+\\)}" + bound t) + (setq env (buffer-substring-no-properties + (match-beginning 2) (match-end 2))) + (cond + ((string= (match-string 1) "end") + (push env end-list)) + ((equal env (car end-list)) + (setq end-list (cdr end-list))) + ((eq t which) + (push (cons env (point)) env-list)) + ((or (eq 1 which) (member env which)) + (throw 'exit (cons env (point)))))) + (nreverse env-list))))) + +(defun reftex-what-special-env (which &optional bound) + ;; Run the special environment parsers and return the matches. + ;; + ;; The return value is (e.g.) either ("my-parser-function" . (point)) + ;; or a list of them. + + ;; If WHICH is nil, immediately return nil. + ;; If WHICH is 1, return innermost enclosing environment. + ;; If WHICH is t, return list of all environments enclosing point. + ;; If WHICH is a list of environments, look only for those environments and + ;; return the name of the first environment in this list found to enclose + ;; point. + + (unless reftex-section-regexp (reftex-compile-variables)) + (catch 'exit + (save-excursion + (if (null reftex-special-env-parsers) (throw 'exit nil)) + (if (null which) (throw 'exit nil)) + (let ((bound (or bound (save-excursion (re-search-backward + reftex-section-regexp nil 1) + (point)))) + (fun-list (if (listp which) + (mapcar (lambda (x) (if (memq x which) x nil)) + reftex-special-env-parsers) + reftex-special-env-parsers)) + specials rtn) + ;; Call all functions + (setq specials (mapcar + (lambda (fun) + (save-excursion + (setq rtn (and fun (funcall fun bound))) + (if rtn (cons (symbol-name fun) rtn) nil))) + fun-list)) + ;; Delete the non-matches + (setq specials (delq nil specials)) + ;; Sort + (setq specials (sort specials (lambda (a b) (> (cdr a) (cdr b))))) + (if (eq which t) + specials + (car specials)))))) + +(defsubst reftex-move-to-next-arg (&optional ignore) + ;; Assuming that we are at the end of a macro name or a macro argument, + ;; move forward to the opening parenthesis of the next argument. + ;; This function understands the splitting of macros over several lines + ;; in TeX. + (cond + ;; Just to be quick: + ((memq (following-char) '(?\[ ?\{))) + ;; Do a search + ((and reftex-allow-detached-macro-args + (looking-at "[ \t]*[\n\r]?\\([ \t]*%[^\n\r]*[\n\r]\\)*[ \t]*[[{]")) + (goto-char (1- (match-end 0))) + t) + (t nil))) + +(defun reftex-nth-arg-wrapper (key) + (let ((entry (assoc key reftex-env-or-mac-alist))) + (reftex-nth-arg (nth 5 entry) (nth 6 entry)))) + +(defun reftex-nth-arg (n &optional opt-args) + ;; Return the nth following {} or [] parentheses content. + ;; OPT-ARGS is a list of argument numbers which are optional. + + ;; If we are sitting at a macro start, skip to end of macro name. + (and (eq (following-char) ?\\) (skip-chars-forward "a-zA-Z*\\\\")) + + (if (= n 1000) + ;; Special case: Skip all touching arguments + (progn + (reftex-move-over-touching-args) + (reftex-context-substring)) + + ;; Do the real thing. + (let ((cnt 1)) + + (when (reftex-move-to-next-arg) + + (while (< cnt n) + (while (and (member cnt opt-args) + (eq (following-char) ?\{)) + (incf cnt)) + (when (< cnt n) + (unless (and (condition-case nil + (or (forward-list 1) t) + (error nil)) + (reftex-move-to-next-arg) + (incf cnt)) + (setq cnt 1000)))) + + (while (and (memq cnt opt-args) + (eq (following-char) ?\{)) + (incf cnt))) + (if (and (= n cnt) + (> (skip-chars-forward "{\\[") 0)) + (reftex-context-substring) + nil)))) + +(defun reftex-move-over-touching-args () + (condition-case nil + (while (memq (following-char) '(?\[ ?\{)) + (forward-list 1)) + (error nil))) + +(defun reftex-context-substring () + ;; Return up to 150 chars from point + ;; When point is just after a { or [, limit string to matching parenthesis + (cond + ((or (= (preceding-char) ?\{) + (= (preceding-char) ?\[)) + ;; Inside a list - get only the list. + (buffer-substring-no-properties + (point) + (min (+ (point) 150) + (point-max) + (condition-case nil + (progn + (up-list 1) + (1- (point))) + (error (point-max)))))) + (t + ;; no list - just grab 150 characters + (buffer-substring-no-properties (point) + (min (+ (point) 150) (point-max)))))) + +;; Variable holding the vector with section numbers +(defvar reftex-section-numbers [0 0 0 0 0 0 0 0]) + +(defun reftex-init-section-numbers (&optional toc-entry appendix) + ;; Initialize the section numbers with zeros or with what is found + ;; in the toc entry. + (let* ((level (or (nth 5 toc-entry) -1)) + (numbers (nreverse (split-string (or (nth 6 toc-entry) "") "\\."))) + (depth (1- (length reftex-section-numbers))) + (i depth) number-string) + (while (>= i 0) + (if (> i level) + (aset reftex-section-numbers i 0) + (setq number-string (or (car numbers) "0")) + (if (string-match "\\`[A-Z]\\'" number-string) + (aset reftex-section-numbers i + (- (string-to-char number-string) ?A -1)) + (aset reftex-section-numbers i (string-to-int number-string))) + (pop numbers)) + (decf i))) + (put 'reftex-section-numbers 'appendix appendix)) + +(defun reftex-section-number (&optional level star) + ;; Return a string with the current section number. + ;; When LEVEL is non-nil, increase section numbers on that level. + (let* ((depth (1- (length reftex-section-numbers))) idx n (string "") + (appendix (get 'reftex-section-numbers 'appendix))) + (when level + (when (and (> level -1) (not star)) + (aset reftex-section-numbers + level (1+ (aref reftex-section-numbers level)))) + (setq idx (1+ level)) + (when (not star) + (while (<= idx depth) + (aset reftex-section-numbers idx 0) + (incf idx)))) + (setq idx 0) + (while (<= idx depth) + (setq n (aref reftex-section-numbers idx)) + (setq string (concat string (if (not (string= string "")) "." "") + (int-to-string n))) + (incf idx)) + (save-match-data + (if (string-match "\\`\\([@0]\\.\\)+" string) + (setq string (replace-match "" nil nil string))) + (if (string-match "\\(\\.0\\)+\\'" string) + (setq string (replace-match "" nil nil string))) + (if (and appendix + (string-match "\\`[0-9]+" string)) + (setq string + (concat + (char-to-string + (1- (+ ?A (string-to-int (match-string 0 string))))) + (substring string (match-end 0)))))) + (if star + (concat (make-string (1- (length string)) ?\ ) "*") + string))) + +;;; reftex-parse.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-ref.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,776 @@ +;;; reftex-ref.el - Code to create labels and references with RefTeX +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-ref) +(require 'reftex) +;;; + +(defun reftex-label-location (&optional bound) + "Return the environment or macro which determines the label type at point. +If optional BOUND is an integer, limit backward searches to that point." + + (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound)) + (loc2 (reftex-what-environment reftex-label-env-list bound)) + (loc3 (reftex-what-special-env 1 bound)) + (p1 (or (cdr loc1) 0)) + (p2 (or (cdr loc2) 0)) + (p3 (or (cdr loc3) 0)) + (pmax (max p1 p2 p3))) + + (setq reftex-location-start pmax) + (cond + ((= p1 pmax) + ;; A macro. Default context after macro name. + (setq reftex-default-context-position (+ p1 (length (car loc1)))) + (or (car loc1) "section")) + ((= p2 pmax) + ;; An environment. Default context after \begin{name}. + (setq reftex-default-context-position (+ p2 8 (length (car loc2)))) + (or (car loc2) "section")) + ((= p3 pmax) + ;; A special. Default context right there. + (setq reftex-default-context-position p3) + (setq loc3 (car loc3)) + (cond ((null loc3) "section") + ((symbolp loc3) (symbol-name loc3)) + ((stringp loc3) loc3) + (t "section"))) + (t ;; This should not happen, I think? + "section")))) + +(defun reftex-label-info-update (cell) + ;; Update information about just one label in a different file. + ;; CELL contains the old info list + (let* ((label (nth 0 cell)) + (typekey (nth 1 cell)) + ;; (text (nth 2 cell)) + (file (nth 3 cell)) + (comment (nth 4 cell)) + (note (nth 5 cell)) + (buf (reftex-get-file-buffer-force + file (not (eq t reftex-keep-temporary-buffers))))) + (if (not buf) + (list label typekey "" file comment "LOST LABEL. RESCAN TO FIX.") + (save-excursion + (set-buffer buf) + (save-restriction + (widen) + (goto-char 1) + + (if (or (re-search-forward + (format reftex-find-label-regexp-format + (regexp-quote label)) nil t) + (re-search-forward + (format reftex-find-label-regexp-format2 + (regexp-quote label)) nil t)) + + (progn + (backward-char 1) + (append (reftex-label-info label file) (list note))) + (list label typekey "" file "LOST LABEL. RESCAN TO FIX."))))))) + +(defun reftex-label-info (label &optional file bound derive env-or-mac) + ;; Return info list on LABEL at point. + (let* ((env-or-mac (or env-or-mac (reftex-label-location bound))) + (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist))) + (file (or file (buffer-file-name))) + (parse (nth 2 (assoc env-or-mac reftex-env-or-mac-alist))) + (text (reftex-short-context env-or-mac parse reftex-location-start + derive)) + (in-comment (reftex-in-comment))) + (list label typekey text file in-comment))) + +;;; Creating labels --------------------------------------------------------- + +(defun reftex-label (&optional environment no-insert) + "Insert a unique label. Return the label. +If ENVIRONMENT is given, don't bother to find out yourself. +If NO-INSERT is non-nil, do not insert label into buffer. +With prefix arg, force to rescan document first. +When you are prompted to enter or confirm a label, and you reply with +just the prefix or an empty string, no label at all will be inserted. +A new label is also recorded into the label list. +This function is controlled by the settings of reftex-insert-label-flags." + + (interactive) + + ;; Ensure access to scanning info and rescan buffer if prefix are is '(4). + (reftex-access-scan-info current-prefix-arg) + + ;; Find out what kind of environment this is and abort if necessary. + (if (or (not environment) + (not (assoc environment reftex-env-or-mac-alist))) + (setq environment (reftex-label-location))) + (unless environment + (error "Can't figure out what kind of label should be inserted")) + + ;; Ok, go ahead. + (catch 'exit + (let* ((entry (assoc environment reftex-env-or-mac-alist)) + (typekey (nth 1 entry)) + (format (nth 3 entry)) + (macro-cell (reftex-what-macro 1)) + (entry1 (assoc (car macro-cell) reftex-env-or-mac-alist)) + label naked prefix valid default force-prompt rescan-is-useful) + (when (and (or (nth 5 entry) (nth 5 entry1)) + (memq (preceding-char) '(?\[ ?\{))) + ;; This is an argument of a label macro. Insert naked label. + (setq naked t format "%s")) + + (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist)) + (concat typekey "-"))) + ;; Replace any escapes in the prefix + (setq prefix (reftex-replace-prefix-escapes prefix)) + + ;; Make a default label. + (cond + + ((reftex-typekey-check typekey (nth 0 reftex-insert-label-flags)) + ;; Derive a label from context. + (setq reftex-active-toc (reftex-last-assoc-before-elt + 'toc (car (reftex-where-am-I)) + (symbol-value reftex-docstruct-symbol))) + (setq default (reftex-no-props + (nth 2 (reftex-label-info " " nil nil t)))) + ;; Catch the cases where the is actually no context available. + (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default) + (string-match "ILLEGAL VALUE OF PARSE" default) + (string-match "SECTION HEADING NOT FOUND" default) + (string-match "HOOK ERROR" default) + (string-match "^[ \t]*$" default)) + (setq default prefix + force-prompt t) ; need to prompt + (setq default + (concat prefix + (funcall reftex-string-to-label-function default))) + + ;; Make it unique. + (setq default (reftex-uniquify-label default nil "-")))) + + ((reftex-typekey-check typekey (nth 1 reftex-insert-label-flags)) + ;; Minimal default: the user will be prompted. + (setq default prefix)) + + (t + ;; Make an automatic label. + (setq default (reftex-uniquify-label prefix t)))) + + ;; Should we ask the user? + (if (or (reftex-typekey-check typekey + (nth 1 reftex-insert-label-flags)) ; prompt + force-prompt) + + (while (not valid) + ;; iterate until we get a legal label + + (setq label (read-string + (if naked "Naked Label: " "Label: ") + default)) + + ;; Lets make sure that this is a legal label + (cond + + ((string-match (concat "\\`\\(" (regexp-quote prefix) + "\\)?[ \t]*\\'") + label) + ;; No label at all, please + (message "No label inserted.") + (throw 'exit nil)) + + ;; Test if label contains strange characters + ((string-match reftex-label-illegal-re label) + (message "Label \"%s\" contains illegal characters" label) + (ding) + (sit-for 2)) + + ;; Look it up in the label list + ((setq entry (assoc label + (symbol-value reftex-docstruct-symbol))) + (ding) + (if (y-or-n-p + (format "Label '%s' exists. Use anyway? " label)) + (setq valid t))) + + ;; Label is ok + (t + (setq valid t)))) + (setq label default)) + + ;; Insert the label into the label list + (let* ((here-I-am-info + (save-excursion + (if (and (or naked no-insert) + (integerp (cdr macro-cell))) + (goto-char (cdr macro-cell))) + (reftex-where-am-I))) + (here-I-am (car here-I-am-info)) + (note (if (cdr here-I-am-info) + "" + "POSITION UNCERTAIN. RESCAN TO FIX.")) + (file (buffer-file-name)) + (text nil) + (tail (memq here-I-am (symbol-value reftex-docstruct-symbol)))) + + (or (cdr here-I-am-info) (setq rescan-is-useful t)) + + (when tail + (push (list label typekey text file nil note) (cdr tail)) + (put reftex-docstruct-symbol 'modified t))) + + ;; Insert the label into the buffer + (unless no-insert + (insert + (if reftex-format-label-function + (funcall reftex-format-label-function label format) + (format format label))) + (if (and reftex-plug-into-AUCTeX + (fboundp 'LaTeX-add-labels)) + ;; Tell AUCTeX about this + (LaTeX-add-labels label))) + + ;; Delete the corresponding selection buffers to force update on next use. + (when reftex-auto-update-selection-buffers + (reftex-erase-buffer (reftex-make-selection-buffer-name typekey)) + (reftex-erase-buffer (reftex-make-selection-buffer-name " "))) + + (when (and rescan-is-useful reftex-allow-automatic-rescan) + (reftex-parse-one)) + + ;; return value of the function is the label + label))) + +(defun reftex-string-to-label (string) + "Convert a string (a sentence) to a label. +Uses `reftex-derive-label-parameters' and `reftex-label-illegal-re'. It +also applies `reftex-translate-to-ascii-function' to the string." + (when (and reftex-translate-to-ascii-function + (fboundp reftex-translate-to-ascii-function)) + (setq string (funcall reftex-translate-to-ascii-function string))) + (apply 'reftex-convert-string string + "[-~ \t\n\r,;]+" reftex-label-illegal-re nil nil + reftex-derive-label-parameters)) + +(defun reftex-latin1-to-ascii (string) + ;; Translate the upper 128 chars in the Latin-1 charset to ASCII equivalents + (let ((tab "@@@@@@@@@@@@@@@@@@'@@@@@@@@@@@@@ icLxY|S\"ca<--R-o|23'uq..1o>423?AAAAAAACEEEEIIIIDNOOOOOXOUUUUYP3aaaaaaaceeeeiiiidnooooo:ouuuuypy") + (emacsp (not (featurep 'xemacs)))) + (mapconcat + (lambda (c) + (cond ((and (> c 127) (< c 256)) ; 8 bit Latin-1 + (char-to-string (aref tab (- c 128)))) + ((and emacsp ; Not for XEmacs + (> c 2175) (< c 2304)) ; Mule Latin-1 + (char-to-string (aref tab (- c 2176)))) + (t (char-to-string c)))) + string ""))) + +(defun reftex-replace-prefix-escapes (prefix) + ;; Replace %escapes in a label prefix + (save-match-data + (let (letter (num 0) replace) + (while (string-match "\\%\\([a-zA-Z]\\)" prefix num) + (setq letter (match-string 1 prefix)) + (setq replace + (cond + ((equal letter "f") + (file-name-sans-extension + (file-name-nondirectory (buffer-file-name)))) + ((equal letter "F") + (let ((masterdir (file-name-directory (reftex-TeX-master-file))) + (file (file-name-sans-extension (buffer-file-name)))) + (if (string-match (concat "\\`" (regexp-quote masterdir)) + file) + (substring file (length masterdir)) + file))) + ((equal letter "u") + (or (user-login-name) "")) + ((equal letter "S") + (let* (macro level) + (save-excursion + (save-match-data + (when (re-search-backward reftex-section-regexp nil t) + (setq macro (reftex-match-string 2) + level + (abs + (cdr (assoc macro reftex-section-levels-all))))) + (cdr (or (assoc macro reftex-section-prefixes) + (assoc level reftex-section-prefixes) + (assq t reftex-section-prefixes) + (list t "sec:"))))))) + (t ""))) + (setq num (1- (+ (match-beginning 1) (length replace))) + prefix (replace-match replace nil nil prefix))) + prefix))) + +(defun reftex-uniquify-label (label &optional force separator) + ;; Make label unique by appending a number. + ;; Optional FORCE means, force appending a number, even if label is unique. + ;; Optional SEPARATOR is a string to stick between label and number. + + ;; Ensure access to scanning info + (reftex-access-scan-info) + + (cond + ((and (not force) + (not (assoc label (symbol-value reftex-docstruct-symbol)))) + label) + (t + (let* ((label-numbers (assq 'label-numbers + (symbol-value reftex-docstruct-symbol))) + (label-numbers-alist (cdr label-numbers)) + (cell (or (assoc label label-numbers-alist) + (car (setcdr label-numbers + (cons (cons label 0) + label-numbers-alist))))) + (num (1+ (cdr cell))) + (sep (or separator ""))) + (while (assoc (concat label sep (int-to-string num)) + (symbol-value reftex-docstruct-symbol)) + (incf num)) + (setcdr cell num) + (concat label sep (int-to-string num)))))) + +;;; Referencing labels ------------------------------------------------------ + +;; Help string for the reference label menu +(defconst reftex-select-label-prompt + "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more") + +(defconst reftex-select-label-help + " n / p Go to next/previous label (Cursor motion works as well) + C-c C-n/p Go to next/previous section heading. + b / l Jump back to previous selection / Reuse last referenced label. + g / s Update menu / Switch label type. + r / C-u r Reparse document / Reparse entire document. + x Switch to label menu of external document (with LaTeX package `xr'). + F t c Toggle: [F]ile borders, [t]able of contents, [c]ontext + # % Toggle: [#] label counters, [%] labels in comments + SPC / f Show full context in other window / Toggle follow mode. + . Show insertion point in other window. + v / V Toggle \\ref <-> \\vref / Rotate \\ref <=> \\fref <=> \\Fref + TAB Enter a label with completion. + m , - + Mark entry. `,-+' also assign a separator. + a / A Put all marked entries into one/many \\ref commands. + q / RET Quit without referencing / Accept current label (also on mouse-2).") + +(defun reftex-reference (&optional type no-insert cut) + "Make a LaTeX reference. Look only for labels of a certain TYPE. +With prefix arg, force to rescan buffer for labels. This should only be +necessary if you have recently entered labels yourself without using +reftex-label. Rescanning of the buffer can also be requested from the +label selection menu. +The function returns the selected label or nil. +If NO-INSERT is non-nil, do not insert \\ref command, just return label. +When called with 2 C-u prefix args, disable magic word recognition." + + (interactive) + + ;; check for active recursive edits + (reftex-check-recursive-edit) + + ;; Ensure access to scanning info and rescan buffer if prefix are is '(4) + (reftex-access-scan-info current-prefix-arg) + + (unless type + ;; guess type from context + (if (and reftex-guess-label-type + (setq type (reftex-guess-label-type))) + (setq cut (cdr type) + type (car type)) + (setq type (reftex-query-label-type)))) + + (let* ((refstyle + (cond ((reftex-typekey-check type reftex-vref-is-default) "\\vref") + ((reftex-typekey-check type reftex-fref-is-default) "\\fref") + (t "\\ref"))) + (reftex-format-ref-function reftex-format-ref-function) + (form "\\ref{%s}") + label labels sep sep1) + + ;; Have the user select a label + (set-marker reftex-select-return-marker (point)) + (setq labels (save-excursion + (reftex-offer-label-menu type))) + (reftex-ensure-compiled-variables) + (set-marker reftex-select-return-marker nil) + ;; If the first entry is the symbol 'concat, concat all all labels. + ;; We keep the cdr of the first label for typekey etc information. + (if (eq (car labels) 'concat) + (setq labels (list (list (mapconcat 'car (cdr labels) ",") + (cdr (nth 1 labels)))))) + (setq type (nth 1 (car labels)) + form (or (cdr (assoc type reftex-typekey-to-format-alist)) + form)) + + (cond + (no-insert + ;; Just return the first label + (car (car labels))) + ((null labels) + (message "Quit") + nil) + (t + (while labels + (setq label (car (car labels)) + sep (nth 2 (car labels)) + sep1 (cdr (assoc sep reftex-multiref-punctuation)) + labels (cdr labels)) + (when cut + (backward-delete-char cut) + (setq cut nil)) + + ;; remove ~ if we do already have a space + (when (and (= ?~ (string-to-char form)) + (member (preceding-char) '(?\ ?\t ?\n))) + (setq form (substring form 1))) + ;; do we have a special format? + (setq reftex-format-ref-function + (cond + ((string= refstyle "\\vref") 'reftex-format-vref) + ((string= refstyle "\\fref") 'reftex-format-fref) + ((string= refstyle "\\Fref") 'reftex-format-Fref) + (t reftex-format-ref-function))) + ;; ok, insert the reference + (if sep1 (insert sep1)) + (insert + (if reftex-format-ref-function + (funcall reftex-format-ref-function label form) + (format form label label))) + ;; take out the initial ~ for good + (and (= ?~ (string-to-char form)) + (setq form (substring form 1)))) + (message "") + label)))) + +(defun reftex-guess-label-type () + ;; Examine context to guess what a \ref might want to reference. + (let ((words reftex-words-to-typekey-alist) + (case-fold-search t) + (bound (max (point-min) (- (point) 35))) + matched cell) + (save-excursion + (while (and (setq cell (pop words)) + (not (setq matched + (re-search-backward (car cell) bound t)))))) + (if matched + (cons (cdr cell) (- (match-end 0) (match-end 1))) + nil))) + +(defvar reftex-select-label-map) +(defun reftex-offer-label-menu (typekey) + ;; Offer a menu with the appropriate labels. + (let* ((buf (current-buffer)) + (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol))) + (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data))) + (xr-index 0) + (here-I-am (car (reftex-where-am-I))) + (here-I-am1 here-I-am) + (toc (reftex-typekey-check typekey reftex-label-menu-flags 0)) + (files (reftex-typekey-check typekey reftex-label-menu-flags 7)) + (context (not (reftex-typekey-check + typekey reftex-label-menu-flags 3))) + (counter (reftex-typekey-check + typekey reftex-label-menu-flags 2)) + (follow (reftex-typekey-check + typekey reftex-label-menu-flags 4)) + (commented (nth 5 reftex-label-menu-flags)) + (prefix "") + selection-buffers + offset rtn key data last-data entries) + + (unwind-protect + (catch 'exit + (while t + (save-window-excursion + (delete-other-windows) + (setq reftex-call-back-to-this-buffer buf + reftex-latex-syntax-table (syntax-table)) + (let ((default-major-mode 'reftex-select-label-mode)) + (if reftex-use-multiple-selection-buffers + (switch-to-buffer-other-window + (save-excursion + (set-buffer buf) + (reftex-make-selection-buffer-name typekey))) + (switch-to-buffer-other-window "*RefTeX Select*") + (reftex-erase-buffer))) + (unless (eq major-mode 'reftex-select-label-mode) + (reftex-select-label-mode)) + (add-to-list 'selection-buffers (current-buffer)) + (setq truncate-lines t) + (setq mode-line-format + (list "---- " 'mode-line-buffer-identification + " " 'global-mode-string " (" mode-name ")" + " S<" 'refstyle ">" + " -%-")) + (cond + ((= 0 (buffer-size)) + (let ((buffer-read-only nil)) + (message "Creating Selection Buffer...") + (setq offset (reftex-insert-docstruct + buf + toc + typekey + nil ; index + files + context + counter + commented + (or here-I-am offset) + prefix + nil ; no a toc buffer + )))) + (here-I-am + (setq offset (reftex-get-offset buf here-I-am typekey))) + (t (setq offset t))) + (setq buffer-read-only t) + (setq offset (or offset t)) + + (setq here-I-am nil) ; turn off determination of offset + (setq rtn + (reftex-select-item + reftex-select-label-prompt + reftex-select-label-help + reftex-select-label-map + offset + 'reftex-show-label-location follow)) + (setq key (car rtn) + data (nth 1 rtn) + last-data (nth 2 rtn) + offset t) + (unless key (throw 'exit nil)) + (cond + ((eq key ?g) + ;; update buffer + (reftex-erase-buffer)) + ((or (eq key ?r) + (eq key ?R)) + ;; rescan buffer + (and current-prefix-arg (setq key ?R)) + (reftex-erase-buffer) + (reftex-reparse-document buf last-data key)) + ((eq key ?c) + ;; toggle context mode + (reftex-erase-buffer) + (setq context (not context))) + ((eq key ?s) + ;; switch type + (setq here-I-am here-I-am1) + (setq typekey (reftex-query-label-type))) + ((eq key ?t) + ;; toggle table of contents display + (reftex-erase-buffer) + (setq toc (not toc))) + ((eq key ?F) + ;; toggle display of included file borders + (reftex-erase-buffer) + (setq files (not files))) + ((eq key ?#) + ;; toggle counter display + (reftex-erase-buffer) + (setq counter (not counter))) + ((eq key ?%) + ;; toggle display of commented labels + (reftex-erase-buffer) + (setq commented (not commented))) + ((eq key ?l) + ;; reuse the last referenced label again + (setq entries reftex-last-used-reference) + (throw 'exit t)) + ((eq key ?x) + ;; select an external document + (setq xr-index (reftex-select-external-document + xr-alist xr-index)) + (setq buf (or (reftex-get-file-buffer-force + (cdr (nth xr-index xr-alist))) + (error "Cannot switch document")) + prefix (or (car (nth xr-index xr-alist)) "")) + (set-buffer buf) + (reftex-access-scan-info)) + ((stringp key) + (setq entries + (list + (list + (or (assoc key (symbol-value reftex-docstruct-symbol)) + (list key typekey))))) + (throw 'exit t)) + ((memq key '(?a ?A return)) + (cond + (reftex-select-marked + (setq entries (nreverse reftex-select-marked))) + (data + (setq entries (list (list data)))) + (t (setq entries nil))) + (when entries + (if (equal key ?a) (push 'concat entries)) + (setq reftex-last-used-reference entries)) + (set-buffer buf) + (throw 'exit t)) + (t (error "This should not happen (reftex-offer-label-menu)")))))) + (save-excursion + (while reftex-buffers-with-changed-invisibility + (set-buffer (car (car reftex-buffers-with-changed-invisibility))) + (setq buffer-invisibility-spec + (cdr (pop reftex-buffers-with-changed-invisibility))))) + (mapcar (lambda (buf) (and (buffer-live-p buf) (bury-buffer buf))) + selection-buffers) + (reftex-kill-temporary-buffers)) + ;; Add the prefixes, put together the relevant information in the form + ;; (LABEL TYPEKEY SEPERATOR) and return a list of those. + (mapcar (lambda (x) + (if (listp x) + (list (concat prefix (car (car x))) + (nth 1 (car x)) + (nth 2 x)) + x)) + entries))) + +(defun reftex-reparse-document (&optional buffer data key) + ;; Rescan the document. + (save-window-excursion + (save-excursion + (if buffer + (if (not (bufferp buffer)) + (error "No such buffer %s" (buffer-name buffer)) + (set-buffer buffer))) + (let ((arg (if (eq key ?R) '(16) '(4))) + (file (nth 3 data))) + (reftex-access-scan-info arg file))))) + +(defun reftex-query-label-type () + ;; Ask for label type + (let ((key (reftex-select-with-char + reftex-type-query-prompt reftex-type-query-help 3))) + (unless (member (char-to-string key) reftex-typekey-list) + (error "No such label type: %s" (char-to-string key))) + (char-to-string key))) + +(defun reftex-show-label-location (data forward no-revisit + &optional stay error) + ;; View the definition site of a label in another window. + ;; DATA is an entry from the docstruct list. + ;; FORWARD indicates if the label is likely forward from current point. + ;; NO-REVISIT means do not load a file to show this label. + ;; STAY means leave the new window selected. + ;; ERROR means throw an error exception when the label cannot be found. + ;; If ERROR is nil, the return value of this function indicates success. + (let* ((this-window (selected-window)) + (errorf (if error 'error 'message)) + label file buffer re found) + + (catch 'exit + (setq label (nth 0 data) + file (nth 3 data)) + + (unless file + (funcall errorf "Unknown label - reparse might help") + (throw 'exit nil)) + + ;; Goto the file in another window + (setq buffer + (if no-revisit + (reftex-get-buffer-visiting file) + (reftex-get-file-buffer-force + file (not reftex-keep-temporary-buffers)))) + (if buffer + ;; good - the file is available + (switch-to-buffer-other-window buffer) + ;; we have got a problem here. The file does not exist. + ;; Let' get out of here.. + (funcall errorf "Label %s not found" label) + (throw 'exit nil)) + + ;; search for that label + (setq re (format reftex-find-label-regexp-format (regexp-quote label))) + (setq found + (if forward + (re-search-forward re nil t) + (re-search-backward re nil t))) + (unless found + (goto-char (point-min)) + (unless (setq found (re-search-forward re nil t)) + ;; Ooops. Must be in a macro with distributed args. + (setq found + (re-search-forward + (format reftex-find-label-regexp-format2 + (regexp-quote label)) nil t)))) + (if (match-end 3) + (progn + (reftex-highlight 0 (match-beginning 3) (match-end 3)) + (reftex-show-entry (match-beginning 3) (match-end 3)) + (recenter '(4)) + (unless stay (select-window this-window))) + (select-window this-window) + (funcall errorf "Label %s not found" label)) + found))) + +(defvar font-lock-mode) +(defun reftex-show-entry (beg-hlt end-hlt) + ;; Show entry if point is hidden + (let* ((n (/ (reftex-window-height) 2)) + (beg (save-excursion + (re-search-backward "[\n\r]" nil 1 n) (point))) + (end (save-excursion + (re-search-forward "[\n\r]" nil 1 n) (point)))) + (cond + ((and (boundp 'buffer-invisibility-spec) buffer-invisibility-spec + (get-char-property (1+ beg-hlt) 'invisible)) + ;; Invisible with text properties. That is easy to change. + (push (cons (current-buffer) buffer-invisibility-spec) + reftex-buffers-with-changed-invisibility) + (setq buffer-invisibility-spec nil)) + ((string-match "\r" (buffer-substring beg end)) + ;; Invisible with selective display. We need to copy it. + (let ((string (buffer-substring-no-properties beg end))) + (switch-to-buffer "*RefTeX Context Copy*") + (setq buffer-read-only nil) + (erase-buffer) + (insert string) + (subst-char-in-region (point-min) (point-max) ?\r ?\n t) + (goto-char (- beg-hlt beg)) + (reftex-highlight 0 (1+ (- beg-hlt beg)) (1+ (- end-hlt beg))) + (if (reftex-refontify) + (when (or (not (eq major-mode 'latex-mode)) + (not font-lock-mode)) + (latex-mode) + (run-hook-with-args + 'reftex-pre-refontification-functions + reftex-call-back-to-this-buffer 'reftex-hidden) + (turn-on-font-lock)) + (when (or (not (eq major-mode 'fundamental-mode)) + font-lock-mode) + (fundamental-mode))) + (run-hooks 'reftex-display-copied-context-hook) + (setq buffer-read-only t)))))) + +(defun reftex-varioref-vref () + "Insert a reference using the `\vref' macro from the varioref package." + (interactive) + (let ((reftex-format-ref-function 'reftex-format-vref)) + (reftex-reference))) +(defun reftex-fancyref-fref () + "Insert a reference using the `\fref' macro from the fancyref package." + (interactive) + (let ((reftex-format-ref-function 'reftex-format-fref) + ;;(reftex-guess-label-type nil) ;FIXME do we want this???? + ) + (reftex-reference))) +(defun reftex-fancyref-Fref () + "Insert a reference using the `\Fref' macro from the fancyref package." + (interactive) + (let ((reftex-format-ref-function 'reftex-format-Fref) + ;;(reftex-guess-label-type nil) ;FIXME do we want this???? + ) + (reftex-reference))) + +(defun reftex-format-vref (label fmt) + (while (string-match "\\\\ref{" fmt) + (setq fmt (replace-match "\\vref{" t t fmt))) + (format fmt label label)) +(defun reftex-format-Fref (label def-fmt) + (format "\\Fref{%s}" label)) +(defun reftex-format-fref (label def-fmt) + (format "\\fref{%s}" label)) + +;;; reftex-ref.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-sel.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,671 @@ +;;; reftex-sel.el - The selection modes for RefTeX +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-sel) +(require 'reftex) +;;; + +(defvar reftex-select-label-map nil + "Keymap used for *RefTeX Select* buffer, when selecting a label. +This keymap can be used to configure the label selection process which is +started with the command \\[reftex-reference].") + +(defun reftex-select-label-mode () + "Major mode for selecting a label in a LaTeX document. +This buffer was created with RefTeX. +It only has a meaningful keymap when you are in the middle of a +selection process. +To select a label, move the cursor to it and press RET. +Press `?' for a summary of important key bindings. + +During a selection process, these are the local bindings. + +\\{reftex-select-label-map}" + + (interactive) + (kill-all-local-variables) + (make-local-hook 'pre-command-hook) + (make-local-hook 'post-command-hook) + (setq major-mode 'reftex-select-label-mode + mode-name "LSelect") + (set (make-local-variable 'reftex-select-marked) nil) + (when (syntax-table-p reftex-latex-syntax-table) + (set-syntax-table reftex-latex-syntax-table)) + ;; We do not set a local map - reftex-select-item does this. + (run-hooks 'reftex-select-label-mode-hook)) + +(defvar reftex-select-bib-map nil + "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry. +This keymap can be used to configure the BibTeX selection process which is +started with the command \\[reftex-citation].") + +(defun reftex-select-bib-mode () + "Major mode for selecting a citation key in a LaTeX document. +This buffer was created with RefTeX. +It only has a meaningful keymap when you are in the middle of a +selection process. +In order to select a citation, move the cursor to it and press RET. +Press `?' for a summary of important key bindings. + +During a selection process, these are the local bindings. + +\\{reftex-select-label-map}" + (interactive) + (kill-all-local-variables) + (make-local-hook 'pre-command-hook) + (make-local-hook 'post-command-hook) + (setq major-mode 'reftex-select-bib-mode + mode-name "BSelect") + (set (make-local-variable 'reftex-select-marked) nil) + ;; We do not set a local map - reftex-select-item does this. + (run-hooks 'reftex-select-bib-mode-hook)) + +(defun reftex-get-offset (buf here-am-I &optional typekey toc index file) + ;; Find the correct offset data, like insert-docstruct would, but faster. + ;; Buffer BUF knows the correct docstruct to use. + ;; Basically this finds the first docstruct entry after HERE-I-AM which + ;; is of allowed type. The optional arguments specify what is allowed. + (catch 'exit + (save-excursion + (set-buffer buf) + (reftex-access-scan-info) + (let* ((rest (memq here-am-I (symbol-value reftex-docstruct-symbol))) + entry) + (while (setq entry (pop rest)) + (if (or (and typekey + (stringp (car entry)) + (or (equal typekey " ") + (equal typekey (nth 1 entry)))) + (and toc (eq (car entry) 'toc)) + (and index (eq (car entry) 'index)) + (and file + (memq (car entry) '(bof eof file-error)))) + (throw 'exit entry))) + nil)))) + +(defun reftex-insert-docstruct + (buf toc labels index-entries files context counter show-commented + here-I-am xr-prefix toc-buffer) + ;; Insert an excerpt of the docstruct list. + ;; Return the data property of the entry corresponding to HERE-I-AM. + ;; BUF is the buffer which has the correct docstruct-symbol. + ;; LABELS non-nil means to include labels into the list. + ;; When a string, indicates the label type to include + ;; FILES non-nil menas to display file boundaries. + ;; CONTEXT non-nil means to include label context. + ;; COUNTER means to count the labels. + ;; SHOW-COMMENTED means to include also labels which are commented out. + ;; HERE-I-AM is a member of the docstruct list. The function will return + ;; a used member near to this one, as a possible starting point. + ;; XR-PREFIX is the prefix to put in front of labels. + ;; TOC-BUFFER means this is to fill the toc buffer. + (let* ((font (reftex-use-fonts)) + (cnt 0) + (index -1) + (toc-indent " ") + (label-indent + (concat "> " + (if toc (make-string (* 7 reftex-level-indent) ?\ ) ""))) + (context-indent + (concat ". " + (if toc (make-string (* 7 reftex-level-indent) ?\ ) ""))) + (mouse-face + (if (memq reftex-highlight-selection '(mouse both)) + reftex-mouse-selected-face + nil)) + (label-face (reftex-verified-face reftex-label-face + 'font-lock-constant-face + 'font-lock-reference-face)) + (index-face (reftex-verified-face reftex-index-face + 'font-lock-constant-face + 'font-lock-reference-face)) + all cell text label typekey note comment master-dir-re + offset from to index-tag docstruct-symbol) + + ;; Pop to buffer buf to get the correct buffer-local variables + (save-excursion + (set-buffer buf) + + ;; Ensure access to scanning info + (reftex-access-scan-info) + + (setq docstruct-symbol reftex-docstruct-symbol + all (symbol-value reftex-docstruct-symbol) + reftex-active-toc nil + master-dir-re + (concat "\\`" (regexp-quote + (file-name-directory (reftex-TeX-master-file)))))) + + (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol) + (set (make-local-variable 'reftex-prefix) + (cdr (assoc labels reftex-typekey-to-prefix-alist))) + (if (equal reftex-prefix " ") (setq reftex-prefix nil)) + + ;; Walk the docstruct and insert the appropriate stuff + (while (setq cell (pop all)) + + (incf index) + (setq from (point)) + + (if (eq cell here-I-am) (setq offset 'attention)) + + (cond + + ((memq (car cell) '(bib thebib label-numbers appendix + master-dir bibview-cache is-multi xr xr-doc))) + ;; These are currently ignored + + ((memq (car cell) '(bof eof file-error)) + ;; Beginning or end of a file + (when files + (if (eq offset 'attention) (setq offset cell)) + (insert + " File " (if (string-match master-dir-re (nth 1 cell)) + (substring (nth 1 cell) (match-end 0)) + (nth 1 cell)) + (cond ((eq (car cell) 'bof) " starts here\n") + ((eq (car cell) 'eof) " ends here\n") + ((eq (car cell) 'file-error) " was not found\n"))) + (setq to (point)) + (when font + (put-text-property from to + 'face reftex-file-boundary-face)) + (when toc-buffer + (if mouse-face + (put-text-property from (1- to) + 'mouse-face mouse-face)) + (put-text-property from to :data cell)))) + + ((eq (car cell) 'toc) + ;; a table of contents entry + (when toc + (if (eq offset 'attention) (setq offset cell)) + (setq reftex-active-toc cell) + (insert (concat toc-indent (nth 2 cell) "\n")) + (setq to (point)) + (when font + (put-text-property from to + 'face reftex-section-heading-face)) + (when toc-buffer + (if mouse-face + (put-text-property from (1- to) + 'mouse-face mouse-face)) + (put-text-property from to :data cell)) + (goto-char to))) + + ((stringp (car cell)) + ;; a label + (when (null (nth 2 cell)) + ;; No context yet. Quick update. + (setcdr cell (cdr (reftex-label-info-update cell))) + (put docstruct-symbol 'modified t)) + + (setq label (car cell) + typekey (nth 1 cell) + text (nth 2 cell) + comment (nth 4 cell) + note (nth 5 cell)) + + (when (and labels + (or (eq labels t) + (string= typekey labels) + (string= labels " ")) + (or show-commented (null comment))) + + ;; Yes we want this one + (incf cnt) + (if (eq offset 'attention) (setq offset cell)) + + (setq label (concat xr-prefix label)) + (when comment (setq label (concat "% " label))) + (insert label-indent label) + (when font + (setq to (point)) + (put-text-property + (- (point) (length label)) to + 'face (if comment + 'font-lock-comment-face + label-face)) + (goto-char to)) + + (insert (if counter (format " (%d) " cnt) "") + (if comment " LABEL IS COMMENTED OUT " "") + (if (stringp note) (concat " " note) "") + "\n") + (setq to (point)) + + (when context + (insert context-indent text "\n") + (setq to (point))) + (put-text-property from to :data cell) + (when mouse-face + (put-text-property from (1- to) + 'mouse-face mouse-face)) + (goto-char to))) + + ((eq (car cell) 'index) + ;; index entry + (when (and index-entries + (or (eq t index-entries) + (string= index-entries (nth 1 cell)))) + (if (eq offset 'attention) (setq offset cell)) + (setq index-tag (format "<%s>" (nth 1 cell))) + (and font + (put-text-property 0 (length index-tag) + 'face reftex-index-tag-face index-tag)) + (insert label-indent index-tag " " (nth 7 cell)) + + (when font + (setq to (point)) + (put-text-property + (- (point) (length (nth 7 cell))) to + 'face index-face) + (goto-char to)) + (insert "\n") + (setq to (point)) + + (when context + (insert context-indent (nth 2 cell) "\n") + (setq to (point))) + (put-text-property from to :data cell) + (when mouse-face + (put-text-property from (1- to) + 'mouse-face mouse-face)) + (goto-char to))))) + + (when (reftex-refontify) + ;; we need to fontify the buffer + (reftex-fontify-select-label-buffer buf)) + (run-hooks 'reftex-display-copied-context-hook) + offset)) + +(defun reftex-find-start-point (fallback &rest locations) + ;; Set point to the first available LOCATION. When a LOCATION is a list, + ;; search for such a :data text property. When it is an integer, + ;; use is as line number. FALLBACK is a buffer position used if everything + ;; else fails. + (catch 'exit + (goto-char (point-min)) + (let (loc pos) + (while locations + (setq loc (pop locations)) + (cond + ((null loc)) + ((listp loc) + (setq pos (text-property-any (point-min) (point-max) :data loc)) + (when pos + (goto-char pos) + (throw 'exit t))) + ((integerp loc) + (when (<= loc (count-lines (point-min) (point-max))) + (goto-line loc) + (throw 'exit t))))) + (goto-char fallback)))) + +(defvar reftex-last-data nil) +(defvar reftex-last-line nil) +(defvar reftex-select-marked nil) + +(defun reftex-select-item (prompt help-string keymap + &optional offset + call-back cb-flag) +;; Select an item, using PROMPT. The function returns a key indicating +;; an exit status, along with a data structure indicating which item was +;; selected. +;; HELP-STRING contains help. KEYMAP is a keymap with the available +;; selection commands. +;; OFFSET can be a label list item which will be selected at start. +;; When it is t, point will start out at the beginning of the buffer. +;; Any other value will cause restart where last selection left off. +;; When CALL-BACK is given, it is a function which is called with the index +;; of the element. +;; CB-FLAG is the initial value of that flag. + + (let* (ev data last-data (selection-buffer (current-buffer))) + + (setq reftex-select-marked nil) + + (setq ev + (catch 'myexit + (save-window-excursion + (setq truncate-lines t) + + ;; Find a good starting point + (reftex-find-start-point + (point-min) offset reftex-last-data reftex-last-line) + (beginning-of-line 1) + (set (make-local-variable 'reftex-last-follow-point) (point)) + + (unwind-protect + (progn + (use-local-map keymap) + (add-hook 'pre-command-hook 'reftex-select-pre-command-hook nil t) + (add-hook 'post-command-hook 'reftex-select-post-command-hook nil t) + (princ prompt) + (set-marker reftex-recursive-edit-marker (point)) + ;; XEmacs does not run post-command-hook here + (and (featurep 'xemacs) (run-hooks 'post-command-hook)) + (recursive-edit)) + + (set-marker reftex-recursive-edit-marker nil) + (save-excursion + (set-buffer selection-buffer) + (use-local-map nil) + (remove-hook 'pre-command-hook 'reftex-select-pre-command-hook t) + (remove-hook 'post-command-hook + 'reftex-select-post-command-hook t)) + ;; Kill the mark overlays + (mapcar (lambda (c) (delete-overlay (nth 1 c))) + reftex-select-marked))))) + + (set (make-local-variable 'reftex-last-line) + (+ (count-lines (point-min) (point)) (if (bolp) 1 0))) + (set (make-local-variable 'reftex-last-data) last-data) + (reftex-kill-buffer "*RefTeX Help*") + (setq reftex-callback-fwd (not reftex-callback-fwd)) ;; ;-))) + (message "") + (list ev data last-data))) + +;; The following variables are all bound dynamically in `reftex-select-item'. +;; The defvars are here only to silence the byte compiler. + +(defvar found-list) +(defvar cb-flag) +(defvar data) +(defvar prompt) +(defvar last-data) +(defvar call-back) +(defvar help-string) +(defvar refstyle) + +;; The selection commands + +(defun reftex-select-pre-command-hook () + (reftex-unhighlight 1) + (reftex-unhighlight 0)) + +(defun reftex-select-post-command-hook () + (let (b e) + (setq data (get-text-property (point) :data)) + (setq last-data (or data last-data)) + + (when (and data cb-flag + (not (equal reftex-last-follow-point (point)))) + (setq reftex-last-follow-point (point)) + (funcall call-back data reftex-callback-fwd + (not reftex-revisit-to-follow))) + (if data + (setq b (or (previous-single-property-change + (1+ (point)) :data) + (point-min)) + e (or (next-single-property-change + (point) :data) + (point-max))) + (setq b (point) e (point))) + (and (memq reftex-highlight-selection '(cursor both)) + (reftex-highlight 1 b e)) + (if (or (not (pos-visible-in-window-p b)) + (not (pos-visible-in-window-p e))) + (recenter '(4))) + (unless (current-message) + (princ prompt)))) + +(defun reftex-select-next (&optional arg) + "Move to next selectable item." + (interactive "p") + (setq reftex-callback-fwd t) + (or (eobp) (forward-char 1)) + (re-search-forward "^[^. \t\n\r]" nil t arg) + (beginning-of-line 1)) +(defun reftex-select-previous (&optional arg) + "Move to previous selectable item." + (interactive "p") + (setq reftex-callback-fwd nil) + (re-search-backward "^[^. \t\n\r]" nil t arg)) +(defun reftex-select-next-heading (&optional arg) + "Move to next table of contentes line." + (interactive "p") + (end-of-line) + (re-search-forward "^ " nil t arg) + (beginning-of-line)) +(defun reftex-select-previous-heading (&optional arg) + "Move to previous table of contentes line." + (interactive "p") + (re-search-backward "^ " nil t arg)) +(defun reftex-select-quit () + "Abort selection process." + (interactive) + (throw 'myexit nil)) +(defun reftex-select-keyboard-quit () + "Abort selection process." + (interactive) + (throw 'exit t)) +(defun reftex-select-jump-to-previous () + "Jump back to where previous selection process left off." + (interactive) + (let (pos) + (cond + ((and (local-variable-p 'reftex-last-data (current-buffer)) + reftex-last-data + (setq pos (text-property-any (point-min) (point-max) + :data reftex-last-data))) + (goto-char pos)) + ((and (local-variable-p 'reftex-last-line (current-buffer)) + (integerp reftex-last-line)) + (goto-line reftex-last-line)) + (t (ding))))) +(defun reftex-select-toggle-follow () + "Toggle follow mode: Other window follows with full context." + (interactive) + (setq reftex-last-follow-point -1) + (setq cb-flag (not cb-flag))) +(defun reftex-select-toggle-varioref () + "Toggle the macro used for referencing the label between \\ref and \\vref." + (interactive) + (if (string= refstyle "\\ref") + (setq refstyle "\\vref") + (setq refstyle "\\ref")) + (force-mode-line-update)) +(defun reftex-select-toggle-fancyref () + "Toggle the macro used for referencing the label between \\ref and \\vref." + (interactive) + (setq refstyle + (cond ((string= refstyle "\\ref") "\\fref") + ((string= refstyle "\\fref") "\\Fref") + (t "\\ref"))) + (force-mode-line-update)) +(defun reftex-select-show-insertion-point () + "Show the point from where selection was started in another window." + (interactive) + (let ((this-window (selected-window))) + (unwind-protect + (progn + (switch-to-buffer-other-window + (marker-buffer reftex-select-return-marker)) + (goto-char (marker-position reftex-select-return-marker)) + (recenter '(4))) + (select-window this-window)))) +(defun reftex-select-callback () + "Show full context in another window." + (interactive) + (if data (funcall call-back data reftex-callback-fwd nil) (ding))) +(defun reftex-select-accept () + "Accept the currently selected item." + (interactive) + (throw 'myexit 'return)) +(defun reftex-select-mouse-accept (ev) + "Accept the item at the mouse click." + (interactive "e") + (mouse-set-point ev) + (setq data (get-text-property (point) :data)) + (setq last-data (or data last-data)) + (throw 'myexit 'return)) +(defun reftex-select-read-label () + "Use minibuffer to read a label to reference, with completion." + (interactive) + (let ((label (completing-read + "Label: " (symbol-value reftex-docstruct-symbol) + nil nil reftex-prefix))) + (unless (or (equal label "") (equal label reftex-prefix)) + (throw 'myexit label)))) +(defun reftex-select-read-cite () + "Use minibuffer to read a citation key with completion." + (interactive) + (let* ((key (completing-read "Citation key: " found-list)) + (entry (assoc key found-list))) + (cond + ((or (null key) (equal key ""))) + (entry + (setq data entry) + (setq last-data data) + (throw 'myexit 'return)) + (t (throw 'myexit key))))) + +(defun reftex-select-mark (&optional separator) + "Mark the entry." + (interactive) + (let* ((data (get-text-property (point) :data)) + boe eoe ovl) + (or data (error "No entry to mark at point")) + (if (assq data reftex-select-marked) + (error "Entry is already marked")) + (setq boe (or (previous-single-property-change (1+ (point)) :data) + (point-min)) + eoe (or (next-single-property-change (point) :data) (point-max))) + (setq ovl (make-overlay boe eoe)) + (push (list data ovl separator) reftex-select-marked) + (overlay-put ovl 'face reftex-select-mark-face) + (if (featurep 'xemacs) + ;; before-string property is broken in Emacs + (overlay-put ovl 'before-string + (if separator + (format "*%c%d* " separator + (length reftex-select-marked)) + (format "*%d* " (length reftex-select-marked))))) + (message "Entry has mark no. %d" (length reftex-select-marked)))) + +(defun reftex-select-mark-comma () + "Mark the entry and store the `comma' separator." + (interactive) + (reftex-select-mark ?,)) +(defun reftex-select-mark-to () + "Mark the entry and store the `to' separator." + (interactive) + (reftex-select-mark ?-)) +(defun reftex-select-mark-and () + "Mark the entry and store `and' to separator." + (interactive) + (reftex-select-mark ?+)) + +(defun reftex-select-unmark () + "Unmark the entry." + (interactive) + (let* ((data (get-text-property (point) :data)) + (cell (assq data reftex-select-marked)) + (ovl (nth 1 cell)) + (cnt 0) + sep) + (unless cell + (error "No marked entry at point")) + (and ovl (delete-overlay ovl)) + (setq reftex-select-marked (delq cell reftex-select-marked)) + (if (featurep 'xemacs) + ;; before-string property is broken in Emacs + (progn + (setq cnt (1+ (length reftex-select-marked))) + (mapcar (lambda (c) + (setq sep (nth 2 c)) + (overlay-put (nth 1 c) 'before-string + (if sep + (format "*%c%d* " sep (decf cnt)) + (format "*%d* " (decf cnt))))) + reftex-select-marked))) + (message "Entry no longer marked"))) + +(defun reftex-select-help () + "Display a summary of the special key bindings." + (interactive) + (with-output-to-temp-buffer "*RefTeX Help*" + (princ help-string)) + (reftex-enlarge-to-fit "*RefTeX Help*" t)) + +;; Common bindings in reftex-select-label-map and reftex-select-bib-map +(let ((map (make-sparse-keymap))) + (substitute-key-definition + 'next-line 'reftex-select-next map global-map) + (substitute-key-definition + 'previous-line 'reftex-select-previous map global-map) + (substitute-key-definition + 'keyboard-quit 'reftex-select-keyboard-quit map global-map) + (substitute-key-definition + 'newline 'reftex-select-accept map global-map) + + (loop for x in + '((" " . reftex-select-callback) + ("n" . reftex-select-next) + ([(down)] . reftex-select-next) + ("p" . reftex-select-previous) + ([(up)] . reftex-select-previous) + ("f" . reftex-select-toggle-follow) + ("\C-m" . reftex-select-accept) + ([(return)] . reftex-select-accept) + ("q" . reftex-select-quit) + ("." . reftex-select-show-insertion-point) + ("?" . reftex-select-help)) + do (define-key map (car x) (cdr x))) + + ;; The mouse-2 binding + (if (featurep 'xemacs) + (define-key map [(button2)] 'reftex-select-mouse-accept) + (define-key map [(mouse-2)] 'reftex-select-mouse-accept)) + + ;; Digit arguments + (loop for key across "0123456789" do + (define-key map (vector (list key)) 'digit-argument)) + (define-key map "-" 'negative-argument) + + ;; Make two maps + (setq reftex-select-label-map map) + (setq reftex-select-bib-map (copy-keymap map))) + +;; Specific bindings in reftex-select-label-map +(loop for key across "aAcgFlrRstx#%" do + (define-key reftex-select-label-map (vector (list key)) + (list 'lambda '() + "Press `?' during selection to find out about this key." + '(interactive) (list 'throw '(quote myexit) key)))) + +(loop for x in + '(("b" . reftex-select-jump-to-previous) + ("v" . reftex-select-toggle-varioref) + ("V" . reftex-select-toggle-fancyref) + ("m" . reftex-select-mark) + ("u" . reftex-select-unmark) + ("," . reftex-select-mark-comma) + ("-" . reftex-select-mark-to) + ("+" . reftex-select-mark-and) + ([(tab)] . reftex-select-read-label) + ("\C-i" . reftex-select-read-label) + ("\C-c\C-n" . reftex-select-next-heading) + ("\C-c\C-p" . reftex-select-previous-heading)) + do + (define-key reftex-select-label-map (car x) (cdr x))) + +;; Specific bindings in reftex-select-bib-map +(loop for key across "grRaA" do + (define-key reftex-select-bib-map (vector (list key)) + (list 'lambda '() + "Press `?' during selection to find out about this key." + '(interactive) (list 'throw '(quote myexit) key)))) + +(loop for x in + '(("\C-i" . reftex-select-read-cite) + ([(tab)] . reftex-select-read-cite) + ("m" . reftex-select-mark) + ("u" . reftex-select-unmark)) + do (define-key reftex-select-bib-map (car x) (cdr x))) + + +;;; reftex-sel.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-toc.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,583 @@ +;;; reftex-toc.el - RefTeX's table of contents mode +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-toc) +(require 'reftex) +;;; + +(defvar reftex-toc-map (make-sparse-keymap) + "Keymap used for *toc* buffer.") + +(defvar reftex-toc-menu) + +(defun reftex-toc-mode () + "Major mode for managing Table of Contents for LaTeX files. +This buffer was created with RefTeX. +Press `?' for a summary of important key bindings. + +Here are all local bindings. + +\\{reftex-toc-map}" + (interactive) + (kill-all-local-variables) + (setq major-mode 'reftex-toc-mode + mode-name "TOC") + (use-local-map reftex-toc-map) + (set (make-local-variable 'revert-buffer-function) 'reftex-toc-revert) + (set (make-local-variable 'reftex-toc-include-labels-indicator) "") + (set (make-local-variable 'reftex-toc-include-index-indicator) "") + (setq mode-line-format + (list "---- " 'mode-line-buffer-identification + " " 'global-mode-string " (" mode-name ")" + " L<" 'reftex-toc-include-labels-indicator ">" + " I<" 'reftex-toc-include-index-indicator ">" + " -%-")) + (setq truncate-lines t) + (make-local-hook 'post-command-hook) + (make-local-hook 'pre-command-hook) + (make-local-variable 'reftex-last-follow-point) + (add-hook 'post-command-hook 'reftex-toc-post-command-hook nil t) + (add-hook 'pre-command-hook 'reftex-toc-pre-command-hook nil t) + (easy-menu-add reftex-toc-menu reftex-toc-map) + (run-hooks 'reftex-toc-mode-hook)) + +(defvar reftex-last-toc-file nil + "Stores the file name from which `reftex-toc' was called. For redo command.") + +(defvar reftex-last-window-height nil) +(defvar reftex-toc-include-labels-indicator nil) +(defvar reftex-toc-include-index-indicator nil) + +(defvar reftex-toc-return-marker (make-marker) + "Marker which makes it possible to return from toc to old position.") + +(defconst reftex-toc-help +" AVAILABLE KEYS IN TOC BUFFER + ============================ +n / p next-line / previous-line +SPC Show the corresponding location of the LaTeX document. +TAB Goto the location and keep the *toc* window. +RET Goto the location and hide the *toc* window (also on mouse-2). +C-c > Display Index. With prefix arg, restrict index to current section. +q / k Hide/Kill *toc* buffer, return to position of reftex-toc command. +l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders. +f / g Toggle follow mode on and off / Refresh *toc* buffer. +r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document. +. In other window, show position from where `reftex-toc' was called. +x Switch to TOC of external document (with LaTeX package `xr').") + +(defun reftex-toc (&optional rebuild) + "Show the table of contents for the current document. +When called with a raw C-u prefix, rescan the document first." + + (interactive) + + (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file))) + current-prefix-arg) + (reftex-erase-buffer "*toc*")) + + (setq reftex-last-toc-file (buffer-file-name)) + (setq reftex-last-toc-master (reftex-TeX-master-file)) + + (set-marker reftex-toc-return-marker (point)) + + ;; If follow mode is active, arrange to delay it one command + (if reftex-toc-follow-mode + (setq reftex-toc-follow-mode 1)) + + (and reftex-toc-include-index-entries + (reftex-ensure-index-support)) + (or reftex-support-index + (setq reftex-toc-include-index-entries nil)) + + ;; Ensure access to scanning info and rescan buffer if prefix are is '(4) + (reftex-access-scan-info current-prefix-arg) + + (let* ((this-buf (current-buffer)) + (docstruct-symbol reftex-docstruct-symbol) + (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol))) + (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data))) + (here-I-am (if rebuild + (get 'reftex-toc :reftex-data) + (car (reftex-where-am-I)))) + offset) + + (if (get-buffer-window "*toc*") + (select-window (get-buffer-window "*toc*")) + (when (or (not reftex-toc-keep-other-windows) + (< (window-height) (* 2 window-min-height))) + (delete-other-windows)) + (setq reftex-last-window-height (window-height)) ; remember + (split-window) + (let ((default-major-mode 'reftex-toc-mode)) + (switch-to-buffer "*toc*"))) + + (or (eq major-mode 'reftex-toc-mode) (reftex-toc-mode)) + (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol) + (setq reftex-toc-include-labels-indicator + (if (eq reftex-toc-include-labels t) + "ALL" + reftex-toc-include-labels)) + (setq reftex-toc-include-index-indicator + (if (eq reftex-toc-include-index-entries t) + "ALL" + reftex-toc-include-index-entries)) + + (cond + ((= (buffer-size) 0) + ;; buffer is empty - fill it with the table of contents + (message "Building *toc* buffer...") + + (setq buffer-read-only nil) + (insert (format +"TABLE-OF-CONTENTS on %s +SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help +------------------------------------------------------------------------------ +" (abbreviate-file-name reftex-last-toc-master))) + + (if (reftex-use-fonts) + (put-text-property 1 (point) 'face reftex-toc-header-face)) + (put-text-property 1 (point) 'intangible t) + (put-text-property 1 2 'xr-alist xr-alist) + + (setq offset + (reftex-insert-docstruct + this-buf + t ; include toc + reftex-toc-include-labels + reftex-toc-include-index-entries + reftex-toc-include-file-boundaries + reftex-toc-include-context + nil ; counter + nil ; commented + here-I-am + "" ; xr-prefix + t ; a toc buffer + )) + + (run-hooks 'reftex-display-copied-context-hook) + (message "Building *toc* buffer...done.") + (setq buffer-read-only t)) + (t + ;; Only compute the offset + (setq offset + (or (reftex-get-offset this-buf here-I-am + (if reftex-toc-include-labels " " nil) + t + reftex-toc-include-index-entries + reftex-toc-include-file-boundaries) + (reftex-last-assoc-before-elt + 'toc here-I-am + (symbol-value reftex-docstruct-symbol)))) + (put 'reftex-toc :reftex-line 3) + (goto-line 3) + (beginning-of-line))) + + ;; Find the correct starting point + (reftex-find-start-point (point) offset (get 'reftex-toc :reftex-line)) + (setq reftex-last-follow-point (point)))) + +(defun reftex-toc-pre-command-hook () + ;; used as pre command hook in *toc* buffer + (reftex-unhighlight 0) + (reftex-unhighlight 1)) + +(defun reftex-toc-post-command-hook () + ;; used in the post-command-hook for the *toc* buffer + (when (get-text-property (point) :data) + (put 'reftex-toc :reftex-data (get-text-property (point) :data)) + (and (> (point) 1) + (not (get-text-property (point) 'intangible)) + (memq reftex-highlight-selection '(cursor both)) + (reftex-highlight 1 + (or (previous-single-property-change (1+ (point)) :data) + (point-min)) + (or (next-single-property-change (point) :data) + (point-max))))) + (if (integerp reftex-toc-follow-mode) + ;; remove delayed action + (setq reftex-toc-follow-mode t) + (and reftex-toc-follow-mode + (not (equal reftex-last-follow-point (point))) + ;; show context in other window + (setq reftex-last-follow-point (point)) + (condition-case nil + (reftex-toc-visit-location nil (not reftex-revisit-to-follow)) + (error t))))) + +(defun reftex-re-enlarge () + ;; Enlarge windiw to a remembered size + (enlarge-window + (max 0 (- (or reftex-last-window-height (window-height)) + (window-height))))) + +(defun reftex-toc-show-help () + "Show a summary of special key bindings." + (interactive) + (with-output-to-temp-buffer "*RefTeX Help*" + (princ reftex-toc-help)) + (reftex-enlarge-to-fit "*RefTeX Help*" t) + ;; If follow mode is active, arrange to delay it one command + (if reftex-toc-follow-mode + (setq reftex-toc-follow-mode 1))) + +(defun reftex-toc-next (&optional arg) + "Move to next selectable item." + (interactive "p") + (setq reftex-callback-fwd t) + (or (eobp) (forward-char 1)) + (goto-char (or (next-single-property-change (point) :data) + (point)))) +(defun reftex-toc-previous (&optional arg) + "Move to previous selectable item." + (interactive "p") + (setq reftex-callback-fwd nil) + (goto-char (or (previous-single-property-change (point) :data) + (point)))) +(defun reftex-toc-next-heading (&optional arg) + "Move to next table of contentes line." + (interactive "p") + (end-of-line) + (re-search-forward "^ " nil t arg) + (beginning-of-line)) +(defun reftex-toc-previous-heading (&optional arg) + "Move to previous table of contentes line." + (interactive "p") + (re-search-backward "^ " nil t arg)) +(defun reftex-toc-toggle-follow () + "Toggle follow (other window follows with context)." + (interactive) + (setq reftex-last-follow-point -1) + (setq reftex-toc-follow-mode (not reftex-toc-follow-mode))) +(defun reftex-toc-toggle-file-boundary () + "Toggle inclusion of file boundaries in *toc* buffer." + (interactive) + (setq reftex-toc-include-file-boundaries + (not reftex-toc-include-file-boundaries)) + (reftex-toc-revert)) +(defun reftex-toc-toggle-labels (arg) + "Toggle inclusion of labels in *toc* buffer. +With prefix ARG, prompt for a label type and include only labels of +that specific type." + (interactive "P") + (setq reftex-toc-include-labels + (if arg (reftex-query-label-type) + (not reftex-toc-include-labels))) + (reftex-toc-revert)) +(defun reftex-toc-toggle-index (arg) + "Toggle inclusion of index in *toc* buffer. +With prefix arg, prompt for an index tag and include only entries of that +specific index." + (interactive "P") + (setq reftex-toc-include-index-entries + (if arg (reftex-index-select-tag) + (not reftex-toc-include-index-entries))) + (reftex-toc-revert)) +(defun reftex-toc-toggle-context () + "Toggle inclusion of label context in *toc* buffer. +Label context is only displayed when the labels are there as well." + (interactive) + (setq reftex-toc-include-context (not reftex-toc-include-context)) + (reftex-toc-revert)) +(defun reftex-toc-view-line () + "View document location in other window." + (interactive) + (reftex-toc-visit-location)) +(defun reftex-toc-goto-line-and-hide () + "Go to document location in other window. Hide the *toc* window." + (interactive) + (reftex-toc-visit-location 'hide)) +(defun reftex-toc-goto-line () + "Go to document location in other window. *toc* window stays." + (interactive) + (reftex-toc-visit-location t)) +(defun reftex-toc-mouse-goto-line-and-hide (ev) + "Go to document location in other window. Hide the *toc* window." + (interactive "e") + (mouse-set-point ev) + (reftex-toc-visit-location 'hide)) +(defun reftex-toc-show-calling-point () + "Show point where reftex-toc was called from." + (interactive) + (let ((this-window (selected-window))) + (unwind-protect + (progn + (switch-to-buffer-other-window + (marker-buffer reftex-toc-return-marker)) + (goto-char (marker-position reftex-toc-return-marker)) + (recenter '(4))) + (select-window this-window)))) +(defun reftex-toc-quit () + "Hide the *toc* window and do not move point." + (interactive) + (or (one-window-p) (delete-window)) + (switch-to-buffer (marker-buffer reftex-toc-return-marker)) + (reftex-re-enlarge) + (goto-char (or (marker-position reftex-toc-return-marker) (point)))) +(defun reftex-toc-quit-and-kill () + "Kill the *toc* buffer." + (interactive) + (kill-buffer "*toc*") + (or (one-window-p) (delete-window)) + (switch-to-buffer (marker-buffer reftex-toc-return-marker)) + (reftex-re-enlarge) + (goto-char (marker-position reftex-toc-return-marker))) +(defun reftex-toc-display-index (&optional arg) + "Display the index buffer for the current document. +This works just like `reftex-display-index' from a LaTeX buffer. +With prefix arg 1, restrict index to the section at point." + (interactive "P") + (let ((data (get-text-property (point) :data)) + (docstruct (symbol-value reftex-docstruct-symbol)) + bor eor restr) + (when (equal arg 2) + (setq bor (reftex-last-assoc-before-elt 'toc data docstruct) + eor (assoc 'toc (cdr (memq bor docstruct))) + restr (list (nth 6 bor) bor eor))) + (reftex-toc-goto-line) + (reftex-display-index (if restr nil arg) restr))) +(defun reftex-toc-rescan (&rest ignore) + "Regenerate the *toc* buffer by reparsing file of section at point." + (interactive) + (if (and reftex-enable-partial-scans + (null current-prefix-arg)) + (let* ((data (get-text-property (point) :data)) + (what (car data)) + (file (cond ((eq what 'toc) (nth 3 data)) + ((memq what '(eof bof file-error)) (nth 1 data)) + ((stringp what) (nth 3 data)) + ((eq what 'index) (nth 3 data)))) + (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) + (if (not file) + (error "Don't know which file to rescan. Try `C-u r'") + (put 'reftex-toc :reftex-line line) + (switch-to-buffer-other-window + (reftex-get-file-buffer-force file)) + (setq current-prefix-arg '(4)) + (reftex-toc t))) + (reftex-toc-Rescan)) + (reftex-kill-temporary-buffers)) +(defun reftex-toc-Rescan (&rest ignore) + "Regenerate the *toc* buffer by reparsing the entire document." + (interactive) + (switch-to-buffer-other-window + (reftex-get-file-buffer-force reftex-last-toc-file)) + (setq current-prefix-arg '(16)) + (reftex-toc t)) +(defun reftex-toc-revert (&rest ignore) + "Regenerate the *toc* from the internal lists." + (interactive) + (switch-to-buffer-other-window + (reftex-get-file-buffer-force reftex-last-toc-file)) + (reftex-erase-buffer "*toc*") + (setq current-prefix-arg nil) + (reftex-toc t)) +(defun reftex-toc-external (&rest ignore) + "Switch to table of contents of an external document." + (interactive) + (let* ((old-buf (current-buffer)) + (xr-alist (get-text-property 1 'xr-alist)) + (xr-index (reftex-select-external-document + xr-alist 0))) + (switch-to-buffer-other-window (or (reftex-get-file-buffer-force + (cdr (nth xr-index xr-alist))) + (error "Cannot switch document"))) + (reftex-toc) + (if (equal old-buf (current-buffer)) + (message "") + (message "Switched document")))) + +(defun reftex-toc-visit-location (&optional final no-revisit) + ;; Visit the tex file corresponding to the toc entry on the current line. + ;; If FINAL is t, stay there + ;; If FINAL is 'hide, hide the *toc* window. + ;; Otherwise, move cursor back into *toc* window. + ;; NO-REVISIT means don't visit files, just use live biffers. + ;; This function is pretty clever about finding back a section heading, + ;; even if the buffer is not live, or things like outline, x-symbol etc. + ;; have been active. + + (let* ((toc (get-text-property (point) :data)) + (toc-window (selected-window)) + show-window show-buffer match) + + (unless toc (error "Don't know which toc line to visit")) + + (cond + + ((eq (car toc) 'toc) + ;; a toc entry + (setq match (reftex-toc-find-section toc no-revisit))) + + ((eq (car toc) 'index) + ;; an index entry + (setq match (reftex-index-show-entry toc no-revisit))) + + ((memq (car toc) '(bof eof)) + ;; A file entry + (setq match + (let ((where (car toc)) + (file (nth 1 toc))) + (if (or (not no-revisit) (reftex-get-buffer-visiting file)) + (progn + (switch-to-buffer-other-window + (reftex-get-file-buffer-force file nil)) + (goto-char (if (eq where 'bof) (point-min) (point-max)))) + (message reftex-no-follow-message) nil)))) + + ((stringp (car toc)) + ;; a label + (setq match (reftex-show-label-location toc reftex-callback-fwd + no-revisit t)))) + + (setq show-window (selected-window) + show-buffer (current-buffer)) + + (unless match + (select-window toc-window) + (error "Cannot find location")) + + (select-window toc-window) + + ;; use the `final' parameter to decide what to do next + (cond + ((eq final t) + (reftex-unhighlight 0) + (select-window show-window)) + ((eq final 'hide) + (reftex-unhighlight 0) + (or (one-window-p) (delete-window)) + (switch-to-buffer show-buffer) + (reftex-re-enlarge)) + (t nil)))) + +(defun reftex-toc-find-section (toc &optional no-revisit) + (let* ((file (nth 3 toc)) + (marker (nth 4 toc)) + (level (nth 5 toc)) + (literal (nth 7 toc)) + (emergency-point (nth 8 toc)) + (match + (cond + ((and (markerp marker) (marker-buffer marker)) + ;; Buffer is still live and we have the marker. Should be easy. + (switch-to-buffer-other-window (marker-buffer marker)) + (goto-char (marker-position marker)) + (or (looking-at (regexp-quote literal)) + (looking-at (reftex-make-regexp-allow-for-ctrl-m literal)) + (looking-at (reftex-make-desperate-section-regexp literal)) + (looking-at (concat "\\\\" + (regexp-quote + (car + (rassq level + reftex-section-levels-all))) + "[[{]")))) + ((or (not no-revisit) + (reftex-get-buffer-visiting file)) + ;; Marker is lost. Use the backup method. + (switch-to-buffer-other-window + (reftex-get-file-buffer-force file nil)) + (goto-char (or emergency-point (point-min))) + (or (looking-at (regexp-quote literal)) + (let ((len (length literal))) + (or (reftex-nearest-match (regexp-quote literal) len) + (reftex-nearest-match + (reftex-make-regexp-allow-for-ctrl-m literal) len) + (reftex-nearest-match + (reftex-make-desperate-section-regexp literal) len))))) + (t (message reftex-no-follow-message) nil)))) + (when match + (goto-char (match-beginning 0)) + (if (not (= (point) (point-max))) (recenter 1)) + (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer))) + match)) + +(defun reftex-make-desperate-section-regexp (old) + ;; Return a regexp which will still match a section statement even if + ;; x-symbol or isotex or the like have been at work in the mean time. + (let* ((n (1+ (string-match "[[{]" old))) + (new (regexp-quote (substring old 0 (1+ (string-match "[[{]" old))))) + (old (substring old n))) + (while (string-match + "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)" + old) + (if (match-beginning 1) + (setq new (concat new "[^\n\r]*[\n\r]")) + (setq new (concat new "[^\n\r]*" (match-string 3 old)))) + (setq old (substring old (match-end 0)))) + new)) + +;; Table of Contents map +(define-key reftex-toc-map (if (featurep 'xemacs) [(button2)] [(mouse-2)]) + 'reftex-toc-mouse-goto-line-and-hide) + +(substitute-key-definition + 'next-line 'reftex-toc-next reftex-toc-map global-map) +(substitute-key-definition + 'previous-line 'reftex-toc-previous reftex-toc-map global-map) + +(loop for x in + '(("n" . reftex-toc-next) + ("p" . reftex-toc-previous) + ("?" . reftex-toc-show-help) + (" " . reftex-toc-view-line) + ("\C-m" . reftex-toc-goto-line-and-hide) + ("\C-i" . reftex-toc-goto-line) + ("\C-c>". reftex-toc-display-index) + ("r" . reftex-toc-rescan) + ("R" . reftex-toc-Rescan) + ("g" . revert-buffer) + ("q" . reftex-toc-quit) + ("k" . reftex-toc-quit-and-kill) + ("f" . reftex-toc-toggle-follow) + ("F" . reftex-toc-toggle-file-boundary) + ("i" . reftex-toc-toggle-index) + ("l" . reftex-toc-toggle-labels) + ("c" . reftex-toc-toggle-context) + ("%" . reftex-toc-toggle-commented) + ("x" . reftex-toc-external) + ("." . reftex-toc-show-calling-point) + ("\C-c\C-n" . reftex-toc-next-heading) + ("\C-c\C-p" . reftex-toc-previous-heading)) + do (define-key reftex-toc-map (car x) (cdr x))) + +(loop for key across "0123456789" do + (define-key reftex-toc-map (vector (list key)) 'digit-argument)) +(define-key reftex-toc-map "-" 'negative-argument) + +(easy-menu-define + reftex-toc-menu reftex-toc-map + "Menu for Table of Contents buffer" + '("TOC" + ["Show Location" reftex-toc-view-line t] + ["Go To Location" reftex-toc-goto-line t] + ["Exit & Go To Location" reftex-toc-goto-line-and-hide t] + ["Index" reftex-toc-display-index t] + ["Quit" reftex-toc-quit t] + "--" + ["External Document TOC " reftex-toc-external t] + "--" + ("Update" + ["Rebuilt *toc* Buffer" revert-buffer t] + ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans] + ["Rescan Entire Document" reftex-toc-Rescan t]) + ("Options" + "TOC Items" + ["File Boundaries" reftex-toc-toggle-file-boundary :style toggle + :selected reftex-toc-include-file-boundaries] + ["Labels" reftex-toc-toggle-labels :style toggle + :selected reftex-toc-include-labels] + ["Index Entries" reftex-toc-toggle-index :style toggle + :selected reftex-toc-include-index-entries] + ["Context" reftex-toc-toggle-context :style toggle + :selected reftex-toc-include-context] + "--" + ["Follow Mode" reftex-toc-toggle-follow :style toggle + :selected reftex-toc-follow-mode]) + "--" + ["Help" reftex-toc-show-help t])) + + +;;; reftex-toc.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-vars.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,1547 @@ +;;; reftex-vars.el - Configuration variables for RefTeX +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-vars) + +;; Define the two constants which are needed during compilation + +(eval-and-compile +(defconst reftex-label-alist-builtin + '( + ;; Some aliases, mostly for backward compatibility + (Sideways "Alias for -->rotating" (rotating)) + (AMSTeX "amsmath with eqref macro" + ((nil ?e nil "~\\eqref{%s}") + amsmath)) + + ;; Individual package defaults + (amsmath "AMS-LaTeX math environments" + (("align" ?e nil nil eqnarray-like) + ("gather" ?e nil nil eqnarray-like) + ("multline" ?e nil nil t) + ("flalign" ?e nil nil eqnarray-like) + ("alignat" ?e nil nil alignat-like) + ("xalignat" ?e nil nil alignat-like) + ("xxalignat" ?e nil nil alignat-like) + ("subequations" ?e nil nil t))) + + (endnotes "The \\endnote macro" + (("\\endnote[]{}" ?N "en:" "~\\ref{%s}" 2 + (regexp "endnotes?" "notes?" "Anmerkung\\(en\\)?" "Anm\\.")))) + + (fancybox "The Beqnarray environment" + (("Beqnarray" ?e nil nil eqnarray-like))) + + (floatfig "The floatingfigure environment" + (("floatingfigure" ?f nil nil caption))) + + (longtable "The longtable environment" + (("longtable" ?t nil nil caption))) + + (picinpar "The figwindow and tabwindow environments" + (("figwindow" ?f nil nil 1) + ("tabwindow" ?f nil nil 1))) + + (rotating "Sidewaysfigure and table" + (("sidewaysfigure" ?f nil nil caption) + ("sidewaystable" ?t nil nil caption))) + + (sidecap "CSfigure and SCtable" + (("SCfigure" ?f nil nil caption) + ("SCtable" ?t nil nil caption))) + + (subfigure "Subfigure environments/macro" + (("subfigure" ?f nil nil caption) + ("subfigure*" ?f nil nil caption) + ("\\subfigure[]{}" ?f nil nil 1))) + + (supertab "Supertabular environment" + (("supertabular" ?t nil nil "\\tablecaption{"))) + + (wrapfig "The wrapfigure environment" + (("wrapfigure" ?f nil nil caption))) + + ;; The LaTeX core stuff + (LaTeX "LaTeX default environments" + (("section" ?s "%S" "~\\ref{%s}" (nil . t) + (regexp "parts?" "chapters?" "chap\\." "sections?" "sect?\\." + "paragraphs?" "par\\." + "\\\\S" "\247" "Teile?" "Kapitel" "Kap\\." "Abschnitte?" + "appendi\\(x\\|ces\\)" "App\\." "Anh\"?ange?" "Anh\\.")) + + ("enumerate" ?i "item:" "~\\ref{%s}" item + (regexp "items?" "Punkte?")) + + ("equation" ?e "eq:" "~(\\ref{%s})" t + (regexp "equations?" "eqs?\\." "eqn\\." "Gleichung\\(en\\)?" "Gl\\.")) + ("eqnarray" ?e "eq:" nil eqnarray-like) + + ("figure" ?f "fig:" "~\\ref{%s}" caption + (regexp "figure?[sn]?" "figs?\\." "Abbildung\\(en\\)?" "Abb\\.")) + ("figure*" ?f nil nil caption) + + ("table" ?t "tab:" "~\\ref{%s}" caption + (regexp "tables?" "tab\\." "Tabellen?")) + ("table*" ?t nil nil caption) + + ("\\footnote[]{}" ?n "fn:" "~\\ref{%s}" 2 + (regexp "footnotes?" "Fussnoten?")) + + ("any" ?\ " " "~\\ref{%s}" nil) + + ;; The label macro is hard coded, but it *could* be defined like this: + ;;("\\label{*}" nil nil nil nil) + )) + + ) + "The default label environment descriptions. +Lower-case symbols correspond to a style file of the same name in the LaTeX +distribution. Mixed-case symbols are convenience aliases.") + +(defconst reftex-cite-format-builtin + '((default "Default macro \\cite{%l}" + "\\cite{%l}") + (natbib "The Natbib package" + ((?\C-m . "\\cite{%l}") + (?t . "\\citet{%l}") + (?T . "\\citet*{%l}") + (?p . "\\citep{%l}") + (?P . "\\citep*{%l}") + (?e . "\\citep[e.g.][]{%l}") + (?s . "\\citep[see][]{%l}") + (?a . "\\citeauthor{%l}") + (?A . "\\citeauthor*{%l}") + (?y . "\\citeyear{%l}"))) + (harvard "The Harvard package" + ((?\C-m . "\\cite{%l}") + (?p . "\\cite{%l}") + (?t . "\\citeasnoun{%l}") + (?n . "\\citeasnoun{%l}") + (?s . "\\possessivecite{%l}") + (?e . "\\citeaffixed{%l}{?}") + (?y . "\\citeyear{%l}") + (?a . "\\citename{%l}"))) + (chicago "The Chicago package" + ((?\C-m . "\\cite{%l}") + (?t . "\\citeN{%l}") + (?T . "\\shortciteN{%l}") + (?p . "\\cite{%l}") + (?P . "\\shortcite{%l}") + (?a . "\\citeA{%l}") + (?A . "\\shortciteA{%l}") + (?y . "\\citeyear{%l}"))) + (astron "The Astron package" + ((?\C-m . "\\cite{%l}") + (?p . "\\cite{%l}" ) + (?t . "%2a (\\cite{%l})"))) + (author-year "Do-it-yourself Author-year" + ((?\C-m . "\\cite{%l}") + (?t . "%2a (%y)\\nocite{%l}") + (?p . "(%2a %y\\nocite{%l})"))) + (locally "Full info in parenthesis" + "(%2a %y, %j %v, %P, %e: %b, %u, %s %<)") + ) + "Builtin versions of the citation format. +The following conventions are valid for all alist entries: +`?\C-m' should always point to a straight \\cite{%l} macro. +`?t' should point to a textual citation (citation as a noun). +`?p' should point to a parenthetical citation.") + +(defconst reftex-index-macros-builtin + '((default "Default \\index and \\glossary macros" + (("\\index{*}" "idx" ?i "" nil) + ("\\glossary{*}" "glo" ?g "" nil))) + (multind "The multind.sty package" + (("\\index{}{*}" 1 ?i "" nil))) + (index "The index.sty package" + (("\\index[]{*}" 1 ?i "" nil) + ("\\index*[]{*}" 1 ?I "" nil))) + (Index-Shortcut "index.sty with \\shortindexingon" + (("\\index[]{*}" 1 ?i "" nil) + ("\\index*[]{*}" 1 ?I "" nil) + ("^[]{*}" 1 ?^ "" texmathp) + ("_[]{*}" 1 ?_ "" texmathp)))) + "Builtin stuff for reftex-index-macros. +Lower-case symbols correspond to a style file of the same name in the LaTeX +distribution. Mixed-case symbols are convenience aliases.") +) + +;; Configuration Variables and User Options for RefTeX ------------------ + +(defgroup reftex nil + "LaTeX label and citation support." + :tag "RefTeX" + :link '(url-link :tag "Home Page" + "http://strw.leidenuniv.nl/~dominik/Tools/") + :link '(emacs-commentary-link :tag "Commentary in reftex.el" "reftex.el") + :link '(custom-manual "(reftex)Top") + :prefix "reftex-" + :group 'tex) + +;; Table of contents configuration -------------------------------------- + +(defgroup reftex-table-of-contents-browser nil + "A multifile table of contents browser." + :group 'reftex) + +(defcustom reftex-toc-keep-other-windows t + "*Non-nil means, split the selected window to display the *toc* buffer. +This helps to keep the window configuration, but makes the *toc* small. +When nil, all other windows except the selected one will be deleted, so +that the *toc* window fills half the frame." + :group 'reftex-table-of-contents-browser + :type 'boolean) + +(defcustom reftex-toc-include-file-boundaries nil + "*Non-nil means, include file boundaries in *toc* buffer. +This flag can be toggled from within the *toc* buffer with the `F' key." + :group 'reftex-table-of-contents-browser + :type 'boolean) + +(defcustom reftex-toc-include-labels nil + "*Non-nil means, include labels in *toc* buffer. +This flag can be toggled from within the *toc* buffer with the `l' key." + :group 'reftex-table-of-contents-browser + :type 'boolean) + +(defcustom reftex-toc-include-index-entries nil + "*Non-nil means, include index entries in *toc* buffer. +This flag can be toggled from within the *toc* buffer with the `i' key." + :group 'reftex-table-of-contents-browser + :type 'boolean) + +(defcustom reftex-toc-include-context nil + "*Non-nil means, include context with labels in the *toc* buffer. +Context will only be shown when labels are visible as well. +This flag can be toggled from within the *toc* buffer with the `c' key." + :group 'reftex-table-of-contents-browser + :type 'boolean) + +(defcustom reftex-toc-follow-mode nil + "*Non-nil means, point in *toc* buffer will cause other window to follow. +The other window will show the corresponding part of the document. +This flag can be toggled from within the *toc* buffer with the `f' key." + :group 'reftex-table-of-contents-browser + :type 'boolean) + +(defcustom reftex-revisit-to-follow nil + "*Non-nil means, follow-mode will revisit files if necessary. +When nil, follow-mode will be suspended for stuff in unvisited files." + :group 'reftex-table-of-contents-browser + :group 'reftex-referencing-labels + :type 'boolean) + +(defcustom reftex-toc-mode-hook nil + "Mode hook for reftex-toc-mode." + :group 'reftex-table-of-contents-browser + :type 'hook) + +;; Label Support Configuration + +(defgroup reftex-label-support nil + "Support for creation, insertion and referencing of labels in LaTeX." + :group 'reftex) + +(defgroup reftex-defining-label-environments nil + "Definition of environments and macros to do with label." + :group 'reftex-label-support) + +(defcustom reftex-default-label-alist-entries + '(amsmath endnotes fancybox floatfig longtable picinpar + rotating sidecap subfigure supertab wrapfig LaTeX) + "Default label alist specifications. LaTeX should always be the last entry. +The value of this variable is a list of symbols with associations in the +constant `reftex-label-alist-builtin'. Check that constant for a full list +of options." + :group 'reftex-defining-label-environments + :set 'reftex-set-dirty + :type `(set + :indent 4 + :inline t + :greedy t + ,@(mapcar + (lambda (x) + (list 'const :tag (concat (symbol-name (nth 0 x)) + ": " (nth 1 x)) + (nth 0 x))) + reftex-label-alist-builtin))) + +(defcustom reftex-label-alist nil + "Alist with information on environments for \\label-\\ref use. + +This docstring is easier to understand after reading the configuration +examples in `reftex.el'. Looking at the builtin defaults in the constant +`reftex-label-alist-builtin' may also be instructive. + +Set this variable to define additions and changes to the default. The only +things you MUST NOT change is that `?s' is the type indicator for section +labels, and SPC for the `any' label type. These are hard-coded at other +places in the code. + +The value of the variable must be a list of items. Each item is a list +itself and has the following structure: + + (ENV-OR-MACRO TYPE-KEY LABEL-PREFIX REFERENCE-FORMAT CONTEXT-METHOD + (MAGIC-WORD ... )) + +Each list entry describes either an environment carrying a counter for use +with \\label and \\ref, or a LaTeX macro defining a label as (or inside) +one of its arguments. The elements of each list entry are: + +ENV-OR-MACRO + Name of the environment (like \"table\") or macro (like \"\\\\myfig\"). + For macros, indicate the macro arguments for best results, as in + \"\\\\myfig[]{}{}{*}{}\". Use square brackets for optional arguments, + a star to mark the label argument, if any. The macro does not have to + have a label argument - you could also use \\label{..} inside one of + its arguments. + Special names: `section' for section labels, `any' to define a group + which contains all labels. + + This may also be a function to do local parsing and identify point + to be in a a non-standard label environment. The function must take + an argument BOUND and limit backward searches to this value. It + should return either nil or a cons cell (FUNCTION . POSITION) with + the function symbol and the position where the special environment + starts. See the Info documentation for an example. + + Finally this may also be nil if the entry is only meant to change + some settings associated with the type indicator character (see below). + +TYPE-KEY + Type indicator character, like `?t', must be a printable ASCII character. + The type indicator is a single character which defines a label type. + Any label inside the environment or macro is assumed to belong to this + type. The same character may occur several times in this list, to cover + cases in which different environments carry the same label type (like + `equation' and `eqnarray'). + If the type indicator is nil and the macro has a label argument {*}, + the macro defines neutral labels just like \label. In this case + the reminder of this entry is ignored. + +LABEL-PREFIX + Label prefix string, like \"tab:\". + The prefix is a short string used as the start of a label. It may be the + empty string. The prefix may contain the following `%' escapes: + %f Current file name with directory and extension stripped. + %F Current file name relative to directory of master file. + %u User login name, on systems which support this. + %S A section prefix derived with variable `reftex-section-prefixes'. + + Example: In a file `intro.tex', \"eq:%f:\" will become \"eq:intro:\"). + +REFERENCE-FORMAT + Format string for reference insert in buffer. `%s' will be replaced by + the label. + When the format starts with `~', the `~' will only be inserted if + there is not already a whitespace before point. + +CONTEXT-METHOD + Indication on how to find the short context. + - If nil, use the text following the \\label{...} macro. + - If t, use + - the section heading for section labels. + - text following the \\begin{...} statement of environments. + (not a good choice for environments like eqnarray or enumerate, + where one has several labels in a single environment). + - text after the macro name (starting with the first arg) for macros. + - If an integer, use the nth argument of the macro. As a special case, + 1000 means to get text after the last macro argument. + - If a string, use as regexp to search *backward* from the label. Context + is then the text following the end of the match. E.g. putting this to + \"\\\\\\\\caption[[{]\" will use the caption in a figure or table + environment. + \"\\\\\\\\begin{eqnarray}\\\\|\\\\\\\\\\\\\\\\\" works for eqnarrays. + - If any of `caption', `item', `eqnarray-like', `alignat-like', this + symbol will internally be translated into an appropriate regexp + (see also the variable `reftex-default-context-regexps'). + - If a function, call this function with the name of the environment/macro + as argument. On call, point will be just after the \\label macro. The + function is expected to return a suitable context string. It should + throw an exception (error) when failing to find context. + As an example, here is a function returning the 10 chars following + the label macro as context: + + (defun my-context-function (env-or-mac) + (if (> (point-max) (+ 10 (point))) + (buffer-substring (point) (+ 10 (point))) + (error \"Buffer too small\"))) + + Label context is used in two ways by RefTeX: For display in the label + menu, and to derive a label string. If you want to use a different + method for each of these, specify them as a dotted pair. + E.g. `(nil . t)' uses the text after the label (nil) for display, and + text from the default position (t) to derive a label string. This is + actually used for section labels. + +MAGIC-WORDS + List of magic words which identify a reference to be of this type. + If the word before point is equal to one of these words when calling + `reftex-reference', the label list offered will be automatically + restricted to labels of the correct type. + If the first element of this wordlist is the symbol `regexp', the + strings are interpreted as regular expressions. RefTeX will add + a \"\\\\W\" to the beginning and other stuff to the end of the regexp. + +If the type indicator characters of two or more entries are the same, RefTeX +will use + - the first non-nil format and prefix + - the magic words of all involved entries. + +Any list entry may also be a symbol. If that has an association in +`reftex-label-alist-builtin', the cddr of that association is spliced into the +list. However, builtin defaults should normally be set with the variable +`reftex-default-label-alist-entries." + :group 'reftex-defining-label-environments + :set 'reftex-set-dirty + :type + `(repeat + (choice :tag "Package or Detailed " + :value ("" ?a nil nil nil nil) + (list :tag "Detailed Entry" + :value ("" ?a nil nil nil nil) + (choice :tag "Environment or \\macro " + (const :tag "Ignore, just use typekey" nil) + (string "") + (symbol :tag "Special parser" my-parser)) + (choice :tag "Type specification " + (const :tag "unspecified, like in \\label" nil) + (character :tag "Char " ?a)) + (choice :tag "Label prefix string " + (const :tag "Default" nil) + (string :tag "String" "lab:")) + (choice :tag "Label reference format" + (const :tag "Default" nil) + (string :tag "String" "~\\ref{%s}")) + (choice :tag "Context method " + (const :tag "Default position" t) + (const :tag "After label" nil) + (number :tag "Macro arg nr" 1) + (regexp :tag "Regexp" "") + (const :tag "Caption in float" caption) + (const :tag "Item in list" item) + (const :tag "Eqnarray-like" eqnarray-like) + (const :tag "Alignat-like" alignat-like) + (symbol :tag "Function" my-func)) + (repeat :tag "Magic words" :extra-offset 2 (string))) + (choice + :tag "Package" + :value AMSTeX + ,@(mapcar + (lambda (x) + (list 'const :tag (concat (symbol-name (nth 0 x))) + (nth 0 x))) + reftex-label-alist-builtin))))) + +;; LaTeX section commands and level numbers +(defcustom reftex-section-levels + '( + ("part" . 0) + ("chapter" . 1) + ("section" . 2) + ("subsection" . 3) + ("subsubsection" . 4) + ("paragraph" . 5) + ("subparagraph" . 6) + ("subsubparagraph" . 7) + ("addchap" . -1) ; KOMA-Script + ("addsec" . -2) ; KOMA-Script +;;; ("minisec" . -7) ; KOMA-Script + ) + "Commands and levels used for defining sections in the document. +This is an alist with each element like (COMMAND-NAME . LEVEL). +The car of each cons cell is the name of the section macro (without +the backslash). The cdr is a number indicating its level. A negative +level means the same level as the positive value, but the section will +never get a number." + :group 'reftex-defining-label-environments + :set 'reftex-set-dirty + :type '(repeat + (cons (string :tag "sectioning macro" "") + (number :tag "level " 0)))) + +(defcustom reftex-section-prefixes '((0 . "part:") (1 . "cha:") (t . "sec:")) + "Prefixes for section labels. +When the label prefix given in an entry in `reftex-label-alist' contains `%S', +this list is used to determine the correct prefix string depending on the +current section level. +The list is an alist, with each entry of the form (KEY . PREFIX) +Possible keys are sectioning macro names like `chapter', section levels +(as given in `reftex-section-levels'), and t for the default." + :group 'reftex-defining-label-environments + :type '(repeat + (cons :value (0 . "") + (choice + (string :tag "macro name") + (integer :tag "section level") + (const :tag "default" t)) + (string :tag "Prefix")))) + +(defcustom reftex-default-context-regexps + '((caption . "\\\\\\(rot\\)?caption\\*?[[{]") + (item . "\\\\item\\(\\[[^]]*\\]\\)?") + (eqnarray-like . "\\\\begin{%s}\\|\\\\\\\\") + (alignat-like . "\\\\begin{%s}{[0-9]*}\\|\\\\\\\\")) +"Alist with default regular expressions for finding context. +The form (format regexp (regexp-quote environment)) is used to calculate +the final regular expression - so %s will be replaced with the environment +or macro." + :group 'reftex-defining-label-environments + :type '(repeat (cons (symbol) (regexp)))) + +(defcustom reftex-special-environment-functions nil + "List of functions to be called when trying to figure out current environment. +These are special functions to detect \"environments\" which do not +start with \\begin and end with \\end. Some LaTeX packages seem to +use such non-standard ways to set up environment-like constructs. The +purpose of each function in this list is to detect if point is +currently inside such a special \"environment\". If the environment +carries a label, you must also set up an entry for it in +`reftex-label-alist'. + +The function should check if point is currently in the special +environment it was written to detect. If so, the function must return +a cons cell (NAME . POSITION). NAME is the name of the environment +detected and POSITION is the buffer position where the environment +starts. The function must return nil on failure to detect the +environment. + +The function must take an argument BOUND. If non-nil, BOUND is a +boundary for backwards searches which should be observed. + +Here is an example. The LaTeX package linguex.sty defines list macros +`\\ex.', `\\a.', etc for lists which are terminated by `\\z.' or an empty +line. + + \\ex. \\label{ex:12} Some text in an exotic language ... + \\a. \\label{ex:13} more stuff + \\b. \\label{ex:14} still more stuff + + ... more text after the empty line terminating all lists + +And here is the setup for RefTeX: + +1. Define a dummy environment for this in `reftex-label-alist'. Dummy means, + make up an environment name even though it is not used with \\begin and + \\end. Here we use \"linguex\" as this name. + + (setq reftex-label-alist + '((\"linguex\" ?x \"ex:\" \"~\\\\ref{%s}\" nil (\"Example\" \"Ex.\")))) + +2. Write a function to detect the list macros and the determinators as well. + + (defun my-detect-linguex-list (bound) + (let ((pos (point)) p1) + (save-excursion + ;; Search for any of the linguex item macros at the beginning of a line + (if (re-search-backward + \"^[ \\t]*\\\\(\\\\\\\\\\\\(ex\\\\|a\\\\|b\\\\|c\\\\|d\\\\|e\\\\|f\\\\)g?\\\\.\\\\)\" bound t) + (progn + (setq p1 (match-beginning 1)) + ;; Make sure no empty line or \\z. is between us and the item macro + (if (re-search-forward \"\\n[ \\t]*\\n\\\\|\\\\\\\\z\\\\.\" pos t) + ;; Return nil because list was already closed + nil + ;; OK, we got it + (cons \"linguex\" p1))) + ;; Return nil for not found + nil)))) + +3. Tell RefTeX to use this function + + (setq reftex-special-environment-functions '(my-detect-linguex-list)) +" + :group 'reftex-defining-label-environments + :type 'hook) + +;; Label insertion + +(defgroup reftex-making-and-inserting-labels nil + "Options on how to create new labels." + :group 'reftex-label-support) + +(defcustom reftex-insert-label-flags '("s" "sft") + "Flags governing label insertion. First flag DERIVE, second flag PROMPT. + +If DERIVE is t, RefTeX will try to derive a sensible label from context. +A section label for example will be derived from the section heading. +The conversion of the context to a legal label is governed by the +specifications given in `reftex-derive-label-parameters'. +If RefTeX fails to derive a label, it will prompt the user. +If DERIVE is nil, the label generated will consist of the prefix and a +unique number, like `eq:23'. + +If PROMPT is t, the user will be prompted for a label string. The prompt will +already contain the prefix, and (if DERIVE is t) a default label derived from +context. When PROMPT is nil, the default label will be inserted without +query. + +So the combination of DERIVE and PROMPT controls label insertion. Here is a +table describing all four possibilities: + +DERIVE PROMPT ACTION +------------------------------------------------------------------------- + nil nil Insert simple label, like eq:22 or sec:13. No query. + nil t Prompt for label. + t nil Derive a label from context and insert without query. + t t Derive a label from context and prompt for confirmation. + +Each flag may be set to t, nil, or a string of label type letters +indicating the label types for which it should be true. The strings work +like character classes. +Thus, the combination may be set differently for each label type. The +default settings \"s\" and \"sft\" mean: Derive section labels from headings +(with confirmation). Prompt for figure and table labels. Use simple labels +without confirmation for everything else. +The available label types are: s (section), f (figure), t (table), i (item), +e (equation), n (footnote), N (endnote), plus any definitions in +`reftex-label-alist'." + :group 'reftex-making-and-inserting-labels + :type '(list (choice :tag "Derive label from context" + (const :tag "always" t) + (const :tag "never" nil) + (string :tag "selected label types" "")) + (choice :tag "Prompt for label string " + :entry-format " %b %v" + (const :tag "always" t) + (const :tag "never" nil) + (string :tag "selected label types" "")))) + +(defcustom reftex-string-to-label-function 'reftex-string-to-label + "Function to turn an arbitrary string into a legal label. +RefTeX's default function uses the variable `reftex-derive-label-parameters'." + :group 'reftex-making-and-inserting-labels + :type 'symbol) + +(defcustom reftex-translate-to-ascii-function 'reftex-latin1-to-ascii + "Filter function which will process a context string before it is used +to derive a label from it. The intended application is to convert ISO or +Mule characters into something legal in labels. The default function +removes the accents from Latin-1 characters. X-Symbol (>=2.6) sets this +variable to the much more general `x-symbol-translate-to-ascii'." + :group 'reftex-making-and-inserting-labels + :type 'symbol) + +(defcustom reftex-derive-label-parameters '(3 20 t 1 "-" + ("the" "on" "in" "off" "a" "for" "by" "of" "and" "is" "to") t) + "Parameters for converting a string into a label. +This variable is a list of the following items. + +NWORDS Number of words to use. +MAXCHAR Maximum number of characters in a label string. +ILLEGAL nil: Throw away any words containing characters illegal in labels. + t: Throw away only the illegal characters, not the whole word. +ABBREV nil: Never abbreviate words. + t: Always abbreviate words (see `reftex-abbrev-parameters'). + not t and not nil: Abbreviate words if necessary to shorten + label string below MAXCHAR. +SEPARATOR String separating different words in the label. +IGNOREWORDS List of words which should not be part of labels. +DOWNCASE t: Downcase words before using them." + :group 'reftex-making-and-inserting-labels + :type '(list (integer :tag "Number of words " 3) + (integer :tag "Maximum label length " 20) + (choice :tag "Illegal characters in words" + (const :tag "throw away entire word" nil) + (const :tag "throw away single chars" t)) + (choice :tag "Abbreviate words " + (const :tag "never" nil) + (const :tag "always" t) + (const :tag "when label is too long" 1)) + (string :tag "Separator between words " "-") + (repeat :tag "Ignore words" + :entry-format " %i %d %v" + (string :tag "")) + (option (boolean :tag "Downcase words ")))) + +(defcustom reftex-label-illegal-re "[^-a-zA-Z0-9_+=:;,.]" + "Regexp matching characters not legal in labels." + :group 'reftex-making-and-inserting-labels + :type '(regexp :tag "Regular Expression")) + +(defcustom reftex-abbrev-parameters '(4 2 "^aeiou" "aeiou") + "Parameters for abbreviation of words. +This variable is a list of the following items. + +MIN-CHARS Minimum number of characters remaining after abbreviation. +MIN-KILL Minimum number of characters to remove when abbreviating words. +BEFORE Character class before abbrev point in word. +AFTER Character class after abbrev point in word." + :group 'reftex-making-and-inserting-labels + :type '(list + (integer :tag "Minimum chars per word" 4) + (integer :tag "Shorten by at least " 2) + (string :tag "cut before char class " "^saeiou") + (string :tag "cut after char class " "aeiou"))) + +(defcustom reftex-format-label-function nil + "Function which produces the string to insert as a label definition. +Normally should be nil, unless you want to do something fancy. +The function will be called with two arguments, the LABEL and the DEFAULT +FORMAT, which usually is `\label{%s}'. The function should return the +string to insert into the buffer." + :group 'reftex-making-and-inserting-labels + :type 'function) + +;; Label referencing + +(defgroup reftex-referencing-labels nil + "Options on how to reference labels." + :group 'reftex-label-support) + +(eval-and-compile + (defconst reftex-tmp + '((const :tag "on" t) + (const :tag "off" nil) + (string :tag "Selected label types")))) + +(defcustom reftex-label-menu-flags '(t t nil nil nil nil t nil) + "List of flags governing the label menu makeup. +The flags are: + +TABLE-OF-CONTENTS Show the labels embedded in a table of context. +SECTION-NUMBERS Include section numbers (like 4.1.3) in table of contents. +COUNTERS Show counters. This just numbers the labels in the menu. +NO-CONTEXT Non-nil means do NOT show the short context. +FOLLOW Follow full context in other window. +SHOW-COMMENTED Show labels from regions which are commented out. +MATCH-IN-TOC Obsolete flag. +SHOW FILES Show begin and end of included files. + +Each of these flags can be set to t or nil, or to a string of type letters +indicating the label types for which it should be true. These strings work +like character classes in regular expressions. Thus, setting one of the +flags to \"sf\" makes the flag true for section and figure labels, nil +for everything else. Setting it to \"^sf\" makes it the other way round. +The available label types are: s (section), f (figure), t (table), i (item), +e (equation), n (footnote), plus any definitions in `reftex-label-alist'. + +Most options can also be switched from the label menu itself - so if you +decide here to not have a table of contents in the label menu, you can still +get one interactively during selection from the label menu." + :group 'reftex-referencing-labels + :type + `(list + (choice :tag "Embed in table of contents " ,@reftex-tmp) + (choice :tag "Show section numbers " ,@reftex-tmp) + (choice :tag "Show individual counters " ,@reftex-tmp) + (choice :tag "Hide short context " ,@reftex-tmp) + (choice :tag "Follow context in other window " ,@reftex-tmp) + (choice :tag "Show commented labels " ,@reftex-tmp) + (choice :tag "Obsolete flag, Don't use. " ,@reftex-tmp) + (choice :tag "Show begin/end of included files" ,@reftex-tmp))) + +(defcustom reftex-multiref-punctuation '((?, . ", ") (?- . "--") (?+ . " and ")) + "Punctuation strings for multiple references. +When marking is used in the selection buffer to select several references, +this variable associates the 3 marking characters `,-+' with prefix strings +to be inserted into the buffer before the corresponding \ref macro. +This is used to string together whole reference sets, like +`eqs. 1,2,3-5,6 and 7' in a single call to `reftex-reference'. See manual." + :group 'reftex-referencing-labels + :type '(repeat (cons (character) (string)))) + +(defcustom reftex-vref-is-default nil + "*Non-nil means, the varioref macro \\vref is used as default. +In the selection buffer, the `v' key toggles the reference macro between +`\\ref' and `\\vref'. The value of this variable determines the default +which is active when entering the selection process. +Instead of nil or t, this may also be a string of type letters indicating +the label types for which it should be true." + :group 'reftex-referencing-labels + :type `(choice :tag "\\vref is default macro" ,@reftex-tmp)) + +(defcustom reftex-fref-is-default nil + "*Non-nil means, the fancyref macro \\fref is used as default. +In the selection buffer, the `V' key toggles the reference macro between +`\\ref', `\\fref' and `\\Fref'. The value of this variable determines +the default which is active when entering the selection process. +Instead of nil or t, this may also be a string of type letters indicating +the label types for which it should be true." + :group 'reftex-referencing-labels + :type `(choice :tag "\\fref is default macro" ,@reftex-tmp)) + +(defcustom reftex-level-indent 2 + "*Number of spaces to be used for indentation per section level." + :group 'reftex-referencing-labels + :type 'integer) + +(defcustom reftex-guess-label-type t + "*Non-nil means, `reftex-reference' will try to guess the label type. +To do that, RefTeX will look at the word before the cursor and compare it with +the words given in `reftex-label-alist'. When it finds a match, RefTeX will +immediately offer the correct label menu - otherwise it will prompt you for +a label type. If you set this variable to nil, RefTeX will always prompt." + :group 'reftex-referencing-labels + :type 'boolean) + +(defcustom reftex-format-ref-function nil + "Function which produces the string to insert as a reference. +Normally should be nil, because the format to insert a reference can +already be specified in `reftex-label-alist'. +This hook also is used by the special commands to insert `\vref' and `\fref' +references, so even if you set this, your setting will be ignored by +the special commands. +The function will be called with two arguments, the LABEL and the DEFAULT +FORMAT, which normally is `~\ref{%s}'. The function should return the +string to insert into the buffer." + :group 'reftex-referencing-labels + :type 'function) + +(defcustom reftex-select-label-mode-hook nil + "Mode hook for reftex-select-label-mode." + :group 'reftex-referencing-labels + :type 'hook) + +;; BibteX citation configuration ---------------------------------------- + +(defgroup reftex-citation-support nil + "Support for referencing bibliographic data with BibTeX." + :group 'reftex) + +(defvar reftex-bibfile-ignore-list nil) ; compatibility +(defcustom reftex-bibfile-ignore-regexps nil + "*List of regular expressions to exclude files in \\bibliography{..}. +File names matched by these regexps will not be parsed by RefTeX. +Intended for files which contain only `@string' macro definitions and the +like, which are ignored by RefTeX anyway." + :group 'reftex-citation-support + :set 'reftex-set-dirty + :type '(repeat (regexp))) + +(defcustom reftex-default-bibliography nil + "*List of BibTeX database files which should be used if none are specified. +When `reftex-citation' is called from a document which has neither a +`\bibliography{..}' statement nor a `thebibliography' environment, +RefTeX will scan these files instead. Intended for using `reftex-citation' +in non-LaTeX files. The files will be searched along the BIBINPUTS or TEXBIB +path." + :group 'reftex-citation-support + :type '(repeat (file))) + +(defcustom reftex-sort-bibtex-matches 'reverse-year + "*Sorting of the entries found in BibTeX databases by reftex-citation. +Possible values: +nil Do not sort entries. +'author Sort entries by author name. +'year Sort entries by increasing year. +'reverse-year Sort entries by decreasing year." + :group 'reftex-citation-support + :type '(choice (const :tag "not" nil) + (const :tag "by author" author) + (const :tag "by year" year) + (const :tag "by year, reversed" reverse-year))) + +(defcustom reftex-cite-format 'default + "*The format of citations to be inserted into the buffer. +It can be a string or an alist or a symbol. In the simplest case this +is just the string \"\\cite{%l}\", which is also the default. See the +definition of `reftex-cite-format-builtin' for more complex examples. + +If `reftex-cite-format' is a string, it will be used as the format. +In the format, the following percent escapes will be expanded. + +%l The BibTeX label of the citation. +%a List of author names, see also `reftex-cite-punctuation. +%2a Like %a, but abbreviate more than 2 authors like Jones et al. +%A First author name only. +%e Works like %a, but on list of editor names. (%2e and %E work a well) + +It is also possible to access all other BibTeX database fields: +%b booktitle %c chapter %d edition %h howpublished +%i institution %j journal %k key %m month +%n number %o organization %p pages %P first page +%r address %s school %u publisher %t title +%v volume %y year +%B booktitle, abbreviated %T title, abbreviated + +Usually, only %l is needed. The other stuff is mainly for the echo area +display, and for (setq reftex-comment-citations t). + +%< as a special operator kills punctuation and space around it after the +string has been formatted. + +Beware that all this only works with BibTeX database files. When +citations are made from the \\bibitems in an explicit thebibliography +environment, only %l is available. + +If `reftex-cite-format' is an alist of characters and strings, the user +will be prompted for a character to select one of the possible format +strings. + In order to configure this variable, you can either set +`reftex-cite-format' directly yourself or set it to the SYMBOL of one of +the predefined styles. The predefined symbols are those which have an +association in the constant `reftex-cite-format-builtin'. +E.g.: (setq reftex-cite-format 'natbib)" + :group 'reftex-citation-support + :type + `(choice + :format "%{%t%}: \n%[Value Menu%] %v" + (radio :tag "Symbolic Builtins" + :indent 4 + :value default + ,@(mapcar + (lambda (x) + (list 'const :tag (concat (symbol-name (nth 0 x)) + ": " (nth 1 x)) + (nth 0 x))) + reftex-cite-format-builtin)) + (string :tag "format string" "\\cite{%l}") + (repeat :tag "key-ed format strings" + :value ((?\r . "\\cite{%l}") + (?t . "\\cite{%l}") (?p . "\\cite{%l}")) + (cons (character :tag "Key character" ?\r) + (string :tag "Format string" ""))))) + +(defcustom reftex-comment-citations nil + "*Non-nil means add a comment for each citation describing the full entry. +The comment is formatted according to `reftex-cite-comment-format'." + :group 'reftex-citation-support + :type 'boolean) + +(defcustom reftex-cite-comment-format + "%% %2a %y, %j %v, %P, %b, %e, %u, %s %<\n" + "Citation format used for commented citations. Must NOT contain %l. +See the variable `reftex-cite-format' for possible percent escapes." + :group 'reftex-citation-support + :type 'string) + +(defcustom reftex-cite-view-format "%2a %y, %T, %B, %j %v:%P, %s %<" + "Citation format used to display citation info in the message area. +Must NOT contain %l. See the variable `reftex-cite-format' for +possible percent escapes." + :group 'reftex-citation-support + :group 'reftex-viewing-cross-references + :type 'string) + +(defcustom reftex-cite-punctuation '(", " " \\& " " {\\it et al.}") + "Punctuation for formatting of name lists in citations. +This is a list of 3 strings. +1. normal names separator, like \", \" in Jones, Brown and Miller +2. final names separator, like \" and \" in Jones, Brown and Miller +3. The \"et al\" string, like \" {\\it et al.}\" in Jones {\\it et al.}" + :group 'reftex-citation-support + :type '(list + (string :tag "Separator for names ") + (string :tag "Separator for last name in list") + (string :tag "string used as et al. "))) + +(defcustom reftex-format-cite-function nil + "Function which produces the string to insert as a citation. +Normally should be nil, because the format to insert a reference can +already be specified in `reftex-cite-format'. +The function will be called with two arguments, the CITATION KEY and the +DEFAULT FORMAT, which is taken from `reftex-cite-format'. The function +should return the string to insert into the buffer." + :group 'reftex-citation-support + :type 'function) + +(defcustom reftex-select-bib-mode-hook nil + "Mode hook for reftex-select-bib-mode." + :group 'reftex-citation-support + :type 'hook) + +;; Index Support Configuration + +(defgroup reftex-index-support nil + "Support for viewing and editing the index." + :group 'reftex) + +(defcustom reftex-support-index t + "*Non-nil means, index entries are parsed as well. +Index support is resource intensive and the internal structure holding the +parsed information can become quite big. Therefore it can be turned off. +When this is nil and you execute a command which requires index support, +you will be asked for confirmation to turn it on and rescan the document." + :group 'reftex-index-support + :type 'boolean) + +(defcustom reftex-index-special-chars '("!" "|" "@" "\"" "\\") + "Special characters in index entries. The value is a list of five strings. +These correspond to the makeindex keywords LEVEL ENCAP ACTUAL QUOTE ESCAPE." + :group 'reftex-index-support + :type '(list + (string :tag "LEVEL separator") + (string :tag "ENCAP char ") + (string :tag "ACTUAL char ") + (string :tag "QUOTE char ") + (string :tag "ESCAPE char "))) + +(defcustom reftex-index-macros nil + "Macros which define index entries. The structure is + +(MACRO INDEX-TAG KEY PREFIX EXCLUDE) + +MACRO is the macro. Arguments should be denoted by empty braces like +\\index[]{*}. Use square brackets to denote optional arguments. The star +marks where the index key is. + +INDEX-TAG is a short name of the index. \"idx\" and \"glo\" are +reserved for the default index and the glossary. Other indices can be +defined as well. If this is an integer, the Nth argument of the macro +holds the index tag. + +KEY is a character which is used to identify the macro for input with +\\[reftex-index]. ?i, ?I, and ?g are reserved for default index and glossary. + +PREFIX can be a prefix which is added to the KEY part of the index entry. +If you have a macro \\newcommand{\\molec}[1]{#1\\index{Molecules!#1}}, this +prefix should be \"Molecules!\". See the manual for details. + +EXCLUDE can be a function. If this function exists and returns a non-nil +value, the index entry at point is ignored. This was implemented to support +the (deprecated) `^' and `_' shortcuts in the LaTeX2e `index' package. + +The final entry may also be a symbol if this entry has a association +in the variable `reftex-index-macros-builtin' to specify the main +indexing package you are using. Legal values are currently +default The LaTeX default - unnecessary to specify this one +multind The multind.sty package +index The index.sty package +index-shortcut The index.sty packages with the ^ and _ shortcuts. + Should not be used - only for old documents. +Note that AUCTeX sets these things internally for RefTeX as well, so +with a sufficiently new version of AUCTeX, you should not set the +package here." + :group 'reftex-index-support + :set 'reftex-set-dirty + :type `(list + (repeat + :inline t + (list :value ("" "idx" ?a "" nil) + (string :tag "Macro with args") + (choice :tag "Index Tag " + (string) + (integer :tag "Macro arg Nr" :value 1)) + (character :tag "Access Key ") + (string :tag "Key Prefix ") + (symbol :tag "Exclusion hook "))) + (option + :tag "Package:" + (choice :tag "Package" + :value index + ,@(mapcar + (lambda (x) + (list 'const :tag (concat (symbol-name (nth 0 x)) + ": " (nth 1 x)) + (nth 0 x))) + reftex-index-macros-builtin))))) + +(defcustom reftex-index-default-macro '(?i "idx" t) + "The default index macro for \\[reftex-index-selection-or-word]. +This is a list with (MACRO-KEY DEFAULT-TAG REPEAT-WORD). + +MACRO-KEY: Character identifying an index macro - see `reftex-index-macros'. +DEFAULT-TAG: This is the tag to be used if the macro requires a TAG argument. + When this is nil and a TAG is needed, RefTeX will ask for it. + When this is the empty string and the TAG argument of the index + macro is optional, the TAG argument will be omitted. +REPEAT-WORD: Non-nil means, the index macro does not typeset the entry in + the text, so that the text has to be repeated outside the index + macro." + :group 'reftex-index-support + :type '(list + (character :tag "Character identifying default macro") + (choice :tag "Default index tag " + (const nil) + (string)) + (boolean :tag "Word needs to be repeated "))) + +(defcustom reftex-index-default-tag "idx" + "Default index tag. +When working with multiple indexes, RefTeX queries for an index tag when +creating index entries or displaying a specific index. This variable controls +the default offered for these queries. The default can be selected with RET +during selection or completion. Legal values of this variable are: + +nil Do not provide a default index +\"tag\" The default index tag given as a string, e.g. \"idx\". +last The last used index tag will be offered as default." + :group 'reftex-index-support + :type '(choice + (const :tag "no default" nil) + (const :tag "last used " 'last) + (string :tag "index tag " "idx"))) + +(defcustom reftex-index-math-format "$%s$" + "Format of index entries when copied from inside math mode. +When `reftex-index-selection-or-word' is executed inside TeX math mode, +the index key copied from the buffer is processed with this format string +through the `format' function. This can be used to add the math delimiters +(e.g. `$') to the string. +Requires the `texmathp.el' library which is part of AUCTeX." + :group 'reftex-index-support + :type 'string) + +(defcustom reftex-index-section-letters "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "The letters which denote sections in the index. +Usually these are all capital letters. Don't use any downcase letters. +Order is not significant, the index will be sorted by whatever the sort +function thinks is correct. +In addition to these letters, RefTeX will create a group `!' which +contains all entries sorted below the lowest specified letter. +In the index buffer, pressing any of these capital letters or `!' will jump +to that section." + :group 'reftex-index-support + :type '(string :tag "Capital letters")) + +(defcustom reftex-index-include-context nil + "*Non-nil means, display the index definition context in the index buffer. +This flag may also be toggled from the index buffer with the `c' key." + :group 'reftex-index-support + :type 'boolean) + +(defcustom reftex-index-follow-mode nil + "*Non-nil means, point in *Index* buffer will cause other window to follow. +The other window will show the corresponding part of the document. +This flag can be toggled from within the *Index* buffer with the `f' key." + :group 'reftex-table-of-contents-browser + :type 'boolean) + +;; Viewing Cross References + +(defgroup reftex-viewing-cross-references nil + "Displaying cross references and citations." + :group 'reftex) + +(defcustom reftex-view-crossref-extra nil + "Macros which can be used for the display of cross references. +This is used when `reftex-view-crossref' is called with point in an +argument of a macro. Note that crossref viewing for citations, +references (both ways) and index entries is hard-coded. This variable +is only to configure additional structures for which crossreference +viewing can be useful. Each entry has the structure + +(MACRO-RE SEARCH-RE HIGHLIGHT). + +MACRO-RE is matched against the macro. SEARCH-RE is the regexp used +to search for cross references. `%s' in this regexp is replaced with +with the macro argument at point. HIGHLIGHT is an integer indicating +which subgroup of the match should be highlighted." + :group 'reftex-viewing-cross-references + :type '(repeat (group (regexp :tag "Macro Regexp ") + (string :tag "Search Regexp ") + (integer :tag "Highlight Group")))) + +(defcustom reftex-auto-view-crossref t + "*Non-nil means, initially turn automatic viewing of crossref info on. +Automatic viewing of crossref info normally uses the echo area. +Whenever point is on the argument of a \\ref or \\cite macro, and no +other message is being displayed, the echo area will display +information about that cross reference. You can also set the variable +to the symbol `window'. In this case a small temporary window is +used for the display. +This feature can be turned on and of from the menu +(Ref->Options)." + :group 'reftex-viewing-cross-references + :type '(choice (const :tag "off" nil) + (const :tag "in Echo Area" t) + (const :tag "in Other Window" window))) + +(defcustom reftex-idle-time 1.2 + "*Time (secs) Emacs has to be idle before automatic crossref display is done." + :group 'reftex-viewing-cross-references + :type 'number) + +(defcustom reftex-revisit-to-echo nil + "*Non-nil means, automatic citation display will revisit files if necessary. +When nil, citation display in echo area will only be active for cached +entries and for BibTeX database files with live associated buffers." + :group 'reftex-viewing-cross-references + :type 'boolean) + +(defcustom reftex-cache-cite-echo t + "*Non-nil means, the information displayed in the echo area for cite macros +is cached and even saved along with the parsing information. The cache +survives document scans. In order to clear it, use M-x reftex-reset-mode." + :group 'reftex-viewing-cross-references + :type 'boolean) + +(defcustom reftex-display-copied-context-hook nil + "Normal Hook which is run before context is displayed anywhere. Designed +for X-Symbol, but may have other uses as well." + :group 'reftex-viewing-cross-references + :group 'reftex-referencing-labels + :type 'hook) + +;; Finding Files -------------------------------------------------------- + +(defgroup reftex-finding-files nil + "Finding files on search paths." + :group 'reftex) + +(defcustom reftex-texpath-environment-variables '("TEXINPUTS") + "*List of specifications how to retrieve the search path for TeX files. +Several entries are possible. +- If an element is the name of an environment variable, its content is used. +- If an element starts with an exclamation mark, it is used as a command + to retrieve the path. A typical command with the kpathsearch library would + be `!kpsewhich -show-path=.tex'. +- Otherwise the element itself is interpreted as a path. +Multiple directories can be separated by the system dependent `path-separator'. +Directories ending in `//' or `!!' will be expanded recursively. +See also `reftex-use-external-file-finders'." + :group 'reftex-finding-files + :set 'reftex-set-dirty + :type '(repeat (string :tag "Specification"))) + +(defcustom reftex-bibpath-environment-variables '("BIBINPUTS" "TEXBIB") + "*List of specifications how to retrieve search path for .bib database files. +Several entries are possible. +- If an element is the name of an environment variable, its content is used. +- If an element starts with an exclamation mark, it is used as a command + to retrieve the path. A typical command with the kpathsearch library would + be `!kpsewhich -show-path=.bib'. +- Otherwise the element itself is interpreted as a path. +Multiple directories can be separated by the system dependent `path-separator'. +Directories ending in `//' or `!!' will be expanded recursively. +See also `reftex-use-external-file-finders'." + :group 'reftex-citation-support + :group 'reftex-finding-files + :set 'reftex-set-dirty + :type '(repeat (string :tag "Specification"))) + +(defcustom reftex-file-extensions '(("tex" . (".tex" ".ltx")) + ("bib" . (".bib"))) + "*Association list with file extensions for different file types. +This is a list of items, each item is like: (TYPE . (DEF-EXT OTHER-EXT ...)) + +TYPE: File type like \"bib\" or \"tex\". +DEF-EXT: The default extension for that file type, like \".tex\" or \".bib\". +OTHER-EXT: Any number of other legal extensions for this file type. + +When a files is searched and it does not have any of the legal extensions, +we try the default extension first, and then the naked file name." + :group 'reftex-finding-files + :type '(repeat (cons (string :tag "File type") + (repeat (string :tag "Extension"))))) + +(defcustom reftex-search-unrecursed-path-first t + "*Non-nil means, search all specified directories before trying recursion. +Thus, in a path \".//:/tex/\", search first \"./\", then \"/tex/\" and then +all subdirectories of \"./\". If this option is nil, the subdirectories of +\"./\" are searched before \"/tex/\". This is mainly for speed - most of the +time the recursive path is for the system files and not for the user files. +Set this to nil if the default makes RefTeX finding files with equal names +in wrong sequence." + :group 'reftex-finding-files + :type 'boolean) + +(defcustom reftex-use-external-file-finders nil + "*Non-nil means, use external programs to find files. +Normally, RefTeX searches the paths given in the environment variables +TEXINPUTS and BIBINPUTS to find TeX files and BibTeX database files. +With this option turned on, it calls an external program specified in the +option `reftex-external-file-finders' instead. As a side effect, +the variables `reftex-texpath-environment-variables' and +`reftex-bibpath-environment-variables' will be ignored." + :group 'reftex-finding-files + :type 'boolean) + +(defcustom reftex-external-file-finders '(("tex" . "kpsewhich -format=.tex %f") + ("bib" . "kpsewhich -format=.bib %f")) + "*Association list with external programs to call for finding files. +Each entry is a cons cell (TYPE . PROGRAM). +TYPE is either \"tex\" or \"bib\". PROGRAM is the external program to use with +any arguments. %f will be replaced by the name of the file to be found. +Note that these commands will be executed directly, not via a shell. +Only relevant when `reftex-use-external-file-finders' is non-nil." + :group 'reftex-finding-files + :type '(repeat (cons (string :tag "File type") + (string :tag "Program ")))) + +;; Tuning the parser ---------------------------------------------------- + +(defgroup reftex-optimizations-for-large-documents nil + "Configuration of parser speed and memory usage." + :group 'reftex) + +(defcustom reftex-keep-temporary-buffers 1 + "*Non-nil means, keep buffers created for parsing and lookup. +RefTeX sometimes needs to visit files related to the current document. +We distinguish files visited for +PARSING: Parts of a multifile document loaded when (re)-parsing the document. +LOOKUP: BibTeX database files and TeX files loaded to find a reference, + to display label context, etc. +The created buffers can be kept for later use, or be thrown away immediately +after use, depending on the value of this variable: + +nil Throw away as much as possible. +t Keep everything. +1 Throw away buffers created for parsing, but keep the ones created + for lookup. + +If a buffer is to be kept, the file is visited normally (which is potentially +slow but will happen only once). +If a buffer is to be thrown away, the initialization of the buffer depends +upon the variable `reftex-initialize-temporary-buffers'." + :group 'reftex-optimizations-for-large-documents + :type '(choice + (const :tag "Throw away everything" nil) + (const :tag "Keep everything" t) + (const :tag "Keep lookup buffers only" 1))) + +(defcustom reftex-initialize-temporary-buffers nil + "*Non-nil means do initializations even when visiting file temporarily. +When nil, RefTeX may turn off find-file hooks and other stuff to briefly +visit a file. +When t, the full default initializations are done (find-file-hook etc.). +Instead of t or nil, this variable may also be a list of hook functions to +do a minimal initialization." + :group 'reftex-optimizations-for-large-documents + :type '(choice + (const :tag "Read files literally" nil) + (const :tag "Fully initialize buffers" t) + (repeat :tag "Hook functions" :value (nil) + (function-item)))) + +(defcustom reftex-no-include-regexps '("\\.pstex_t\\'") + "*List of regular expressions to exclude certain input files from parsing. +If the name of a file included via \\include or \\input is matched by any +of the regular expressions in this list, that file is not parsed by RefTeX." + :group 'reftex-optimizations-for-large-documents + :type '(repeat (regexp))) + +(defcustom reftex-enable-partial-scans nil + "*Non-nil means, re-parse only 1 file when asked to re-parse. +Re-parsing is normally requested with a `C-u' prefix to many RefTeX commands, +or with the `r' key in menus. When this option is t in a multifile document, +we will only parse the current buffer, or the file associated with the label +or section heading near point in a menu. Requesting re-parsing of an entire +multifile document then requires a `C-u C-u' prefix or the capital `R' key +in menus." + :group 'reftex-optimizations-for-large-documents + :type 'boolean) + +(defcustom reftex-allow-automatic-rescan t + "*Non-nil means, RefTeX may rescan the document when this seems necessary. +Currently this applies only to rescanning after label insertion, when +the new label cannot be inserted correctly into the internal label +list." + :group 'reftex-optimizations-for-large-documents + :type 'boolean) + +(defcustom reftex-save-parse-info nil + "*Non-nil means, save information gathered with parsing in a file. +The file MASTER.rel in the same directory as MASTER.tex is used to save the +information. When this variable is t, +- accessing the parsing information for the first time in an editing session + will read that file (if available) instead of parsing the document. +- exiting Emacs or killing a buffer in reftex-mode will cause a new version + of the file to be written." + :group 'reftex-optimizations-for-large-documents + :type 'boolean) + +(defcustom reftex-use-multiple-selection-buffers nil + "*Non-nil means use a separate selection buffer for each label type. +These buffers are kept from one selection to the next and need not to be +created for each use - so the menu generally comes up faster. The +selection buffers will be erased (and therefore updated) automatically +when new labels in its category are added. See the variable +`reftex-auto-update-selection-buffers'." + :group 'reftex-optimizations-for-large-documents + :group 'reftex-referencing-labels + :type 'boolean) + +(defcustom reftex-auto-update-selection-buffers t + "*Non-nil means, selection buffers will be updated automatically. +When a new label is defined with `reftex-label', all selection buffers +associated with that label category are emptied, in order to force an +update upon next use. When nil, the buffers are left alone and have to be +updated by hand, with the `g' key from the label selection process. +The value of this variable will only have any effect when +`reftex-use-multiple-selection-buffers' is non-nil." + :group 'reftex-optimizations-for-large-documents + :group 'reftex-referencing-labels + :type 'boolean) + +;; Fontification and Faces ---------------------------------------------- + +(defgroup reftex-fontification-configurations nil + "Options concerning the faces used in RefTeX." + :group 'reftex) + +(defcustom reftex-use-fonts t + "*Non-nil means, use fonts in *toc* and selection buffers. +Font-lock must be loaded as well to actually get fontified display. +When changing this option, a rescan may be necessary to activate the change." + :group 'reftex-fontification-configurations + :type 'boolean) + +(defcustom reftex-refontify-context 1 + "*Non-nil means, re-fontify the context in the label menu with font-lock. +This slightly slows down the creation of the label menu. It is only necessary +when you definitely want the context fontified. + +This option may have 3 different values: +nil Never refontify. +t Always refontify. +1 Refontify when absolutely necessary, e.g. when old versions of X-Symbol. +The option is ignored when `reftex-use-fonts' is nil." + :group 'reftex-fontification-configurations + :group 'reftex-referencing-labels + :type '(choice + (const :tag "Never" nil) + (const :tag "Always" t) + (const :tag "When necessary" 1))) + +(defcustom reftex-highlight-selection 'cursor + "*Non-nil mean, highlight selected text in selection and *toc* buffers. +Normally, the text near the cursor is the selected text, and it is +highlighted. This is the entry most keys in the selction and *toc* +buffers act on. However, if you mainly use the mouse to select an +item, you may find it nice to have mouse-triggered highlighting +instead or as well. The variable may have one of these values: + + nil No highlighting. + cursor Highlighting is cursor driven. + mouse Highlighting is mouse driven. + both Both cursor and mouse trigger highlighting. + +Changing this variable requires to rebuild the selection and *toc* buffers +to become effective (keys `g' or `r')." + :group 'reftex-fontification-configurations + :type '(choice + (const :tag "Never" nil) + (const :tag "Cursor driven" cursor) + (const :tag "Mouse driven" mouse) + (const :tag "Mouse and Cursor driven." both))) + +(defcustom reftex-cursor-selected-face 'highlight + "Face name to highlight cursor selected item in toc and selection buffers. +See also the variable `reftex-highlight-selection'." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-mouse-selected-face 'secondary-selection + "Face name to highlight mouse selected item in toc and selection buffers. +See also the variable `reftex-highlight-selection'." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-file-boundary-face 'font-lock-comment-face + "Face name for file boundaries in selection buffer." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-label-face 'font-lock-constant-face + "Face name for labels in selection buffer." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-section-heading-face 'font-lock-function-name-face + "Face name for section headings in toc and selection buffers." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-toc-header-face 'font-lock-comment-face + "Face name for the header of a toc buffer." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-bib-author-face 'font-lock-keyword-face + "Face name for author names in bib selection buffer." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-bib-year-face 'font-lock-comment-face + "Face name for year in bib selection buffer." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-bib-title-face 'font-lock-function-name-face + "Face name for article title in bib selection buffer." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-bib-extra-face 'font-lock-comment-face + "Face name for bibliographic information in bib selection buffer." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-select-mark-face 'bold + "Face name for marked entries in the selection buffers." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-index-header-face 'font-lock-comment-face + "Face name for the header of an index buffer." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-index-section-face 'font-lock-function-name-face + "Face name for the start of a new letter section in the index." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-index-tag-face 'font-lock-keyword-face + "Face name for index names (for multiple indices)." + :group 'reftex-fontification-configurations + :type 'symbol) +(defcustom reftex-index-face 'font-lock-constant-face + "Face name for index entries." + :group 'reftex-fontification-configurations + :type 'symbol) + +(defcustom reftex-pre-refontification-functions nil + "X-Symbol specific hook. +Functions get two arguments, the buffer from where the command started and a +symbol indicating in what context the hook is called." + :group 'reftex-fontification-configurations + :type 'hook) + +;; Miscellaneous configurations ----------------------------------------- + +(defgroup reftex-miscellaneous-configurations nil + "Collection of further configurations." + :group 'reftex) + +(defcustom reftex-extra-bindings nil + "Non-nil means, make additional key bindings on startup. +These extra bindings are located in the users `C-c letter' map." + :group 'reftex-miscellaneous-configurations + :type 'boolean) + +(defcustom reftex-plug-into-AUCTeX nil + "*Plug-in flags for AUCTeX interface. +This variable is a list of 4 boolean flags. When a flag is non-nil, +RefTeX will + + - supply labels in new sections and environments (flag 1) + - supply arguments for macros like `\\label'. (flag 2) + - supply arguments for macros like `\\ref'. (flag 3) + - supply arguments for macros like `\\cite'. (flag 4) + - supply arguments for macros like `\\index'. (flag 5) + +You may also set the variable itself to t or nil in order to turn all +plug-ins on or off, respectively. +\\<LaTeX-mode-map>Supplying labels in new sections and environments applies when creating +sections with \\[LaTeX-section] and environments with \\[LaTeX-environment]. +Supplying macro arguments applies when you insert such a macro interactively +with \\[TeX-insert-macro]. +See the AUCTeX documentation for more information. +RefTeX uses `fset' to take over the function calls. Changing the variable +may require a restart of Emacs in order to become effective." + :group 'reftex-miscellaneous-configurations + :group 'LaTeX + :type '(choice + (const :tag "No plug-ins" nil) + (const :tag "All possible plug-ins" t) + (list + :tag "Individual choice" + :value (t t t t t) + (boolean :tag "supply label in new sections and environments") + (boolean :tag "supply argument for macros like `\\label' ") + (boolean :tag "supply argument for macros like `\\ref' ") + (boolean :tag "supply argument for macros like `\\cite' ") + (boolean :tag "supply argument for macros like `\\index' ") + ))) + +(defcustom reftex-allow-detached-macro-args nil + "*Non-nil means, allow arguments of macros to be detached by whitespace. +When this is t, `aaa' will be considered as argument of \\bb in the following +construct: \\bbb [xxx] {aaa}." + :group 'reftex-miscellaneous-configurations + :type 'boolean) + + +(defcustom reftex-load-hook nil + "Hook which is being run when loading reftex.el." + :group 'reftex-miscellaneous-configurations + :type 'hook) + +(defcustom reftex-mode-hook nil + "Hook which is being run when turning on RefTeX mode." + :group 'reftex-miscellaneous-configurations + :type 'hook) + +;;; reftex-vars.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/textmodes/reftex-vcr.el Mon Aug 16 07:42:41 1999 +0000 @@ -0,0 +1,452 @@ +;;; reftex-vcr.el - Viewing cross references and citations with RefTeX +;;; Version: 4.5 +;;; +;;; See main file reftex.el for licensing information + +(provide 'reftex-vcr) +(require 'reftex) +;;; + +(defun reftex-view-crossref (&optional arg auto-how) + "View cross reference of macro at point. Point must be on the KEY +argument. When at at `\ref' macro, show corresponding `\label' +definition, also in external documents (`xr'). When on a label, show +a locations where KEY is referenced. Subsequent calls find additional +locations. When on a `\cite', show the associated `\bibitem' macro or +the BibTeX database entry. When on a `\bibitem', show a `\cite' macro +which uses this KEY. When on an `\index', show other locations marked +by the same index entry. +To define additional cross referencing items, use the option +`reftex-view-crossref-extra'. See also `reftex-view-crossref-from-bibtex'. +With one or two C-u prefixes, enforce rescanning of the document. +With argument 2, select the window showing the cross reference. +AUTO-HOW is only for the automatic crossref display and is handed through +to the functions `reftex-view-cr-cite' and `reftex-view-cr-ref'." + + (interactive "P") + ;; See where we are. + (let* ((macro (car (reftex-what-macro-safe 1))) + (key (reftex-this-word "^{}%\n\r,")) + dw) + + (if (or (null macro) (reftex-in-comment)) + (error "Not on a crossref macro argument")) + + (setq reftex-call-back-to-this-buffer (current-buffer)) + + (cond + ((string-match "\\`\\\\cite\\|cite\\*?\\'" macro) + ;; A citation macro: search for bibitems or BibTeX entries + (setq dw (reftex-view-cr-cite arg key auto-how))) + ((string-match "\\`\\\\ref\\|ref\\(range\\)?\\*?\\'" macro) + ;; A reference macro: search for labels + (setq dw (reftex-view-cr-ref arg key auto-how))) + (auto-how nil) ;; No further action for automatic display (speed) + ((or (equal macro "\\label") + (member macro reftex-macros-with-labels)) + ;; A label macro: search for reference macros + (reftex-access-scan-info arg) + (setq dw (reftex-view-regexp-match + (format reftex-find-reference-format (regexp-quote key)) + 4 nil nil))) + ((equal macro "\\bibitem") + ;; A bibitem macro: search for citations + (reftex-access-scan-info arg) + (setq dw (reftex-view-regexp-match + (format reftex-find-citation-regexp-format (regexp-quote key)) + 3 nil nil))) + ((member macro reftex-macros-with-index) + (reftex-access-scan-info arg) + (setq dw (reftex-view-regexp-match + (format reftex-find-index-entry-regexp-format + (regexp-quote key)) + 3 nil nil))) + (t + (reftex-access-scan-info arg) + (catch 'exit + (let ((list reftex-view-crossref-extra) + entry mre action group) + (while (setq entry (pop list)) + (setq mre (car entry) + action (nth 1 entry) + group (nth 2 entry)) + (when (string-match mre macro) + (setq dw (reftex-view-regexp-match + (format action key) group nil nil)) + (throw 'exit t)))) + (error "Not on a crossref macro argument")))) + (if (and (eq arg 2) (windowp dw)) (select-window dw)))) + +(defun reftex-view-cr-cite (arg key how) + ;; View crossreference of a ref cite. HOW can have the values + ;; nil: Show in another window. + ;; echo: Show one-line info in echo area. + ;; tmp-window: Show in small window and arrange for window to disappear. + + ;; Ensure access to scanning info + (reftex-access-scan-info (or arg current-prefix-arg)) + + (if (eq how 'tmp-window) + ;; Remember the window configuration + (put 'reftex-auto-view-crossref 'last-window-conf + (current-window-configuration))) + + (let (files size item (pos (point)) (win (selected-window)) pop-win) + ;; Find the citation mode and the file list + (cond + ((assq 'bib (symbol-value reftex-docstruct-symbol)) + (setq item nil + files (reftex-get-bibfile-list))) + ((assq 'thebib (symbol-value reftex-docstruct-symbol)) + (setq item t + files (list (cdr (assq 'thebib + (symbol-value reftex-docstruct-symbol)))))) + (reftex-default-bibliography + (setq item nil + files (reftex-default-bibliography))) + (how) ;; don't throw for special display + (t (error "Cannot display crossref"))) + + (if (eq how 'echo) + ;; Display in Echo area + (reftex-echo-cite key files item) + ;; Display in a window + (if (not (eq how 'tmp-window)) + ;; Normal display + (reftex-pop-to-bibtex-entry key files nil t item) + ;; A temporary window + (condition-case nil + (reftex-pop-to-bibtex-entry key files nil t item) + (error (goto-char pos) + (message "cite: no such citation key %s" key) + (error ""))) + ;; Resize the window + (setq size (max 1 (count-lines (point) + (reftex-end-of-bib-entry item)))) + (let ((window-min-height 2)) + (shrink-window (1- (- (window-height) size))) + (recenter 0)) + ;; Arrange restoration + (add-hook 'pre-command-hook 'reftex-restore-window-conf)) + + ;; Normal display in other window + (add-hook 'pre-command-hook 'reftex-highlight-shall-die) + (setq pop-win (selected-window)) + (select-window win) + (goto-char pos) + (when (equal arg 2) + (select-window pop-win))))) + +(defun reftex-view-cr-ref (arg label how) + ;; View crossreference of a ref macro. HOW can have the values + ;; nil: Show in another window. + ;; echo: Show one-line info in echo area. + ;; tmp-window: Show in small window and arrange for window to disappear. + + ;; Ensure access to scanning info + (reftex-access-scan-info (or arg current-prefix-arg)) + + (if (eq how 'tmp-window) + ;; Remember the window configuration + (put 'reftex-auto-view-crossref 'last-window-conf + (current-window-configuration))) + + (let* ((xr-data (assoc 'xr (symbol-value reftex-docstruct-symbol))) + (xr-re (nth 2 xr-data)) + (entry (assoc label (symbol-value reftex-docstruct-symbol))) + (win (selected-window)) pop-win (pos (point))) + + (if (and (not entry) (stringp label) xr-re (string-match xr-re label)) + ;; Label is defined in external document + (save-excursion + (save-match-data + (set-buffer + (or (reftex-get-file-buffer-force + (cdr (assoc (match-string 1 label) (nth 1 + xr-data)))) + (error "Problem with external label %s" label)))) + (setq label (substring label (match-end 1))) + (reftex-access-scan-info) + (setq entry + (assoc label (symbol-value reftex-docstruct-symbol))))) + (if (eq how 'echo) + ;; Display in echo area + (reftex-echo-ref label entry (symbol-value reftex-docstruct-symbol)) + (let ((window-conf (current-window-configuration))) + (condition-case nil + (reftex-show-label-location entry t nil t t) + (error (set-window-configuration window-conf) + (message "ref: Label %s not found" label) + (error "ref: Label %s not found" label)))) ;; 2nd is line OK + (add-hook 'pre-command-hook 'reftex-highlight-shall-die) + + (when (eq how 'tmp-window) + ;; Resize window and arrange restauration + (shrink-window (1- (- (window-height) 9))) + (recenter '(4)) + (add-hook 'pre-command-hook 'reftex-restore-window-conf)) + (setq pop-win (selected-window)) + (select-window win) + (goto-char pos) + (when (equal arg 2) + (select-window pop-win))))) + +(defun reftex-mouse-view-crossref (ev) + "View cross reference of \\ref or \\cite macro where you click. +If the macro at point is a \\ref, show the corresponding label definition. +If it is a \\cite, show the BibTeX database entry. +If there is no such macro at point, search forward to find one. +With argument, actually select the window showing the cross reference." + (interactive "e") + (mouse-set-point ev) + (reftex-view-crossref current-prefix-arg)) + +(defun reftex-view-crossref-when-idle () + ;; Display info about crossref at point in echo area or a window. + ;; This function was desigend to work with an idle timer. + ;; We try to get out of here as quickly as possible if the call is useless. + (and reftex-mode + ;; Make sure message area is free if we need it. + (or (eq reftex-auto-view-crossref 'window) (not (current-message))) + ;; Make sure we are not already displaying this one + (not (memq last-command '(reftex-view-crossref + reftex-mouse-view-crossref))) + ;; Quick precheck if this might be a relevant spot + ;; FIXME: Can fail with backslash in comment + (save-excursion + (search-backward "\\" nil t) + (looking-at "\\\\[a-zA-Z]*\\(cite\\|ref\\)")) + + (condition-case nil + (let ((current-prefix-arg nil)) + (cond + ((eq reftex-auto-view-crossref t) + (reftex-view-crossref -1 'echo)) + ((eq reftex-auto-view-crossref 'window) + (reftex-view-crossref -1 'tmp-window)) + (t nil))) + (error nil)))) + +(defun reftex-restore-window-conf () + (set-window-configuration (get 'reftex-auto-view-crossref 'last-window-conf)) + (put 'reftex-auto-view-crossref 'last-window-conf nil) + (remove-hook 'pre-command-hook 'reftex-restore-window-conf)) + +(defun reftex-echo-ref (label entry docstruct) + ;; Display crossref info in echo area. + (cond + ((null docstruct) + (message (substitute-command-keys (format reftex-no-info-message "ref")))) + ((null entry) + (message "ref: unknown label: %s" label)) + (t + (when (stringp (nth 2 entry)) + (message "ref(%s): %s" (nth 1 entry) (nth 2 entry))) + (let ((buf (get-buffer " *Echo Area*"))) + (when buf + (save-excursion + (set-buffer buf) + (run-hooks 'reftex-display-copied-context-hook))))))) + +(defun reftex-echo-cite (key files item) + ;; Display citation info in echo area. + (let* ((cache (assq 'bibview-cache (symbol-value reftex-docstruct-symbol))) + (cache-entry (assoc key (cdr cache))) + entry string buf (all-files files)) + + (if (and reftex-cache-cite-echo cache-entry) + ;; We can just use the cache + (setq string (cdr cache-entry)) + + ;; Need to look in the database + (unless reftex-revisit-to-echo + (setq files (reftex-visited-files files))) + + (setq entry + (condition-case nil + (save-excursion + (reftex-pop-to-bibtex-entry key files nil nil item t)) + (error + (if (and files (= (length all-files) (length files))) + (message "cite: no such database entry: %s" key) + (message (substitute-command-keys + (format reftex-no-info-message "cite")))) + nil))) + (when entry + (if item + (setq string (reftex-nicify-text entry)) + (setq string (reftex-make-cite-echo-string + (reftex-parse-bibtex-entry entry) + reftex-docstruct-symbol))))) + (unless (or (null string) (equal string "")) + (message "cite: %s" string)) + (when (setq buf (get-buffer " *Echo Area*")) + (save-excursion + (set-buffer buf) + (run-hooks 'reftex-display-copied-context-hook))))) + +(defvar reftex-use-itimer-in-xemacs nil + "*Non-nil means use the idle timers in XEmacs for crossref display. +Currently, idle timer restart is broken and we use the post-command-hook.") + +(defun reftex-toggle-auto-view-crossref () + "Toggle the automatic display of crossref information in the echo area. +When active, leaving point idle in the argument of a \\ref or \\cite macro +will display info in the echo area." + (interactive) + (if reftex-auto-view-crossref-timer + (progn + (if (featurep 'xemacs) + (if reftex-use-itimer-in-xemacs + (delete-itimer reftex-auto-view-crossref-timer) + (remove-hook 'post-command-hook 'reftex-start-itimer-once)) + (cancel-timer reftex-auto-view-crossref-timer)) + (setq reftex-auto-view-crossref-timer nil) + (message "Automatic display of crossref information was turned off")) + (setq reftex-auto-view-crossref-timer + (if (featurep 'xemacs) + (if reftex-use-itimer-in-xemacs + (start-itimer "RefTeX Idle Timer" + 'reftex-view-crossref-when-idle + reftex-idle-time reftex-idle-time t) + (add-hook 'post-command-hook 'reftex-start-itimer-once) + t) + (run-with-idle-timer + reftex-idle-time t 'reftex-view-crossref-when-idle))) + (unless reftex-auto-view-crossref + (setq reftex-auto-view-crossref t)) + (message "Automatic display of crossref information was turned on"))) + +(defun reftex-start-itimer-once () + (and reftex-mode + (not (itimer-live-p reftex-auto-view-crossref-timer)) + (setq reftex-auto-view-crossref-timer + (start-itimer "RefTeX Idle Timer" + 'reftex-view-crossref-when-idle + reftex-idle-time nil t)))) + +(defun reftex-view-crossref-from-bibtex (&optional arg) + "View location in a LaTeX document which cites the BibTeX entry at point. +Since BibTeX files can be used by many LaTeX documents, this function +prompts upon first use for a buffer in RefTeX mode. To reset this +link to a document, call the function with with a prefix arg. +Calling this function several times find successive citation locations." + (interactive "P") + (when arg + ;; Break connection to reference buffer + (remprop 'reftex-bibtex-view-cite-locations :ref-buffer)) + (let ((ref-buffer (get 'reftex-bibtex-view-cite-locations :ref-buffer))) + ;; Establish connection to reference buffer + (unless ref-buffer + (setq ref-buffer + (save-excursion + (completing-read + "Reference buffer: " + (delq nil + (mapcar + (lambda (b) + (set-buffer b) + (if reftex-mode (list (buffer-name b)) nil)) + (buffer-list))) + nil t))) + (put 'reftex-bibtex-view-cite-locations :ref-buffer ref-buffer)) + ;; Search for citations + (bibtex-beginning-of-entry) + (if (looking-at + "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*\\([^, \t\r\n}]+\\)") + (progn + (goto-char (match-beginning 1)) + (reftex-view-regexp-match + (format reftex-find-citation-regexp-format + (regexp-quote (match-string 1))) + 3 arg ref-buffer)) + (error "Cannot find citation key in BibTeX entry")))) + +(defun reftex-view-regexp-match (re &optional highlight-group new ref-buffer) + ;; Search for RE in current document or in the document of REF-BUFFER. + ;; Continue the search, if the same re was searched last. + ;; Highlight the group HIGHLIGHT-GROUP of the match. + ;; When NEW is non-nil, start a new search regardless. + ;; Match point is displayed in another window. + ;; Upon success, returns the window which displays the match. + + ;;; Decide if new search or continued search + (let* ((oldprop (get 'reftex-view-regexp-match :props)) + (newprop (list (current-buffer) re)) + (cont (and (not new) (equal oldprop newprop))) + (cnt (if cont (get 'reftex-view-regexp-match :cnt) 0)) + (current-window (selected-window)) + (window-conf (current-window-configuration)) + match pop-window) + (switch-to-buffer-other-window (or ref-buffer (current-buffer))) + ;; Search + (condition-case nil + (if cont + (setq match (reftex-global-search-continue)) + (reftex-access-scan-info) + (setq match (reftex-global-search re (reftex-all-document-files)))) + (error nil)) + ;; Evaluate the match. + (if match + (progn + (put 'reftex-view-regexp-match :props newprop) + (put 'reftex-view-regexp-match :cnt (incf cnt)) + (reftex-highlight 0 (match-beginning highlight-group) + (match-end highlight-group)) + (add-hook 'pre-command-hook 'reftex-highlight-shall-die) + (setq pop-window (selected-window))) + (remprop 'reftex-view-regexp-match :props) + (or cont (set-window-configuration window-conf))) + (select-window current-window) + (if match + (progn + (message "Match Nr. %s" cnt) + pop-window) + (if cont + (error "No further matches (total number of matches: %d)" cnt) + (error "No matches"))))) + +(defvar reftex-global-search-marker (make-marker)) +(defun reftex-global-search (regexp file-list) + ;; Start a search for REGEXP in all files of FILE-LIST + (put 'reftex-global-search :file-list file-list) + (put 'reftex-global-search :regexp regexp) + (move-marker reftex-global-search-marker nil) + (reftex-global-search-continue)) + +(defun reftex-global-search-continue () + ;; Continue a global search started with `reftex-global-search' + (unless (get 'reftex-global-search :file-list) + (error "No global search to continue")) + (let* ((file-list (get 'reftex-global-search :file-list)) + (regexp (get 'reftex-global-search :regexp)) + (buf (or (marker-buffer reftex-global-search-marker) + (reftex-get-file-buffer-force (car file-list)))) + (pos (or (marker-position reftex-global-search-marker) 1)) + file) + ;; Take up starting position + (unless buf (error "No such buffer %s" buf)) + (switch-to-buffer buf) + (widen) + (goto-char pos) + ;; Search and switch file if necessary + (if (catch 'exit + (while t + (when (re-search-forward regexp nil t) + (move-marker reftex-global-search-marker (point)) + (throw 'exit t)) + ;; No match - goto next file + (pop file-list) + (or file-list (throw 'exit nil)) + (setq file (car file-list) + buf (reftex-get-file-buffer-force file)) + (unless buf (error "Cannot access file %s" file)) + (put 'reftex-global-search :file-list file-list) + (switch-to-buffer buf) + (widen) + (goto-char 1))) + t + (move-marker reftex-global-search-marker nil) + (error "All files processed")))) + +;;; reftex-vcr.el ends here