-
Notifications
You must be signed in to change notification settings - Fork 5
/
org-clones.el
1184 lines (1025 loc) · 40.7 KB
/
org-clones.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; org-clones.el --- Clone Orgmode headings -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Jeff Filipovits
;; Author: Jeff Filipovits <[email protected]>
;; URL: http://www.github.com/legalnonsense/org-clones
;; Version: 0.1-pre
;; Package-Requires: ((emacs "25.2"))
;; Keywords: org transclusion clones outline
;; This file is not part of GNU Emacs.
;;; Commentary:
;; This package creates clones org headings. It uses the following
;; terminology:
;; "Node" means an entry in an org outline
;;
;; "Headline" means the headline text of the entry, but does
;; not include the headline's todo state or tags
;;
;; "Body" means everything after the headline, planning line,
;; and property drawers until the next node.
;; This package allows the user to clone a node, which duplicates
;; the node's headline and body. An overlay is then placed over each clone.
;; If the user attempts to edit the clone, they are prompted to either enter
;; an edit-mode, which will sync all changes to other clones upon completion,
;; or to unlink the clone.
;;;; Installation
;;;;; Manual
;; Put this file in your load-path, and put this in your init
;; file:
;; (require 'org-clones)
;;;; Usage
;; M-x `org-clones-mode' (or add it to your org-mode hook).
;; With the cursor on an org heading, run
;; `org-clones-create-clone'. You have created a clone.
;; Edit the clone, and your edits will sync.
;; You can also create clones across files with
;; `org-clones-store-marker' at an org heading and
;; running `org-clones-create-clone-from-marker'
;; at the location (of any file) where you want the
;; clone.
;; See http://www.github.com/legalnonsense/org-clones for more
;; information.
;;;; Tips
;; + You can customize settings in the `org-clones' group.
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
;;;; Requirements
(require 'org)
(require 'org-id)
(require 'org-element)
;;;; Customization
(defgroup org-clones ()
"Heading transclusion for Orgmode files."
:group 'org
:prefix "org-clones-")
(defcustom org-clones-commit-edit-shortcut "C-c C-c"
"Shortcut to commit edits when in `org-clones-edit-mode'
Accepts any string acceptable to `kbd'."
:group 'org-clones
:type 'string)
(defcustom org-clones-abort-edit-shortcut "C-c C-k"
"Shortcut to abort edits when in `org-clones-edit-mode'
Accepts any string acceptable to `kbd'."
:group 'org-clones
:type 'string)
(defcustom org-clones-start-edit-shortcut "C-c C-c"
"Shortcut to initiate `org-clones-edit-mode'
Accepts any string acceptable to `kbd'."
:group 'org-clones
:type 'string)
(defcustom org-clones-jump-to-next-clone-shortcut "n"
"Shortcut to jump to next clone via `org-clones-jump-to-clones'
Accepts any string acceptable to `kbd'."
:group 'org-clones
:type 'string)
(defcustom org-clones-clone-prefix-icon "◈ "
"String prepended to the headline of a cloned node."
:group 'org-clones
:type 'string)
(defcustom org-clones-empty-body-string "[empty clone body]"
"Place holder inserted into clones with empty bodies.
Can be any string other than whitespace. Must end with a newline.
Must be a string other than whitespace."
:group 'org-clones
:type 'string)
(defcustom org-clones-empty-headling-string "[empty clone headline]"
"Place holder inserted into clones with empty headlines.
Must be a string other than whitespace."
:group 'org-clones
:type 'string)
(defcustom org-clones-use-popup-prompt nil
"Whether to use a dialog box to prompt before syncing clones."
:group 'org-clones
:type 'boolean)
(defcustom org-clones-prompt-before-syncing nil
"Whether to prompt the user before syncing changes to all clones."
:group 'org-clones
:type 'boolean)
;;;; Keymaps
(defvar org-clones--clone-cycle-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd org-clones-jump-to-next-clone-shortcut)
#'org-clones-jump-to-clones)
map)
"Transient keymap for clone cycling.")
(defvar org-clones--transient-clone-overlay-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd org-clones-start-edit-shortcut)
#'org-clones--begin-edit)
map)
"Keymap for transient overlays.")
;;;; Faces
(defgroup org-clones-faces ()
"Faces for org-clones."
:group 'org-clones)
(defface org-clones-current-clone
'((t (:background "orchid" :foreground "black")))
"Face applied when the point is inside a cloned
node (i.e., headline or body)."
:group 'org-clones-faces)
(defface org-clones-clone
'((t (:background "black")))
"Face applied to the headline and body of each cloned node."
:group 'org-clones-faces)
;;;; Variables
(defvar org-clones--cursor-sensor-functions '(org-clones--text-watcher)
"List of cursor-sensor-functions to apply to the headline
and body of cloned nodes.")
(defvar org-clones--temp-marker nil
"Temporary storage for a marker for clone creation.")
(defvar org-clones--clone-cycle-list nil
"Temporary storage when cycling through clones.")
(defvar org-clones--clone-cycle-buffer-list nil
"Temporary storage for the buffer name and `header-line-format'
list when cycling through clones.")
(defvar org-clones--clone-cycle-header-msg
"Press 'n' to jump to next clone; any key to quit."
"Message displayed in the header-line when cycling
through clones.")
(defvar org-clones--transient-overlay-properties
`(face org-clones-current-clone
keymap ,org-clones--transient-clone-overlay-map
priority 1000
evaporate t)
"Properties to be added to the transient overlay.")
(defvar org-clones--clone-headline-overlay-props
`(before-string ,org-clones-clone-prefix-icon
evaporate t)
"Overlays placed on each clone, regardless of whether the
cursor is on the cloned node. Must be a plist of overlay properties.
By default, this only displays `org-clones-clone-prefix-icon'.")
(defvar org-clones--edit-mode-header-line
'(:eval
(format
"Edit cloned node. '%s' to finish and update. '%s' to abandon."
org-clones-commit-edit-shortcut
org-clones-abort-edit-shortcut))
"The value of header-line-format when `org-clones-edit-mode' is
invoked.")
(defvar org-clones--node-text-properties nil
"Text properties to place on the headline and body of each node.
Note: If you want to chage the cursor-sensor-functions property,
(perhaps for use with a different package) use
`org-clones--put-clone-cursor-sensor-props'. This variable is for other
text properties (e.g., a face, keymap, etc.).
Note: 'face does not work with org-mode. Use 'font-lock-face.
'keymap does not work with org-mode. Use a keymap in an
overlay instead." )
(defvar org-clones--node-overlay-properties
'(face org-clones-clone)
"Overlay properties placed at the headline and body of each node.")
(defvar-local org-clones--previous-header-line nil
"Temporary storage for the value of `header-line-format'.")
(defvar-local org-clones--transient-overlay nil
"Temporary holder for the transient headline
or body overlay.")
(defvar-local org-clones--restore-state nil
"When editing a clone, save the current headline and body
to restore if the edit is abandoned.")
(defvar org-clones--org-headline-re org-outline-regexp
"Org headline regexp.")
(defvar org-clones--not-whitespace-re "[^[:space:]]"
"Regexp matching any non-whitespace charcter.")
(defvar org-clones--progress-cookie-re "\\[[[:digit:]]*/[[:digit:]]*\\]\\|\\[[[:digit:]]*\\%\\]"
"Regexp for org progress cookies, e.g., [2/3] or [55%].")
(defvar org-clones--inline-code-result-re "{{{.*}}}"
"Regexp for incline org-babel code results")
(defvar org-clones--headline-comment-re (concat " " org-comment-string " ")
"Regexp for COMMENT prefix for org headlines.")
(defvar org-clones--priority-cookie-re ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
"Regexp for priority cookies for org headlines.")
(defvar org-clones--tag-re (concat ":" org-tag-re ":")
"Regexp for headline tags.")
;;;; Macros
(defmacro org-clones--iterate-over-clones (&rest body)
"Execute BODY at each clone of node at point."
`(save-excursion
(when-let ((clone-ids (org-clones--get-clone-ids)))
(cl-loop for clone-id in clone-ids
do (org-clones--with-point-at-id clone-id
,@body)))))
(defmacro org-clones--iterate-over-all-clones-in-buffer (&rest body)
"Execute BODY at any clone which has a non-nil :ORG-CLONES: property,
in the buffer (but do not iterate over clones outside the buffer)."
`(save-excursion
(goto-char (point-min))
(while (re-search-forward org-property-drawer-re nil t)
(goto-char (match-beginning 0))
(when (re-search-forward ":ORG-CLONES:" nil (match-end 0))
(when (org-entry-get (point) "ORG-CLONES")
,@body)))))
(defmacro org-clones--with-point-at-id (id &rest body)
"Switch to the buffer containing the entry with `org-id' ID.
Move the cursor to that entry in that buffer, execute BODY,
move back."
(declare (indent defun))
`(when-let ((marker (org-id-find ,id 'marker)))
(with-current-buffer (marker-buffer marker)
(goto-char marker)
(org-show-entry)
,@body)))
;;;; Cursor movement functions
(defun org-clones--goto-previous-non-whitespace-char ()
"Move the point back to the nearest non-whitespace character in
the buffer."
(when (re-search-backward org-clones--not-whitespace-re
nil'no-error)
(goto-char (match-end 0))))
;;;; Headline functions
(defun org-clones--goto-headline-start ()
"Goto the first point of the headline, after the
leading stars, TODO state, or COMMENT."
(org-back-to-heading)
;; Apparently priority cookies can be anywhere in the headline, so
;; I am just going to do this by comparing the end results of the
;; appropriate regexp searches.
(let ((new-point 0)) ;; If it stays 0, thow an error later.
(cl-flet ((new-point-p (candidate)
(pcase candidate
((and (pred numberp)
(pred (< new-point)))
(setq new-point candidate)))))
;; Since these fuckers can be in any order, I am just going to
;; test each of them. This is also already fixed by normalizing
;; the headline so they are in a predictable order, but that is
;; not stable yet.
(mapc #'new-point-p `(,(org-clones--goto-after-todo-state)
,(org-clones--goto-after-priority-cookie)
,(org-clones--goto-after-leading-stars)
,(org-clones--goto-after-comment-indicator)))
(if (= new-point 0)
(error "No headline detected.")
(goto-char new-point)))))
(defun org-clones--goto-after-todo-state ()
"Go to the next non-whitespace point after the TODO state.
Return nil if there is no point."
(org-back-to-heading)
(when (org-get-todo-state)
(when (re-search-forward (org-get-todo-state) (point-at-eol) t)
(forward-char 1)
(point))))
(defun org-clones--goto-after-priority-cookie ()
"Go to the next non-whitespace point after the priority cookie.
Return nil if there is no point."
(org-back-to-heading)
(when (re-search-forward org-clones--priority-cookie-re (point-at-eol) t)
(point)))
(defun org-clones--goto-after-comment-indicator ()
"Go to the next non-whitespace point after the comment indicator.
Return nil if there is no point."
(org-back-to-heading)
(when (org-in-commented-heading-p)
(re-search-forward org-clones--headline-comment-re
(point-at-eol) t)))
(defun org-clones--goto-after-leading-stars ()
"Go to the next non-whitespace point after the leading stars.
Return nil if there is no point."
(org-back-to-heading)
(re-search-forward org-clones--org-headline-re
(point-at-eol) t))
(defun org-clones--get-headline-start ()
"Get the point at the start of the headling, after
the leading stars."
(save-excursion
(org-clones--goto-headline-start)))
(defun org-clones--normalize-headline ()
"Move any progress tracking cookie to the end of the headline."
(org-clones--fix-progress-cookie-location)
(org-clones--fix-priority-cookie-location))
(defun org-clones--fix-priority-cookie-location ()
"Move any priority cookie in the headline to appear
after the TODO keyword or at the start of the headline
if there is no TODO keyword."
(org-back-to-heading)
(let (match)
(when (re-search-forward org-clones--priority-cookie-re
nil (point-at-eol))
(setq match (match-string 1))
(delete-region (match-beginning 1) (match-end 1))
;; If there is a TODO, go after it.
(unless (org-clones--goto-after-todo)
;; Otherwise, the priority comes first
(org-clones--goto-after-leading-stars))
(insert match)
(org-back-to-heading))))
(defun org-clones--fix-progress-cookie-location ()
;; Move the progress cookies to the end of the headline
(org-back-to-heading)
(let (match cookies)
(while (re-search-forward org-clones--progress-cookie-re
(point-at-eol)
'no-error)
(setq match (match-string 0))
(replace-match "")
(when (looking-at " ")
(delete-char 1))
(push match cookies))
(unless
(re-search-forward org-clones--tag-re (point-at-eol) t)
(org-end-of-line))
(cl-loop for cookie in (reverse cookies)
do (insert " " cookie))))
(defun org-clones--goto-headline-end ()
"Goto the last point of the headline (i.e., before the progress cookie
and tag line."
(org-back-to-heading t)
(cond
;; We assume that the headline is in this position:
;; TODO headline babel-results progress-cookie tags
((re-search-forward org-clones--inline-code-result-re (point-at-eol) t)
(goto-char (match-beginning 0))
(org-clones--goto-previous-non-whitespace-char))
((re-search-forward org-clones--progress-cookie-re (point-at-eol) t)
(goto-char (match-beginning 0))
(org-clones--goto-previous-non-whitespace-char))
((re-search-forward
org-clones--tag-re (point-at-eol) t)
(goto-char (match-beginning 0))
(org-clones--goto-previous-non-whitespace-char))
(t (end-of-line)))
(when (re-search-backward org-clones--not-whitespace-re)
(goto-char (match-end 0)))
(point))
(defun org-clones--get-headline-end ()
"Get the point at the end of the headline, but
before the ellipsis."
(save-excursion (org-clones--goto-headline-end)))
(defun org-clones--at-headline-p ()
"Is the point inside a headline?"
(and (outline-on-heading-p t)
(<= (point) (org-clones--get-headline-end))
(>= (point) (org-clones--get-headline-start))))
(defun org-clones--get-headline-string ()
"Get the full text of a headline at point, but excluding:
- leading stars
- TODO state
- COMMENT prefix
- org-babel inline results
- tags
- priority cookies
- progress cookies."
(buffer-substring-no-properties (org-clones--get-headline-start)
(org-clones--get-headline-end)))
(defun org-clones--delete-headline ()
"Delete the headline of the heading at point."
(let ((inhibit-read-only t))
(unless (string=
(plist-get (cadr (org-element-at-point)) :raw-value)
"")
(delete-region (org-clones--get-headline-start)
(org-clones--get-headline-end)))))
(defun org-clones--replace-headline (headline)
"Replace the headline text at point with HEADLINE."
(when (string-match "\n" headline)
(error "You can't have a newline in a headline string."))
(let ((inhibit-read-only t))
(save-excursion
(org-clones--normalize-headline)
(org-clones--delete-headline)
(org-clones--goto-headline-start)
(insert headline)
(org-align-tags))))
;;;; Body functions
(defun org-clones--node-body-p ()
"Does this node have a body (i.e., a 'section' in org-element
parlance)?"
(org-back-to-heading)
(org-clones--parse-body))
(defun org-clones--goto-body-end ()
"Goto the end of the body of the current node,
and return the point. The end of the body is defined
as the last non-whitespace character before the next heading."
(goto-char (org-entry-end-position))
(re-search-backward org-clones--not-whitespace-re nil t)
(goto-char (match-end 0))
(point))
(defun org-clones--get-body-end ()
"Get the end point of the body of the current node."
(save-excursion (org-clones--goto-body-end)))
(defun org-clones--goto-body-start ()
"Go to the start of the body of the current node,
and return the point. The start of the body is defined as
the point after the planning line, drawers immediately following
the planning line, and any closing note."
(org-end-of-meta-data t)
(let ((section (org-clones--get-section-elements)))
;; This is a ridiculous way to check if there is a note!
(if (and section
(eq (caar section) 'plain-list)
(eq (car (caddar section)) 'item)
(eq (caaddr (caddar section)) 'paragraph)
;; I don't think there is a variable to control
;; the prefix of the closing note, so using "CLOSING NOTE "
;; and assuming it is static.
(string= (caddr (caddr (caddar section))) "CLOSING NOTE "))
(goto-char (plist-get (cadar section) :end))
(point))))
(defun org-clones--get-body-start ()
"Get the start point of the body of the current node."
(save-excursion (org-clones--goto-body-start)))
(defun org-clones--at-body-p ()
"Is the point inside the body of a node?"
(when-let ((start (org-clones--get-body-start))
(end (org-clones--get-body-end)))
(and (<= (point) end)
(>= (point) start))))
(defun org-clones--normalize-body ()
"Placeholder for normalizing the format of a node."
(save-excursion
(org-end-of-meta-data t)
(when (looking-at org-clones--org-headline-re)
(backward-char)
(unless (looking-at "^[[:space:]]")
(insert "\norg-clones-empty-body-string\n")))))
(defun org-clones--replace-body (body)
"Replace the body of the current node with
BODY. If BODY is nil, then use `org-clones-empty-body-string'."
(let ((inhibit-read-only t))
(org-back-to-heading)
(save-excursion
(org-clones--delete-body))
(org-clones--goto-body-start)
(save-excursion
(insert (or body
org-clones-empty-body-string)
"\n"))
(org-clones--normalize-body)))
(defun org-clones--parse-body ()
"Parse all elements from the start of the body to the next node.
and return the tree beginning with the section element."
(org-element--parse-elements (save-excursion (org-back-to-heading)
(org-end-of-meta-data t)
(point))
(or (save-excursion (outline-next-heading))
(point-max))
'first-section nil nil nil nil))
(defun org-clones--remove-closing-note-from-section-elements (section)
"If there is a closing note in the section, remove it and return the section list."
(if (and (eq (caar section) 'plain-list)
(eq (car (caddar section)) 'item)
(eq (caaddr (caddar section)) 'paragraph)
(string= (caddr (caddr (caddar section))) "CLOSING NOTE "))
(cdr section)
section))
(defun org-clones--get-body-string ()
"Get the body of the current node as a string."
(org-no-properties
(org-element-interpret-data
(org-clones--remove-closing-note-from-section-elements
(org-clones--get-section-elements)))))
(defun org-clones--get-section-elements ()
"Reduce the section data to the component elements,
e.g., '((paragraph (...))
(src-block (...)) ...)."
(cddar (org-clones--parse-body)))
(defun org-clones--get-body-section-plist ()
"Get the plist associated with the section element,
e.g. (:begin 1 :end 10 :contents-begin ...)."
(cadar (org-clones--parse-body)))
(defun org-clones--delete-body ()
(let ((inhibit-read-only t))
(delete-region (org-clones--get-body-start)
(save-excursion
(outline-next-heading) (point)))))
(defun org-clones--fold-property-drawer (&optional unfold)
"Fold the property drawer for the heading at point. If
UNFOLD is non-nil, then unfold the drawer."
(save-excursion
(org-back-to-heading t)
(when (re-search-forward
org-property-drawer-re
(save-excursion (or (outline-next-heading)
(point-max)))
'no-error)
(org-flag-drawer (not unfold)))))
;;;; Clone functions
(defun org-clones--at-clone-p (&optional pos)
"Is the current point (or POS) in a cloned node?"
(org-entry-get (or pos (point)) "ORG-CLONES"))
(defun org-clones--get-clone-ids ()
"Get the org-ids of this node's clones. Return
nil if there are none."
(org-entry-get-multivalued-property
(point)
"ORG-CLONES"))
(defun org-clones--get-range-of-field-at-point ()
"Return a cons cell with the car being the start point and
cdr being the end point of the of the headline or body of the node,
depending on the location of the point."
(cond ((org-clones--at-headline-p)
(cons (org-clones--get-headline-start)
(org-clones--get-headline-end)))
((org-clones--at-body-p)
(cons (org-clones--get-body-start)
(org-clones--get-body-end)))))
(defun org-clones--last-node-p ()
"Is this the last node in the file?"
(not (or (save-excursion (org-get-next-sibling))
(save-excursion (org-goto-first-child)))))
(defun org-clones--prompt-for-source-node-and-move ()
"Prompt user for a node and move to it."
(org-goto))
(defun org-clones--sync-clones (&optional no-prompt)
"Update all clones of the current node to match
the headline and body of the current node and
place text properties and overlays in the cloned nodes.
If NO-PROMPT is non-nil, do not prompt the user
regardless of the value of `org-clones-prompt-before-syncing'."
(interactive)
(if (and (not no-prompt)
org-clones-prompt-before-syncing)
(org-clones--prompt-before-syncing)
(org-clones--remove-clone-effects)
(org-clones--normalize-headline)
(org-clones--normalize-body)
(let ((inhibit-read-only t)
(headline (org-clones--get-headline-string))
(body (if (string= "" (org-clones--get-body-string))
org-clones-empty-body-string
(org-clones--get-body-string))))
;; Replace the body in the current node to
;; normalize whitespace
(org-clones--replace-headline headline)
(org-clones--replace-body body)
(org-clones--put-clone-effects)
(org-clones--iterate-over-clones
(org-clones--remove-clone-effects)
(org-clones--replace-headline headline)
(org-clones--replace-body body)
(org-clones--put-clone-effects))))
(when org-clones-edit-mode
(org-clones-edit-mode -1))
(org-clones--put-transient-overlay)
(message "Clones synced."))
;;; Overlays
(defun org-clones--put-overlay-props (overlay props)
"Add PROPS to OVERLAY and return OVERLAY."
(cl-loop for x from 0 to (1- (length props)) by 2
do (overlay-put overlay
(nth x props)
(nth (1+ x) props))
finally return overlay))
(defun org-clones--put-node-overlay (&optional remove)
"Put `org-clones--node-overlay-properties' in the headline and body of
the node at point. If REMOVE is non-nil, remove the overlays."
(when org-clones--node-overlay-properties
(if remove
(progn
(remove-overlays (org-clones--get-headline-start)
(org-clones--get-headline-end)
'org-clones-user-overlay t)
(remove-overlays (org-clones--get-body-start)
(org-clones--get-body-end)
'org-clones-user-overlay t))
(let* ((headline-overlay (make-overlay (org-clones--get-headline-start)
(org-clones--get-headline-end)))
(body-overlay (make-overlay (org-clones--get-body-start)
(org-clones--get-body-end)))
(props (append org-clones--node-overlay-properties '(org-clones-user-overlay t))))
(cons (org-clones--put-overlay-props headline-overlay props)
(org-clones--put-overlay-props body-overlay props))))))
(defun org-clones--remove-node-overlays ()
"Remove `org-clones--node-overlay-properties' from the current node."
(org-clones--put-node-overlay 'remove))
(defun org-clones--put-headline-overlay (&optional remove)
"Put overlays in `org-clones--clone-headline-overlay-props' on the
current node. If REMOVE is non-nil, remove the the overlay."
(if remove
(remove-overlays (org-clones--get-headline-start)
(org-clones--get-headline-end)
'org-clones-clone-headline-overlay t)
(org-clones--remove-headline-overlay)
(let ((headline-overlay (make-overlay (org-clones--get-headline-start)
(org-clones--get-headline-end)))
(props (append org-clones--clone-headline-overlay-props
'(org-clones-clone-headline-overlay t))))
(org-clones--put-overlay-props headline-overlay props))))
(defun org-clones--remove-headline-overlay ()
"Remove the overlays in `org-clones--clone-headline-overlay-props' from the
current node."
(org-clones--put-headline-overlay 'remove))
(defun org-clones--put-transient-overlay ()
"Put the `org-clones--transient-overlay' in the cursor field
at point."
(when-let* ((points (org-clones--get-range-of-field-at-point))
(beg (car points))
(end (cdr points)))
(put-text-property beg end 'read-only t)
(move-overlay org-clones--transient-overlay beg end)))
;;;; Text properties
(defun org-clones--put-node-text-properties (&optional remove)
"Put `org-clones--node-text-properties' in the headline and body of
the node at point. If REMOVE is non-nil, remove the properties."
(when org-clones--node-text-properties
(cl-loop for start in `(,(org-clones--get-headline-start)
,(org-clones--get-body-start))
for end in `(,(org-clones--get-headline-end)
,(org-clones--get-body-end))
do (if remove
(remove-list-of-text-properties
start
end
org-clones--node-text-properties)
(add-text-properties
start
end
org-clones--node-text-properties)))))
(defun org-clones--remove-node-text-properties ()
"Remove `org-clones--node-text-properties' from the current node."
(org-clones--put-node-text-properties 'remove))
(defun org-clones--put-clone-effects ()
"Put overlay and text properties at the current
node. 'Clone effects' means: cursor sensor properties,
text properties, headline overlay, node overlay, and
automatically folding the property drawer."
(let ((inhibit-read-only t))
(org-clones--normalize-body)
(org-clones--put-clone-cursor-sensor-props)
(org-clones--put-headline-overlay)
(org-clones--put-node-overlay)
(org-clones--put-node-text-properties)
(org-clones--fold-property-drawer)))
(defun org-clones--remove-clone-effects ()
"Remove overlay and text properties at the current
node."
(let ((inhibit-read-only t))
(org-clones--remove-node-text-properties)
(org-clones--remove-cursor-sensor-props)
(org-clones--remove-node-overlays)
(org-clones--remove-headline-overlay)))
(defun org-clones--reset-clone-effects ()
"Remove and replace all clone effects for the current
node."
(let ((inhibit-read-only t))
(org-clones--remove-clone-effects)
(org-clones--put-clone-effects)))
;;;; Developement functions
(defun org-clones--remove-all-cursor-sensors-in-buffer ()
"Remove all cursor sensor text properties in the buffer."
(when (y-or-n-p
(concat
"This will remove all cursor-sensor-functions, even "
"those that are not associated with org-clones. Continue?"))
(let ((inhibit-read-only t))
(set-text-properties
(point-min)
(point-max)
'(cursor-sensor-functions nil)))))
(defun org-clones--highlight-cursor-sensor-props ()
"Highlight any points in the buffer with a non-nil cursor-sensor-functions
text property."
(ov-clear 'org-clones-testing 'any)
(save-excursion
(goto-char (point-min))
(cl-loop for points being the intervals of (current-buffer)
property 'cursor-sensor-functions
do (when (get-text-property (car points)
'cursor-sensor-functions)
(ov
(car points)
(cdr points)
'font-lock-face
'(:background "yellow" :foreground "black")
'org-clones-testing t)))))
;;;; Buffer preparation functions
(defun org-clones--reset-all-clone-effects-in-buffer ()
"Reset all clone effets on all clones in buffer."
(org-clones--iterate-over-all-clones-in-buffer
(org-clones--remove-clone-effects)
(org-clones--put-clone-effects)))
(defun org-clones--remove-all-clone-effects-in-buffer ()
"Remove all clone effets on all clones in buffer."
(org-clones--iterate-over-all-clones-in-buffer
(org-clones--remove-clone-effects)))
;;;; Cursor-sensor-functions
(defun org-clones--change-cursor-sensor-prop (beg end func &optional remove)
"Add FUNC to the list of cursor-sensor-functions from BEG to END. If REMOVE
is non-nil, then remove FUNC from the cursor-sensor-functions property. This
preserves any other functions already stored in the cursor-sensor-fucntions
text property, so it will not interfere with other modes using that text property."
(cl-loop
for pos from beg to end
do (let ((func-list (get-text-property pos 'cursor-sensor-functions)))
(put-text-property
pos (1+ pos) 'cursor-sensor-functions
(if remove
(pcase func-list
((pred listp)
(remq func func-list))
((guard (equal func func-list)) nil)
(_ func-list))
(pcase func-list
((pred null) (list func))
((pred listp) (cl-pushnew func func-list))
(_ (if (equal func-list func)
func
(list func func-list)))))))))
(defun org-clones--put-clone-cursor-sensor-props (&optional remove)
"Put `org-clones--cursor-sensor-functions' on the headline and body
of the current node. Note: these are treated differently than other
text properties because the value is a list and we need to preserve
and pre-existing values."
(cl-loop for start in `(,(org-clones--get-headline-start)
,(org-clones--get-body-start))
for end in `(,(org-clones--get-headline-end)
,(org-clones--get-body-end))
do (cl-loop for func in org-clones--cursor-sensor-functions
do (org-clones--change-cursor-sensor-prop
start end func remove))))
(defun org-clones--remove-cursor-sensor-props ()
"Remove `org-clones--cursor-sensor-functions' from the headline and
body of the current node."
(org-clones--put-clone-cursor-sensor-props 'remove))
(defun org-clones--text-watcher (_window last-pos entered-or-left)
"If ENTERED-OR-LEFT is eq to 'enter, then place the transient
overlay in the headline or body (as appropriate) and make the text
read only. If the value is 'left, then delete the transient overlay
and remove the read-only text property. See `cursor-sensor-mode' for
details on the arguments."
(pcase entered-or-left
(`entered
(org-clones--put-transient-overlay)
(message "Entered cloned node. Type '%s' to edit."
org-clones-start-edit-shortcut))
(`left
(when-let* ((points
(save-excursion (goto-char last-pos)
(org-clones--get-range-of-field-at-point)))
(beg (car points))
(end (cdr points)))
(let ((inhibit-read-only t))
(put-text-property beg end 'read-only nil)
(delete-overlay org-clones--transient-overlay))))))
;;;; Editing clones
(defun org-clones--begin-edit ()
"Invoke `org-clones-edit-mode'."
(interactive)
(org-clones-edit-mode 1))
(defun org-clones--prompt-before-syncing ()
"Ask the user if they want to edit the node
without syncing the clones. If so, unlink the current
clone."
(interactive)
;; Without this let, y-or-n-p pops a dialog box
;; by default due to something involving `cursor-sensor-mode'
(let ((last-nonmenu-event
(not org-clones-use-popup-prompt)))
(if (y-or-n-p "Sync your changes to all clones?")
(org-clones--sync-clones t)
(if (y-or-n-p "Unsync this clone?")
(org-clones-unclone-this-clone)
(if (y-or-n-p "Discard this edit?")
(org-clones--discard-edit)
;; If the user takes an impossible path,
;; send them back to the beginning.
(org-clones--prompt-before-syncing)))))
(org-clones-edit-mode -1))
(defun org-clones--discard-edit ()
"Discard the current edit and restore the node
to its previous state, and turn off the minor mode."
(interactive)
(org-clones--replace-headline
(car org-clones--restore-state))
(org-clones--replace-body
(cdr org-clones--restore-state))
(org-clones--put-clone-effects)
(org-clones-edit-mode -1)
(message "Org-clones: Discarded edit."))
(define-minor-mode org-clones-edit-mode
"Mode to edit clones."
nil
" EDIT-CLONE"
(let ((map (make-sparse-keymap)))
(define-key map (kbd org-clones-commit-edit-shortcut)
#'org-clones--sync-clones)
(define-key map (kbd org-clones-abort-edit-shortcut)
#'org-clones--discard-edit)
map)
(if org-clones-edit-mode
(progn
(setq cursor-sensor-inhibit nil)
(setq org-clones--previous-header-line header-line-format)
(setq header-line-format org-clones--edit-mode-header-line)
(setq org-clones--restore-state
(cons (org-clones--get-headline-string)
(org-clones--get-body-string)))
(overlay-put org-clones--transient-overlay 'keymap nil)
(let ((inhibit-read-only t)
(bounds (org-clones--get-range-of-field-at-point)))
(put-text-property (car bounds) (cdr bounds) 'read-only nil)))
(setq header-line-format org-clones--previous-header-line)
(when (equal header-line-format
org-clones--previous-header-line)
(setq header-line-format nil))
(overlay-put org-clones--transient-overlay
'keymap
org-clones--transient-clone-overlay-map)
(setq org-clones--restore-state nil)))
;;;; Initialization
(defun org-clones--initialize-transient-overlay ()
"Initialize `org-clones--transient-overlay'
used to highlight the clone at point. This overlay is reused
each time the point is in the headline or body of a cloned node."
(setq org-clones--transient-overlay
(make-overlay 1 2 nil nil t))
(org-clones--put-overlay-props
org-clones--transient-overlay
org-clones--transient-overlay-properties)
(delete-overlay org-clones--transient-overlay))
(defun org-clones--initialize-overlays-in-buffer ()
"Put overlays on all clones in current buffer."
(org-clones--iterate-over-all-clones-in-buffer
(org-clones--put-headline-overlay)))
(defun org-clones--cursor-sensor-mode-check ()
"Turn `cursor-sensor-mode' on or off depending on
whether there are any cursor-sensor-functions text
properties in the buffer."
(if (save-excursion
(save-restriction
(widen)
(next-single-property-change
(point-min)
'cursor-sensor-functions)))
;; If so, enable cursor-sensor-mode...
(cursor-sensor-mode 1)
;; ...otherwise, disable it.
(cursor-sensor-mode -1)))