1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-26 07:33:47 +00:00
emacs/lisp/textmodes/reftex-ref.el
Ralf Angeli 4f595e1504 Merge from standalone RefTeX repository.
Here is the ChangeLog of the standalone version of RefTeX without
information about Makefiles and other auxiliary files.  The differences to
the Emacs repository are documented in the respective ChangeLog files.

2010-11-06  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-cite.el (reftex-create-bibtex-file): Make sure that
	entries with whitespace at various places are found.
	(reftex-extract-bib-entries-from-thebibliography): Remove
	superfluous backslash.

2010-10-16  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-cite.el
	(reftex-extract-bib-entries-from-thebibliography): Use
	`with-current-buffer'.

2010-09-14  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-cite.el (reftex-do-citation): Make it possible again
	to insert non-existent entries.

2010-01-30  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-base.el (reftex-syntax-table-for-bib, reftex-mode):
	Do not derive `reftex-syntax-table-for-bib' from
	`reftex-syntax-table' because parens have to retain their paren
	syntax in order for parsing of BibTeX entries like @book(...) to
	work.

2009-09-12  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-vars.el (reftex-label-alist): Doc fix.

	* lisp/reftex-toc.el (reftex-re-enlarge): Call `enlarge-window'
	only if there is something to do because in Emacs the horizontal
	version throws an error even if the parameter is 0.

2009-08-08  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-index.el: Suppress byte-compiler warnings.  Move
	provide statement to end of file.

	* lisp/reftex-dcr.el: Suppress byte-compiler warnings.  Move
	provide statement to end of file.

	* lisp/reftex-auc.el: Suppress byte-compiler warnings.

	* doc/reftex.texi (Imprint): Mention Wolfgang in list of
	contributors.

	* lisp/reftex-vars.el (reftex-plug-into-AUCTeX): Doc fix.

2009-07-05  Ralf Angeli  <angeli@caeruleus.net>

	* doc/reftex.texi (Creating Citations): Give a hint about how to
	auto-revert the BibTeX database file when using external editors.

	* lisp/reftex-cite.el (reftex-do-citation): Save match data when
	asking for optional arguments.

2009-04-29  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-base.el (reftex-in-comment): Do not error out if
	`comment-start-skip' is not set.

2009-03-01  Wolfgang Mayer  <wmayer7@gmail.com>

	* lisp/reftex-cite.el (reftex-all-used-citation-keys):
	Fix regexp to correctly extract all citations in the same line.

2008-12-29  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-vars.el (reftex-vref-is-default)
	(reftex-fref-is-default): Adapt doc string.
	(reftex-ref-style-default-list): New name for
	`reftex-ref-style-active-list'.  Use :set instead of :repeat.
	(reftex-vref-is-default, reftex-fref-is-default): Adapt to new
	name.

	* lisp/reftex-base.el (reftex-tie-multifile-symbols): Add doc
	string.
	(reftex-tie-multifile-symbols): Initialize
	`reftex-ref-style-list'.
	(reftex-untie-multifile-symbols): Add doc string.
	(reftex-add-index-macros): Doc fix.
	(reftex-ref-style-activate, reftex-ref-style-toggle)
	(reftex-ref-style-list): New functions.
	(reftex-mode-menu): Use them.

	* lisp/reftex-sel.el (reftex-select-cycle-ref-style-internal): Use
	`reftex-ref-style-list' function.

	* lisp/reftex-ref.el (reftex-reference): Use
	`reftex-ref-style-list' function.

	* doc/reftex.texi (Referencing Labels): Simplify section about
	reference macro cycling.
	(Reference Styles, Options (Referencing Labels)): Adapt to changed
	implementation.

2008-12-18  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-base.el (reftex-in-comment): Deal correctly with
	escaped comment characters.

2008-11-30  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-vars.el (reftex-ref-style-alist): Change structure
	so that it is not possible to use multiple different package names
	within a style.
	(reftex-ref-style-active-list): Adapt to new structure of
	`reftex-ref-style-alist'.

	* lisp/reftex-sel.el (reftex-select-cycle-active-ref-styles):
	Remove.
	(reftex-select-cycle-ref-style-internal): Adapt to new structure
	of `reftex-ref-style-alist'.

	* lisp/reftex-ref.el: Adapt creation of `reftex-<package>-<macro>'
	functions to new structure of `reftex-ref-style-alist'.
	(reftex-reference): Adapt to new structure of
	`reftex-ref-style-alist'.

	* lisp/reftex-base.el (reftex-mode-menu): Adapt to new structure
	of `reftex-ref-style-alist'.

	* doc/reftex.texi (Options (Referencing Labels)): Adapt to new
	structure of `reftex-ref-style-alist'.

