-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from jgru/add-consult-buffer-source
Add consult-org-roam-buffer
- Loading branch information
Showing
3 changed files
with
188 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
;;; consult-org-roam-buffer.el --- Consult-buffer integration for org-roam -*- lexical-binding: t; -*- | ||
;; Copyright (C) 2022 jgru | ||
|
||
;; Author: apc <https://github.com/apc> and jgru <https://github.com/jgru> | ||
;; Created: October 7th, 2022 | ||
;; SPDX-License-Identifier: GPL-3.0-or-later | ||
;; Version: 0.1 | ||
;; Homepage: https://github.com/jgru/consult-org-roam | ||
;; Package-Requires: ((emacs "27.1") (org-roam "2.2.0") (consult "0.16")) | ||
|
||
;;; Commentary: | ||
|
||
;; This is a set of functions to add a custom source to consult-buffer | ||
;; for org-roam notes. | ||
|
||
;;; Code: | ||
|
||
;; ============================================================================ | ||
;;;; Dependencies | ||
;; ============================================================================ | ||
|
||
(require 'org-roam) | ||
(require 'consult) | ||
|
||
;; ============================================================================ | ||
;;;; Customize definitions | ||
;; ============================================================================ | ||
|
||
(defgroup consult-org-roam-buffer nil | ||
"Consult buffer source for org-roam." | ||
:group 'org | ||
:group 'convenience | ||
:prefix "consult-org-roam-buffer-") | ||
|
||
(defcustom consult-org-roam-buffer-narrow-key ?n | ||
"Narrow key for consult-buffer" | ||
:type 'key | ||
:group 'consult-org-roam-buffer) | ||
|
||
(defcustom consult-org-roam-buffer-after-buffers nil | ||
"If non-nil, display org-roam buffers right after non-org-roam buffers. | ||
Otherwise, display org-roam buffers after any other visible default | ||
source") | ||
|
||
;; ============================================================================ | ||
;;;; Functions | ||
;; ============================================================================ | ||
|
||
(defun consult-org-roam-buffer--state () | ||
(let ((preview (consult--buffer-preview))) | ||
(lambda | ||
(action cand) | ||
(funcall preview action | ||
(consult-org-roam-buffer--with-title cand)) | ||
(when | ||
(and cand | ||
(eq action 'return)) | ||
(consult--buffer-action | ||
(consult-org-roam-buffer--with-title cand)))))) | ||
|
||
(defun consult-org-roam-buffer--get-title (buffer) | ||
"Get title of org-roam BUFFER." | ||
(if (org-roam-buffer-p buffer) | ||
(let* ((title | ||
(with-current-buffer buffer | ||
(org-roam-db--file-title))) | ||
(filename (buffer-file-name buffer)) | ||
(fhash (consult-org-roam-db--file-hash filename))) | ||
(if fhash | ||
(progn | ||
;; Add hash to differentiate between notes with identical | ||
;; titles but make it invisible to not disturb the user | ||
(add-text-properties 0 (length fhash) '(invisible t) fhash) | ||
(concat title fhash)) | ||
;; Handle edge cases where the org-roam buffer has not yet | ||
;; been written to disk (and DB) | ||
(concat title " [File deleted]"))))) | ||
|
||
(defun consult-org-roam-db--file-hash (fname) | ||
"Retrieve the hash of FNAME from org-roam's db " | ||
(let* ((fhash (org-roam-db-query | ||
[:select [hash] | ||
:from files | ||
:where (like file $s1) | ||
] | ||
(concat "%" fname)))) | ||
(car (car fhash)))) | ||
|
||
(defun consult-org-roam-buffer--add-title (buffer) | ||
"Build a cons consisting of the BUFFER title and the BUFFER name" | ||
(cons (consult-org-roam-buffer--get-title buffer) buffer)) | ||
|
||
(defun consult-org-roam-buffer--update-open-buffer-list () | ||
"Generate an alist of the form `(TITLE . BUF)’, where TITLE is the title of an open org-roam buffer" | ||
(setq org-roam-buffer-open-buffer-list | ||
(mapcar #'consult-org-roam-buffer--add-title | ||
(org-roam-buffer-list)))) | ||
|
||
(defun consult-org-roam-buffer--with-title (title) | ||
"Find buffer name with TITLE from among the list of open org-roam buffers" | ||
(consult-org-roam-buffer--update-open-buffer-list) | ||
(cdr (assoc title org-roam-buffer-open-buffer-list))) | ||
|
||
(defun consult-org-roam-buffer--get-roam-bufs () | ||
"Return list of currently open org-roam buffers" | ||
(consult--buffer-query | ||
:sort 'visibility | ||
:as #'consult-org-roam-buffer--get-title | ||
:filter t | ||
:predicate (lambda (buf) (org-roam-buffer-p buf)))) | ||
|
||
;; Define source for consult-buffer | ||
(defvar org-roam-buffer-source | ||
`(:name "Org-roam" | ||
:hidden nil | ||
:narrow ,consult-org-roam-buffer-narrow-key | ||
:category org-roam-buffer | ||
:annotate ,(lambda (cand) | ||
(f-filename | ||
(buffer-file-name | ||
(consult-org-roam-buffer--with-title cand)))) | ||
:state ,#'consult-org-roam-buffer--state | ||
:items ,#'consult-org-roam-buffer--get-roam-bufs)) | ||
|
||
(defun consult-org-roam-buffer-setup () | ||
;; Remove potentially org-roam-buffer-source to avoid duplicate | ||
(setq consult-buffer-sources | ||
(delete 'org-roam-buffer-source consult-buffer-sources)) | ||
(if consult-org-roam-buffer-after-buffers | ||
(let* ((idx (cl-position 'consult--source-buffer consult-buffer-sources :test 'equal)) | ||
(tail (nthcdr idx consult-buffer-sources))) | ||
(setcdr | ||
(nthcdr (1- idx) consult-buffer-sources) | ||
(append (list 'org-roam-buffer-source) tail))) | ||
(add-to-list 'consult-buffer-sources 'org-roam-buffer-source 'append))) | ||
|
||
(eval-after-load | ||
;; Customize consult--source-buffer to show org-roam buffers only in | ||
;; their dedicated section | ||
(consult-customize | ||
consult--source-buffer | ||
:items (lambda () | ||
(consult--buffer-query | ||
:sort 'visibility | ||
:as #'buffer-name | ||
:predicate (lambda (buf) (not (org-roam-buffer-p buf)))))) | ||
|
||
(consult-org-roam-buffer-setup)) | ||
|
||
(provide 'consult-org-roam-buffer) | ||
;;; consult-org-roam-buffer.el ends here |
This file contains 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
This file contains 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