org-fold.el 36 KB

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