Run `./2to3.py -w jinja2`
[jinja2.git] / ext / jinja.el
1 ;;; jinja.el --- Jinja mode highlighting
2 ;;
3 ;; Author: Georg Brandl
4 ;; Copyright: (c) 2009 by the Jinja Team
5 ;; Last modified: 2008-05-22 23:04 by gbr
6 ;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;
9 ;;; Commentary:
10 ;;
11 ;; Mostly ripped off django-mode by Lennart Borgman.
12 ;;
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 ;;
15 ;; This program is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License as
17 ;; published by the Free Software Foundation; either version 2, or
18 ;; (at your option) any later version.
19 ;;
20 ;; This program is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 ;; General Public License for more details.
24 ;;
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program; see the file COPYING.  If not, write to
27 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
28 ;; Floor, Boston, MA 02110-1301, USA.
29 ;;
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;;
32 ;;; Code:
33
34 (defconst jinja-font-lock-keywords
35   (list
36 ;   (cons (rx "{% comment %}" (submatch (0+ anything))
37 ;             "{% endcomment %}") (list 1 font-lock-comment-face))
38    '("{# ?\\(.*?\\) ?#}" . (1 font-lock-comment-face))
39    '("{%-?\\|-?%}\\|{{\\|}}" . font-lock-preprocessor-face)
40    '("{#\\|#}" . font-lock-comment-delimiter-face)
41    ;; first word in a block is a command
42    '("{%-?[ \t\n]*\\([a-zA-Z_]+\\)" . (1 font-lock-keyword-face))
43    ;; variables
44    '("\\({{ ?\\)\\([^|]*?\\)\\(|.*?\\)? ?}}" . (1 font-lock-variable-name-face))
45    ;; keywords and builtins
46    (cons (rx word-start
47              (or "in" "as" "recursive" "not" "and" "or" "if" "else"
48                  "import" "with" "without" "context")
49              word-end)
50          font-lock-keyword-face)
51    (cons (rx word-start
52              (or "true" "false" "none" "loop" "self" "super")
53              word-end)
54          font-lock-builtin-face)
55    ;; tests
56    '("\\(is\\)[ \t]*\\(not\\)[ \t]*\\([a-zA-Z_]+\\)"
57      (1 font-lock-keyword-face) (2 font-lock-keyword-face)
58      (3 font-lock-function-name-face))
59    ;; builtin filters
60    (cons (rx
61           "|" (* space)
62           (submatch
63            (or "abs" "batch" "capitalize" "capture" "center" "count" "default"
64                "dformat" "dictsort" "e" "escape" "filesizeformat" "first"
65                "float" "format" "getattribute" "getitem" "groupby" "indent"
66                "int" "join" "jsonencode" "last" "length" "lower" "markdown"
67                "pprint" "random" "replace" "reverse" "round" "rst" "slice"
68                "sort" "string" "striptags" "sum" "textile" "title" "trim"
69                "truncate" "upper" "urlencode" "urlize" "wordcount" "wordwrap"
70                "xmlattr")))
71          (list 1 font-lock-builtin-face))
72    )
73    "Minimal highlighting expressions for Jinja mode")
74
75 (define-derived-mode jinja-mode nil "Jinja"
76   "Simple Jinja mode for use with `mumamo-mode'.
77 This mode only provides syntax highlighting."
78   ;;(set (make-local-variable 'comment-start) "{#")
79   ;;(set (make-local-variable 'comment-end)   "#}")
80   (setq font-lock-defaults '(jinja-font-lock-keywords)))
81
82 ;; mumamo stuff
83 (when (require 'mumamo nil t)
84
85   (defun mumamo-chunk-jinja3 (pos max)
86     "Find {# ... #}"
87     (mumamo-quick-chunk-forward pos max "{#" "#}" 'borders 'jinja-mode))
88
89   (defun mumamo-chunk-jinja2 (pos max)
90     "Find {{ ... }}"
91     (mumamo-quick-chunk-forward pos max "{{" "}}" 'borders 'jinja-mode))
92
93   (defun mumamo-chunk-jinja (pos max)
94     "Find {% ... %}"
95     (mumamo-quick-chunk-forward pos max "{%" "%}" 'borders 'jinja-mode))
96
97 ;;;###autoload
98   (define-mumamo-multi-major-mode jinja-html-mumamo
99     "Turn on multiple major modes for Jinja with main mode `html-mode'.
100 This also covers inlined style and javascript."
101     ("Jinja HTML Family" html-mode
102      (mumamo-chunk-jinja
103       mumamo-chunk-jinja2
104       mumamo-chunk-jinja3
105       mumamo-chunk-inlined-style
106       mumamo-chunk-inlined-script
107       mumamo-chunk-style=
108       mumamo-chunk-onjs=
109       )))
110
111 ;;;###autoload
112   (define-mumamo-multi-major-mode jinja-nxhtml-mumamo
113     "Turn on multiple major modes for Jinja with main mode `nxhtml-mode'.
114 This also covers inlined style and javascript."
115     ("Jinja nXhtml Family" nxhtml-mode
116      (mumamo-chunk-jinja
117       mumamo-chunk-jinja2
118       mumamo-chunk-jinja3
119       mumamo-chunk-inlined-style
120       mumamo-chunk-inlined-script
121       mumamo-chunk-style=
122       mumamo-chunk-onjs=
123       )))
124   )
125
126 (provide 'jinja)
127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128 ;;; jinja.el ends here