10

我将 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

4

3 回答 3

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)))
于 2010-11-03T19:11:12.313 回答
6

好的,我得到了大纲次要模式,可以很好地使用以下大纲正则表达式: "[ \t]*# \|[ \t]+\(class\|def\|if\|elif\|else\|while \|for\|try\|except\|with\) " 现在我使用 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://stackoverflow.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)
于 2010-11-04T04:25:57.097 回答
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)

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

于 2015-03-15T06:12:59.260 回答