Q for Emacs users: code-folding (hideshow)

Discussion in 'Python' started by kj, Jul 15, 2010.

  1. kj

    kj Guest

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

    How do you do Python code-folding in Emacs?

    Thanks!

    ~K
     
    kj, Jul 15, 2010
    #1
    1. Advertising

  2. kj

    Peter Guest

    On Jul 16, 2:45 am, kj <> 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 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
     
    Peter, Jul 15, 2010
    #2
    1. Advertising

  3. kj

    thebjorn Guest

    On Jul 15, 10:34 pm, Peter <> wrote:
    > On Jul 16, 2:45 am, kj <> 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

    >

    [...]
    > 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...
     
    thebjorn, Jul 16, 2010
    #3
  4. kj

    ernest Guest

    On 15 Jul, 18:45, kj <> 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.

    Bye.

    Ernest
     
    ernest, Jul 16, 2010
    #4
  5. On Fri, 16 Jul 2010 06:29:46 -0700 (PDT) ernest <>
    wrote:

    > On 15 Jul, 18:45, kj <> 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.
    >

    Yeah, that's Emacs for ya ...

    Like, BURRRN!


    ....

    OK, I'll move along.
    /W

    --
    INVALID? DE!
     
    Andreas Waldenburger, Jul 16, 2010
    #5
  6. On Fri, Jul 16, 2010 at 9:29 AM, ernest <> wrote:
    > On 15 Jul, 18:45, kj <> 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.
     
    David Robinow, Jul 16, 2010
    #6
  7. kj

    Anssi Saari Guest

    David Robinow <> 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?
     
    Anssi Saari, Jul 17, 2010
    #7
  8. * 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)
     
    Teemu Likonen, Jul 17, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin
    Replies:
    0
    Views:
    1,113
    Martin
    Jul 3, 2006
  2. Li Daobing
    Replies:
    1
    Views:
    1,098
    Sean Richards
    Oct 13, 2004
  3. John Salerno
    Replies:
    14
    Views:
    804
  4. Andrew Walrond

    Folding editor for ruby code browsing

    Andrew Walrond, Apr 24, 2005, in forum: Ruby
    Replies:
    25
    Views:
    286
    Andrew Walrond
    Apr 27, 2005
  5. Adam Funk
    Replies:
    4
    Views:
    258
    Adam Funk
    Jan 29, 2007
Loading...

Share This Page