1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-13 09:30:24 -08:00
emacs/lisp/textmodes/reftex-ref.el
Paul Eggert 6afef3f6ca Go back to grave quoting in source-code docstrings etc.
This reverts almost all my recent changes to use curved quotes
in docstrings and/or strings used for error diagnostics.
There are a few exceptions, e.g., Bahá’í proper names.
* admin/unidata/unidata-gen.el (unidata-gen-table):
* lisp/abbrev.el (expand-region-abbrevs):
* lisp/align.el (align-region):
* lisp/allout.el (allout-mode, allout-solicit-alternate-bullet)
(outlineify-sticky):
* lisp/apropos.el (apropos-library):
* lisp/bookmark.el (bookmark-default-annotation-text):
* lisp/button.el (button-category-symbol, button-put)
(make-text-button):
* lisp/calc/calc-aent.el (math-read-if, math-read-factor):
* lisp/calc/calc-embed.el (calc-do-embedded):
* lisp/calc/calc-ext.el (calc-user-function-list):
* lisp/calc/calc-graph.el (calc-graph-show-dumb):
* lisp/calc/calc-help.el (calc-describe-key)
(calc-describe-thing, calc-full-help):
* lisp/calc/calc-lang.el (calc-c-language)
(math-parse-fortran-vector-end, math-parse-tex-sum)
(math-parse-eqn-matrix, math-parse-eqn-prime)
(calc-yacas-language, calc-maxima-language, calc-giac-language)
(math-read-giac-subscr, math-read-math-subscr)
(math-read-big-rec, math-read-big-balance):
* lisp/calc/calc-misc.el (calc-help, report-calc-bug):
* lisp/calc/calc-mode.el (calc-auto-why, calc-save-modes)
(calc-auto-recompute):
* lisp/calc/calc-prog.el (calc-fix-token-name)
(calc-read-parse-table-part, calc-user-define-invocation)
(math-do-arg-check):
* lisp/calc/calc-store.el (calc-edit-variable):
* lisp/calc/calc-units.el (math-build-units-table-buffer):
* lisp/calc/calc-vec.el (math-read-brackets):
* lisp/calc/calc-yank.el (calc-edit-mode):
* lisp/calc/calc.el (calc, calc-do, calc-user-invocation):
* lisp/calendar/appt.el (appt-display-message):
* lisp/calendar/diary-lib.el (diary-check-diary-file)
(diary-mail-entries, diary-from-outlook):
* lisp/calendar/icalendar.el (icalendar-export-region)
(icalendar--convert-float-to-ical)
(icalendar--convert-date-to-ical)
(icalendar--convert-ical-to-diary)
(icalendar--convert-recurring-to-diary)
(icalendar--add-diary-entry):
* lisp/calendar/time-date.el (format-seconds):
* lisp/calendar/timeclock.el (timeclock-mode-line-display)
(timeclock-make-hours-explicit, timeclock-log-data):
* lisp/calendar/todo-mode.el (todo-prefix, todo-delete-category)
(todo-item-mark, todo-check-format)
(todo-insert-item--next-param, todo-edit-item--next-key)
(todo-mode):
* lisp/cedet/ede/pmake.el (ede-proj-makefile-insert-dist-rules):
* lisp/cedet/mode-local.el (describe-mode-local-overload)
(mode-local-print-binding, mode-local-describe-bindings-2):
* lisp/cedet/semantic/complete.el (semantic-displayor-show-request):
* lisp/cedet/srecode/srt-mode.el (srecode-macro-help):
* lisp/cus-start.el (standard):
* lisp/cus-theme.el (describe-theme-1):
* lisp/custom.el (custom-add-dependencies, custom-check-theme)
(custom--sort-vars-1, load-theme):
* lisp/descr-text.el (describe-text-properties-1, describe-char):
* lisp/dired-x.el (dired-do-run-mail):
* lisp/dired.el (dired-log):
* lisp/emacs-lisp/advice.el (ad-read-advised-function)
(ad-read-advice-class, ad-read-advice-name, ad-enable-advice)
(ad-disable-advice, ad-remove-advice, ad-set-argument)
(ad-set-arguments, ad--defalias-fset, ad-activate)
(ad-deactivate):
* lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand)
(byte-compile-unfold-lambda, byte-optimize-form-code-walker)
(byte-optimize-while, byte-optimize-apply):
* lisp/emacs-lisp/byte-run.el (defun, defsubst):
* lisp/emacs-lisp/bytecomp.el (byte-compile-lapcode)
(byte-compile-log-file, byte-compile-format-warn)
(byte-compile-nogroup-warn, byte-compile-arglist-warn)
(byte-compile-cl-warn)
(byte-compile-warn-about-unresolved-functions)
(byte-compile-file, byte-compile--declare-var)
(byte-compile-file-form-defmumble, byte-compile-form)
(byte-compile-normal-call, byte-compile-check-variable)
(byte-compile-variable-ref, byte-compile-variable-set)
(byte-compile-subr-wrong-args, byte-compile-setq-default)
(byte-compile-negation-optimizer)
(byte-compile-condition-case--old)
(byte-compile-condition-case--new, byte-compile-save-excursion)
(byte-compile-defvar, byte-compile-autoload)
(byte-compile-lambda-form)
(byte-compile-make-variable-buffer-local, display-call-tree)
(batch-byte-compile):
* lisp/emacs-lisp/cconv.el (cconv-convert, cconv--analyze-use):
* lisp/emacs-lisp/chart.el (chart-space-usage):
* lisp/emacs-lisp/check-declare.el (check-declare-scan)
(check-declare-warn, check-declare-file)
(check-declare-directory):
* lisp/emacs-lisp/checkdoc.el (checkdoc-this-string-valid-engine)
(checkdoc-message-text-engine):
* lisp/emacs-lisp/cl-extra.el (cl-parse-integer)
(cl--describe-class):
* lisp/emacs-lisp/cl-generic.el (cl-defgeneric)
(cl--generic-describe, cl-generic-generalizers):
* lisp/emacs-lisp/cl-macs.el (cl--parse-loop-clause, cl-tagbody)
(cl-symbol-macrolet):
* lisp/emacs-lisp/cl.el (cl-unload-function, flet):
* lisp/emacs-lisp/copyright.el (copyright)
(copyright-update-directory):
* lisp/emacs-lisp/edebug.el (edebug-read-list):
* lisp/emacs-lisp/eieio-base.el (eieio-persistent-read):
* lisp/emacs-lisp/eieio-core.el (eieio--slot-override)
(eieio-oref):
* lisp/emacs-lisp/eieio-opt.el (eieio-help-constructor):
* lisp/emacs-lisp/eieio-speedbar.el:
(eieio-speedbar-child-make-tag-lines)
(eieio-speedbar-child-description):
* lisp/emacs-lisp/eieio.el (defclass, change-class):
* lisp/emacs-lisp/elint.el (elint-file, elint-get-top-forms)
(elint-init-form, elint-check-defalias-form)
(elint-check-let-form):
* lisp/emacs-lisp/ert.el (ert-get-test, ert-results-mode-menu)
(ert-results-pop-to-backtrace-for-test-at-point)
(ert-results-pop-to-messages-for-test-at-point)
(ert-results-pop-to-should-forms-for-test-at-point)
(ert-describe-test):
* lisp/emacs-lisp/find-func.el (find-function-search-for-symbol)
(find-function-library):
* lisp/emacs-lisp/generator.el (iter-yield):
* lisp/emacs-lisp/gv.el (gv-define-simple-setter):
* lisp/emacs-lisp/lisp-mnt.el (lm-verify):
* lisp/emacs-lisp/macroexp.el (macroexp--obsolete-warning):
* lisp/emacs-lisp/map-ynp.el (map-y-or-n-p):
* lisp/emacs-lisp/nadvice.el (advice--make-docstring)
(advice--make, define-advice):
* lisp/emacs-lisp/package-x.el (package-upload-file):
* lisp/emacs-lisp/package.el (package-version-join)
(package-disabled-p, package-activate-1, package-activate)
(package--download-one-archive)
(package--download-and-read-archives)
(package-compute-transaction, package-install-from-archive)
(package-install, package-install-selected-packages)
(package-delete, package-autoremove, describe-package-1)
(package-install-button-action, package-delete-button-action)
(package-menu-hide-package, package-menu--list-to-prompt)
(package-menu--perform-transaction)
(package-menu--find-and-notify-upgrades):
* lisp/emacs-lisp/pcase.el (pcase-exhaustive, pcase--u1):
* lisp/emacs-lisp/re-builder.el (reb-enter-subexp-mode):
* lisp/emacs-lisp/ring.el (ring-previous, ring-next):
* lisp/emacs-lisp/rx.el (rx-check, rx-anything)
(rx-check-any-string, rx-check-any, rx-check-not, rx-=)
(rx-repeat, rx-check-backref, rx-syntax, rx-check-category)
(rx-form):
* lisp/emacs-lisp/smie.el (smie-config-save):
* lisp/emacs-lisp/subr-x.el (internal--check-binding):
* lisp/emacs-lisp/tabulated-list.el (tabulated-list-put-tag):
* lisp/emacs-lisp/testcover.el (testcover-1value):
* lisp/emacs-lisp/timer.el (timer-event-handler):
* lisp/emulation/viper-cmd.el (viper-toggle-parse-sexp-ignore-comments)
(viper-toggle-search-style, viper-kill-buffer)
(viper-brac-function):
* lisp/emulation/viper-macs.el (viper-record-kbd-macro):
* lisp/env.el (setenv):
* lisp/erc/erc-button.el (erc-nick-popup):
* lisp/erc/erc.el (erc-cmd-LOAD, erc-handle-login, english):
* lisp/eshell/em-dirs.el (eshell/cd):
* lisp/eshell/em-glob.el (eshell-glob-regexp)
(eshell-glob-entries):
* lisp/eshell/em-pred.el (eshell-parse-modifiers):
* lisp/eshell/esh-opt.el (eshell-show-usage):
* lisp/facemenu.el (facemenu-add-new-face)
(facemenu-add-new-color):
* lisp/faces.el (read-face-name, read-face-font, describe-face)
(x-resolve-font-name):
* lisp/files-x.el (modify-file-local-variable):
* lisp/files.el (locate-user-emacs-file, find-alternate-file)
(set-auto-mode, hack-one-local-variable--obsolete)
(dir-locals-set-directory-class, write-file, basic-save-buffer)
(delete-directory, copy-directory, recover-session)
(recover-session-finish, insert-directory)
(file-modes-char-to-who, file-modes-symbolic-to-number)
(move-file-to-trash):
* lisp/filesets.el (filesets-add-buffer, filesets-remove-buffer):
* lisp/find-cmd.el (find-generic, find-to-string):
* lisp/finder.el (finder-commentary):
* lisp/font-lock.el (font-lock-fontify-buffer):
* lisp/format.el (format-write-file, format-find-file)
(format-insert-file):
* lisp/frame.el (get-device-terminal, select-frame-by-name):
* lisp/fringe.el (fringe--check-style):
* lisp/gnus/nnmairix.el (nnmairix-widget-create-query):
* lisp/help-fns.el (help-fns--key-bindings)
(help-fns--compiler-macro, help-fns--parent-mode)
(help-fns--obsolete, help-fns--interactive-only)
(describe-function-1, describe-variable):
* lisp/help.el (describe-mode)
(describe-minor-mode-from-indicator):
* lisp/image.el (image-type):
* lisp/international/ccl.el (ccl-dump):
* lisp/international/fontset.el (x-must-resolve-font-name):
* lisp/international/mule-cmds.el (prefer-coding-system)
(select-safe-coding-system-interactively)
(select-safe-coding-system, activate-input-method)
(toggle-input-method, describe-current-input-method)
(describe-language-environment):
* lisp/international/mule-conf.el (code-offset):
* lisp/international/mule-diag.el (describe-character-set)
(list-input-methods-1):
* lisp/mail/feedmail.el (feedmail-run-the-queue):
* lisp/mouse.el (minor-mode-menu-from-indicator):
* lisp/mpc.el (mpc-playlist-rename):
* lisp/msb.el (msb--choose-menu):
* lisp/net/ange-ftp.el (ange-ftp-shell-command):
* lisp/net/imap.el (imap-interactive-login):
* lisp/net/mairix.el (mairix-widget-create-query):
* lisp/net/newst-backend.el (newsticker--sentinel-work):
* lisp/net/newst-treeview.el (newsticker--treeview-load):
* lisp/net/rlogin.el (rlogin):
* lisp/obsolete/iswitchb.el (iswitchb-possible-new-buffer):
* lisp/obsolete/otodo-mode.el (todo-more-important-p):
* lisp/obsolete/pgg-gpg.el (pgg-gpg-process-region):
* lisp/obsolete/pgg-pgp.el (pgg-pgp-process-region):
* lisp/obsolete/pgg-pgp5.el (pgg-pgp5-process-region):
* lisp/org/ob-core.el (org-babel-goto-named-src-block)
(org-babel-goto-named-result):
* lisp/org/ob-fortran.el (org-babel-fortran-ensure-main-wrap):
* lisp/org/ob-ref.el (org-babel-ref-resolve):
* lisp/org/org-agenda.el (org-agenda-prepare):
* lisp/org/org-clock.el (org-clock-notify-once-if-expired)
(org-clock-resolve):
* lisp/org/org-ctags.el (org-ctags-ask-rebuild-tags-file-then-find-tag):
* lisp/org/org-feed.el (org-feed-parse-atom-entry):
* lisp/org/org-habit.el (org-habit-parse-todo):
* lisp/org/org-mouse.el (org-mouse-popup-global-menu)
(org-mouse-context-menu):
* lisp/org/org-table.el (org-table-edit-formulas):
* lisp/org/ox.el (org-export-async-start):
* lisp/proced.el (proced-log):
* lisp/progmodes/ada-mode.el (ada-get-indent-case)
(ada-check-matching-start, ada-goto-matching-start):
* lisp/progmodes/ada-prj.el (ada-prj-display-page):
* lisp/progmodes/ada-xref.el (ada-find-executable):
* lisp/progmodes/ebrowse.el (ebrowse-tags-apropos):
* lisp/progmodes/etags.el (etags-tags-apropos-additional):
* lisp/progmodes/flymake.el (flymake-parse-err-lines)
(flymake-start-syntax-check-process):
* lisp/progmodes/python.el (python-shell-get-process-or-error)
(python-define-auxiliary-skeleton):
* lisp/progmodes/sql.el (sql-comint):
* lisp/progmodes/verilog-mode.el (verilog-load-file-at-point):
* lisp/progmodes/vhdl-mode.el (vhdl-widget-directory-validate):
* lisp/recentf.el (recentf-open-files):
* lisp/replace.el (query-replace-read-from)
(occur-after-change-function, occur-1):
* lisp/scroll-bar.el (scroll-bar-columns):
* lisp/server.el (server-get-auth-key):
* lisp/simple.el (execute-extended-command)
(undo-outer-limit-truncate, list-processes--refresh)
(compose-mail, set-variable, choose-completion-string)
(define-alternatives):
* lisp/startup.el (site-run-file, tty-handle-args, command-line)
(command-line-1):
* lisp/subr.el (noreturn, define-error, add-to-list)
(read-char-choice, version-to-list):
* lisp/term/common-win.el (x-handle-xrm-switch)
(x-handle-name-switch, x-handle-args):
* lisp/term/x-win.el (x-handle-parent-id, x-handle-smid):
* lisp/textmodes/reftex-ref.el (reftex-label):
* lisp/textmodes/reftex-toc.el (reftex-toc-rename-label):
* lisp/textmodes/two-column.el (2C-split):
* lisp/tutorial.el (tutorial--describe-nonstandard-key)
(tutorial--find-changed-keys):
* lisp/type-break.el (type-break-noninteractive-query):
* lisp/wdired.el (wdired-do-renames, wdired-do-symlink-changes)
(wdired-do-perm-changes):
* lisp/whitespace.el (whitespace-report-region):
Prefer grave quoting in source-code strings used to generate help
and diagnostics.
* lisp/faces.el (face-documentation):
No need to convert quotes, since the result is a docstring.
* lisp/info.el (Info-virtual-index-find-node)
(Info-virtual-index, info-apropos):
Simplify by generating only curved quotes, since info files are
typically that ways nowadays anyway.
* lisp/international/mule-diag.el (list-input-methods):
Don’t assume text quoting style is curved.
* lisp/org/org-bibtex.el (org-bibtex-fields):
Revert my recent changes, going back to the old quoting style.
2015-09-07 16:09:27 -07:00

