org-fold.el 48 KB

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