1

I'm trying to use electric pair mode for backticks in markdown mode. The following code hasn't worked and throws and error whenever I type a backtick. How was it wrong?

(defvar markdown-electric-pairs '((?\` . ?\`)) "Electric pairs for markdown-mode.") (defun markdown-add-electric-pairs () (setq-local electric-pair-pairs (append electric-pair-pairs markdown-electric-pairs)) (setq-local electric-pair-text-pairs electric-pair-pairs)) (add-hook 'markdown-mode-hook (lambda () (pandoc-mode t) (markdown-add-electric-pairs))) 

The error is

Wrong type argument: commandp, electric-pair

2
  • 3
    You're not showing enough code. You don't show any use of function electric-pair. Set debug-on-error to t and show the backtrace, or show where you use function electric-pair. You're apparently trying to invoke it as a command. (Is there even a function named electric-pair? Commented Jun 23, 2024 at 16:59
  • 1
    Maybe you wanted to say M-x electric-pair-mode? FWIW, that works for me (after I (require 'elec-pair), load the above file and turn on electric-pair-mode in the buffer). Commented Jun 23, 2024 at 18:31

1 Answer 1

2

If you use Emacs' "Easy Customization" via M-x customize-group and then type or select electricity you can add your own pairs under the heading "Electric Pair Pairs." Note that you will have to set it and/or save it for future sessions. It will also show you the saved lisp expression for the setting, which is helpful in general.

If you want to set/change a custom variable in elisp, use the macro setopt instead of setq as setopt runs any behind the scenes code set up on the variable. As a bonus, it defaults to setq behavior for non-custom variables.

I have learned to be aware of the difference between regular variables and customizable variables. If you setq or setq-local a customizable variable, it may (very likely) not work, or not work exactly how you intended. This is because additional setup code is run for custom variables.

More info:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Easy-Customization.html

https://www.gnu.org/software/emacs/manual/html_node/emacs/Examining.html

3
  • 1
    You are correct about the additional setup in general, but these two (electric-pair-pairs and electric-pair-text-pairs) don't have any additional setup, so the OP's original code should work. I believe that he made a mistake (calling the non-existent electric-pair function, instead of electric-pair-mode) - see my comment under the question. Commented Jul 2, 2024 at 15:03
  • I see what you are saying. Whatever search leads them here is better served by your answer. I have been a reader of SE for years, but not an answerer. What's the process? I can edit my answer, vote it down? I will check the help docs. Your guidance is appreciated. Commented Jul 4, 2024 at 1:26
  • No, no, no - your answer is fine (and the OP accepted it, so it must have worked for them). My comment is just a (small) clarification: one doesn't have to go the full customize way in this particular case, but in general, it is safer to do it the way you describe. Commented Jul 4, 2024 at 2:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.