-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorgrr.el
1906 lines (1794 loc) · 88.5 KB
/
orgrr.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
;;; orgrr.el --- A fast and feature-complete Zettelkasten -*- lexical-binding: t; -*-
;; Maintainer: René Trappel <[email protected]>
;; URL: https://github.com/rtrppl/orgrr
;; Version: 1.0.3
;; Package-Requires: ((emacs "27.2"))
;; Keywords: comm wp outlines
;; This file is not part of GNU Emacs.
;; This file 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, or (at your option)
;; any later version.
;; This file 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.
;; For a full copy of the GNU General Public License
;; see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Orgrr is a minimalist but complete note-taking system for Emacs. Its
;; intended purpose is the creation and management of a Zettelkasten-like
;; system, e.g. many small notes that can easily be linked together.
;;
;;
;;
;;; News
;;
;; 1.0.3
;; - Better handling of existing headings of notes by
;; `orgrr-compile-sequence' (=increasing them by one level)
;;
;; 1.0.2
;; - Improved handling of `orgrr-update-cache'
;;
;; 1.0.1
;; - Fixes some issues with special characters in filenames
;;
;; 1.0
;; - Caching is now a thing in orgrr; massive speed gains
;;
;; 0.9.19
;; - Improved spacing for lists in `orgrr-show-sequence' and
;; `orgrr-show-multiverse'
;;
;; 0.9.18
;; - Added `orgrr-add-to-other-window'
;;
;; 0.9.17
;; - Added option to use active region for `orgrr-add-to-project'
;;
;; 0.9.16
;; - Added convience: typing "q" in any results buffer now closes that buffer
;; (via a minor-mode)
;;
;; 0.9.15
;; - Added `orgrr-show-multiverse'; modified `orgrr-show-sequence' to
;; also show parent zettel
;;
;;
;; For more changes see the changelog.
;;
;;; Code:
(require 'ucs-normalize)
(require 'org)
(require 'json)
(defvar orgrr-window-management "single-window")
(defvar orgrr-quick-add-token "quicknote")
(defvar orgrr-compile-open-link-other-window t) ;; set this to nil if orgrr-compile-draft should respect orgrr-window-management settings
(defvar orgrr-use-caching nil)
;; The following list of hashtables create the data structure in which orgrr stores metadata on notes.
(defvar orgrr-title-filename (make-hash-table :test 'equal) "Hashtable with the key title and the value filename.")
(defvar orgrr-filename-title (make-hash-table :test 'equal) "Hashtable with the the key filename and the value title.")
(defvar orgrr-filename-tags (make-hash-table :test 'equal) "Hashtable with the key filename and the value tags.")
(defvar orgrr-short_filename-filename (make-hash-table :test 'equal) "Hashtable with the key filename without path and the value filename+path.")
(defvar orgrr-zettel-filename (make-hash-table :test 'equal) "Hashtable with the key zettel and the value filename.")
(defvar orgrr-filename-zettel (make-hash-table :test 'equal) "Hashtable with the key filename and the value zettel.")
(defvar orgrr-zettelrank-zettel (make-hash-table :test 'equal) "Hashtable with the key rank of a zettel and the value zettel.") ;; rank means how a zettel value of a note relates to other notes
(defvar orgrr-zettel-zettelrank (make-hash-table :test 'equal) "Hashtable with the key zettel and the value rank of a zettel.")
(defvar orgrr-short_filename-filename (make-hash-table :test 'equal) "Hashtable containing all org-files accross all containers.")
(defvar orgrr-short_filename-title (make-hash-table :test 'equal) "Hashtable containing filenames-titles for all org-files accross all containers.")
(defvar orgrr-title-short_filename (make-hash-table :test 'equal) "Hashtable containing titles-filenames for all org-files accross all containers.")
(defvar orgrr-filename-mentions (make-hash-table :test 'equal) "Hashtable necessary for `orgrr-show-related-notes'.")
(define-minor-mode orgrr-results-buffer-mode
"A minor mode for orgrr results buffers."
:lighter " orgrr-results-buffer"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "q") 'orgrr-close-buffer)
map))
(defun orgrr-open-file (filename)
"A wrapper to open FILENAME either with `find-file' or `find-file-other-window'."
(if (equal orgrr-window-management "multi-window")
(find-file-other-window filename)
(if (equal orgrr-window-management "single-window")
(find-file filename)
(find-file-other-window filename))))
(defun orgrr-open-buffer (buffer)
"A wrapper to open BUFFER according to `orgrr-window-management' settings."
(when (equal orgrr-window-management "multi-window")
(display-buffer-in-side-window
(current-buffer)
'((side . right)
(slot . -1)
(window-width . 60))))
(when (equal orgrr-window-management "single-window")
(switch-to-buffer buffer)))
(defun orgrr-prepare-findings-buffer (buffer)
"Preparing the orgrr findings buffer."
(with-current-buffer buffer
(org-mode))
(let ((window (get-buffer-window buffer)))
(select-window window)
(goto-char (point-min))
(org-next-visible-heading 2)
(orgrr-results-buffer-mode t)
(deactivate-mark)))
(defun orgrr-close-buffer ()
"A wrapper to close BUFFER according to `orgrr-window-management' settings."
(interactive)
(when (equal orgrr-window-management "multi-window")
(kill-buffer))
(when (equal orgrr-window-management "single-window")
(kill-buffer)))
(defun orgrr-toggle-window-mode ()
"Switch between single-window-mode and multi-window mode (which uses
side-buffers)."
(interactive)
(if (equal orgrr-window-management "multi-window")
(progn
(setq orgrr-window-management "single-window")
(setq org-link-frame-setup '((file . find-file))))
(progn
(setq orgrr-window-management "multi-window")
(setq org-link-frame-setup '((file . find-file-other-window))))))
(defun orgrr-on-macos-p ()
"Check if Emacs is running on macOS. This became necessary due to some
normalization issues with filenames that contain non-ascii characters and
require NCD-formating."
(eq system-type 'darwin))
(defun orgrr-show-backlinks (arg)
"Show all backlinks in `org-directory' to the current org-file."
(interactive "P")
(let ((call-with-arg nil))
(when (equal arg '(4))
(setq call-with-arg 1))
(if (not (string-match-p "backlinks for *" (buffer-name (current-buffer))))
(progn
(orgrr-get-all-meta)
(let* ((filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name)))
(title (pcase (org-collect-keywords '("TITLE"))
(`(("TITLE" . ,val)) (car val))))
(backlink-buffer (concat "backlinks for *" title "*"))
(backlinks 0)
(orgrr-counter-quote (make-hash-table :test 'equal))
(orgrr-counter-filename (make-hash-table :test 'equal))
(orgrr-name-container (orgrr-get-list-of-containers))
(containers (nreverse (hash-table-values orgrr-name-container))))
;; get all backlinks first order
(with-temp-buffer
(when (not call-with-arg)
(setq containers ())
(setq containers (cons org-directory containers)))
(dolist (container containers)
(erase-buffer)
(if (orgrr-on-macos-p)
(insert (shell-command-to-string (concat "rg -F \"" (ucs-normalize-HFS-NFD-string (file-name-nondirectory filename)) "\" \"" (expand-file-name container) "\" -n --sort accessed -g \"*.org\"")))
(insert (shell-command-to-string (concat "rg -F \"" (file-name-nondirectory filename) "\" \"" (expand-file-name container) "\" -n --sort accessed -g \"*.org\""))))
(let ((lines (split-string (buffer-string) "\n" t)))
(dolist (line lines)
(when (string-match "^\\(.*?\\):\\(.*\\)$" line)
(setq backlinks (+ backlinks 1))
(puthash backlinks (match-string 1 line) orgrr-counter-filename)
(puthash backlinks (match-string 2 line) orgrr-counter-quote))))))
;; match-string 2 includes the line number!
(with-current-buffer (get-buffer-create backlink-buffer)
(erase-buffer)
(orgrr-open-buffer backlink-buffer)
(insert (concat "\*\[\[file:" filename "\]\[" title "\]\]\*\n\n"))
(if (= backlinks 1)
(insert "* 1 backlink\n\n")
(insert (concat "* " (number-to-string backlinks) " backlinks\n\n")))
;; Going through the backlinks
(dolist (counter (hash-table-keys orgrr-counter-filename))
(let ((entry (gethash counter orgrr-counter-filename)))
(when (and (stringp entry)
(not (string= entry filename)))
(let ((key entry)
(value (gethash counter orgrr-counter-quote)))
(when (stringp value)
(let* ((short_filename (file-name-nondirectory key))
(full-filename key)
(result (gethash (concat "\\" short_filename) orgrr-short_filename-title)))
(string-match "^\\(.*?\\):\\(.*\\)$" value)
(let* ((line-number (match-string 1 value))
(snippet (match-string 2 value))
(snippet (orgrr-adjust-links snippet))
(snippet (string-trim-left (string-trim-left snippet "*"))))
(insert (concat "\*\* \[\[file:" full-filename "::" line-number "\]" "\[" result "\]\]:\n\n" snippet "\n\n")))))))))
(orgrr-prepare-findings-buffer backlink-buffer))))
(orgrr-close-buffer))))
(defun orgrr-check-caching ()
"Helper function to adjust `after-save-hook'."
(when orgrr-use-caching
(when (not (member "(orgrr-update-cache)" after-save-hook))
(add-hook 'after-save-hook 'orgrr-update-cache)))
(when (not orgrr-use-caching)
(when (member "(orgrr-update-cache)" after-save-hook)
(remove-hook 'after-save-hook 'orgrr-update-cache))))
(defun orgrr-get-meta ()
"Gets the value for #+title, #+roam_alias, #+roam_tags and #+zettel for all
org-files and adds them to hashtables.
Updates the following hashtables: `orgrr-title-filename',
`orgrr-filename-title', `orgrr-zettel-filename', `orgrr-filename-zettel',
`orgrr-filename-tags'."
(orgrr-check-caching)
(when (not orgrr-use-caching)
(clrhash orgrr-filename-title)
(clrhash orgrr-title-filename)
(clrhash orgrr-zettel-filename)
(clrhash orgrr-filename-zettel)
(clrhash orgrr-filename-tags)
(with-temp-buffer
(insert (shell-command-to-string (concat "rg -i --sort modified \"^\\#\\+(title:.*|roam_alias.*|roam_tags.*|zettel:.*)\" \"" (expand-file-name org-directory) "\" -g \"*.org\"")))
(goto-char (point-min))
(while (not (eobp))
(let ((current-entry (buffer-substring (line-beginning-position) (line-end-position))))
;; The following checks if this is a #+title line and is so, adds the title + filename to orgrr-title-filename and filename + title to orgrr-filename-title.
(when (string-match "\\(#\\+title:\\|#+TITLE:\\)\\s-*\\(.+\\)" current-entry)
(let* ((line (split-string current-entry "\\(:#\\+title:\\|:#+TITLE:\\)\\s-*\\(.+\\)" t))
(filename (car line))
(line (split-string current-entry "^.+\\(#\\+title:\\|:#+TITLE:\\)\\s-*" t))
(title (car line)))
(puthash title filename orgrr-title-filename)
(puthash (concat "\\" filename) title orgrr-filename-title)))
;; The following checks if this is a #+roam_alias line and if so, adds all alias to orgrr-title-filename.
(when (string-match "\\(#\\+roam_alias:\\|#+ROAM_ALIAS:\\)\\s-*\\(.+\\)" current-entry)
(let* ((line (split-string current-entry "\\(: \\|:\\)" t))
(filename (car line)))
(with-temp-buffer
(insert current-entry)
(goto-char (point-min))
(while (re-search-forward "\"\\(.*?\\)\\\"" nil t)
(puthash (match-string 1) filename orgrr-title-filename)))))
;; The following checks if this is a #+zettel line and if so, adds the zettel-no to orgrr-zettel-filename.
(when (string-match "\\(#\\+zettel:\\|#+ZETTEL:\\)\\s-*\\(.+\\)" current-entry)
(let* ((line (split-string current-entry "\\(: \\|:\\)" t))
(filename (car line))
(zettel (car (cdr (cdr line)))))
(puthash zettel filename orgrr-zettel-filename)
(puthash (concat "\\" filename) zettel orgrr-filename-zettel)))
;; The following checks if the line contains tags and if so copies the tags to orgrr-tags-filename.
(when (string-match "\\(#\\+roam_tags:\\|#+ROAM_TAGS:\\)\\s-*\\(.+\\)" current-entry)
(let* ((line (split-string current-entry "\\(: \\|:\\)" t))
(filename (car line))
(tags (car (cdr (cdr line)))))
(puthash (concat "\\" filename) tags orgrr-filename-tags)))
(forward-line))))))
;;;###autoload
(defun orgrr-update-meta-cache ()
"This updates the cache of meta data for the current container. It is a
asynchronos functional replacement for `orgrr-get-meta'."
(clrhash orgrr-filename-title)
(clrhash orgrr-title-filename)
(clrhash orgrr-zettel-filename)
(clrhash orgrr-filename-zettel)
(clrhash orgrr-filename-tags)
(let ((update-buffer-name "*orgrr-update-meta*"))
(make-process
:name "orgrr-metadata-update"
:buffer update-buffer-name
:command (list "rg" "-i" "--sort" "modified"
"-e"
"^\\#\\+(title:.*|roam_alias.*|roam_tags.*|zettel:.*)"
(expand-file-name org-directory)
"-g" "*.org")
:connection-type 'pipe
:sentinel #'orgrr-process-meta-cache)))
;;;###autoload
(defun orgrr-update-all-meta-cache ()
"This updates the cache of ALL meta data for the current container. It is a
asynchronos functional replacement for `orgrr-get-all-meta'."
(clrhash orgrr-short_filename-filename)
(clrhash orgrr-short_filename-title)
(clrhash orgrr-title-short_filename)
(let* ((update-buffer-name "*orgrr-update-all-meta")
(orgrr-name-container (orgrr-get-list-of-containers))
(containers (nreverse (hash-table-values orgrr-name-container))))
(dolist (container containers)
(make-process
:name "orgrr-all-metadata-update"
:buffer (concat update-buffer-name "-" container "*")
:command (list "rg" "-i" "--sort" "modified"
"-e"
"^\\#\\+(title:.*|roam_alias.*)"
(expand-file-name container)
"-g" "*.org")
:connection-type 'pipe
:sentinel #'orgrr-process-all-meta-cache))))
;;;###autoload
(defun orgrr-update-cache ()
"Updates all metadata for orgrr."
(interactive)
(when (and (not (process-live-p (get-process "orgrr-all-metadata-update")))
(not (process-live-p (get-process "orgrr-metadata-update"))))
(orgrr-update-meta-cache)
(orgrr-update-all-meta-cache)))
;;;###autoload
(defun orgrr-process-meta-cache (_process event)
"Processes the data from `orgrr-update-meta-cache' and fills the proper
hashtables."
(let ((update-buffer-name "*orgrr-update-meta*"))
(when (string-equal event "finished\n")
(with-current-buffer update-buffer-name
(goto-char (point-min))
(while (not (eobp))
(let ((current-entry (buffer-substring (line-beginning-position) (line-end-position))))
;; The following checks if this is a #+title line and is so, adds the title + filename to orgrr-title-filename and filename + title to orgrr-filename-title.
(when (string-match "\\(#\\+title:\\|#+TITLE:\\)\\s-*\\(.+\\)" current-entry)
(let* ((line (split-string current-entry "\\(:#\\+title:\\|:#+TITLE:\\)\\s-*\\(.+\\)" t))
(filename (car line))
(line (split-string current-entry "^.+\\(#\\+title:\\|:#+TITLE:\\)\\s-*" t))
(title (car line)))
(puthash title filename orgrr-title-filename)
(puthash (concat "\\" filename) title orgrr-filename-title)))
;; The following checks if this is a #+roam_alias line and if so, adds all alias to orgrr-title-filename.
(when (string-match "\\(#\\+roam_alias:\\|#+ROAM_ALIAS:\\)\\s-*\\(.+\\)" current-entry)
(let* ((line (split-string current-entry "\\(: \\|:\\)" t))
(filename (car line)))
(with-temp-buffer
(insert current-entry)
(goto-char (point-min))
(while (re-search-forward "\"\\(.*?\\)\\\"" nil t)
(puthash (match-string 1) filename orgrr-title-filename)))))
;; The following checks if this is a #+zettel line and if so, adds the zettel-no to orgrr-zettel-filename.
(when (string-match "\\(#\\+zettel:\\|#+ZETTEL:\\)\\s-*\\(.+\\)" current-entry)
(let* ((line (split-string current-entry "\\(: \\|:\\)" t))
(filename (car line))
(zettel (car (cdr (cdr line)))))
(puthash zettel filename orgrr-zettel-filename)
(puthash (concat "\\" filename) zettel orgrr-filename-zettel)))
;; The following checks if the line contains tags and if so copies the tags to orgrr-tags-filename.
(when (string-match "\\(#\\+roam_tags:\\|#+ROAM_TAGS:\\)\\s-*\\(.+\\)" current-entry)
(let* ((line (split-string current-entry "\\(: \\|:\\)" t))
(filename (car line))
(tags (car (cdr (cdr line)))))
(puthash (concat "\\" filename) tags orgrr-filename-tags)))
(forward-line)))
(kill-buffer update-buffer-name)))))
;;;###autoload
(defun orgrr-process-all-meta-cache (process event)
"Processes the data from `orgrr-update-all-meta-cache' and fills the proper
hashtables."
(when (string-equal event "finished\n")
(with-current-buffer (process-buffer process)
(goto-char (point-min))
(while (not (eobp))
(let* ((current-entry (buffer-substring (line-beginning-position) (line-end-position))))
(when (string-match "\\(.*\\):#\\+\\(title\\|TITLE\\):\\s-*\\(.+\\)" current-entry)
(let* ((filename (match-string 1 current-entry))
(title (match-string 3 current-entry)))
(puthash (concat "\\" (file-name-nondirectory filename)) filename orgrr-short_filename-filename)
(puthash (concat "\\" (file-name-nondirectory filename)) title orgrr-short_filename-title)
(puthash title (file-name-nondirectory filename) orgrr-title-short_filename)))
(when (string-match "\\(#\\+roam_alias:\\|#+ROAM_ALIAS:\\)\\s-*\\(.+\\)" current-entry)
(let* ((line (split-string current-entry "\\(: \\|:\\)" t))
(filename (car line)))
(with-temp-buffer
(insert current-entry)
(goto-char (point-min))
(while (re-search-forward "\"\\(.*?\\)\\\"" nil t)
(puthash (match-string 1) (file-name-nondirectory filename) orgrr-title-short_filename)))))
(forward-line)))
(kill-buffer (process-buffer process)))
(message "orgrr caching...done.")))
;; orgrr-presorted-completion-table is based on
;; https://emacs.stackexchange.com/questions/8115/make-completing-read
;; -respect-sorting-order-of-a-collection, thanks @[email protected] for the hint!
;; lambda functions and let for whatever reason don't like each other,
;; hence this construction.
(defun orgrr-presorted-completion-table (orgrr-selection-list)
(let ((orgrr-selection-list-completion ()))
(setq orgrr-selection-list-completion
(lambda (string pred action)
(if (eq action 'metadata)
`(metadata (display-sort-function . ,#'identity))
(complete-with-action action orgrr-selection-list string pred))))
orgrr-selection-list-completion))
(defun orgrr-selection ()
"Prepare the symbol orgrr-selection for completing-read and send the result
in selection to orgrr-find and orgrr-insert. Prepends tags and zettel in front
of title and alias."
(orgrr-get-meta)
(let* ((orgrr-selection-list ())
(orgrr-selection-list-completion)
(final-title)
(selection)
(titles (hash-table-keys orgrr-title-filename))
(filenames-for-tags (hash-table-keys orgrr-filename-tags))
(filenames-for-zettel (hash-table-keys orgrr-filename-zettel)))
(dolist (title titles)
(let* ((filename (gethash title orgrr-title-filename)))
(if (member (concat "\\" filename) filenames-for-tags)
(if (member (concat "\\" filename) filenames-for-zettel)
(setq final-title (concat "[" (gethash (concat "\\" filename) orgrr-filename-zettel) "]" " (" (gethash (concat "\\" filename) orgrr-filename-tags) ") " title))
(setq final-title (concat "(" (gethash (concat "\\" filename) orgrr-filename-tags) ") "title)))
(if (member (concat "\\" filename) filenames-for-zettel)
(setq final-title (concat "[" (gethash (concat "\\" filename) orgrr-filename-zettel) "] " title))
(setq final-title title)))
(setq orgrr-selection-list (cons final-title orgrr-selection-list))))
(setq orgrr-selection-list (reverse orgrr-selection-list))
(setq orgrr-selection-list-completion (orgrr-presorted-completion-table orgrr-selection-list))
(if (region-active-p)
(setq selection (completing-read "Select: " orgrr-selection-list-completion nil nil (buffer-substring-no-properties (region-beginning)(region-end))))
(setq selection (completing-read "Select: " orgrr-selection-list-completion)))
(if (string-match "^\\[" selection)
(setq selection (replace-regexp-in-string "\\[.*?\\]\\s-*" "" selection)))
(if (string-match "^\(" selection)
(setq selection (replace-regexp-in-string "\(.*?\)\\s-*" "" selection)))
selection)) ;; this line ensures that the value of selection is returned when this function is called
(defun orgrr-global-selection ()
"Use data from all containers to prepare the symbol orgrr-selection for
completing-read and send the result in selection to orgrr-find and
orgrr-insert.
Does not prepend tags and zettel in front of title and alias."
(orgrr-get-all-meta)
(let* ((orgrr-selection-list-completion)
(selection)
(orgrr-selection-list (hash-table-keys orgrr-title-short_filename)))
(setq orgrr-selection-list-completion (orgrr-presorted-completion-table orgrr-selection-list))
(if (region-active-p)
(setq selection (completing-read "Select: " orgrr-selection-list-completion nil nil (buffer-substring-no-properties (region-beginning)(region-end))))
(setq selection (completing-read "Select: " orgrr-selection-list-completion)))
selection)) ;; this line ensures that the value of selection is returned when this function is called
(defun orgrr-selection-zettel (&optional current-zettel)
"Prepare the symbol orgrr-selection for completing-read and send the result
in selection to `orgrr-find-zettel' and `orgrr-insert-zettel'. Only includes
files that have a value for zettel. Prepends zettel value in front of title and
alias."
(let* ((orgrr-selection-list (orgrr-prepare-zettel-selection-list))
(orgrr-selection-list-completion (orgrr-presorted-completion-table orgrr-selection-list))
(selection))
(if current-zettel
(setq selection (completing-read "Select: " orgrr-selection-list-completion nil nil current-zettel))
(setq selection (completing-read "Select: " orgrr-selection-list-completion)))
(if (string-match "^\\[\\(.*?\\)\\]" selection)
(setq selection (replace-regexp-in-string "\\[.*?\\]\\s-*" "" selection)))
selection))
(defun orgrr-prepare-zettel-selection-list ()
"A function preparing a list of all zettel for selection and other functions."
(interactive)
(orgrr-get-meta)
(let* ((orgrr-selection-list ())
(titles (hash-table-keys orgrr-title-filename))
(filenames-for-zettel (hash-table-keys orgrr-filename-zettel))
(final-title))
(dolist (title titles)
(let* ((filename (gethash title orgrr-title-filename)))
(if (member (concat "\\" filename) filenames-for-zettel)
(progn
(setq final-title (concat "[" (gethash (concat "\\" filename) orgrr-filename-zettel) "]\s" title))
(setq orgrr-selection-list (cons final-title orgrr-selection-list))))))
(setq orgrr-selection-list (reverse orgrr-selection-list))))
(defun orgrr-find-zettel ()
"Like `orgrr-find', but only considers notes that have a value for zettel. If
the selected file name does not exist, a new one is created. Starts with the
current zettel ID, which allows you to search within a context."
(interactive)
(let ((selection (orgrr-selection-zettel)))
(when (member selection (hash-table-keys orgrr-title-filename))
(let ((filename (gethash selection orgrr-title-filename)))
(orgrr-open-file filename)))
(when (not (member selection (hash-table-keys orgrr-title-filename)))
(let* ((time (format-time-string "%Y%m%d%H%M%S"))
(filename (concat (file-name-as-directory org-directory) time "-" (replace-regexp-in-string "[\"'?,:;\\\s\/]" "_" selection))))
(orgrr-open-file (concat filename ".org"))
(insert (concat "#+title: " selection "\n"))))))
(defun orgrr-no-selection-zettel ()
"Prepare the symbol orgrr-selection for completing-read and send the result
in selection to orgrr-find and orgrr-insert. Excludes zettel. "
(interactive)
(orgrr-get-meta)
(let* ((orgrr-selection-list ())
(orgrr-selection-list-completion)
(final-title)
(selection)
(titles (hash-table-keys orgrr-title-filename))
(filenames-for-tags (hash-table-keys orgrr-filename-tags))
(filenames-for-zettel (hash-table-keys orgrr-filename-zettel)))
(dolist (title titles)
(let* ((filename (gethash title orgrr-title-filename)))
(if (member (concat "\\" filename) filenames-for-tags)
(if (not (member (concat "\\" filename) filenames-for-zettel))
(progn
(setq final-title (concat "(" (gethash (concat "\\" filename) orgrr-filename-tags) ") " title))
(setq orgrr-selection-list (cons final-title orgrr-selection-list)))))
(if (not (member (concat "\\" filename) filenames-for-tags))
(if (not (member (concat "\\" filename) filenames-for-zettel))
(progn
(setq final-title title)
(setq orgrr-selection-list (cons final-title orgrr-selection-list)))))))
(setq orgrr-selection-list (reverse orgrr-selection-list))
(setq orgrr-selection-list-completion (orgrr-presorted-completion-table orgrr-selection-list))
(if (region-active-p)
(setq selection (completing-read "Select: " orgrr-selection-list-completion nil nil (buffer-substring-no-properties (region-beginning)(region-end))))
(setq selection (completing-read "Select: " orgrr-selection-list-completion)))
(if (string-match "^\(" selection)
(setq selection (replace-regexp-in-string "\(.*?\)\\s-*" "" selection)))
selection))
(defun orgrr-no-find-zettel ()
"Like orgrr-find-zettel, but only considers notes that have no value for
zettel."
(interactive)
(let ((selection (orgrr-no-selection-zettel))
(filename))
(when (member selection (hash-table-keys orgrr-title-filename))
(setq filename (gethash selection orgrr-title-filename))
(orgrr-open-file filename))))
(defun orgrr-add-zettel ()
"Drill down a list of notes with values for zettel to allow the user to find
the correct spot for a new zettel and then insert a line with #+zettel:
zettel-value after the last line starting with #+ (from the beginning of the
file)."
(interactive)
(orgrr-get-meta)
(let ((current-zettel (orgrr-read-current-zettel)))
(if (not current-zettel)
(progn
(let ((orgrr-selection-list ())
(orgrr-selection-list-completion)
(selection-zettel)
(inserted-already)
(final-title)
(titles (hash-table-keys orgrr-title-filename))
(filenames-for-zettel (hash-table-keys orgrr-filename-zettel))
(saved-point (point-marker)))
(dolist (title titles)
(let* ((filename (gethash title orgrr-title-filename)))
(when (member (concat "\\" filename) filenames-for-zettel)
(setq final-title (concat "[" (gethash (concat "\\" filename) orgrr-filename-zettel) "]\s" title))
(setq orgrr-selection-list (cons final-title orgrr-selection-list)))))
(setq orgrr-selection-list (reverse orgrr-selection-list))
(setq orgrr-selection-list-completion (orgrr-presorted-completion-table orgrr-selection-list))
(setq selection-zettel (completing-read "Hit enter to narrow down: " orgrr-selection-list-completion))
(if (string-match "^\\[\\(.*?\\)\\]" selection-zettel)
(setq selection-zettel (match-string 1 selection-zettel)))
(while (member selection-zettel (hash-table-values orgrr-filename-zettel))
(setq selection-zettel (completing-read "Hit enter to narrow down: " orgrr-selection-list-completion nil nil selection-zettel))
(when (string-match "^\\[\\(.*?\\)\\]" selection-zettel)
(setq selection-zettel (match-string 1 selection-zettel))))
(goto-char (point-min))
(setq inserted-already nil)
(while (not inserted-already)
(let ((current-entry (buffer-substring (line-beginning-position) (line-end-position))))
(if (string-prefix-p "#+" current-entry)
(forward-line)
(progn
(setq inserted-already t)
(insert (concat "#+zettel: " selection-zettel "\n"))
(goto-char saved-point)
(save-buffer)))))))
(message "This note already has a value for zettel!"))))
(defun orgrr-read-current-zettel ()
"Returns #+zettel for current note."
(let ((current-zettel nil))
(when (eq major-mode 'org-mode)
(let ((current-entry "")
(buffer (buffer-substring-no-properties (point-min) (point-max))))
(with-temp-buffer
(insert buffer)
(goto-char (point-min))
(while (not (eobp))
(setq current-entry (buffer-substring (line-beginning-position) (line-end-position)))
(if (string-match "\\(#\\+zettel:\\|#+ZETTEL:\\)\\s-*\\(.+\\)" current-entry)
(progn
(let* ((line (split-string current-entry "\\: " t))
(zettel (car (cdr line)))
(zettel (string-trim-left zettel)))
(setq current-zettel zettel))))
(forward-line)))))
current-zettel))
(defun orgrr-show-sequence (&optional zettel-title)
"Shows a sequence of notes for any given zettel value. If run while visiting
a buffer that has a value for zettel, this is taken as the starting value for
zettel. Results are presented in a different buffer in accordance with the
variable orgrr-window-management."
(interactive)
(when (not (string-match-p "sequence for *" (buffer-name (current-buffer))))
(orgrr-prepare-zettelrank)
(let* ((current-zettel (orgrr-read-current-zettel))
(zettel-title (or zettel-title (orgrr-selection-zettel current-zettel)))
(zettel-filename (gethash zettel-title orgrr-title-filename))
(selection-zettel (gethash (concat "\\" zettel-filename) orgrr-filename-zettel))
(parent-zettel (orgrr-read-zettel-parent selection-zettel))
(max-zettel-id-value 0)
(orgrr-zettel-list (hash-table-values orgrr-filename-zettel))
(orgrr-zettel-list (sort orgrr-zettel-list 'orgrr-dictionary-lessp))
(sequence-buffer (concat "sequence for *[" selection-zettel "]*")))
(with-current-buffer (get-buffer-create sequence-buffer)
(let ((inhibit-read-only t))
(erase-buffer)
(insert (concat (orgrr-return-fullzettel-linked-head selection-zettel) "\n\n"))
(insert "* sequence:\n\n")
(if (not (string-equal parent-zettel ""))
(insert (concat "** parent zettel: \t" (orgrr-return-fullzettel-linked parent-zettel) "\n\n"))
(insert "** This is a root zettel with no parent.\n\n"))
;; the following do list should only get the maxium length of any
;; zettel id used. This is necessary for adding the correct amount
;; of spacing in the list.
(dolist (element orgrr-zettel-list)
(let* ((last-char (substring selection-zettel -1))
(is-last-char-num (string-match-p "[0-9]" last-char))
(regex (if is-last-char-num
(concat "^" selection-zettel "[a-zA-Z]")
(concat "^" selection-zettel))))
(when (string-match regex element)
(when (not (equal element selection-zettel))
(when (> (length element) max-zettel-id-value)
(setq max-zettel-id-value (length element)))))))
(dolist (element orgrr-zettel-list)
(let* ((last-char (substring selection-zettel -1))
(is-last-char-num (string-match-p "[0-9]" last-char))
(regex (if is-last-char-num
(concat "^" selection-zettel "[a-zA-Z]")
(concat "^" selection-zettel))))
(when (string-match regex element)
(if (not (equal element selection-zettel))
(insert (concat "** " (orgrr-return-fullzettel-linked element max-zettel-id-value) "\n")))))))
;;Starting here it is only window-management
(orgrr-open-buffer sequence-buffer)
(orgrr-prepare-findings-buffer sequence-buffer))))
(when (string-match-p "sequence for *" (buffer-name (current-buffer)))
(orgrr-close-buffer)))
;; The following three functions have been taken from
;; https://stackoverflow.com/questions/1942045/natural-order-sort-for-emacs-lisp
;; They work really well for dictionary compare.
(defun orgrr-dictionary-lessp (str1 str2)
"Return t if STR1 is < STR2 when doing a dictionary compare
(splitting the string at numbers and doing numeric compare with them)"
(let ((str1-components (orgrr-dict-split str1))
(str2-components (orgrr-dict-split str2)))
(orgrr-dict-lessp str1-components str2-components)))
(defun orgrr-dict-lessp (slist1 slist2)
"Compare the two lists of strings & numbers"
(cond ((null slist1)
(not (null slist2)))
((null slist2)
nil)
((and (numberp (car slist1))
(stringp (car slist2)))
t)
((and (numberp (car slist2))
(stringp (car slist1)))
nil)
((and (numberp (car slist1))
(numberp (car slist2)))
(or (< (car slist1) (car slist2))
(and (= (car slist1) (car slist2))
(orgrr-dict-lessp (cdr slist1) (cdr slist2)))))
(t
(or (string-lessp (car slist1) (car slist2))
(and (string-equal (car slist1) (car slist2))
(orgrr-dict-lessp (cdr slist1) (cdr slist2)))))))
(defun orgrr-dict-split (str)
"split a string into a list of number and non-number components"
(save-match-data
(let ((res nil))
(while (and str (not (string-equal "" str)))
(let ((p (string-match "[0-9]*\\.?[0-9]+" str)))
(cond ((null p)
(setq res (cons str res))
(setq str nil))
((= p 0)
(setq res (cons (string-to-number (match-string 0 str)) res))
(setq str (substring str (match-end 0))))
(t
(setq res (cons (substring str 0 (match-beginning 0)) res))
(setq str (substring str (match-beginning 0)))))))
(reverse res))))
(defun orgrr-open-next-zettel ()
"Opens the next zettel."
(interactive)
(let ((current-zettel (orgrr-read-current-zettel)))
(when current-zettel
(orgrr-prepare-zettelrank)
(let* ((current-zettel-rank (gethash current-zettel orgrr-zettel-zettelrank))
(next-rank (+ (string-to-number current-zettel-rank) 1))
(matched-zettel (gethash (number-to-string next-rank) orgrr-zettelrank-zettel))
(matched-zettel-filename (gethash matched-zettel orgrr-zettel-filename)))
(orgrr-open-file matched-zettel-filename)))
(when (not current-zettel)
(message "This note has no value for zettel, so there is no next zettel!"))))
(defun orgrr-prepare-zettelrank ()
"Prepares a hashtable that contains the rank of all zettel."
(orgrr-get-meta)
(let* ((zettelrank 0)
(orgrr-zettel-list (hash-table-values orgrr-filename-zettel))
(orgrr-zettel-list (sort orgrr-zettel-list 'orgrr-dictionary-lessp)))
(clrhash orgrr-zettelrank-zettel)
(clrhash orgrr-zettel-zettelrank)
(dolist (zettel orgrr-zettel-list)
(setq zettelrank (+ zettelrank 1))
(puthash (number-to-string zettelrank) zettel orgrr-zettelrank-zettel)
(puthash zettel (number-to-string zettelrank) orgrr-zettel-zettelrank))))
(defun orgrr-return-fullzettel (zettel)
"Returns the full name of a zettel (as in orgrr-zettel-list)."
(let* ((matched-zettel-filename (gethash zettel orgrr-zettel-filename))
(matched-zettel-title (gethash (concat "\\" matched-zettel-filename) orgrr-filename-title)))
(setq zettel (concat "\[" zettel "\]\t" matched-zettel-title))))
(defun orgrr-return-fullzettel-linked (zettel &optional max-zettel-id-value)
"Returns the full name of a zettel (as in orgrr-zettel-list) and links the
title to the note."
(let* ((max-zettel-id-value (or max-zettel-id-value 0))
(spacing 0)
(matched-zettel-filename (gethash zettel orgrr-zettel-filename))
(matched-zettel-title (gethash (concat "\\" matched-zettel-filename) orgrr-filename-title)))
(when (>= max-zettel-id-value 1)
(setq max-zettel-id-value (+ 3 max-zettel-id-value))
(setq spacing (- max-zettel-id-value (length zettel)))
(setq zettel (concat "\[" zettel "\]" (make-string spacing ?\s) "\[\[file:" matched-zettel-filename "\]\[" matched-zettel-title "\]\]")))
(when (= max-zettel-id-value 0)
(setq zettel (concat "\[" zettel "\]\t\[\[file:" matched-zettel-filename "\]\[" matched-zettel-title "\]\]")))
zettel))
(defun orgrr-return-fullzettel-linked-starred (zettel)
"Returns the full name of a zettel (as in orgrr-zettel-list) and links the
title to the note. Adds stars for org-bolding."
(let* ((matched-zettel-filename (gethash zettel orgrr-zettel-filename))
(matched-zettel-title (gethash (concat "\\" matched-zettel-filename) orgrr-filename-title)))
(setq zettel (concat "*" zettel "* \[\[file:" matched-zettel-filename "\]\[" matched-zettel-title "\]\]"))))
(defun orgrr-return-fullnote-linked-starred (title)
"Returns the full name of a zettel (as in orgrr-zettel-list) and links the
title to the note. Adds stars for org-bolding."
(let* ((filename (gethash title orgrr-title-filename)))
(setq title (concat "*\[\[file:" filename "\]\[" title "\]\]*"))))
(defun orgrr-return-zettel-linked (zettel)
"Returns the zettel as an org-link."
(let* ((matched-zettel-filename (gethash zettel orgrr-zettel-filename)))
(setq zettel (concat "\[\[file:" matched-zettel-filename "\]\[" zettel "\]\]"))))
(defun orgrr-return-fullzettel-linked-head (zettel)
"A version of orgrr-return-fullzettel-linked in a special format."
(let* ((matched-zettel-filename (gethash zettel orgrr-zettel-filename))
(matched-zettel-title (gethash (concat "\\" matched-zettel-filename) orgrr-filename-title)))
(setq zettel (concat "*\[" zettel "\]*\t\[\[file:" matched-zettel-filename "\]\[" matched-zettel-title "\]\]"))))
(defun orgrr-return-fullzettel-content (zettel)
"Returns the full content of a zettel without meta-data."
(let* ((matched-zettel-filename (gethash zettel orgrr-zettel-filename)))
(with-temp-buffer
(insert-file-contents matched-zettel-filename)
(goto-char (point-min))
(while (not (eobp))
(let ((current-entry (thing-at-point 'line t)))
(if (or (string-prefix-p "#+title" current-entry t)
(string-prefix-p "#+roam_alias" current-entry t)
(string-prefix-p "#+roam_key" current-entry t)
(string-prefix-p "#+roam_tags" current-entry t)
(string-prefix-p "#+zettel" current-entry t))
(delete-region (line-beginning-position) (line-end-position))
(forward-line 1))))
(string-trim (buffer-string)))))
(defun orgrr-return-zettel-from-title (title)
"Returns the matched zettel from a title."
(let* ((matched-title-filename (gethash title orgrr-title-filename))
(matched-zettel (gethash (concat "\\" matched-title-filename) orgrr-filename-zettel)))
matched-zettel))
(defun orgrr-open-previous-zettel ()
"Opens the previous zettel."
(interactive)
(let ((current-zettel (orgrr-read-current-zettel)))
(when current-zettel
(orgrr-prepare-zettelrank)
(let* ((current-zettel-rank (gethash current-zettel orgrr-zettel-zettelrank))
(previous-rank (- (string-to-number current-zettel-rank) 1))
(matched-zettel (gethash (number-to-string previous-rank) orgrr-zettelrank-zettel))
(matched-zettel-filename (gethash matched-zettel orgrr-zettel-filename)))
(orgrr-open-file matched-zettel-filename)))
(when (not current-zettel)
(message "This note has no value for zettel, so there is no next zettel!"))))
(defun orgrr-find (arg)
"Find org-file in `org-directory' via mini-buffer completion. If the
selected file name does not exist, a new one is created."
(interactive "P")
(let ((selection)
(filename)
(time))
(when (equal arg '(4))
(setq selection (orgrr-global-selection))
(when (member selection (hash-table-keys orgrr-title-short_filename))
(setq filename (gethash selection orgrr-title-short_filename))
(setq filename (gethash (concat "\\" filename) orgrr-short_filename-filename))
(orgrr-open-file filename)))
(when (not (equal arg '(4)))
(setq selection (orgrr-selection))
(when (member selection (hash-table-keys orgrr-title-filename))
(setq filename (gethash selection orgrr-title-filename))
(orgrr-open-file filename))
(when (not (member selection (hash-table-keys orgrr-title-filename)))
(setq time (format-time-string "%Y%m%d%H%M%S"))
(setq filename (concat (file-name-as-directory org-directory) time "-" (replace-regexp-in-string "[\"'?,.:;\(\)\\\s\/]" "_" selection)))
(setq filename (replace-regexp-in-string "_\\{2,\\}" "_" filename))
(when (orgrr-on-macos-p)
(setq filename (ucs-normalize-HFS-NFD-string filename)))
(orgrr-open-file (concat filename ".org"))
(insert (concat "#+title: " selection "\n"))))))
(defun orgrr-global-find ()
"A simple wrapper for a global orgrr-find."
(interactive)
(orgrr-find '(4)))
(defun orgrr-quick-add (&optional container)
"Create org-file in container or org-directory."
(interactive)
(let* ((filename)
(orgrr-name-container (orgrr-get-list-of-containers))
(containers (nreverse (hash-table-keys orgrr-name-container)))
(time))
(if (member container containers)
(setq container (gethash container orgrr-name-container))
(setq container org-directory))
(setq time (format-time-string "%Y%m%d%H%M%S"))
(setq filename (concat (file-name-as-directory container) time "-" orgrr-quick-add-token))
(when (orgrr-on-macos-p)
(setq filename (ucs-normalize-HFS-NFD-string filename)))
(orgrr-open-file (concat filename ".org"))
(insert (concat "#+title: " time "-" orgrr-quick-add-token "\n\n"))))
(defun orgrr-global-quick-add ()
"Create org-file in a specific selected container."
(interactive)
(let* ((orgrr-name-container (orgrr-get-list-of-containers))
(containers (nreverse (hash-table-keys orgrr-name-container)))
(container))
(while (not (member container containers))
(setq container (completing-read "Select container: " containers)))
(orgrr-quick-add container)))
(defun orgrr-insert (arg)
"Insert links to an org-file in `org-directory' via mini-buffer completion.
If the selected title does not exist, a new note is created."
(interactive "P")
(let ((path-of-current-note
(if (buffer-file-name)
(file-name-directory (buffer-file-name))
default-directory))
(selection)
(filename))
(when (equal arg '(4))
(setq selection (orgrr-global-selection))
(setq filename (gethash selection orgrr-title-short_filename))
(setq filename (gethash (concat "\\" filename) orgrr-short_filename-filename))
(setq filename (file-relative-name filename path-of-current-note))
(if (region-active-p)
(kill-region (region-beginning) (region-end)))
(insert (concat "\[\[file:" filename "\]\[" selection "\]\]")))
(when (not (equal arg '(4)))
(setq selection (orgrr-selection))
(if (member selection (hash-table-keys orgrr-title-filename))
(progn
(setq filename (gethash selection orgrr-title-filename))
(setq filename (file-relative-name filename path-of-current-note))
(if (region-active-p)
(kill-region (region-beginning) (region-end)))
(insert (concat "\[\[file:" filename "\]\[" selection "\]\]")))
(progn
(let* ((time (format-time-string "%Y%m%d%H%M%S"))
(filename (concat (file-name-as-directory org-directory) time "-" (replace-regexp-in-string "[\"'?,.:;\(\)\\\s\/]" "_" selection)))
(filename (replace-regexp-in-string "_\\{2,\\}" "_" filename)))
(when (orgrr-on-macos-p)
(setq filename (ucs-normalize-HFS-NFD-string filename)))
(if (region-active-p)
(kill-region (region-beginning) (region-end)))
(insert (concat "\[\[file:" (file-relative-name filename path-of-current-note) ".org" "\]\[" selection "\]\]"))
(orgrr-open-file (concat filename ".org"))
(insert (concat "#+title: " selection "\n"))))))))
(defun orgrr-global-insert ()
"A simple wrapper for a global orgrr-insert."
(interactive)
(orgrr-insert '(4)))
(defun orgrr-random-note ()
"Opens random org-file in `org-directory'."
(interactive)
(orgrr-get-meta)
(let* ((titles (hash-table-keys orgrr-title-filename))
(random-title (elt titles (random (length titles))))
(filename (gethash random-title orgrr-title-filename)))
(orgrr-open-file filename)))
(defun orgrr-rename (&optional old-filename new-filename)
"Rename current file and adjust all mentions of said file in other org-files
in all containers. Does work across directories."
(interactive)
(let* ((old-filename-mentions '())
(orgrr-name-container (orgrr-get-list-of-containers))
(containers (nreverse (hash-table-values orgrr-name-container)))
(lines))
(when (not old-filename)
(setq old-filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name))))
(when (not new-filename)
(setq new-filename (read-from-minibuffer "Filename to change: " old-filename)))
(rename-file old-filename new-filename)
(set-visited-file-name new-filename nil t)
(save-some-buffers t) ;; necessary, as we are working directly with the files
;; Add all files that mention filename to the list old-filename-mentions.
(dolist (container containers)
(with-temp-buffer
(if (orgrr-on-macos-p)
(insert (shell-command-to-string (concat "rg -l -F \"" (ucs-normalize-HFS-NFD-string (file-name-nondirectory old-filename)) "\" \"" (expand-file-name container) "\" -n -g \"*.org\"")))
(insert (shell-command-to-string (concat "rg -l -F \"" (file-name-nondirectory old-filename) "\" \"" (expand-file-name container) "\" -n -g \"*.org\""))))
(setq lines (split-string (buffer-string) "\n" t)))
(dolist (line lines)
(if (string-match "\\.org$" line)
(push line old-filename-mentions))))
;; This corrects the links. Please be aware that this is an intrusive action and might affect your data.
(dolist (filename old-filename-mentions)
(with-current-buffer (find-file-noselect filename)
(goto-char (point-min))
(while (re-search-forward (file-name-nondirectory old-filename) nil t)
(replace-match (file-name-nondirectory new-filename)))))
(save-some-buffers t)))
(defun orgrr-rename-title-and-file ()
"Changes the title and filename of the current note. The first part of the
filename (with the creation date) will not be modified."
(interactive)
(let ((old-filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name)))
(old-creation-time)
(new-title)
(new-filename))
(save-buffer)
(setq new-title (read-from-minibuffer "New title: "))
(orgrr-change-title new-title)
(setq new-filename (replace-regexp-in-string "[\"'?,:;\\\s\/]" "_" new-title))
(string-match "\[0-9\]+-" old-filename)
(setq old-creation-time (match-string 0 old-filename))
(setq new-filename (concat old-creation-time new-filename ".org"))
(orgrr-rename nil new-filename)))