org-fold.el 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. ;;; org-fold.el --- Folding of Org entries -*- lexical-binding: t; -*-
  2. ;;
  3. ;; Copyright (C) 2020-2020 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Ihor Radchenko <yantar92 at gmail dot com>
  6. ;; Keywords: folding, invisible text
  7. ;; URL: https://orgmode.org
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file contains code handling temporary invisibility (folding
  25. ;; and unfolding) of text in org buffers.
  26. ;; The folding is implemented using generic org-fold-core library. This file
  27. ;; contains org-specific implementation of the folding. Also, various
  28. ;; useful functions from org-fold-core are aliased under shorted `org-fold'
  29. ;; prefix.
  30. ;; The following features are implemented:
  31. ;; - Folding/unfolding various Org mode elements and regions of Org buffers:
  32. ;; + Region before first heading;
  33. ;; + Org headings, their text, children (subtree), siblings, parents, etc;
  34. ;; + Org blocks and drawers
  35. ;; - Revealing Org structure around invisible point location
  36. ;; - Revealing folded Org elements broken by user edits
  37. ;;; Code:
  38. (require 'org-macs)
  39. (org-assert-version)
  40. (require 'org-macs)
  41. (require 'org-fold-core)
  42. (defvar org-inlinetask-min-level)
  43. (defvar org-link--link-folding-spec)
  44. (defvar org-link--description-folding-spec)
  45. (defvar org-odd-levels-only)
  46. (defvar org-drawer-regexp)
  47. (defvar org-property-end-re)
  48. (defvar org-link-descriptive)
  49. (defvar org-outline-regexp-bol)
  50. (defvar org-archive-tag)
  51. (defvar org-custom-properties-overlays)
  52. (defvar org-element-headline-re)
  53. (declare-function isearch-filter-visible "isearch" (beg end))
  54. (declare-function org-element-type "org-element" (element))
  55. (declare-function org-element-at-point "org-element" (&optional pom cached-only))
  56. (declare-function org-element-property "org-element" (property element))
  57. (declare-function org-element--current-element "org-element" (limit &optional granularity mode structure))
  58. (declare-function org-element--cache-active-p "org-element" ())
  59. (declare-function org-toggle-custom-properties-visibility "org" ())
  60. (declare-function org-item-re "org-list" ())
  61. (declare-function org-up-heading-safe "org" ())
  62. (declare-function org-get-tags "org" (&optional pos local fontify))
  63. (declare-function org-get-valid-level "org" (level &optional change))
  64. (declare-function org-before-first-heading-p "org" ())
  65. (declare-function org-goto-sibling "org" (&optional previous))
  66. (declare-function org-block-map "org" (function &optional start end))
  67. (declare-function org-map-region "org" (fun beg end))
  68. (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
  69. (declare-function org-back-to-heading-or-point-min "org" (&optional invisible-ok))
  70. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  71. (declare-function org-at-heading-p "org" (&optional invisible-not-ok))
  72. (declare-function org-cycle-hide-drawers "org-cycle" (state))
  73. (declare-function outline-show-branches "outline" ())
  74. (declare-function outline-hide-sublevels "outline" (levels))
  75. (declare-function outline-get-next-sibling "outline" ())
  76. (declare-function outline-invisible-p "outline" (&optional pos))
  77. (declare-function outline-next-heading "outline" ())
  78. ;;; Customization
  79. (defgroup org-fold-reveal-location nil
  80. "Options about how to make context of a location visible."
  81. :tag "Org Reveal Location"
  82. :group 'org-structure)
  83. (defcustom org-fold-show-context-detail '((agenda . local)
  84. (bookmark-jump . lineage)
  85. (isearch . lineage)
  86. (default . ancestors))
  87. "Alist between context and visibility span when revealing a location.
  88. \\<org-mode-map>Some actions may move point into invisible
  89. locations. As a consequence, Org always exposes a neighborhood
  90. around point. How much is shown depends on the initial action,
  91. or context. Valid contexts are
  92. agenda when exposing an entry from the agenda
  93. org-goto when using the command `org-goto' (`\\[org-goto]')
  94. occur-tree when using the command `org-occur' (`\\[org-sparse-tree] /')
  95. tags-tree when constructing a sparse tree based on tags matches
  96. link-search when exposing search matches associated with a link
  97. mark-goto when exposing the jump goal of a mark
  98. bookmark-jump when exposing a bookmark location
  99. isearch when exiting from an incremental search
  100. default default for all contexts not set explicitly
  101. Allowed visibility spans are
  102. minimal show current headline; if point is not on headline,
  103. also show entry
  104. local show current headline, entry and next headline
  105. ancestors show current headline and its direct ancestors; if
  106. point is not on headline, also show entry
  107. ancestors-full show current subtree and its direct ancestors
  108. lineage show current headline, its direct ancestors and all
  109. their children; if point is not on headline, also show
  110. entry and first child
  111. tree show current headline, its direct ancestors and all
  112. their children; if point is not on headline, also show
  113. entry and all children
  114. canonical show current headline, its direct ancestors along with
  115. their entries and children; if point is not located on
  116. the headline, also show current entry and all children
  117. As special cases, a nil or t value means show all contexts in
  118. `minimal' or `canonical' view, respectively.
  119. Some views can make displayed information very compact, but also
  120. make it harder to edit the location of the match. In such
  121. a case, use the command `org-fold-reveal' (`\\[org-fold-reveal]') to show
  122. more context."
  123. :group 'org-fold-reveal-location
  124. :version "26.1"
  125. :package-version '(Org . "9.0")
  126. :type '(choice
  127. (const :tag "Canonical" t)
  128. (const :tag "Minimal" nil)
  129. (repeat :greedy t :tag "Individual contexts"
  130. (cons
  131. (choice :tag "Context"
  132. (const agenda)
  133. (const org-goto)
  134. (const occur-tree)
  135. (const tags-tree)
  136. (const link-search)
  137. (const mark-goto)
  138. (const bookmark-jump)
  139. (const isearch)
  140. (const default))
  141. (choice :tag "Detail level"
  142. (const minimal)
  143. (const local)
  144. (const ancestors)
  145. (const ancestors-full)
  146. (const lineage)
  147. (const tree)
  148. (const canonical))))))
  149. (defvar org-fold-reveal-start-hook nil
  150. "Hook run before revealing a location.")
  151. (defcustom org-fold-catch-invisible-edits 'smart
  152. "Check if in invisible region before inserting or deleting a character.
  153. Valid values are:
  154. nil Do not check, so just do invisible edits.
  155. error Throw an error and do nothing.
  156. show Make point visible, and do the requested edit.
  157. show-and-error Make point visible, then throw an error and abort the edit.
  158. smart Make point visible, and do insertion/deletion if it is
  159. adjacent to visible text and the change feels predictable.
  160. Never delete a previously invisible character or add in the
  161. middle or right after an invisible region. Basically, this
  162. allows insertion and backward-delete right before ellipses.
  163. FIXME: maybe in this case we should not even show?"
  164. :group 'org-edit-structure
  165. :version "24.1"
  166. :type '(choice
  167. (const :tag "Do not check" nil)
  168. (const :tag "Throw error when trying to edit" error)
  169. (const :tag "Unhide, but do not do the edit" show-and-error)
  170. (const :tag "Show invisible part and do the edit" show)
  171. (const :tag "Be smart and do the right thing" smart)))
  172. ;;; Core functionality
  173. ;;; API
  174. ;;;; Modifying folding specs
  175. (defalias 'org-fold-folding-spec-p #'org-fold-core-folding-spec-p)
  176. (defalias 'org-fold-add-folding-spec #'org-fold-core-add-folding-spec)
  177. (defalias 'org-fold-remove-folding-spec #'org-fold-core-remove-folding-spec)
  178. (defun org-fold-initialize (ellipsis)
  179. "Setup folding in current Org buffer."
  180. (setq-local org-fold-core-isearch-open-function #'org-fold--isearch-reveal)
  181. (setq-local org-fold-core-extend-changed-region-functions (list #'org-fold--extend-changed-region))
  182. ;; FIXME: Converting org-link + org-description to overlays when
  183. ;; search matches hidden "[[" part of the link, reverses priority of
  184. ;; link and description and hides the whole link. Working around
  185. ;; this until there will be no need to convert text properties to
  186. ;; overlays for isearch.
  187. (setq-local org-fold-core--isearch-special-specs '(org-link))
  188. (org-fold-core-initialize
  189. `((,(if (eq org-fold-core-style 'text-properties) 'org-fold-outline 'outline)
  190. (:ellipsis . ,ellipsis)
  191. (:fragile . ,#'org-fold--reveal-outline-maybe)
  192. (:isearch-open . t)
  193. ;; This is needed to make sure that inserting a
  194. ;; new planning line in folded heading is not
  195. ;; revealed. Also, the below combination of :front-sticky and
  196. ;; :rear-sticky conforms to the overlay properties in outline.el
  197. ;; and the older Org versions as in `outline-flag-region'.
  198. (:front-sticky . t)
  199. (:rear-sticky . nil)
  200. (:alias . (headline heading outline inlinetask plain-list)))
  201. (,(if (eq org-fold-core-style 'text-properties) 'org-fold-block 'org-hide-block)
  202. (:ellipsis . ,ellipsis)
  203. (:fragile . ,#'org-fold--reveal-drawer-or-block-maybe)
  204. (:isearch-open . t)
  205. (:front-sticky . t)
  206. (:alias . ( block center-block comment-block
  207. dynamic-block example-block export-block
  208. quote-block special-block src-block
  209. verse-block)))
  210. (,(if (eq org-fold-core-style 'text-properties) 'org-fold-drawer 'org-hide-drawer)
  211. (:ellipsis . ,ellipsis)
  212. (:fragile . ,#'org-fold--reveal-drawer-or-block-maybe)
  213. (:isearch-open . t)
  214. (:front-sticky . t)
  215. (:alias . (drawer property-drawer)))
  216. ,org-link--description-folding-spec
  217. ,org-link--link-folding-spec)))
  218. ;;;; Searching and examining folded text
  219. (defalias 'org-fold-folded-p #'org-fold-core-folded-p)
  220. (defalias 'org-fold-get-folding-spec #'org-fold-core-get-folding-spec)
  221. (defalias 'org-fold-get-folding-specs-in-region #'org-fold-core-get-folding-specs-in-region)
  222. (defalias 'org-fold-get-region-at-point #'org-fold-core-get-region-at-point)
  223. (defalias 'org-fold-get-regions #'org-fold-core-get-regions)
  224. (defalias 'org-fold-next-visibility-change #'org-fold-core-next-visibility-change)
  225. (defalias 'org-fold-previous-visibility-change #'org-fold-core-previous-visibility-change)
  226. (defalias 'org-fold-next-folding-state-change #'org-fold-core-next-folding-state-change)
  227. (defalias 'org-fold-previous-folding-state-change #'org-fold-core-previous-folding-state-change)
  228. (defalias 'org-fold-search-forward #'org-fold-core-search-forward)
  229. ;;;;; Macros
  230. (defalias 'org-fold-save-outline-visibility #'org-fold-core-save-visibility)
  231. ;;;; Changing visibility (regions, blocks, drawers, headlines)
  232. ;;;;; Region visibility
  233. (defalias 'org-fold-region #'org-fold-core-region)
  234. (defalias 'org-fold-regions #'org-fold-core-regions)
  235. (defun org-fold-show-all (&optional types)
  236. "Show all contents in the visible part of the buffer.
  237. By default, the function expands headings, blocks and drawers.
  238. When optional argument TYPES is a list of symbols among `blocks',
  239. `drawers' and `headings', to only expand one specific type."
  240. (interactive)
  241. (dolist (type (or types '(blocks drawers headings)))
  242. (org-fold-region (point-min) (point-max) nil
  243. (pcase type
  244. (`blocks 'block)
  245. (`drawers 'drawer)
  246. (`headings 'headline)
  247. (_ (error "Invalid type: %S" type))))))
  248. (defun org-fold-flag-above-first-heading (&optional arg)
  249. "Hide from bob up to the first heading.
  250. Move point to the beginning of first heading or end of buffer."
  251. (goto-char (point-min))
  252. (unless (org-at-heading-p)
  253. (outline-next-heading))
  254. (unless (bobp)
  255. (org-fold-region 1 (1- (point)) (not arg) 'outline)))
  256. ;;;;; Heading visibility
  257. (defun org-fold-heading (flag &optional entry)
  258. "Fold/unfold the current heading. FLAG non-nil means make invisible.
  259. When ENTRY is non-nil, show the entire entry."
  260. (save-excursion
  261. (org-back-to-heading t)
  262. ;; Check if we should show the entire entry
  263. (if (not entry)
  264. (org-fold-region
  265. (line-end-position 0) (line-end-position) flag 'outline)
  266. (org-fold-show-entry)
  267. (save-excursion
  268. ;; FIXME: potentially catches inlinetasks
  269. (and (outline-next-heading)
  270. (org-fold-heading nil))))))
  271. (defun org-fold-hide-entry ()
  272. "Hide the body directly following this heading."
  273. (interactive)
  274. (save-excursion
  275. (org-back-to-heading-or-point-min t)
  276. (when (org-at-heading-p) (forward-line))
  277. (unless (or (eobp) (org-at-heading-p)) ; Current headline is empty.
  278. (org-fold-region
  279. (line-end-position 0)
  280. (save-excursion
  281. (if (re-search-forward
  282. (concat "[\r\n]" (org-get-limited-outline-regexp)) nil t)
  283. (line-end-position 0)
  284. (point-max)))
  285. t
  286. 'outline))))
  287. (defun org-fold-subtree (flag)
  288. (save-excursion
  289. (org-back-to-heading t)
  290. (org-fold-region
  291. (line-end-position)
  292. (progn (org-end-of-subtree t) (point))
  293. flag
  294. 'outline)))
  295. ;; Replaces `outline-hide-subtree'.
  296. (defun org-fold-hide-subtree ()
  297. "Hide everything after this heading at deeper levels."
  298. (interactive)
  299. (org-fold-subtree t))
  300. ;; Replaces `outline-hide-sublevels'
  301. (defun org-fold-hide-sublevels (levels)
  302. "Hide everything but the top LEVELS levels of headers, in whole buffer.
  303. This also unhides the top heading-less body, if any.
  304. Interactively, the prefix argument supplies the value of LEVELS.
  305. When invoked without a prefix argument, LEVELS defaults to the level
  306. of the current heading, or to 1 if the current line is not a heading."
  307. (interactive (list
  308. (cond
  309. (current-prefix-arg (prefix-numeric-value current-prefix-arg))
  310. ((save-excursion (beginning-of-line)
  311. (looking-at outline-regexp))
  312. (funcall outline-level))
  313. (t 1))))
  314. (if (< levels 1)
  315. (error "Must keep at least one level of headers"))
  316. (save-excursion
  317. (let* ((beg (progn
  318. (goto-char (point-min))
  319. ;; Skip the prelude, if any.
  320. (unless (org-at-heading-p) (outline-next-heading))
  321. (point)))
  322. (end (progn
  323. (goto-char (point-max))
  324. ;; Keep empty last line, if available.
  325. (max (point-min) (if (bolp) (1- (point)) (point))))))
  326. (if (< end beg)
  327. (setq beg (prog1 end (setq end beg))))
  328. ;; First hide everything.
  329. (org-fold-region beg end t 'headline)
  330. ;; Then unhide the top level headers.
  331. (org-map-region
  332. (lambda ()
  333. (when (<= (funcall outline-level) levels)
  334. (org-fold-heading nil)))
  335. beg end)
  336. ;; Finally unhide any trailing newline.
  337. (goto-char (point-max))
  338. (if (and (bolp) (not (bobp)) (outline-invisible-p (1- (point))))
  339. (org-fold-region (max (point-min) (1- (point))) (point) nil)))))
  340. (defun org-fold-show-entry (&optional hide-drawers)
  341. "Show the body directly following its heading.
  342. Show the heading too, if it is currently invisible."
  343. (interactive)
  344. (save-excursion
  345. (org-back-to-heading-or-point-min t)
  346. (org-fold-region
  347. (line-end-position 0)
  348. (save-excursion
  349. (if (re-search-forward
  350. (concat "[\r\n]\\(" (org-get-limited-outline-regexp) "\\)") nil t)
  351. (match-beginning 1)
  352. (point-max)))
  353. nil
  354. 'outline)
  355. (when hide-drawers (org-cycle-hide-drawers 'children))))
  356. (defalias 'org-fold-show-hidden-entry #'org-fold-show-entry
  357. "Show an entry where even the heading is hidden.")
  358. (defun org-fold-show-siblings ()
  359. "Show all siblings of the current headline."
  360. (save-excursion
  361. (while (org-goto-sibling) (org-fold-heading nil)))
  362. (save-excursion
  363. (while (org-goto-sibling 'previous)
  364. (org-fold-heading nil))))
  365. (defun org-fold-show-children (&optional level)
  366. "Show all direct subheadings of this heading.
  367. Prefix arg LEVEL is how many levels below the current level
  368. should be shown. Default is enough to cause the following
  369. heading to appear."
  370. (interactive "p")
  371. (unless (org-before-first-heading-p)
  372. (save-excursion
  373. (org-with-limited-levels (org-back-to-heading t))
  374. (let* ((current-level (funcall outline-level))
  375. (max-level (org-get-valid-level
  376. current-level
  377. (if level (prefix-numeric-value level) 1)))
  378. (end (save-excursion (org-end-of-subtree t t)))
  379. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  380. (past-first-child nil)
  381. ;; Make sure to skip inlinetasks.
  382. (re (format regexp-fmt
  383. current-level
  384. (cond
  385. ((not (featurep 'org-inlinetask)) "")
  386. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  387. 3))
  388. (t (1- org-inlinetask-min-level))))))
  389. ;; Display parent heading.
  390. (org-fold-heading nil)
  391. (forward-line)
  392. ;; Display children. First child may be deeper than expected
  393. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  394. ;; MAX-LEVEL accordingly.
  395. (while (re-search-forward re end t)
  396. (unless past-first-child
  397. (setq re (format regexp-fmt
  398. current-level
  399. (max (funcall outline-level) max-level)))
  400. (setq past-first-child t))
  401. (org-fold-heading nil))))))
  402. (defun org-fold-show-subtree ()
  403. "Show everything after this heading at deeper levels."
  404. (interactive)
  405. (org-fold-region
  406. (point) (save-excursion (org-end-of-subtree t t)) nil 'outline))
  407. (defun org-fold-show-branches ()
  408. "Show all subheadings of this heading, but not their bodies."
  409. (interactive)
  410. (org-fold-show-children 1000))
  411. (defun org-fold-show-branches-buffer ()
  412. "Show all branches in the buffer."
  413. (org-fold-flag-above-first-heading)
  414. (org-fold-hide-sublevels 1)
  415. (unless (eobp)
  416. (org-fold-show-branches)
  417. (while (outline-get-next-sibling)
  418. (org-fold-show-branches)))
  419. (goto-char (point-min)))
  420. ;;;;; Blocks and drawers visibility
  421. (defun org-fold--hide-wrapper-toggle (element category force no-error)
  422. "Toggle visibility for ELEMENT.
  423. ELEMENT is a block or drawer type parsed element. CATEGORY is
  424. either `block' or `drawer'. When FORCE is `off', show the block
  425. or drawer. If it is non-nil, hide it unconditionally. Throw an
  426. error when not at a block or drawer, unless NO-ERROR is non-nil.
  427. Return a non-nil value when toggling is successful."
  428. (let ((type (org-element-type element)))
  429. (cond
  430. ((memq type
  431. (pcase category
  432. (`drawer '(drawer property-drawer))
  433. (`block '(center-block
  434. comment-block dynamic-block example-block export-block
  435. quote-block special-block src-block verse-block))
  436. (_ (error "Unknown category: %S" category))))
  437. (let* ((post (org-element-property :post-affiliated element))
  438. (start (save-excursion
  439. (goto-char post)
  440. (line-end-position)))
  441. (end (save-excursion
  442. (goto-char (org-element-property :end element))
  443. (skip-chars-backward " \t\n")
  444. (line-end-position))))
  445. ;; Do nothing when not before or at the block opening line or
  446. ;; at the block closing line.
  447. (unless (let ((eol (line-end-position)))
  448. (and (> eol start) (/= eol end)))
  449. (org-fold-region start end
  450. (cond ((eq force 'off) nil)
  451. (force t)
  452. ((org-fold-folded-p start category) nil)
  453. (t t))
  454. category)
  455. ;; When the block is hidden away, make sure point is left in
  456. ;; a visible part of the buffer.
  457. (when (invisible-p (max (1- (point)) (point-min)))
  458. (goto-char post))
  459. ;; Signal success.
  460. t)))
  461. (no-error nil)
  462. (t
  463. (user-error (format "%s@%s: %s"
  464. (buffer-file-name (buffer-base-buffer))
  465. (point)
  466. (if (eq category 'drawer)
  467. "Not at a drawer"
  468. "Not at a block")))))))
  469. (defun org-fold-hide-block-toggle (&optional force no-error element)
  470. "Toggle the visibility of the current block.
  471. When optional argument FORCE is `off', make block visible. If it
  472. is non-nil, hide it unconditionally. Throw an error when not at
  473. a block, unless NO-ERROR is non-nil. When optional argument
  474. ELEMENT is provided, consider it instead of the current block.
  475. Return a non-nil value when toggling is successful."
  476. (interactive)
  477. (org-fold--hide-wrapper-toggle
  478. (or element (org-element-at-point)) 'block force no-error))
  479. (defun org-fold-hide-drawer-toggle (&optional force no-error element)
  480. "Toggle the visibility of the current drawer.
  481. When optional argument FORCE is `off', make drawer visible. If
  482. it is non-nil, hide it unconditionally. Throw an error when not
  483. at a drawer, unless NO-ERROR is non-nil. When optional argument
  484. ELEMENT is provided, consider it instead of the current drawer.
  485. Return a non-nil value when toggling is successful."
  486. (interactive)
  487. (org-fold--hide-wrapper-toggle
  488. (or element (org-element-at-point)) 'drawer force no-error))
  489. (defun org-fold-hide-block-all ()
  490. "Fold all blocks in the current buffer."
  491. (interactive)
  492. (org-block-map (apply-partially #'org-fold-hide-block-toggle 'hide)))
  493. (defun org-fold-hide-drawer-all ()
  494. "Fold all drawers in the current buffer."
  495. (let ((begin (point-min))
  496. (end (point-max)))
  497. (org-fold--hide-drawers begin end)))
  498. (defun org-fold--hide-drawers (begin end)
  499. "Hide all drawers between BEGIN and END."
  500. (save-excursion
  501. (goto-char begin)
  502. (while (and (< (point) end)
  503. (re-search-forward org-drawer-regexp end t))
  504. ;; Skip folded drawers
  505. (if (org-fold-folded-p nil 'drawer)
  506. (goto-char (org-fold-next-folding-state-change 'drawer nil end))
  507. (let* ((drawer (org-element-at-point))
  508. (type (org-element-type drawer)))
  509. (when (memq type '(drawer property-drawer))
  510. (org-fold-hide-drawer-toggle t nil drawer)
  511. ;; Make sure to skip drawer entirely or we might flag it
  512. ;; another time when matching its ending line with
  513. ;; `org-drawer-regexp'.
  514. (goto-char (org-element-property :end drawer))))))))
  515. (defun org-fold-hide-archived-subtrees (beg end)
  516. "Re-hide all archived subtrees after a visibility state change."
  517. (org-with-wide-buffer
  518. (let ((case-fold-search nil)
  519. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  520. (goto-char beg)
  521. ;; Include headline point is currently on.
  522. (beginning-of-line)
  523. (while (and (< (point) end) (re-search-forward re end t))
  524. (when (member org-archive-tag (org-get-tags nil t))
  525. (org-fold-subtree t)
  526. (org-end-of-subtree t))))))
  527. ;;;;; Reveal point location
  528. (defun org-fold-show-context (&optional key)
  529. "Make sure point and context are visible.
  530. Optional argument KEY, when non-nil, is a symbol. See
  531. `org-fold-show-context-detail' for allowed values and how much is to
  532. be shown."
  533. (org-fold-show-set-visibility
  534. (cond ((symbolp org-fold-show-context-detail) org-fold-show-context-detail)
  535. ((cdr (assq key org-fold-show-context-detail)))
  536. (t (cdr (assq 'default org-fold-show-context-detail))))))
  537. (defvar org-hide-emphasis-markers); Defined in org.el
  538. (defvar org-pretty-entities); Defined in org.el
  539. (defun org-fold-show-set-visibility (detail)
  540. "Set visibility around point according to DETAIL.
  541. DETAIL is either nil, `minimal', `local', `ancestors',
  542. `ancestors-full', `lineage', `tree', `canonical' or t. See
  543. `org-show-context-detail' for more information."
  544. ;; Show current heading and possibly its entry, following headline
  545. ;; or all children.
  546. (if (and (org-at-heading-p) (not (eq detail 'local)))
  547. (org-fold-heading nil)
  548. (org-fold-show-entry)
  549. ;; If point is hidden make sure to expose it.
  550. (when (org-invisible-p)
  551. ;; FIXME: No clue why, but otherwise the following might not work.
  552. (redisplay)
  553. (let ((region (org-fold-get-region-at-point)))
  554. ;; Reveal emphasis markers.
  555. (when (eq detail 'local)
  556. (let (org-hide-emphasis-markers
  557. org-link-descriptive
  558. org-pretty-entities
  559. (org-hide-macro-markers nil)
  560. (region (or (org-find-text-property-region (point) 'org-emphasis)
  561. (org-find-text-property-region (point) 'org-macro)
  562. (org-find-text-property-region (point) 'invisible)
  563. region)))
  564. ;; Silence byte-compiler.
  565. (ignore org-hide-macro-markers)
  566. (when region
  567. (org-with-point-at (car region)
  568. (beginning-of-line)
  569. (let (font-lock-extend-region-functions)
  570. (font-lock-fontify-region (max (point-min) (1- (car region))) (cdr region))))))
  571. ;; Unfold links.
  572. (when region
  573. (dolist (spec '(org-link org-link-description))
  574. (org-fold-region (car region) (cdr region) nil spec))))
  575. (when region
  576. (dolist (spec (org-fold-core-folding-spec-list))
  577. ;; Links are taken care by above.
  578. (unless (memq spec '(org-link org-link-description))
  579. (org-fold-region (car region) (cdr region) nil spec))))))
  580. (unless (org-before-first-heading-p)
  581. (org-with-limited-levels
  582. (cl-case detail
  583. ((tree canonical t) (org-fold-show-children))
  584. ((nil minimal ancestors ancestors-full))
  585. (t (save-excursion
  586. (outline-next-heading)
  587. (org-fold-heading nil)))))))
  588. ;; Show whole subtree.
  589. (when (eq detail 'ancestors-full) (org-fold-show-subtree))
  590. ;; Show all siblings.
  591. (when (eq detail 'lineage) (org-fold-show-siblings))
  592. ;; Show ancestors, possibly with their children.
  593. (when (memq detail '(ancestors ancestors-full lineage tree canonical t))
  594. (save-excursion
  595. (while (org-up-heading-safe)
  596. (org-fold-heading nil)
  597. (when (memq detail '(canonical t)) (org-fold-show-entry))
  598. (when (memq detail '(tree canonical t)) (org-fold-show-children))))))
  599. (defun org-fold-reveal (&optional siblings)
  600. "Show current entry, hierarchy above it, and the following headline.
  601. This can be used to show a consistent set of context around
  602. locations exposed with `org-fold-show-context'.
  603. With optional argument SIBLINGS, on each level of the hierarchy all
  604. siblings are shown. This repairs the tree structure to what it would
  605. look like when opened with hierarchical calls to `org-cycle'.
  606. With a \\[universal-argument] \\[universal-argument] prefix, \
  607. go to the parent and show the entire tree."
  608. (interactive "P")
  609. (run-hooks 'org-fold-reveal-start-hook)
  610. (cond ((equal siblings '(4)) (org-fold-show-set-visibility 'canonical))
  611. ((equal siblings '(16))
  612. (save-excursion
  613. (when (org-up-heading-safe)
  614. (org-fold-show-subtree)
  615. (run-hook-with-args 'org-cycle-hook 'subtree))))
  616. (t (org-fold-show-set-visibility 'lineage))))
  617. ;;; Make isearch search in some text hidden via text properties.
  618. (defun org-fold--isearch-reveal (&rest _)
  619. "Reveal text at POS found by isearch."
  620. (org-fold-show-context 'isearch))
  621. ;;; Handling changes in folded elements
  622. (defun org-fold--extend-changed-region (from to)
  623. "Consider folded regions in the next/previous line when fixing
  624. region visibility.
  625. This function is intended to be used as a member of
  626. `org-fold-core-extend-changed-region-functions'."
  627. ;; If the edit is done in the first line of a folded drawer/block,
  628. ;; the folded text is only starting from the next line and needs to
  629. ;; be checked.
  630. (setq to (save-excursion (goto-char to) (line-beginning-position 2)))
  631. ;; If the ":END:" line of the drawer is deleted, the folded text is
  632. ;; only ending at the previous line and needs to be checked.
  633. (setq from (save-excursion (goto-char from) (line-beginning-position 0)))
  634. (cons from to))
  635. (defun org-fold--reveal-headline-at-point ()
  636. "Reveal header line and empty contents inside.
  637. Reveal the header line and, if present, also reveal its contents, when
  638. the contents consists of blank lines.
  639. Assume that point is located at the header line."
  640. (org-with-wide-buffer
  641. (beginning-of-line)
  642. (org-fold-region
  643. (max (point-min) (1- (point)))
  644. (let ((endl (line-end-position)))
  645. (save-excursion
  646. (goto-char endl)
  647. (skip-chars-forward "\n\t\r ")
  648. ;; Unfold blank lines after newly inserted headline.
  649. (if (equal (point)
  650. (save-excursion
  651. (goto-char endl)
  652. (org-end-of-subtree)
  653. (skip-chars-forward "\n\t\r ")))
  654. (point)
  655. endl)))
  656. nil 'headline)))
  657. (defun org-fold--reveal-outline-maybe (region _)
  658. "Reveal folded outline in REGION when needed.
  659. This function is intended to be used as :fragile property of
  660. `org-fold-outline' spec. See `org-fold-core--specs' for details."
  661. (save-match-data
  662. (org-with-wide-buffer
  663. (goto-char (car region))
  664. ;; The line before beginning of the fold should be either a
  665. ;; headline or a list item.
  666. (backward-char)
  667. (beginning-of-line)
  668. ;; Make sure that headline is not partially hidden.
  669. (unless (org-fold-folded-p nil 'headline)
  670. (org-fold--reveal-headline-at-point))
  671. ;; Never hide level 1 headlines
  672. (save-excursion
  673. (goto-char (line-end-position))
  674. (unless (>= (point) (cdr region))
  675. (when (re-search-forward (rx bol "* ") (cdr region) t)
  676. (org-fold--reveal-headline-at-point))))
  677. ;; Make sure that headline after is not partially hidden.
  678. (goto-char (cdr region))
  679. (beginning-of-line)
  680. (unless (org-fold-folded-p nil 'headline)
  681. (when (looking-at-p org-element-headline-re)
  682. (org-fold--reveal-headline-at-point)))
  683. ;; Check the validity of headline
  684. (goto-char (car region))
  685. (backward-char)
  686. (beginning-of-line)
  687. (unless (let ((case-fold-search t))
  688. (looking-at (rx-to-string
  689. `(or (regex ,(org-item-re))
  690. (regex ,org-outline-regexp-bol)))))
  691. t))))
  692. (defun org-fold--reveal-drawer-or-block-maybe (region spec)
  693. "Reveal folded drawer/block (according to SPEC) in REGION when needed.
  694. This function is intended to be used as :fragile property of
  695. `org-fold-drawer' or `org-fold-block' spec."
  696. (let ((begin-re (cond
  697. ((eq spec (org-fold-core-get-folding-spec-from-alias 'drawer))
  698. org-drawer-regexp)
  699. ;; Group one below contains the type of the block.
  700. ((eq spec (org-fold-core-get-folding-spec-from-alias 'block))
  701. (rx bol (zero-or-more (any " " "\t"))
  702. "#+begin"
  703. (or ":"
  704. (seq "_"
  705. (group (one-or-more (not (syntax whitespace))))))))))
  706. ;; To be determined later. May depend on `begin-re' match (i.e. for blocks).
  707. end-re)
  708. (save-match-data ; we should not clobber match-data in after-change-functions
  709. (let ((fold-begin (car region))
  710. (fold-end (cdr region)))
  711. (let (unfold?)
  712. (catch :exit
  713. ;; The line before folded text should be beginning of
  714. ;; the drawer/block.
  715. (save-excursion
  716. (goto-char fold-begin)
  717. ;; The line before beginning of the fold should be the
  718. ;; first line of the drawer/block.
  719. (backward-char)
  720. (beginning-of-line)
  721. (unless (let ((case-fold-search t))
  722. (looking-at begin-re)) ; the match-data will be used later
  723. (throw :exit (setq unfold? t))))
  724. ;; Set `end-re' for the current drawer/block.
  725. (setq end-re
  726. (cond
  727. ((eq spec (org-fold-core-get-folding-spec-from-alias 'drawer))
  728. org-property-end-re)
  729. ((eq spec (org-fold-core-get-folding-spec-from-alias 'block))
  730. (let ((block-type (match-string 1))) ; the last match is from `begin-re'
  731. (concat (rx bol (zero-or-more (any " " "\t")) "#+end")
  732. (if block-type
  733. (concat "_"
  734. (regexp-quote block-type)
  735. (rx (zero-or-more (any " " "\t")) eol))
  736. (rx (opt ":") (zero-or-more (any " " "\t")) eol)))))))
  737. ;; The last line of the folded text should match `end-re'.
  738. (save-excursion
  739. (goto-char fold-end)
  740. (beginning-of-line)
  741. (unless (let ((case-fold-search t))
  742. (looking-at end-re))
  743. (throw :exit (setq unfold? t))))
  744. ;; There should be no `end-re' or
  745. ;; `org-outline-regexp-bol' anywhere in the
  746. ;; drawer/block body.
  747. (save-excursion
  748. (goto-char fold-begin)
  749. (when (save-excursion
  750. (let ((case-fold-search t))
  751. (re-search-forward (rx-to-string `(or (regex ,end-re)
  752. (regex ,org-outline-regexp-bol)))
  753. (max (point)
  754. (1- (save-excursion
  755. (goto-char fold-end)
  756. (line-beginning-position))))
  757. t)))
  758. (throw :exit (setq unfold? t)))))
  759. unfold?)))))
  760. ;; Catching user edits inside invisible text
  761. (defun org-fold-check-before-invisible-edit (kind)
  762. "Check if editing KIND is dangerous with invisible text around.
  763. The detailed reaction depends on the user option
  764. `org-fold-catch-invisible-edits'."
  765. ;; First, try to get out of here as quickly as possible, to reduce overhead
  766. (when (and org-fold-catch-invisible-edits
  767. (or (not (boundp 'visible-mode)) (not visible-mode))
  768. (or (org-invisible-p)
  769. (org-invisible-p (max (point-min) (1- (point))))))
  770. ;; OK, we need to take a closer look. Only consider invisibility
  771. ;; caused by folding of headlines, drawers, and blocks. Edits
  772. ;; inside links will be handled by font-lock.
  773. (let* ((invisible-at-point (org-fold-folded-p (point) '(headline drawer block)))
  774. (invisible-before-point
  775. (and (not (bobp))
  776. (org-fold-folded-p (1- (point)) '(headline drawer block))))
  777. (border-and-ok-direction
  778. (or
  779. ;; Check if we are acting predictably before invisible
  780. ;; text.
  781. (and invisible-at-point (not invisible-before-point)
  782. (memq kind '(insert delete-backward)))
  783. ;; Check if we are acting predictably after invisible text
  784. ;; This works not well, and I have turned it off. It seems
  785. ;; better to always show and stop after invisible text.
  786. ;; (and (not invisible-at-point) invisible-before-point
  787. ;; (memq kind '(insert delete)))
  788. )))
  789. (when (or invisible-at-point invisible-before-point)
  790. (when (eq org-fold-catch-invisible-edits 'error)
  791. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  792. (if (and org-custom-properties-overlays
  793. (y-or-n-p "Display invisible properties in this buffer? "))
  794. (org-toggle-custom-properties-visibility)
  795. ;; Make the area visible
  796. (save-excursion
  797. (org-fold-show-set-visibility 'local))
  798. (when invisible-before-point
  799. (org-with-point-at (1- (point)) (org-fold-show-set-visibility 'local)))
  800. (cond
  801. ((eq org-fold-catch-invisible-edits 'show)
  802. ;; That's it, we do the edit after showing
  803. (message
  804. "Unfolding invisible region around point before editing")
  805. (sit-for 1))
  806. ((and (eq org-fold-catch-invisible-edits 'smart)
  807. border-and-ok-direction)
  808. (message "Unfolding invisible region around point before editing"))
  809. (t
  810. ;; Don't do the edit, make the user repeat it in full visibility
  811. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  812. (provide 'org-fold)
  813. ;;; org-fold.el ends here