org-cycle.el 31 KB

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