将python模式与emacs的org模式相结合
题
我将ORG模式与LISP模式相结合,以实现LISP代码的Emacs中的美丽代码折叠: LISP-ORGI模式。基本上,我使用';'而不是“*”作为标题角色。为了评论,我只是在“;”之前放一个空间;因此,它不算为标题...
但是,使用Python模式进行同样的事情不起作用...可能是因为Python注释使用的“#”字符会干扰Org-Mode设置...
有人能够成功地结合功能吗?我知道人们已经将Python模式与大纲模式相结合(关联),但是OUline模式不如org模式...
编辑:让它与 大纲魔术: :python-magic.el
解决方案
我用 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)))
其他提示
好的,我得到了以下大纲 - regexp的概述 - 刻录模式:“ [ t]*# | [ t]+(class | def | if | elif | elif | else | while while while while | for | try | extbore | with )“现在我使用Python语法和评论行作为标题获得代码折叠。
是否可以适应您的代码来使用选项卡来调用“缩进为tab-command”,如果无事可做,请调用'outline'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)
我认为 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)
也许有人会发现这有用。我认为这是使编辑和导航代码更加容易的一种了不起的方法。
不隶属于 StackOverflow