Emacs:*仅*在注释中突出显示 TODO

发布于 2024-08-23 17:24:33 字数 325 浏览 10 评论 0原文

这个问题与另一个问题相关,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有伤那来痛 2024-08-30 17:24:33

查看库 fic-mode.el,它已在 C++ 中验证和 Emacs-Lisp。

专门为了回答这个问题而写的。

安装就像任何标准包一样:

(require 'fic-mode)
(add-hook 'c++-mode-hook 'turn-on-fic-mode) 

虽然 Wei Hu 确实要求一种简单的方法将其添加到多种模式,所以这里是:

(defun add-something-to-mode-hooks (mode-list something)
  "helper function to add a callback to multiple hooks"
  (dolist (mode mode-list)
    (add-hook (intern (concat (symbol-name mode) "-mode-hook")) something)))

(add-something-to-mode-hooks '(c++ tcl emacs-lisp) 'turn-on-fic-mode)

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:

(require 'fic-mode)
(add-hook 'c++-mode-hook 'turn-on-fic-mode) 

Though Wei Hu did ask for an easy way to add it to multiple modes, so here goes:

(defun add-something-to-mode-hooks (mode-list something)
  "helper function to add a callback to multiple hooks"
  (dolist (mode mode-list)
    (add-hook (intern (concat (symbol-name mode) "-mode-hook")) something)))

(add-something-to-mode-hooks '(c++ tcl emacs-lisp) 'turn-on-fic-mode)
皓月长歌 2024-08-30 17:24:33

这是可能的,但有点棘手。 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 in font-lock-syntactic-keywords rather than font-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.....

简美 2024-08-30 17:24:33

也许这会有所帮助:有一个 fn c-in-literal
cc-mode,以及 csharp 模式中类似的 csharp-in-literal。这
如果在 C 风格注释中,返回值为 c;如果在 C++ 中,返回值为 c++
风格评论。您可以将其添加到代码中
Emacs:左侧的 TODO 指示器
得到你想要的。

(defun annotate-todo ()
   "put fringe marker on TODO: lines in the curent buffer"
  (interactive)
  (let (lit)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "TODO:" nil t)
      (progn
        (setq lit (c-in-literal)) ;; or csharp-in-literal
        (if (or (eq lit 'c) (eq lit 'c++))
            (let ((overlay (make-overlay (- (point) 5) (point))))
              (overlay-put overlay 'before-string
                           (propertize "A"
                                       'display
                                       '(left-fringe   ;; right
                                         horizontal-bar
                                         better-fringes-important-bitmap))))))))))

Maybe this will help: there's a fn c-in-literal in
cc-mode, and a similar csharp-in-literal in csharp mode. The
return 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.

(defun annotate-todo ()
   "put fringe marker on TODO: lines in the curent buffer"
  (interactive)
  (let (lit)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "TODO:" nil t)
      (progn
        (setq lit (c-in-literal)) ;; or csharp-in-literal
        (if (or (eq lit 'c) (eq lit 'c++))
            (let ((overlay (make-overlay (- (point) 5) (point))))
              (overlay-put overlay 'before-string
                           (propertize "A"
                                       'display
                                       '(left-fringe   ;; right
                                         horizontal-bar
                                         better-fringes-important-bitmap))))))))))
半城柳色半声笛 2024-08-30 17:24:33

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文