-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathekg.el
More file actions
2743 lines (2471 loc) · 115 KB
/
ekg.el
File metadata and controls
2743 lines (2471 loc) · 115 KB
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-2026 Andrew Hyatt <ahyatt@gmail.com>
;; Author: Andrew Hyatt <ahyatt@gmail.com>
;; Homepage: https://github.com/ahyatt/ekg
;; Package-Requires: ((triples "0.6.1") (emacs "28.1") (llm "0.30.0") (vui "1.0.0"))
;; Keywords: outlines, hypermedia
;; Version: 0.9.0
;; 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 'triples-fts)
(require 'seq)
(require 'vui)
(declare-function vui-rerender "vui")
(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")
(declare-function ekg-org-view--note-at-point "ekg-org")
;;; 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-hidden-tags (list ekg-trash-tag ekg-draft-tag)
"List of tags that are hidden from normal view.
These tags are treated similarly to the draft and trash tags, in
that notes with these tags are filtered out of most views unless
specifically requested.
Modules can add to this list to hide their internal tags."
:type '(repeat string)
: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-truncation-method 'word
"Method used for truncating text.
Possible values are:
- `word': Truncate text based on word count (default).
- `character': Truncate text based on character count.
This affects functions like `ekg-truncate-at' and text
selection for embeddings."
:type '(choice (const :tag "Word-based" word)
(const :tag "Character-based" character))
: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.
Most formatters can take a maximum number of words to output, and a
format list, which is a list of desired output properties as symbols.
Right now, the possibilities are:
- `oneline', the results must be on one line only.
- `plaintext', the results must not have string properties.
For OTHER formatters, everything must
be standardized to take just the number of words and output properties,
which are applied to each non-standard property in turn."
:type 'string
:group 'ekg)
(defcustom ekg-oneliner-note-template "%n(tagged 20 oneline) %n(titled 20 oneline) %n(text 40 oneline)"
"Template for displaying one-line notes in a note buffer.
Otherwise the same format as `ekg-display-note-template'."
:type 'string
:group 'ekg)
(defcustom ekg-notes-display-images t
"Whether images are displayed by default."
:type 'boolean
:group 'ekg)
(defcustom ekg-header-hidden-properties nil
"List of property keywords to hide from the header line.
Each element should be a keyword like `:embedding/embedding'.
Packages that add properties unsuitable for display (such as
large vectors or internal IDs) should add entries via `add-to-list'."
:type '(repeat symbol)
:group 'ekg)
(defvar ekg-property-format-functions nil
"Alist mapping property keywords to formatting functions.
Each entry is (PROPERTY . FUNCTION), where PROPERTY is a keyword
like `:org/scheduled' and FUNCTION takes a single value and
returns a string representation. Used in the header line to
format property values for display.")
(defcustom ekg-save-no-message nil
"Non-nil means do not print any message when saving."
:type 'boolean
:group 'ekg)
(defcustom ekg-search-max-results 20
"Maximum number of results to show in the `ekg-search' preview."
:type 'integer
: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)
(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.")
(defvar ekg-db nil
"Live sqlite database connection.")
(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.")
(defvar ekg-query-pred-abbrevs '(("tag" . "tagged/tag")
("title" . "titled/title")
("text" . "text/text"))
"Abbreviations for predicates in queries.")
(defconst ekg-version "0.8.0"
"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)
(triples-set-type ekg-db 'ekg 'ekg :version (version-to-list ekg-version)))
;; Always run upgrades — they are idempotent and
;; handle invariants that may need to be
;; re-established even without a version change.
(ekg-upgrade-db (plist-get ekg-plist :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-property '(name :base/type string :base/unique t))
(triples-set-type ekg-db 'tagged/tag 'ekg-property :name "Tags")
(triples-set-type ekg-db 'titled/title 'ekg-property :name "Title")
;; The `ekg-note-type' type is a signifier that the type is safe to delete
;; when a note is deleted.
(triples-add-schema ekg-db 'ekg-note-type)
(dolist (type '(text time-tracked inline titled tagged))
(triples-set-type ekg-db type 'ekg-note-type))
(triples-add-schema ekg-db 'ekg '(version :base/type cons :base/unique t))
(run-hooks 'ekg-add-schema-hook))
(defun ekg-property-name-for (prop)
"Return the human-readable name for property PROP.
PROP is a keyword such as :tagged/tag. If there is no name,
return the symbol converted to a string (without the colon)."
(ekg-connect)
(let ((decoloned-prop (triples--decolon prop)))
(or (plist-get (triples-get-type ekg-db decoloned-prop 'ekg-property) :name)
(symbol-name decoloned-prop))))
(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--plist-without-key (plist key)
"Return a copy of PLIST with all occurrences of KEY removed."
(let (result)
(while plist
(if (eq (car plist) key)
(setq plist (cddr plist))
(push (car plist) result)
(push (cadr plist) result)
(setq plist (cddr plist))))
(nreverse result)))
(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--virtual-reversed-p (combined-prop)
"Return non-nil if COMBINED-PROP is a virtual-reversed property."
(plist-get (triples-properties-for-predicate ekg-db combined-prop)
:base/virtual-reversed))
(defun ekg--filter-virtual-reversed (properties)
"Return PROPERTIES with virtual-reversed properties removed."
(let ((result nil))
(cl-loop for (key value) on properties by #'cddr do
(let ((cpred (intern (substring (symbol-name key) 1))))
(unless (ekg--virtual-reversed-p cpred)
(setq result (plist-put result key value)))))
result))
(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)
(let ((saveable-props (ekg--filter-virtual-reversed
(ekg-note-properties 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)))
;; Ensure creation-time is set; it may be nil if the note was
;; retrieved from a DB that lacks the time-tracked type for
;; this note (e.g., notes created by external tools).
(unless (ekg-note-creation-time note)
(setf (ekg-note-creation-time note) modified-time))
(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) saveable-props)
;; 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 saveable-props 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 returned are sorted in reverse chronological order."
(ekg-connect)
(sort
(seq-uniq (mapcan (lambda (tag) (ekg-get-notes-with-tag tag))
(seq-difference tags
(append (list ekg-function-tag)
;; Don't include hidden tags unless specifically requested.
(seq-difference ekg-hidden-tags tags)))))
#'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 hidden notes unless they are specifically requested.
(not
(seq-some (lambda (hidden-tag)
(and (not (member hidden-tag tags))
(member hidden-tag (ekg-note-tags note))))
ekg-hidden-tags)))
(delq nil
(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-get-notes-with-parent-tag (tag)
"Get all notes with TAG or any of its subtags."
(ekg-connect)
(seq-uniq
(apply #'append
(ekg-get-notes-with-tag tag)
(mapcar #'ekg-get-notes-with-tag
(ekg-tags-with-prefix (format "%s/" tag))))
(lambda (a b)
(equal (ekg-note-id a) (ekg-note-id b)))))
(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 note with ID, or nil if it does not exist."
(ekg-connect)
(let ((v (triples-get-subject ekg-db id)))
(when v
(let* ((stored-types (triples-get-types ekg-db id))
(core-types '(text time-tracked inline titled tagged))
(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)))
;; Query extension types not already returned by
;; triples-get-subject, to pick up virtual reversed
;; properties like org/children.
(extra-props
(mapcan (lambda (type)
(unless (or (memq type stored-types)
(memq type core-types))
(let ((type-data (triples-get-type ekg-db id type)))
(when type-data
(cl-loop for (k v) on type-data by #'cddr
nconc (list (intern (format ":%s/%s" type
(substring (symbol-name k) 1)))
v))))))
(ekg-note-cotypes))))
(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 (append
(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)
extra-props))))))
(defun ekg-get-notes-with-title (title)
"Get a list of note structs with TITLE."
(ekg-connect)
(delq nil (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-cotypes ()
"Return the list of triple types that are owned by notes."
(triples-subjects-of-type ekg-db 'ekg-note-type))
(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-note-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
(append (list ekg-template-tag ekg-function-tag)
ekg-hidden-tags))))
(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 ekg-hidden-tags (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 exists, is not a draft, and has
at least one non-trash tag."
(ekg-connect)
(let ((note (ekg-get-note-with-id id)))
(and note (ekg-note-active-p note))))
(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 num &optional truncation-indicator)
"Return S truncated, with TRUNCATION-INDICATOR.
Truncation method depends on `ekg-truncation-method'.
If `ekg-truncation-method' is `word', NUM is the number of words.
If `ekg-truncation-method' is `character', NUM is the number of
characters. If NUM is greater than the number of
words/characters of S, return S unchanged.
TRUNCATION-INDICATOR is the string to append when truncation
occurs. If nil, defaults to \"...\" (ellipsis)."
(with-temp-buffer
(insert s)
(goto-char (point-max))
(cond
((eq ekg-truncation-method 'word)
(when (> (count-words (point-min) (point-max)) num)
(goto-char (point-min))
(cl-loop with i = 0 while (and (< i num)
(skip-syntax-forward "w")
(skip-syntax-forward "._-()\"\\"))
do (cl-incf i))
;; Move back to the end of the last word/character
(skip-syntax-backward "-")))
((eq ekg-truncation-method 'character)
(when (> (- (point-max) (point-min)) num)
(goto-char (min (point-max) (+ (point-min) num))))))
(when (< (point)
(save-excursion
(goto-char (point-max))
(skip-syntax-backward "-")
(point)))
(insert (or truncation-indicator "…")))
(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--format (text numwords format)
"Format TEXT, only displaying NUMWORDS.
FORMAT is a list of formatting options. See `ekg-display-note-template'
for details."
(let ((unformatted (concat (string-trim-right
(ekg-truncate-at text
(or numwords ekg-note-inline-max-words))) "\n")))
(dolist (f format)
(pcase f
('oneline (setq unformatted (string-replace "\n" " " unformatted)))
('plaintext (setq unformatted (substring-no-properties unformatted)))))
unformatted))
(defun ekg-display-note-text (note &optional numwords &rest format)
"Return text, with mode-specific properties, of NOTE.
NUMWORDS and FORMAT are standard options, see
`ekg-display-note-template' for details."
(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 (member 'plaintext format)) (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 (member 'plaintext format)
(font-lock-ensure)
(put-text-property (point-min) (point-max) 'ekg-note-id (ekg-note-id note)))
(ekg-display--format (buffer-string) numwords format)))
(defun ekg-display-note-tagged (note &optional numwords &rest format)
"Return text of the tags of NOTE.
NUMWORDS and FORMAT are as described in `ekg-display-note-template'."
(ekg-display--format
(concat (mapconcat (lambda (tag) (propertize tag 'face 'ekg-tag))
(ekg-note-tags note) " ") "\n")
numwords format))
(defun ekg-display-note-time-tracked (note &optional format-str &rest format)
"Return text of the times NOTE was created and modified.
FORMAT-STR controls how the time is formatted.
FORMAT is the list of
desired output properties as described in `ekg-display-note-template'."
(let ((format-str (or format-str "%Y-%m-%d")))
(format "Created: %s Modified: %s%s"
(format-time-string format-str (ekg-note-creation-time note))
(format-time-string format-str (ekg-note-modified-time note))
(if (member 'oneline format)
"" "\n"))))
(defun ekg-display-note-titled (note &optional numwords &rest format)
"Return text of the title of NOTE.
NUMWORDS and FORMAT are as described in `ekg-display-note-template'."
(ekg-display--format (if-let (titles (plist-get (ekg-note-properties note)
:titled/title))
(propertize (concat (mapconcat #'identity titles ", ") "\n")
'face 'ekg-title)
"")
numwords format))
(defun ekg-inline-command-transclude-note (id &optional numwords)
"Return the text of ID.
NUMWORDS is the maximum number of words to use."
(if-let* ((note (ekg-get-note-with-id id)))
(string-trim-right (ekg-display-note-text note numwords) "\n")
(format "[Note %s not found]" id)))
(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? ")