diff options
| author | Simen Heggestøyl | 2019-06-22 12:49:04 +0200 |
|---|---|---|
| committer | Simen Heggestøyl | 2019-06-23 07:27:19 +0200 |
| commit | abf7d0d802728e0ae8e064683eb5fb411bad911e (patch) | |
| tree | dff14f64a25a3f5245a6a06e6cf173f855e53679 | |
| parent | b9d0337c84a6be7f26fd7134615048293320e234 (diff) | |
| download | emacs-abf7d0d802728e0ae8e064683eb5fb411bad911e.tar.gz emacs-abf7d0d802728e0ae8e064683eb5fb411bad911e.zip | |
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'. (Bug#36009)
| -rw-r--r-- | lisp/textmodes/page.el | 59 | ||||
| -rw-r--r-- | 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 8921b697f3b..a42fc6e0538 100644 --- a/lisp/textmodes/page.el +++ b/lisp/textmodes/page.el | |||
| @@ -125,41 +125,50 @@ thus showing a page other than the one point was originally in." | |||
| 125 | (point))))) | 125 | (point))))) |
| 126 | (put 'narrow-to-page 'disabled t) | 126 | (put 'narrow-to-page 'disabled t) |
| 127 | 127 | ||
| 128 | (defun count-lines-page () | 128 | (defun page--count-lines-page () |
| 129 | "Report number of lines on current page, and how many are before or after point." | 129 | "Return a list of line counts on the current page. |
| 130 | (interactive) | 130 | The list is on the form (TOTAL BEFORE AFTER), where TOTAL is the |
| 131 | total number of lines on the current page, while BEFORE and AFTER | ||
| 132 | are the number of lines on the current page before and after | ||
| 133 | point, respectively." | ||
| 131 | (save-excursion | 134 | (save-excursion |
| 132 | (let ((opoint (point)) beg end | 135 | (let ((opoint (point))) |
| 133 | total before after) | ||
| 134 | (forward-page) | 136 | (forward-page) |
| 135 | (beginning-of-line) | 137 | (beginning-of-line) |
| 136 | (or (looking-at page-delimiter) | 138 | (unless (looking-at page-delimiter) |
| 137 | (end-of-line)) | 139 | (end-of-line)) |
| 138 | (setq end (point)) | 140 | (let ((end (point))) |
| 139 | (backward-page) | 141 | (backward-page) |
| 140 | (setq beg (point)) | 142 | (list (count-lines (point) end) |
| 141 | (setq total (count-lines beg end) | 143 | (count-lines (point) opoint) |
| 142 | before (count-lines beg opoint) | 144 | (count-lines opoint end)))))) |
| 143 | after (count-lines opoint end)) | ||
| 144 | (message (ngettext "Page has %d line (%d + %d)" | ||
| 145 | "Page has %d lines (%d + %d)" total) | ||
| 146 | total before after)))) | ||
| 147 | 145 | ||
| 148 | (defun what-page () | 146 | (defun count-lines-page () |
| 149 | "Print page and line number of point." | 147 | "Report number of lines on current page, and how many are before or after point." |
| 150 | (interactive) | 148 | (interactive) |
| 149 | (pcase-let ((`(,total ,before ,after) (page--count-lines-page))) | ||
| 150 | (message (ngettext "Page has %d line (%d + %d)" | ||
| 151 | "Page has %d lines (%d + %d)" total) | ||
| 152 | total before after))) | ||
| 153 | |||
| 154 | (defun page--what-page () | ||
| 155 | "Return a list of the page and line number of point." | ||
| 151 | (save-restriction | 156 | (save-restriction |
| 152 | (widen) | 157 | (widen) |
| 153 | (save-excursion | 158 | (save-excursion |
| 154 | (let ((count 1) | 159 | (let ((count 1) |
| 155 | (opoint (point))) | 160 | (opoint (point))) |
| 156 | (goto-char (point-min)) | 161 | (goto-char (point-min)) |
| 157 | (while (re-search-forward page-delimiter opoint t) | 162 | (while (re-search-forward page-delimiter opoint t) |
| 158 | (if (= (match-beginning 0) (match-end 0)) | 163 | (when (= (match-beginning 0) (match-end 0)) |
| 159 | (forward-char 1)) | 164 | (forward-char)) |
| 160 | (setq count (1+ count))) | 165 | (setq count (1+ count))) |
| 161 | (message "Page %d, line %d" count (line-number-at-pos opoint)))))) | 166 | (list count (line-number-at-pos opoint)))))) |
| 162 | 167 | ||
| 168 | (defun what-page () | ||
| 169 | "Print page and line number of point." | ||
| 170 | (interactive) | ||
| 171 | (apply #'message (cons "Page %d, line %d" (page--what-page)))) | ||
| 163 | 172 | ||
| 164 | 173 | ||
| 165 | ;;; Place `provide' at end of file. | 174 | ;;; Place `provide' at end of file. |
diff --git a/test/lisp/textmodes/page-tests.el b/test/lisp/textmodes/page-tests.el index 0834d654338..517f1d5a9e5 100644 --- a/test/lisp/textmodes/page-tests.el +++ b/test/lisp/textmodes/page-tests.el | |||
| @@ -82,5 +82,22 @@ | |||
| 82 | (narrow-to-page -1) | 82 | (narrow-to-page -1) |
| 83 | (should (equal (buffer-string) "bar\n")))) | 83 | (should (equal (buffer-string) "bar\n")))) |
| 84 | 84 | ||
| 85 | (provide 'page-tests) | 85 | (ert-deftest page-tests-count-lines-page () |
| 86 | (with-temp-buffer | ||
| 87 | (insert "foo\n\nbar\n\nbaz") | ||
| 88 | (goto-char (point-min)) | ||
| 89 | (should (equal (page--count-lines-page) '(1 0 1))) | ||
| 90 | (goto-char (point-max)) | ||
| 91 | (should (equal (page--count-lines-page) '(2 2 0))))) | ||
| 92 | |||
| 93 | (ert-deftest page-tests-what-page () | ||
| 94 | (with-temp-buffer | ||
| 95 | (insert "foo\n\nbar\n\nbaz") | ||
| 96 | (goto-char (point-min)) | ||
| 97 | (should (equal (page--what-page) '(1 1))) | ||
| 98 | (forward-page) | ||
| 99 | (should (equal (page--what-page) '(2 2))) | ||
| 100 | (forward-page) | ||
| 101 | (should (equal (page--what-page) '(3 4))))) | ||
| 102 | |||
| 86 | ;;; page-tests.el ends here | 103 | ;;; page-tests.el ends here |