I am writing a small major mode in Emacs, and I want to add some support for when electrc-*-modes are enabled.

In particular, I need the following functionality:

  1. Automatic newline when | is typed; and
  2. Indent and newline when [ is typed.

I have been looking into different ways of doing this, and I have come up with the following:

  1. Using electric-layout-mode, e.g. (add-to-list 'electric-layout-rules '(?| . before)); and
  2. Using electric-indent-mode, e.g. (setq-local electric-indent-chars (append electric-indent-chars '(?\[))).

I have also read about the electric-pairs-mode which might be more appropriate for the latter. There are also more manual ways of doing this, with hooks (post self insert hooks?).

However, I am struggling to find any documentation on any electric functions, and different other major modes do these kind of things in different ways. My question is: what is the best way to do what I am after? If there is one function that I can bundle both of these features in, then that might be better. But maybe it’s best that I keep these separate in case the user wants to enable, for example, layout mode but not pairs mode.

I’d love to hear from developers of major modes with experience implementing such a feature!