all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Simen Heggestøyl" <simenheg@gmail.com>
To: Stefan Kangas <stefan@marxist.se>
Cc: 36009@debbugs.gnu.org, Paul Eggert <eggert@cs.ucla.edu>
Subject: bug#36009: [PATCH] Use lexical-binding in textmodes/page.el and add tests
Date: Sat, 22 Jun 2019 12:55:17 +0200	[thread overview]
Message-ID: <1561200917.30499.0@smtp.gmail.com> (raw)
In-Reply-To: <CADwFkmm4oVUJ6C1VX9ruz5Fd0LufOUwhHDm40YFsc3Lbc5R0AA@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 49 bytes --]

How about something along these lines?

-- Simen

[-- Attachment #1.2: Type: text/html, Size: 121 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Split-up-and-add-tests-for-two-page.el-functions.patch --]
[-- Type: text/x-patch, Size: 4774 bytes --]

From aaa78cb17dbfcfa8e7301d3e6734c5157a6da4ab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <simenheg@gmail.com>
Date: Sat, 22 Jun 2019 12:49:04 +0200
Subject: [PATCH] Split up and add tests for two page.el functions

* lisp/textmodes/page.el (page--count-lines-page): New function
extracted from `count-lines-page'.
(count-lines-page): Extract main logic into `page--count-lines-page'.
(page--what-page); New function extracted from `what-page'.
(what-page): Extract main logic into `page--what-page'.

* test/lisp/textmodes/page-tests.el (page-tests-count-lines-page)
(page-tests-what-page): New tests for `page--count-lines-page' and
`page--what-page'.
---
 lisp/textmodes/page.el            | 59 ++++++++++++++++++-------------
 test/lisp/textmodes/page-tests.el | 19 +++++++++-
 2 files changed, 52 insertions(+), 26 deletions(-)

diff --git a/lisp/textmodes/page.el b/lisp/textmodes/page.el
index 8921b697f3..a42fc6e053 100644
--- a/lisp/textmodes/page.el
+++ b/lisp/textmodes/page.el
@@ -125,41 +125,50 @@ narrow-to-page
 			(point)))))
 (put 'narrow-to-page 'disabled t)
 
-(defun count-lines-page ()
-  "Report number of lines on current page, and how many are before or after point."
-  (interactive)
+(defun page--count-lines-page ()
+  "Return a list of line counts on the current page.
+The list is on the form (TOTAL BEFORE AFTER), where TOTAL is the
+total number of lines on the current page, while BEFORE and AFTER
+are the number of lines on the current page before and after
+point, respectively."
   (save-excursion
-    (let ((opoint (point)) beg end
-	  total before after)
+    (let ((opoint (point)))
       (forward-page)
       (beginning-of-line)
-      (or (looking-at page-delimiter)
-	  (end-of-line))
-      (setq end (point))
-      (backward-page)
-      (setq beg (point))
-      (setq total (count-lines beg end)
-	    before (count-lines beg opoint)
-	    after (count-lines opoint end))
-      (message (ngettext "Page has %d line (%d + %d)"
-			 "Page has %d lines (%d + %d)" total)
-	       total before after))))
+      (unless (looking-at page-delimiter)
+        (end-of-line))
+      (let ((end (point)))
+        (backward-page)
+        (list (count-lines (point) end)
+              (count-lines (point) opoint)
+              (count-lines opoint end))))))
 
-(defun what-page ()
-  "Print page and line number of point."
+(defun count-lines-page ()
+  "Report number of lines on current page, and how many are before or after point."
   (interactive)
+  (pcase-let ((`(,total ,before ,after) (page--count-lines-page)))
+    (message (ngettext "Page has %d line (%d + %d)"
+                       "Page has %d lines (%d + %d)" total)
+             total before after)))
+
+(defun page--what-page ()
+  "Return a list of the page and line number of point."
   (save-restriction
     (widen)
     (save-excursion
       (let ((count 1)
-	    (opoint (point)))
-	(goto-char (point-min))
-	(while (re-search-forward page-delimiter opoint t)
-          (if (= (match-beginning 0) (match-end 0))
-              (forward-char 1))
-	  (setq count (1+ count)))
-	(message "Page %d, line %d" count (line-number-at-pos opoint))))))
+            (opoint (point)))
+        (goto-char (point-min))
+        (while (re-search-forward page-delimiter opoint t)
+          (when (= (match-beginning 0) (match-end 0))
+            (forward-char))
+          (setq count (1+ count)))
+        (list count (line-number-at-pos opoint))))))
 
+(defun what-page ()
+  "Print page and line number of point."
+  (interactive)
+  (apply #'message (cons "Page %d, line %d" (page--what-page))))
 
 \f
 ;;; Place `provide' at end of file.
diff --git a/test/lisp/textmodes/page-tests.el b/test/lisp/textmodes/page-tests.el
index 0834d65433..517f1d5a9e 100644
--- a/test/lisp/textmodes/page-tests.el
+++ b/test/lisp/textmodes/page-tests.el
@@ -82,5 +82,22 @@ page-tests-narrow-to-page
     (narrow-to-page -1)
     (should (equal (buffer-string) "bar\n"))))
 
-(provide 'page-tests)
+(ert-deftest page-tests-count-lines-page ()
+  (with-temp-buffer
+    (insert "foo\n\f\nbar\n\f\nbaz")
+    (goto-char (point-min))
+    (should (equal (page--count-lines-page) '(1 0 1)))
+    (goto-char (point-max))
+    (should (equal (page--count-lines-page) '(2 2 0)))))
+
+(ert-deftest page-tests-what-page ()
+  (with-temp-buffer
+    (insert "foo\n\f\nbar\n\f\nbaz")
+    (goto-char (point-min))
+    (should (equal (page--what-page) '(1 1)))
+    (forward-page)
+    (should (equal (page--what-page) '(2 2)))
+    (forward-page)
+    (should (equal (page--what-page) '(3 4)))))
+
 ;;; page-tests.el ends here
-- 
2.20.1


  reply	other threads:[~2019-06-22 10:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-30 18:31 bug#36009: [PATCH] Use lexical-binding in textmodes/page.el and add tests Stefan Kangas
2019-06-02  1:58 ` Paul Eggert
2019-06-02 10:25   ` Simen Heggestøyl
2019-06-13 22:02     ` Stefan Kangas
2019-06-22 10:55       ` Simen Heggestøyl [this message]
2019-06-22 21:32         ` Stefan Kangas
2019-06-22 23:57           ` Paul Eggert
2019-06-23  5:29             ` Simen Heggestøyl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1561200917.30499.0@smtp.gmail.com \
    --to=simenheg@gmail.com \
    --cc=36009@debbugs.gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=stefan@marxist.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.