Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; refbib.el --- convert refer-style references to ones usable by Latex bib |
2 | ||
f2e3589a | 3 | ;; Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, |
ae940284 | 4 | ;; 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
9750e079 | 5 | |
56c586a8 | 6 | ;; Author: Henry Kautz <kautz@research.att.com> |
e3854e65 | 7 | ;; Maintainer: FSF |
56c586a8 | 8 | ;; Keywords: bib, tex |
e5167999 | 9 | |
a2535589 JA |
10 | ;; This file is part of GNU Emacs. |
11 | ||
1fecc8fe | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
a2535589 | 13 | ;; it under the terms of the GNU General Public License as published by |
1fecc8fe GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
a2535589 JA |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
1fecc8fe | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
a2535589 | 24 | |
e5167999 ER |
25 | ;;; Commentary: |
26 | ||
a2535589 JA |
27 | ;; Use: from a buffer containing the refer-style bibliography, |
28 | ;; M-x r2b-convert-buffer | |
29 | ;; Program will prompt for an output buffer name, and will log | |
30 | ;; warnings during the conversion process in the buffer *Log*. | |
31 | ||
e5167999 ER |
32 | ;;; Change Log: |
33 | ||
b578f267 EN |
34 | ;; HISTORY |
35 | ;; 9/88, created H.Kautz | |
36 | ;; modified 1/19/89, allow books with editor but no author; | |
37 | ;; added %O ordering field; | |
5181ff9f | 38 | ;; appended invalid multiple fields, instead of |
b578f267 EN |
39 | ;; discarding; |
40 | ;; added rule, a tech report whose %R number | |
41 | ;; contains "ISBN" is really a book | |
42 | ;; added rule, anything with an editor is a book | |
43 | ;; or a proceedings | |
44 | ;; added 'manual type, for items with institution | |
45 | ;; but no author or editor | |
46 | ;; fixed bug so trailing blanks are trimmed | |
47 | ;; added 'proceedings type | |
48 | ;; used "organization" field for proceedings | |
49 | ;; modified 2/16/89, updated help messages | |
50 | ;; modified 2/23/89, include capitalize stop words in r2b stop words, | |
51 | ;; fixed problems with contractions (e.g. it's), | |
52 | ;; caught multiple stop words in a row | |
53 | ;; modified 3/1/89, fixed capitalize-title for first words all caps | |
54 | ;; modified 3/15/89, allow use of " to delimit fields | |
55 | ;; modified 4/18/89, properly "quote" special characters on output | |
e5167999 ER |
56 | |
57 | ;;; Code: | |
58 | ||
a2535589 JA |
59 | ;********************************************************** |
60 | ; User Parameters | |
61 | ||
d1ebc62e SE |
62 | (defgroup refbib nil |
63 | "Convert refer-style references to ones usable by Latex bib." | |
64 | :prefix "r2b-" | |
65 | :group 'wp) | |
66 | ||
67 | (defcustom r2b-trace-on nil | |
68 | "*Non-nil means trace conversion." | |
69 | :type 'boolean | |
70 | :group 'refbib) | |
71 | ||
72 | (defcustom r2b-journal-abbrevs | |
db95369b | 73 | '( |
d1ebc62e | 74 | ) |
db95369b | 75 | "Abbreviation list for journal names. |
a2535589 JA |
76 | If the car of an element matches a journal name exactly, it is replaced by |
77 | the cadr when output. Braces must be included if replacement is a | |
78 | {string}, but not if replacement is a bibtex abbreviation. The cadr | |
db95369b | 79 | may be eliminated if is exactly the same as the car. |
a2535589 | 80 | Because titles are capitalized before matching, the abbreviation |
db95369b | 81 | for the journal name should be listed as beginning with a capital |
a2535589 JA |
82 | letter, even if it really doesn't. |
83 | For example, a value of '((\"Aij\" \"{Artificial Intelligence}\") | |
a4e104bf | 84 | \(\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string |
db95369b | 85 | \"Artificial Intelligence\", but would replace Ijcai81 with the |
d1ebc62e SE |
86 | BibTeX macro \"ijcai7\"." |
87 | :type '(repeat (list string string)) | |
88 | :group 'refbib) | |
89 | ||
db95369b JB |
90 | (defcustom r2b-booktitle-abbrevs |
91 | '( | |
d1ebc62e SE |
92 | ) |
93 | "Abbreviation list for book and proceedings names. | |
94 | If the car of an element matches a title or booktitle exactly, it is | |
95 | replaced by the cadr when output. Braces must be included if | |
96 | replacement is a {string}, but not if replacement is a bibtex | |
97 | abbreviation. The cadr may be eliminated if is exactly the same as | |
98 | the car. | |
a2535589 JA |
99 | Because titles are capitalized before matching, the abbreviated title |
100 | should be listed as beginning with a capital letter, even if it doesn't. | |
101 | For example, a value of '((\"Aij\" \"{Artificial Intelligence}\") | |
a4e104bf | 102 | \(\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string |
db95369b | 103 | \"Artificial Intelligence\", but would replace Ijcai81 with the |
d1ebc62e SE |
104 | BibTeX macro \"ijcai7\"." |
105 | :type '(repeat (list string string)) | |
106 | :group 'refbib) | |
a2535589 | 107 | |
d1ebc62e SE |
108 | (defcustom r2b-proceedings-list |
109 | '() | |
110 | "Assoc list of books or journals which are really conference proceedings, | |
14c5b721 JB |
111 | but whose name and whose abbrev expansion (as defined in `r2b-journal-abbrevs' |
112 | and `r2b-booktitle-abbrevs') does not contain the words \"conference\" or | |
113 | \"proceedings\". (Those cases are handled automatically.) | |
a2535589 | 114 | The entry must match the given data exactly. |
db95369b | 115 | Because titles are capitalized before matching, the items in this list |
a2535589 JA |
116 | should begin with a capital letter. |
117 | For example, suppose the title \"Ijcai81\" is used for the proceedings of | |
7635fc7c | 118 | a conference, and its expansion is the BibTeX macro \"ijcai7\". Then |
14c5b721 | 119 | `r2b-proceedings-list' should be '((\"Ijcai81\") ...). If instead its |
a2535589 | 120 | expansion were \"Proceedings of the Seventh International Conference |
14c5b721 | 121 | on Artificial Intelligence\", then you would NOT need to include Ijcai81 |
d1ebc62e SE |
122 | in `r2b-proceedings-list' (although it wouldn't cause an error)." |
123 | :type '(repeat (list string string)) | |
124 | :group 'refbib) | |
a2535589 JA |
125 | |
126 | (defvar r2b-additional-stop-words | |
d1ebc62e SE |
127 | "Some\\|What" |
128 | "Words not to be used to build the citation key. | |
e9dac1e1 | 129 | This is in addition to the `r2b-capitalize-title-stop-words'.") |
a2535589 | 130 | |
d1ebc62e SE |
131 | (defcustom r2b-delimit-with-quote t |
132 | "*If true, then use \" to delimit fields, otherwise use braces." | |
133 | :type 'boolean | |
134 | :group 'refbib) | |
a2535589 JA |
135 | |
136 | ;********************************************************** | |
137 | ; Utility Functions | |
138 | ||
e9dac1e1 | 139 | (defvar r2b-capitalize-title-stop-words |
a2535589 JA |
140 | (concat |
141 | "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|" | |
142 | "by\\|with\\|that\\|its") | |
e9dac1e1 | 143 | "Words not to be capitalized in a title (unless the first word).") |
a2535589 | 144 | |
e9dac1e1 RS |
145 | (defvar r2b-capitalize-title-stop-regexp |
146 | (concat "\\(" r2b-capitalize-title-stop-words "\\)\\(\\b\\|'\\)")) | |
a2535589 | 147 | |
e9dac1e1 | 148 | (defun r2b-capitalize-title-region (begin end) |
14c5b721 | 149 | "Like `capitalize-region', but don't capitalize stop words, except the first." |
a2535589 JA |
150 | (interactive "r") |
151 | (let ((case-fold-search nil) (orig-syntax-table (syntax-table))) | |
152 | (unwind-protect | |
153 | (save-restriction | |
154 | (set-syntax-table text-mode-syntax-table) | |
155 | (narrow-to-region begin end) | |
156 | (goto-char (point-min)) | |
157 | (if (looking-at "[A-Z][a-z]*[A-Z]") | |
158 | (forward-word 1) | |
159 | (capitalize-word 1)) | |
160 | (while (re-search-forward "\\<" nil t) | |
161 | (if (looking-at "[A-Z][a-z]*[A-Z]") | |
162 | (forward-word 1) | |
163 | (if (let ((case-fold-search t)) | |
e9dac1e1 | 164 | (looking-at r2b-capitalize-title-stop-regexp)) |
a2535589 JA |
165 | (downcase-word 1) |
166 | (capitalize-word 1))) | |
167 | )) | |
168 | (set-syntax-table orig-syntax-table)))) | |
169 | ||
170 | ||
e9dac1e1 RS |
171 | (defun r2b-capitalize-title (s) |
172 | "Like `capitalize', but don't capitalize stop words, except the first." | |
9a529312 SM |
173 | (with-current-buffer (get-buffer-create "$$$Scratch$$$") |
174 | (erase-buffer) | |
175 | (insert s) | |
176 | (r2b-capitalize-title-region (point-min) (point-max)) | |
177 | (buffer-string))) | |
a2535589 JA |
178 | |
179 | ;********************************************************* | |
180 | (defun r2b-reset () | |
14c5b721 | 181 | "Unbind defvars, for debugging." |
a2535589 JA |
182 | (interactive) |
183 | (makunbound 'r2b-journal-abbrevs) | |
184 | (makunbound 'r2b-booktitle-abbrevs) | |
185 | (makunbound 'r2b-proceedings-list) | |
e9dac1e1 RS |
186 | (makunbound 'r2b-capitalize-title-stop-words) |
187 | (makunbound 'r2b-capitalize-title-stop-regexp) | |
a2535589 | 188 | (makunbound 'r2b-additional-stop-words) |
14c5b721 | 189 | (makunbound 'r2b-stop-regexp)) |
a2535589 JA |
190 | |
191 | (defvar r2b-stop-regexp | |
db95369b | 192 | (concat "\\`\\(\\(" |
e9dac1e1 | 193 | r2b-additional-stop-words "\\|" r2b-capitalize-title-stop-words |
a2535589 JA |
194 | "\\)\\('\\w*\\)?\\W+\\)*\\([A-Z0-9]+\\)")) |
195 | ||
196 | ||
197 | (defun r2b-trace (&rest args) | |
198 | (if r2b-trace-on | |
199 | (progn | |
200 | (apply (function message) args) | |
14c5b721 | 201 | (sit-for 0)))) |
a2535589 JA |
202 | |
203 | (defun r2b-match (exp) | |
14c5b721 | 204 | "Returns string matched in current buffer." |
a2535589 JA |
205 | (buffer-substring (match-beginning exp) (match-end exp))) |
206 | ||
d1ebc62e SE |
207 | (defcustom r2b-out-buf-name "*Out*" |
208 | "*Name of buffer for output from refer-to-bibtex." | |
209 | :type 'string | |
210 | :group 'refbib) | |
211 | ||
212 | (defcustom r2b-log-name "*Log*" | |
213 | "*Name of buffer for logs errors from refer-to-bibtex." | |
214 | :type 'string | |
215 | :group 'refbib) | |
216 | ||
a2535589 JA |
217 | (defvar r2b-in-buf nil) |
218 | (defvar r2b-out-buf nil) | |
219 | (defvar r2b-log nil) | |
220 | ||
221 | (defvar r2b-error-found nil) | |
222 | ||
0c86715d DN |
223 | (defvar r2b-variables) (defvar r2bv-address) (defvar r2bv-annote) |
224 | (defvar r2bv-author) (defvar r2bv-booktitle) (defvar r2bv-date) | |
225 | (defvar r2bv-decade) (defvar r2bv-editor) (defvar r2bv-entry-kind) | |
226 | (defvar r2bv-institution) (defvar r2bv-journal) (defvar r2bv-keywords) | |
227 | (defvar r2bv-kn) (defvar r2bv-month) (defvar r2bv-note) | |
228 | (defvar r2bv-number) (defvar r2bv-ordering) (defvar r2bv-organization) | |
229 | (defvar r2bv-pages) (defvar r2bv-primary-author) (defvar r2bv-publisher) | |
230 | (defvar r2bv-school) (defvar r2bv-title) (defvar r2bv-title-first-word) | |
231 | (defvar r2bv-tr) (defvar r2bv-type) (defvar r2bv-volume) | |
232 | (defvar r2bv-where) (defvar r2bv-year) | |
177a0809 | 233 | |
a2535589 | 234 | (setq r2b-variables '( |
177a0809 JB |
235 | r2b-error-found |
236 | r2bv-author | |
237 | r2bv-primary-author | |
238 | r2bv-date | |
239 | r2bv-year | |
240 | r2bv-decade | |
241 | r2bv-month | |
242 | r2bv-title | |
243 | r2bv-title-first-word | |
244 | r2bv-editor | |
245 | r2bv-annote | |
246 | r2bv-tr | |
247 | r2bv-address | |
248 | r2bv-institution | |
249 | r2bv-keywords | |
250 | r2bv-booktitle | |
251 | r2bv-journal | |
252 | r2bv-volume | |
253 | r2bv-number | |
254 | r2bv-pages | |
255 | r2bv-booktitle | |
256 | r2bv-kn | |
257 | r2bv-publisher | |
258 | r2bv-organization | |
259 | r2bv-school | |
260 | r2bv-type | |
261 | r2bv-where | |
262 | r2bv-note | |
263 | r2bv-ordering | |
264 | )) | |
a2535589 JA |
265 | |
266 | (defun r2b-clear-variables () | |
14c5b721 | 267 | "Set all global vars used by r2b to nil." |
a2535589 JA |
268 | (let ((vars r2b-variables)) |
269 | (while vars | |
270 | (set (car vars) nil) | |
14c5b721 | 271 | (setq vars (cdr vars))))) |
a2535589 JA |
272 | |
273 | (defun r2b-warning (&rest args) | |
274 | (setq r2b-error-found t) | |
275 | (princ (apply (function format) args) r2b-log) | |
276 | (princ "\n" r2b-log) | |
277 | (princ "\n" r2b-out-buf) | |
278 | (princ "% " r2b-out-buf) | |
14c5b721 | 279 | (princ (apply (function format) args) r2b-out-buf)) |
a2535589 JA |
280 | |
281 | (defun r2b-get-field (var field &optional unique required capitalize) | |
282 | "Set VAR to string value of FIELD, if any. If none, VAR is set to | |
283 | nil. If multiple fields appear, then separate values with the | |
284 | '\\nand\\t\\t', unless UNIQUE is non-nil, in which case log a warning | |
285 | and just concatenate the values. Trim off leading blanks and tabs on | |
286 | first line, and trailing blanks and tabs of every line. Log a warning | |
287 | and set VAR to the empty string if REQUIRED is true. Capitalize as a | |
288 | title if CAPITALIZE is true. Returns value of VAR." | |
289 | (let (item val (not-past-end t)) | |
290 | (r2b-trace "snarfing %s" field) | |
291 | (goto-char (point-min)) | |
292 | (while (and not-past-end | |
db95369b | 293 | (re-search-forward |
a2535589 JA |
294 | (concat "^" field "\\b[ \t]*\\(.*[^ \t\n]\\)[ \t]*") nil t)) |
295 | (setq item (r2b-match 1)) | |
296 | (while (and (setq not-past-end (zerop (forward-line 1))) | |
297 | (not (looking-at "[ \t]*$\\|%"))) | |
298 | (looking-at "\\(.*[^ \t\n]\\)[ \t]*$") | |
299 | (setq item (concat item "\n" (r2b-match 1))) | |
300 | ) | |
301 | (if (null val) | |
302 | (setq val item) | |
303 | (if unique | |
304 | (progn | |
5181ff9f | 305 | (r2b-warning "*Invalid multiple field %s %s" field item) |
a2535589 JA |
306 | (setq val (concat val "\n" item)) |
307 | ) | |
308 | (setq val (concat val "\n\t\tand " item)) | |
309 | ) | |
310 | ) | |
311 | ) | |
312 | (if (and val capitalize) | |
e9dac1e1 | 313 | (setq val (r2b-capitalize-title val))) |
a2535589 JA |
314 | (set var val) |
315 | (if (and (null val) required) | |
316 | (r2b-require var)) | |
317 | )) | |
318 | ||
319 | (defun r2b-set-match (var n regexp string ) | |
14c5b721 | 320 | "Set VAR to the Nth subpattern in REGEXP matched by STRING, or nil if none." |
a2535589 JA |
321 | (set var |
322 | (if (and (stringp string) (string-match regexp string)) | |
323 | (substring string (match-beginning n) (match-end n)) | |
324 | nil) | |
325 | ) | |
326 | ) | |
327 | ||
328 | (defvar r2b-month-abbrevs | |
329 | '(("jan") ("feb") ("mar") ("apr") ("may") ("jun") ("jul") ("aug") | |
330 | ("sep") ("oct") ("nov") ("dec"))) | |
331 | ||
332 | (defun r2b-convert-month () | |
14c5b721 | 333 | "Try to convert `r2bv-month' to a standard 3 letter name." |
a2535589 JA |
334 | (if r2bv-month |
335 | (let ((months r2b-month-abbrevs)) | |
336 | (if (string-match "[^0-9]" r2bv-month) | |
337 | (progn | |
db95369b | 338 | (while (and months (not (string-match (car (car months)) |
a2535589 JA |
339 | r2bv-month))) |
340 | (setq months (cdr months))) | |
341 | (if months | |
342 | (setq r2bv-month (car (car months))))) | |
343 | (progn | |
344 | (setq months (car (read-from-string r2bv-month))) | |
345 | (if (and (numberp months) | |
346 | (> months 0) | |
347 | (< months 13)) | |
348 | (setq r2bv-month (car (nth months r2b-month-abbrevs))) | |
349 | (progn | |
350 | (r2b-warning "* Ridiculous month") | |
351 | (setq r2bv-month nil)) | |
352 | )) | |
353 | )) | |
354 | ) | |
355 | ) | |
356 | ||
357 | (defun r2b-snarf-input () | |
14c5b721 | 358 | "Parse buffer into global variables." |
a2535589 JA |
359 | (let ((case-fold-search t)) |
360 | (r2b-trace "snarfing...") | |
361 | (sit-for 0) | |
362 | (set-buffer r2b-in-buf) | |
363 | (goto-char (point-min)) | |
364 | (princ " " r2b-log) | |
365 | (princ (buffer-substring (point) (progn (end-of-line) (point))) r2b-log) | |
366 | (terpri r2b-log) | |
367 | ||
368 | (r2b-get-field 'r2bv-author "%A") | |
369 | (r2b-get-field 'r2bv-editor "%E") | |
370 | (cond | |
371 | (r2bv-author | |
372 | (r2b-set-match 'r2bv-primary-author 1 | |
373 | "\\b\\(\\w+\\)[ \t]*\\($\\|,\\)" r2bv-author) | |
374 | ) | |
375 | (r2bv-editor | |
376 | (r2b-set-match 'r2bv-primary-author 1 | |
377 | "\\b\\(\\w+\\)[ \t]*\\($\\|,\\)" r2bv-editor) | |
378 | ) | |
379 | (t | |
380 | (setq r2bv-primary-author "") | |
381 | ) | |
382 | ) | |
383 | ||
384 | (r2b-get-field 'r2bv-date "%D" t t) | |
385 | (r2b-set-match 'r2bv-year 0 "[12][0-9][0-9][0-9]" r2bv-date) | |
386 | (and (null r2bv-year) | |
387 | (r2b-set-match 'r2bv-year 1 "[^0-9]\\([0-9][0-9]\\)$" r2bv-date) | |
388 | (setq r2bv-year (concat "19" r2bv-year))) | |
389 | (r2b-set-match 'r2bv-decade 1 "..\\(..\\)" r2bv-year) | |
390 | (r2b-set-match 'r2bv-month 0 | |
391 | "[0-9]+/\\|[a-zA-Z]+" r2bv-date) | |
392 | (if (and (stringp r2bv-month) (string-match "\\(.*\\)/$" r2bv-month)) | |
393 | (setq r2bv-month (substring r2bv-month 0 (match-end 1)))) | |
394 | (r2b-convert-month) | |
395 | ||
396 | (r2b-get-field 'r2bv-title "%T" t t t) | |
397 | (r2b-set-match 'r2bv-title-first-word 4 | |
398 | r2b-stop-regexp | |
399 | r2bv-title) | |
db95369b | 400 | |
a2535589 JA |
401 | (r2b-get-field 'r2bv-annote "%X" t ) |
402 | (r2b-get-field 'r2bv-tr "%R" t) | |
403 | (r2b-get-field 'r2bv-address "%C" t) | |
404 | (r2b-get-field 'r2bv-institution "%I" t) | |
405 | (r2b-get-field 'r2bv-keywords "%K") | |
406 | (r2b-get-field 'r2bv-booktitle "%B" t nil t) | |
407 | (r2b-get-field 'r2bv-journal "%J" t nil t) | |
408 | (r2b-get-field 'r2bv-volume "%V" t) | |
409 | (r2b-get-field 'r2bv-number "%N" t) | |
410 | (r2b-get-field 'r2bv-pages "%P" t) | |
411 | (r2b-get-field 'r2bv-where "%W" t) | |
412 | (r2b-get-field 'r2bv-ordering "%O" t) | |
413 | ) | |
414 | ) | |
415 | ||
416 | ||
417 | (defun r2b-put-field (field data &optional abbrevs) | |
14c5b721 | 418 | "Print bibtex FIELD = {DATA} if DATA not null; precede |
a2535589 | 419 | with a comma and newline; if ABBREVS list is given, then |
14c5b721 | 420 | try to replace the {DATA} with an abbreviation." |
a2535589 JA |
421 | (if data |
422 | (let (match nodelim multi-line index) | |
423 | (cond | |
424 | ((and abbrevs (setq match (assoc data abbrevs))) | |
425 | (if (null (cdr match)) | |
426 | (setq data (car match)) | |
427 | (setq data (car (cdr match)))) | |
428 | (setq nodelim t)) | |
429 | ((and (not (equal data "")) | |
430 | (not (string-match "[^0-9]" data))) | |
431 | (setq nodelim t)) | |
432 | (t | |
433 | (setq index 0) | |
434 | (while (string-match "[\\~^]" data index) | |
435 | (setq data (concat (substring data 0 (match-beginning 0)) | |
436 | "\\verb+" | |
437 | (substring data (match-beginning 0) (match-end 0)) | |
438 | "+" | |
439 | (substring data (match-end 0)))) | |
440 | (setq index (+ (match-end 0) 7))) | |
441 | (setq index 0) | |
442 | (while (string-match "[$&%#_{}]" data index) | |
443 | (setq data (concat (substring data 0 (match-beginning 0)) | |
444 | "\\" | |
445 | (substring data (match-beginning 0)))) | |
446 | (setq index (+ (match-end 0) 1))) | |
447 | (setq index 0) | |
448 | (if r2b-delimit-with-quote | |
449 | (while (string-match "\"" data index) | |
450 | (setq data (concat (substring data 0 (match-beginning 0)) | |
451 | "{\"}" | |
452 | (substring data (match-end 0)))) | |
453 | (setq index (+ (match-end 0) 2)))) | |
454 | )) | |
455 | (princ ", \n ") | |
456 | (princ field) | |
457 | (princ " =\t") | |
db95369b | 458 | (if (not nodelim) |
a2535589 JA |
459 | (if r2b-delimit-with-quote |
460 | (princ "\"") | |
461 | (princ "{"))) | |
462 | (string-match ".*" data) | |
463 | (if (> (match-end 0) 59) | |
464 | (princ "\n")) | |
465 | (princ data) | |
db95369b | 466 | (if (not nodelim) |
a2535589 JA |
467 | (if r2b-delimit-with-quote |
468 | (princ "\"") | |
469 | (princ "}"))) | |
470 | ) | |
471 | )) | |
472 | ||
473 | ||
474 | (defun r2b-require (vars) | |
14c5b721 | 475 | "If any of VARS is null, set to empty string and log error." |
db95369b | 476 | (cond |
a2535589 JA |
477 | ((null vars)) |
478 | ((listp vars) (r2b-require (car vars)) (r2b-require (cdr vars))) | |
479 | (t | |
480 | (if (null (symbol-value vars)) | |
481 | (progn | |
482 | (r2b-warning "*Missing value for field %s" vars) | |
483 | (set vars "") | |
484 | ))) | |
485 | ) | |
486 | ) | |
487 | ||
488 | ||
489 | (defmacro r2b-moveq (new old) | |
14c5b721 | 490 | "Set NEW to OLD and set OLD to nil." |
a2535589 JA |
491 | (list 'progn (list 'setq new old) (list 'setq old 'nil))) |
492 | ||
493 | (defun r2b-isa-proceedings (name) | |
14c5b721 | 494 | "Return t if NAME is the name of proceedings." |
a2535589 JA |
495 | (and |
496 | name | |
497 | (or | |
498 | (string-match "proceedings\\|conference" name) | |
499 | (assoc name r2b-proceedings-list) | |
500 | (let ((match (assoc name r2b-booktitle-abbrevs))) | |
501 | (and match | |
502 | (string-match "proceedings\\|conference" (car (cdr match))))) | |
503 | ))) | |
504 | ||
505 | (defun r2b-isa-university (name) | |
14c5b721 JB |
506 | "Return t if NAME is a university or similar organization, |
507 | but not a publisher." | |
db95369b | 508 | (and |
a2535589 JA |
509 | name |
510 | (string-match "university" name) | |
511 | (not (string-match "press" name)) | |
512 | ||
513 | )) | |
514 | ||
515 | (defun r2b-barf-output () | |
14c5b721 | 516 | "Generate bibtex based on global variables." |
a2535589 JA |
517 | (let ((standard-output r2b-out-buf) (case-fold-search t) match) |
518 | ||
519 | (r2b-trace "...barfing") | |
520 | (sit-for 0) | |
521 | (set-buffer r2b-out-buf) | |
522 | ||
523 | (setq r2bv-kn (concat r2bv-primary-author r2bv-decade | |
524 | r2bv-title-first-word)) | |
db95369b | 525 | |
a2535589 | 526 | (setq r2bv-entry-kind |
db95369b | 527 | (cond |
a2535589 JA |
528 | ((r2b-isa-proceedings r2bv-journal) |
529 | (r2b-moveq r2bv-booktitle r2bv-journal) | |
530 | (if (r2b-isa-university r2bv-institution) | |
531 | (r2b-moveq r2bv-organization r2bv-institution) | |
532 | (r2b-moveq r2bv-publisher r2bv-institution)) | |
533 | (r2b-moveq r2bv-note r2bv-tr) | |
534 | (r2b-require 'r2bv-author) | |
535 | 'inproceedings) | |
536 | ((r2b-isa-proceedings r2bv-booktitle) | |
537 | (if (r2b-isa-university r2bv-institution) | |
538 | (r2b-moveq r2bv-organization r2bv-institution) | |
539 | (r2b-moveq r2bv-publisher r2bv-institution)) | |
540 | (r2b-moveq r2bv-note r2bv-tr) | |
541 | (r2b-require 'r2bv-author) | |
542 | 'inproceedings) | |
543 | ((and r2bv-tr (string-match "phd" r2bv-tr)) | |
544 | (r2b-moveq r2bv-school r2bv-institution) | |
545 | (r2b-require 'r2bv-school ) | |
546 | (r2b-require 'r2bv-author) | |
547 | 'phdthesis) | |
548 | ((and r2bv-tr (string-match "master" r2bv-tr)) | |
549 | (r2b-moveq r2bv-school r2bv-institution) | |
550 | (r2b-require 'r2bv-school ) | |
551 | (r2b-require 'r2bv-author) | |
552 | 'mastersthesis) | |
553 | ((and r2bv-tr (string-match "draft\\|unpublish" r2bv-tr)) | |
554 | (r2b-moveq r2bv-note r2bv-institution) | |
555 | (r2b-require 'r2bv-author) | |
556 | 'unpublished) | |
557 | (r2bv-journal | |
558 | (r2b-require 'r2bv-author) | |
559 | 'article) | |
560 | (r2bv-booktitle | |
561 | (r2b-moveq r2bv-publisher r2bv-institution) | |
562 | (r2b-moveq r2bv-note r2bv-tr) | |
563 | (r2b-require 'r2bv-publisher) | |
564 | (r2b-require 'r2bv-author) | |
565 | 'incollection) | |
566 | ((and r2bv-author | |
567 | (null r2bv-editor) | |
568 | (string-match "\\`personal communication\\'" r2bv-title)) | |
569 | 'misc) | |
570 | ((r2b-isa-proceedings r2bv-title) | |
571 | (if (r2b-isa-university r2bv-institution) | |
572 | (r2b-moveq r2bv-organization r2bv-institution) | |
573 | (r2b-moveq r2bv-publisher r2bv-institution)) | |
574 | (r2b-moveq r2bv-note r2bv-tr) | |
575 | 'proceedings) | |
576 | ((or r2bv-editor | |
577 | (and r2bv-author | |
db95369b | 578 | (or |
a2535589 JA |
579 | (null r2bv-tr) |
580 | (string-match "\\bisbn\\b" r2bv-tr)))) | |
581 | (r2b-moveq r2bv-publisher r2bv-institution) | |
582 | (r2b-moveq r2bv-note r2bv-tr) | |
583 | (r2b-require 'r2bv-publisher) | |
584 | (if (null r2bv-editor) | |
585 | (r2b-require 'r2bv-author)) | |
586 | 'book) | |
587 | (r2bv-tr | |
588 | (r2b-require 'r2bv-institution) | |
db95369b JB |
589 | (if (string-match |
590 | "\\`\\(\\(.\\|\n\\)+\\)[ \t\n]+\\([^ \t\n]\\)+\\'" | |
a2535589 JA |
591 | r2bv-tr) |
592 | (progn | |
593 | (setq r2bv-type (substring r2bv-tr 0 (match-end 1))) | |
db95369b | 594 | (setq r2bv-number (substring r2bv-tr |
a2535589 JA |
595 | (match-beginning 3))) |
596 | (setq r2bv-tr nil)) | |
597 | (r2b-moveq r2bv-number r2bv-tr)) | |
598 | (r2b-require 'r2bv-author) | |
599 | 'techreport) | |
600 | (r2bv-institution | |
601 | (r2b-moveq r2bv-organization r2bv-institution) | |
602 | 'manual) | |
603 | (t | |
604 | 'misc) | |
605 | )) | |
606 | ||
607 | (r2b-require '( r2bv-year)) | |
608 | ||
609 | (if r2b-error-found | |
610 | (princ "\n% Warning -- Errors During Conversion Next Entry\n")) | |
611 | ||
612 | (princ "\n@") | |
613 | (princ r2bv-entry-kind) | |
614 | (princ "( ") | |
615 | (princ r2bv-kn) | |
616 | ||
617 | (r2b-put-field "author" r2bv-author ) | |
618 | (r2b-put-field "title" r2bv-title r2b-booktitle-abbrevs) | |
619 | (r2b-put-field "year" r2bv-year ) | |
620 | ||
621 | (r2b-put-field "month" r2bv-month r2b-month-abbrevs) | |
622 | (r2b-put-field "journal" r2bv-journal r2b-journal-abbrevs) | |
623 | (r2b-put-field "volume" r2bv-volume) | |
624 | (r2b-put-field "type" r2bv-type) | |
625 | (r2b-put-field "number" r2bv-number) | |
626 | (r2b-put-field "booktitle" r2bv-booktitle r2b-booktitle-abbrevs) | |
627 | (r2b-put-field "editor" r2bv-editor) | |
628 | (r2b-put-field "publisher" r2bv-publisher) | |
629 | (r2b-put-field "institution" r2bv-institution) | |
630 | (r2b-put-field "organization" r2bv-organization) | |
631 | (r2b-put-field "school" r2bv-school) | |
632 | (r2b-put-field "pages" r2bv-pages) | |
633 | (r2b-put-field "address" r2bv-address) | |
634 | (r2b-put-field "note" r2bv-note) | |
635 | (r2b-put-field "keywords" r2bv-keywords) | |
636 | (r2b-put-field "where" r2bv-where) | |
637 | (r2b-put-field "ordering" r2bv-ordering) | |
638 | (r2b-put-field "annote" r2bv-annote) | |
639 | ||
640 | (princ " )\n") | |
641 | ) | |
642 | ) | |
643 | ||
644 | ||
177a0809 JB |
645 | (defun r2b-convert-record (output) |
646 | "Transform current bib entry and append to buffer OUTPUT. | |
647 | Do `\\[r2b-help]' for more info." | |
db95369b | 648 | (interactive |
a2535589 JA |
649 | (list (read-string "Output to buffer: " r2b-out-buf-name))) |
650 | (let (rec-end rec-begin not-done) | |
177a0809 JB |
651 | (setq r2b-out-buf-name output) |
652 | (setq r2b-out-buf (get-buffer-create output)) | |
a2535589 JA |
653 | (setq r2b-in-buf (current-buffer)) |
654 | (set-buffer r2b-out-buf) | |
655 | (goto-char (point-max)) | |
656 | (setq r2b-log (get-buffer-create r2b-log-name)) | |
657 | (set-buffer r2b-log) | |
658 | (goto-char (point-max)) | |
659 | (set-buffer r2b-in-buf) | |
660 | (setq not-done (re-search-forward "[^ \t\n]" nil t)) | |
661 | (if not-done | |
662 | (progn | |
663 | (re-search-backward "^[ \t]*$" nil 2) | |
664 | (re-search-forward "^%") | |
665 | (beginning-of-line nil) | |
666 | (setq rec-begin (point)) | |
667 | (re-search-forward "^[ \t]*$" nil 2) | |
668 | (setq rec-end (point)) | |
669 | (narrow-to-region rec-begin rec-end) | |
670 | (r2b-clear-variables) | |
671 | (r2b-snarf-input) | |
672 | (r2b-barf-output) | |
673 | (set-buffer r2b-in-buf) | |
674 | (widen) | |
675 | (goto-char rec-end) | |
676 | t) | |
677 | nil | |
678 | ) | |
679 | )) | |
db95369b JB |
680 | |
681 | ||
177a0809 | 682 | (defun r2b-convert-buffer (output) |
9a529312 | 683 | "Transform current buffer and append to buffer OUTPUT. |
177a0809 | 684 | Do `\\[r2b-help]' for more info." |
9a529312 SM |
685 | (interactive |
686 | (list (read-string "Output to buffer: " r2b-out-buf-name))) | |
687 | (with-current-buffer (setq r2b-log (get-buffer-create r2b-log-name)) | |
688 | (erase-buffer)) | |
689 | (widen) | |
690 | (goto-char (point-min)) | |
691 | (message "Working, please be patient...") | |
692 | (sit-for 0) | |
693 | (while (r2b-convert-record output) t) | |
694 | (message "Done, results in %s, errors in %s" | |
695 | r2b-out-buf-name r2b-log-name)) | |
a2535589 | 696 | |
a2535589 JA |
697 | (defvar r2b-help-message |
698 | " Refer to Bibtex Bibliography Conversion | |
699 | ||
700 | A refer-style database is of the form: | |
701 | ||
702 | %A Joe Blow | |
703 | %T Great Thoughts I've Thought | |
704 | %D 1977 | |
705 | etc. | |
706 | ||
707 | This utility converts these kind of databases to bibtex form, for | |
708 | users of TeX and LaTex. Instructions: | |
709 | 1. Visit the file containing the refer-style database. | |
710 | 2. The command | |
711 | M-x r2b-convert-buffer | |
7635fc7c | 712 | converts the entire buffer, appending its output by default in a |
a2535589 JA |
713 | buffer named *Out*, and logging progress and errors in a buffer |
714 | named *Log*. The original file is never modified. | |
715 | Note that results are appended to *Out*, so if that buffer | |
716 | buffer already exists and contains material you don't want to | |
717 | save, you should kill it first. | |
718 | 3. Switch to the buffer *Out* and save it as a named file. | |
719 | 4. To convert a single refer-style entry, simply position the cursor | |
720 | at the entry and enter | |
721 | M-x r2b-convert-record | |
722 | Again output is appended to *Out* and errors are logged in *Log*. | |
723 | ||
724 | This utility is very robust and pretty smart about determining the | |
725 | type of the entry. It includes facilities for expanding refer macros | |
726 | to text, or substituting bibtex macros. Do M-x describe-variable on | |
727 | r2b-journal-abbrevs | |
728 | r2b-booktitle-abbrevs | |
729 | r2b-proceedings-list | |
730 | for information on these features. | |
731 | ||
56c586a8 ER |
732 | Please send bug reports and suggestions to |
733 | Henry Kautz | |
734 | kautz@research.att.com | |
735 | allegra!kautz") | |
a2535589 JA |
736 | |
737 | ||
738 | (defun r2b-help () | |
9a529312 SM |
739 | "Print help describing the `refbib' package." |
740 | (interactive) | |
741 | (with-output-to-temp-buffer "*Help*" | |
742 | (princ r2b-help-message) | |
743 | (with-current-buffer standard-output | |
744 | (help-mode)))) | |
a2535589 | 745 | |
1fc4747e | 746 | (provide 'refbib) |
49116ac0 | 747 | (provide 'refer-to-bibtex) |
6594deb0 | 748 | |
cbee283d | 749 | ;; arch-tag: 664afee2-6e76-4408-ba56-981d8a179586 |
6594deb0 | 750 | ;;; refbib.el ends here |