diff --git a/lisp/simple.el b/lisp/simple.el index 2e40e3261c..0cd1739c4d 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -1340,18 +1340,19 @@ goto-line ;; Leave mark at previous position (or (region-active-p) (push-mark)) ;; Move to the specified line number in that buffer. - (if (and (not relative) (not widen-automatically)) - (save-restriction - (widen) - (goto-char (point-min)) - (if (eq selective-display t) - (re-search-forward "[\n\C-m]" nil 'end (1- line)) - (forward-line (1- line)))) - (unless relative (widen)) - (goto-char (point-min)) - (if (eq selective-display t) - (re-search-forward "[\n\C-m]" nil 'end (1- line)) - (forward-line (1- line))))) + (let ((p (save-restriction + (unless relative (widen)) + (goto-char (point-min)) + (if (eq selective-display t) + (re-search-forward "[\n\C-m]" nil 'end (1- line)) + (forward-line (1- line))) + (point)))) + (when (and (not relative) + (buffer-narrowed-p) + widen-automatically + (not (and (<= (point-min) p) (<= p (point-max))))) + (widen)) + (goto-char p))) (defun goto-line-relative (line &optional buffer) "Go to LINE, counting from line at (point-min).