-
Notifications
You must be signed in to change notification settings - Fork 0
/
django-html-mode.el
487 lines (414 loc) · 16 KB
/
django-html-mode.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
;;; django-html-mode.el --- Major mode for editing Django HTML templates
;; Author: Eduardo de Oliviera Padoan <edcrypt@gmail.com>
;; Michael J. Korman <mike@mkorman.org>
;; Török Gábor <gabor@20y.hu>
;; Unknown Original Author
;; Keywords: languages
;;; Commentary:
;;
;; This django-html-mode is mainly derived from html-mode.
;;; History:
;;
;; TODO: Make comment-region work with Django comments instead of HTML comments
(require 'sgml-mode)
;;; Code:
(defgroup django-html nil
"Customizations for `django-html-mode'."
:prefix "django-html-"
:group 'django)
(defvar django-html-mode-hook nil
"List of functions to be executed on entry to `django-html-mode'.")
(defvar django-html-mode-map
(let ((django-html-mode-map (make-keymap)))
(define-key django-html-mode-map "\C-c\C-dj" 'newline-and-indent)
(define-key django-html-mode-map "\C-c\C-d]" 'django-html-close-tag)
(define-key django-html-mode-map "\C-c\C-di" 'django-html-insert-tag)
django-html-mode-map)
"Keymap for Django major mode.")
;; if : if, if not, if A or B, if not A or B, if not A and B
;; for : for a in alist reversed
;; forloop.counter The current iteration of the loop (1-indexed)
;; forloop.counter0 The current iteration of the loop (0-indexed)
;; forloop.revcounter The number of iterations from the end of the loop
;; (1-indexed)
;; forloop.revcounter0 The number of iterations from the end of the loop
;; (0-indexed)
;; forloop.first True if this is the first time through the loop
;; forloop.last True if this is the last time through the loop
;; forloop.parentloop For nested loops, this is the loop "above" the
;; current one
;; ifequal : ifequal A B
;; comment : {% This is comment %}
;; filter : {{ name | lower }}
;; keyword-end : if, for, ifequal, block, ifnotequal, spaceless
;; keyword-3 : regroup
;; keyword-2 : for, ifequal
;; keyword-1 : if, block, extends, include, ifchanged, load, now, ssi, withratio
;; keyword-0 : else, spaceless
(defconst django-html-open-block "{%"
"Start keyword for template blocks.")
(defconst django-html-close-block "%}"
"End keyword for template blocks.")
(defconst django-html-open-comment "{#"
"Start keyword for template comments.")
(defconst django-html-close-comment "#}"
"End keyword for template comments.")
(defconst django-html-open-variable "{{"
"Start keyword for template variables.")
(defconst django-html-close-variable "}}"
"End keyword for template variables.")
(defconst django-html-font-lock-keywords-1
(append
;; html-mode keyword
sgml-font-lock-keywords-1)
"First level keyword highlighting.")
(defconst django-html-font-lock-keywords-2
(append
django-html-font-lock-keywords-1
sgml-font-lock-keywords-2))
(defconst django-html-font-lock-keywords-3
(append
django-html-font-lock-keywords-1
django-html-font-lock-keywords-2
`(;; comment
(,(rx (eval django-html-open-comment)
(1+ space)
(0+ (not (any "#")))
(1+ space)
(eval django-html-close-comment))
. font-lock-comment-face)
;; variable font lock
(,(rx (eval django-html-open-variable)
(1+ space)
(group (0+ (not (any "}"))))
(1+ space)
(eval django-html-close-variable))
(1 font-lock-variable-name-face))
;; start, end keyword font lock
(,(rx (group (or (eval django-html-open-block)
(eval django-html-close-block)
(eval django-html-open-comment)
(eval django-html-close-comment)
(eval django-html-open-variable)
(eval django-html-close-variable))))
(1 font-lock-builtin-face))
;; end prefix keyword font lock
(,(rx (eval django-html-open-block)
(1+ space)
(group (and "end"
;; end prefix keywords
(or "autoescape" "block" "blocktrans" "cache" "comment"
"filter" "for" "if" "ifchanged" "ifequal"
"ifnotequal" "spaceless" "trans" "with")))
(1+ space)
(eval django-html-close-block))
(1 font-lock-keyword-face))
;; more words after keyword
(,(rx (eval django-html-open-block)
(1+ space)
(group (or "autoescape" "block" "blocktrans" "cache" "comment"
"cycle" "debug" "else" "empty" "extends" "filter" "firstof" "for"
"if" "ifchanged" "ifequal" "ifnotequal" "include"
"load" "now" "regroup" "spaceless" "ssi" "templatetag"
"trans" "url" "widthratio" "with"))
;; TODO: is there a more beautiful way?
(0+ (not (any "}")))
(1+ space)
(eval django-html-close-block))
(1 font-lock-keyword-face))
;; TODO: if specific cases for supporting "or", "not", and "and"
;; for sepcific cases for supporting in
(,(rx (eval django-html-open-block)
(1+ space)
"for"
(1+ space)
(group (1+ (or word ?_ ?.)))
(1+ space)
(group "in")
(1+ space)
(group (1+ (or word ?_ ?.)))
(group (? (1+ space) "reverse"))
(1+ space)
(eval django-html-close-block))
(1 font-lock-variable-name-face) (2 font-lock-keyword-face)
(3 font-lock-variable-name-face) (4 font-lock-keyword-face)))))
(defvar django-html-font-lock-keywords
django-html-font-lock-keywords-1)
(defvar django-html-mode-syntax-table
(let ((django-html-mode-syntax-table (make-syntax-table)))
django-html-mode-syntax-table)
"Syntax table for django-html-mode.")
;;; Auto-close tags
(defvar django-html-closable-tags
'("autoescape" "blocktrans" "block" "cache"
"comment" "filter" "for" "ifchanged"
"ifequal" "ifnotequal" "if" "spaceless"
"with"))
;;; Non-auto close tags
(defvar django-html-nonclosable-tags
'("cycle" "debug" "empty" "extends" "firstof" "include"
"load" "now" "regroup" "ssi" "templatetag"
"url" "widthratio"))
(defvar django-html-all-tags
(append django-html-closable-tags django-html-nonclosable-tags))
(defvar django-html-tag-re
(concat
django-html-open-block
"\\s *\\(end\\)?\\("
(mapconcat 'identity django-html-closable-tags "\\|")
"\\)[^%]*"
django-html-close-block))
;;;###autoload
(define-derived-mode django-html-mode html-mode "django-html"
"Major mode for editing Django html templates (.djhtml).
\\{django-html-mode-map}"
:group 'django-html
;; it mainly from sgml-mode font lock setting
(set (make-local-variable 'font-lock-defaults)
'((django-html-font-lock-keywords
django-html-font-lock-keywords-1
django-html-font-lock-keywords-2
django-html-font-lock-keywords-3)
nil t nil nil
(font-lock-syntactic-keywords
. sgml-font-lock-syntactic-keywords))))
(add-to-list 'auto-mode-alist '("\\.djhtml$" . django-html-mode))
(defun django-html-find-open-tag ()
"Return open tag for closed template tag.
If tags are unbalanced, raise error."
(if (search-backward-regexp django-html-tag-re nil t)
(if (match-string 1) ; If it's an end tag
(if (not (string= (match-string 2) (django-html-find-open-tag)))
(error "Unmatched Django tag")
(django-html-find-open-tag))
(match-string 2)) ; Otherwise, return the match
nil))
(defun django-html-close-tag ()
"Close the previously opened template tag."
(interactive)
(let ((open-tag (save-excursion (django-html-find-open-tag))))
(if open-tag
(insert
(format "%s end%s %s"
django-html-open-block open-tag django-html-close-block))
(error "Nothing to close"))))
(define-skeleton django-html-closing-template
"Insert a generic template with a closing tag." nil
django-html-open-block " " str " " django-html-close-block
_
django-html-open-block " " "end" str " " django-html-close-block)
(define-skeleton django-html-nonclosing-template
"Insert a generic template without a closing tag." nil
django-html-open-block " " str " " django-html-close-block)
(defun django-html-make-opening-tag (tag)
(format "%s %s %s"
django-html-open-block
tag
django-html-close-block))
(defun django-html-make-closing-tag (tag)
(django-html-make-opening-tag
(concat "end" tag)))
;;;; Skeletons for inserting tags.
;; TODO: regroup tag. This has a more complicated syntax.
;; TODO: url tag. Maybe this should read URLs from the URLconf?
;; TODO: auto-complete filters.
(define-skeleton django-html-autoescape-template
"Insert \"autoescape\" template." nil
(let ((on-or-off (if (y-or-n-p "autoescape on? ")
"on" "off")))
(format "%s autoescape %s %s"
django-html-open-block
on-or-off
django-html-close-block)))
(define-skeleton django-html-for-template
"Insert \"for\" template." nil
(format "%s for %s in %s %s"
django-html-open-block
(read-string "item: ")
(read-string "array: ")
django-html-close-block) ?\n
_ ?\n
(when (y-or-n-p "\"empty\" clause? ")
(django-html-make-opening-tag "empty")) ?\n
(django-html-make-closing-tag "for"))
(define-skeleton django-html-if-template
"Insert \"if\" template." nil
(format "%s if %s "
django-html-open-block
(setq v1 (skeleton-read "condition: ")))
(if (string= "" v1) -1)
django-html-close-block ?\n
_ ?\n
(when (y-or-n-p "\"else\" clause? ")
(django-html-make-opening-tag "else")) ?\n
(django-html-make-closing-tag "if"))
(define-skeleton django-html-ifequal-template
"Insert \"ifequal\" template." nil
(format "%s ifequal %s %s %s "
django-html-open-block
(read-string "variable 1: ")
(read-string "variable 2: ")
django-html-close-block) ?\n
_ ?\n
(when (y-or-n-p "\"else\" clause? ")
(django-html-make-opening-tag "else")) ?\n
(django-html-make-closing-tag "ifequal"))
(define-skeleton django-html-ifnotequal-template
"Insert \"ifnotequal\" template." nil
(format "%s ifnotequal %s %s %s "
django-html-open-block
(read-string "variable 1: ")
(read-string "variable 2: ")
django-html-close-block) ?\n
_ ?\n
(when (y-or-n-p "\"else\" clause? ")
(django-html-make-opening-tag "else")) ?\n
(django-html-make-closing-tag "ifnotequal"))
(define-skeleton django-html-include-template
"Insert \"include\" template." nil
(format "%s include " django-html-open-block)
(read-string "template: ")
" " django-html-close-block)
(define-skeleton django-html-load-template
"Insert \"load\" template." nil
(format "%s load " django-html-open-block)
(read-string "module: ")
" " django-html-close-block)
(define-skeleton django-html-now-template
"Insert \"now\" template." nil
(format "%s now " django-html-open-block)
"\"" (read-string "format string: ") "\""
" " django-html-close-block)
(define-skeleton django-html-ssi-template
"Insert \"ssi\" template." nil
(format "%s ssi " django-html-open-block)
(read-string "file: ")
" "
(if (y-or-n-p "parsed? ")
"parsed ")
django-html-close-block)
(define-skeleton django-html-templatetag-template
"Insert \"templatetag\" template." nil
(format "%s templatetag " django-html-open-block)
(completing-read "template tag (TAB for completion): "
'("openblock" "closeblock" "openvariable"
"closevariable" "openbrace" "closebrace"
"opencomment" "closecomment") nil t)
" "
django-html-close-block)
(define-skeleton django-html-widthratio-template
"Insert \"widthratio\" template." nil
(format "%s widthratio %s %s %s %s" django-html-open-block
(read-string "given value: ")
(read-string "max value: ")
(read-string "constant: ")
django-html-close-block))
(define-skeleton django-html-with-template
"Insert \"with\" template." nil
(format "%s with %s as %s %s"
django-html-open-block
(read-string "variable: ")
(read-string "alias: ")
django-html-close-block)
_
(django-html-make-closing-tag "with"))
(define-skeleton django-html-block-template
"Insert \"block\" template." nil
(let ((block-name (read-string "block: ")))
(format "%s block %s %s"
django-html-open-block
block-name
django-html-close-block)) ?\n
_ ?\n
(django-html-make-closing-tag "block"))
(define-skeleton django-html-cycle-template
"Insert \"cycle\" template." nil
(format "%s cycle " django-html-open-block)
("item: " str " ") -1
" as "
(setq v1 (skeleton-read "name: "))
(if (string= "" v1) -4) " " django-html-close-block)
(define-skeleton django-html-extends-template
"Insert \"extends\" template." nil
(format "%s extends " django-html-open-block)
(read-string "parent: ")
" " django-html-close-block)
(define-skeleton django-html-filter-template
"Insert \"filter\" template." nil
(format "%s filter " django-html-open-block)
("filter: " str "|") -1
" " django-html-close-block)
(define-skeleton django-html-firstof-template
"Insert \"firstof\" template." nil
(format "%s firstof " django-html-open-block)
("item: " str " ") -1
" \"" (setq v1 (skeleton-read "fallback value: ")) "\""
(if (string= "" v1) -3)
" " django-html-close-block)
(defun django-html-insert-tag ()
"Prompts the user for a tag, and inserts opening and closing tags."
(interactive)
(let ((tag (completing-read "Tag (TAB for completion): " django-html-all-tags)))
(cond ((string= tag "autoescape")
(django-html-autoescape-template))
((string= tag "cycle")
(django-html-cycle-template))
((string= tag "extends")
(django-html-extends-template))
((string= tag "filter")
(django-html-filter-template))
((string= tag "firstof")
(django-html-firstof-template))
((string= tag "for")
(django-html-for-template))
((string= tag "if")
(django-html-if-template))
((string= tag "ifequal")
(django-html-ifequal-template))
((string= tag "ifnotequal")
(django-html-ifnotequal-template))
((string= tag "include")
(django-html-include-template))
((string= tag "load")
(django-html-load-template))
((string= tag "now")
(django-html-now-template))
((string= tag "ssi")
(django-html-ssi-template))
((string= tag "templatetag")
(django-html-templatetag-template))
((string= tag "widthratio")
(django-html-widthratio-template))
((string= tag "with")
(django-html-with-template))
((string= tag "block")
(django-html-block-template))
((member tag django-html-closable-tags)
(django-html-closing-template tag))
(t
(django-html-nonclosing-template tag)))))
(easy-menu-define django-html-menu django-html-mode-map "Django-HTML menu"
'("Django-HTML"
["Insert Tag" django-html-insert-tag t]
["Auto-close Tag" django-html-close-tag t]
("Tag Templates"
["autoescape" django-html-autoescape-template t]
["block" django-html-block-template t]
["cycle" django-html-cycle-template t]
["extends" django-html-extends-template t]
["filter" django-html-filter-template t]
["firstof" django-html-firstof-template t]
["for" django-html-for-template t]
["if" django-html-if-template t]
["ifequal" django-html-ifequal-template t]
["ifnotequal" django-html-ifnotequal-template t]
["include" django-html-include-template t]
["load" django-html-load-template t]
["now" django-html-now-template t]
["ssi" django-html-ssi-template t]
["templatetag" django-html-templatetag-template t]
["widthratio" django-html-widthratio-template t]
["with" django-html-with-template t])))
(easy-menu-add django-html-menu django-html-mode-map)
(provide 'django-html-mode)
;;; django-html-mode.el ends here