谁能给我提供一个 emacs 中主要模式的 hello world 示例?我想这是一个初学者的问题,但我还是很喜欢写一个主要模式,既学习 emacs 又学习 elisp,以便能够充分利用定制。

到目前为止我所做的(并且正在发挥作用):

  • 编写一个文件sample-mode.el并将其放入lisp目录中
  • 在 .emacs 中调用 (require 'sample-mode)
  • 在里面写了一些defun,并在最后提供了 (provide 'sample-mode)

但似乎仍然不是 活性, ,我无法用 M-sample-mode 来调用它。

那么如何做到这一点呢?谁能为我提供一个非常非常简单的 Hello World 工作示例?

有帮助吗?

解决方案

好吧,经过更多的谷歌搜索后,我至少又前进了一步:

(define-derived-mode sample-mode ...) 

因为提供的内容并没有像我首先想到的那样定义模式。这是我发现的:

http://xahlee.org/emacs/elisp_syntax_coloring.html

对于 emacs 爱好者来说,这是一个非常非常好的网站。

在此帮助下:我现在自己制作了一个 HelloWorld 示例:这是一个(尽可能小的)Csharp 模式。我用过 欧拉1 作为示例而不是 HelloWorld。您需要了解的文件是:

  • 将应用该模式的文件 Euler1.cs
  • .emacs
  • 当然还有模式本身

既然一张照片值得,至少有一堆文字:1 个屏幕上的所有文件:

alt text

但由于这张漂亮的照片似乎有一半时间就消失了(有人有线索吗?在新选项卡中打开总是会打开它,并且网址也可以)也有一些词:-):

  1. 模式:cs-模式.el

    (setq myKeywords 
     '(("WriteLine" . font-lock-function-name-face)
       ("public\\|static\\|void\\|int\\|for\\|if\\|class"
    . font-lock-constant-face)))
    
    (define-derived-mode cs-mode fundamental-mode
      (setq font-lock-defaults '(myKeywords)))
    
    (provide 'cs-mode)
    
  2. .emacs,使 .cs 文件以正确的模式打开:

;; cs
(require 'cs-mode)
(add-to-list 'auto-mode-alist '("\\.cs\\'" . cs-mode))

就这样 :这 cs-code 本身对她来说没什么用,因为它不会显示出给关键词着色的效果。要查看该图片,或在另一个选项卡/窗口中打开图片。

干杯,ph

其他提示

网上有几个例子 像这样。我还可以向您推荐几本 Emacs 书籍:

  • 学习 GNU Emacs(最好的恕我直言)
  • 编写 GNU Emacs 扩展
  • 官方 GNU Emacs lisp 参考/手册

好吧,让我们从 这个答案, ,它使用 define-generic-mode.

用一些评论字符来充实它,例如: /* */, ,一些关键词: hello hi 等等,重新使用原始答案中的内容、文件扩展名 .hello, ,以及进行进一步定制的函数调用。

还有额外的一行可以让自动加载工作,但你必须 生成loaddefs.el 文件。这比 hello world 更高级。

而且,你最终会得到这样的结果:

(make-face 'my-date-face)
(set-face-attribute 'my-date-face nil :underline t)
(set-face-attribute 'my-date-face nil :family "times")
(set-face-attribute 'my-date-face nil :slant 'normal)
(set-face-attribute 'my-date-face nil :height '340)

;;;###autoload
(define-generic-mode hello-world
  '(("/*" . "*/"))                           ; comment characters
  '("hello" "hi" "howdy" "greetings" "hola") ; keywords
  '(("\\([0-9]+/[0-9]+/[0-9]+\\)"
     (1 'my-date-face)))                ; font lock
  '("\\.hello$")                        ; auto-mode-alist  
  '(hello-world-special-setup)          ; function-list
  "An example major mode.
We have comments, keywords, a special face for dates, and recognize .hello files.")

(defun hello-world-special-setup ()
  "Some custom setup stuff done here by mode writer."
  (message "You've just enabled the most amazing mode ever."))

Elisp手册很好地介绍了主要模式,它包括一个呈现“hello-world”的节点 例子. 。我认为至少这是意图。

这些示例可能无法涵盖您正在寻找的所有内容。在这种情况下,请考虑请求您认为缺少的任何对用户有更多帮助的内容。为此,请使用 M-x report-emacs-bug (这也适用于增强请求)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top