-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvulpea-utils.el
197 lines (172 loc) · 6.08 KB
/
vulpea-utils.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
185
186
187
188
189
190
191
192
193
194
195
196
197
;;; vulpea-utils.el --- Vulpea utilities -*- 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: 29 Dec 2020
;;
;; URL: https://github.com/d12frosted/vulpea
;;
;; License: GPLv3
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Vulpea is a fox.
;;
;;; Code:
(require 'org)
(require 'vulpea-note)
(defvar vulpea-utils--uuid-regexp
(concat
"\\("
"[a-zA-Z0-9]\\{8\\}"
"-"
"[a-zA-Z0-9]\\{4\\}"
"-"
"[a-zA-Z0-9]\\{4\\}"
"-"
"[a-zA-Z0-9]\\{4\\}"
"-"
"[a-zA-Z0-9]\\{12\\}"
"\\)")
"UUID regexp.")
(defmacro vulpea-utils-process-notes (notes &rest body)
"Evaluate BODY for each element of NOTES.
Each element of NOTE in turn is bound to `it' and its index within NOTES
to `it-index' before evaluating BODY.
This function can be used for simple migrations as it provides some
useful features and properties:
- Visibility. Each step is logged, so the progress is visible.
- While result of BODY evaluation is discarded, any changes to the
buffer are saved. For better performance, *all* Org mode buffers are
*killed* after each step. Based on benchmarks, saving and killing
buffers after each step is times faster than saving after all
modifications.
- Each (id . file) pair is processed only once, meaning that BODY is not
called multiple times on the same node. Despite this, keep your BODY
idempotent.
- Point is placed at the beginning of note. Meaning that for
file-level notes the point is at the beginning of buffer; and
for heading-level notes the point is at the beginning of
heading."
(declare (debug (form body)) (indent 1))
(let ((l (make-symbol "notes"))
(i (make-symbol "i"))
(count (make-symbol "count"))
(countl (make-symbol "countl"))
(level (make-symbol "level"))
(file-name (make-symbol "file-name")))
`(let* ((,l (-remove #'vulpea-note-primary-title ,notes))
(,count (seq-length ,l))
(,countl (number-to-string (length (number-to-string ,count))))
(,i 0))
(pcase ,count
(`0 (message "No notes to process"))
(`1 (message "Processing 1 note"))
(_ (message "Processing %d notes" ,count)))
(while ,l
(let* ((it (pop ,l))
(it-index ,i)
(,level (vulpea-note-level it))
(,file-name (file-name-nondirectory (vulpea-note-path it))))
(message
(s-truncate
80
(format
(concat
"[%" ,countl ".d/%d] Processing %s")
(+ it-index 1)
,count
(if (= 0 ,level)
,file-name
(concat ,file-name "#" (vulpea-note-title it))))))
(cl-letf (((symbol-function 'message) (lambda (&rest _))))
(vulpea-visit it))
(ignore it it-index)
,@body
(save-some-buffers t)
(kill-matching-buffers-no-ask ".*\\.org$"))
(setq ,i (1+ ,i))))))
(defmacro vulpea-utils-with-file (file &rest body)
"Execute BODY in `org-mode' FILE.
In most cases you should use `vulpea-utils-with-note', because
that macro properly handles notes with level greater than 0."
(declare (indent 1) (debug t))
`(with-current-buffer (find-file-noselect ,file)
,@body))
(defmacro vulpea-utils-with-note (note &rest body)
"Execute BODY in with buffer visiting NOTE.
If note level is equal to 0, then the point is placed at the
beginning of the buffer. Otherwise at the heading with note id."
(declare (indent 1) (debug t))
`(with-current-buffer (find-file-noselect (vulpea-note-path ,note))
(when (> (vulpea-note-level ,note) 0)
(goto-char (org-find-entry-with-id (vulpea-note-id ,note))))
,@body))
(defun vulpea-utils-link-make-string (note &optional description)
"Make a bracket link to NOTE.
By default `vulpea-note-title' is used as description unless DESCRIPTION
is provided explicitly."
(org-link-make-string
(concat "id:" (vulpea-note-id note))
(or description (vulpea-note-title note))))
(defun vulpea-utils-note-hash (note)
"Compute the hash of NOTE."
(with-temp-buffer
(set-buffer-multibyte nil)
(insert-file-contents-literally (vulpea-note-path note))
(secure-hash 'sha1 (current-buffer))))
(defun vulpea-utils-collect-while (fn filter &rest args)
"Repeat FN and collect it's results until `C-g` is used.
Repeat cycle stops when `C-g` is used or FILTER returns nil.
If FILTER is nil, it does not affect repeat cycle.
If FILTER returns nil, the computed value is not added to result.
ARGS are passed to FN."
(let (result
value
(continue t)
(inhibit-quit t))
(with-local-quit
(while continue
(setq value (apply fn args))
(if (and filter
(null (funcall filter value)))
(setq continue nil)
(setq result (cons value result)))))
(setq quit-flag nil)
(seq-reverse result)))
(defun vulpea-utils-repeat-while (fn filter &rest args)
"Repeat FN and return the first unfiltered result.
Repeat cycle stops when `C-g` is used or FILTER returns nil.
ARGS are passed to FN."
(let (value
(continue t)
(inhibit-quit t))
(with-local-quit
(while continue
(setq value (apply fn args))
(when (null (funcall filter value))
(setq continue nil))))
(setq quit-flag nil)
(when (null continue)
value)))
(provide 'vulpea-utils)
;;; vulpea-utils.el ends here