Browse Source

org.el: Restore the scroll position after cycling

* lisp/org.el (org-scroll-position-to-restore): New variable.
(org-optimize-window-after-visibility-change): Use it to restore
the scroll position when needed.

TINYCHANGE

See <https://orgmode.org/list/CA+Yh0STTE+Zx-zct3qy7eyqTovogr_fqwwOCLx9z3da7_hFRnw@mail.gmail.com>
Dmitrii Korobeinikov 4 years ago
parent
commit
a8e812e601
1 changed files with 16 additions and 6 deletions
  1. 16 6
      lisp/org.el

+ 16 - 6
lisp/org.el

@@ -6588,16 +6588,26 @@ With numerical argument N, show content up to level N."
 	  (outline-show-branches))
 	(when (bobp) (throw 'exit nil))))))
 
+(defvar org-scroll-position-to-restore nil
+  "Temporarily store scroll position to restore.")
 (defun org-optimize-window-after-visibility-change (state)
   "Adjust the window after a change in outline visibility.
 This function is the default value of the hook `org-cycle-hook'."
   (when (get-buffer-window (current-buffer))
-    (cond
-     ((eq state 'content)  nil)
-     ((eq state 'all)      nil)
-     ((eq state 'folded)   nil)
-     ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
-     ((eq state 'subtree)  (or (org-subtree-end-visible-p) (recenter 1))))))
+    (let ((wstart (window-start)))
+      (cond
+       ((eq state 'content)  nil)
+       ((eq state 'all)      nil)
+       ((and (eq state 'folded) (eq last-command this-command))
+	(set-window-start nil org-scroll-position-to-restore))
+       ((eq state 'folded) nil)
+       ((eq state 'children)
+	(setq org-scroll-position-to-restore (window-start))
+	(or (org-subtree-end-visible-p) (recenter 1)))
+       ((eq state 'subtree)
+	(when (not (eq last-command this-command))
+	  (setq org-scroll-position-to-restore (window-start)))
+	(or (org-subtree-end-visible-p) (recenter 1)))))))
 
 (defun org-clean-visibility-after-subtree-move ()
   "Fix visibility issues after moving a subtree."