dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

org-fold-core.el (77802B)


      1 ;;; org-fold-core.el --- Folding buffer text -*- lexical-binding: t; -*-
      2 ;;
      3 ;; Copyright (C) 2020-2023 Free Software Foundation, Inc.
      4 ;;
      5 ;; Author: Ihor Radchenko <yantar92 at gmail dot com>
      6 ;; Keywords: folding, invisible text
      7 ;; URL: https://orgmode.org
      8 ;;
      9 ;; This file is part of GNU Emacs.
     10 ;;
     11 ;; GNU Emacs is free software: you can redistribute it and/or modify
     12 ;; it under the terms of the GNU General Public License as published by
     13 ;; the Free Software Foundation, either version 3 of the License, or
     14 ;; (at your option) any later version.
     15 
     16 ;; GNU Emacs is distributed in the hope that it will be useful,
     17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19 ;; GNU General Public License for more details.
     20 
     21 ;; You should have received a copy of the GNU General Public License
     22 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     24 ;;
     25 ;;; Commentary:
     26 
     27 ;; This file contains library to control temporary invisibility
     28 ;; (folding and unfolding) of text in buffers.
     29 
     30 ;; The file implements the following functionality:
     31 ;;
     32 ;; - Folding/unfolding regions of text
     33 ;; - Searching and examining boundaries of folded text
     34 ;; - Interactive searching in folded text (via isearch)
     35 ;; - Handling edits in folded text
     36 ;; - Killing/yanking (copying/pasting) of the folded text
     37 
     38 ;; To setup folding in an arbitrary buffer, one must call
     39 ;; `org-fold-core-initialize', optionally providing the list of folding specs to be
     40 ;; used in the buffer.  The specs can be added, removed, or
     41 ;; re-configured later.  Read below for more details.
     42 
     43 ;;; Folding/unfolding regions of text
     44 
     45 ;; User can temporarily hide/reveal (fold/unfold) arbitrary regions or
     46 ;; text.  The folds can be nested.
     47 
     48 ;; Internally, nested folds are marked with different folding specs
     49 ;; Overlapping folds marked with the same folding spec are
     50 ;; automatically merged, while folds with different folding specs can
     51 ;; coexist and be folded/unfolded independently.
     52 
     53 ;; When multiple folding specs are applied to the same region of text,
     54 ;; text visibility is decided according to the folding spec with
     55 ;; topmost priority.
     56 
     57 ;; By default, we define two types of folding specs:
     58 ;; - 'org-fold-visible :: the folded text is not hidden
     59 ;; - 'org-fold-hidden  :: the folded text is completely hidden
     60 ;;
     61 ;; The 'org-fold-visible spec has highest priority allowing parts of
     62 ;; text folded with 'org-fold-hidden to be shown unconditionally.
     63 
     64 ;; Consider the following Org mode link:
     65 ;; [[file:/path/to/file/file.ext][description]]
     66 ;; Only the word "description" is normally visible in this link.
     67 ;;
     68 ;; The way this partial visibility is achieved is combining the two
     69 ;; folding specs.  The whole link is folded using 'org-fold-hidden
     70 ;; folding spec, but the visible part is additionally folded using
     71 ;; 'org-fold-visible:
     72 ;;
     73 ;; <begin org-fold-hidden>[[file:/path/to/file/file.ext][<begin org-fold-visible>description<end org-fold-visible>]]<end org-fold-hidden>
     74 ;;
     75 ;; Because 'org-fold-visible has higher priority than
     76 ;; 'org-fold-hidden, it suppresses the 'org-fold-hidden effect and
     77 ;; thus reveals the description part of the link.
     78 
     79 ;; Similar to 'org-fold-visible, display of any arbitrary folding spec
     80 ;; can be configured using folding spec properties.  In particular,
     81 ;; `:visible' folding spec property controls whether the folded text
     82 ;; is visible or not.  If the `:visible' folding spec property is nil,
     83 ;; folded text is hidden or displayed as a constant string (ellipsis)
     84 ;; according to the value of `:ellipsis' folding spec property.  See
     85 ;; docstring of `org-fold-core--specs' for the description of all the available
     86 ;; folding spec properties.
     87 
     88 ;; Folding spec properties of any valid folding spec can be changed
     89 ;; any time using `org-fold-core-set-folding-spec-property'.
     90 
     91 ;; If necessary, one can add or remove folding specs using
     92 ;; `org-fold-core-add-folding-spec' and `org-fold-core-remove-folding-spec'.
     93 
     94 ;; If a buffer initialized with `org-fold-core-initialize' is cloned into indirect
     95 ;; buffers, it's folding state is copied to that indirect buffer.
     96 ;; The folding states are independent.
     97 
     98 ;; When working with indirect buffers that are handled by this
     99 ;; library, one has to keep in mind that folding state is preserved on
    100 ;; copy when using non-interactive functions.  Moreover, the folding
    101 ;; states of all the indirect buffers will be copied together.
    102 ;;
    103 ;; Example of the implications:
    104 ;; Consider a base buffer and indirect buffer with the following state:
    105 ;; ----- base buffer --------
    106 ;; * Heading<begin fold>
    107 ;; Some text folded in the base buffer, but unfolded in the indirect buffer<end fold>
    108 ;; * Other heading
    109 ;; Heading unfolded in both the buffers.
    110 ;; ---------------------------
    111 ;; ------ indirect buffer ----
    112 ;; * Heading
    113 ;; Some text folded in the base buffer, but unfolded in the indirect buffer
    114 ;; * Other heading
    115 ;; Heading unfolded in both the buffers.
    116 ;; ----------------------------
    117 ;; If some Elisp code copies the whole "Heading" from the indirect
    118 ;; buffer with `buffer-substring' or match data and inserts it into
    119 ;; the base buffer, the inserted heading will be folded since the
    120 ;; internal setting for the folding state is shared between the base
    121 ;; and indirect buffers.  It's just that the indirect buffer ignores
    122 ;; the base buffer folding settings.  However, as soon as the text is
    123 ;; copied back to the base buffer, the folding state will become
    124 ;; respected again.
    125 
    126 ;; If the described situation is undesired, Elisp code can use
    127 ;; `filter-buffer-substring' instead of `buffer-substring'.  All the
    128 ;; folding states that do not belong to the currently active buffer
    129 ;; will be cleared in the copied text then.  See
    130 ;; `org-fold-core--buffer-substring-filter' for more details.
    131 
    132 ;; Because of details of implementation of the folding, it is also not
    133 ;; recommended to set text visibility in buffer directly by setting
    134 ;; `invisible' text property to anything other than t.  While this
    135 ;; should usually work just fine, normal folding can be broken if one
    136 ;; sets `invisible' text property to a value not listed in
    137 ;; `buffer-invisibility-spec'.
    138 
    139 ;;; Searching and examining boundaries of folded text
    140 
    141 ;; It is possible to examine folding specs (there may be several) of
    142 ;; text at point or search for regions with the same folding spec.
    143 ;; See functions defined under ";;;; Searching and examining folded
    144 ;; text" below for details.
    145 
    146 ;; All the folding specs can be specified by symbol representing their
    147 ;; name.  However, this is not always convenient, especially if the
    148 ;; same spec can be used for fold different syntactical structures.
    149 ;; Any folding spec can be additionally referenced by a symbol listed
    150 ;; in the spec's `:alias' folding spec property.  For example, Org
    151 ;; mode's `org-fold-outline' folding spec can be referenced as any
    152 ;; symbol from the following list: '(headline heading outline
    153 ;; inlinetask plain-list) The list is the value of the spec's `:alias'
    154 ;; property.
    155 
    156 ;; Most of the functions defined below that require a folding spec
    157 ;; symbol as their argument, can also accept any symbol from the
    158 ;; `:alias' spec property to reference that folding spec.
    159 
    160 ;; If one wants to search invisible text without using the provided
    161 ;; functions, it is important to keep in mind that 'invisible text
    162 ;; property may have multiple possible values (not just nil and
    163 ;; t). Hence, (next-single-char-property-change pos 'invisible) is not
    164 ;; guaranteed to return the boundary of invisible/visible text.
    165 
    166 ;;; Interactive searching inside folded text (via isearch)
    167 
    168 ;; The library provides a way to control if the folded text can be
    169 ;; searchable using isearch.  If the text is searchable, it is also
    170 ;; possible to control to unfold it temporarily during interactive
    171 ;; isearch session.
    172 
    173 ;; The isearch behavior is controlled on per-folding-spec basis by
    174 ;; setting `isearch-open' and `isearch-ignore' folding spec
    175 ;; properties.  The the docstring of `org-fold-core--specs' for more details.
    176 
    177 ;;; Handling edits inside folded text
    178 
    179 ;; The visibility of the text inserted in front, rear, or in the
    180 ;; middle of a folded region is managed according to `:front-sticky'
    181 ;; and `:rear-sticky' folding properties of the corresponding folding
    182 ;; spec.  The rules are the same with stickiness of text properties in
    183 ;; Elisp.
    184 
    185 ;; If a text being inserted into the buffer is already folded and
    186 ;; invisible (before applying the stickiness rules), then it is
    187 ;; revealed.  This behavior can be changed by wrapping the insertion
    188 ;; code into `org-fold-core-ignore-modifications' macro.  The macro will disable
    189 ;; all the processing related to buffer modifications.
    190 
    191 ;; The library also provides a way to unfold the text after some
    192 ;; destructive changes breaking syntactical structure of the buffer.
    193 ;; For example, Org mode automatically reveals folded drawers when the
    194 ;; drawer becomes syntactically incorrect:
    195 ;; ------- before modification -------
    196 ;; :DRAWER:<begin fold>
    197 ;; Some folded text inside drawer
    198 ;; :END:<end fold>
    199 ;; -----------------------------------
    200 ;; If the ":END:" is edited, drawer syntax is not correct anymore and
    201 ;; the folded text is automatically unfolded.
    202 ;; ------- after modification --------
    203 ;; :DRAWER:
    204 ;; Some folded text inside drawer
    205 ;; :EN:
    206 ;; -----------------------------------
    207 
    208 ;; The described automatic unfolding is controlled by `:fragile'
    209 ;; folding spec property.  It's value can be a function checking if
    210 ;; changes inside (or around) the fold should drigger the unfold.  By
    211 ;; default, only changes that directly involve folded regions will
    212 ;; trigger the check.  In addition, `org-fold-core-extend-changed-region-functions'
    213 ;; can be set to extend the checks to all folded regions intersecting
    214 ;; with the region returned by the functions listed in the variable.
    215 
    216 ;; The fragility checks can be bypassed if the code doing
    217 ;; modifications is wrapped into `org-fold-core-ignore-fragility-checks' macro.
    218 
    219 ;;; Performance considerations
    220 
    221 ;; This library is using text properties to hide text.  Text
    222 ;; properties are much faster than overlays, that could be used for
    223 ;; the same purpose.  Overlays are implemented with O(n) complexity in
    224 ;; Emacs (as for 2021-03-11).  It means that any attempt to move
    225 ;; through hidden text in a file with many invisible overlays will
    226 ;; require time scaling with the number of folded regions (the problem
    227 ;; Overlays note of the manual warns about).  For curious, historical
    228 ;; reasons why overlays are not efficient can be found in
    229 ;; https://www.jwz.org/doc/lemacs.html.
    230 
    231 ;; Despite using text properties, the performance is still limited by
    232 ;; Emacs display engine.  For example, >7Mb of text hidden within
    233 ;; visible part of a buffer may cause noticeable lags (which is still
    234 ;; orders of magnitude better in comparison with overlays).  If the
    235 ;; performance issues become critical while using this library, it is
    236 ;; recommended to minimize the number of folding specs used in the
    237 ;; same buffer at a time.
    238 
    239 ;; Alternatively, the library provides `org-fold-core--optimise-for-huge-buffers'
    240 ;; for additional speedup.  This can be used as a file-local variable
    241 ;; in huge buffers.  The variable can be set to enable various levels
    242 ;; of extra optimization.  See the docstring for detailed information.
    243 
    244 ;; It is worth noting that when using `org-fold-core--optimise-for-huge-buffers'
    245 ;; with `grab-invisible' option, folded regions copied to other
    246 ;; buffers (including buffers that do not use this library) will
    247 ;; remain invisible.  org-fold-core provides functions to work around
    248 ;; this issue: `org-fold-core-remove-optimisation' and `org-fold-core-update-optimisation', but
    249 ;; it is unlikely that a random external package will use them.
    250 
    251 ;; Another possible bottleneck is the fragility check after the change
    252 ;; related to the folded text.  The functions used in `:fragile'
    253 ;; folding properties must be optimized.  Also,
    254 ;; `org-fold-core-ignore-fragility-checks' or even `org-fold-core-ignore-modifications' may be
    255 ;; used when appropriate in the performance-critical code.  When
    256 ;; inserting text from within `org-fold-core-ignore-modifications' macro, it is
    257 ;; recommended to use `insert-and-inherit' instead of `insert' and
    258 ;; `insert-before-markers-and-inherit' instead of
    259 ;; `insert-before-markers' to avoid revealing inserted text in the
    260 ;; middle of a folded region.
    261 
    262 ;; Performance of isearch is currently limited by Emacs isearch
    263 ;; implementation.  For now, Emacs isearch only supports searching
    264 ;; through text hidden using overlays.  This library handles isearch
    265 ;; by converting folds with matching text to overlays, which may
    266 ;; affect performance in case of large number of matches.  In the
    267 ;; future, Emacs will hopefully accept the relevant patch allowing
    268 ;; isearch to work with text hidden via text properties, but the
    269 ;; performance hit has to be accepted meanwhile.
    270 
    271 ;;; Code:
    272 
    273 (require 'org-macs)
    274 (org-assert-version)
    275 
    276 (require 'org-macs)
    277 (require 'org-compat)
    278 
    279 (declare-function isearch-filter-visible "isearch" (beg end))
    280 
    281 ;;; Customization
    282 
    283 (defcustom org-fold-core-style 'text-properties
    284   "Internal implementation detail used to hide folded text.
    285 Can be either `text-properties' or `overlays'.
    286 The former is faster on large files, while the latter is generally
    287 less error-prone with regard to third-party packages that haven't yet
    288 adapted to the new folding implementation.
    289 
    290 Important: This variable must be set before loading Org."
    291   :group 'org
    292   :package-version '(Org . "9.6")
    293   :type '(choice
    294           (const :tag "Overlays" overlays)
    295           (const :tag "Text properties" text-properties)))
    296 
    297 (defvar-local org-fold-core-isearch-open-function #'org-fold-core--isearch-reveal
    298   "Function used to reveal hidden text found by isearch.
    299 The function is called with a single argument - point where text is to
    300 be revealed.")
    301 
    302 (defvar-local org-fold-core--optimise-for-huge-buffers nil
    303   "Non-nil turns on extra speedup on huge buffers (Mbs of folded text).
    304 
    305 This setting is risky and may cause various artifacts and degraded
    306 functionality, especially when using external packages.  It is
    307 recommended to enable it on per-buffer basis as file-local variable.
    308 
    309 When set to non-nil, must be a list containing one or multiple the
    310 following symbols:
    311 
    312 - `grab-invisible': Use `invisible' text property to hide text.  This
    313   will reduce the load on Emacs display engine and one may use it if
    314   moving point across folded regions becomes slow.  However, as a side
    315   effect, some external packages extracting i.e. headlings from folded
    316   parts of buffer may keep the text invisible.
    317 
    318 - `ignore-fragility-checks': Do not try to detect when user edits
    319   break structure of the folded elements.  This will speed up
    320   modifying the folded regions at the cost that some higher-level
    321   functions relying on this package might not be able to unfold the
    322   edited text.  For example, removed leading stars from a folded
    323   headline in Org mode will break visibility cycling since Org mode
    324   will not be aware that the following folded text belonged to
    325   headline.
    326 
    327 - `ignore-modification-checks': Do not try to detect insertions in the
    328   middle of the folded regions.  This will speed up non-interactive
    329   edits of the folded regions.  However, text inserted in the middle
    330   of the folded regions may become visible for some external packages
    331   inserting text using `insert' instead of `insert-and-inherit' (the
    332   latter is rarely used in practice).
    333 
    334 - `ignore-indirect': Do not decouple folding state in the indirect
    335   buffers.  This can speed up Emacs display engine (and thus motion of
    336   point), especially when large number of indirect buffers is being
    337   used.
    338 
    339 - `merge-folds': Do not distinguish between different types of folding
    340   specs.  This is the most aggressive optimization with unforeseen and
    341   potentially drastic effects.")
    342 (put 'org-fold-core--optimise-for-huge-buffers 'safe-local-variable 'listp)
    343 
    344 ;;; Core functionality
    345 
    346 ;;;; Folding specs
    347 
    348 (defvar-local org-fold-core--specs '((org-fold-visible
    349 	                 (:visible . t)
    350                          (:alias . (visible)))
    351                         (org-fold-hidden
    352 			 (:ellipsis . "...")
    353                          (:isearch-open . t)
    354                          (:alias . (hidden))))
    355   "Folding specs defined in current buffer.
    356 
    357 Each spec is a list (SPEC-SYMBOL SPEC-PROPERTIES).
    358 SPEC-SYMBOL is the symbol representing the folding spec.
    359 SPEC-PROPERTIES is an alist defining folding spec properties.
    360 
    361 If a text region is folded using multiple specs, only the folding spec
    362 listed earlier is used.
    363 
    364 The following properties are known:
    365 - :ellipsis         :: must be nil or string to show when text is folded
    366                       using this spec.
    367 - :global           :: non-nil means that folding state will be preserved
    368                       when copying folded text between buffers.
    369 - :isearch-ignore   :: non-nil means that folded text is not searchable
    370                       using isearch.
    371 - :isearch-open     :: non-nil means that isearch can reveal text hidden
    372                       using this spec.  This property does nothing
    373                       when `isearch-ignore' property is non-nil.
    374 - :front-sticky     :: non-nil means that text prepended to the folded text
    375                       is automatically folded.
    376 - :rear-sticky      :: non-nil means that text appended to the folded text
    377                       is folded.
    378 - :visible          :: non-nil means that folding spec visibility is not
    379                        managed.  Instead, visibility settings in
    380                        `buffer-invisibility-spec' will be used as is.
    381                        Note that changing this property from nil to t may
    382                        clear the setting in `buffer-invisibility-spec'.
    383 - :alias            :: a list of aliases for the SPEC-SYMBOL.
    384 - :fragile          :: Must be a function accepting two arguments.
    385                        Non-nil means that changes in region may cause
    386                        the region to be revealed.  The region is
    387                        revealed after changes if the function returns
    388                        non-nil.
    389                        The function called after changes are made with
    390                        two arguments: cons (beg . end) representing the
    391                        folded region and spec symbol.")
    392 (defvar-local org-fold-core--spec-symbols nil
    393   "Alist holding buffer spec symbols and aliases.
    394 
    395 This variable is defined to reduce load on Emacs garbage collector
    396 reducing the number of transiently allocated variables.")
    397 (defvar-local org-fold-core--spec-list nil
    398   "List holding buffer spec symbols, but not aliases.
    399 
    400 This variable is defined to reduce load on Emacs garbage collector
    401 reducing the number of transiently allocated variables.")
    402 
    403 (defvar-local org-fold-core-extend-changed-region-functions nil
    404   "Special hook run just before handling changes in buffer.
    405 
    406 This is used to account changes outside folded regions that still
    407 affect the folded region visibility.  For example, removing all stars
    408 at the beginning of a folded Org mode heading should trigger the
    409 folded text to be revealed.  Each function is called with two
    410 arguments: beginning and the end of the changed region.")
    411 
    412 ;;; Utility functions
    413 
    414 (defsubst org-fold-core-folding-spec-list (&optional buffer)
    415   "Return list of all the folding spec symbols in BUFFER."
    416   (or (buffer-local-value 'org-fold-core--spec-list (or buffer (current-buffer)))
    417       (with-current-buffer (or buffer (current-buffer))
    418         (setq org-fold-core--spec-list (mapcar #'car org-fold-core--specs)))))
    419 
    420 (defun org-fold-core-get-folding-spec-from-alias (spec-or-alias)
    421   "Return the folding spec symbol for SPEC-OR-ALIAS.
    422 Return nil when there is no matching folding spec."
    423   (when spec-or-alias
    424     (unless org-fold-core--spec-symbols
    425       (dolist (spec (org-fold-core-folding-spec-list))
    426         (push (cons spec spec) org-fold-core--spec-symbols)
    427         (dolist (alias (assq :alias (assq spec org-fold-core--specs)))
    428           (push (cons alias spec) org-fold-core--spec-symbols))))
    429     (alist-get spec-or-alias org-fold-core--spec-symbols)))
    430 
    431 (defsubst org-fold-core-folding-spec-p (spec-or-alias)
    432   "Check if SPEC-OR-ALIAS is a registered folding spec."
    433   (org-fold-core-get-folding-spec-from-alias spec-or-alias))
    434 
    435 (defsubst org-fold-core--check-spec (spec-or-alias)
    436   "Throw an error if SPEC-OR-ALIAS is not in `org-fold-core--spec-priority-list'."
    437   (unless (org-fold-core-folding-spec-p spec-or-alias)
    438     (error "%s is not a valid folding spec" spec-or-alias)))
    439 
    440 (defsubst org-fold-core-get-folding-spec-property (spec-or-alias property)
    441   "Get PROPERTY of a folding SPEC-OR-ALIAS.
    442 Possible properties can be found in `org-fold-core--specs' docstring."
    443   (org-fold-core--check-spec spec-or-alias)
    444   (if (and (memql 'ignore-indirect org-fold-core--optimise-for-huge-buffers)
    445            (eq property :global))
    446       t
    447     (if (and (memql 'merge-folds org-fold-core--optimise-for-huge-buffers)
    448              (eq property :visible))
    449         nil
    450       (cdr (assq property (assq (org-fold-core-get-folding-spec-from-alias spec-or-alias) org-fold-core--specs))))))
    451 
    452 (defconst org-fold-core--spec-property-prefix "org-fold--spec-"
    453   "Prefix used to create property symbol.")
    454 
    455 (defsubst org-fold-core-get-folding-property-symbol (spec &optional buffer global)
    456   "Get folding text property using to store SPEC in current buffer or BUFFER.
    457 If GLOBAL is non-nil, do not make the property unique in the BUFFER."
    458   (if (memql 'merge-folds org-fold-core--optimise-for-huge-buffers)
    459       (intern (format "%s-global" org-fold-core--spec-property-prefix))
    460     (intern (format (concat org-fold-core--spec-property-prefix "%s-%S")
    461                     (symbol-name spec)
    462                     ;; (sxhash buf) appears to be not constant over time.
    463                     ;; Using buffer-name is safe, since the only place where
    464                     ;; buffer-local text property actually matters is an indirect
    465                     ;; buffer, where the name cannot be same anyway.
    466                     (if (or global
    467                             (memql 'ignore-indirect org-fold-core--optimise-for-huge-buffers))
    468                         'global
    469                       (sxhash (buffer-name (or buffer (current-buffer)))))))))
    470 
    471 (defsubst org-fold-core-get-folding-spec-from-folding-prop (folding-prop)
    472   "Return folding spec symbol used for folding property with name FOLDING-PROP."
    473   (catch :exit
    474     (dolist (spec (org-fold-core-folding-spec-list))
    475       ;; We know that folding properties have
    476       ;; folding spec in their name.
    477       (when (string-match-p (symbol-name spec)
    478                             (symbol-name folding-prop))
    479         (throw :exit spec)))))
    480 
    481 (defvar org-fold-core--property-symbol-cache (make-hash-table :test 'equal)
    482   "Saved values of folding properties for (buffer . spec) conses.")
    483 (defvar-local org-fold-core--indirect-buffers nil
    484   "List of indirect buffers created from current buffer.
    485 
    486 The first element of the list is always the current buffer.
    487 
    488 This variable is needed to work around Emacs bug#46982, while Emacs
    489 does not provide a way `after-change-functions' in any other buffer
    490 than the buffer where the change was actually made.")
    491 
    492 (defmacro org-fold-core-cycle-over-indirect-buffers (&rest body)
    493   "Execute BODY in current buffer and all its indirect buffers.
    494 
    495 Also, make sure that folding properties from killed buffers are not
    496 hanging around."
    497   (declare (debug (form body)) (indent 0))
    498   `(let (buffers dead-properties)
    499      (if (and (not (buffer-base-buffer))
    500               (not (memq (current-buffer) org-fold-core--indirect-buffers)))
    501          ;; We are in base buffer with `org-fold-core--indirect-buffers' value from
    502          ;; different buffer.  This can happen, for example, when
    503          ;; org-capture copies local variables into *Capture* buffer.
    504          (setq buffers (list (current-buffer)))
    505        (dolist (buf (cons (or (buffer-base-buffer) (current-buffer))
    506                           (buffer-local-value 'org-fold-core--indirect-buffers (or (buffer-base-buffer) (current-buffer)))))
    507          (if (buffer-live-p buf)
    508              (push buf buffers)
    509            (dolist (spec (org-fold-core-folding-spec-list))
    510              (when (and (not (org-fold-core-get-folding-spec-property spec :global))
    511                         (gethash (cons buf spec) org-fold-core--property-symbol-cache))
    512                ;; Make sure that dead-properties variable can be passed
    513                ;; as argument to `remove-text-properties'.
    514                (push t dead-properties)
    515                (push (gethash (cons buf spec) org-fold-core--property-symbol-cache)
    516                      dead-properties))))))
    517      (dolist (buf buffers)
    518        (with-current-buffer buf
    519          (with-silent-modifications
    520            (save-restriction
    521              (widen)
    522              (remove-text-properties
    523               (point-min) (point-max)
    524               dead-properties)))
    525          ,@body))))
    526 
    527 ;; This is the core function used to fold text in buffers.  We use
    528 ;; text properties to hide folded text, however 'invisible property is
    529 ;; not directly used (unless risky `org-fold-core--optimise-for-huge-buffers' is
    530 ;; enabled). Instead, we define unique text property (folding
    531 ;; property) for every possible folding spec and add the resulting
    532 ;; text properties into `char-property-alias-alist', so that
    533 ;; 'invisible text property is automatically defined if any of the
    534 ;; folding properties is non-nil.  This approach lets us maintain
    535 ;; multiple folds for the same text region - poor man's overlays (but
    536 ;; much faster).  Additionally, folding properties are ensured to be
    537 ;; unique for different buffers (especially for indirect
    538 ;; buffers). This is done to allow different folding states in
    539 ;; indirect buffers.
    540 (defun org-fold-core--property-symbol-get-create (spec &optional buffer return-only)
    541   "Return a unique symbol suitable as folding text property.
    542 Return value is unique for folding SPEC in BUFFER.
    543 If the buffer already have buffer-local setup in `char-property-alias-alist'
    544 and the setup appears to be created for different buffer,
    545 copy the old invisibility state into new buffer-local text properties,
    546 unless RETURN-ONLY is non-nil."
    547   (if (eq org-fold-core-style 'overlays)
    548       (org-fold-core-get-folding-property-symbol spec nil 'global)
    549     (let* ((buf (or buffer (current-buffer))))
    550       ;; Create unique property symbol for SPEC in BUFFER
    551       (let ((local-prop (or (gethash (cons buf spec) org-fold-core--property-symbol-cache)
    552 			    (puthash (cons buf spec)
    553                                      (org-fold-core-get-folding-property-symbol
    554                                       spec buf
    555                                       (org-fold-core-get-folding-spec-property spec :global))
    556                                      org-fold-core--property-symbol-cache))))
    557         (prog1
    558             local-prop
    559           (unless return-only
    560 	    (with-current-buffer buf
    561               ;; Update folding properties carried over from other
    562               ;; buffer (implying that current buffer is indirect
    563               ;; buffer). Normally, `char-property-alias-alist' in new
    564               ;; indirect buffer is a copy of the same variable from
    565               ;; the base buffer. Then, `char-property-alias-alist'
    566               ;; would contain folding properties, which are not
    567               ;; matching the generated `local-prop'.
    568 	      (unless (member local-prop (cdr (assq 'invisible char-property-alias-alist)))
    569                 ;; Add current buffer to the list of indirect buffers in the base buffer.
    570                 (when (buffer-base-buffer)
    571                   (with-current-buffer (buffer-base-buffer)
    572                     (setq-local org-fold-core--indirect-buffers
    573                                 (let (bufs)
    574                                   (org-fold-core-cycle-over-indirect-buffers
    575                                     (push (current-buffer) bufs))
    576                                   (push buf bufs)
    577                                   (delete-dups bufs)))))
    578                 ;; Copy all the old folding properties to preserve the folding state
    579                 (with-silent-modifications
    580                   (dolist (old-prop (cdr (assq 'invisible char-property-alias-alist)))
    581                     (org-with-wide-buffer
    582                      (let* ((pos (point-min))
    583 	                    (spec (org-fold-core-get-folding-spec-from-folding-prop old-prop))
    584                             ;; Generate new buffer-unique folding property
    585 	                    (new-prop (when spec (org-fold-core--property-symbol-get-create spec nil 'return-only))))
    586                        ;; Copy the visibility state for `spec' from `old-prop' to `new-prop'
    587                        (unless (eq old-prop new-prop)
    588                          (while (< pos (point-max))
    589 	                   (let ((val (get-text-property pos old-prop))
    590                                  (next (next-single-char-property-change pos old-prop)))
    591 	                     (when val
    592 	                       (put-text-property pos next new-prop val))
    593                              (setq pos next)))))))
    594                   ;; Update `char-property-alias-alist' with folding
    595                   ;; properties unique for the current buffer.
    596                   (setq-local char-property-alias-alist
    597 	                      (cons (cons 'invisible
    598 			                  (mapcar (lambda (spec)
    599 				                    (org-fold-core--property-symbol-get-create spec nil 'return-only))
    600 				                  (org-fold-core-folding-spec-list)))
    601 		                    (remove (assq 'invisible char-property-alias-alist)
    602 			                    char-property-alias-alist)))
    603                   ;; Set folding property stickiness according to
    604                   ;; their `:font-sticky' and `:rear-sticky'
    605                   ;; parameters.
    606                   (let (full-prop-list)
    607                     (org-fold-core-cycle-over-indirect-buffers
    608                       (setq full-prop-list
    609                             (append full-prop-list
    610                                     (delq nil
    611                                           (mapcar (lambda (spec)
    612                                                     (cond
    613                                                      ((org-fold-core-get-folding-spec-property spec :front-sticky)
    614                                                       (cons (org-fold-core--property-symbol-get-create spec nil 'return-only)
    615                                                             nil))
    616                                                      ((org-fold-core-get-folding-spec-property spec :rear-sticky)
    617                                                       nil)
    618                                                      (t
    619                                                       (cons (org-fold-core--property-symbol-get-create spec nil 'return-only)
    620                                                             t))))
    621                                                   (org-fold-core-folding-spec-list))))))
    622                     (org-fold-core-cycle-over-indirect-buffers
    623                       (setq-local text-property-default-nonsticky
    624                                   (delete-dups (append
    625                                                 text-property-default-nonsticky
    626                                                 full-prop-list))))))))))))))
    627 
    628 (defun org-fold-core-decouple-indirect-buffer-folds ()
    629   "Copy and decouple folding state in a newly created indirect buffer.
    630 This function is mostly intended to be used in
    631 `clone-indirect-buffer-hook'."
    632   (when (and (buffer-base-buffer)
    633              (eq org-fold-core-style 'text-properties)
    634              (not (memql 'ignore-indirect org-fold-core--optimise-for-huge-buffers)))
    635     (org-fold-core--property-symbol-get-create (car (org-fold-core-folding-spec-list)))))
    636 
    637 ;;; API
    638 
    639 ;;;; Modifying folding specs
    640 
    641 (defun org-fold-core-set-folding-spec-property (spec property value &optional force)
    642   "Set PROPERTY of a folding SPEC to VALUE.
    643 Possible properties and values can be found in `org-fold-core--specs' docstring.
    644 Do not check previous value when FORCE is non-nil."
    645   (pcase property
    646     (:ellipsis
    647      (unless (and (not force) (equal value (org-fold-core-get-folding-spec-property spec :ellipsis)))
    648        (remove-from-invisibility-spec (cons spec (org-fold-core-get-folding-spec-property spec :ellipsis)))
    649        (unless (org-fold-core-get-folding-spec-property spec :visible)
    650          (add-to-invisibility-spec (cons spec value)))))
    651     (:visible
    652      (unless (or (memql 'merge-folds org-fold-core--optimise-for-huge-buffers)
    653                  (and (not force) (equal value (org-fold-core-get-folding-spec-property spec :visible))))
    654        (if value
    655 	   (remove-from-invisibility-spec (cons spec (org-fold-core-get-folding-spec-property spec :ellipsis)))
    656          (add-to-invisibility-spec (cons spec (org-fold-core-get-folding-spec-property spec :ellipsis))))))
    657     (:alias
    658      ;; Clear symbol cache.
    659      (setq org-fold-core--spec-symbols nil))
    660     (:isearch-open nil)
    661     (:isearch-ignore nil)
    662     (:front-sticky nil)
    663     (:rear-sticky nil)
    664     (_ nil))
    665   (setf (cdr (assq property (assq spec org-fold-core--specs))) value))
    666 
    667 (defun org-fold-core-add-folding-spec (spec &optional properties buffer append)
    668   "Add a new folding SPEC with PROPERTIES in BUFFER.
    669 
    670 SPEC must be a symbol.  BUFFER can be a buffer to set SPEC in or nil to
    671 set SPEC in current buffer.
    672 
    673 By default, the added SPEC will have highest priority among the
    674 previously defined specs.  When optional APPEND argument is non-nil,
    675 SPEC will have the lowest priority instead.  If SPEC was already
    676 defined earlier, it will be redefined according to provided optional
    677 arguments.
    678 `
    679 The folding spec properties will be set to PROPERTIES (see
    680 `org-fold-core--specs' for details)."
    681   (when (eq spec 'all) (error "Cannot use reserved folding spec symbol 'all"))
    682   (with-current-buffer (or buffer (current-buffer))
    683     ;; Clear the cache.
    684     (setq org-fold-core--spec-list nil
    685           org-fold-core--spec-symbols nil)
    686     (let* ((full-properties (mapcar (lambda (prop) (cons prop (cdr (assq prop properties))))
    687                                     '( :visible :ellipsis :isearch-ignore
    688                                        :global :isearch-open :front-sticky
    689                                        :rear-sticky :fragile :alias)))
    690            (full-spec (cons spec full-properties)))
    691       (add-to-list 'org-fold-core--specs full-spec append)
    692       (mapc (lambda (prop-cons) (org-fold-core-set-folding-spec-property spec (car prop-cons) (cdr prop-cons) 'force)) full-properties)
    693       ;; Update buffer inivisibility specs.
    694       (org-fold-core--property-symbol-get-create spec))))
    695 
    696 (defun org-fold-core-remove-folding-spec (spec &optional buffer)
    697   "Remove a folding SPEC in BUFFER.
    698 
    699 SPEC must be a symbol.
    700 
    701 BUFFER can be a buffer to remove SPEC in, nil to remove SPEC in current
    702 buffer, or `all' to remove SPEC in all open `org-mode' buffers and all
    703 future org buffers."
    704   (org-fold-core--check-spec spec)
    705   (when (eq buffer 'all)
    706     (setq-default org-fold-core--specs (delete (cdr (assq spec org-fold-core--specs)) org-fold-core--specs))
    707     (mapc (lambda (buf)
    708 	    (org-fold-core-remove-folding-spec spec buf))
    709 	  (buffer-list)))
    710   (let ((buffer (or buffer (current-buffer))))
    711     (with-current-buffer buffer
    712       ;; Clear the cache.
    713       (setq org-fold-core--spec-list nil
    714             org-fold-core--spec-symbols nil)
    715       (org-fold-core-set-folding-spec-property spec :visible t)
    716       (setq org-fold-core--specs (delete (cdr (assq spec org-fold-core--specs)) org-fold-core--specs)))))
    717 
    718 (defun org-fold-core-initialize (&optional specs)
    719   "Setup folding in current buffer using SPECS as value of `org-fold-core--specs'."
    720   ;; Preserve the priorities.
    721   (when specs (setq specs (nreverse specs)))
    722   (unless specs (setq specs org-fold-core--specs))
    723   (setq org-fold-core--specs nil
    724         org-fold-core--spec-list nil
    725         org-fold-core--spec-symbols nil)
    726   (dolist (spec specs)
    727     (org-fold-core-add-folding-spec (car spec) (cdr spec)))
    728   (add-hook 'after-change-functions 'org-fold-core--fix-folded-region nil 'local)
    729   (add-hook 'clone-indirect-buffer-hook #'org-fold-core-decouple-indirect-buffer-folds nil 'local)
    730   ;; Setup killing text
    731   (setq-local filter-buffer-substring-function #'org-fold-core--buffer-substring-filter)
    732   (if (and (boundp 'isearch-opened-regions)
    733            (eq org-fold-core-style 'text-properties))
    734       ;; Use new implementation of isearch allowing to search inside text
    735       ;; hidden via text properties.
    736       (org-fold-core--isearch-setup 'text-properties)
    737     (org-fold-core--isearch-setup 'overlays)))
    738 
    739 ;;;; Searching and examining folded text
    740 
    741 (defsubst org-fold-core-folded-p (&optional pos spec-or-alias)
    742   "Non-nil if the character after POS is folded.
    743 If POS is nil, use `point' instead.
    744 If SPEC-OR-ALIAS is a folding spec or a list, only check the given
    745 folding spec or the listed specs."
    746   (if (and spec-or-alias (listp spec-or-alias))
    747       (catch :found
    748         (dolist (spec spec-or-alias)
    749           (let ((val (org-fold-core-get-folding-spec spec pos)))
    750             (when val (throw :found val)))))
    751     (org-fold-core-get-folding-spec spec-or-alias pos)))
    752 
    753 (defun org-fold-core-region-folded-p (beg end &optional spec-or-alias)
    754   "Non-nil if the region between BEG and END is folded.
    755 If SPEC-OR-ALIAS is a folding spec, only check the given folding spec."
    756   (org-with-point-at beg
    757     (catch :visible
    758       (while (< (point) end)
    759         (unless (org-fold-core-get-folding-spec spec-or-alias) (throw :visible nil))
    760         (goto-char (org-fold-core-next-folding-state-change spec-or-alias nil end)))
    761       t)))
    762 
    763 (defun org-fold-core-get-folding-spec (&optional spec-or-alias pom)
    764   "Get folding state at `point' or POM.
    765 Return nil if there is no folding at point or POM.
    766 If SPEC-OR-ALIAS is nil, return a folding spec with highest priority
    767 among present at `point' or POM.
    768 If SPEC-OR-ALIAS is `all', return the list of all present folding
    769 specs.
    770 If SPEC-OR-ALIAS is a valid folding spec or a spec alias, return the
    771 corresponding folding spec (if the text is folded using that spec)."
    772   (let ((spec (if (eq spec-or-alias 'all)
    773                   'all
    774                 (org-fold-core-get-folding-spec-from-alias spec-or-alias))))
    775     (when (and spec (not (eq spec 'all))) (org-fold-core--check-spec spec))
    776     (org-with-point-at pom
    777       (cond
    778        ((eq spec 'all)
    779         (let ((result))
    780 	  (dolist (spec (org-fold-core-folding-spec-list))
    781 	    (let ((val (get-char-property (point) (org-fold-core--property-symbol-get-create spec nil t))))
    782 	      (when val (push val result))))
    783           (reverse result)))
    784        ((null spec)
    785         (let ((result (get-char-property (point) 'invisible)))
    786           (when (org-fold-core-folding-spec-p result) result)))
    787        (t (get-char-property (point) (org-fold-core--property-symbol-get-create spec nil t)))))))
    788 
    789 (defun org-fold-core-get-folding-specs-in-region (beg end)
    790   "Get all folding specs in region from BEG to END."
    791   (let ((pos beg)
    792 	all-specs)
    793     (while (< pos end)
    794       (setq all-specs (append all-specs (org-fold-core-get-folding-spec nil pos)))
    795       (setq pos (org-fold-core-next-folding-state-change nil pos end)))
    796     (unless (listp all-specs) (setq all-specs (list all-specs)))
    797     (delete-dups all-specs)))
    798 
    799 (defun org-fold-core-get-region-at-point (&optional spec-or-alias pom)
    800   "Return region folded using SPEC-OR-ALIAS at POM.
    801 If SPEC is nil, return the largest possible folded region.
    802 The return value is a cons of beginning and the end of the region.
    803 Return nil when no fold is present at point of POM."
    804   (let ((spec (org-fold-core-get-folding-spec-from-alias spec-or-alias)))
    805     (org-with-point-at (or pom (point))
    806       (if spec
    807           (if (eq org-fold-core-style 'text-properties)
    808 	      (org-find-text-property-region (point) (org-fold-core--property-symbol-get-create spec nil t))
    809             (let ((ov (cdr (get-char-property-and-overlay (point) (org-fold-core--property-symbol-get-create spec nil t)))))
    810               (when ov (cons (overlay-start ov) (overlay-end ov)))))
    811         (let ((region (cons (point) (point))))
    812 	  (dolist (spec (org-fold-core-get-folding-spec 'all))
    813             (let ((local-region (org-fold-core-get-region-at-point spec)))
    814               (when (< (car local-region) (car region))
    815                 (setcar region (car local-region)))
    816               (when (> (cdr local-region) (cdr region))
    817                 (setcdr region (cdr local-region)))))
    818 	  (unless (eq (car region) (cdr region)) region))))))
    819 
    820 (defun org-fold-core-next-visibility-change (&optional pos limit ignore-hidden-p previous-p)
    821   "Return next point from POS up to LIMIT where text becomes visible/invisible.
    822 By default, text hidden by any means (i.e. not only by folding, but
    823 also via fontification) will be considered.
    824 If IGNORE-HIDDEN-P is non-nil, consider only folded text.
    825 If PREVIOUS-P is non-nil, search backwards."
    826   (let* ((pos (or pos (point)))
    827 	 (invisible-p (if ignore-hidden-p
    828 			  #'org-fold-core-folded-p
    829 			#'invisible-p))
    830          (invisible-initially? (funcall invisible-p pos))
    831 	 (limit (or limit (if previous-p
    832 			      (point-min)
    833 			    (point-max))))
    834          (cmp (if previous-p #'> #'<))
    835 	 (next-change (if previous-p
    836 			  (if ignore-hidden-p
    837                               (lambda (p) (org-fold-core-previous-folding-state-change (org-fold-core-get-folding-spec nil p) p limit))
    838 			    (lambda (p) (max limit (1- (previous-single-char-property-change p 'invisible nil limit)))))
    839                         (if ignore-hidden-p
    840                             (lambda (p) (org-fold-core-next-folding-state-change (org-fold-core-get-folding-spec nil p) p limit))
    841 			  (lambda (p) (next-single-char-property-change p 'invisible nil limit)))))
    842 	 (next pos))
    843     (while (and (funcall cmp next limit)
    844 		(not (org-xor invisible-initially? (funcall invisible-p next))))
    845       (setq next (funcall next-change next)))
    846     next))
    847 
    848 (defun org-fold-core-previous-visibility-change (&optional pos limit ignore-hidden-p)
    849   "Call `org-fold-core-next-visibility-change' searching backwards."
    850   (org-fold-core-next-visibility-change pos limit ignore-hidden-p 'previous))
    851 
    852 (defun org-fold-core-next-folding-state-change (&optional spec-or-alias pos limit previous-p)
    853   "Return point after POS where folding state changes up to LIMIT.
    854 If SPEC-OR-ALIAS is nil, return next point where _any_ single folding
    855 spec changes.
    856 For example, (org-fold-core-next-folding-state-change nil) with point
    857 somewhere in the below structure will return the nearest <...> point.
    858 
    859 * Headline <begin outline fold>
    860 :PROPERTIES:<begin drawer fold>
    861 :ID: test
    862 :END:<end drawer fold>
    863 
    864 Fusce suscipit, wisi nec facilisis facilisis, est dui fermentum leo,
    865 quis tempor ligula erat quis odio.
    866 
    867 ** Another headline
    868 :DRAWER:<begin drawer fold>
    869 :END:<end drawer fold>
    870 ** Yet another headline
    871 <end of outline fold>
    872 
    873 If SPEC-OR-ALIAS is a folding spec symbol, only consider that folding
    874 spec.
    875 
    876 If SPEC-OR-ALIAS is a list, only consider changes of folding specs
    877 from the list.
    878 
    879 Search backwards when PREVIOUS-P is non-nil."
    880   (when (and spec-or-alias (symbolp spec-or-alias))
    881     (setq spec-or-alias (list spec-or-alias)))
    882   (when spec-or-alias
    883     (setq spec-or-alias
    884 	  (mapcar (lambda (spec-or-alias)
    885 		    (or (org-fold-core-get-folding-spec-from-alias spec-or-alias)
    886 			spec-or-alias))
    887                   spec-or-alias))
    888     (mapc #'org-fold-core--check-spec spec-or-alias))
    889   (unless spec-or-alias
    890     (setq spec-or-alias (org-fold-core-folding-spec-list)))
    891   (setq pos (or pos (point)))
    892   (apply (if previous-p
    893 	     #'max
    894 	   #'min)
    895          (mapcar (if previous-p
    896 		     (lambda (prop) (max (or limit (point-min)) (previous-single-char-property-change pos prop nil (or limit (point-min)))))
    897 		   (lambda (prop) (next-single-char-property-change pos prop nil (or limit (point-max)))))
    898                  (mapcar (lambda (el) (org-fold-core--property-symbol-get-create el nil t))
    899 		         spec-or-alias))))
    900 
    901 (defun org-fold-core-previous-folding-state-change (&optional spec-or-alias pos limit)
    902   "Call `org-fold-core-next-folding-state-change' searching backwards."
    903   (org-fold-core-next-folding-state-change spec-or-alias pos limit 'previous))
    904 
    905 (defun org-fold-core-search-forward (spec-or-alias &optional limit)
    906   "Search next region folded via folding SPEC-OR-ALIAS up to LIMIT.
    907 Move point right after the end of the region, to LIMIT, or
    908 `point-max'.  The `match-data' will contain the region."
    909   (let ((spec (org-fold-core-get-folding-spec-from-alias spec-or-alias)))
    910     (let ((prop-symbol (org-fold-core--property-symbol-get-create spec nil t)))
    911       (goto-char (or (next-single-char-property-change (point) prop-symbol nil limit) limit (point-max)))
    912       (when (and (< (point) (or limit (point-max)))
    913 	         (not (org-fold-core-get-folding-spec spec)))
    914         (goto-char (next-single-char-property-change (point) prop-symbol nil limit)))
    915       (when (org-fold-core-get-folding-spec spec)
    916         (let ((region (org-fold-core-get-region-at-point spec)))
    917 	  (when (< (cdr region) (or limit (point-max)))
    918 	    (goto-char (1+ (cdr region)))
    919             (set-match-data (list (set-marker (make-marker) (car region) (current-buffer))
    920 				  (set-marker (make-marker) (cdr region) (current-buffer))))))))))
    921 
    922 (cl-defun org-fold-core-get-regions (&key specs from to with-markers relative)
    923   "Find all the folded regions in current buffer.
    924 
    925 Each element of the returned list represent folded region boundaries
    926 and the folding spec: (BEG END SPEC).
    927 
    928 Search folds intersecting with (FROM TO) buffer region if FROM and TO
    929 are provided.
    930 
    931 If FROM is non-nil and TO is nil, search the folded regions at FROM.
    932 
    933 When both FROM and TO are nil, search folded regions in the whole buffer.
    934 
    935 When SPECS is non-nil it should be a list of folding specs or a symbol.
    936 Only return the matching fold types.
    937 
    938 When WITH-MARKERS is non-nil, use markers to represent region
    939 boundaries.
    940 
    941 When RELATIVE is a buffer position, regions boundaries are given
    942 relative to that position.
    943 When RELATIVE is t, use FROM as the position.
    944 WITH-MARKERS must be nil when RELATIVE is non-nil."
    945   (when (and relative with-markers)
    946     (error "Cannot use markers in non-absolute region boundaries"))
    947   (when (eq relative t) (setq relative from))
    948   (unless (listp specs) (setq specs (list specs)))
    949   (let (regions region mk-region)
    950     (org-with-wide-buffer
    951      (when (and (not from) (not to))
    952        (setq from (point-min)
    953              to (point-max)))
    954      (when (and from (not to)) (setq to (point-max)))
    955      (when (and from (< from (point-min))) (setq from (point-min)))
    956      (when (and to (> to (point-max))) (setq to (point-max)))
    957      (unless from (setq from (point-min)))
    958      (dolist (spec (or specs (org-fold-core-folding-spec-list)) regions)
    959        (goto-char from)
    960        (catch :exit
    961          (while (or (not to) (< (point) to))
    962            (when (org-fold-core-get-folding-spec spec)
    963              (setq region (org-fold-core-get-region-at-point spec))
    964              (when relative
    965                (cl-decf (car region) relative)
    966                (cl-decf (cdr region) relative))
    967              (if (not with-markers)
    968                  (setq mk-region `(,(car region) ,(cdr region) ,spec))
    969                (setq mk-region `(,(make-marker) ,(make-marker) ,spec))
    970                (move-marker (nth 0 mk-region) (car region))
    971                (move-marker (nth 1 mk-region) (cdr region)))
    972              (push mk-region regions))
    973            (unless to (throw :exit nil))
    974            (goto-char (org-fold-core-next-folding-state-change spec nil to))))))))
    975 
    976 ;;;; Changing visibility
    977 
    978 ;;;;; Region visibility
    979 
    980 ;; This is the core function performing actual folding/unfolding.  The
    981 ;; folding state is stored in text property (folding property)
    982 ;; returned by `org-fold-core--property-symbol-get-create'.  The value of the
    983 ;; folding property is folding spec symbol.
    984 (defun org-fold-core-region (from to flag &optional spec-or-alias)
    985   "Hide or show lines from FROM to TO, according to FLAG.
    986 SPEC-OR-ALIAS is the folding spec or foldable element, as a symbol.
    987 If SPEC-OR-ALIAS is omitted and FLAG is nil, unfold everything in the region."
    988   (let ((spec (org-fold-core-get-folding-spec-from-alias spec-or-alias)))
    989     (when spec (org-fold-core--check-spec spec))
    990     (with-silent-modifications
    991       (org-with-wide-buffer
    992        (when (eq org-fold-core-style 'overlays) (remove-overlays from to 'invisible spec))
    993        (if flag
    994 	   (if (not spec)
    995                (error "Calling `org-fold-core-region' with missing SPEC")
    996              (if (eq org-fold-core-style 'overlays)
    997                  ;; Use `front-advance' since text right before to the beginning of
    998                  ;; the overlay belongs to the visible line than to the contents.
    999                  (let ((o (make-overlay from to nil
   1000                                         (org-fold-core-get-folding-spec-property spec :front-sticky)
   1001                                         (org-fold-core-get-folding-spec-property spec :rear-sticky))))
   1002                    (overlay-put o 'evaporate t)
   1003                    (overlay-put o (org-fold-core--property-symbol-get-create spec) spec)
   1004                    (overlay-put o 'invisible spec)
   1005                    (overlay-put o 'isearch-open-invisible #'org-fold-core--isearch-show)
   1006                    (overlay-put o 'isearch-open-invisible-temporary #'org-fold-core--isearch-show-temporary))
   1007 	       (put-text-property from to (org-fold-core--property-symbol-get-create spec) spec)
   1008 	       (put-text-property from to 'isearch-open-invisible #'org-fold-core--isearch-show)
   1009 	       (put-text-property from to 'isearch-open-invisible-temporary #'org-fold-core--isearch-show-temporary)
   1010                (when (memql 'grab-invisible org-fold-core--optimise-for-huge-buffers)
   1011                  ;; If the SPEC has highest priority, assign it directly
   1012                  ;; to 'invisible property as well.  This is done to speed
   1013                  ;; up Emacs redisplay on huge (Mbs) folded regions where
   1014                  ;; we don't even want Emacs to spend time cycling over
   1015                  ;; `char-property-alias-alist'.
   1016                  (when (eq spec (caar org-fold-core--specs)) (put-text-property from to 'invisible spec)))))
   1017          (if (not spec)
   1018              (mapc (lambda (spec) (org-fold-core-region from to nil spec)) (org-fold-core-folding-spec-list))
   1019            (when (and (memql 'grab-invisible org-fold-core--optimise-for-huge-buffers)
   1020                       (eq org-fold-core-style 'text-properties))
   1021              (when (eq spec (caar org-fold-core--specs))
   1022                (let ((pos from))
   1023                  (while (< pos to)
   1024                    (if (eq spec (get-text-property pos 'invisible))
   1025                        (let ((next (org-fold-core-next-folding-state-change spec pos to)))
   1026                          (remove-text-properties pos next '(invisible t))
   1027                          (setq pos next))
   1028                      (setq pos (next-single-char-property-change pos 'invisible nil to)))))))
   1029            (when (eq org-fold-core-style 'text-properties)
   1030 	     (remove-text-properties from to (list (org-fold-core--property-symbol-get-create spec) nil)))))))))
   1031 
   1032 (cl-defmacro org-fold-core-regions (regions &key override clean-markers relative)
   1033   "Fold every region in REGIONS list in current buffer.
   1034 
   1035 Each region in the list is a list (BEG END SPEC-OR-ALIAS) describing
   1036 region and folding spec to be applied.
   1037 
   1038 When optional argument OVERRIDE is non-nil, clear folding state in the
   1039 buffer first.
   1040 
   1041 When optional argument CLEAN-MARKERS is non-nil, clear markers used to
   1042 mark region boundaries in REGIONS.
   1043 
   1044 When optional argument RELATIVE is non-nil, it must be a buffer
   1045 position.  REGION boundaries are then treated as relative distances
   1046 from that position."
   1047   `(org-with-wide-buffer
   1048     (when ,override (org-fold-core-region (point-min) (point-max) nil))
   1049     (pcase-dolist (`(,beg ,end ,spec) (delq nil ,regions))
   1050       (let ((rel ,relative))
   1051         (if rel
   1052             (org-fold-core-region (+ rel beg) (+ rel end) t spec)
   1053           (org-fold-core-region beg end t spec)))
   1054       (when ,clean-markers
   1055         (when (markerp beg) (set-marker beg nil))
   1056         (when (markerp end) (set-marker end nil))))))
   1057 
   1058 (defmacro org-fold-core-save-visibility (use-markers &rest body)
   1059   "Save and restore folding state around BODY.
   1060 If USE-MARKERS is non-nil, use markers for the positions.  This
   1061 means that the buffer may change while running BODY, but it also
   1062 means that the buffer should stay alive during the operation,
   1063 because otherwise all these markers will point to nowhere."
   1064   (declare (debug (form body)) (indent 1))
   1065   (org-with-gensyms (regions)
   1066     `(let* ((,regions (org-fold-core-get-regions :with-markers ,use-markers)))
   1067        (unwind-protect (progn ,@body)
   1068          (org-fold-core-regions ,regions :override t :clean-markers t)))))
   1069 
   1070 ;;; Make isearch search in some text hidden via text propertoes
   1071 
   1072 (defvar org-fold-core--isearch-overlays nil
   1073   "List of overlays temporarily created during isearch.
   1074 This is used to allow searching in regions hidden via text properties.
   1075 As for [2020-05-09 Sat], Isearch only has special handling of hidden overlays.
   1076 Any text hidden via text properties is not revealed even if `search-invisible'
   1077 is set to `t'.")
   1078 
   1079 (defvar-local org-fold-core--isearch-local-regions (make-hash-table :test 'equal)
   1080   "Hash table storing temporarily shown folds from isearch matches.")
   1081 
   1082 (defun org-fold-core--isearch-setup (type)
   1083   "Initialize isearch in org buffer.
   1084 TYPE can be either `text-properties' or `overlays'."
   1085   (pcase type
   1086     (`text-properties
   1087      (setq-local search-invisible 'open-all)
   1088      (add-hook 'isearch-mode-end-hook #'org-fold-core--clear-isearch-state nil 'local)
   1089      (add-hook 'isearch-mode-hook #'org-fold-core--clear-isearch-state nil 'local)
   1090      (setq-local isearch-filter-predicate #'org-fold-core--isearch-filter-predicate-text-properties))
   1091     (`overlays
   1092      (when (eq org-fold-core-style 'text-properties)
   1093        (setq-local isearch-filter-predicate #'org-fold-core--isearch-filter-predicate-overlays)
   1094        (add-hook 'isearch-mode-end-hook #'org-fold-core--clear-isearch-overlays nil 'local)))
   1095     (_ (error "%s: Unknown type of setup for `org-fold-core--isearch-setup'" type))))
   1096 
   1097 (defun org-fold-core--isearch-reveal (pos)
   1098   "Default function used to reveal hidden text at POS for isearch."
   1099   (let ((region (org-fold-core-get-region-at-point pos)))
   1100     (org-fold-core-region (car region) (cdr region) nil)))
   1101 
   1102 (defun org-fold-core--isearch-filter-predicate-text-properties (beg end)
   1103   "Make sure that folded text is searchable when user want so.
   1104 This function is intended to be used as `isearch-filter-predicate'."
   1105   (and
   1106    ;; Check folding specs that cannot be searched
   1107    (not (memq nil (mapcar (lambda (spec) (not (org-fold-core-get-folding-spec-property spec :isearch-ignore)))
   1108                       (org-fold-core-get-folding-specs-in-region beg end))))
   1109    ;; Check 'invisible properties that are not folding specs.
   1110    (or (eq search-invisible t) ; User wants to search anyway, allow it.
   1111        (let ((pos beg)
   1112 	     unknown-invisible-property)
   1113 	 (while (and (< pos end)
   1114 		     (not unknown-invisible-property))
   1115 	   (when (and (get-text-property pos 'invisible)
   1116                       (not (org-fold-core-folding-spec-p (get-text-property pos 'invisible))))
   1117 	     (setq unknown-invisible-property t))
   1118 	   (setq pos (next-single-char-property-change pos 'invisible)))
   1119 	 (not unknown-invisible-property)))
   1120    (or (and (eq search-invisible t)
   1121 	    ;; FIXME: this opens regions permanenly for now.
   1122             ;; I also tried to force search-invisible 'open-all around
   1123             ;; `isearch-range-invisible', but that somehow causes
   1124             ;; infinite loop in `isearch-lazy-highlight'.
   1125             (prog1 t
   1126 	      ;; We still need to reveal the folded location
   1127 	      (org-fold-core--isearch-show-temporary (cons beg end) nil)))
   1128        (not (isearch-range-invisible beg end)))))
   1129 
   1130 (defun org-fold-core--clear-isearch-state ()
   1131   "Clear `org-fold-core--isearch-local-regions'."
   1132   (clrhash org-fold-core--isearch-local-regions))
   1133 
   1134 (defun org-fold-core--isearch-show (region)
   1135   "Reveal text in REGION found by isearch.
   1136 REGION can also be an overlay in current buffer."
   1137   (when (overlayp region)
   1138     (setq region (cons (overlay-start region)
   1139                        (overlay-end region))))
   1140   (org-with-point-at (car region)
   1141     (while (< (point) (cdr region))
   1142       (funcall org-fold-core-isearch-open-function (car region))
   1143       (goto-char (org-fold-core-next-visibility-change (point) (cdr region) 'ignore-hidden)))))
   1144 
   1145 (defun org-fold-core--isearch-show-temporary (region hide-p)
   1146   "Temporarily reveal text in REGION.
   1147 Hide text instead if HIDE-P is non-nil.
   1148 REGION can also be an overlay in current buffer."
   1149   (when (overlayp region)
   1150     (setq region (cons (overlay-start region)
   1151                        (overlay-end region))))
   1152   (if (not hide-p)
   1153       (let ((pos (car region)))
   1154 	(while (< pos (cdr region))
   1155           (let ((spec-no-open
   1156                  (catch :found
   1157                    (dolist (spec (org-fold-core-get-folding-spec 'all pos))
   1158                      (unless (org-fold-core-get-folding-spec-property spec :isearch-open)
   1159                        (throw :found spec))))))
   1160             (if spec-no-open
   1161                 ;; Skip regions folded with folding specs that cannot be opened.
   1162                 (setq pos (org-fold-core-next-folding-state-change spec-no-open pos (cdr region)))
   1163 	      (dolist (spec (org-fold-core-get-folding-spec 'all pos))
   1164 	        (push (cons spec (org-fold-core-get-region-at-point spec pos)) (gethash region org-fold-core--isearch-local-regions)))
   1165               (org-fold-core--isearch-show region)
   1166 	      (setq pos (org-fold-core-next-folding-state-change nil pos (cdr region)))))))
   1167     (mapc (lambda (val) (org-fold-core-region (cadr val) (cddr val) t (car val))) (gethash region org-fold-core--isearch-local-regions))
   1168     (remhash region org-fold-core--isearch-local-regions)))
   1169 
   1170 (defvar-local org-fold-core--isearch-special-specs nil
   1171   "List of specs that can break visibility state when converted to overlays.
   1172 This is a hack, but I do not see a better way around until isearch
   1173 gets support of text properties.")
   1174 (defun org-fold-core--create-isearch-overlays (beg end)
   1175   "Replace text property invisibility spec by overlays between BEG and END.
   1176 All the searchable folded regions will be changed to use overlays
   1177 instead of text properties.  The created overlays will be stored in
   1178 `org-fold-core--isearch-overlays'."
   1179   (let ((pos beg))
   1180     (while (< pos end)
   1181       ;; We need loop below to make sure that we clean all invisible
   1182       ;; properties, which may be nested.
   1183       (dolist (spec (org-fold-core-get-folding-spec 'all pos))
   1184         (unless (org-fold-core-get-folding-spec-property spec :isearch-ignore)
   1185 	  (let* ((region (org-fold-core-get-region-at-point spec pos)))
   1186             (when (memq spec org-fold-core--isearch-special-specs)
   1187               (setq pos (min pos (car region)))
   1188               (setq end (max end (cdr region))))
   1189 	    ;; Changing text properties is considered buffer modification.
   1190 	    ;; We do not want it here.
   1191 	    (with-silent-modifications
   1192               (org-fold-core-region (car region) (cdr region) nil spec)
   1193 	      ;; The overlay is modeled after `outline-flag-region'
   1194 	      ;; [2020-05-09 Sat] overlay for 'outline blocks.
   1195 	      (let ((o (make-overlay (car region) (cdr region) nil 'front-advance)))
   1196 	        (overlay-put o 'evaporate t)
   1197 	        (overlay-put o 'invisible spec)
   1198                 (overlay-put o 'org-invisible spec)
   1199                 ;; Make sure that overlays are applied in the same order
   1200                 ;; with the folding specs.
   1201                 ;; Note: `memq` returns cdr with car equal to the first
   1202                 ;; found matching element.
   1203                 (overlay-put o 'priority (length (memq spec (org-fold-core-folding-spec-list))))
   1204 	        ;; `delete-overlay' here means that spec information will be lost
   1205 	        ;; for the region. The region will remain visible.
   1206                 (if (org-fold-core-get-folding-spec-property spec :isearch-open)
   1207 	            (overlay-put o 'isearch-open-invisible #'delete-overlay)
   1208                   (overlay-put o 'isearch-open-invisible #'ignore)
   1209                   (overlay-put o 'isearch-open-invisible-temporary #'ignore))
   1210 	        (push o org-fold-core--isearch-overlays))))))
   1211       (setq pos (org-fold-core-next-folding-state-change nil pos end)))))
   1212 
   1213 (defun org-fold-core--isearch-filter-predicate-overlays (beg end)
   1214   "Return non-nil if text between BEG and END is deemed visible by isearch.
   1215 This function is intended to be used as `isearch-filter-predicate'."
   1216   (org-fold-core--create-isearch-overlays beg end) ;; trick isearch by creating overlays in place of invisible text
   1217   (isearch-filter-visible beg end))
   1218 
   1219 (defun org-fold-core--clear-isearch-overlay (ov)
   1220   "Convert OV region back into using text properties."
   1221   (let ((spec (if isearch-mode-end-hook-quit
   1222                   ;; Restore all folds.
   1223                   (overlay-get ov 'org-invisible)
   1224                 ;; Leave opened folds open.
   1225                 (overlay-get ov 'invisible))))
   1226     ;; Ignore deleted overlays.
   1227     (when (and spec
   1228 	       (overlay-buffer ov))
   1229       ;; Changing text properties is considered buffer modification.
   1230       ;; We do not want it here.
   1231       (with-silent-modifications
   1232 	(when (<= (overlay-end ov) (point-max))
   1233 	  (org-fold-core-region (overlay-start ov) (overlay-end ov) t spec)))))
   1234   (when (member ov isearch-opened-overlays)
   1235     (setq isearch-opened-overlays (delete ov isearch-opened-overlays)))
   1236   (delete-overlay ov))
   1237 
   1238 (defun org-fold-core--clear-isearch-overlays ()
   1239   "Convert overlays from `org-fold-core--isearch-overlays' back to text properties."
   1240   (when org-fold-core--isearch-overlays
   1241     (mapc #'org-fold-core--clear-isearch-overlay org-fold-core--isearch-overlays)
   1242     (setq org-fold-core--isearch-overlays nil)))
   1243 
   1244 ;;; Handling changes in folded elements
   1245 
   1246 (defvar org-fold-core--ignore-modifications nil
   1247   "Non-nil: skip processing modifications in `org-fold-core--fix-folded-region'.")
   1248 (defvar org-fold-core--ignore-fragility-checks nil
   1249   "Non-nil: skip fragility checks in `org-fold-core--fix-folded-region'.")
   1250 
   1251 (defmacro org-fold-core-ignore-modifications (&rest body)
   1252   "Run BODY ignoring buffer modifications in `org-fold-core--fix-folded-region'."
   1253   (declare (debug (form body)) (indent 0))
   1254   `(let ((org-fold-core--ignore-modifications t))
   1255      (unwind-protect (progn ,@body)
   1256        (setq org-fold-core--last-buffer-chars-modified-tick (buffer-chars-modified-tick)))))
   1257 
   1258 (defmacro org-fold-core-ignore-fragility-checks (&rest body)
   1259   "Run BODY skipping :fragility checks in `org-fold-core--fix-folded-region'."
   1260   (declare (debug (form body)) (indent 0))
   1261   `(let ((org-fold-core--ignore-fragility-checks t))
   1262      (progn ,@body)))
   1263 
   1264 (defvar-local org-fold-core--last-buffer-chars-modified-tick nil
   1265   "Variable storing the last return value of `buffer-chars-modified-tick'.")
   1266 
   1267 (defun org-fold-core--fix-folded-region (from to _)
   1268   "Process modifications in folded elements within FROM . TO region.
   1269 This function intended to be used as one of `after-change-functions'.
   1270 
   1271 This function does nothing if text the only modification was changing
   1272 text properties (for the sake of reducing overheads).
   1273 
   1274 If a text was inserted into invisible region, hide the inserted text.
   1275 If a text was inserted in front/back of the region, hide it according
   1276 to :front-sticky/:rear-sticky folding spec property.
   1277 
   1278 If the folded region is folded with a spec with non-nil :fragile
   1279 property, unfold the region if the :fragile function returns non-nil."
   1280   ;; If no insertions or deletions in buffer, skip all the checks.
   1281   (unless (or (eq org-fold-core--last-buffer-chars-modified-tick (buffer-chars-modified-tick))
   1282               org-fold-core--ignore-modifications
   1283               (memql 'ignore-modification-checks org-fold-core--optimise-for-huge-buffers))
   1284     ;; Store the new buffer modification state.
   1285     (setq org-fold-core--last-buffer-chars-modified-tick (buffer-chars-modified-tick))
   1286     (save-match-data
   1287       ;; Handle changes in all the indirect buffers and in the base
   1288       ;; buffer.  Work around Emacs bug#46982.
   1289       (when (eq org-fold-core-style 'text-properties)
   1290         (org-fold-core-cycle-over-indirect-buffers
   1291           ;; Re-hide text inserted in the middle/front/back of a folded
   1292           ;; region.
   1293           (unless (equal from to) ; Ignore deletions.
   1294 	    (dolist (spec (org-fold-core-folding-spec-list))
   1295               ;; Reveal fully invisible text inserted in the middle
   1296               ;; of visible portion of the buffer.  This is needed,
   1297               ;; for example, when there was a deletion in a folded
   1298               ;; heading, the heading was unfolded, end `undo' was
   1299               ;; called.  The `undo' would insert the folded text.
   1300               (when (and (or (eq from (point-min))
   1301                              (not (org-fold-core-folded-p (1- from) spec)))
   1302                          (or (eq to (point-max))
   1303                              (not (org-fold-core-folded-p to spec)))
   1304                          (org-fold-core-region-folded-p from to spec))
   1305                 (org-fold-core-region from to nil spec))
   1306               ;; Look around and fold the new text if the nearby folds are
   1307               ;; sticky.
   1308               (unless (org-fold-core-region-folded-p from to spec)
   1309 	        (let ((spec-to (org-fold-core-get-folding-spec spec (min to (1- (point-max)))))
   1310 		      (spec-from (org-fold-core-get-folding-spec spec (max (point-min) (1- from)))))
   1311                   ;; Reveal folds around undone deletion.
   1312                   (when undo-in-progress
   1313                     (let ((lregion (org-fold-core-get-region-at-point spec (max (point-min) (1- from))))
   1314                           (rregion (org-fold-core-get-region-at-point spec (min to (1- (point-max))))))
   1315                       (if (and lregion rregion)
   1316                           (org-fold-core-region (car lregion) (cdr rregion) nil spec)
   1317                         (when lregion
   1318                           (org-fold-core-region (car lregion) (cdr lregion) nil spec))
   1319                         (when rregion
   1320                           (org-fold-core-region (car rregion) (cdr rregion) nil spec)))))
   1321                   ;; Hide text inserted in the middle of a fold.
   1322 	          (when (and (or spec-from (eq from (point-min)))
   1323                              (or spec-to (eq to (point-max)))
   1324                              (or spec-from spec-to)
   1325                              (eq spec-to spec-from)
   1326                              (or (org-fold-core-get-folding-spec-property spec :front-sticky)
   1327                                  (org-fold-core-get-folding-spec-property spec :rear-sticky)))
   1328                     (unless (and (eq from (point-min)) (eq to (point-max))) ; Buffer content replaced.
   1329 	              (org-fold-core-region from to t (or spec-from spec-to))))
   1330                   ;; Hide text inserted at the end of a fold.
   1331                   (when (and spec-from (org-fold-core-get-folding-spec-property spec-from :rear-sticky))
   1332                     (org-fold-core-region from to t spec-from))
   1333                   ;; Hide text inserted in front of a fold.
   1334                   (when (and spec-to
   1335                              (not (eq to (point-max))) ; Text inserted at the end of buffer is not prepended anywhere.
   1336                              (org-fold-core-get-folding-spec-property spec-to :front-sticky))
   1337                     (org-fold-core-region from to t spec-to))))))))
   1338       ;; Process all the folded text between `from' and `to'.  Do it
   1339       ;; only in current buffer to avoid verifying semantic structure
   1340       ;; multiple times in indirect buffers that have exactly same
   1341       ;; text anyway.
   1342       (unless (or org-fold-core--ignore-fragility-checks
   1343                   (memql 'ignore-fragility-checks org-fold-core--optimise-for-huge-buffers))
   1344         (dolist (func org-fold-core-extend-changed-region-functions)
   1345           (let ((new-region (funcall func from to)))
   1346             (setq from (car new-region))
   1347             (setq to (cdr new-region))))
   1348         (org-fold-core-cycle-over-indirect-buffers
   1349           (dolist (spec (org-fold-core-folding-spec-list))
   1350             ;; No action is needed when :fragile is nil for the spec.
   1351             (when (org-fold-core-get-folding-spec-property spec :fragile)
   1352               (org-with-wide-buffer
   1353                ;; Expand the considered region to include partially present fold.
   1354                ;; Note: It is important to do this inside loop over all
   1355                ;; specs.  Otherwise, the region may be expanded to huge
   1356                ;; outline fold, potentially involving majority of the
   1357                ;; buffer.  That would cause the below code to loop over
   1358                ;; almost all the folds in buffer, which would be too slow.
   1359                (let ((local-from from)
   1360                      (local-to to)
   1361                      (region-from (org-fold-core-get-region-at-point spec (max (point-min) (1- from))))
   1362                      (region-to (org-fold-core-get-region-at-point spec (min to (1- (point-max))))))
   1363                  (when region-from (setq local-from (car region-from)))
   1364                  (when region-to (setq local-to (cdr region-to)))
   1365                  (let ((pos local-from))
   1366                    ;; Move to the first hidden region.
   1367                    (unless (org-fold-core-get-folding-spec spec pos)
   1368                      (setq pos (org-fold-core-next-folding-state-change spec pos local-to)))
   1369                    ;; Cycle over all the folds.
   1370                    (while (< pos local-to)
   1371                      (save-match-data ; we should not clobber match-data in after-change-functions
   1372                        (let ((fold-begin (and (org-fold-core-get-folding-spec spec pos)
   1373                                               pos))
   1374                              (fold-end (org-fold-core-next-folding-state-change spec pos local-to)))
   1375                          (when (and fold-begin fold-end)
   1376                            (when (save-excursion
   1377                                    (funcall (org-fold-core-get-folding-spec-property spec :fragile)
   1378                                             (cons fold-begin fold-end)
   1379                                             spec))
   1380                              ;; Reveal completely, not just from the SPEC.
   1381                              (org-fold-core-region fold-begin fold-end nil)))))
   1382                      ;; Move to next fold.
   1383                      (setq pos (org-fold-core-next-folding-state-change spec pos local-to)))))))))))))
   1384 
   1385 ;;; Handling killing/yanking of folded text
   1386 
   1387 ;; By default, all the text properties of the killed text are
   1388 ;; preserved, including the folding text properties.  This can be
   1389 ;; awkward when we copy a text from an indirect buffer to another
   1390 ;; indirect buffer (or the base buffer).  The copied text might be
   1391 ;; visible in the source buffer, but might disappear if we yank it in
   1392 ;; another buffer.  This happens in the following situation:
   1393 ;; ---- base buffer ----
   1394 ;; * Headline<begin fold>
   1395 ;; Some text hidden in the base buffer, but revealed in the indirect
   1396 ;; buffer.<end fold>
   1397 ;; * Another headline
   1398 ;;
   1399 ;; ---- end of base buffer ----
   1400 ;; ---- indirect buffer ----
   1401 ;; * Headline
   1402 ;; Some text hidden in the base buffer, but revealed in the indirect
   1403 ;; buffer.
   1404 ;; * Another headline
   1405 ;;
   1406 ;; ---- end of indirect buffer ----
   1407 ;; If we copy the text under "Headline" from the indirect buffer and
   1408 ;; insert it under "Another headline" in the base buffer, the inserted
   1409 ;; text will be hidden since it's folding text properties are copied.
   1410 ;; Basically, the copied text would have two sets of folding text
   1411 ;; properties: (1) Properties for base buffer telling that the text is
   1412 ;; hidden; (2) Properties for the indirect buffer telling that the
   1413 ;; text is visible.  The first set of the text properties in inactive
   1414 ;; in the indirect buffer, but will become active once we yank the
   1415 ;; text back into the base buffer.
   1416 ;;
   1417 ;; To avoid the above situation, we simply clear all the properties,
   1418 ;; unrealated to current buffer when a text is copied.
   1419 ;; FIXME: Ideally, we may want to carry the folding state of copied
   1420 ;; text between buffer (probably via user customization).
   1421 (defun org-fold-core--buffer-substring-filter (beg end &optional delete)
   1422   "Clear folding state in killed text.
   1423 This function is intended to be used as `filter-buffer-substring-function'.
   1424 The arguments and return value are as specified for `filter-buffer-substring'."
   1425   (let ((return-string (buffer-substring--filter beg end delete))
   1426 	;; The list will be used as an argument to `remove-text-properties'.
   1427 	props-list)
   1428     ;; There is no easy way to examine all the text properties of a
   1429     ;; string, so we utilize the fact that printed string
   1430     ;; representation lists all its properties.
   1431     ;; Loop over the elements of string representation.
   1432     (unless (or (string= "" return-string)
   1433                 (<= end beg)
   1434                 (eq org-fold-core-style 'overlays))
   1435       ;; Collect all the text properties the string is completely
   1436       ;; hidden with.
   1437       (dolist (spec (org-fold-core-folding-spec-list))
   1438         (when (and (org-fold-core-region-folded-p beg end spec)
   1439                    (org-region-invisible-p beg end))
   1440           (push (org-fold-core--property-symbol-get-create spec nil t) props-list)))
   1441       (dolist (plist
   1442                (if (fboundp 'object-intervals)
   1443                    (object-intervals return-string)
   1444                  ;; Backward compatibility with Emacs <28.
   1445                  ;; FIXME: Is there any better way to do it?
   1446                  ;; Yes, it is a hack.
   1447                  ;; The below gives us string representation as a list.
   1448                  ;; Note that we need to remove unreadable values, like markers (#<...>).
   1449                  (seq-partition
   1450                   (cdr (let ((data (read (replace-regexp-in-string
   1451                                           "^#(" "("
   1452                                           (replace-regexp-in-string
   1453                                            " #(" " ("
   1454                                            (replace-regexp-in-string
   1455                                             "#<[^>]+>" "dummy"
   1456                                             ;; Get text representation of the string object.
   1457                                             ;; Make sure to print everything (see `prin1' docstring).
   1458                                             ;; `prin1' is used to print "%S" format.
   1459                                             (let (print-level print-length)
   1460                                               (format "%S" return-string))))))))
   1461                          (if (listp data) data (list data))))
   1462                   3)))
   1463         (let* ((start (car plist))
   1464                (fin (cadr plist))
   1465                (plist (car (cddr plist))))
   1466           ;; Only lists contain text properties.
   1467           (when (listp plist)
   1468             ;; Collect all the relevant text properties.
   1469             (while plist
   1470               (let* ((prop (car plist))
   1471                      (prop-name (symbol-name prop)))
   1472                 ;; Reveal hard-hidden text.  See
   1473                 ;; `org-fold-core--optimise-for-huge-buffers'.
   1474                 (when (and (eq prop 'invisible)
   1475                            (member (cadr plist) (org-fold-core-folding-spec-list)))
   1476                   (remove-text-properties start fin '(invisible t) return-string))
   1477                 ;; We do not care about values now.
   1478                 (setq plist (cddr plist))
   1479                 (when (string-match-p org-fold-core--spec-property-prefix prop-name)
   1480                   ;; Leave folding specs from current buffer.  See
   1481                   ;; comments in `org-fold-core--property-symbol-get-create' to
   1482                   ;; understand why it works.
   1483                   (unless (member prop (cdr (assq 'invisible char-property-alias-alist)))
   1484                     (push prop props-list))))))))
   1485       (remove-text-properties 0 (length return-string) props-list return-string))
   1486     return-string))
   1487 
   1488 (defun org-fold-core-update-optimisation (beg end)
   1489   "Update huge buffer optimization between BEG and END.
   1490 See `org-fold-core--optimise-for-huge-buffers'."
   1491   (when (and (memql 'grab-invisible org-fold-core--optimise-for-huge-buffers)
   1492              (eq org-fold-core-style 'text-properties))
   1493     (let ((pos beg))
   1494       (while (< pos end)
   1495         (when (and (org-fold-core-folded-p pos (caar org-fold-core--specs))
   1496                    (not (eq (caar org-fold-core--specs) (get-text-property pos 'invisible))))
   1497           (put-text-property pos (org-fold-core-next-folding-state-change (caar org-fold-core--specs) pos end)
   1498                              'invisible (caar org-fold-core--specs)))
   1499         (setq pos (org-fold-core-next-folding-state-change (caar org-fold-core--specs) pos end))))))
   1500 
   1501 (defun org-fold-core-remove-optimisation (beg end)
   1502   "Remove huge buffer optimization between BEG and END.
   1503 See `org-fold-core--optimise-for-huge-buffers'."
   1504   (when (and (memql 'grab-invisible org-fold-core--optimise-for-huge-buffers)
   1505              (eq org-fold-core-style 'text-properties))
   1506     (let ((pos beg))
   1507       (while (< pos end)
   1508         (if (and (org-fold-core-folded-p pos (caar org-fold-core--specs))
   1509                  (eq (caar org-fold-core--specs) (get-text-property pos 'invisible)))
   1510             (remove-text-properties pos (org-fold-core-next-folding-state-change (caar org-fold-core--specs) pos end)
   1511                                     '(invisible t)))
   1512         (setq pos (org-fold-core-next-folding-state-change (caar org-fold-core--specs) pos end))))))
   1513 
   1514 (provide 'org-fold-core)
   1515 
   1516 ;;; org-fold-core.el ends here