oc-natbib.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ;;; oc-natbib.el --- Citation processor using natbib LaTeX package -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;; This library registers the `natbib' citation processor, which provides the
  16. ;; "export" capability for citations.
  17. ;; The processor relies on "natbib" LaTeX package. As such it ensures that the
  18. ;; package is properly required in the document's preamble. More accurately, it
  19. ;; will use any "\\usepackage{natbib}" command already present in the document
  20. ;; (e.g., through `org-latex-packages-alist'), or insert one using options
  21. ;; defined in `org-cite-natbib-options'.
  22. ;; It supports the following citation styles:
  23. ;;
  24. ;; - author (a), including caps (c), and full (f) variants,
  25. ;; - noauthor (na), including bare (b) variant,
  26. ;; - text (t), including bare (b), caps (c), full (f), bare-caps (bc),
  27. ;; bare-full (bf), caps-full (cf), and bare-caps-full (bcf) variants,
  28. ;; - default, including bare (b), caps (c), full (f), bare-caps (bc),
  29. ;; bare-full (bf), caps-full (cf), and bare-caps-full (bcf) variants.
  30. ;; Bibliography accepts any style supported by "natbib" package.
  31. ;;; Code:
  32. (require 'oc)
  33. (declare-function org-element-property "org-element" (property element))
  34. (declare-function org-export-data "org-export" (data info))
  35. ;;; Customization
  36. (defcustom org-cite-natbib-options nil
  37. "List of options added to \"natbib\" package.
  38. If \"natbib\" package is already required in the document, e.g., through
  39. `org-latex-packages-alist' variable, these options are ignored."
  40. :group 'org-cite
  41. :package-version '(Org . "9.5")
  42. :type
  43. '(set
  44. (const :tag "use round parentheses (default)" round)
  45. (const :tag "use square brackets" square)
  46. (const :tag "use curly braces" curly)
  47. (const :tag "use angle brackets" angle)
  48. (const :tag "separate multiple citations with colons (default)" colon)
  49. (const :tag "separate multiple citations with comas" comma)
  50. (const :tag "generate author-year citations" authoryear)
  51. (const :tag "generate numerical citations" numbers)
  52. (const :tag "generate superscripted numerical citations" super)
  53. (const :tag "order multiple citations according to the list of references" sort)
  54. (const :tag "order as above, but numerical citations are compressed if possible" sort&compress)
  55. (const :tag "display full author list on first citation, abbreviate the others" longnamesfirst)
  56. (const :tag "redefine \\thebibliography to issue \\section* instead of \\chapter*" sectionbib)
  57. (const :tag "keep all the authors' names in a citation on one line" nonamebreak))
  58. :safe t)
  59. ;;; Internal functions
  60. (defun org-cite-natbib--style-to-command (style)
  61. "Return command name to use according to STYLE pair."
  62. (pcase style
  63. ;; "author" style.
  64. (`(,(or "author" "a") . ,(or "caps" "c")) "\\Citeauthor")
  65. (`(,(or "author" "a") . ,(or "full" "f")) "\\citeauthor*")
  66. (`(,(or "author" "a") . ,_) "\\citeauthor")
  67. ;; "noauthor" style.
  68. (`(,(or "noauthor" "na") . ,(or "bare" "b")) "\\citeyear")
  69. (`(,(or "noauthor" "na") . ,_) "\\citeyearpar")
  70. ;; "nocite" style.
  71. (`(,(or "nocite" "n") . ,_) "\\nocite")
  72. ;; "text" style.
  73. (`(,(or "text" "t") . ,(or "bare" "b")) "\\citealt")
  74. (`(,(or "text" "t") . ,(or "caps" "c")) "\\Citet")
  75. (`(,(or "text" "t") . ,(or "full" "f")) "\\citet*")
  76. (`(,(or "text" "t") . ,(or "bare-caps" "bc")) "\\Citealt")
  77. (`(,(or "text" "t") . ,(or "bare-full" "bf")) "\\citealt*")
  78. (`(,(or "text" "t") . ,(or "caps-full" "cf")) "\\Citet*")
  79. (`(,(or "text" "t") . ,(or "bare-caps-full" "bcf")) "\\Citealt*")
  80. (`(,(or "text" "t") . ,_) "\\citet")
  81. ;; Default ("nil") style.
  82. (`(,_ . ,(or "bare" "b")) "\\citealp")
  83. (`(,_ . ,(or "caps" "c")) "\\Citep")
  84. (`(,_ . ,(or "full" "f")) "\\citep*")
  85. (`(,_ . ,(or "bare-caps" "bc")) "\\Citealp")
  86. (`(,_ . ,(or "bare-full" "bf")) "\\citealp*")
  87. (`(,_ . ,(or "caps-full" "cf")) "\\Citep*")
  88. (`(,_ . ,(or "bare-caps-full" "bcf")) "\\Citealp*")
  89. (`(,_ . ,_) "\\citep")
  90. ;; This should not happen.
  91. (_ (error "Invalid style: %S" style))))
  92. (defun org-cite-natbib--build-optional-arguments (citation info)
  93. "Build optional arguments for citation command.
  94. CITATION is the citation object. INFO is the export state, as a property list."
  95. (let* ((origin (pcase (org-cite-get-references citation)
  96. (`(,reference) reference)
  97. (_ citation)))
  98. (suffix (org-element-property :suffix origin))
  99. (prefix (org-element-property :prefix origin)))
  100. (concat (and prefix (format "[%s]" (org-trim (org-export-data prefix info))))
  101. (cond
  102. (suffix (format "[%s]" (org-trim (org-export-data suffix info))))
  103. (prefix "[]")
  104. (t nil)))))
  105. (defun org-cite-natbib--build-arguments (citation)
  106. "Build arguments for citation command for CITATION object."
  107. (format "{%s}"
  108. (mapconcat #'identity
  109. (org-cite-get-references citation t)
  110. ",")))
  111. ;;; Export capability
  112. (defun org-cite-natbib-export-bibliography (_keys files style &rest _)
  113. "Print references from bibliography FILES.
  114. FILES is a list of absolute file names. STYLE is the bibliography style, as
  115. a string or nil."
  116. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  117. (format "\\bibliography{%s}"
  118. (mapconcat #'file-name-sans-extension
  119. files
  120. ","))))
  121. (defun org-cite-natbib-export-citation (citation style _ info)
  122. "Export CITATION object.
  123. STYLE is the citation style, as a pair of strings or nil. INFO is the export
  124. state, as a property list."
  125. (concat (org-cite-natbib--style-to-command style)
  126. (org-cite-natbib--build-optional-arguments citation info)
  127. (org-cite-natbib--build-arguments citation)))
  128. (defun org-cite-natbib-use-package (output &rest _)
  129. "Ensure output requires \"natbib\" package.
  130. OUTPUT is the final output of the export process."
  131. (with-temp-buffer
  132. (save-excursion (insert output))
  133. (when (search-forward "\\begin{document}" nil t)
  134. ;; Ensure there is a \usepackage{natbib} somewhere or add one.
  135. (goto-char (match-beginning 0))
  136. (let ((re (rx "\\usepackage" (opt "[" (*? nonl) "]") "{natbib}")))
  137. (unless (re-search-backward re nil t)
  138. (insert
  139. (format "\\usepackage%s{natbib}\n"
  140. (if (null org-cite-natbib-options)
  141. ""
  142. (format "[%s]"
  143. (mapconcat #'symbol-name
  144. org-cite-natbib-options
  145. ","))))))))
  146. (buffer-string)))
  147. ;;; Register `natbib' processor
  148. (org-cite-register-processor 'natbib
  149. :export-bibliography #'org-cite-natbib-export-bibliography
  150. :export-citation #'org-cite-natbib-export-citation
  151. :export-finalizer #'org-cite-natbib-use-package
  152. :cite-styles
  153. '((("author" "a") ("caps" "a") ("full" "f"))
  154. (("noauthor" "na") ("bare" "b"))
  155. (("text" "t")
  156. ("bare" "b") ("caps" "c") ("full" "f") ("bare-caps" "bc")
  157. ("bare-full" "bf") ("caps-full" "cf") ("bare-caps-full" "bcf"))
  158. (("nil")
  159. ("bare" "b") ("caps" "c") ("full" "f") ("bare-caps" "bc")
  160. ("bare-full" "bf") ("caps-full" "cf") ("bare-caps-full" "bcf"))))
  161. (provide 'org-cite-natbib)
  162. (provide 'oc-natbib)
  163. ;;; oc-natbib.el ends here