2008-10-16  Ralf Angeli  <angeli@caeruleus.net>

	* doc/reftex.texi (Referencing Labels, Reference Styles): Document
	changes in the referencing functionality.

2008-10-15  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-vars.el (reftex-ref-style-alist): Remove the symbols
	for symbols for macro type distinction.  Add characters for macro
	selection.
	(reftex-ref-macro-prompt): New variable.

	* lisp/reftex-sel.el (reftex-select-cycle-ref-style-internal):
	Remove code for testing macro type.
	(reftex-select-toggle-numref-pageref): Remove.
	(reftex-select-label-map): Remove binding for
	`reftex-select-toggle-numref-pageref'.

	* lisp/reftex-ref.el (reftex-reference): Prompt for a reference
	macro if `reftex-ref-macro-prompt' is non-nil.

	* lisp/reftex-base.el (reftex-select-with-char): Kill the RefTeX
	Select buffer when done.

2008-06-07  David Kastrup  <dak@gnu.org>

	* lisp/reftex-base.el (reftex-remove-if): Improve performance.

2008-06-07  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-cite.el (reftex-get-string-refs): Use a regexp
	alternative for better performance.

	* doc/reftex.texi (Commands): Mention options for definition of
	header and footer in BibTeX files.
	(Options (Creating Citations)): Document
	`reftex-create-bibtex-header' and `reftex-create-bibtex-footer'.

	* lisp/reftex-cite.el (reftex-stringref-p): Remove.
	(reftex-get-string-refs): Do without `reftex-stringref-p' and use
	`reftex-remove-if' instead of the cl-based `remove-if'.
	(reftex-create-bibtex-file): Doc fix.

	* lisp/reftex-base.el (reftex-remove-if): New function.

2008-06-07  Wolfgang Mayer  <wmayer7@gmail.com>

	* lisp/reftex-vars.el (reftex-create-bibtex-header)
	(reftex-create-bibtex-footer): New variables.

	* lisp/reftex-cite.el (reftex-parse-bibtex-entry): Accept
	additional optional argument `raw' and keep quotes or braces if it
	is non-nil.
	(reftex-stringref-p, reftex-get-string-refs): New functions.
	(reftex-create-bibtex-file): Include entries that are
	cross-referenced from cited entries.  Include @String definitions
	in the resulting bib file.  Add header and footer defined in
	`reftex-create-bibtex-header' and `reftex-create-bibtex-footer'.

