org-cycle.el 31 KB

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