org-cycle.el 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. ;;; org-cycle.el --- Visibility cycling of Org entries -*- lexical-binding: t; -*-
  2. ;;
  3. ;; Copyright (C) 2020-2020 Free Software Foundation, Inc.
  4. ;;
  5. ;; Maintainer: Ihor Radchenko <yantar92 at gmail dot com>
  6. ;; Keywords: folding, visibility cycling, 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 controlling global folding state in buffer
  25. ;; and TAB-cycling.
  26. ;;; Code:
  27. (require 'org-macs)
  28. (require 'org-fold)
  29. (declare-function org-element-type "org-element" (element))
  30. (declare-function org-element-property "org-element" (property element))
  31. (declare-function org-element-lineage "org-element" (datum &optional types with-self))
  32. (declare-function org-element-at-point "org-element" (&optional pom cached-only))
  33. (declare-function org-get-tags "org" (&optional pos local fontify))
  34. (declare-function org-subtree-end-visible-p "org" ())
  35. (declare-function org-narrow-to-subtree "org" (&optional element))
  36. (declare-function org-at-property-p "org" ())
  37. (declare-function org-re-property "org" (property &optional literal allow-null value))
  38. (declare-function org-item-beginning-re "org" ())
  39. (declare-function org-at-heading-p "org" (&optional invisible-not-ok))
  40. (declare-function org-at-item-p "org" ())
  41. (declare-function org-before-first-heading-p "org" ())
  42. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  43. (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
  44. (declare-function org-entry-end-position "org" ())
  45. (declare-function org-try-cdlatex-tab "org" ())
  46. (declare-function org-cycle-level "org" ())
  47. (declare-function org-table-next-field "org-table" ())
  48. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  49. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  50. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  51. (declare-function org-list-get-all-items "org-list" (item struct prevs))
  52. (declare-function org-list-get-bottom-point "org-list" (struct))
  53. (declare-function org-list-prevs-alist "org-list" (struct))
  54. (declare-function org-list-set-item-visibility "org-list" (item struct view))
  55. (declare-function org-list-search-forward "org-list" (regexp &optional bound noerror))
  56. (declare-function org-list-has-child-p "org-list" (item struct))
  57. (declare-function org-list-get-item-end-before-blank "org-list" (item struct))
  58. (declare-function org-list-struct "org-list" ())
  59. (declare-function org-cycle-item-indentation "org-list" ())
  60. (declare-function outline-previous-heading "outline" ())
  61. (declare-function outline-next-heading "outline" ())
  62. (declare-function outline-end-of-heading "outline" ())
  63. (declare-function outline-up-heading "outline" (arg &optional invisible-ok))
  64. (defvar org-drawer-regexp)
  65. (defvar org-odd-levels-only)
  66. (defvar org-startup-folded)
  67. (defvar org-archive-tag)
  68. (defvar org-cycle-include-plain-lists)
  69. (defvar org-outline-regexp-bol)
  70. (defvar-local org-cycle-global-status nil)
  71. (put 'org-cycle-global-status 'org-state t)
  72. (defvar-local org-cycle-subtree-status nil)
  73. (put 'org-cycle-subtree-status 'org-state t)
  74. ;;;; Customisation:
  75. (defgroup org-cycle nil
  76. "Options concerning visibility cycling in Org mode."
  77. :tag "Org Cycle"
  78. :group 'org-structure)
  79. (defcustom org-cycle-skip-children-state-if-no-children t
  80. "Non-nil means skip CHILDREN state in entries that don't have any."
  81. :group 'org-cycle
  82. :type 'boolean)
  83. (defcustom org-cycle-max-level nil
  84. "Maximum level which should still be subject to visibility cycling.
  85. Levels higher than this will, for cycling, be treated as text, not a headline.
  86. When `org-odd-levels-only' is set, a value of N in this variable actually
  87. means 2N-1 stars as the limiting headline.
  88. When nil, cycle all levels.
  89. Note that the limiting level of cycling is also influenced by
  90. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  91. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  92. than its value."
  93. :group 'org-cycle
  94. :type '(choice
  95. (const :tag "No limit" nil)
  96. (integer :tag "Maximum level")))
  97. (defcustom org-cycle-hide-block-startup nil
  98. "Non-nil means entering Org mode will fold all blocks.
  99. This can also be set in on a per-file basis with
  100. #+STARTUP: hideblocks
  101. #+STARTUP: showblocks"
  102. :group 'org-startup
  103. :group 'org-cycle
  104. :type 'boolean)
  105. (defcustom org-cycle-global-at-bob nil
  106. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  107. This makes it possible to do global cycling without having to use `S-TAB'
  108. or `\\[universal-argument] TAB'. For this special case to work, the first \
  109. line of the buffer
  110. must not be a headline -- it may be empty or some other text.
  111. When used in this way, `org-cycle-hook' is disabled temporarily to make
  112. sure the cursor stays at the beginning of the buffer.
  113. When this option is nil, don't do anything special at the beginning of
  114. the buffer."
  115. :group 'org-cycle
  116. :type 'boolean)
  117. (defcustom org-cycle-level-after-item/entry-creation t
  118. "Non-nil means cycle entry level or item indentation in new empty entries.
  119. When the cursor is at the end of an empty headline, i.e., with only stars
  120. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  121. and then all possible ancestor states, before returning to the original state.
  122. This makes data entry extremely fast: M-RET to create a new headline,
  123. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  124. When the cursor is at the end of an empty plain list item, one TAB will
  125. make it a subitem, two or more tabs will back up to make this an item
  126. higher up in the item hierarchy."
  127. :group 'org-cycle
  128. :type 'boolean)
  129. (defcustom org-cycle-emulate-tab t
  130. "Where should `org-cycle' emulate TAB.
  131. nil Never
  132. white Only in completely white lines
  133. whitestart Only at the beginning of lines, before the first non-white char
  134. t Everywhere except in headlines
  135. exc-hl-bol Everywhere except at the start of a headline
  136. If TAB is used in a place where it does not emulate TAB, the current subtree
  137. visibility is cycled."
  138. :group 'org-cycle
  139. :type '(choice (const :tag "Never" nil)
  140. (const :tag "Only in completely white lines" white)
  141. (const :tag "Before first char in a line" whitestart)
  142. (const :tag "Everywhere except in headlines" t)
  143. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  144. (defcustom org-cycle-separator-lines 2
  145. "Number of empty lines needed to keep an empty line between collapsed trees.
  146. If you leave an empty line between the end of a subtree and the following
  147. headline, this empty line is hidden when the subtree is folded.
  148. Org mode will leave (exactly) one empty line visible if the number of
  149. empty lines is equal or larger to the number given in this variable.
  150. So the default 2 means at least 2 empty lines after the end of a subtree
  151. are needed to produce free space between a collapsed subtree and the
  152. following headline.
  153. If the number is negative, and the number of empty lines is at least -N,
  154. all empty lines are shown.
  155. Special case: when 0, never leave empty lines in collapsed view."
  156. :group 'org-cycle
  157. :type 'integer)
  158. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  159. (defcustom org-cycle-pre-hook nil
  160. "Hook that is run before visibility cycling is happening.
  161. The function(s) in this hook must accept a single argument which indicates
  162. the new state that will be set right after running this hook. The
  163. argument is a symbol. Before a global state change, it can have the values
  164. `overview', `content', or `all'. Before a local state change, it can have
  165. the values `folded', `children', or `subtree'."
  166. :group 'org-cycle
  167. :type 'hook)
  168. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  169. org-cycle-show-empty-lines
  170. org-cycle-optimize-window-after-visibility-change)
  171. "Hook that is run after `org-cycle' has changed the buffer visibility.
  172. The function(s) in this hook must accept a single argument which indicates
  173. the new state that was set by the most recent `org-cycle' command. The
  174. argument is a symbol. After a global state change, it can have the values
  175. `overview', `contents', or `all'. After a local state change, it can have
  176. the values `folded', `children', or `subtree'."
  177. :group 'org-cycle
  178. :package-version '(Org . "9.4")
  179. :type 'hook)
  180. (defcustom org-cycle-open-archived-trees nil
  181. "Non-nil means `org-cycle' will open archived trees.
  182. An archived tree is a tree marked with the tag ARCHIVE.
  183. When nil, archived trees will stay folded. You can still open them with
  184. normal outline commands like `show-all', but not with the cycling commands."
  185. :group 'org-archive
  186. :group 'org-cycle
  187. :type 'boolean)
  188. (defvar org-cycle-tab-first-hook nil
  189. "Hook for functions to attach themselves to TAB.
  190. See `org-ctrl-c-ctrl-c-hook' for more information.
  191. This hook runs as the first action when TAB is pressed, even before
  192. `org-cycle' messes around with the `outline-regexp' to cater for
  193. inline tasks and plain list item folding.
  194. If any function in this hook returns t, any other actions that
  195. would have been caused by TAB (such as table field motion or visibility
  196. cycling) will not occur.")
  197. ;;;; Implementation:
  198. (defun org-cycle-hide-drawers (state)
  199. "Re-hide all drawers after a visibility state change.
  200. STATE should be one of the symbols listed in the docstring of
  201. `org-cycle-hook'."
  202. (when (derived-mode-p 'org-mode)
  203. (cond ((not (memq state '(overview folded contents)))
  204. (let* ((global? (eq state 'all))
  205. (beg (if global? (point-min) (line-beginning-position)))
  206. (end (cond (global? (point-max))
  207. ((eq state 'children) (org-entry-end-position))
  208. (t (save-excursion (org-end-of-subtree t t))))))
  209. (if (not global?)
  210. (org-fold--hide-drawers beg end)
  211. ;; Delay folding drawers inside folded subtrees until
  212. ;; first unfold.
  213. (add-hook 'org-fold-core-first-unfold-functions
  214. #'org-fold--hide-drawers))))
  215. ((memq state '(overview contents))
  216. ;; Hide drawers before first heading.
  217. (let ((beg (point-min))
  218. (end (save-excursion
  219. (goto-char (point-min))
  220. (if (org-before-first-heading-p)
  221. (org-entry-end-position)
  222. (point-min)))))
  223. (when (< beg end)
  224. (org-fold--hide-drawers beg end)))))))
  225. ;;;###autoload
  226. (defun org-cycle (&optional arg)
  227. "TAB-action and visibility cycling for Org mode.
  228. This is the command invoked in Org mode by the `TAB' key. Its main
  229. purpose is outline visibility cycling, but it also invokes other actions
  230. in special contexts.
  231. When this function is called with a `\\[universal-argument]' prefix, rotate \
  232. the entire
  233. buffer through 3 states (global cycling)
  234. 1. OVERVIEW: Show only top-level headlines.
  235. 2. CONTENTS: Show all headlines of all levels, but no body text.
  236. 3. SHOW ALL: Show everything.
  237. With a `\\[universal-argument] \\[universal-argument]' prefix argument, \
  238. switch to the startup visibility,
  239. determined by the variable `org-startup-folded', and by any VISIBILITY
  240. properties in the buffer.
  241. With a `\\[universal-argument] \\[universal-argument] \
  242. \\[universal-argument]' prefix argument, show the entire buffer, including
  243. any drawers.
  244. When inside a table, re-align the table and move to the next field.
  245. When point is at the beginning of a headline, rotate the subtree started
  246. by this line through 3 different states (local cycling)
  247. 1. FOLDED: Only the main headline is shown.
  248. 2. CHILDREN: The main headline and the direct children are shown.
  249. From this state, you can move to one of the children
  250. and zoom in further.
  251. 3. SUBTREE: Show the entire subtree, including body text.
  252. If there is no subtree, switch directly from CHILDREN to FOLDED.
  253. When point is at the beginning of an empty headline and the variable
  254. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  255. of the headline by demoting and promoting it to likely levels. This
  256. speeds up creation document structure by pressing `TAB' once or several
  257. times right after creating a new headline.
  258. When there is a numeric prefix, go up to a heading with level ARG, do
  259. a `show-subtree' and return to the previous cursor position. If ARG
  260. is negative, go up that many levels.
  261. When point is not at the beginning of a headline, execute the global
  262. binding for `TAB', which is re-indenting the line. See the option
  263. `org-cycle-emulate-tab' for details.
  264. As a special case, if point is at the very beginning of the buffer, if
  265. there is no headline there, and if the variable `org-cycle-global-at-bob'
  266. is non-nil, this function acts as if called with prefix argument \
  267. \(`\\[universal-argument] TAB',
  268. same as `S-TAB') also when called without prefix argument."
  269. (interactive "P")
  270. (org-load-modules-maybe)
  271. (unless (or (run-hook-with-args-until-success 'org-cycle-tab-first-hook)
  272. (and org-cycle-level-after-item/entry-creation
  273. (or (org-cycle-level)
  274. (org-cycle-item-indentation))))
  275. (let* ((limit-level
  276. (or org-cycle-max-level
  277. (and (boundp 'org-inlinetask-min-level)
  278. org-inlinetask-min-level
  279. (1- org-inlinetask-min-level))))
  280. (nstars
  281. (and limit-level
  282. (if org-odd-levels-only
  283. (1- (* 2 limit-level))
  284. limit-level)))
  285. (org-outline-regexp
  286. (format "\\*%s " (if nstars (format "\\{1,%d\\}" nstars) "+"))))
  287. (cond
  288. ((equal arg '(16))
  289. (setq last-command 'dummy)
  290. (org-cycle-set-startup-visibility)
  291. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  292. ((equal arg '(64))
  293. (org-fold-show-all)
  294. (org-unlogged-message "Entire buffer visible, including drawers"))
  295. ((equal arg '(4)) (org-cycle-internal-global))
  296. ;; Show-subtree, ARG levels up from here.
  297. ((integerp arg)
  298. (save-excursion
  299. (org-back-to-heading)
  300. (outline-up-heading (if (< arg 0) (- arg)
  301. (- (funcall outline-level) arg)))
  302. (org-fold-show-subtree)))
  303. ;; Global cycling at BOB: delegate to `org-cycle-internal-global'.
  304. ((and org-cycle-global-at-bob
  305. (bobp)
  306. (not (looking-at org-outline-regexp)))
  307. (let ((org-cycle-hook
  308. (remq 'org-cycle-optimize-window-after-visibility-change
  309. org-cycle-hook)))
  310. (org-cycle-internal-global)))
  311. ;; Try CDLaTeX TAB completion.
  312. ((org-try-cdlatex-tab))
  313. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  314. ((and (featurep 'org-inlinetask)
  315. (org-inlinetask-at-task-p)
  316. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  317. (org-inlinetask-toggle-visibility))
  318. (t
  319. (let ((pos (point))
  320. (element (org-element-at-point)))
  321. (cond
  322. ;; Try toggling visibility for block at point.
  323. ((org-fold-hide-block-toggle nil t element))
  324. ;; Try toggling visibility for drawer at point.
  325. ((org-fold-hide-drawer-toggle nil t element))
  326. ;; Table: enter it or move to the next field.
  327. ((and (org-match-line "[ \t]*[|+]")
  328. (org-element-lineage element '(table) t))
  329. (if (and (eq 'table (org-element-type element))
  330. (eq 'table.el (org-element-property :type element)))
  331. (message (substitute-command-keys "\\<org-mode-map>\
  332. Use `\\[org-edit-special]' to edit table.el tables"))
  333. (org-table-justify-field-maybe)
  334. (call-interactively #'org-table-next-field)))
  335. ((run-hook-with-args-until-success
  336. 'org-tab-after-check-for-table-hook))
  337. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  338. ((and (or (and org-cycle-include-plain-lists
  339. (let ((item (org-element-lineage element
  340. '(item plain-list)
  341. t)))
  342. (and item
  343. (= (line-beginning-position)
  344. (org-element-property :post-affiliated
  345. item)))))
  346. (org-match-line org-outline-regexp))
  347. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  348. (org-cycle-internal-local))
  349. ;; From there: TAB emulation and template completion.
  350. (buffer-read-only (org-back-to-heading))
  351. ((run-hook-with-args-until-success
  352. 'org-tab-after-check-for-cycling-hook))
  353. ((run-hook-with-args-until-success
  354. 'org-tab-before-tab-emulation-hook))
  355. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  356. (or (not (bolp))
  357. (not (looking-at org-outline-regexp))))
  358. (call-interactively (global-key-binding (kbd "TAB"))))
  359. ((or (eq org-cycle-emulate-tab t)
  360. (and (memq org-cycle-emulate-tab '(white whitestart))
  361. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  362. (or (and (eq org-cycle-emulate-tab 'white)
  363. (= (match-end 0) (point-at-eol)))
  364. (and (eq org-cycle-emulate-tab 'whitestart)
  365. (>= (match-end 0) pos)))))
  366. (call-interactively (global-key-binding (kbd "TAB"))))
  367. (t
  368. (save-excursion
  369. (org-back-to-heading)
  370. (org-cycle))))))))))
  371. (defun org-cycle-force-archived ()
  372. "Cycle subtree even if it is archived."
  373. (interactive)
  374. (setq this-command 'org-cycle)
  375. (let ((org-cycle-open-archived-trees t))
  376. (call-interactively 'org-cycle)))
  377. (defun org-cycle-internal-global ()
  378. "Do the global cycling action."
  379. ;; Hack to avoid display of messages for .org attachments in Gnus
  380. (let ((ga (string-match-p "\\*fontification" (buffer-name))))
  381. (cond
  382. ((and (eq last-command this-command)
  383. (eq org-cycle-global-status 'overview))
  384. ;; We just created the overview - now do table of contents
  385. ;; This can be slow in very large buffers, so indicate action
  386. (run-hook-with-args 'org-cycle-pre-hook 'contents)
  387. (unless ga (org-unlogged-message "CONTENTS..."))
  388. (org-cycle-content)
  389. (unless ga (org-unlogged-message "CONTENTS...done"))
  390. (setq org-cycle-global-status 'contents)
  391. (run-hook-with-args 'org-cycle-hook 'contents))
  392. ((and (eq last-command this-command)
  393. (eq org-cycle-global-status 'contents))
  394. ;; We just showed the table of contents - now show everything
  395. (run-hook-with-args 'org-cycle-pre-hook 'all)
  396. (org-fold-show-all '(headings blocks))
  397. (unless ga (org-unlogged-message "SHOW ALL"))
  398. (setq org-cycle-global-status 'all)
  399. (run-hook-with-args 'org-cycle-hook 'all))
  400. (t
  401. ;; Default action: go to overview
  402. (run-hook-with-args 'org-cycle-pre-hook 'overview)
  403. (org-cycle-overview)
  404. (unless ga (org-unlogged-message "OVERVIEW"))
  405. (setq org-cycle-global-status 'overview)
  406. (run-hook-with-args 'org-cycle-hook 'overview)))))
  407. (defun org-cycle-internal-local ()
  408. "Do the local cycling action."
  409. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  410. ;; First, determine end of headline (EOH), end of subtree or item
  411. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  412. (save-excursion
  413. (if (org-at-item-p)
  414. (progn
  415. (beginning-of-line)
  416. (setq struct (org-list-struct))
  417. (setq eoh (point-at-eol))
  418. (setq eos (org-list-get-item-end-before-blank (point) struct))
  419. (setq has-children (org-list-has-child-p (point) struct)))
  420. (org-back-to-heading)
  421. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  422. (setq eos (save-excursion
  423. (org-end-of-subtree t t)
  424. (unless (eobp) (forward-char -1))
  425. (point)))
  426. (setq has-children
  427. (or
  428. (save-excursion
  429. (let ((level (funcall outline-level)))
  430. (outline-next-heading)
  431. (and (org-at-heading-p)
  432. (> (funcall outline-level) level))))
  433. (and (eq org-cycle-include-plain-lists 'integrate)
  434. (save-excursion
  435. (org-list-search-forward (org-item-beginning-re) eos t))))))
  436. ;; Determine end invisible part of buffer (EOL)
  437. (beginning-of-line 2)
  438. (if (eq org-fold-core-style 'text-properties)
  439. (while (and (not (eobp)) ;this is like `next-line'
  440. (org-fold-folded-p (1- (point))))
  441. (goto-char (org-fold-next-visibility-change nil nil t))
  442. (and (eolp) (beginning-of-line 2)))
  443. (while (and (not (eobp)) ;this is like `next-line'
  444. (get-char-property (1- (point)) 'invisible))
  445. (goto-char (next-single-char-property-change (point) 'invisible))
  446. (and (eolp) (beginning-of-line 2))))
  447. (setq eol (point)))
  448. ;; Find out what to do next and set `this-command'
  449. (cond
  450. ((= eos eoh)
  451. ;; Nothing is hidden behind this heading
  452. (unless (org-before-first-heading-p)
  453. (run-hook-with-args 'org-cycle-pre-hook 'empty))
  454. (org-unlogged-message "EMPTY ENTRY")
  455. (setq org-cycle-subtree-status nil)
  456. (save-excursion
  457. (goto-char eos)
  458. (org-with-limited-levels
  459. (outline-next-heading))
  460. (when (org-invisible-p) (org-fold-heading nil))))
  461. ((and (or (>= eol eos)
  462. (save-excursion (goto-char eol) (skip-chars-forward "[:space:]" eos) (= (point) eos)))
  463. (or has-children
  464. (not (setq children-skipped
  465. org-cycle-skip-children-state-if-no-children))))
  466. ;; Entire subtree is hidden in one line: children view
  467. (unless (org-before-first-heading-p)
  468. (org-with-limited-levels
  469. (run-hook-with-args 'org-cycle-pre-hook 'children)))
  470. (if (org-at-item-p)
  471. (org-list-set-item-visibility (point-at-bol) struct 'children)
  472. (org-fold-show-entry)
  473. (org-with-limited-levels (org-fold-show-children))
  474. (org-fold-show-set-visibility 'tree)
  475. ;; Fold every list in subtree to top-level items.
  476. (when (eq org-cycle-include-plain-lists 'integrate)
  477. (save-excursion
  478. (org-back-to-heading)
  479. (while (org-list-search-forward (org-item-beginning-re) eos t)
  480. (beginning-of-line 1)
  481. (let* ((struct (org-list-struct))
  482. (prevs (org-list-prevs-alist struct))
  483. (end (org-list-get-bottom-point struct)))
  484. (dolist (e (org-list-get-all-items (point) struct prevs))
  485. (org-list-set-item-visibility e struct 'folded))
  486. (goto-char (if (< end eos) end eos)))))))
  487. (org-unlogged-message "CHILDREN")
  488. (save-excursion
  489. (goto-char eos)
  490. (org-with-limited-levels
  491. (outline-next-heading))
  492. (when (and
  493. ;; Subtree does not end at the end of visible section of the
  494. ;; buffer.
  495. (< (point) (point-max))
  496. (org-invisible-p))
  497. ;; Reveal the following heading line.
  498. (org-fold-heading nil)))
  499. (setq org-cycle-subtree-status 'children)
  500. (unless (org-before-first-heading-p)
  501. (run-hook-with-args 'org-cycle-hook 'children)))
  502. ((or children-skipped
  503. (and (eq last-command this-command)
  504. (eq org-cycle-subtree-status 'children)))
  505. ;; We just showed the children, or no children are there,
  506. ;; now show everything.
  507. (unless (org-before-first-heading-p)
  508. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  509. (org-fold-region eoh eos nil 'outline)
  510. (org-unlogged-message
  511. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  512. (setq org-cycle-subtree-status 'subtree)
  513. (unless (org-before-first-heading-p)
  514. (run-hook-with-args 'org-cycle-hook 'subtree)))
  515. (t
  516. ;; Default action: hide the subtree.
  517. (run-hook-with-args 'org-cycle-pre-hook 'folded)
  518. (org-fold-region eoh eos t 'outline)
  519. (org-unlogged-message "FOLDED")
  520. (setq org-cycle-subtree-status 'folded)
  521. (unless (org-before-first-heading-p)
  522. (run-hook-with-args 'org-cycle-hook 'folded))))))
  523. ;;;###autoload
  524. (defun org-cycle-global (&optional arg)
  525. "Cycle the global visibility. For details see `org-cycle'.
  526. With `\\[universal-argument]' prefix ARG, switch to startup visibility.
  527. With a numeric prefix, show all headlines up to that level."
  528. (interactive "P")
  529. (cond
  530. ((integerp arg)
  531. (org-cycle-content arg)
  532. (setq org-cycle-global-status 'contents))
  533. ((equal arg '(4))
  534. (org-cycle-set-startup-visibility)
  535. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  536. (t
  537. (org-cycle '(4)))))
  538. (defun org-cycle-set-startup-visibility ()
  539. "Set the visibility required by startup options and properties."
  540. (cond
  541. ((eq org-startup-folded t)
  542. (org-cycle-overview))
  543. ((eq org-startup-folded 'content)
  544. (org-cycle-content))
  545. ((eq org-startup-folded 'show2levels)
  546. (org-cycle-content 2))
  547. ((eq org-startup-folded 'show3levels)
  548. (org-cycle-content 3))
  549. ((eq org-startup-folded 'show4levels)
  550. (org-cycle-content 4))
  551. ((eq org-startup-folded 'show5levels)
  552. (org-cycle-content 5))
  553. ((or (eq org-startup-folded 'showeverything)
  554. (eq org-startup-folded nil))
  555. (org-fold-show-all)))
  556. (unless (eq org-startup-folded 'showeverything)
  557. (when org-cycle-hide-block-startup (org-fold-hide-block-all))
  558. (org-cycle-set-visibility-according-to-property)
  559. (org-cycle-hide-archived-subtrees 'all)
  560. (org-cycle-hide-drawers 'all)
  561. (org-cycle-show-empty-lines t)))
  562. (defun org-cycle-set-visibility-according-to-property ()
  563. "Switch subtree visibility according to VISIBILITY property."
  564. (interactive)
  565. (let ((regexp (org-re-property "VISIBILITY")))
  566. (org-with-point-at 1
  567. (while (re-search-forward regexp nil t)
  568. (let ((state (match-string 3)))
  569. (if (not (org-at-property-p)) (outline-next-heading)
  570. (save-excursion
  571. (org-back-to-heading t)
  572. (org-fold-subtree t)
  573. (org-fold-reveal)
  574. (pcase state
  575. ("folded"
  576. (org-fold-subtree t))
  577. ("children"
  578. (org-fold-show-hidden-entry)
  579. (org-fold-show-children))
  580. ("content"
  581. (save-excursion
  582. (save-restriction
  583. (org-narrow-to-subtree)
  584. (org-cycle-content))))
  585. ((or "all" "showall")
  586. (org-fold-show-subtree))
  587. (_ nil)))
  588. (org-end-of-subtree)))))))
  589. (defun org-cycle-overview--overlays ()
  590. "Switch to overview mode, showing only top-level headlines."
  591. (interactive)
  592. (org-fold-show-all '(headings drawers))
  593. (save-excursion
  594. (goto-char (point-min))
  595. (when (re-search-forward org-outline-regexp-bol nil t)
  596. (let* ((last (line-end-position))
  597. (level (- (match-end 0) (match-beginning 0) 1))
  598. (regexp (format "^\\*\\{1,%d\\} " level)))
  599. (while (re-search-forward regexp nil :move)
  600. (org-fold-region last (line-end-position 0) t 'outline)
  601. (setq last (line-end-position))
  602. (setq level (- (match-end 0) (match-beginning 0) 1))
  603. (setq regexp (format "^\\*\\{1,%d\\} " level)))
  604. (org-fold-region last (point) t 'outline)))))
  605. (defun org-cycle-overview--text-properties ()
  606. "Switch to overview mode, showing only top-level headlines."
  607. (interactive)
  608. (save-excursion
  609. (goto-char (point-min))
  610. ;; Hide top-level drawer.
  611. (save-restriction
  612. (narrow-to-region (point-min) (or (re-search-forward org-outline-regexp-bol nil t) (point-max)))
  613. (org-fold-hide-drawer-all))
  614. (goto-char (point-min))
  615. (when (re-search-forward org-outline-regexp-bol nil t)
  616. (let* ((last (line-end-position))
  617. (level (- (match-end 0) (match-beginning 0) 1))
  618. (regexp (format "^\\*\\{1,%d\\} " level)))
  619. (while (re-search-forward regexp nil :move)
  620. (org-fold-region last (line-end-position 0) t 'outline)
  621. (setq last (line-end-position))
  622. (setq level (- (match-end 0) (match-beginning 0) 1))
  623. (setq regexp (format "^\\*\\{1,%d\\} " level)))
  624. (org-fold-region last (point) t 'outline)))))
  625. (defun org-cycle-overview ()
  626. "Switch to overview mode, showing only top-level headlines."
  627. (interactive)
  628. (if (eq org-fold-core-style 'text-properties)
  629. (org-cycle-overview--text-properties)
  630. (org-cycle-overview--overlays)))
  631. (defun org-cycle-content--text-properties (&optional arg)
  632. "Show all headlines in the buffer, like a table of contents.
  633. With numerical argument N, show content up to level N."
  634. (interactive "p")
  635. (org-fold-show-all '(headings))
  636. (save-excursion
  637. (goto-char (point-min))
  638. ;; Hide top-level drawer.
  639. (save-restriction
  640. (narrow-to-region (point-min) (or (re-search-forward org-outline-regexp-bol nil t) (point-max)))
  641. (org-fold-hide-drawer-all))
  642. (goto-char (point-max))
  643. (let ((regexp (if (and (wholenump arg) (> arg 0))
  644. (format "^\\*\\{1,%d\\} " arg)
  645. "^\\*+ "))
  646. (last (point)))
  647. (while (re-search-backward regexp nil t)
  648. (org-fold-region (line-end-position) last t 'outline)
  649. (setq last (line-end-position 0))))))
  650. (defun org-cycle-content--overlays (&optional arg)
  651. "Show all headlines in the buffer, like a table of contents.
  652. With numerical argument N, show content up to level N."
  653. (interactive "p")
  654. (org-fold-show-all '(headings drawers))
  655. (save-excursion
  656. (goto-char (point-max))
  657. (let ((regexp (if (and (wholenump arg) (> arg 0))
  658. (format "^\\*\\{1,%d\\} " arg)
  659. "^\\*+ "))
  660. (last (point)))
  661. (while (re-search-backward regexp nil t)
  662. (org-fold-region (line-end-position) last t 'outline)
  663. (setq last (line-end-position 0))))))
  664. (defun org-cycle-content (&optional arg)
  665. "Show all headlines in the buffer, like a table of contents.
  666. With numerical argument N, show content up to level N."
  667. (interactive "p")
  668. (if (eq org-fold-core-style 'text-properties)
  669. (org-cycle-content--text-properties arg)
  670. (org-cycle-content--overlays arg)))
  671. (defvar org-cycle-scroll-position-to-restore nil
  672. "Temporarily store scroll position to restore.")
  673. (defun org-cycle-optimize-window-after-visibility-change (state)
  674. "Adjust the window after a change in outline visibility.
  675. This function is the default value of the hook `org-cycle-hook'."
  676. (when (get-buffer-window (current-buffer))
  677. (let ((repeat (eq last-command this-command)))
  678. (unless repeat
  679. (setq org-cycle-scroll-position-to-restore nil))
  680. (cond
  681. ((eq state 'content) nil)
  682. ((eq state 'all) nil)
  683. ((and org-cycle-scroll-position-to-restore repeat
  684. (eq state 'folded))
  685. (set-window-start nil org-cycle-scroll-position-to-restore))
  686. ((eq state 'folded) nil)
  687. ((eq state 'children)
  688. (setq org-cycle-scroll-position-to-restore (window-start))
  689. (or (org-subtree-end-visible-p) (recenter 1)))
  690. ((eq state 'subtree)
  691. (unless repeat
  692. (setq org-cycle-scroll-position-to-restore (window-start)))
  693. (or (org-subtree-end-visible-p) (recenter 1)))))))
  694. (defun org-cycle-show-empty-lines (state)
  695. "Show empty lines above all visible headlines.
  696. The region to be covered depends on STATE when called through
  697. `org-cycle-hook'. Lisp program can use t for STATE to get the
  698. entire buffer covered. Note that an empty line is only shown if there
  699. are at least `org-cycle-separator-lines' empty lines before the headline."
  700. (when (/= org-cycle-separator-lines 0)
  701. (save-excursion
  702. (let* ((n (abs org-cycle-separator-lines))
  703. (re (cond
  704. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  705. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  706. (t (let ((ns (number-to-string (- n 2))))
  707. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  708. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  709. beg end)
  710. (cond
  711. ((memq state '(overview contents t))
  712. (setq beg (point-min) end (point-max)))
  713. ((memq state '(children folded))
  714. (setq beg (point)
  715. end (progn (org-end-of-subtree t t)
  716. (line-beginning-position 2)))))
  717. (when beg
  718. (goto-char beg)
  719. (while (re-search-forward re end t)
  720. (unless (org-invisible-p (match-end 1))
  721. (let ((e (match-end 1))
  722. (b (if (>= org-cycle-separator-lines 0)
  723. (match-beginning 1)
  724. (save-excursion
  725. (goto-char (match-beginning 0))
  726. (skip-chars-backward " \t\n")
  727. (line-end-position)))))
  728. (org-fold-region b e nil 'outline))))))))
  729. ;; Never hide empty lines at the end of the file.
  730. (save-excursion
  731. (goto-char (point-max))
  732. (outline-previous-heading)
  733. (outline-end-of-heading)
  734. (when (and (looking-at "[ \t\n]+")
  735. (= (match-end 0) (point-max)))
  736. (org-fold-region (point) (match-end 0) nil 'outline))))
  737. (defun org-cycle-hide-archived-subtrees (state)
  738. "Re-hide all archived subtrees after a visibility state change.
  739. STATE should be one of the symbols listed in the docstring of
  740. `org-cycle-hook'."
  741. (when (and (not org-cycle-open-archived-trees)
  742. (not (memq state '(overview folded))))
  743. (let ((globalp (memq state '(contents all))))
  744. (if globalp
  745. ;; Delay hiding inside folded subtrees until first unfold.
  746. (add-hook 'org-fold-core-first-unfold-functions
  747. #'org-fold-hide-archived-subtrees)
  748. (org-fold-hide-archived-subtrees
  749. (point)
  750. (save-excursion
  751. (org-end-of-subtree t))))
  752. (when (and (not globalp)
  753. (member org-archive-tag
  754. (org-get-tags nil 'local)))
  755. (message "%s" (substitute-command-keys
  756. "Subtree is archived and stays closed. Use \
  757. `\\[org-cycle-force-archived]' to cycle it anyway."))))))
  758. (provide 'org-cycle)
  759. ;;; org-cycle.el ends here