1 ;;; refbib.el --- convert refer-style references to ones usable by Latex bib
3 ;; Copyright (C) 1989 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;; Use: from a buffer containing the refer-style bibliography,
22 ;; M-x r2b-convert-buffer
23 ;; Program will prompt for an output buffer name, and will log
24 ;; warnings during the conversion process in the buffer *Log*.
28 ; modified 1/19/89, allow books with editor but no author;
29 ; added %O ordering field;
30 ; appended illegal multiple fields, instead of
32 ; added rule, a tech report whose %R number
33 ; contains "ISBN" is really a book
34 ; added rule, anything with an editor is a book
36 ; added 'manual type, for items with institution
37 ; but no author or editor
38 ; fixed bug so trailing blanks are trimmed
39 ; added 'proceedings type
40 ; used "organization" field for proceedings
41 ; modified 2/16/89, updated help messages
42 ; modified 2/23/89, include capitalize stop words in r2b stop words,
43 ; fixed problems with contractions (e.g. it's),
44 ; caught multiple stop words in a row
45 ; modified 3/1/89, fixed capitialize-title for first words all caps
46 ; modified 3/15/89, allow use of " to delimit fields
47 ; modified 4/18/89, properly "quote" special characters on output
48 ;**********************************************************
51 (defvar r2b-trace-on nil
"*trace conversion")
53 (defvar r2b-journal-abbrevs
56 " Abbreviation list for journal names.
57 If the car of an element matches a journal name exactly, it is replaced by
58 the cadr when output. Braces must be included if replacement is a
59 {string}, but not if replacement is a bibtex abbreviation. The cadr
60 may be eliminated if is exactly the same as the car.
61 Because titles are capitalized before matching, the abbreviation
62 for the journal name should be listed as beginning with a capital
63 letter, even if it really doesn't.
64 For example, a value of '((\"Aij\" \"{Artificial Intelligence}\")
65 (\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
66 \"Artificial Intelligence\", but would replace Ijcai81 with the
67 BibTeX macro \"ijcai7\".")
69 (defvar r2b-booktitle-abbrevs
72 " Abbreviation list for book and proceedings names. If the car of
73 an element matches a title or booktitle exactly, it is replaced by
74 the cadr when output. Braces must be included if replacement is
75 a {string}, but not if replacement is a bibtex abbreviation. The cadr
76 may be eliminated if is exactly the same as the car.
77 Because titles are capitalized before matching, the abbreviated title
78 should be listed as beginning with a capital letter, even if it doesn't.
79 For example, a value of '((\"Aij\" \"{Artificial Intelligence}\")
80 (\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
81 \"Artificial Intelligence\", but would replace Ijcai81 with the
82 BibTeX macro \"ijcai7\".")
84 (defvar r2b-proceedings-list
86 " Assoc list of books or journals which are really conference proceedings,
87 but whose name and whose abbrev expansion (as defined in `r2b-journal-abbrevs'
88 and `r2b-booktitle-abbrevs') does not contain the words \"conference\" or
89 \"proceedings\". (Those cases are handled automatically.)
90 The entry must match the given data exactly.
91 Because titles are capitalized before matching, the items in this list
92 should begin with a capital letter.
93 For example, suppose the title \"Ijcai81\" is used for the proceedings of
94 a conference, and it's expansion is the BibTeX macro \"ijcai7\". Then
95 `r2b-proceedings-list' should be '((\"Ijcai81\") ...). If instead its
96 expansion were \"Proceedings of the Seventh International Conference
97 on Artificial Intelligence\", then you would NOT need to include Ijcai81
98 in `r2b-proceedings-list' (although it wouldn't cause an error).")
100 (defvar r2b-additional-stop-words
102 "Words other than the `capitialize-title-stop-words'
103 which are not to be used to build the citation key")
106 (defvar r2b-delimit-with-quote
108 "*If true, then use \" to delimit fields, otherwise use braces")
110 ;**********************************************************
113 (defvar capitalize-title-stop-words
115 "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|"
116 "by\\|with\\|that\\|its")
117 "Words not to be capitialized in a title (unless they are the first
120 (defvar capitalize-title-stop-regexp
121 (concat "\\(" capitalize-title-stop-words
"\\)\\(\\b\\|'\\)"))
123 (defun capitalize-title-region (begin end
)
124 "Like `capitalize-region', but don't capitalize stop words, except the first."
126 (let ((case-fold-search nil
) (orig-syntax-table (syntax-table)))
129 (set-syntax-table text-mode-syntax-table
)
130 (narrow-to-region begin end
)
131 (goto-char (point-min))
132 (if (looking-at "[A-Z][a-z]*[A-Z]")
135 (while (re-search-forward "\\<" nil t
)
136 (if (looking-at "[A-Z][a-z]*[A-Z]")
138 (if (let ((case-fold-search t
))
139 (looking-at capitalize-title-stop-regexp
))
141 (capitalize-word 1)))
143 (set-syntax-table orig-syntax-table
))))
146 (defun capitalize-title (s)
147 "Like capitalize, but don't capitalize stop words, except the first."
149 (set-buffer (get-buffer-create "$$$Scratch$$$"))
152 (capitalize-title-region (point-min) (point-max))
155 ;*********************************************************
157 "Unbind defvars, for debugging."
159 (makunbound 'r2b-journal-abbrevs
)
160 (makunbound 'r2b-booktitle-abbrevs
)
161 (makunbound 'r2b-proceedings-list
)
162 (makunbound 'capitalize-title-stop-words
)
163 (makunbound 'capitalize-title-stop-regexp
)
164 (makunbound 'r2b-additional-stop-words
)
165 (makunbound 'r2b-stop-regexp
))
167 (defvar r2b-stop-regexp
169 r2b-additional-stop-words
"\\|" capitalize-title-stop-words
170 "\\)\\('\\w*\\)?\\W+\\)*\\([A-Z0-9]+\\)"))
173 (defun r2b-trace (&rest args
)
176 (apply (function message
) args
)
179 (defun r2b-match (exp)
180 "Returns string matched in current buffer."
181 (buffer-substring (match-beginning exp
) (match-end exp
)))
183 (defvar r2b-out-buf-name
"*Out*" "*output from refer-to-bibtex" )
184 (defvar r2b-log-name
"*Log*" "*logs errors from refer-to-bibtex" )
185 (defvar r2b-in-buf nil
)
186 (defvar r2b-out-buf nil
)
189 (defvar r2b-error-found nil
)
191 (setq r2b-variables
'(
200 r2bv-title-first-word
223 (defun r2b-clear-variables ()
224 "Set all global vars used by r2b to nil."
225 (let ((vars r2b-variables
))
228 (setq vars
(cdr vars
)))))
230 (defun r2b-warning (&rest args
)
231 (setq r2b-error-found t
)
232 (princ (apply (function format
) args
) r2b-log
)
234 (princ "\n" r2b-out-buf
)
235 (princ "% " r2b-out-buf
)
236 (princ (apply (function format
) args
) r2b-out-buf
))
238 (defun r2b-get-field (var field
&optional unique required capitalize
)
239 "Set VAR to string value of FIELD, if any. If none, VAR is set to
240 nil. If multiple fields appear, then separate values with the
241 '\\nand\\t\\t', unless UNIQUE is non-nil, in which case log a warning
242 and just concatenate the values. Trim off leading blanks and tabs on
243 first line, and trailing blanks and tabs of every line. Log a warning
244 and set VAR to the empty string if REQUIRED is true. Capitalize as a
245 title if CAPITALIZE is true. Returns value of VAR."
246 (let (item val
(not-past-end t
))
247 (r2b-trace "snarfing %s" field
)
248 (goto-char (point-min))
249 (while (and not-past-end
251 (concat "^" field
"\\b[ \t]*\\(.*[^ \t\n]\\)[ \t]*") nil t
))
252 (setq item
(r2b-match 1))
253 (while (and (setq not-past-end
(zerop (forward-line 1)))
254 (not (looking-at "[ \t]*$\\|%")))
255 (looking-at "\\(.*[^ \t\n]\\)[ \t]*$")
256 (setq item
(concat item
"\n" (r2b-match 1)))
262 (r2b-warning "*Illegal multiple field %s %s" field item
)
263 (setq val
(concat val
"\n" item
))
265 (setq val
(concat val
"\n\t\tand " item
))
269 (if (and val capitalize
)
270 (setq val
(capitalize-title val
)))
272 (if (and (null val
) required
)
276 (defun r2b-set-match (var n regexp string
)
277 "Set VAR to the Nth subpattern in REGEXP matched by STRING, or nil if none."
279 (if (and (stringp string
) (string-match regexp string
))
280 (substring string
(match-beginning n
) (match-end n
))
285 (defvar r2b-month-abbrevs
286 '(("jan") ("feb") ("mar") ("apr") ("may") ("jun") ("jul") ("aug")
287 ("sep") ("oct") ("nov") ("dec")))
289 (defun r2b-convert-month ()
290 "Try to convert `r2bv-month' to a standard 3 letter name."
292 (let ((months r2b-month-abbrevs
))
293 (if (string-match "[^0-9]" r2bv-month
)
295 (while (and months
(not (string-match (car (car months
))
297 (setq months
(cdr months
)))
299 (setq r2bv-month
(car (car months
)))))
301 (setq months
(car (read-from-string r2bv-month
)))
302 (if (and (numberp months
)
305 (setq r2bv-month
(car (nth months r2b-month-abbrevs
)))
307 (r2b-warning "* Ridiculous month")
308 (setq r2bv-month nil
))
314 (defun r2b-snarf-input ()
315 "Parse buffer into global variables."
316 (let ((case-fold-search t
))
317 (r2b-trace "snarfing...")
319 (set-buffer r2b-in-buf
)
320 (goto-char (point-min))
322 (princ (buffer-substring (point) (progn (end-of-line) (point))) r2b-log
)
325 (r2b-get-field 'r2bv-author
"%A")
326 (r2b-get-field 'r2bv-editor
"%E")
329 (r2b-set-match 'r2bv-primary-author
1
330 "\\b\\(\\w+\\)[ \t]*\\($\\|,\\)" r2bv-author
)
333 (r2b-set-match 'r2bv-primary-author
1
334 "\\b\\(\\w+\\)[ \t]*\\($\\|,\\)" r2bv-editor
)
337 (setq r2bv-primary-author
"")
341 (r2b-get-field 'r2bv-date
"%D" t t
)
342 (r2b-set-match 'r2bv-year
0 "[12][0-9][0-9][0-9]" r2bv-date
)
343 (and (null r2bv-year
)
344 (r2b-set-match 'r2bv-year
1 "[^0-9]\\([0-9][0-9]\\)$" r2bv-date
)
345 (setq r2bv-year
(concat "19" r2bv-year
)))
346 (r2b-set-match 'r2bv-decade
1 "..\\(..\\)" r2bv-year
)
347 (r2b-set-match 'r2bv-month
0
348 "[0-9]+/\\|[a-zA-Z]+" r2bv-date
)
349 (if (and (stringp r2bv-month
) (string-match "\\(.*\\)/$" r2bv-month
))
350 (setq r2bv-month
(substring r2bv-month
0 (match-end 1))))
353 (r2b-get-field 'r2bv-title
"%T" t t t
)
354 (r2b-set-match 'r2bv-title-first-word
4
358 (r2b-get-field 'r2bv-annote
"%X" t
)
359 (r2b-get-field 'r2bv-tr
"%R" t
)
360 (r2b-get-field 'r2bv-address
"%C" t
)
361 (r2b-get-field 'r2bv-institution
"%I" t
)
362 (r2b-get-field 'r2bv-keywords
"%K")
363 (r2b-get-field 'r2bv-booktitle
"%B" t nil t
)
364 (r2b-get-field 'r2bv-journal
"%J" t nil t
)
365 (r2b-get-field 'r2bv-volume
"%V" t
)
366 (r2b-get-field 'r2bv-number
"%N" t
)
367 (r2b-get-field 'r2bv-pages
"%P" t
)
368 (r2b-get-field 'r2bv-where
"%W" t
)
369 (r2b-get-field 'r2bv-ordering
"%O" t
)
374 (defun r2b-put-field (field data
&optional abbrevs
)
375 "Print bibtex FIELD = {DATA} if DATA not null; precede
376 with a comma and newline; if ABBREVS list is given, then
377 try to replace the {DATA} with an abbreviation."
379 (let (match nodelim multi-line index
)
381 ((and abbrevs
(setq match
(assoc data abbrevs
)))
382 (if (null (cdr match
))
383 (setq data
(car match
))
384 (setq data
(car (cdr match
))))
386 ((and (not (equal data
""))
387 (not (string-match "[^0-9]" data
)))
391 (while (string-match "[\\~^]" data index
)
392 (setq data
(concat (substring data
0 (match-beginning 0))
394 (substring data
(match-beginning 0) (match-end 0))
396 (substring data
(match-end 0))))
397 (setq index
(+ (match-end 0) 7)))
399 (while (string-match "[$&%#_{}]" data index
)
400 (setq data
(concat (substring data
0 (match-beginning 0))
402 (substring data
(match-beginning 0))))
403 (setq index
(+ (match-end 0) 1)))
405 (if r2b-delimit-with-quote
406 (while (string-match "\"" data index
)
407 (setq data
(concat (substring data
0 (match-beginning 0))
409 (substring data
(match-end 0))))
410 (setq index
(+ (match-end 0) 2))))
416 (if r2b-delimit-with-quote
419 (string-match ".*" data
)
420 (if (> (match-end 0) 59)
424 (if r2b-delimit-with-quote
431 (defun r2b-require (vars)
432 "If any of VARS is null, set to empty string and log error."
435 ((listp vars
) (r2b-require (car vars
)) (r2b-require (cdr vars
)))
437 (if (null (symbol-value vars
))
439 (r2b-warning "*Missing value for field %s" vars
)
446 (defmacro r2b-moveq
(new old
)
447 "Set NEW to OLD and set OLD to nil."
448 (list 'progn
(list 'setq new old
) (list 'setq old
'nil
)))
450 (defun r2b-isa-proceedings (name)
451 "Return t if NAME is the name of proceedings."
455 (string-match "proceedings\\|conference" name
)
456 (assoc name r2b-proceedings-list
)
457 (let ((match (assoc name r2b-booktitle-abbrevs
)))
459 (string-match "proceedings\\|conference" (car (cdr match
)))))
462 (defun r2b-isa-university (name)
463 "Return t if NAME is a university or similar organization,
464 but not a publisher."
467 (string-match "university" name
)
468 (not (string-match "press" name
))
472 (defun r2b-barf-output ()
473 "Generate bibtex based on global variables."
474 (let ((standard-output r2b-out-buf
) (case-fold-search t
) match
)
476 (r2b-trace "...barfing")
478 (set-buffer r2b-out-buf
)
480 (setq r2bv-kn
(concat r2bv-primary-author r2bv-decade
481 r2bv-title-first-word
))
483 (setq r2bv-entry-kind
485 ((r2b-isa-proceedings r2bv-journal
)
486 (r2b-moveq r2bv-booktitle r2bv-journal
)
487 (if (r2b-isa-university r2bv-institution
)
488 (r2b-moveq r2bv-organization r2bv-institution
)
489 (r2b-moveq r2bv-publisher r2bv-institution
))
490 (r2b-moveq r2bv-note r2bv-tr
)
491 (r2b-require 'r2bv-author
)
493 ((r2b-isa-proceedings r2bv-booktitle
)
494 (if (r2b-isa-university r2bv-institution
)
495 (r2b-moveq r2bv-organization r2bv-institution
)
496 (r2b-moveq r2bv-publisher r2bv-institution
))
497 (r2b-moveq r2bv-note r2bv-tr
)
498 (r2b-require 'r2bv-author
)
500 ((and r2bv-tr
(string-match "phd" r2bv-tr
))
501 (r2b-moveq r2bv-school r2bv-institution
)
502 (r2b-require 'r2bv-school
)
503 (r2b-require 'r2bv-author
)
505 ((and r2bv-tr
(string-match "master" r2bv-tr
))
506 (r2b-moveq r2bv-school r2bv-institution
)
507 (r2b-require 'r2bv-school
)
508 (r2b-require 'r2bv-author
)
510 ((and r2bv-tr
(string-match "draft\\|unpublish" r2bv-tr
))
511 (r2b-moveq r2bv-note r2bv-institution
)
512 (r2b-require 'r2bv-author
)
515 (r2b-require 'r2bv-author
)
518 (r2b-moveq r2bv-publisher r2bv-institution
)
519 (r2b-moveq r2bv-note r2bv-tr
)
520 (r2b-require 'r2bv-publisher
)
521 (r2b-require 'r2bv-author
)
525 (string-match "\\`personal communication\\'" r2bv-title
))
527 ((r2b-isa-proceedings r2bv-title
)
528 (if (r2b-isa-university r2bv-institution
)
529 (r2b-moveq r2bv-organization r2bv-institution
)
530 (r2b-moveq r2bv-publisher r2bv-institution
))
531 (r2b-moveq r2bv-note r2bv-tr
)
537 (string-match "\\bisbn\\b" r2bv-tr
))))
538 (r2b-moveq r2bv-publisher r2bv-institution
)
539 (r2b-moveq r2bv-note r2bv-tr
)
540 (r2b-require 'r2bv-publisher
)
541 (if (null r2bv-editor
)
542 (r2b-require 'r2bv-author
))
545 (r2b-require 'r2bv-institution
)
547 "\\`\\(\\(.\\|\n\\)+\\)[ \t\n]+\\([^ \t\n]\\)+\\'"
550 (setq r2bv-type
(substring r2bv-tr
0 (match-end 1)))
551 (setq r2bv-number
(substring r2bv-tr
552 (match-beginning 3)))
554 (r2b-moveq r2bv-number r2bv-tr
))
555 (r2b-require 'r2bv-author
)
558 (r2b-moveq r2bv-organization r2bv-institution
)
564 (r2b-require '( r2bv-year
))
567 (princ "\n% Warning -- Errors During Conversion Next Entry\n"))
570 (princ r2bv-entry-kind
)
574 (r2b-put-field "author" r2bv-author
)
575 (r2b-put-field "title" r2bv-title r2b-booktitle-abbrevs
)
576 (r2b-put-field "year" r2bv-year
)
578 (r2b-put-field "month" r2bv-month r2b-month-abbrevs
)
579 (r2b-put-field "journal" r2bv-journal r2b-journal-abbrevs
)
580 (r2b-put-field "volume" r2bv-volume
)
581 (r2b-put-field "type" r2bv-type
)
582 (r2b-put-field "number" r2bv-number
)
583 (r2b-put-field "booktitle" r2bv-booktitle r2b-booktitle-abbrevs
)
584 (r2b-put-field "editor" r2bv-editor
)
585 (r2b-put-field "publisher" r2bv-publisher
)
586 (r2b-put-field "institution" r2bv-institution
)
587 (r2b-put-field "organization" r2bv-organization
)
588 (r2b-put-field "school" r2bv-school
)
589 (r2b-put-field "pages" r2bv-pages
)
590 (r2b-put-field "address" r2bv-address
)
591 (r2b-put-field "note" r2bv-note
)
592 (r2b-put-field "keywords" r2bv-keywords
)
593 (r2b-put-field "where" r2bv-where
)
594 (r2b-put-field "ordering" r2bv-ordering
)
595 (r2b-put-field "annote" r2bv-annote
)
602 (defun r2b-convert-record (output-name)
603 "Transform current bib entry and append to buffer OUTPUT;
604 do \"M-x r2b-help\" for more info."
606 (list (read-string "Output to buffer: " r2b-out-buf-name
)))
607 (let (rec-end rec-begin not-done
)
608 (setq r2b-out-buf-name output-name
)
609 (setq r2b-out-buf
(get-buffer-create output-name
))
610 (setq r2b-in-buf
(current-buffer))
611 (set-buffer r2b-out-buf
)
612 (goto-char (point-max))
613 (setq r2b-log
(get-buffer-create r2b-log-name
))
615 (goto-char (point-max))
616 (set-buffer r2b-in-buf
)
617 (setq not-done
(re-search-forward "[^ \t\n]" nil t
))
620 (re-search-backward "^[ \t]*$" nil
2)
621 (re-search-forward "^%")
622 (beginning-of-line nil
)
623 (setq rec-begin
(point))
624 (re-search-forward "^[ \t]*$" nil
2)
625 (setq rec-end
(point))
626 (narrow-to-region rec-begin rec-end
)
627 (r2b-clear-variables)
630 (set-buffer r2b-in-buf
)
639 (defun r2b-convert-buffer (output-name)
640 "Transform current buffer and append to buffer OUTPUT;
641 do \"M-x r2b-help\" for more info."
643 (list (read-string "Output to buffer: " r2b-out-buf-name
)))
645 (setq r2b-log
(get-buffer-create r2b-log-name
))
649 (goto-char (point-min))
650 (message "Working, please be patient...")
652 (while (r2b-convert-record output-name
) t
)
653 (message "Done, results in %s, errors in %s"
654 r2b-out-buf-name r2b-log-name
)
657 (defvar r2b-load-quietly nil
"*Don't print help message when loaded")
659 (defvar r2b-help-message
660 " Refer to Bibtex Bibliography Conversion
662 A refer-style database is of the form:
665 %T Great Thoughts I've Thought
669 This utility converts these kind of databases to bibtex form, for
670 users of TeX and LaTex. Instructions:
671 1. Visit the file containing the refer-style database.
673 M-x r2b-convert-buffer
674 converts the entire buffer, appending it's output by default in a
675 buffer named *Out*, and logging progress and errors in a buffer
676 named *Log*. The original file is never modified.
677 Note that results are appended to *Out*, so if that buffer
678 buffer already exists and contains material you don't want to
679 save, you should kill it first.
680 3. Switch to the buffer *Out* and save it as a named file.
681 4. To convert a single refer-style entry, simply position the cursor
682 at the entry and enter
683 M-x r2b-convert-record
684 Again output is appended to *Out* and errors are logged in *Log*.
686 This utility is very robust and pretty smart about determining the
687 type of the entry. It includes facilities for expanding refer macros
688 to text, or substituting bibtex macros. Do M-x describe-variable on
690 r2b-booktitle-abbrevs
692 for information on these features.
694 If you don't want to see this help message when you load this utility,
695 then include the following line in your .emacs file:
696 (setq r2b-load-quietly t)
697 To see this message again, perform
702 "Print help message."
704 (with-output-to-temp-buffer "*Help*"
705 (princ r2b-help-message
)))
707 (if (not r2b-load-quietly
)
710 (message "r2b loaded")
712 (provide 'refer-to-bibtex
)
714 ;;; refbib.el ends here