885 lines
36 KiB
EmacsLisp

;;; reftex-ref.el --- code to create labels and references with RefTeX
;; Copyright (C) 1997-2015 Free Software Foundation, Inc.
;; Author: Carsten Dominik <dominik@science.uva.nl>
;; Maintainer: auctex-devel@gnu.org
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(eval-when-compile (require 'cl))
(require 'reftex)
(require 'reftex-parse)
;;;###autoload
(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"))))
;;;###autoload
(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.")
(with-current-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.")))))))
;;;###autoload
(defun reftex-label-info (label &optional file bound derive env-or-mac)
;; Return info list on LABEL at point.
(let* ((prefix (if (string-match "^[a-zA-Z0-9]+:" label)
(match-string 0 label)))
(typekey (cdr (assoc prefix reftex-prefix-to-typekey-alist)))
(file (or file (buffer-file-name)))
(trust reftex-trust-label-prefix)
(in-comment (reftex-in-comment)))
(if (and typekey
(cond ((eq trust t) t)
((null trust) nil)
((stringp trust) (string-match trust typekey))
((listp trust) (member typekey trust))
(t nil)))
(list label typekey
(reftex-nicify-text (reftex-context-substring))
file in-comment)
(let* ((env-or-mac (or env-or-mac (reftex-label-location bound)))
(typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist)))
(parse (nth 2 (assoc env-or-mac reftex-env-or-mac-alist)))
(text (reftex-short-context env-or-mac parse reftex-location-start
derive)))
(list label typekey text file in-comment)))))
;;; Creating labels ---------------------------------------------------------
;;;###autoload
(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 "INVALID 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 valid label
(setq label (read-string
(if naked "Naked Label: " "Label: ")
default))
;; Let's make sure that this is a valid 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 invalid 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-message "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
(save-match-data
(cond
((equal letter "f")
(file-name-base))
((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 "m")
(file-name-base (reftex-TeX-master-file)))
((equal letter "M")
(file-name-nondirectory
(substring (file-name-directory (reftex-TeX-master-file))
0 -1)))
((equal letter "u")
(or (user-login-name) ""))
((equal letter "S")
(let* (macro level-exp level)
(save-excursion
(save-match-data
(when (re-search-backward reftex-section-regexp nil t)
(setq macro (reftex-match-string 2)
level-exp (cdr (assoc macro reftex-section-levels-all))
level (if (symbolp level-exp)
(abs (save-match-data
(funcall level-exp)))
(abs level-exp))))
(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.
z Jump to a specific section, e.g. '3 z' jumps to section 3.
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).")
;;;###autoload
(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 is '(4)
(reftex-access-scan-info current-prefix-arg)
(let ((reftex-refstyle (when (and (boundp 'reftex-refstyle) reftex-refstyle)
reftex-refstyle))
(reftex-format-ref-function reftex-format-ref-function)
(form "\\ref{%s}")
label labels sep sep1 style-alist)
(unless reftex-refstyle
(if reftex-ref-macro-prompt
(progn
;; Build a temporary list which handles more easily.
(dolist (elt reftex-ref-style-alist)
(when (member (car elt) (reftex-ref-style-list))
(mapc (lambda (x)
(add-to-list 'style-alist (cons (cadr x) (car x)) t))
(nth 2 elt))))
;; Prompt the user for the macro.
(let ((key (reftex-select-with-char
"" (concat "SELECT A REFERENCE FORMAT\n\n"
(mapconcat
(lambda (x)
(format "[%c] %s %s" (car x)
(if (> (car x) 31) " " "")
(cdr x)))
style-alist "\n")))))
(setq reftex-refstyle (cdr (assoc key style-alist)))
(unless reftex-refstyle
(error "No reference macro associated with key `%c'" key))))
;; Get the first macro from `reftex-ref-style-alist' which
;; matches the first entry in the list of active styles.
(setq reftex-refstyle
(or (caar (nth 2 (assoc (car (reftex-ref-style-list))
reftex-ref-style-alist)))
;; Use the first entry in r-r-s-a as a last resort.
(caar (nth 2 (car reftex-ref-style-alist)))))))
(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))))
;; 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 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?
(unless (string= reftex-refstyle "\\ref")
(setq reftex-format-ref-function 'reftex-format-special))
;; ok, insert the reference
(if sep1 (insert sep1))
(insert
(if reftex-format-ref-function
(funcall reftex-format-ref-function label form reftex-refstyle)
(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))
(if reftex-use-multiple-selection-buffers
(switch-to-buffer-other-window
(with-current-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<" 'reftex-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, or change depth
(reftex-erase-buffer)
(if current-prefix-arg
(setq reftex-toc-max-level (prefix-numeric-value
current-prefix-arg))
(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)))))
(mapc (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 SEPARATOR) 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)))))
;;;###autoload
(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)))
;;;###autoload
(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))))))
;; Generate functions for direct insertion of specific referencing
;; macros. The functions are named `reftex-<package>-<macro>',
;; e.g. `reftex-varioref-vref'.
(dolist (elt reftex-ref-style-alist)
(when (stringp (nth 1 elt))
(dolist (item (nth 2 elt))
(let ((macro (car item))
(package (nth 1 elt)))
(eval `(defun ,(intern (format "reftex-%s-%s" package
(substring macro 1 (length macro)))) ()
,(format "Insert a reference using the `%s' macro from the %s \
package.\n\nThis is a generated function."
macro package)
(interactive)
(let ((reftex-refstyle ,macro))
(reftex-reference))))))))
(defun reftex-format-special (label fmt refstyle)
"Apply selected reference style to format FMT and add LABEL.
Replace any occurrences of \"\\ref\" with REFSTYLE."
;; Replace instances of \ref in `fmt' with the special reference
;; style selected by the user.
(cond
((while (string-match "\\(\\\\ref\\)[ \t]*{" fmt)
(setq fmt (replace-match refstyle t t fmt 1))))
((string-match "\\(\\\\[[:alpha:]]+\\)[ \t]*{" fmt)
(setq fmt (replace-match refstyle t t fmt 1))))
(format fmt label))
;;;###autoload
(defun reftex-goto-label (&optional other-window)
"Prompt for a label (with completion) and jump to the location of this label.
Optional prefix argument OTHER-WINDOW goes to the label in another window."
(interactive "P")
(reftex-access-scan-info)
(let* ((wcfg (current-window-configuration))
(docstruct (symbol-value reftex-docstruct-symbol))
;; If point is inside a \ref{} or \pageref{}, use that as
;; default value.
(default (when (looking-back "\\\\\\(?:page\\)?ref{[-a-zA-Z0-9_*.:]*"
(line-beginning-position))
(reftex-this-word "-a-zA-Z0-9_*.:")))
(label (completing-read (if default
(format "Label (default %s): " default)
"Label: ")
docstruct
(lambda (x) (stringp (car x))) t nil nil
default))
(selection (assoc label docstruct))
(where (progn
(reftex-show-label-location selection t nil 'stay)
(point-marker))))
(unless other-window
(set-window-configuration wcfg)
(switch-to-buffer (marker-buffer where))
(goto-char where))
(reftex-unhighlight 0)))
(provide 'reftex-ref)
;;; reftex-ref.el ends here
;; Local Variables:
;; generated-autoload-file: "reftex.el"
;; End: