-
Notifications
You must be signed in to change notification settings - Fork 2
/
twig.el
208 lines (178 loc) · 4.93 KB
/
twig.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
;;###autoload
(defgroup twig nil
"Convenience functions for working with Twig"
:prefix "twig-"
:group 'convenience
)
(defun twig-trans-text (text)
"print-a-text-in-a-twig-trans-label"
(interactive "sText:")
(insert (concat "{% trans %}" text "{% endtrans %}"))
)
(defun twig-trans-region ()
"include-a-region-between-trans-labels"
(interactive)
"print-a-text-in-a-twig-trans-label"
(goto-char (region-end))
(insert "{% endtrans %}")
(goto-char (region-beginning))
(insert "{% trans %}")
)
(defun twig-create-block (block-name)
"create-an-empty-block"
(interactive "sBlock-Name:")
(insert (concat "{% block " block-name " %}"))
(newline)
(newline)
(insert (concat "{% endblock " block-name " %}"))
(previous-line)
)
(defun twig-for (variable in-variable create-else)
"create-a-for-block"
(interactive
(list
(read-string "Variable:")
(read-string "In variable:")
(y-or-n-p "Create else statement?")
))
(insert (concat "{% for " variable " in " in-variable " %}"))
(newline)
(setq point-to-insert (point))
(newline)
(if create-else
(progn
(insert "{% else %}")
(newline)
)
)
(insert (concat "{% endfor %}"))
(goto-char point-to-insert)
(indent-according-to-mode)
)
(defun twig-insert-variable (var-name)
"insert-a-variable"
(interactive
(list
(read-string "Variable:")
))
(insert (concat "{{ " var-name " }}"))
)
(defun twig-insert-function (function-name)
"insert-a-function"
(interactive
(list
(read-string "Function:")
))
(insert (concat "{% " function-name " %}"))
)
(defun twig-insert-condition (condition-param create-else)
"insert-a-condition"
(interactive
(list
(read-string "Condition:")
(y-or-n-p "Insert else statement?")
))
(insert (concat "{% if " condition-param " %}"))
(newline)
(setq point-to-insert (point))
(newline)
(if create-else
(progn
(insert "{% else %}")
(newline)
)
)
(insert (concat "{% endfif %}"))
(goto-char point-to-insert)
(indent-according-to-mode)
)
(defvar twig-mode-keymap (make-keymap)
"keymappings for twig-mode"
)
(define-key twig-mode-keymap
(kbd "C-c C-t t")
'twig-trans-text
)
(define-key twig-mode-keymap
(kbd "C-c C-t r")
'twig-trans-region
)
(define-key twig-mode-keymap
(kbd "C-c C-t b")
'twig-create-block
)
(define-key twig-mode-keymap
(kbd "C-c C-t f")
'twig-for
)
(define-key twig-mode-keymap
(kbd "C-c C-t v")
'twig-insert-variable
)
(define-key twig-mode-keymap
(kbd "C-c C-t x")
'twig-insert-function
)
(define-key twig-mode-keymap
(kbd "C-c C-t i")
'twig-insert-condition
)
;;Make a menu
(define-key twig-mode-keymap [menu-bar] (make-sparse-keymap))
(define-key twig-mode-keymap [menu-bar twig]
(cons "Twig" (make-sparse-keymap "Twig")))
;; Define specific subcommands in this menu.
(define-key twig-mode-keymap [menu-bar twig trans-text]
'("Insert Translate Block" . twig-trans-text))
(define-key twig-mode-keymap [menu-bar twig trans-region]
'("Translate Region" . twig-trans-region))
(define-key twig-mode-keymap [menu-bar twig create-block]
'("Create A New Block" . twig-create-block))
(define-key twig-mode-keymap [menu-bar twig for]
'("Insert For Loop" . twig-for))
(define-key twig-mode-keymap [menu-bar twig insert-variable]
'("Insert Variable" . twig-insert-variable))
(define-key twig-mode-keymap [menu-bar twig insert-function]
'("Insert Function" . twig-insert-function))
(define-key twig-mode-keymap [menu-bar twig insert-condition]
'("Insert Condition" . twig-insert-condition))
;; (defvar twig-keywords
;; '(
;; ("{% | %}" 1 font-lock-type-face)
;; ("title=\\|re[lv]=\\|h\\(ref=\\|ttp-equiv=\\)\\|content=\\|name=" . font-lock-variable-name-face)
;; ;; ("<\\(a\\)" 1 font-lock-function-name-face)
;; ;; ("\\(/a\\)>" 1 font-lock-function-name-face)
;; ;; ("\t" . 'show-paren-mismatch-face)
;; )
;; )
;;;###autoload
;; (define-derived-mode twig-mode html-mode "HTML"
;; (set (make-local-variable 'comment-start) "{# ")
;; (set (make-local-variable 'comment-end) " #}")
;; ;;(font-lock-add-keywords 'html-mode '(twig-keywords))
;; ;;(setq font-lock-defaults '(twig-keywords))
;; (twig-minor-mode-on)
;; )
(defvar twig-minor-mode-previous-comments nil
"Storage for comment start/end that was before twig mode was enabled")
(define-minor-mode twig-minor-mode "twig" :lighter "[twig]"
:keymap twig-mode-keymap
:group 'twig
(unless twig-minor-mode-previous-comments
(set (make-local-variable 'twig-minor-mode-previous-comments) (cons comment-start comment-end)))
(if twig-minor-mode
(progn
(setq comment-start "{#")
(setq comment-end "#}"))
(setq comment-start (car twig-minor-mode-previous-comments))
(setq comment-end (cdr twig-minor-mode-previous-comments))))
(defun twig-minor-mode-on ()
(interactive)
(twig-minor-mode t)
)
(defun twig-minor-mode-off ()
(interactive)
(twig-minor-mode nil)
)
(provide 'twig)
;;;twig.el ends here