How do you get compilation mode to properly detect and underline the file:line pattern of a given tool, to be able to click to directly get to them?
1 Answer
After trying various examples around the web that simply didn't work (with my emacs 29.1), here's what ended up working.
To add a tool (ansible-lint
), I added this to my .emacs
:
;;; ansible-lint
; Compile regex for ansible-lint
(require 'compile)
(add-to-list 'compilation-error-regexp-alist
'yaml)
(add-to-list 'compilation-error-regexp-alist-alist
'(yaml "^\\(.*?\\):\\([0-9]+\\)" 1 2)
)
where "^\\(.*?\\):\\([0-9]+\\)"
is my custom regex (for more details on the format, see here).
To complete the description, I also added this to call ansible-lint
when running compile from yaml-mode
:
; Replace make -k with ansible-lint, with an UTF-8 locale to avoid crashes
(defun ansible-lint-errors ()
(make-local-variable 'compile-command)
(let ((ansiblelint_command "ansible-lint ") (loc "LANG=C.UTF-8 "))
(setq compile-command (concat loc ansiblelint_command buffer-file-name)))
)
(add-hook 'yaml-mode-hook 'ansible-lint-errors)