Function: org-fold-core-region

org-fold-core-region is a byte-compiled function defined in org-fold-core.el.gz.

Signature

(org-fold-core-region FROM TO FLAG &optional SPEC-OR-ALIAS)

Documentation

Hide or show lines from FROM to TO, according to FLAG.

SPEC-OR-ALIAS is the folding spec or foldable element, as a symbol. If SPEC-OR-ALIAS is omitted and FLAG is nil, unfold everything in the region.

Aliases

org-flag-region (obsolete since 9.6) org-fold-region

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-fold-core.el.gz
;; This is the core function performing actual folding/unfolding.  The
;; folding state is stored in text property (folding property)
;; returned by `org-fold-core--property-symbol-get-create'.  The value of the
;; folding property is folding spec symbol.
(defun org-fold-core-region (from to flag &optional spec-or-alias)
  "Hide or show lines from FROM to TO, according to FLAG.
SPEC-OR-ALIAS is the folding spec or foldable element, as a symbol.
If SPEC-OR-ALIAS is omitted and FLAG is nil, unfold everything in the region."
  (let ((spec (org-fold-core-get-folding-spec-from-alias spec-or-alias)))
    (when spec (org-fold-core--check-spec spec))
    (with-silent-modifications
      (org-with-wide-buffer
       ;; Arrange fontifying newlines after all the folds between FROM
       ;; and TO to match the first character before the fold; not the
       ;; last as per Emacs defaults.  This makes :extend faces span
       ;; past the ellipsis.  See bug#65896.  The face properties are
       ;; assigned via `org-activate-folds'.
       (when (or (not spec) (org-fold-core-get-folding-spec-property spec :font-lock))
         (when (equal ?\n (char-after from))
           (font-lock-flush from (1+ from)))
         (when (equal ?\n (char-after to))
           (font-lock-flush to (1+ to)))
         (dolist (region (org-fold-core-get-regions :from from :to to :specs spec))
           (when (equal ?\n (char-after (cadr region)))
             (font-lock-flush (cadr region) (1+ (cadr region))))
           ;; Re-fontify beginning of the fold - we may
           ;; unfold inside an existing fold, with FROM begin a newline
           ;; after spliced fold.
           (when (equal ?\n (char-after (car region)))
             (font-lock-flush (car region) (1+ (car region))))))
       (when (eq org-fold-core-style 'overlays)
         (if org-fold-core--keep-overlays
             (mapc
              (lambda (ov)
                (when (or (not spec)
                          (eq spec (overlay-get ov 'invisible)))
                  (when (and org-fold-core--isearch-active
                             (overlay-get ov 'invisible)
                             (org-fold-core-get-folding-spec-property
                              (overlay-get ov 'invisible) :isearch-open))
                    (when (overlay-get ov 'invisible)
                      (overlay-put ov 'org-invisible (overlay-get ov 'invisible)))
                    (overlay-put ov 'invisible nil)
                    (when org-fold-core--isearch-active
                      (cl-pushnew ov org-fold-core--isearch-overlays)))))
              (overlays-in from to))
           (when spec
             (remove-overlays from to 'org-invisible spec)
             (remove-overlays from to 'invisible spec))))
       (if flag
	   (if (not spec)
               (error "Calling `org-fold-core-region' with missing SPEC")
             (if (eq org-fold-core-style 'overlays)
                 ;; Use `front-advance' since text right before to the beginning of
                 ;; the overlay belongs to the visible line than to the contents.
                 (let ((o (make-overlay from to nil
                                        (org-fold-core-get-folding-spec-property spec :front-sticky)
                                        (org-fold-core-get-folding-spec-property spec :rear-sticky))))
                   (when org-fold-core--isearch-active
                     (push o org-fold-core--isearch-overlays))
                   (overlay-put o 'evaporate t)
                   (overlay-put o (org-fold-core--property-symbol-get-create spec) spec)
                   (overlay-put o 'invisible spec)
                   ;; Preserve priority.
                   (overlay-put o 'priority (length (member spec (org-fold-core-folding-spec-list))))
                   (overlay-put o 'isearch-open-invisible #'org-fold-core--isearch-show))
	       (put-text-property from to (org-fold-core--property-symbol-get-create spec) spec)
	       (put-text-property from to 'isearch-open-invisible #'org-fold-core--isearch-show)
	       (put-text-property from to 'isearch-open-invisible-temporary #'org-fold-core--isearch-show-temporary)
               (when (memql 'grab-invisible org-fold-core--optimize-for-huge-buffers)
                 ;; If the SPEC has highest priority, assign it directly
                 ;; to 'invisible property as well.  This is done to speed
                 ;; up Emacs redisplay on huge (Mbs) folded regions where
                 ;; we don't even want Emacs to spend time cycling over
                 ;; `char-property-alias-alist'.
                 (when (eq spec (caar org-fold-core--specs)) (put-text-property from to 'invisible spec)))))
         (if (not spec)
             (mapc (lambda (spec) (org-fold-core-region from to nil spec)) (org-fold-core-folding-spec-list))
           (when (and (memql 'grab-invisible org-fold-core--optimize-for-huge-buffers)
                      (eq org-fold-core-style 'text-properties))
             (when (eq spec (caar org-fold-core--specs))
               (let ((pos from))
                 (while (< pos to)
                   (if (eq spec (get-text-property pos 'invisible))
                       (let ((next (org-fold-core-next-folding-state-change spec pos to)))
                         (remove-text-properties pos next '(invisible t))
                         (setq pos next))
                     (setq pos (next-single-char-property-change pos 'invisible nil to)))))))
           (when (eq org-fold-core-style 'text-properties)
	     (remove-text-properties from to (list (org-fold-core--property-symbol-get-create spec) nil)))))
       ;; Re-calculate trailing faces for all the folds revealed
       ;; by unfolding or created by folding.
       (when (or (not spec) (org-fold-core-get-folding-spec-property spec :font-lock))
         (dolist (region (org-fold-core-get-regions :from from :to to :specs spec))
           (when (equal ?\n (char-after (cadr region)))
             (font-lock-flush (cadr region) (1+ (cadr region))))))))))