Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: highlighting TODO *only* in comments

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?

like image 609
Wei Hu Avatar asked Mar 02 '10 23:03

Wei Hu


3 Answers

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)
like image 155
Trey Jackson Avatar answered Oct 31 '22 16:10

Trey Jackson


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

like image 42
Joe Casadonte Avatar answered Oct 31 '22 16:10

Joe Casadonte


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))))))))))
like image 2
Cheeso Avatar answered Oct 31 '22 15:10

Cheeso