org-fold.el 49 KB

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