Emacs:*仅*在注释中突出显示 TODO
这个问题与另一个问题相关,Emacs :TODO 指示器位于左侧。我最近遇到了一个我非常喜欢的小模式,称为 FixmeMode。它支持自动突出显示TODO标记,并在它们之间导航。但是,我认为仅在注释中识别“TODO”字符串比污染整个文件更有意义。是否可以?
This question is related to another one, Emacs :TODO indicator at left side. I recently came across a minor mode I like a lot called FixmeMode. It supports auto highlighting of TODO marks, and navigating between them. However, I think it makes more sense to recognize the "TODO" strings only in comments, rather than polluting the whole file. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看库 fic-mode.el,它已在 C++ 中验证和 Emacs-Lisp。
专门为了回答这个问题而写的。
安装就像任何标准包一样:
虽然 Wei Hu 确实要求一种简单的方法将其添加到多种模式,所以这里是:
Check out the library fic-mode.el, it has been verified in C++ and Emacs-Lisp.
It was written specifically to answer this question.
The installation is like any standard package:
Though Wei Hu did ask for an easy way to add it to multiple modes, so here goes:
这是可能的,但有点棘手。 Fixme 模式使用
font-lock
来突出显示,因此它会在您键入时突出显示关键字。字体锁定挂钩在一个非常低的级别,基本上在每次对缓冲区内容进行更改后运行。不过,它经过了高度优化,可以在现代计算机上即时显示。左边缘的 TODO 指示器是静态的。执行该功能,当前所有 TODO 都会突出显示;更改缓冲区(添加或删除 TODO)不会更改边缘指示器;仅当函数再次运行时才会改变。
您的方法必须进入语法表,首先确定您何时处于评论中,然后查找关键字。棘手的部分在于以交互方式执行此操作(即在您键入时)。您应该能够连接到
font-lock
构造来执行此操作,但是您提供的用于搜索注释语法表,然后搜索关键字的函数会非常高效,因为它将运行每次缓冲区更改时(尽管我认为它只会在更改的区域上运行)。您可能希望将所有这些内容填充到font-lock-syntropic-keywords
而不是font-lock-keywords
中,因为语法关键字传递发生在语法传递之前(其中发生在关键字传递之前),并且您需要在设置注释本身之前在注释内设置 TODO。抱歉,这不是完整的工作代码答案......
It's possible but quite a bit trickier. Fixme mode uses
font-lock
to do its highlighting, so it works on an as-you-type basis to highlight the keywords. Font-lock hooks in at a very low level, basically running after every change is made to the buffer's contents. It is highly optimized, though, which allows it to appear instantaneous on modern computers.The TODO indicator in the left fringe is static. Execute the function and all current TODO's are highlighted; change the buffer (adding or removing TODO's) does not change the fringe indicator; that's only changed when the function runs again.
Your approach would have to get into syntax tables, determining first when you're in a comment and then looking for the keywords. The tricky part comes in doing this interactively (i.e. as you type). You should be able to hook into the
font-lock
constructs to do this, but the function you provide to search for the comment syntax table and then for the keywords better be very efficient, as it will be run each and every time a buffer changes (though it will only run on the changed region, I think). You would want to stuff all of this infont-lock-syntactic-keywords
rather thanfont-lock-keywords
because the syntactic-keyword pass happens before the syntactic pass (which happens before the keyword pass), and you need to set TODO inside comments before comments themselves are set.Sorry it's not a full working-code answer.....
也许这会有所帮助:有一个 fn
c-in-literal
cc-mode,以及 csharp 模式中类似的
csharp-in-literal
。这如果在 C 风格注释中,返回值为
c
;如果在 C++ 中,返回值为c++
风格评论。您可以将其添加到代码中
Emacs:左侧的 TODO 指示器
得到你想要的。
Maybe this will help: there's a fn
c-in-literal
incc-mode, and a similar
csharp-in-literal
in csharp mode. Thereturn value is
c
if in a C-style comment,c++
if in a C++style comment. You could add that to the code at
Emacs :TODO indicator at left side
to get what you want.
https://github.com/tarsius/hl-todo 似乎完全符合你的要求。我刚刚尝试过并且喜欢它。
https://github.com/tarsius/hl-todo seems to do exactly what you want. I just tried it and love it.