forked from minad/tempel-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempel-collection.el
109 lines (93 loc) · 3.95 KB
/
tempel-collection.el
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
;;; tempel-collection.el --- Collection of templates for Tempel
;; Copyright (C) 2022 Tempel collection contributors
;; Author: Vitalii Drevenchuk <[email protected]>,
;; Max Penet <[email protected]>,
;; Daniel Mendler <[email protected]>
;; Maintainer: Vitalii Drevenchuk <[email protected]>,
;; Max Penet <[email protected]>,
;; Daniel Mendler <[email protected]>
;; Keywords: tools
;; Version: 0.2
;; Package-Requires: ((tempel "0.5") (emacs "29.1"))
;; Homepage: https://github.com/Crandel/tempel-collection
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package registers a collection of templates with Tempel.
;;; Code:
(require 'tempel)
(eval-when-compile
(require 'cl-lib)
(require 'subr-x))
(defconst tempel-collection--dir
(expand-file-name
"templates/"
(file-name-directory
(cond
(load-in-progress load-file-name)
((and (boundp 'byte-compile-current-file) byte-compile-current-file)
byte-compile-current-file)
(:else (buffer-file-name))))))
(defvar tempel-collection--templates nil)
(defvar tempel-collection--loaded nil)
;; Maps directly to mode names as stored in <mode-name>.eld file, as
;; some ts modes do not have a counterpart non-ts mode in Emacs core,
;; e.g., rust-mode. Do not alias ts modes which have a common parent
;; mode with their non-ts mode. For example, python-ts-mode and
;; python-mode have a common parent, python-base-mode. Thus add python
;; templates to python-base.eld. Note that in the lisp data template file,
;; the alias mode must be enabled, too.
(defvar tempel-collection--aliases
'((c++-ts-mode . "c++")
(c-ts-mode . "c")
(cmake-ts-mode . "cmake")
(csharp-ts-mode . "csharp")
(dockerfile-ts-mode . "dockerfile")
(go-ts-mode . "go")
(go-mod-ts-mode . "go-mod")
(java-ts-mode . "java")
(js-ts-mode . "js")
(json-ts-mode . "json")
(rust-ts-mode . "rust")
(toml-ts-mode . "toml")
(typescript-ts-base-mode . "typescript")
(yaml-ts-mode . "yaml")))
(defun tempel-collection--mode-file (mode-name)
"Get the file name for the templates of MODE-NAME, if it exists."
(let ((file (format "%s%s.eld" tempel-collection--dir mode-name)))
(if (file-exists-p file)
file
nil)))
;;;###autoload
(defun tempel-collection ()
"Template loader."
(let ((mode major-mode))
(while (and mode (not (memq mode tempel-collection--loaded)))
(push mode tempel-collection--loaded)
(let ((file
(or (tempel-collection--mode-file (downcase (string-remove-suffix "-mode" (symbol-name mode))))
(tempel-collection--mode-file (alist-get mode tempel-collection--aliases)))))
(when file
(setq tempel-collection--templates
(nconc (tempel--file-read file)
tempel-collection--templates))))
(setq mode (and (not (eq mode #'fundamental-mode))
(or (get mode 'derived-mode-parent)
#'fundamental-mode)))))
;; TODO code duplication with tempel-path-templates
(cl-loop for (modes plist . templates) in tempel-collection--templates
if (tempel--condition-p modes plist)
append templates))
;;;###autoload
(with-eval-after-load 'tempel
(add-to-list 'tempel-template-sources 'tempel-collection 'append))
(provide 'tempel-collection)
;;; tempel-collection.el ends here