在Emacs中将Python模式与Org模式结合使用

10 投票
3 回答
3372 浏览
提问于 2025-04-16 06:26

我把 org-mode 和 lisp-mode 结合起来,让 emacs 中的 lisp 代码可以漂亮地折叠起来,具体的实现可以在这里找到:lisp-orgi-mode。基本上,我用 ';' 代替 '*' 作为标题字符。对于注释,我只需在 ';' 前加一个空格,变成 ' ;',这样它就不会被当作标题了……

不过,用同样的方法在 python-mode 中就不行了……可能是因为 python 的注释用 '#' 字符,这和 org-mode 的设置冲突了……

有没有人成功地把这两者的功能结合起来?我知道有人把 python-mode 和 outline-mode 结合过(链接),但 outline-mode 没有 org-mode 好用……

编辑:我用 outline-magic 让它运行得很好:python-magic.el

3 个回答

1

我觉得 outline-magic 已经被 outshine 取代了,我不确定上面的代码是否能和它一起工作。不过,我能让下面的代码正常运行,这要感谢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)

也许有人会觉得这个有用。我觉得这是一个很棒的方法,可以让编辑和浏览代码变得更简单。

6

好的,我把outline-minor-mode设置得很好,使用的outline-regexp是:"[ \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)
9

我使用了 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)))

撰写回答