2008-05-03  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-parse.el (reftex-parse-from-file): Move backward one
	char if a `\' was matched after a section macro.

	* lisp/reftex-global.el (reftex-isearch-switch-to-next-file): Use
	`reverse' instead of `nreverse' and `copy-list' in order to make
	the byte compiler happy.  Get rid of unused `orig-flist' variable.

	* lisp/reftex-base.el (reftex-compile-variables): Revert last
	change.  Match `\' after a section macro.
	(reftex-mapconcat-with-predicate): Remove.

2008-04-13  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-base.el (reftex-mapconcat-with-predicate): New
	function.
	(reftex-compile-variables): Use it.  Treat environments and macros
	differently in the regexp for section matching.

	* lisp/reftex-parse.el (reftex-parse-from-file): Use beginning of
	match instead of end as bound.

	* lisp/reftex-sel.el (reftex-select-label-map): Changing binding
	of reference style toggling to "s" which is not already taken.

	* doc/reftex.texi (Reference Styles): Reflect change in key
	binding for toggling reference styles.  Some minor changes.

2008-03-27  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-vars.el: Some whitespace, doc and checkdoc fixes.

2008-03-16  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-index.el (reftex-index-selection-or-word): Use
	`reftex-region-active-p'.
	(reftex-index-show-entry)
	(reftex-index-initialize-phrases-buffer)
	(reftex-index-phrases-apply-to-region): Sync with Emacs trunk.

	* lisp/reftex-dcr.el (reftex-start-itimer-once): Silence the byte
	compiler.

	* lisp/reftex-auc.el: Move `provide' call to bottom of file.

	* lisp/reftex-base.el: Require easymenu and define autoloads
	earlier to avoid compiler warnings.
	(reftex-region-active-p, reftex-select-with-char)
	(reftex-show-commentary): Sync with version in Emacs trunk.
	(reftex-make-overlay, reftex-overlay-put, reftex-move-overlay)
	(reftex-delete-overlay): Define in a way which hides the XEmacs
	symbols from the byte compiler.
	(reftex-info): Silence the byte compiler.  Use `reftex.info'
	instead of `reftex' in order to get the correct file.

2008-03-07  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-cite.el (reftex-extract-bib-entries): Check if
	BibTeX file changed on disk and ask if it should be reread in case
	it did.

2008-03-02  Ralf Angeli  <angeli@caeruleus.net>

	* doc/reftex.texi: Lots of small fixes.
	(Reference Styles): New section.
	(varioref (LaTeX package), fancyref (LaTeX package)): Remove.
	(Options (Referencing Labels)): Remove descriptions of deprecated
	variables `reftex-vref-is-default' and `reftex-fref-is-default'.
	Add descriptions for `reftex-ref-style-alist' and
	`reftex-ref-style-active-list'.
	(Referencing Labels): Update regarding reference styles.

2008-02-17  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-ref.el (reftex-format-special): Add third argument
	for refstyle possible to be passed, making the byte compiler
	happy.
	(reftex-reference): Pass refstyle to `reftex-format-special'.

	* lisp/reftex-vars.el (reftex-ref-style-active-list): Make
	creation of type compatible with Emacs 21.
	(reftex-format-ref-function): Mention third argument of special
	format function.

	* lisp/reftex-base.el (reftex-mode-menu): Make creation of
	Reference Style menu compatible with Emacs 21.

	* doc/reftex.texi: Fix some typos.

2008-01-27  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-vars.el (reftex-ref-style-active-list): Doc fix.

	* lisp/reftex-base.el (reftex-erase-all-selection-and-index-buffers)
	(reftex-access-parse-file): Use `mapc' instead of `mapcar' where
	return value is not used.

	* lisp/reftex-ref.el (reftex-offer-label-menu): Use `mapc' instead
	of `mapcar' where return value is not used.

	* lisp/reftex-sel.el (reftex-select-item, reftex-select-unmark):
	Use `mapc' instead of `mapcar' where return value is not used.

	* lisp/reftex-vars.el (reftex-ref-style-alist)
	(reftex-ref-style-active-list): New variables.
	(reftex-vref-is-default, reftex-fref-is-default): Adapt doc string
	to new implementation.  Mark as obsolete.  Add compatibility code
	for honoring the variable values in case they are set.

	* lisp/reftex-base.el (reftex-mode-menu): Reference styles are now
	computed from `reftex-ref-style-alist'.  Fix typo.

	* lisp/reftex-ref.el (reftex-reference): Determine reference macro
	by looking at `reftex-ref-style-active-list' and
	`reftex-ref-style-alist'.  Use only one special format function.
	(reftex-varioref-vref, reftex-fancyref-fref)
	(reftex-fancyref-Fref): Remove definitions.  The functions are now
	generated from `reftex-ref-style-alist'.
	(reftex-format-vref, reftex-format-Fref, reftex-format-fref):
	Remove.
	(reftex-format-special): New function.

	* lisp/reftex-sel.el (reftex-select-toggle-varioref)
	(reftex-select-toggle-fancyref): Remove.
	(reftex-select-cycle-active-ref-styles)
	(reftex-select-cycle-ref-style-internal)
	(reftex-select-cycle-ref-style-forward)
	(reftex-select-cycle-ref-style-backward)
	(reftex-select-toggle-numref-pageref): New functions.
	(reftex-select-label-map): Use `v' and `V' for general cycling
	through reference styles.  Add `p' for switching between number
	and page reference types.

2008-01-06  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-cite.el (reftex-pop-to-bibtex-entry)
	(reftex-extract-bib-entries-from-thebibliography): Match \bibitem
	entries with spaces or tabs in front of arguments.
	(reftex-insert-bib-matches): Use `mapc' instead of `mapcar'
	because return value is not used.

