mclear-tools / consult-notes

Use consult to search notes
GNU General Public License v3.0
164 stars 15 forks source link

Not sure how to handle Denote silos and consult-notes-denote-mode #39

Closed pdxmph closed 1 year ago

pdxmph commented 1 year ago

I have three denote silos and I'm not sure how to find the right balance of features and flexibility:

I have Denote configured with denote-directory set to my main silo.

So I tried configuring consult-notes-file-dir-sources and left consult-notes-denote-mode on:

What I would desire is:

I don't know how to configure that: consult-notes-file-dir-sources seems to exist independent of consult-notes-denote-mode (or vice versa)? It is not taken into account by consult-notes-denote-mode.

mclearc commented 1 year ago

Denote doesn't give us a lot of options for handling this. There may well be a more elegant solution, but I think the following works for your particular use case.

The basic idea is that you create a variable for each silo path as a separate source, like so:

(setq silo1 "path/to/denote-silo-1/"
      silo2 "path/to/denote-silo-2/")

Then you define a consult source:

(defvar consult-notes-denote--silo1
  (list :name     (propertize "Silo 1" 'face 'consult-notes-sep)
        :narrow   ?x
        :category 'consult-notes-category
        :annotate #'consult-notes-denote--annotate
        :items    (lambda ()
                    (let* ((max-width 0)
                           (denote-directory silo1)
                           (cands (mapcar (lambda (f)
                                            (let* ((id (denote-retrieve-filename-identifier f))
                                                   (title-1 (or (denote-retrieve-title-value f (denote-filetype-heuristics f)) (denote-retrieve-filename-title f)))
                                                   (title (if consult-notes-denote-display-id
                                                              (concat id " " title-1)
                                                            title-1))
                                                   (dir (file-relative-name (file-name-directory f) denote-directory))
                                                   (keywords (denote-extract-keywords-from-path f)))
                                              (let ((current-width (string-width title)))
                                                (when (> current-width max-width)
                                                  (setq max-width (+ 24 current-width))))
                                              (propertize title 'denote-path f 'denote-keywords keywords)))
                                          (funcall consult-notes-denote-files-function))))
                      (mapcar (lambda (c)
                                (let* ((keywords (get-text-property 0 'denote-keywords c))
                                       (path (get-text-property 0 'denote-path c))
                                       (dirs (directory-file-name (file-relative-name (file-name-directory path) denote-directory))))
                                  (concat c
                                          ;; align keywords
                                          (propertize " " 'display `(space :align-to (+ left ,(+ 2 max-width))))
                                          (format "%18s"
                                                  (if keywords
                                                      (concat (propertize "#" 'face 'consult-notes-name)
                                                              (propertize (mapconcat 'identity keywords " ") 'face 'consult-notes-name))
                                                    ""))
                                          (when consult-notes-denote-dir (format "%18s" (propertize (concat "/" dirs) 'face 'consult-notes-name))))))
                              cands)))
        ;; Custom preview
        :state  #'consult-notes-denote--state
        ;; Create new note on match fail
        :new     #'consult-notes-denote--new-note))

Then you add that source:

         (add-to-list 'consult-notes-all-sources 'consult-notes-denote--silo1 'append)

to make this easier define the lambda as a function so you can just easily call it instead of repeating all the code.

I did some limited testing and the above all seems to work fine, and is compatible with using consult-notes for non-denote dirs and org-files, etc.

pdxmph commented 1 year ago

Wow. I set up my three silos using this, and it all worked exactly as I hoped it would.

Thank you so much!