diff --git a/lisp/emacs-lisp/tabulated-list.el b/lisp/emacs-lisp/tabulated-list.el index b6b49b1bfa..57dca910a8 100644 --- a/lisp/emacs-lisp/tabulated-list.el +++ b/lisp/emacs-lisp/tabulated-list.el @@ -395,10 +395,21 @@ changing `tabulated-list-sort-key'." (set-buffer-modified-p nil) ;; If REMEMBER-POS was specified, move to the "old" location. (if saved-pt - (progn (goto-char saved-pt) - (move-to-column saved-col) - (when window-line - (recenter window-line))) + (let* ((lines (window-screen-lines)) + (id (tabulated-list-get-id saved-pt)) + (entries (if (functionp tabulated-list-entries) + (funcall tabulated-list-entries) + tabulated-list-entries)) + (position (seq-position (map-keys entries) id)) + (num-entries (length entries)) + (remaining-entries (- num-entries position))) + (goto-char saved-pt) + (move-to-column saved-col) + ;; Only recenter if there are enough lines so that there's no empty + ;; space below the end of the text + (when (and window-line + (> remaining-entries lines)) + (recenter window-line))) (goto-char (point-min))))) (defun tabulated-list-print-entry (id cols)