Code folder with Emacs

G

Grant Edwards

Has anybody figured out how to do code folding of Python source
files in emacs?

I tried "hide-show" minor mode, but it doesn't really work for
Python code: the only think it knows how to hide/show are
function bodies. It can't do normal things like hide/show a
code block like it can for other languages.

Google also found my "folding mode", but that's based on
user-inserted tokens and isn't syntax aware.
 
D

Diez B. Roggisch

Grant said:
Has anybody figured out how to do code folding of Python source
files in emacs?

I tried "hide-show" minor mode, but it doesn't really work for
Python code: the only think it knows how to hide/show are
function bodies. It can't do normal things like hide/show a
code block like it can for other languages.

Google also found my "folding mode", but that's based on
user-inserted tokens and isn't syntax aware.

I just recently started hacking in emacs, to enhance the python-mode and
make pdb work with persisten breakpoints (by that I mean BPs that
survive one debug-session).

Code-folding isn't currently on my agenda, but an interesting idea.
given that e.g. ecb already has structural analysis buffers, there are
python-aware code parsers (cedet?)

So it shouldn't be too hard. The only interesting/important thing would
be to integrate it with ecb because I wouldn't want several parse-runs
at once.


BTW, I can highly recommend flymake for running pylint over the sources!
That really helps me a lot these days!

Diez
 
G

Grant Edwards

I just recently started hacking in emacs, to enhance the
python-mode and make pdb work with persisten breakpoints (by
that I mean BPs that survive one debug-session).

Code-folding isn't currently on my agenda, but an interesting
idea. given that e.g. ecb already has structural analysis
buffers, there are python-aware code parsers (cedet?)

Given the simplicity of python's indentation-defined
block-delimiting, It's hard to understand how hide-show managed
to come up with a "block" definition that works for function
bodies but not for other identically delimited blocks.
So it shouldn't be too hard. The only interesting/important
thing would be to integrate it with ecb because I wouldn't
want several parse-runs at once.

When I have some time, I'm going to take alook at hide-show's
Python support, but my lisp skills are a bit rusty...
 
M

Martin Sand Christensen

Grant> Has anybody figured out how to do code folding of Python source
Grant> files in emacs?

I use outline-minor-mode with the following home baked configuration:

;; Python stuff for outline mode.
(defvar py-outline-regexp "^\\([ \t]*\\)\\(def\\|class\\|if\\|elif\\|else\\|while\\|for\\|try\\|except\\|with\\)"
"This variable defines what constitutes a 'headline' to outline mode.")

(defun py-outline-level ()
"Report outline level for Python outlining."
(save-excursion
(end-of-line)
(let ((indentation (progn
(re-search-backward py-outline-regexp)
(match-string-no-properties 1))))
(if (and (> (length indentation) 0)
(string= "\t" (substring indentation 0 1)))
(length indentation)
(/ (length indentation) py-indent-offset)))))
(add-hook 'python-mode-hook
'(lambda ()
(outline-minor-mode 1)
(setq
outline-regexp py-outline-regexp
outline-level 'py-outline-level)))


Martin
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top