Is it possible to define multi-character pairs (like LaTeX displayed equation pair \[ ... \]
or markdown bold ** ... **
) for electric-pair-mode?
1 Answer
Looking at the source code for the minor-mode, I couldn't find any way of doing it. But if you really want to implement it for multi-char strings, you can write your own function and add it to post-self-insert-hook
. This function would be called every time an self-inserting character is typed in the buffer, i.e. characters that you care about here. To see the last character you typed, use the value of the variable last-command-event
.
Here is a very fragile implementation:
(defvar star-state :no-stars)
(defun star-electric ()
(cond
((and (eq this-command 'self-insert-command)
(eq last-command-event ?*)
(eq star-state :no-stars))
(setq star-state :one-beg-star))
((and (eq this-command 'self-insert-command)
(eq last-command-event ?*)
(eq star-state :one-beg-star))
(progn (insert "**")
(backward-char 2)
(setq star-state :two-beg-stars)))
((and (eq this-command 'self-insert-command)
(eq last-command-event ?*)
(eq star-state :two-beg-stars))
(progn
(setq star-state :one-end-star)
(when (looking-at "\\s-*\\*")
(delete-char -1)
(search-forward "*"))))
((and (eq this-command 'self-insert-command)
(eq last-command 'self-insert-command)
(eq last-command-event ?*)
(eq star-state :one-end-star))
(delete-char -1)
(forward-char 1)
(setq star-state :no-stars))))
(add-hook 'post-self-insert-hook 'star-electric)
smartparens
, which you can use as an alternative toelectric-pair-mode
(it provides other features too): github.com/Fuco1/smartparens