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

时间:2010-11-02 16:10:51

标签: python emacs org-mode folding

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

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

任何人都能成功组合这些功能吗? 我知道人们已经将python-mode与outline-mode(link)结合起来,但是ouline-mode不如org-mode ...

编辑:使用outline-magic:python-magic.el

很好地工作

3 个答案:

答案 0 :(得分:10)

我为此目的使用hideshow-organd a small introduction here),我觉得它真的非常好用。

这些是一些额外但有用的摘录:

(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)))

答案 1 :(得分: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)

答案 2 :(得分:1)

我认为outline-magic已被outshine取代,我不知道上面的代码是否适用。但是我能够使用a blog post by Ryan Davis提供以下代码:

(defun python-mode-outline-hook ()
  (setq outline-level 'python-outline-level)

  (setq outline-regexp
    (rx (or
         ;; Commented outline heading
         (group
          (* space)  ; 0 or more spaces
          (one-or-more (syntax comment-start))
          (one-or-more space)
          ;; Heading level
          (group (repeat 1 8 "\*"))  ; Outline stars
          (one-or-more space))

         ;; Python keyword heading
         (group
          ;; Heading level
          (group (* space)) ; 0 or more spaces
          bow
          ;; Keywords
          (or "class" "def" "else" "elif" "except" "for" "if" "try" "while")
          eow)))))

(defun python-outline-level ()
  (or
   ;; Commented outline heading
   (and (string-match (rx
               (* space)
               (one-or-more (syntax comment-start))
               (one-or-more space)
               (group (one-or-more "\*"))
               (one-or-more space))
              (match-string 0))
    (- (match-end 0) (match-beginning 0)))

   ;; Python keyword heading, set by number of indentions
   ;; Add 8 (the highest standard outline level) to every Python keyword heading
   (+ 8 (- (match-end 0) (match-beginning 0)))))

(add-hook 'python-mode-hook 'python-mode-outline-hook)

也许有人会觉得这很有用。我认为这是一种让编辑和导航代码更容易的惊人方法。