forked from protesilaos/denote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
denote.el
3454 lines (2916 loc) · 131 KB
/
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
;;; denote.el --- Simple notes with an efficient file-naming scheme -*- lexical-binding: t -*-
;; Copyright (C) 2022 Free Software Foundation, Inc.
;; Author: Protesilaos Stavrou <[email protected]>
;; Maintainer: Denote Development <~protesilaos/[email protected]>
;; URL: https://git.sr.ht/~protesilaos/denote
;; Mailing-List: https://lists.sr.ht/~protesilaos/denote
;; Version: 1.2.0
;; Package-Requires: ((emacs "28.1"))
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Denote aims to be a simple-to-use, focused-in-scope, and effective
;; note-taking tool for Emacs. The manual describes all the
;; technicalities about the file-naming scheme, points of entry to
;; creating new notes, commands to check links between notes, and more:
;; <https://protesilaos.com/emacs/denote>. If you have the info manual
;; available, evaluate:
;;
;; (info "(denote) Top")
;;
;; What follows is a general overview of its core core design
;; principles (again: please read the manual for the technicalities):
;;
;; * Predictability :: File names must follow a consistent and
;; descriptive naming convention (see the manual's "The file-naming
;; scheme"). The file name alone should offer a clear indication of
;; what the contents are, without reference to any other metadatum.
;; This convention is not specific to note-taking, as it is pertinent
;; to any form of file that is part of the user's long-term storage
;; (see the manual's "Renaming files").
;;
;; * Composability :: Be a good Emacs citizen, by integrating with other
;; packages or built-in functionality instead of re-inventing
;; functions such as for filtering or greping. The author of Denote
;; (Protesilaos, aka "Prot") writes ordinary notes in plain text
;; (`.txt'), switching on demand to an Org file only when its expanded
;; set of functionality is required for the task at hand (see the
;; manual's "Points of entry").
;;
;; * Portability :: Notes are plain text and should remain portable.
;; The way Denote writes file names, the front matter it includes in
;; the note's header, and the links it establishes must all be
;; adequately usable with standard Unix tools. No need for a databse
;; or some specialised software. As Denote develops and this manual
;; is fully fleshed out, there will be concrete examples on how to do
;; the Denote-equivalent on the command-line.
;;
;; * Flexibility :: Do not assume the user's preference for a
;; note-taking methodology. Denote is conceptually similar to the
;; Zettelkasten Method, which you can learn more about in this
;; detailed introduction: <https://zettelkasten.de/introduction/>.
;; Notes are atomic (one file per note) and have a unique identifier.
;; However, Denote does not enforce a particular methodology for
;; knowledge management, such as a restricted vocabulary or mutually
;; exclusive sets of keywords. Denote also does not check if the user
;; writes thematically atomic notes. It is up to the user to apply
;; the requisite rigor and/or creativity in pursuit of their preferred
;; workflow (see the manual's "Writing metanotes").
;;
;; * Hackability :: Denote's code base consists of small and reusable
;; functions. They all have documentation strings. The idea is to
;; make it easier for users of varying levels of expertise to
;; understand what is going on and make surgical interventions where
;; necessary (e.g. to tweak some formatting). In this manual, we
;; provide concrete examples on such user-level configurations (see
;; the manual's "Keep a journal or diary").
;;
;; Now the important part... "Denote" is the familiar word, though it
;; also is a play on the "note" concept. Plus, we can come up with
;; acronyms, recursive or otherwise, of increasingly dubious utility
;; like:
;;
;; + Don't Ever Note Only The Epiphenomenal
;; + Denote Everything Neatly; Omit The Excesses
;;
;; But we'll let you get back to work. Don't Eschew or Neglect your
;; Obligations, Tasks, and Engagements.
;;; Code:
(require 'seq)
(require 'xref)
(require 'dired)
(require 'xdg)
(eval-when-compile (require 'subr-x))
(defgroup denote ()
"Simple notes with an efficient file-naming scheme."
:group 'files
:link '(info-link "(denote) Top"))
;;;; User options
;; About the autoload: (info "(elisp) File Local Variables")
;;;###autoload (put 'denote-directory 'safe-local-variable (lambda (val) (or (eq val 'local) (eq val 'default-directory))))
(defcustom denote-directory (expand-file-name "notes" (xdg-user-dir "DOCUMENTS"))
"Directory for storing personal notes.
A safe local value of either `default-directory' or `local' can
be added as a value in a .dir-local.el file. Do this if you
intend to use multiple directory silos for your notes while still
relying on a global value (which is the value of this variable).
The Denote manual has a sample (search for '.dir-locals.el').
Those silos do not communicate with each other: they remain
separate.
The local value influences where commands such as `denote' will
place the newly created note. If the command is called from a
directory or file where the local value exists, then that value
take precedence, otherwise the global value is used.
If you intend to reference this variable in Lisp, consider using
the function `denote-directory' instead: it returns the path as a
directory and also checks if a safe local value should be used."
:group 'denote
:safe (lambda (val) (or (eq val 'local) (eq val 'default-directory)))
:package-version '(denote . "0.5.0")
:link '(info-link "(denote) Maintain separate directories for notes")
:type 'directory)
(defcustom denote-known-keywords
'("emacs" "philosophy" "politics" "economics")
"List of strings with predefined keywords for `denote'.
Also see user options: `denote-allow-multi-word-keywords',
`denote-infer-keywords', `denote-sort-keywords'."
:group 'denote
:package-version '(denote . "0.1.0")
:type '(repeat string))
(defcustom denote-infer-keywords t
"Whether to infer keywords from existing notes' file names.
When non-nil, search the file names of existing notes in the
variable `denote-directory' for their keyword field and extract
the entries as \"inferred keywords\". These are combined with
`denote-known-keywords' and are presented as completion
candidates while using `denote' and related commands
interactively.
If nil, refrain from inferring keywords. The aforementioned
completion prompt only shows the `denote-known-keywords'. Use
this if you want to enforce a restricted vocabulary.
The user option `denote-excluded-keywords-regexp' can be used to
exclude keywords that match a regular expression.
Inferred keywords are specific to the value of the variable
`denote-directory'. If a silo with a local value is used, as
explained in that variable's doc string, the inferred keywords
are specific to the given silo.
For advanced Lisp usage, the function `denote-keywords' returns
the appropriate list of strings."
:group 'denote
:package-version '(denote . "0.1.0")
:type 'boolean)
(defcustom denote-prompts '(title keywords)
"Specify the prompts of the `denote' command for interactive use.
The value is a list of symbols, which includes any of the following:
- `title': Prompt for the title of the new note.
- `keywords': Prompts with completion for the keywords of the new
note. Available candidates are those specified in the user
option `denote-known-keywords'. If the user option
`denote-infer-keywords' is non-nil, keywords in existing note
file names are included in the list of candidates. The
`keywords' prompt uses `completing-read-multiple', meaning that
it can accept multiple keywords separated by a comma (or
whatever the value of `crm-separator' is).
- `file-type': Prompts with completion for the file type of the
new note. Available candidates are those specified in the user
option `denote-file-type'. Without this prompt, `denote' uses
the value of `denote-file-type'.
- `subdirectory': Prompts with completion for a subdirectory in
which to create the note. Available candidates are the value
of the user option `denote-directory' and all of its
subdirectories. Any subdirectory must already exist: Denote
will not create it.
- `date': Prompts for the date of the new note. It will expect
an input like 2022-06-16 or a date plus time: 2022-06-16 14:30.
Without the `date' prompt, the `denote' command uses the
`current-time'. (To leverage the more sophisticated Org
method, see the `denote-date-prompt-use-org-read-date'.)
- `template': Prompts for a KEY among `denote-templates'. The
value of that KEY is used to populate the new note with
content, which is added after the front matter.
The prompts occur in the given order.
If the value of this user option is nil, no prompts are used.
The resulting file name will consist of an identifier (i.e. the
date and time) and a supported file type extension (per
`denote-file-type').
Recall that Denote's standard file-naming scheme is defined as
follows (read the manual for the technicalities):
DATE--TITLE__KEYWORDS.EXT
If either or both of the `title' and `keywords' prompts are not
included in the value of this variable, file names will be any of
those permutations:
DATE.EXT
DATE--TITLE.EXT
DATE__KEYWORDS.EXT
When in doubt, always include the `title' and `keywords' prompts.
Finally, this user option only affects the interactive use of the
`denote' command (advanced users can call it from Lisp). For
ad-hoc interactive actions that do not change the default
behaviour of the `denote' command, users can invoke these
convenience commands: `denote-type', `denote-subdirectory',
`denote-date', `denote-template'."
:group 'denote
:package-version '(denote . "0.5.0")
:link '(info-link "(denote) The denote-prompts option")
:type '(radio (const :tag "Use no prompts" nil)
(set :tag "Available prompts" :greedy t
(const :tag "Title" title)
(const :tag "Keywords" keywords)
(const :tag "Date" date)
(const :tag "File type extension" file-type)
(const :tag "Subdirectory" subdirectory)
(const :tag "Template" template))))
(defcustom denote-sort-keywords t
"Whether to sort keywords in new files.
When non-nil, the keywords of `denote' are sorted with
`string-lessp' regardless of the order they were inserted at the
minibuffer prompt.
If nil, show the keywords in their given order."
:group 'denote
:package-version '(denote . "0.1.0")
:type 'boolean)
(defcustom denote-allow-multi-word-keywords t
"If non-nil keywords can consist of multiple words.
Words are automatically separated by a hyphen when using the
`denote' command or related. The hyphen is the only legal
character---no spaces, no other characters. If, for example, the
user types <word1_word2> or <word1 word2>, it is converted to
<word1-word2>.
When nil, do not allow keywords to consist of multiple words.
Reduce them to a single word, such as by turning <word1_word2> or
<word1 word2> into <word1word2>."
:group 'denote
:package-version '(denote . "0.1.0")
:type 'boolean)
(defcustom denote-file-type nil
"The file type extension for new notes.
By default (a nil value), the file type is that of Org mode.
Though the `org' symbol can be specified for the same effect.
When the value is the symbol `markdown-yaml', the file type is
that of Markdown mode and the front matter uses YAML notation.
Similarly, `markdown-toml' is Markdown but has TOML syntax in the
front matter.
When the value is `text', the file type is that of Text mode.
Any other non-nil value is the same as the default.
NOTE: expert users can change the supported file types by leaving
the value of this user option to nil and directly editing the
value of `denote-file-types'. That variable, which is not a user
option, controls the behaviour of all file-type-aware
functions (creating notes, renaming them, inserting front matter,
formatting a link, etc.). Consult its documentation for the
technicalities."
:type '(choice
(const :tag "Unspecified (defaults to Org)" nil)
(const :tag "Org mode (default)" org)
(const :tag "Markdown (YAML front matter)" markdown-yaml)
(const :tag "Markdown (TOML front matter)" markdown-toml)
(const :tag "Plain text" text))
:package-version '(denote . "0.6.0")
:group 'denote)
(defcustom denote-date-format nil
"Date format in the front matter (file header) of new notes.
When nil (the default value), use a file-type-specific
format (also check `denote-file-type'):
- For Org, an inactive timestamp is used, such as [2022-06-30 Wed
15:31].
- For Markdown, the RFC3339 standard is applied:
2022-06-30T15:48:00+03:00.
- For plain text, the format is that of ISO 8601: 2022-06-30.
If the value is a string, ignore the above and use it instead.
The string must include format specifiers for the date. These
are described in the doc string of `format-time-string'."
:type '(choice
(const :tag "Use appropiate format for each file type" nil)
(string :tag "Custom format for `format-time-string'"))
:package-version '(denote . "0.2.0")
:group 'denote)
(defcustom denote-date-prompt-use-org-read-date nil
"Whether to use `org-read-date' in date prompts.
If non-nil, use `org-read-date'. If nil, input the date as a
string, as described in `denote'.
This option is relevant when `denote-prompts' includes a `date'
and/or when the user invokes the command `denote-date'."
:group 'denote
:package-version '(denote . "0.6.0")
:type 'boolean)
(defcustom denote-templates nil
"Alist of content templates for new notes.
A template is arbitrary text that Denote will add to a newly
created note right below the front matter.
Templates are expressed as a (KEY . STRING) association.
- The KEY is the name which identifies the template. It is an
arbitrary symbol, such as `report', `memo', `statement'.
- The STRING is ordinary text that Denote will insert as-is. It
can contain newline characters to add spacing. The manual of
Denote contains examples on how to use the `concat' function,
beside writing a generic string.
The user can choose a template either by invoking the command
`denote-template' or by changing the user option `denote-prompts'
to always prompt for a template when calling the `denote'
command."
:type '(alist :key-type symbol :value-type string)
:package-version '(denote . "0.5.0")
:link '(info-link "(denote) The denote-templates option")
:group 'denote)
(defcustom denote-backlinks-show-context nil
"When non-nil, show link context in the backlinks buffer.
The context is the line a link to the current note is found in.
The context includes multiple links to the same note, if those
are present.
When nil, only show a simple list of file names that link to the
current note."
:group 'denote
:package-version '(denote . "1.2.0")
:type 'boolean)
(make-obsolete-variable 'denote-link-fontify-backlinks 'denote-backlinks-show-context "1.2.0")
(defcustom denote-excluded-directories-regexp nil
"Regular expression of directories to exclude from all operations.
Omit matching directories from file prompts and also exclude them
from all functions that check the contents of the variable
`denote-directory'. The regexp needs to match only the name of
the directory, not its full path.
File prompts are used by several commands, such as `denote-link'
and `denote-subdirectory'.
Functions that check for files include `denote-directory-files'
and `denote-directory-subdirectories'.
The match is performed with `string-match-p'."
:group 'denote
:package-version '(denote . "1.2.0")
:type 'string)
(defcustom denote-excluded-keywords-regexp nil
"Regular expression of keywords to not infer.
Keywords are inferred from file names and provided at relevant
prompts as completion candidates when the user option
`denote-infer-keywords' is non-nil.
The match is performed with `string-match-p'."
:group 'denote
:package-version '(denote . "1.2.0")
:type 'string)
;;;; Main variables
;; For character classes, evaluate: (info "(elisp) Char Classes")
(define-obsolete-variable-alias
'denote--id-format
'denote-id-format
"1.0.0")
(defconst denote-id-format "%Y%m%dT%H%M%S"
"Format of ID prefix of a note's filename.
The note's ID is derived from the date and time of its creation.")
(define-obsolete-variable-alias
'denote--id-regexp
'denote-id-regexp
"1.0.0")
(defconst denote-id-regexp "\\([0-9]\\{8\\}\\)\\(T[0-9]\\{6\\}\\)"
"Regular expression to match `denote-id-format'.")
(define-obsolete-variable-alias
'denote--title-regexp
'denote-title-regexp
"1.0.0")
(defconst denote-title-regexp "--\\([[:alnum:][:nonascii:]-]*\\)"
"Regular expression to match the TITLE field in a file name.")
(define-obsolete-variable-alias
'denote--keywords-regexp
'denote-keywords-regexp
"1.0.0")
(defconst denote-keywords-regexp "__\\([[:alnum:][:nonascii:]_-]*\\)"
"Regular expression to match the KEYWORDS field in a file name.")
(define-obsolete-variable-alias
'denote--punctuation-regexp
'denote-excluded-punctuation-regexp
"1.0.0")
(defconst denote-excluded-punctuation-regexp "[][{}!@#$%^&*()=+'\"?,.\|;:~`‘’“”/]*"
"Punctionation that is removed from file names.
We consider those characters illegal for our purposes.")
(define-obsolete-variable-alias
'denote-punctuation-excluded-extra-regexp
'denote-excluded-punctuation-extra-regexp
"1.0.0")
(defvar denote-excluded-punctuation-extra-regexp nil
"Additional punctuation that is removed from file names.
This variable is for advanced users who need to extend the
`denote-excluded-punctuation-regexp'. Once we have a better
understanding of what we should be omitting, we will update
things accordingly.")
;;;; File helper functions
(defun denote--completion-table (category candidates)
"Pass appropriate metadata CATEGORY to completion CANDIDATES."
(lambda (string pred action)
(if (eq action 'metadata)
`(metadata (category . ,category))
(complete-with-action action candidates string pred))))
(defun denote-directory ()
"Return path of variable `denote-directory' as a proper directory."
(let* ((val (or (buffer-local-value 'denote-directory (current-buffer))
denote-directory))
(path (if (or (eq val 'default-directory) (eq val 'local)) default-directory val)))
(unless (file-directory-p path)
(make-directory path t))
(file-name-as-directory (expand-file-name path))))
(defun denote--slug-no-punct (str)
"Convert STR to a file name slug."
(replace-regexp-in-string
(concat denote-excluded-punctuation-regexp
denote-excluded-punctuation-extra-regexp)
"" str))
(defun denote--slug-hyphenate (str)
"Replace spaces and underscores with hyphens in STR.
Also replace multiple hyphens with a single one and remove any
leading and trailing hyphen."
(replace-regexp-in-string
"^-\\|-$" ""
(replace-regexp-in-string
"-\\{2,\\}" "-"
(replace-regexp-in-string "_\\|\s+" "-" str))))
(defun denote-sluggify (str)
"Make STR an appropriate slug for file names and related."
(downcase (denote--slug-hyphenate (denote--slug-no-punct str))))
(define-obsolete-function-alias
'denote--sluggify
'denote-sluggify
"1.0.0")
(defun denote-sluggify-and-join (str)
"Sluggify STR while joining separate words."
(downcase
(replace-regexp-in-string
"-" ""
(denote--slug-hyphenate (denote--slug-no-punct str)))))
(define-obsolete-function-alias
'denote--sluggify-and-join
'denote-sluggify-and-join
"1.0.0")
(defun denote-sluggify-keywords (keywords)
"Sluggify KEYWORDS, which is a list of strings."
(mapcar (if denote-allow-multi-word-keywords
#'denote-sluggify
#'denote-sluggify-and-join)
keywords))
(define-obsolete-function-alias
'denote--sluggify-keywords
'denote-sluggify-keywords
"1.0.0")
(defun denote-desluggify (str)
"Upcase first char in STR and dehyphenate STR, inverting `denote-sluggify'."
(let ((str (replace-regexp-in-string "-" " " str)))
(aset str 0 (upcase (aref str 0)))
str))
(define-obsolete-function-alias
'denote--desluggify
'denote-desluggify
"1.0.0")
(defun denote--file-empty-p (file)
"Return non-nil if FILE is empty."
(zerop (or (file-attribute-size (file-attributes file)) 0)))
(defun denote-file-is-note-p (file)
"Return non-nil if FILE is an actual Denote note.
For our purposes, a note must note be a directory, must satisfy
`file-regular-p', 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'."
(let ((file-name (file-name-nondirectory file)))
(and (not (file-directory-p file))
(file-regular-p file)
(string-prefix-p (denote-directory) (expand-file-name file))
(string-match-p (concat "\\`" denote-id-regexp) file-name)
(denote-file-has-supported-extension-p file))))
(define-obsolete-function-alias
'denote--only-note-p
'denote-file-is-note-p
"1.0.0")
(defun denote-file-has-identifier-p (file)
"Return non-nil if FILE has a Denote identifier."
(when file
(string-match-p (concat "\\`" denote-id-regexp)
(file-name-nondirectory file))))
(define-obsolete-function-alias
'denote--file-has-identifier-p
'denote-file-has-identifier-p
"1.0.0")
(defun denote-file-directory-p (file)
"Return non-nil if FILE is a directory.
Omit FILE if it matches the value of user option
`denote-excluded-directories-regexp'."
(and (file-directory-p file)
denote-excluded-directories-regexp
(not (string-match-p denote-excluded-directories-regexp file))))
(defun denote-file-has-supported-extension-p (file)
"Return non-nil if FILE has supported extension.
Also account for the possibility of an added .gpg suffix.
Supported extensions are those implied by `denote-file-type'."
(let* ((extensions (denote--extensions))
(valid-extensions (append extensions
(mapcar (lambda (e)
(concat e ".gpg"))
extensions))))
(seq-some
(lambda (e) (string-suffix-p e file))
valid-extensions)))
(define-obsolete-function-alias
'denote--file-supported-extension-p
'denote-file-has-supported-extension-p
"1.0.0")
(defun denote--file-regular-writable-p (file)
"Return non-nil if FILE is regular and writable."
(and (file-regular-p file)
(file-writable-p file)))
(defun denote-file-is-writable-and-supported-p (file)
"Return non-nil if FILE is writable and has supported extension."
(and (denote--file-regular-writable-p file)
(denote-file-has-supported-extension-p file)))
(define-obsolete-function-alias
'denote--writable-and-supported-p
'denote-file-is-writable-and-supported-p
"1.0.0")
(defun denote-get-file-name-relative-to-denote-directory (file)
"Return name of FILE relative to the variable `denote-directory'.
FILE must be an absolute path."
(when-let* ((dir (denote-directory))
((file-name-absolute-p file))
(file-name (expand-file-name file))
((string-prefix-p dir file-name)))
(substring-no-properties file-name (length dir))))
(define-obsolete-function-alias
'denote--file-name-relative-to-denote-directory
'denote-get-file-name-relative-to-denote-directory
"1.0.0")
(defun denote-extract-id-from-string (string)
"Return existing Denote identifier in STRING, else nil."
(when (string-match denote-id-regexp string)
(match-string 0 string)))
(define-obsolete-function-alias
'denote-link--id-from-string
'denote-extract-id-from-string
"1.0.0")
;; TODO 2022-09-26: Maybe we can consolidate this with
;; `denote--dir-in-denote-directory-p'? Another check for the
;; directory prefix is done in `denote-file-is-note-p'.
(defun denote--default-dir-has-denote-prefix ()
"Test `default-directory' for variable `denote-directory' prefix."
(string-prefix-p (denote-directory)
(expand-file-name default-directory)))
(defun denote-directory-files ()
"Return list of absolute file paths in variable `denote-directory'.
Files only need to have an identifier. The return value may thus
include file types that are not implied by `denote-file-type'.
To limit the return value to text files, use the function
`denote-directory-text-only-files'.
Remember that the variable `denote-directory' accepts a dir-local
value, as explained in its doc string."
(mapcar
#'expand-file-name
(seq-remove
(lambda (f)
(not (denote-file-has-identifier-p f)))
(directory-files-recursively
(denote-directory)
directory-files-no-dot-files-regexp
:include-directories
(lambda (f)
(cond
((when-let ((regexp denote-excluded-directories-regexp))
(not (string-match-p regexp f))))
((file-readable-p f))
(t)))
:follow-symlinks))))
(defun denote-directory-text-only-files ()
"Return list of text files in variable `denote-directory'.
Filter `denote-directory-files' using `denote-file-is-note-p'."
(seq-filter #'denote-file-is-note-p (denote-directory-files)))
(define-obsolete-function-alias
'denote--directory-files
'denote-directory-files
"1.0.0")
(defun denote-directory-subdirectories ()
"Return list of subdirectories in variable `denote-directory'.
Omit dotfiles (such as .git) unconditionally. Also exclude
whatever matches `denote-excluded-directories-regexp'."
(seq-remove
(lambda (filename)
(let ((rel (denote-get-file-name-relative-to-denote-directory filename)))
(or (not (file-directory-p filename))
(string-match-p "\\`\\." rel)
(string-match-p "/\\." rel)
(when-let ((regexp denote-excluded-directories-regexp))
(string-match-p regexp rel)))))
(directory-files-recursively (denote-directory) ".*" t t)))
(define-obsolete-function-alias
'denote--subdirs
'denote-directory-subdirectories
"1.0.0")
(defun denote-get-path-by-id (id)
"Return absolute path of ID string in `denote-directory-files'."
(seq-find
(lambda (f)
(and (string-prefix-p id (file-name-nondirectory f))
;; The directory can contain exported html and other
;; derivative files that have the same name sans extetion as
;; the note.
(denote-file-is-note-p f)))
(denote-directory-files)))
(define-obsolete-function-alias
'denote--get-note-path-by-id
'denote-get-path-by-id
"1.0.0")
(defun denote-get-relative-path-by-id (id &optional directory)
"Return relative path of ID string in `denote-directory-files'.
The path is relative to DIRECTORY (default: ‘default-directory’)."
(file-relative-name (denote-get-path-by-id id) directory))
(defun denote-directory-files-matching-regexp (regexp)
"Return list of files matching REGEXP in `denote-directory-files'."
(seq-filter
(lambda (f)
(string-match-p regexp (denote-get-file-name-relative-to-denote-directory f)))
(denote-directory-files)))
(define-obsolete-function-alias
'denote--directory-files-matching-regexp
'denote-directory-files-matching-regexp
"1.0.0")
(defun denote-file-prompt (&optional initial-text)
"Prompt for file with identifier in variable `denote-directory'.
With optional INITIAL-TEXT, use it to prepopulate the minibuffer."
(let* ((project-find-functions #'denote-project-find)
(project (project-current nil (denote-directory)))
(dirs (list (project-root project)))
(all-files (project-files project dirs))
(completion-ignore-case read-file-name-completion-ignore-case))
(funcall project-read-file-name-function
"Select note: " all-files nil 'denote--title-history initial-text)))
(define-obsolete-function-alias
'denote--retrieve-read-file-prompt
'denote-file-prompt
"1.0.0")
;;;; Keywords
(defun denote-extract-keywords-from-path (path)
"Extract keywords from PATH and return them as a list of strings.
PATH must be a Denote-style file name where keywords are prefixed
with an underscore.
If PATH has no such keywords, return nil."
(let* ((file-name (file-name-nondirectory path))
(kws (when (string-match denote-keywords-regexp file-name)
(match-string-no-properties 1 file-name))))
(when kws
(split-string kws "_"))))
(define-obsolete-function-alias
'denote--extract-keywords-from-path
'denote-extract-keywords-from-path
"1.0.0")
(defun denote--inferred-keywords ()
"Extract keywords from `denote-directory-files'.
This function returns duplicates. The `denote-keywords' is the
one that doesn't."
(let ((kw (mapcan #'denote-extract-keywords-from-path (denote-directory-files))))
(if-let ((regexp denote-excluded-keywords-regexp))
(seq-filter (lambda (k) (not (string-match-p regexp k))) kw)
kw)))
(defun denote-keywords ()
"Return appropriate list of keyword candidates.
If `denote-infer-keywords' is non-nil, infer keywords from
existing notes and combine them into a list with
`denote-known-keywords'. Else use only the latter.
Inferred keywords are filtered by the user option
`denote-excluded-keywords-regexp'."
(delete-dups
(if denote-infer-keywords
(append (denote--inferred-keywords) denote-known-keywords)
denote-known-keywords)))
(defvar denote--keyword-history nil
"Minibuffer history of inputted keywords.")
(defun denote--keywords-crm (keywords &optional prompt)
"Use `completing-read-multiple' for KEYWORDS.
With optional PROMPT, use it instead of a generic text for file
keywords."
(delete-dups
(completing-read-multiple
(or prompt "File keyword: ") keywords
nil nil nil 'denote--keyword-history)))
(defun denote-keywords-prompt ()
"Prompt for one or more keywords.
In the case of multiple entries, those are separated by the
`crm-sepator', which typically is a comma. In such a case, the
output is sorted with `string-lessp'.
Process the return value with `denote-keywords-sort'."
(denote-keywords-sort (denote--keywords-crm (denote-keywords))))
(defun denote-keywords-sort (keywords)
"Sort KEYWORDS if `denote-sort-keywords' is non-nil.
KEYWORDS is a list of strings, per `denote-keywords-prompt'."
(if denote-sort-keywords
(sort keywords #'string-lessp)
keywords))
(define-obsolete-function-alias
'denote--keywords-prompt
'denote-keywords-prompt
"1.0.0")
(defun denote--keywords-combine (keywords)
"Format KEYWORDS output of `denote-keywords-prompt'."
(mapconcat #'downcase keywords "_"))
(defun denote--keywords-add-to-history (keywords)
"Append KEYWORDS to `denote--keyword-history'."
(mapc (lambda (kw)
(add-to-history 'denote--keyword-history kw))
(delete-dups keywords)))
;;;; File types
(defvar denote-org-front-matter
"#+title: %s
#+date: %s
#+filetags: %s
#+identifier: %s
\n"
"Org front matter.
It is passed to `format' with arguments TITLE, DATE, KEYWORDS,
ID. Advanced users are advised to consult Info node `(denote)
Change the front matter format'.")
(defvar denote-yaml-front-matter
"---
title: %s
date: %s
tags: %s
identifier: %S
---\n\n"
"YAML (Markdown) front matter.
It is passed to `format' with arguments TITLE, DATE, KEYWORDS,
ID. Advanced users are advised to consult Info node `(denote)
Change the front matter format'.")
(defvar denote-toml-front-matter
"+++
title = %s
date = %s
tags = %s
identifier = %S
+++\n\n"
"TOML (Markdown) front matter.
It is passed to `format' with arguments TITLE, DATE, KEYWORDS,
ID. Advanced users are advised to consult Info node `(denote)
Change the front matter format'.")
(defvar denote-text-front-matter
"title: %s
date: %s
tags: %s
identifier: %s
---------------------------\n\n"
"Plain text front matter.
It is passed to `format' with arguments TITLE, DATE, KEYWORDS,
ID. Advanced users are advised to consult Info node `(denote)
Change the front matter format'.")
(defun denote-surround-with-quotes (s)
"Surround string S with quotes.
This can be used in `denote-file-types' to format front mattter."
(format "%S" s))
(defun denote-trim-whitespace (s)
"Trim whitespace around string S.
This can be used in `denote-file-types' to format front mattter."
(if (string-blank-p s)
""
(let ((trims "[ \t\n\r]+"))
(string-trim s trims trims))))
(defun denote--trim-quotes (s)
"Trim quotes around string S."
(let ((trims "[\"']+"))
(string-trim s trims trims)))
(defun denote-trim-whitespace-then-quotes (s)
"Trim whitespace then quotes around string S.
This can be used in `denote-file-types' to format front mattter."
(if (string-blank-p s)
""
(denote--trim-quotes (denote-trim-whitespace s))))
(defun denote-format-keywords-for-md-front-matter (keywords)
"Format front matter KEYWORDS for markdown file type.
KEYWORDS is a list of strings. Consult the `denote-file-types'
for how this is used."
(format "[%s]" (mapconcat (lambda (k) (format "%S" k)) keywords ", ")))
(defun denote-format-keywords-for-text-front-matter (keywords)
"Format front matter KEYWORDS for text file type.
KEYWORDS is a list of strings. Consult the `denote-file-types'
for how this is used."
(string-join keywords " "))
(defun denote-format-keywords-for-org-front-matter (keywords)
"Format front matter KEYWORDS for org file type.
KEYWORDS is a list of strings. Consult the `denote-file-types'
for how this is used."
(if keywords
(format ":%s:" (string-join keywords ":"))
""))
(defun denote-extract-keywords-from-front-matter (keywords-string)
"Extract keywords list from front matter KEYWORDS-STRING.
Split KEYWORDS-STRING into a list of strings. If KEYWORDS-STRING
satisfies `string-blank-p', return an empty string.
Consult the `denote-file-types' for how this is used."
(if (string-blank-p keywords-string)
""
(split-string keywords-string "[:,\s]+" t "[][ \"']+")))
(defvar denote-file-types
'((org
:extension ".org"
:date-function denote-date-org-timestamp
:front-matter denote-org-front-matter
:title-key-regexp "^#\\+title\\s-*:"
:title-value-function identity
:title-value-reverse-function denote-trim-whitespace
:keywords-key-regexp "^#\\+filetags\\s-*:"
:keywords-value-function denote-format-keywords-for-org-front-matter
:keywords-value-reverse-function denote-extract-keywords-from-front-matter
:link denote-org-link-format
:link-in-context-regexp denote-org-link-in-context-regexp)
(markdown-yaml
:extension ".md"
:date-function denote-date-rfc3339
:front-matter denote-yaml-front-matter
:title-key-regexp "^title\\s-*:"
:title-value-function denote-surround-with-quotes
:title-value-reverse-function denote-trim-whitespace-then-quotes
:keywords-key-regexp "^tags\\s-*:"
:keywords-value-function denote-format-keywords-for-md-front-matter
:keywords-value-reverse-function denote-extract-keywords-from-front-matter
:link denote-md-link-format
:link-in-context-regexp denote-md-link-in-context-regexp)
(markdown-toml
:extension ".md"
:date-function denote-date-rfc3339
:front-matter denote-toml-front-matter
:title-key-regexp "^title\\s-*="
:title-value-function denote-surround-with-quotes
:title-value-reverse-function denote-trim-whitespace-then-quotes
:keywords-key-regexp "^tags\\s-*="
:keywords-value-function denote-format-keywords-for-md-front-matter
:keywords-value-reverse-function denote-extract-keywords-from-front-matter
:link denote-md-link-format
:link-in-context-regexp denote-md-link-in-context-regexp)
(text
:extension ".txt"
:date-function denote-date-iso-8601
:front-matter denote-text-front-matter
:title-key-regexp "^title\\s-*:"
:title-value-function identity
:title-value-reverse-function denote-trim-whitespace
:keywords-key-regexp "^tags\\s-*:"
:keywords-value-function denote-format-keywords-for-text-front-matter
:keywords-value-reverse-function denote-extract-keywords-from-front-matter
:link denote-org-link-format
:link-in-context-regexp denote-org-link-in-context-regexp))
"Alist of `denote-file-type' and their format properties.
Each element is of the form (SYMBOL PROPERTY-LIST). SYMBOL is
one of those specified in `denote-file-type' or an arbitrary