diff options
| author | Simen Heggestøyl | 2019-05-31 19:06:37 +0200 |
|---|---|---|
| committer | Simen Heggestøyl | 2019-05-31 19:07:48 +0200 |
| commit | 12f530a7358aefe32134eeec3bc20ce58305e473 (patch) | |
| tree | 3363e6aff6de9e9f347a2ffa9be6c14dc439b716 | |
| parent | c3748b13765a193fcff7f75b9d1ba99b24fa3509 (diff) | |
| download | emacs-12f530a7358aefe32134eeec3bc20ce58305e473.tar.gz emacs-12f530a7358aefe32134eeec3bc20ce58305e473.zip | |
Use lexical-binding in autoinsert.el and add tests
* lisp/autoinsert.el: Use lexical-binding.
Remove redundant :group args.
(auto-insert): Simplify.
* test/lisp/autoinsert-tests.el: New file with tests for
autoinsert.el.
| -rw-r--r-- | lisp/autoinsert.el | 53 | ||||
| -rw-r--r-- | test/lisp/autoinsert-tests.el | 104 |
2 files changed, 129 insertions, 28 deletions
diff --git a/lisp/autoinsert.el b/lisp/autoinsert.el index f7ecfe27bb1..a77ca091d29 100644 --- a/lisp/autoinsert.el +++ b/lisp/autoinsert.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files | 1 | ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files -*- lexical-binding: t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1985-1987, 1994-1995, 1998, 2000-2019 Free Software | 3 | ;; Copyright (C) 1985-1987, 1994-1995, 1998, 2000-2019 Free Software |
| 4 | ;; Foundation, Inc. | 4 | ;; Foundation, Inc. |
| @@ -49,6 +49,8 @@ | |||
| 49 | 49 | ||
| 50 | ;;; Code: | 50 | ;;; Code: |
| 51 | 51 | ||
| 52 | (require 'seq) | ||
| 53 | |||
| 52 | (defgroup auto-insert nil | 54 | (defgroup auto-insert nil |
| 53 | "Automatic mode-dependent insertion of text into new files." | 55 | "Automatic mode-dependent insertion of text into new files." |
| 54 | :prefix "auto-insert-" | 56 | :prefix "auto-insert-" |
| @@ -72,22 +74,19 @@ With \\[auto-insert], this is always treated as if it were t." | |||
| 72 | :type '(choice (const :tag "Insert if possible" t) | 74 | :type '(choice (const :tag "Insert if possible" t) |
| 73 | (const :tag "Do nothing" nil) | 75 | (const :tag "Do nothing" nil) |
| 74 | (other :tag "insert if possible, mark as unmodified." | 76 | (other :tag "insert if possible, mark as unmodified." |
| 75 | not-modified)) | 77 | not-modified))) |
| 76 | :group 'auto-insert) | ||
| 77 | 78 | ||
| 78 | (defcustom auto-insert-query 'function | 79 | (defcustom auto-insert-query 'function |
| 79 | "Non-nil means ask user before auto-inserting. | 80 | "Non-nil means ask user before auto-inserting. |
| 80 | When this is `function', only ask when called non-interactively." | 81 | When this is `function', only ask when called non-interactively." |
| 81 | :type '(choice (const :tag "Don't ask" nil) | 82 | :type '(choice (const :tag "Don't ask" nil) |
| 82 | (const :tag "Ask if called non-interactively" function) | 83 | (const :tag "Ask if called non-interactively" function) |
| 83 | (other :tag "Ask" t)) | 84 | (other :tag "Ask" t))) |
| 84 | :group 'auto-insert) | ||
| 85 | 85 | ||
| 86 | (defcustom auto-insert-prompt "Perform %s auto-insertion? " | 86 | (defcustom auto-insert-prompt "Perform %s auto-insertion? " |
| 87 | "Prompt to use when querying whether to auto-insert. | 87 | "Prompt to use when querying whether to auto-insert. |
| 88 | If this contains a %s, that will be replaced by the matching rule." | 88 | If this contains a %s, that will be replaced by the matching rule." |
| 89 | :type 'string | 89 | :type 'string) |
| 90 | :group 'auto-insert) | ||
| 91 | 90 | ||
| 92 | 91 | ||
| 93 | (defcustom auto-insert-alist | 92 | (defcustom auto-insert-alist |
| @@ -316,8 +315,7 @@ described above, e.g. [\"header.insert\" date-and-author-update]." | |||
| 316 | ;; There's no custom equivalent of "repeat" for vectors. | 315 | ;; There's no custom equivalent of "repeat" for vectors. |
| 317 | :value-type (choice file function | 316 | :value-type (choice file function |
| 318 | (sexp :tag "Skeleton or vector"))) | 317 | (sexp :tag "Skeleton or vector"))) |
| 319 | :version "25.1" | 318 | :version "25.1") |
| 320 | :group 'auto-insert) | ||
| 321 | 319 | ||
| 322 | 320 | ||
| 323 | ;; Establish a default value for auto-insert-directory | 321 | ;; Establish a default value for auto-insert-directory |
| @@ -325,8 +323,7 @@ described above, e.g. [\"header.insert\" date-and-author-update]." | |||
| 325 | "Directory from which auto-inserted files are taken. | 323 | "Directory from which auto-inserted files are taken. |
| 326 | The value must be an absolute directory name; | 324 | The value must be an absolute directory name; |
| 327 | thus, on a GNU or Unix system, it must end in a slash." | 325 | thus, on a GNU or Unix system, it must end in a slash." |
| 328 | :type 'directory | 326 | :type 'directory) |
| 329 | :group 'auto-insert) | ||
| 330 | 327 | ||
| 331 | 328 | ||
| 332 | ;;;###autoload | 329 | ;;;###autoload |
| @@ -338,23 +335,23 @@ Matches the visited file name against the elements of `auto-insert-alist'." | |||
| 338 | (or (eq this-command 'auto-insert) | 335 | (or (eq this-command 'auto-insert) |
| 339 | (and auto-insert | 336 | (and auto-insert |
| 340 | (bobp) (eobp))) | 337 | (bobp) (eobp))) |
| 341 | (let ((alist auto-insert-alist) | 338 | (let* ((case-fold-search nil) |
| 342 | case-fold-search cond desc action) | 339 | (desc nil) |
| 343 | (goto-char 1) | 340 | ;; Find first matching alist entry. |
| 344 | ;; find first matching alist entry | 341 | (action |
| 345 | (while alist | 342 | (seq-some |
| 346 | (if (atom (setq cond (car (car alist)))) | 343 | (pcase-lambda (`(,cond . ,action)) |
| 347 | (setq desc cond) | 344 | (if (atom cond) |
| 348 | (setq desc (cdr cond) | 345 | (setq desc cond) |
| 349 | cond (car cond))) | 346 | (setq desc (cdr cond) |
| 350 | (if (if (symbolp cond) | 347 | cond (car cond))) |
| 351 | (derived-mode-p cond) | 348 | (when (if (symbolp cond) |
| 352 | (and buffer-file-name | 349 | (derived-mode-p cond) |
| 353 | (string-match cond buffer-file-name))) | 350 | (and buffer-file-name |
| 354 | (setq action (cdr (car alist)) | 351 | (string-match cond buffer-file-name))) |
| 355 | alist nil) | 352 | action)) |
| 356 | (setq alist (cdr alist)))) | 353 | auto-insert-alist))) |
| 357 | 354 | (goto-char 1) | |
| 358 | ;; Now, if we found something, do it | 355 | ;; Now, if we found something, do it |
| 359 | (and action | 356 | (and action |
| 360 | (or (not (stringp action)) | 357 | (or (not (stringp action)) |
diff --git a/test/lisp/autoinsert-tests.el b/test/lisp/autoinsert-tests.el new file mode 100644 index 00000000000..a7b0547633b --- /dev/null +++ b/test/lisp/autoinsert-tests.el | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | ;;; autoinsert-tests.el --- Tests for autoinsert.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 'autoinsert) | ||
| 30 | (require 'ert) | ||
| 31 | |||
| 32 | (ert-deftest autoinsert-tests-auto-insert-skeleton () | ||
| 33 | (let ((auto-insert-alist '((text-mode nil "f" _ "oo"))) | ||
| 34 | (auto-insert-query nil)) | ||
| 35 | (with-temp-buffer | ||
| 36 | (text-mode) | ||
| 37 | (auto-insert) | ||
| 38 | (should (equal (buffer-string) "foo")) | ||
| 39 | (should (equal (point) (+ (point-min) 1)))))) | ||
| 40 | |||
| 41 | (ert-deftest autoinsert-tests-auto-insert-file () | ||
| 42 | (let ((temp-file (make-temp-file "autoinsert-tests" nil nil "foo"))) | ||
| 43 | (unwind-protect | ||
| 44 | (let ((auto-insert-alist `((text-mode . ,temp-file))) | ||
| 45 | (auto-insert-query nil)) | ||
| 46 | (with-temp-buffer | ||
| 47 | (text-mode) | ||
| 48 | (auto-insert) | ||
| 49 | (should (equal (buffer-string) "foo")))) | ||
| 50 | (when (file-exists-p temp-file) | ||
| 51 | (delete-file temp-file))))) | ||
| 52 | |||
| 53 | (ert-deftest autoinsert-tests-auto-insert-function () | ||
| 54 | (let ((auto-insert-alist '((text-mode . (lambda () (insert "foo"))))) | ||
| 55 | (auto-insert-query nil)) | ||
| 56 | (with-temp-buffer | ||
| 57 | (text-mode) | ||
| 58 | (auto-insert) | ||
| 59 | (should (equal (buffer-string) "foo"))))) | ||
| 60 | |||
| 61 | (ert-deftest autoinsert-tests-auto-insert-vector () | ||
| 62 | (let ((auto-insert-alist '((text-mode . [(nil "f" _ "bar") | ||
| 63 | (lambda () (insert "oo"))]))) | ||
| 64 | (auto-insert-query nil)) | ||
| 65 | (with-temp-buffer | ||
| 66 | (text-mode) | ||
| 67 | (auto-insert) | ||
| 68 | (should (equal (buffer-string) "foobar"))))) | ||
| 69 | |||
| 70 | (ert-deftest autoinsert-tests-auto-insert-regexp-match () | ||
| 71 | (let ((auto-insert-alist '(("foobar" nil "1st") | ||
| 72 | ("fo+bar" nil "2nd") | ||
| 73 | ("fo*bar" nil "3rd"))) | ||
| 74 | (auto-insert-query nil)) | ||
| 75 | (with-temp-buffer | ||
| 76 | (setq-local buffer-file-name "fooobar") | ||
| 77 | (auto-insert) | ||
| 78 | (should (equal (buffer-string) "2nd"))))) | ||
| 79 | |||
| 80 | (ert-deftest autoinsert-tests-define-auto-insert-before () | ||
| 81 | (let ((auto-insert-alist | ||
| 82 | (list (cons 'text-mode '(lambda () (insert "foo"))))) | ||
| 83 | (auto-insert-query nil)) | ||
| 84 | (define-auto-insert 'text-mode | ||
| 85 | '(lambda () (insert "bar"))) | ||
| 86 | (with-temp-buffer | ||
| 87 | (text-mode) | ||
| 88 | (auto-insert) | ||
| 89 | (should (equal (buffer-string) "barfoo"))))) | ||
| 90 | |||
| 91 | (ert-deftest autoinsert-tests-define-auto-insert-after () | ||
| 92 | (let ((auto-insert-alist | ||
| 93 | (list (cons 'text-mode '(lambda () (insert "foo"))))) | ||
| 94 | (auto-insert-query nil)) | ||
| 95 | (define-auto-insert 'text-mode | ||
| 96 | '(lambda () (insert "bar")) | ||
| 97 | t) | ||
| 98 | (with-temp-buffer | ||
| 99 | (text-mode) | ||
| 100 | (auto-insert) | ||
| 101 | (should (equal (buffer-string) "foobar"))))) | ||
| 102 | |||
| 103 | (provide 'autoinsert-tests) | ||
| 104 | ;;; autoinsert-tests.el ends here | ||