【问题标题】:combine python-mode with org-mode for emacs将 python-mode 与 emacs 的 org-mode 结合使用
【发布时间】:2011-05-04 00:56:53
【问题描述】:

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

但是,用 python-mode 做同样的事情是行不通的...可能是因为 python cmets 使用的 '#' 字符会干扰 org-mode 设置...

有人能够成功地结合功能吗? 我知道人们已经将 python-mode 与 outline-mode (link) 结合在一起,但 ouline-mode 不如 org-mode...

编辑:与outline-magic 很好地配合使用:python-magic.el

【问题讨论】:

    标签: python emacs org-mode folding


    【解决方案1】:

    我为此使用了hideshow-org (and a small introduction here),我认为它真的非常好用。

    这些是一些额外但有用的 sn-ps:

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

    【讨论】:

    • 不错。是否可以循环折叠级别超过一次迭代?
    • 不,它不如 org-mode 循环不同的折叠级别。但它仍然非常有用,而且比 C-x n n 快很多。
    • 下面的解决方案与 org-mode 一样好,可以循环不同的折叠级别。你试过了吗?
    • 不,没有试过下面的代码。上面的代码几乎适用于任何具有相同功能的语言(lisp、java、python...),非常棒。
    【解决方案2】:

    好的,我得到了大纲小模式与以下大纲正则表达式的完美配合:“[ \t]*# \|[ \t]+\(class\|def\|if\|elif\|else \|而\|对于\|尝试\|除了\|与\)" 现在,我使用 python 语法和注释行作为标题进行代码折叠。
    是否可以调整您的代码以使用 tab 调用 'indent-for-tab-command ,如果无事可做,调用 'outline-cycle ?

    python-magic.el:

    ;需要 CarstenDominik 的 outline-magic.el 在这里找到: ; http://www.astro.uva.nl/~dominik/Tools/outline-magic.el ; Nikwin在此处修改的代码在这里略有发现: ; http://*.com/questions/1085170/how-to-achieve-code-folding-effects-in-emacs/1085551#1085551 (add-hook 'outline-minor-mode-hook (拉姆达() (需要'大纲魔术) )) (add-hook 'python-mode-hook 'my-python-outline-hook) (defun py-outline-level () (let (buffer-invisibility-spec) (保存游览 (跳过字符向前“”) (当前列)))) (defun my-python-outline-hook () (setq outline-regexp "[ \t]*# \\|[ \t]+\\(class\\|def\\|if\\|elif\\|else\\|while\\|for\\ |尝试\\|除了\\|与\\)") (setq 大纲级别 'py 大纲级别) (outline-minor-mode t) (隐藏身体) (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) ) (提供'python-magic)

    【讨论】:

      【解决方案3】:

      我认为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)
      

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

      【讨论】: