From af05e125fce67e916c317bc179461375f2eb179b Mon Sep 17 00:00:00 2001 From: Aaron Ecay Date: Mon, 16 Dec 2013 18:38:03 -0500 Subject: [PATCH] fix C-u C-u TAB -> set visibility to initial status MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lisp/org.el (org-set-startup-visibility): refactor to work properly Previously, ‘org-set-startup-visibility’ would use a pre-calculated number of trips through ‘org-cycle’ to get the right visibility. This is fragile, and sometimes wrong. This new implementation instead loops until the visibility is correct (erroring out after 5 tries to avoid an infloop). --- lisp/org.el | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 59f55a8..07a2942 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -6891,12 +6891,35 @@ With a numeric prefix, show all headlines up to that level." (defun org-set-startup-visibility () "Set the visibility required by startup options and properties." - (cond - ((eq org-startup-folded t) - (org-cycle '(4))) - ((eq org-startup-folded 'content) - (let ((this-command 'org-cycle) (last-command 'org-cycle)) - (org-cycle '(4)) (org-cycle '(4))))) + (let ((goal-state 'all) + ;; Hack the following two variables to make + ;; `org-cycle-internal-global' do the right thing; with these + ;; unbound it perseverates in the overview state. + (last-command 'org-cycle) + (this-command 'org-cycle)) + (cond + ((eq org-startup-folded t) + (setq goal-state 'overview)) + ((eq org-startup-folded 'content) + ;; Annoyingly 'content' is spelled with an 's' in one variable, + ;; and without in another. + (setq goal-state 'contents))) + ;; Cycle once unconditionally so that `org-cycle-global-status' is + ;; synced with the state of the buffer (this may turn out to be + ;; overly conservative). + (org-cycle '(4)) + (dotimes (_ 5) + ;; Now try to get into the right state. We try 5 times just to + ;; make sure, even though strictly speaking 3 is the maximum + ;; number of states we could progress thorugh; see the + ;; implementation of `org-cycle-internal-global'. + (unless (eq org-cycle-global-status goal-state) + (org-cycle '(4)))) + ;; Something weird is going on; we cannot get into the right + ;; state. Error out and hope this prompts a bug report. + (unless (eq org-cycle-global-status goal-state) + (error "Could not get the correct visibility state after trying 5 times!"))) + (unless (eq org-startup-folded 'showeverything) (if org-hide-block-startup (org-hide-block-all)) (org-set-visibility-according-to-property 'no-cleanup) -- 1.8.5.1