Emacs Polymode对Markdown和Python的支持

3

我使用python3的pweave库(http://mpastell.com/pweave/usage.html)进行文学编程

pweave以markdown为文本模式,以python3为代码模式,并且可以使用nowebhttps://en.wikipedia.org/wiki/Noweb)文学编程语法。

为了在emacs中正确显示语法高亮,我尝试使用polymode库(https://polymode.github.io/https://github.com/polymode)。

我使用的是emacs版本26.1,我能够从melpa中安装polymode。

不幸的是,在主机模式为markdown、内部模式为python3、语法为noweb的情况下没有预先存在的polymode,因此我尝试根据文档和现有代码编写自己的poly-pweave-mode,通过将以下lisp代码放入我的.emacs文件来实现。

(require 'polymode-classes)

(defcustom pm-host/pweave-text
  (pm-host-chunkmode :name "pweave-text"
                     :mode 'markdown-mode)
  "markdown host chunkmode"
  :group 'poly-hostmodes
  :type 'object)

(defcustom  pm-inner/pweave-code
  (pm-inner-chunkmode :name "pweave-code"
                      :head-matcher "^[ \t]*<<\\(.*\\)>>="
                      :tail-matcher "^[ \t]*@.*$"
                      :mode 'python-mode)
  "noweb static python3 inner chunkmode."
  :group 'poly-innermodes
  :type 'object)

(define-polymode poly-pweave-mode
  :hostmode 'pm-host/pweave-text
  :innermode 'pm-inner/pweave-code)

(add-to-list 'auto-mode-alist '("\\.pymd" . poly-pweave-mode))

但是某种原因,Emacs 没有处理这个。

当我打开 Emacs 时,我会得到以下错误:

Warning (initialization): An error occurred while loading `/Users/abc/.emacs':

Symbol's function definition is void: pm-host-chunkmode

To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file.  Start Emacs with
the `--debug-init' option to view a complete error backtrace.

我做错了什么?我该如何使所需的polymode运行?


1
我无法提供解决方案,但很想看到它能够工作,因为我越来越多地使用Python而不是传统上使用Rmarkdown的R。如果您还没有在Emacs Stackexchange上发布,这可能值得一试。 - slackline
找了一些资料,虽然有点过时,但也许这个帖子会有用。 - slackline
@slackline:抱歉回复这么慢。我联系了 polymode 包的作者 Vspinu ,我们针对该主题开了一个问题: https://github.com/polymode/polymode/issues/180 。该问题尚未完全解决(20181031),但是您应该能够在 Emacs 中运行“用于 Markdown 和 Python 的 polymode”版本,按照我们在那里的讨论。 - bue
谢谢提醒,看起来非常实用,即使还没有完全解决。干杯! - slackline
另外,我找到了这篇帖子: - bue
1个回答

3
这是关于如何指定markdown-python3-noweb polymode的解决方案。
;; define pwn polymode
(require 'poly-noweb)
(require 'poly-markdown)

(defcustom pm-inner/noweb-python
  (clone pm-inner/noweb
         :name "noweb-python"
         :mode 'python-mode)
  "Noweb for Python"
  :group 'poly-innermodes
  :type 'object)

(define-polymode poly-pweave-mode poly-markdown-mode
  :innermodes '(pm-inner/noweb-python :inherit))

(add-to-list 'auto-mode-alist '("\\.pymd" . poly-pweave-mode))

感谢 polymode 插件的作者 Vitalie Spinu 帮助我解决了这个问题!详细讨论请查看Github上的 polymode 问题180:https://github.com/polymode/polymode/issues/180
另外,我在emacs stack exchange找到了这篇文章:https://emacs.stackexchange.com/questions/20136/pythontex-and-auctex。按照这篇文章中的方法,我成功使用 markdown-python3-noweb mmm-mode
;; define pwn multi major modes mode
(require 'mmm-auto)

(mmm-add-classes
 '((noweb-python
    :submode python-mode
    :face mmm-default-submode-face
    :front "^<<.*>>=\n"
    :back "^@$")))

(setq mmm-global-mode 'maybe)
(mmm-add-mode-ext-class 'markdown-mode nil 'noweb-python)

(add-to-list 'auto-mode-alist '("\\.pymd" . markdown-mode))

感谢Jean Pierre提供的详细解释,使得我能够轻松地运行它!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接