Search code examples
pythonemacsorg-modefolding

combine python-mode with org-mode for emacs


I combined org-mode with lisp-mode to achieve beautiful code folding in emacs for lisp code: lisp-orgi-mode. Basically, I use ';' instead of '*' as the heading character. For comments, I just put a space before the ';', making it ' ;' so it doesn't count as a heading...

However, doing the same thing with python-mode doesn't work... Probably because the '#' character used by python comments interferes with org-mode settings...

Anyone been able to combine the functionality successfully ? I know people have combined python-mode with outline-mode (link), but ouline-mode isn't as good as org-mode...

Edit: Got it working nicely with outline-magic: python-magic.el


Solution

  • I use hideshow-org (and a small introduction here) for this purpose, and I think it works really, really good.

    These are some additional, but useful snippets:

    (dolist (hook (list 'c-mode-common-hook
                'emacs-lisp-mode-hook
                'java-mode-hook
                'lisp-mode-hook
                'perl-mode-hook
                'sh-mode-hook))
      (add-hook hook 'my-hideshow-hook))
    
    (defun my-hideshow-hook ()
      "thisandthat."
      (interactive)
      (progn (require 'hideshow-org)
         (global-set-key (kbd "C-c h") 'hs-org/minor-mode)
         (hs-org/minor-mode)))
    
    (defadvice goto-line (after expand-after-goto-line activate compile)
      "hideshow-expand affected block when using goto-line in a collapsed buffer"
      (save-excursion
        (hs-show-block)))