1 ;;; mumamo-aspnet.el --- Support for ASP .Net in `mumamo-mode'.
3 ;;;;; John: Please change here to what you want:
4 ;; Author: John J Foerch (jjfoerch A earthlink O net)
8 ;; Last-Updated: Wed Dec 12 21:55:11 2007 (3600 +0100)
9 ;; URL: http://OurComments.org/Emacs/Emacs.html
13 ;; Features that might be required by this library:
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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)
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.
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.
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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:
42 ;; http://www.emacswiki.org/cgi-bin/wiki/VbDotNetMode
44 ;; A C# mode is already included in nXhtml. That is the one that this
45 ;; library has been tested with.
50 ;; Put this file in you Emacs `load-path' and add in your .emacs:
52 ;; (eval-after-load 'mumamo
53 ;; (require 'mumamo-aspnet)
54 ;; (mumamo-aspnet-add-me))
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.
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 (eval-when-compile (require 'mumamo))
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=
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=
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)
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)
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
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)
116 (defconst mumamo-aspnet-language-regex
117 (rx (0+ (not (any ">")))
118 word-start "language" (0+ space) "=" (0+ space) ?\" (submatch (0+ (not (any ?\" ?>)))) ?\"
121 (defun mumamo-aspnet-get-page-language-mode-spec ()
122 (or mumamo-aspnet-page-language-mode-spec
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))))))
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))
139 ((eq chunk-type 'directive)
141 ;;(t (mumamo-mode-from-modespec
142 (t (mumamo-get-major-mode-substitute
143 (mumamo-aspnet-get-page-language-mode-spec)
148 (defun mumamo-chunk-aspnet(pos min max)
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))
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 "<%")))
162 (mumamo-aspnet-get-mode-for-chunk
163 (if (eq (char-after exc-start) ?@)
166 (defconst mumamo-aspnet-script-tag-start-regex
167 (rx "<script" word-end
169 word-start "runat" (0+ space) "=" (0+ space) ?\" "server" ?\"
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))))
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))
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))))))
193 (defun mumamo-search-fw-exc-start-aspnet-script(pos max)
195 (skip-chars-backward "^<")
198 (eq ?< (char-before))
202 (skip-chars-backward "^<"))
205 (let ((exc-start (search-forward "<script" max t))
208 (goto-char (- exc-start 7))
209 (when (looking-at mumamo-aspnet-script-tag-start-regex)
210 (goto-char (match-end 0))
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))
222 ;; Fix-me: define a multi major mode for asp. Or maybe just drop this
225 (provide 'mumamo-aspnet)
226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 ;;; mumamo-aspnet.el ends here