2

Is it possible to define multi-character pairs (like LaTeX displayed equation pair \[ ... \] or markdown bold ** ... **) for electric-pair-mode?

Nova
  • 1,059
  • 9
  • 21
premopie
  • 378
  • 2
  • 11
  • 5
    It is possible with `smartparens`, which you can use as an alternative to `electric-pair-mode` (it provides other features too): https://github.com/Fuco1/smartparens – Erik Sjöstrand Feb 23 '17 at 09:49

1 Answers1

1

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)
Drew
  • 75,699
  • 9
  • 109
  • 225
narendraj9
  • 304
  • 2
  • 7