forked from ahyatt/ekg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ekg.el
2210 lines (1994 loc) · 92.4 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-2023 Andrew Hyatt <[email protected]>
;; Author: Andrew Hyatt <[email protected]>
;; Homepage: https://github.com/ahyatt/ekg
;; Package-Requires: ((triples "0.3.5") (emacs "28.1") (llm "0.4.0"))
;; Keywords: outlines, hypermedia
;; Version: 0.4.3
;; 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")
;;; 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-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-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)
(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))
"Metadata fields to functions for updating data based on buffer text.
Each function updates the buffer's `ekg-note' with the results of
the field. 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.")
(defconst ekg-property-multivalue-type '(("Tags" . comma)
("Title" . line))
"Defines per type how 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.4.3"
"The version of ekg, 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))
;; `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
ekg-document-titles 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))
(triples-add-schema ekg-db 'named 'name)
(triples-add-schema ekg-db 'email 'address)
;; Person is just a marker
(triples-add-schema ekg-db 'person)
;; 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-save-note (note)
"Save NOTE in database, replacing note information there."
(ekg-connect)
(ekg--normalize-note 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))
;; update ekg-document-titles
(when ekg-document-titles
(let* ((id (ekg-note-id note))
(new-title (plist-get (ekg-note-properties note) :titled/title)))
(setq ekg-document-titles
(nconc (assoc-delete-all id ekg-document-titles)
(mapcar (lambda (title) (cons id title)) new-title)))))
(run-hook-with-args 'ekg-note-save-hook note))
(triples-backups-maybe-backup ekg-db (ekg-db-file))
(set-buffer-modified-p nil))
(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)
(or (not (member ekg-draft-tag (ekg-note-tags note)))
(member ekg-draft-tag tags)))
(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)
"Move NOTE to the trash.
This prepends all tags with `trash/'. This can be garbage
collected at a later time. If all tags are already trash tags,
then the note is really deleted."
(ekg-connect)
(triples-with-transaction
ekg-db
(if (seq-every-p #'ekg-tag-trash-p (ekg-note-tags note))
(ekg-note-delete note)
(setf (ekg-note-tags note)
(mapcar (lambda (tag) (if (ekg-tag-trash-p tag)
tag
(ekg-mark-trashed tag)))
(ekg-note-tags note)))
(ekg-save-note note)))
(triples-backups-maybe-backup ekg-db (ekg-db-file))
(when ekg-document-titles
(setq ekg-document-titles
(assoc-delete-all (ekg-note-id note) ekg-document-titles #'equal))))
(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."
(and (not (member tag (list
;; ekg-draft-tag
ekg-template-tag
ekg-function-tag)))
(not (ekg-tag-trash-p 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."
(and (not (seq-every-p (lambda (tag) (ekg-tag-trash-p tag))
(ekg-note-tags note)))
;; (not (member ekg-draft-tag (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-has-live-tags-p (sub)
"Return non-nil if SUB represents an undeleted note."
(ekg-connect)
(seq-filter (lambda (tag) (not (ekg-tag-trash-p 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
(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."
(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)))
(with-temp-buffer
(insert
(propertize
(format "[%s]\n" (ekg-note-id note))
'face 'ekg-resource))
(put-text-property (point-min) (point-max) 'ekg-note-props
`(:id ,(ekg-note-id note)))
(buffer-string))
""))
(defun ekg-display-note-text (note &optional numwords)
"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."
(with-temp-buffer
(if (ekg-note-text note)
(insert (ekg-insert-inlines-results
(ekg-note-text note)
(ekg-note-inlines note)
note))
(insert " "))
(when (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)
(font-lock-ensure)
(insert "\n")
(put-text-property (point-min) (point-max) 'ekg-note-props
`(:text ,(ekg-note-text note)))
;; TODO: how to make trim/truncate work with props
;; (concat (ekg-truncate-at (buffer-string) (or numwords ekg-note-inline-max-words)) "\n")
(buffer-string)))
(defun ekg-display-note-tagged (note)
"Return text of the tags of NOTE."
(with-temp-buffer
(insert (mapconcat (lambda (tag) (propertize tag 'face 'ekg-tag))
(ekg-note-tags note) " ")
"\n")
(put-text-property (point-min) (point-max) 'ekg-note-props
`(:tags ,(ekg-note-tags note)))
(buffer-string)))
(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 (title (plist-get (ekg-note-properties note) :titled/title))
(with-temp-buffer
(insert
(propertize
(concat (mapconcat #'identity title "\n") "\n")
'face 'ekg-title))
(put-text-property (point-min) (point-max) 'ekg-note-props
`(:titled/title ,title))
(buffer-string))
""))
(defun ekg-inline-command-transclude-note (id &optional numwords)
"Return the text of ID."
(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."
(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."
(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: "))))
("transclude-website" (setq args (list (read-from-minibuffer "Url: " )))))
(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) "…" ""))))
(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#" #'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
(when ekg-capture-mode
(setq-local completion-at-point-functions
(append (list #'ekg--capf #'ekg--transclude-titled-note-completion)
completion-at-point-functions)
header-line-format
(substitute-command-keys
"\\<ekg-capture-mode-map>Capture buffer. Finish \
`\\[ekg-capture-finalize]'."))))
(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 ekg-edit-mode-hook nil
"Hook for `ekg-edit-mode'.")
(defvar-local ekg-note nil
"Holds the note information for buffers adding or changing notes.")
(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 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 "u" #'ekg-notes-update-info-at-point)
(define-key map "w" #'ekg-notes-copy-info-at-point)
(define-key map "n" #'ekg-notes-next)
(define-key map "o" #'ekg-notes-open)
(define-key map "b" #'ekg-notes-browse)
(define-key map "B" #'ekg-notes-select-and-browse-url)
(define-key map "p" #'ekg-notes-previous)
(define-key map "t" #'ekg-notes-tag)
(define-key map "q" #'kill-current-buffer)
(define-key map "k" #'ekg-notes-kill)
map))
(define-derived-mode ekg-notes-mode fundamental-mode "ekg-notes"
"Major mode for showing a list of notes that can be interacted with."
(setq buffer-read-only t)
(setq truncate-lines t)
(visual-line-mode 1)
(if (eq ekg-capture-default-mode 'org-mode)
(progn
(require 'org)
(define-key ekg-notes-mode-map "\C-c\C-o" #'org-open-at-point)))
(add-hook 'post-command-hook #'ekg--note-highlight nil t))
(defvar-local ekg-notes-fetch-notes-function nil
"Function to call to fetch the notes that define this buffer.
The order the notes are returned in is the order that they are
displayed.")
(defvar-local ekg-notes-name ""
"Name displayed at the top of the buffer.")
(defvar-local ekg-notes-ewoc nil
"Ewoc for the notes buffer.")
(defvar-local ekg-notes-hl nil
"Highlight for the notes buffer.")
(defvar-local ekg-notes-tags nil
"List of associated tags for creating and removing notes.")
(cl-defun ekg-note-create (&key text mode tags properties id)
"Create a new `ekg-note' with TEXT, MODE, TAGS, PROPERTIES and ID."
(let* ((time (time-convert (current-time) 'integer))
(id (or id (ekg--generate-id)))
(text (or text ""))
(mode (or mode ekg-capture-default-mode)))
(make-ekg-note :id id
:text text
:mode mode
:tags tags
:creation-time time
:modified-time time
:properties properties)))
(defun ekg--metadata-string-to-tag (s)
"Return string S as a tag."
(replace-regexp-in-string (rx ?\") "" s))
(defun ekg--metadata-string (property value)
"Return a representation of PROPERTY with VALUE for the metadata.
This will be displayed at the top of the note buffer."
(format "%s%s%s"
(concat
(propertize (concat property ":") 'face 'bold 'read-only t)
(propertize " " 'read-only t 'rear-nonsticky t))
value
(propertize "\n" 'read-only t 'rear-nonsticky t)))
(defun ekg-should-show-id-p (id)
"Return non-nil if the note ID should be shown to the user.
True when the ID represents a meaningful resource to the user,
rather than an auto-generated number."
(not (numberp id)))
(defun ekg--replace-metadata ()
"Replace the metadata in a buffer."
(let ((note ekg-note))
(with-temp-buffer
(when (ekg-should-show-id-p (ekg-note-id note))
(insert (ekg--metadata-string "Resource" (ekg-note-id note))))
(insert
(ekg--metadata-string "Tags"
(mapconcat (lambda (tag) (format "%s" tag))
(ekg-note-tags note) ", ")))
(map-apply (lambda (k v)
(when-let ((label (assoc-default k ekg-metadata-labels)))
(if (listp v)
(pcase (assoc-default label ekg-property-multivalue-type)
('line (cl-loop for val in v do
(insert (ekg--metadata-string label val))))
('comma (insert (ekg--metadata-string
label
(if (listp v)
(mapconcat (lambda (v) (format "%s" v))
v ", ")
(format "%s" v)))))))))
(ekg-note-properties note))
(buffer-string))))
(defun ekg--metadata-overlay ()
"Return the overlay used for metadata."
(or (car (seq-filter
(lambda (o) (eq 'ekg-metadata (overlay-get o 'category)))
(overlays-in (point-min) (point-max))))
(make-overlay (point-min) (point-max) nil nil t)))
(defun ekg--metadata-on-insert-behind (_ after begin-mod end-mod &optional _)
"Make sure nothing is inserted behind the metadata overlay.
Also make sure we always have a line with which the user can add text."
(when after
(delete-region begin-mod end-mod)
(when (= (point) (point-max))
(insert "\n"))))
(defun ekg--metadata-modification (overlay after _ _ &optional _)
"Make sure the metadata region doesn't interfere with editing.
This function is called on modification within the metadata.
BEGIN-MOD and END-MOD are the beginning and end points of the
modification by the user.
We want to make sure of a few things:
1) The user isn't adding more than one empty line.
2) There is at least one non-metadata line in the buffer.
Argument OVERLAY is the overlay whose modification triggers this
method. Argument AFTER is non-nil if method is being called