From: David Robinow on
On Fri, Jul 16, 2010 at 9:29 AM, ernest <nfdisco(a)gmail.com> wrote:
> On 15 Jul, 18:45, kj <no.em...(a)please.post> wrote:
>> This is a question _for Emacs users_ (the rest of you, go away :)  ).
>>
>> How do you do Python code-folding in Emacs?
>>
>> Thanks!
>>
>> ~K
>
> I tried the outline-mode and it seemed to work. It can
> collapse different blocks of code, such as functions,
> classes, etc.
>
> However, I never got used to it because of the bizarre
> key bindings.
Really, if you can't be bothered to set your key bindings to something
you prefer, then I don't think Emacs is the right tool for you.
From: Anssi Saari on
David Robinow <drobinow(a)gmail.com> writes:

> Really, if you can't be bothered to set your key bindings to something
> you prefer, then I don't think Emacs is the right tool for you.

Uh, I absolutely think Emacs is the right tool for me, but I don't
think I've never changed any key bindings in the 20 years I've used
it. Considering the number of functions and existing key bindings, the
whole idea makes me think it would be like a game of musical chairs,
always some function would be left without a key binding?
From: Teemu Likonen on
* 2010-07-16 06:29 (-0700), ernest wrote:

> I tried the outline-mode and it seemed to work. It can collapse
> different blocks of code, such as functions, classes, etc.
>
> However, I never got used to it because of the bizarre key bindings.

I use outline-minor-mode and the code below to make key bindings fast
and easy. Alt + up/down arrow will go to previous/next heading or code
block. Alt + left/right will close or open one sublevel. And that's
about it. My code is slightly modified version of the code from

http://www.emacswiki.org/emacs/OutlineMinorMode

Note that on that page there is also a tip to use Vim-like manual fold
markers (like the Vim's default "{{{").



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; outline-mode-easy-bindings.el
;;
;; Installation: Store this file as outline-mode-easy-bindings.el
;; somewhere in your load-path and create hooks for outline modes to
;; load this automatically, for example:

;; (add-hook 'outline-mode-hook 'my-outline-easy-bindings)
;; (add-hook 'outline-minor-mode-hook 'my-outline-easy-bindings)
;;
;; (defun my-outline-easy-bindings ()
;; (require 'outline-mode-easy-bindings nil t))


(defun outline-body-p ()
(save-excursion
(outline-back-to-heading)
(outline-end-of-heading)
(and (not (eobp))
(progn (forward-char 1)
(not (outline-on-heading-p))))))

(defun outline-body-visible-p ()
(save-excursion
(outline-back-to-heading)
(outline-end-of-heading)
(not (outline-invisible-p))))

(defun outline-subheadings-p ()
(save-excursion
(outline-back-to-heading)
(let ((level (funcall outline-level)))
(outline-next-heading)
(and (not (eobp))
(< level (funcall outline-level))))))

(defun outline-subheadings-visible-p ()
(interactive)
(save-excursion
(outline-next-heading)
(not (outline-invisible-p))))

(defun outline-hide-more ()
(interactive)
(when (outline-on-heading-p)
(cond ((and (outline-body-p)
(outline-body-visible-p))
(hide-entry) (hide-leaves))
(t (hide-subtree)))))

(defun outline-show-more ()
(interactive)
(when (outline-on-heading-p)
(cond ((and (outline-subheadings-p)
(not (outline-subheadings-visible-p)))
(show-children))
((and (not (outline-subheadings-p))
(not (outline-body-visible-p)))
(show-subtree))
((and (outline-body-p)
(not (outline-body-visible-p)))
(show-entry))
(t (show-subtree)))))


(let ((major outline-mode-map)
(minor outline-minor-mode-map))

(define-key major (kbd "M-<left>") 'outline-hide-more)
(define-key major (kbd "M-<right>") 'outline-show-more)
(define-key major (kbd "M-<up>") 'outline-previous-visible-heading)
(define-key major (kbd "M-<down>") 'outline-next-visible-heading)
(define-key major (kbd "C-c J") 'outline-hide-more)
(define-key major (kbd "C-c L") 'outline-show-more)
(define-key major (kbd "C-c I") 'outline-previous-visible-heading)
(define-key major (kbd "C-c K") 'outline-next-visible-heading)

(define-key minor (kbd "M-<left>") 'outline-hide-more)
(define-key minor (kbd "M-<right>") 'outline-show-more)
(define-key minor (kbd "M-<up>") 'outline-previous-visible-heading)
(define-key minor (kbd "M-<down>") 'outline-next-visible-heading)
(define-key minor (kbd "C-c J") 'outline-hide-more)
(define-key minor (kbd "C-c L") 'outline-show-more)
(define-key minor (kbd "C-c I") 'outline-previous-visible-heading)
(define-key minor (kbd "C-c K") 'outline-next-visible-heading))


(provide 'outline-mode-easy-bindings)