-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathekg.el
2505 lines (2254 loc) · 104 KB
/
ekg.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
;;; ekg.el --- A system for recording and linking information -*- lexical-binding: t -*-
;; Copyright (c) 2022-2024 Andrew Hyatt <[email protected]>
;; Author: Andrew Hyatt <[email protected]>
;; Homepage: https://github.com/ahyatt/ekg
;; Package-Requires: ((triples "0.4.0") (emacs "28.1") (llm "0.18.0"))
;; Keywords: outlines, hypermedia
;; Version: 0.6.4
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; EKG is a note-taking and information storing application, centered around
;; tags, but with the ability to have other note metadata.
(require 'triples)
(require 'triples-backups)
(require 'triples-upgrade)
(require 'seq)
(require 'ewoc)
(require 'cl-lib)
(require 'map)
(require 'ffap)
(require 'hl-line)
(require 'iso8601)
(require 'url-parse)
(declare-function org-open-at-point "org")
(declare-function org-redisplay-inline-images "org")
(declare-function org-activate-links "org")
(declare-function org-in-regexp "org")
(declare-function markdown-follow-thing-at-point "markdown-mode")
(declare-function markdown-wiki-link-p "markdown-mode")
(declare-function markdown-wiki-link-link "markdown-mode")
;;; Code:
(defgroup ekg nil
"The Emacs knowledge graph, an app for notes and structured data."
:group 'applications)
(defcustom ekg-capture-default-mode 'org-mode
"The default mode for all new notes."
:type 'symbol
:group 'ekg)
(defcustom ekg-acceptable-modes '(org-mode markdown-mode text-mode)
"Modes that make sense to use as note types."
:type '(set symbol)
:group 'ekg)
(defcustom ekg-capture-auto-tag-funcs '(ekg-date-tag)
"Functions to run to create tags automatically.
The functions are run in the note buffer while creating it.
Return a list of tags to add."
:type '(set function)
:group 'ekg)
(defcustom ekg-format-funcs '()
"Functions to run to format what we display to the user.
What we display on the UI is different than what we store
internally, which is the document itself. Each function here
will be run in sequence on a temporary buffer, and is responsible
for making the changes in the current buffer to affect the
formatting however it wants. It is the responsibility of each
function to check for the mode of the buffer."
:type '(set function)
:group 'ekg)
(defcustom ekg-linkify-inline-tags t
"When non-nil, turn inline tags into links to ekg tags.
This will also make sure to detect the links when detecting tags to add
when saving notes."
:type 'boolean
:group 'ekg)
(defcustom ekg-notes-size 20
"How many recently created or updated notes to show."
:type 'integer
:group 'ekg)
(defcustom ekg-db-file nil
"The filename for the ekg database.
Initially set as nil, which will mean that we use
`triples-default-database-filename'. If you don't want to do
that use this, set to the filename you want to use. If the file
named by `ekg-db-file-obsolete' exists, that is used instead."
:type 'file
:group 'ekg)
(defcustom ekg-template-tag "template"
"Special tag marking notes acting as templates for other tags.
See `ekg-on-add-tag-insert-template' for details on how this works."
:type '(string :tag "tag")
:group 'ekg)
(defcustom ekg-function-tag "tag-defun"
"Special tag marking notes with elisp code run for other tags.
This takes effect on the note buffer when editing or capturing,
and will take effect for all other tags on the same note that
holds this tag."
:type '(string :tag "tag")
:group 'ekg)
(defcustom ekg-trash-tag "trash"
"The tag that is used to mark a note as a trashed noted."
:type '(string :tag "tag")
:group 'ekg)
(defcustom ekg-draft-tag "draft"
"The tag that is used to mark a note as a draft.
If this is nil, then there is no distinction between a draft and
a saved note. This may mean that saving a note in progress may
take longer, as more processing will be run on it."
:type '(string :tag "tag")
:group 'ekg)
(defcustom ekg-note-inline-max-words 500
"How many words to display for inlines if the caller does not specify."
:type 'integer
:group 'ekg)
(defcustom ekg-display-note-template "%n(id)%n(tagged)%n(titled)%n(text 500)%n(other)"
"Template for displaying notes in notes buffers.
This follows normal templating rules, but it is most likely the
user is interested in the various %n templates that correspond to
the types that the note can have. An exception is the %n(other)
inline, which displays all other types that are displayable, but
not in the template."
:type 'string
:group 'ekg)
(defcustom ekg-metadata-separator-text "--text follows this line--"
"Separator between the metadata of the note and the note text."
:type 'string
:group 'ekg)
(defcustom ekg-notes-display-images t
"Whether images are displayed by default."
:type 'boolean
:group 'ekg)
(defcustom ekg-save-no-message nil
"Non-nil means do not print any message when saving."
:type 'boolean
:group 'ekg)
(defcustom ekg-confirm-on-buffer-kill nil
"Non-nil if the user should confirm before killing a note buffer.
The affects killing both the capture and edit buffer, only when
there are unsaved changes."
:type 'boolean
:group 'ekg)
(defcustom ekg-inline-custom-tag-completion-symbols '((?@ . "person")
(?! . "idea"))
"A map of custom symbols to tag prefixes.
The character in the car of the aliast will be used to trigger
completion, and complete tags with the prefix of the cdr. The
character `#' will always be used to complete arbitrary tags, and
should not appear here."
:type '(alist :key-type character :value-type string)
:group 'ekg)
(defcustom ekg-inline-populate-inline-text-tags t
"Whether to detect and link inline tags out of text tags.
This happens when saving notes, which, if this is non-nil, will
look for various kinds of tags in the text, with the `#' or
prefixes in `ekg-inline-custom-tag-completion-symbols', and make
links out of them, as well as adding them to the note text."
:type 'boolean
:group 'ekg)
(defcustom ekg-command-regex-for-narrowing '("^org-insert" "org-meta-return")
"A list of regex for commands which need a narrowed buffer."
:type '(repeat string)
:group 'ekg)
(defconst ekg-db-file-obsolete (file-name-concat user-emacs-directory "ekg.db")
"The original database name that ekg started with.")
(defconst ekg-default-num-backups 5
"The number of backups to set when first using the database.
This can be overwritten by other database users, and will not be
set again. If you want to change the number of backups in your
database after it has been created, run `triples-backups-setup'.")
(defconst ekg-default-backups-strategy 'daily
"The default database backup strategy when first setting up the database.
This can be overwritten by other database users, and
will not be set again. If you want to change the number of
backups in your database after it has been created, run
`triples-backups-setup'.")
(defface ekg-notes-mode-title
'((((type graphic)) :height 2.0 :box t)
(((type tty))) :underline t)
"Face shown for the titles of EKG notes mode.")
(defface ekg-tag
'((((type graphic)) :height 1.0 :box t)
(((type tty))) :underline t)
"Face shown for EKG tags.")
(defface ekg-title
'((((type graphic)) :height 1.2 :underline t)
(((type tty))) :underline t)
"Face shown for EKG titles.")
(defface ekg-resource
'((((type graphic)) :inherit fixed-pitch)
(((type tty))) :underline t)
"Face shown for EKG resource.")
(defface ekg-metadata
'((default :inherit default :weight bold))
"Face shown for the metadata section of notes.")
(defvar ekg-db nil
"Live sqlite database connection.")
(defvar ekg-metadata-parsers '(("Tags" . ekg--metadata-update-tag)
("Resource" . ekg--metadata-update-resource)
("Title" . ekg--metadata-update-title))
"Functions that update a note from the buffer's metadata text.
Each function takes its field's property value and updates the
buffer's `ekg-note' with the results of parsing that value. The
function takes one argument, a list of the field metadata
property values for multivalue types, or a single one for single
value types. If `ekg-property-multivalue-type' has an entry, it
is a multivalue type.")
(defconst ekg-property-multivalue-type '(("Tags" . comma)
("Title" . line))
"Defines per typehow multiple values are separated.
The values are symbols, COMMA means a comma-separated value.
LINE means each value gets its own property line.")
(defvar ekg-metadata-labels '((:titled/title . "Title"))
"Alist of properties that can be on the note and their labels.
The label needs to match the keys in the `ekg-metadata-parsers' alist.")
(defvar ekg-add-schema-hook nil
"Hook run when ensuring schema for ekg.
This is run on connection. Calls to `triples-add-schema' are
idempotent, so it's recommended to run them without checking if
the schema already exists.")
(defvar ekg-note-pre-save-hook nil
"Hook run before saving a note.
This is not run in the same database transaction as the save.
All functions are called with the `ekg-note' as the single
argument.")
(defvar ekg-note-save-hook nil
"Hook run after saving a note, within the save transaction.
At this point the note itself has been saved. All functions are
called with the `ekg-note' as the single argument.")
(defvar ekg-note-pre-delete-hook nil
"Hook run before deleting a note.
This is not run in the same database transaction as the delete.
All functions are called with the ID of the note that is being
deleted as the single argument.")
(defvar ekg-note-delete-hook nil
"Hook run after deleting a note.
This is run in the same transaction as the deletion. All
functions are called with the ID of the note that is being
deleted as the single argument.")
(defvar ekg-note-add-tag-hook '(ekg-on-add-tag-insert-template)
"Hook run after a new tag is added to a note.
This includes new notes that start with tags. All functions are
called with the tag as the single argument, and run in the buffer
editing the note.")
(defconst ekg-version "0.6.4"
"The version of ekg.
This is used to understand when the database needs upgrading.")
(cl-defstruct ekg-note
id text mode tags creation-time modified-time properties inlines)
(cl-defstruct ekg-inline pos command type)
(defun ekg-db-file ()
"Return the database file we should use in ekg.
If `ekg-db-file' is nil and `ekg-db-file-obsolete' exists, use
`ekg-db-file-obsolete', otherwise use `ekg-db-file'. If that is
non-nil, it will be used as the filename, otherwise
`triples-default-database-filename' is used."
(if (and (null ekg-db-file) (file-exists-p ekg-db-file-obsolete))
ekg-db-file-obsolete
ekg-db-file))
(defun ekg--notes-directory ()
"Return the directory ekg notes buffers have as the default directory.
This will be the location of the database file."
(file-name-directory
(or (ekg-db-file)
triples-default-database-filename)))
;; `ekg-connect' will do things that might themselves call `ekg-connect', so we
;; need to protect against an infinite recursion.
(defalias 'ekg-connect
(let ((ekg--in-connect-call))
(lambda ()
(unless ekg--in-connect-call
(setq ekg--in-connect-call t)
(unwind-protect
(progn (unless ekg-db
(setq ekg-db (triples-connect (ekg-db-file))))
(let ((ekg-plist (triples-get-type ekg-db 'ekg 'ekg)))
(when (or (null (plist-get ekg-plist :version))
(version-list-< (plist-get ekg-plist :version) (version-to-list ekg-version)))
(ekg-add-schema)
(ekg-upgrade-db (plist-get ekg-plist :version))
(triples-set-type ekg-db 'ekg 'ekg :version (version-to-list ekg-version))))
(unless (triples-backups-configuration ekg-db)
(triples-backups-setup ekg-db ekg-default-num-backups
ekg-default-backups-strategy)))
(setq ekg--in-connect-call nil)))))
"Ensure EKG-DB is connected.
Also make sure the database is set up correctly. This should be
called before any access to triples, unless we are sure all
callers have already called this function")
(defun ekg-close ()
"Close the EKG-DB connection."
(interactive)
(when ekg-db
(triples-close ekg-db)
(setq ekg-db nil)))
(defun ekg-add-schema ()
"Add schema necessary for EKG to function."
;; Schema here needs to also be taken care of when deleted in ekg-note-delete
;; or ekg-tag-delete.
(triples-add-schema ekg-db 'tagged '(tag :base/type string))
(triples-add-schema ekg-db 'text
'(text :base/unique t :base/type string)
'(mode :base/unique t :base/type symbol)
'(inlines :base/virtual-reversed inline/for-text))
(triples-add-schema ekg-db 'time-tracked
'(creation-time :base/unique t :base/type integer)
'(modified-time :base/unique t :base/type integer))
(triples-add-schema ekg-db 'inline
'(command :base/unique t :base/type symbol)
'args
'(pos :base/unique t :base/type integer)
'(type :base/unique t :base/type symbol)
'(for-text :base/unique t))
(triples-add-schema ekg-db 'tag
'(tagged :base/virtual-reversed tagged/tag))
;; A URL can be a subject too, and has data, including the title. The title is
;; something that can be used to select the subject via completion.
(triples-add-schema ekg-db 'titled '(title :base/type string))
(triples-add-schema ekg-db 'ekg '(version :base/type cons :base/unique t))
(run-hooks 'ekg-add-schema-hook))
(defvar ekg-schema-text-cotypes '(text tagged time-tracked titled)
"All the types that are used in the ekg schema on text entities.
Extensions can add to this list, but should not remove from it.
Any type here in considered owned by ekg and will be removed
during note deletion.
These are not guaranteed to be only on text entities, however.")
(defun ekg--generate-id ()
"Return a unique ID for a note.
This is not suitable for generating a large number of IDs in a
small time frame. About one ID per second is reasonable."
(sxhash (cons (time-convert (current-time) 'integer) (random 100))))
(defun ekg--normalize-tag (tag)
"Return a normalized version of TAG.
No tag should be input from the user without being normalized
before storage."
(string-trim (downcase (string-replace "," "" tag))))
(defun ekg--normalize-note (note)
"Make sure NOTE adheres to ekg-wide constraints before saving.
This
1) makes sure all tags are lowercase and trimmed.
2) removes commas in tags, since those are used to separate tags.
3) removes any properties from the text.
4) makes sure the note has a reasonable ID, otherwise generates one.
Note: we used to also trim text, but with inline commands, that
is not a great idea, because an inline command sits outside of
the text and may be after trailing whitespace."
(setf (ekg-note-tags note)
(mapcar #'ekg--normalize-tag (ekg-note-tags note)))
(setf (ekg-note-text note)
(substring-no-properties (ekg-note-text note)))
(when (or (equal (ekg-note-id note) "") (not (ekg-note-id note)))
(setf (ekg-note-id note) (ekg--generate-id))))
(defun ekg-backup (&optional force)
"Backup the database.
FORCE, if non-nil, will make sure a backup is stored, regardless
of the settings of max-backups. Otherwise, a backup is just made
if it is time for one, according to the settings in
`ekg-default-num-backups' and `ekg-default-backups-strategy'."
(condition-case err
(if force
(triples-backup ekg-db ekg-db-file most-positive-fixnum)
(triples-backups-maybe-backup ekg-db (ekg-db-file)))
(file-missing
(let ((msg "Could not backup database, perhaps because of missing sqlite3 executable. Ensure the executable exists at the location specified by `triples-sqlite-executable' or `emacsql-sqlite-executable': %s"))
;; If we are forcing the backup, this error probably is serious and
;; should be investigated.
(if force
(error msg (cdr err))
(lwarn :error 'ekg msg (cdr err)))))))
(defun ekg-save-note (note)
"Save NOTE in database, replacing note information there."
(ekg-connect)
(ekg--normalize-note note)
(when (and (eq (ekg-note-mode note) 'org-mode) ekg-linkify-inline-tags)
(ekg--convert-inline-tags-to-links note))
(ekg--populate-inline-tags note)
(run-hook-with-args 'ekg-note-pre-save-hook note)
(triples-with-transaction
ekg-db
(triples-set-type ekg-db (ekg-note-id note) 'tagged :tag (ekg-note-tags note))
(triples-set-type ekg-db (ekg-note-id note) 'text
:text (ekg-note-text note)
:mode (ekg-note-mode note))
;; Delete any previous linked inlines.
(cl-loop for inline-id in (triples-subjects-with-predicate-object
ekg-db 'inline/for-text (ekg-note-id note))
do (triples-remove-type ekg-db inline-id 'inline))
;; Now store the new inlines.
(cl-loop for inline in (ekg-note-inlines note) do
(triples-set-type ekg-db (format "%S/pos:%d" (ekg-note-id note)
(ekg-inline-pos inline))
'inline
:command (car (ekg-inline-command inline))
:args (cdr (ekg-inline-command inline))
:pos (ekg-inline-pos inline)
:for-text (ekg-note-id note)
:type (ekg-inline-type inline)))
;; Note that we recalculate modified time here, since we are modifying the
;; entity.
(let ((modified-time (time-convert (current-time) 'integer)))
(triples-set-type ekg-db (ekg-note-id note) 'time-tracked
:creation-time (ekg-note-creation-time note)
:modified-time modified-time)
(setf (ekg-note-modified-time note) modified-time))
(mapc (lambda (tag) (triples-set-type ekg-db tag 'tag)) (ekg-note-tags note))
(apply #'triples-set-types ekg-db (ekg-note-id note) (ekg-note-properties note))
;; For any properties that no longer have a value, delete the type. Iterate
;; over the plist, and for each key, if the value is nil, delete the type.
(let ((empty-types)
(nonempty-types))
(cl-loop for (key value) on (ekg-note-properties note) by #'cddr do
(push (car (triples-combined-to-type-and-prop key))
(if value nonempty-types empty-types)))
(mapc (lambda (type) (triples-remove-type ekg-db (ekg-note-id note) type))
(seq-difference empty-types nonempty-types)))
(run-hook-with-args 'ekg-note-save-hook note))
(ekg-backup)
(set-buffer-modified-p nil))
(defun ekg-get-notes-with-any-tags (tags)
"Get all notes with any of the tags in TAGS.
This returns notes that have any of the tags in TAGS. Special
tags are not returned.
The notes returtned are sorted in reverse chronological order."
(ekg-connect)
(sort
(seq-uniq (mapcan (lambda (tag) (ekg-get-notes-with-tag tag))
(seq-difference tags (list ekg-draft-tag
ekg-trash-tag
ekg-function-tag))))
#'ekg-sort-by-creation-time))
(defun ekg-get-notes-with-tags (tags)
"Get all notes with TAGS, returning a list of `ekg-note' structs.
This returns only notes that have all the tags in TAGS.
Draft notes are not returned, unless TAGS contains the draft tag."
(ekg-connect)
(let ((ids-by-tag
(mapcar (lambda (tag)
(plist-get (triples-get-type ekg-db tag 'tag) :tagged))
tags)))
(seq-filter
(lambda (note)
;; Remove draft and trash notes unless they are specifically requested.
(not
(or (and (not (member ekg-draft-tag tags))
(member ekg-draft-tag (ekg-note-tags note)))
(and (not (member ekg-trash-tag tags))
(member ekg-trash-tag (ekg-note-tags note))))))
(mapcar #'ekg-get-note-with-id
(seq-reduce #'seq-intersection
ids-by-tag
(car ids-by-tag))))))
(defun ekg-get-notes-with-tag (tag)
"Get all notes with TAG, returning a list of `ekg-note' structs."
(ekg-get-notes-with-tags (list tag)))
(defun ekg-note-with-id-exists-p (id)
"Return non-nil if a note with ID exists."
(ekg-connect)
(triples-get-subject ekg-db id))
(defun ekg-get-note-with-id (id)
"Get the specific note with ID.
If the ID does not exist, create a new note with that ID."
(ekg-connect)
(let* ((v (triples-get-subject ekg-db id))
(inlines (mapcar (lambda (iid)
(let ((iv (triples-get-type ekg-db iid
'inline)))
(make-ekg-inline :pos (plist-get iv :pos)
:command (cons
(plist-get iv :command)
(plist-get iv :args))
:type (plist-get iv :type))))
(plist-get v :text/inlines))))
(make-ekg-note :id id
:text (plist-get v :text/text)
:mode (plist-get v :text/mode)
:inlines inlines
:tags (plist-get v :tagged/tag)
:creation-time (plist-get v :time-tracked/creation-time)
:modified-time (plist-get v :time-tracked/modified-time)
:properties (map-into
(map-filter
(lambda (plist-key _)
(not (member plist-key
'(:text/text
:text/mode
:text/inlines
:tagged/tag
:time-tracked/creation-time
:time-tracked/modified-time)))) v)
'plist))))
(defun ekg-get-notes-with-title (title)
"Get a list of note structs with TITLE."
(ekg-connect)
(mapcar #'ekg-get-note-with-id (triples-subjects-with-predicate-object ekg-db 'titled/title title)))
(defun ekg-note-delete (note)
"Delete NOTE from the database."
(ekg-note-delete-by-id (ekg-note-id note)))
(defun ekg-note-delete-by-id (id)
"Delete all note data associated with ID."
(ekg-connect)
(run-hook-with-args 'ekg-note-pre-delete-hook id)
(triples-with-transaction
ekg-db
(cl-loop for type in ekg-schema-text-cotypes do
(triples-remove-type ekg-db id type))
(cl-loop for inline-id in (triples-subjects-with-predicate-object
ekg-db 'inline/for-text id)
do (triples-remove-type ekg-db inline-id 'inline))
(run-hook-with-args 'ekg-note-delete-hook id)))
(defun ekg-tag-delete (tag)
"Delete all tag data associated with TAG."
(ekg-connect)
(triples-remove-type ekg-db tag 'tag))
(defun ekg-note-trash (note)
"Add the trash tag to NOTE."
(ekg-connect)
(if (member ekg-trash-tag (ekg-note-tags note))
(ekg-note-delete note)
(triples-with-transaction
ekg-db
(push ekg-trash-tag (ekg-note-tags note))
(ekg-save-note note)))
(ekg-backup))
(defun ekg-content-tag-p (tag)
"Return non-nil if TAG represents user content.
This is opposed to tags that are used for internal purposes."
(not (member tag
(list ekg-draft-tag ekg-template-tag ekg-function-tag ekg-trash-tag))))
(defun ekg-note-active-p (note)
"Return non-nil if NOTE is active.
This is similar to `ekg-active-id-p', but takes a note, which may
be unsaved."
(not (seq-intersection (list ekg-draft-tag ekg-trash-tag)
(ekg-note-tags note))))
(defun ekg-note-is-content-p (note)
"Return non-nil if NOTE has no control-type tags.
Those tags are things such as `ekg-draft-tag', or `ekg-function-tag'."
(seq-every-p #'ekg-content-tag-p (ekg-note-tags note)))
(defun ekg-active-id-p (id)
"Return non-nil if the note with ID is active.
This will return true if the note is not a draft, and has at
least one non-trash tag."
(ekg-connect)
(ekg-note-active-p (ekg-get-note-with-id id)))
(defun ekg-live-id-p (sub)
"Return non-nil if SUB represents an undeleted note."
(ekg-connect)
(not (seq-some (lambda (tag) (equal tag ekg-trash-tag)) (plist-get (triples-get-type ekg-db sub 'tagged) :tag))))
(defun ekg-extract-inlines (text)
"Return a cons of TEXT without inline commands, and the commands.
The commands returned are the most specific type of struct known,
or, if unknown, `ekg-inline'."
(let ((inlines)
(newtext text)
(inline-rx (rx (seq (group-n 1 "%"
(group-n 2 (zero-or-one "n"))
(literal "(")
(group-n 3 (*? anychar))
(literal ")"))))))
;; Keep removing commands left to right to make sure our positions are
;; without commands, since that's how they will need to be inserted.
(while (when-let (index (string-match inline-rx newtext))
(push (make-ekg-inline :pos index
:command (read (format "(%s)" (match-string 3 newtext)))
:type (pcase (match-string 2 newtext)
("" 'command)
("n" 'note)))
inlines)
(setq newtext (replace-match "" nil nil newtext 1))))
(cons newtext (nreverse inlines))))
(defun ekg-truncate-at (s numwords)
"Return S with ellipses after NUMWORDS words.
If NUMWORDS is greater than the number of words of S, return S
unchanged."
(with-temp-buffer
(insert s)
(goto-char 0)
(cl-loop with i = 0 while (and (< i numwords)
(forward-word))
do (cl-incf i))
(when (< (point) (point-max))
(insert "…")
(delete-region (point) (point-max)))
(buffer-string)))
(defun ekg-insert-inlines-and-process (text inlines func)
"Return the result of inserting INLINES into TEXT.
FUNC is executed with the cdr of each inline command and its
return value is inserted into the buffer at the appropriate
point. NUMTOK is the number of tokens available to be used."
(with-temp-buffer
(insert text)
(let ((mils (cl-loop for il in inlines do
(goto-char (+ (ekg-inline-pos il) 1))
collect (cons (point-marker)
(condition-case err
(funcall func il)
(error
(propertize
(format "Error executing inline command %s: %s"
(ekg-inline-to-text il)
(error-message-string err))
'face 'error)))))))
(cl-loop for mil in mils do
(when (cdr mil)
(goto-char (car mil))
(insert-before-markers (cdr mil)))))
(buffer-string)))
(defun ekg-inline-to-text (inline)
"Return the text representation of INLINE."
(format "%%%s%S"
(if (eq 'note (ekg-inline-type inline))
"n" "") (ekg-inline-command inline)))
(defun ekg-insert-inlines-representation (text inlines)
"Return the result of inserting INLINES into TEXT.
INLINES are inserted in their unevaluated text forms."
(ekg-insert-inlines-and-process
text inlines #'ekg-inline-to-text))
(defun ekg-inline-to-result (inline note)
"Return the result of evaluating INLINE with NOTE as context."
(let ((f (intern (format
(pcase (ekg-inline-type inline)
('command "ekg-inline-command-%s")
('note "ekg-display-note-%s")
(_ (error "Unknown inline type %s" (ekg-inline-type inline))))
(car (ekg-inline-command inline))))))
(if (fboundp f)
(pcase (ekg-inline-type inline)
('command (apply f (cdr (ekg-inline-command inline))))
('note (progn
(unless note
(error "No note supplied for display inline"))
(apply f note (cdr (ekg-inline-command inline))))))
(format "%%Unknown command %s: `%s' not found"
(car (ekg-inline-command inline)) (symbol-name f)))))
(defun ekg-insert-inlines-results (text inlines note)
"Return the results of executing INLINES into TEXT.
NOTE is the `ekg-note' that needs to exist for the `display'
inlines."
(ekg-insert-inlines-and-process
text inlines
(lambda (inline) (ekg-inline-to-result inline note))))
(defun ekg--transclude-titled-note-completion ()
"Completion function for file transclusion."
(let ((begin (save-excursion
(search-backward ">t" (line-beginning-position) t)
(+ 2 (point))))
(end (point)))
(when (<= begin end)
(list begin end
(completion-table-dynamic (lambda (_)
(mapcar (lambda (title-cons)
(cons (cdr title-cons)
(car title-cons)))
(ekg-document-titles))))
:exclusive t :exit-function #'ekg--transclude-cap-exit))))
(defun ekg--transclude-cap-exit (completion finished)
"Clean up CAP after COMPLETION.
FINISHED is non-nil if the completion has been finished by the
user."
(when finished
(save-excursion
(let* ((docs (mapcar (lambda (title-cons)
(cons (cdr title-cons)
(car title-cons)))
(ekg-document-titles)))
(id (cdr (assoc completion docs #'equal))))
(unless id (error "No document with title %s" completion))
(when (search-backward (format ">%s" completion) (line-beginning-position) t)
(replace-match (format "%%(transclude-note %S)" id)))))))
(defun ekg-display-note-id (note &optional force)
"Show the id of NOTE, if it is interesting.
Interesting is defined by whether it has meaning in itself.
However, if FORCE is non-nil, it will be shown regardless."
(if (or force
(ekg-should-show-id-p (ekg-note-id note)))
(propertize
(format "[%s]\n" (ekg-note-id note))
'face 'ekg-resource)
""))
(defun ekg-display-note-text (note &optional numwords plaintext)
"Return text, with mode-specific properties, of NOTE.
NUMWORDS is the max number of words to display in the note, or
nil for all words.
If PLAINTEXT is non-nil, return the text without any formatting."
(with-temp-buffer
(when (ekg-note-text note)
(insert (ekg-insert-inlines-results
(ekg-note-text note)
(ekg-note-inlines note)
note)))
(when (and (not plaintext) (ekg-note-mode note))
(let ((mode-func (intern (format "%s-mode" (ekg-note-mode note)))))
(if (fboundp mode-func) (funcall mode-func)
(funcall (ekg-note-mode note)))))
(mapc #'funcall ekg-format-funcs)
(unless plaintext
(font-lock-ensure)
(put-text-property (point-min) (point-max) 'ekg-note-id (ekg-note-id note)))
(concat (string-trim-right
(let ((buftext (if plaintext
(buffer-substring-no-properties (point-min)(point-max))
(buffer-string))))
(if numwords
(ekg-truncate-at buftext (or numwords ekg-note-inline-max-words))
buftext))) "\n")))
(defun ekg-display-note-tagged (note)
"Return text of the tags of NOTE."
(concat (mapconcat (lambda (tag) (propertize tag 'face 'ekg-tag))
(ekg-note-tags note) " ") "\n"))
(defun ekg-display-note-time-tracked (note &optional format-str)
"Return text of the times NOTE was created and modified.
FORMAT-STR controls how the time is formatted."
(let ((format-str (or format-str "%Y-%m-%d")))
(format "Created: %s Modified: %s\n"
(format-time-string format-str (ekg-note-creation-time note))
(format-time-string format-str (ekg-note-modified-time note)))))
(defun ekg-display-note-titled (note)
"Return text of the title of NOTE."
(if-let (titles (plist-get (ekg-note-properties note) :titled/title))
(propertize (concat (mapconcat #'identity titles ", ") "\n")
'face 'ekg-title)
""))
(defun ekg-inline-command-transclude-note (id &optional numwords)
"Return the text of ID.
NUMWORDS is the maximum number of words to use."
(string-trim-right (ekg-display-note-text (ekg-get-note-with-id id) numwords) "\n"))
(defun ekg-inline-command-transclude-file (file &optional numwords)
"Return the contents of FILE.
NUMWORDS is the maximum number of words to use."
(with-temp-buffer
(insert-file-contents file)
(ekg-truncate-at (buffer-string) (or numwords ekg-note-inline-max-words))))
(defun ekg-inline-command-transclude-website (url &optional numwords)
"Return the contents of the URL.
NUMWORDS is the maximum number of words to use."
(let ((url-buffer (url-retrieve-synchronously url)))
(with-current-buffer url-buffer
(goto-char (point-min))
(re-search-forward "^$" nil 'move) ; skip headers
(shr-render-region (point) (point-max))
(ekg-truncate-at (buffer-string) (or numwords ekg-note-inline-max-words)))))
(defun ekg-select-note ()
"Select a note interactively.
Returns the ID of the note."
(if (y-or-n-p "Select note by title? ")
(let* ((title-id-pairs (mapcar (lambda (note) (cons (cdr note) (car note)))
(ekg-document-titles)))
(selected-title (completing-read "Title: " title-id-pairs nil t)))
(cdr (assoc selected-title title-id-pairs)))
(let* ((notes (ekg-get-notes-with-tag
(completing-read "Tag: " (ekg-tags) nil t)))
(completion-pairs (mapcar
(lambda (note)
(cons (ekg-display-note-text note 10)
note)) notes)))
(ekg-note-id (cdr (assoc (completing-read "Note: " completion-pairs nil t)
completion-pairs))))))
(defun ekg-edit-add-inline ()
"Add an inline command to the current note."
(interactive)
(let ((command (completing-read
"Add inline: "
(mapcar (lambda (name)
(string-remove-prefix "ekg-inline-command-" name))
(seq-difference
(mapcar #'symbol-name
(apropos-internal "^ekg-inline-command-"))
'("ekg-inline-command--cmacro")))))
(args))
(pcase command
("transclude-note" (setq args (list (ekg-select-note))))
("transclude-file" (setq args (list (read-file-name "File: ")))))
(insert (format "%%%S" (cons (intern command) args)))))
(defun ekg-note-snippet (note &optional max-length)
"Return a short snippet for NOTE.
The snippet is just the beginning of the text, cut off after
MAX-LENGTH characters, with ellipses afterwards. If MAX-LENGTH is
not supplied, we use a default of 10."
(let ((display-length (min (length (ekg-note-text note)) (or max-length 10))))
(format "%s%s" (substring-no-properties (ekg-note-text note) 0 display-length)
(if (> (length (ekg-note-text note)) display-length) "…" ""))))
(defun ekg--kill-buffer-query-function ()
"Action to take after a kill is run on a capture or edit buffer.
If final result returns t, the buffer will be killed. If it
returns nil, the buffer will be left open. This always returns t
for saved buffers."
(or (not ekg-confirm-on-buffer-kill)
(not (buffer-modified-p))
(yes-or-no-p "Unsaved changes exist, do you still want to exit? ")))
(defun ekg--markdown-follow-thing-at-point (arg)
"Follow the thing at point, including ekg wiki links.
ARG is the prefix argument, if used it opens in another window."
(interactive "P")
(if (markdown-wiki-link-p)
(let* ((first-char (string-to-char (markdown-wiki-link-link)))
(symbol (when (member first-char (mapcar #'car ekg-inline-custom-tag-completion-symbols))
first-char)))
(when arg
(other-window 1))
(ekg-show-notes-with-tag (if symbol
(ekg--add-prefix-to-inline-tag
(substring (markdown-wiki-link-link) 1)
(char-to-string symbol))
(markdown-wiki-link-link))))
(markdown-follow-thing-at-point arg)))
(defun ekg--set-local-variables ()
"Set some common local variables."
(setq-local
completion-at-point-functions
(append (list #'ekg--capf #'ekg--transclude-titled-note-completion
#'ekg--inline-tag-completion)
completion-at-point-functions)
kill-buffer-query-functions
(append (list #'ekg--kill-buffer-query-function)
kill-buffer-query-functions)
header-line-format (ekg--header-line-format))
(add-hook 'pre-command-hook #'ekg-narrow-for-command nil t)
(add-hook 'post-command-hook #'ekg-unnarrow-for-command nil t)
(when (eq major-mode 'markdown-mode)
(setq-local markdown-enable-wiki-links t
markdown-wiki-link-fontify-missing nil)
;; We can't redefine how wiki links are handled, and we shouldn't use
;; advice, so instead we create a new function that handles wiki links, and
;; use it in keybindings.
(define-key (current-local-map)
[remap markdown-follow-thing-at-point]
#'ekg--markdown-follow-thing-at-point)))
(defvar ekg-capture-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-c" #'ekg-capture-finalize)
(define-key map "\C-c\C-k" #'ekg-capture-abort)
(define-key map "\C-c#" #'ekg-edit-add-inline)
(substitute-key-definition #'save-buffer #'ekg-save-draft map global-map)
map)
"Key map for `ekg-capture-mode', a minor mode.
This is used when capturing new notes.")
(define-minor-mode ekg-capture-mode
"Minor mode for simple finish/cancel keybindings."
:init-value nil
:lighter " EKG-CAP"
:interactive nil
(ekg--set-local-variables))
(defvar ekg-capture-mode-hook nil
"Hook for `ekg-capture-mode'.")
(defvar ekg-edit-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-c" #'ekg-edit-finalize)
(define-key map "\C-c#" #'ekg-edit-add-inline)
(substitute-key-definition #'save-buffer #'ekg-edit-save map global-map)
map)
"Key map for `ekg-edit-mode', a minor mode.
This is used when editing existing notes.")
(define-minor-mode ekg-edit-mode
"Minor mode for simple finish/cancel keybindings."
:init-value nil
:lighter " EKG-ED"
:interactive nil)
(defvar-local ekg-note nil
"Holds the note information for buffers adding or changing notes.")
(defvar-local ekg-note-orig-note nil
"Holds the original note before edit.")
(defvar-local ekg-note-orig-id nil
"Holds the original ID (subject) for this note.
This is needed to identify references to refresh when the subject is changed.")
(defvar-local ekg-note-orig-fields nil
"Holds the fields that were populated when the note was loaded.")
(defvar-local ekg-note-auto-narrowed nil
"If we are currently in an auto-narrowed state.
This happens when a command is run that likely needs to see a
narrowed part of the buffer. It is only non-nil when running a
command that needs to be narrowed for it, and it is needed so we
can keep track of whether we need to unnarrow or not.")
(defvar ekg-notes-mode-map
(let ((map (make-keymap)))
(suppress-keymap map t)
(define-key map "A" #'ekg-notes-any-tags)
(define-key map "a" #'ekg-notes-any-note-tags)
(define-key map "c" #'ekg-notes-create)
(define-key map "d" #'ekg-notes-delete)
(define-key map "g" #'ekg-notes-refresh)
(define-key map "n" #'ekg-notes-next)
(define-key map "o" #'ekg-notes-open)
(define-key map "b" #'ekg-notes-browse)