org-fold.el 36 KB

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