merge emacs-23
[bpt/emacs.git] / lisp / progmodes / js.el
index 96b4949..60ed14a 100644 (file)
@@ -1,6 +1,6 @@
 ;;; js.el --- Major mode for editing JavaScript
 
-;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
 
 ;; Author: Karl Landstrom <karl.landstrom@brgeight.se>
 ;;         Daniel Colascione <dan.colascione@gmail.com>
@@ -436,6 +436,13 @@ The value must be no less than minus `js-indent-level'."
   :type 'integer
   :group 'js)
 
+(defcustom js-auto-indent-flag t
+  "Whether to automatically indent when typing punctuation characters.
+If non-nil, the characters {}();,: also indent the current line
+in Javascript mode."
+  :type 'boolean
+  :group 'js)
+
 (defcustom js-flat-functions nil
   "Treat nested functions as top-level functions in `js-mode'.
 This applies to function movement, marking, and so on."
@@ -467,8 +474,7 @@ for preventing Firefox from stealing the keyboard focus."
 (defcustom js-js-tmpdir
   "~/.emacs.d/js/js"
   "Temporary directory used by `js-mode' to communicate with Mozilla.
-This directory must be readable and writable by both Mozilla and
-Emacs."
+This directory must be readable and writable by both Mozilla and Emacs."
   :type 'directory
   :group 'js)
 
@@ -483,21 +489,39 @@ getting timeout messages."
 
 (defvar js-mode-map
   (let ((keymap (make-sparse-keymap)))
+    (mapc (lambda (key)
+           (define-key keymap key #'js-insert-and-indent))
+         '("{" "}" "(" ")" ":" ";" ","))
     (define-key keymap [(control ?c) (meta ?:)] #'js-eval)
     (define-key keymap [(control ?c) (control ?j)] #'js-set-js-context)
     (define-key keymap [(control meta ?x)] #'js-eval-defun)
     (define-key keymap [(meta ?.)] #'js-find-symbol)
     (easy-menu-define nil keymap "Javascript Menu"
       '("Javascript"
-        ["Select new Mozilla context…" js-set-js-context
+        ["Select New Mozilla Context..." js-set-js-context
          (fboundp #'inferior-moz-process)]
-        ["Evaluate expression in Mozilla context…" js-eval
+        ["Evaluate Expression in Mozilla Context..." js-eval
          (fboundp #'inferior-moz-process)]
-        ["Send current function to Mozilla…" js-eval-defun
+        ["Send Current Function to Mozilla..." js-eval-defun
          (fboundp #'inferior-moz-process)]))
     keymap)
   "Keymap for `js-mode'.")
 
+(defun js-insert-and-indent (key)
+  "Run the command bound to KEY, and indent if necessary.
+Indentation does not take place if point is in a string or
+comment."
+  (interactive (list (this-command-keys)))
+  (call-interactively (lookup-key (current-global-map) key))
+  (let ((syntax (save-restriction (widen) (syntax-ppss))))
+    (when (or (and (not (nth 8 syntax))
+                   js-auto-indent-flag)
+              (and (nth 4 syntax)
+                   (eq (current-column)
+                       (1+ (current-indentation)))))
+      (indent-according-to-mode))))
+
+
 ;;; Syntax table and parsing
 
 (defvar js-mode-syntax-table
@@ -2603,7 +2627,7 @@ with `js--js-encode-value'."
        js-js-timeout))))
 
 (defsubst js--js-not (value)
-  (memq value '(nil false undefined)))
+  (memq value '(nil null false undefined)))
 
 (defsubst js--js-true (value)
   (not (js--js-not value)))
@@ -3302,6 +3326,13 @@ Key bindings:
         comment-start-skip "\\(//+\\|/\\*+\\)\\s *")
 
   (let ((c-buffer-is-cc-mode t))
+    ;; FIXME: These are normally set by `c-basic-common-init'.  Should
+    ;; we call it instead?  (Bug#6071)
+    (make-local-variable 'paragraph-start)
+    (make-local-variable 'paragraph-separate)
+    (make-local-variable 'paragraph-ignore-fill-prefix)
+    (make-local-variable 'adaptive-fill-mode)
+    (make-local-variable 'adaptive-fill-regexp)
     (c-setup-paragraph-variables))
 
   (set (make-local-variable 'syntax-begin-function)
@@ -3320,6 +3351,7 @@ Key bindings:
   (let (font-lock-keywords) ; leaves syntactic keywords intact
     (font-lock-fontify-buffer)))
 
+;;;###autoload
 (defalias 'javascript-mode 'js-mode)
 
 (eval-after-load 'folding