Open reactormonk opened 9 years ago
+1
It looks as if markdown-mode provides this feature, though it has an easier time with providing it because it just needs to look at headers.
Something like this in .emacs
(add-hook 'yaml-mode-hook
(lambda ()
(outline-minor-mode)
(define-key yaml-mode-map (kbd "TAB") 'outline-toggle-children)
(setq outline-regexp "^ *\\([A-Za-z0-9_-]*: *[>|]?$\\|-\\b\\)")))
Or individual YAML files
Local Variables:
mode: outline-minor
outline-regexp: "^ *\\([A-Za-z0-9_-]*: *[>|]?$\\|-\\b\\)"
End:
@vibrog Thanks for the solution, it almost works for me, but the regexp you provide does not accept some of the keys that I would like to use as outlines.
I'm editing a YAML file containing an OpenAPI 3 spec, and I have some paths like this:
paths:
/jobs:
foo: 'bar'
'/jobs/{id}':
foo: 'baz'
I tried editing the regex, so that the first []
group also contains '/{}
, but without success.
Hello,
in case someone is interested, on my side I have this for now,:
(use-package yaml-mode
:ensure t
:mode (".yaml$")
:hook
(yaml-mode . yaml-mode-outline-hook)
:init
(defun yaml-outline-level ()
"Return the outline level based on the indentation, hardcoded at 2 spaces."
(s-count-matches "[ ]\\{2\\}" (match-string 0)))
(defun yaml-mode-outline-hook ()
(outline-minor-mode)
(setq outline-regexp "^\\([ ]\\{2\\}\\)*\\([-] \\)?\\([\"][^\"]*[\"]\\|[a-zA-Z0-9_-]*\\): *\\([>|]\\|&[a-zA-Z0-9_-]*\\)?$")
(setq outline-level 'yaml-outline-level))
)
@seblemaguer This is not entirely correct. For a counterexample:
foo:
bar:
baz: 42
quux: 3.14
plugh:
xyzzy: false
Putting point on bar:
and invoking (outline-hide-subtree)
hides baz
and quux
, because neither is considered to be a heading and thus both are subordinate to bar
.
I cropped the regexp after :
and it seems to work for me.
This is what works pretty well for me:
(setq outline-regexp
(rx
(seq
bol
(group (zero-or-more " ")
(or (group
(seq (or (seq "\"" (*? (not (in "\"" "\n"))) "\"")
(seq "'" (*? (not (in "'" "\n"))) "'")
(*? (not (in ":" "\n"))))
":"
(?? (seq
(*? " ")
(or (seq "&" (one-or-more nonl))
(seq ">-")
(seq "|"))
eol))))
(group (seq
"- "
(+ (not (in ":" "\n")))
":"
(+ nonl)
eol)))))))
With this YAML file for testing:
# file test
test:
some: 'key'
yea: 'hi'
sub:
subkey: is nice
# some abc
test: >-
some text and such
I can put things here, without care <3
other: |
asdf
and: hahaha
other:
yes:
- 'string'
- 'string'
ups:
- geht
- trotzdem
other_ups:
- "hey": abc
sup: false
array_of_hashes: &name
- hello: my dear
this: works
"nice thing": yaya
subobjects:
ear: worm
- other: my friend
@leoc thanks, yours is definitely better than mine :D
@leoc Is your regex working properly for the ups
in your emacs? When I tried to hide elements beneeth ups
, nothing got folded. I suspect it's because that - geht
is getting the same indent level with ups
.
Everything (except the case mentioned above) works perfectly. Thanks for this helpful regex!
ups:
- geht
- trotzdem
other_ups:
- "hey": abc
sup: false
Hi @phye, yes, it works for me. This is my full yaml-mode config:
https://gist.github.com/leoc/f8c0868051003c4ea6eff638bc614575
Thansk for your confirmation. I'll check further with your config :)
Just checked further, if I changed your test case a little bit(add value to geht
and trotzdem
. Folding at ups
won't work.
ups
- geht: world
- trotzdem: new
Not sure if you can reproduce it :)
It would be amazing if the the spacing conformed to the conventional block/indentation such that hs and other default frameworks could know what to fold.
It would be nice to have an org-mode style folding for nested keys.