Q for Emacs users: code-folding (hideshow)

K

kj

This is a question _for Emacs users_ (the rest of you, go away :) ).

How do you do Python code-folding in Emacs?

Thanks!

~K
 
P

Peter

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 don't - when I first looked at folding-mode (I assume this is what
you are referring too?) I couldn't be bothered putting the {{{ }}}
strings into the code comments - maybe somebody else has had better
success with it? But I just couldn't be bothered. Although if you use
a template generating system such as ELSE, you could probably work it
into the code templates and have them generated automatically. I must
admit that I started to modify my code templates once upon a time to
provide this but lost interest part way through and just put up with
splitting the window and "hiding" the intervening code that way.

Personally I think something like "folding" would be better worked
into the major mode and performed on a syntactic scan basis i.e. the
elisp must understand where the code blocks begin and end and make
folding decisions based on where point is located in relation to
surround code blocks.

Actually, it might be possible now that pymacs is available (well, I
say "now" - even though pymacs has been available for quite some time,
I looked at folding mode even earlier!) i.e. call a Python helper
program that would provide the syntactic scanning of the buffer
contents and return appropriate markers into the buffer for the
folding mode code to work with. One day when I have run out of other
(programming) things to do I might investigate this :)

Anybody else now of any better ideas or whatever? Now that I think
about it, I wouldn't mind using folding mode if I could make it
"easier" to use myself! :)

Peter
 
T

thebjorn

This is a question _for Emacs users_ (the rest of you, go away :)  ).
How do you do Python code-folding in Emacs?
[...]
Anybody else now of any better ideas or whatever? Now that I think
about it, I wouldn't mind using folding mode if I could make it
"easier" to use myself! :)

Peter

I gave up too :-(

Komodo from ActiveState has Emacs bindings and folding, and is
generally a good editor and environment for when you want such a
thing. It also has an out-of-process debugger that has helped me solve
some nasty bugs.

Still, I keep going back to Emacs, it's just snappier(*) and
easier(**) to work with... Instead of folding I either split the
window or create a new Frame (Ctrl 5 2) for the content I wish to
refer to.

-- bjorn


(*) ha!
(**) ...I know...
 
E

ernest

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.

Bye.

Ernest
 
A

Andreas Waldenburger

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.
Yeah, that's Emacs for ya ...

Like, BURRRN!


....

OK, I'll move along.
/W
 
D

David Robinow

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.
 
A

Anssi Saari

David Robinow said:
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?
 
T

Teemu Likonen

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top