-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-fold-core.el
1636 lines (1478 loc) · 81.6 KB
/
org-fold-core.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-fold-core.el --- Folding buffer text -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2020-2025 Free Software Foundation, Inc.
;;
;; Author: Ihor Radchenko <yantar92 at posteo dot net>
;; Maintainer: Ihor Radchenko <yantar92 at posteo dot net>
;; Keywords: folding, invisible text
;; URL: https://orgmode.org
;;
;; This file is part of GNU Emacs.
;;
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;; This file contains library to control temporary invisibility
;; (folding and unfolding) of text in buffers.
;; The file implements the following functionality:
;;
;; - Folding/unfolding regions of text
;; - Searching and examining boundaries of folded text
;; - Interactive searching in folded text (via isearch)
;; - Handling edits in folded text
;; - Killing/yanking (copying/pasting) of the folded text
;; To setup folding in an arbitrary buffer, one must call
;; `org-fold-core-initialize', optionally providing the list of folding specs to be
;; used in the buffer. The specs can be added, removed, or
;; re-configured later. Read below for more details.
;;; Folding/unfolding regions of text
;; User can temporarily hide/reveal (fold/unfold) arbitrary regions or
;; text. The folds can be nested.
;; Internally, nested folds are marked with different folding specs
;; Overlapping folds marked with the same folding spec are
;; automatically merged, while folds with different folding specs can
;; coexist and be folded/unfolded independently.
;; When multiple folding specs are applied to the same region of text,
;; text visibility is decided according to the folding spec with
;; topmost priority.
;; By default, we define two types of folding specs:
;; - 'org-fold-visible :: the folded text is not hidden
;; - 'org-fold-hidden :: the folded text is completely hidden
;;
;; The 'org-fold-visible spec has highest priority allowing parts of
;; text folded with 'org-fold-hidden to be shown unconditionally.
;; Consider the following Org mode link:
;; [[file:/path/to/file/file.ext][description]]
;; Only the word "description" is normally visible in this link.
;;
;; The way this partial visibility is achieved is combining the two
;; folding specs. The whole link is folded using 'org-fold-hidden
;; folding spec, but the visible part is additionally folded using
;; 'org-fold-visible:
;;
;; <begin org-fold-hidden>[[file:/path/to/file/file.ext][<begin org-fold-visible>description<end org-fold-visible>]]<end org-fold-hidden>
;;
;; Because 'org-fold-visible has higher priority than
;; 'org-fold-hidden, it suppresses the 'org-fold-hidden effect and
;; thus reveals the description part of the link.
;; Similar to 'org-fold-visible, display of any arbitrary folding spec
;; can be configured using folding spec properties. In particular,
;; `:visible' folding spec property controls whether the folded text
;; is visible or not. If the `:visible' folding spec property is nil,
;; folded text is hidden or displayed as a constant string (ellipsis)
;; according to the value of `:ellipsis' folding spec property. See
;; docstring of `org-fold-core--specs' for the description of all the available
;; folding spec properties.
;; Folding spec properties of any valid folding spec can be changed
;; any time using `org-fold-core-set-folding-spec-property'.
;; If necessary, one can add or remove folding specs using
;; `org-fold-core-add-folding-spec' and `org-fold-core-remove-folding-spec'.
;; If a buffer initialized with `org-fold-core-initialize' is cloned into indirect
;; buffers, it's folding state is copied to that indirect buffer.
;; The folding states are independent.
;; When working with indirect buffers that are handled by this
;; library, one has to keep in mind that folding state is preserved on
;; copy when using non-interactive functions. Moreover, the folding
;; states of all the indirect buffers will be copied together.
;;
;; Example of the implications:
;; Consider a base buffer and indirect buffer with the following state:
;; ----- base buffer --------
;; * Heading<begin fold>
;; Some text folded in the base buffer, but unfolded in the indirect buffer<end fold>
;; * Other heading
;; Heading unfolded in both the buffers.
;; ---------------------------
;; ------ indirect buffer ----
;; * Heading
;; Some text folded in the base buffer, but unfolded in the indirect buffer
;; * Other heading
;; Heading unfolded in both the buffers.
;; ----------------------------
;; If some Elisp code copies the whole "Heading" from the indirect
;; buffer with `buffer-substring' or match data and inserts it into
;; the base buffer, the inserted heading will be folded since the
;; internal setting for the folding state is shared between the base
;; and indirect buffers. It's just that the indirect buffer ignores
;; the base buffer folding settings. However, as soon as the text is
;; copied back to the base buffer, the folding state will become
;; respected again.
;; If the described situation is undesired, Elisp code can use
;; `filter-buffer-substring' instead of `buffer-substring'. All the
;; folding states that do not belong to the currently active buffer
;; will be cleared in the copied text then. See
;; `org-fold-core--buffer-substring-filter' for more details.
;; Because of details of implementation of the folding, it is also not
;; recommended to set text visibility in buffer directly by setting
;; `invisible' text property to anything other than t. While this
;; should usually work just fine, normal folding can be broken if one
;; sets `invisible' text property to a value not listed in
;; `buffer-invisibility-spec'.
;;; Searching and examining boundaries of folded text
;; It is possible to examine folding specs (there may be several) of
;; text at point or search for regions with the same folding spec.
;; See functions defined under ";;;; Searching and examining folded
;; text" below for details.
;; All the folding specs can be specified by symbol representing their
;; name. However, this is not always convenient, especially if the
;; same spec can be used for fold different syntactical structures.
;; Any folding spec can be additionally referenced by a symbol listed
;; in the spec's `:alias' folding spec property. For example, Org
;; mode's `org-fold-outline' folding spec can be referenced as any
;; symbol from the following list: '(headline heading outline
;; inlinetask plain-list) The list is the value of the spec's `:alias'
;; property.
;; Most of the functions defined below that require a folding spec
;; symbol as their argument, can also accept any symbol from the
;; `:alias' spec property to reference that folding spec.
;; If one wants to search invisible text without using the provided
;; functions, it is important to keep in mind that 'invisible text
;; property may have multiple possible values (not just nil and
;; t). Hence, (next-single-char-property-change pos 'invisible) is not
;; guaranteed to return the boundary of invisible/visible text.
;;; Interactive searching inside folded text (via isearch)
;; The library provides a way to control if the folded text can be
;; searchable using isearch. If the text is searchable, it is also
;; possible to control to unfold it temporarily during interactive
;; isearch session.
;; The isearch behavior is controlled on per-folding-spec basis by
;; setting `isearch-open' and `isearch-ignore' folding spec
;; properties. See the docstring of `org-fold-core--specs' for more details.
;;; Handling edits inside folded text
;; The visibility of the text inserted in front, rear, or in the
;; middle of a folded region is managed according to `:front-sticky'
;; and `:rear-sticky' folding properties of the corresponding folding
;; spec. The rules are the same with stickiness of text properties in
;; Elisp.
;; If a text being inserted into the buffer is already folded and
;; invisible (before applying the stickiness rules), then it is
;; revealed. This behavior can be changed by wrapping the insertion
;; code into `org-fold-core-ignore-modifications' macro. The macro will disable
;; all the processing related to buffer modifications.
;; The library also provides a way to unfold the text after some
;; destructive changes breaking syntactical structure of the buffer.
;; For example, Org mode automatically reveals folded drawers when the
;; drawer becomes syntactically incorrect:
;; ------- before modification -------
;; :DRAWER:<begin fold>
;; Some folded text inside drawer
;; :END:<end fold>
;; -----------------------------------
;; If the ":END:" is edited, drawer syntax is not correct anymore and
;; the folded text is automatically unfolded.
;; ------- after modification --------
;; :DRAWER:
;; Some folded text inside drawer
;; :EN:
;; -----------------------------------
;; The described automatic unfolding is controlled by `:fragile'
;; folding spec property. It's value can be a function checking if
;; changes inside (or around) the fold should drigger the unfold. By
;; default, only changes that directly involve folded regions will
;; trigger the check. In addition, `org-fold-core-extend-changed-region-functions'
;; can be set to extend the checks to all folded regions intersecting
;; with the region returned by the functions listed in the variable.
;; The fragility checks can be bypassed if the code doing
;; modifications is wrapped into `org-fold-core-ignore-fragility-checks' macro.
;;; Performance considerations
;; This library is using text properties to hide text. Text
;; properties are much faster than overlays, that could be used for
;; the same purpose. Overlays are implemented with O(n) complexity in
;; Emacs (as for 2021-03-11). It means that any attempt to move
;; through hidden text in a file with many invisible overlays will
;; require time scaling with the number of folded regions (the problem
;; Overlays note of the manual warns about). For curious, historical
;; reasons why overlays are not efficient can be found in
;; https://www.jwz.org/doc/lemacs.html.
;; Despite using text properties, the performance is still limited by
;; Emacs display engine. For example, >7Mb of text hidden within
;; visible part of a buffer may cause noticeable lags (which is still
;; orders of magnitude better in comparison with overlays). If the
;; performance issues become critical while using this library, it is
;; recommended to minimize the number of folding specs used in the
;; same buffer at a time.
;; Alternatively, the library provides `org-fold-core--optimise-for-huge-buffers'
;; for additional speedup. This can be used as a file-local variable
;; in huge buffers. The variable can be set to enable various levels
;; of extra optimization. See the docstring for detailed information.
;; It is worth noting that when using `org-fold-core--optimise-for-huge-buffers'
;; with `grab-invisible' option, folded regions copied to other
;; buffers (including buffers that do not use this library) will
;; remain invisible. org-fold-core provides functions to work around
;; this issue: `org-fold-core-remove-optimisation' and `org-fold-core-update-optimisation', but
;; it is unlikely that a random external package will use them.
;; Another possible bottleneck is the fragility check after the change
;; related to the folded text. The functions used in `:fragile'
;; folding properties must be optimized. Also,
;; `org-fold-core-ignore-fragility-checks' or even `org-fold-core-ignore-modifications' may be
;; used when appropriate in the performance-critical code. When
;; inserting text from within `org-fold-core-ignore-modifications' macro, it is
;; recommended to use `insert-and-inherit' instead of `insert' and
;; `insert-before-markers-and-inherit' instead of
;; `insert-before-markers' to avoid revealing inserted text in the
;; middle of a folded region.
;; Performance of isearch is currently limited by Emacs isearch
;; implementation. For now, Emacs isearch only supports searching
;; through text hidden using overlays. This library handles isearch
;; by converting folds with matching text to overlays, which may
;; affect performance in case of large number of matches. In the
;; future, Emacs will hopefully accept the relevant patch allowing
;; isearch to work with text hidden via text properties, but the
;; performance hit has to be accepted meanwhile.
;;; Code:
(require 'org-macs)
(org-assert-version)
(require 'org-macs)
(require 'org-compat)
(declare-function isearch-filter-visible "isearch" (beg end))
;;; Customization
(defcustom org-fold-core-style (if (version< emacs-version "29")
'text-properties
'overlays)
"Internal implementation detail used to hide folded text.
Can be either `text-properties' or `overlays'.
The former is faster on large files in Emacs <29, while the latter is
generally less error-prone with regard to third-party packages.
Important: This variable must be set before loading Org."
:group 'org
:package-version '(Org . "9.7")
:type '(choice
(const :tag "Overlays" overlays)
(const :tag "Text properties" text-properties)))
(defvar-local org-fold-core-isearch-open-function #'org-fold-core--isearch-reveal
"Function used to reveal hidden text found by isearch.
The function is called with a single argument - point where text is to
be revealed.")
(defvar-local org-fold-core--optimise-for-huge-buffers nil
"Non-nil turns on extra speedup on huge buffers (Mbs of folded text).
This setting is risky and may cause various artifacts and degraded
functionality, especially when using external packages. It is
recommended to enable it on per-buffer basis as file-local variable.
When set to non-nil, must be a list containing one or multiple the
following symbols:
- `grab-invisible': Use `invisible' text property to hide text. This
will reduce the load on Emacs display engine and one may use it if
moving point across folded regions becomes slow. However, as a side
effect, some external packages extracting i.e. headlings from folded
parts of buffer may keep the text invisible.
- `ignore-fragility-checks': Do not try to detect when user edits
break structure of the folded elements. This will speed up
modifying the folded regions at the cost that some higher-level
functions relying on this package might not be able to unfold the
edited text. For example, removed leading stars from a folded
headline in Org mode will break visibility cycling since Org mode
will not be aware that the following folded text belonged to
headline.
- `ignore-modification-checks': Do not try to detect insertions in the
middle of the folded regions. This will speed up non-interactive
edits of the folded regions. However, text inserted in the middle
of the folded regions may become visible for some external packages
inserting text using `insert' instead of `insert-and-inherit' (the
latter is rarely used in practice).
- `ignore-indirect': Do not decouple folding state in the indirect
buffers. This can speed up Emacs display engine (and thus motion of
point), especially when large number of indirect buffers is being
used.
- `merge-folds': Do not distinguish between different types of folding
specs. This is the most aggressive optimization with unforeseen and
potentially drastic effects.")
(put 'org-fold-core--optimise-for-huge-buffers 'safe-local-variable 'listp)
;;; Core functionality
;;;; Folding specs
(defvar-local org-fold-core--specs '((org-fold-visible
(:visible . t)
(:alias . (visible)))
(org-fold-hidden
(:ellipsis . "...")
(:isearch-open . t)
(:alias . (hidden))))
"Folding specs defined in current buffer.
Each spec is a list (SPEC-SYMBOL SPEC-PROPERTIES).
SPEC-SYMBOL is the symbol representing the folding spec.
SPEC-PROPERTIES is an alist defining folding spec properties.
If a text region is folded using multiple specs, only the folding spec
listed earlier is used.
The following properties are known:
- :ellipsis :: must be nil or string to show when text is folded
using this spec.
- :global :: non-nil means that folding state will be preserved
when copying folded text between buffers.
- :isearch-ignore :: non-nil means that folded text is not searchable
using isearch.
- :isearch-open :: non-nil means that isearch can reveal text hidden
using this spec. This property does nothing
when `isearch-ignore' property is non-nil.
- :front-sticky :: non-nil means that text prepended to the folded text
is automatically folded.
- :rear-sticky :: non-nil means that text appended to the folded text
is folded.
- :visible :: non-nil means that folding spec visibility is not
managed. Instead, visibility settings in
`buffer-invisibility-spec' will be used as is.
Note that changing this property from nil to t may
clear the setting in `buffer-invisibility-spec'.
- :font-lock :: non-nil means that newlines after the fold should
be re-fontified upon folding/unfolding. See
`org-activate-folds'.
- :alias :: a list of aliases for the SPEC-SYMBOL.
- :fragile :: Must be a function accepting two arguments.
Non-nil means that changes in region may cause
the region to be revealed. The region is
revealed after changes if the function returns
non-nil.
The function called after changes are made with
two arguments: cons (beg . end) representing the
folded region and spec symbol.")
(defvar-local org-fold-core--spec-symbols nil
"Alist holding buffer spec symbols and aliases.
This variable is defined to reduce load on Emacs garbage collector
reducing the number of transiently allocated variables.")
(defvar-local org-fold-core--spec-list nil
"List holding buffer spec symbols, but not aliases.
This variable is defined to reduce load on Emacs garbage collector
reducing the number of transiently allocated variables.")
(defvar-local org-fold-core-extend-changed-region-functions nil
"Special hook run just before handling changes in buffer.
This is used to account changes outside folded regions that still
affect the folded region visibility. For example, removing all stars
at the beginning of a folded Org mode heading should trigger the
folded text to be revealed. Each function is called with two
arguments: beginning and the end of the changed region.")
;;; Utility functions
(defsubst org-fold-core-folding-spec-list (&optional buffer)
"Return list of all the folding spec symbols in BUFFER."
(or (buffer-local-value 'org-fold-core--spec-list (or buffer (current-buffer)))
(with-current-buffer (or buffer (current-buffer))
(setq org-fold-core--spec-list (mapcar #'car org-fold-core--specs)))))
(defun org-fold-core-get-folding-spec-from-alias (spec-or-alias)
"Return the folding spec symbol for SPEC-OR-ALIAS.
Return nil when there is no matching folding spec."
(when spec-or-alias
(unless org-fold-core--spec-symbols
(dolist (spec (org-fold-core-folding-spec-list))
(push (cons spec spec) org-fold-core--spec-symbols)
(dolist (alias (cdr (assq :alias (assq spec org-fold-core--specs))))
(push (cons alias spec) org-fold-core--spec-symbols))))
(alist-get spec-or-alias org-fold-core--spec-symbols)))
(defsubst org-fold-core-folding-spec-p (spec-or-alias)
"Check if SPEC-OR-ALIAS is a registered folding spec."
(org-fold-core-get-folding-spec-from-alias spec-or-alias))
(defsubst org-fold-core--check-spec (spec-or-alias)
"Throw an error if SPEC-OR-ALIAS is not in `org-fold-core-folding-spec-list'."
(unless (org-fold-core-folding-spec-p spec-or-alias)
(error "%s is not a valid folding spec" spec-or-alias)))
(defsubst org-fold-core-get-folding-spec-property (spec-or-alias property)
"Get PROPERTY of a folding SPEC-OR-ALIAS.
Possible properties can be found in `org-fold-core--specs' docstring."
(org-fold-core--check-spec spec-or-alias)
(if (and (memql 'ignore-indirect org-fold-core--optimise-for-huge-buffers)
(eq property :global))
t
(if (and (memql 'merge-folds org-fold-core--optimise-for-huge-buffers)
(eq property :visible))
nil
(cdr (assq property (assq (org-fold-core-get-folding-spec-from-alias spec-or-alias) org-fold-core--specs))))))
(defconst org-fold-core--spec-property-prefix "org-fold--spec-"
"Prefix used to create property symbol.")
(defsubst org-fold-core-get-folding-property-symbol (spec &optional buffer global)
"Get folding text property using to store SPEC in current buffer or BUFFER.
If GLOBAL is non-nil, do not make the property unique in the BUFFER."
(if (memql 'merge-folds org-fold-core--optimise-for-huge-buffers)
(intern (format "%s-global" org-fold-core--spec-property-prefix))
(intern (format (concat org-fold-core--spec-property-prefix "%s-%S")
(symbol-name spec)
;; (sxhash buf) appears to be not constant over time.
;; Using buffer-name is safe, since the only place where
;; buffer-local text property actually matters is an indirect
;; buffer, where the name cannot be same anyway.
(if (or global
(memql 'ignore-indirect org-fold-core--optimise-for-huge-buffers))
'global
(sxhash (buffer-name (or buffer (current-buffer)))))))))
(defsubst org-fold-core-get-folding-spec-from-folding-prop (folding-prop)
"Return folding spec symbol used for folding property with name FOLDING-PROP."
(catch :exit
(dolist (spec (org-fold-core-folding-spec-list))
;; We know that folding properties have
;; folding spec in their name.
(when (string-match-p (symbol-name spec)
(symbol-name folding-prop))
(throw :exit spec)))))
(defvar org-fold-core--property-symbol-cache (make-hash-table :test 'equal)
"Saved values of folding properties for (buffer . spec) conses.")
(defvar-local org-fold-core--indirect-buffers nil
"List of indirect buffers created from current buffer.
The first element of the list is always the current buffer.
This variable is needed to work around Emacs bug#46982, while Emacs
does not provide a way `after-change-functions' in any other buffer
than the buffer where the change was actually made.")
(defmacro org-fold-core-cycle-over-indirect-buffers (&rest body)
"Execute BODY in current buffer and all its indirect buffers.
Also, make sure that folding properties from killed buffers are not
hanging around."
(declare (debug (form body)) (indent 0))
`(let (buffers dead-properties)
(if (and (not (buffer-base-buffer))
(not (memq (current-buffer) org-fold-core--indirect-buffers)))
;; We are in base buffer with `org-fold-core--indirect-buffers' value from
;; different buffer. This can happen, for example, when
;; org-capture copies local variables into *Capture* buffer.
(setq buffers (list (current-buffer)))
(let ((all-buffers (buffer-local-value
'org-fold-core--indirect-buffers
(or (buffer-base-buffer) (current-buffer)))))
(dolist (buf (cons (or (buffer-base-buffer) (current-buffer))
(buffer-local-value 'org-fold-core--indirect-buffers (or (buffer-base-buffer) (current-buffer)))))
(if (buffer-live-p buf)
(push buf buffers)
(dolist (spec (org-fold-core-folding-spec-list))
(when (and (not (org-fold-core-get-folding-spec-property spec :global))
(gethash (cons buf spec) org-fold-core--property-symbol-cache))
;; Make sure that dead-properties variable can be passed
;; as argument to `remove-text-properties'.
(push t dead-properties)
(push (gethash (cons buf spec) org-fold-core--property-symbol-cache)
dead-properties)))))
(when dead-properties
(with-current-buffer (or (buffer-base-buffer) (current-buffer))
(setq-local org-fold-core--indirect-buffers
(seq-filter #'buffer-live-p all-buffers))))))
(dolist (buf buffers)
(with-current-buffer buf
(when dead-properties
(with-silent-modifications
(save-restriction
(widen)
(remove-text-properties
(point-min) (point-max)
dead-properties))))
,@body))))
;; This is the core function used to fold text in buffers. We use
;; text properties to hide folded text, however 'invisible property is
;; not directly used (unless risky `org-fold-core--optimise-for-huge-buffers' is
;; enabled). Instead, we define unique text property (folding
;; property) for every possible folding spec and add the resulting
;; text properties into `char-property-alias-alist', so that
;; 'invisible text property is automatically defined if any of the
;; folding properties is non-nil. This approach lets us maintain
;; multiple folds for the same text region - poor man's overlays (but
;; much faster). Additionally, folding properties are ensured to be
;; unique for different buffers (especially for indirect
;; buffers). This is done to allow different folding states in
;; indirect buffers.
(defun org-fold-core--property-symbol-get-create (spec &optional buffer return-only)
"Return a unique symbol suitable as folding text property.
Return value is unique for folding SPEC in BUFFER.
If the buffer already have buffer-local setup in `char-property-alias-alist'
and the setup appears to be created for different buffer,
copy the old invisibility state into new buffer-local text properties,
unless RETURN-ONLY is non-nil."
(if (eq org-fold-core-style 'overlays)
(or (gethash (cons 'global spec) org-fold-core--property-symbol-cache)
(puthash (cons 'global spec)
(org-fold-core-get-folding-property-symbol spec nil 'global)
org-fold-core--property-symbol-cache))
(let* ((buf (or buffer (current-buffer))))
;; Create unique property symbol for SPEC in BUFFER
(let ((local-prop (or (gethash (cons buf spec) org-fold-core--property-symbol-cache)
(puthash (cons buf spec)
(org-fold-core-get-folding-property-symbol
spec buf
(org-fold-core-get-folding-spec-property spec :global))
org-fold-core--property-symbol-cache))))
(prog1
local-prop
(unless return-only
(with-current-buffer buf
;; Update folding properties carried over from other
;; buffer (implying that current buffer is indirect
;; buffer). Normally, `char-property-alias-alist' in new
;; indirect buffer is a copy of the same variable from
;; the base buffer. Then, `char-property-alias-alist'
;; would contain folding properties, which are not
;; matching the generated `local-prop'.
(unless (member local-prop (cdr (assq 'invisible char-property-alias-alist)))
;; Copy all the old folding properties to preserve the folding state
(with-silent-modifications
(dolist (old-prop (cdr (assq 'invisible char-property-alias-alist)))
(org-with-wide-buffer
(let* ((pos (point-min))
(spec (org-fold-core-get-folding-spec-from-folding-prop old-prop))
;; Generate new buffer-unique folding property
(new-prop (when spec (org-fold-core--property-symbol-get-create spec nil 'return-only))))
;; Copy the visibility state for `spec' from `old-prop' to `new-prop'
(unless (eq old-prop new-prop)
(while (< pos (point-max))
(let ((val (get-text-property pos old-prop))
(next (next-single-char-property-change pos old-prop)))
(when val
(put-text-property pos next new-prop val))
(setq pos next)))))))
;; Update `char-property-alias-alist' with folding
;; properties unique for the current buffer.
(setq-local char-property-alias-alist
(cons (cons 'invisible
(mapcar (lambda (spec)
(org-fold-core--property-symbol-get-create spec nil 'return-only))
(org-fold-core-folding-spec-list)))
(remove (assq 'invisible char-property-alias-alist)
char-property-alias-alist)))
;; Set folding property stickiness according to
;; their `:font-sticky' and `:rear-sticky'
;; parameters.
(let (full-prop-list)
(org-fold-core-cycle-over-indirect-buffers
(setq full-prop-list
(append full-prop-list
(delq nil
(mapcar (lambda (spec)
(cond
((org-fold-core-get-folding-spec-property spec :front-sticky)
(cons (org-fold-core--property-symbol-get-create spec nil 'return-only)
nil))
((org-fold-core-get-folding-spec-property spec :rear-sticky)
nil)
(t
(cons (org-fold-core--property-symbol-get-create spec nil 'return-only)
t))))
(org-fold-core-folding-spec-list))))))
(org-fold-core-cycle-over-indirect-buffers
(setq-local text-property-default-nonsticky
(delete-dups (append
text-property-default-nonsticky
full-prop-list))))))))))))))
(defun org-fold-core--update-buffer-folds ()
"Copy folding state in a new buffer with text copied from old buffer."
(org-fold-core--property-symbol-get-create (car (org-fold-core-folding-spec-list))))
(defun org-fold-core-decouple-indirect-buffer-folds ()
"Copy and decouple folding state in a newly created indirect buffer.
This function is mostly intended to be used in
`clone-indirect-buffer-hook'."
;; Add current buffer to the list of indirect buffers in the base buffer.
(when (buffer-base-buffer)
(let ((new-buffer (current-buffer)))
(with-current-buffer (buffer-base-buffer)
(setq-local org-fold-core--indirect-buffers
(let (bufs)
(org-fold-core-cycle-over-indirect-buffers
(push (current-buffer) bufs))
(push new-buffer bufs)
(delete-dups bufs))))))
(when (and (buffer-base-buffer)
(eq org-fold-core-style 'text-properties)
(not (memql 'ignore-indirect org-fold-core--optimise-for-huge-buffers)))
(org-fold-core--update-buffer-folds)))
;;; API
;;;; Modifying folding specs
(defun org-fold-core-set-folding-spec-property (spec property value &optional force)
"Set PROPERTY of a folding SPEC to VALUE.
Possible properties and values can be found in `org-fold-core--specs' docstring.
Do not check previous value when FORCE is non-nil."
(pcase property
(:ellipsis
(unless (and (not force) (equal value (org-fold-core-get-folding-spec-property spec :ellipsis)))
(remove-from-invisibility-spec (cons spec (org-fold-core-get-folding-spec-property spec :ellipsis)))
(unless (org-fold-core-get-folding-spec-property spec :visible)
(add-to-invisibility-spec (cons spec value)))))
(:visible
(unless (or (memql 'merge-folds org-fold-core--optimise-for-huge-buffers)
(and (not force) (equal value (org-fold-core-get-folding-spec-property spec :visible))))
(if value
(remove-from-invisibility-spec (cons spec (org-fold-core-get-folding-spec-property spec :ellipsis)))
(add-to-invisibility-spec (cons spec (org-fold-core-get-folding-spec-property spec :ellipsis))))))
(:alias
;; Clear symbol cache.
(setq org-fold-core--spec-symbols nil))
(:isearch-open nil)
(:isearch-ignore nil)
(:front-sticky nil)
(:rear-sticky nil)
(_ nil))
(setf (cdr (assq property (assq spec org-fold-core--specs))) value))
(defun org-fold-core-add-folding-spec (spec &optional properties buffer append)
"Add a new folding SPEC with PROPERTIES in BUFFER.
SPEC must be a symbol. BUFFER can be a buffer to set SPEC in or nil to
set SPEC in current buffer.
By default, the added SPEC will have highest priority among the
previously defined specs. When optional APPEND argument is non-nil,
SPEC will have the lowest priority instead. If SPEC was already
defined earlier, it will be redefined according to provided optional
arguments.
`
The folding spec properties will be set to PROPERTIES (see
`org-fold-core--specs' for details)."
(when (eq spec 'all) (error "Cannot use reserved folding spec symbol 'all"))
(with-current-buffer (or buffer (current-buffer))
;; Clear the cache.
(setq org-fold-core--spec-list nil
org-fold-core--spec-symbols nil)
(let* ((full-properties (mapcar (lambda (prop) (cons prop (cdr (assq prop properties))))
'( :visible :ellipsis :isearch-ignore
:global :isearch-open :front-sticky
:rear-sticky :fragile :alias :font-lock)))
(full-spec (cons spec full-properties)))
(add-to-list 'org-fold-core--specs full-spec append)
(mapc (lambda (prop-cons) (org-fold-core-set-folding-spec-property spec (car prop-cons) (cdr prop-cons) 'force)) full-properties)
;; Update buffer inivisibility specs.
(org-fold-core--property-symbol-get-create spec))))
(defun org-fold-core-remove-folding-spec (spec &optional buffer)
"Remove a folding SPEC in BUFFER.
SPEC must be a symbol.
BUFFER can be a buffer to remove SPEC in, nil to remove SPEC in current
buffer, or `all' to remove SPEC in all open `org-mode' buffers and all
future org buffers."
(org-fold-core--check-spec spec)
(when (eq buffer 'all)
(setq-default org-fold-core--specs (delete (cdr (assq spec org-fold-core--specs)) org-fold-core--specs))
(mapc (lambda (buf)
(org-fold-core-remove-folding-spec spec buf))
(buffer-list)))
(let ((buffer (or buffer (current-buffer))))
(with-current-buffer buffer
;; Clear the cache.
(setq org-fold-core--spec-list nil
org-fold-core--spec-symbols nil)
(org-fold-core-set-folding-spec-property spec :visible t)
(setq org-fold-core--specs (delete (cdr (assq spec org-fold-core--specs)) org-fold-core--specs)))))
(defun org-fold-core-initialize (&optional specs)
"Setup folding in current buffer using SPECS as value of `org-fold-core--specs'."
;; Preserve the priorities.
(when specs (setq specs (nreverse specs)))
(unless specs (setq specs org-fold-core--specs))
(setq org-fold-core--specs nil
org-fold-core--spec-list nil
org-fold-core--spec-symbols nil)
(dolist (spec specs)
(org-fold-core-add-folding-spec (car spec) (cdr spec)))
(add-hook 'after-change-functions 'org-fold-core--fix-folded-region nil 'local)
(add-hook 'clone-indirect-buffer-hook #'org-fold-core-decouple-indirect-buffer-folds nil 'local)
;; Setup killing text
(setq-local filter-buffer-substring-function #'org-fold-core--buffer-substring-filter)
(if (and (boundp 'isearch-opened-regions)
(eq org-fold-core-style 'text-properties))
;; Use new implementation of isearch allowing to search inside text
;; hidden via text properties.
(org-fold-core--isearch-setup 'text-properties)
(org-fold-core--isearch-setup 'overlays)))
;;;; Searching and examining folded text
(defsubst org-fold-core-folded-p (&optional pos spec-or-alias)
"Non-nil if the character after POS is folded.
If POS is nil, use `point' instead.
If SPEC-OR-ALIAS is a folding spec or a list, only check the given
folding spec or the listed specs."
(if (and spec-or-alias (listp spec-or-alias))
(catch :found
(dolist (spec spec-or-alias)
(let ((val (org-fold-core-get-folding-spec spec pos)))
(when val (throw :found val)))))
(org-fold-core-get-folding-spec spec-or-alias pos)))
(defun org-fold-core-region-folded-p (beg end &optional spec-or-alias)
"Non-nil if the region between BEG and END is folded.
If SPEC-OR-ALIAS is a folding spec, only check the given folding spec."
(org-with-point-at beg
(catch :visible
(while (< (point) end)
(unless (org-fold-core-get-folding-spec spec-or-alias) (throw :visible nil))
(goto-char (org-fold-core-next-folding-state-change spec-or-alias nil end)))
t)))
(defun org-fold-core-get-folding-spec (&optional spec-or-alias pom)
"Get folding state at `point' or POM.
Return nil if there is no folding at point or POM.
If SPEC-OR-ALIAS is nil, return a folding spec with highest priority
among present at `point' or POM.
If SPEC-OR-ALIAS is `all', return the list of all present folding
specs.
If SPEC-OR-ALIAS is a valid folding spec or a spec alias, return the
corresponding folding spec (if the text is folded using that spec)."
(let ((spec (if (eq spec-or-alias 'all)
'all
(org-fold-core-get-folding-spec-from-alias spec-or-alias))))
(when (and spec (not (eq spec 'all))) (org-fold-core--check-spec spec))
(org-with-point-at pom
(cond
((or (null spec) (eq spec 'all))
(catch :single-spec
(let ((result))
(dolist (lspec (org-fold-core-folding-spec-list))
(let ((val (if (eq org-fold-core-style 'text-properties)
(get-text-property (point) (org-fold-core--property-symbol-get-create lspec nil t))
(get-char-property (point) (org-fold-core--property-symbol-get-create lspec nil t)))))
(when (and val (null spec)) (throw :single-spec val))
(when val (push val result))))
(reverse result))))
(t (if (eq org-fold-core-style 'text-properties)
(get-text-property (point) (org-fold-core--property-symbol-get-create spec nil t))
(get-char-property (point) (org-fold-core--property-symbol-get-create spec nil t))))))))
(defun org-fold-core-get-folding-specs-in-region (beg end)
"Get all folding specs in region from BEG to END."
(let ((pos beg)
all-specs)
(while (< pos end)
(setq all-specs (append all-specs (org-fold-core-get-folding-spec nil pos)))
(setq pos (org-fold-core-next-folding-state-change nil pos end)))
(unless (listp all-specs) (setq all-specs (list all-specs)))
(delete-dups all-specs)))
(defun org-fold-core-get-region-at-point (&optional spec-or-alias pom)
"Return region folded using SPEC-OR-ALIAS at POM.
If SPEC is nil, return the largest possible folded region.
The return value is a cons of beginning and the end of the region.
Return nil when no fold is present at point of POM."
(let ((spec (org-fold-core-get-folding-spec-from-alias spec-or-alias)))
(org-with-point-at (or pom (point))
(if spec
(if (eq org-fold-core-style 'text-properties)
(org-find-text-property-region (point) (org-fold-core--property-symbol-get-create spec nil t))
(let ((ov (cdr (get-char-property-and-overlay (point) (org-fold-core--property-symbol-get-create spec nil t)))))
(when ov (cons (overlay-start ov) (overlay-end ov)))))
(let ((region (cons (point) (point))))
(dolist (spec (org-fold-core-get-folding-spec 'all))
(let ((local-region (org-fold-core-get-region-at-point spec)))
(when (< (car local-region) (car region))
(setcar region (car local-region)))
(when (> (cdr local-region) (cdr region))
(setcdr region (cdr local-region)))))
(unless (eq (car region) (cdr region)) region))))))
(defun org-fold-core-next-visibility-change (&optional pos limit ignore-hidden-p previous-p)
"Return next point from POS up to LIMIT where text becomes visible/invisible.
By default, text hidden by any means (i.e. not only by folding, but
also via fontification) will be considered.
If IGNORE-HIDDEN-P is non-nil, consider only folded text.
If PREVIOUS-P is non-nil, search backwards."
(let* ((pos (or pos (point)))
(invisible-p (if ignore-hidden-p
#'org-fold-core-folded-p
#'invisible-p))
(invisible-initially? (funcall invisible-p pos))
(limit (or limit (if previous-p
(point-min)
(point-max))))
(cmp (if previous-p #'> #'<))
(next-change (if previous-p
(if ignore-hidden-p
(lambda (p) (org-fold-core-previous-folding-state-change (org-fold-core-get-folding-spec nil p) p limit))
(lambda (p) (max limit (previous-single-char-property-change p 'invisible nil limit))))
(if ignore-hidden-p
(lambda (p) (org-fold-core-next-folding-state-change (org-fold-core-get-folding-spec nil p) p limit))
(lambda (p) (next-single-char-property-change p 'invisible nil limit)))))
(next pos))
(while (and (funcall cmp next limit)
(not (org-xor
invisible-initially?
(funcall invisible-p
(if previous-p
;; NEXT-1 -> NEXT is the change.
(max limit (1- next))
;; NEXT -> NEXT+1 is the change.
next)))))
(setq next (funcall next-change next)))
next))
(defun org-fold-core-previous-visibility-change (&optional pos limit ignore-hidden-p)
"Call `org-fold-core-next-visibility-change' searching backwards."
(org-fold-core-next-visibility-change pos limit ignore-hidden-p 'previous))
(defun org-fold-core-next-folding-state-change (&optional spec-or-alias pos limit previous-p)
"Return point after POS where folding state changes up to LIMIT.
If SPEC-OR-ALIAS is nil, return next point where _any_ single folding
spec changes.
For example, (org-fold-core-next-folding-state-change nil) with point
somewhere in the below structure will return the nearest <...> point.
* Headline <begin outline fold>
:PROPERTIES:<begin drawer fold>
:ID: test
:END:<end drawer fold>
Fusce suscipit, wisi nec facilisis facilisis, est dui fermentum leo,
quis tempor ligula erat quis odio.
** Another headline
:DRAWER:<begin drawer fold>
:END:<end drawer fold>
** Yet another headline
<end of outline fold>
If SPEC-OR-ALIAS is a folding spec symbol, only consider that folding
spec.
If SPEC-OR-ALIAS is a list, only consider changes of folding specs
from the list.
Search backwards when PREVIOUS-P is non-nil."
(when (and spec-or-alias (symbolp spec-or-alias))
(setq spec-or-alias (list spec-or-alias)))
(when spec-or-alias
(setq spec-or-alias
(mapcar (lambda (spec-or-alias)
(or (org-fold-core-get-folding-spec-from-alias spec-or-alias)
spec-or-alias))
spec-or-alias))
(mapc #'org-fold-core--check-spec spec-or-alias))
(unless spec-or-alias
(setq spec-or-alias (org-fold-core-folding-spec-list)))
(setq pos (or pos (point)))
(let ((limit (or limit (if previous-p (point-min) (point-max)))))
(catch :limit
(dolist (prop (mapcar
(lambda (el)
(org-fold-core--property-symbol-get-create el nil t))
spec-or-alias))
(when (= limit pos) (throw :limit limit))
(setq
limit
(if previous-p
(previous-single-char-property-change pos prop nil limit)
(next-single-char-property-change pos prop nil limit))))
limit)))
(defun org-fold-core-previous-folding-state-change (&optional spec-or-alias pos limit)
"Call `org-fold-core-next-folding-state-change' searching backwards."
(org-fold-core-next-folding-state-change spec-or-alias pos limit 'previous))
(defun org-fold-core-search-forward (spec-or-alias &optional limit)
"Search next region folded via folding SPEC-OR-ALIAS up to LIMIT.
Move point right after the end of the region, to LIMIT, or
`point-max'. The `match-data' will contain the region."
(let ((spec (org-fold-core-get-folding-spec-from-alias spec-or-alias)))
(let ((prop-symbol (org-fold-core--property-symbol-get-create spec nil t)))
(goto-char (or (next-single-char-property-change (point) prop-symbol nil limit) limit (point-max)))
(when (and (< (point) (or limit (point-max)))
(not (org-fold-core-get-folding-spec spec)))
(goto-char (next-single-char-property-change (point) prop-symbol nil limit)))
(when (org-fold-core-get-folding-spec spec)
(let ((region (org-fold-core-get-region-at-point spec)))
(when (< (cdr region) (or limit (point-max)))
(goto-char (1+ (cdr region)))
(set-match-data (list (set-marker (make-marker) (car region) (current-buffer))
(set-marker (make-marker) (cdr region) (current-buffer))))))))))
(cl-defun org-fold-core-get-regions (&key specs from to with-markers relative)
"Find all the folded regions in current buffer.
Each element of the returned list represent folded region boundaries
and the folding spec: (BEG END SPEC).
Search folds intersecting with (FROM TO) buffer region if FROM and TO
are provided.
If FROM is non-nil and TO is nil, search the folded regions at FROM.
When both FROM and TO are nil, search folded regions in the whole buffer.
When SPECS is non-nil it should be a list of folding specs or a symbol.
Only return the matching fold types.
When WITH-MARKERS is non-nil, use markers to represent region
boundaries.
When RELATIVE is a buffer position, regions boundaries are given
relative to that position.
When RELATIVE is t, use FROM as the position.
WITH-MARKERS must be nil when RELATIVE is non-nil."
(when (and relative with-markers)
(error "Cannot use markers in non-absolute region boundaries"))
(when (eq relative t) (setq relative from))
(unless (listp specs) (setq specs (list specs)))
(let (regions region mk-region)
(org-with-wide-buffer
(when (and (not from) (not to))
(setq from (point-min)
to (point-max)))
(when (and from (not to)) (setq to (point-max)))
(when (and from (< from (point-min))) (setq from (point-min)))
(when (and to (> to (point-max))) (setq to (point-max)))
(unless from (setq from (point-min)))
(dolist (spec (or specs (org-fold-core-folding-spec-list)) regions)
(goto-char from)
(catch :exit
(while (or (not to) (< (point) to))
(when (org-fold-core-get-folding-spec spec)
(setq region (org-fold-core-get-region-at-point spec))
(when relative