I got inspired by ini-generic-mode today.
(define-generic-mode inventory-generic-mode
'(?#)
nil
'((“^\(\[.\]\)" 1 font-lock-constant-face)
("^\([^=\n\r]\)=\([^\n\r]\)$"
(1 font-lock-function-name-face)
(2 font-lock-variable-name-face)))
'(“inventory”)
(list
(function
(lambda ()
(setq imenu-generic-expression
'((nil "^\[\(.\)\]” 1)
(“Variables” “^\s-\([^=]+\)\s-=” 1))))))
“Generic mode for Ansible inventory files.”)
(add-to-list 'generic-extras-enable-list 'inventory-generic-mode t)
(define-generic-mode inventory-generic-mode
... (add-to-list 'generic-extras-enable-list 'inventory-generic-mode t)
Would you be interested in contributing that to Emacs, in generic-x.el?
If you're not, would it be OK if I reimplemented your example and added
it?
There are a lot more neat customizations that can be done for Ansible
inventory files specifically, besides the generic INI stuff:
* syntax highlighting of host name, port, and vars separately
* show expansions of numeric and alphabetic ranges in an unobtrusive way (e.g. eldoc mode)
* syntax check with Flycheck/Flymake
Thanks
Ted
I wrote to Ted via email. I’ll update this thread with progress.
Thanks for commit 299f194, Ted!
For anyone interested, here's how you could add this mode for your
particular inventory files in a way that fails gracefully on older
Emacsen without the new ansible-inventory-generic-mode:
(when (fboundp 'ansible-inventory-generic-mode)
(setq auto-mode-alist
(append '(("ansible/hosts\\'" . ansible-inventory-generic-mode)
)
auto-mode-alist)))
This feature will be in 25.2 but not the upcoming 25.1, which is in a
feature freeze.
If anyone has suggestions on improving the syntax highlighting or
syntax-checking for Ansible inventory files or Ansible playbooks, please
let us know. I'll be glad to help write the Emacs Lisp needed and get
the code comitted.
Thanks
Ted
I have an Ansible mode for Emacs that uses
https://github.com/vspinu/polymode to highlight the YAML parts of a
playbook as YAML, and the Jinja2 parts as Jinja2. However, because of
https://github.com/vspinu/polymode/issues/75 it's barely usable.
I suspect this is fundamental to the way polymode works. Maybe something
could be done with one of the other mode-in-mode modes (
https://emacswiki.org/emacs/MultipleModes).
Could you post a link to your mode and some examples of the problem for
Ansible specifically? And are you interested in contributing it to
Emacs, either in the core or in GNU ELPA or another package repository?
The specific issue could be a simple parser adjustment, or it could
require switching to SMIE. There's also been discussion of more thorough
support of multiple modes at the C level in Emacs, but that doesn't help
us right now. I think there may be some other ways to help, though.
Ted