summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/mumamo-aspnet.el
blob: c6bb2c7808609cae1fc0bd35aab62c29c44f9700 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
;;; mumamo-aspnet.el --- Support for ASP .Net in `mumamo-mode'.
;;
;;;;; John: Please change here to what you want:
;; Author: John J Foerch (jjfoerch A earthlink O net)
;; Maintainer:
;; Created: ??
;; Version: ==
;; Last-Updated: Wed Dec 12 21:55:11 2007 (3600 +0100)
;; URL: http://OurComments.org/Emacs/Emacs.html
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 3, 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 GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Support for ASP .Net in `mumamo-mode'.  If you want to use VB then
;; you have to get the vb mode that this is written for here:
;;
;;   http://www.emacswiki.org/cgi-bin/wiki/VbDotNetMode
;;
;; A C# mode is already included in nXhtml. That is the one that this
;; library has been tested with.
;;
;;
;;; Usage:
;;
;; Put this file in you Emacs `load-path' and add in your .emacs:
;;
;;   (eval-after-load 'mumamo
;;     (require 'mumamo-aspnet)
;;     (mumamo-aspnet-add-me))
;;
;; A file with the extension .aspx will no be opened with nxhtml-mode
;; as the main major mode and with chunks in csharp-mode etc.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(eval-when-compile (require 'mumamo))

;;;

;; (defun mumamo-aspnet-add-me()
;;   "Make mumamo aware of the ASP.Net extension."
;;   (add-to-list 'mumamo-chunk-family-list
;;                '("ASP.Net nXhtml Family" nxhtml-mode
;;                  (mumamo-chunk-aspnet
;;                   mumamo-chunk-aspnet-script
;;                   mumamo-chunk-inlined-style
;;                   mumamo-chunk-inlined-script
;;                   mumamo-chunk-style=
;;                   mumamo-chunk-onjs=
;;                   ))
;;                t)
;;   (add-to-list 'mumamo-chunk-family-list
;;                '("ASP.Net XHTML Family" html-mode
;;                  (mumamo-chunk-aspnet
;;                   mumamo-chunk-aspnet-script
;;                   mumamo-chunk-inlined-style
;;                   mumamo-chunk-inlined-script
;;                   mumamo-chunk-style=
;;                   mumamo-chunk-onjs=
;;                   ))
;;                t)


;;   (add-to-list 'mumamo-filenames-list
;;                '("\\.aspx\\'" "ASP.Net nXhtml Family"))
;;   ;; Make it SET for current session in Custom.
;;   (customize-set-variable 'mumamo-filenames-list mumamo-filenames-list)
;;   (customize-set-value 'mumamo-filenames-list mumamo-filenames-list)

;;   ;; this is how to set up mode aliases, should we need them.
;;   (add-to-list 'mumamo-major-modes '(csharp-mode csharp-mode))
;;   (add-to-list 'mumamo-major-modes '(vbnet-mode vbnet-mode))
;;   ;; Make it SET for current session in Custom.
;;   (customize-set-variable 'mumamo-major-modes mumamo-major-modes)
;;   (customize-set-value 'mumamo-major-modes mumamo-major-modes)
;;   )


;;; aspnet

(defvar mumamo-aspnet-page-language-mode-spec nil
  "A mumamo mode-spec for the default language of an ASP.Net page.
This is what is set with the directive `@ Page Language' on the
page.

