Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign structure of org-roam-bibtex package #58

Merged
merged 1 commit into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions orb-core.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
;;; orb-core.el --- Org Roam Bibtex: Core library -*- coding: utf-8; lexical-binding: t -*-

;; Copyright © 2020 Mykhailo Shevchuk <[email protected]>
;; Copyright © 2020 Leo Vivier <[email protected]>

;; Author: Mykhailo Shevchuk <[email protected]>
;; Leo Vivier <[email protected]>
;; URL: https://github.com/org-roam/org-roam-bibtex
;; Keywords: org-mode, roam, convenience, bibtex, helm-bibtex, ivy-bibtex, org-ref
;; Version: 0.2.3

;; This file is NOT part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:
;;
;; This file provides org-roam-bibtex' dependencies and thus should
;; normally be required by org-roam-bibtex feature libraries. It
;; defines customize groups and provides general utility functions
;; that depend on extra features provided through org-roam,
;; bibtex-completion and their dependencies.

;;; Code:
;; * Library requires
;;
;; 1. org-roam requires org, org-ref (with many dependencies),
;; org-element, dash, f, s, emacsql, emacsql-sqlite;
;; 2. bibtex-completion additionally requires parsebib and biblio;
;;
;; So all these libraries are always at our disposal
;;
(require 'org-roam)
(require 'bibtex-completion)

(require 'orb-utils)

;; Customize groups
(defgroup org-roam-bibtex nil
"Org-ref and Bibtex-completion integration for Org-roam."
:group 'org-roam
:prefix "orb-")

(defgroup orb-note-actions nil
"Orb Note Actions - run actions useful in note's context."
:group 'org-roam-bibtex
:prefix "orb-note-actions-")

;;;###autoload
(defun orb-process-file-field (citekey)
"Process the 'file' BibTeX field and resolve if there are multiples.
Search the disk for the document associated with this BibTeX
entry. The disk matching is based on looking in the
`bibtex-completion-library-path' for a file with the
CITEKEY.
\(Mendeley, Zotero, normal paths) are all supported. If there
are multiple files found the user is prompted to select which one
to enter"
(let* ((entry (bibtex-completion-get-entry citekey))
(paths (bibtex-completion-find-pdf entry)))
(if (= (length paths) 1)
(car paths)
(completing-read "File to use: " paths))))

;;;###autoload
(defun orb-find-note-file (citekey)
"Find note file associated from BibTeX’s CITEKEY.
Returns the path to the note file, or nil if it doesn’t exist."
(let* ((completions (org-roam--get-ref-path-completions)))
(plist-get (cdr (assoc citekey completions)) :path)))

(provide 'orb-core)
;;; orb-core.el ends here
;; Local Variables:
;; fill-column: 70
;; End:
75 changes: 37 additions & 38 deletions orb-note-actions.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; orb-note-actions.el --- Connector between Org-roam, BibTeX-completion, and Org-ref -*- coding: utf-8; lexical-binding: t -*-
;;; orb-note-actions.el --- Org Roam Bibtex: Note actions -*- coding: utf-8; lexical-binding: t -*-

;; Copyright © 2020 Mykhailo Shevchuk <[email protected]>
;; Copyright © 2020 Leo Vivier <[email protected]>
Expand Down Expand Up @@ -43,25 +43,17 @@
;;; Code:
;; * Library requires

(require 'org-roam-bibtex)
(require 'orb-core)

(require 'warnings)
(require 'cl-lib)
;; TODO: get rid of after we have our own format function
(require 'org-ref)

(declare-function ido-completing-read "ido")
(declare-function helm "helm")
(declare-function ivy-read "ivy")
(declare-function defhydra "hydra")

;; * Customize definitions

(defgroup orb-note-actions nil
"Orb Note Actions - run actions useful in note's context."
:group 'org-roam-bibtex
:prefix "orb-note-actions-")

(defcustom orb-note-actions-frontend 'default
"Interface frontend for `orb-note-actions'.
Supported values (frontends) are 'default, 'ido, 'hydra, 'ivy and 'helm.
Expand Down Expand Up @@ -130,10 +122,6 @@ constructed from `orb-note-actions-default',
,(format "Provide note actions using %s interface.
CITEKEY is the citekey." (capitalize frontend-name))
(let ((name (org-ref-format-entry citekey)) ;; TODO: make a native format function
;; TODO: this throws an error for an unclear reason
;; (name (bibtex-completion-format-entry
;; (bibtex-completion-get-entry citekey)
;; (window-body-width)))
(candidates
,(unless (eq frontend 'hydra)
'(append orb-note-actions-default
Expand All @@ -151,31 +139,42 @@ CITEKEY is the citekey." (capitalize frontend-name))
(funcall f (list citekey))))

(declare-function orb-note-actions-hydra/body "orb-note-actions" nil t)

(orb-note-actions--frontend! 'hydra
(ignore candidates) ; silence the byte compiler
; for a nice hydra we need
; each group separately
(let ((k ?a)
actions)
(dolist (type (list "Default" "Extra" "User"))
(let ((actions-var (intern (concat "orb-note-actions-" (downcase type)))))
(dolist (action (symbol-value actions-var))
;; this pushes defhydra HEADS list of form:
;; ("a" (some-action citekey-value) "Some-action description" :column "Type")
(cl-pushnew
`(,(format "%c" k) (,(cdr action) (list ,citekey))
,(car action) :column ,(concat type " actions"))
actions)
;; increment key a->b->c...
(setq k (1+ k))))) ; TODO: figure out a way to supply mnemonic keys
(setq actions (nreverse actions))
(eval
`(defhydra orb-note-actions-hydra (:color blue :hint nil)
;; defhydra docstring
,(format "^\n %s \n\n^" (s-word-wrap (- (window-body-width) 2) name))
;; defhydra HEADS
,@actions)))
(orb-note-actions-hydra/body))
;; we don't use candidates here because for a nice hydra we need each
;; group of completions separately (default, extra, user), so just
;; silence the compiler
(if (fboundp 'defhydra)
(progn
(ignore candidates)
(let ((k ?a)
actions)
(dolist (type (list "Default" "Extra" "User"))
(let ((actions-var
(intern (concat "orb-note-actions-" (downcase type)))))
(dolist (action (symbol-value actions-var))
;; this makes defhydra HEADS list of form:
;; ("a" (some-action citekey-value) "Some-action description" :column "Type")
(cl-pushnew
`(,(format "%c" k) (,(cdr action) (list ,citekey))
,(car action) :column ,(concat type " actions"))
actions)
;; increment key a->b->c...
(setq k (1+ k))))) ; TODO: figure out a way to supply
; mnemonic keys
(setq actions (nreverse actions))
;; yes, we redefine hydra on every call
(eval
`(defhydra orb-note-actions-hydra (:color blue :hint nil)
;; defhydra docstring
,(format "^\n %s \n\n^"
(s-word-wrap (- (window-body-width) 2) name))
;; defhydra HEADS
,@actions)))
(orb-note-actions-hydra/body))
(display-warning :warning "You must have Hydra installed to use it! \
Falling back to default.")
(orb-note-actions--default citekey)))

(orb-note-actions--frontend! 'ivy
(if (fboundp 'ivy-read)
Expand Down
14 changes: 8 additions & 6 deletions orb-macs.el → orb-utils.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; orb-macs.el --- Connector between Org-roam, BibTeX-completion, and Org-ref -*- coding: utf-8; lexical-binding: t -*-
;;; orb-utils.el --- Org Roam BibTeX: Utility macros and functions -*- coding: utf-8; lexical-binding: t -*-

;; Copyright © 2020 Mykhailo Shevchuk <[email protected]>
;; Copyright © 2020 Leo Vivier <[email protected]>
Expand All @@ -8,7 +8,7 @@
;; URL: https://github.com/org-roam/org-roam-bibtex
;; Keywords: org-mode, roam, convenience, bibtex, helm-bibtex, ivy-bibtex, org-ref
;; Version: 0.2.3
;; Package-Requires: ((emacs "26.1") (f "0.20.0") (s "1.12.0") (org "9.3") (org-roam "1.0.0") (bibtex-completion "2.0.0"))
;; Package-Requires: ((emacs "26.1"))

;; This file is NOT part of GNU Emacs.

Expand All @@ -29,8 +29,10 @@

;;; Commentary:
;;
;; This file contains macros and helper functions used accross different
;; org-mode-bibtex modules.
;; This file contains utility macros and helper functions used accross
;; different org-mode-bibtex modules. This library may be required
;; directly or through orb-core.el. Definitions in this file should
;; only depend on built-in Emacs libraries.

;;; Code:
;; * Library requires
Expand All @@ -53,8 +55,8 @@ Format is `orb-citekey-format'."
(length orb-citekey-format)))))
(substring citekey beg end)))

