initial commit
[emacs-init.git] / nxhtml / util / mumamo-aspnet.el
1 ;;; mumamo-aspnet.el --- Support for ASP .Net in `mumamo-mode'.
2 ;;
3 ;;;;; John: Please change here to what you want:
4 ;; Author: John J Foerch (jjfoerch A earthlink O net)
5 ;; Maintainer:
6 ;; Created: ??
7 ;; Version: ==
8 ;; Last-Updated: Wed Dec 12 21:55:11 2007 (3600 +0100)
9 ;; URL: http://OurComments.org/Emacs/Emacs.html
10 ;; Keywords:
11 ;; Compatibility:
12 ;;
13 ;; Features that might be required by this library:
14 ;;
15 ;;   None
16 ;;
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18 ;;
19 ;; This program is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 3, or (at your option)
22 ;; any later version.
23 ;;
24 ;; This program is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 ;; GNU General Public License for more details.
28 ;;
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
31 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
33 ;;
34 ;;
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;;
37 ;;; Commentary:
38 ;;
39 ;; Support for ASP .Net in `mumamo-mode'.  If you want to use VB then
40 ;; you have to get the vb mode that this is written for here:
41 ;;
42 ;;   http://www.emacswiki.org/cgi-bin/wiki/VbDotNetMode
43 ;;
44 ;; A C# mode is already included in nXhtml. That is the one that this
45 ;; library has been tested with.
46 ;;
47 ;;
48 ;;; Usage:
49 ;;
50 ;; Put this file in you Emacs `load-path' and add in your .emacs:
51 ;;
52 ;;   (eval-after-load 'mumamo
53 ;;     (require 'mumamo-aspnet)
54 ;;     (mumamo-aspnet-add-me))
55 ;;
56 ;; A file with the extension .aspx will no be opened with nxhtml-mode
57 ;; as the main major mode and with chunks in csharp-mode etc.
58 ;;
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
60
61 (eval-when-compile (require 'mumamo))
62
63 ;;;
64
65 ;; (defun mumamo-aspnet-add-me()
66 ;;   "Make mumamo aware of the ASP.Net extension."
67 ;;   (add-to-list 'mumamo-chunk-family-list
68 ;;                '("ASP.Net nXhtml Family" nxhtml-mode
69 ;;                  (mumamo-chunk-aspnet
70 ;;                   mumamo-chunk-aspnet-script
71 ;;                   mumamo-chunk-inlined-style
72 ;;                   mumamo-chunk-inlined-script
73 ;;                   mumamo-chunk-style=
74 ;;                   mumamo-chunk-onjs=
75 ;;                   ))
76 ;;                t)
77 ;;   (add-to-list 'mumamo-chunk-family-list
78 ;;                '("ASP.Net XHTML Family" html-mode
79 ;;                  (mumamo-chunk-aspnet
80 ;;                   mumamo-chunk-aspnet-script
81 ;;                   mumamo-chunk-inlined-style
82 ;;                   mumamo-chunk-inlined-script
83 ;;                   mumamo-chunk-style=
84 ;;                   mumamo-chunk-onjs=
85 ;;                   ))
86 ;;                t)
87
88
89 ;;   (add-to-list 'mumamo-filenames-list
90 ;;                '("\\.aspx\\'" "ASP.Net nXhtml Family"))
91 ;;   ;; Make it SET for current session in Custom.
92 ;;   (customize-set-variable 'mumamo-filenames-list mumamo-filenames-list)
93 ;;   (customize-set-value 'mumamo-filenames-list mumamo-filenames-list)
94
95 ;;   ;; this is how to set up mode aliases, should we need them.
96 ;;   (add-to-list 'mumamo-major-modes '(csharp-mode csharp-mode))
97 ;;   (add-to-list 'mumamo-major-modes '(vbnet-mode vbnet-mode))
98 ;;   ;; Make it SET for current session in Custom.
99 ;;   (customize-set-variable 'mumamo-major-modes mumamo-major-modes)
100 ;;   (customize-set-value 'mumamo-major-modes mumamo-major-modes)
101 ;;   )
102
103
104 ;;; aspnet
105
106 (defvar mumamo-aspnet-page-language-mode-spec nil
107   "A mumamo mode-spec for the default language of an ASP.Net page.
108 This is what is set with the directive `@ Page Language' on the
109 page.
110
111 Internal variable.")
112 (make-variable-buffer-local 'mumamo-aspnet-page-language-mode-spec)
113 ;;(add-to-list 'mumamo-survive 'mumamo-aspnet-page-language-mode-spec)
114 (put 'mumamo-aspnet-page-language-mode-spec 'permanent-local t)
115
116 (defconst mumamo-aspnet-language-regex
117   (rx (0+ (not (any ">")))
118       word-start "language" (0+ space) "=" (0+ space) ?\" (submatch (0+ (not (any ?\" ?>)))) ?\"
119       ))
120
121 (defun mumamo-aspnet-get-page-language-mode-spec ()
122   (or mumamo-aspnet-page-language-mode-spec
123       (save-excursion
124         (goto-char (point-min))
125         (when (search-forward "<%@ Page")
126           (let ((case-fold-search t))
127             (when (looking-at mumamo-aspnet-language-regex)
128               (mumamo-aspnet-mode-spec-for-language (match-string 1))))))
129       'fundamental-mode))
130
131 (defun mumamo-aspnet-get-mode-for-chunk (&optional chunk-type)
132   (cond ((eq chunk-type 'script)
133          (mumamo-get-major-mode-substitute
134           (or (if (looking-at mumamo-aspnet-language-regex)
135                   (mumamo-aspnet-mode-spec-for-language (match-string 1))
136                   (mumamo-aspnet-get-page-language-mode-spec))
137               'fundamental-mode)
138           'fontification))
139         ((eq chunk-type 'directive)
140          'fundamental-mode)
141         ;;(t (mumamo-mode-from-modespec
142         (t (mumamo-get-major-mode-substitute
143             (mumamo-aspnet-get-page-language-mode-spec)
144             'fontification
145             ))))
146
147
148 (defun mumamo-chunk-aspnet(pos min max)
149   "Find <% ... %>."
150   (mumamo-find-possible-chunk pos min max
151                               'mumamo-search-bw-exc-start-aspnet
152                               'mumamo-search-bw-exc-end-jsp
153                               'mumamo-search-fw-exc-start-jsp
154                               'mumamo-search-fw-exc-end-jsp))
155
156 (defun mumamo-search-bw-exc-start-aspnet(pos min)
157   ;;(let ((exc-start (mumamo-search-bw-exc-start-str pos min "<%")))
158   (let ((exc-start (mumamo-chunk-start-bw-str pos min "<%")))
159     (when (and exc-start
160                (<= exc-start pos))
161       (cons exc-start
162             (mumamo-aspnet-get-mode-for-chunk
163              (if (eq (char-after exc-start) ?@)
164                  'directive))))))
165
166 (defconst mumamo-aspnet-script-tag-start-regex
167   (rx "<script" word-end
168       (0+ (not (any ">")))
169       word-start "runat" (0+ space) "=" (0+ space) ?\" "server" ?\"
170       (0+ (not (any ">")))
171       ">"
172       ))
173
174 (defun mumamo-aspnet-mode-spec-for-language (language)
175   (let ((language (downcase language)))
176     (cond ((equal language "c#") 'csharp-mode)
177           ((equal language "vb") 'vbnet-mode)
178           (t 'fundamental-mode))))
179
180 (defun mumamo-search-bw-exc-start-aspnet-script(pos min)
181   (goto-char (+ pos 7))
182   (let ((marker-start (search-backward "<script" min t))
183         exc-mode
184         exc-start)
185     (when marker-start
186       (when (looking-at mumamo-aspnet-script-tag-start-regex)
187         (setq exc-start (match-end 0))
188         (setq exc-mode (mumamo-aspnet-get-mode-for-chunk 'script))
189         (goto-char exc-start)
190         (when (<= exc-start pos)
191           (cons (point) exc-mode))))))
192
193 (defun mumamo-search-fw-exc-start-aspnet-script(pos max)
194   (goto-char (1+ pos))
195   (skip-chars-backward "^<")
196   ;; Handle <![CDATA[
197   (when (and
198          (eq ?< (char-before))
199          (eq ?! (char-after))
200          (not (bobp)))
201     (backward-char)
202     (skip-chars-backward "^<"))
203   (unless (bobp)
204     (backward-char 1))
205   (let ((exc-start (search-forward "<script" max t))
206         exc-mode)
207     (when exc-start
208       (goto-char (- exc-start 7))
209       (when (looking-at mumamo-aspnet-script-tag-start-regex)
210         (goto-char (match-end 0))
211         (point)
212         ))))
213
214 (defun mumamo-chunk-aspnet-script(pos min max)
215   "Find inlined script, <script runat=\"server\">...</script>."
216   (mumamo-find-possible-chunk pos min max
217                               'mumamo-search-bw-exc-start-aspnet-script
218                               'mumamo-search-bw-exc-end-inlined-script
219                               'mumamo-search-fw-exc-start-aspnet-script
220                               'mumamo-search-fw-exc-end-inlined-script))
221
222 ;; Fix-me: define a multi major mode for asp. Or maybe just drop this
223 ;; file?
224
225 (provide 'mumamo-aspnet)
226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 ;;; mumamo-aspnet.el ends here