Code folder with Emacs

Martin Sand Christensen msc at
Tue Mar 25 11:14:54 CET 2008

>>>>> "Grant" == Grant Edwards <grante at> writes:
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."
    (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)
	      outline-regexp py-outline-regexp
	      outline-level 'py-outline-level)))


More information about the Python-list mailing list