using autopair mode in js2-mode

(Update @ 2013 March: This article is now deprecated. With the latest js2-mode from GNU ELPA, autopair works fine with js2-mode without any tweaks. Old js2-mode’s own pairing mode js2-mirror-mode is gone. But now Emacs 24 includes electric-pair-mode which is another pairing mode other than autopair. electric-pair-mode is turned off by default. As of 2013 March, autopair has more features than electric-pair-mode. See my js2-mode setup recommendation and how to install autopair.)

js2-mode already does some of what autopair mode does. When both autopair-mode and js2-mode are turned on in a JavaScript buffer, the two modes seem to conflict. For example, inserting an open parenthesis results in two closing parentheses being auto-inserted. There are two ways to resolve this.

1. Turning off autopair mode in js2-mode

(eval-after-load "autopair"
  '(progn
     (autopair-global-mode 1)

     (setq my-autopair-off-modes '(js2-mode))
     (dolist (m my-autopair-off-modes)
       (add-hook (intern (concat (symbol-name m) "-hook"))
                 #'(lambda () (setq autopair-dont-activate t))))
     ))

With the code above, you can turn on autopair-global-mode while using js2-mode’s own autopairing feature in js2-mode buffers.

2. Turning off js2-mode’s own autopairing feature and using autopair mode in js2-mode buffers.

If you prefer to use autopair mode in js2-mode buffers like me, use the following code.

(eval-after-load "autopair"
  '(progn
     (autopair-global-mode 1)))

(eval-after-load "js2-mode"
  '(progn
     (setq js2-mirror-mode nil)))

Following is a minimal dotemacs setup example for autopair and js2-mode.

;; the whole content of the minimal dotemacs file.

(eval-after-load "autopair-autoloads"
  '(progn
     (require 'autopair)))

(eval-after-load "autopair"
  '(progn
     (autopair-global-mode 1)

     (setq my-autopair-off-modes '(
                                   ;; js2-mode
                                   ))
     (dolist (m my-autopair-off-modes)
       (add-hook (intern (concat (symbol-name m) "-hook"))
                 #'(lambda () (setq autopair-dont-activate t))))
     ))

(eval-after-load "js2-mode"
  '(progn
     (if (and (boundp 'my-autopair-off-modes)
              (not (memq 'js2-mode my-autopair-off-modes)))
         (setq js2-mirror-mode nil))
     ))

(require 'package)
(package-initialize)
This entry was posted in Emacs and tagged , , , . Bookmark the permalink.

One Response to using autopair mode in js2-mode

  1. Pingback: js2-mode setup recommendation | Yoo Box

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s