2008-01-05  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-toc.el (reftex-make-separate-toc-frame): Hide
	non-operational call to `focus-frame' in Emacs for the compilers's
	sake.

2008-01-03  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-dcr.el (reftex-mouse-view-crossref): Explain why
	point is set.

	* lisp/reftex-toc.el (reftex-toc-do-promote): Use `mapc' instead
	of `mapcar' because return value is not used.
	(reftex-toggle-auto-toc-recenter): Fix typo.

	* lisp/reftex-cite.el (reftex-do-citation): Use `mapc' instead of
	`mapcar' because return value is not used.

2007-10-10  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-vars.el (reftex-cite-format-builtin)
	(reftex-bibliography-commands): Add support for ConTeXt.

	* doc/reftex.texi (Citation Styles): Mention support for ConTeXt.

2007-08-23  Ralf Angeli  <angeli@caeruleus.net>

	* doc/reftex.texi (Options (Defining Label Environments)): Fix
	typo.

2007-07-22  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-base.el (reftex-set-cite-format): Autoload.
	(reftex-access-parse-file): Create parse file in a way that does
	not interfere with recentf mode.
	(reftex-access-parse-file): Do not risk destroying an existing
	buffer.

2007-07-07  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-parse.el (reftex-nth-arg): Use `forward-comment'
	instead of `comment-forward'.  The latter is not always available
	and the former is sufficient for LaTeX.
	(reftex-nth-arg): Revert last change since moving over whitespace
	and comments is done by `reftex-move-to-next-arg'.

2007-05-20  Ralf Angeli  <angeli@caeruleus.net>

	* doc/reftex.texi (Citation Styles): Correct some mistakes.

2007-04-03  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-cite.el: Delete trailing whitespace.
	(reftex-pop-to-bibtex-entry, reftex-extract-bib-entries)
	(reftex-parse-bibtex-entry, reftex-create-bibtex-file): Match
	entries containing numbers and symbol constituents.

2007-03-28  Ralf Angeli  <angeli@caeruleus.net>

	* doc/reftex.texi: Replace BibTeX by @BibTeX{} throughout the
	file.

2007-03-12  John Paul Wallington  <jpw@pobox.com>

	* lisp/reftex-vars.el (reftex-format-ref-function)
	(reftex-format-cite-function): Fix custom type.

