-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjavascript-mode.el
296 lines (245 loc) · 10.5 KB
/
javascript-mode.el
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
;; javascript-mode.el --- major mode for editing javascript (.js) files
;;
;; Copyright (C) 1997 Peter Kruse
;; Author: Peter Kruse <[email protected]>
;; Keywords: languages
;; Time-stamp: <2005-12-14 16:29:39 tomtenthij>
;; This file is *NOT* part of GNU Emacs.
;; This file 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 2 of the License, or
;; (at your option) any later version.
;; javascript-mode.el 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 this file; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;; Get the latest version from
;; <http://www.brigadoon.de/peter/javascript-mode.el>
;; Basically this is c-mode, for indentation, that's where the line
;; (load-library "c-mode") comes from. colorization is done via hilit19.el.
;; Actually did not do much programming myself, just wanted to have
;; colorization, indentation and some functions (hm that's what major modes
;; are about?) hey, this is my first try!
;;; HOW TO INSTALL:
;; Put the following forms in your .emacs to enable autoloading of JavaScript
;; mode, and auto-recognition of ".js" files.
;;
;; (autoload 'javascript-mode "javascript-mode" "JavaScript mode" t)
;; (setq auto-mode-alist (append '(("\\.js$" . javascript-mode))
;; auto-mode-alist))
;;
;; This mode requires another lisp file, tempo.el. This can be
;; retrieved from ftp://ftp.lysator.liu.se/pub/emacs/tempo.el
;;
;; You might want to get browse-url.el, for the online help.
;; Get it from
;; http://wombat.doc.ic.ac.uk/emacs/browse-url.el
;;; Change Log:
;;
;; Sun Apr 12 19:48:48 1998
;;
;; included a menu
;;
;; Mon Sep 22 20:03:12 MET DST 1997
;;
;; improvement of Syntax-table
;;
;; Sun Sep 7 17:57:50 MET DST 1997
;;
;; new variable: javascript-interactive
;;
;; Mon Sep 1 14:52:37 MET DST 1997
;;
;; javascript online help - all it does, is browse-url to
;; javascript-base-help-href
;;
;; Fri Aug 29 21:01:22 MET DST 1997 Peter Kruse
;; <[email protected]>
;;
;; 1st release
;;; TODO
;;
;; - online help should work like describe-\(function\|variable\),
;; but get the info from the web. Netscape's documention is in progress,
;; there is no final complete docu on JavaScript1.2, so perhaps we wait.
;;
;; - should include an interface to signing scripts, but zigbert is not
;; available for linux
;;; Bugs
;;
;; - strings in single-quotes do not highlight
;;; Code:
;; user-variables
(defvar javascript-indentation 4
"The width for further indentation in JavaScript mode.")
(defvar javascript-base-help-href "http://developer.netscape.com/library/documentation/communicator/jsguide/"
"URL where the javascript guide can be found.")
(defvar javascript-browse-url-function 'browse-url-w3
"how to view online help.")
(defvar javascript-interactive t
"If t user will be prompted for strings in templates.")
;;;
(defvar javascript-mode-map (make-sparse-keymap)
"Keymap for javascript-mode")
(defvar javascript-mode-syntax-table nil
"Syntax table for javascript-mode.")
(defvar javascript-mode-hook nil
"*Hook run when javascript-mode is started.")
(if javascript-mode-syntax-table
()
(setq javascript-mode-syntax-table (make-syntax-table text-mode-syntax-table))
(modify-syntax-entry ?_ "w" javascript-mode-syntax-table)
(modify-syntax-entry ?' "\"" javascript-mode-syntax-table)
(modify-syntax-entry ?% "_" javascript-mode-syntax-table)
(modify-syntax-entry ?\" "\"" javascript-mode-syntax-table)
(modify-syntax-entry ?\\ "\\" javascript-mode-syntax-table)
(modify-syntax-entry ?. "_" javascript-mode-syntax-table))
(defvar javascript-mode-abbrev-table nil
"Abbrev table used while in javascript-mode.")
(define-abbrev-table 'javascript-mode-abbrev-table ())
(require 'tempo)
(tempo-define-template
"javascript-for"
(list "for (" '(p "initial: ") "; " '(p "condition: ") "; " '(p "increment: ") ") {" 'n> 'p 'n "}" '>)
nil "insert a for loop" nil)
(tempo-define-template
"javascript-for-in"
(list "for (" '(p "variable: ") " in " '(p "object: ") ") {" '> 'n> 'p 'n "}" '>)
nil "insert a for loop" nil)
(tempo-define-template
"javascript-if"
(list "if (" '(p "condition: ") ") {" 'n> 'p 'n "}" '>)
nil "insert an if statement" nil)
(tempo-define-template
"javascript-while"
(list "while (" '(p "condition: ") ") {" 'n> 'p 'n "}" '>)
nil "insert a while statement" nil)
(tempo-define-template
"javascript-do"
(list "do {" '> 'n> 'p 'n "} while(" '(p "condition: ") ");" '>)
nil "insert a do-while statement" nil)
(tempo-define-template
"javascript-with"
(list "with (" '(p "with what? ") ") {" 'n> 'p 'n "}" '>)
nil "insert a with statement" nil)
(tempo-define-template
"javascript-defun"
(list "function " '(p "function name: ") "(" '(p "arguments: ") ") {" 'n> 'p 'n "}" '>)
nil "insert a function definition" nil)
(tempo-define-template
"javascript-switch"
(list "switch (" '(p "variable: ") ") {" '> 'n> "case '" 'p "' :" '> 'n> "break;" '> 'n> "default :" '> 'n> "}" '>)
nil "insert a switch statement" nil)
(tempo-define-template
"javascript-case"
(list "case '" 'p "' :" '> 'n> "break;" '>)
nil "insert a case" nil)
;;; now for the help facility
;;; from man.el
(defun javascript-help (entry)
"Opens a browser via browse-url with a help entry on the current word."
(interactive
(list (let* ((default-entry (current-word))
(input (read-string
(format "Help entry%s: "
(if (string= default-entry "")
""
(format " (default %s)" default-entry))))))
(if (string= input "")
(if (string= default-entry "")
(error "No entry given")
default-entry)
input))))
(let ((url (concat javascript-base-help-href "contents.htm" "#" entry))
(browse-url-browser-function javascript-browse-url-function))
(if (boundp 'browse-url-browser-function)
(progn
(pop-to-buffer " javascript-help")
(apply browse-url-browser-function (list url)))
(error "browse-url not found"))))
(modify-frame-parameters (selected-frame) '((menu-bar-lines . 2)))
(define-key javascript-mode-map [menu-bar javascript]
(cons "JavaScript" javascript-mode-map))
(define-key javascript-mode-map [menu-bar javascript Help]
'("Help" . javascript-help))
(define-key javascript-mode-map [menu-bar javascript for]
'("for" . tempo-template-javascript-for))
(define-key javascript-mode-map [menu-bar javascript forin]
'("for .. in" . tempo-template-javascript-for-in))
(define-key javascript-mode-map [menu-bar javascript if]
'("if" . tempo-template-javascript-if))
(define-key javascript-mode-map [menu-bar javascript while]
'("while" . tempo-template-javascript-while))
(define-key javascript-mode-map [menu-bar javascript with]
'("with" . tempo-template-javascript-with))
(define-key javascript-mode-map [menu-bar javascript switch]
'("switch" . tempo-template-javascript-switch))
(define-key javascript-mode-map [menu-bar javascript case]
'("case" . tempo-template-javascript-case))
(define-key javascript-mode-map [menu-bar javascript do]
'("do" . tempo-template-javascript-do))
(define-key javascript-mode-map [menu-bar javascript function]
'("function" . tempo-template-javascript-defun))
(define-key javascript-mode-map "\C-c\C-h" 'javascript-help)
(define-key javascript-mode-map "\C-c\C-f" 'tempo-template-javascript-for)
(define-key javascript-mode-map "\C-c\C-n" 'tempo-template-javascript-for-in)
(define-key javascript-mode-map "\C-c\C-i" 'tempo-template-javascript-if)
(define-key javascript-mode-map "\C-c\C-w" 'tempo-template-javascript-while)
(define-key javascript-mode-map "\C-c\C-t" 'tempo-template-javascript-with)
(define-key javascript-mode-map "\C-c\C-s" 'tempo-template-javascript-switch)
(define-key javascript-mode-map "\C-c\C-c" 'tempo-template-javascript-case)
(define-key javascript-mode-map "\C-c\C-d" 'tempo-template-javascript-do)
(define-key javascript-mode-map "\C-c(" 'tempo-template-javascript-defun)
(define-key javascript-mode-map "{" 'electric-c-brace)
(define-key javascript-mode-map "}" 'electric-c-brace)
(defun javascript-mode ()
"Major mode for editing javascript code. Basically this is c-mode,
because it does a nice indentation. c-mode gets called via `load-library'.
Colorization is done with hilit19. A few commands are defined through
`tempo.el'. The online help facility gets done through browse-url.el.
\\{javascript-mode-map}
You can set the indentation level by setting the variable
`javascript-indentation' to an integer-value. Default is 4.
The variable javascript-base-help-href sets the URL for the JavaScript guide."
(interactive)
(kill-all-local-variables)
;;(load-library "c-mode")
(require 'browse-url)
(use-local-map javascript-mode-map)
(setq major-mode 'javascript-mode)
(setq mode-name "JavaScript")
(set-syntax-table javascript-mode-syntax-table)
(make-local-variable 'comment-start)
(setq comment-start "// ")
(make-local-variable 'comment-start-skip)
(setq comment-start-skip "/\\*+ *\\|// *")
(make-local-variable 'tempo-interactive)
(setq tempo-interactive javascript-interactive)
(make-local-variable 'indent-line-function)
(setq indent-line-function 'c-indent-line)
(make-local-variable 'c-indent-level)
(setq c-indent-level javascript-indentation)
(run-hooks 'javascript-mode-hook))
;;;
(if (featurep 'hilit19)
(hilit-set-mode-patterns
'javascript-mode
'(("/\\*" "\\*/" comment)
("//" "$" comment)
("\\<function\\>\\s +[^0-9]\\w+\\s *([^)]*)" nil defun)
("\\<\\(abstract\\|boolean\\|break\\|byte\\|case\\|catch\\|char\\|class\\|const\\|continue\\|default\\|delete\\|do\\|double\\|else\\|extends\\|false\\|final\\|finally\\|float\\|for\\|goto\\|if\\|implements\\|in\\|instanceof\\|int\\|interface\\|long\\|native\\|new\\|null\\|package\\|private\\|protected\\|public\\|return\\|short\\|static\\|super\\|switch\\|synchronized\\|this\\|throw\\|throws\\|transient\\|true\\|try\\|typeof\\|var\\|void\\|while\\)\\>" 1 keyword)
("\\<with\\>\\s *([^)]*)" nil include)
("\\<\\(import\\|export\\)\\>\\s +.*" nil include)
("\"[^\\\"]*\\(\\\\.[^\\\"]*\\)*\"" nil string)
("^\\s *\\w+\\s *:\\s *$" nil label))
nil nil)
nil)
(provide 'javascript-mode)
;; javascript-mode.el ends here
;; Local Variables:
;; local-write-file-hooks:(time-stamp)
;; End: