oc-bibtex.el 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;; oc-bibtex.el --- Vanilla citation processor for LaTeX -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021 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. (declare-function org-export-data "org-export" (data info))
  30. ;;; Export capability
  31. (defun org-cite-bibtex-export-bibliography (_keys files style &rest _)
  32. "Print references from bibliography FILES.
  33. FILES is a list of absolute file names. STYLE is the bibliography style, as
  34. a string or nil."
  35. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  36. (format "\\bibliography{%s}"
  37. (mapconcat #'file-name-sans-extension
  38. files
  39. ","))))
  40. (defun org-cite-bibtex-export-citation (citation style _ info)
  41. "Export CITATION object.
  42. STYLE is the citation style, as a pair of strings or nil. INFO is the export
  43. state, as a property list."
  44. (let ((references (org-cite-get-references citation)))
  45. (format "\\%s%s{%s}"
  46. (pcase style
  47. (`(,(or "nocite" "n") . ,_) "nocite")
  48. (_ "cite"))
  49. (let ((suffix
  50. (org-element-property :suffix
  51. (pcase references
  52. (`(,ref) ref)
  53. (_ 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