this post was submitted on 08 Oct 2023
2 points (100.0% liked)

Emacs

314 readers
2 users here now

A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!

Get Emacs

Rules

  1. Posts should be emacs related
  2. Be kind please
  3. Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.

Emacs Resources

Emacs Tutorials

Useful Emacs configuration files and distributions

Quick pain-saver tip

founded 1 year ago
MODERATORS
 

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!

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here