summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimen Heggestøyl <simenheg@gmail.com>2019-05-31 19:06:37 +0200
committerSimen Heggestøyl <simenheg@gmail.com>2019-05-31 19:07:48 +0200
commit12f530a7358aefe32134eeec3bc20ce58305e473 (patch)
tree3363e6aff6de9e9f347a2ffa9be6c14dc439b716
parentc3748b13765a193fcff7f75b9d1ba99b24fa3509 (diff)
downloademacs-12f530a7358aefe32134eeec3bc20ce58305e473.tar.gz
emacs-12f530a7358aefe32134eeec3bc20ce58305e473.tar.bz2
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.el53
-rw-r--r--test/lisp/autoinsert-tests.el104
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 @@
-;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
+;;; autoinsert.el --- automatic mode-dependent insertion of text into new files -*- lexical-binding: t -*-
;; Copyright (C) 1985-1987, 1994-1995, 1998, 2000-2019 Free Software
;; Foundation, Inc.
@@ -49,6 +49,8 @@
;;; Code:
+(require 'seq)
+
(defgroup auto-insert nil
"Automatic mode-dependent insertion of text into new files."
:prefix "auto-insert-"
@@ -72,22 +74,19 @@ With \\[auto-insert], this is always treated as if it were t."
:type '(choice (const :tag "Insert if possible" t)
(const :tag "Do nothing" nil)
(other :tag "insert if possible, mark as unmodified."
- not-modified))
- :group 'auto-insert)
+ not-modified)))
(defcustom auto-insert-query 'function
"Non-nil means ask user before auto-inserting.
When this is `function', only ask when called non-interactively."
:type '(choice (const :tag "Don't ask" nil)
(const :tag "Ask if called non-interactively" function)
- (other :tag "Ask" t))
- :group 'auto-insert)
+ (other :tag "Ask" t)))
(defcustom auto-insert-prompt "Perform %s auto-insertion? "
"Prompt to use when querying whether to auto-insert.
If this contains a %s, that will be replaced by the matching rule."
- :type 'string
- :group 'auto-insert)
+ :type 'string)
(defcustom auto-insert-alist
@@ -316,8 +315,7 @@ described above, e.g. [\"header.insert\" date-and-author-update]."
;; There's no custom equivalent of "repeat" for vectors.
:value-type (choice file function
(sexp :tag "Skeleton or vector")))
- :version "25.1"
- :group 'auto-insert)
+ :version "25.1")
;; Establish a default value for auto-insert-directory
@@ -325,8 +323,7 @@ described above, e.g. [\"header.insert\" date-and-author-update]."
"Directory from which auto-inserted files are taken.
The value must be an absolute directory name;
thus, on a GNU or Unix system, it must end in a slash."
- :type 'directory
- :group 'auto-insert)
+ :type 'directory)
;;;###autoload
@@ -338,23 +335,23 @@ Matches the visited file name against the elements of `auto-insert-alist'."
(or (eq this-command 'auto-insert)
(and auto-insert
(bobp) (eobp)))
- (let ((alist auto-insert-alist)
- case-fold-search cond desc action)
- (goto-char 1)
- ;; find first matching alist entry
- (while alist
- (if (atom (setq cond (car (car alist))))
- (setq desc cond)
- (setq desc (cdr cond)
- cond (car cond)))
- (if (if (symbolp cond)
- (derived-mode-p cond)
- (and buffer-file-name
- (string-match cond buffer-file-name)))
- (setq action (cdr (car alist))
- alist nil)
- (setq alist (cdr alist))))
-
+ (let* ((case-fold-search nil)
+ (desc nil)
+ ;; Find first matching alist entry.
+ (action
+ (seq-some
+ (pcase-lambda (`(,cond . ,action))
+ (if (atom cond)
+ (setq desc cond)
+ (setq desc (cdr cond)
+ cond (car cond)))
+ (when (if (symbolp cond)
+ (derived-mode-p cond)
+ (and buffer-file-name
+ (string-match cond buffer-file-name)))
+ action))
+ auto-insert-alist)))
+ (goto-char 1)
;; Now, if we found something, do it
(and action
(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 @@
+;;; autoinsert-tests.el --- Tests for autoinsert.el -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2019 Free Software Foundation, Inc.
+
+;; Author: Simen Heggestøyl <simenheg@gmail.com>
+;; 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 <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;
+
+;;; Code:
+
+(require 'autoinsert)
+(require 'ert)
+
+(ert-deftest autoinsert-tests-auto-insert-skeleton ()
+ (let ((auto-insert-alist '((text-mode nil "f" _ "oo")))
+ (auto-insert-query nil))
+ (with-temp-buffer
+ (text-mode)
+ (auto-insert)
+ (should (equal (buffer-string) "foo"))
+ (should (equal (point) (+ (point-min) 1))))))
+
+(ert-deftest autoinsert-tests-auto-insert-file ()
+ (let ((temp-file (make-temp-file "autoinsert-tests" nil nil "foo")))
+ (unwind-protect
+ (let ((auto-insert-alist `((text-mode . ,temp-file)))
+ (auto-insert-query nil))
+ (with-temp-buffer
+ (text-mode)
+ (auto-insert)
+ (should (equal (buffer-string) "foo"))))
+ (when (file-exists-p temp-file)
+ (delete-file temp-file)))))
+
+(ert-deftest autoinsert-tests-auto-insert-function ()
+ (let ((auto-insert-alist '((text-mode . (lambda () (insert "foo")))))
+ (auto-insert-query nil))
+ (with-temp-buffer
+ (text-mode)
+ (auto-insert)
+ (should (equal (buffer-string) "foo")))))
+
+(ert-deftest autoinsert-tests-auto-insert-vector ()
+ (let ((auto-insert-alist '((text-mode . [(nil "f" _ "bar")
+ (lambda () (insert "oo"))])))
+ (auto-insert-query nil))
+ (with-temp-buffer
+ (text-mode)
+ (auto-insert)
+ (should (equal (buffer-string) "foobar")))))
+
+(ert-deftest autoinsert-tests-auto-insert-regexp-match ()
+ (let ((auto-insert-alist '(("foobar" nil "1st")
+ ("fo+bar" nil "2nd")
+ ("fo*bar" nil "3rd")))
+ (auto-insert-query nil))
+ (with-temp-buffer
+ (setq-local buffer-file-name "fooobar")
+ (auto-insert)
+ (should (equal (buffer-string) "2nd")))))
+
+(ert-deftest autoinsert-tests-define-auto-insert-before ()
+ (let ((auto-insert-alist
+ (list (cons 'text-mode '(lambda () (insert "foo")))))
+ (auto-insert-query nil))
+ (define-auto-insert 'text-mode
+ '(lambda () (insert "bar")))
+ (with-temp-buffer
+ (text-mode)
+ (auto-insert)
+ (should (equal (buffer-string) "barfoo")))))
+
+(ert-deftest autoinsert-tests-define-auto-insert-after ()
+ (let ((auto-insert-alist
+ (list (cons 'text-mode '(lambda () (insert "foo")))))
+ (auto-insert-query nil))
+ (define-auto-insert 'text-mode
+ '(lambda () (insert "bar"))
+ t)
+ (with-temp-buffer
+ (text-mode)
+ (auto-insert)
+ (should (equal (buffer-string) "foobar")))))
+
+(provide 'autoinsert-tests)
+;;; autoinsert-tests.el ends here