Skip to content

Commit 7be66d8

Browse files
committed
* lisp/outline.el: Keep point on the same outline after revert.
(outline-hidden-headings-paths): Return the current path as well. (outline-hidden-headings-restore-paths): Move point to the path that was current before reverting the buffer (bug#71466). (outline-revert-buffer-restore-visibility): Handle both values returned from outline-hidden-headings-paths.
1 parent f18a915 commit 7be66d8

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

lisp/outline.el

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,49 +1704,61 @@ LEVEL, decides of subtree visibility according to
17041704
Every hash key is a list whose elements compose a complete path
17051705
of headings descending from the top level down to the bottom level.
17061706
This is useful to save the hidden outlines and restore them later
1707-
after reverting the buffer."
1708-
(let ((paths (make-hash-table :test #'equal))
1709-
current-path)
1707+
after reverting the buffer. Also return the outline where point
1708+
was located before reverting the buffer."
1709+
(let* ((paths (make-hash-table :test #'equal))
1710+
path current-path
1711+
(current-heading-p (outline-on-heading-p))
1712+
(current-beg (when current-heading-p (pos-bol)))
1713+
(current-end (when current-heading-p (pos-eol))))
17101714
(outline-map-region
17111715
(lambda ()
17121716
(let* ((level (funcall outline-level))
17131717
(heading (buffer-substring-no-properties (pos-bol) (pos-eol))))
1714-
(while (and current-path (>= (cdar current-path) level))
1715-
(pop current-path))
1716-
(push (cons heading level) current-path)
1718+
(while (and path (>= (cdar path) level))
1719+
(pop path))
1720+
(push (cons heading level) path)
17171721
(when (save-excursion
17181722
(outline-end-of-heading)
17191723
(seq-some (lambda (o) (eq (overlay-get o 'invisible)
17201724
'outline))
17211725
(overlays-at (point))))
1722-
(setf (gethash (mapcar #'car current-path) paths) t))))
1726+
(setf (gethash (mapcar #'car path) paths) t))
1727+
(when (and current-heading-p (<= current-beg (point) current-end))
1728+
(setq current-path (mapcar #'car path)))))
17231729
(point-min) (point-max))
1724-
paths))
1730+
(list paths current-path)))
17251731

1726-
(defun outline-hidden-headings-restore-paths (paths)
1732+
(defun outline-hidden-headings-restore-paths (paths current-path)
17271733
"Restore hidden outlines from a hash of hidden headings.
17281734
This is useful after reverting the buffer to restore the outlines
1729-
hidden by `outline-hidden-headings-paths'."
1730-
(let (current-path outline-view-change-hook)
1735+
hidden by `outline-hidden-headings-paths'. Also restore point
1736+
on the same outline where point was before reverting the buffer."
1737+
(let (path current-point outline-view-change-hook)
17311738
(outline-map-region
17321739
(lambda ()
17331740
(let* ((level (funcall outline-level))
17341741
(heading (buffer-substring (pos-bol) (pos-eol))))
1735-
(while (and current-path (>= (cdar current-path) level))
1736-
(pop current-path))
1737-
(push (cons heading level) current-path)
1738-
(when (gethash (mapcar #'car current-path) paths)
1739-
(outline-hide-subtree))))
1740-
(point-min) (point-max))))
1742+
(while (and path (>= (cdar path) level))
1743+
(pop path))
1744+
(push (cons heading level) path)
1745+
(when (gethash (mapcar #'car path) paths)
1746+
(outline-hide-subtree))
1747+
(when (and current-path (equal current-path (mapcar #'car path)))
1748+
(setq current-point (point)))))
1749+
(point-min) (point-max))
1750+
(when current-point (goto-char current-point))))
17411751

17421752
(defun outline-revert-buffer-restore-visibility ()
17431753
"Preserve visibility when reverting buffer under `outline-minor-mode'.
17441754
This function restores the visibility of outlines after the buffer
17451755
under `outline-minor-mode' is reverted by `revert-buffer'."
17461756
(let ((paths (outline-hidden-headings-paths)))
1747-
(unless (hash-table-empty-p paths)
1757+
(unless (and (hash-table-empty-p (nth 0 paths))
1758+
(null (nth 1 paths)))
17481759
(lambda ()
1749-
(outline-hidden-headings-restore-paths paths)))))
1760+
(outline-hidden-headings-restore-paths
1761+
(nth 0 paths) (nth 1 paths))))))
17501762

17511763
(defun outline-revert-buffer-rehighlight ()
17521764
"Rehighlight outlines when reverting buffer under `outline-minor-mode'.

0 commit comments

Comments
 (0)