Skip to content

Instantly share code, notes, and snippets.

@pcn
Forked from tbonza2/gist:20757551443a61254aa6
Last active January 13, 2021 15:35
Show Gist options
  • Save pcn/d4a9cd1ea42e6e02f5e1 to your computer and use it in GitHub Desktop.
Save pcn/d4a9cd1ea42e6e02f5e1 to your computer and use it in GitHub Desktop.
Saltstack mode for emacs

Saltstack mode for emacs

Earlier today I was looking for a Saltstack mode for emacs. Couldn't find one, so I posted the question on Stack overflow. Somebody had a good suggestion about using yaml-mode because a Sublime Text implementation appeared to be taking this approach.Another person thought that Saltstack's use of jinja templates would make an emacs mode easy enough to configure. These were both helpful ideas so I wanted to implement parts of each.

Reasoning

After looking around, I found that someone had configured an emacs mode for a similar situation. They needed to combine HTML and Python to use Mako templates for a project not involving Saltstack. Since Edx uses Mako templates instead of jinja in their Django site, Mako seemed like a good alternative to Jinja templates.

Since someone previously combined HTML and Python modes it seemed reasonable to combine Mako and Yaml modes in Emacs.

Setup

Multiple Major (MMM) Mode allows Emacs major modes to be combined. MMM-Mako Mode provides support for Mako files using MMM. Configuring the setup can be a pain so I wanted to document it:

  1. Download & untar MMM mode from http://sourceforge.net/projects/mmm-mode/files

    mv ~/Downloads/mmm-mode-0.5.1.tar.gz ~/InstalledPrograms/
    tar -xvf mmm-mode-0.5.1.tar.gz && cd mmm-mode-0.5.1/
    whereis emacs
  2. Make & install MMM mode but first specify where your Emacs is installed. I'm using Emacs 24 installed in /usr/bin/emacs.

    EMACS="/usr/bin/emacs"
    ./configure
    make
    sudo make install

My elisp files were installed to /usr/local/share/emacs/site-lisp. You may need to add this directory to the Emacs load path. You will also need to tell Emacs to load the module. Here's what I added to my .emacs configuration.

````elisp
(add-to-list
 ;; Adding mmm-mode
 'load-path "/usr/local/share/emacs/site-lisp")
(require 'mmm-auto)
(setq mmm-global-mode 'maybe)
````

Make sure it's installed correctly by opening emacs M-x mmm-mode to enable MMM-mode.

  1. Next up, installing MMM-Mako. If I can install something with the package manager then I'll do it. Let's first make sure the package will be available by adding melpa to our .emacs

    $ emacs -nw ~/.emacs

Placed the following in my .emacs file

```elisp
(require 'package) ;; You might already have this line
(add-to-list 'package-archives
             '("melpa" . "http://melpa.org/packages/") t)
```

Back to emacs M-x package-install, Install package: mmm-mako. Once mmm-mako is installed then just finish configuring your .emacs

```elisp
(add-to-list
 ;; Saltstack config
 'auto-mode-alist '("\\.sls\\'" . yaml-mode))
(mmm-add-mode-ext-class 'html-mode "\\.sls\\'" 'mako)
```

My full .emacs file can be viewed here. Also, I want to say thank you to Zach for putting together the post about Mako/HTML.

This approach is a work in progress and I'll update as I find bugs/improvements.

@glynnforrest
Copy link

For anyone reading, there is now a dedicated Salt major mode under active development 💥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment