Skip to content

Questions about a consult-notes command attempt #210

@hmelman

Description

@hmelman

I'm trying to use consult--multi to manage several directories of notes files. I have the following:

(defun hrm-notes-closure (dir)
  "Return a function for an hrm-notes source rooted in DIR.
The function called with no arg returns a list of filenames in DIR.
Called with an arg f opens the file in DIR."
  (lambda (&optional f)
    (if f
	(find-file (expand-file-name f dir))
      (directory-files dir))))

(defvar hrm-notes-restaurants-function (hrm-notes-closure "~/Dropbox/Restaurants/"))
(defvar hrm-notes-lectures-function (hrm-notes-closure "~/Dropbox/Lectures/"))

(defvar hrm-notes-restaurants-source
  `(:name     "Restaurants"
    :narrow   ?r
    :category file
    :face     consult-file
    :history  hrm-notes-history
    :items    ,hrm-notes-restaurants-function
    :action   ,hrm-notes-restaurants-function))

(defvar hrm-notes-lecture-source
  `(:name     "Lectures"
    :narrow   ?l
    :category file
    :face     consult-file
    :history  hrm-notes-history
    :items    ,hrm-notes-lectures-function
    :action   ,hrm-notes-lectures-function))

(defvar hrm-notes-sources
  (list 'hrm-notes-restaurants-source
	'hrm-notes-lecture-source))

(defvar hrm-notes-history nil
  "History variable for hrm-notes.")

(defun hrm-notes ()
  "Find a file in a notes directory."
  (interactive)
  (consult--multi
   hrm-notes-sources
   :prompt "Notes File: "
   :history 'hrm-notes-history))

First off, it works, but do you see any problems in the above?

I'd like to figure out how to add annotations to these. At first just the source name and then maybe dates and sizes like marginalia does for files. I couldn't get that going, even for just the simple fixed string case. I tried adding a :annotate to the source and to the consult--multi call with a function that returned ("Restaurants") but it didn't show. For file attributes I'll have another problem as the default-directory will be different. Do you have a recommendation for how to get it to the annotation function? I could make a different one for each source, or store it in a property. It wasn't clear to me if the return of :items had to be just a list of strings or if like candidates it could be a list of cons with a string and value that could be the full path.

I had tried before but was confused by how to ultimately call find-file, so the addition of the :action param definitely helped me. I think I now get how :state and "restore" works with the new description. IIRC I'd probably rename "restore" to "final" and say the final call is used to restore things if previewing has occurred and then to perform the desired action.

When I get this done I'll add it to wiki as an example and I'll give comments on consult--multi documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions