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

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

问题描述

有些背景,我对Emacs Lisp感到满意,并且写了很多行。然而,我从来没有写过一个主要的模式,所以我对字体锁定机制的作用是非常新的。



对于我目前的项目,我想添加内联的javascript和css突出显示到 html-mode 。目前,我使用MMM模式,但它是庞大的,我不使用其他功能,所以我只想做一个小模式,甚至只是一个黑客,我可以添加到 sgml-mode-hook 只是做突出显示。



我发现这个部分,这非常缺乏一个例子,而这个emacswiki 页面的破解代码。



有人可以显示我明白了一个如何做到这一点的例子?



编辑:我应该澄清一点,我不想看到模式特定的字体锁定在javascript / css块。唯一的要求是我可以通过对他们应用不同的面孔来看到这些块。

解决方案

在下面的示例中,我使用锚定形式的font-lock关键字,它允许您搜索超过当前行。 诀窍是预挂钩做两件事情:1)它允许您将点定位到搜索的开始,2)它允许您通过返回结束位置来限制搜索。在下面的示例中,我使用了第二个属性。



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

 (defun my-end-of-paragraph-position(& rest foo)
返回下一个空行的位置
(保存-excursion
(while(not(or(eobp));停止在缓冲区结束
(和(bolp);或空行
(eolp))))
(转发))
(点)))

(setq font-lock-multiline t)

(font-lock-add-keywords nil
'((^ FOO
(0 font-lock-keyword-face);;面对FOO
(BAR
(my-end-of-段落位置)
nil
(0 font-lock-variable-name-face)))))

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

  FOO BAR BAR BAR BAR 
BAR BAR BAR BAR

BAR BAR BAR BAR


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.

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.

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?

EDIT: 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.

解决方案

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.

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)))))

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天全站免登陆