diff options
| author | Simen Heggestøyl | 2019-05-29 20:29:28 +0200 |
|---|---|---|
| committer | Simen Heggestøyl | 2019-05-29 20:47:16 +0200 |
| commit | 49cdbb4a35f8d1d2139e8469bffcf33f65679094 (patch) | |
| tree | 0d2cfb4978f07906a171f801620ec84fae7cf3aa | |
| parent | 7f3b0d1c0003f3b883a6df3d2fae7f847f6d3070 (diff) | |
| download | emacs-49cdbb4a35f8d1d2139e8469bffcf33f65679094.tar.gz emacs-49cdbb4a35f8d1d2139e8469bffcf33f65679094.zip | |
Use lexical-binding in makesum.el and add tests
* lisp/makesum.el: Use lexical-binding.
(make-command-summary): Replace `if..progn' with `when'.
(double-column): Add docstring and apply trivial simplifications.
* test/lisp/makesum-tests.el: New file with tests for makesum.el.
| -rw-r--r-- | lisp/makesum.el | 44 | ||||
| -rw-r--r-- | test/lisp/makesum-tests.el | 58 |
2 files changed, 78 insertions, 24 deletions
diff --git a/lisp/makesum.el b/lisp/makesum.el index 10ad78ea174..50f5d63871f 100644 --- a/lisp/makesum.el +++ b/lisp/makesum.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; makesum.el --- generate key binding summary for Emacs | 1 | ;;; makesum.el --- generate key binding summary for Emacs -*- lexical-binding:t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1985, 2001-2019 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 1985, 2001-2019 Free Software Foundation, Inc. |
| 4 | 4 | ||
| @@ -59,15 +59,14 @@ Previous contents of that buffer are killed first." | |||
| 59 | (while (search-forward "C-i" nil t) | 59 | (while (search-forward "C-i" nil t) |
| 60 | (replace-match "TAB")) | 60 | (replace-match "TAB")) |
| 61 | (goto-char (point-min)) | 61 | (goto-char (point-min)) |
| 62 | (if (re-search-forward "^Local Bindings:" nil t) | 62 | (when (re-search-forward "^Local Bindings:" nil t) |
| 63 | (progn | 63 | (forward-char -1) |
| 64 | (forward-char -1) | 64 | (insert " for " (format-mode-line cur-mode) " Mode") |
| 65 | (insert " for " (format-mode-line cur-mode) " Mode") | 65 | (while (search-forward "??\n" nil t) |
| 66 | (while (search-forward "??\n" nil t) | 66 | (delete-region (point) |
| 67 | (delete-region (point) | 67 | (progn |
| 68 | (progn | 68 | (forward-line -1) |
| 69 | (forward-line -1) | 69 | (point))))) |
| 70 | (point)))))) | ||
| 71 | (goto-char (point-min)) | 70 | (goto-char (point-min)) |
| 72 | (insert "Emacs command summary, " (substring (current-time-string) 0 10) | 71 | (insert "Emacs command summary, " (substring (current-time-string) 0 10) |
| 73 | ".\n") | 72 | ".\n") |
| @@ -84,28 +83,25 @@ Previous contents of that buffer are killed first." | |||
| 84 | (message "Making command summary...done")) | 83 | (message "Making command summary...done")) |
| 85 | 84 | ||
| 86 | (defun double-column (start end) | 85 | (defun double-column (start end) |
| 86 | "Reformat buffer contents from START to END into two columns." | ||
| 87 | (interactive "r") | 87 | (interactive "r") |
| 88 | (let (half line lines nlines | 88 | (let (half lines |
| 89 | (nlines (count-lines start end)) | ||
| 89 | (from-end (- (point-max) end))) | 90 | (from-end (- (point-max) end))) |
| 90 | (setq nlines (count-lines start end)) | 91 | (when (> nlines 1) |
| 91 | (if (<= nlines 1) | ||
| 92 | nil | ||
| 93 | (setq half (/ (1+ nlines) 2)) | 92 | (setq half (/ (1+ nlines) 2)) |
| 94 | (goto-char start) | 93 | (goto-char start) |
| 95 | (save-excursion | 94 | (save-excursion |
| 96 | (forward-line half) | 95 | (forward-line half) |
| 97 | (while (< half nlines) | 96 | (dotimes (_ (- nlines half)) |
| 98 | (setq half (1+ half)) | 97 | (push (buffer-substring (point) (line-end-position)) |
| 99 | (setq line (buffer-substring (point) (line-end-position))) | 98 | lines) |
| 100 | (setq lines (cons line lines)) | ||
| 101 | (delete-region (point) (progn (forward-line 1) (point))))) | 99 | (delete-region (point) (progn (forward-line 1) (point))))) |
| 102 | (setq lines (nreverse lines)) | 100 | (dolist (line (nreverse lines)) |
| 103 | (while lines | 101 | (end-of-line) |
| 104 | (end-of-line) | ||
| 105 | (indent-to 41) | 102 | (indent-to 41) |
| 106 | (insert (car lines)) | 103 | (insert line) |
| 107 | (forward-line 1) | 104 | (forward-line 1))) |
| 108 | (setq lines (cdr lines)))) | ||
| 109 | (goto-char (- (point-max) from-end)))) | 105 | (goto-char (- (point-max) from-end)))) |
| 110 | 106 | ||
| 111 | (provide 'makesum) | 107 | (provide 'makesum) |
diff --git a/test/lisp/makesum-tests.el b/test/lisp/makesum-tests.el new file mode 100644 index 00000000000..09661593c62 --- /dev/null +++ b/test/lisp/makesum-tests.el | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | ;;; makesum-tests.el --- Tests for makesum.el -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2019 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Simen Heggestøyl <simenheg@gmail.com> | ||
| 6 | ;; Keywords: | ||
| 7 | |||
| 8 | ;; This file is part of GNU Emacs. | ||
| 9 | |||
| 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 11 | ;; it under the terms of the GNU General Public License as published by | ||
| 12 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 13 | ;; (at your option) any later version. | ||
| 14 | |||
| 15 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | ;; GNU General Public License for more details. | ||
| 19 | |||
| 20 | ;; You should have received a copy of the GNU General Public License | ||
| 21 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 22 | |||
| 23 | ;;; Commentary: | ||
| 24 | |||
| 25 | ;; | ||
| 26 | |||
| 27 | ;;; Code: | ||
| 28 | |||
| 29 | (require 'ert) | ||
| 30 | (require 'makesum) | ||
| 31 | |||
| 32 | (ert-deftest makesum-tests-double-column-even-lines () | ||
| 33 | (with-temp-buffer | ||
| 34 | (insert "a\nb\nc\nd\ne\nf") | ||
| 35 | (double-column (point-min) (point-max)) | ||
| 36 | (should (string-match-p "a[ \t]+d\nb[ \t]+e\nc[ \t]+f" (buffer-string))))) | ||
| 37 | |||
| 38 | (ert-deftest makesum-tests-double-column-odd-lines () | ||
| 39 | (with-temp-buffer | ||
| 40 | (insert "a\nb\nc\nd\ne") | ||
| 41 | (double-column (point-min) (point-max)) | ||
| 42 | (should (string-match-p "a[ \t]+d\nb[ \t]+e\nc" (buffer-string))))) | ||
| 43 | |||
| 44 | (ert-deftest makesum-tests-double-column-noop () | ||
| 45 | (with-temp-buffer | ||
| 46 | (insert "foo") | ||
| 47 | (let ((prev-buffer-string (buffer-string))) | ||
| 48 | (double-column (point-min) (point-max)) | ||
| 49 | (should (equal prev-buffer-string (buffer-string)))))) | ||
| 50 | |||
| 51 | (ert-deftest makesum-tests-double-column-partial () | ||
| 52 | (with-temp-buffer | ||
| 53 | (insert "a\nb\nc\nd\ne\nf") | ||
| 54 | (double-column 3 10) | ||
| 55 | (should (string-match-p "a\nb[ \t]+d\nc[ \t]+e\nf" (buffer-string))))) | ||
| 56 | |||
| 57 | (provide 'makesum-tests) | ||
| 58 | ;;; makesum-tests.el ends here | ||