0

I use hs-minor-mode currently.

When point is on class Foo... a M-x hs-hide-level will fold all methods of that class.

This is exactly the state I need for all *.py files when I open them. Is it possible?

When I open a python file all methods of all classes (there could be more then one!) should be folded.

hs-hide-level was pointed out in that answer. https://emacs.stackexchange.com/a/50144/12999

buhtz
  • 679
  • 4
  • 22

1 Answers1

1

Here's a quick idea:

(defun my-collapse ()
  (interactive)
  (when (and (stringp buffer-file-name)
             (string-match "\\.py\\'" buffer-file-name))
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward "class" (point-max) t)
        (hs-hide-level 1)))))

This checks that the file being visited ends with ".py". It then saves where you are in the buffer before searching for the word "class". At each match, it calls the function to 'collapse' items in that block whose indent level is greater than or equal to 1. This should be all methods, assuming the class is not nested. Your point is returned to where it was prior to the search.

To run it on every .py file, use a hook:

(add-hook 'find-file-hook 'my-collapse)
Lorem Ipsum
  • 4,327
  • 2
  • 14
  • 35