Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 rivejä
10 KiB

4 vuotta sitten
  1. ;;; helm-bibtex.el --- A bibliography manager based on Helm
  2. ;; Author: Titus von der Malsburg <malsburg@posteo.de>
  3. ;; Maintainer: Titus von der Malsburg <malsburg@posteo.de>
  4. ;; Version: 2.0.0
  5. ;; Package-Requires: ((helm "1.5.5") (parsebib "1.0") (s "1.9.0") (dash "2.6.0") (f "0.16.2") (cl-lib "0.5") (biblio "0.2"))
  6. ;; This program is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; A bibliography manager for Emacs, based on Helm and the
  18. ;; bibtex-completion backend.
  19. ;;
  20. ;; News:
  21. ;; - 09/06/2018: Added virtual APA field `author-or-editor` for use in
  22. ;; notes templates.
  23. ;; - 02/06/2018: Reload bibliography proactively when bib files are
  24. ;; changed.
  25. ;; - 21/10/2017: Added support for multiple PDFs and other file
  26. ;; types. See `bibtex-completion-pdf-extension' and
  27. ;; `bibtex-completion-find-additional-pdfs' for details.
  28. ;; - 10/10/2017: Added support for ~@string~ constants.
  29. ;; - 02/10/2017: Date field is used when year is undefined.
  30. ;; - 29/09/2017: BibTeX entry, citation macro, or org-bibtex entry at
  31. ;; point, will be pre-selected in helm-bibtex and ivy-bibtex giving
  32. ;; quick access to PDFs and other functions.
  33. ;;
  34. ;; See NEWS.org for old news.
  35. ;;
  36. ;; Key features:
  37. ;; - Quick access to your bibliography from within Emacs
  38. ;; - Powerful search capabilities
  39. ;; - Provides instant search results as you type
  40. ;; - Tightly integrated with LaTeX authoring, emails, Org mode, etc.
  41. ;; - Open the PDFs, URLs, or DOIs associated with an entry
  42. ;; - Insert LaTeX cite commands, Ebib links, or Pandoc citations,
  43. ;; BibTeX entries, or plain text references at point, attach PDFs to
  44. ;; emails
  45. ;; - Support for note taking
  46. ;; - Quick access to online bibliographic databases such as Pubmed,
  47. ;; arXiv, Google Scholar, Library of Congress, etc.
  48. ;; - Imports BibTeX entries from CrossRef and other sources.
  49. ;;
  50. ;; See the github page for details:
  51. ;;
  52. ;; https://github.com/tmalsburg/helm-bibtex
  53. ;;; Install:
  54. ;; Put this file in a directory included in your load path or install
  55. ;; helm-bibtex from MELPA (preferred). Then add the following in your
  56. ;; Emacs startup file:
  57. ;;
  58. ;; (require 'helm-bibtex)
  59. ;;
  60. ;; Alternatively, you can use autoload:
  61. ;;
  62. ;; (autoload 'helm-bibtex "helm-bibtex" "" t)
  63. ;;
  64. ;; Requirements are parsebib, helm, s, dash, and f. The easiest way
  65. ;; to install these packages is through MELPA. Make sure helm is
  66. ;; properly configured (see
  67. ;; https://github.com/emacs-helm/helm#install-from-emacs-packaging-system).
  68. ;;
  69. ;; Let helm-bibtex know where it can find your bibliography by setting
  70. ;; the variable `bibtex-completion-bibliography'. See the manual for
  71. ;; more details:
  72. ;;
  73. ;; https://github.com/tmalsburg/helm-bibtex#minimal-configuration
  74. ;;; Usage:
  75. ;; You can search entries using the command `helm-bibtex'. Select an
  76. ;; entry and press TAB to access all available actions. At the end of
  77. ;; the list of matches you find some dummy entries that can be used
  78. ;; for searching in online databases. Apart from that, familiarize
  79. ;; yourself with Helm. It's more powerful that you might think.
  80. ;;; Code:
  81. (require 'helm)
  82. (require 'helm-net)
  83. (require 'helm-easymenu)
  84. (require 'bibtex-completion)
  85. ;; Silence the byte compiler
  86. (eval-when-compile (defvar helm-bibtex-local-bib))
  87. ;; The following allows people to continue using their old helm-bibtex
  88. ;; configurations:
  89. (cl-loop
  90. for var in '("bibliography" "library-path" "pdf-open-function"
  91. "pdf-symbol" "format-citation-functions" "notes-path"
  92. "notes-template-multiple-files"
  93. "notes-template-one-file" "notes-key-pattern"
  94. "notes-extension" "notes-symbol" "fallback-options"
  95. "browser-function" "additional-search-fields"
  96. "no-export-fields" "cite-commands"
  97. "cite-default-command"
  98. "cite-prompt-for-optional-arguments"
  99. "cite-default-as-initial-input" "pdf-field")
  100. for oldvar = (intern (concat "helm-bibtex-" var))
  101. for newvar = (intern (concat "bibtex-completion-" var))
  102. do
  103. (define-obsolete-variable-alias oldvar newvar "2016-03-20"))
  104. ;; Helm-specific configurations:
  105. (defcustom helm-bibtex-full-frame t
  106. "Non-nil means open `helm-bibtex' using the entire window.
  107. When nil, the window will split below."
  108. :group 'bibtex-completion
  109. :type 'boolean)
  110. (easy-menu-add-item nil '("Tools" "Helm" "Tools") ["BibTeX" helm-bibtex t])
  111. ;; Candidate formatter:
  112. ;; The function `window-width' does not necessarily report the correct
  113. ;; number of characters that fit on a line. This is a
  114. ;; work-around. See also this bug report:
  115. ;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=19395
  116. (defun helm-bibtex-window-width ()
  117. "Return the width of the window to pass to `helm-bibtex-candidates-formatter'."
  118. (1- (window-body-width)))
  119. (defun helm-bibtex-candidates-formatter (candidates _)
  120. "Format CANDIDATES for display in helm."
  121. (cl-loop
  122. with width = (with-helm-window (helm-bibtex-window-width))
  123. for entry in candidates
  124. for entry = (cdr entry)
  125. for entry-key = (bibtex-completion-get-value "=key=" entry)
  126. collect (cons (bibtex-completion-format-entry entry width) entry-key)))
  127. ;; Warp bibtex-completion actions with some helm-specific code:
  128. (defmacro helm-bibtex-helmify-action (action name)
  129. "Wrap ACTION in another function NAME.
  130. Then pass the candidates marked in helm to ACTION. Also uses
  131. `with-helm-current-buffer' such that when ACTION inserts text it
  132. comes out in the right buffer."
  133. `(defun ,name (_)
  134. (let ((keys (helm-marked-candidates :with-wildcard t)))
  135. (with-helm-current-buffer
  136. (,action keys)))))
  137. (helm-bibtex-helmify-action bibtex-completion-open-pdf helm-bibtex-open-pdf)
  138. (helm-bibtex-helmify-action bibtex-completion-open-url-or-doi helm-bibtex-open-url-or-doi)
  139. (helm-bibtex-helmify-action bibtex-completion-open-any helm-bibtex-open-any)
  140. (helm-bibtex-helmify-action bibtex-completion-insert-citation helm-bibtex-insert-citation)
  141. (helm-bibtex-helmify-action bibtex-completion-insert-reference helm-bibtex-insert-reference)
  142. (helm-bibtex-helmify-action bibtex-completion-insert-key helm-bibtex-insert-key)
  143. (helm-bibtex-helmify-action bibtex-completion-insert-bibtex helm-bibtex-insert-bibtex)
  144. (helm-bibtex-helmify-action bibtex-completion-add-PDF-attachment helm-bibtex-add-PDF-attachment)
  145. (helm-bibtex-helmify-action bibtex-completion-edit-notes helm-bibtex-edit-notes)
  146. (helm-bibtex-helmify-action bibtex-completion-show-entry helm-bibtex-show-entry)
  147. (helm-bibtex-helmify-action bibtex-completion-add-pdf-to-library helm-bibtex-add-pdf-to-library)
  148. ;; Helm sources:
  149. (defvar helm-source-bibtex
  150. (helm-build-sync-source "BibTeX entries"
  151. :header-name (lambda (name)
  152. (format "%s%s: " name (if helm-bibtex-local-bib " (local)" "")))
  153. :candidates 'helm-bibtex-candidates
  154. :filtered-candidate-transformer 'helm-bibtex-candidates-formatter
  155. :action (helm-make-actions
  156. "Open PDF, URL or DOI" 'helm-bibtex-open-any
  157. "Open URL or DOI in browser" 'helm-bibtex-open-url-or-doi
  158. "Insert citation" 'helm-bibtex-insert-citation
  159. "Insert reference" 'helm-bibtex-insert-reference
  160. "Insert BibTeX key" 'helm-bibtex-insert-key
  161. "Insert BibTeX entry" 'helm-bibtex-insert-bibtex
  162. "Attach PDF to email" 'helm-bibtex-add-PDF-attachment
  163. "Edit notes" 'helm-bibtex-edit-notes
  164. "Show entry" 'helm-bibtex-show-entry
  165. "Add PDF to library" 'helm-bibtex-add-pdf-to-library))
  166. "Source for searching in BibTeX files.")
  167. (defvar helm-source-fallback-options
  168. '((name . "Fallback options")
  169. (match (lambda (_candidate) t))
  170. (candidates . bibtex-completion-fallback-candidates)
  171. (no-matchplugin)
  172. (nohighlight)
  173. (action . (lambda (candidate) (bibtex-completion-fallback-action candidate helm-pattern))))
  174. "Source for online look-up.")
  175. ;; Helm-bibtex command:
  176. ;;;###autoload
  177. (defun helm-bibtex (&optional arg local-bib input)
  178. "Search BibTeX entries.
  179. With a prefix ARG, the cache is invalidated and the bibliography
  180. reread.
  181. If LOCAL-BIB is non-nil, display that the BibTeX entries are read
  182. from the local bibliography. This is set internally by
  183. `helm-bibtex-with-local-bibliography'.
  184. If INPUT is non-nil and a string, that value is going to be used
  185. as a predefined search term. Can be used to define functions for
  186. frequent searches (e.g. your own publications)."
  187. (interactive "P")
  188. (when arg
  189. (bibtex-completion-clear-cache))
  190. (bibtex-completion-init)
  191. (let* ((candidates (bibtex-completion-candidates))
  192. (key (bibtex-completion-key-at-point))
  193. (preselect (and key
  194. (cl-position-if (lambda (cand)
  195. (member (cons "=key=" key)
  196. (cdr cand)))
  197. candidates))))
  198. (helm :sources (list helm-source-bibtex helm-source-fallback-options)
  199. :full-frame helm-bibtex-full-frame
  200. :buffer "*helm bibtex*"
  201. :input input
  202. :preselect (lambda ()
  203. (and preselect
  204. (> preselect 0)
  205. (helm-next-line preselect)))
  206. :candidate-number-limit (max 500 (1+ (or preselect 0)))
  207. :bibtex-candidates candidates
  208. :bibtex-local-bib local-bib)))
  209. ;;;###autoload
  210. (defun helm-bibtex-with-local-bibliography (&optional arg)
  211. "Search BibTeX entries with local bibliography.
  212. With a prefix ARG the cache is invalidated and the bibliography
  213. reread."
  214. (interactive "P")
  215. (let* ((local-bib (bibtex-completion-find-local-bibliography))
  216. (bibtex-completion-bibliography (or local-bib
  217. bibtex-completion-bibliography)))
  218. (helm-bibtex arg local-bib)))
  219. (provide 'helm-bibtex)
  220. ;; Local Variables:
  221. ;; byte-compile-warnings: (not cl-functions obsolete)
  222. ;; coding: utf-8
  223. ;; indent-tabs-mode: nil
  224. ;; End:
  225. ;;; helm-bibtex.el ends here