oc-bibtex.el 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 'org-macs)
  30. (org-assert-version)
  31. (require 'oc)
  32. (declare-function org-element-property "org-element" (property element))
  33. (declare-function org-export-data "org-export" (data info))
  34. ;;; Export capability
  35. (defun org-cite-bibtex-export-bibliography (_keys files style &rest _)
  36. "Print references from bibliography FILES.
  37. FILES is a list of absolute file names. STYLE is the bibliography style, as
  38. a string or nil."
  39. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  40. (format "\\bibliography{%s}"
  41. (mapconcat #'file-name-sans-extension
  42. files
  43. ","))))
  44. (defun org-cite-bibtex-export-citation (citation style _ info)
  45. "Export CITATION object.
  46. STYLE is the citation style, as a pair of strings or nil. INFO is the export
  47. state, as a property list."
  48. (let ((references (org-cite-get-references citation)))
  49. (format "\\%s%s{%s}"
  50. (pcase style
  51. (`(,(or "nocite" "n") . ,_) "nocite")
  52. (_ "cite"))
  53. (let ((suffix (cdr (org-cite-main-affixes citation))))
  54. (if suffix
  55. (format "[%s]" (org-trim (org-export-data suffix info)))
  56. ""))
  57. (mapconcat (lambda (r) (org-element-property :key r))
  58. references
  59. ","))))
  60. ;;; Register `bibtex' processor
  61. (org-cite-register-processor 'bibtex
  62. :export-bibliography #'org-cite-bibtex-export-bibliography
  63. :export-citation #'org-cite-bibtex-export-citation
  64. :cite-styles
  65. '((("nocite" "n"))
  66. (("nil"))))
  67. (provide 'oc-bibtex)
  68. ;;; oc-bibtex.el ends here