SOLVED: Emacs: fold, jump around and restructure code using Haddock-style multi-level headings

I'd like to keep snippets of Tidal code in an org-mode file, because org makes it easy to divide a file into a hierarchy, jump around that hierarchy, and hide irrelevant branches of it. The problem is that in org-mode you don't have the handy syntax highlighting and indentation of haskell-mode. So I'd like to be able to switch between those two modes while viewing the org file. But currently, each time I switch modes, the view that I had carefully curated gets obliterated -- either everything is revealed (when leaving org-mode), or everything but the top level is hidden (when re-entering it).

I just asked about this on Stack Exchange. If you know how to do it, please answer! If you'd like to know, please make some noise on the page!

1 Like

are you using #+BEGIN_SRC haskell for your src blocks? have you tried switching into haskell-mode using C-c ' from the org file?

I took a quick look at your SE post, and what you might want to consider is using an indirect buffer (try C-x 4 c) to 'clone' your org file. you can have one view in org-mode and the other in haskell-mode then either split frame for side-by-side, or flip between full frame buffers...

Thanks, Nik! I didn't know about indirect buffers. That would do exactly what I was asking for if there were a way to link the points in the two buffers so they always moved in tandem.

And that appears to be possible, although it will take some coding.

I have a feeling there is something in Melpa that links cursor movement between buffers. If I can remember what it's called I'll let you know. Might also be interesting to experiment with using multiple cursors {and/or bookmarks in the buffers) as part of the live coding cycle...

You're right! MELPA

Trying it now. Thanks again, Nik!

There's a simpler solution! Namely, to enable outline-minor-mode, configuring it to fold Haddock-style headings. Here's some demonstration code:

{-
(haskell-mode)
(outline-minor-mode)
(setq-local outline-regexp "-- [*]+")
-}

-- * 1
-- ** 11
let this_haskell_variable = 3
-- ** 12
-- * 2
let another_haskell_variable = 4

Evaluating the elisp code at the top (highlight it, without the "{- ... -}" braces, and run M-x eval-region) makes it possible to fold, hop around and restructure the headings using commands like (respectively) outline-cycle, outline-next-visible-heading and outline-move-subtree-down.

(I've radically renamed this thread -- now the title indicates my underlying purpose, rather than the initial approach I was taking, which I never got to work.)