refbib.el 24 KB

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