(provide 'orb-macs)
;;; orb-macs.el ends here
(provide 'orb-utils)
;;; orb-utils.el ends here
;; Local Variables:
;; fill-column: 70
;; End:
50 changes: 6 additions & 44 deletions org-roam-bibtex.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; org-roam-bibtex.el --- Connector between Org-roam, BibTeX-completion, and Org-ref -*- coding: utf-8; lexical-binding: t -*-
;;; org-roam-bibtex.el --- Org Roam meets BibTeX -*- coding: utf-8; lexical-binding: t -*-

;; Copyright © 2020 Jethro Kuan <[email protected]>
;; Copyright © 2020 Mykhailo Shevchuk <[email protected]>
Expand All @@ -10,7 +10,9 @@
;; URL: https://github.com/org-roam/org-roam-bibtex
;; Keywords: org-mode, roam, convenience, bibtex, helm-bibtex, ivy-bibtex, org-ref
;; Version: 0.2.3
;; Package-Requires: ((emacs "26.1") (f "0.20.0") (s "1.12.0") (org "9.3") (org-roam "1.0.0") (bibtex-completion "2.0.0"))
;; Package-Requires: ((emacs "26.1") (org-roam "1.0.0") (bibtex-completion "2.0.0"))

;; Soft dependencies: projectile, persp-mode, helm, ivy, hydra

;; This file is NOT part of GNU Emacs.

Expand Down Expand Up @@ -65,37 +67,19 @@

;;; Code:
;; * Library requires
(require 'orb-core)

;; We do not require `org-ref' here, because it is too expensive to be
;; loaded unconditionally and the user might not even need
;; it. Instead, we require it in the body of `orb-notes-fn'.

(require 'org-roam)
(require 'bibtex-completion)
(eval-when-compile
(require 'subr-x)
(require 'cl-lib))

(require 'orb-compat)
(require 'orb-macs)

(defvar org-ref-notes-function)

(declare-function org-ref-find-bibliography "org-ref-core")
(declare-function projectile-relevant-open-projects "projectile")
(declare-function persp-switch "persp-mode")
(declare-function persp-names "persp-mode")


;; * Customize definitions

(defgroup org-roam-bibtex nil
"Org-ref and Bibtex-completion integration for Org-roam."
:group 'org-roam
:group 'org-ref
:group 'bibtex-completion
:prefix "orb-")

(defcustom orb-preformat-templates t
"Non-nil to enable template preformatting.
See `orb-edit-notes' for details."
Expand Down Expand Up @@ -238,6 +222,7 @@ extra integration between the two packages.
This is a wrapper function around `orb-edit-notes'
intended for use with Org-ref."
;; org-roam softly requires org-ref, so do we
(when (require 'org-ref nil t)
(let ((bibtex-completion-bibliography (org-ref-find-bibliography)))
(orb-edit-notes citekey))))
Expand All @@ -249,23 +234,6 @@ This function replaces `bibtex-completion-edit-notes'. Only the
first key from KEYS will actually be used."
(orb-edit-notes (car keys)))

;;;###autoload
(defun orb-process-file-field (citekey)
"Process the 'file' BibTeX field and resolve if there are multiples.
Search the disk for the document associated with this BibTeX
entry. The disk matching is based on looking in the
`bibtex-completion-library-path' for a file with the
CITEKEY.
\(Mendeley, Zotero, normal paths) are all supported. If there
are multiple files found the user is prompted to select which one
to enter"
(let* ((entry (bibtex-completion-get-entry citekey))
(paths (bibtex-completion-find-pdf entry)))
(if (= (length paths) 1)
(car paths)
(completing-read "File to use: " paths))))


;; * Helper functions

Expand Down Expand Up @@ -426,12 +394,6 @@ CANDIDATES is a an alist of candidates to consider. Defaults to
(make-sparse-keymap)
"Keymap for function `org-roam-bibtex-mode'.")

(defun orb-find-note-file (citekey)
"Find note file associated from BibTeX’s CITEKEY.
Returns the path to the note file, or nil if it doesn’t exist."
(let* ((completions (org-roam--get-ref-path-completions)))
(plist-get (cdr (assoc citekey completions)) :path)))

;;;###autoload
(define-minor-mode org-roam-bibtex-mode
"Sets `orb-edit-notes' as a function for editing bibliography notes.
Expand Down