Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs modes for editing files where indentation implies structure

I want to edit a big, hairy YAML file in Emacs. File structure is implied by indentation: If line A is indented ten spaces, then A starts a section that includes every following line that is indented by more than ten spaces. This is a fairly common convention that is used well beyond YAML.

What I really want is an emacs mode that has definitions that respect this structure. For example:

  • I would like forward-sexp to skip past a sexp, where a sexp is implied by the indentation rather than by matching pairs of brackets. Similarly, I'd like there to be other navigation commands to move to the line after the end of the current section, to the next sibling section to the current one, and so forth.
  • I'd like there to be a command to collapse the current section, or all but the current section, similar to the way outline-mode can collapse the current outline section, but with sections defined by indentation.
  • I'd like the mode line to display something useful about the section I'm in, as implied by indentation.
  • I'd like there to be an option to automatically highlight all the lines that are ancestors of the current line.
  • Other things I haven't thought of.

I have looked at yaml-mode, but it doesn't do anything useful. Several people have suggested I try Python mode, but it doesn't work very well for this purpose.

A generic indented-lines mode seems like the sort of thing someone would have written a long time ago, but if it exists, I can't find it. Or maybe there's some way to hack outline-mode to work the way I want?

Thanks for any suggestions.

like image 377
Mark Dominus Avatar asked Jan 07 '13 01:01

Mark Dominus


2 Answers

Using outline-mode and setting outline-regexp to " *" doesn't get me exactly what I want, but it does get a fair amount of it. For example, hide-subtree, bound to C-c C-d by default, seems to collapse the current section in just the way I described above. outline-forward-same-level, bound to C-c C-f, also does what I wanted.

Some things still don't work quite right. I'll modify this answer if I find any other useful ways to tweak outline-mode.

This is a slight modification of a suggestion of José Antonio Ortega Ruiz.

like image 165
Mark Dominus Avatar answered Sep 30 '22 04:09

Mark Dominus


To go to the next/current sibling I wrote a function. Load the file and use M-x my-yaml-next-sibling. This package is still rudimentary but quite useful IMO !

update: this function has grown into a real package, indent-tools. Its first goal is navigation in blocks of indentation: go to the next node of same indentation, to the parent, etc. Then it's handy to indent blocks, comment, fold, etc.

Not in melpa yet but in the process.

like image 21
Ehvince Avatar answered Sep 30 '22 04:09

Ehvince