emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] /srv/bzr/emacs/trunk r105434: * lisp/progmodes/js.el (js-s


From: Stefan Monnier
Subject: [Emacs-diffs] /srv/bzr/emacs/trunk r105434: * lisp/progmodes/js.el (js-syntax-propertize, js-syntax-propertize-regexp):
Date: Fri, 12 Aug 2011 11:32:39 -0400
User-agent: Bazaar (2.3.1)

------------------------------------------------------------
revno: 105434
fixes bug(s): http://debbugs.gnu.org/cgi/bugreport.cgi?bug=9183
committer: Stefan Monnier <address@hidden>
branch nick: trunk
timestamp: Fri 2011-08-12 11:32:39 -0400
message:
  * lisp/progmodes/js.el (js-syntax-propertize, js-syntax-propertize-regexp):
  New function.
  (js--regexp-literal, js-syntax-propertize-function): Remove.
  (js-mode): Use js-syntax-propertize to handle multilines.
  (js-mode-map): Don't rebind electric keys.
  (js-insert-and-indent): Remove.
  (js-mode): Setup electric-layout and electric-indent instead.
modified:
  lisp/ChangeLog
  lisp/progmodes/js.el
=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2011-08-12 13:49:45 +0000
+++ b/lisp/ChangeLog    2011-08-12 15:32:39 +0000
@@ -1,5 +1,13 @@
 2011-08-12  Stefan Monnier  <address@hidden>
 
+       * progmodes/js.el (js-syntax-propertize, js-syntax-propertize-regexp):
+       New function.
+       (js--regexp-literal, js-syntax-propertize-function): Remove.
+       (js-mode): Use js-syntax-propertize to handle multilines (bug#9183).
+       (js-mode-map): Don't rebind electric keys.
+       (js-insert-and-indent): Remove.
+       (js-mode): Setup electric-layout and electric-indent instead.
+
        * epa-file.el (epa-file-select-keys): Revert to nil default (bug#9280).
 
 2011-08-12  Daiki Ueno  <address@hidden>

=== modified file 'lisp/progmodes/js.el'
--- a/lisp/progmodes/js.el      2011-08-05 19:53:46 +0000
+++ b/lisp/progmodes/js.el      2011-08-12 15:32:39 +0000
@@ -61,6 +61,7 @@
 (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" ())
 
@@ -507,9 +508,6 @@
 
 (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)
@@ -525,21 +523,6 @@
     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
@@ -1653,25 +1636,35 @@
                                    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."))
+(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 "\"/")))))
 
-(defconst js-syntax-propertize-function
-  (syntax-propertize-rules
-   (js--regexp-literal (1 "\"") (2 "\""))))
+(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
 
@@ -3302,7 +3295,7 @@
   (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)
@@ -3335,6 +3328,11 @@
         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)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]