summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/related/django.el
blob: f592550c9149ddb5dc2fffc4b0d2ebb39c87e027 (plain)
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
;;; django.el ---
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: Sun Nov 18 18:29:41 2007
;; Version: 0.3
;; Last-Updated: 2008-08-08T13:22:19+0200 Fri
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Simple highlighting for Django for use with mumamo.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 2, 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; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

;; Maybe there are something to get here?
;; http://github.com/cosmin/emacs-utils/tree/85cc1d2bd447cb9b2fc98e27b5f8780453e5b978/django-html-mode.el

(defconst django-indenting-keywords
  '("block" "comment" "else"
    "filter" "for" "if" "ifchanged" "ifequal"
    "ifnotequal" "spaceless" "with"))

;; (append '(or) django-indenting-keywords)
(defconst django-font-lock-keywords
  (list
   (cons (rx-to-string
          `(and
            word-start
            (or "as" "autoescape" "csrf_token" "cycle" "debug" "extends"
                "firstof" "in" "include" "load" "now" "regroup" "ssi"
                "templatetag" "url" "widthratio"
                (seq
                 (opt "end")
                 ;; (or "autoescape" "block" "comment" "cycle" "debug" "else"
                 ;;     "extends" "filter" "firstof" "for" "if" "ifchanged" "ifequal"
                 ;;     "ifnotequal" "include" "load" "now" "regroup"
                 ;;     "spaceless" "ssi" "templatetag" "url" "widthratio"
                 ;;     "with")
                 ,(append '(or) django-indenting-keywords)
                 ))
          word-end))
         font-lock-keyword-face)
   )
   "Minimal highlighting expressions for Django mode")

(defcustom django-indent-width 2
  "Indentation width for Django."
  :type 'integer
  :group 'django)

(defun django-indent-line ()
  "Indent current line as Django code.
Indent like the examples on URL
`http://docs.djangoproject.com/en/1.1/ref/templates/builtins/'."
  (save-match-data
    (let* ((indent-re (rx-to-string `(and word-start
                                          ,(append '(or "else") django-indenting-keywords))))
           (deindent-re (rx-to-string `(and word-start
                                            (or "else"
                                                (seq
                                                 "end"
                                                 ,(append '(or) django-indenting-keywords))))))
           (here (point-marker))
           (this-indentation (current-indentation))
           (this-line-start (progn (beginning-of-line) (point)))
           (prev-line-start (progn (skip-chars-backward " \t\n\r\f")
                                   (beginning-of-line)
                                   (when (< (point) this-line-start)
                                     (point))))
           (prev-indentation (if prev-line-start (current-indentation) 0))
           (shift-in (if (and prev-line-start
                              (re-search-forward indent-re (point-at-eol) t))
                         django-indent-width 0))
           (shift-out (progn
                        (goto-char this-line-start)
                        (if (re-search-forward deindent-re (point-at-eol) t)
                            (- django-indent-width) 0)))
           (new-indentation (max 0 (+ prev-indentation shift-in shift-out)))
           )
      (goto-char this-line-start)
      (cond
       ((> new-indentation this-indentation)
        (skip-chars-forward " \t")
        (indent-to new-indentation))
       ((< new-indentation this-indentation)
        (back-to-indentation)
        (delete-region this-line-start (point))
        (indent-to new-indentation)))
      (goto-char here))))

;;;###autoload
(define-derived-mode django-mode nil "Django"
  "Simple Django mode for use with mumamo.
This mode only provides syntax highlighting."
  (set (make-local-variable 'indent-line-function) 'django-indent-line)
  (setq font-lock-defaults '(django-font-lock-keywords)))

;;; Comments mode
;; (defconst django-comment-font-lock-keywords
;;   (list
;;    (cons "\\(.*\\)" (list 1 font-lock-comment-face))
;;    ))

;; (defvar django-comment-font-lock-defaults
;;   '(django-comment-font-lock-keywords t t))

;; (define-derived-mode django-comment-mode nil "Django comment"
;;   "For django comment blocks."
;;   (set (make-local-variable 'font-lock-defaults) django-comment-font-lock-defaults))

;;; Variables mode

(defconst django-variable-font-lock-keywords
  (list
   ;; Built in filters:
   (cons (rx
          "|"
          (submatch
           (or "add" "addslashes" "capfirst" "center" "cut"
               "date" "default" "default_if_none"
               "dictsort" "dictsortreversed"
               "divisibleby"
               "escape"
               "filesizeformat"
               "first"
               "fixampersands"
               "floatformat"
               "force_escape"
               "iriencode"
               "join"
               "length" "length_is"
               "linebreaks" "linebreaksbr" "linenumbers"
               "ljust"
               "lower"
               "make_list"
               "phone2numeric"
               "pluralize"
               "pprint"
               "random"
               "removetags"
               "rjust"
               "safe" "slice" "slugify" "stringformat" "striptags"
               "time" "timesince" "timeuntil"
               "title" "truncatewords" "truncatewords_html"
               "unordered_list"
               "upper" "urlencode" "urlize" "urlizetrunc"
               "wordcount" "wordwrap" "yesno")))
         (list 1 font-lock-builtin-face))
   (cons (rx
          "|"
          (submatch
           (0+ (any "a-z"))))
         (list 1 font-lock-function-name-face))
   (cons "\\([^|]*\\)" (list 1 font-lock-variable-name-face))
   ))

(defvar django-variable-font-lock-defaults
  '(django-variable-font-lock-keywords
    t t
    ;; This still gives teh syntax symbol to |, why?
    ((?| . ". "))
    ))

(define-derived-mode django-variable-mode nil "Django variable"
  "For django comment blocks."
  ;;(modify-syntax-entry ?| ?.)
  (set (make-local-variable 'font-lock-defaults) django-variable-font-lock-defaults))

(provide 'django)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; django.el ends here