Merge from trunk.
[bpt/emacs.git] / lisp / textmodes / reftex-ref.el
index e6883c9..b47f2f6 100644 (file)
@@ -1,11 +1,11 @@
 ;;; reftex-ref.el --- code to create labels and references with RefTeX
 
-;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-;;   2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1997-2011 Free Software Foundation, Inc.
 
 ;; Author: Carsten Dominik <dominik@science.uva.nl>
 ;; Maintainer: auctex-devel@gnu.org
 ;; Version: 4.31
+;; Package: reftex
 
 ;; This file is part of GNU Emacs.
 
@@ -78,8 +78,7 @@ If optional BOUND is an integer, limit backward searches to that point."
                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)
+      (with-current-buffer buf
         (save-restriction
           (widen)
           (goto-char 1)
@@ -180,8 +179,8 @@ This function is controlled by the settings of reftex-insert-label-flags."
                 (string-match "^[ \t]*$" default))
             (setq default prefix
                   force-prompt t)       ; need to prompt
-          (setq default 
-                (concat prefix 
+          (setq default
+                (concat prefix
                         (funcall reftex-string-to-label-function default)))
 
           ;; Make it unique.
@@ -227,7 +226,7 @@ This function is controlled by the settings of reftex-insert-label-flags."
              ((setq entry (assoc label
                                  (symbol-value reftex-docstruct-symbol)))
               (ding)
-              (if (y-or-n-p 
+              (if (y-or-n-p
                    (format "Label '%s' exists. Use anyway? " label))
                   (setq valid t)))
 
@@ -237,9 +236,9 @@ This function is controlled by the settings of reftex-insert-label-flags."
         (setq label default))
 
       ;; Insert the label into the label list
-      (let* ((here-I-am-info 
+      (let* ((here-I-am-info
               (save-excursion
-                (if (and (or naked no-insert) 
+                (if (and (or naked no-insert)
                          (integerp (cdr macro-cell)))
                     (goto-char (cdr macro-cell)))
                 (reftex-where-am-I)))
@@ -294,7 +293,7 @@ also applies `reftex-translate-to-ascii-function' to the 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 
+    (mapconcat
      (lambda (c)
        (cond ((and (> c 127) (< c 256))                 ; 8 bit Latin-1
               (char-to-string (aref tab (- c 128))))
@@ -430,7 +429,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
               type (car type))
       (setq type (reftex-query-label-type))))
 
-  (let* ((refstyle 
+  (let* ((reftex-refstyle
           (cond ((reftex-typekey-check type reftex-vref-is-default) "\\vref")
                 ((reftex-typekey-check type reftex-fref-is-default) "\\fref")
                 (t "\\ref")))
@@ -452,7 +451,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
     (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
@@ -466,7 +465,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
               sep (nth 2 (car labels))
               sep1 (cdr (assoc sep reftex-multiref-punctuation))
               labels (cdr labels))
-        (when cut 
+        (when cut
           (backward-delete-char cut)
           (setq cut nil))
 
@@ -477,9 +476,9 @@ When called with 2 C-u prefix args, disable magic word recognition."
         ;; 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)
+               ((string= reftex-refstyle "\\vref") 'reftex-format-vref)
+               ((string= reftex-refstyle "\\fref") 'reftex-format-fref)
+               ((string= reftex-refstyle "\\Fref") 'reftex-format-Fref)
                (t reftex-format-ref-function)))
         ;; ok, insert the reference
         (if sep1 (insert sep1))
@@ -501,7 +500,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
         matched cell)
     (save-excursion
       (while (and (setq cell (pop words))
-                  (not (setq matched 
+                  (not (setq matched
                              (re-search-backward (car cell) bound t))))))
     (if matched
         (cons (cdr cell) (- (match-end 0) (match-end 1)))
@@ -536,14 +535,12 @@ When called with 2 C-u prefix args, disable magic word recognition."
               (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)))
+              (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))
@@ -551,7 +548,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
               (setq mode-line-format
                     (list "----  " 'mode-line-buffer-identification
                           "  " 'global-mode-string "   (" mode-name ")"
-                          "  S<" 'refstyle ">"
+                          "  S<" 'reftex-refstyle ">"
                           " -%-"))
               (cond
                ((= 0 (buffer-size))
@@ -566,9 +563,9 @@ When called with 2 C-u prefix args, disable magic word recognition."
                                 context
                                 counter
                                 commented
-                                (or here-I-am offset) 
+                                (or here-I-am offset)
                                 prefix
-                                nil  ; no a toc buffer 
+                                nil  ; no a toc buffer
                                 ))))
                (here-I-am
                 (setq offset (reftex-get-offset buf here-I-am typekey)))
@@ -692,13 +689,13 @@ When called with 2 C-u prefix args, disable magic word recognition."
 
 (defun reftex-query-label-type ()
   ;; Ask for label type
-  (let ((key (reftex-select-with-char 
+  (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 
+(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.
@@ -720,7 +717,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
         (throw 'exit nil))
 
       ;; Goto the file in another window
-      (setq buffer 
+      (setq buffer
             (if no-revisit
                 (reftex-get-buffer-visiting file)
               (reftex-get-file-buffer-force
@@ -786,7 +783,7 @@ When called with 2 C-u prefix args, disable magic word recognition."
             (when (or (not (eq major-mode 'latex-mode))
                       (not font-lock-mode))
               (latex-mode)
-              (run-hook-with-args 
+              (run-hook-with-args
                'reftex-pre-refontification-functions
                reftex-call-back-to-this-buffer 'reftex-hidden)
               (turn-on-font-lock))
@@ -832,8 +829,16 @@ Optional prefix argument OTHER-WINDOW goes to the label in another window."
   (reftex-access-scan-info)
   (let* ((wcfg (current-window-configuration))
          (docstruct (symbol-value reftex-docstruct-symbol))
-         (label (completing-read "Label: " docstruct
-                                 (lambda (x) (stringp (car x))) t))
+        ;; 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)
@@ -841,10 +846,8 @@ Optional prefix argument OTHER-WINDOW goes to the label in another window."
     (unless other-window
       (set-window-configuration wcfg)
       (switch-to-buffer (marker-buffer where))
-      (goto-char where))      
+      (goto-char where))
     (reftex-unhighlight 0)))
 
 
-
-;; arch-tag: 52f14032-fb76-4d31-954f-750c72415675
 ;;; reftex-ref.el ends here