about summary refs log tree commit diff stats
path: root/lisp/acdw-autoinsert.el
blob: bc0810a2c68139b103da2af5c917ae6a199fe74e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
;;; acdw-autoinsert.el --- autoinsert.el            -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Case Duckworth

;; Author: Case Duckworth <acdw@acdw.ne

;;; License:

;; Everyone is permitted to do whatever with this software, without
;; limitation.  This software comes without any warranty whatsoever,
;; but with two pieces of advice:

;; - Be kind to yourself.

;; - Make good choices.

;;; Commentary:

;; These are my bespoke changes to the `autoinsert' library.

;;; Code:

(require 'autoinsert)
(require 'cl-lib)

(defun acdw/define-auto-insert (options condition action)
  "Associate CONDITION with ACTION in `auto-insert-alist'.
This function differs from `define-auto-insert' in that it won't
allow more than one duplicate entry in `auto-insert-alist'.

OPTIONS is a plist with three optional arguments:

- `:testfn' takes a function to test the given CONDITION against
  the already-existing ones in `auto-insert-alist'.  It defaults
  to testing the cdr of CONDITION against the cdar of each entry
  in `auto-insert-alist'.

- `:replace', if non-nil, will replace the matching entry with
  the given one.  Default: nil.

- `:after' is the third, optional argument to `define-auto-insert'."
  (declare (indent 1))
  (let ((testfn (or (plist-get options :testfn)
                    (lambda (a b)
                      (string= (cdr-safe a) (cdar b)))))
        (replace (or (plist-get options :replace) nil))
        (after (or (plist-get options :after) nil)))
    (if replace
        (progn (setq auto-insert-alist
                     (assoc-delete-all (list condition)
                                       auto-insert-alist
                                       testfn))
               (define-auto-insert condition action after))
      (unless (assoc (list condition) auto-insert-alist testfn)
        (define-auto-insert condition action after)))))

(provide 'acdw-autoinsert)
;;; acdw-autoinsert.el ends here