Commit 12f530a7 authored by Simen Heggestøyl's avatar Simen Heggestøyl

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
parent c3748b13
Pipeline #1846 failed with stage
in 4 seconds
;;; 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."
:group 'auto-insert)
(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)
......@@ -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.
(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)))
(goto-char 1)
;; Now, if we found something, do it
(and action
(or (not (stringp action))
;;; autoinsert-tests.el --- Tests for autoinsert.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
;; 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 'autoinsert)
(require 'ert)
(ert-deftest autoinsert-tests-auto-insert-skeleton ()
(let ((auto-insert-alist '((text-mode nil "f" _ "oo")))
(auto-insert-query nil))
(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")))
(let ((auto-insert-alist `((text-mode . ,temp-file)))
(auto-insert-query nil))
(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))
(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))
(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))
(setq-local buffer-file-name "fooobar")
(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")))
(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"))
(should (equal (buffer-string) "foobar")))))
(provide 'autoinsert-tests)
;;; autoinsert-tests.el ends here
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment