how to install autopair from emacs package system

how to install autopair

autopair is available in Marmalade ELPA. So your init file should have these lines:

(require 'package)
(add-to-list 'package-archives
             '("marmalade" . "") t)

then you can do M-x list-packages and install autopair from there.

how to enable autopair without using require

If you include the following code in your emacs init file

(require 'autopair)
(autopair-global-mode 1)

then start up error may occur because path for autopair is not added to load-path until after package-initialize has run. One way to avoid this problem is to put the line (package-initialize) somewhere before (require 'autopair) in your init file, another way is to use a hook that runs after package-initialize. A third way which I prefer is to use eval-after-load like this:

(eval-after-load "autopair-autoloads" ; <-- "autopair-autoloads" not "autopair"
     (require 'autopair)
     (autopair-global-mode 1)

     ;; and some further customization of autopair
     ;; ...

How does that work? Emacs always executes package-initialize after loading your init file (unless -q option is passed). What package-initialize does is for each installed package, say xyz, it adds to load-path the path to the directory where xyz.el is, but it doesn’t run xyz.el. Instead it runs xyz-autoloads.el. If xyz package were for a major mode, running xyz-autoloads.el would add an entry to auto-mode-alist so that when Emacs opens a file with the relevant filename extension, it turns on the xyz major mode automatically by running the command xyz-mode. The xyz-autoloads would also setup autoload for the command xyz-mode by telling Emacs that the definition of the function xyz-mode can be found in a file named xyz.el. That was for a major mode package. What about a package like autopair that gives you a global minor mode?

autopair-autoloads.el could do either (1) do nothing or (2) setup autoload for the global minor mode command (autopair-global-mode) or (3) not only setup autoload for the global minor mode command but also execute that command to turn on the global mode. (1) means that after Emacs has finished loading, you have to execute (require ‘autopair) before being able to do M-x autopair-global-mode. (2) means that you don’t have to, but you still have to do M-x autopair-global-mode to enable the global mode. (3) means that the global mode will be automatically turned on after Emacs start up is finished. If you include the eval-after-load code above anywhere in your init file (as a top level form of course), it will turn on the global mode for you automatically in all three cases. Also, graceful degradation: the same eval-after-load code above does not cause start up error even in an environment where autopair is not installed.

how to disable autopair in some modes while keeping autopair globally on

;; Disables autopair mode in JavaScript mode (js-mode) and C mode buffers.
(dolist (hk (list 'js-mode-hook 'c-mode-common-hook))
  (add-hook hk
            #'(lambda ()
                (autopair-mode -1))))

A nice thing about (add-hook HOOK FUNCTION) is that unlike add-to-list it works even when the hook variable is not defined at the time (add-hook HOOK FUNCTION) runs. This means that it will be fine to place the add-hook code outside the eval-after-load form.

In order to find the name of the hook you want to use, press C-h m and then click on the relevant el file and search for -hook.

further customization of autopair

One would expect that one can customize autopair a lot from M-x customize-group RET autopair RET but last time I checked there was just one autopair user option shown in the Customize interface, the option for turning it on and off. The official autopair page has information on how to customize the behavior of autopair.

This entry was posted in Emacs and tagged , . Bookmark the permalink.

2 Responses to how to install autopair from emacs package system

  1. Pingback: using autopair mode in js2-mode | Yoo Box

  2. Ben says:

    Thanks! Your trick for disabling autopair in certain modes worked great.

Leave a Reply

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

You are commenting using your 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