aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-org.el
blob: 69a4a9b6bbe196b016aa8360afbd257038c67564 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
;;; my-org.el -- Extensions for org -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotfiles 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 Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Extensions for org.

;;; Code:


(require 'org)

;;; org mode
(defun my-org-open-shell-at-attach-dir ()
  (interactive)
  (require 'my-prog)
  (my-shell-with-directory (concat (org-attach-dir-get-create) "/")))

(defun my-org-links-in-entry ()
  "Get all link urls in an entry"
  (save-excursion
    (org-back-to-heading t)
    (let (links
	        (end (save-excursion (outline-next-heading) (point))))
	    (while (re-search-forward org-link-any-re end t)
	      (push
         (org-unbracket-string "<" ">"
					 (or
						;; [[target][desc]]
						(match-string-no-properties 2)
						;; plain link or <...>
						(match-string-no-properties 0)))
         links))
      links)))

(defun my-org-http-s-links-in-entry ()
  "Get all http(s) urls in an entry"
  (seq-filter (lambda (link)
                (string-prefix-p
                 "http"
                 (progn (string-match org-link-types-re link)
                        (match-string 1 link))))
              (my-org-links-in-entry)))

(defun my-org-insert-date-range (inactive)
  "Insert two dates to form an active date range.

With a prefix, insert inactive dates.
"
  (interactive "P")
  (org-time-stamp nil inactive)
  (insert "--")
  (org-time-stamp nil inactive))

(defun my-org-follow-link-after ()
  (when (eq major-mode 'mhtml-mode)
    (browse-url-of-buffer)))

(defvar my-org-edit-src-was-live-window nil
  "Set to non-nil by `my-org-edit-src-before-exit' if the edit src
buffer was a live window.")

(defun my-org-edit-src-before-exit ()
  "A :before advice for org-edit-src-exit."
  (delete-trailing-whitespace)
  (setq my-org-edit-src-was-live-window (get-buffer-window (current-buffer))))

(defun my-org-element-block-p (element)
  "Returns t if ELEMENT is an org block."
  (memq (org-element-type element)
			  '(center-block comment-block dynamic-block
					             example-block export-block quote-block
					             special-block src-block verse-block)))

(defun my-org-edit-src-after-exit ()
  "An :after advice for `org-edit-src-exit'.

If the block is empty, remove it.
Otherwise deactivate mark and move point to after the block."
  ;; Only proceed if buffer is in a live window, needed to avoid
  ;; indenting triggering this function
  (when my-org-edit-src-was-live-window
    (deactivate-mark)
    (let ((element (org-element-at-point)))
      (cl-assert (my-org-element-block-p element))
      (if (string-empty-p (org-element-property :value element))
          (delete-region (org-element-property :begin element)
                         (org-element-property :end element))
        (goto-char (org-element-property :end element))))))

;; navigation
(defun my-org-jump-to-last-visible-child ()
  "Goto the last visible child."
  (interactive)
  (let (level (pos (point)) (re org-outline-regexp-bol))
    (when (ignore-errors (org-back-to-heading t))
      (setq level (outline-level))
      (forward-char 1)
      (while (and (re-search-forward re nil t) (> (outline-level) level))
	      (when (and (= (outline-level) (1+ level))
		               (not (get-char-property (point) 'invisible)))
	        (setq pos (match-beginning 0)))))
    (goto-char pos)))

(defun my-org-entry-toggle-drawer-visibility ()
  (interactive)
  (save-excursion
    (save-restriction
      (org-narrow-to-subtree)
      (goto-char (point-min))
      (while (re-search-forward "^\\ *:[A-Z]+:\\ *$" nil t)
        (org-hide-drawer-toggle)
        (re-search-forward "^\\ *:END:\\ *$" nil t)))))

(defun my-org-beginning-of-line-or-indent ()
  "Move to org beginning of line, or indentation"
  (interactive)
  (if (bolp)
      (back-to-indentation)
    (org-beginning-of-line)))

(defun my-org-kill-line (&optional arg)
  "Call `org-kill-line', or `my-kill-forward' with a prefix arg."
  (interactive "P")
  (if arg
      (my-kill-forward)
    (org-kill-line)))

(defun my-org-kill-line-backwards (&optional arg)
  "Kill backwards in an org way, or `my-kill-backward' with a prefix arg."
  (interactive "P")
  (if arg
      (my-kill-backward)
    (if (bolp)
        (delete-char -1)
      (kill-region
       (save-excursion (my-org-beginning-of-line-or-indent) (point))
       (point)))))

(defvar my-shell-buffer-list nil
  "List of default org notes file buffers.")

(defun my-org-open-or-cycle-notes (arg)
  "Open default notes file.

Switch to the first buffer with the notes file that's not in a
live window, or find the notes file.

If repeated, cycle through indirect buffers of the default notes.

With a prefix ARG, creates a new indirect buffer of the default
notes file."
  (interactive "P")
  (if arg
      (with-current-buffer (find-file-noselect org-default-notes-file)
        (clone-indirect-buffer nil t)
        (setq my-notes-buffer-list
              (setq-filter 'my-buffer-with-same-base-p
                           (buffer-list))))
    (if (eq last-command 'my-org-open-or-cycle-notes)
        (progn
          (setq my-notes-buffer-list
                (my-list-cycle my-notes-buffer-list))
          (switch-to-buffer (car my-notes-buffer-list)))
      (let ((buffer
             (find-file-noselect org-default-notes-file)))
        (with-current-buffer buffer
          (setq my-notes-buffer-list
                (seq-filter 'my-buffer-with-same-base-p (buffer-list))))
        ;; Find first buffer that is not in a live window. If no such
        ;; buffer exists, fall back to `find-file-noselect'.
        (setq buffer
              (or
               (seq-find
                (lambda (buffer)
                  (not (my-buffer-live-window-p buffer)))
                my-notes-buffer-list)
               (find-file-noselect org-default-notes-file)))
        (while (not (eq buffer (car my-notes-buffer-list)))
          (setq my-notes-buffer-list
                (my-list-cycle my-notes-buffer-list))))
      (switch-to-buffer (car my-notes-buffer-list))
      )))

;; links
(defun my-org-substitute-gnus-link-after-archiving ()
  "Fix a captured gnus article link after they've been archived"
  (interactive)
  (when (org-in-regexp org-link-bracket-re)
    ;; We do have a link at point, and we are going to edit it.
    (save-excursion
      (let ((remove (list (match-beginning 0) (match-end 0)))
            (desc (when (match-end 2) (match-string-no-properties 2)))
            (link (match-string-no-properties 1))
            (target (alist-get 'archive my-gnus-group-default-targets))
            (new-link))
        (pcase-dolist (`(,re . ,info) my-gnus-group-alist)
          (when (and (string-match re link)
                     (alist-get 'archive info))
            (setq target (alist-get 'archive info))))
        (setq new-link
	            (replace-regexp-in-string "/.*?#" (format "/%s#" target)
                                        link))
        (apply #'delete-region remove)
        (insert (org-link-make-string new-link desc))
        (sit-for 0)))))

;; editing heading
(defun my-org-orgzly-merge-link ()
  "Fixes orgzly entries with links separated from headlines.
Find the first link in the entry, and add that to the headline
title, and remove the body."
  (interactive)
  (save-restriction
    (save-excursion
      (org-narrow-to-subtree)
      (goto-char (point-min))
      (forward-line)
      (when (re-search-forward org-link-any-re)
	      (let ((link (buffer-substring-no-properties
		                 (match-beginning 0) (match-end 0)))
              (unused (replace-match "" nil))
	            (desc (org-entry-get (point) "ITEM"))
              (title-loc))
          (goto-char (point-min))
          (search-forward desc nil t)
          (setq title-loc (match-beginning 0))
          (replace-match "" nil)
          (while (search-forward desc nil t) (replace-match "" nil))
	        (goto-char title-loc)
	        (insert (org-link-make-string link desc))))))
  (my-org-node-flush-empty-lines))

(defun my-org-node-flush-empty-lines ()
  (save-restriction
    (save-excursion
      (org-narrow-to-subtree)
      (flush-lines "^$"))))

(defun my-org-element-contents-at-point ()
  (let ((element (org-element-at-point)))
    (buffer-substring-no-properties
     (org-element-property :contents-begin element)
     (org-element-property :contents-end element))))


(defun my-org-append-subheading (arg)
	"Append a subheading as a first child, or with an arg as a last child."
	(interactive "P")
	(if arg
			(org-insert-subheading '(4))
		(let ((required-level (1+ (or (org-current-level) 0))))
			(org-show-children)
			(org-next-visible-heading 1)
			(org-insert-subheading nil)
			(while (> (org-current-level) required-level)
				(org-promote-subtree))
			(while (< (org-current-level) required-level)
				(org-demote-subtree)))))

;; copy a link
;;; fixme: do we still need this?
(defun my-org-copy-link-at-point ()
  (interactive)
  (let ((link (my-org-link-at-point)))
    (if link
        (progn
          (kill-new link)
          (message "Copied: %s" link))
      (message "Point is not an org link!"))))

(defun my-org-link-at-point ()
  (interactive)
  (when (org-in-regexp org-link-any-re)
    (org-unbracket-string "<" ">"
			(or
			 ;; [[target][desc]]
			 (match-string-no-properties 2)
			 ;; plain link or <...>
			 (match-string-no-properties 0)))))

(defun my-org-store-link-and-return ()
  "run org-goto to select a heading, stores its link and insert it."
  (interactive)
  (save-restriction
    (widen)
    (save-excursion
      (call-interactively 'org-goto)
      (call-interactively 'org-store-link)))
  (call-interactively 'org-insert-last-stored-link))

;; overload org-insert-all-links (do we need autoload as in the original file?)
(defun my-org-insert-all-links (arg &optional pre post)
  "Insert all links in `org-stored-links'.
When a universal prefix, do not delete the links from `org-stored-links'.
When `ARG' is a number, insert the last N link(s).
`PRE' and `POST' are optional arguments to define a string to
prepend or to append."
  (interactive "P")
  (let ((org-link-keep-stored-after-insertion (equal arg '(4)))
	      (links (copy-sequence org-stored-links))
	      (pr (or pre "- "))
	      (po (or post "\n"))
	      (cnt 1) l)
    (if (null org-stored-links)
	      (message "No link to insert")
      (while (and (or (listp arg) (>= arg cnt))
		              (setq l (if (listp arg)
			                        (pop links)
			                      (pop org-stored-links))))
	      (setq cnt (1+ cnt))
	      (insert pr)
	      (org-insert-link nil (car l) (or (cadr l) ""))
	      (insert po)))))

;; overload org-open-at-point-global to fix bug property link not
;; opened in external browser (2d0e61c8-da74-417e-8ccd-c4099ccd88d8)
(defun my-org-open-at-point-global (&optional arg)
  "Follow a link or a time-stamp like Org mode does.
Also follow links and emails as seen by `thing-at-point'.
This command can be called in any mode to follow an external
link or a time-stamp that has Org mode syntax.  Its behavior
is undefined when called on internal links like fuzzy links.
Raise a user error when there is nothing to follow."
  (interactive "P")
  (let ((tap-url (thing-at-point 'url))
	      (tap-email (thing-at-point 'email)))
    (cond ((org-in-regexp org-link-any-re)
	         (org-link-open-from-string (match-string-no-properties 0) arg))
	        ((or (org-in-regexp org-ts-regexp-both nil t)
	             (org-in-regexp org-tsr-regexp-both nil t))
	         (org-follow-timestamp-link))
	        (tap-url (org-link-open-from-string tap-url))
	        (tap-email (org-link-open-from-string
		                  (concat "mailto:" tap-email)))
	        (t (user-error "No link found")))))

;; overload org-refile-get-targets
(defun my-org-refile-get-targets (&optional default-buffer)
  "Produce a table with refile targets."
  (let ((case-fold-search nil)
	      ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
	      (entries (or org-refile-targets '((nil . (:level . 1)))))
	      targets tgs files desc descre)
    (message "Getting targets...")
    (with-current-buffer (or default-buffer (current-buffer))
      (dolist (entry entries)
	      (setq files (car entry) desc (cdr entry))
	      (cond
	       ((null files) (setq files (list (current-buffer))))
	       ((eq files 'org-agenda-files)
	        (setq files (org-agenda-files 'unrestricted)))
	       ((and (symbolp files) (fboundp files))
	        (setq files (funcall files)))
	       ((and (symbolp files) (boundp files))
	        (setq files (symbol-value files))))
	      (when (stringp files) (setq files (list files)))
	      (cond
	       ((eq (car desc) :tag)
	        (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
	       ((eq (car desc) :todo)
	        (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
	       ((eq (car desc) :regexp)
	        (setq descre (cdr desc)))
	       ((eq (car desc) :level)
	        (setq descre (concat "^\\*\\{" (number-to-string
					                                (if org-odd-levels-only
					                                    (1- (* 2 (cdr desc)))
					                                  (cdr desc)))
			                         "\\}[ \t]")))
	       ((eq (car desc) :maxlevel)
	        (setq descre (concat "^\\*\\{1," (number-to-string
					                                  (if org-odd-levels-only
						                                    (1- (* 2 (cdr desc)))
					                                    (cdr desc)))
			                         "\\}[ \t]")))
	       (t (error "Bad refiling target description %s" desc)))
	      (dolist (f files)
	        (with-current-buffer (if (bufferp f) f (org-get-agenda-file-buffer f))
	          (or
	           (setq tgs (org-refile-cache-get
                        (buffer-file-name
                         (when (bufferp f) (buffer-base-buffer f)))
                        descre))
	           (progn
	             (when (bufferp f)
		             (setq f (buffer-file-name (buffer-base-buffer f))))
	             (setq f (and f (expand-file-name f)))
	             (when (eq org-refile-use-outline-path 'file)
		             (push (list (and f (file-name-nondirectory f)) f nil nil) tgs))
	             (when (eq org-refile-use-outline-path 'buffer-name)
		             (push (list (buffer-name (buffer-base-buffer)) f nil nil) tgs))
	             (when (eq org-refile-use-outline-path 'full-file-path)
		             (push (list (and (buffer-file-name (buffer-base-buffer))
                                  (file-truename (buffer-file-name (buffer-base-buffer))))
                             f nil nil) tgs))
	             (org-with-wide-buffer
		            (goto-char (point-min))
		            (setq org-outline-path-cache nil)
		            (while (re-search-forward descre nil t)
		              (beginning-of-line)
		              (let ((case-fold-search nil))
		                (looking-at org-complex-heading-regexp))
		              (let ((begin (point))
			                  (heading (match-string-no-properties 4)))
		                (unless (or (and
				                         org-refile-target-verify-function
				                         (not
				                          (funcall org-refile-target-verify-function)))
				                        (not heading))
		                  (let ((re (format org-complex-heading-regexp-format
					                              (regexp-quote heading)))
			                      (target
			                       (if (not org-refile-use-outline-path) heading
			                         (mapconcat
				                        #'identity
				                        (append
				                         (pcase org-refile-use-outline-path
				                           (`file (list
                                           (and (buffer-file-name (buffer-base-buffer))
                                                (file-name-nondirectory
                                                 (buffer-file-name (buffer-base-buffer))))))
				                           (`full-file-path
				                            (list (buffer-file-name
					                                 (buffer-base-buffer))))
				                           (`buffer-name
				                            (list (buffer-name
					                                 (buffer-base-buffer))))
				                           (_ nil))
				                         (mapcar (lambda (s) (replace-regexp-in-string
						                                          "/" "\\/" s nil t))
					                               (org-get-outline-path t t)))
				                        "/"))))
			                  (push (list target f re (org-refile-marker (point)))
			                        tgs)))
		                (when (= (point) begin)
		                  ;; Verification function has not moved point.
		                  (end-of-line)))))))
	          (when org-refile-use-cache
	            (org-refile-cache-put tgs (buffer-file-name) descre))
	          (setq targets (append tgs targets))))))
    (message "Getting targets...done")
    (delete-dups (nreverse targets))))

;; shadow org-insert-last-stored-link (do not insert \n at the end)
(defun my-org-insert-last-stored-link (arg)
  "Insert the last link stored in `org-stored-links'."
  (interactive "p")
  (org-insert-all-links arg "" ""))

(defun my-org-info-open-new-window (path)
  "Open info in a new buffer"
  (my-select-new-window-matching-mode 'Info-mode)
  (org-info-follow-link path))

(defun my-org-rt-open-new-window (path)
  "Open rt in a new buffer"
  (my-select-new-window-matching-mode 'rt-liber-browser-mode)
  (rt-org-open path))

;; fix org src overlay face
(defun my-org-src--make-source-overlay (beg end edit-buffer)
  "Create overlay between BEG and END positions and return it.
EDIT-BUFFER is the buffer currently editing area between BEG and
END."
  (let ((overlay (make-overlay beg end)))
    (overlay-put overlay 'face 'region)
    (overlay-put overlay 'edit-buffer edit-buffer)
    (overlay-put overlay 'help-echo
		             "Click with mouse-1 to switch to buffer editing this segment")
    (overlay-put overlay 'face 'region)
    (overlay-put overlay 'keymap
		             (let ((map (make-sparse-keymap)))
		               (define-key map [mouse-1] 'org-edit-src-continue)
		               map))
    (let ((read-only
	         (list
	          (lambda (&rest _)
	            (user-error
	             "Cannot modify an area being edited in a dedicated buffer")))))
      (overlay-put overlay 'modification-hooks read-only)
      (overlay-put overlay 'insert-in-front-hooks read-only)
      (overlay-put overlay 'insert-behind-hooks read-only))
    overlay))

(defun my-org-copy-property-value (name)
  (interactive
   (list (completing-read "Copy property: " (org-entry-properties))))
  (let ((value (org-entry-get (point) name)))
    (kill-new value)
    (message "Copied %s" value)))

(defvar my-org-common-properties nil
  "Property list for completion when setting the property of an org node, to
  avoid scanning the whole notes.")

(defun my-org-set-common-property ()
  (interactive)
  (let* ((property
          (completing-read "Which property to set: "
                           my-org-common-properties))
         (value
          (org-read-property-value property)))
    (org-set-property property value)))

(defun my-org-copy-src-block-at-point ()
  (interactive)
  (when (org-in-src-block-p)
    (kill-new (nth 1 (org-babel-get-src-block-info t)))
    (message "org src block copied!")))
(defun my-org-in-or-at-block-p ()
  (or (org-at-block-p)
      (org-in-block-p '("example" "source" "export"
                        "center" "quote" "verse"))))
(defun my-org-copy-block-at-point ()
  (interactive)
  (save-excursion
    (unless (org-at-block-p)
      (org-previous-block 1)
      (let ((element (org-element-at-point)))
        (kill-new (or
                   (org-element-property :value element)
                   (buffer-substring
                    (org-element-property :contents-begin element)
                    (org-element-property :contents-end element))))
        (message "org block copied!")))))

;; clock save timer doesn't seem to be working
(defun my-org-clock-maybe-save ()
  (when (equal major-mode 'org-mode)
    (org-clock-save)))

(defun my-org-clock-kill-entries ()
  "Kill all clock entries at the org node at point.

Assuming they are in the logbook drawer"
  (interactive)
  (let ((end)
        (kill-whole-line t))
    (save-restriction
      (org-narrow-to-subtree)
      (goto-char (point-min))
      (when (re-search-forward "^\\ *:LOGBOOK:\\ *$" nil t)
        (kill-new "")
        (setq end (save-excursion
                    (re-search-forward "^\\ *:END:\\ *$")
                    (point)))
        (while (re-search-forward "^\\ *CLOCK: .*--.*$" end t)
          (beginning-of-line)
          (append-next-kill)
          (kill-line))))))

(defun my-org-element-at-drawer-p ()
  "Returns t if point is at a drawer beginning or end."
  (not (not
        (member (org-element-type (org-element-at-point))
                '(drawer property-drawer)))))

(defun my-org-kill-logbook-entries ()
  "Kill all logbook entries of the org node at point."
  (interactive)
  (let ((beg))
    (save-excursion
      (save-restriction
        (org-narrow-to-subtree)
        (goto-char (point-min))
        (when (and (re-search-forward "^\\ *:LOGBOOK:\\ *$" nil t)
                   (my-org-element-at-drawer-p))
          (beginning-of-line 2)
          (setq beg (point))
          (when (re-search-forward "^\\ *:END:\\ *$" nil t)
            (beginning-of-line)
            (kill-region beg (point))))))))

(defun my-org-logbook-yank ()
  "Yank whatever is in the kill ring into the logbook drawer."
  (interactive)
  (let ((end))
    (save-restriction
      (org-narrow-to-subtree)
      (goto-char (point-min))
      (if (and (re-search-forward "^\\ *:LOGBOOK:\\ *$" nil t)
               (my-org-element-at-drawer-p))
          ;; If there's already a logbook, move to where the clock
          ;; entries should be inserted
          (progn
            (setq end (save-excursion
                        (re-search-forward "^\\ *:END:\\ *$")
                        (beginning-of-line)
                        (point)))
            ;; Insert after active clock (if any)
            (if (re-search-forward "^\\ *CLOCK: .*--.*$" end t)
                (beginning-of-line)
              (goto-char end)))
        (beginning-of-line 2)
        (org-insert-drawer nil "LOGBOOK")
        (delete-char 1))
      (yank))))

(defun my-org-refile-logbook ()
  (interactive)
  (my-org-kill-logbook-entries)
  (save-excursion
    (org-refile '(4))
    (my-org-logbook-yank)))

;;; to remove
(defun my-org-clock-collect-entries (drawer &optional remove)
  "Collect all clock entries from DRAWER.
Remove them from DRAWER if REMOVE is non-nil."
  (cl-assert (eq (org-element-type drawer) 'drawer)
             nil
             "Expected a drawer got %s" (org-element-type drawer))
  (when (string-equal (org-element-property :drawer-name drawer)
                      (or (org-log-into-drawer) "LOGBOOK"))
    (let ((ret
           (cl-loop
            for element in-ref (org-element-contents drawer)
            if (eq (org-element-type element) 'clock)
            collect element
            and if remove do (setf element nil))))
      (org-element-set-contents
       (cl-remove nil (org-element-contents drawer)))
      ret)))

(defun org-element-remove (el)
  "Delete org element EL from its parent."
  (let ((parent (org-element-property :parent el)))
    (org-element-set-contents
     parent
     (cl-remove el (org-element-contents parent)))))

(defun org-element-clock-start<= (c1 c2)
  "Compare two clock elements as returned by `org-element-clock-parser'
and return non-nil if the C1 starts not later than c2."
  (setq c1 (org-element-property :value c1)
	c2 (org-element-property :value c2))
  (cl-loop with val1 with val2
	   for test in '(:year-start :month-start :day-start :hour-start :minute-start)
	   do (setq val1 (org-element-property test c1) val2 (org-element-property test c2))
	   if (< val1 val2)
	   return t
	   if (> val1 val2)
	   return nil
	   finally return t))

(defun org-merge-subtree-clocks (&optional remove)
  "Merge clocks in subtree of headline starting at POINT.
Remove empty logbooks in the sub-tree if REMOVE is non-nil.

Interactively remove empty logbooks when called with prefix-arg \\[universal-argument]."
  (interactive "P")
  (let* (b e
           (headline (org-element-at-point))
           (type (car headline))
           (props (cadr headline)))
    (cl-assert (eq type 'headline)
               nil
               "Expected headline got %s" type)
    (setq b (plist-get props :begin)
          e (plist-get props :end))
    (save-restriction
      (narrow-to-region b e)
      (let* ((data (org-element-parse-buffer))
             (clocks (apply #'append
                            (org-element-map data 'drawer
                              (lambda (el)
                                "Remove all clocks from all drawers and collect them in CLOCKS."
                                (prog1 (org-collect-clock-entries el t)
                                  (when (and remove
					                                   (null (org-element-contents el)))
                                    (org-element-remove el)
                                    )))))))
	(setq clocks (sort clocks #'org-element-clock-start<=))
        (setq headline (org-element-map data 'headline 'identity nil t)) ;; get the first headline within data
        (or (org-element-map
                (org-element-contents headline)
                'drawer ;; check for existing drawer
              (lambda (el)
                "Write all clocks to the first LOGBOOK drawer."
                (when (equal (org-element-property :drawer-name el)
                             (or (org-log-into-drawer) "LOGBOOK"))
                  (org-element-set-contents el clocks)
                  t) ;; indicate that we are done
                )
              nil t 'headline) ;; check only first drawer of current headline
            ;; no drawer yet: add a new one
            (org-element-set-contents
             headline
             (cons (list 'drawer '(:drawer-name "LOGBOOK")
                         clocks)
                   (org-element-contents headline))))
        (kill-region (point-min) (point-max))
        (insert (org-element-interpret-data data))))))

(defun my-org-refile-cache-rebuild ()
  (org-refile-cache-clear)
  (org-refile-get-targets))

(defun my-org-store-agenda-view-A ()
  (interactive)
  (org-store-agenda-views)
  (my-org-agenda-ensure-A))

(defun my-org-agenda-priority-0 ()
  (interactive)
  (org-agenda-priority ?\ ))
(defun my-org-agenda-priority-A ()
  (interactive)
  (org-agenda-priority ?A))
(defun my-org-agenda-priority-B ()
  (interactive)
  (org-agenda-priority ?B))
(defun my-org-agenda-priority-C ()
  (interactive)
  (org-agenda-priority ?C))

(defun my-org-next-block-or-results (arg &optional backward)
  "Jump to the next block or results.

With a prefix argument ARG, jump forward ARG many blocks.

When BACKWARD is non-nil, jump to the previous block.

When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
Match data is set according to this regexp when the function
returns.

Return point at beginning of the opening line of found block.
Throw an error if no block is found."
  (interactive "p")
  (let ((re "^[ \t]*#\\+\\(BEGIN\\|RESULTS:\\)")
	      (case-fold-search t)
	      (search-fn (if backward #'re-search-backward #'re-search-forward))
	      (count (or arg 1))
	      (origin (point))
	      last-element)
    (if backward (beginning-of-line) (end-of-line))
    (while (and (> count 0) (funcall search-fn re nil t))
      (let ((element (save-excursion
		                   (goto-char (match-beginning 0))
		                   (save-match-data (org-element-at-point)))))
	      (when (and (memq (org-element-type element)
			                   '(center-block comment-block dynamic-block
					                              example-block export-block quote-block
					                              special-block src-block verse-block fixed-width))
		               (<= (match-beginning 0)
		                   (org-element-property :post-affiliated element)))
	        (setq last-element element)
	        (cl-decf count))))
    (if (= count 0)
	      (prog1 (goto-char (org-element-property :post-affiliated last-element))
	        (save-match-data (org-show-context)))
      (goto-char origin)
      (user-error "No %s code blocks" (if backward "previous" "further")))))

(defun my-org-previous-block-or-results (arg)
  "Jump to the previous block or results.
With a prefix argument ARG, jump backward ARG many source blocks.
When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  (interactive "p")
  (my-org-next-block-or-results arg t))

;; override org-next-link to include search in any places, including property
;; drawers.
;; TODO: not working yet
;; https://lists.gnu.org/archive/html/emacs-orgmode/2020-01/msg00186.html
(defun my-org-next-link ()
  (interactive)
  (when (org-in-regexp org-any-link-re)
    (re-search-forward org-any-link-re nil t))
  (re-search-forward org-any-link-re nil t)
  (re-search-backward org-any-link-re nil t)
  (when-let ((link (org-element-lineage (org-element-context) '(link) t)))
    (goto-char (org-element-property :begin link)))
  (when (org-invisible-p) (org-show-context)))

(defun my-org-previous-link ()
  (interactive)
  (re-search-backward org-any-link-re nil t)
  (when-let ((link (org-element-lineage (org-element-context) '(link) t)))
    (goto-char (org-element-property :begin link)))
  (when (org-invisible-p) (org-show-context)))

(defun my-org-attach-edit-attached-image ()
  (interactive)
  (start-process
   "pinta" nil "/usr/bin/pinta"
   (concat (org-attach-dir) "/"
	         (org-element-property :path (org-element-context)))))

(defun my-org-capture-place-template-dont-delete-windows (oldfun args)
  (cl-letf (((symbol-function 'delete-other-windows) 'ignore))
    (apply oldfun args)))

(defvar my-org-attach-copy-attached-targets nil
  "Alist of targets to copy attached to, in the form of (name . path)")
(defvar my-org-attach-copy-attached-doc-exts
  '("epub" "pdf" "mobi"))
(defvar my-org-attach-copy-attached-doc-re
  (format "\\.\\(%s\\)$"
          (string-join
           my-org-attach-copy-attached-doc-exts "\\|")))

(defun my-org-attach-copy-attached-docs ()
  (interactive)
  (let* ((name
          (completing-read "Copy attached docs to: "
                           my-org-attach-copy-attached-targets))
         (path (alist-get name my-org-attach-copy-attached-targets
                          nil nil #'equal)))
    (let ((basedir (org-attach-dir)))
      (dolist (attached (org-attach-file-list basedir))
        (when (string-match my-org-attach-copy-attached-doc-re attached)
	        (message "Copying %s to %s (%s)..." attached name path)
	        (copy-file (file-name-concat basedir attached)
		                 (file-name-concat
                      path
			                (replace-regexp-in-string ":" "_" attached)))
	        (message "Done!")))))
  )

(defun my-org-attach-all-url-plaintext (arg)
  (interactive "P")
  (dolist (url (my-org-http-s-links-in-entry))
    (my-org-attach-url-plaintext url)))

(defun my-org-attach-url-plaintext (url)
  (interactive (list (completing-read "Url to fetch: " (my-org-http-s-links-in-entry))))
  (my-org-attach-url-plaintext-internal url current-prefix-arg t))

(defun my-org-attach-url-plaintext-all-media (url)
  (interactive (list (completing-read "Url to fetch: "
                                      (my-org-http-s-links-in-entry))))
  (my-org-attach-url-plaintext-internal url current-prefix-arg t t))

(defun my-org-attach-url (url)
  (interactive (list (completing-read "Url to fetch: "
                                      (my-org-http-s-links-in-entry))))
  (let* ((url (my-rewrite-url url))
         (filename (expand-file-name (my-make-filename-from-url url)
                                     (org-attach-dir t))))
    (my-wget-async url filename current-prefix-arg)))

(defun my-org-attach-url-plaintext-internal (url &optional no-tor move-if-large save-all-media)
  (let* ((lynx-buffer (format "*lynx %s*" url))
         (url (my-rewrite-url url))
         (filename (expand-file-name (my-make-filename-from-url url)
                                     (org-attach-dir t)))
         (coding-system-for-write 'utf-8))
    (ignore-errors (kill-buffer lynx-buffer))
    (my-touch-new-file filename)
    (org-attach-sync)
    (set-process-sentinel
     (my-start-process-with-torsocks
      current-prefix-arg
      "org-lynx" lynx-buffer "lynx" "-dump" "--display_charset" "utf-8" url)
     (lambda (process event)
       (with-current-buffer (process-buffer process)
         (goto-char (point-min))
         (write-file filename)
         (message "Lynx dumped to: %s" filename)
         (when save-all-media
           (when-let ((urls (http-s-media-links-in-buffer)))
             (message "Downloading %d media files..." (length urls))
             (wget-async-urls-with-prefix
              urls (concat filename "-") no-tor move-if-large))))))))

;; node creation; start of grok
;; FIXME: decouple clients from org
(defun my-org-create-node (info &optional attach)
  (cond ((alist-get "Authors" info nil nil 'string=)
	       (my-org-create-book-node info attach))
	      ((alist-get "Director" info nil nil 'string=)
	       (my-org-create-video-node info attach))
	      ((and (alist-get "Developers" info nil nil 'string=)
              (string-match "\\<game\\>"
                            (alist-get "Description" info nil nil 'string=)))
	       (my-org-create-video-game-node info attach))
        ((alist-get "Developers" info nil nil 'string=)
         (my-org-create-software-node info attach))
	      ((alist-get "Designers" info nil nil 'string=)
	       (my-org-create-game-node info attach))
	      ((alist-get "Founded" info nil nil 'string=)
         (my-org-create-organisation-node info attach))
        ((alist-get "Latitude" info nil nil 'string=)
	       (my-org-create-location-node info attach))
        ((alist-get "Born" info nil nil 'string=)
         (my-org-create-people-node info attach))
	      (t (my-org-create-entity-node info attach))))

(defun my-org-attach-and-add-properties-to-node (info attach)
  (when (and attach (alist-get "Cover" info nil nil 'string=))
    (ignore-error 'file-already-exists
      (org-attach-url (alist-get "Cover" info nil nil 'string=)))
    (setq info (assoc-delete-all "Cover" info 'string=)))
  (dolist (pair info)
    (when (and (cdr pair) (string> (cdr pair) ""))
      (org-entry-put (point)
		                 (decode-coding-string (car pair) 'utf-8)
		                 (decode-coding-string (cdr pair) 'utf-8))))
  (org-entry-put (point) "CREATED"
                 (format-time-string "[%Y-%m-%d %a %H:%M]" (current-time)))
  (org-attach-sync)
  (when (buffer-narrowed-p)
    (goto-char (point-min))))

(defun my-org-create-book-node (book-info attach)
  (org-capture nil "book")
  (insert (format
	         "%s - %s - %s"
	         (or (alist-get "Authors" book-info "" nil 'string=) "")
	         (alist-get "Title" book-info "" nil 'string=)
	         (my-extract-year
	          (alist-get "Published" book-info "" nil 'string=))))
  (my-org-attach-and-add-properties-to-node book-info attach))
(defun my-org-create-video-node (video-info attach)
  (org-capture nil "video")
  (insert (format
	         "%s - %s - %s"
	         (alist-get "Director" video-info "" nil 'string=)
	         (alist-get "Title" video-info "" nil 'string=)
	         (my-extract-year
	          (alist-get "Released" video-info "" nil 'string=))))
  (my-org-attach-and-add-properties-to-node video-info attach))
(defun my-org-create-location-node (book-info attach)
  (org-capture nil "location")
  (insert (format
	         "%s"
	         (alist-get "Title" book-info "" nil 'string=)))
  (my-org-attach-and-add-properties-to-node book-info attach))
(defun my-org-create-game-node (game-info attach)
  (org-capture nil "game")
  (insert (format
	         "%s - %s - %s"
	         (alist-get "Designers" game-info "" nil 'string=)
	         (alist-get "Title" game-info "" nil 'string=)
	         (my-extract-year
	          (alist-get "Published" game-info "" nil 'string=))))
  (my-org-attach-and-add-properties-to-node game-info attach))
(defun my-org-create-video-game-node (game-info attach)
  (org-capture nil "videogame")
  (insert (format
	         "%s - %s - %s"
	         (alist-get "Developers" game-info "" nil 'string=)
	         (alist-get "Title" game-info "" nil 'string=)
	         (my-extract-year
	          (alist-get "Released" game-info "" nil 'string=))))
  (my-org-attach-and-add-properties-to-node game-info attach))
(defun my-org-create-software-node (software-info attach)
  (org-capture nil "software")
  (insert (format
	         "%s - %s"
           (alist-get "Title" software-info "" nil 'string=)
	         (my-extract-year
	          (alist-get "Released" software-info "" nil 'string=))))
  (my-org-attach-and-add-properties-to-node software-info attach))
(defun my-org-create-organisation-node (organisation-info attach)
  (org-capture nil "organisation")
  (insert (format
	         "%s - %s"
           (alist-get "Title" organisation-info "" nil 'string=)
           (my-extract-year
            (alist-get "Founded" organisation-info "" nil 'string=))))
  (my-org-attach-and-add-properties-to-node organisation-info attach))
(defun my-org-create-people-node (people-info attach)
  (org-capture nil "people")
  (insert (format
	         "%s - %s-%s"
           (alist-get "Title" people-info "" nil 'string=)
           (my-extract-year (alist-get "Born" people-info "" nil 'string=))
           (my-extract-year (alist-get "Died" people-info "" nil 'string=))))
  (my-org-attach-and-add-properties-to-node people-info attach))
(defun my-org-create-pacman-software-node (package)
  (interactive "sPacman package name: ")
  (my-org-create-software-node (my-grok-pacman package) nil))
(defun my-org-create-entity-node (entity-info attach)
  (org-capture nil "entity")
  (insert (format
	         "%s"
	         (alist-get "Title" entity-info "" nil 'string=)))
  (my-org-attach-and-add-properties-to-node entity-info attach))
(defun my-org-create-audio-node (audio-info attach)
  (org-capture nil "ya")
  (insert (format
	         "%s - %s - %s"
	         (or (alist-get "Authors" audio-info "" nil 'string=) "")
	         (alist-get "Title" audio-info "" nil 'string=)
	         (my-extract-year
	          (alist-get "Published" audio-info "" nil 'string=))))
  (my-org-attach-and-add-properties-to-node audio-info attach))

;; TODO: these requires are unnecessary for more essential functionalities of
;; org customisation. Find a way to delay them
(require 'my-wikipedia)
(require 'my-github)
(require 'my-gitlab)
(require 'my-pacman)
(require 'my-openlibrary)
(require 'my-codeberg)
(defun my-grok-dispatcher (url)
  (when-let ((host (url-host (url-generic-parse-url url))))
    (cond ((string-match "wikipedia\\.org" host) 'my-grok-wikipedia)
          ((string-match "github\\.com" host) 'my-grok-github)
          ((string-match "codeberg\\.org" host) 'my-grok-codeberg)
          ((string-match "\\(gitlab\\.\\|salsa.debian.org\\)" host)
           'my-grok-gitlab)
          ((string-match "openlibrary.org" host) 'my-grok-openlibrary)
          (t nil))))
(defun my-grok-update-properties ()
  (interactive)
  (when-let* ((url (org-entry-get (point) "Source"))
              (source-dispatcher (my-grok-dispatcher url)))
    (my-org-attach-and-add-properties-to-node
     (funcall source-dispatcher url) t))
  (when-let ((isbn (org-entry-get (point) "ISBN")))
    (my-org-attach-and-add-properties-to-node (my-grok-openlibrary-isbn isbn) t))
  (when-let ((url (org-entry-get (point) "OpenLibrary-link")))
    (my-org-attach-and-add-properties-to-node (my-grok-openlibrary url) t))
  (when-let ((package (org-entry-get (point) "Pacman-package-name")))
    (my-org-attach-and-add-properties-to-node (my-grok-pacman package) nil))
  (when-let ((url (org-entry-get (point) "Wikipedia-link")))
    (my-org-attach-and-add-properties-to-node (my-grok-wikipedia url) t)))
(defun my-org-protocol-grok (data)
  (when-let ((url (plist-get data :url)))
    (my-org-grok url))
  nil)

(defun my-org-grok (url)
  (when-let* ((grok-fun (my-grok-dispatcher url))
              (info (funcall grok-fun url)))
    (my-org-create-node info t)))

(defun my-eww-org-protocol-grok ()
  "grok from eww"
  (interactive)
  (org-protocol-grok
   (list :url (plist-get eww-data :url))))

;; org capture rss
(defun my-org-rss-xml-create-audio-node (url)
  (interactive (list (read-string "Feed URL: "
				                          (thing-at-point-url-at-point))))
  (my-org-rss-xml-create-node url 'my-org-create-audio-node))
(defun my-org-rss-xml-create-book-node (url)
  (interactive (list (read-string "Feed URL: "
				                          (thing-at-point-url-at-point))))
  (my-org-rss-xml-create-node url 'my-org-create-book-node))
(defun my-org-rss-xml-create-node (url create-node-fun)
  (let* ((xml
	        (with-current-buffer (url-retrieve-synchronously url)
	          (my-skip-http-header)
	          (car (xml-parse-region (point) (point-max)))))
	       (channel (my-xml-get-first-child xml 'channel))
	       )
    (funcall create-node-fun
	           (list
	            (cons "Feed-url" url)
	            (cons "Title" (decode-coding-string
                             (my-xml-get-first-child-text channel 'title) 'utf-8))
	            (cons "Description"
		                (decode-coding-string
                     (my-xml-get-first-child-text channel 'description) 'utf-8))
	            (cons "Website" (my-xml-get-first-child-text channel 'link))
	            (cons "Cover"
		                (or (my-xml-get-first-child-text
			                   (my-xml-get-first-child channel 'image) 'url)
			                  (dom-attr
			                   (my-xml-get-first-child channel 'itunes:image) 'href)))
	            (cons "Authors" (my-xml-get-first-child-text
                               channel 'itunes:author)))
	           t)))

(require 'my-algo)
(defun my-radix-org-from-tree (tree)
  (let ((radix-tree-type 'vector))
    (radix-tree-iter-subtrees
     tree
     (my-radix-org-iter-n 1 []))))

(defun my-radix-org-iter-n (depth prefix)
  (lambda (p s)
    (let ((nprefix (seq-concatenate radix-tree-type prefix p)))
      (insert (make-string depth ?*) " ")
      (pcase s
        ((radix-tree-leaf v) (insert "[[" (string-join nprefix "/") "][" (string-join p "/") "]]" "\n"))
        (_
         (insert (string-join p "/") "\n")
         (radix-tree-iter-subtrees s (my-radix-org-iter-n (1+ depth) nprefix)))))))

(defun my-radix-org ()
  (interactive)
  (let* ((file-name (buffer-file-name))
         (buffer-name (file-name-with-extension
                       (file-name-base file-name)
                       "org"))
         (save-file-name (file-name-with-extension file-name "org"))
         (tree (let ((max-lisp-eval-depth 32000))
                 (save-excursion (my-radix-tree-from-list)))))
    (with-current-buffer (get-buffer-create buffer-name)
      (org-mode)
      (erase-buffer)
      (my-radix-org-from-tree tree)
      (goto-char (point-min)))
    (switch-to-buffer buffer-name)))

;;; format org mode elements
(defun my-org-format-link (url text)
  (format "[[%s][%s]]" url text))

(defun my-org-format-heading (text level)
  (format "%s %s"
          (make-string level ?*) text))

(defun my-org-update-updated ()
  (interactive)
  (when (and (derived-mode-p 'org-mode)
             (not (org-before-first-heading-p)))
    (org-entry-put
     (point) "UPDATED"
     (format-time-string "[%Y-%m-%d %a %H:%M]" (current-time)))))

;;; override org-recoll-format-results
(defun my-org-recoll-format-results ()
  (require 'org-recoll)
  "Format recoll results in buffer."
  ;; Format results in org format and tidy up
  (org-recoll-regexp-replace-in-buffer
   "^.*?\\[\\(.*?\\)\\]\\s-*\\[\\(.*?\\)\\]\\(.*\\)$"
   "* [[\\1][\\2]] <\\1>\\3")
  (org-recoll-regexp-replace-in-buffer
   (format "<file://.*?%s\\(.*/\\).*>" (substring my-docs-root-dir 1))
   "(\\1)")
  (org-recoll-regexp-replace-in-buffer "\\/ABSTRACT" "")
  (org-recoll-regexp-replace-in-buffer "ABSTRACT" "")
  ;; Justify results
  (goto-char (point-min))
  (org-recoll-fill-region-paragraphs)
  ;; Add emphasis
  (highlight-phrase (org-recoll-reformat-for-file-search
                     org-recoll-search-query)
                    'bold-italic))

(defun my-org-recoll-mdn (query)
  (interactive "sSearch mdn: ")
  (org-recoll-search (format "%s dir:mdn" query)))

(defun my-org-recoll-python (query)
  (interactive "sSearch python: ")
  (org-recoll-search (format "%s dir:python-3.9.7-docs-html" query)))

(defun my-org-recoll-php (query)
  (interactive "sSearch php: ")
  (org-recoll-search (format "%s dir:php-chunked-xhtml" query)))

(defun my-org-recoll-yesod (query)
  (interactive "sSearch yesod: ")
  (org-recoll-search (format "%s dir:yesod-cookbook OR dir:yesodweb.com" query)))

(defun my-org-entry-at-point-to-tsv (id)
  (string-join
   (cons (number-to-string id)
         (mapcar
          (lambda (key) (org-entry-get (point) key))
          (list "ITEM" "Referral" "Wikipedia-link" "IMDB-link")))
   "\t"))

(defun my-org-swap-referral-with-headline ()
  "Swap Referral property with headline of org node at point.

Remove the Referral property if empty."
  (interactive)
  (let ((ref (or (org-entry-get (point) "Referral") ""))
        (headline (org-entry-get (point) "ITEM")))
    (if (string-empty-p headline)
        (org-delete-property "Referral")
      (org-entry-put (point) "Referral" headline))
    (org-edit-headline ref)))

(defun my-org-clean-up-entry ()
  "Clean up an org entry.

Delete empty standard properties.
Sync attachment.
Remove trailing whitespaces.
Flush lines with only some common symbols."
  (interactive)
  (my-org-delete-empty-properties)
  (org-attach-sync)
  (save-excursion
    (save-restriction
      (org-narrow-to-subtree)
      (flush-lines "^\\ *[-]+\\ *$")
      (delete-trailing-whitespace)
      (goto-char (point-max))
      (when (<= (skip-chars-backward "\n") -2)
        (delete-region (1+ (point)) (point-max))))))

(defun my-org-delete-empty-properties ()
  "Delete empty (standard) properties at point."
  (interactive)
  (pcase-dolist (`(,name . ,value) (org-entry-properties (point) 'standard))
    (when (string-empty-p value) (org-entry-delete (point) name))))

(defvar org-entries-tsv-buffer "*org-entries-tsv*")
(defun my-org-entries-at-point-to-tsv (beg end)
  (interactive "r")
  (with-current-buffer (get-buffer-create org-entries-tsv-buffer)
    (erase-buffer))
  (let ((row) (id 0))
    (save-excursion
      (goto-char beg)
      (while (< (point) end)
        (when (equal (org-entry-get (point) "TODO") "TODO")
          (setq row (my-org-entry-at-point-to-tsv id))
          (with-current-buffer org-entries-tsv-buffer
            (insert row "\n"))
          (setq id (1+ id)))
        (org-next-visible-heading 1))))
  (switch-to-buffer-other-window org-entries-tsv-buffer))

(defun my-org-entries-attach-plaintext-all-media (beg end)
  (interactive "r")
  (save-excursion
    (goto-char beg)
    (while (< (point) end)
      (my-org-attach-url-plaintext-all-media (car (my-org-http-s-links-in-entry)))
      (org-next-visible-heading 1))))

(defvar my-org-doc-dir nil "Directory to docs written in org.")
(defun my-org-open-org-doc (filename)
  (interactive
   (list
    (completing-read
     "Open org doc: "
     (mapcar (lambda (name) (substring name (length my-org-doc-dir)))
             (directory-files-recursively my-org-doc-dir "\\.org$")))))
  (find-file (concat my-org-doc-dir filename)))

(defun my-org-open-org-file (filename)
  (interactive
   (list
    (completing-read
     "Open org file: "
     (directory-files org-directory nil "\\.org$"))))
  (find-file (file-name-concat org-directory filename)))

(defun my-org-agenda-after-show () (beginning-of-line 1))

(defun my-org-agenda-ensure-A ()
  (org-agenda nil "A")
  (unless (get-text-property (point) 'org-series-redo-cmd)
    (kill-buffer)
    (org-agenda nil "A")))

(defun my-org-agenda-redo-all ()
  (interactive)
  (message "time now is %s"
           (format-time-string "%Y-%m-%d %a %H:%M:%S" (current-time)))
  (my-org-agenda-ensure-A)
  (dolist (buffer (buffer-list))
    (with-current-buffer buffer
      (when (derived-mode-p 'org-agenda-mode)
        (print buffer)
        (org-agenda-redo t)))))

(defun my-org-copy-dwim ()
  (interactive)
  (cond ((org-in-src-block-p)
	       (my-org-copy-src-block-at-point))
        ((my-org-in-or-at-block-p) (my-org-copy-block-at-point))
        (t (org-refile-copy))))

;; to override `org--mouse-open-at-point' - we don't want
;; `org-open-at-point' to toggle a checkbox when point is at the
;; beginning of a link
(defun my-org--mouse-open-at-point (orig-fun &rest args)
  (let ((context (org-context)))
    (cond
     ((assq :headline-stars context) (org-cycle))
     ((assq :item-bullet context)
      (let ((org-cycle-include-plain-lists t)) (org-cycle)))
     ((org-footnote-at-reference-p) nil)
     (t (apply orig-fun args)))))

(defun my-org-vote-up (&optional points)
  (interactive "p")
  (org-entry-put
   (point)
   "Vote"
   (number-to-string
    (+ (or points 1)
       (string-to-number
        (or (org-entry-get (point) "Vote") "0"))))))

(defun my-org-vote-down (&optional points)
  (interactive "p")
  (my-org-vote-up (- points)))

;; used to add an :after advice to `org-insert-structure-template'.
(require 'my-buffer)
(defun my-org-edit-special (type)
  "Choose mode if src block. Then enter edit special

With a prefix arg, yank and exit immediately."
  (when (equal type "src")
    (insert (string-remove-suffix
             "-mode" (prin1-to-string (my-read-major-mode)))))
  (let ((mark-was-active mark-active))
    (org-edit-special)
    (when current-prefix-arg
      (unless mark-was-active
        (yank))
      (org-edit-src-exit))))

(defun my-link-to-line-number-in-prog-mode ()
  "When in prog-mode, use line number as search item."
  (when (derived-mode-p 'prog-mode)
    (number-to-string (org-current-line))))

;; override `org-capture-set-plist'
(defun my-org-capture-set-plist (entry)
  "Initialize the property list for ENTRY from the template definition."
  (setq org-capture-plist (copy-sequence (nthcdr 5 entry)))
  (org-capture-put :key (car entry) :description (nth 1 entry)
		               :target (nth 3 entry))
  (let ((txt (nth 4 entry)) (type (or (nth 2 entry) 'entry)))
    (when (or (not txt) (and (stringp txt) (not (string-match "\\S-" txt))))
      ;; The template may be empty or omitted for special types.
      ;; Here we insert the default templates for such cases.
      (cond
       ((eq type 'item) (setq txt "- %?"))
       ((eq type 'checkitem) (setq txt "- [ ] %?"))
       ((eq type 'table-line) (setq txt "| %? |"))
       ;; >>> the overriding difference
       ((member type '(nil entry)) (setq txt "* %?"))))
    (org-capture-put :template txt :type type)))

(defun my-org-entry-sizes ()
  "Size analysis of all entries in the current org buffer.

A new org buffer appears, with the whole org tree of the original
buffer preserved, annotated with the size"
  (with-current-buffer (get-buffer-create "*org-sizes*")
    (erase-buffer)
    (org-mode))
  (save-excursion
    (save-restriction
      (org-content)
      (beginning-of-buffer)
      (unless (org-at-heading-p)
        (org-next-visible-heading 1))
      (let ((beg) (line))
        (while (org-at-heading-p)
          (setq beg (point)
                line (buffer-substring-no-properties
                      beg (save-excursion (org-end-of-line) (point))))
          (org-next-visible-heading 1)
          (setq line (format "%s (%d)" line (- (point) beg)))
          (with-current-buffer "*org-sizes*"
            (insert line "\n"))))))
  (switch-to-buffer-other-window "*org-sizes*"))

(defun my-org-capture-todo ()
  "Capture todo template."
  (interactive)
  (org-capture nil "t")
  (org-capture-finalize)
  (message "todo captured."))

;; for `org-link-frame-setup'
(defun my-org-gnus-other-window-advice ()
  (other-window 1))


;; override `org-id-store-link' with a priority property
(defun my-org-id-store-link ()
  "Store a link to the current entry, using its ID.

If before first heading store first title-keyword as description
or filename if no title."
  (interactive)
  (when (and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
    (let* ((link (concat "id:" (org-id-get-create)))
	         (case-fold-search nil)
	         (desc (save-excursion
		               (org-back-to-heading-or-point-min t)
                   (cond ((org-before-first-heading-p)
                          (let ((keywords (org-collect-keywords '("TITLE"))))
                            (if keywords
                                (cadr (assoc "TITLE" keywords))
                              (file-name-nondirectory
			                         (buffer-file-name (buffer-base-buffer))))))
		                     ((looking-at org-complex-heading-regexp)
			                    (if (match-end 4)
			                        (match-string 4)
			                      (match-string 0)))
                         (t link))))
           (prio (org-entry-get (point) "PRIORITY")))
      (org-link-store-props :link link :description desc :type "id"
                            :priority prio)
      link)))

(provide 'my-org)
;;; my-org.el ends here