Internal variable.")
(make-variable-buffer-local 'mumamo-aspnet-page-language-mode-spec)
;;(add-to-list 'mumamo-survive 'mumamo-aspnet-page-language-mode-spec)
(put 'mumamo-aspnet-page-language-mode-spec 'permanent-local t)

(defconst mumamo-aspnet-language-regex
  (rx (0+ (not (any ">")))
      word-start "language" (0+ space) "=" (0+ space) ?\" (submatch (0+ (not (any ?\" ?>)))) ?\"
      ))

(defun mumamo-aspnet-get-page-language-mode-spec ()
  (or mumamo-aspnet-page-language-mode-spec
      (save-excursion
        (goto-char (point-min))
        (when (search-forward "<%@ Page")
          (let ((case-fold-search t))
            (when (looking-at mumamo-aspnet-language-regex)
              (mumamo-aspnet-mode-spec-for-language (match-string 1))))))
      'fundamental-mode))

(defun mumamo-aspnet-get-mode-for-chunk (&optional chunk-type)
  (cond ((eq chunk-type 'script)
         (mumamo-get-major-mode-substitute
          (or (if (looking-at mumamo-aspnet-language-regex)
                  (mumamo-aspnet-mode-spec-for-language (match-string 1))
                  (mumamo-aspnet-get-page-language-mode-spec))
              'fundamental-mode)
          'fontification))
        ((eq chunk-type 'directive)
         'fundamental-mode)
        ;;(t (mumamo-mode-from-modespec
        (t (mumamo-get-major-mode-substitute
            (mumamo-aspnet-get-page-language-mode-spec)
            'fontification
            ))))


(defun mumamo-chunk-aspnet(pos min max)
  "Find <% ... %>."
  (mumamo-find-possible-chunk pos min max
                              'mumamo-search-bw-exc-start-aspnet
                              'mumamo-search-bw-exc-end-jsp
                              'mumamo-search-fw-exc-start-jsp
                              'mumamo-search-fw-exc-end-jsp))

(defun mumamo-search-bw-exc-start-aspnet(pos min)
  ;;(let ((exc-start (mumamo-search-bw-exc-start-str pos min "<%")))
  (let ((exc-start (mumamo-chunk-start-bw-str pos min "<%")))
    (when (and exc-start
               (<= exc-start pos))
      (cons exc-start
            (mumamo-aspnet-get-mode-for-chunk
             (if (eq (char-after exc-start) ?@)
                 'directive))))))

(defconst mumamo-aspnet-script-tag-start-regex
  (rx "<script" word-end
      (0+ (not (any ">")))
      word-start "runat" (0+ space) "=" (0+ space) ?\" "server" ?\"
      (0+ (not (any ">")))
      ">"
      ))

(defun mumamo-aspnet-mode-spec-for-language (language)
  (let ((language (downcase language)))
    (cond ((equal language "c#") 'csharp-mode)
          ((equal language "vb") 'vbnet-mode)
          (t 'fundamental-mode))))

(defun mumamo-search-bw-exc-start-aspnet-script(pos min)
  (goto-char (+ pos 7))
  (let ((marker-start (search-backward "<script" min t))
        exc-mode
        exc-start)
    (when marker-start
      (when (looking-at mumamo-aspnet-script-tag-start-regex)
        (setq exc-start (match-end 0))
        (setq exc-mode (mumamo-aspnet-get-mode-for-chunk 'script))
        (goto-char exc-start)
        (when (<= exc-start pos)
          (cons (point) exc-mode))))))

(defun mumamo-search-fw-exc-start-aspnet-script(pos max)
  (goto-char (1+ pos))
  (skip-chars-backward "^<")
  ;; Handle <![CDATA[
  (when (and
         (eq ?< (char-before))
         (eq ?! (char-after))
         (not (bobp)))
    (backward-char)
    (skip-chars-backward "^<"))
  (unless (bobp)
    (backward-char 1))
  (let ((exc-start (search-forward "<script" max t))
        exc-mode)
    (when exc-start
      (goto-char (- exc-start 7))
      (when (looking-at mumamo-aspnet-script-tag-start-regex)
        (goto-char (match-end 0))
        (point)
        ))))

(defun mumamo-chunk-aspnet-script(pos min max)
  "Find inlined script, <script runat=\"server\">...</script>."
  (mumamo-find-possible-chunk pos min max
                              'mumamo-search-bw-exc-start-aspnet-script
                              'mumamo-search-bw-exc-end-inlined-script
                              'mumamo-search-fw-exc-start-aspnet-script
                              'mumamo-search-fw-exc-end-inlined-script))

;; Fix-me: define a multi major mode for asp. Or maybe just drop this
;; file?

(provide 'mumamo-aspnet)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; mumamo-aspnet.el ends here