bbatsov / prelude

Prelude is an enhanced Emacs 25.1+ distribution that should make your experience with Emacs both more pleasant and more powerful.
https://prelude.emacsredux.com
5.12k stars 1.86k forks source link

code folding in emacs #786

Closed learner010 closed 5 years ago

learner010 commented 9 years ago

Before Emacs I used Vim. In my Vim configuration file I had following key maps for folding (taken from http://vim.spf13.com/). The folding is based upon indent level.

f0 = fold to top level indent f1 = fold to 1st level indent f2 = fold to 2nd level indent and so on. Lets consider following code: ``` python class AClass: def a_method(self): pass def b_method(self): pass def c_method(self): pass ``` would look like below at 2nd level fold. ``` python class AClass: def a_method(self): ... def b_method(self): ... def c_method(self): ... ``` Is there anyway in emacs to achieve it? Thanx for any adivce in advanced.
tuhdo commented 9 years ago

You should have a look hs-minor-mode.

EDIT: Here is a quick setup and demo:

(add-hook 'prog-mode-hook 'hs-minor-mode)

Then, in your source file above, press C-c @ C-l to hide all methods. Press C-c @ C-M-s again to show all methods.

If you want to hide a specific level, add a prefix argument before C-c @ C-l. For example, press C-u 2 C-c @ C-l will hide blocks that are two levels below current block.

learner010 commented 9 years ago

@tuhdo Following ur suggestion, I added following configuration to my personal config.el.

(defun hide-level-1 ()
  (interactive)
  (hs-hide-level 1))

(defun hide-level-2 ()
  (interactive)
  (hs-hide-level 2))

(defun hide-level-3 ()
  (interactive)
  (hs-hide-level 3))

(evil-leader/set-key "f1" 'hide-level-1)
(evil-leader/set-key "f2" 'hide-level-2)
(evil-leader/set-key "f3" 'hide-level-3)

The problem is, when I press ,f2 the python code folds in alternative manner. Something like below:

class AClass:
   def a_method(self): ...
   def b_method(self): 
       pass
   def c_method(self): ...
   def c_method(self): 
        pass

It works fine for lisp and javascript but in python.

Avsecz commented 8 years ago

I can also confirm this. A workaround was suggested here:

http://emacs.stackexchange.com/questions/18381/hideshow-with-python-hiding-only-one-part

which is unfortunately not using the hs-* functions to do the folding.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

stale[bot] commented 5 years ago

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.