Question

EDIT I think pretty-mode is conceptually closer to what I want than the various folding mode, but I'd need to be able to replace strings with other strings, instead of a single character. As far as I can tell pretty-mode only allows to replace a string with a unique character.

I'm looking to do something very specific in Emacs and it's a kind of code folding. I've read this: Code folding in Emacs

which has been closed as a dupe of this:

How to achieve code folding effects in Emacs?

However I'm trying to do something a bit particular in that I don't want to fold functions lines: I do want to fold characters, on a single line.

I'm also not at all into navigation: it's not to facilitate navigation. It's just to de-clutter the screen a bit.

For example if I have this:

super verbose syntax my_bar = 42;
hyper long syntax my_foo = 84;

I'd like to have it appear, inside Emacs, as this:

svs my_bar = 42;
hls my_foo = 84;

Because I've not only read a million time "super verbose syntax" / "hyper long syntax" and because since ten years when I type 'svs' it's automatically expanded to "super verbose syntax" and 'hls' automatically expanding to "hyper long syntax", there's no chance I'd ever use "svs" as a real identifier and there's no chance I'd not instantly recognize what's going on. Of course the file/buffer should still contain the awfully verbose syntax.

I purely want a "characters folding on one line".

So, in short, when I type "super verbose syntax" I'd like it to fold on screen automatically to 'svs' (when I enter 'svs' TAB it expands to "super verbose syntax", but that is something else... In my case typing svs TAB would expand to "super verbose syntax" which then should be folded directly to 'svs' on screen).

This would save a lot of screen real estate and help me focus on more important things.

(as a cool side effect it would also line up vertically reserved keywords that do not have the same length, making the code even more readable to me)

How can I achieve this under Emacs? Is there a minor-mode already doing this?

Was it helpful?

Solution

Try adding these two expressions to your mode-hook:

(font-lock-add-keywords
 nil
 '(("super verbose syntax" (0 '(face default display "svs") append))
   ("hyper long syntax" (0 '(face default display "hls") append)))
 t)
(push 'display font-lock-extra-managed-props)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top