|
code@11
|
1 |
;;; ecmascript-mode.el --- major mode for editing ECMAScript code
|
|
code@11
|
2 |
|
|
code@11
|
3 |
;; Copyright (c) 2004-2005 David Lindquist <david.lindquist@gmail.com>
|
|
code@11
|
4 |
|
|
code@11
|
5 |
;; Author: David Lindquist <david.lindquist@gmail.com>
|
|
code@11
|
6 |
;; Keywords: languages ecmascript javascript
|
|
code@11
|
7 |
|
|
code@11
|
8 |
;; This is free software; you can redistribute it and/or modify it
|
|
code@11
|
9 |
;; under the terms of the GNU General Public License as published by
|
|
code@11
|
10 |
;; the Free Software Foundation; either version 2, or (at your option)
|
|
code@11
|
11 |
;; any later version.
|
|
code@11
|
12 |
|
|
code@11
|
13 |
;; This is distributed in the hope that it will be useful, but WITHOUT
|
|
code@11
|
14 |
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
code@11
|
15 |
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
code@11
|
16 |
;; License for more details.
|
|
code@11
|
17 |
|
|
code@11
|
18 |
;; You should have received a copy of the GNU General Public License
|
|
code@11
|
19 |
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
code@11
|
20 |
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
code@11
|
21 |
;; Boston, MA 02111-1307, USA.
|
|
code@11
|
22 |
|
|
code@11
|
23 |
;;; Commentary:
|
|
code@11
|
24 |
|
|
code@11
|
25 |
;; ecmascript-mode is a basic major mode for editing code conforming
|
|
code@11
|
26 |
;; to the ECMA-262 standard (3rd edition). The most notable languages
|
|
code@11
|
27 |
;; employing all or most of the standard are JavaScript (Netscape),
|
|
code@11
|
28 |
;; JScript (Microsoft), and ActionScript (Macromedia).
|
|
code@11
|
29 |
|
|
code@11
|
30 |
;; See also:
|
|
code@11
|
31 |
;; http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf
|
|
code@11
|
32 |
|
|
code@11
|
33 |
;;; History:
|
|
code@11
|
34 |
|
|
code@11
|
35 |
;; 2005-11-24 david Fixed void function error and corrected font-lock
|
|
code@11
|
36 |
;; problems that appeared in emacs version 21.3.50.1
|
|
code@11
|
37 |
;; (probably due to changes in java-mode).
|
|
code@11
|
38 |
;; 2004-??-?? david Initial release.
|
|
code@11
|
39 |
|
|
code@11
|
40 |
;;; Code:
|
|
code@11
|
41 |
|
|
code@11
|
42 |
(require 'font-lock)
|
|
code@11
|
43 |
(require 'cc-mode)
|
|
code@11
|
44 |
(eval-when-compile
|
|
code@11
|
45 |
(require 'regexp-opt))
|
|
code@11
|
46 |
|
|
code@11
|
47 |
(defconst ecmascript-mode-version "1.1"
|
|
code@11
|
48 |
"ECMAScript Mode version number.")
|
|
code@11
|
49 |
|
|
code@11
|
50 |
(defgroup ecmascript nil
|
|
code@11
|
51 |
"Major mode for editing ECMAScript code."
|
|
code@11
|
52 |
:group 'languages
|
|
code@11
|
53 |
:prefix "ecmascript-")
|
|
code@11
|
54 |
|
|
code@11
|
55 |
(defcustom ecmascript-mode-hook nil
|
|
code@11
|
56 |
"Hook for customizing `ecmascript-mode'."
|
|
code@11
|
57 |
:group 'ecmascript
|
|
code@11
|
58 |
:type 'hook)
|
|
code@11
|
59 |
|
|
code@11
|
60 |
(defvar ecmascript-mode-map (c-make-inherited-keymap)
|
|
code@11
|
61 |
"Keymap used in `ecmascript-mode' buffers.")
|
|
code@11
|
62 |
|
|
code@11
|
63 |
;;;###autoload
|
|
code@11
|
64 |
(define-derived-mode ecmascript-mode java-mode "ECMAScript"
|
|
code@11
|
65 |
"Major mode for editing ECMAScript code.
|
|
code@11
|
66 |
|
|
code@11
|
67 |
This mode is derived from `java-mode'; see its documentation for further
|
|
code@11
|
68 |
information.
|
|
code@11
|
69 |
|
|
code@11
|
70 |
\\{ecmascript-mode-map}"
|
|
code@11
|
71 |
(make-local-variable 'font-lock-defaults)
|
|
code@11
|
72 |
(setq font-lock-defaults
|
|
code@11
|
73 |
'((;; comment out the lines below to adjust
|
|
code@11
|
74 |
;; syntax highlighting gaudiness
|
|
code@11
|
75 |
ecmascript-font-lock-keywords-1
|
|
code@11
|
76 |
ecmascript-font-lock-keywords-2
|
|
code@11
|
77 |
ecmascript-font-lock-keywords-3
|
|
code@11
|
78 |
)
|
|
code@11
|
79 |
nil nil ((?_ . "w") (?$ . "w")) nil))
|
|
code@11
|
80 |
|
|
code@11
|
81 |
(easy-menu-define c-ecmascript-menu ecmascript-mode-map
|
|
code@11
|
82 |
"ECMAScript Mode Commands" (c-mode-menu "ECMAScript"))
|
|
code@11
|
83 |
)
|
|
code@11
|
84 |
|
|
code@11
|
85 |
(defvar ecmascript-font-lock-default-face 'ecmascript-font-lock-default-face)
|
|
code@11
|
86 |
|
|
code@11
|
87 |
(defconst ecmascript-font-lock-keywords-1
|
|
code@11
|
88 |
(append
|
|
code@11
|
89 |
java-font-lock-keywords-1
|
|
code@11
|
90 |
(list
|
|
code@11
|
91 |
|
|
code@11
|
92 |
'("\\<\\(function\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
|
|
code@11
|
93 |
(1 font-lock-keyword-face t)
|
|
code@11
|
94 |
(2 font-lock-function-name-face nil t))
|
|
code@11
|
95 |
|
|
code@11
|
96 |
;; need to fix this to handle: var a, b;
|
|
code@11
|
97 |
'("\\<\\(var\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
|
|
code@11
|
98 |
(1 font-lock-keyword-face t)
|
|
code@11
|
99 |
(2 font-lock-variable-name-face nil t))
|
|
code@11
|
100 |
))
|
|
code@11
|
101 |
"Subdued level highlighting for ECMAScript mode.")
|
|
code@11
|
102 |
|
|
code@11
|
103 |
(defconst ecmascript-font-lock-keywords-2
|
|
code@11
|
104 |
(append
|
|
code@11
|
105 |
java-font-lock-keywords-2
|
|
code@11
|
106 |
ecmascript-font-lock-keywords-1
|
|
code@11
|
107 |
(list
|
|
code@11
|
108 |
|
|
code@11
|
109 |
'("\\<\\(debugger\\|delete\\|export\\|in\\|typeof\\|with\\)\\>"
|
|
code@11
|
110 |
(1 font-lock-keyword-face t))
|
|
code@11
|
111 |
|
|
code@11
|
112 |
(list (concat
|
|
code@11
|
113 |
"\\<\\("
|
|
code@11
|
114 |
(mapconcat 'identity java-font-lock-extra-types nil)
|
|
code@11
|
115 |
"\\)\\>\\.")
|
|
code@11
|
116 |
'(1 font-lock-type-face nil t))
|
|
code@11
|
117 |
|
|
code@11
|
118 |
;; In Java, `void' is a type. In ECMAScript, it is an operator.
|
|
code@11
|
119 |
;; This overrides the inherited notion of keyword `void'.
|
|
code@11
|
120 |
'("\\<\\(void\\)\\>\\(?:\\s-+\\(\\sw+\\)\\)?"
|
|
code@11
|
121 |
(1 font-lock-keyword-face t)
|
|
code@11
|
122 |
(2 ecmascript-font-lock-default-face t t))
|
|
code@11
|
123 |
|
|
code@11
|
124 |
;; Value properties of the global object
|
|
code@11
|
125 |
'("\\<\\(Infinity\\|NaN\\|undefined\\)\\>" 0 font-lock-constant-face t)
|
|
code@11
|
126 |
|
|
code@11
|
127 |
;; Properties of the Number constructor
|
|
code@11
|
128 |
(list (concat
|
|
code@11
|
129 |
"\\<Number\\."
|
|
code@11
|
130 |
(regexp-opt
|
|
code@11
|
131 |
'("MAX_VALUE" "MIN_VALUE" "NaN" "NEGATIVE_INFINITY"
|
|
code@11
|
132 |
"POSITIVE_INFINITY") t)
|
|
code@11
|
133 |
"\\>")
|
|
code@11
|
134 |
'(1 font-lock-constant-face))
|
|
code@11
|
135 |
|
|
code@11
|
136 |
;; Value properties of the Math object
|
|
code@11
|
137 |
(list (concat
|
|
code@11
|
138 |
"\\<Math\\."
|
|
code@11
|
139 |
(regexp-opt
|
|
code@11
|
140 |
'("E" "LN10" "LN2" "LOG2E" "LOG10E" "PI" "SQRT1_2" "SQRT2") t)
|
|
code@11
|
141 |
"\\>")
|
|
code@11
|
142 |
'(1 font-lock-constant-face))
|
|
code@11
|
143 |
))
|
|
code@11
|
144 |
"Medium level highlighting for ECMAScript mode.")
|
|
code@11
|
145 |
|
|
code@11
|
146 |
(defconst ecmascript-font-lock-keywords-3
|
|
code@11
|
147 |
(append
|
|
code@11
|
148 |
java-font-lock-keywords-3
|
|
code@11
|
149 |
ecmascript-font-lock-keywords-2
|
|
code@11
|
150 |
(list
|
|
code@11
|
151 |
|
|
code@11
|
152 |
;; Properties of the Date constructor
|
|
code@11
|
153 |
'("\\<Date\\.\\(parse\\|UTC\\)\\>" 1 font-lock-builtin-face)
|
|
code@11
|
154 |
|
|
code@11
|
155 |
;; Function properties of the Math object
|
|
code@11
|
156 |
(list (concat
|
|
code@11
|
157 |
"\\<Math\\."
|
|
code@11
|
158 |
(regexp-opt
|
|
code@11
|
159 |
'("abs" "acos" "asin" "atan" "atan2" "ceil" "cos" "exp" "floor"
|
|
code@11
|
160 |
"log" "max" "min" "pow" "random" "round" "sin" "sqrt" "tan") t)
|
|
code@11
|
161 |
"\\>")
|
|
code@11
|
162 |
'(1 font-lock-builtin-face))
|
|
code@11
|
163 |
|
|
code@11
|
164 |
(list (regexp-opt
|
|
code@11
|
165 |
'(;; URI handling function properties
|
|
code@11
|
166 |
"decodeURI" "decodeURIComponent" "encodeURI" "encodeURIComponent"
|
|
code@11
|
167 |
;; Function properties of the global object
|
|
code@11
|
168 |
"eval" "isFinite" "isNaN" "parseFloat" "parseInt") 'words)
|
|
code@11
|
169 |
'(0 font-lock-builtin-face))
|
|
code@11
|
170 |
|
|
code@11
|
171 |
(list (concat
|
|
code@11
|
172 |
"\\."
|
|
code@11
|
173 |
(regexp-opt
|
|
code@11
|
174 |
'(;; Properties of the Object prototype object
|
|
code@11
|
175 |
"hasOwnProperty" "isPrototypeOf" "propertyIsEnumerable"
|
|
code@11
|
176 |
"toLocaleString" "toString" "valueOf"
|
|
code@11
|
177 |
;; Properties of the Function prototype object
|
|
code@11
|
178 |
"apply" "call"
|
|
code@11
|
179 |
;; Properties of the Array prototype object
|
|
code@11
|
180 |
"concat" "join" "pop" "push" "reverse" "shift" "slice" "sort"
|
|
code@11
|
181 |
"splice" "unshift"
|
|
code@11
|
182 |
;; Properties of the String prototype object
|
|
code@11
|
183 |
"charAt" "charCodeAt" "fromCharCode" "indexOf" "lastIndexOf"
|
|
code@11
|
184 |
"localeCompare" "match" "replace" "search" "split" "substring"
|
|
code@11
|
185 |
"toLocaleLowerCase" "toLocaleUpperCase" "toLowerCase"
|
|
code@11
|
186 |
"toUpperCase"
|
|
code@11
|
187 |
;; Properties of the Number prototype object
|
|
code@11
|
188 |
"toExponential" "toFixed" "toPrecision"
|
|
code@11
|
189 |
;; Properties of the Date prototype object
|
|
code@11
|
190 |
"getDate" "getDay" "getFullYear" "getHours" "getMilliseconds"
|
|
code@11
|
191 |
"getMinutes" "getMonth" "getSeconds" "getTime"
|
|
code@11
|
192 |
"getTimezoneOffset" "getUTCDate" "getUTCDay" "getUTCFullYear"
|
|
code@11
|
193 |
"getUTCHours" "getUTCMilliseconds" "getUTCMinutes" "getUTCMonth"
|
|
code@11
|
194 |
"getUTCSeconds" "setDate" "setFullYear" "setHours"
|
|
code@11
|
195 |
"setMilliseconds" "setMinutes" "setMonth" "setSeconds" "setTime"
|
|
code@11
|
196 |
"setUTCDate" "setUTCFullYear" "setUTCHours" "setUTCMilliseconds"
|
|
code@11
|
197 |
"setUTCMinutes" "setUTCMonth" "setUTCSeconds" "toDateString"
|
|
code@11
|
198 |
"toLocaleDateString" "toLocaleString" "toLocaleTimeString"
|
|
code@11
|
199 |
"toTimeString" "toUTCString"
|
|
code@11
|
200 |
;; Properties of the RegExp prototype object
|
|
code@11
|
201 |
"exec" "test"
|
|
code@11
|
202 |
) t)
|
|
code@11
|
203 |
"\\>")
|
|
code@11
|
204 |
'(1 font-lock-builtin-face))
|
|
code@11
|
205 |
))
|
|
code@11
|
206 |
"Gaudy level highlighting for ECMAScript mode.")
|
|
code@11
|
207 |
|
|
code@11
|
208 |
(provide 'ecmascript-mode)
|
|
code@11
|
209 |
|
|
code@11
|
210 |
;;; ecmascript-mode.el ends here
|