/
jf-denote.el
1045 lines (933 loc) · 43 KB
/
jf-denote.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
;;; jf-denote.el --- Note taking -*- lexical-binding: t -*-
;; Copyright (C) 2022 Jeremy Friesen
;; Author: Jeremy Friesen <jeremy@jeremyfriesen.com>
;; This file is NOT part of GNU Emacs.
;;; Commentary:
;;;; Preamble
;;
;; Prior to `denote' I made extensive use of `org-roam'; I was following
;; `denote' development and appreciate Protesilaos's pedagological approach to
;; documentation. I also appreciate the design considerations; which I wrote
;; about here: https://takeonrules.com/2022/10/09/denote-emacs-configuration/
;;
;; I installed denote and began exploring. I am a software developer by trade,
;; and found the code accessible and discernable; that with it's sole dependency
;; being `emacs' I felt warranted further exploration. Accessible, discernable,
;; and no dependencies are attractive attributes of software that I use as my
;; tools of work and play. In my experience, the maintenance and enhancement is
;; easier for this kind of software.
;;
;; With further exploration, I migrated fully from `org-roam' to `denote'.
;;
;;
;;;; On Domains
;;
;; This package configures and extends `denote' by adding conceptual
;; domains to my note taking. The domains are larger demarcations than
;; simple tags. This is built on top of the `denote-directory' variable
;; and function.
;;
;; Further by leveraging domains, I have three means of searching:
;;
;; - "posts/" are all of my blog posts
;; - "-word" will find title's with "word" in them
;; - "_tag" will find the "tag" amongst the files keywords
;;
;; This allows me to leverage, if I want, Denote's siloing feature.
;;;; On Org Mode integration
;;
;; I make extensive use of `org-mode'; it is the format I use for crafting my
;; blog posts (see https://takeonrules.com). It is also the tool I use for my
;; day to day task tracking and time tracking.
;;
;; I have structured my workflow so that any of these day to day activities can
;; easily produce blog posts. I want my internal writing to have lots of
;; connective references; to help me find previous notes and perhaps look for
;; interesting connections.
;;
;; I also want posts that I publish to provide a similar experience; but the
;; links need to only be for publicly available connections. In other words,
;; when I export a blog post, any internal links that have an external proxy are
;; rendered as links to those external proxies. Any internal links without an
;; external proxy are rendered without links.
;;
;; This is done via `org-link-set-parameters' and denote's documentation (see
;; https://protesilaos.com/emacs/denote) provides excellent examples a
;; `org-link-set-parameters'.
;;; Code:
(require 'cl-lib)
;;;; Loading dependencies
(use-package denote
;; A narrow focus tool for organizing notes. I appreciate the design
;; constraints and lack of external dependencies. This package provides
;; portability. It sits as an alternate to the amazing `org-roam' package.
:straight (:host github :type git :repo "jeremyf/denote")
:commands (denote-directory
denote-file-prompt
denote--title-prompt
denote-get-path-by-id)
:bind ("H-l" . 'jf/denote/link-or-create)
("H-i" . 'jf/denote/link-or-create)
:hook (dired-mode . denote-dired-mode)
(org-mode . denote-rename-buffer-mode)
:init
(setq denote-known-keywords
(split-string-and-unquote
(shell-command-to-string
(concat
"rg \"#\\+TAG:\\s([\\w-]+)\" "
(expand-file-name "denote/glossary" org-directory)
" --only-matching"
" --no-filename "
" --replace '$1' | "
"ruby -ne 'puts $_.gsub(/^(\\w)\\w+-/) { |m| m[0].upcase + m[1..-1] }.gsub(/-(\\w)/) { |m| m[1].upcase }'"))
"\n"))
:custom
(denote-journal-extras-title-format 'day-date-month-year)
(denote-infer-keywords t)
(denote-excluded-punctuation-regexp "[][{}!@#$%^&*()=+'\"?,.|;:~`‘’“”/—–]*")
(denote-modules '(xref ffap))
(denote-org-capture-specifiers
"%(jf/denote/capture-wrap :link \"%L\" :content \"%i\")")
(denote-directory (expand-file-name "denote" org-directory))
;; These are the minimum viable prompts for notes
(denote-prompts '(title keywords))
;; I love ‘org-mode format; reading ahead I'm setting this
(denote-file-type 'org)
;; Our controlled vocabulary...if you will. This originally was a function
;; call, however there was a timing conflict with requiring denote-org-dblock
;; and when/where I declared the previous function. By "inlining" the
;; function, I remove that temporal dependency.
;; (split-string-and-unquote
;; (shell-command-to-string
;; (concat
;; "rg \"#\\+TAG:\\s([\\w-]+)\" "
;; (expand-file-name "denote/glossary" org-directory)
;; " --only-matching"
;; " --no-filename "
;; " --replace '$1' | "
;; "ruby -ne 'puts $_.gsub(/^(\\w)\\w+-/) { |m| m[0].upcase + m[1..-1] }.gsub(/-(\\w)/) { |m| m[1].upcase }'"))
;; "\n")
;; Explicitly ensuring that tags can be multi-word (e.g. two or more
;; words joined with a dash). Given that I export these tags, they
;; should be accessible to screen-readers. And without the dashes
;; they are a garbled word salad.
(denote-allow-multi-word-keywords nil)
;; And `org-read-date' is an amazing bit of tech
(denote-date-prompt-denote-date-prompt-use-org-read-date t)
(denote-file-name-slug-functions
'((title . jf/denote-sluggify-title)
(signature . jf/denote-sluggify-signature)
(keyword . jf/denote-sluggify-keyword)))
:config
(setq denote-file-name-letter-casing '((title . downcase)
(signature . downcase)
(keywords . verbatim)
(t . downcase)))
(defvar jf/diacritics-to-non-diacritics-map
'(("ž" . "z") ("Ž" . "Z")
("ý" . "y") ("ÿ" . "y") ("Ÿ" . "Y")
("š" . "s") ("Š" . "S")
("ñ" . "n") ("Ñ" . "N")
("ü" . "u") ("û" . "u") ("ú" . "u") ("ù" . "u")
("Ü" . "U") ("Û" . "U") ("Ú" . "U") ("Ù" . "U")
("ï" . "i") ("î" . "i") ("í" . "i") ("ì" . "i")
("Ï" . "I") ("Î" . "I") ("Í" . "I") ("Ì" . "I")
("Ð" . "D")
("ç" . "c") ("Ç" . "C")
("ð" . "e") ("ë" . "e") ("ê" . "e") ("é" . "e") ("è" . "e")
("Ë" . "E") ("Ê" . "E") ("É" . "E") ("È" . "E")
("ø" . "o") ("ö" . "o") ("õ" . "o") ("ô" . "o") ("ó" . "o") ("ò" . "o")
("Ø" . "O") ("Ö" . "O") ("Õ" . "O") ("Ô" . "O") ("Ó" . "O") ("Ò" . "O")
("å" . "a") ("ä" . "a") ("ã" . "a") ("â" . "a") ("á" . "a") ("à" . "a")
("Å" . "A") ("Ä" . "A") ("Ã" . "A") ("Â" . "A") ("Á" . "A") ("À" . "A")
("…" . "") ;; Ellipsis
("—" . "-") ;; em dash
("–" . "-") ;; en dash
)
"Map of diacritic to non-diacritic form.")
(defun jf/remove-diacritics-from (string)
"Remove the diacritics from STRING."
(when string
(cl-reduce (lambda (text diacritic-map-element)
(s-replace (car diacritic-map-element)
(cdr diacritic-map-element) text))
jf/diacritics-to-non-diacritics-map
:initial-value string)))
(defun jf/denote-sluggify-title (str)
(denote-sluggify-title (jf/remove-diacritics-from str)))
(defun jf/denote-sluggify-keyword (str)
(jf/remove-diacritics-from str))
(defun jf/denote-sluggify-signature (str)
(denote-sluggify-signature
(jf/remove-diacritics-from
(s-replace "=" "_" (s-replace "-" "_" str)))))
(defun jf/denote-link-ol-get-id ()
"Use `org-id-get' to find/create ID."
(org-id-get (point) 'create))
(defun jf/denote-sluggify (args)
(let ((type (car args))
(text (cadr args)))
(list type (cond
((eq 'title type) (jf/denote-sluggify-title text))
((eq 'signature type) (jf/denote-sluggify-signature text))
(t text)))))
(defun jf/denote-filename-is-note-p (filename)
"Return non-nil if FILENAME is a valid name for a Denote note.
For our purposes, its path must be part of the variable
`denote-directory', it must have a Denote identifier in its name,
and use one of the extensions implied by `denote-file-type'."
(and (or
(string-match-p "/Documents/denote-" (expand-file-name filename))
(string-prefix-p (denote-directory) (expand-file-name filename)))
(denote-file-has-identifier-p filename)
(denote-file-has-supported-extension-p filename)))
(advice-add #'denote-sluggify :filter-args #'jf/denote-sluggify)
(advice-add #'denote-link-ol-get-id :override #'jf/denote-link-ol-get-id)
(advice-add #'denote-filename-is-note-p :override #'jf/denote-filename-is-note-p))
(defun jf/dired-rename-files-to-denote-schema ()
"Rename marked files in `dired-mode'."
(interactive)
(when (seq-find (lambda (file)
(member (file-name-nondirectory file) '("." "..")))
(dired-get-marked-files))
(user-error "Can't rename \".\" or \"..\" files"))
(dolist (file (dired-get-marked-files))
(let ((current-prefix-arg nil))
(apply #'jf/rename-file-to-denote-schema
(list :file file :signature :prompt)))))
(cl-defun jf/rename-file-to-denote-schema (&key file id title keywords
dir date signature
force dry-run)
"Rename FILE using `denote' schema.
When `current-prefix-arg' is non-nil prompt for many of the parameters.
When no FILE is provided use `buffer-file-name'.
- DIR: target directory to move the file to; by default put the
new file in the same directory.
- ID: the identifier for this file, defaulting to one created via
`denote-create-unique-file-identifier'.
- TITLE: The tile for this file; default's to a
`s-titleized-words' of the given FILE's base name.
- KEYWORDS: A list of keywords to apply to the file. When passed
:none, skip prompting, via `denote-keywords-prompt'
for a list of keywords.
- SIGNATURE: The optional signature of the file. When passed
:none, skip prompting for a signature.
- DATE: When non-nil, use this date for creating the ID via
`denote-create-unique-file-identifier'.
- FORCE: When non-nil, rename the file without prompting to
confirm the change.
- DRY-RUN: When non-nil, do not perform the name change but
instead message the file's new name."
(interactive)
(let* ((file
(or file (if current-prefix-arg
(call-interactively (lambda (f)
(interactive "f") f))
(buffer-file-name))))
(title
(or title
(read-string "Title: "
(denote-desluggify-title
(or (denote-retrieve-filename-title file)
(s-titleized-words (f-base file)))))))
(id
(or (denote-retrieve-filename-identifier file)
id
(denote-create-unique-file-identifier
file (denote--get-all-used-ids) date)))
(keywords
(if (equal keywords :none)
'()
(or keywords
(denote-keywords-prompt))))
(signature
(or (when (equal signature :none) "")
(and signature (not (equal signature :prompt)))
(when (or current-prefix-arg (equal signature :prompt))
(completing-read "Signature: " (jf/tor-series-list)))
""))
(dir
(f-join
(or (and dir (not (equal dir :prompt)))
(if (or current-prefix-arg (equal dir :prompt))
(call-interactively (lambda (d)
(interactive "D") d))
(f-dirname file)))
"./"))
(extension
(f-ext file t))
(new-file (denote-format-file-name
dir id keywords title extension signature)))
(if dry-run
(message "Changing %S to %S" file new-file)
(when (or force (denote-rename-file-prompt file new-file))
(denote-rename-file-and-buffer file new-file)
(denote-update-dired-buffers)))
new-file))
(require 'denote-org-extras)
(require 'denote-journal-extras)
(add-to-list 'denote-templates '(journal . "#+CATEGORY: Journal"))
(add-to-list 'org-tags-exclude-from-inheritance denote-journal-extras-keyword)
(add-hook 'denote-journal-extras-hook #'jf/org-mode/agenda-files-update)
(use-package consult-notes
;;Let’s add another way at looking up files. I appreciate the ability to
;;search all files and start with a character (e.g. =b=) followed by <space>
;;to filter to the note source keyed as =s= (e.g. Scientist).
:straight (:type git :host github :repo "mclear-tools/consult-notes")
;; :after (consult denote)
:bind
("H-d s" . 'consult-notes-search-in-all-notes)
("H-f" . 'consult-notes)
;; Ensuring that I search my denote/scientist sub-directory, which is
;; excluded from it's containing project's git repository.
:custom (consult-notes-use-rg t)
(consult-notes-ripgrep-args
(concat
"rg --null --line-buffered --color=never --max-columns=1000 "
"--path-separator / --ignore-case --no-heading --line-number "
"--hidden --glob=!.git/ -L --sortr=accessed"))
:commands (consult-notes
consult-notes-search-in-all-notes))
;;;; Note taking configurations
;;;;; `denote' and `org-mode' integration
(cl-defun jf/denote/org-property-from-id (&key identifier property)
;; This function helps me retrieve Org-Mode properties from the given Denote
;; ID.
"Given an IDENTIFIER and PROPERTY return it's value or nil.
Return nil when:
- is not a `denote' file
- IDENTIFIER leads to a non `org-mode' file
- PROPERTY does not exist on the file"
(when-let ((filename (denote-get-path-by-id identifier)))
(when (string= (file-name-extension filename) "org")
(with-current-buffer (find-file-noselect filename)
(cadar (org-collect-keywords (list property)))))))
(cl-defun jf/denote/org-keywords-from-id (&key identifier keywords)
"Given an IDENTIFIER and KEYWORDS list return an a-list of values.
Return nil when:
- is not a denote file
- IDENTIFIER leads to a non `org-mode' file
- KEYWORD does not exist on the file.
This function is the plural version of `jf/denote/org-property-from-id'."
;; ;; Testing jf/denote/org-property-from-id
;; (message "%s" (jf/denote/org-property-from-id
;; :identifier "20220930T215235"
;; :property "ABBR"))
;; ;; Testing jf/denote/org-keywords-from-id
;; (message "%s" (jf/denote/org-keywords-from-id
;; :identifier "20220930T215235"
;; :properties '("TITLE" "ABBR")))
(when-let ((filename (denote-get-path-by-id identifier)))
(when (string= (file-name-extension filename) "org")
(with-current-buffer (find-file-noselect filename)
(org-collect-keywords keywords)))))
(defun jf/denote/plist-for-export-of-id (identifier)
"Given an IDENTIFIER export a `plist' with the following properties:
- :title
- :key
- :url
Return nil when:
- is not a denote file
- IDENTIFIER leads to a non `org-mode' file"
;; Testing
;; (message "%s" (jf/denote/plist-for-export-of-id "20221009T115949"))
(when-let ((filename (denote-get-path-by-id identifier)))
(when (string= (file-name-extension filename) "org")
(with-current-buffer (find-file-noselect filename)
(let ((kw-plist (jf/org-keywords-as-plist
:keywords-regexp "\\(TITLE\\|GLOSSARY_KEY\\|OFFER\\|ROAM_REFS\\|SAME_AS\\)")))
(list
:title (lax-plist-get kw-plist "TITLE")
:key (lax-plist-get kw-plist "GLOSSARY_KEY")
:url (or
(lax-plist-get kw-plist "OFFER")
(when-let ((refs (lax-plist-get kw-plist "ROAM_REFS")))
(if (listp refs)
(first (s-split " " refs t))
refs))
(lax-plist-get kw-plist "SAME_AS"))))))))
;;;;; `denote' file finding functions
(defun jf/denote/link-or-create (target &optional id-only)
"Use `denote-link' on TARGET file, creating it if necessary.
As `denote-link-or-create' but use `jf/denote/file-prompt'
instead of `denote-file-prompt'.
This function is intended for a global find of all notes. With
ID-ONLY link without title."
(interactive (list (jf/denote/file-prompt)
current-prefix-arg))
(if (and target (file-exists-p target))
(let ((type (denote-filetype-heuristics target)))
(denote-link target type
(denote--link-get-description target)
id-only)
)
(denote--command-with-title-history #'denote-link-after-creating)))
(defun jf/denote/file-prompt (&optional files-matching-regexp)
"Prompt for a file based on subdirectories.
See `denote-file-prompt'"
;; I’m not looking at active silo-ing and want to be able to search
;; specifically from the top-level and all subdirectories.
(when-let* ((vc-dirs-ignores (mapcar
(lambda (dir)
(concat dir "/"))
vc-directory-exclusion-list))
(files (mapcan
(lambda (sub-dir)
(project--files-in-directory (f-join
(denote-directory)
sub-dir)
vc-dirs-ignores))
jf/denote/subdirectories))
(file (funcall project-read-file-name-function
"Select note" files nil 'file-name-history)))
(let ((completion-ignore-case read-file-name-completion-ignore-case))
(add-to-history 'denote--file-history file)
file)))
(setq consult-notes-sources (list))
(setq jf/denote/subdirectories (list))
(defun jf/denote/find-file ()
"Find file in the current denote directory."
(interactive)
(require 'consult-projectile)
(require 'denote)
;; For this query, override the `projectile-git-command' so that I can
;; include my "denote/scientist" notes.
(let ((projectile-git-command
"git ls-files -zco --exclude-from=.projectile.gitignore"))
(consult-projectile--file (denote-directory))))
;;;;; Note taking Domains
(cl-defmacro jf/denote/create-functions-for (&key domain key (create-fn nil))
"A macro to CREATE-FN for the given DOMAIN.
The KEY is the ASCII value of the binding key.
Creates:
- Wrapping function of `jf/denote/find-file' that narrows results
to the given DOMAIN.
- Create linking function for DOMAIN.
- Add the domain to the `jf/denote/subdirectories'.
- Adds DOMAIN to `consult-notes-sources'."
(let* ((finder-fn (intern (concat "jf/denote/find-file--" domain)))
(subdirectory (f-join "~/git/org/denote" domain))
(finder-docstring (concat "Find file in \""
domain
"\" subdirectory of `denote-directory'."))
(default-create-fn (intern (concat "jf/denote/create--"
domain
"--default")))
(default-create-docstring (concat "Create denote in \""
domain
"\" subdirectory of "
"`denote-directory'."))
(link-or-creator-fn (intern (concat "jf/denote/link-or-create--" domain)))
(link-or-creator-docstring (concat "Link to denote in \""
domain
"\" subdirectory of "
"`denote-directory'.")))
`(progn
(add-to-list 'jf/denote/subdirectories ,domain)
(when (boundp 'consult-notes-sources)
(add-to-list 'consult-notes-sources '(,domain ,key ,subdirectory)))
(defun ,default-create-fn ()
,default-create-docstring
(interactive)
(let ((denote-directory (f-join (denote-directory) ,domain)))
(call-interactively #'denote)))
(bind-key (format "H-d c %c" ,key) (or ,create-fn ',default-create-fn))
(bind-key (format "H-d f %c" ,key) ',finder-fn)
(defun ,finder-fn ()
,finder-docstring
(interactive)
(let ((denote-directory (f-join (denote-directory) ,domain)))
(call-interactively #'jf/denote/find-file)))
(bind-key (format "H-d l %c" ,key) ',link-or-creator-fn)
(defun ,link-or-creator-fn ()
,link-or-creator-docstring
(interactive)
(let ((denote-directory (f-join (denote-directory) ,domain)))
(call-interactively #'denote-link-or-create)))
)))
;;;;;; Blog Posts
;; The blog-post domain is for things that I have, will, or might publish to
;; https://takeonrules.com
(jf/denote/create-functions-for :domain "blog-posts"
:key ?b)
(defun jf/denote/find-file--blog-posts-draft (filename)
"Find a draft FILENAME in the \"blog-posts\" sub-directory of denote-directory."
(interactive
(list (jf/find-file-via-matching
:prompt "Draft filename: "
:matching "^#\\+ROAM_REFS:"
:switch "--files-without-match"
:in (f-join (denote-directory) "blog-posts"))))
(find-file filename))
(bind-key "H-d f B" #'jf/denote/find-file--blog-posts-draft)
;;;;;; Scratch
(defun jf/denote/create-scratch (title)
"Create a scratch note with TITLE."
(interactive (list (read-string
"Scratch title: "
(format-time-string "%Y-%m-%d Scratch"))))
(denote title
nil
'org
(f-join (denote-directory) "scratch")))
;; The scratch domain is a place to capture random notes. These can be promoted
;; to another directory or eventually discarded.
(jf/denote/create-functions-for :domain "scratch"
:create-fn #'jf/denote/create-scratch
:key ?s)
;;;;;; Scientist
(jf/denote/create-functions-for :domain "work"
:key ?w)
(jf/denote/create-functions-for :domain "private"
:key ?v)
(jf/denote/create-functions-for :domain "journal"
:key ?j)
;;;;;; Epigraphs
(cl-defun jf/denote/create-epigraph (&key
(body (read-from-minibuffer
"Epigraph Text: "))
;; Todo prompt for Author Name
(author_name (read-from-minibuffer
"Author Name: "))
;; Todo prompt for Work Title
(work_title (read-from-minibuffer
"Work Title: "))
(nth-words 8))
"Create an epigraph from BODY, AUTHOR_NAME, and WORK_TITLE.
Default the note’s title to the first NTH-WORDS of the BODY."
(interactive)
(let* ((body-as-list (s-split-words body))
(title (s-join " " (if (> (length body-as-list) nth-words)
(cl-subseq body-as-list 0 nth-words)
body-as-list)))
(template (concat
;; The name of the author
"#+AUTHOR_NAME: " author_name "\n"
;; Where can you “find” this author?
"#+AUTHOR_URL:\n"
;; The GLOSSARY_KEY for the given author
"#+AUTHOR_KEY:\n"
;; What’s the title of the work?
"#+WORK_TITLE: " work_title "\n"
;; Where can you “get” this work?
"#+WORK_URL:\n"
;; The GLOSSARY_KEY for the given work
"#+WORK_KEY:\n"
;; Indicates if this is a poem (or not)
"#+POEM:\n"
;; The page in which this passage appears in the given
;; work.
"#+PAGE:\n"
;; The name of the translator
"#+TRANSLATOR_NAME:\n")))
(denote title
nil
'org
(f-join (denote-directory) "epigraphs")
nil
template)))
(jf/denote/create-functions-for :domain "epigraphs"
:key ?e
:create-fn 'jf/denote/create-epigraph)
;;;;;; Glossary Entries
(cl-defun jf/denote/create-glossary-entry
(&key
(title (read-from-minibuffer "Name the Entry: "))
(is-a-game (yes-or-no-p "Is this a game?"))
(abbr (read-from-minibuffer "Abbreviation (empty to skip): ")))
"Create a `denote' entry for the given TITLE and ABBR.
And if this IS-A-GAME then amend accordingly.
NOTE: At present there is no consideration for uniqueness."
(interactive)
(let* ((key (downcase (denote-sluggify-title (if (s-present? abbr) abbr title))))
(template (concat "#+GLOSSARY_KEY: " key "\n"
(when (s-present? abbr)
(concat "#+ABBR: " abbr "\n"))
;; TODO: Include a prompt of existing disclaimers
"#+CONTENT_DISCLAIMER:\n"
"#+DESCRIPTION:\n"
(when is-a-game (concat "#+GAME: " key "\n"))
"#+ITEMID:\n"
"#+ITEMTYPE:\n"
"#+MENTION_AS:\n"
"#+NO_TITLE:\n"
"#+OFFER:\n"
"#+PLURAL_ABBR:\n"
"#+PLURAL_TITLE:\n"
"#+SAME_AS:\n"
"#+TAG:\n" ;; TODO: Assert uniqueness
"#+VERBOSE_TITLE:\n"))
(keywords (list)))
;; Add both "abbr" and the abbr to the keywords; both help in searching
;; results
(when (s-present? abbr) (add-to-list 'keywords "abbr"))
(when is-a-game (add-to-list 'keywords "game"))
(denote title
keywords
'org
(f-join (denote-directory) "glossary")
nil
template
(when (s-present? abbr) (progn (denote-sluggify-signature abbr))))))
(jf/denote/create-functions-for :domain "glossary"
:key ?g
:create-fn 'jf/denote/create-glossary-entry)
;; Testing jf/denote/org-property-from-id
;; (message "%s" (jf/denote/org-property-from-id :id "20220930T215235"
;; :property "ABBR"))
;;;;;; Melange
;; All the other things; perhaps they could become blog posts, but for now they
;; remain part of the mixture and medley.
(jf/denote/create-functions-for :domain "melange"
:key ?m)
;;;;;; People
;; I do write notes about people I interact with. Technically I have glossary
;; entries for people. But those entries are for folks I don’t interact with.
(jf/denote/create-functions-for :domain "people"
:key ?p)
;;;;;; Indices
;; On my site I write https://takeonrules.com/series/. I track this data in a
;; YAML file; I’d like to treat this data similar to my glossary.
(cl-defun jf/denote/create-indices-entry (&key
(title (read-from-minibuffer
"Name the index: "))
(is-a-series
(yes-or-no-p
"Take on Rules series?")))
"Create a `denote' index entry for the given TITLE.
Consider different logic if IS-A-SERIES."
(interactive)
(let* ((keywords (list))
(template (concat (when (s-present? is-a-series)
"#+HIGHLIGHT: true\n"))))
(when (s-present? is-a-series)
(add-to-list 'keywords "series"))
(denote title
nil
'org
(f-join (denote-directory) "indices")
nil
template)))
(jf/denote/create-functions-for :domain "references"
:key ?r)
(jf/denote/create-functions-for :domain "indices"
:key ?i
:create-fn 'jf/denote/create-indices-entry)
;;;;; `org-link-parameters'
(cl-defun jf/org-link-complete-link-for (parg &key scheme filter subdirectory)
"Prompt for `denote' with filename FILTER in the given SUBDIRECTORY.
Returns a string of format: \"SCHEME:<id>\" where <id> is
an `denote' identifier.
PARG is part of the method signature for `org-link-parameters'."
(let* ((denote-directory (if subdirectory
(f-join (denote-directory)
(concat subdirectory "/"))
(denote-directory)))
(file (funcall project-read-file-name-function
"Select note: "
(denote-all-files)
;; Filter might be nil; if so pass nil. Otherwise the string.
(when filter (lambda (fname) (s-contains? filter fname t)))
'denote--file-history)))
;; This leverages a post v1.0.0 parameter of Denote
;; See https://git.sr.ht/~protesilaos/denote/commit/c6c3fc95c66ba093a266c775f411c0c8615c14c7
(concat scheme ":" (denote-retrieve-filename-identifier file))))
(cl-defun jf/denote/link-ol-abbr-with-property (link
description
format
protocol
&key
keyword
additional-hugo-parameters)
"Export a LINK with DESCRIPTION for the given PROTOCOL and FORMAT.
FORMAT is an Org export backend. We will discard the given
DESCRIPTION. PROTOCOL is ignored."
(let* ((keyword-alist (jf/denote/org-keywords-from-id
:identifier link
:keywords (list "TITLE" keyword "GLOSSARY_KEY")))
(title (car (alist-get "TITLE" keyword-alist nil nil #'string=)))
(keyword-value (car (alist-get keyword keyword-alist nil nil #'string=)))
(key (car (alist-get "GLOSSARY_KEY" keyword-alist nil nil #'string=))))
(cond
((or (eq format 'html) (eq format 'md))
(if jf/exporting-org-to-tor
(format "{{< glossary key=\"%s\" %s >}}"
key
additional-hugo-parameters)
(format "<abbr title=\"%s\">%s</abbr>"
title
keyword-value)))
((or (eq format 'latex) (eq format 'beamer))
(format "\\ac{%s}" keyword-value))
(t (format "%s (%s)"
title
keyword-value)))))
(org-link-set-parameters "abbr"
:complete (lambda (&optional parg)
(jf/org-link-complete-link-for
parg
:scheme "abbr"
:filter "_abbr"))
:export (lambda (link description format protocol)
(jf/denote/link-ol-abbr-with-property
link description format protocol
:keyword "ABBR"
:additional-hugo-parameters "abbr=\"t\""))
:face #'jf/org-faces-abbr
:follow #'denote-link-ol-follow
)
(org-link-set-parameters "abbr-plural"
:complete (lambda (&optional parg)
(jf/org-link-complete-link-for
parg
:scheme "abbr-plural"
:filter "_abbr"))
:export (lambda (link description format protocol)
(jf/denote/link-ol-abbr-with-property
link description format protocol
:keyword "PLURAL_ABBR"
:additional-hugo-parameters "abbr=\"t\" plural=\"t\""))
:face #'jf/org-faces-abbr
:follow #'denote-link-ol-follow
;;;; I'm unclear if/how I want to proceed with this
;; :store (lambda (jf/org-link-store-link-for :scheme "abbr-plural"))
)
(org-link-set-parameters "date"
:complete #'jf/denote/link-complete-date
:export #'jf/denote/link-export-date
:face #'jf/org-faces-date
:follow #'jf/denote/link-follow-date)
(cl-defun jf/denote/link-complete-date (&optional parg)
"Prompt for the given DATE.
While we are prompting for a year, month, and day; a reminder
that this is intended to be conformant with the TIME element.
But for my typical use I write these as either years; years and
months; and most often year, month, and days.
PARG is for conformant method signature."
(format "date:%s" (org-read-date)))
(cl-defun jf/denote/link-export-date (link description format protocol)
"Export a date for the given LINK, DESCRIPTION, FORMAT, and PROTOCOL."
(cond
((or (eq format 'html) (eq format 'md))
(format "<time datetime=\"%s\" title=\"%s\">%s</time>" link link description))
((eq format 'beamer)
(format "%s" description))
(t (format "%s (%s)" description link))))
(cl-defun jf/denote/link-follow-date (date &optional parg)
"Follow the given DATE; uncertain what that means.
PARG is for a conformant method signature."
(message "TODO, implement link for %s" date))
;; I want to be able to link and export my epigraph entries. For now, I'm
;; going to focus on the HTML and Markdown version; as most often when I
;; include an epigraph it is for my blog posts.
(cl-defun jf/denote/link-ol-epigraph-link (link
description format protocol
&key
additional-hugo-parameters
(jf/exporting-org-to-tor
jf/exporting-org-to-tor))
"Export the epigraph for the given LINK, DESCRIPTION, PROTOCOL, and FORMAT.
NOTE: This only works for blog export.
TODO: Consider how to expand beyond blog support."
(cond
((and jf/exporting-org-to-tor (or (eq format 'html) (eq format 'md)))
(format "{{< epigraph key=\"%s\" >}}" link))
((or (eq format 'html) (eq format 'md))
(concat "<blockquote>\n"
(jf/epigraph-text-for :identifier link)
"\n</blockquote>"))
(t nil)))
(cl-defun jf/epigraph-text-for (&key identifier)
"Return the epigraph text for `denote' IDENTIFIER."
(let ((filename (denote-get-path-by-id identifier)))
(with-current-buffer (find-file-noselect filename)
(let ((text (s-join "\n\n" (org-element-map
(org-element-parse-buffer)
'paragraph
(lambda (p) (caddr p))))))
(if (cadar (org-collect-keywords '("POEM")))
(format "<pre class=\"poem\">\n%s\n</pre>" text)
(format "%s" text))))))
(org-link-set-parameters "epigraph"
:complete (lambda (&optional parg)
(jf/org-link-complete-link-for
parg
:scheme "epigraph"
:subdirectory "epigraphs"))
:export (lambda (link description format protocol)
(jf/denote/link-ol-epigraph-link
link description format protocol))
:face #'jf/org-faces-epigraph
:follow #'denote-link-ol-follow)
(defface jf/org-faces-date '((default :inherit link))
"Face used to style `org-mode' date links in the buffer."
:group 'denote-faces
:package-version '(denote . "0.5.0"))
(defface jf/org-faces-epigraph '((default :inherit link))
"Face used to style `org-mode' epigraph links in the buffer."
:group 'denote-faces
:package-version '(denote . "0.5.0"))
(defface jf/org-faces-abbr '((default :inherit link))
"Face used to style `org-mode' abbr links in the buffer."
:group 'denote-faces
:package-version '(denote . "0.5.0"))
(cl-defun jf/denote/link-ol-export (link
description
format
protocol
&key
(jf/exporting-org-to-tor
jf/exporting-org-to-tor))
"Export a `denote:' link from Org files.
The LINK, DESCRIPTION, FORMAT, and PROTOCOL are handled by the
export backend.
When USE_HUGO_SHORTCODE is given use glossary based exporting."
(let* ((path-id (denote-link--ol-resolve-link-to-target link :path-id))
(path (file-name-nondirectory (car path-id)))
(export-plist (jf/denote/plist-for-export-of-id link))
(title (plist-get export-plist :title))
(url (plist-get export-plist :url))
(glossary_key (plist-get export-plist :key))
(desc (or description title)))
(if url
(cond
((and jf/exporting-org-to-tor glossary_key)
(format "{{< glossary key=\"%s\" >}}" glossary_key))
;; Use the TakeOnRules shortcode that leverages Hugo built-in
((and jf/exporting-org-to-tor (s-starts-with? "https://takeonrules.com/" url))
(format "{{< linkToPath \"%s\" >}}"
(s-trim (s-replace "https://takeonrules.com/" "/" url))))
((eq format 'html)
(format "<a href=\"%s\">%s</a>" url desc))
((eq format 'md) (format "[%s](%s)" desc url))
((or (eq format 'latex) (eq format 'beamer))
(format "\\href{%s}{%s}"
(replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" path)
desc))
((eq format 'texinfo) (format "@uref{%s,%s}" path desc))
((eq format 'ascii) (format "[%s] <denote:%s>" desc path))
(t path))
desc)))
(advice-add #'denote-link-ol-export
:override #'jf/denote/link-ol-export
'((name . "wrapper")))
(defun jf/associate-blog-post-url-with-identifier (url identifier)
"Associate given URL with the `denote' IDENTIFIER."
(message "Associating URL: %s with IDENTIFIER: %s." identifier url)
(let* ((filename (denote-get-path-by-id identifier))
(buffer (find-file-noselect filename)))
(with-current-buffer buffer
(jf/export-org-to-tor--global-buffer-prop-ensure
:key "ROAM_REFS"
:plist (jf/org-keywords-as-plist :keywords-regexp "ROAM_REFS")
:default url)
(save-buffer))))
(defun jf/org-mode/convert-link-type (&optional element)
"Replace the given `org-mode' ELEMENT's link type and text."
(interactive)
(let* ((types '("abbr" "abbr-plural" "denote"))
(element (or element (org-element-context))))
(if (eq 'link (car element))
(let ((type (org-element-property :type (org-element-context)))
(denote-id (plist-get (cadr element) :path)))
(if (member type types)
(when-let ((new-type (completing-read "New link type: " types nil t)))
(if-let ((new-text (jf/denote/org-property-from-id
:identifier denote-id
:property (cond
((string= "abbr" new-type) "ABBR")
((string= "abbr-plural" new-type) "PLURAL_ABBR")
((string= "denote" new-type) "TITLE")))))
(progn
(replace-regexp-in-region "\\[\\[\\([^:]+\\):\\([0-9A-Z]+\\)\\]\\[\\([^]]+\\)\\]\\]"
(format "[[%s:%s][%s]]" new-type denote-id new-text)
(org-element-property :begin element)
(org-element-property :end element))
(org-link-descriptive-ensure))
(user-error "Expected denote-id %s to have a %s acceptable property" denote-id new-type)))
(user-error "Current element is of type %s; it must be one of the following: %s" type types)))
(user-error "Current element must be of type 'link; it is %S" (car element)))))
;;;;; Capturing functions for applications
(defun jf/menu--org-capture-firefox ()
"Create an `denote' entry from Firefox page."
(interactive)
(require 'grab-mac-link)
(jf/denote/capture-reference :url (car (grab-mac-link-firefox-1))))
(defun jf/menu--org-capture-safari ()
"Create an `denote' entry from Safari page."
(interactive)
(require 'grab-mac-link)
(jf/denote/capture-reference :url (car (grab-mac-link-safari-1))))
(defun jf/capture/denote/from/eww-data ()
"Create an `denote' entry from `eww' data."
(interactive)
(jf/denote/capture-reference :url (plist-get eww-data :url)))
(defun jf/capture/denote/from/elfeed-show-entry ()
"Create `denote' entry from `elfeed-show-entry'."
(interactive)
(jf/denote/capture-reference :url (elfeed-entry-link elfeed-show-entry)))
;; I'd love to avoid re-fetching the content.
(cl-defun jf/sanitized-dom (&key html)
"Convert HTML to sanitized dom."
(with-temp-buffer
(insert html)
(org-web-tools--sanitized-dom)
(buffer-string)))
(cl-defun jf/denote/capture-reference (&key url
(keywords (denote-keywords-prompt))
(domain "references"))
"Create a `denote' entry in DOMAIN for URL with KEYWORDS.
The DOM could be as sanitized by `org-web-tools--sanitized-dom'."
(-let* ((url (or url (org-web-tools--get-first-url)))
(dom (plz 'get url :as #'org-web-tools--sanitized-dom))
((title . readable) (org-web-tools--eww-readable dom))
(title (org-web-tools--cleanup-title (or title "")))
(article (org-web-tools--html-to-org-with-pandoc readable)))
(denote title
keywords
'org
(f-join (denote-directory) domain)
nil
(concat "#+ROAM_REFS: " url "\n\n" article))))