From: Simen Heggestøyl Date: Wed, 29 May 2019 18:29:28 +0000 (+0200) Subject: Use lexical-binding in makesum.el and add tests X-Git-Tag: emacs-27.0.90~2762 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=49cdbb4a35f8d1d2139e8469bffcf33f65679094;p=emacs.git 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. --- 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 @@ -;;; makesum.el --- generate key binding summary for Emacs +;;; makesum.el --- generate key binding summary for Emacs -*- lexical-binding:t -*- ;; Copyright (C) 1985, 2001-2019 Free Software Foundation, Inc. @@ -59,15 +59,14 @@ Previous contents of that buffer are killed first." (while (search-forward "C-i" nil t) (replace-match "TAB")) (goto-char (point-min)) - (if (re-search-forward "^Local Bindings:" nil t) - (progn - (forward-char -1) - (insert " for " (format-mode-line cur-mode) " Mode") - (while (search-forward "??\n" nil t) - (delete-region (point) - (progn - (forward-line -1) - (point)))))) + (when (re-search-forward "^Local Bindings:" nil t) + (forward-char -1) + (insert " for " (format-mode-line cur-mode) " Mode") + (while (search-forward "??\n" nil t) + (delete-region (point) + (progn + (forward-line -1) + (point))))) (goto-char (point-min)) (insert "Emacs command summary, " (substring (current-time-string) 0 10) ".\n") @@ -84,28 +83,25 @@ Previous contents of that buffer are killed first." (message "Making command summary...done")) (defun double-column (start end) + "Reformat buffer contents from START to END into two columns." (interactive "r") - (let (half line lines nlines + (let (half lines + (nlines (count-lines start end)) (from-end (- (point-max) end))) - (setq nlines (count-lines start end)) - (if (<= nlines 1) - nil + (when (> nlines 1) (setq half (/ (1+ nlines) 2)) (goto-char start) (save-excursion (forward-line half) - (while (< half nlines) - (setq half (1+ half)) - (setq line (buffer-substring (point) (line-end-position))) - (setq lines (cons line lines)) + (dotimes (_ (- nlines half)) + (push (buffer-substring (point) (line-end-position)) + lines) (delete-region (point) (progn (forward-line 1) (point))))) - (setq lines (nreverse lines)) - (while lines - (end-of-line) + (dolist (line (nreverse lines)) + (end-of-line) (indent-to 41) - (insert (car lines)) - (forward-line 1) - (setq lines (cdr lines)))) + (insert line) + (forward-line 1))) (goto-char (- (point-max) from-end)))) (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 @@ +;;; makesum-tests.el --- Tests for makesum.el -*- lexical-binding: t; -*- + +;; Copyright (C) 2019 Free Software Foundation, Inc. + +;; Author: Simen Heggestøyl +;; Keywords: + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: + +;; + +;;; Code: + +(require 'ert) +(require 'makesum) + +(ert-deftest makesum-tests-double-column-even-lines () + (with-temp-buffer + (insert "a\nb\nc\nd\ne\nf") + (double-column (point-min) (point-max)) + (should (string-match-p "a[ \t]+d\nb[ \t]+e\nc[ \t]+f" (buffer-string))))) + +(ert-deftest makesum-tests-double-column-odd-lines () + (with-temp-buffer + (insert "a\nb\nc\nd\ne") + (double-column (point-min) (point-max)) + (should (string-match-p "a[ \t]+d\nb[ \t]+e\nc" (buffer-string))))) + +(ert-deftest makesum-tests-double-column-noop () + (with-temp-buffer + (insert "foo") + (let ((prev-buffer-string (buffer-string))) + (double-column (point-min) (point-max)) + (should (equal prev-buffer-string (buffer-string)))))) + +(ert-deftest makesum-tests-double-column-partial () + (with-temp-buffer + (insert "a\nb\nc\nd\ne\nf") + (double-column 3 10) + (should (string-match-p "a\nb[ \t]+d\nc[ \t]+e\nf" (buffer-string))))) + +(provide 'makesum-tests) +;;; makesum-tests.el ends here