How to achieve code folding effects in Emacs?

后端 未结 14 2170
眼角桃花
眼角桃花 2020-11-30 16:15

Whats the best way to achieve something like code folding, or the type of cycling that org-mode uses. What would be the best solution in elisp to create this type of behavi

相关标签:
14条回答
  • 2020-11-30 16:54

    You can also get code folding by using CEDET with following code in init file:

    (global-semantic-folding-mode t)
    

    After evaluation of this code, the small triangle will appear in fringle area, so you will able to fold & unfold code using it. This method is more precise, as it uses syntactic information, extracted from source code

    0 讨论(0)
  • 2020-11-30 16:57

    hs-minor-mode works beautifully.

    It works even more beautifully when paired with fold-dwim (do what I mean). Then there's fold-dwim-org, which provides org-mode like key-bindings for code folding! Both can be installed via marmalade (and I think elpa).

    0 讨论(0)
  • 2020-11-30 16:57

    I am surprised that noone mentioned narrowing-widening. It's super awesome, it comes bundled with emacs as well. Simply select code you want to focus on and press C-x n n, to go back to full view mode just do C-x n w. I always prefer to use built-in emacs functionality than clutter my user space with another package. If emacs can do it with built-in functionality then I'll prefer that over some third party package, but that's just me, doesn't mean or imply anything else besides that I like simple stuff over complex long configs.

    0 讨论(0)
  • 2020-11-30 16:58

    hide-show mode (hs-minor-mode) with default keybinding C-c @ C-M-h to trigger the folding (hs-hide-all)

    0 讨论(0)
  • 2020-11-30 17:02

    I believe that your comparison of your "project" to folding is not quite good, because folding is about changing the appearance while keeping the buffer contents intact (the text). Your project would involve showing extra text while keeping the buffer contents intact, AFAIU. So. it's not implementable as a composition of text-insertion and folding (then, the buffer contents would be changed).

    But perhaps, it's indeed possible with the same mechanism as folding is done with -- "overlays"... Consider the "before-string" and "after-string" overlay properties; perhaps, you could put your function definitions into these strings belonging to a zero-length overlay at the point. Look at outline-flag-region function to see how overlays are used in the outline mode.

    0 讨论(0)
  • 2020-11-30 17:02

    If you use hs-minor-mode, it might be a good idea to also set a more convenient shortcut, e.g.:

    (eval-after-load 'hideshow
     '(progn
       (global-set-key (kbd "C-+") 'hs-toggle-hiding)))
    
    0 讨论(0)
提交回复
热议问题