Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 592
Naveen Avatar asked Nov 02 '10 16:11

Naveen


2 Answers

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)))
like image 109
monotux Avatar answered Oct 11 '22 17:10

monotux


Ok, I got outline-minor-mode working nicely with the following outline-regexp: "[ \t]*# \|[ \t]+\(class\|def\|if\|elif\|else\|while\|for\|try\|except\|with\) " Now I get code folding using both python syntax and comment lines as headings.
Would it be possible to adapt your code for using tab to call 'indent-for-tab-command and if there is nothing to do, call 'outline-cycle ?

python-magic.el :

; require outline-magic.el by CarstenDominik found here: 
; http://www.astro.uva.nl/~dominik/Tools/outline-magic.el
; modified code here by Nikwin slightly found here: 
;  http://stackoverflow.com/questions/1085170/how-to-achieve-code-folding-effects-in-emacs/1085551#1085551

(add-hook 'outline-minor-mode-hook 
           (lambda () 
             (require 'outline-magic)
))
(add-hook 'python-mode-hook 'my-python-outline-hook)

(defun py-outline-level ()
  (let (buffer-invisibility-spec)
    (save-excursion
      (skip-chars-forward "    ")
      (current-column))))

(defun my-python-outline-hook ()
  (setq outline-regexp "[ \t]*# \\|[ \t]+\\(class\\|def\\|if\\|elif\\|else\\|while\\|for\\|try\\|except\\|with\\) ")
  (setq outline-level 'py-outline-level)

  (outline-minor-mode t)
  (hide-body)
  (show-paren-mode 1)
  (define-key python-mode-map [tab] 'outline-cycle)
  (define-key outline-minor-mode-map [S-tab] 'indent-for-tab-command)
  (define-key outline-minor-mode-map [M-down] 'outline-move-subtree-down)
  (define-key outline-minor-mode-map [M-up] 'outline-move-subtree-up)
)
(provide 'python-magic)
like image 33
Naveen Avatar answered Oct 11 '22 19:10

Naveen