将 python-mode 与 emacs 的 org-mode 结合起来

发布于 2024-09-29 23:30:39 字数 642 浏览 0 评论 0 原文

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

但是,用 python-mode 做同样的事情不起作用...可能是因为 python 注释使用的“#”字符干扰了 org-mode 设置...

任何人能够成功地组合功能吗? 我知道人们将 python 模式与大纲模式结合起来(链接),但 ouline-mode 不如 org-mode...

编辑:让它与 outline-magic: python-magic.el

I combined org-mode with lisp-mode to achieve beautiful code folding in emacs for lisp code: lisp-orgi-mode.
Basically, I use ';' instead of '*' as the heading character. For comments, I just put a space before the ';', making it ' ;' so it doesn't count as a heading...

However, doing the same thing with python-mode doesn't work... Probably because the '#' character used by python comments interferes with org-mode settings...

Anyone been able to combine the functionality successfully ?
I know people have combined python-mode with outline-mode (link), but ouline-mode isn't as good as org-mode...

Edit: Got it working nicely with outline-magic: python-magic.el

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月无声 2024-10-06 23:30:39

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

I use hideshow-org (and a small introduction here) for this purpose, and I think it works really, really good.

These are some additional, but useful snippets:

(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)))
非要怀念 2024-10-06 23:30:39

好吧,我的大纲次要模式与以下大纲正则表达式配合得很好: "[ \t]*# \|[ \t]+\(class\|def\|if\|elif\|else\|while \|对于\|尝试\|除了\|与\) "
现在我使用 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)

Ok, I got outline-minor-mode working nicely with the following outline-regexp: "[ \t]*# \|[ \t]+\(class\|def\|if\|elif\|else\|while\|for\|try\|except\|with\) "
Now I get code folding using both python syntax and comment lines as headings.
Would it be possible to adapt your code for using tab to call 'indent-for-tab-command and if there is nothing to do, call '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)
伤感在游骋 2024-10-06 23:30:39

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

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

I think outline-magic has been superseded by outshine, and I don't know if the code above works with it. But I was able to get the following code to work, courtesy of 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)

Maybe someone will find this useful. I think it's an amazing way to make editing and navigating code easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文