2

My little research suggests that the simplest way to get code folding for Python in Emacs (24.4) is (see the comment of @scytale on this answer):

(add-hook 'python-mode-hook 'outline-minor-mode)

This works almost great. The problem is the scope of folding. Consider the following example:

if foo == bar:
   do 1
   do 2
else:
   do 3
   do 4
do 5
do 6

If point in in either of the first three lines then folding looks like:

if foo == bar:...
else:
   do 3
   do 4
do 5
do 6

So far so good. However, if the point is in lines 4-6 then folded view is:

if foo == bar:
   do 1
   do 2
else:...

Note that do 5 and do 6 are folded as well. Is there a way to limit the folding only to the right block?

Community
  • 1
  • 1
Dror
  • 12,174
  • 21
  • 90
  • 160

1 Answers1

3

It looks like the package yafolding does what you want: http://wikemacs.org/wiki/Folding#Yafolding

I could hide the else part without hiding the rest.

Here's a little Hydra for it:

(defhydra yafolding-hydra (:color red :columns 3)
  "
  Fold code based on indentation levels.
  "
  ("t" yafolding-toggle-element "toggle element")
  ("s" yafolding-show-element "show element")
  ("h" yafolding-hide-element "hide element")
  ("T" yafolding-toggle-all "toggle all")
  ("S" yafolding-show-all "show all")
  ("H" yafolding-hide-all "hide all")
  ("p" yafolding-hide-parent-element "hide parent element")
  ("i" yafolding-get-indent-level "get indent level")
  ("g" yafolding-go-parent-element "go parent element"))

(global-set-key (kbd "C-c f") 'yafolding-hydra/body)
Ehvince
  • 17,274
  • 7
  • 58
  • 79