org-fold.el 49 KB

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