1 ;;; javascript.el --- Major mode for editing JavaScript source text
3 ;; Copyright (C) 2006 Karl Landström
5 ;; Author: Karl Landström <kland@comhem.se>
6 ;; Maintainer: Karl Landström <kland@comhem.se>
9 ;; Keywords: languages, oop
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)
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.
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.
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
33 ;; This package has (only) been tested with GNU Emacs 21.4 (the latest
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:
42 ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . javascript-mode))
43 ;; (autoload 'javascript-mode "javascript" nil t)
47 ;; This mode assumes that block comments are not nested inside block
48 ;; comments and that strings do not contain line breaks.
50 ;; Exported names start with "javascript-" whereas private names start
55 ;; See javascript.el.changelog.
63 (defgroup javascript nil
64 "Customization variables for `javascript-mode'."
68 (defcustom javascript-indent-level 4
69 "Number of spaces for each indentation step."
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."
82 (defvar javascript-mode-map nil
83 "Keymap used in JavaScript mode.")
85 (unless javascript-mode-map
86 (setq javascript-mode-map (make-sparse-keymap)))
88 (when javascript-auto-indent-flag
90 (define-key javascript-mode-map key 'javascript-insert-and-indent))
91 '("{" "}" "(" ")" ":" ";" ",")))
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))
101 ;; --- Syntax Table And Parsing ---
103 (defvar javascript-mode-syntax-table
104 (let ((table (make-syntax-table)))
105 (c-populate-syntax-table table)
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)
114 "Syntax table used in JavaScript mode.")
117 (defun js-re-search-forward-inner (regexp &optional bound count)
118 "Auxiliary function for `js-re-search-forward'."
120 (saved-point (point-min)))
122 (re-search-forward regexp bound)
123 (setq parse (parse-partial-sexp saved-point (point)))
126 (concat "\\([^\\]\\|^\\)" (string (nth 3 parse)))
127 (save-excursion (end-of-line) (point)) t))
131 (and (eq (char-before) ?\/) (eq (char-after) ?\*)))
132 (re-search-forward "\\*/"))
134 (setq count (1- count))))
135 (setq saved-point (point))))
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))
146 '(js-re-search-forward-inner regexp bound 1))
148 '(js-re-search-backward-inner regexp bound (- count)))
150 '(js-re-search-forward-inner regexp bound count)))))
154 (goto-char saved-point)
156 (error (error-message-string err)))))))
159 (defun js-re-search-backward-inner (regexp &optional bound count)
160 "Auxiliary function for `js-re-search-backward'."
162 (saved-point (point-min)))
164 (re-search-backward regexp bound)
165 (when (and (> (point) (point-min))
166 (save-excursion (backward-char) (looking-at "/[/*]")))
168 (setq parse (parse-partial-sexp saved-point (point)))
171 (concat "\\([^\\]\\|^\\)" (string (nth 3 parse)))
172 (save-excursion (beginning-of-line) (point)) t))
174 (goto-char (nth 8 parse)))
176 (and (eq (char-before) ?/) (eq (char-after) ?*)))
177 (re-search-backward "/\\*"))
179 (setq count (1- count))))))
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))
190 '(js-re-search-backward-inner regexp bound 1))
192 '(js-re-search-forward-inner regexp bound (- count)))
194 '(js-re-search-backward-inner regexp bound count)))))
198 (goto-char saved-point)
200 (error (error-message-string err)))))))
203 (defun js-continued-var-decl-list-p ()
204 "Return non-nil if point is inside a continued variable declaration
207 (let ((start (save-excursion (js-re-search-backward "\\<var\\>" nil t))))
209 (save-excursion (re-search-backward "\n" start t))
211 (js-re-search-backward
212 ";\\|[^, \t][ \t]*\\(/[/*]\\|$\\)" start t))))))
217 (defun js-inside-param-list-p ()
218 "Return non-nil if point is inside a function parameter list."
222 (and (looking-at "(")
223 (progn (backward-word 1)
224 (or (looking-at "function")
225 (progn (backward-word 1) (looking-at "function"))))))
229 (defconst js-function-heading-1-re
230 "^[ \t]*function[ \t]+\\(\\w+\\)"
231 "Regular expression matching the start of a function header.")
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.")
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"
249 "Regular expression matching any JavaScript keyword.")
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.")
256 (defconst js-constant-re
257 (regexp-opt '("false" "null" "true") 'words)
258 "Regular expression matching any future reserved words in JavaScript.")
261 (defconst js-font-lock-keywords-1
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.")
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.")
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
281 ;; var x, y = f(a, b), z
283 ;; z will not be highlighted.
285 (defconst js-font-lock-keywords-3
287 js-font-lock-keywords-2
290 ;; variable declarations
292 (concat "\\<\\(const\\|var\\)\\>\\|" js-basic-type-re)
293 (list "\\(\\w+\\)[ \t]*\\([=;].*\\|,\\|/[/*]\\|$\\)"
296 '(1 font-lock-variable-name-face)))
298 ;; continued variable declaration list
300 (concat "^[ \t]*\\w+[ \t]*\\([,;=]\\|/[/*]\\|$\\)")
301 (list "\\(\\w+\\)[ \t]*\\([=;].*\\|,\\|/[/*]\\|$\\)"
302 '(if (save-excursion (backward-char) (js-continued-var-decl-list-p))
306 '(1 font-lock-variable-name-face)))
310 (concat "\\<function\\>\\([ \t]+\\w+\\)?[ \t]*([ \t]*\\w")
311 (list "\\(\\w+\\)\\([ \t]*).*\\)?"
314 '(1 font-lock-variable-name-face)))
316 ;; continued formal parameter list
318 (concat "^[ \t]*\\w+[ \t]*[,)]")
320 '(if (save-excursion (backward-char) (js-inside-param-list-p))
324 '(0 font-lock-variable-name-face)))))
325 "Level three font lock.")
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'.")
333 ;; --- Indentation ---
335 (defconst js-possibly-braceless-keyword-re
337 '("catch" "do" "else" "finally" "for" "if" "try" "while" "with" "let")
339 "Regular expression matching keywords that are optionally
340 followed by an opening brace.")
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.")
349 (defun js-looking-at-operator-p ()
350 "Return non-nil if text after point is an operator (that is not
353 (and (looking-at js-indent-operator-re)
354 (or (not (looking-at ":"))
356 (and (js-re-search-backward "[?:{]\\|\\<case\\>" nil t)
357 (looking-at "?")))))))
360 (defun js-continued-expression-p ()
361 "Returns non-nil if the current line continues an expression."
363 (back-to-indentation)
364 (or (js-looking-at-operator-p)
365 (and (js-re-search-backward "\n" nil t)
367 (skip-chars-backward " \t")
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 "++\\|--\\|/[/*]"))))))))))
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."
384 (when (looking-at "\\s-*\\<while\\>")
386 (skip-chars-backward "[ \t\n]*}")
387 (looking-at "[ \t\n]*}"))
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)))))))))
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
406 (back-to-indentation)
407 (when (save-excursion
408 (and (not (looking-at "[{]"))
410 (js-re-search-backward "[[:graph:]]" nil t)
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))))
418 (goto-char (match-beginning 0))
419 (+ (current-indentation) javascript-indent-level)))))
422 (defun js-proper-indentation (parse-status)
423 "Return the proper indentation for the current line."
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]*\\(/[/*]\\|$\\)")
437 (skip-syntax-backward " ")
438 (when (= (char-before) ?\)) (backward-list))
439 (back-to-indentation)
443 (+ (current-column) (* 2 javascript-indent-level)))
445 (+ (current-column) javascript-indent-level))))
446 (unless same-indent-p
448 (skip-chars-forward " \t"))
450 (continued-expr-p javascript-indent-level)
454 (defun javascript-indent-line ()
455 "Indent the current line as JavaScript source text."
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)))))
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
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."
477 (while (not (or (bobp) (looking-at "^[ \t]*$")))
479 (when (not (bobp)) (forward-line 1)))
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."
486 (while (not (or (eobp) (looking-at "^[ \t]*$")))
488 (when (not (eobp)) (backward-char 1)))
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))))
499 (narrow-to-region (save-excursion
500 (goto-char (nth 8 parse-status)) (point-at-bol))
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))
509 (js-forward-paragraph)
510 (when (looking-at "^[ \t]*$") (backward-char))
512 (goto-char (point-min))
514 (delete-horizontal-space)
516 (let ((fill-column (- fill-column offset))
517 (fill-paragraph-function nil))
518 (fill-paragraph justify))
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))
526 (goto-char (point-min))
529 (forward-line 1))))))
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."
537 (while (and (not (bobp))
538 (looking-at "^[ \t]*//[ \t]*[[:graph:]]"))
540 (unless (bobp) (forward-line 1))
544 (defun js-sline-comment-par-end ()
545 "Return point at end of current single-line comment paragraph."
548 (while (and (not (eobp))
549 (looking-at "^[ \t]*//[ \t]*[[:graph:]]"))
551 (unless (bobp) (backward-char))
555 (defun js-sline-comment-offset (line)
556 "Return the column at the start of the current single-line
560 (re-search-forward "//" (point-at-eol))
561 (goto-char (match-beginning 0))
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."
570 (re-search-forward "//[ \t]*" (point-at-eol))
574 (defun js-at-empty-sline-comment-p ()
575 "Return non-nil if inside an empty single-line comment."
578 (not (looking-at "^.*//.*[[:graph:]]")))
580 (re-search-backward "//" (point-at-bol) t))))
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
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)))
596 (narrow-to-region start end)
597 (goto-char (point-min))
598 (while (re-search-forward "^[ \t]*//[ \t]*" nil t)
601 (let ((fill-paragraph-function nil)
602 (fill-column (- fill-column text-offset)))
603 (fill-paragraph justify))
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))
611 (goto-char (point-min))
615 (indent-to text-offset)
616 (forward-line 1)))))))
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
624 (when (nth 4 parse-status)
625 (goto-char (nth 8 parse-status))
626 (skip-chars-backward " \t")
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
636 (when (nth 4 parse-status)
637 (goto-char (nth 8 parse-status))
638 (looking-at "/\\*")))))
641 (defun javascript-fill-paragraph (&optional justify)
642 "If inside a comment, fill the current comment paragraph.
643 Trailing comments are ignored."
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))))
656 (defconst js-imenu-generic-expression
660 "function\\s-+\\(\\w+\\)\\s-*("
662 "Regular expression matching top level procedures. Used by imenu.")
665 ;; --- Main Function ---
668 (defun javascript-mode ()
669 "Major mode for editing JavaScript source text.
673 \\{javascript-mode-map}"
675 (kill-all-local-variables)
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))
682 (set (make-local-variable 'parse-sexp-ignore-comments) t)
685 (setq comment-start "// ")
686 (setq comment-end "")
687 (set (make-local-variable 'fill-paragraph-function)
688 'javascript-fill-paragraph)
690 ;; Make c-mark-function work
691 (setq c-nonsymbol-token-regexp "!=\\|%=\\|&[&=]\\|\\*[/=]\\|\\+[+=]\\|-[=-]\\|/[*/=]\\|<\\(?:<=\\|[<=]\\)\\|==\\|>\\(?:>\\(?:>=\\|[=>]\\)\\|[=>]\\)\\|\\^=\\||[=|]\\|[]!%&(-,./:-?[{-~^-]"
692 c-stmt-delim-chars "^;{}?:"
693 c-syntactic-ws-end "[ \n
695 c-syntactic-eol "\\(\\s \\|/\\*\\([^*\n
697 ]\\)*\\*/\\)*\\(\\(/\\*\\([^*\n
699 ]\\)*\\|\\\\\\)?$\\|//\\)")
702 (setq imenu-case-fold-search nil)
703 (set (make-local-variable 'imenu-generic-expression)
704 js-imenu-generic-expression)
706 (setq major-mode 'javascript-mode)
707 (setq mode-name "JavaScript")
708 (run-hooks 'javascript-mode-hook))
711 (provide 'javascript-mode)
712 ;;; javascript.el ends here