Remove old and crusty Sun pkg
[packages] / xemacs-packages / prog-modes / javascript-mode.el
1 ;;; javascript-mode.el --- major mode for editing JavaScript code
2
3 ;; Copyright (C) 1997-2001 Steven Champeon
4 ;;               2002-2006 Ville Skyttä
5
6 ;; Author:     1997 Steven Champeon <schampeo@hesketh.com>
7 ;; Maintainer: Ville Skyttä <scop@xemacs.org>
8 ;; Keywords:   languages javascript
9
10 ;; This file is part of XEmacs.
11
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING.  If not, write to
24 ;; the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;; Synched up with: not in GNU Emacs.
28
29 ;;; Commentary:
30
31 ;; javascript-mode was originally derived from java-cust.el
32 ;; (by Jonathan Payne) by Steven Champeon. It has been modified
33 ;; a lot afterwards by Ville Skyttä.
34
35 ;; Contributors:
36 ;;   Sreng Truong      (bug fix for 21.1)
37 ;;   Sebastian Delmont (fix for prototype function indentation problems)
38 ;;   Stefan Schlee     (GNU Emacs compatibility fixes)
39 ;;   Igor Rayak        (ditto)
40 ;;   Tsirkin Evgeny    (regexp improvement ideas)
41
42 ;; TODO:
43 ;; - Multiple font-lock/highlight levels.
44 ;; - Investigate if Semantic Bovinator should be used.
45 ;; - Check syntax-table stuff.
46
47 ;;; Code:
48
49 (require 'cc-mode)
50 (require 'comint)
51
52 (eval-when-compile
53   (require 'regexp-opt)
54   (require 'font-lock)
55   (require 'speedbar)
56   )
57
58 ;; ------------------------------------------------------------------------ ;;
59
60 (defconst javascript-mode-version "1.10" "Version of `javascript-mode'.")
61
62 ;; ------------------------------------------------------------------------ ;;
63
64 (defgroup javascript nil
65   "Major mode for editing JavaScript code."
66   :tag "JavaScript"
67   :group 'languages
68   :prefix "javascript-")
69
70 (defcustom javascript-mode-hook nil
71   "Hook for customizing `javascript-mode'."
72   :group 'javascript
73   :type 'hook)
74
75 (defgroup javascript-shell nil
76   "JavaScript shell options."
77   :group 'javascript
78   :prefix "javascript-shell-")
79
80 (defcustom javascript-shell-command "jsshell"
81   "*Command for starting `javascript-shell'.
82 Set arguments for this command in `javascript-shell-command-args'."
83   :type 'string
84   :group 'javascript-shell)
85
86 (defcustom javascript-shell-command-args '()
87   "*Command line arguments for `javascript-shell-command'."
88   :type '(repeat (string :tag "Argument"))
89   :group 'javascript-shell)
90
91 (defcustom javascript-shell-prompt-pattern "^js> *"
92   "*JavaScript shell prompt pattern."
93   :type 'regexp
94   :group 'javascript-shell)
95
96 (defcustom javascript-shell-mode-hook nil
97   "Hook for customizing `javascript-shell-mode'."
98   :type 'hook
99   :group 'javascript-shell)
100
101 ;; ------------------------------------------------------------------------ ;;
102
103 (defvar javascript-mode-abbrev-table nil
104   "Abbrev table in use in `javascript-mode' buffers.")
105 (define-abbrev-table 'javascript-mode-abbrev-table ())
106
107 ;; ------------------------------------------------------------------------ ;;
108
109 (defvar javascript-mode-map (c-make-inherited-keymap)
110   "Keymap used in `javascript-mode' buffers.")
111
112 (defvar javascript-menu nil)
113 (easy-menu-define javascript-menu javascript-mode-map
114                   "JavaScript Mode Commands" (c-mode-menu "JavaScript"))
115
116 ;; ------------------------------------------------------------------------ ;;
117
118 ;; Reserved words in JavaScript.
119 (defconst javascript-reserved-words
120   (eval-when-compile
121     (regexp-opt
122      '(
123        "abstract"
124        "boolean"
125        "break"
126        "byte"
127        "case"
128        "catch"
129        "char"
130        "class"
131        "const"
132        "continue"
133        "debugger"
134        "default"
135        "delete"
136        "do"
137        "double"
138        "else"
139        "enum"
140        "export"
141        "extends"
142        "false"
143        "final"
144        "finally"
145        "float"
146        "for"
147        "function"
148        "goto"
149        "if"
150        "implements"
151        "import"
152        "in"
153        "instanceof"
154        "int"
155        "interface"
156        "long"
157        "native"
158        "new"
159        "null"
160        "package"
161        "private"
162        "protected"
163        "public"
164        "return"
165        "short"
166        "static"
167        "super"
168        "switch"
169        "synchronized"
170        "this"
171        "throw"
172        "throws"
173        "transient"
174        "true"
175        "try"
176        "typeof"
177        "var"
178        "void"
179        "volatile"
180        "while"
181        "with"
182        ) t))
183   "Expression for matching reserved words in `javascript-mode' buffers.
184
185 From Core JavaScript Reference 1.5, Reserved Words:
186 <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Reserved_Words>")
187
188
189 ;; JavaScript identifiers
190 ;; This one is intentionally not too strict...
191 (defconst javascript-identifier
192   "[a-zA-Z_\\$][a-zA-Z0-9_\\$]*"
193   "Expression for matching identifiers in `javascript-mode' buffers.
194
195 From Core JavaScript Guide 1.5, Core Language Features:
196 <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:About:Core_Language_Features>")
197
198 ;; ------------------------------------------------------------------------ ;;
199
200 ;; Font lock keywords etc
201
202 ;; eg. function foo(bar, quux)
203 (defconst javascript-function-re
204   (concat "\\(^\\|[ \t;{]\\)"
205           "function[ \t]+"
206           "\\("
207           "\\(" javascript-identifier "\\.\\)*"
208           javascript-identifier
209           "\\)")
210   "Regular expression for matching \"normal\" function declarations.")
211
212 ;; eg. foo: function(bar, quux)
213 (defconst javascript-property-function-re
214   (concat "\\(^\\|[ \t;{]+\\)"
215           "\\("
216           "\\(" javascript-identifier "\\.\\)*"
217           javascript-identifier
218           "\\)"
219           "[ \t]*:[ \t]*"
220           "\\(new[ \t]+Function\\|function\\)"
221           "[ \t]*(")
222   "Regular expression for matching property style function declarations.")
223
224 ;; eg. foo.bar.quux = function(baz)
225 (defconst javascript-prototype-function-re
226   (concat "\\(^\\|[ \t;{]+\\)"
227           "\\("
228           "\\(" javascript-identifier "\\.\\)*"
229           javascript-identifier
230           "\\)"
231           "[ \t]*=[ \t]*"
232           "\\(new[ \t]+Function\\|function\\)"
233           "[ \t]*(")
234   "Regular expression for matching prototype style function declarations.")
235
236 (defconst javascript-variable-re
237   (concat "\\(^\\|[ \t;{(]\\)\\(const\\|var\\)[ \t]+"
238           "\\("
239           javascript-identifier
240           "\\)")
241   "Regular expression for matching variables.")
242
243 (defconst javascript-font-lock-keywords
244   (list
245
246    ;; Reserved words.
247    (cons (concat
248           "\\(^\\|[ \t;{(]\\)\\("
249           javascript-reserved-words
250           "\\)[ \t\n(){};,]")
251          '(2 'font-lock-keyword-face))
252
253    ;; Function declarations.
254    (cons javascript-function-re '(2 'font-lock-function-name-face))
255    (cons javascript-property-function-re '(2 'font-lock-function-name-face))
256    (cons javascript-prototype-function-re '(2 'font-lock-function-name-face))
257    ;; This would catch both declarations and calls.
258    ;(cons (concat
259    ;       "\\(^\\|[ \t.;{(]\\)\\("
260    ;       javascript-identifier
261    ;       "\\)[ \t]*(")
262    ;      '(2 'font-lock-function-name-face))
263
264    ;; Variables and constants.
265    (cons javascript-variable-re '(3 'font-lock-variable-name-face))
266    ;; This would catch more of them and properties as well.
267    ;(cons (concat
268    ;       "\\(^\\|[ \t(\\[\\.{;]\\)\\("
269    ;       javascript-identifier
270    ;       "\\)[ \t]*[^(]")
271    ;      '(2 'font-lock-variable-name-face))
272
273    )
274   "Highlighting rules for `javascript-mode' buffers.")
275
276 ;; ------------------------------------------------------------------------ ;;
277
278 (defvar javascript-imenu-generic-expression
279   `((nil ,javascript-function-re 2)
280     (nil ,javascript-property-function-re 2)
281     (nil ,javascript-prototype-function-re 2)
282     ;; ("Variables" ,javascript-variable-re 3)
283     )
284   "Imenu generic expression for JavaScript mode.
285 See `imenu-generic-expression'.")
286
287 ;; ------------------------------------------------------------------------ ;;
288
289 ;;;###autoload
290 (defun javascript-mode ()
291   "Major mode for editing JavaScript code.
292
293 See the documentation for `c++-mode': JavaScript mode is an extension of it.
294 Use the hook `javascript-mode-hook' to execute custom code when entering
295 JavaScript mode.
296
297 \\{javascript-mode-map}"
298   (interactive)
299
300   (let ((current-c++-mode-hook (and (boundp 'c++-mode-hook) c++-mode-hook)))
301
302     ;; Temporarily disable the c++-mode hook; don't wanna run
303     ;; it when loading up c++-mode.
304     (setq c++-mode-hook nil)
305     (c++-mode)
306
307     ;; Do our stuff.
308     (setq major-mode 'javascript-mode mode-name "JavaScript")
309     (use-local-map javascript-mode-map)
310     (setq local-abbrev-table javascript-mode-abbrev-table)
311     (c-set-offset 'inher-cont '+)
312
313     ;; Change menu name.  Kudos to Geert Ribbers and Igor Rayak.
314     (easy-menu-remove '("C++"))
315     (easy-menu-add javascript-menu)
316
317     ;; GNU Emacs reportedly needs this for font locking to work properly.
318     (unless (featurep 'xemacs)
319       (set (make-local-variable 'font-lock-defaults)
320            '(javascript-font-lock-keywords nil nil)))
321
322     ;; cc-mode does not handle JavaScript prototype function declarations well.
323     ;; Thanks to Sebastian Delmont.
324     (set (make-local-variable 'c-lambda-key) "function")
325     (c-set-offset 'inlambda 0)
326
327     ;; imenu support.
328     (set (make-local-variable 'imenu-generic-expression)
329          javascript-imenu-generic-expression)
330
331     ;; Restore the original c++-mode-hook.
332     (setq c++-mode-hook current-c++-mode-hook)
333
334     (run-hooks 'javascript-mode-hook)))
335
336 ;; ------------------------------------------------------------------------ ;;
337
338 ;;;###autoload
339 (defun javascript-shell ()
340   "Run a JavaScript shell as an inferior process.
341
342 Use the `javascript-shell-command' variable to set the command and
343 `javascript-shell-command-args' for its arguments to specify the
344 command line that invokes your preferred JavaScript shell.
345
346 Free JavaScript shell implementations are available for example from
347 <http://www.mozilla.org/js/>.
348
349 Usage examples:        command    arguments
350  Mozilla SpiderMonkey  jsshell
351  Mozilla Rhino         java       -jar /path/to/js.jar"
352
353   (interactive)
354
355   (unless (comint-check-proc "*JavaScript*")
356     (set-buffer
357      (apply 'make-comint "JavaScript"
358             javascript-shell-command nil javascript-shell-command-args))
359     (javascript-shell-mode)
360     )
361
362   (pop-to-buffer "*JavaScript*")
363   )
364
365
366 (defun javascript-shell-mode ()
367   "Major mode for interacting with a JavaScript shell."
368   (comint-mode)
369   (setq comint-prompt-regexp javascript-shell-prompt-pattern)
370   (setq mode-name 'javascript-shell-mode)
371   (setq mode-name "JavaScript Shell")
372   (setq mode-line-process '(":%s"))
373   (run-hooks 'javascript-shell-mode-hook)
374   )
375
376 ;; ------------------------------------------------------------------------ ;;
377
378 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.js$" . javascript-mode))
379 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.pac$" . javascript-mode))
380
381 ;; Speedbar handling
382 (if (fboundp 'speedbar-add-supported-extension)
383     (speedbar-add-supported-extension '(".js" ".pac")))
384
385 ;; ------------------------------------------------------------------------ ;;
386
387 (provide 'javascript-mode)
388
389 ;;; javascript-mode.el ends here