6

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?

  • 1
    I've never used autopair 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 3 '15 at 4:45
  • electric-pair-mode is by the author of autopair, but included in Emacs itself. – sanityinc Jan 3 '15 at 12:51
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.

  • 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 3 '15 at 19:02
1

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!

  • +1 Awesome trick, however I am not really sure it would work for all global minor modes – Iqbal Ansari Jan 5 '15 at 6: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 6 '15 at 0:41
  • Cool trick, but it seems like this is fixed in emacs 25 so the trick will no longer work, see this: "':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 4 '16 at 22:52
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.