Emacs Auctex定制语法高亮 [英] Emacs Auctex custom syntax highlight

查看:211
本文介绍了Emacs Auctex定制语法高亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强调我在LaTeX中创建的一个新命令:

  \新命令{\conceito} [3] {
\subsection {#1}(Original:\textit {#2}#3)。
}

我以这种方式使用这个代码:

  \conceito {Foo} {Bar} {Bla} 

我遵循手册,并将此代码放入我的〜/ .emacs ,但它没有工作:

 (add-hook'LaTeX-mode-hook 
(lambda()
(font-lock-add-keywords nil
'((\\\ \\\(\\conceito)\\>1 font-lock-warning-face t)))))

有什么问题?

解决方案

编辑:Deokhwan Kim原来指出您的正则表达式包含两个连续的双引号,并且右括号需要使用双引号进行转义:

 (add-hook'LaTeX-mode-hook 
(lambda()
(font-lock-add-keywords nil
'((\\< \\ (\\conceito\\)\\>1 font-lock-warning-face t)))))

除了Deokhwan Kim指出的要点之外,还有以下两个问题:




  • 在conceito前面需要四个反斜杠而不是两个反斜杠: \\\\\conceito


  • 反斜杠序列 \\< 仅在单词开头匹配空字符串,但是开头的反斜杠您的新LaTeX命令不被视为单词的一部分,因此 \\ <将不匹配。




尝试改为:

  add-hook'LaTeX-mode-hook 
(lambda()
(font-lock-add-keywords nil
'((\\\\\\\\)\\>1 font-lock-warning-face t)))

编辑:Deokhwan Kim所做的另一个好的观察是,在这种特殊情况下,你根本不需要圆括号,因为你试图匹配整个表达式。所以最后一行的替代方法可以是:

 '((\\\\ \\\conceito\\>0 font-lock-warning-face t)))))

关于括号的一点是正确的,但是您可以将正则表达式扩展到只有当开放式大括号 {跟随conceito一词时才匹配)。但是,由于您不希望突出显示该括号,所以使用由括号定义的子组是可行的:

 (add-hook'LaTeX-mode-hook 
(lambda()
(font-lock-add-keywords nil
'((\\ (\\\\\conceito\\)\\s - * {1 font-lock-warning-face t)))
{(除非有空格)之间),我们根本不需要 \\> 的测试。



一般来说,尝试 Mx重建器以交互方式制作正则表达式:您可以在一个小型缓冲区中编辑一个新的正则表达式,并立即看到在调用重建器的缓冲区中突出显示的内容。 / p>

I'd like to highlight a new command I created in LaTeX:

\newcommand{\conceito}[3]{
  \subsection{#1} (Original: \textit{#2} #3).
}

I use this code in this way:

\conceito{Foo}{Bar}{Bla}

I followed the manual and put this code in my ~/.emacs, but it didn't work:

(add-hook 'LaTeX-mode-hook
    (lambda ()
        (font-lock-add-keywords nil
          '((""\\<\\(\\conceito)\\>"" 1 font-lock-warning-face t)))))

What's wrong?

解决方案

EDIT: Deokhwan Kim originally pointed out that your regexp contains two consecutive double quotes, and that the closing parenthesis ) needs to be escaped with double quotes as well:

(add-hook 'LaTeX-mode-hook
    (lambda ()
        (font-lock-add-keywords nil
          '(("\\<\\(\\conceito\\)\\>" 1 font-lock-warning-face t)))))

In addition to the points pointed out by Deokhwan Kim, there are also the following two issues:

  • You need four backslashs instead of two in front of 'conceito': \\\\conceito

  • The backslash sequence \\< matches the empty string only at the beginning of a word, however, the backslash at the beginning of your new LaTeX command is not considered part of a word, so \\< will not match.

Try this instead:

(add-hook 'LaTeX-mode-hook
    (lambda ()
        (font-lock-add-keywords nil
            '(("\\(\\\\conceito\\)\\>" 1 font-lock-warning-face t)))

EDIT: Another good observation that Deokhwan Kim made is that in this particular case, you don't really need the parenthesis at all, because you're attempting to match the whole expression anyway. So an alternative to the last line could be:

'(("\\\\conceito\\>" 0 font-lock-warning-face t)))))

The point about the parenthesis is correct, but you could actually extend your regexp to only match when an opening curly brace { follows the word "conceito". But since you don't really want to highlight that brace, using sub-groups defined by parentheses is the way to go:

(add-hook 'LaTeX-mode-hook
    (lambda ()
        (font-lock-add-keywords nil
            '(("\\(\\\\conceito\\)\\s-*{" 1 font-lock-warning-face t)))

Note that since we're testing for a { that follows directly after "conceito" (unless there's whitespace in between), we don't need the test for \\> any more at all.

In general, try M-x re-builder to craft regular expression interactively: you can edit a new regexp in a small buffer and instantly see what is highlighted in the buffer from which you invoked the re-builder.

这篇关于Emacs Auctex定制语法高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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