reddit / emacs

How to create an outline based on an arbitrary regex?

Huge_Spinach_5784 · 2022-09-30 18:34
Say I put some line separator I can regex. 1. How can I fold all the other content so I can quickly hide/show all but the divided (just like tab cycles work in org-mode)? 2. How can I create a document outline like a tree whose elements are the lines matching my regex? Like this [https://i.stack.imgur.com/t52dn.png](https://i.stack.imgur.com/t52dn.png) ​ // Run checks ------ Some code here Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. // Read files ------ Some code here Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Some code here // Print out results ------ Some code here Some code here // Cleanup ------ Some code here
MitchellMarquez42 · 2022-09-30 18:56
Look into outline-minor-mode. Not sure if it'll work out of the box, but it should get you started in the right direction.
Huge_Spinach_5784 · 2022-09-30 19:06
Ah, thanks for pointing this out, this seems relevant: https://emacs.stackexchange.com/questions/60420/emacs-outline-mode-custom-heading-and-space-between-headers
eleven_cupfuls · 2022-09-30 19:32
This is the relevant page from the manual: https://www.gnu.org/software/emacs/manual/html_node/emacs/Outline-Format.html If you can't edit the file to provide the value of `outline-regexp` directly, `M-: (setq outline-regexp "/+")` and then `M-x outline-mode` should work.
crlsh · 2022-09-30 21:54
outshine?? https://github.com/alphapapa/outshine
yoreh · 2022-09-30 23:14
I would recommend looking into outline-minor-mode, outline-minor-faces and bicycle. Here's a relevant [reddit thread](https://www.reddit.com/r/emacs/comments/e2u5n9/code_folding_with_outlineminormode/). You also need to specify appropriate regexes. For python I use: (defun my/python-outline-level () "Length of first group." (if (match-end 1) (- (match-end 1) (match-beginning 1)) 20)) (add-hook! python-mode (setq-local outline-heading-end-regexp "\n") (setq-local outline-regexp (rx (or (and (zero-or-more space) bow (or "def" "class") eow) (and (zero-or-more space) "@") (and "#" space (group (one-or-more "*")) space)))) (setq-local outline-minor-faces-regexp (rx (and "#" space (one-or-more "*") space (zero-or-more nonl)))) (setq-local outline-level #'my/python-outline-level)) This will consider class and functions definitions to be headings. It will also treat comments starting with stars as headings (commented org mode-style headings) and fontify them.
JDRiverRun · 2022-10-05 02:33
This is what outline-mode is designed for. If you always want the same style of heading in various modes, and you want your headlines to have depth for hierarchical folding (e.g. `//` top level, `///` next lower level, etc.), you might check out my little mode [outli](https://github.com/jdtsmith/outli).