emacs inventory-generic-mode

102 views
Skip to first unread message

Tim Chambers

unread,
Jun 8, 2016, 3:53:26 PM6/8/16
to Ansible Development
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)

Ted Zlatanov

unread,
Jun 9, 2016, 8:48:34 AM6/9/16
to ansibl...@googlegroups.com
On Wed, 8 Jun 2016 12:53:26 -0700 (PDT) Tim Chambers <tbcha...@gmail.com> wrote:

TC> I got inspired by ini-generic-mode today.

(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

Tim Chambers

unread,
Jun 9, 2016, 1:29:20 PM6/9/16
to Ansible Development
I wrote to Ted via email. I'll update this thread with progress.

Tim Chambers

unread,
Jun 14, 2016, 3:45:36 PM6/14/16
to Ansible Development

Tim Chambers

unread,
Jun 15, 2016, 1:14:22 PM6/15/16
to Ansible Development
Thanks for commit 299f194, Ted!

Ted Zlatanov

unread,
Jun 16, 2016, 9:40:30 AM6/16/16
to ansibl...@googlegroups.com
On Wed, 15 Jun 2016 10:14:22 -0700 (PDT) Tim Chambers <tbcha...@gmail.com> wrote:

TC> Thanks for commit 299f194
TC> <http://lists.gnu.org/archive/html/emacs-diffs/2016-06/msg00115.html>, 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

Peter Oliver

unread,
Jun 16, 2016, 2:28:33 PM6/16/16
to Ansible Development
On 16 June 2016 at 14:40, Ted Zlatanov <t...@lifelogs.com> wrote:

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.

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

--
Peter Oliver

Ted Zlatanov

unread,
Jun 17, 2016, 8:26:22 AM6/17/16
to ansibl...@googlegroups.com
On Thu, 16 Jun 2016 19:28:11 +0100 Peter Oliver <ansibl...@mavit.org.uk> wrote:

PO> On 16 June 2016 at 14:40, Ted Zlatanov <t...@lifelogs.com> wrote:
>>
>> 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.

PO> I have an Ansible mode for Emacs that uses
PO> https://github.com/vspinu/polymode to highlight the YAML parts of a
PO> playbook as YAML, and the Jinja2 parts as Jinja2. However, because of
PO> https://github.com/vspinu/polymode/issues/75 it's barely usable.

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

Peter Oliver

unread,
Jun 30, 2016, 3:43:07 PM6/30/16
to Ansible Development
On 17 June 2016 at 13:26, Ted Zlatanov <t...@lifelogs.com> wrote:
On Thu, 16 Jun 2016 19:28:11 +0100 Peter Oliver <ansibl...@mavit.org.uk> wrote:

PO> I have an Ansible mode for Emacs that uses
PO> https://github.com/vspinu/polymode to highlight the YAML parts of a
PO> playbook as YAML, and the Jinja2 parts as Jinja2.  However, because of
PO> https://github.com/vspinu/polymode/issues/75 it's barely usable.

Could you post a link to your mode and some examples of the problem for
Ansible specifically?

Reply all
Reply to author
Forward
0 new messages