origami.el icon indicating copy to clipboard operation
origami.el copied to clipboard

Persistent folds?

Open longouyang opened this issue 10 years ago • 11 comments

How hard would it be to get folds to persist for a file across buffer / Emacs sessions?

longouyang avatar Oct 28 '15 23:10 longouyang

Not too hard. Origami builds a data structure by parsing the buffer each time it changes. It compares this data structure to the previous one, works out the differences and then changes the displayed folds accordingly. All that would be needed is to serialize this data structure on killing the buffer etc and then read it back in and apply it when the file is opened.

Is this a feature request or are you thinking of adding this functionality? I can't tell by the way you've phrased the question.

gregsexton avatar Oct 29 '15 04:10 gregsexton

A feature request, but if you're too busy I might take a crack at it (though I'm an elisp novice) On Oct 28, 2015 9:00 PM, "Greg Sexton" [email protected] wrote:

Not too hard. Origami builds a data structure by parsing the buffer each time it changes. It compares this data structure to the previous one, works out the differences and then changes the displayed folds accordingly. All that would be needed is to serialize this data structure on killing the buffer etc and then read it back in and apply it when the file is opened.

Is this a feature request or are you thinking of adding this functionality? I can't tell by the way you've phrased the question.

— Reply to this email directly or view it on GitHub https://github.com/gregsexton/origami.el/issues/19#issuecomment-152069859 .

longouyang avatar Oct 29 '15 04:10 longouyang

Honestly, I'm pretty busy with other commitments at the moment. Sounds like a cool feature but it's not likely to make my todo list for a while. Feel free to take a crack. I'll help you out with any questions you have. Do ask before making any major changes or design decisions though. Otherwise you risk me not merging.

gregsexton avatar Oct 30 '15 11:10 gregsexton

Would it suffice to store the buffer-local value origami-history when the file is saved and restore it when loading the file?

longouyang avatar Nov 11 '15 20:11 longouyang

(bump)

longouyang avatar Nov 25 '15 01:11 longouyang

Yes. That sounds like a good strategy. You'll probably need to reload the history and then apply the latest tree with origami-apply-new-tree. Let me know how it goes! :)

gregsexton avatar Nov 29 '15 09:11 gregsexton

@longouyang saving file-local variables persistently is an interesting problem. If you do find a solution, I'd love to see if it can be applied to other problems.

PythonNut avatar Jan 07 '16 05:01 PythonNut

So I just got a basic solution working in the persist branch of my fork

It recursively serializes the entire history object, converting overlays to lambdas that are later evaluated.

So a history like this:

[([1 2305843009213693951 0 t nil root])
 [1 2305843009213693951 0 t
    ([2 207 32 nil nil #<overlay from 34 to 207 in foo.el>])
    root]
 nil]

Gets saved as this:

(lambda
  (buf)
  (vector
   (list
    (vector 1 2305843009213693951 0 't nil 'root))
   (vector 1 2305843009213693951 0 't
           (list
            (vector 2 207 32 nil nil
                    (let
                        ((ov
                          (origami-create-overlay 34 207 0 buf)))
                      (origami-hide-overlay ov)
                      ov)))
           'root)
   nil))

(origami-hide-overlay is only called for hidden nodes)

This needs more testing and some cleaning up but the basic idea seems to work

longouyang avatar Jan 07 '16 06:01 longouyang

Oh, and @PythonNut, I think vimish-fold has a completed persistence feature, so you might wanna look at that too.

longouyang avatar Jan 07 '16 06:01 longouyang

There seems to be a more generic package dedicated to the problem of making any overlays persistent: https://github.com/mneilly/Emacs-Persistent-Overlays. Should work with hideshow and outline modes out-of-the-box. I couldn't make it work with origami the easy way though. Maybe you guys with deeper understanding of overlays can do it?

dev-plvlml avatar Mar 21 '18 09:03 dev-plvlml

Does anyone work on that Issue today?

buhtz avatar Jun 06 '22 16:06 buhtz