org-cycle.el 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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: nohideblocks"
  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. (org-fold--hide-drawers beg end)))
  210. ((memq state '(overview contents))
  211. ;; Hide drawers before first heading.
  212. (let ((beg (point-min))
  213. (end (save-excursion
  214. (goto-char (point-min))
  215. (if (org-before-first-heading-p)
  216. (org-entry-end-position)
  217. (point-min)))))
  218. (when (< beg end)
  219. (org-fold--hide-drawers beg end)))))))
  220. ;;;###autoload
  221. (defun org-cycle (&optional arg)
  222. "TAB-action and visibility cycling for Org mode.
  223. This is the command invoked in Org mode by the `TAB' key. Its main
  224. purpose is outline visibility cycling, but it also invokes other actions
  225. in special contexts.
  226. When this function is called with a `\\[universal-argument]' prefix, rotate \
  227. the entire
  228. buffer through 3 states (global cycling)
  229. 1. OVERVIEW: Show only top-level headlines.
  230. 2. CONTENTS: Show all headlines of all levels, but no body text.
  231. 3. SHOW ALL: Show everything.
  232. With a `\\[universal-argument] \\[universal-argument]' prefix argument, \
  233. switch to the startup visibility,
  234. determined by the variable `org-startup-folded', and by any VISIBILITY
  235. properties in the buffer.
  236. With a `\\[universal-argument] \\[universal-argument] \
  237. \\[universal-argument]' prefix argument, show the entire buffer, including
  238. any drawers.
  239. When inside a table, re-align the table and move to the next field.
  240. When point is at the beginning of a headline, rotate the subtree started
  241. by this line through 3 different states (local cycling)
  242. 1. FOLDED: Only the main headline is shown.
  243. 2. CHILDREN: The main headline and the direct children are shown.
  244. From this state, you can move to one of the children
  245. and zoom in further.
  246. 3. SUBTREE: Show the entire subtree, including body text.
  247. If there is no subtree, switch directly from CHILDREN to FOLDED.
  248. When point is at the beginning of an empty headline and the variable
  249. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  250. of the headline by demoting and promoting it to likely levels. This
  251. speeds up creation document structure by pressing `TAB' once or several
  252. times right after creating a new headline.
  253. When there is a numeric prefix, go up to a heading with level ARG, do
  254. a `show-subtree' and return to the previous cursor position. If ARG
  255. is negative, go up that many levels.
  256. When point is not at the beginning of a headline, execute the global
  257. binding for `TAB', which is re-indenting the line. See the option
  258. `org-cycle-emulate-tab' for details.
  259. As a special case, if point is at the very beginning of the buffer, if
  260. there is no headline there, and if the variable `org-cycle-global-at-bob'
  261. is non-nil, this function acts as if called with prefix argument \
  262. \(`\\[universal-argument] TAB',
  263. same as `S-TAB') also when called without prefix argument."
  264. (interactive "P")
  265. (org-load-modules-maybe)
  266. (unless (or (run-hook-with-args-until-success 'org-cycle-tab-first-hook)
  267. (and org-cycle-level-after-item/entry-creation
  268. (or (org-cycle-level)
  269. (org-cycle-item-indentation))))
  270. (let* ((limit-level
  271. (or org-cycle-max-level
  272. (and (boundp 'org-inlinetask-min-level)
  273. org-inlinetask-min-level
  274. (1- org-inlinetask-min-level))))
  275. (nstars
  276. (and limit-level
  277. (if org-odd-levels-only
  278. (1- (* 2 limit-level))
  279. limit-level)))
  280. (org-outline-regexp
  281. (format "\\*%s " (if nstars (format "\\{1,%d\\}" nstars) "+"))))
  282. (cond
  283. ((equal arg '(16))
  284. (setq last-command 'dummy)
  285. (org-cycle-set-startup-visibility)
  286. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  287. ((equal arg '(64))
  288. (org-fold-show-all)
  289. (org-unlogged-message "Entire buffer visible, including drawers"))
  290. ((equal arg '(4)) (org-cycle-internal-global))
  291. ;; Show-subtree, ARG levels up from here.
  292. ((integerp arg)
  293. (save-excursion
  294. (org-back-to-heading)
  295. (outline-up-heading (if (< arg 0) (- arg)
  296. (- (funcall outline-level) arg)))
  297. (org-fold-show-subtree)))
  298. ;; Global cycling at BOB: delegate to `org-cycle-internal-global'.
  299. ((and org-cycle-global-at-bob
  300. (bobp)
  301. (not (looking-at org-outline-regexp)))
  302. (let ((org-cycle-hook
  303. (remq 'org-cycle-optimize-window-after-visibility-change
  304. org-cycle-hook)))
  305. (org-cycle-internal-global)))
  306. ;; Try CDLaTeX TAB completion.
  307. ((org-try-cdlatex-tab))
  308. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  309. ((and (featurep 'org-inlinetask)
  310. (org-inlinetask-at-task-p)
  311. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  312. (org-inlinetask-toggle-visibility))
  313. (t
  314. (let ((pos (point))
  315. (element (org-element-at-point)))
  316. (cond
  317. ;; Try toggling visibility for block at point.
  318. ((org-fold-hide-block-toggle nil t element))
  319. ;; Try toggling visibility for drawer at point.
  320. ((org-fold-hide-drawer-toggle nil t element))
  321. ;; Table: enter it or move to the next field.
  322. ((and (org-match-line "[ \t]*[|+]")
  323. (org-element-lineage element '(table) t))
  324. (if (and (eq 'table (org-element-type element))
  325. (eq 'table.el (org-element-property :type element)))
  326. (message (substitute-command-keys "\\<org-mode-map>\
  327. Use `\\[org-edit-special]' to edit table.el tables"))
  328. (org-table-justify-field-maybe)
  329. (call-interactively #'org-table-next-field)))
  330. ((run-hook-with-args-until-success
  331. 'org-tab-after-check-for-table-hook))
  332. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  333. ((and (or (and org-cycle-include-plain-lists
  334. (let ((item (org-element-lineage element
  335. '(item plain-list)
  336. t)))
  337. (and item
  338. (= (line-beginning-position)
  339. (org-element-property :post-affiliated
  340. item)))))
  341. (org-match-line org-outline-regexp))
  342. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  343. (org-cycle-internal-local))
  344. ;; From there: TAB emulation and template completion.
  345. (buffer-read-only (org-back-to-heading))
  346. ((run-hook-with-args-until-success
  347. 'org-tab-after-check-for-cycling-hook))
  348. ((run-hook-with-args-until-success
  349. 'org-tab-before-tab-emulation-hook))
  350. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  351. (or (not (bolp))
  352. (not (looking-at org-outline-regexp))))
  353. (call-interactively (global-key-binding (kbd "TAB"))))
  354. ((or (eq org-cycle-emulate-tab t)
  355. (and (memq org-cycle-emulate-tab '(white whitestart))
  356. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  357. (or (and (eq org-cycle-emulate-tab 'white)
  358. (= (match-end 0) (point-at-eol)))
  359. (and (eq org-cycle-emulate-tab 'whitestart)
  360. (>= (match-end 0) pos)))))
  361. (call-interactively (global-key-binding (kbd "TAB"))))
  362. (t
  363. (save-excursion
  364. (org-back-to-heading)
  365. (org-cycle))))))))))
  366. (defun org-cycle-force-archived ()
  367. "Cycle subtree even if it is archived."
  368. (interactive)
  369. (setq this-command 'org-cycle)
  370. (let ((org-cycle-open-archived-trees t))
  371. (call-interactively 'org-cycle)))
  372. (defun org-cycle-internal-global ()
  373. "Do the global cycling action."
  374. ;; Hack to avoid display of messages for .org attachments in Gnus
  375. (let ((ga (string-match-p "\\*fontification" (buffer-name))))
  376. (cond
  377. ((and (eq last-command this-command)
  378. (eq org-cycle-global-status 'overview))
  379. ;; We just created the overview - now do table of contents
  380. ;; This can be slow in very large buffers, so indicate action
  381. (run-hook-with-args 'org-cycle-pre-hook 'contents)
  382. (unless ga (org-unlogged-message "CONTENTS..."))
  383. (org-cycle-content)
  384. (unless ga (org-unlogged-message "CONTENTS...done"))
  385. (setq org-cycle-global-status 'contents)
  386. (run-hook-with-args 'org-cycle-hook 'contents))
  387. ((and (eq last-command this-command)
  388. (eq org-cycle-global-status 'contents))
  389. ;; We just showed the table of contents - now show everything
  390. (run-hook-with-args 'org-cycle-pre-hook 'all)
  391. (org-fold-show-all '(headings blocks))
  392. (unless ga (org-unlogged-message "SHOW ALL"))
  393. (setq org-cycle-global-status 'all)
  394. (run-hook-with-args 'org-cycle-hook 'all))
  395. (t
  396. ;; Default action: go to overview
  397. (run-hook-with-args 'org-cycle-pre-hook 'overview)
  398. (org-cycle-overview)
  399. (unless ga (org-unlogged-message "OVERVIEW"))
  400. (setq org-cycle-global-status 'overview)
  401. (run-hook-with-args 'org-cycle-hook 'overview)))))
  402. (defun org-cycle-internal-local ()
  403. "Do the local cycling action."
  404. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  405. ;; First, determine end of headline (EOH), end of subtree or item
  406. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  407. (save-excursion
  408. (if (org-at-item-p)
  409. (progn
  410. (beginning-of-line)
  411. (setq struct (org-list-struct))
  412. (setq eoh (point-at-eol))
  413. (setq eos (org-list-get-item-end-before-blank (point) struct))
  414. (setq has-children (org-list-has-child-p (point) struct)))
  415. (org-back-to-heading)
  416. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  417. (setq eos (save-excursion
  418. (org-end-of-subtree t t)
  419. (unless (eobp) (forward-char -1))
  420. (point)))
  421. (setq has-children
  422. (or
  423. (save-excursion
  424. (let ((level (funcall outline-level)))
  425. (outline-next-heading)
  426. (and (org-at-heading-p)
  427. (> (funcall outline-level) level))))
  428. (and (eq org-cycle-include-plain-lists 'integrate)
  429. (save-excursion
  430. (org-list-search-forward (org-item-beginning-re) eos t))))))
  431. ;; Determine end invisible part of buffer (EOL)
  432. (beginning-of-line 2)
  433. (if (eq org-fold-core-style 'text-properties)
  434. (while (and (not (eobp)) ;this is like `next-line'
  435. (org-fold-folded-p (1- (point))))
  436. (goto-char (org-fold-next-visibility-change nil nil t))
  437. (and (eolp) (beginning-of-line 2)))
  438. (while (and (not (eobp)) ;this is like `next-line'
  439. (get-char-property (1- (point)) 'invisible))
  440. (goto-char (next-single-char-property-change (point) 'invisible))
  441. (and (eolp) (beginning-of-line 2))))
  442. (setq eol (point)))
  443. ;; Find out what to do next and set `this-command'
  444. (cond
  445. ((= eos eoh)
  446. ;; Nothing is hidden behind this heading
  447. (unless (org-before-first-heading-p)
  448. (run-hook-with-args 'org-cycle-pre-hook 'empty))
  449. (org-unlogged-message "EMPTY ENTRY")
  450. (setq org-cycle-subtree-status nil)
  451. (save-excursion
  452. (goto-char eos)
  453. (org-with-limited-levels
  454. (outline-next-heading))
  455. (when (org-invisible-p) (org-fold-heading nil))))
  456. ((and (or (>= eol eos)
  457. (save-excursion (goto-char eol) (skip-chars-forward "[:space:]" eos) (= (point) eos)))
  458. (or has-children
  459. (not (setq children-skipped
  460. org-cycle-skip-children-state-if-no-children))))
  461. ;; Entire subtree is hidden in one line: children view
  462. (unless (org-before-first-heading-p)
  463. (org-with-limited-levels
  464. (run-hook-with-args 'org-cycle-pre-hook 'children)))
  465. (if (org-at-item-p)
  466. (org-list-set-item-visibility (point-at-bol) struct 'children)
  467. (org-fold-show-entry)
  468. (org-with-limited-levels (org-fold-show-children))
  469. (org-fold-show-set-visibility 'tree)
  470. ;; Fold every list in subtree to top-level items.
  471. (when (eq org-cycle-include-plain-lists 'integrate)
  472. (save-excursion
  473. (org-back-to-heading)
  474. (while (org-list-search-forward (org-item-beginning-re) eos t)
  475. (beginning-of-line 1)
  476. (let* ((struct (org-list-struct))
  477. (prevs (org-list-prevs-alist struct))
  478. (end (org-list-get-bottom-point struct)))
  479. (dolist (e (org-list-get-all-items (point) struct prevs))
  480. (org-list-set-item-visibility e struct 'folded))
  481. (goto-char (if (< end eos) end eos)))))))
  482. (org-unlogged-message "CHILDREN")
  483. (save-excursion
  484. (goto-char eos)
  485. (org-with-limited-levels
  486. (outline-next-heading))
  487. (when (and
  488. ;; Subtree does not end at the end of visible section of the
  489. ;; buffer.
  490. (< (point) (point-max))
  491. (org-invisible-p))
  492. ;; Reveal the following heading line.
  493. (org-fold-heading nil)))
  494. (setq org-cycle-subtree-status 'children)
  495. (unless (org-before-first-heading-p)
  496. (run-hook-with-args 'org-cycle-hook 'children)))
  497. ((or children-skipped
  498. (and (eq last-command this-command)
  499. (eq org-cycle-subtree-status 'children)))
  500. ;; We just showed the children, or no children are there,
  501. ;; now show everything.
  502. (unless (org-before-first-heading-p)
  503. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  504. (org-fold-region eoh eos nil 'outline)
  505. (org-unlogged-message
  506. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  507. (setq org-cycle-subtree-status 'subtree)
  508. (unless (org-before-first-heading-p)
  509. (run-hook-with-args 'org-cycle-hook 'subtree)))
  510. (t
  511. ;; Default action: hide the subtree.
  512. (run-hook-with-args 'org-cycle-pre-hook 'folded)
  513. (org-fold-region eoh eos t 'outline)
  514. (org-unlogged-message "FOLDED")
  515. (setq org-cycle-subtree-status 'folded)
  516. (unless (org-before-first-heading-p)
  517. (run-hook-with-args 'org-cycle-hook 'folded))))))
  518. ;;;###autoload
  519. (defun org-cycle-global (&optional arg)
  520. "Cycle the global visibility. For details see `org-cycle'.
  521. With `\\[universal-argument]' prefix ARG, switch to startup visibility.
  522. With a numeric prefix, show all headlines up to that level."
  523. (interactive "P")
  524. (cond
  525. ((integerp arg)
  526. (org-cycle-content arg)
  527. (setq org-cycle-global-status 'contents))
  528. ((equal arg '(4))
  529. (org-cycle-set-startup-visibility)
  530. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  531. (t
  532. (org-cycle '(4)))))
  533. (defun org-cycle-set-startup-visibility ()
  534. "Set the visibility required by startup options and properties."
  535. (cond
  536. ((eq org-startup-folded t)
  537. (org-cycle-overview))
  538. ((eq org-startup-folded 'content)
  539. (org-cycle-content))
  540. ((eq org-startup-folded 'show2levels)
  541. (org-cycle-content 2))
  542. ((eq org-startup-folded 'show3levels)
  543. (org-cycle-content 3))
  544. ((eq org-startup-folded 'show4levels)
  545. (org-cycle-content 4))
  546. ((eq org-startup-folded 'show5levels)
  547. (org-cycle-content 5))
  548. ((or (eq org-startup-folded 'showeverything)
  549. (eq org-startup-folded nil))
  550. (org-fold-show-all)))
  551. (unless (eq org-startup-folded 'showeverything)
  552. (when org-cycle-hide-block-startup (org-fold-hide-block-all))
  553. (org-cycle-set-visibility-according-to-property)
  554. (org-cycle-hide-archived-subtrees 'all)
  555. (org-cycle-hide-drawers 'all)
  556. (org-cycle-show-empty-lines t)))
  557. (defun org-cycle-set-visibility-according-to-property ()
  558. "Switch subtree visibility according to VISIBILITY property."
  559. (interactive)
  560. (let ((regexp (org-re-property "VISIBILITY")))
  561. (org-with-point-at 1
  562. (while (re-search-forward regexp nil t)
  563. (let ((state (match-string 3)))
  564. (if (not (org-at-property-p)) (outline-next-heading)
  565. (save-excursion
  566. (org-back-to-heading t)
  567. (org-fold-subtree t)
  568. (org-fold-reveal)
  569. (pcase state
  570. ("folded"
  571. (org-fold-subtree t))
  572. ("children"
  573. (org-fold-show-hidden-entry)
  574. (org-fold-show-children))
  575. ("content"
  576. (save-excursion
  577. (save-restriction
  578. (org-narrow-to-subtree)
  579. (org-cycle-content))))
  580. ((or "all" "showall")
  581. (org-fold-show-subtree))
  582. (_ nil)))
  583. (org-end-of-subtree)))))))
  584. (defun org-cycle-overview--overlays ()
  585. "Switch to overview mode, showing only top-level headlines."
  586. (interactive)
  587. (org-fold-show-all '(headings drawers))
  588. (save-excursion
  589. (goto-char (point-min))
  590. (when (re-search-forward org-outline-regexp-bol nil t)
  591. (let* ((last (line-end-position))
  592. (level (- (match-end 0) (match-beginning 0) 1))
  593. (regexp (format "^\\*\\{1,%d\\} " level)))
  594. (while (re-search-forward regexp nil :move)
  595. (org-fold-region last (line-end-position 0) t 'outline)
  596. (setq last (line-end-position))
  597. (setq level (- (match-end 0) (match-beginning 0) 1))
  598. (setq regexp (format "^\\*\\{1,%d\\} " level)))
  599. (org-fold-region last (point) t 'outline)))))
  600. (defun org-cycle-overview--text-properties ()
  601. "Switch to overview mode, showing only top-level headlines."
  602. (interactive)
  603. (save-excursion
  604. (goto-char (point-min))
  605. ;; Hide top-level drawer.
  606. (save-restriction
  607. (narrow-to-region (point-min) (or (re-search-forward org-outline-regexp-bol nil t) (point-max)))
  608. (org-fold-hide-drawer-all))
  609. (goto-char (point-min))
  610. (when (re-search-forward org-outline-regexp-bol nil t)
  611. (let* ((last (line-end-position))
  612. (level (- (match-end 0) (match-beginning 0) 1))
  613. (regexp (format "^\\*\\{1,%d\\} " level)))
  614. (while (re-search-forward regexp nil :move)
  615. (org-fold-region last (line-end-position 0) t 'outline)
  616. (setq last (line-end-position))
  617. (setq level (- (match-end 0) (match-beginning 0) 1))
  618. (setq regexp (format "^\\*\\{1,%d\\} " level)))
  619. (org-fold-region last (point) t 'outline)))))
  620. (defun org-cycle-overview ()
  621. "Switch to overview mode, showing only top-level headlines."
  622. (interactive)
  623. (if (eq org-fold-core-style 'text-properties)
  624. (org-cycle-overview--text-properties)
  625. (org-cycle-overview--overlays)))
  626. (defun org-cycle-content--text-properties (&optional arg)
  627. "Show all headlines in the buffer, like a table of contents.
  628. With numerical argument N, show content up to level N."
  629. (interactive "p")
  630. (org-fold-show-all '(headings))
  631. (save-excursion
  632. (goto-char (point-min))
  633. ;; Hide top-level drawer.
  634. (save-restriction
  635. (narrow-to-region (point-min) (or (re-search-forward org-outline-regexp-bol nil t) (point-max)))
  636. (org-fold-hide-drawer-all))
  637. (goto-char (point-max))
  638. (let ((regexp (if (and (wholenump arg) (> arg 0))
  639. (format "^\\*\\{1,%d\\} " arg)
  640. "^\\*+ "))
  641. (last (point)))
  642. (while (re-search-backward regexp nil t)
  643. (org-fold-region (line-end-position) last t 'outline)
  644. (setq last (line-end-position 0))))))
  645. (defun org-cycle-content--overlays (&optional arg)
  646. "Show all headlines in the buffer, like a table of contents.
  647. With numerical argument N, show content up to level N."
  648. (interactive "p")
  649. (org-fold-show-all '(headings drawers))
  650. (save-excursion
  651. (goto-char (point-max))
  652. (let ((regexp (if (and (wholenump arg) (> arg 0))
  653. (format "^\\*\\{1,%d\\} " arg)
  654. "^\\*+ "))
  655. (last (point)))
  656. (while (re-search-backward regexp nil t)
  657. (org-fold-region (line-end-position) last t 'outline)
  658. (setq last (line-end-position 0))))))
  659. (defun org-cycle-content (&optional arg)
  660. "Show all headlines in the buffer, like a table of contents.
  661. With numerical argument N, show content up to level N."
  662. (interactive "p")
  663. (if (eq org-fold-core-style 'text-properties)
  664. (org-cycle-content--text-properties arg)
  665. (org-cycle-content--overlays arg)))
  666. (defvar org-cycle-scroll-position-to-restore nil
  667. "Temporarily store scroll position to restore.")
  668. (defun org-cycle-optimize-window-after-visibility-change (state)
  669. "Adjust the window after a change in outline visibility.
  670. This function is the default value of the hook `org-cycle-hook'."
  671. (when (get-buffer-window (current-buffer))
  672. (let ((repeat (eq last-command this-command)))
  673. (unless repeat
  674. (setq org-cycle-scroll-position-to-restore nil))
  675. (cond
  676. ((eq state 'content) nil)
  677. ((eq state 'all) nil)
  678. ((and org-cycle-scroll-position-to-restore repeat
  679. (eq state 'folded))
  680. (set-window-start nil org-cycle-scroll-position-to-restore))
  681. ((eq state 'folded) nil)
  682. ((eq state 'children)
  683. (setq org-cycle-scroll-position-to-restore (window-start))
  684. (or (org-subtree-end-visible-p) (recenter 1)))
  685. ((eq state 'subtree)
  686. (unless repeat
  687. (setq org-cycle-scroll-position-to-restore (window-start)))
  688. (or (org-subtree-end-visible-p) (recenter 1)))))))
  689. (defun org-cycle-show-empty-lines (state)
  690. "Show empty lines above all visible headlines.
  691. The region to be covered depends on STATE when called through
  692. `org-cycle-hook'. Lisp program can use t for STATE to get the
  693. entire buffer covered. Note that an empty line is only shown if there
  694. are at least `org-cycle-separator-lines' empty lines before the headline."
  695. (when (/= org-cycle-separator-lines 0)
  696. (save-excursion
  697. (let* ((n (abs org-cycle-separator-lines))
  698. (re (cond
  699. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  700. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  701. (t (let ((ns (number-to-string (- n 2))))
  702. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  703. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  704. beg end)
  705. (cond
  706. ((memq state '(overview contents t))
  707. (setq beg (point-min) end (point-max)))
  708. ((memq state '(children folded))
  709. (setq beg (point)
  710. end (progn (org-end-of-subtree t t)
  711. (line-beginning-position 2)))))
  712. (when beg
  713. (goto-char beg)
  714. (while (re-search-forward re end t)
  715. (unless (org-invisible-p (match-end 1))
  716. (let ((e (match-end 1))
  717. (b (if (>= org-cycle-separator-lines 0)
  718. (match-beginning 1)
  719. (save-excursion
  720. (goto-char (match-beginning 0))
  721. (skip-chars-backward " \t\n")
  722. (line-end-position)))))
  723. (org-fold-region b e nil 'outline))))))))
  724. ;; Never hide empty lines at the end of the file.
  725. (save-excursion
  726. (goto-char (point-max))
  727. (outline-previous-heading)
  728. (outline-end-of-heading)
  729. (when (and (looking-at "[ \t\n]+")
  730. (= (match-end 0) (point-max)))
  731. (org-fold-region (point) (match-end 0) nil 'outline))))
  732. (defun org-cycle-hide-archived-subtrees (state)
  733. "Re-hide all archived subtrees after a visibility state change.
  734. STATE should be one of the symbols listed in the docstring of
  735. `org-cycle-hook'."
  736. (when (and (not org-cycle-open-archived-trees)
  737. (not (memq state '(overview folded))))
  738. (let ((globalp (memq state '(contents all))))
  739. (if globalp
  740. (org-fold-hide-archived-subtrees (point-min) (point-max))
  741. (org-fold-hide-archived-subtrees
  742. (point)
  743. (save-excursion
  744. (org-end-of-subtree t))))
  745. (when (and (not globalp)
  746. (member org-archive-tag
  747. (org-get-tags nil 'local)))
  748. (message "%s" (substitute-command-keys
  749. "Subtree is archived and stays closed. Use \
  750. `\\[org-cycle-force-archived]' to cycle it anyway."))))))
  751. (provide 'org-cycle)
  752. ;;; org-cycle.el ends here