-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorg-starter.el
1830 lines (1569 loc) · 74.6 KB
/
org-starter.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
;;; org-starter.el --- A basic configuration framework for org mode -*- lexical-binding: t -*-
;; Copyright (C) 2018-2022 by Akira Komamura
;; Author: Akira Komamura <[email protected]>
;; Version: 0.2.10
;; Package-Requires: ((emacs "25.1") (dash "2.18"))
;; URL: https://github.com/akirak/org-starter
;; This file is not part of GNU Emacs.
;;; License:
;; 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:
;; This package helps you configure org mode. See README for details.
;;; Code:
(require 'org)
(require 'cl-lib)
(require 'subr-x)
(require 'dash)
(require 'org-capture)
(declare-function posframe-show "ext:posframe")
(declare-function posframe-delete-frame "ext:posframe")
(declare-function posframe-poshandler-frame-center "ext:posframe")
(declare-function posframe-workable-p "ext:posframe")
(defvar org-agenda-custom-commands)
(defvar org-agenda-window-setup)
(defvar org-agenda-sticky)
(defvar org-agenda-buffer-name)
;;;; Compatibility
(eval-and-compile
(with-no-warnings
(defalias 'org-starter--when-let* #'when-let)
(function-put #'org-starter--when-let* 'lisp-indent-function 1)))
(defconst org-starter-message-buffer "*org-starter message*")
;;;; Custom variables
(defcustom org-starter-capture-template-map-function nil
"A function used to transform each entry in `org-capture' templates."
:type 'function
:group 'org-starter)
(defcustom org-starter-alternative-find-function
(cond
((fboundp 'helm-org-rifle-files) #'helm-org-rifle-files)
(t #'org-starter-sparse-tree-on-file))
"An alternative function to find an Org file.
This function is called by `org-starter-find-file-by-key' when
a sequence of two universal arguments are given."
:group 'org-starter
:type 'function)
(defcustom org-starter-use-child-frame t
"If non-nil, use child frames for multi line messages.
You will need posframe.el for actually using this feature."
:type 'boolean
:group 'org-starter)
(defcustom org-starter-child-frame-border-width 2
"Border width of child frames."
:type 'number
:group 'org-starter)
(defcustom org-starter-child-frame-border-color
"white"
"Border color of child frames."
:type 'color
:group 'org-starter)
(defcustom org-starter-child-frame-foreground-color nil
"Border color of child frames."
:type '(choice color nil)
:group 'org-starter)
(defcustom org-starter-child-frame-background-color nil
"Border color of child frames."
:type '(choice color nil)
:group 'org-starter)
(defcustom org-starter-child-frame-override-parameters nil
"Override posframe parameters with this plist."
:type 'plist
:group 'org-starter)
(defcustom org-starter-child-frame-poshandler
#'posframe-poshandler-frame-center
"Poshandler function."
:type 'function
:group 'org-starter)
(defcustom org-starter-verify-agenda-configuration t
"Whether to verify configuration in `org-starter-add-agenda-custom-command'."
:type 'boolean
:group 'org-starter)
(define-widget 'org-starter-bindings 'lazy
"List of custom keybindings."
:tag "Keybindings"
:type '(repeat (list (string :tag "Key")
(function :tag "Command")
(choice :tag "Help"
string
(const nil)))))
(defcustom org-starter-find-file-visit-window
nil
"When non-nil, visit the window displaying a file if any.
When this variable is set to non-nil,
`org-starter-find-file-by-key' selects a window displaying the
target file if there is such a window in the same frame."
:type 'boolean
:group 'org-starter)
(defcustom org-starter-extra-find-file-map
nil
"Extra bindings available in `org-starter-find-file-by-key'."
:group 'org-starter
:type 'org-starter-bindings)
(defcustom org-starter-extra-refile-map
'(("/" org-refile "normal refile"))
"Extra bindings available in `org-starter-refile-by-key'."
:group 'org-starter
:type 'org-starter-bindings)
(defcustom org-starter-extra-alternative-find-file-map
nil
"Extra keybindings in `org-starter-alternative-find-file-by-key'."
:group 'org-starter
:type 'org-starter-bindings)
(defcustom org-starter-require-file-by-default t
"When non-nil, require defined files by default.
This is the default value of \":required\" option in
`org-starter-define-file'. If this option is non-nil,
all files defined by org-starter must exist. If a file does not
exist, it throws an error.
This option does not affect the behavior of directory definitions."
:group 'org-starter
:type 'boolean)
(defcustom org-starter-config-file-name ".org-config.el"
"File name of external config files for org-starter.
See function `org-starter-load-config-files' for details."
:risky t
:group 'org-starter
:type 'string)
(defcustom org-starter-load-config-files nil
"When non-nil, load config files in known directories.
Org-Starter loads configuration files with
`org-starter-config-file-name' if this variable is set to non-nil.
When this variable is set to non-nil, org-starter loads Emacs
Lisp configuration files with from the following places:
- When org-starter.el is loaded, org-starter loads configuration files
in `org-starter-path'.
- After org-starter.el is loaded, org-starter loads configuration
files as directories are defined using
`org-starter-define-directory' (or `org-starter-def' on a
directory).
If a file with `org-starter-config-file-name' does not exist in a
given directory, the file will not be loaded."
:group 'org-starter
:type 'boolean)
(defcustom org-starter-refresh-agenda-on-redefinition t
"When non-nil, refresh an existing agenda buffer on redefinition.
If you set this variable to non-nil, both
`org-starter-add-agenda-custom-command' and
`org-starter-add-block-agenda-command' refreshes a corresponding existing agenda
buffer if it is updates an existing buffer of the same key.
This is useful for experimenting with a new custom agenda command.
If `org-agenda-sticky' is non-nil, it checks for a stick agenda
buffer of the same key. If such a buffer exists, the buffer is
killed and the agenda is redispatched.
If `org-agenda-sticky' is nil, it checks if there is an agenda buffer
and redispatches the redefined agenda, no matter which agenda command
is currently displayed in the agenda buffer.
If the value is \"confirm\" instead of t, it asks the user if he/she
wants to redispatches the agenda."
:group 'org-starter
:type '(choice (const :tag "Without confirmation" t)
(const :tag "With confirmation" confirm)
(const :tag "Never" nil)))
(defcustom org-starter-override-agenda-window-setup
nil
"If non-nil, override `org-agenda-window-setup' when redispatching an agenda.
This is effective if and only if an agenda command is dispatched
by org-starter as documented for
`org-starter-refresh-agenda-on-redefinition'.
If you are experimenting with a custom agenda command, you
probably don't want to display the agenda in the same buffer as
the source code. To handle such a situation, this option lets you
override `org-agenda-window-setup' only when an agenda command is
dispatched due to redefining it. The options are the same as
`org-agenda-window-setup'.
If this variable is nil, it doesn't take effect, and the same
window setup is used."
:group 'org-starter
:type '(choice (const current-window)
(const other-window)
(const only-window)
(const reorganize-frame)
(const other-frame)
(const nil)))
(defcustom org-starter-check-truename t
"Whether to compare file names according to true names.
When this option is t, it compares file names by true names in
several functions. This is accurate if you store org files in
symlinked directories, but it is slower.
Set the value to nil if your org files don't involve symlinks."
:group 'org-starter
:type 'boolean)
;;;; Variables
(defvar org-starter-suppress-override-messages-once nil)
;;;; The error buffer and error logging
;; This is used by `org-starter-verify-configuration'.
(defcustom org-starter-enable-local-variables nil
"Override `enable-local-variables' when files are loaded.
When this variable is set to a value other than nil and function
`org-starter-mode' is turned on, `enable-local-variables' is set
to the value temporarily when a file in `org-starter-known-files'
is loaded. For example, if the variable is set to `:all', all
local variables defined in the file are applied when it is loaded
without confirmation. As variables defined in your own files are
supposed to be safe, this is also usually safe. However, when
this option is set to `:all', please don't add a file that can be
edited by someone else, as local variables can bring
vulnerability.
When the variable is set to nil, org-starter does not override the
value of `enable-local-variables`."
:group 'org-starter
:type 'symbol)
(defconst org-starter-error-buffer "*org-starter errors*")
(defvar org-starter-found-errors nil
"Non-nil if an error is found while configuring org-starter.")
(defvar org-starter-prevent-local-config-directories nil
"List of directories from which config files shouldn't be loaded.
This is updated by `org-starter-define-directory'.
The user should not update this value.")
(defvar org-starter-truename-cache nil
"Hash table used to store true names of files and directories.")
;;;###autoload
(define-minor-mode org-starter-mode
"Turn on/off features of org-starter.
This includes the following features:
- Activate a function advice around `find-file-noselect', so
`org-starter-enable-local-variables' option is respected.
- When Org files are loaded, set file-local variables defined as
:local-variables option in `org-starter-define-file'."
:lighter " Org-Starter"
:require 'org-starter
:global t
:group 'org-starter
(cond
;; Turn on
(org-starter-mode
(add-hook 'org-mode-hook #'org-starter-org-mode-hook t)
(advice-add #'find-file-noselect :around
#'org-starter--ad-around-find-file-noselect))
;; Turn off
(t
(advice-remove #'find-file-noselect
#'org-starter--ad-around-find-file-noselect)
(remove-hook 'org-mode-hook #'org-starter-org-mode-hook))))
(defun org-starter--clear-errors ()
"Reset the status of the error buffer."
(org-starter--when-let* ((buf (get-buffer org-starter-error-buffer)))
(with-current-buffer buf
(let ((inhibit-read-only t))
(erase-buffer))))
(setq org-starter-found-errors nil))
(defun org-starter--create-error-buffer ()
"Return the error buffer."
(or (get-buffer org-starter-error-buffer)
(with-current-buffer (generate-new-buffer org-starter-error-buffer)
(local-set-key "q" 'quit-window)
(local-set-key "g" 'org-starter-verify-configuration)
(setq buffer-read-only t)
(current-buffer))))
(defun org-starter--log-error-no-newline (format-string &rest args)
"Variant of `org-starter--log-error' that does not append a newline.
FORMAT-STRING is the format spec, and ARGS are parameters."
(declare (indent 0))
(with-current-buffer (org-starter--create-error-buffer)
(let ((inhibit-read-only t))
(insert (apply #'format format-string args))))
(setq org-starter-found-errors (1+ (or org-starter-found-errors 0))))
(defun org-starter--log-error (format-string &rest args)
"Like `message', but logs a string to `org-starter-error-buffer'.
FORMAT-STRING is the format spec, and ARGS are parameters."
(declare (indent 0))
(org-starter--log-error-no-newline (concat format-string "\n") args))
;;;; File registry: known files and directories, path, deprecated files, etc.
(defvar org-starter-known-files nil
"List of files registered by `org-starter-define-file'.")
(defcustom org-starter-path nil
"List of directories from which org files are searched."
:group 'org-starter
:type '(repeat string)
:risky t
:set (lambda (key value)
(if (featurep 'org-starter)
(let ((added (-difference value (symbol-value key))))
(set-default key value)
(message "Add directories to org-starter-path: %s"
(string-join added " "))
(mapc #'load-file (org-starter--get-existing-config-files added)))
(set-default key value))))
(defvar org-starter-deprecated-files nil
"List of deprecated org files currently existing.")
(defvar org-starter-known-directories nil)
(defun org-starter--lookup-known-file (filename)
"Look up a FILENAME (without a directory) in `org-starter-known-files'."
(car (cl-remove-if-not (lambda (fpath)
(equal filename
(file-name-nondirectory fpath)))
org-starter-known-files)))
(defun org-starter--search-file (filename &optional check-known-files)
"Search FILENAME from `org-starter-path'.
If CHECK-KNOWN-FILES is non-nil, `org-starter-known-files' is first checked for
the FILENAME."
(or (and check-known-files
(org-starter--lookup-known-file filename))
(cl-loop for dir in org-starter-path
for fpath = (expand-file-name filename dir)
when (file-exists-p fpath)
return fpath)))
(defun org-starter-locate-file (filename &optional directory check-known-files)
"Search FILENAME from DIRECTORY and `org-starter-path' and return its path.
This function returns an existing path to a file which has the same
sans-directory file name as FILENAME. If such a file is not found, this
function returns nil.
If CHECK-KNOWN-FILES is non-nil, `org-starter-known-files' is first checked for
the FILENAME."
(cond
((file-name-absolute-p filename)
(when (file-exists-p filename)
filename))
((null directory)
(org-starter--search-file filename check-known-files))
(t
(let* ((dir (cond
((stringp directory) directory)
((symbolp directory) (symbol-value directory))
(t (error "Unsupported type of directory: %s"
(prin1-to-string directory)))))
(default-file (expand-file-name filename dir)))
(if (file-exists-p default-file)
default-file
(let ((found-file (org-starter--search-file filename
check-known-files)))
(when (and found-file
(yes-or-no-p (format "%s does not exist, but %s is found. Add it instead?"
default-file found-file)))
found-file)))))))
(defun org-starter--to-symbol-list (obj)
"Make a list of symbols from a symbol or a list.
OBJ should be a symbol, or a list of symbols."
(cond
((null obj) nil)
((symbolp obj) (list obj))
((listp obj) obj)))
(cl-defun org-starter--file-member-p (file list &key expand)
"Return non-nil if the file is included in the list.
FILE is an absolute path to a file, and LIST is a list of files.
If EXPAND is non-nil, use `expand-file-name' to expand abbreviate
file names. It doesn't take effect if
`org-starter-check-truename' is non-nil."
(cond
(org-starter-check-truename
(cl-member file list :test #'equal
:key #'org-starter--cached-truename))
(expand
(cl-member file list :test #'equal :key #'expand-file-name))
(t
(member file list))))
(defun org-starter--cached-truename (file)
"Return the true name of FILE, from a cache value if available."
(unless org-starter-truename-cache
(setq org-starter-truename-cache
(make-hash-table :test #'equal)))
(or (gethash file org-starter-truename-cache)
(let ((truename (file-truename file)))
(puthash file truename org-starter-truename-cache)
truename)))
;;;; Exclude files in org-starter from recentf
(defcustom org-starter-exclude-from-recentf nil
"If non-nil, exclude items in `org-starter-known-files' from recentf."
:group 'org-starter
:type '(set (const :tag "Exclude known files" 'known-files)
(const :tag "Exclude files in path" 'path)))
(defun org-starter-recentf-excluded-p (file)
"Return non-nil if FILE should be excluded from recentf."
(and (string-match-p org-agenda-file-regexp file)
(listp org-starter-exclude-from-recentf)
org-starter-exclude-from-recentf
(or (and (memq 'known-files org-starter-exclude-from-recentf)
(org-starter--file-member-p (expand-file-name file)
org-starter-known-files))
(and (memq 'path org-starter-exclude-from-recentf)
(org-starter--file-member-p (file-name-directory file)
org-starter-path)))))
(add-hook 'recentf-exclude #'org-starter-recentf-excluded-p t)
;;;; Defining directories
(defvar org-starter-directory-origins nil
"Alist of pairs of a directory and its origin.
Each item in this alist is a cons of a path to a local directory and its origin
repository URL.")
(defun org-starter--clone (origin dest)
"Clone a repository at ORIGIN to DEST."
;; TODO: check origin URL
(process-lines "git" "clone" origin dest))
(defun org-starter-clone-all ()
"Clone all missing repositories specified in `org-starter-directory-origins'."
(interactive)
(cl-loop for (dest . origin) in org-starter-directory-origins
unless (file-directory-p dest)
do (org-starter--clone origin dest)))
(defun org-starter--define-glob-function (dpath id)
"Define a function to glob entries in a directory.
This function defines a function to glob a list of org files in a directory.
DPATH is a path to the directory, and ID is a symbol/string to uniquely
identify the directory."
(let ((name (intern (format "org-starter-glob-files/%s"
(cl-etypecase id
(symbol (symbol-name id))
(string id))))))
(eval `(defun ,name ()
(when (file-directory-p ,dpath)
(directory-files ,dpath t org-agenda-file-regexp))))))
(cl-defun org-starter-define-directory (dpath &key
agenda
refile
id
origin
ensure
add-to-path
custom-vars
no-config-file
files)
"Define a directory for org files.
DPATH is a path to the directory. This option cannot be nil.
If AGENDA is non-nil, the directory is added to `org-agenda-files'.
If REFILE is set, files in the directory are added to
`org-refile-targets'. In this case, you have to also specify ID
to uniquely identify the directory. This identifier is used to
determine the name of a function to glob files in the directory,
which is called by `org-refile'.
ORIGIN is a repository URL from which you want to clone the
repository. If this option is set and ENSURE option is non-nil,
this function automatically clones the repository when the
directory does not exist.
If ADD-TO-PATH is non-nil, the directory is added to
`org-starter-path'.
CUSTOM-VARS can be either a symbol or a list of symbols.
These symbols are names of variables that should be set to the path
of the directory. `customize-set-variable' is used to set the value.
If NO-CONFIG-FILE is set to non-nil, the configuration file in
the directory will not be loaded even if
`org-starter-load-config-files' is set and the directory contains
the file.
FILES is a list whose item accepts the same options as
`org-starter-define-file', except for `:directory' option.
You can define files in the directory.
If the directory exists and the configuration is done properly,
the path to the directory is returned as the result of this function."
(declare (indent 1))
(let ((exists (file-directory-p dpath)))
(setq dpath (expand-file-name (file-name-as-directory dpath)))
(when (and ensure (not exists))
(unless origin
(error "%s is a required directory, but `:ensure' property is unset" dpath))
(org-starter--clone origin dpath))
(when agenda
(add-to-list 'org-agenda-files dpath 'append #'file-equal-p))
(when origin
(add-to-list 'org-starter-directory-origins (cons dpath origin)))
(when (and add-to-path exists)
(cl-adjoin dpath org-starter-path :test #'file-equal-p))
(when refile
(unless id
(error "To add %s to refile targets, you must set `:id' property" dpath))
(let* ((func (org-starter--define-glob-function dpath id))
(pair (assq func org-refile-targets)))
(if pair
(setf (cdr pair) refile)
(add-to-list 'org-refile-targets (cons func refile) 'append))))
(dolist (symbol (cl-typecase custom-vars
(list custom-vars)
(symbol (list custom-vars))))
(customize-set-variable symbol dpath))
(when no-config-file
(add-to-list 'org-starter-prevent-local-config-directories dpath))
(cl-loop for (filename . options) in files
do (apply #'org-starter-define-file filename :directory dpath
options))
(add-to-list 'org-starter-known-directories dpath)
(when (and add-to-path
after-init-time
(not (cl-member dpath org-starter-prevent-local-config-directories
:test #'file-equal-p)))
(let ((file (expand-file-name org-starter-config-file-name dpath)))
(when (file-exists-p file)
(load-file file))))
dpath))
;;;; Defining files
;;;;; File-local variables and modes
(defvar org-starter-file-local-variables nil
"Alist of file-local variable definitions.")
(defvar org-starter-file-local-minor-modes nil
"Alist of file-local minor modes.")
(defvar org-starter-file-local-hooks nil
"Alist of file-local load hooks.")
(defun org-starter-org-mode-hook ()
"Apply file-local settings."
(org-starter--when-let* ((fpath (buffer-file-name)))
(dolist (x (cdr (assoc fpath org-starter-file-local-variables)))
(pcase x
(`(,symbol . ,value)
(cl-assert (symbolp symbol))
(set (make-local-variable symbol) value))))
(dolist (x (cdr (assoc fpath org-starter-file-local-minor-modes)))
(pcase x
((pred symbolp) (funcall x 1))
(`(,mode ,arg) (funcall mode arg))))
(org-starter--when-let*
((hook (cdr (assoc fpath org-starter-file-local-hooks))))
(funcall hook))))
(define-obsolete-function-alias 'org-starter-load-local-variables
#'org-starter-org-mode-hook
"0.2.9")
;;;;; Keymap for visiting a known file (deprecated)
(defvar org-starter-file-map (make-sparse-keymap)
"Keymap used to find a file.")
(defcustom org-starter-define-file-commands 'with-keys
"Define commands to find a specific file.
When non-nil, org-starter define commands to find (or jump to) a specific file
defined by `org-starter-define-file'. The defined commands are also used to
bind keys to files.
This is convenient for the follwing
reasons:
- You can access a file quickly using \\[execute-command\\].
- You can use the command names for which-key replacements."
:group 'org-starter
:type '(choice (const :tag "All defined files" all)
(const :tag "Files with keys" with-keys)
(const :tag "Never" nil)))
(defcustom org-starter-file-command-template "org-starter-find-file:%s"
"Template used to determine command names for files.
'%s' in the template is replaced with the base name of the file.
This is applicable when `org-starter-define-file-commands' is non-nil."
:type 'string
:group 'org-starter)
(defmacro org-starter--file-command-name (fpath)
"Build a command name for FPATH."
`(intern (format org-starter-file-command-template (file-name-base ,fpath))))
(defun org-starter--define-file-command (fpath)
"Define a command to find FPATH."
(eval
`(defun ,(org-starter--file-command-name fpath) ()
(interactive)
(find-file ,fpath))))
(defvar org-starter-key-file-alist nil)
(defun org-starter--funcall-on-file-by-key (func &optional
prompt
extra-map
extra-help)
"Pick an Org file by key and apply a function on it.
This function picks an Org file by a key specified as :key argument
of `org-starter-define-file', and apply FUNC on it.
If PROMPT is given, use it as the prompt.
If EXTRA-MAP is given, use it as the extra map.
The original bindings defined by :key property are overridden by it.
EXTRA-HELP is an alist for the items in the extra map."
(let* ((map (make-sparse-keymap))
(message-log-max nil)
(help-items (mapcar (lambda (cell)
(format "[%s]: %s"
(car cell) (cdr cell)))
(cl-union extra-help
(mapcar (lambda (cell)
(cons (car cell)
(file-name-nondirectory (cdr cell))))
org-starter-key-file-alist)
:key #'car))))
(dolist (cell org-starter-key-file-alist)
(define-key map (car cell)
(lambda () (interactive) (funcall func (cdr cell)))))
(org-starter--display-options prompt help-items)
(set-transient-map (if extra-map
(make-composed-keymap extra-map map)
map))))
;;;###autoload
(defun org-starter-find-file-by-key (&optional arg)
"Visit an Org file quickly by key.
With this command, you can quickly open a file by a key sequence
specified as :key property of the file.
To access a file which is not assigned a key, you can select it
using `completing-read' by pressing \"/\" key.
If a universal prefix is given as ARG, open the file in other
window.
If two universal prefix arguments (C-u C-u) is given, call a function
specified as `org-starter-alternative-find-function' with the file
as the argument."
(interactive "P")
(let* ((map (make-sparse-keymap))
(extra-help (cl-loop for (key command . help) in org-starter-extra-find-file-map
do (define-key map (kbd key) command)
when help
collect (cons key (car help)))))
(pcase arg
('(4) (progn
(define-key map (kbd "/") #'org-starter-select-file-other-window)
(org-starter--funcall-on-file-by-key
#'org-starter--find-file-other-window
"Find an Org file in other window:"
map extra-help)))
('(16) (progn
(define-key map (kbd "/")
(lambda ()
(interactive)
(funcall org-starter-alternative-find-function
(org-starter-select-file "Select an Org file: "))))
(org-starter--funcall-on-file-by-key
org-starter-alternative-find-function
(format "Call %s:"
(if (symbolp org-starter-alternative-find-function)
(symbol-name org-starter-alternative-find-function)
"the function"))
map extra-help)))
(_ (progn
(define-key map (kbd "/") #'org-starter-select-file)
(org-starter--funcall-on-file-by-key
#'org-starter--find-file
"Find an Org file:"
map extra-help))))))
(defun org-starter--find-file (file)
"Switch to a buffer visiting FILE."
(org-starter--select-file-window file
#'switch-to-buffer))
(defun org-starter--find-file-other-window (file)
"Switch to a buffer visiting FILE in other window."
(org-starter--select-file-window file
#'switch-to-buffer-other-window))
(defun org-starter--select-file-window (file fallback)
"Select a window displaying a file or open the file.
If there is a window displaying the buffer of FILE, select the window.
Otherwise, switch to the buffer of the file using FALLBACK function."
(let ((buffer (or (find-buffer-visiting file)
(find-file-noselect file))))
(if-let ((window (and org-starter-find-file-visit-window
(get-buffer-window buffer))))
(select-window window)
(funcall fallback buffer))))
;;;###autoload
(defun org-starter-alternative-find-file-by-key (&optional arg)
"Visit an Org file using `org-starter-alternative-find-function'.
This is like `org-starter-find-file-by-key' but uses
`org-starter-alternative-find-function' to visit a file.
Keys are configured as :key properties of files, which are the same
as `org-starter-find-file-by-key'.
To access a file which is not assigned a key, you can select it
using `completing-read' by pressing \"/\" key.
If a universal prefix is given as ARG, visit the file using
`find-file'.
Extra commands are configured in
`org-starter-extra-alternative-find-file-map'."
(interactive "P")
(let* ((map (make-sparse-keymap))
(extra-help (cl-loop for (key command . help) in org-starter-extra-alternative-find-file-map
do (define-key map (kbd key) command)
when help
collect (cons key (car help)))))
(pcase arg
('(4) (progn
(define-key map (kbd "/") #'org-starter-select-file)
(org-starter--funcall-on-file-by-key
#'find-file "Find an Org file:"
map extra-help)))
(_ (progn
(define-key map (kbd "/") #'org-starter-select-file-alternative)
(org-starter--funcall-on-file-by-key
org-starter-alternative-find-function
"Visit an Org file using the alternative command:"
map extra-help))))))
(defun org-starter--refile-target-of-file (file)
"Get a refile target spec to FILE."
(cl-assoc file (cl-remove-if-not (lambda (cell) (stringp (car cell)))
org-refile-targets)
:test 'file-equal-p))
;;;###autoload
(defun org-starter-refile-by-key (&optional arg)
"Run `org-refile' with the target limited to a file by key.
With this command, you can quickly refile the current entry to a file by a key
sequence specified as :key property of the file.
ARG is passed to `org-refile' function.
You can also access keybindings defined in
`org-starter-extra-refile-map'.
For example, you can run a normal `org-refile' by pressing \"/\" key
by default."
(interactive "P")
(unless (derived-mode-p 'org-mode)
(error "Not in org-mode"))
(let* ((extra-map (make-sparse-keymap))
(extra-help (cl-loop for (key command . help) in org-starter-extra-refile-map
do (define-key extra-map (kbd key) command)
when help
collect (cons key (car help)))))
(org-starter--funcall-on-file-by-key
(lambda (file)
(let ((org-refile-targets (list (or (org-starter--refile-target-of-file file)
`(,file :maxlevel . 9)))))
(org-refile arg)))
"Refile to file:" extra-map extra-help)))
(defun org-starter--bind-file-key (key fpath)
"Bind KEY to a command to visit FPATH."
;; (let ((command-name (org-starter--file-command-name fpath)))
;; (define-key 'org-starter-file-map key
;; (if (and org-starter-define-file-commands
;; (fboundp command-name))
;; command-name
;; `(lambda () (interactive) (find-file ,fpath)))))
(cl-pushnew (cons key fpath) org-starter-key-file-alist
:key 'car :test 'equal))
;;;;; Defining a file
(cl-defun org-starter-define-file (filename &key
directory
(required t)
deprecated
agenda
refile
custom-vars
minor-modes
set-default
capture
key
local-variables
local-config)
"Define an org file.
FILENAME is the file name of the org file. This can be either a file name
without a directory or an absolute file path.
DIRECTORY is a directory that should contain the file. FILENAME and DIRECTORY
are passed to `org-starter-locate-file' function to find an actual path to the
file.
By default, this function raises an error if the file does not exist. This can
be prevented by setting REQUIRED to nil or DEPRECATED to t.
If DEPRECATED is non-nil, this file is not added to `org-refile-targets' even if
the refile option is specified. The file is added to
`org-starter-deprecated-files' which you can use for testing.
If AGENDA is non-nil, the file is added to `org-agenda-files'.
If REFILE is set, the file is added to `org-refile-targets' with the option
value. For example, if you set REFILE to \"'(:maxlevel . 5)\", then
\"'(PATH (:maxlevel . 5))\" is added to `org-refile-targets'.
REFILE can be also a function. If it is a function, the function
is used in `org-starter-refile-by-key'. See
`org-starter-extras-def-reverse-datetree-refile' for example. You
also have to specify KEY, and the file won't be added to
`org-refile-targets'.
If you specify variable names as a list of symbols in CUSTOM-VARS, those
variables are set to the path of the defined file using
`customize-set-variable'.
For example, you can use this function to set `org-default-notes-file' based
on the actual path.
MINOR-MODES is a list of minor modes to turn on in the file. Each
item is usually a symbol like `org-recur-mode', and in that case,
the mode is turned on after the file is loaded. Alternatively, it
can be a cell like \"(org-recur-mode -1)\" to ensure the mode is
turned off in the file.
SET-DEFAULT is almost the same as CUSTOM-VARS. It exists only for
backwards-compatibility. It uses `set-default' instead of
`customize-set-variable'.
CAPTURE specifies a list of `org-capture' templates into the file.
To properly override an existing template with the same key, items in
`org-capture-templates' should be sorted lexicographically by key.
KEY is a string to represent a key binding used to jump to the file.
If this property is nil, the file will not be bound on the map.
LOCAL-VARIABLES allows you to specify file-local variables which should be set
after org-mode is initialized. This can be done in the file footer, but it is
sometimes convenient to be able to define them outside of the file, especially
if you define a complex function. This option should be an alist of variable
names and values.
LOCAL-CONFIG can be a function or an expression called or
evaluated after the file is loaded. For example, you can call
`add-hook' to add buffer-local hooks via this property.
If the file exists and it is properly defined, the path to the file
is returned as the result of this function."
(declare (indent 1))
(let ((fpath (org-starter-locate-file filename directory)))
(cond
(fpath (progn
(when (cl-ecase org-starter-define-file-commands
(all t)
(with-keys key)
(nil nil))
(org-starter--define-file-command fpath))
(when deprecated
(org-starter--log-error "%s file is deprecated"
(abbreviate-file-name fpath))
(add-to-list 'org-starter-deprecated-files fpath))
(if agenda
(add-to-list 'org-agenda-files fpath 'append #'file-equal-p)
(cl-delete fpath org-agenda-files :test #'file-equal-p))
(mapc (lambda (symbol) (customize-set-variable symbol fpath))
(org-starter--to-symbol-list custom-vars))
(mapc (lambda (symbol) (set-default symbol fpath))
(org-starter--to-symbol-list set-default))
(when (and refile (not deprecated))
(cl-etypecase refile
(function
(if key
(let* ((item (list key refile filename))
(cell (assoc (car item) org-starter-extra-refile-map)))
(if cell
(setcdr cell (cdr item))
(push item org-starter-extra-refile-map)))
(user-error "You have to specify KEY if REFILE is a function")))
(list
(let ((pair (assoc fpath org-refile-targets)))
(if pair
(setf (cdr pair) refile)
(add-to-list 'org-refile-targets (cons fpath refile) 'append))))))
(unless deprecated
(dolist (spec (mapcar (or org-starter-capture-template-map-function
#'identity)
capture))
(org-starter-add-file-capture-template fpath spec)))
(when key
(org-starter--bind-file-key key fpath))
(when local-variables
(push (cons fpath local-variables) org-starter-file-local-variables))
(when minor-modes
(push (cons fpath minor-modes) org-starter-file-local-minor-modes))
(when local-config
(push (cons fpath
(cl-etypecase local-config
(function local-config)
;; sexp
(list `(lambda () ,local-config))))
org-starter-file-local-hooks))
(add-to-list 'org-starter-known-files fpath)