(defvar inferior-moz-buffer)
(defvar moz-repl-name)
(defvar ido-cur-list)
+(defvar electric-layout-rules)
(declare-function ido-mode "ido")
(declare-function inferior-moz-process "ext:mozrepl" ())
(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)
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
js--font-lock-keywords-3)
"Font lock keywords for `js-mode'. See `font-lock-keywords'.")
-;; XXX: Javascript can continue a regexp literal across lines so long
-;; as the newline is escaped with \. Account for that in the regexp
-;; below.
-(eval-and-compile
- (defconst js--regexp-literal
- (concat
- ;; We want to match regular expressions only at the beginning of
- ;; expressions.
- ;; FIXME: Should we also allow /regexp/ after infix operators such as +,
- ;; /, -, *, >, ...?
- "\\(?:\\`\\|[=([{,:;]\\)\\(?:\\s-\\|\n\\)*"
- "\\(/\\)\\(?:\\\\.\\|[^/*\\]\\)\\(?:\\\\.\\|[^/\\]\\)*\\(/\\)")
- "Regexp matching a JavaScript regular expression literal.
-Match groups 1 and 2 are the characters forming the beginning and
-end of the literal."))
-
-(defconst js-syntax-propertize-function
- (syntax-propertize-rules
- (js--regexp-literal (1 "\"") (2 "\""))))
+(defun js-syntax-propertize-regexp (end)
+ (when (eq (nth 3 (syntax-ppss)) ?/)
+ ;; A /.../ regexp.
+ (when (re-search-forward "\\(?:\\=\\|[^\\]\\)\\(?:\\\\\\\\\\)*/" end 'move)
+ (put-text-property (1- (point)) (point)
+ 'syntax-table (string-to-syntax "\"/")))))
+
+(defun js-syntax-propertize (start end)
+ ;; Javascript allows immediate regular expression objects, written /.../.
+ (goto-char start)
+ (js-syntax-propertize-regexp end)
+ (funcall
+ (syntax-propertize-rules
+ ;; Distinguish /-division from /-regexp chars (and from /-comment-starter).
+ ("\\(?:^\\|[=([{,:;]\\)\\(?:[ \t]\\)*\\(/\\)[^/*]"
+ (1 (ignore
+ (forward-char -1)
+ (when (or (not (memq (char-after (match-beginning 0)) '(?\s ?\t)))
+ ;; If the / is at the beginning of line, we have to check
+ ;; the end of the previous text.
+ (save-excursion
+ (goto-char (match-beginning 0))
+ (forward-comment (- (point)))
+ (memq (char-before)
+ (eval-when-compile (append "=({[,:;" '(nil))))))
+ (put-text-property (match-beginning 1) (match-end 1)
+ 'syntax-table (string-to-syntax "\"/"))
+ (js-syntax-propertize-regexp end))))))
+ (point) end))
;;; Indentation
(set (make-local-variable 'font-lock-defaults)
(list js--font-lock-keywords))
(set (make-local-variable 'syntax-propertize-function)
- js-syntax-propertize-function)
+ #'js-syntax-propertize)
(set (make-local-variable 'parse-sexp-ignore-comments) t)
(set (make-local-variable 'parse-sexp-lookup-properties) t)
c-comment-start-regexp "/[*/]\\|\\s!"
comment-start-skip "\\(//+\\|/\\*+\\)\\s *")
+ (set (make-local-variable 'electric-indent-chars)
+ (append "{}():;," electric-indent-chars))
+ (set (make-local-variable 'electric-layout-rules)
+ '((?\; . after) (?\{ . after) (?\} . before)))
+
(let ((c-buffer-is-cc-mode t))
;; FIXME: These are normally set by `c-basic-common-init'. Should
;; we call it instead? (Bug#6071)