Skip to content

Commit

Permalink
Add flymake backend hl-todo-flymake
Browse files Browse the repository at this point in the history
The backend must be registered as follows in the user configuration:

  (add-hook 'flymake-diagnostic-functions #'flymake-hl-todo nil 'local)

Declare `flymake-make-diagnostic' to avoid byte compilation errors
since the function is only available on Emacs 26 and newer.  The
`hl-todo-flymake' backend is only supported by Emacs 26 and newer.
  • Loading branch information
minad authored and tarsius committed Aug 15, 2023
1 parent 5054954 commit baba5ef
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions hl-todo.el
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

(defvar grep-find-template)
(declare-function grep-read-files "grep" (regexp))
(declare-function flymake-make-diagnostic "flymake")

(defgroup hl-todo nil
"Highlight TODO and similar keywords in comments and strings."
Expand Down Expand Up @@ -373,6 +374,32 @@ Also see option `hl-todo-keyword-faces'."
current-prefix-arg))))
(rgrep regexp files dir confirm))

;;;###autoload
(defun hl-todo-flymake (report-fn &rest plist)
"Flymake backend for `hl-todo-mode'.
Diagnostics are reported to REPORT-FN and additional options are
given as PLIST. Use `add-hook' to register this function in
`flymake-diagnostic-functions' before enabling `flymake-mode'."
(let (diags rbeg rend)
(when hl-todo-mode
(save-excursion
(save-restriction
(save-match-data
(goto-char (or (plist-get plist :changes-start) (point-min)))
(setq rbeg (pos-bol))
(goto-char (or (plist-get plist :changes-end) (point-max)))
(setq rend (pos-eol))
(goto-char rbeg)
(while (hl-todo--search nil rend)
(let ((beg (match-beginning 0))
(end (pos-eol)))
(push (flymake-make-diagnostic
(current-buffer) beg end :note
(buffer-substring-no-properties beg end))
diags)))))))
(apply report-fn (nreverse diags)
(and rbeg `(:region (,rbeg . ,rend))))))

;;;###autoload
(defun hl-todo-insert (keyword)
"Insert TODO or similar keyword.
Expand Down

0 comments on commit baba5ef

Please sign in to comment.