org-fold.el 47 KB

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