Function: org-fold--reveal-drawer-or-block-maybe

org-fold--reveal-drawer-or-block-maybe is a byte-compiled function defined in org-fold.el.gz.

Signature

(org-fold--reveal-drawer-or-block-maybe REGION SPEC)

Documentation

Reveal folded drawer/block (according to SPEC) in REGION when needed.

This function is intended to be used as :fragile property of org-fold-drawer or org-fold-block spec.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-fold.el.gz
(defun org-fold--reveal-drawer-or-block-maybe (region spec)
  "Reveal folded drawer/block (according to SPEC) in REGION when needed.

This function is intended to be used as :fragile property of
`org-fold-drawer' or `org-fold-block' spec."
  (let ((begin-re (cond
		   ((eq spec (org-fold-core-get-folding-spec-from-alias 'drawer))
		    org-drawer-regexp)
		   ;; Group one below contains the type of the block.
		   ((eq spec (org-fold-core-get-folding-spec-from-alias 'block))
		    (rx bol (zero-or-more (any " " "\t"))
			"#+begin"
			(or ":"
			    (seq "_"
				 (group (one-or-more (not (syntax whitespace))))))))))
        ;; To be determined later. May depend on `begin-re' match (i.e. for blocks).
        end-re)
    (save-match-data ; we should not clobber match-data in after-change-functions
      (let ((fold-begin (car region))
	    (fold-end (cdr region)))
	(let (unfold?)
	  (catch :exit
	    ;; The line before folded text should be beginning of
	    ;; the drawer/block.
	    (save-excursion
	      (goto-char fold-begin)
	      ;; The line before beginning of the fold should be the
	      ;; first line of the drawer/block.
	      (backward-char)
	      (beginning-of-line)
	      (unless (let ((case-fold-search t))
			(looking-at begin-re)) ; the match-data will be used later
		(throw :exit (setq unfold? t))))
            ;; Set `end-re' for the current drawer/block.
            (setq end-re
		  (cond
		   ((eq spec (org-fold-core-get-folding-spec-from-alias 'drawer))
                    org-property-end-re)
		   ((eq spec (org-fold-core-get-folding-spec-from-alias 'block))
		    (let ((block-type (match-string 1))) ; the last match is from `begin-re'
		      (concat (rx bol (zero-or-more (any " " "\t")) "#+end")
			      (if block-type
				  (concat "_"
					  (regexp-quote block-type)
					  (rx (zero-or-more (any " " "\t")) eol))
				(rx (opt ":") (zero-or-more (any " " "\t")) eol)))))))
	    ;; The last line of the folded text should match `end-re'.
	    (save-excursion
	      (goto-char fold-end)
	      (beginning-of-line)
	      (unless (let ((case-fold-search t))
			(looking-at end-re))
		(throw :exit (setq unfold? t))))
	    ;; There should be no `end-re' or
	    ;; `org-outline-regexp-bol' anywhere in the
	    ;; drawer/block body.
	    (save-excursion
	      (goto-char fold-begin)
	      (when (save-excursion
		      (let ((case-fold-search t))
			(re-search-forward (rx-to-string `(or (regex ,end-re)
                                                              (regex ,org-outline-regexp-bol)))
					   (max (point)
						(1- (save-excursion
						      (goto-char fold-end)
						      (line-beginning-position))))
					   t)))
		(throw :exit (setq unfold? t)))))
          unfold?)))))