org-fold.el 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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. `((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 (data-val)
  271. (dolist (spec ,specs)
  272. (let ((pos (point-min)))
  273. (while (< pos (point-max))
  274. (when (org-fold-get-folding-spec spec pos)
  275. (let ((region (org-fold-get-region-at-point spec pos)))
  276. (if ,markers?
  277. (push (list (copy-marker (car region))
  278. (copy-marker (cdr region) t)
  279. spec)
  280. data-val)
  281. (push (list (car region) (cdr region) spec)
  282. data-val))))
  283. (setq pos (org-fold-next-folding-state-change spec pos)))))
  284. data-val))))
  285. (unwind-protect (progn ,@body)
  286. (org-with-wide-buffer
  287. (org-fold-region (point-min) (point-max) nil)
  288. (pcase-dolist (`(,beg ,end ,spec) (delq nil ,data))
  289. (org-fold-region beg end t spec)
  290. (when ,markers?
  291. (set-marker beg nil)
  292. (set-marker end nil))))))))
  293. (defmacro org-fold-save-outline-visibility (use-markers &rest body)
  294. "Save and restore outline visibility around BODY.
  295. If USE-MARKERS is non-nil, use markers for the positions. This
  296. means that the buffer may change while running BODY, but it also
  297. means that the buffer should stay alive during the operation,
  298. because otherwise all these markers will point to nowhere."
  299. (declare (debug (form body)) (indent 1))
  300. `(if (eq org-fold-core-style 'text-properties)
  301. (org-fold-save-outline-visibility--text-properties ,use-markers ,@body)
  302. (org-fold-save-outline-visibility--overlays ,use-markers ,@body)))
  303. ;;;; Changing visibility (regions, blocks, drawers, headlines)
  304. ;;;;; Region visibility
  305. ;; (defalias 'org-fold-region #'org-fold-core-region)
  306. (defun org-fold-region--overlays (from to flag spec)
  307. "Hide or show lines from FROM to TO, according to FLAG.
  308. SPEC is the invisibility spec, as a symbol."
  309. (remove-overlays from to 'invisible spec)
  310. ;; Use `front-advance' since text right before to the beginning of
  311. ;; the overlay belongs to the visible line than to the contents.
  312. (when flag
  313. (let ((o (make-overlay from to nil 'front-advance)))
  314. (overlay-put o 'evaporate t)
  315. (overlay-put o 'invisible spec)
  316. (overlay-put o
  317. 'isearch-open-invisible
  318. (lambda (&rest _) (org-fold-show-context 'isearch))))))
  319. (defsubst org-fold-region (from to flag &optional spec)
  320. "Hide or show lines from FROM to TO, according to FLAG.
  321. SPEC is the invisibility spec, as a symbol."
  322. (if (eq org-fold-core-style 'text-properties)
  323. (org-fold-core-region from to flag spec)
  324. (org-fold-region--overlays from to flag spec)))
  325. (defun org-fold-show-all--text-properties (&optional types)
  326. "Show all contents in the visible part of the buffer.
  327. By default, the function expands headings, blocks and drawers.
  328. When optional argument TYPES is a list of symbols among `blocks',
  329. `drawers' and `headings', to only expand one specific type."
  330. (interactive)
  331. (dolist (type (or types '(blocks drawers headings)))
  332. (org-fold-region (point-min) (point-max) nil
  333. (pcase type
  334. (`blocks 'block)
  335. (`drawers 'drawer)
  336. (`headings 'headline)
  337. (_ (error "Invalid type: %S" type))))))
  338. (defun org-fold-show-all--overlays (&optional types)
  339. "Show all contents in the visible part of the buffer.
  340. By default, the function expands headings, blocks and drawers.
  341. When optional argument TYPE is a list of symbols among `blocks',
  342. `drawers' and `headings', to only expand one specific type."
  343. (interactive)
  344. (let ((types (or types '(blocks drawers headings))))
  345. (when (memq 'blocks types)
  346. (org-fold-region (point-min) (point-max) nil 'org-hide-block))
  347. (cond
  348. ;; Fast path. Since headings and drawers share the same
  349. ;; invisible spec, clear everything in one go.
  350. ((and (memq 'headings types)
  351. (memq 'drawers types))
  352. (org-fold-region (point-min) (point-max) nil 'outline))
  353. ((memq 'headings types)
  354. (org-fold-region (point-min) (point-max) nil 'outline)
  355. (org-cycle-hide-drawers 'all))
  356. ((memq 'drawers types)
  357. (save-excursion
  358. (goto-char (point-min))
  359. (while (re-search-forward org-drawer-regexp nil t)
  360. (let* ((pair (get-char-property-and-overlay (line-beginning-position)
  361. 'invisible))
  362. (o (cdr-safe pair)))
  363. (if (overlayp o) (goto-char (overlay-end o))
  364. (pcase (get-char-property-and-overlay (point) 'invisible)
  365. (`(outline . ,o)
  366. (goto-char (overlay-end o))
  367. (delete-overlay o))
  368. (_ nil))))))))))
  369. (defsubst org-fold-show-all (&optional types)
  370. "Show all contents in the visible part of the buffer.
  371. By default, the function expands headings, blocks and drawers.
  372. When optional argument TYPES is a list of symbols among `blocks',
  373. `drawers' and `headings', to only expand one specific type."
  374. (interactive)
  375. (if (eq org-fold-core-style 'text-properties)
  376. (org-fold-show-all--text-properties types)
  377. (org-fold-show-all--overlays types)))
  378. (defun org-fold-flag-above-first-heading (&optional arg)
  379. "Hide from bob up to the first heading.
  380. Move point to the beginning of first heading or end of buffer."
  381. (goto-char (point-min))
  382. (unless (org-at-heading-p)
  383. (outline-next-heading))
  384. (unless (bobp)
  385. (org-fold-region 1 (1- (point)) (not arg) 'outline)))
  386. ;;;;; Heading visibility
  387. (defun org-fold-heading (flag &optional entry)
  388. "Fold/unfold the current heading. FLAG non-nil means make invisible.
  389. When ENTRY is non-nil, show the entire entry."
  390. (save-excursion
  391. (org-back-to-heading t)
  392. ;; Check if we should show the entire entry
  393. (if (not entry)
  394. (org-fold-region
  395. (line-end-position 0) (line-end-position) flag 'outline)
  396. (org-fold-show-entry)
  397. (save-excursion
  398. ;; FIXME: potentially catches inlinetasks
  399. (and (outline-next-heading)
  400. (org-fold-heading nil))))))
  401. (defun org-fold-hide-entry ()
  402. "Hide the body directly following this heading."
  403. (interactive)
  404. (save-excursion
  405. (org-back-to-heading-or-point-min t)
  406. (when (org-at-heading-p) (forward-line))
  407. (unless (eobp) ; Current headline is empty and ends at the end of buffer.
  408. (org-fold-region
  409. (line-end-position 0)
  410. (save-excursion
  411. (if (re-search-forward
  412. (concat "[\r\n]" (org-get-limited-outline-regexp)) nil t)
  413. (line-end-position 0)
  414. (point-max)))
  415. t
  416. 'outline))))
  417. (defun org-fold-subtree (flag)
  418. (save-excursion
  419. (org-back-to-heading t)
  420. (org-fold-region
  421. (line-end-position)
  422. (progn (org-end-of-subtree t) (point))
  423. flag
  424. 'outline)))
  425. ;; Replaces `outline-hide-subtree'.
  426. (defun org-fold-hide-subtree ()
  427. "Hide everything after this heading at deeper levels."
  428. (interactive)
  429. (org-fold-subtree t))
  430. ;; Replaces `outline-hide-sublevels'
  431. (defun org-fold-hide-sublevels (levels)
  432. "Hide everything but the top LEVELS levels of headers, in whole buffer.
  433. This also unhides the top heading-less body, if any.
  434. Interactively, the prefix argument supplies the value of LEVELS.
  435. When invoked without a prefix argument, LEVELS defaults to the level
  436. of the current heading, or to 1 if the current line is not a heading."
  437. (interactive (list
  438. (cond
  439. (current-prefix-arg (prefix-numeric-value current-prefix-arg))
  440. ((save-excursion (beginning-of-line)
  441. (looking-at outline-regexp))
  442. (funcall outline-level))
  443. (t 1))))
  444. (if (< levels 1)
  445. (error "Must keep at least one level of headers"))
  446. (save-excursion
  447. (let* ((beg (progn
  448. (goto-char (point-min))
  449. ;; Skip the prelude, if any.
  450. (unless (org-at-heading-p) (outline-next-heading))
  451. (point)))
  452. (end (progn
  453. (goto-char (point-max))
  454. ;; Keep empty last line, if available.
  455. (max (point-min) (if (bolp) (1- (point)) (point))))))
  456. (if (< end beg)
  457. (setq beg (prog1 end (setq end beg))))
  458. ;; First hide everything.
  459. (org-fold-region beg end t 'headline)
  460. ;; Then unhide the top level headers.
  461. (org-map-region
  462. (lambda ()
  463. (when (<= (funcall outline-level) levels)
  464. (org-fold-heading nil)))
  465. beg end)
  466. ;; Finally unhide any trailing newline.
  467. (goto-char (point-max))
  468. (if (and (bolp) (not (bobp)) (outline-invisible-p (1- (point))))
  469. (org-fold-region (max (point-min) (1- (point))) (point) nil)))))
  470. (defun org-fold-show-entry ()
  471. "Show the body directly following its heading.
  472. Show the heading too, if it is currently invisible."
  473. (interactive)
  474. (save-excursion
  475. (org-back-to-heading-or-point-min t)
  476. (org-fold-region
  477. (line-end-position 0)
  478. (save-excursion
  479. (if (re-search-forward
  480. (concat "[\r\n]\\(" (org-get-limited-outline-regexp) "\\)") nil t)
  481. (match-beginning 1)
  482. (point-max)))
  483. nil
  484. 'outline)
  485. (org-cycle-hide-drawers 'children)))
  486. (defalias 'org-fold-show-hidden-entry #'org-fold-show-entry
  487. "Show an entry where even the heading is hidden.")
  488. (defun org-fold-show-siblings ()
  489. "Show all siblings of the current headline."
  490. (save-excursion
  491. (while (org-goto-sibling) (org-fold-heading nil)))
  492. (save-excursion
  493. (while (org-goto-sibling 'previous)
  494. (org-fold-heading nil))))
  495. (defun org-fold-show-children (&optional level)
  496. "Show all direct subheadings of this heading.
  497. Prefix arg LEVEL is how many levels below the current level
  498. should be shown. Default is enough to cause the following
  499. heading to appear."
  500. (interactive "p")
  501. (unless (org-before-first-heading-p)
  502. (save-excursion
  503. (org-with-limited-levels (org-back-to-heading t))
  504. (let* ((current-level (funcall outline-level))
  505. (max-level (org-get-valid-level
  506. current-level
  507. (if level (prefix-numeric-value level) 1)))
  508. (end (save-excursion (org-end-of-subtree t t)))
  509. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  510. (past-first-child nil)
  511. ;; Make sure to skip inlinetasks.
  512. (re (format regexp-fmt
  513. current-level
  514. (cond
  515. ((not (featurep 'org-inlinetask)) "")
  516. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  517. 3))
  518. (t (1- org-inlinetask-min-level))))))
  519. ;; Display parent heading.
  520. (org-fold-heading nil)
  521. (forward-line)
  522. ;; Display children. First child may be deeper than expected
  523. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  524. ;; MAX-LEVEL accordingly.
  525. (while (re-search-forward re end t)
  526. (unless past-first-child
  527. (setq re (format regexp-fmt
  528. current-level
  529. (max (funcall outline-level) max-level)))
  530. (setq past-first-child t))
  531. (org-fold-heading nil))))))
  532. (defun org-fold-show-subtree ()
  533. "Show everything after this heading at deeper levels."
  534. (interactive)
  535. (org-fold-region
  536. (point) (save-excursion (org-end-of-subtree t t)) nil 'outline))
  537. (defun org-fold-show-branches ()
  538. "Show all subheadings of this heading, but not their bodies."
  539. (interactive)
  540. (org-fold-show-children 1000))
  541. (defun org-fold-show-branches-buffer--text-properties ()
  542. "Show all branches in the buffer."
  543. (org-fold-flag-above-first-heading)
  544. (org-fold-hide-sublevels 1)
  545. (unless (eobp)
  546. (org-fold-show-branches)
  547. (while (outline-get-next-sibling)
  548. (org-fold-show-branches)))
  549. (goto-char (point-min)))
  550. (defun org-fold-show-branches-buffer--overlays ()
  551. "Show all branches in the buffer."
  552. (org-fold-flag-above-first-heading)
  553. (outline-hide-sublevels 1)
  554. (unless (eobp)
  555. (outline-show-branches)
  556. (while (outline-get-next-sibling)
  557. (outline-show-branches)))
  558. (goto-char (point-min)))
  559. (defsubst org-fold-show-branches-buffer ()
  560. "Show all branches in the buffer."
  561. (if (eq org-fold-core-style 'text-properties)
  562. (org-fold-show-branches-buffer--text-properties)
  563. (org-fold-show-branches-buffer--overlays)))
  564. ;;;;; Blocks and drawers visibility
  565. (defun org-fold--hide-wrapper-toggle (element category force no-error)
  566. "Toggle visibility for ELEMENT.
  567. ELEMENT is a block or drawer type parsed element. CATEGORY is
  568. either `block' or `drawer'. When FORCE is `off', show the block
  569. or drawer. If it is non-nil, hide it unconditionally. Throw an
  570. error when not at a block or drawer, unless NO-ERROR is non-nil.
  571. Return a non-nil value when toggling is successful."
  572. (let ((type (org-element-type element)))
  573. (cond
  574. ((memq type
  575. (pcase category
  576. (`drawer '(drawer property-drawer))
  577. (`block '(center-block
  578. comment-block dynamic-block example-block export-block
  579. quote-block special-block src-block verse-block))
  580. (_ (error "Unknown category: %S" category))))
  581. (let* ((post (org-element-property :post-affiliated element))
  582. (start (save-excursion
  583. (goto-char post)
  584. (line-end-position)))
  585. (end (save-excursion
  586. (goto-char (org-element-property :end element))
  587. (skip-chars-backward " \t\n")
  588. (line-end-position))))
  589. ;; Do nothing when not before or at the block opening line or
  590. ;; at the block closing line.
  591. (unless (let ((eol (line-end-position)))
  592. (and (> eol start) (/= eol end)))
  593. (let* ((spec (if (eq org-fold-core-style 'text-properties)
  594. category
  595. (if (eq category 'block) 'org-hide-block 'outline)))
  596. (flag
  597. (cond ((eq force 'off) nil)
  598. (force t)
  599. ((if (eq org-fold-core-style 'text-properties)
  600. (org-fold-folded-p start spec)
  601. (eq spec (get-char-property start 'invisible)))
  602. nil)
  603. (t t))))
  604. (org-fold-region start end flag spec))
  605. ;; When the block is hidden away, make sure point is left in
  606. ;; a visible part of the buffer.
  607. (when (invisible-p (max (1- (point)) (point-min)))
  608. (goto-char post))
  609. ;; Signal success.
  610. t)))
  611. (no-error nil)
  612. (t
  613. (user-error (format "%s@%s: %s"
  614. (buffer-file-name (buffer-base-buffer))
  615. (point)
  616. (if (eq category 'drawer)
  617. "Not at a drawer"
  618. "Not at a block")))))))
  619. (defun org-fold-hide-block-toggle (&optional force no-error element)
  620. "Toggle the visibility of the current block.
  621. When optional argument FORCE is `off', make block visible. If it
  622. is non-nil, hide it unconditionally. Throw an error when not at
  623. a block, unless NO-ERROR is non-nil. When optional argument
  624. ELEMENT is provided, consider it instead of the current block.
  625. Return a non-nil value when toggling is successful."
  626. (interactive)
  627. (org-fold--hide-wrapper-toggle
  628. (or element (org-element-at-point)) 'block force no-error))
  629. (defun org-fold-hide-drawer-toggle (&optional force no-error element)
  630. "Toggle the visibility of the current drawer.
  631. When optional argument FORCE is `off', make drawer visible. If
  632. it is non-nil, hide it unconditionally. Throw an error when not
  633. at a drawer, unless NO-ERROR is non-nil. When optional argument
  634. ELEMENT is provided, consider it instead of the current drawer.
  635. Return a non-nil value when toggling is successful."
  636. (interactive)
  637. (org-fold--hide-wrapper-toggle
  638. (or element (org-element-at-point)) 'drawer force no-error))
  639. (defun org-fold-hide-block-all ()
  640. "Fold all blocks in the current buffer."
  641. (interactive)
  642. (org-block-map (apply-partially #'org-fold-hide-block-toggle 'hide)))
  643. (defun org-fold-hide-drawer-all ()
  644. "Fold all drawers in the current buffer."
  645. (let ((begin (point-min))
  646. (end (point-max)))
  647. (org-fold--hide-drawers begin end)))
  648. (defun org-fold--hide-drawers--overlays (begin end)
  649. "Hide all drawers between BEGIN and END."
  650. (save-excursion
  651. (goto-char begin)
  652. (while (and (< (point) end) (re-search-forward org-drawer-regexp end t))
  653. (let* ((pair (get-char-property-and-overlay (line-beginning-position)
  654. 'invisible))
  655. (o (cdr-safe pair)))
  656. (if (overlayp o) (goto-char (overlay-end o)) ;invisible drawer
  657. (pcase (get-char-property-and-overlay (point) 'invisible)
  658. (`(outline . ,o) (goto-char (overlay-end o))) ;already folded
  659. (_
  660. (let* ((drawer (org-element-at-point))
  661. (type (org-element-type drawer)))
  662. (when (memq type '(drawer property-drawer))
  663. (org-fold-hide-drawer-toggle t nil drawer)
  664. ;; Make sure to skip drawer entirely or we might flag it
  665. ;; another time when matching its ending line with
  666. ;; `org-drawer-regexp'.
  667. (goto-char (org-element-property :end drawer)))))))))))
  668. (defun org-fold--hide-drawers--text-properties (begin end)
  669. "Hide all drawers between BEGIN and END."
  670. (save-excursion
  671. (goto-char begin)
  672. (while (and (< (point) end)
  673. (re-search-forward org-drawer-regexp end t))
  674. ;; Skip folded drawers
  675. (if (org-fold-folded-p nil 'drawer)
  676. (goto-char (org-fold-next-folding-state-change 'drawer nil end))
  677. (let* ((drawer (org-element-at-point))
  678. (type (org-element-type drawer)))
  679. (when (memq type '(drawer property-drawer))
  680. (org-fold-hide-drawer-toggle t nil drawer)
  681. ;; Make sure to skip drawer entirely or we might flag it
  682. ;; another time when matching its ending line with
  683. ;; `org-drawer-regexp'.
  684. (goto-char (org-element-property :end drawer))))))))
  685. (defun org-fold--hide-drawers (begin end)
  686. "Hide all drawers between BEGIN and END."
  687. (if (eq org-fold-core-style 'text-properties)
  688. (org-fold--hide-drawers--text-properties begin end)
  689. (org-fold--hide-drawers--overlays begin end)))
  690. (defun org-fold-hide-archived-subtrees (beg end)
  691. "Re-hide all archived subtrees after a visibility state change."
  692. (org-with-wide-buffer
  693. (let ((case-fold-search nil)
  694. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  695. (goto-char beg)
  696. ;; Include headline point is currently on.
  697. (beginning-of-line)
  698. (while (and (< (point) end) (re-search-forward re end t))
  699. (when (member org-archive-tag (org-get-tags nil t))
  700. (org-fold-subtree t)
  701. (org-end-of-subtree t))))))
  702. ;;;;; Reveal point location
  703. (defun org-fold-show-context (&optional key)
  704. "Make sure point and context are visible.
  705. Optional argument KEY, when non-nil, is a symbol. See
  706. `org-fold-show-context-detail' for allowed values and how much is to
  707. be shown."
  708. (org-fold-show-set-visibility
  709. (cond ((symbolp org-fold-show-context-detail) org-fold-show-context-detail)
  710. ((cdr (assq key org-fold-show-context-detail)))
  711. (t (cdr (assq 'default org-fold-show-context-detail))))))
  712. (defun org-fold-show-set-visibility--overlays (detail)
  713. "Set visibility around point according to DETAIL.
  714. DETAIL is either nil, `minimal', `local', `ancestors',
  715. `ancestors-full', `lineage', `tree', `canonical' or t. See
  716. `org-show-context-detail' for more information."
  717. ;; Show current heading and possibly its entry, following headline
  718. ;; or all children.
  719. (if (and (org-at-heading-p) (not (eq detail 'local)))
  720. (org-fold-heading nil)
  721. (org-fold-show-entry)
  722. ;; If point is hidden within a drawer or a block, make sure to
  723. ;; expose it.
  724. (dolist (o (overlays-at (point)))
  725. (when (memq (overlay-get o 'invisible) '(org-hide-block outline))
  726. (delete-overlay o)))
  727. (unless (org-before-first-heading-p)
  728. (org-with-limited-levels
  729. (cl-case detail
  730. ((tree canonical t) (org-fold-show-children))
  731. ((nil minimal ancestors ancestors-full))
  732. (t (save-excursion
  733. (outline-next-heading)
  734. (org-fold-heading nil)))))))
  735. ;; Show whole subtree.
  736. (when (eq detail 'ancestors-full) (org-fold-show-subtree))
  737. ;; Show all siblings.
  738. (when (eq detail 'lineage) (org-fold-show-siblings))
  739. ;; Show ancestors, possibly with their children.
  740. (when (memq detail '(ancestors ancestors-full lineage tree canonical t))
  741. (save-excursion
  742. (while (org-up-heading-safe)
  743. (org-fold-heading nil)
  744. (when (memq detail '(canonical t)) (org-fold-show-entry))
  745. (when (memq detail '(tree canonical t)) (org-fold-show-children))))))
  746. (defvar org-hide-emphasis-markers); Defined in org.el
  747. (defvar org-pretty-entities); Defined in org.el
  748. (defun org-fold-show-set-visibility--text-properties (detail)
  749. "Set visibility around point according to DETAIL.
  750. DETAIL is either nil, `minimal', `local', `ancestors',
  751. `ancestors-full', `lineage', `tree', `canonical' or t. See
  752. `org-show-context-detail' for more information."
  753. ;; Show current heading and possibly its entry, following headline
  754. ;; or all children.
  755. (if (and (org-at-heading-p) (not (eq detail 'local)))
  756. (org-fold-heading nil)
  757. (org-fold-show-entry)
  758. ;; If point is hidden make sure to expose it.
  759. (when (org-invisible-p)
  760. ;; FIXME: No clue why, but otherwise the following might not work.
  761. (redisplay)
  762. (let ((region (org-fold-get-region-at-point)))
  763. ;; Reveal emphasis markers.
  764. (when (eq detail 'local)
  765. (let (org-hide-emphasis-markers
  766. org-link-descriptive
  767. org-pretty-entities
  768. (org-hide-macro-markers nil)
  769. (region (or (org-find-text-property-region (point) 'org-emphasis)
  770. (org-find-text-property-region (point) 'org-macro)
  771. (org-find-text-property-region (point) 'invisible)
  772. region)))
  773. ;; Silence byte-compiler.
  774. (ignore org-hide-macro-markers)
  775. (when region
  776. (org-with-point-at (car region)
  777. (beginning-of-line)
  778. (let (font-lock-extend-region-functions)
  779. (font-lock-fontify-region (max (point-min) (1- (car region))) (cdr region))))))
  780. ;; Unfold links.
  781. (when region
  782. (dolist (spec '(org-link org-link-description))
  783. (org-fold-region (car region) (cdr region) nil spec))))
  784. (when region
  785. (dolist (spec (org-fold-core-folding-spec-list))
  786. ;; Links are taken care by above.
  787. (unless (memq spec '(org-link org-link-description))
  788. (org-fold-region (car region) (cdr region) nil spec))))))
  789. (unless (org-before-first-heading-p)
  790. (org-with-limited-levels
  791. (cl-case detail
  792. ((tree canonical t) (org-fold-show-children))
  793. ((nil minimal ancestors ancestors-full))
  794. (t (save-excursion
  795. (outline-next-heading)
  796. (org-fold-heading nil)))))))
  797. ;; Show whole subtree.
  798. (when (eq detail 'ancestors-full) (org-fold-show-subtree))
  799. ;; Show all siblings.
  800. (when (eq detail 'lineage) (org-fold-show-siblings))
  801. ;; Show ancestors, possibly with their children.
  802. (when (memq detail '(ancestors ancestors-full lineage tree canonical t))
  803. (save-excursion
  804. (while (org-up-heading-safe)
  805. (org-fold-heading nil)
  806. (when (memq detail '(canonical t)) (org-fold-show-entry))
  807. (when (memq detail '(tree canonical t)) (org-fold-show-children))))))
  808. (defun org-fold-show-set-visibility (detail)
  809. "Set visibility around point according to DETAIL.
  810. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  811. `tree', `canonical' or t. See `org-fold-show-context-detail' for more
  812. information."
  813. (if (eq org-fold-core-style 'text-properties)
  814. (org-fold-show-set-visibility--text-properties detail)
  815. (org-fold-show-set-visibility--overlays detail)))
  816. (defun org-fold-reveal (&optional siblings)
  817. "Show current entry, hierarchy above it, and the following headline.
  818. This can be used to show a consistent set of context around
  819. locations exposed with `org-fold-show-context'.
  820. With optional argument SIBLINGS, on each level of the hierarchy all
  821. siblings are shown. This repairs the tree structure to what it would
  822. look like when opened with hierarchical calls to `org-cycle'.
  823. With a \\[universal-argument] \\[universal-argument] prefix, \
  824. go to the parent and show the entire tree."
  825. (interactive "P")
  826. (run-hooks 'org-fold-reveal-start-hook)
  827. (cond ((equal siblings '(4)) (org-fold-show-set-visibility 'canonical))
  828. ((equal siblings '(16))
  829. (save-excursion
  830. (when (org-up-heading-safe)
  831. (org-fold-show-subtree)
  832. (run-hook-with-args 'org-cycle-hook 'subtree))))
  833. (t (org-fold-show-set-visibility 'lineage))))
  834. ;;; Make isearch search in some text hidden via text properties.
  835. (defun org-fold--isearch-reveal (&rest _)
  836. "Reveal text at POS found by isearch."
  837. (org-fold-show-context 'isearch))
  838. ;;; Handling changes in folded elements
  839. (defun org-fold--extend-changed-region (from to)
  840. "Consider folded regions in the next/previous line when fixing
  841. region visibility.
  842. This function is intended to be used as a member of
  843. `org-fold-core-extend-changed-region-functions'."
  844. ;; If the edit is done in the first line of a folded drawer/block,
  845. ;; the folded text is only starting from the next line and needs to
  846. ;; be checked.
  847. (setq to (save-excursion (goto-char to) (line-beginning-position 2)))
  848. ;; If the ":END:" line of the drawer is deleted, the folded text is
  849. ;; only ending at the previous line and needs to be checked.
  850. (setq from (save-excursion (goto-char from) (line-beginning-position 0)))
  851. (cons from to))
  852. (defun org-fold--reveal-headline-at-point ()
  853. "Reveal header line and empty contents inside.
  854. Reveal the header line and, if present, also reveal its contents, when
  855. the contents consists of blank lines.
  856. Assume that point is located at the header line."
  857. (org-with-wide-buffer
  858. (beginning-of-line)
  859. (org-fold-region
  860. (max (point-min) (1- (point)))
  861. (let ((endl (line-end-position)))
  862. (save-excursion
  863. (goto-char endl)
  864. (skip-chars-forward "\n\t\r ")
  865. ;; Unfold blank lines after newly inserted headline.
  866. (if (equal (point)
  867. (save-excursion
  868. (goto-char endl)
  869. (org-end-of-subtree)
  870. (skip-chars-forward "\n\t\r ")))
  871. (point)
  872. endl)))
  873. nil 'headline)))
  874. (defun org-fold--reveal-outline-maybe (region _)
  875. "Reveal folded outline in REGION when needed.
  876. This function is intended to be used as :fragile property of
  877. `org-fold-outline' spec. See `org-fold-core--specs' for details."
  878. (save-match-data
  879. (org-with-wide-buffer
  880. (goto-char (car region))
  881. ;; The line before beginning of the fold should be either a
  882. ;; headline or a list item.
  883. (backward-char)
  884. (beginning-of-line)
  885. ;; Make sure that headline is not partially hidden.
  886. (unless (org-fold-folded-p nil 'headline)
  887. (org-fold--reveal-headline-at-point))
  888. ;; Never hide level 1 headlines
  889. (save-excursion
  890. (goto-char (line-end-position))
  891. (unless (>= (point) (cdr region))
  892. (when (re-search-forward (rx bol "* ") (cdr region) t)
  893. (org-fold--reveal-headline-at-point))))
  894. ;; Make sure that headline after is not partially hidden.
  895. (goto-char (cdr region))
  896. (beginning-of-line)
  897. (unless (org-fold-folded-p nil 'headline)
  898. (when (looking-at-p org-element-headline-re)
  899. (org-fold--reveal-headline-at-point)))
  900. ;; Check the validity of headline
  901. (goto-char (car region))
  902. (backward-char)
  903. (beginning-of-line)
  904. (unless (let ((case-fold-search t))
  905. (looking-at (rx-to-string
  906. `(or (regex ,(org-item-re))
  907. (regex ,org-outline-regexp-bol)))))
  908. t))))
  909. (defun org-fold--reveal-drawer-or-block-maybe (region spec)
  910. "Reveal folded drawer/block (according to SPEC) in REGION when needed.
  911. This function is intended to be used as :fragile property of
  912. `org-fold-drawer' or `org-fold-block' spec."
  913. (let ((begin-re (cond
  914. ((eq spec (org-fold-core-get-folding-spec-from-alias 'drawer))
  915. org-drawer-regexp)
  916. ;; Group one below contains the type of the block.
  917. ((eq spec (org-fold-core-get-folding-spec-from-alias 'block))
  918. (rx bol (zero-or-more (any " " "\t"))
  919. "#+begin"
  920. (or ":"
  921. (seq "_"
  922. (group (one-or-more (not (syntax whitespace))))))))))
  923. ;; To be determined later. May depend on `begin-re' match (i.e. for blocks).
  924. end-re)
  925. (save-match-data ; we should not clobber match-data in after-change-functions
  926. (let ((fold-begin (car region))
  927. (fold-end (cdr region)))
  928. (let (unfold?)
  929. (catch :exit
  930. ;; The line before folded text should be beginning of
  931. ;; the drawer/block.
  932. (save-excursion
  933. (goto-char fold-begin)
  934. ;; The line before beginning of the fold should be the
  935. ;; first line of the drawer/block.
  936. (backward-char)
  937. (beginning-of-line)
  938. (unless (let ((case-fold-search t))
  939. (looking-at begin-re)) ; the match-data will be used later
  940. (throw :exit (setq unfold? t))))
  941. ;; Set `end-re' for the current drawer/block.
  942. (setq end-re
  943. (cond
  944. ((eq spec (org-fold-core-get-folding-spec-from-alias 'drawer))
  945. org-property-end-re)
  946. ((eq spec (org-fold-core-get-folding-spec-from-alias 'block))
  947. (let ((block-type (match-string 1))) ; the last match is from `begin-re'
  948. (concat (rx bol (zero-or-more (any " " "\t")) "#+end")
  949. (if block-type
  950. (concat "_"
  951. (regexp-quote block-type)
  952. (rx (zero-or-more (any " " "\t")) eol))
  953. (rx (opt ":") (zero-or-more (any " " "\t")) eol)))))))
  954. ;; The last line of the folded text should match `end-re'.
  955. (save-excursion
  956. (goto-char fold-end)
  957. (beginning-of-line)
  958. (unless (let ((case-fold-search t))
  959. (looking-at end-re))
  960. (throw :exit (setq unfold? t))))
  961. ;; There should be no `end-re' or
  962. ;; `org-outline-regexp-bol' anywhere in the
  963. ;; drawer/block body.
  964. (save-excursion
  965. (goto-char fold-begin)
  966. (when (save-excursion
  967. (let ((case-fold-search t))
  968. (re-search-forward (rx-to-string `(or (regex ,end-re)
  969. (regex ,org-outline-regexp-bol)))
  970. (max (point)
  971. (1- (save-excursion
  972. (goto-char fold-end)
  973. (line-beginning-position))))
  974. t)))
  975. (throw :exit (setq unfold? t)))))
  976. unfold?)))))
  977. ;; Catching user edits inside invisible text
  978. (defun org-fold-check-before-invisible-edit--overlays (kind)
  979. "Check if editing KIND is dangerous with invisible text around.
  980. The detailed reaction depends on the user option
  981. `org-fold-catch-invisible-edits'."
  982. ;; First, try to get out of here as quickly as possible, to reduce overhead
  983. (when (and org-fold-catch-invisible-edits
  984. (or (not (boundp 'visible-mode)) (not visible-mode))
  985. (or (get-char-property (point) 'invisible)
  986. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  987. ;; OK, we need to take a closer look. Do not consider
  988. ;; invisibility obtained through text properties (e.g., link
  989. ;; fontification), as it cannot be toggled.
  990. (let* ((invisible-at-point
  991. (pcase (get-char-property-and-overlay (point) 'invisible)
  992. (`(,_ . ,(and (pred overlayp) o)) o)))
  993. (invisible-before-point
  994. (and (not (bobp))
  995. (pcase (get-char-property-and-overlay (1- (point)) 'invisible)
  996. (`(,_ . ,(and (pred overlayp) o)) o))))
  997. (border-and-ok-direction
  998. (or
  999. ;; Check if we are acting predictably before invisible
  1000. ;; text.
  1001. (and invisible-at-point (not invisible-before-point)
  1002. (memq kind '(insert delete-backward)))
  1003. ;; Check if we are acting predictably after invisible text
  1004. ;; This works not well, and I have turned it off. It seems
  1005. ;; better to always show and stop after invisible text.
  1006. ;; (and (not invisible-at-point) invisible-before-point
  1007. ;; (memq kind '(insert delete)))
  1008. )))
  1009. (when (or invisible-at-point invisible-before-point)
  1010. (when (eq org-fold-catch-invisible-edits 'error)
  1011. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  1012. (if (and org-custom-properties-overlays
  1013. (y-or-n-p "Display invisible properties in this buffer? "))
  1014. (org-toggle-custom-properties-visibility)
  1015. ;; Make the area visible
  1016. (save-excursion
  1017. (when (and (not invisible-at-point) invisible-before-point)
  1018. (goto-char
  1019. (previous-single-char-property-change (point) 'invisible)))
  1020. ;; Remove whatever overlay is currently making yet-to-be
  1021. ;; edited text invisible. Also remove nested invisibility
  1022. ;; related overlays.
  1023. (delete-overlay (or invisible-at-point invisible-before-point))
  1024. (let ((origin (if invisible-at-point (point) (1- (point)))))
  1025. (while (pcase (get-char-property-and-overlay origin 'invisible)
  1026. (`(,_ . ,(and (pred overlayp) o))
  1027. (delete-overlay o)
  1028. t)))))
  1029. (cond
  1030. ((eq org-fold-catch-invisible-edits 'show)
  1031. ;; That's it, we do the edit after showing
  1032. (message
  1033. "Unfolding invisible region around point before editing")
  1034. (sit-for 1))
  1035. ((and (eq org-fold-catch-invisible-edits 'smart)
  1036. border-and-ok-direction)
  1037. (message "Unfolding invisible region around point before editing"))
  1038. (t
  1039. ;; Don't do the edit, make the user repeat it in full visibility
  1040. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  1041. (defun org-fold-check-before-invisible-edit--text-properties (kind)
  1042. "Check if editing KIND is dangerous with invisible text around.
  1043. The detailed reaction depends on the user option
  1044. `org-fold-catch-invisible-edits'."
  1045. ;; First, try to get out of here as quickly as possible, to reduce overhead
  1046. (when (and org-fold-catch-invisible-edits
  1047. (or (not (boundp 'visible-mode)) (not visible-mode))
  1048. (or (org-invisible-p)
  1049. (org-invisible-p (max (point-min) (1- (point))))))
  1050. ;; OK, we need to take a closer look. Only consider invisibility
  1051. ;; caused by folding of headlines, drawers, and blocks. Edits
  1052. ;; inside links will be handled by font-lock.
  1053. (let* ((invisible-at-point (org-fold-folded-p (point) '(headline drawer block)))
  1054. (invisible-before-point
  1055. (and (not (bobp))
  1056. (org-fold-folded-p (1- (point)) '(headline drawer block))))
  1057. (border-and-ok-direction
  1058. (or
  1059. ;; Check if we are acting predictably before invisible
  1060. ;; text.
  1061. (and invisible-at-point (not invisible-before-point)
  1062. (memq kind '(insert delete-backward)))
  1063. ;; Check if we are acting predictably after invisible text
  1064. ;; This works not well, and I have turned it off. It seems
  1065. ;; better to always show and stop after invisible text.
  1066. ;; (and (not invisible-at-point) invisible-before-point
  1067. ;; (memq kind '(insert delete)))
  1068. )))
  1069. (when (or invisible-at-point invisible-before-point)
  1070. (when (eq org-fold-catch-invisible-edits 'error)
  1071. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  1072. (if (and org-custom-properties-overlays
  1073. (y-or-n-p "Display invisible properties in this buffer? "))
  1074. (org-toggle-custom-properties-visibility)
  1075. ;; Make the area visible
  1076. (save-excursion
  1077. (org-fold-show-set-visibility 'local))
  1078. (when invisible-before-point
  1079. (org-with-point-at (1- (point)) (org-fold-show-set-visibility 'local)))
  1080. (cond
  1081. ((eq org-fold-catch-invisible-edits 'show)
  1082. ;; That's it, we do the edit after showing
  1083. (message
  1084. "Unfolding invisible region around point before editing")
  1085. (sit-for 1))
  1086. ((and (eq org-fold-catch-invisible-edits 'smart)
  1087. border-and-ok-direction)
  1088. (message "Unfolding invisible region around point before editing"))
  1089. (t
  1090. ;; Don't do the edit, make the user repeat it in full visibility
  1091. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  1092. (defsubst org-fold-check-before-invisible-edit (kind)
  1093. "Check if editing KIND is dangerous with invisible text around.
  1094. The detailed reaction depends on the user option
  1095. `org-fold-catch-invisible-edits'."
  1096. ;; First, try to get out of here as quickly as possible, to reduce overhead
  1097. (if (eq org-fold-core-style 'text-properties)
  1098. (org-fold-check-before-invisible-edit--text-properties kind)
  1099. (org-fold-check-before-invisible-edit--overlays kind)))
  1100. (provide 'org-fold)
  1101. ;;; org-fold.el ends here