-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
vulpea-select.el
184 lines (157 loc) · 5.75 KB
/
vulpea-select.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
;;; vulpea-select.el --- Metadata manipulation -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2020-2021 Boris Buliga
;;
;; Author: Boris Buliga <[email protected]>
;; Maintainer: Boris Buliga <[email protected]>
;;
;; 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 of the
;; License, 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 this program. If not, see
;; <http://www.gnu.org/licenses/>.
;;
;; Created: 11 Jul 2021
;;
;; URL: https://github.com/d12frosted/vulpea
;;
;; License: GPLv3
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Functionality to select a note.
;;
;;; Code:
(require 'org-roam)
(require 'vulpea-utils)
(require 'vulpea-db)
(defvar vulpea-select-describe-fn #'vulpea-note-title
"Function to describe a note for completion.
Accepts a `vulpea-note'. Returns a `string'.")
(defvar vulpea-select-annotate-fn #'vulpea-select-annotate
"Function to annotate a note for completion.
Accepts a `vulpea-note'. Returns a `string'.")
(defun vulpea-select-describe (note)
"Describe a NOTE for completion."
(propertize
(concat
(funcall vulpea-select-describe-fn note)
(propertize
(funcall vulpea-select-annotate-fn note)
'face 'completions-annotations))
'vulpea-note-id
(vulpea-note-id note)))
(defun vulpea-select-annotate (note)
"Annotate a NOTE for completion."
(let* ((alias-str
(if (vulpea-note-primary-title note)
(concat "("
(vulpea-note-primary-title note)
")")
""))
(tags-str (mapconcat
(lambda (x) (concat "#" x))
(vulpea-note-tags note)
" "))
(sections (seq-remove #'string-empty-p
(list alias-str
tags-str))))
(if (null sections)
""
(concat " " (string-join sections " ")))))
(cl-defun vulpea-select (prompt
&key
require-match
initial-prompt
filter-fn)
"Select a note.
Returns a selected `vulpea-note'. If `vulpea-note-id' is nil, it
means that user selected non-existing note.
When REQUIRE-MATCH is non-nil, use may select only existing note.
PROMPT is a message to present.
INITIAL-PROMPT is the initial title prompt.
FILTER-FN is the function to apply on the candidates, which takes
as its argument a `vulpea-note'."
(let ((notes (vulpea-db-query filter-fn)))
(vulpea-select-from
prompt notes
:require-match require-match
:initial-prompt initial-prompt)))
(cl-defun vulpea-select-from (prompt
notes
&key
require-match
initial-prompt)
"Select a note from the list of NOTES.
Returns a selected `vulpea-note'. If `vulpea-note-id' is nil, it
means that user selected non-existing note.
When REQUIRE-MATCH is non-nil, use may select only existing note.
PROMPT is a message to present.
INITIAL-PROMPT is the initial title prompt."
(let* ((completions (seq-map
(lambda (n)
(cons (vulpea-select-describe n)
n))
notes))
(notes-table (make-hash-table :test #'equal)))
(seq-each (lambda (note)
(puthash (vulpea-note-id note) note notes-table))
notes)
(let ((note (completing-read
(concat prompt ": ")
completions
nil require-match initial-prompt)))
(or (cdr (assoc note completions))
(make-vulpea-note
:title (substring-no-properties note)
:level 0)))))
(cl-defun vulpea-select-multiple-from (prompt
notes
&key
require-match
initial-prompt
select-fn)
"Collect multiple elements from list of NOTES.
When REQUIRE-MATCH is non-nil, use may select only existing note.
PROMPT is a message to present.
INITIAL-PROMPT is the initial title prompt.
It behaves the same as the following code
(vulpea-utils-collect-while
#\='vulpea-select-from nil prompt notes
:require-match require-match
:initial-prompt initial-prompt)
The only difference, it allows to select a single note only once, i.e.
the next prompt iteration doesn't contain already selected notes.
Optionally, an interactive SELECT-FN can be provided to be used instead
of `vulpea-select-from'. Signatures must match."
(let (result
value
(continue t)
(inhibit-quit t))
(with-local-quit
(while continue
(setq value
(funcall-interactively
(or select-fn #'vulpea-select-from)
(concat prompt " (C-g to stop)")
notes
:require-match require-match
:initial-prompt initial-prompt))
(setq notes (--remove (string-equal (vulpea-note-id it)
(vulpea-note-id value))
notes))
(setq result (cons value result))))
(setq quit-flag nil)
(reverse result)))
(provide 'vulpea-select)
;;; vulpea-select.el ends here