config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

org-fold-core.el (83326B)


      1 ;;; org-fold-core.el --- Folding buffer text -*- lexical-binding: t; -*-
      2 ;;
      3 ;; Copyright (C) 2020-2024 Free Software Foundation, Inc.
      4 ;;
      5 ;; Author: Ihor Radchenko <yantar92 at posteo dot net>
      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.  See 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 (if (version< emacs-version "29")
    284                                    'text-properties
    285                                  'overlays)
    286   "Internal implementation detail used to hide folded text.
    287 Can be either `text-properties' or `overlays'.
    288 The former is faster on large files in Emacs <29, while the latter is
    289 generally less error-prone with regard to third-party packages.
    290 
    291 Important: This variable must be set before loading Org."
    292   :group 'org
    293   :package-version '(Org . "9.7")
    294   :type '(choice
    295           (const :tag "Overlays" overlays)
    296           (const :tag "Text properties" text-properties)))
    297 
    298 (defvar-local org-fold-core-isearch-open-function #'org-fold-core--isearch-reveal
    299   "Function used to reveal hidden text found by isearch.
    300 The function is called with a single argument - point where text is to
    301 be revealed.")
    302 
    303 (defvar-local org-fold-core--optimise-for-huge-buffers nil
    304   "Non-nil turns on extra speedup on huge buffers (Mbs of folded text).
    305 
    306 This setting is risky and may cause various artifacts and degraded
    307 functionality, especially when using external packages.  It is
    308 recommended to enable it on per-buffer basis as file-local variable.
    309 
    310 When set to non-nil, must be a list containing one or multiple the
    311 following symbols:
    312 
    313 - `grab-invisible': Use `invisible' text property to hide text.  This
    314   will reduce the load on Emacs display engine and one may use it if
    315   moving point across folded regions becomes slow.  However, as a side
    316   effect, some external packages extracting i.e. headlings from folded
    317   parts of buffer may keep the text invisible.
    318 
    319 - `ignore-fragility-checks': Do not try to detect when user edits
    320   break structure of the folded elements.  This will speed up
    321   modifying the folded regions at the cost that some higher-level
    322   functions relying on this package might not be able to unfold the
    323   edited text.  For example, removed leading stars from a folded
    324   headline in Org mode will break visibility cycling since Org mode
    325   will not be aware that the following folded text belonged to
    326   headline.
    327 
    328 - `ignore-modification-checks': Do not try to detect insertions in the
    329   middle of the folded regions.  This will speed up non-interactive
    330   edits of the folded regions.  However, text inserted in the middle
    331   of the folded regions may become visible for some external packages
    332   inserting text using `insert' instead of `insert-and-inherit' (the
    333   latter is rarely used in practice).
    334 
    335 - `ignore-indirect': Do not decouple folding state in the indirect
    336   buffers.  This can speed up Emacs display engine (and thus motion of
    337   point), especially when large number of indirect buffers is being
    338   used.
    339 
    340 - `merge-folds': Do not distinguish between different types of folding
    341   specs.  This is the most aggressive optimization with unforeseen and
    342   potentially drastic effects.")
    343 (put 'org-fold-core--optimise-for-huge-buffers 'safe-local-variable 'listp)
    344 
    345 ;;; Core functionality
    346 
    347 ;;;; Folding specs
    348 
    349 (defvar-local org-fold-core--specs '((org-fold-visible
    350 	                 (:visible . t)
    351                          (:alias . (visible)))
    352                         (org-fold-hidden
    353 			 (:ellipsis . "...")
    354                          (:isearch-open . t)
    355                          (:alias . (hidden))))
    356   "Folding specs defined in current buffer.
    357 
    358 Each spec is a list (SPEC-SYMBOL SPEC-PROPERTIES).
    359 SPEC-SYMBOL is the symbol representing the folding spec.
    360 SPEC-PROPERTIES is an alist defining folding spec properties.
    361 
    362 If a text region is folded using multiple specs, only the folding spec
    363 listed earlier is used.
    364 
    365 The following properties are known:
    366 - :ellipsis         :: must be nil or string to show when text is folded
    367                       using this spec.
    368 - :global           :: non-nil means that folding state will be preserved
    369                       when copying folded text between buffers.
    370 - :isearch-ignore   :: non-nil means that folded text is not searchable
    371                       using isearch.
    372 - :isearch-open     :: non-nil means that isearch can reveal text hidden
    373                       using this spec.  This property does nothing
    374                       when `isearch-ignore' property is non-nil.
    375 - :front-sticky     :: non-nil means that text prepended to the folded text
    376                       is automatically folded.
    377 - :rear-sticky      :: non-nil means that text appended to the folded text
    378                       is folded.
    379 - :visible          :: non-nil means that folding spec visibility is not
    380                        managed.  Instead, visibility settings in
    381                        `buffer-invisibility-spec' will be used as is.
    382                        Note that changing this property from nil to t may
    383                        clear the setting in `buffer-invisibility-spec'.
    384 - :font-lock        :: non-nil means that newlines after the fold should
    385                        be re-fontified upon folding/unfolding.  See
    386                        `org-activate-folds'.
    387 - :alias            :: a list of aliases for the SPEC-SYMBOL.
    388 - :fragile          :: Must be a function accepting two arguments.
    389                        Non-nil means that changes in region may cause
    390                        the region to be revealed.  The region is
    391                        revealed after changes if the function returns
    392                        non-nil.
    393                        The function called after changes are made with
    394                        two arguments: cons (beg . end) representing the
    395                        folded region and spec symbol.")
    396 (defvar-local org-fold-core--spec-symbols nil
    397   "Alist holding buffer spec symbols and aliases.
    398 
    399 This variable is defined to reduce load on Emacs garbage collector
    400 reducing the number of transiently allocated variables.")
    401 (defvar-local org-fold-core--spec-list nil
    402   "List holding buffer spec symbols, but not aliases.
    403 
    404 This variable is defined to reduce load on Emacs garbage collector
    405 reducing the number of transiently allocated variables.")
    406 
    407 (defvar-local org-fold-core-extend-changed-region-functions nil
    408   "Special hook run just before handling changes in buffer.
    409 
    410 This is used to account changes outside folded regions that still
    411 affect the folded region visibility.  For example, removing all stars
    412 at the beginning of a folded Org mode heading should trigger the
    413 folded text to be revealed.  Each function is called with two
    414 arguments: beginning and the end of the changed region.")
    415 
    416 ;;; Utility functions
    417 
    418 (defsubst org-fold-core-folding-spec-list (&optional buffer)
    419   "Return list of all the folding spec symbols in BUFFER."
    420   (or (buffer-local-value 'org-fold-core--spec-list (or buffer (current-buffer)))
    421       (with-current-buffer (or buffer (current-buffer))
    422         (setq org-fold-core--spec-list (mapcar #'car org-fold-core--specs)))))
    423 
    424 (defun org-fold-core-get-folding-spec-from-alias (spec-or-alias)
    425   "Return the folding spec symbol for SPEC-OR-ALIAS.
    426 Return nil when there is no matching folding spec."
    427   (when spec-or-alias
    428     (unless org-fold-core--spec-symbols
    429       (dolist (spec (org-fold-core-folding-spec-list))
    430         (push (cons spec spec) org-fold-core--spec-symbols)
    431         (dolist (alias (cdr (assq :alias (assq spec org-fold-core--specs))))
    432           (push (cons alias spec) org-fold-core--spec-symbols))))
    433     (alist-get spec-or-alias org-fold-core--spec-symbols)))
    434 
    435 (defsubst org-fold-core-folding-spec-p (spec-or-alias)
    436   "Check if SPEC-OR-ALIAS is a registered folding spec."
    437   (org-fold-core-get-folding-spec-from-alias spec-or-alias))
    438 
    439 (defsubst org-fold-core--check-spec (spec-or-alias)
    440   "Throw an error if SPEC-OR-ALIAS is not in `org-fold-core-folding-spec-list'."
    441   (unless (org-fold-core-folding-spec-p spec-or-alias)
    442     (error "%s is not a valid folding spec" spec-or-alias)))
    443 
    444 (defsubst org-fold-core-get-folding-spec-property (spec-or-alias property)
    445   "Get PROPERTY of a folding SPEC-OR-ALIAS.
    446 Possible properties can be found in `org-fold-core--specs' docstring."
    447   (org-fold-core--check-spec spec-or-alias)
    448   (if (and (memql 'ignore-indirect org-fold-core--optimise-for-huge-buffers)
    449            (eq property :global))
    450       t
    451     (if (and (memql 'merge-folds org-fold-core--optimise-for-huge-buffers)
    452              (eq property :visible))
    453         nil
    454       (cdr (assq property (assq (org-fold-core-get-folding-spec-from-alias spec-or-alias) org-fold-core--specs))))))
    455 
    456 (defconst org-fold-core--spec-property-prefix "org-fold--spec-"
    457   "Prefix used to create property symbol.")
    458 
    459 (defsubst org-fold-core-get-folding-property-symbol (spec &optional buffer global)
    460   "Get folding text property using to store SPEC in current buffer or BUFFER.
    461 If GLOBAL is non-nil, do not make the property unique in the BUFFER."
    462   (if (memql 'merge-folds org-fold-core--optimise-for-huge-buffers)
    463       (intern (format "%s-global" org-fold-core--spec-property-prefix))
    464     (intern (format (concat org-fold-core--spec-property-prefix "%s-%S")
    465                     (symbol-name spec)
    466                     ;; (sxhash buf) appears to be not constant over time.
    467                     ;; Using buffer-name is safe, since the only place where
    468                     ;; buffer-local text property actually matters is an indirect
    469                     ;; buffer, where the name cannot be same anyway.
    470                     (if (or global
    471                             (memql 'ignore-indirect org-fold-core--optimise-for-huge-buffers))
    472                         'global
    473                       (sxhash (buffer-name (or buffer (current-buffer)))))))))
    474 
    475 (defsubst org-fold-core-get-folding-spec-from-folding-prop (folding-prop)
    476   "Return folding spec symbol used for folding property with name FOLDING-PROP."
    477   (catch :exit
    478     (dolist (spec (org-fold-core-folding-spec-list))
    479       ;; We know that folding properties have
    480       ;; folding spec in their name.
    481       (when (string-match-p (symbol-name spec)
    482                             (symbol-name folding-prop))
    483         (throw :exit spec)))))
    484 
    485 (defvar org-fold-core--property-symbol-cache (make-hash-table :test 'equal)
    486   "Saved values of folding properties for (buffer . spec) conses.")
    487 (defvar-local org-fold-core--indirect-buffers nil
    488   "List of indirect buffers created from current buffer.
    489 
    490 The first element of the list is always the current buffer.
    491 
    492 This variable is needed to work around Emacs bug#46982, while Emacs
    493 does not provide a way `after-change-functions' in any other buffer
    494 than the buffer where the change was actually made.")
    495 
    496 (defmacro org-fold-core-cycle-over-indirect-buffers (&rest body)
    497   "Execute BODY in current buffer and all its indirect buffers.
    498 
    499 Also, make sure that folding properties from killed buffers are not
    500 hanging around."
    501   (declare (debug (form body)) (indent 0))
    502   `(let (buffers dead-properties)
    503      (if (and (not (buffer-base-buffer))
    504               (not (memq (current-buffer) org-fold-core--indirect-buffers)))
    505          ;; We are in base buffer with `org-fold-core--indirect-buffers' value from
    506          ;; different buffer.  This can happen, for example, when
    507          ;; org-capture copies local variables into *Capture* buffer.
    508          (setq buffers (list (current-buffer)))
    509        (let ((all-buffers (buffer-local-value
    510                            'org-fold-core--indirect-buffers
    511                            (or (buffer-base-buffer) (current-buffer)))))
    512          (dolist (buf (cons (or (buffer-base-buffer) (current-buffer))
    513                             (buffer-local-value 'org-fold-core--indirect-buffers (or (buffer-base-buffer) (current-buffer)))))
    514            (if (buffer-live-p buf)
    515                (push buf buffers)
    516              (dolist (spec (org-fold-core-folding-spec-list))
    517                (when (and (not (org-fold-core-get-folding-spec-property spec :global))
    518                           (gethash (cons buf spec) org-fold-core--property-symbol-cache))
    519                  ;; Make sure that dead-properties variable can be passed
    520                  ;; as argument to `remove-text-properties'.
    521                  (push t dead-properties)
    522                  (push (gethash (cons buf spec) org-fold-core--property-symbol-cache)
    523                        dead-properties)))))
    524          (when dead-properties
    525            (with-current-buffer (or (buffer-base-buffer) (current-buffer))
    526              (setq-local org-fold-core--indirect-buffers
    527                          (seq-filter #'buffer-live-p all-buffers))))))
    528      (dolist (buf buffers)
    529        (with-current-buffer buf
    530          (when dead-properties
    531            (with-silent-modifications
    532              (save-restriction
    533                (widen)
    534                (remove-text-properties
    535                 (point-min) (point-max)
    536                 dead-properties))))
    537          ,@body))))
    538 
    539 ;; This is the core function used to fold text in buffers.  We use
    540 ;; text properties to hide folded text, however 'invisible property is
    541 ;; not directly used (unless risky `org-fold-core--optimise-for-huge-buffers' is
    542 ;; enabled). Instead, we define unique text property (folding
    543 ;; property) for every possible folding spec and add the resulting
    544 ;; text properties into `char-property-alias-alist', so that
    545 ;; 'invisible text property is automatically defined if any of the
    546 ;; folding properties is non-nil.  This approach lets us maintain
    547 ;; multiple folds for the same text region - poor man's overlays (but
    548 ;; much faster).  Additionally, folding properties are ensured to be
    549 ;; unique for different buffers (especially for indirect
    550 ;; buffers). This is done to allow different folding states in
    551 ;; indirect buffers.
    552 (defun org-fold-core--property-symbol-get-create (spec &optional buffer return-only)
    553   "Return a unique symbol suitable as folding text property.
    554 Return value is unique for folding SPEC in BUFFER.
    555 If the buffer already have buffer-local setup in `char-property-alias-alist'
    556 and the setup appears to be created for different buffer,
    557 copy the old invisibility state into new buffer-local text properties,
    558 unless RETURN-ONLY is non-nil."
    559   (if (eq org-fold-core-style 'overlays)
    560       (or (gethash (cons 'global spec) org-fold-core--property-symbol-cache)
    561           (puthash (cons 'global spec)
    562                    (org-fold-core-get-folding-property-symbol spec nil 'global)
    563                    org-fold-core--property-symbol-cache))
    564     (let* ((buf (or buffer (current-buffer))))
    565       ;; Create unique property symbol for SPEC in BUFFER
    566       (let ((local-prop (or (gethash (cons buf spec) org-fold-core--property-symbol-cache)
    567 			    (puthash (cons buf spec)
    568                                      (org-fold-core-get-folding-property-symbol
    569                                       spec buf
    570                                       (org-fold-core-get-folding-spec-property spec :global))
    571                                      org-fold-core--property-symbol-cache))))
    572         (prog1
    573             local-prop
    574           (unless return-only
    575 	    (with-current-buffer buf
    576               ;; Update folding properties carried over from other
    577               ;; buffer (implying that current buffer is indirect
    578               ;; buffer). Normally, `char-property-alias-alist' in new
    579               ;; indirect buffer is a copy of the same variable from
    580               ;; the base buffer. Then, `char-property-alias-alist'
    581               ;; would contain folding properties, which are not
    582               ;; matching the generated `local-prop'.
    583 	      (unless (member local-prop (cdr (assq 'invisible char-property-alias-alist)))
    584                 ;; Copy all the old folding properties to preserve the folding state
    585                 (with-silent-modifications
    586                   (dolist (old-prop (cdr (assq 'invisible char-property-alias-alist)))
    587                     (org-with-wide-buffer
    588                      (let* ((pos (point-min))
    589 	                    (spec (org-fold-core-get-folding-spec-from-folding-prop old-prop))
    590                             ;; Generate new buffer-unique folding property
    591 	                    (new-prop (when spec (org-fold-core--property-symbol-get-create spec nil 'return-only))))
    592                        ;; Copy the visibility state for `spec' from `old-prop' to `new-prop'
    593                        (unless (eq old-prop new-prop)
    594                          (while (< pos (point-max))
    595 	                   (let ((val (get-text-property pos old-prop))
    596                                  (next (next-single-char-property-change pos old-prop)))
    597 	                     (when val
    598 	                       (put-text-property pos next new-prop val))
    599                              (setq pos next)))))))
    600                   ;; Update `char-property-alias-alist' with folding
    601                   ;; properties unique for the current buffer.
    602                   (setq-local char-property-alias-alist
    603 	                      (cons (cons 'invisible
    604 			                  (mapcar (lambda (spec)
    605 				                    (org-fold-core--property-symbol-get-create spec nil 'return-only))
    606 				                  (org-fold-core-folding-spec-list)))
    607 		                    (remove (assq 'invisible char-property-alias-alist)
    608 			                    char-property-alias-alist)))
    609                   ;; Set folding property stickiness according to
    610                   ;; their `:font-sticky' and `:rear-sticky'
    611                   ;; parameters.
    612                   (let (full-prop-list)
    613                     (org-fold-core-cycle-over-indirect-buffers
    614                       (setq full-prop-list
    615                             (append full-prop-list
    616                                     (delq nil
    617                                           (mapcar (lambda (spec)
    618                                                     (cond
    619                                                      ((org-fold-core-get-folding-spec-property spec :front-sticky)
    620                                                       (cons (org-fold-core--property-symbol-get-create spec nil 'return-only)
    621                                                             nil))
    622                                                      ((org-fold-core-get-folding-spec-property spec :rear-sticky)
    623                                                       nil)
    624                                                      (t
    625                                                       (cons (org-fold-core--property-symbol-get-create spec nil 'return-only)
    626                                                             t))))
    627                                                   (org-fold-core-folding-spec-list))))))
    628                     (org-fold-core-cycle-over-indirect-buffers
    629                       (setq-local text-property-default-nonsticky
    630                                   (delete-dups (append
    631                                                 text-property-default-nonsticky
    632                                                 full-prop-list))))))))))))))
    633 
    634 (defun org-fold-core--update-buffer-folds ()
    635   "Copy folding state in a new buffer with text copied from old buffer."
    636   (org-fold-core--property-symbol-get-create (car (org-fold-core-folding-spec-list))))
    637 
    638 (defun org-fold-core-decouple-indirect-buffer-folds ()
    639   "Copy and decouple folding state in a newly created indirect buffer.
    640 This function is mostly intended to be used in
    641 `clone-indirect-buffer-hook'."
    642   ;; Add current buffer to the list of indirect buffers in the base buffer.
    643   (when (buffer-base-buffer)
    644     (let ((new-buffer (current-buffer)))
    645       (with-current-buffer (buffer-base-buffer)
    646         (setq-local org-fold-core--indirect-buffers
    647                     (let (bufs)
    648                       (org-fold-core-cycle-over-indirect-buffers
    649                         (push (current-buffer) bufs))
    650                       (push new-buffer bufs)
    651                       (delete-dups bufs))))))
    652   (when (and (buffer-base-buffer)
    653              (eq org-fold-core-style 'text-properties)
    654              (not (memql 'ignore-indirect org-fold-core--optimise-for-huge-buffers)))
    655     (org-fold-core--update-buffer-folds)))
    656 
    657 ;;; API
    658 
    659 ;;;; Modifying folding specs
    660 
    661 (defun org-fold-core-set-folding-spec-property (spec property value &optional force)
    662   "Set PROPERTY of a folding SPEC to VALUE.
    663 Possible properties and values can be found in `org-fold-core--specs' docstring.
    664 Do not check previous value when FORCE is non-nil."
    665   (pcase property
    666     (:ellipsis
    667      (unless (and (not force) (equal value (org-fold-core-get-folding-spec-property spec :ellipsis)))
    668        (remove-from-invisibility-spec (cons spec (org-fold-core-get-folding-spec-property spec :ellipsis)))
    669        (unless (org-fold-core-get-folding-spec-property spec :visible)
    670          (add-to-invisibility-spec (cons spec value)))))
    671     (:visible
    672      (unless (or (memql 'merge-folds org-fold-core--optimise-for-huge-buffers)
    673                  (and (not force) (equal value (org-fold-core-get-folding-spec-property spec :visible))))
    674        (if value
    675 	   (remove-from-invisibility-spec (cons spec (org-fold-core-get-folding-spec-property spec :ellipsis)))
    676          (add-to-invisibility-spec (cons spec (org-fold-core-get-folding-spec-property spec :ellipsis))))))
    677     (:alias
    678      ;; Clear symbol cache.
    679      (setq org-fold-core--spec-symbols nil))
    680     (:isearch-open nil)
    681     (:isearch-ignore nil)
    682     (:front-sticky nil)
    683     (:rear-sticky nil)
    684     (_ nil))
    685   (setf (cdr (assq property (assq spec org-fold-core--specs))) value))
    686 
    687 (defun org-fold-core-add-folding-spec (spec &optional properties buffer append)
    688   "Add a new folding SPEC with PROPERTIES in BUFFER.
    689 
    690 SPEC must be a symbol.  BUFFER can be a buffer to set SPEC in or nil to
    691 set SPEC in current buffer.
    692 
    693 By default, the added SPEC will have highest priority among the
    694 previously defined specs.  When optional APPEND argument is non-nil,
    695 SPEC will have the lowest priority instead.  If SPEC was already
    696 defined earlier, it will be redefined according to provided optional
    697 arguments.
    698 `
    699 The folding spec properties will be set to PROPERTIES (see
    700 `org-fold-core--specs' for details)."
    701   (when (eq spec 'all) (error "Cannot use reserved folding spec symbol 'all"))
    702   (with-current-buffer (or buffer (current-buffer))
    703     ;; Clear the cache.
    704     (setq org-fold-core--spec-list nil
    705           org-fold-core--spec-symbols nil)
    706     (let* ((full-properties (mapcar (lambda (prop) (cons prop (cdr (assq prop properties))))
    707                                     '( :visible :ellipsis :isearch-ignore
    708                                        :global :isearch-open :front-sticky
    709                                        :rear-sticky :fragile :alias :font-lock)))
    710            (full-spec (cons spec full-properties)))
    711       (add-to-list 'org-fold-core--specs full-spec append)
    712       (mapc (lambda (prop-cons) (org-fold-core-set-folding-spec-property spec (car prop-cons) (cdr prop-cons) 'force)) full-properties)
    713       ;; Update buffer inivisibility specs.
    714       (org-fold-core--property-symbol-get-create spec))))
    715 
    716 (defun org-fold-core-remove-folding-spec (spec &optional buffer)
    717   "Remove a folding SPEC in BUFFER.
    718 
    719 SPEC must be a symbol.
    720 
    721 BUFFER can be a buffer to remove SPEC in, nil to remove SPEC in current
    722 buffer, or `all' to remove SPEC in all open `org-mode' buffers and all
    723 future org buffers."
    724   (org-fold-core--check-spec spec)
    725   (when (eq buffer 'all)
    726     (setq-default org-fold-core--specs (delete (cdr (assq spec org-fold-core--specs)) org-fold-core--specs))
    727     (mapc (lambda (buf)
    728 	    (org-fold-core-remove-folding-spec spec buf))
    729 	  (buffer-list)))
    730   (let ((buffer (or buffer (current-buffer))))
    731     (with-current-buffer buffer
    732       ;; Clear the cache.
    733       (setq org-fold-core--spec-list nil
    734             org-fold-core--spec-symbols nil)
    735       (org-fold-core-set-folding-spec-property spec :visible t)
    736       (setq org-fold-core--specs (delete (cdr (assq spec org-fold-core--specs)) org-fold-core--specs)))))
    737 
    738 (defun org-fold-core-initialize (&optional specs)
    739   "Setup folding in current buffer using SPECS as value of `org-fold-core--specs'."
    740   ;; Preserve the priorities.
    741   (when specs (setq specs (nreverse specs)))
    742   (unless specs (setq specs org-fold-core--specs))
    743   (setq org-fold-core--specs nil
    744         org-fold-core--spec-list nil
    745         org-fold-core--spec-symbols nil)
    746   (dolist (spec specs)
    747     (org-fold-core-add-folding-spec (car spec) (cdr spec)))
    748   (add-hook 'after-change-functions 'org-fold-core--fix-folded-region nil 'local)
    749   (add-hook 'clone-indirect-buffer-hook #'org-fold-core-decouple-indirect-buffer-folds nil 'local)
    750   ;; Setup killing text
    751   (setq-local filter-buffer-substring-function #'org-fold-core--buffer-substring-filter)
    752   (if (and (boundp 'isearch-opened-regions)
    753            (eq org-fold-core-style 'text-properties))
    754       ;; Use new implementation of isearch allowing to search inside text
    755       ;; hidden via text properties.
    756       (org-fold-core--isearch-setup 'text-properties)
    757     (org-fold-core--isearch-setup 'overlays)))
    758 
    759 ;;;; Searching and examining folded text
    760 
    761 (defsubst org-fold-core-folded-p (&optional pos spec-or-alias)
    762   "Non-nil if the character after POS is folded.
    763 If POS is nil, use `point' instead.
    764 If SPEC-OR-ALIAS is a folding spec or a list, only check the given
    765 folding spec or the listed specs."
    766   (if (and spec-or-alias (listp spec-or-alias))
    767       (catch :found
    768         (dolist (spec spec-or-alias)
    769           (let ((val (org-fold-core-get-folding-spec spec pos)))
    770             (when val (throw :found val)))))
    771     (org-fold-core-get-folding-spec spec-or-alias pos)))
    772 
    773 (defun org-fold-core-region-folded-p (beg end &optional spec-or-alias)
    774   "Non-nil if the region between BEG and END is folded.
    775 If SPEC-OR-ALIAS is a folding spec, only check the given folding spec."
    776   (org-with-point-at beg
    777     (catch :visible
    778       (while (< (point) end)
    779         (unless (org-fold-core-get-folding-spec spec-or-alias) (throw :visible nil))
    780         (goto-char (org-fold-core-next-folding-state-change spec-or-alias nil end)))
    781       t)))
    782 
    783 (defun org-fold-core-get-folding-spec (&optional spec-or-alias pom)
    784   "Get folding state at `point' or POM.
    785 Return nil if there is no folding at point or POM.
    786 If SPEC-OR-ALIAS is nil, return a folding spec with highest priority
    787 among present at `point' or POM.
    788 If SPEC-OR-ALIAS is `all', return the list of all present folding
    789 specs.
    790 If SPEC-OR-ALIAS is a valid folding spec or a spec alias, return the
    791 corresponding folding spec (if the text is folded using that spec)."
    792   (let ((spec (if (eq spec-or-alias 'all)
    793                   'all
    794                 (org-fold-core-get-folding-spec-from-alias spec-or-alias))))
    795     (when (and spec (not (eq spec 'all))) (org-fold-core--check-spec spec))
    796     (org-with-point-at pom
    797       (cond
    798        ((or (null spec) (eq spec 'all))
    799         (catch :single-spec
    800           (let ((result))
    801 	    (dolist (lspec (org-fold-core-folding-spec-list))
    802 	      (let ((val (if (eq org-fold-core-style 'text-properties)
    803                              (get-text-property (point) (org-fold-core--property-symbol-get-create lspec nil t))
    804                            (get-char-property (point) (org-fold-core--property-symbol-get-create lspec nil t)))))
    805                 (when (and val (null spec)) (throw :single-spec val))
    806 	        (when val (push val result))))
    807             (reverse result))))
    808        (t (if (eq org-fold-core-style 'text-properties)
    809               (get-text-property (point) (org-fold-core--property-symbol-get-create spec nil t))
    810             (get-char-property (point) (org-fold-core--property-symbol-get-create spec nil t))))))))
    811 
    812 (defun org-fold-core-get-folding-specs-in-region (beg end)
    813   "Get all folding specs in region from BEG to END."
    814   (let ((pos beg)
    815 	all-specs)
    816     (while (< pos end)
    817       (setq all-specs (append all-specs (org-fold-core-get-folding-spec nil pos)))
    818       (setq pos (org-fold-core-next-folding-state-change nil pos end)))
    819     (unless (listp all-specs) (setq all-specs (list all-specs)))
    820     (delete-dups all-specs)))
    821 
    822 (defun org-fold-core-get-region-at-point (&optional spec-or-alias pom)
    823   "Return region folded using SPEC-OR-ALIAS at POM.
    824 If SPEC is nil, return the largest possible folded region.
    825 The return value is a cons of beginning and the end of the region.
    826 Return nil when no fold is present at point of POM."
    827   (let ((spec (org-fold-core-get-folding-spec-from-alias spec-or-alias)))
    828     (org-with-point-at (or pom (point))
    829       (if spec
    830           (if (eq org-fold-core-style 'text-properties)
    831 	      (org-find-text-property-region (point) (org-fold-core--property-symbol-get-create spec nil t))
    832             (let ((ov (cdr (get-char-property-and-overlay (point) (org-fold-core--property-symbol-get-create spec nil t)))))
    833               (when ov (cons (overlay-start ov) (overlay-end ov)))))
    834         (let ((region (cons (point) (point))))
    835 	  (dolist (spec (org-fold-core-get-folding-spec 'all))
    836             (let ((local-region (org-fold-core-get-region-at-point spec)))
    837               (when (< (car local-region) (car region))
    838                 (setcar region (car local-region)))
    839               (when (> (cdr local-region) (cdr region))
    840                 (setcdr region (cdr local-region)))))
    841 	  (unless (eq (car region) (cdr region)) region))))))
    842 
    843 (defun org-fold-core-next-visibility-change (&optional pos limit ignore-hidden-p previous-p)
    844   "Return next point from POS up to LIMIT where text becomes visible/invisible.
    845 By default, text hidden by any means (i.e. not only by folding, but
    846 also via fontification) will be considered.
    847 If IGNORE-HIDDEN-P is non-nil, consider only folded text.
    848 If PREVIOUS-P is non-nil, search backwards."
    849   (let* ((pos (or pos (point)))
    850 	 (invisible-p (if ignore-hidden-p
    851 			  #'org-fold-core-folded-p
    852 			#'invisible-p))
    853          (invisible-initially? (funcall invisible-p pos))
    854 	 (limit (or limit (if previous-p
    855 			      (point-min)
    856 			    (point-max))))
    857          (cmp (if previous-p #'> #'<))
    858 	 (next-change (if previous-p
    859 			  (if ignore-hidden-p
    860                               (lambda (p) (org-fold-core-previous-folding-state-change (org-fold-core-get-folding-spec nil p) p limit))
    861 			    (lambda (p) (max limit (previous-single-char-property-change p 'invisible nil limit))))
    862                         (if ignore-hidden-p
    863                             (lambda (p) (org-fold-core-next-folding-state-change (org-fold-core-get-folding-spec nil p) p limit))
    864 			  (lambda (p) (next-single-char-property-change p 'invisible nil limit)))))
    865 	 (next pos))
    866     (while (and (funcall cmp next limit)
    867 		(not (org-xor
    868                     invisible-initially?
    869                     (funcall invisible-p
    870                              (if previous-p
    871                                  ;; NEXT-1 -> NEXT is the change.
    872                                  (max limit (1- next))
    873                                ;; NEXT -> NEXT+1 is the change.
    874                                next)))))
    875       (setq next (funcall next-change next)))
    876     next))
    877 
    878 (defun org-fold-core-previous-visibility-change (&optional pos limit ignore-hidden-p)
    879   "Call `org-fold-core-next-visibility-change' searching backwards."
    880   (org-fold-core-next-visibility-change pos limit ignore-hidden-p 'previous))
    881 
    882 (defun org-fold-core-next-folding-state-change (&optional spec-or-alias pos limit previous-p)
    883   "Return point after POS where folding state changes up to LIMIT.
    884 If SPEC-OR-ALIAS is nil, return next point where _any_ single folding
    885 spec changes.
    886 For example, (org-fold-core-next-folding-state-change nil) with point
    887 somewhere in the below structure will return the nearest <...> point.
    888 
    889 * Headline <begin outline fold>
    890 :PROPERTIES:<begin drawer fold>
    891 :ID: test
    892 :END:<end drawer fold>
    893 
    894 Fusce suscipit, wisi nec facilisis facilisis, est dui fermentum leo,
    895 quis tempor ligula erat quis odio.
    896 
    897 ** Another headline
    898 :DRAWER:<begin drawer fold>
    899 :END:<end drawer fold>
    900 ** Yet another headline
    901 <end of outline fold>
    902 
    903 If SPEC-OR-ALIAS is a folding spec symbol, only consider that folding
    904 spec.
    905 
    906 If SPEC-OR-ALIAS is a list, only consider changes of folding specs
    907 from the list.
    908 
    909 Search backwards when PREVIOUS-P is non-nil."
    910   (when (and spec-or-alias (symbolp spec-or-alias))
    911     (setq spec-or-alias (list spec-or-alias)))
    912   (when spec-or-alias
    913     (setq spec-or-alias
    914 	  (mapcar (lambda (spec-or-alias)
    915 		    (or (org-fold-core-get-folding-spec-from-alias spec-or-alias)
    916 			spec-or-alias))
    917                   spec-or-alias))
    918     (mapc #'org-fold-core--check-spec spec-or-alias))
    919   (unless spec-or-alias
    920     (setq spec-or-alias (org-fold-core-folding-spec-list)))
    921   (setq pos (or pos (point)))
    922   (let ((limit (or limit (if previous-p (point-min) (point-max)))))
    923     (catch :limit
    924       (dolist (prop (mapcar
    925                      (lambda (el)
    926                        (org-fold-core--property-symbol-get-create el nil t))
    927                      spec-or-alias))
    928         (when (= limit pos) (throw :limit limit))
    929         (setq
    930          limit
    931          (if previous-p
    932              (previous-single-char-property-change pos prop nil limit)
    933            (next-single-char-property-change pos prop nil limit))))
    934       limit)))
    935 
    936 (defun org-fold-core-previous-folding-state-change (&optional spec-or-alias pos limit)
    937   "Call `org-fold-core-next-folding-state-change' searching backwards."
    938   (org-fold-core-next-folding-state-change spec-or-alias pos limit 'previous))
    939 
    940 (defun org-fold-core-search-forward (spec-or-alias &optional limit)
    941   "Search next region folded via folding SPEC-OR-ALIAS up to LIMIT.
    942 Move point right after the end of the region, to LIMIT, or
    943 `point-max'.  The `match-data' will contain the region."
    944   (let ((spec (org-fold-core-get-folding-spec-from-alias spec-or-alias)))
    945     (let ((prop-symbol (org-fold-core--property-symbol-get-create spec nil t)))
    946       (goto-char (or (next-single-char-property-change (point) prop-symbol nil limit) limit (point-max)))
    947       (when (and (< (point) (or limit (point-max)))
    948 	         (not (org-fold-core-get-folding-spec spec)))
    949         (goto-char (next-single-char-property-change (point) prop-symbol nil limit)))
    950       (when (org-fold-core-get-folding-spec spec)
    951         (let ((region (org-fold-core-get-region-at-point spec)))
    952 	  (when (< (cdr region) (or limit (point-max)))
    953 	    (goto-char (1+ (cdr region)))
    954             (set-match-data (list (set-marker (make-marker) (car region) (current-buffer))
    955 				  (set-marker (make-marker) (cdr region) (current-buffer))))))))))
    956 
    957 (cl-defun org-fold-core-get-regions (&key specs from to with-markers relative)
    958   "Find all the folded regions in current buffer.
    959 
    960 Each element of the returned list represent folded region boundaries
    961 and the folding spec: (BEG END SPEC).
    962 
    963 Search folds intersecting with (FROM TO) buffer region if FROM and TO
    964 are provided.
    965 
    966 If FROM is non-nil and TO is nil, search the folded regions at FROM.
    967 
    968 When both FROM and TO are nil, search folded regions in the whole buffer.
    969 
    970 When SPECS is non-nil it should be a list of folding specs or a symbol.
    971 Only return the matching fold types.
    972 
    973 When WITH-MARKERS is non-nil, use markers to represent region
    974 boundaries.
    975 
    976 When RELATIVE is a buffer position, regions boundaries are given
    977 relative to that position.
    978 When RELATIVE is t, use FROM as the position.
    979 WITH-MARKERS must be nil when RELATIVE is non-nil."
    980   (when (and relative with-markers)
    981     (error "Cannot use markers in non-absolute region boundaries"))
    982   (when (eq relative t) (setq relative from))
    983   (unless (listp specs) (setq specs (list specs)))
    984   (let (regions region mk-region)
    985     (org-with-wide-buffer
    986      (when (and (not from) (not to))
    987        (setq from (point-min)
    988              to (point-max)))
    989      (when (and from (not to)) (setq to (point-max)))
    990      (when (and from (< from (point-min))) (setq from (point-min)))
    991      (when (and to (> to (point-max))) (setq to (point-max)))
    992      (unless from (setq from (point-min)))
    993      (dolist (spec (or specs (org-fold-core-folding-spec-list)) regions)
    994        (goto-char from)
    995        (catch :exit
    996          (while (or (not to) (< (point) to))
    997            (when (org-fold-core-get-folding-spec spec)
    998              (setq region (org-fold-core-get-region-at-point spec))
    999              (when relative
   1000                (cl-decf (car region) relative)
   1001                (cl-decf (cdr region) relative))
   1002              (if (not with-markers)
   1003                  (setq mk-region `(,(car region) ,(cdr region) ,spec))
   1004                (setq mk-region `(,(make-marker) ,(make-marker) ,spec))
   1005                (move-marker (nth 0 mk-region) (car region))
   1006                (move-marker (nth 1 mk-region) (cdr region)))
   1007              (push mk-region regions))
   1008            (unless to (throw :exit nil))
   1009            (goto-char (org-fold-core-next-folding-state-change spec nil to))))))))
   1010 
   1011 ;;;; Changing visibility
   1012 
   1013 ;;;;; Region visibility
   1014 
   1015 (defvar org-fold-core--keep-overlays nil
   1016   "When non-nil, `org-fold-core-region' will not remove existing overlays.")
   1017 (defvar org-fold-core--isearch-overlays) ; defined below
   1018 (defmacro org-fold-core--keep-overlays (&rest body)
   1019   "Run BODY with `org-fold-core--keep-overlays' set to t."
   1020   (declare (debug (body)))
   1021   `(let ((org-fold-core--keep-overlays t))
   1022      ,@body))
   1023 
   1024 (defvar org-fold-core--isearch-active nil
   1025   "When non-nil, `org-fold-core-region' records created overlays.
   1026 New overlays will be added to `org-fold-core--isearch-overlays'.")
   1027 (defmacro org-fold-core--with-isearch-active (&rest body)
   1028   "Run BODY with `org-fold-core--isearch-active' set to t."
   1029   (declare (debug (body)))
   1030   `(let ((org-fold-core--isearch-active t))
   1031      ,@body))
   1032 
   1033 ;; This is the core function performing actual folding/unfolding.  The
   1034 ;; folding state is stored in text property (folding property)
   1035 ;; returned by `org-fold-core--property-symbol-get-create'.  The value of the
   1036 ;; folding property is folding spec symbol.
   1037 (defun org-fold-core-region (from to flag &optional spec-or-alias)
   1038   "Hide or show lines from FROM to TO, according to FLAG.
   1039 SPEC-OR-ALIAS is the folding spec or foldable element, as a symbol.
   1040 If SPEC-OR-ALIAS is omitted and FLAG is nil, unfold everything in the region."
   1041   (let ((spec (org-fold-core-get-folding-spec-from-alias spec-or-alias)))
   1042     (when spec (org-fold-core--check-spec spec))
   1043     (with-silent-modifications
   1044       (org-with-wide-buffer
   1045        ;; Arrange fontifying newlines after all the folds between FROM
   1046        ;; and TO to match the first character before the fold; not the
   1047        ;; last as per Emacs defaults.  This makes :extend faces span
   1048        ;; past the ellipsis.  See bug#65896.  The face properties are
   1049        ;; assigned via `org-activate-folds'.
   1050        (when (or (not spec) (org-fold-core-get-folding-spec-property spec :font-lock))
   1051          (when (equal ?\n (char-after from))
   1052            (font-lock-flush from (1+ from)))
   1053          (when (equal ?\n (char-after to))
   1054            (font-lock-flush to (1+ to)))
   1055          (dolist (region (org-fold-core-get-regions :from from :to to :specs spec))
   1056            (when (equal ?\n (char-after (cadr region)))
   1057              (font-lock-flush (cadr region) (1+ (cadr region))))
   1058            ;; Re-fontify beginning of the fold - we may
   1059            ;; unfold inside an existing fold, with FROM begin a newline
   1060            ;; after spliced fold.
   1061            (when (equal ?\n (char-after (car region)))
   1062              (font-lock-flush (car region) (1+ (car region))))))
   1063        (when (eq org-fold-core-style 'overlays)
   1064          (if org-fold-core--keep-overlays
   1065              (mapc
   1066               (lambda (ov)
   1067                 (when (or (not spec)
   1068                           (eq spec (overlay-get ov 'invisible)))
   1069                   (when (and org-fold-core--isearch-active
   1070                              (overlay-get ov 'invisible)
   1071                              (org-fold-core-get-folding-spec-property
   1072                               (overlay-get ov 'invisible) :isearch-open))
   1073                     (when (overlay-get ov 'invisible)
   1074                       (overlay-put ov 'org-invisible (overlay-get ov 'invisible)))
   1075                     (overlay-put ov 'invisible nil)
   1076                     (when org-fold-core--isearch-active
   1077                       (cl-pushnew ov org-fold-core--isearch-overlays)))))
   1078               (overlays-in from to))
   1079            (when spec
   1080              (remove-overlays from to 'org-invisible spec)
   1081              (remove-overlays from to 'invisible spec))))
   1082        (if flag
   1083 	   (if (not spec)
   1084                (error "Calling `org-fold-core-region' with missing SPEC")
   1085              (if (eq org-fold-core-style 'overlays)
   1086                  ;; Use `front-advance' since text right before to the beginning of
   1087                  ;; the overlay belongs to the visible line than to the contents.
   1088                  (let ((o (make-overlay from to nil
   1089                                         (org-fold-core-get-folding-spec-property spec :front-sticky)
   1090                                         (org-fold-core-get-folding-spec-property spec :rear-sticky))))
   1091                    (when org-fold-core--isearch-active
   1092                      (push o org-fold-core--isearch-overlays))
   1093                    (overlay-put o 'evaporate t)
   1094                    (overlay-put o (org-fold-core--property-symbol-get-create spec) spec)
   1095                    (overlay-put o 'invisible spec)
   1096                    ;; Preserve priority.
   1097                    (overlay-put o 'priority (length (member spec (org-fold-core-folding-spec-list))))
   1098                    (overlay-put o 'isearch-open-invisible #'org-fold-core--isearch-show))
   1099 	       (put-text-property from to (org-fold-core--property-symbol-get-create spec) spec)
   1100 	       (put-text-property from to 'isearch-open-invisible #'org-fold-core--isearch-show)
   1101 	       (put-text-property from to 'isearch-open-invisible-temporary #'org-fold-core--isearch-show-temporary)
   1102                (when (memql 'grab-invisible org-fold-core--optimise-for-huge-buffers)
   1103                  ;; If the SPEC has highest priority, assign it directly
   1104                  ;; to 'invisible property as well.  This is done to speed
   1105                  ;; up Emacs redisplay on huge (Mbs) folded regions where
   1106                  ;; we don't even want Emacs to spend time cycling over
   1107                  ;; `char-property-alias-alist'.
   1108                  (when (eq spec (caar org-fold-core--specs)) (put-text-property from to 'invisible spec)))))
   1109          (if (not spec)
   1110              (mapc (lambda (spec) (org-fold-core-region from to nil spec)) (org-fold-core-folding-spec-list))
   1111            (when (and (memql 'grab-invisible org-fold-core--optimise-for-huge-buffers)
   1112                       (eq org-fold-core-style 'text-properties))
   1113              (when (eq spec (caar org-fold-core--specs))
   1114                (let ((pos from))
   1115                  (while (< pos to)
   1116                    (if (eq spec (get-text-property pos 'invisible))
   1117                        (let ((next (org-fold-core-next-folding-state-change spec pos to)))
   1118                          (remove-text-properties pos next '(invisible t))
   1119                          (setq pos next))
   1120                      (setq pos (next-single-char-property-change pos 'invisible nil to)))))))
   1121            (when (eq org-fold-core-style 'text-properties)
   1122 	     (remove-text-properties from to (list (org-fold-core--property-symbol-get-create spec) nil)))))
   1123        ;; Re-calculate trailing faces for all the folds revealed
   1124        ;; by unfolding or created by folding.
   1125        (when (or (not spec) (org-fold-core-get-folding-spec-property spec :font-lock))
   1126          (dolist (region (org-fold-core-get-regions :from from :to to :specs spec))
   1127            (when (equal ?\n (char-after (cadr region)))
   1128              (font-lock-flush (cadr region) (1+ (cadr region))))))))))
   1129 
   1130 (cl-defmacro org-fold-core-regions (regions &key override clean-markers relative)
   1131   "Fold every region in REGIONS list in current buffer.
   1132 
   1133 Each region in the list is a list (BEG END SPEC-OR-ALIAS) describing
   1134 region and folding spec to be applied.
   1135 
   1136 When optional argument OVERRIDE is non-nil, clear folding state in the
   1137 buffer first.
   1138 
   1139 When optional argument CLEAN-MARKERS is non-nil, clear markers used to
   1140 mark region boundaries in REGIONS.
   1141 
   1142 When optional argument RELATIVE is non-nil, it must be a buffer
   1143 position.  REGION boundaries are then treated as relative distances
   1144 from that position."
   1145   `(org-with-wide-buffer
   1146     (when ,override (org-fold-core-region (point-min) (point-max) nil))
   1147     (pcase-dolist (`(,beg ,end ,spec) (delq nil ,regions))
   1148       (let ((rel ,relative))
   1149         (if rel
   1150             (org-fold-core-region (+ rel beg) (+ rel end) t spec)
   1151           (org-fold-core-region beg end t spec)))
   1152       (when ,clean-markers
   1153         (when (markerp beg) (set-marker beg nil))
   1154         (when (markerp end) (set-marker end nil))))))
   1155 
   1156 (defmacro org-fold-core-save-visibility (use-markers &rest body)
   1157   "Save and restore folding state around BODY.
   1158 If USE-MARKERS is non-nil, use markers for the positions.  This
   1159 means that the buffer may change while running BODY, but it also
   1160 means that the buffer should stay alive during the operation,
   1161 because otherwise all these markers will point to nowhere."
   1162   (declare (debug (form body)) (indent 1))
   1163   (org-with-gensyms (regions)
   1164     `(let* ((,regions (org-fold-core-get-regions :with-markers ,use-markers)))
   1165        (unwind-protect (progn ,@body)
   1166          (org-fold-core-regions ,regions :override t :clean-markers t)))))
   1167 
   1168 ;;; Make isearch search in some text hidden via text propertoes
   1169 
   1170 (defvar org-fold-core--isearch-overlays nil
   1171   "List of overlays temporarily created during isearch.
   1172 This is used to allow searching in regions hidden via text properties.
   1173 As for [2020-05-09 Sat], Isearch only has special handling of hidden overlays.
   1174 Any text hidden via text properties is not revealed even if `search-invisible'
   1175 is set to t.")
   1176 
   1177 (defvar-local org-fold-core--isearch-local-regions (make-hash-table :test 'equal)
   1178   "Hash table storing temporarily shown folds from isearch matches.")
   1179 
   1180 (defun org-fold-core--isearch-setup (type)
   1181   "Initialize isearch in org buffer.
   1182 TYPE can be either `text-properties' or `overlays'."
   1183   (pcase type
   1184     (`text-properties
   1185      (setq-local search-invisible 'open-all)
   1186      (add-hook 'isearch-mode-end-hook #'org-fold-core--clear-isearch-state nil 'local)
   1187      (add-hook 'isearch-mode-hook #'org-fold-core--clear-isearch-state nil 'local)
   1188      (setq-local isearch-filter-predicate #'org-fold-core--isearch-filter-predicate-text-properties))
   1189     (`overlays
   1190      (when (eq org-fold-core-style 'text-properties)
   1191        (add-function :before (local 'isearch-filter-predicate) #'org-fold-core--create-isearch-overlays)
   1192        ;; When `isearch-filter-predicate' is called outside isearch,
   1193        ;; it is common that `isearch-mode-end-hook' does not get
   1194        ;; executed, but `isearch-clean-overlays' usually does.
   1195        (advice-add
   1196         'isearch-clean-overlays :after
   1197         #'org-fold-core--clear-isearch-overlays
   1198         '((name . isearch-clean-overlays@org-fold-core)))))
   1199     (_ (error "%s: Unknown type of setup for `org-fold-core--isearch-setup'" type))))
   1200 
   1201 (defun org-fold-core--isearch-reveal (pos)
   1202   "Default function used to reveal hidden text at POS for isearch."
   1203   (let ((region (org-fold-core-get-region-at-point nil pos)))
   1204     (org-fold-core-region (car region) (cdr region) nil)))
   1205 
   1206 (defun org-fold-core--isearch-filter-predicate-text-properties (beg end)
   1207   "Make sure that folded text is searchable when user want so.
   1208 This function is intended to be used as `isearch-filter-predicate'."
   1209   (and
   1210    ;; Check folding specs that cannot be searched
   1211    (not (memq nil (mapcar (lambda (spec) (not (org-fold-core-get-folding-spec-property spec :isearch-ignore)))
   1212                       (org-fold-core-get-folding-specs-in-region beg end))))
   1213    ;; Check 'invisible properties that are not folding specs.
   1214    (or (eq search-invisible t) ; User wants to search anyway, allow it.
   1215        (let ((pos beg)
   1216 	     unknown-invisible-property)
   1217 	 (while (and (< pos end)
   1218 		     (not unknown-invisible-property))
   1219 	   (when (and (get-text-property pos 'invisible)
   1220                       (not (org-fold-core-folding-spec-p (get-text-property pos 'invisible))))
   1221 	     (setq unknown-invisible-property t))
   1222 	   (setq pos (next-single-char-property-change pos 'invisible)))
   1223 	 (not unknown-invisible-property)))
   1224    (or (and (eq search-invisible t)
   1225 	    ;; FIXME: this opens regions permanenly for now.
   1226             ;; I also tried to force search-invisible 'open-all around
   1227             ;; `isearch-range-invisible', but that somehow causes
   1228             ;; infinite loop in `isearch-lazy-highlight'.
   1229             (prog1 t
   1230 	      ;; We still need to reveal the folded location
   1231 	      (org-fold-core--isearch-show-temporary (cons beg end) nil)))
   1232        (not (isearch-range-invisible beg end)))))
   1233 
   1234 (defun org-fold-core--clear-isearch-state ()
   1235   "Clear `org-fold-core--isearch-local-regions'."
   1236   (clrhash org-fold-core--isearch-local-regions))
   1237 
   1238 (defun org-fold-core--isearch-show (overlay-or-region)
   1239   "Reveal text at OVERLAY-OR-REGION found by isearch."
   1240   (let (beg end)
   1241     (if (overlayp overlay-or-region)
   1242         (setq beg (overlay-start overlay-or-region)
   1243               end (overlay-end overlay-or-region))
   1244       (setq beg (car overlay-or-region)
   1245             end (cdr overlay-or-region)))
   1246     ;; FIXME: Reveal the match (usually point, but may sometimes go beyond the region).
   1247     (when (< beg (point) end)
   1248       (funcall org-fold-core-isearch-open-function (point)))
   1249     (if (overlayp overlay-or-region)
   1250         (delete-overlay overlay-or-region)
   1251       (org-fold-core-region beg end nil))))
   1252 
   1253 (defun org-fold-core--isearch-show-temporary (region hide-p)
   1254   "Temporarily reveal text in REGION.
   1255 Hide text instead if HIDE-P is non-nil.
   1256 REGION can also be an overlay in current buffer."
   1257   (save-match-data ; match data must not be modified.
   1258     (let ((org-fold-core-style (if (overlayp region) 'overlays 'text-properties)))
   1259       (if hide-p
   1260           (if (not (overlayp region))
   1261               nil ;; FIXME: after isearch supports text properties.
   1262             (when (overlay-get region 'org-invisible)
   1263               (overlay-put region 'invisible (overlay-get region 'org-invisible))))
   1264         ;; isearch expects all the temporarily opened overlays to exist.
   1265         ;; See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=60399
   1266         (org-fold-core--keep-overlays
   1267          (org-fold-core--with-isearch-active
   1268           (org-fold-core--isearch-show region)))))))
   1269 
   1270 (defvar-local org-fold-core--isearch-special-specs nil
   1271   "List of specs that can break visibility state when converted to overlays.
   1272 This is a hack, but I do not see a better way around until isearch
   1273 gets support of text properties.")
   1274 (defun org-fold-core--create-isearch-overlays (beg end)
   1275   "Replace text property invisibility spec by overlays between BEG and END.
   1276 All the searchable folded regions will be changed to use overlays
   1277 instead of text properties.  The created overlays will be stored in
   1278 `org-fold-core--isearch-overlays'."
   1279   (let ((pos beg))
   1280     (while (< pos end)
   1281       ;; We need loop below to make sure that we clean all invisible
   1282       ;; properties, which may be nested.
   1283       (catch :repeat
   1284         (dolist (spec (org-fold-core-get-folding-spec 'all pos))
   1285           (unless (org-fold-core-get-folding-spec-property spec :isearch-ignore)
   1286 	    (let* ((region (org-fold-core-get-region-at-point spec pos)))
   1287               (when (memq spec org-fold-core--isearch-special-specs)
   1288                 (setq end (max end (cdr region)))
   1289                 (when (< (car region) beg)
   1290                   (setq beg (car region))
   1291                   (setq pos beg)
   1292                   (throw :repeat t)))
   1293 	      ;; Changing text properties is considered buffer modification.
   1294 	      ;; We do not want it here.
   1295 	      (with-silent-modifications
   1296                 (org-fold-core-region (car region) (cdr region) nil spec)
   1297                 (let ((org-fold-core-style 'overlays))
   1298                   (org-fold-core--with-isearch-active
   1299                    (org-fold-core-region (car region) (cdr region) t spec)))))))
   1300         (setq pos (org-fold-core-next-folding-state-change nil pos end))))))
   1301 
   1302 (defun org-fold-core--clear-isearch-overlay (ov)
   1303   "Convert OV region back into using text properties."
   1304   (let ((spec (overlay-get ov 'invisible)))
   1305     ;; Ignore deleted overlays.
   1306     (when (and spec
   1307 	       (overlay-buffer ov))
   1308       ;; Changing text properties is considered buffer modification.
   1309       ;; We do not want it here.
   1310       (with-silent-modifications
   1311 	(when (<= (overlay-end ov) (point-max))
   1312 	  (org-fold-core-region (overlay-start ov) (overlay-end ov) t spec)))))
   1313   (delete-overlay ov))
   1314 
   1315 (defun org-fold-core--clear-isearch-overlays ()
   1316   "Convert overlays from `org-fold-core--isearch-overlays' back to text properties."
   1317   (when org-fold-core--isearch-overlays
   1318     (mapc #'org-fold-core--clear-isearch-overlay org-fold-core--isearch-overlays)
   1319     (setq org-fold-core--isearch-overlays nil)))
   1320 
   1321 ;;; Handling changes in folded elements
   1322 
   1323 (defvar org-fold-core--ignore-modifications nil
   1324   "Non-nil: skip processing modifications in `org-fold-core--fix-folded-region'.")
   1325 (defvar org-fold-core--ignore-fragility-checks nil
   1326   "Non-nil: skip fragility checks in `org-fold-core--fix-folded-region'.")
   1327 (defvar org-fold-core--suppress-folding-fix nil
   1328   "Non-nil: skip folding fix in `org-fold-core--fix-folded-region'.")
   1329 
   1330 (defmacro org-fold-core-ignore-modifications (&rest body)
   1331   "Run BODY ignoring buffer modifications in `org-fold-core--fix-folded-region'."
   1332   (declare (debug (form body)) (indent 0))
   1333   `(let ((org-fold-core--ignore-modifications t))
   1334      (unwind-protect (progn ,@body)
   1335        (setq org-fold-core--last-buffer-chars-modified-tick (buffer-chars-modified-tick)))))
   1336 
   1337 (defmacro org-fold-core-suppress-folding-fix (&rest body)
   1338   "Run BODY skipping re-folding checks in `org-fold-core--fix-folded-region'."
   1339   (declare (debug (form body)) (indent 0))
   1340   `(let ((org-fold-core--suppress-folding-fix t))
   1341      (progn ,@body)))
   1342 
   1343 (defmacro org-fold-core-ignore-fragility-checks (&rest body)
   1344   "Run BODY skipping :fragility checks in `org-fold-core--fix-folded-region'."
   1345   (declare (debug (form body)) (indent 0))
   1346   `(let ((org-fold-core--ignore-fragility-checks t))
   1347      (progn ,@body)))
   1348 
   1349 (defvar org-fold-core--region-delayed-list nil
   1350   "List holding (MKFROM MKTO FLAG SPEC-OR-ALIAS) arguments to process.
   1351 The list is used by `org-fold-core--region-delayed'.")
   1352 (defun org-fold-core--region-delayed (from to flag &optional spec-or-alias)
   1353   "Call `org-fold-core-region' after current command.
   1354 Pass the same FROM, TO, FLAG, and SPEC-OR-ALIAS."
   1355   ;; Setup delayed folding.
   1356   (add-hook 'post-command-hook #'org-fold-core--process-delayed)
   1357   (let ((frommk (make-marker))
   1358         (tomk (make-marker)))
   1359     (set-marker frommk from (current-buffer))
   1360     (set-marker tomk to (current-buffer))
   1361     (push (list frommk tomk flag spec-or-alias) org-fold-core--region-delayed-list)))
   1362 
   1363 (defun org-fold-core--process-delayed ()
   1364   "Perform folding for `org-fold-core--region-delayed-list'."
   1365   (when org-fold-core--region-delayed-list
   1366     (mapc (lambda (args)
   1367             (when (and (buffer-live-p (marker-buffer (nth 0 args)))
   1368                        (buffer-live-p (marker-buffer (nth 1 args)))
   1369                        (< (nth 0 args) (nth 1 args)))
   1370               (org-with-point-at (car args)
   1371                 (apply #'org-fold-core-region args))))
   1372           ;; Restore the initial folding order.
   1373           (nreverse org-fold-core--region-delayed-list))
   1374     ;; Cleanup `post-command-hook'.
   1375     (remove-hook 'post-command-hook #'org-fold-core--process-delayed)
   1376     (setq org-fold-core--region-delayed-list nil)))
   1377 
   1378 (defvar-local org-fold-core--last-buffer-chars-modified-tick nil
   1379   "Variable storing the last return value of `buffer-chars-modified-tick'.")
   1380 
   1381 (defun org-fold-core--fix-folded-region (from to _)
   1382   "Process modifications in folded elements within FROM . TO region.
   1383 This function intended to be used as one of `after-change-functions'.
   1384 
   1385 This function does nothing if text the only modification was changing
   1386 text properties (for the sake of reducing overheads).
   1387 
   1388 If a text was inserted into invisible region, hide the inserted text.
   1389 If a text was inserted in front/back of the region, hide it according
   1390 to :front-sticky/:rear-sticky folding spec property.
   1391 
   1392 If the folded region is folded with a spec with non-nil :fragile
   1393 property, unfold the region if the :fragile function returns non-nil."
   1394   ;; If no insertions or deletions in buffer, skip all the checks.
   1395   (unless (or org-fold-core--ignore-modifications
   1396               (eq org-fold-core--last-buffer-chars-modified-tick (buffer-chars-modified-tick))
   1397               (memql 'ignore-modification-checks org-fold-core--optimise-for-huge-buffers))
   1398     ;; Store the new buffer modification state.
   1399     (setq org-fold-core--last-buffer-chars-modified-tick (buffer-chars-modified-tick))
   1400     (save-match-data
   1401       ;; Handle changes in all the indirect buffers and in the base
   1402       ;; buffer.  Work around Emacs bug#46982.
   1403       ;; Re-hide text inserted in the middle/front/back of a folded
   1404       ;; region.
   1405       (unless (or org-fold-core--suppress-folding-fix (equal from to)) ; Ignore deletions.
   1406         (when (eq org-fold-core-style 'text-properties)
   1407           (org-fold-core-cycle-over-indirect-buffers
   1408 	    (dolist (spec (org-fold-core-folding-spec-list))
   1409               ;; Reveal fully invisible text inserted in the middle
   1410               ;; of visible portion of the buffer.  This is needed,
   1411               ;; for example, when there was a deletion in a folded
   1412               ;; heading, the heading was unfolded, end `undo' was
   1413               ;; called.  The `undo' would insert the folded text.
   1414               (when (and (or (eq from (point-min))
   1415                              (not (org-fold-core-folded-p (1- from) spec)))
   1416                          (or (eq to (point-max))
   1417                              (not (org-fold-core-folded-p to spec)))
   1418                          (org-fold-core-region-folded-p from to spec))
   1419                 (org-fold-core-region from to nil spec))
   1420               ;; Look around and fold the new text if the nearby folds are
   1421               ;; sticky.
   1422               (unless (org-fold-core-region-folded-p from to spec)
   1423 	        (let ((spec-to (org-fold-core-get-folding-spec spec (min to (1- (point-max)))))
   1424 		      (spec-from (org-fold-core-get-folding-spec spec (max (point-min) (1- from)))))
   1425                   ;; Reveal folds around undone deletion.
   1426                   (when undo-in-progress
   1427                     (let ((lregion (org-fold-core-get-region-at-point spec (max (point-min) (1- from))))
   1428                           (rregion (org-fold-core-get-region-at-point spec (min to (1- (point-max))))))
   1429                       (if (and lregion rregion)
   1430                           (org-fold-core-region (car lregion) (cdr rregion) nil spec)
   1431                         (when lregion
   1432                           (org-fold-core-region (car lregion) (cdr lregion) nil spec))
   1433                         (when rregion
   1434                           (org-fold-core-region (car rregion) (cdr rregion) nil spec)))))
   1435                   ;; Hide text inserted in the middle of a fold.
   1436 	          (when (and (or spec-from (eq from (point-min)))
   1437                              (or spec-to (eq to (point-max)))
   1438                              (or spec-from spec-to)
   1439                              (eq spec-to spec-from)
   1440                              (or (org-fold-core-get-folding-spec-property spec :front-sticky)
   1441                                  (org-fold-core-get-folding-spec-property spec :rear-sticky)))
   1442                     (unless (and (eq from (point-min)) (eq to (point-max))) ; Buffer content replaced.
   1443 	              (org-fold-core-region from to t (or spec-from spec-to))))
   1444                   ;; Hide text inserted at the end of a fold.
   1445                   (when (and spec-from (org-fold-core-get-folding-spec-property spec-from :rear-sticky))
   1446                     (org-fold-core-region from to t spec-from))
   1447                   ;; Hide text inserted in front of a fold.
   1448                   (when (and spec-to
   1449                              (not (eq to (point-max))) ; Text inserted at the end of buffer is not prepended anywhere.
   1450                              (org-fold-core-get-folding-spec-property spec-to :front-sticky))
   1451                     (org-fold-core-region from to t spec-to))))))))
   1452       ;; Process all the folded text between `from' and `to'.  Do it
   1453       ;; only in current buffer to avoid verifying semantic structure
   1454       ;; multiple times in indirect buffers that have exactly same
   1455       ;; text anyway.
   1456       (unless (or org-fold-core--ignore-fragility-checks
   1457                   (memql 'ignore-fragility-checks org-fold-core--optimise-for-huge-buffers))
   1458         (dolist (func org-fold-core-extend-changed-region-functions)
   1459           (let ((new-region (funcall func from to)))
   1460             (setq from (car new-region))
   1461             (setq to (cdr new-region))))
   1462         (org-fold-core-cycle-over-indirect-buffers
   1463           (dolist (spec (org-fold-core-folding-spec-list))
   1464             ;; No action is needed when :fragile is nil for the spec.
   1465             (when (org-fold-core-get-folding-spec-property spec :fragile)
   1466               (org-with-wide-buffer
   1467                ;; Expand the considered region to include partially present fold.
   1468                ;; Note: It is important to do this inside loop over all
   1469                ;; specs.  Otherwise, the region may be expanded to huge
   1470                ;; outline fold, potentially involving majority of the
   1471                ;; buffer.  That would cause the below code to loop over
   1472                ;; almost all the folds in buffer, which would be too slow.
   1473                (let ((local-from from)
   1474                      (local-to to)
   1475                      (region-from (org-fold-core-get-region-at-point spec (max (point-min) (1- from))))
   1476                      (region-to (org-fold-core-get-region-at-point spec (min to (1- (point-max))))))
   1477                  (when region-from (setq local-from (car region-from)))
   1478                  (when region-to (setq local-to (cdr region-to)))
   1479                  (let ((pos local-from))
   1480                    ;; Move to the first hidden region.
   1481                    (unless (org-fold-core-get-folding-spec spec pos)
   1482                      (setq pos (org-fold-core-next-folding-state-change spec pos local-to)))
   1483                    ;; Cycle over all the folds.
   1484                    (while (< pos local-to)
   1485                      (save-match-data ; we should not clobber match-data in after-change-functions
   1486                        (let ((fold-begin (and (org-fold-core-get-folding-spec spec pos)
   1487                                               pos))
   1488                              (fold-end (org-fold-core-next-folding-state-change spec pos local-to)))
   1489                          (when (and fold-begin fold-end)
   1490                            (when (save-excursion
   1491                                    (funcall (org-fold-core-get-folding-spec-property spec :fragile)
   1492                                             (cons fold-begin fold-end)
   1493                                             spec))
   1494                              ;; Reveal completely, not just from the SPEC.
   1495                              ;; Do it only after command is finished -
   1496                              ;; some Emacs commands assume that
   1497                              ;; visibility is not altered by `after-change-functions'.
   1498                              (org-fold-core--region-delayed fold-begin fold-end nil)))))
   1499                      ;; Move to next fold.
   1500                      (setq pos (org-fold-core-next-folding-state-change spec pos local-to)))))))))))))
   1501 
   1502 ;;; Handling killing/yanking of folded text
   1503 
   1504 ;; By default, all the text properties of the killed text are
   1505 ;; preserved, including the folding text properties.  This can be
   1506 ;; awkward when we copy a text from an indirect buffer to another
   1507 ;; indirect buffer (or the base buffer).  The copied text might be
   1508 ;; visible in the source buffer, but might disappear if we yank it in
   1509 ;; another buffer.  This happens in the following situation:
   1510 ;; ---- base buffer ----
   1511 ;; * Headline<begin fold>
   1512 ;; Some text hidden in the base buffer, but revealed in the indirect
   1513 ;; buffer.<end fold>
   1514 ;; * Another headline
   1515 ;;
   1516 ;; ---- end of base buffer ----
   1517 ;; ---- indirect buffer ----
   1518 ;; * Headline
   1519 ;; Some text hidden in the base buffer, but revealed in the indirect
   1520 ;; buffer.
   1521 ;; * Another headline
   1522 ;;
   1523 ;; ---- end of indirect buffer ----
   1524 ;; If we copy the text under "Headline" from the indirect buffer and
   1525 ;; insert it under "Another headline" in the base buffer, the inserted
   1526 ;; text will be hidden since it's folding text properties are copied.
   1527 ;; Basically, the copied text would have two sets of folding text
   1528 ;; properties: (1) Properties for base buffer telling that the text is
   1529 ;; hidden; (2) Properties for the indirect buffer telling that the
   1530 ;; text is visible.  The first set of the text properties in inactive
   1531 ;; in the indirect buffer, but will become active once we yank the
   1532 ;; text back into the base buffer.
   1533 ;;
   1534 ;; To avoid the above situation, we simply clear all the properties,
   1535 ;; unrealated to current buffer when a text is copied.
   1536 ;; FIXME: Ideally, we may want to carry the folding state of copied
   1537 ;; text between buffer (probably via user customization).
   1538 (defun org-fold-core--buffer-substring-filter (beg end &optional delete)
   1539   "Clear folding state in killed text.
   1540 This function is intended to be used as `filter-buffer-substring-function'.
   1541 The arguments and return value are as specified for `filter-buffer-substring'."
   1542   (let ((return-string (buffer-substring--filter beg end delete))
   1543 	;; The list will be used as an argument to `remove-text-properties'.
   1544 	props-list)
   1545     ;; There is no easy way to examine all the text properties of a
   1546     ;; string, so we utilize the fact that printed string
   1547     ;; representation lists all its properties.
   1548     ;; Loop over the elements of string representation.
   1549     (unless (or (string= "" return-string)
   1550                 (<= end beg)
   1551                 (eq org-fold-core-style 'overlays))
   1552       ;; Collect all the text properties the string is completely
   1553       ;; hidden with.
   1554       (dolist (spec (org-fold-core-folding-spec-list))
   1555         (when (and (org-fold-core-region-folded-p beg end spec)
   1556                    (org-region-invisible-p beg end))
   1557           (push (org-fold-core--property-symbol-get-create spec nil t) props-list)))
   1558       (dolist (plist
   1559                (if (fboundp 'object-intervals)
   1560                    (object-intervals return-string)
   1561                  ;; Backward compatibility with Emacs <28.
   1562                  ;; FIXME: Is there any better way to do it?
   1563                  ;; Yes, it is a hack.
   1564                  ;; The below gives us string representation as a list.
   1565                  ;; Note that we need to remove unreadable values, like markers (#<...>).
   1566                  (seq-partition
   1567                   (cdr (let ((data (read (replace-regexp-in-string
   1568                                           "^#(" "("
   1569                                           (replace-regexp-in-string
   1570                                            " #(" " ("
   1571                                            (replace-regexp-in-string
   1572                                             "#<[^>]+>" "dummy"
   1573                                             ;; Get text representation of the string object.
   1574                                             ;; Make sure to print everything (see `prin1' docstring).
   1575                                             ;; `prin1' is used to print "%S" format.
   1576                                             (let (print-level print-length)
   1577                                               (format "%S" return-string))))))))
   1578                          (if (listp data) data (list data))))
   1579                   3)))
   1580         (let* ((start (car plist))
   1581                (fin (cadr plist))
   1582                (plist (car (cddr plist))))
   1583           ;; Only lists contain text properties.
   1584           (when (listp plist)
   1585             ;; Collect all the relevant text properties.
   1586             (while plist
   1587               (let* ((prop (car plist))
   1588                      (prop-name (symbol-name prop)))
   1589                 ;; Reveal hard-hidden text.  See
   1590                 ;; `org-fold-core--optimise-for-huge-buffers'.
   1591                 (when (and (eq prop 'invisible)
   1592                            (member (cadr plist) (org-fold-core-folding-spec-list)))
   1593                   (remove-text-properties start fin '(invisible t) return-string))
   1594                 ;; We do not care about values now.
   1595                 (setq plist (cddr plist))
   1596                 (when (string-match-p org-fold-core--spec-property-prefix prop-name)
   1597                   ;; Leave folding specs from current buffer.  See
   1598                   ;; comments in `org-fold-core--property-symbol-get-create' to
   1599                   ;; understand why it works.
   1600                   (unless (member prop (cdr (assq 'invisible char-property-alias-alist)))
   1601                     (push prop props-list))))))))
   1602       (remove-text-properties 0 (length return-string) props-list return-string))
   1603     return-string))
   1604 
   1605 (defun org-fold-core-update-optimisation (beg end)
   1606   "Update huge buffer optimization between BEG and END.
   1607 See `org-fold-core--optimise-for-huge-buffers'."
   1608   (when (and (memql 'grab-invisible org-fold-core--optimise-for-huge-buffers)
   1609              (eq org-fold-core-style 'text-properties))
   1610     (let ((pos beg))
   1611       (while (< pos end)
   1612         (when (and (org-fold-core-folded-p pos (caar org-fold-core--specs))
   1613                    (not (eq (caar org-fold-core--specs) (get-text-property pos 'invisible))))
   1614           (put-text-property pos (org-fold-core-next-folding-state-change (caar org-fold-core--specs) pos end)
   1615                              'invisible (caar org-fold-core--specs)))
   1616         (setq pos (org-fold-core-next-folding-state-change (caar org-fold-core--specs) pos end))))))
   1617 
   1618 (defun org-fold-core-remove-optimisation (beg end)
   1619   "Remove huge buffer optimization between BEG and END.
   1620 See `org-fold-core--optimise-for-huge-buffers'."
   1621   (when (and (memql 'grab-invisible org-fold-core--optimise-for-huge-buffers)
   1622              (eq org-fold-core-style 'text-properties))
   1623     (let ((pos beg))
   1624       (while (< pos end)
   1625         (if (and (org-fold-core-folded-p pos (caar org-fold-core--specs))
   1626                  (eq (caar org-fold-core--specs) (get-text-property pos 'invisible)))
   1627             (remove-text-properties pos (org-fold-core-next-folding-state-change (caar org-fold-core--specs) pos end)
   1628                                     '(invisible t)))
   1629         (setq pos (org-fold-core-next-folding-state-change (caar org-fold-core--specs) pos end))))))
   1630 
   1631 (provide 'org-fold-core)
   1632 
   1633 ;;; org-fold-core.el ends here