# emacs inventory-generic-mode

**URL:** <https://forum.ansible.com/t/emacs-inventory-generic-mode/21702>\
**Category:** Ansible Developer\
**Created:** [June 8, 2016, 7:53pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702 "2016-06-08T19:53:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Chambers](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tim\_Chambers](https://forum.ansible.com/u/Tim_Chambers)\
**Post date:** [June 8, 2016, 7:53pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702/1 "2016-06-08T19:53:26Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Ted\_Zlatanov](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@Ted\_Zlatanov](https://forum.ansible.com/u/Ted_Zlatanov)\
**Post date:** [June 9, 2016, 12:47pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702/2 "2016-06-09T12:47:59Z")

</div>

(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

---

<div class="post-metadata">

**Author:** ![Tim\_Chambers](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tim\_Chambers](https://forum.ansible.com/u/Tim_Chambers)\
**Post date:** [June 9, 2016, 5:29pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702/3 "2016-06-09T17:29:19Z")

</div>

I wrote to Ted via email. I’ll update this thread with progress.

---

<div class="post-metadata">

**Author:** ![Tim\_Chambers](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tim\_Chambers](https://forum.ansible.com/u/Tim_Chambers)\
**Post date:** [June 14, 2016, 7:45pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702/4 "2016-06-14T19:45:36Z")

</div>

Follow-up to the [thread on the appropriate GNU Emacs list](http://lists.gnu.org/archive/html/bug-gnu-emacs/2016-06/msg00591.html).

---

<div class="post-metadata">

**Author:** ![Tim\_Chambers](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tim\_Chambers](https://forum.ansible.com/u/Tim_Chambers)\
**Post date:** [June 15, 2016, 5:14pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702/5 "2016-06-15T17:14:22Z")

</div>

Thanks for commit [299f194](http://lists.gnu.org/archive/html/emacs-diffs/2016-06/msg00115.html), Ted!

---

<div class="post-metadata">

**Author:** ![Ted\_Zlatanov](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@Ted\_Zlatanov](https://forum.ansible.com/u/Ted_Zlatanov)\
**Post date:** [June 16, 2016, 1:40pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702/6 "2016-06-16T13:40:17Z")

</div>

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:

&nbsp;&nbsp;&nbsp;&nbsp;(when (fboundp 'ansible-inventory-generic-mode)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(setq auto-mode-alist  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(append '(("ansible/hosts\\'" . ansible-inventory-generic-mode)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Peter\_Oliver](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@Peter\_Oliver](https://forum.ansible.com/u/Peter_Oliver)\
**Post date:** [June 16, 2016, 6:28pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702/7 "2016-06-16T18:28:11Z")

</div>

I have an Ansible mode for Emacs that uses  
[https://github.com/vspinu/polymode](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](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](https://emacswiki.org/emacs/MultipleModes)).

---

<div class="post-metadata">

**Author:** ![Ted\_Zlatanov](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@Ted\_Zlatanov](https://forum.ansible.com/u/Ted_Zlatanov)\
**Post date:** [June 17, 2016, 12:26pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702/8 "2016-06-17T12:26:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Oliver](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@Peter\_Oliver](https://forum.ansible.com/u/Peter_Oliver)\
**Post date:** [June 30, 2016, 7:42pm UTC](https://forum.ansible.com/t/emacs-inventory-generic-mode/21702/9 "2016-06-30T19:42:44Z")

</div>

Please see [https://bitbucket.org/snippets/mavit/jxKzE](https://bitbucket.org/snippets/mavit/jxKzE).
