From: Lars Ingebrigtsen Date: Fri, 22 Apr 2022 12:04:43 +0000 (+0200) Subject: Fix problem with (narrow-to-page 1) with point at point-max X-Git-Tag: emacs-29.0.90~1931^2~362 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=3aaed2e1ccfcc230f813d3fe7867a7abc5b22109;p=emacs.git Fix problem with (narrow-to-page 1) with point at point-max * lisp/textmodes/page.el (forward-page): Make this work more consistently if point is on bol (bug#20663). --- diff --git a/lisp/textmodes/page.el b/lisp/textmodes/page.el index 3fc18323349..5d6f017eb99 100644 --- a/lisp/textmodes/page.el +++ b/lisp/textmodes/page.el @@ -35,11 +35,18 @@ A page boundary is any line whose beginning matches the regexp (interactive "p") (or count (setq count 1)) (while (and (> count 0) (not (eobp))) - ;; In case the page-delimiter matches the null string, - ;; don't find a match without moving. - (if (bolp) (forward-char 1)) - (unless (re-search-forward page-delimiter nil t) - (goto-char (point-max))) + (if (and (looking-at page-delimiter) + (> (match-end 0) (point))) + ;; If we're standing at the page delimiter, then just skip to + ;; the end of it. (But only if it's not a zero-length + ;; delimiter, because then we wouldn't have forward progress.) + (goto-char (match-end 0)) + ;; In case the page-delimiter matches the null string, + ;; don't find a match without moving. + (when (bolp) + (forward-char 1)) + (unless (re-search-forward page-delimiter nil t) + (goto-char (point-max)))) (setq count (1- count))) (while (and (< count 0) (not (bobp))) ;; In case the page-delimiter matches the null string, diff --git a/test/lisp/textmodes/page-tests.el b/test/lisp/textmodes/page-tests.el index 596f3a6ceb7..b7217e69f0c 100644 --- a/test/lisp/textmodes/page-tests.el +++ b/test/lisp/textmodes/page-tests.el @@ -80,6 +80,17 @@ (narrow-to-page 2) (should (equal (buffer-string) "baz")) (narrow-to-page -1) + (should (equal (buffer-string) "bar\n")) + + (widen) + (goto-char (point-min)) + (narrow-to-page) + (should (equal (buffer-string) "foo\n")) + (goto-char (point-max)) + (narrow-to-page 2) + (should (equal (buffer-string) "baz")) + (goto-char (point-max)) + (narrow-to-page -1) (should (equal (buffer-string) "bar\n")))) (ert-deftest page-tests-count-lines-page () @@ -100,4 +111,5 @@ (forward-page) (should (equal (page--what-page) '(3 4))))) + ;;; page-tests.el ends here