2

(This question applies to any language -- Python and Haskell are examples -- that lets you organize code via indentation instead of parentheses. Such languages are more readable and take less time to write, at least according to people like myself.)

The code I'm reading has some big indented passages that don't fit on one screen, or sometimes ten screens (I use a big font), and it'd be much easier to see what was going on if I could go to some line and press a keyboard shortcut to hide everything that follows until the next line that starts at the same position or farther left.

I've found a number of code-folding packages for Emacs, but all seem to suffer at least one of these problems:

  • they've got few features and appear to be ill-maintained
  • they rely on explicit brackets (such as {{{ ... }}}) to know where
  • they let you fold top-level code, but they won't let you fold from an arbitrary inner level
  • they let you fold inner levels, but only in parallel -- that is, if you fold one of the level-2 items in a level-1 block, you have to fold the rest also.
Jeffrey Benjamin Brown
  • 3,427
  • 2
  • 28
  • 40
  • Could you please add to your question which packages/modes did you tested and don't work for you? And why? Otherwise we will ask you a lot of general questions like - did you checked https://www.emacswiki.org/emacs/HideShow? Or did you saw that question https://stackoverflow.com/questions/5244485/python-code-folding-in-emacs – rsm Jan 18 '19 at 23:22

1 Answers1

1

For Python, use elpy - the Emacs Python Development Environment. It has a built-in module for code folding:

Elpy offers code folding by enhancing the builtin folding minor mode Hideshow.

When opening a python buffer, Elpy will indicate foldable things with an arrow in the left fringe. Clicking on an arrow will fold the corresponding code blocks. Folded code blocks can be unfolded by clicking on the … button at the end of the line.

If you don’t want to use your mouse, you can achieve the same thing with the function

C-c @ C-c (elpy-folding-toggle-at-point)

    Toggle folding for the thing at point, it can be a docstring, 
    a comment or a code block.
Thomas
  • 17,016
  • 4
  • 46
  • 70
  • Do I remember incorrectly that `elpy` only lets you fold at certain levels, such as the tops of function or class method definitions? When I wrote this question I was hoping to be able to fold and unfold code in any language at any level, like JEdit lets you do. – Jeffrey Benjamin Brown Apr 25 '21 at 00:40