emacs:是否有一个清晰的多行字体锁定示例? [英] emacs: is there a clear example of multi-line font-locking?

查看:22
本文介绍了emacs:是否有一个清晰的多行字体锁定示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景知识,我对 Emacs Lisp 很熟悉,并且已经写了很多行.但是我从来没有写过一个主要模式,所以我对字体锁定机制的工作原理还很陌生.

Some background, I'm comfortable with Emacs Lisp, and have written lots of lines of it. However I've never written a major mode, so I'm fairly new to how the font-locking mechanism works.

对于我当前的项目,我想向 html-mode 添加内联 javascript 和 css 突出显示.目前,我使用 MMM 模式执行此操作,但它体积庞大且我不使用它的其他功能,因此我只想制作一个小模式,甚至只是一个可以添加到 的 hacksgml-mode-hook 只是做突出显示.

For my current project, I'd like to add inlined javascript and css highlighting to html-mode. Currently, I do this with MMM-mode, but it's bulky and I don't use other features of it, so I'd just like to make a minor-mode or even just a hack that I can add to the sgml-mode-hook to just do the highlighting.

我找到了这个部分 手册,它严重缺乏示例,以及这个 emacswiki page 损坏的代码.

I've found this section of the manual, which sorely lacks an example, and this emacswiki page of broken code.

有人可以向我展示如何做到这一点的清晰示例吗?

Can someone show me a clear example of how this is can be done?

我应该澄清一下,我不想在 javascript/css 块中看到特定于模式的字体锁定.唯一的要求是我能够通过对它们应用不同的面来看到块.

I should clarify that I don't want to see mode-specific font-locking within the javascript/css chunks. The only requirement is that I'm able to see the chunks by applying a different face to them.

推荐答案

在下面的例子中,我使用了 font-lock 关键字的锚定"形式,它允许你搜索比当前行更多的内容.技巧"是pre"钩子做两件事:1)它允许您将点定位到搜索的开始位置;2)它允许您通过返回结束位置来限制搜索.在下面的示例中,我使用了第二个属性.

In the example below, I use the "anchored" form of font-lock keywords, it allows you to search more than the current line. The "trick" is that the "pre" hook do two things: 1) it allows you to position the point to the start of the search and 2) it allows you to limit the search by returning the end-position. In the example below, I have used the second property.

请注意,这只是一个概念验证.您需要确保 font-lock-multiline 变量和 font-lock 关键字应用于正确的缓冲区.

Note that this is only a proof-of-concept. You will need to make sure that the font-lock-multiline variable and the font-lock keywords are applied to the correct buffer.

(defun my-end-of-paragraph-position (&rest foo)
  "Return position of next empty line."
  (save-excursion
    (while (not (or (eobp)             ; Stop at end of buffer.
                    (and (bolp)        ; Or at an empty line.
                         (eolp))))
      (forward-line))
    (point)))

(setq font-lock-multiline t)

(font-lock-add-keywords nil
                        '(("^FOO"
                           (0 font-lock-keyword-face)  ;; Face for FOO
                           ("BAR"
                            (my-end-of-paragraph-position)
                            nil
                            (0 font-lock-variable-name-face)))))

下面,将突出显示 BAR 的前两行,但不会突出显示最后一行:

Below, the first two lines of BAR will be highlighted, but not the last:

FOO BAR BAR BAR BAR
BAR BAR BAR BAR

BAR BAR BAR BAR

这篇关于emacs:是否有一个清晰的多行字体锁定示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