branch: elpa/jinja2-mode
commit 757e3fa1620e9a37212a35a5cf58a5c25d0274fd
Author: Florian Mounier <[email protected]>
Commit: Florian Mounier <[email protected]>
Add defcustom for kw and fun
---
jinja2-mode.el | 252 ++++++++++++++++++++++++++++++---------------------------
1 file changed, 131 insertions(+), 121 deletions(-)
diff --git a/jinja2-mode.el b/jinja2-mode.el
index 50e8457a33..de4906caf7 100644
--- a/jinja2-mode.el
+++ b/jinja2-mode.el
@@ -26,27 +26,39 @@
(require 'sgml-mode)
-(if (not (boundp 'jinja2-user-keywords))
- (setq jinja2-user-keywords nil))
-(if (not (boundp 'jinja2-user-functions))
- (setq jinja2-user-functions nil))
+(defgroup jinja2 nil
+ "Major mode for editing jinja2 code."
+ :prefix "jinja2-"
+ :group 'languages)
+
+(defcustom jinja2-user-keywords nil
+ "Custom keyword names"
+ :type '(repeat string)
+ :group 'jinja2)
+
+(defcustom jinja2-user-functions nil
+ "Custom function names"
+ :type '(repeat string)
+ :group 'jinja2)
+
+;; (defcustom jinja2-debug nil
+;; "Log indentation logic"
+;; :type 'boolean
+;; :group 'jinja2)
(defun jinja2-closing-keywords ()
(append
jinja2-user-keywords
- '(
- "if" "for" "block" "filter" "with"
- "raw" "macro" "autoescape" "trans" "call"
- )))
+ '("if" "for" "block" "filter" "with"
+ "raw" "macro" "autoescape" "trans" "call")))
(defun jinja2-indenting-keywords ()
(append
(jinja2-closing-keywords)
- '("else" "elif" )))
+ '("else" "elif")))
(defun jinja2-builtin-keywords ()
- '(
- "as" "autoescape" "debug" "extends"
+ '("as" "autoescape" "debug" "extends"
"firstof" "in" "include" "load"
"now" "regroup" "ssi" "templatetag"
"url" "widthratio" "elif" "true"
@@ -60,43 +72,41 @@
(defun jinja2-functions-keywords ()
(append
jinja2-user-functions
- '(
- "abs" "attr" "batch" "capitalize"
- "center" "default" "dictsort"
- "escape" "filesizeformat" "first"
- "float" "forceescape" "format"
- "groupby" "indent" "int" "join"
- "last" "length" "list" "lower"
- "pprint" "random" "replace"
- "reverse" "round" "safe" "slice"
- "sort" "string" "striptags" "sum"
- "title" "trim" "truncate" "upper"
- "urlize" "wordcount" "wordwrap" "xmlattr"
- )))
+ '("abs" "attr" "batch" "capitalize"
+ "center" "default" "dictsort"
+ "escape" "filesizeformat" "first"
+ "float" "forceescape" "format"
+ "groupby" "indent" "int" "join"
+ "last" "length" "list" "lower"
+ "pprint" "random" "replace"
+ "reverse" "round" "safe" "slice"
+ "sort" "string" "striptags" "sum"
+ "title" "trim" "truncate" "upper"
+ "urlize" "wordcount" "wordwrap" "xmlattr")))
(defun jinja2-find-open-tag ()
(if (search-backward-regexp
(rx-to-string
- `(and "{%"
- (? "-")
- (* whitespace)
- (? (group
- "end"))
- (group
- ,(append '(or)
- (jinja2-closing-keywords)
- ))
- (group
- (*? anything))
- (* whitespace)
- (? "-")
- "%}")) nil t)
+ `(and "{%"
+ (? "-")
+ (* whitespace)
+ (? (group
+ "end"))
+ (group
+ ,(append '(or)
+ (jinja2-closing-keywords)
+ ))
+ (group
+ (*? anything))
+ (* whitespace)
+ (? "-")
+ "%}")) nil t)
(if (match-string 1) ;; End tag, going on
- (let ((matches (jinja2-find-open-tag)))
- (if (string= (car matches) (match-string 2))
- (jinja2-find-open-tag)
- (list (match-string 2) (match-string 3))))
- (list (match-string 2) (match-string 3)))
+ (let ((matches (jinja2-find-open-tag)))
+ (if (string= (car matches) (match-string 2))
+ (jinja2-find-open-tag)
+ (list (match-string 2) (match-string 3))))
+ (list (match-string 2) (match-string 3)))
nil))
(defun jinja2-close-tag ()
@@ -104,12 +114,12 @@
(interactive)
(let ((open-tag (save-excursion (jinja2-find-open-tag))))
(if open-tag
- (insert
- (if (string= (car open-tag) "block")
- (format "{%% end%s%s %%}"
- (car open-tag)(nth 1 open-tag))
- (format "{%% end%s %%}"
- (match-string 2))))
+ (insert
+ (if (string= (car open-tag) "block")
+ (format "{%% end%s%s %%}"
+ (car open-tag)(nth 1 open-tag))
+ (format "{%% end%s %%}"
+ (match-string 2))))
(error "Nothing to close")))
(save-excursion (jinja2-indent-line)))
@@ -137,15 +147,15 @@
(save-excursion
(insert " #}")))
-(defconst jinja2-font-lock-comments
+(defconst jinja2-font-lock-comments
`(
(,(rx "{#"
- (* whitespace)
- (group
- (*? anything)
- )
- (* whitespace)
- "#}")
+ (* whitespace)
+ (group
+ (*? anything)
+ )
+ (* whitespace)
+ "#}")
. (1 font-lock-comment-face t))))
(defconst jinja2-font-lock-keywords-1
@@ -164,51 +174,51 @@
jinja2-font-lock-keywords-2
`(
(,(rx "{{"
- (* whitespace)
- (group
- (*? anything)
- )
- (*
- "|" (* whitespace) (*? anything))
- (* whitespace)
- "}}") (1 font-lock-variable-name-face t))
+ (* whitespace)
+ (group
+ (*? anything)
+ )
+ (*
+ "|" (* whitespace) (*? anything))
+ (* whitespace)
+ "}}") (1 font-lock-variable-name-face t))
(,(rx (group "|" (* whitespace))
- (group (+ word))
- )
+ (group (+ word))
+ )
(1 font-lock-keyword-face t)
(2 font-lock-warning-face t))
(,(rx-to-string `(and (group "|" (* whitespace))
- (group
- ,(append '(or)
- (jinja2-functions-keywords)
- ))))
+ (group
+ ,(append '(or)
+ (jinja2-functions-keywords)
+ ))))
(1 font-lock-keyword-face t)
(2 font-lock-function-name-face t)
)
(,(rx-to-string `(and word-start
- (? "end")
- ,(append '(or)
- (jinja2-indenting-keywords)
- )
- word-end)) (0 font-lock-keyword-face))
+ (? "end")
+ ,(append '(or)
+ (jinja2-indenting-keywords)
+ )
+ word-end)) (0 font-lock-keyword-face))
(,(rx-to-string `(and word-start
- ,(append '(or)
- (jinja2-builtin-keywords)
- )
- word-end)) (0 font-lock-builtin-face))
+ ,(append '(or)
+ (jinja2-builtin-keywords)
+ )
+ word-end)) (0 font-lock-builtin-face))
(,(rx (or "{%" "%}" "{%-" "-%}")) (0 font-lock-function-name-face t))
(,(rx (or "{{" "}}")) (0 font-lock-type-face t))
(,(rx "{#"
- (* whitespace)
- (group
- (*? anything)
- )
- (* whitespace)
- "#}")
+ (* whitespace)
+ (group
+ (*? anything)
+ )
+ (* whitespace)
+ "#}")
(1 font-lock-comment-face t))
(,(rx (or "{#" "#}")) (0 font-lock-comment-delimiter-face t))
- )))
+ )))
(defvar jinja2-font-lock-keywords
jinja2-font-lock-keywords-1)
@@ -216,33 +226,33 @@
(defun sgml-indent-line-num ()
"Indent the current line as SGML."
(let* ((savep (point))
- (indent-col
- (save-excursion
- (back-to-indentation)
- (if (>= (point) savep) (setq savep nil))
- (sgml-calculate-indent))))
+ (indent-col
+ (save-excursion
+ (back-to-indentation)
+ (if (>= (point) savep) (setq savep nil))
+ (sgml-calculate-indent))))
(if (null indent-col)
- 0
+ 0
(if savep
- (save-excursion indent-col)
- indent-col))))
+ (save-excursion indent-col)
+ indent-col))))
(defun jinja2-calculate-indent-backward (default)
"Return indent column based on previous lines"
(let ((indent-width sgml-basic-offset) (default (sgml-indent-line-num)))
(forward-line -1)
(if (looking-at "^[ \t]*{%-? *end") ; Don't indent after end
- (current-indentation)
+ (current-indentation)
(if (looking-at (concat "^[ \t]*{%-? *.*?{% *end" (regexp-opt
(jinja2-indenting-keywords))))
- (current-indentation)
- (if (looking-at (concat "^[ \t]*{%-? *" (regexp-opt
(jinja2-indenting-keywords)))) ; Check start tag
- (+ (current-indentation) indent-width)
- (if (looking-at "^[ \t]*<") ; Assume sgml block trust sgml
- default
- (if (bobp)
- 0
- (jinja2-calculate-indent-backward default))))))))
-
+ (current-indentation)
+ (if (looking-at (concat "^[ \t]*{%-? *" (regexp-opt
(jinja2-indenting-keywords)))) ; Check start tag
+ (+ (current-indentation) indent-width)
+ (if (looking-at "^[ \t]*<") ; Assume sgml block trust sgml
+ default
+ (if (bobp)
+ 0
+ (jinja2-calculate-indent-backward default))))))))
+
(defun jinja2-calculate-indent ()
"Return indent column"
@@ -250,18 +260,18 @@
0
(let ((indent-width sgml-basic-offset) (default (sgml-indent-line-num)))
(if (looking-at "^[ \t]*{%-? *e\\(nd\\|lse\\|lif\\)") ; Check close tag
- (save-excursion
- (forward-line -1)
- (if
- (and
- (looking-at (concat "^[ \t]*{%-? *" (regexp-opt
(jinja2-indenting-keywords))))
- (not (looking-at (concat "^[ \t]*{%-? *.*?{% *end" (regexp-opt
(jinja2-indenting-keywords))))))
- (current-indentation)
- (- (current-indentation) indent-width)))
- (if (looking-at "^[ \t]*</") ; Assume sgml end block trust sgml
- default
- (save-excursion
- (jinja2-calculate-indent-backward default)))))))
+ (save-excursion
+ (forward-line -1)
+ (if
+ (and
+ (looking-at (concat "^[ \t]*{%-? *" (regexp-opt
(jinja2-indenting-keywords))))
+ (not (looking-at (concat "^[ \t]*{%-? *.*?{% *end"
(regexp-opt (jinja2-indenting-keywords))))))
+ (current-indentation)
+ (- (current-indentation) indent-width)))
+ (if (looking-at "^[ \t]*</") ; Assume sgml end block trust sgml
+ default
+ (save-excursion
+ (jinja2-calculate-indent-backward default)))))))
(defun jinja2-indent-line ()
"Indent current line as Jinja code"
@@ -269,7 +279,7 @@
(beginning-of-line)
(let ((indent (jinja2-calculate-indent)))
(if (< indent 0)
- (setq indent 0))
+ (setq indent 0))
(indent-line-to indent)))
;;;###autoload
@@ -279,10 +289,10 @@
;; it mainly from sgml-mode font lock setting
(set (make-local-variable 'font-lock-defaults)
'((
- jinja2-font-lock-keywords
- jinja2-font-lock-keywords-1
- jinja2-font-lock-keywords-2
- jinja2-font-lock-keywords-3)
+ jinja2-font-lock-keywords
+ jinja2-font-lock-keywords-1
+ jinja2-font-lock-keywords-2
+ jinja2-font-lock-keywords-3)
nil t nil nil
(font-lock-syntactic-keywords
. sgml-font-lock-syntactic-keywords)))