More tweaks of skeleton documentation wrt \n behavior at bol/eol.
[bpt/emacs.git] / lisp / textmodes / reftex-ref.el
index 2372348..f2fa815 100644 (file)
@@ -1,12 +1,9 @@
 ;;; 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, 2009, 2010 Free Software Foundation, Inc.
+;; Copyright (C) 1997-2014 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.
 
 ;;; Code:
 
 (eval-when-compile (require 'cl))
-(provide 'reftex-ref)
+
 (require 'reftex)
 (require 'reftex-parse)
-;;;
 
 (defun reftex-label-location (&optional bound)
   "Return the environment or macro which determines the label type at point.
@@ -180,8 +176,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.
@@ -207,7 +203,7 @@ This function is controlled by the settings of reftex-insert-label-flags."
                          (if naked "Naked Label: " "Label: ")
                          default))
 
-            ;; Lets make sure that this is a valid label
+            ;; Let's make sure that this is a valid label
             (cond
 
              ((string-match (concat "\\`\\(" (regexp-quote prefix)
@@ -227,7 +223,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 +233,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 +290,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))))
@@ -314,8 +310,7 @@ also applies `reftex-translate-to-ascii-function' to the string."
               (save-match-data
                 (cond
                  ((equal letter "f")
-                  (file-name-sans-extension
-                   (file-name-nondirectory (buffer-file-name))))
+                  (file-name-base))
                  ((equal letter "F")
                   (let ((masterdir (file-name-directory (reftex-TeX-master-file)))
                         (file (file-name-sans-extension (buffer-file-name))))
@@ -324,8 +319,7 @@ also applies `reftex-translate-to-ascii-function' to the string."
                         (substring file (length masterdir))
                       file)))
                  ((equal letter "m")
-                  (file-name-sans-extension
-                   (file-name-nondirectory (reftex-TeX-master-file))))
+                  (file-name-base (reftex-TeX-master-file)))
                  ((equal letter "M")
                   (file-name-nondirectory
                    (substring (file-name-directory (reftex-TeX-master-file))
@@ -416,27 +410,54 @@ When called with 2 C-u prefix args, disable magic word recognition."
 
   (interactive)
 
-  ;; check for active recursive edits
+  ;; Check for active recursive edits
   (reftex-check-recursive-edit)
 
-  ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
+  ;; Ensure access to scanning info and rescan buffer if prefix is '(4)
   (reftex-access-scan-info current-prefix-arg)
 
-  (unless type
-    ;; guess type from context
-    (if (and reftex-guess-label-type
-             (setq type (reftex-guess-label-type)))
-        (setq cut (cdr type)
-              type (car type))
-      (setq type (reftex-query-label-type))))
-
-  (let* ((refstyle 
-          (cond ((reftex-typekey-check type reftex-vref-is-default) "\\vref")
-                ((reftex-typekey-check type reftex-fref-is-default) "\\fref")
-                (t "\\ref")))
-         (reftex-format-ref-function reftex-format-ref-function)
-         (form "\\ref{%s}")
-         label labels sep sep1)
+  (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))
@@ -452,7 +473,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 +487,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))
 
@@ -475,17 +496,13 @@ When called with 2 C-u prefix args, disable magic word recognition."
                    (member (preceding-char) '(?\ ?\t ?\n ?~)))
           (setq form (substring form 1)))
         ;; do we have a special format?
-        (setq reftex-format-ref-function
-              (cond
-               ((string= refstyle "\\vref") 'reftex-format-vref)
-               ((string= refstyle "\\fref") 'reftex-format-fref)
-               ((string= refstyle "\\Fref") 'reftex-format-Fref)
-               (t reftex-format-ref-function)))
+       (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)
+             (funcall reftex-format-ref-function label form reftex-refstyle)
            (format form label label)))
         ;; take out the initial ~ for good
         (and (= ?~ (string-to-char form))
@@ -501,7 +518,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)))
@@ -549,7 +566,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))
@@ -564,9 +581,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)))
@@ -690,13 +707,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.
@@ -718,7 +735,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
@@ -784,7 +801,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))
@@ -794,34 +811,31 @@ When called with 2 C-u prefix args, disable magic word recognition."
         (run-hooks 'reftex-display-copied-context-hook)
         (setq buffer-read-only t))))))
 
-(defun reftex-varioref-vref ()
-  "Insert a reference using the `\\vref' macro from the varioref package."
-  (interactive)
-  (let ((reftex-format-ref-function 'reftex-format-vref))
-    (reftex-reference)))
-(defun reftex-fancyref-fref ()
-  "Insert a reference using the `\\fref' macro from the fancyref package."
-  (interactive)
-  (let ((reftex-format-ref-function 'reftex-format-fref)
-        ;;(reftex-guess-label-type nil) ;FIXME do we want this????
-        )
-    (reftex-reference)))
-(defun reftex-fancyref-Fref ()
-  "Insert a reference using the `\\Fref' macro from the fancyref package."
-  (interactive)
-  (let ((reftex-format-ref-function 'reftex-format-Fref)
-        ;;(reftex-guess-label-type nil) ;FIXME do we want this????
-        )
-    (reftex-reference)))
-
-(defun reftex-format-vref (label fmt)
-  (while (string-match "\\\\ref{" fmt)
-    (setq fmt (replace-match "\\vref{" t t fmt)))
-  (format fmt label label))
-(defun reftex-format-Fref (label def-fmt)
-  (format "\\Fref{%s}" label))
-(defun reftex-format-fref (label def-fmt)
-  (format "\\fref{%s}" label))
+;; 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.
@@ -830,8 +844,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)
@@ -839,10 +861,9 @@ 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)))
 
+(provide 'reftex-ref)
 
-
-;; arch-tag: 52f14032-fb76-4d31-954f-750c72415675
 ;;; reftex-ref.el ends here