-
Notifications
You must be signed in to change notification settings - Fork 13
/
zk.el
1120 lines (977 loc) · 39.4 KB
/
zk.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; zk.el --- Functions for working with Zettelkasten-style linked notes -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2024 Grant Rosson
;; Author: Grant Rosson <https://github.com/localauthor>
;; Created: January 4, 2022
;; License: GPL-3.0-or-later
;; Version: 0.8
;; URL: https://github.com/localauthor/zk
;; Package-Requires: ((emacs "28.1"))
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This set of functions aims to implement many (but not all) of the features
;; of the package 'Zetteldeft', while circumventing and eliminating any
;; dependency on 'Deft', or any other external packages for that matter. It
;; does not use any backend cache or database, but instead queries a
;; directory of notes directly, treating and utilizing that directory as a
;; sufficient database unto itself.
;; To that end, these functions rely, at the lowest level, on simple calls to
;; 'grep', which returns lists of files, links, and tags to
;; 'completing-read', from which files can be opened and links and tags can
;; be inserted into an open buffer.
;; The primary connector between notes is the simple link, which takes the
;; form of an ID number enclosed in double-brackets, eg, [[202012091130]]. A
;; note's ID number, by default, is a twelve-digit string corresponding to
;; the date and time the note was originally created. For example, a note
;; created on December 9th, 2020 at 11:30 will have the zk ID "202012091130".
;; Linking to such a note involves nothing more than placing the string
;; [[202012091130]] into another note in the directory.
;; A note's filename is constructed as follows: the zk ID number followed by
;; the title of the note followed by the file extension, e.g. "202012091130
;; On the origin of species.txt". A key consequence of this ID/linking scheme
;; is that a note's title can change without any existing links to the note
;; being broken, wherever they might be in the directory.
;; The directory is a single folder containing all notes.
;; The structural simplicity of this set of functions is---one hopes, at
;; least---in line with the structural simplicity of the so-called
;; "Zettelkasten method," of which much can be read in many places, including
;; at https://www.zettelkasten.de.
;;; Code:
(require 'thingatpt)
(require 'format-spec)
;;; Variable Declarations
(defvar embark-keymap-alist)
(defvar embark-target-finders)
(defvar embark-multitarget-actions)
(defvar embark-general-map)
(defvar embark-file-map)
(defvar embark--associated-file-fn-alist)
;;; Variables
(defgroup zk nil
"A Zettelkasten implementation for Emacs."
:group 'text
:group 'files
:prefix "zk-")
;; Fundamental variables
(defcustom zk-directory nil
"Main zk directory."
:type 'string)
;; Borrowed from Deft by Jason R. Blevins <[email protected]>
(defcustom zk-directory-recursive nil
"Recursively search for files in subdirectories of `zk-directory'.
If you set this, also consider setting `zk-subdirectory-function'."
:type 'boolean)
(defcustom zk-directory-recursive-ignore-dir-regexp
"\\(?:\\.\\|\\.\\.\\)$"
"Regexp for subdirs to be ignored when ‘zk-directory-recursive’ is non-nil."
:type 'string)
(defcustom zk-subdirectory-function nil
"Function that returns a subdirectory of `zk-directory'.
Used when `zk-directory-recursive' is non-nil to create new notes
in the desired subdirectory. When nil, new notes are created in
`zk-directory'."
:type 'function)
(defcustom zk-file-extension nil
"The extension for zk files."
:type 'string)
(defcustom zk-file-name-separator " "
"Character(s), as a string, to separate elements of filename.
Useful for keeping spaces out of file-names. When set to \"-\",
for example, the file-name will be in the form
\"202012341234-Title-of-note.ext\". In notes, the title will be
rendered with spaces."
:type 'string)
(defcustom zk-id-time-string-format "%Y%m%d%H%M"
"Format for new zk IDs.
For supported options, please consult `format-time-string'.
Note: the regexp to find zk IDs is set separately.
If you change this value, set `zk-id-regexp' so that
the zk IDs can be found."
:type 'string)
(defcustom zk-id-regexp "\\([0-9]\\{12\\}\\)"
"The regular expression used to search for zk IDs.
Set it so that it matches strings generated with
`zk-id-format'."
:type 'regexp)
(defcustom zk-tag-regexp "\\s#[a-zA-Z0-9]\\+"
"The regular expression used to search for tags."
:type 'regexp)
;; Function variables
(defcustom zk-new-note-header-function #'zk-new-note-header
"Function called by `zk-new-note' to insert header in a new note.
A user-defined function should use `insert' to insert a string or
strings. The arguments NEW-ID, TITLE, and ORIG-ID can be used to
those corresponding values from `zk-new-note' available for
insertion. See `zk-new-note-header' for an example."
:type 'function)
(defcustom zk-select-file-function #'zk--select-file
"Function for performing completing read.
Must take an optional prompt and a list of files"
:type 'function)
(defcustom zk-tag-insert-function nil
"Function for inserting tag.
Function must take a single argument TAG, as a string.
If nil, tag will be inserted at point."
:type 'function)
(defcustom zk-search-function #'zk-grep
"Function used by `zk-search'.
Must take a single STRING argument."
:type 'function)
(make-obsolete-variable 'zk-grep-function "The use of the
'zk-grep-function' variable is deprecated.
'zk-search-function' should be used instead"
"0.5")
(defcustom zk-tag-search-function #'zk-grep
"Function used by `zk-tag-search'.
Must take a single STRING argument."
:type 'function)
(make-obsolete-variable 'zk-tag-grep-function "The use of the
'zk-tag-grep-function' variable is deprecated.
'zk-tag-search-function' should be used instead"
"0.5")
(defcustom zk-current-notes-function nil
"User-defined function for listing currently open notes.
See `zk-current-notes' for details."
:type 'function)
(defcustom zk-format-function #'zk-format-id-and-title
"Function for formatting zk file information.
It should accept three variables: FORMAT-SPEC, ID, and TITLE. See
`zk--format' for details."
:type 'function)
;; Format variables
(defcustom zk-link-format "[[%s]]"
"Format for inserted links.
Used in conjunction with `format', the string `%s' will be
replaced by a note's ID."
:type 'string)
(defcustom zk-link-and-title-format "%t [[%i]]"
"Format for link and title when inserted to together.
By default (when `zk-format-function' is nil), the string `%t' will be
replaced by the note's title and `%i' will be replaced by its ID."
:type 'string)
(defcustom zk-completion-at-point-format "[[%i]] %t"
"Format for completion table used by `zk-completion-at-point'.
By default (when `zk-format-function' is nil), the string `%t' will be
replaced by the note's title and `%i' will be replaced by its ID."
:type 'string)
;; Link variables
(defcustom zk-new-note-link-insert 'ask
"Should `zk-new-note' insert link to new note at point?
Options:
1. t - Always insert a link
2. `zk - Insert link only inside an existing note
3. `ask - Ask user, yes or no
4. nil - Never insert a link
Calling `zk-new-note' with a prefix-argument inserts a link
regardless of how `zk-new-note-link-insert' is set."
:type '(choice (const :tag "Always" t)
(const :tag "Ask" ask)
(const :tag "Only in zk notes" zk)
(const :tag "Never" nil)))
(defcustom zk-link-and-title t
"Should `zk-insert-link' insert both link and title?
Options:
1. t - Always inserts link and title; with `prefix-arg', only link
2. `ask - Ask user, yes or no; with `prefix-arg', only link
3. nil - Only insert link, not title; with `prefix-arg', include title
The format in which link and title are inserted can be configured
by setting the variable `zk-link-and-title-format'."
:type '(choice (const :tag "Always" t)
(const :tag "Ask" ask)
(const :tag "Never" nil)))
(defcustom zk-enable-link-buttons t
"When non-nil, valid zk-id links will be clickable buttons.
Allows `zk-make-link-buttons' to be added to `find-file-hook', so
buttons will be automatically created when a note is opened."
:type 'boolean)
(defcustom zk-default-backlink nil
"When non-nil, should be a single zk ID.
See `zk-new-note' for details."
:type 'string)
(defvar zk-file-history nil)
(defvar zk-search-history nil)
(defvar zk--no-gc nil)
;;; Embark Integration
(defvar zk-id-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") #'zk-follow-link-at-point)
(define-key map (kbd "k") #'zk-copy-link-and-title)
(define-key map (kbd "s") #'zk-search)
map)
"Keymap for Embark zk-id at-point actions.")
(defvar zk-file-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "i") #'zk-insert-link)
(define-key map (kbd "f") #'zk-find-file)
(define-key map (kbd "k") #'zk-copy-link-and-title)
map)
"Keymap for Embark zk-file minibuffer actions.")
;;;###autoload
(defun zk-embark-target-zk-id-at-point ()
"Target zk-id at point."
(when (thing-at-point-looking-at zk-id-regexp)
(let ((zk-id (match-string-no-properties 0)))
`(zk-id ,zk-id . ,(bounds-of-thing-at-point 'symbol)))))
;;;###autoload
(defun zk-setup-embark ()
"Setup Embark integration for zk.
Adds zk-id as an Embark target, and adds `zk-id-map' and
`zk-file-map' to `embark-keymap-alist'."
(with-eval-after-load 'embark
(add-to-list 'embark--associated-file-fn-alist '(zk-file . identity))
(add-to-list 'embark-multitarget-actions 'zk-copy-link-and-title)
(add-to-list 'embark-multitarget-actions 'zk-insert-link)
(add-to-list 'embark-target-finders 'zk-embark-target-zk-id-at-point)
(add-to-list 'embark-keymap-alist '(zk-id . zk-id-map))
(add-to-list 'embark-keymap-alist '(zk-file . zk-file-map))
(set-keymap-parent zk-id-map embark-general-map)
(set-keymap-parent zk-file-map embark-file-map)))
;;; Low-Level Functions
(defun zk--singleton-p (list)
"Return non-NIL if LIST is not null, is a list, and has a single element."
(and list
(listp list)
(null (cdr list))))
(defun zk-file-name-regexp ()
"Return the correct regexp matching zk file names.
The regexp captures these groups:
Group 1 is the zk ID.
Group 2 is the title."
(concat "\\(?1:" zk-id-regexp "\\)"
"."
"\\(?2:.*?\\)"
"\\."
zk-file-extension
".*"))
(defun zk-link-regexp ()
"Return the correct regexp for zk links.
The value is based on `zk-link-format' and `zk-id-regexp'."
(format (regexp-quote zk-link-format) zk-id-regexp))
(defun zk--file-id (file)
"Return the ID of the given zk FILE."
(when (string-match (zk-file-name-regexp) file)
(match-string-no-properties 1 file)))
(defun zk-file-p (&optional file strict)
"Return t if FILE is a zk-file.
If FILE is not given, get it from variable `buffer-file-name'.
If STRICT is non-nil, make sure the file is in `zk-directory',
otherwise just match against `zk-file-name-regexp'."
(let ((file (cond ((stringp file) file)
((null file) buffer-file-name)
((listp file) (car file))
(t
(signal 'wrong-type-argument '(file))))))
(and file
(zk--file-id file)
(or (not strict)
(save-match-data
(file-in-directory-p file zk-directory))))))
(defun zk--generate-id ()
"Generate and return a zk ID.
The ID is created using `zk-id-time-string-format'."
(let ((id (format-time-string zk-id-time-string-format)))
(while (zk-id-p id)
(setq id (1+ (string-to-number id)))
(setq id (number-to-string id)))
id))
(defun zk--id-list (&optional str zk-alist)
"Return a list of zk IDs for notes in `zk-directory'.
Optional search for STR in note title, case-insenstive. Takes an
optional ZK-ALIST, for efficiency if `zk--id-list' is called in
an internal loop."
(if str
(let ((zk-alist (or zk-alist (zk--alist)))
(case-fold-search t)
(ids))
(dolist (item zk-alist)
(if str
(when (string-match str (cadr item))
(push (car item) ids))
(push (car item) ids)))
ids)
(ensure-list (zk--parse-file 'id (zk--directory-files t)))))
(defun zk-id-p (id)
"Return t if ID is already in use as a zk-id."
(when (and (listp (zk--id-list))
(member id (zk--id-list)))
t))
(defun zk--current-id ()
"Return the ID of zk note in current buffer."
(or (zk--file-id buffer-file-name)
(user-error "Not a zk file")))
(make-obsolete 'zk--current-id 'zk--file-id "0.5")
(defun zk--directory-files (&optional full regexp)
"Return list of zk-files in `zk-directory'.
Excludes lockfiles, autosave files, and backup files. When FULL is
non-nil, return full file-paths. If REGEXP is non-nil, it must be
a regexp to replace the default, `zk-id-regexp'.
When `zk-directory-recursive' is non-nil, searches recursively in
subdirectories of `zk-directory' (except those matching
`zk-directory-recursive-ignore-dir-regexp') and returns full
file-paths."
(unless zk--no-gc (garbage-collect))
;; note: this call to gc prevents eventual slowdown,
;; but gc is expensive for certain operations in zk-index and zk-luhmann,
;; so we let-bind zk--no-gc there to prevent it
(let* ((regexp (or regexp zk-id-regexp))
(list
(if (not zk-directory-recursive)
(directory-files zk-directory full regexp)
(directory-files-recursively
zk-directory regexp nil
(lambda (dir)
(not (string-match
zk-directory-recursive-ignore-dir-regexp
dir))))))
(files
(remq nil (mapcar
(lambda (x)
(when (and (string-match (zk-file-name-regexp) x)
(not (string-match-p
"^[.]\\|[#|~]$"
(file-name-nondirectory x))))
x))
list))))
files))
(defun zk--current-notes-list ()
"Return list of files for currently open notes."
(remq nil
(mapcar
(lambda (x)
(when (and (buffer-file-name x)
(zk-file-p (buffer-file-name x)))
(buffer-file-name x)))
(buffer-list))))
(defun zk--grep-file-list (str &optional extended invert)
"Return a list of files containing regexp STR.
If EXTENDED is non-nil, use egrep. If INVERT is non-nil,
return list of files not matching the regexp."
(split-string
(shell-command-to-string
(concat (if extended "egrep" "grep")
(if invert " --files-without-match" " --files-with-matches")
" --recursive"
" --ignore-case"
" --include=\\*." zk-file-extension
" --regexp=" (shell-quote-argument str)
" " zk-directory
" 2>/dev/null"))
"\n" t))
(defun zk--grep-id-list (str)
"Return a list of IDs for files containing STR."
(let ((ids (zk--parse-file 'id (zk--grep-file-list str))))
(if (stringp ids) (list ids)
ids)))
(defun zk--grep-tag-list ()
"Return list of tags from all notes in zk directory."
(let* ((files (shell-command-to-string (concat
"grep -ohir --include \\*."
zk-file-extension
" -e "
(shell-quote-argument
zk-tag-regexp)
" "
zk-directory " 2>/dev/null")))
(list (split-string files "\n" t "\s")))
(delete-dups list)))
(defun zk--select-file (&optional prompt list group sort)
"Wrapper around `completing-read' to select zk-file.
Offers candidates from `zk--directory-files', or from LIST when
supplied. Can take a PROMPT argument."
(let* ((files (or list
(zk--directory-files t)))
(group (or group 'zk--group-function))
(sort (or sort nil)))
(completing-read
(or prompt
"Select File: ")
(lambda (string predicate action)
(if (eq action 'metadata)
`(metadata
(group-function . ,group)
(display-sort-function . ,sort)
(category . zk-file))
(complete-with-action action files string predicate)))
nil t nil 'zk-file-history)))
(defun zk--group-function (file transform)
"TRANSFORM completion candidate FILE to note title."
(if transform
(progn
(string-match (zk-file-name-regexp) file)
(match-string 2 file))
"zk"))
(defun zk--id-at-point ()
"Return ID at point."
(cond ((thing-at-point-looking-at zk-id-regexp)
(match-string-no-properties 0))
((thing-at-point-looking-at (zk-link-regexp))
(match-string-no-properties 1))))
(defun zk--alist ()
"Return an alist ID, title, and file-path triples."
(mapcar
(lambda (file)
(when (string= (file-name-extension file) zk-file-extension)
(string-match (zk-file-name-regexp) file)
`(,(match-string-no-properties 1 file)
,(replace-regexp-in-string zk-file-name-separator " "
(match-string-no-properties 2 file))
,file)))
(zk--directory-files t)))
(defun zk--parse-id (target ids &optional zk-alist)
"Return TARGET, either `file-path or `title, from files with IDS.
Takes a single ID, as a string, or a list of IDs. Takes an
optional ZK-ALIST, for efficiency if `zk--parse-id' is called
in an internal loop."
(cond
((and (eq target 'file-path)
(stringp ids))
(car (zk--directory-files t ids)))
((and (eq target 'file-path)
(zk--singleton-p ids))
(car (zk--directory-files t (car ids))))
(t
(let* ((zk-alist (or zk-alist
(zk--alist)))
(zk-id-list (zk--id-list))
(return
(cond ((eq target 'file-path)
(cond ((stringp ids)
(if (member ids zk-id-list)
(cddr (assoc ids zk-alist))
(user-error "No file associated with %s" ids)))
((listp ids)
(mapcar
(lambda (x)
(caddr (assoc x zk-alist)))
ids))))
((eq target 'title)
(cond ((stringp ids)
(if (member ids zk-id-list)
(cadr (assoc ids zk-alist))
(user-error "No file associated with %s" ids)))
((listp ids)
(mapcar
(lambda (x)
(cadr (assoc x zk-alist)))
ids)))))))
(if (zk--singleton-p return)
(car return)
return)))))
(defun zk--parse-file (target files)
"Return TARGET, either `id or `title, from FILES.
Takes a single file-path, as a string, or a list of file-paths.
A note's title is understood to be the portion of its filename
following the zk ID, in the format `zk-id-regexp', and preceding the
file extension."
(let ((result
(mapcar (lambda (file)
(when (string-match (zk-file-name-regexp) file)
(pcase target
('id (match-string 1 file))
('title (replace-regexp-in-string
(regexp-quote zk-file-name-separator)
" "
(match-string 2 file)))
(_ (signal 'wrong-type-argument
`((or 'id 'title) ,target))))))
(ensure-list files))))
(if (zk--singleton-p result)
(car result)
result)))
;;; Formatting
(defun zk--processor (arg)
"Return list of files.
ARG can be zk-file or zk-id as string or list, single or multiple."
(let* ((zk-alist (zk--alist))
(files (cond
((stringp arg)
(if (zk-file-p arg)
(list arg)
(list (zk--parse-id 'file-path arg zk-alist))))
((zk--singleton-p arg)
(if (zk-file-p (car arg))
arg
(list (zk--parse-id 'file-path (car arg) zk-alist))))
(t
(if (zk-file-p (car arg))
arg
(zk--parse-id 'file-path arg zk-alist))))))
files))
(defun zk--formatter (arg format &optional no-proc)
"Return formatted list from FILES, according to FORMAT.
ARG can be zk-file or zk-id as string or list, single or multiple.
When NO-PROC is non-nil, bypass `zk--processor'."
(let ((files (if no-proc
arg
(zk--processor arg)))
items)
(dolist (file files)
(when (string-match (zk-file-name-regexp) file)
(let ((id (match-string 1 file))
(title (replace-regexp-in-string zk-file-name-separator " "
(match-string 2 file))))
(push (zk--format format id title) items))))
items))
(defun zk--formatted-string (arg format)
"Format a multi-line string from items in ARG, following FORMAT.
ARG can be zk-file or zk-id as string or list, single or multiple."
(let ((items (zk--formatter arg format)))
(mapconcat #'identity items "\n\n")))
(defun zk-format-id-and-title (format id title)
"Format ID and TITLE based on the `format-spec' FORMAT.
This is the default function set in `zk-format-function' and used by
`zk--format' otherwise, replace the sequence `%t' with the TITLE and
`%i' with the ID."
(format-spec format `((?i . ,id) (?t . ,title))))
(defun zk--format (format id title)
"Format ID and TITLE based on the `format-spec' FORMAT."
(if (eq format zk-link-format)
(format zk-link-format id)
(funcall zk-format-function format id title)))
;;; Buttons
(defun zk-setup-auto-link-buttons ()
"Enable automatic link creation when zk-file is opened.
Adds `zk-make-link-buttons' to `find-file-hook.'"
(setq zk-enable-link-buttons t)
(add-hook 'find-file-hook #'zk-make-link-buttons))
(eval-and-compile
(define-button-type 'zk-link
'action 'zk-follow-link-at-point
'follow-link t
'help-echo (lambda (_win _obj pos)
(format
"%s"
(zk--parse-id
'title
(button-label
(button-at pos)))))))
(defun zk-make-link-buttons ()
"Make `zk-link-regexp's in current buffer into zk-link buttons."
(interactive)
(when (and (zk-file-p)
zk-enable-link-buttons)
(let ((ids (zk--id-list)))
(save-excursion
(goto-char (point-min))
(while (re-search-forward (zk-link-regexp) nil t)
(let ((beg (match-beginning 1))
(end (match-end 1))
(id (match-string-no-properties 1)))
(when (member id ids)
(make-button beg end 'type 'zk-link))))))))
(defun zk-make-button-before-point ()
"Find `zk-link-regexp' before point and make it a zk-link button."
(interactive)
(save-excursion
(re-search-backward (zk-link-regexp) (line-beginning-position))
(make-button (match-beginning 1) (match-end 1)
'type 'zk-link)))
;;; Note Functions
(defun zk--note-file-path (id title)
"Generate full file-path for note with given ID and TITLE."
(let ((base-name (format "%s%s%s.%s"
id
zk-file-name-separator
title
zk-file-extension)))
(concat (file-name-as-directory zk-directory)
(when (functionp zk-subdirectory-function)
(file-name-as-directory (funcall zk-subdirectory-function id)))
(replace-regexp-in-string " "
zk-file-name-separator
base-name))))
;;;###autoload
(defun zk-new-note (&optional title)
"Create a new note, insert link at point of creation.
Optional TITLE argument."
(interactive)
(let* ((pref-arg current-prefix-arg)
(new-id (zk--generate-id))
(orig-id (ignore-errors (zk--file-id buffer-file-name)))
(text (when (use-region-p)
(buffer-substring
(region-beginning)
(region-end))))
(title (cond (title title)
((use-region-p)
(with-temp-buffer
(insert text)
(goto-char (point-min))
(buffer-substring
(point)
(line-end-position))))
(t (read-string "Note title: "))))
(body (when (use-region-p)
(with-temp-buffer
(insert text)
(goto-char (point-min))
(forward-line 2)
(buffer-substring
(point)
(point-max)))))
(file-name (zk--note-file-path new-id title)))
(unless orig-id
(setq orig-id zk-default-backlink))
(when (use-region-p)
(kill-region (region-beginning) (region-end)))
(when (or pref-arg
(eq zk-new-note-link-insert 't)
(and (eq zk-new-note-link-insert 'zk)
(zk-file-p))
(and (eq zk-new-note-link-insert 'ask)
(y-or-n-p "Insert link at point? ")))
(unless buffer-read-only
(zk-insert-link file-name)))
(when buffer-file-name
(save-buffer))
(find-file file-name)
(funcall zk-new-note-header-function title new-id orig-id)
(when body (insert body))
(when zk-enable-link-buttons (zk-make-link-buttons))
(save-buffer)))
(defun zk-new-note-header (title new-id &optional orig-id)
"Insert header in new notes with args TITLE and NEW-ID.
Optionally use ORIG-ID for backlink."
(insert (format "# %s %s\n===\ntags: \n" new-id title))
(when (ignore-errors (zk--parse-id 'title orig-id)) ;; check for file
(progn
(insert "===\n<- ")
(zk--insert-link-and-title orig-id)
(newline)))
(insert "===\n\n"))
;;;###autoload
(defun zk-rename-note ()
"Rename current note and replace title in header.
When header title does not match file title, ask to accept header
title as new title. If no, prompt for new title and replace
header title in buffer. If yes, file name changed to header
title."
(interactive)
(read-only-mode -1)
(let* ((id (zk--file-id buffer-file-name))
(file-title (zk--parse-id 'title id))
(header-title (progn
(save-excursion
(goto-char (point-min))
(re-search-forward (concat id "."))
(buffer-substring-no-properties
(point)
(line-end-position)))))
(new-title))
(unless id
(user-error "Not a zk file"))
(if (not (string= file-title header-title))
(if (y-or-n-p (format "Change from \"%s\" to \"%s\"? " file-title header-title))
(setq new-title header-title)
(setq new-title (read-string "New title: " file-title)))
(setq new-title (read-string "New title: " file-title)))
(when (string-match "\n" new-title)
(setq new-title (replace-regexp-in-string "\n" "" new-title)))
(save-excursion
(goto-char (point-min))
(re-search-forward id)
(re-search-forward " ")
(delete-region (point) (line-end-position))
(insert new-title))
(let ((new-file (zk--note-file-path id new-title)))
(rename-file buffer-file-name new-file t)
(set-visited-file-name new-file t t)
(save-buffer))))
;;; Find File
;;;###autoload
(defun zk-find-file ()
"Find file in `zk-directory'."
(interactive)
(find-file (funcall zk-select-file-function "Find file: ")))
;;;###autoload
(defun zk-find-file-by-id (id)
"Find file associated with ID."
(find-file (zk--parse-id 'file-path id)))
;;;###autoload
(defun zk-find-file-by-full-text-search (str)
"Find files containing regexp STR."
(interactive
(list (read-string "Search string: "
nil 'zk-search-history)))
(let ((files (zk--grep-file-list str)))
(if files
(find-file (funcall zk-select-file-function
(format "Files containing \"%s\": " str) files))
(user-error "No results for \"%s\"" str))))
;;;###autoload
(defun zk-current-notes ()
"Select from list of currently open notes.
Optionally call a custom function by setting the variable
`zk-current-notes-function' to a function name."
(interactive)
(if zk-current-notes-function
(funcall zk-current-notes-function)
(find-file
(funcall zk-select-file-function
"Current Notes:"
(zk--current-notes-list)))))
;;; Follow Links
;;;###autoload
(defun zk-follow-link-at-point (&optional id)
"Open note that corresponds with the zk ID at point."
(interactive)
(let ((id (or (zk--id-at-point)
id)))
(if id
(find-file (zk--parse-id 'file-path id))
(error "No zk-link at point"))))
(defun zk--links-in-note-list ()
"Return list of zk files that are linked from the current buffer."
(let* ((zk-alist (zk--alist))
(zk-ids (zk--id-list))
id-list)
(save-buffer)
(save-excursion
(goto-char (point-min))
(while (re-search-forward (zk-link-regexp) nil t)
(if (member (match-string-no-properties 1) zk-ids)
(push (match-string-no-properties 1) id-list))))
(cond ((zk--singleton-p id-list)
(list (zk--parse-id 'file-path id-list zk-alist)))
(id-list
(zk--parse-id 'file-path (delete-dups id-list) zk-alist))
(t
(error "No zk-links in note")))))
;;;###autoload
(defun zk-links-in-note ()
"Select from list of notes linked to in the current note."
(interactive)
(let* ((files (zk--links-in-note-list)))
(if files
(find-file (funcall zk-select-file-function "Links: " files))
(user-error "No links found"))))
;;; Insert Link
;;;###autoload
(defun zk-insert-link (arg)
"Insert link to note, from ARG.
ARG can be zk-file or zk-id as string or list, single or multiple.
By default, only a link is inserted. With prefix-argument, both
link and title are inserted. See variable `zk-link-and-title'
for additional configurations."
(interactive
(list (list (funcall zk-select-file-function "Insert link: "))))
(if (zk--id-at-point)
(user-error "Move point off zk-id before inserting")
(let* ((pref current-prefix-arg))
(cond
((or (and (not pref) (eq 't zk-link-and-title))
(and pref (not zk-link-and-title)))
(zk--insert-link-and-title arg))
((and (not pref) (eq 'ask zk-link-and-title))
(if (y-or-n-p "Include title? ")
(zk--insert-link-and-title arg)
(zk--insert-link arg)))
((or t
(and pref (eq 't zk-link-and-title)))
(zk--insert-link arg))))))
(defun zk--insert-link-and-title (arg)
"Insert link from ARG according to `zk-link-and-title-format'."
(insert (zk--formatted-string arg zk-link-and-title-format))
(when zk-enable-link-buttons
(zk-make-link-buttons)))
(defun zk--insert-link (arg)
"Insert link to note from ARG, with button optional.
ARG can be zk-file or zk-id as string or list, single or multiple."
(insert (zk--formatted-string arg zk-link-format))
(when zk-enable-link-buttons
(zk-make-link-buttons)))
;;; Completion at Point
(defun zk--format-candidates (&optional files format)
"Return a list of FILES as formatted candidates, following FORMAT.
See `zk--format' for details about FORMAT. If nil,
`zk-completion-at-point-format' will be used by default.
FILES must be a list of filepaths. If nil, all files in `zk-directory'
will be returned as formatted candidates."
(let* ((format (or format
zk-completion-at-point-format)))
(if files
(zk--formatter files format)
(zk--formatter (zk--directory-files) format t))))
(defun zk-completion-at-point ()
"Completion-at-point function for zk-links.
When added to `completion-at-point-functions', typing two
brackets \"[[\" initiates completion."
(let ((case-fold-search t)
(origin (point)))
(save-excursion
(when (and (re-search-backward "\\[\\["
(line-beginning-position)
t)
(save-excursion
(not (search-forward "]]" origin t))))
(list (match-end 0)
origin
(completion-table-dynamic
(lambda (_)
(zk--format-candidates)))
:exit-function
(lambda (str _status)
(delete-char (- -2 (length str)))
(insert str)
(when zk-enable-link-buttons
(zk-make-button-before-point))))))))
;;; Copy Link and Title
;;;###autoload
(defun zk-copy-link-and-title (arg)
"Copy link and title from ARG.
ARG can be zk-file or zk-id as string or list, single or multiple."
(interactive (list (funcall zk-select-file-function "Copy link: ")))
(let ((links (zk--formatted-string arg zk-link-and-title-format)))
(kill-new links)
(message "Copied: %s" links)))
;;; List Backlinks
(defun zk--backlinks-list (id)
"Return list of notes that link to note with ID."
(zk--grep-file-list (regexp-quote (format zk-link-format id))))
;;;###autoload
(defun zk-backlinks ()
"Select from list of all notes that link to the current note."
(interactive)
(let* ((id (zk--file-id buffer-file-name))
(files (zk--backlinks-list id)))
(if files
(find-file (funcall zk-select-file-function "Backlinks: " files))
(user-error "No backlinks found"))))
;;; Search
;;;###autoload
(defun zk-search (string)
"Search for STRING using function set in `zk-search-function'.
Defaults to `zk-grep.'"
(interactive
(list (read-string "Search: "
nil 'zk-search-history)))
(funcall zk-search-function string))
(defun zk-grep (regexp)
"Wrapper around `rgrep' to search for REGEXP in all notes.
Opens search results in a grep buffer."
(interactive
(list (read-string "zk-grep: "
nil 'zk-search-history)))
(grep-compute-defaults)
(rgrep regexp (concat "*." zk-file-extension) zk-directory nil))
;;; Tag Functions
;;;###autoload
(defun zk-tag-search (tag)
"Open grep buffer containing results of search for TAG.
Select TAG, with completion, from list of all tags in zk notes.
Defaults to `zk-grep'."
(interactive (list (completing-read "Find tag: " (zk--grep-tag-list))))