8

I would like electric-pair-mode to be enabled only in the buffers where I am editing, say, Emacs Lisp.

But it seems that electric-pair-mode is a "global minor mode": if it is activated in one buffer, it automatically becomes active in all buffers. Thus, including (electric-pair-mode) in the emacs-lisp-mode-hook does not behave in a useful way.

Is there a way to configure Emacs to only use electric pairs in some buffers?

scaramouche
  • 1,772
  • 10
  • 24
  • 1
    I've never used [autopair](http://www.emacswiki.org/emacs/AutoPairs#toc1) but it may be what you need, as it is a minor mode. I'm also thinking about defining a custom minor mode and enable electric pair mode inside this custom minor mode, probably useless :\ – Nsukami _ Jan 03 '15 at 04:45
  • 1
    `electric-pair-mode` is by the author of `autopair`, but included in Emacs itself. – sanityinc Jan 03 '15 at 12:51

3 Answers3

5

You can use electric-pair-local-mode instead.

vaer-k
  • 211
  • 3
  • 8
4

This is not a complete answer since it does not disable electric-pair-mode, however it does disable automatic insertion of the matching parens. You can customize electric-pair-inhibit-predicate. From the documentation C-hvelectric-pair-inhibit-predicateRET

Predicate to prevent insertion of a matching pair.

You can set it to a function which returns t only if the current major-mode is emacs-lisp-mode, something like the following

(defvar my-electic-pair-modes '(emacs-lisp-mode))

(defun my-inhibit-electric-pair-mode (char)
  (not (member major-mode my-electic-pair-modes)))

(setq electric-pair-inhibit-predicate #'my-inhibit-electric-pair-mode)

You can add any additional major-modes where you want automatic insertion to work to my-electric-pair-modes.

Iqbal Ansari
  • 7,468
  • 1
  • 28
  • 31
  • 1
    This works. Many thanks! I wish in the future `electric-pair-mode` will be fixed to behave as a standard minor mode, as the standard way to activate a minor mode by calling it from a major mode hook, not by writing a function that "tricks" the minor mode into not doing anything in some cases. – scaramouche Jan 03 '15 at 19:02
2

here's a simple alternative (you can add it to your config file):

(defun my-local-electric-pair-mode ()
  (make-variable-buffer-local 'electric-pair-mode)
  (electric-pair-mode +1))

(add-hook 'emacs-lisp-mode-hook 'my-local-electric-pair-mode)

Assuming electric-pair-mode is globally disabled (you can disable it evaluating (electric-pair-mode -1)), the mode will be activated only on emacs-lisp-mode buffers and nowhere else.

You can create similar functions for other global minor modes, such as show-paren-mode.

P.S. Remember to avoid enabling electric-pair-mode at startup or the above code won't work!

undostres
  • 1,793
  • 12
  • 15
  • +1 Awesome trick, however I am not really sure it would work for all global minor modes – Iqbal Ansari Jan 05 '15 at 06:02
  • To be honest, I'm not sure either. But it might since the snippet works by making the variable associated with the minor mode buffer local. Every minor mode has both a function and a variable with the same name that tracks if the mode has been activated or not. Nevertheless, I use it for `show-paren-mode` and it works OK. – undostres Jan 06 '15 at 00:41
  • Cool trick, but it seems like this is fixed in emacs 25 so the trick will no longer work, see [this](http://git.savannah.gnu.org/cgit/emacs.git/tree/etc/NEWS?h=emacs-25#n1395): "':global' minor mode use 'setq-default' rather than 'setq'. This means that you can't use 'make-local-variable' and expect them to "magically" become buffer-local." – Jorge Israel Peña Jun 04 '16 at 22:52