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