]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/nxhtml/related/django.el
remove toolbar and menubar
[.emacs.d.git] / emacs / nxhtml / related / django.el
1 ;;; django.el ---
2 ;;
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: Sun Nov 18 18:29:41 2007
5 ;; Version: 0.3
6 ;; Last-Updated: 2008-08-08T13:22:19+0200 Fri
7 ;; URL:
8 ;; Keywords:
9 ;; Compatibility:
10 ;;
11 ;; Features that might be required by this library:
12 ;;
13 ;;   None
14 ;;
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 ;;
17 ;;; Commentary:
18 ;;
19 ;; Simple highlighting for Django for use with mumamo.
20 ;;
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 ;;
23 ;;; Change log:
24 ;;
25 ;;
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;
28 ;; This program is free software; you can redistribute it and/or
29 ;; modify it under the terms of the GNU General Public License as
30 ;; published by the Free Software Foundation; either version 2, or
31 ;; (at your option) any later version.
32 ;;
33 ;; This program is distributed in the hope that it will be useful,
34 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
35 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
36 ;; General Public License for more details.
37 ;;
38 ;; You should have received a copy of the GNU General Public License
39 ;; along with this program; see the file COPYING.  If not, write to
40 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
41 ;; Floor, Boston, MA 02110-1301, USA.
42 ;;
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;;
45 ;;; Code:
46
47 ;; Maybe there are something to get here?
48 ;; http://github.com/cosmin/emacs-utils/tree/85cc1d2bd447cb9b2fc98e27b5f8780453e5b978/django-html-mode.el
49
50 (defconst django-indenting-keywords
51   '("block" "comment" "else"
52     "filter" "for" "if" "ifchanged" "ifequal"
53     "ifnotequal" "spaceless" "with"))
54
55 ;; (append '(or) django-indenting-keywords)
56 (defconst django-font-lock-keywords
57   (list
58    (cons (rx-to-string
59           `(and
60             word-start
61             (or "as" "autoescape" "csrf_token" "cycle" "debug" "extends"
62                 "firstof" "in" "include" "load" "now" "regroup" "ssi"
63                 "templatetag" "url" "widthratio"
64                 (seq
65                  (opt "end")
66                  ;; (or "autoescape" "block" "comment" "cycle" "debug" "else"
67                  ;;     "extends" "filter" "firstof" "for" "if" "ifchanged" "ifequal"
68                  ;;     "ifnotequal" "include" "load" "now" "regroup"
69                  ;;     "spaceless" "ssi" "templatetag" "url" "widthratio"
70                  ;;     "with")
71                  ,(append '(or) django-indenting-keywords)
72                  ))
73           word-end))
74          font-lock-keyword-face)
75    )
76    "Minimal highlighting expressions for Django mode")
77
78 (defcustom django-indent-width 2
79   "Indentation width for Django."
80   :type 'integer
81   :group 'django)
82
83 (defun django-indent-line ()
84   "Indent current line as Django code.
85 Indent like the examples on URL
86 `http://docs.djangoproject.com/en/1.1/ref/templates/builtins/'."
87   (save-match-data
88     (let* ((indent-re (rx-to-string `(and word-start
89                                           ,(append '(or "else") django-indenting-keywords))))
90            (deindent-re (rx-to-string `(and word-start
91                                             (or "else"
92                                                 (seq
93                                                  "end"
94                                                  ,(append '(or) django-indenting-keywords))))))
95            (here (point-marker))
96            (this-indentation (current-indentation))
97            (this-line-start (progn (beginning-of-line) (point)))
98            (prev-line-start (progn (skip-chars-backward " \t\n\r\f")
99                                    (beginning-of-line)
100                                    (when (< (point) this-line-start)
101                                      (point))))
102            (prev-indentation (if prev-line-start (current-indentation) 0))
103            (shift-in (if (and prev-line-start
104                               (re-search-forward indent-re (point-at-eol) t))
105                          django-indent-width 0))
106            (shift-out (progn
107                         (goto-char this-line-start)
108                         (if (re-search-forward deindent-re (point-at-eol) t)
109                             (- django-indent-width) 0)))
110            (new-indentation (max 0 (+ prev-indentation shift-in shift-out)))
111            )
112       (goto-char this-line-start)
113       (cond
114        ((> new-indentation this-indentation)
115         (skip-chars-forward " \t")
116         (indent-to new-indentation))
117        ((< new-indentation this-indentation)
118         (back-to-indentation)
119         (delete-region this-line-start (point))
120         (indent-to new-indentation)))
121       (goto-char here))))
122
123 ;;;###autoload
124 (define-derived-mode django-mode nil "Django"
125   "Simple Django mode for use with mumamo.
126 This mode only provides syntax highlighting."
127   (set (make-local-variable 'indent-line-function) 'django-indent-line)
128   (setq font-lock-defaults '(django-font-lock-keywords)))
129
130 ;;; Comments mode
131 ;; (defconst django-comment-font-lock-keywords
132 ;;   (list
133 ;;    (cons "\\(.*\\)" (list 1 font-lock-comment-face))
134 ;;    ))
135
136 ;; (defvar django-comment-font-lock-defaults
137 ;;   '(django-comment-font-lock-keywords t t))
138
139 ;; (define-derived-mode django-comment-mode nil "Django comment"
140 ;;   "For django comment blocks."
141 ;;   (set (make-local-variable 'font-lock-defaults) django-comment-font-lock-defaults))
142
143 ;;; Variables mode
144
145 (defconst django-variable-font-lock-keywords
146   (list
147    ;; Built in filters:
148    (cons (rx
149           "|"
150           (submatch
151            (or "add" "addslashes" "capfirst" "center" "cut"
152                "date" "default" "default_if_none"
153                "dictsort" "dictsortreversed"
154                "divisibleby"
155                "escape"
156                "filesizeformat"
157                "first"
158                "fixampersands"
159                "floatformat"
160                "force_escape"
161                "iriencode"
162                "join"
163                "length" "length_is"
164                "linebreaks" "linebreaksbr" "linenumbers"
165                "ljust"
166                "lower"
167                "make_list"
168                "phone2numeric"
169                "pluralize"
170                "pprint"
171                "random"
172                "removetags"
173                "rjust"
174                "safe" "slice" "slugify" "stringformat" "striptags"
175                "time" "timesince" "timeuntil"
176                "title" "truncatewords" "truncatewords_html"
177                "unordered_list"
178                "upper" "urlencode" "urlize" "urlizetrunc"
179                "wordcount" "wordwrap" "yesno")))
180          (list 1 font-lock-builtin-face))
181    (cons (rx
182           "|"
183           (submatch
184            (0+ (any "a-z"))))
185          (list 1 font-lock-function-name-face))
186    (cons "\\([^|]*\\)" (list 1 font-lock-variable-name-face))
187    ))
188
189 (defvar django-variable-font-lock-defaults
190   '(django-variable-font-lock-keywords
191     t t
192     ;; This still gives teh syntax symbol to |, why?
193     ((?| . ". "))
194     ))
195
196 (define-derived-mode django-variable-mode nil "Django variable"
197   "For django comment blocks."
198   ;;(modify-syntax-entry ?| ?.)
199   (set (make-local-variable 'font-lock-defaults) django-variable-font-lock-defaults))
200
201 (provide 'django)
202 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
203 ;;; django.el ends here