oc-bibtex.el 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ;;; oc-bibtex.el --- Vanilla citation processor for LaTeX -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021-2022 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; This file is part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library registers the `bibtex' citation processor, which
  17. ;; provides the "export" capability for citations. It doesn't require
  18. ;; any LaTeX package.
  19. ;;
  20. ;; It supports the following citation styles:
  21. ;;
  22. ;; - nocite (n),
  23. ;; - default.
  24. ;;
  25. ;; Only suffixes are supported. Prefixes are ignored.
  26. ;;
  27. ;; Bibliography should consist of ".bib" files only.
  28. ;;; Code:
  29. (require 'oc)
  30. (declare-function org-element-property "org-element" (property element))
  31. (declare-function org-export-data "org-export" (data info))
  32. ;;; Export capability
  33. (defun org-cite-bibtex-export-bibliography (_keys files style &rest _)
  34. "Print references from bibliography FILES.
  35. FILES is a list of absolute file names. STYLE is the bibliography style, as
  36. a string or nil."
  37. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  38. (format "\\bibliography{%s}"
  39. (mapconcat #'file-name-sans-extension
  40. files
  41. ","))))
  42. (defun org-cite-bibtex-export-citation (citation style _ info)
  43. "Export CITATION object.
  44. STYLE is the citation style, as a pair of strings or nil. INFO is the export
  45. state, as a property list."
  46. (let ((references (org-cite-get-references citation)))
  47. (format "\\%s%s{%s}"
  48. (pcase style
  49. (`(,(or "nocite" "n") . ,_) "nocite")
  50. (_ "cite"))
  51. (let ((suffix (cdr (org-cite-main-affixes citation))))
  52. (if suffix
  53. (format "[%s]" (org-trim (org-export-data suffix info)))
  54. ""))
  55. (mapconcat (lambda (r) (org-element-property :key r))
  56. references
  57. ","))))
  58. ;;; Register `bibtex' processor
  59. (org-cite-register-processor 'bibtex
  60. :export-bibliography #'org-cite-bibtex-export-bibliography
  61. :export-citation #'org-cite-bibtex-export-citation
  62. :cite-styles
  63. '((("nocite" "n"))
  64. (("nil"))))
  65. (provide 'oc-bibtex)
  66. ;;; oc-bibtex.el ends here