Add 2010 to copyright years.
[bpt/emacs.git] / lisp / textmodes / refbib.el
index e0eb061..14e7454 100644 (file)
@@ -1,13 +1,18 @@
 ;;; refbib.el --- convert refer-style references to ones usable by Latex bib
 
-;; Copyright (C) 1989 Free Software Foundation, Inc.
+;; Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005,
+;;   2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+
+;; Author: Henry Kautz <kautz@research.att.com>
+;; Maintainer: FSF
+;; Keywords: bib, tex
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; 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 1, or (at your option)
-;; any later version.
+;; 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
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
 
 ;; Use: from a buffer containing the refer-style bibliography,
 ;;   M-x r2b-convert-buffer
 ;; Program will prompt for an output buffer name, and will log
 ;; warnings during the conversion process in the buffer *Log*.
 
-; HISTORY
-; 9/88, created
-; modified 1/19/89, allow books with editor but no author;
-;                   added %O ordering field;
-;                   appended illegal multiple fields, instead of 
-;                     discarding;
-;                   added rule, a tech report whose %R number
-;                     contains "ISBN" is really a book
-;                   added rule, anything with an editor is a book
-;                     or a proceedings
-;                   added 'manual type, for items with institution
-;                     but no author or editor
-;                   fixed bug so trailing blanks are trimmed
-;                   added 'proceedings type
-;                   used "organization" field for proceedings
-; modified 2/16/89, updated help messages
-; modified 2/23/89, include capitalize stop words in r2b stop words,
-;                   fixed problems with contractions (e.g. it's),
-;                   caught multiple stop words in a row
-; modified 3/1/89,  fixed capitialize-title for first words all caps
-; modified 3/15/89, allow use of " to delimit fields
-; modified 4/18/89, properly "quote" special characters on output
+;;; Change Log:
+
+;; HISTORY
+;; 9/88, created H.Kautz
+;; modified 1/19/89, allow books with editor but no author;
+;;                   added %O ordering field;
+;;                   appended invalid multiple fields, instead of
+;;                     discarding;
+;;                   added rule, a tech report whose %R number
+;;                     contains "ISBN" is really a book
+;;                   added rule, anything with an editor is a book
+;;                     or a proceedings
+;;                   added 'manual type, for items with institution
+;;                     but no author or editor
+;;                   fixed bug so trailing blanks are trimmed
+;;                   added 'proceedings type
+;;                   used "organization" field for proceedings
+;; modified 2/16/89, updated help messages
+;; modified 2/23/89, include capitalize stop words in r2b stop words,
+;;                   fixed problems with contractions (e.g. it's),
+;;                   caught multiple stop words in a row
+;; modified 3/1/89,  fixed capitalize-title for first words all caps
+;; modified 3/15/89, allow use of " to delimit fields
+;; modified 4/18/89, properly "quote" special characters on output
+
+;;; Code:
+
 ;**********************************************************
 ; User Parameters
 
-(defvar r2b-trace-on nil "*trace conversion")
+(defgroup refbib nil
+  "Convert refer-style references to ones usable by Latex bib."
+  :prefix "r2b-"
+  :group 'wp)
 
-(defvar r2b-journal-abbrevs
-   '(  
-       )
-   "  Abbreviation list for journal names.  
+(defcustom r2b-trace-on nil
+  "*Non-nil means trace conversion."
+  :type 'boolean
+  :group 'refbib)
+
+(defcustom r2b-journal-abbrevs
+  '(
+    )
+  "Abbreviation list for journal names.
 If the car of an element matches a journal name exactly, it is replaced by
 the cadr when output.  Braces must be included if replacement is a
 {string}, but not if replacement is a bibtex abbreviation.  The cadr
-may be eliminated if is exactly the same as the car.  
+may be eliminated if is exactly the same as the car.
   Because titles are capitalized before matching, the abbreviation
-for the journal name should be listed as beginning with a capital 
+for the journal name should be listed as beginning with a capital
 letter, even if it really doesn't.
   For example, a value of '((\"Aij\" \"{Artificial Intelligence}\")
-(\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
-\"Artificial Intelligence\", but would replace Ijcai81 with the 
-BibTeX macro \"ijcai7\".")
-
-(defvar r2b-booktitle-abbrevs 
-   '(  
-       )
-   "  Abbreviation list for book and proceedings names.  If the car of
-an element matches a title or booktitle exactly, it is replaced by 
-the cadr when output.  Braces must be included if replacement is 
-a {string}, but not if replacement is a bibtex abbreviation.  The cadr 
-may be eliminated if is exactly the same as the car.  
+\(\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
+\"Artificial Intelligence\", but would replace Ijcai81 with the
+BibTeX macro \"ijcai7\"."
+  :type '(repeat (list string string))
+  :group 'refbib)
+
+(defcustom r2b-booktitle-abbrevs
+  '(
+    )
+  "Abbreviation list for book and proceedings names.
+If the car of an element matches a title or booktitle exactly, it is
+replaced by the cadr when output.  Braces must be included if
+replacement is a {string}, but not if replacement is a bibtex
+abbreviation.  The cadr may be eliminated if is exactly the same as
+the car.
   Because titles are capitalized before matching, the abbreviated title
 should be listed as beginning with a capital letter, even if it doesn't.
   For example, a value of '((\"Aij\" \"{Artificial Intelligence}\")
-(\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
-\"Artificial Intelligence\", but would replace Ijcai81 with the 
-BibTeX macro \"ijcai7\".")
-
-(defvar r2b-proceedings-list
-   '()
-   "  Assoc list of books or journals which are really conference proceedings,
+\(\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
+\"Artificial Intelligence\", but would replace Ijcai81 with the
+BibTeX macro \"ijcai7\"."
+  :type '(repeat (list string string))
+  :group 'refbib)
+
+(defcustom r2b-proceedings-list
+  '()
+  "Assoc list of books or journals which are really conference proceedings,
 but whose name and whose abbrev expansion (as defined in `r2b-journal-abbrevs'
 and `r2b-booktitle-abbrevs') does not contain the words \"conference\" or
 \"proceedings\".  (Those cases are handled automatically.)
 The entry must match the given data exactly.
-  Because titles are capitalized before matching, the items in this list 
+  Because titles are capitalized before matching, the items in this list
 should begin with a capital letter.
   For example, suppose the title \"Ijcai81\" is used for the proceedings of
-a conference, and it's expansion is the BibTeX macro \"ijcai7\".  Then
+a conference, and its expansion is the BibTeX macro \"ijcai7\".  Then
 `r2b-proceedings-list' should be '((\"Ijcai81\") ...).  If instead its
 expansion were \"Proceedings of the Seventh International Conference
 on Artificial Intelligence\", then you would NOT need to include Ijcai81
-in `r2b-proceedings-list' (although it wouldn't cause an error).")
+in `r2b-proceedings-list' (although it wouldn't cause an error)."
+  :type '(repeat (list string string))
+  :group 'refbib)
 
 (defvar r2b-additional-stop-words
-        "Some\\|What"
-   "Words other than the `capitialize-title-stop-words'
-which are not to be used to build the citation key")
-
+  "Some\\|What"
+  "Words not to be used to build the citation key.
+This is in addition to the `r2b-capitalize-title-stop-words'.")
 
-(defvar r2b-delimit-with-quote
-  t
-  "*If true, then use \" to delimit fields, otherwise use braces")
+(defcustom r2b-delimit-with-quote t
+  "*If true, then use \" to delimit fields, otherwise use braces."
+  :type 'boolean
+  :group 'refbib)
 
 ;**********************************************************
 ; Utility Functions
 
-(defvar capitalize-title-stop-words
+(defvar r2b-capitalize-title-stop-words
    (concat
       "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|"
       "by\\|with\\|that\\|its")
-   "Words not to be capitialized in a title (unless they are the first
-word in the title)")
+   "Words not to be capitalized in a title (unless the first word).")
 
-(defvar capitalize-title-stop-regexp
-   (concat "\\(" capitalize-title-stop-words "\\)\\(\\b\\|'\\)"))
+(defvar r2b-capitalize-title-stop-regexp
+   (concat "\\(" r2b-capitalize-title-stop-words "\\)\\(\\b\\|'\\)"))
 
-(defun capitalize-title-region (begin end)
+(defun r2b-capitalize-title-region (begin end)
    "Like `capitalize-region', but don't capitalize stop words, except the first."
    (interactive "r")
    (let ((case-fold-search nil) (orig-syntax-table (syntax-table)))
@@ -136,21 +161,20 @@ word in the title)")
               (if (looking-at "[A-Z][a-z]*[A-Z]")
                  (forward-word 1)
                  (if (let ((case-fold-search t))
-                        (looking-at capitalize-title-stop-regexp))
+                        (looking-at r2b-capitalize-title-stop-regexp))
                     (downcase-word 1)
                     (capitalize-word 1)))
               ))
         (set-syntax-table orig-syntax-table))))
 
 
-(defun capitalize-title (s)
-   "Like capitalize, but don't capitalize stop words, except the first."
-   (save-excursion
-      (set-buffer (get-buffer-create "$$$Scratch$$$"))
-      (erase-buffer)
-      (insert s)
-      (capitalize-title-region (point-min) (point-max))
-      (buffer-string)))
+(defun r2b-capitalize-title (s)
+   "Like `capitalize', but don't capitalize stop words, except the first."
+   (with-current-buffer (get-buffer-create "$$$Scratch$$$")
+     (erase-buffer)
+     (insert s)
+     (r2b-capitalize-title-region (point-min) (point-max))
+     (buffer-string)))
 
 ;*********************************************************
 (defun r2b-reset ()
@@ -159,14 +183,14 @@ word in the title)")
    (makunbound 'r2b-journal-abbrevs)
    (makunbound 'r2b-booktitle-abbrevs)
    (makunbound 'r2b-proceedings-list)
-   (makunbound 'capitalize-title-stop-words)
-   (makunbound 'capitalize-title-stop-regexp)
+   (makunbound 'r2b-capitalize-title-stop-words)
+   (makunbound 'r2b-capitalize-title-stop-regexp)
    (makunbound 'r2b-additional-stop-words)
    (makunbound 'r2b-stop-regexp))
 
 (defvar r2b-stop-regexp
-   (concat "\\`\\(\\(" 
-      r2b-additional-stop-words "\\|" capitalize-title-stop-words
+   (concat "\\`\\(\\("
+      r2b-additional-stop-words "\\|" r2b-capitalize-title-stop-words
       "\\)\\('\\w*\\)?\\W+\\)*\\([A-Z0-9]+\\)"))
 
 
@@ -180,45 +204,64 @@ word in the title)")
    "Returns string matched in current buffer."
    (buffer-substring (match-beginning exp) (match-end exp)))
 
-(defvar r2b-out-buf-name "*Out*" "*output from refer-to-bibtex" )
-(defvar r2b-log-name "*Log*" "*logs errors from refer-to-bibtex" )
+(defcustom r2b-out-buf-name "*Out*"
+  "*Name of buffer for output from refer-to-bibtex."
+  :type 'string
+  :group 'refbib)
+
+(defcustom r2b-log-name "*Log*"
+  "*Name of buffer for logs errors from refer-to-bibtex."
+  :type 'string
+  :group 'refbib)
+
 (defvar r2b-in-buf nil)
 (defvar r2b-out-buf nil)
 (defvar r2b-log nil)
 
 (defvar r2b-error-found nil)
 
+(defvar r2b-variables) (defvar r2bv-address)    (defvar r2bv-annote)
+(defvar r2bv-author)   (defvar r2bv-booktitle)  (defvar r2bv-date)
+(defvar r2bv-decade)   (defvar r2bv-editor)     (defvar r2bv-entry-kind)
+(defvar r2bv-institution) (defvar r2bv-journal) (defvar r2bv-keywords)
+(defvar r2bv-kn)       (defvar r2bv-month)      (defvar r2bv-note)
+(defvar r2bv-number)   (defvar r2bv-ordering)   (defvar r2bv-organization)
+(defvar r2bv-pages)    (defvar r2bv-primary-author) (defvar r2bv-publisher)
+(defvar r2bv-school)   (defvar r2bv-title)      (defvar r2bv-title-first-word)
+(defvar r2bv-tr)       (defvar r2bv-type)       (defvar r2bv-volume)
+(defvar r2bv-where)    (defvar r2bv-year)
+
 (setq r2b-variables '(
-                       r2b-error-found
-                         r2bv-author
-                         r2bv-primary-author
-                         r2bv-date
-                         r2bv-year
-                         r2bv-decade
-                         r2bv-month
-                         r2bv-title
-                         r2bv-title-first-word
-                         r2bv-editor
-                         r2bv-annote
-                         r2bv-tr
-                         r2bv-address
-                         r2bv-institution
-                         r2bv-keywords
-                         r2bv-booktitle
-                         r2bv-journal
-                         r2bv-volume
-                         r2bv-number
-                         r2bv-pages
-                         r2bv-booktitle
-                         r2bv-kn
-                         r2bv-publisher
-                         r2bv-organization
-                         r2bv-school
-                         r2bv-type
-                         r2bv-where
-                         r2bv-note
-                         r2bv-ordering
-                         ))
+                      r2b-error-found
+                      r2bv-author
+                      r2bv-primary-author
+                      r2bv-date
+                      r2bv-year
+                      r2bv-decade
+                      r2bv-month
+                      r2bv-title
+                      r2bv-title-first-word
+                      r2bv-editor
+                      r2bv-annote
+                      r2bv-tr
+                      r2bv-address
+                      r2bv-institution
+                      r2bv-keywords
+                      r2bv-booktitle
+                      r2bv-journal
+                      r2bv-volume
+                      r2bv-number
+                      r2bv-pages
+                      r2bv-booktitle
+                      r2bv-kn
+                      r2bv-publisher
+                      r2bv-organization
+                      r2bv-school
+                      r2bv-type
+                      r2bv-where
+                      r2bv-note
+                      r2bv-ordering
+                      ))
 
 (defun r2b-clear-variables ()
    "Set all global vars used by r2b to nil."
@@ -247,7 +290,7 @@ title if CAPITALIZE is true.  Returns value of VAR."
       (r2b-trace "snarfing %s" field)
       (goto-char (point-min))
       (while (and not-past-end
-               (re-search-forward 
+               (re-search-forward
                   (concat "^" field "\\b[ \t]*\\(.*[^ \t\n]\\)[ \t]*") nil t))
         (setq item (r2b-match 1))
         (while (and (setq not-past-end (zerop (forward-line 1)))
@@ -259,7 +302,7 @@ title if CAPITALIZE is true.  Returns value of VAR."
            (setq val item)
            (if unique
               (progn
-                 (r2b-warning "*Illegal multiple field %s %s" field item)
+                 (r2b-warning "*Invalid multiple field %s %s" field item)
                  (setq val (concat val "\n" item))
                  )
               (setq val (concat val "\n\t\tand " item))
@@ -267,7 +310,7 @@ title if CAPITALIZE is true.  Returns value of VAR."
            )
         )
       (if (and val capitalize)
-        (setq val (capitalize-title val)))
+        (setq val (r2b-capitalize-title val)))
       (set var val)
       (if (and (null val) required)
         (r2b-require var))
@@ -292,7 +335,7 @@ title if CAPITALIZE is true.  Returns value of VAR."
       (let ((months r2b-month-abbrevs))
         (if (string-match "[^0-9]" r2bv-month)
            (progn
-              (while (and months (not (string-match (car (car months)) 
+              (while (and months (not (string-match (car (car months))
                                          r2bv-month)))
                  (setq months (cdr months)))
               (if months
@@ -354,7 +397,7 @@ title if CAPITALIZE is true.  Returns value of VAR."
       (r2b-set-match 'r2bv-title-first-word 4
         r2b-stop-regexp
         r2bv-title)
-      
+
       (r2b-get-field 'r2bv-annote "%X" t )
       (r2b-get-field 'r2bv-tr "%R" t)
       (r2b-get-field 'r2bv-address "%C" t)
@@ -412,7 +455,7 @@ try to replace the {DATA} with an abbreviation."
       (princ ", \n  ")
       (princ field)
       (princ " =\t")
-      (if (not nodelim) 
+      (if (not nodelim)
        (if r2b-delimit-with-quote
          (princ "\"")
          (princ "{")))
@@ -420,7 +463,7 @@ try to replace the {DATA} with an abbreviation."
       (if (> (match-end 0) 59)
        (princ "\n"))
       (princ data)
-      (if (not nodelim) 
+      (if (not nodelim)
        (if r2b-delimit-with-quote
          (princ "\"")
          (princ "}")))
@@ -430,7 +473,7 @@ try to replace the {DATA} with an abbreviation."
 
 (defun r2b-require (vars)
    "If any of VARS is null, set to empty string and log error."
-   (cond 
+   (cond
       ((null vars))
       ((listp vars) (r2b-require (car vars)) (r2b-require (cdr vars)))
       (t
@@ -462,7 +505,7 @@ try to replace the {DATA} with an abbreviation."
 (defun r2b-isa-university (name)
    "Return t if NAME is a university or similar organization,
 but not a publisher."
-   (and 
+   (and
       name
       (string-match "university" name)
       (not (string-match "press" name))
@@ -479,9 +522,9 @@ but not a publisher."
 
       (setq r2bv-kn (concat r2bv-primary-author r2bv-decade
                        r2bv-title-first-word))
-      
+
       (setq r2bv-entry-kind
-        (cond 
+        (cond
            ((r2b-isa-proceedings r2bv-journal)
               (r2b-moveq r2bv-booktitle r2bv-journal)
               (if (r2b-isa-university r2bv-institution)
@@ -532,7 +575,7 @@ but not a publisher."
               'proceedings)
            ((or r2bv-editor
                (and r2bv-author
-                  (or 
+                  (or
                      (null r2bv-tr)
                      (string-match "\\bisbn\\b" r2bv-tr))))
               (r2b-moveq r2bv-publisher r2bv-institution)
@@ -543,12 +586,12 @@ but not a publisher."
               'book)
            (r2bv-tr
               (r2b-require 'r2bv-institution)
-              (if (string-match 
-                     "\\`\\(\\(.\\|\n\\)+\\)[ \t\n]+\\([^ \t\n]\\)+\\'" 
+              (if (string-match
+                     "\\`\\(\\(.\\|\n\\)+\\)[ \t\n]+\\([^ \t\n]\\)+\\'"
                      r2bv-tr)
                  (progn
                     (setq r2bv-type (substring r2bv-tr 0 (match-end 1)))
-                    (setq r2bv-number (substring r2bv-tr 
+                    (setq r2bv-number (substring r2bv-tr
                                          (match-beginning 3)))
                     (setq r2bv-tr nil))
                  (r2b-moveq r2bv-number r2bv-tr))
@@ -599,14 +642,14 @@ but not a publisher."
    )
 
 
-(defun r2b-convert-record (output-name)
-   "Transform current bib entry and append to buffer OUTPUT;
-do \"M-x r2b-help\" for more info."
-   (interactive 
+(defun r2b-convert-record (output)
+   "Transform current bib entry and append to buffer OUTPUT.
+Do `\\[r2b-help]' for more info."
+   (interactive
       (list (read-string "Output to buffer: " r2b-out-buf-name)))
    (let (rec-end rec-begin not-done)
-      (setq r2b-out-buf-name output-name)
-      (setq r2b-out-buf (get-buffer-create output-name))
+      (setq r2b-out-buf-name output)
+      (setq r2b-out-buf (get-buffer-create output))
       (setq r2b-in-buf (current-buffer))
       (set-buffer r2b-out-buf)
       (goto-char (point-max))
@@ -634,27 +677,22 @@ do \"M-x r2b-help\" for more info."
         nil
         )
       ))
-      
-      
-(defun r2b-convert-buffer (output-name)
-   "Transform current buffer and append to buffer OUTPUT;
-do \"M-x r2b-help\" for more info."
-   (interactive 
-      (list (read-string "Output to buffer: " r2b-out-buf-name)))
-   (save-excursion
-      (setq r2b-log (get-buffer-create r2b-log-name))
-      (set-buffer r2b-log)
-      (erase-buffer))
-   (widen)
-   (goto-char (point-min))
-   (message "Working, please be patient...")
-   (sit-for 0)
-   (while (r2b-convert-record output-name) t)
-   (message "Done, results in %s, errors in %s" 
-      r2b-out-buf-name r2b-log-name)
-   )
 
-(defvar r2b-load-quietly nil "*Don't print help message when loaded")
+
+(defun r2b-convert-buffer (output)
+  "Transform current buffer and append to buffer OUTPUT.
+Do `\\[r2b-help]' for more info."
+  (interactive
+   (list (read-string "Output to buffer: " r2b-out-buf-name)))
+  (with-current-buffer (setq r2b-log (get-buffer-create r2b-log-name))
+    (erase-buffer))
+  (widen)
+  (goto-char (point-min))
+  (message "Working, please be patient...")
+  (sit-for 0)
+  (while (r2b-convert-record output) t)
+  (message "Done, results in %s, errors in %s"
+           r2b-out-buf-name r2b-log-name))
 
 (defvar r2b-help-message
 "                   Refer to Bibtex Bibliography Conversion
@@ -671,7 +709,7 @@ users of TeX and LaTex.  Instructions:
 1.  Visit the file containing the refer-style database.
 2.  The command
        M-x r2b-convert-buffer
-    converts the entire buffer, appending it's output by default in a
+    converts the entire buffer, appending its output by default in a
     buffer named *Out*, and logging progress and errors in a buffer
     named *Log*.  The original file is never modified.
        Note that results are appended to *Out*, so if that buffer
@@ -691,24 +729,22 @@ to text, or substituting bibtex macros.  Do M-x describe-variable on
      r2b-proceedings-list
 for information on these features.
 
-If you don't want to see this help message when you load this utility,
-then include the following line in your .emacs file:
-       (setq r2b-load-quietly t)
-To see this message again, perform 
-         M-x r2b-help")
+Please send bug reports and suggestions to
+       Henry Kautz
+        kautz@research.att.com
+       allegra!kautz")
 
 
 (defun r2b-help ()
-   "Print help message."
-   (interactive)
-   (with-output-to-temp-buffer "*Help*"
-      (princ r2b-help-message)))
-
-(if (not r2b-load-quietly)
-   (r2b-help))
-
-(message "r2b loaded")
-
+  "Print help describing the `refbib' package."
+  (interactive)
+  (with-output-to-temp-buffer "*Help*"
+    (princ r2b-help-message)
+    (with-current-buffer standard-output
+      (help-mode))))
+
+(provide 'refbib)
 (provide 'refer-to-bibtex)
 
+;; arch-tag: 664afee2-6e76-4408-ba56-981d8a179586
 ;;; refbib.el ends here