2007-03-10  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-base.el: Remove information in comment already in
	imprint or cluttering output of `finder-commentary'.
	Require reftex.el.

	* lisp/reftex.el: Remove.  Now generated from reftex.el.in.

	* lisp/reftex-dcr.el: Remove version info in header.  Add
	maintainer info.

	* lisp/reftex-global.el Remove version info in header.  Add
	maintainer info.

	* lisp/reftex-index.el Remove version info in header.  Add
	maintainer info.

	* lisp/reftex-parse.el Remove version info in header.  Add
	maintainer info.

	* lisp/reftex-ref.el Remove version info in header.  Add
	maintainer info.

	* lisp/reftex-sel.el Remove version info in header.  Add
	maintainer info.

	* lisp/reftex-toc.el Remove version info in header.  Add
	maintainer info.

	* lisp/reftex-vars.el Remove version info in header.  Add
	maintainer info.

2007-03-08  Ralf Angeli  <angeli@caeruleus.net>

	* doc/reftex.texi: Include macros.texi and version.texi.  Change
	maintainer and version information.  Express TeX, LaTeX, AUCTeX
	and RefTeX with macros.
	(Imprint): Change maintainer information.

2007-03-04  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex.el: Update.

	* lisp/reftex-base.el (reftex-show-commentary): Look in
	reftex-base.el.
	(reftex-report-bug): New function.

	* lisp/reftex.el: Move original content to reftex-base.el.  Add
	new header and automatically generated autoloads.

	* lisp/reftex-base.el: New file.  Insert original content of
	reftex.el.  Remove autoload for `reftex-index-phrases-mode'.
	Delete trailing whitespace.

2007-02-26  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-index.el (reftex-index-visit-phrases-buffer): Set
	marker when visiting buffer.  This allows for returning from the
	phrases file to the file one was just editing instead of the file
	where the last phrases was added from.

2007-02-25  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-index.el (reftex-index-phrases-syntax-table): New
	variable.  Give ?\" punctuation syntax as it usually is not used
	as string quote in TeX-related modes and may occur unmatched.  The
	change also prevents fontification of quoted content.
	(reftex-index-phrases-mode): Use it.

	* lisp/reftex-cite.el (reftex-parse-bibtex-entry): Match fields
	containing hyphens (besides word constituents).

2007-02-25  David Kastrup  <dak@gnu.org>

	* lisp/reftex.el (reftex-uniquify, reftex-uniquify-by-car):
	Replace O(n^2) algorithms with O(n log n).  Introduce optional
	argument SORT (not yet used).  TODO: figure out callers that can
	specify SORT, in order to further speed this up.

2007-02-25  Ralf Angeli  <angeli@caeruleus.net>

	* lisp/reftex-vars.el (reftex-cite-key-separator): New variable.

	* lisp/reftex-auc.el (reftex-arg-cite): Use
	`reftex-cite-key-separator'.

	* lisp/reftex-cite.el (reftex-do-citation)
	(reftex-figure-out-cite-format): Use `reftex-cite-key-separator'.

	* doc/reftex.texi (Options (Creating Citations)): Document
	`reftex-cite-key-separator'.

	* lisp/reftex-cite.el (reftex-do-citation): Return all keys, not
	just the first one.

	* lisp/reftex-auc.el (reftex-arg-cite): Correctly handle new
	value type returned by `reftex-citation'.

2007-02-24  Ralf Angeli  <angeli@caeruleus.net>

	* doc/reftex.texi: Set correct filename for standalone
	distribution.

2007-02-07  Ralf Angeli  <angeli@caeruleus.net>

	Create `reftex' module.
2012-09-30 22:30:13 +02:00

870 lines
35 KiB
EmacsLisp

;;; reftex-ref.el --- code to create labels and references with RefTeX
;; Copyright (C) 1997-2012 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)
(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.")
(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.")))))))
(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 ---------------------------------------------------------
(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 "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).")
(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)))))
(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))))))
;; 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.
(while (string-match "\\(\\\\ref\\)[ \t]*{" fmt)
(setq fmt (replace-match refstyle t t fmt 1)))
(format fmt label))
(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_*.:]*")
(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