]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/nxhtml/related/tt-mode.el
submodulized .emacs.d setup
[.emacs.d.git] / emacs / nxhtml / related / tt-mode.el
1 ;; tt-mode.el --- Emacs major mode for editing Template Toolkit files
2 ;;
3 ;; Copyright (c) 2002 Dave Cross, all rights reserved.
4 ;;
5 ;; This file may be distributed under the same terms as GNU Emacs.
6 ;;
7 ;; $Id: tt-mode.el 13 2008-01-27 09:35:16Z dave $
8 ;;
9 ;; This file adds simple font highlighting of TT directives when you are
10 ;; editing Template Toolkit files.
11 ;;
12 ;; I usually give these files an extension of .tt and in order to automatically
13 ;; invoke this mode for these files, I have the following in my .emacs file.
14 ;;
15 ;; (setq load-path
16 ;;      (cons "/home/dave/xemacs" load-path))
17 ;; (autoload 'tt-mode "tt-mode")
18 ;; (setq auto-mode-alist
19 ;;  (append '(("\\.tt$" . tt-mode))  auto-mode-alist ))
20 ;;
21 ;; Something similar may well work for you.
22 ;;
23 ;; Author: Dave Cross <dave@dave.org.uk>
24 ;;
25 ;; Modified by Lennart Borgman 2008-08-06
26
27 (require 'font-lock)
28
29 (defvar tt-mode-hook nil
30   "List of functions to call when entering TT mode")
31
32 (defvar tt-keywords
33   (concat "\\b\\(?:"
34           (regexp-opt (list "GET" "CALL" "SET" "DEFAULT" "INSERT" "INCLUDE"
35                             "BLOCK" "END" "PROCESS" "WRAPPER" "IF" "UNLESS"
36                             "ELSIF" "ELSE" "SWITCH" "CASE" "FOR" "FOREACH"
37                             "WHILE" "FILTER" "USE" "MACRO" "PERL" "RAWPERL"
38                             "TRY" "THROW" "CATCH" "FINAL" "LAST" "RETURN"
39                             "STOP" "CLEAR" "META" "TAGS"))
40           "\\)\\b"))
41
42 (defvar tt-font-lock-keywords
43    (list
44     ;; Fontify [& ... &] expressions
45     '("\\(\\[%[-+]?\\)\\(\\(.\\|\n\\)+?\\)\\([-+]?%\\]\\)"
46       (1 font-lock-string-face t)
47       (2 font-lock-variable-name-face t)
48       (4 font-lock-string-face t))
49     ;; Look for keywords within those expressions
50     ;;
51     ;; Comment out whole directive tag
52     '("\\[%\\(#.*?\\)%\\]"
53       (1 font-lock-comment-face t))
54     ;; Comments to end of line
55 ;;;     '("\\[%\\(?:.\\|\n\\)*\\(#.*\\)"
56 ;;;       (1 font-lock-comment-face t))
57     '("\\[% *\\([a-z_0-9]*\\) *%\\]"
58       (1 font-lock-constant-face t))
59     (list (concat
60            "\\(\\[%[-+]?\\|;\\)[ \n\t]*\\("
61            tt-keywords
62            "\\)")
63           2 font-lock-keyword-face t)
64     )
65   "Expressions to font-lock in tt-mode.")
66
67 ;; (defvar tt-font-lock-keywords
68 ;;   ;; Since this is used in a multi major mode we
69 ;;    (list
70 ;;     ;; Fontify [& ... &] expressions
71 ;; ;;;     '("^\\([-+]?\\)\\(\\(.\\|\n\\)+?\\)\\([-+]?\\)$"
72 ;; ;;;       (1 font-lock-string-face t)
73 ;; ;;;       (2 font-lock-variable-name-face t)
74 ;; ;;;       (4 font-lock-string-face t))
75 ;;     '("\\(#.*\\)$"
76 ;;       (1 font-lock-comment-face t))
77 ;;     '("^ *\\([a-z_0-9]*\\) *$"
78 ;;       (1 font-lock-constant-face t))
79 ;;     (list (concat
80 ;;            "^\\(?:[-+]?\\|;\\)[ \n\t]*\\("
81 ;;            tt-keywords
82 ;;            "\\)")
83 ;;            )
84 ;;        1 font-lock-keyword-face t)
85 ;;     )
86 ;;   "Expressions to font-lock in tt-mode.")
87
88 (defvar tt-font-lock-defaults
89   '(tt-font-lock-keywords nil t))
90
91 (defun tt-mode-after-change (beg end pre-len)
92   ;; add/remove font-lock-multiline
93   ;; Fix-me: add variable for search lengths
94   (let* ((here (point))
95          (beg-is-ml (get-text-property beg 'font-lock-multiline))
96          tt-beg
97          tt-end
98          )
99     (when beg-is-ml
100       (let ((beg-ok (not (previous-single-property-change
101                           here 'font-lock-multiline
102                           nil (- here 1))))
103             )
104         (when beg-ok
105           (goto-char beg)
106           (search-forward "%]" end t)
107           )
108             (search-forward "[%" end t)
109         ))
110     (when tt-end
111       (remove-list-of-text-properties here tt-beg '(font-lock-multiline))
112       (set-text-properties tt-beg tt-end '(font-lock-multiline t))))
113   )
114
115
116 ;;;###autoload
117 (define-derived-mode tt-mode fundamental-mode "TT"
118   "Major mode for editing Template Toolkit files."
119   (set (make-local-variable 'font-lock-defaults) tt-font-lock-defaults)
120   (add-hook 'after-change-functions 'tt-mode-after-change nil t)
121   )
122
123 (provide 'tt-mode)
124 ;; tt-mode.el ends here