]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/nxhtml/alts/javascript-mozlab.el
bcec39b6c1358ca2d30b0564debac7a4a601c05f
[.emacs.d.git] / emacs / nxhtml / alts / javascript-mozlab.el
1 ;;; javascript.el --- Major mode for editing JavaScript source text
2
3 ;; Copyright (C) 2006 Karl Landström
4
5 ;; Author: Karl Landström <kland@comhem.se>
6 ;; Maintainer: Karl Landström <kland@comhem.se>
7 ;; Version: 2.0 Beta 8
8 ;; Date: 2006-12-26
9 ;; Keywords: languages, oop
10
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27 ;;
28 ;; The main features of this JavaScript mode are syntactic
29 ;; highlighting (enabled with `font-lock-mode' or
30 ;; `global-font-lock-mode'), automatic indentation and filling of
31 ;; comments.
32 ;;
33 ;; This package has (only) been tested with GNU Emacs 21.4 (the latest
34 ;; stable release).
35 ;;
36 ;; Installation:
37 ;;
38 ;; Put this file in a directory where Emacs can find it (`C-h v
39 ;; load-path' for more info). Then add the following lines to your
40 ;; Emacs initialization file:
41 ;; 
42 ;;    (add-to-list 'auto-mode-alist '("\\.js\\'" . javascript-mode))
43 ;;    (autoload 'javascript-mode "javascript" nil t)
44 ;;    
45 ;; General Remarks:
46 ;; 
47 ;; This mode assumes that block comments are not nested inside block
48 ;; comments and that strings do not contain line breaks.
49 ;; 
50 ;; Exported names start with "javascript-" whereas private names start
51 ;; with "js-".
52 ;; 
53 ;; Changes:
54 ;;
55 ;; See javascript.el.changelog.
56
57 ;;; Code:
58
59 (require 'cc-mode)
60 (require 'font-lock)
61 (require 'newcomment)
62
63 (defgroup javascript nil 
64   "Customization variables for `javascript-mode'."
65   :tag "JavaScript"
66   :group 'languages)
67
68 (defcustom javascript-indent-level 4
69   "Number of spaces for each indentation step."
70   :type 'integer
71   :group 'javascript)
72
73 (defcustom javascript-auto-indent-flag t
74   "Automatic indentation with punctuation characters. If non-nil, the
75 current line is indented when certain punctuations are inserted."
76   :type 'boolean
77   :group 'javascript)
78
79
80 ;; --- Keymap ---
81
82 (defvar javascript-mode-map nil 
83   "Keymap used in JavaScript mode.")
84
85 (unless javascript-mode-map 
86   (setq javascript-mode-map (make-sparse-keymap)))
87
88 (when javascript-auto-indent-flag
89   (mapc (lambda (key) 
90           (define-key javascript-mode-map key 'javascript-insert-and-indent))
91         '("{" "}" "(" ")" ":" ";" ",")))
92
93 (defun javascript-insert-and-indent (key)
94   "Run command bound to key and indent current line. Runs the command
95 bound to KEY in the global keymap and indents the current line."
96   (interactive (list (this-command-keys)))
97   (call-interactively (lookup-key (current-global-map) key))
98   (indent-according-to-mode))
99
100
101 ;; --- Syntax Table And Parsing ---
102
103 (defvar javascript-mode-syntax-table
104   (let ((table (make-syntax-table)))
105     (c-populate-syntax-table table)
106
107     ;; The syntax class of underscore should really be `symbol' ("_")
108     ;; but that makes matching of tokens much more complex as e.g.
109     ;; "\\<xyz\\>" matches part of e.g. "_xyz" and "xyz_abc". Defines
110     ;; it as word constituent for now.
111     (modify-syntax-entry ?_ "w" table)
112
113     table)
114   "Syntax table used in JavaScript mode.")
115
116
117 (defun js-re-search-forward-inner (regexp &optional bound count)
118   "Auxiliary function for `js-re-search-forward'."
119   (let ((parse)
120         (saved-point (point-min)))
121     (while (> count 0)
122       (re-search-forward regexp bound)
123       (setq parse (parse-partial-sexp saved-point (point)))
124       (cond ((nth 3 parse)
125              (re-search-forward 
126               (concat "\\([^\\]\\|^\\)" (string (nth 3 parse))) 
127               (save-excursion (end-of-line) (point)) t))
128             ((nth 7 parse)
129              (forward-line))
130             ((or (nth 4 parse)
131                  (and (eq (char-before) ?\/) (eq (char-after) ?\*)))
132              (re-search-forward "\\*/"))
133             (t
134              (setq count (1- count))))
135       (setq saved-point (point))))
136   (point))
137
138
139 (defun js-re-search-forward (regexp &optional bound noerror count)
140   "Search forward but ignore strings and comments. Invokes
141 `re-search-forward' but treats the buffer as if strings and
142 comments have been removed."
143   (let ((saved-point (point))
144         (search-expr 
145          (cond ((null count)
146                 '(js-re-search-forward-inner regexp bound 1))
147                ((< count 0)
148                 '(js-re-search-backward-inner regexp bound (- count)))
149                ((> count 0)
150                 '(js-re-search-forward-inner regexp bound count)))))
151     (condition-case err
152         (eval search-expr)
153       (search-failed
154        (goto-char saved-point)
155        (unless noerror
156          (error (error-message-string err)))))))
157
158
159 (defun js-re-search-backward-inner (regexp &optional bound count)
160   "Auxiliary function for `js-re-search-backward'."
161   (let ((parse)
162         (saved-point (point-min)))
163     (while (> count 0)
164       (re-search-backward regexp bound)
165       (when (and (> (point) (point-min))
166                  (save-excursion (backward-char) (looking-at "/[/*]")))
167         (forward-char))
168       (setq parse (parse-partial-sexp saved-point (point)))
169       (cond ((nth 3 parse)
170              (re-search-backward
171               (concat "\\([^\\]\\|^\\)" (string (nth 3 parse))) 
172               (save-excursion (beginning-of-line) (point)) t))
173             ((nth 7 parse) 
174              (goto-char (nth 8 parse)))
175             ((or (nth 4 parse)
176                  (and (eq (char-before) ?/) (eq (char-after) ?*)))
177              (re-search-backward "/\\*"))
178             (t
179              (setq count (1- count))))))
180   (point))
181
182
183 (defun js-re-search-backward (regexp &optional bound noerror count)
184   "Search backward but ignore strings and comments. Invokes
185 `re-search-backward' but treats the buffer as if strings and
186 comments have been removed."
187   (let ((saved-point (point))
188         (search-expr 
189          (cond ((null count)
190                 '(js-re-search-backward-inner regexp bound 1))
191                ((< count 0)
192                 '(js-re-search-forward-inner regexp bound (- count)))
193                ((> count 0)
194                 '(js-re-search-backward-inner regexp bound count)))))
195     (condition-case err
196         (eval search-expr)
197       (search-failed
198        (goto-char saved-point)
199        (unless noerror
200          (error (error-message-string err)))))))
201
202
203 (defun js-continued-var-decl-list-p ()
204   "Return non-nil if point is inside a continued variable declaration
205 list."
206   (interactive)
207   (let ((start (save-excursion (js-re-search-backward "\\<var\\>" nil t))))
208     (and start
209          (save-excursion (re-search-backward "\n" start t))
210          (not (save-excursion 
211                 (js-re-search-backward 
212                  ";\\|[^, \t][ \t]*\\(/[/*]\\|$\\)" start t))))))
213
214
215 ;; --- Font Lock ---
216
217 (defun js-inside-param-list-p ()
218   "Return non-nil if point is inside a function parameter list."
219   (condition-case err
220       (save-excursion
221         (up-list -1)
222         (and (looking-at "(")
223              (progn (backward-word 1)
224                     (or (looking-at "function")
225                         (progn (backward-word 1) (looking-at "function"))))))
226     (error nil)))
227
228
229 (defconst js-function-heading-1-re 
230   "^[ \t]*function[ \t]+\\(\\w+\\)"
231   "Regular expression matching the start of a function header.")
232
233 (defconst js-function-heading-2-re 
234   "^[ \t]*\\(\\w+\\)[ \t]*:[ \t]*function\\>"
235   "Regular expression matching the start of a function entry in
236   an associative array.")
237
238 (defconst js-keyword-re
239   (regexp-opt '("abstract" "break" "case" "catch" "class" "const"
240                 "continue" "debugger" "default" "delete" "do" "else" 
241                 "enum" "export" "extends" "final" "finally" "for" 
242                 "function" "goto" "if" "implements" "import" "in" 
243                 "instanceof" "interface" "native" "new" "package" 
244                 "private" "protected" "public" "return" "static" 
245                 "super" "switch" "synchronized" "this" "throw" 
246                 "throws" "transient" "try" "typeof" "var" "void" 
247                 "volatile" "while" "with"
248                 "let") 'words)
249   "Regular expression matching any JavaScript keyword.")
250
251 (defconst js-basic-type-re
252   (regexp-opt '("boolean" "byte" "char" "double" "float" "int" "long"
253                 "short" "void") 'words)
254   "Regular expression matching any predefined type in JavaScript.")
255
256 (defconst js-constant-re
257   (regexp-opt '("false" "null" "true") 'words)
258   "Regular expression matching any future reserved words in JavaScript.")
259
260
261 (defconst js-font-lock-keywords-1
262   (list 
263    "\\<import\\>" 
264    (list js-function-heading-1-re 1 font-lock-function-name-face)
265    (list js-function-heading-2-re 1 font-lock-function-name-face)
266    (list "[=(][ \t]*\\(/.*?[^\\]/\\w*\\)" 1 font-lock-string-face))
267   "Level one font lock.")
268
269 (defconst js-font-lock-keywords-2
270   (append js-font-lock-keywords-1
271           (list (list js-keyword-re 1 font-lock-keyword-face)
272                 (cons js-basic-type-re font-lock-type-face)
273                 (cons js-constant-re font-lock-constant-face)))
274   "Level two font lock.")
275
276
277 ;; Limitations with variable declarations: There seems to be no
278 ;; sensible way to highlight variables occuring after an initialized
279 ;; variable in a variable list. For instance, in
280 ;;
281 ;;    var x, y = f(a, b), z
282 ;;
283 ;; z will not be highlighted.
284
285 (defconst js-font-lock-keywords-3
286   (append 
287    js-font-lock-keywords-2
288    (list 
289
290     ;; variable declarations
291     (list
292      (concat "\\<\\(const\\|var\\)\\>\\|" js-basic-type-re)
293      (list "\\(\\w+\\)[ \t]*\\([=;].*\\|,\\|/[/*]\\|$\\)"
294            nil
295            nil
296            '(1 font-lock-variable-name-face)))
297
298     ;; continued variable declaration list
299     (list
300      (concat "^[ \t]*\\w+[ \t]*\\([,;=]\\|/[/*]\\|$\\)")
301      (list "\\(\\w+\\)[ \t]*\\([=;].*\\|,\\|/[/*]\\|$\\)"
302            '(if (save-excursion (backward-char) (js-continued-var-decl-list-p))
303                 (backward-word 1) 
304               (end-of-line))
305            '(end-of-line)
306            '(1 font-lock-variable-name-face)))
307
308     ;; formal parameters
309     (list
310      (concat "\\<function\\>\\([ \t]+\\w+\\)?[ \t]*([ \t]*\\w")
311      (list "\\(\\w+\\)\\([ \t]*).*\\)?"
312            '(backward-char)
313            '(end-of-line)
314            '(1 font-lock-variable-name-face)))
315     
316     ;; continued formal parameter list
317     (list
318      (concat "^[ \t]*\\w+[ \t]*[,)]")
319      (list "\\w+"
320            '(if (save-excursion (backward-char) (js-inside-param-list-p))
321                 (backward-word 1) 
322               (end-of-line))
323            '(end-of-line)
324            '(0 font-lock-variable-name-face)))))
325   "Level three font lock.")
326
327 (defconst js-font-lock-keywords
328   '(js-font-lock-keywords-3 js-font-lock-keywords-1 js-font-lock-keywords-2
329                             js-font-lock-keywords-3)
330   "See `font-lock-keywords'.")
331
332
333 ;; --- Indentation ---
334
335 (defconst js-possibly-braceless-keyword-re
336   (regexp-opt
337    '("catch" "do" "else" "finally" "for" "if" "try" "while" "with" "let")
338    'words)
339   "Regular expression matching keywords that are optionally
340   followed by an opening brace.")
341
342 (defconst js-indent-operator-re
343   (concat "[-+*/%<>=&^|?:.]\\([^-+*/]\\|$\\)\\|"
344           (regexp-opt '("in" "instanceof") 'words))
345   "Regular expression matching operators that affect indentation
346   of continued expressions.")
347
348
349 (defun js-looking-at-operator-p ()
350   "Return non-nil if text after point is an operator (that is not
351 a comma)."
352   (save-match-data
353     (and (looking-at js-indent-operator-re)
354          (or (not (looking-at ":"))
355              (save-excursion
356                (and (js-re-search-backward "[?:{]\\|\\<case\\>" nil t)
357                     (looking-at "?")))))))
358
359
360 (defun js-continued-expression-p ()
361   "Returns non-nil if the current line continues an expression."
362   (save-excursion
363     (back-to-indentation)
364     (or (js-looking-at-operator-p)
365         (and (js-re-search-backward "\n" nil t)
366              (progn 
367                (skip-chars-backward " \t")
368                (backward-char)
369                (and (> (point) (point-min))
370                     (save-excursion (backward-char) (not (looking-at "[/*]/")))
371                     (js-looking-at-operator-p)
372                     (and (progn (backward-char)
373                                 (not (looking-at "++\\|--\\|/[/*]"))))))))))
374
375
376 (defun js-end-of-do-while-loop-p ()
377   "Returns non-nil if word after point is `while' of a do-while
378 statement, else returns nil. A braceless do-while statement
379 spanning several lines requires that the start of the loop is
380 indented to the same column as the current line."
381   (interactive)
382   (save-excursion
383     (save-match-data
384       (when (looking-at "\\s-*\\<while\\>")
385         (if (save-excursion 
386               (skip-chars-backward "[ \t\n]*}")
387               (looking-at "[ \t\n]*}"))
388             (save-excursion 
389               (backward-list) (backward-word 1) (looking-at "\\<do\\>"))
390           (js-re-search-backward "\\<do\\>" (point-at-bol) t)
391           (or (looking-at "\\<do\\>")
392               (let ((saved-indent (current-indentation)))
393                 (while (and (js-re-search-backward "^[ \t]*\\<" nil t)
394                             (/= (current-indentation) saved-indent)))
395                 (and (looking-at "[ \t]*\\<do\\>")
396                      (not (js-re-search-forward 
397                            "\\<while\\>" (point-at-eol) t))
398                      (= (current-indentation) saved-indent)))))))))
399
400
401 (defun js-ctrl-statement-indentation ()
402   "Returns the proper indentation of the current line if it
403 starts the body of a control statement without braces, else
404 returns nil."
405   (save-excursion
406     (back-to-indentation)
407     (when (save-excursion
408             (and (not (looking-at "[{]"))
409                  (progn
410                    (js-re-search-backward "[[:graph:]]" nil t)
411                    (forward-char)
412                    (when (= (char-before) ?\)) (backward-list))
413                    (skip-syntax-backward " ")
414                    (skip-syntax-backward "w")
415                    (looking-at js-possibly-braceless-keyword-re))
416                  (not (js-end-of-do-while-loop-p))))
417       (save-excursion
418         (goto-char (match-beginning 0))
419         (+ (current-indentation) javascript-indent-level)))))
420
421
422 (defun js-proper-indentation (parse-status)
423   "Return the proper indentation for the current line."
424   (save-excursion
425     (back-to-indentation)
426     (let ((ctrl-stmt-indent (js-ctrl-statement-indentation))
427           (same-indent-p (looking-at "[]})]\\|\\<case\\>\\|\\<default\\>"))
428           (continued-expr-p (js-continued-expression-p)))
429       (cond (ctrl-stmt-indent)
430             ((js-continued-var-decl-list-p)
431              (js-re-search-backward "\\<var\\>" nil t)
432              (+ (current-indentation) javascript-indent-level))
433             ((nth 1 parse-status)
434              (goto-char (nth 1 parse-status))
435              (if (looking-at "[({[][ \t]*\\(/[/*]\\|$\\)")
436                  (progn
437                    (skip-syntax-backward " ")
438                    (when (= (char-before) ?\)) (backward-list))
439                    (back-to-indentation)
440                    (cond (same-indent-p
441                           (current-column))
442                          (continued-expr-p
443                           (+ (current-column) (* 2 javascript-indent-level)))
444                          (t
445                           (+ (current-column) javascript-indent-level))))
446                (unless same-indent-p
447                  (forward-char)
448                  (skip-chars-forward " \t"))
449                (current-column)))
450             (continued-expr-p javascript-indent-level)
451             (t 0)))))
452
453
454 (defun javascript-indent-line ()
455   "Indent the current line as JavaScript source text."
456   (interactive)
457   (let ((parse-status 
458          (save-excursion (parse-partial-sexp (point-min) (point-at-bol))))
459         (offset (- (current-column) (current-indentation))))
460     (when (not (nth 8 parse-status))
461       (indent-line-to (js-proper-indentation parse-status))
462       (when (> offset 0) (forward-char offset)))))
463
464
465 ;; --- Filling ---
466
467 ;; FIXME: It should be possible to use the more sofisticated function
468 ;; `c-fill-paragraph' in `cc-cmds.el' instead. However, just setting
469 ;; `fill-paragraph-function' to `c-fill-paragraph' does not work;
470 ;; inside `c-fill-paragraph', `fill-paragraph-function' evaluates to
471 ;; nil!?
472
473 (defun js-backward-paragraph ()
474   "Move backward to start of paragraph. Postcondition: Point is at
475 beginning of buffer or the previous line contains only whitespace."
476   (forward-line -1)
477   (while (not (or (bobp) (looking-at "^[ \t]*$")))
478     (forward-line -1))
479   (when (not (bobp)) (forward-line 1)))
480
481
482 (defun js-forward-paragraph ()
483   "Move forward to end of paragraph. Postcondition: Point is at
484 end of buffer or the next line contains only whitespace."
485   (forward-line 1)
486   (while (not (or (eobp) (looking-at "^[ \t]*$")))
487     (forward-line 1))
488   (when (not (eobp)) (backward-char 1)))
489  
490
491 (defun js-fill-block-comment-paragraph (parse-status justify)
492   "Fill current paragraph as a block comment. PARSE-STATUS is the
493 result of `parse-partial-regexp' from beginning of buffer to
494 point. JUSTIFY has the same meaning as in `fill-paragraph'."
495   (let ((offset (save-excursion 
496                   (goto-char (nth 8 parse-status)) (current-indentation))))
497     (save-excursion
498       (save-restriction
499         (narrow-to-region (save-excursion 
500                             (goto-char (nth 8 parse-status)) (point-at-bol))
501                           (save-excursion 
502                             (goto-char (nth 8 parse-status))
503                             (re-search-forward "*/")))
504         (narrow-to-region (save-excursion 
505                             (js-backward-paragraph)
506                             (when (looking-at "^[ \t]*$") (forward-line 1))
507                             (point))
508                           (save-excursion 
509                             (js-forward-paragraph) 
510                             (when (looking-at "^[ \t]*$") (backward-char))
511                             (point)))
512         (goto-char (point-min))
513         (while (not (eobp))
514           (delete-horizontal-space)
515           (forward-line 1))
516         (let ((fill-column (- fill-column offset))
517               (fill-paragraph-function nil))
518           (fill-paragraph justify))
519
520         ;; In Emacs 21.4 as opposed to CVS Emacs 22,
521         ;; `fill-paragraph' seems toadd a newline at the end of the
522         ;; paragraph. Remove it!
523         (goto-char (point-max))
524         (when (looking-at "^$") (backward-delete-char 1))
525
526         (goto-char (point-min))
527         (while (not (eobp))
528           (indent-to offset)
529           (forward-line 1))))))
530
531
532 (defun js-sline-comment-par-start ()
533   "Return point at the beginning of the line where the current
534 single-line comment paragraph starts."
535   (save-excursion
536     (beginning-of-line)
537     (while (and (not (bobp)) 
538                 (looking-at "^[ \t]*//[ \t]*[[:graph:]]"))
539       (forward-line -1))
540     (unless (bobp) (forward-line 1))
541     (point)))
542
543
544 (defun js-sline-comment-par-end ()
545   "Return point at end of current single-line comment paragraph."
546   (save-excursion
547     (beginning-of-line)
548     (while (and (not (eobp)) 
549                 (looking-at "^[ \t]*//[ \t]*[[:graph:]]"))
550       (forward-line 1))
551     (unless (bobp) (backward-char))
552     (point)))
553
554
555 (defun js-sline-comment-offset (line)
556   "Return the column at the start of the current single-line
557 comment paragraph."
558   (save-excursion 
559     (goto-line line)
560     (re-search-forward "//" (point-at-eol))
561     (goto-char (match-beginning 0))
562     (current-column)))
563
564
565 (defun js-sline-comment-text-offset (line)
566   "Return the column at the start of the text of the current
567 single-line comment paragraph."
568   (save-excursion
569     (goto-line line)
570     (re-search-forward "//[ \t]*" (point-at-eol))
571     (current-column)))
572
573
574 (defun js-at-empty-sline-comment-p ()
575   "Return non-nil if inside an empty single-line comment."
576   (and (save-excursion
577          (beginning-of-line)
578          (not (looking-at "^.*//.*[[:graph:]]")))
579        (save-excursion
580          (re-search-backward "//" (point-at-bol) t))))
581
582          
583 (defun js-fill-sline-comments (parse-status justify)
584   "Fill current paragraph as a sequence of single-line comments.
585 PARSE-STATUS is the result of `parse-partial-regexp' from
586 beginning of buffer to point. JUSTIFY has the same meaning as in
587 `fill-paragraph'."
588   (when (not (js-at-empty-sline-comment-p))
589     (let* ((start (js-sline-comment-par-start))
590            (start-line (1+ (count-lines (point-min) start)))
591            (end (js-sline-comment-par-end))
592            (offset (js-sline-comment-offset start-line))
593            (text-offset (js-sline-comment-text-offset start-line)))
594       (save-excursion
595         (save-restriction
596           (narrow-to-region start end)
597           (goto-char (point-min))
598           (while (re-search-forward "^[ \t]*//[ \t]*" nil t)
599             (replace-match "")
600             (forward-line 1))
601           (let ((fill-paragraph-function nil)
602                 (fill-column (- fill-column text-offset)))
603             (fill-paragraph justify))
604
605           ;; In Emacs 21.4 as opposed to CVS Emacs 22,
606           ;; `fill-paragraph' seems toadd a newline at the end of the
607           ;; paragraph. Remove it!
608           (goto-char (point-max))
609           (when (looking-at "^$") (backward-delete-char 1))
610
611           (goto-char (point-min))
612           (while (not (eobp))
613             (indent-to offset)
614             (insert "//")
615             (indent-to text-offset)
616             (forward-line 1)))))))
617   
618
619 (defun js-trailing-comment-p (parse-status)
620   "Return non-nil if inside a trailing comment. PARSE-STATUS is
621 the result of `parse-partial-regexp' from beginning of buffer to
622 point."
623   (save-excursion 
624     (when (nth 4 parse-status)
625       (goto-char (nth 8 parse-status))
626       (skip-chars-backward " \t")
627       (not (bolp)))))
628
629
630 (defun js-block-comment-p (parse-status)
631   "Return non-nil if inside a block comment. PARSE-STATUS is the
632 result of `parse-partial-regexp' from beginning of buffer to
633 point."
634   (save-excursion 
635     (save-match-data
636       (when (nth 4 parse-status)
637         (goto-char (nth 8 parse-status))
638         (looking-at "/\\*")))))
639
640
641 (defun javascript-fill-paragraph (&optional justify)
642   "If inside a comment, fill the current comment paragraph.
643 Trailing comments are ignored."
644   (interactive)
645   (let ((parse-status (parse-partial-sexp (point-min) (point))))
646     (when (and (nth 4 parse-status) 
647                (not (js-trailing-comment-p parse-status)))
648       (if (js-block-comment-p parse-status)
649           (js-fill-block-comment-paragraph parse-status justify)
650         (js-fill-sline-comments parse-status justify))))
651   t)
652
653
654 ;; --- Imenu ---
655
656 (defconst js-imenu-generic-expression 
657   (list
658    (list
659     nil 
660     "function\\s-+\\(\\w+\\)\\s-*("
661     1))
662   "Regular expression matching top level procedures. Used by imenu.")
663
664
665 ;; --- Main Function ---
666
667 ;;;###autoload
668 (defun javascript-mode ()
669   "Major mode for editing JavaScript source text.
670
671 Key bindings:
672
673 \\{javascript-mode-map}"
674   (interactive)
675   (kill-all-local-variables)
676
677   (use-local-map javascript-mode-map)
678   (set-syntax-table javascript-mode-syntax-table)
679   (set (make-local-variable 'indent-line-function) 'javascript-indent-line)
680   (set (make-local-variable 'font-lock-defaults) (list js-font-lock-keywords))
681
682   (set (make-local-variable 'parse-sexp-ignore-comments) t) 
683
684   ;; Comments
685   (setq comment-start "// ")
686   (setq comment-end "")
687   (set (make-local-variable 'fill-paragraph-function) 
688        'javascript-fill-paragraph)
689
690   ;; Make c-mark-function work
691   (setq c-nonsymbol-token-regexp "!=\\|%=\\|&[&=]\\|\\*[/=]\\|\\+[+=]\\|-[=-]\\|/[*/=]\\|<\\(?:<=\\|[<=]\\)\\|==\\|>\\(?:>\\(?:>=\\|[=>]\\)\\|[=>]\\)\\|\\^=\\||[=|]\\|[]!%&(-,./:-?[{-~^-]"
692         c-stmt-delim-chars "^;{}?:"
693         c-syntactic-ws-end "[ \n        
694 \v\f/]"
695         c-syntactic-eol "\\(\\s \\|/\\*\\([^*\n
696 ]\\|\\*[^/\n
697 ]\\)*\\*/\\)*\\(\\(/\\*\\([^*\n
698 ]\\|\\*[^/\n
699 ]\\)*\\|\\\\\\)?$\\|//\\)")
700
701   ;; Imenu
702   (setq imenu-case-fold-search nil)
703   (set (make-local-variable 'imenu-generic-expression)
704        js-imenu-generic-expression)
705
706   (setq major-mode 'javascript-mode)
707   (setq mode-name "JavaScript")
708   (run-hooks 'javascript-mode-hook))
709
710
711 (provide 'javascript-mode)
712 ;;; javascript.el ends here