Last active
April 17, 2022 20:07
-
-
Save ademberbic/f99a15dc4a38d1dedd3235b26ae6828c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'citar) | |
(defvar bibtex-note-org-property "Key" | |
"The org-mode property which BibTeX keys are attached to") | |
(defun get-first-citar-notes-path () | |
"Return 'citar-notes-paths' if it is a string, or its car | |
if it is a list (as 'citar' normally expects)" | |
(cond ((stringp citar-notes-paths) citar-notes-paths) | |
((listp citar-notes-paths) (car citar-notes-paths)) | |
(t (error "'citar-notes-paths' is neither a string, nor a list")))) | |
(defun open-bibtex-note-from-citar (key entry) | |
"Open or create a note for bibtex key KEY" | |
(find-file (get-first-citar-notes-path)) | |
(outline-show-all) | |
(save-buffer) ; So we can revert if user chooses C-c C-k | |
(let ((pos (org-find-property bibtex-note-org-property key))) | |
(if pos | |
; Heading exists for key: go to it | |
(goto-char pos) | |
; Heading doesn't exist for key: create it | |
(end-of-buffer) | |
(org-insert-heading nil nil t) | |
(save-excursion | |
; Citar can format the heading text for us | |
(insert (citar--format-entry-no-widths | |
entry | |
(citar-get-template 'note))) | |
; Store the BibTeX key as a property | |
(org-entry-put (point) bibtex-note-org-property key)))) | |
; TODO More "org-like" spawnng of notes window | |
(org-narrow-to-subtree) | |
(edit-bibtex-note-mode 1) | |
(end-of-buffer)) | |
(define-minor-mode edit-bibtex-note-mode | |
"'org-capture'-like minor mode for managing bibtex notes" | |
:keymap (let ((map (make-sparse-keymap))) | |
(define-key map (kbd "C-c C-c") 'finish-bibtex-note-buffer) | |
(define-key map (kbd "C-c C-k") 'abort-bibtex-note-buffer) | |
map) | |
(setq-local | |
header-line-format | |
(substitute-command-keys | |
" Finish \\[finish-bibtex-note-buffer], abort \\[abort-bibtex-note-buffer]"))) | |
(defun close-bibtex-note-buffer (abort) | |
"Closes the org-capture-esque BibTex note editor window, | |
saving if ABORT is nil and reverting is t" | |
(widen) | |
(edit-bibtex-note-mode -1) | |
(setq-local header-line-format nil) | |
(if abort | |
(revert-buffer t t) | |
(save-buffer)) | |
(if (not (one-window-p)) | |
(delete-window) | |
(switch-to-buffer (other-buffer)))) | |
(defun finish-bibtex-note-buffer () | |
"Save note buffer and close window" | |
(interactive) | |
(close-bibtex-note-buffer nil)) | |
(defun abort-bibtex-note-buffer () | |
"Revert note buffer and close window" | |
(interactive) | |
(close-bibtex-note-buffer t)) | |
(defun citar-has-note () | |
"Overwrites 'citar' function testing if KEY has note" | |
(lambda (key entry) | |
(with-temp-buffer | |
(insert-file-contents (get-first-citar-notes-path)) | |
(org-find-property bibtex-note-org-property key)))) | |
(provide 'citar-one-file-for-notes) | |
;; init.el: | |
;; (require 'citar-one-file-for-notes "XXX/citar-one-file-for-notes.el") | |
;; (setq citar-notes-paths '("XXX/notes.org") | |
;; citar-open-note-function 'open-bibtex-note-from-citar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment