将python-mode与org-mode结合用于emacs

Nav*_*een 11 python emacs folding org-mode

我将org-mode与lisp-mode相结合,以便在emacs中为lisp代码实现漂亮的代码折叠:lisp-orgi-mode.基本上,我用';' 而不是'*'作为标题字符.对于评论,我只是在';'之前放置一个空格,使其成为';' 所以它不算作标题......

但是,使用python-mode执行相同操作不起作用...可能是因为python注释使用的'#'字符会干扰org-mode设置...

任何人都能成功地组合功能?我知道人们已经将python-mode与outline-mode(链接)相结合,但是ouline-mode不如org-mode ...

编辑:使用outline-magic:python-magic.el 很好地工作

mon*_*tux 10

我为此目的使用hideshow-org(以及一个小介绍),我认为它真的非常好用.

这些是一些额外但有用的片段:

(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)))
Run Code Online (Sandbox Code Playgroud)


Nav*_*een 7

好吧,我使用以下outline-regexp很好地使用了outline-minor-mode:"[\ t]*#\ | [\ t] + \(class\| def\| if\| elif\| else\| while\| for\| try\| except\| with \""现在我使用python语法和注释行作为标题进行代码折叠.
是否可以调整代码以使用tab来调用'indent-for-tab-command,如果没有任何事情可做,请调用'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)