Set Up Michelson Mode in Emacs

3 minute read

Michelson Mode
Viewing the stack in Michelson Mode

Note: This guide assumes that you’ve followed the instructions in the previous guide and have a working tezbook.

A quick shoutout to klakplok and MiloDavis for making the Michelson mode happen in the first place.

According to the instructions, you can use the mode with both a locally compiled Tezos Client, or with the Alphanet through Docker. I found it easiest to use the Alphanet.

Install Docker

We’ll be following these instructions for installing docker. Here are the commands:

Update the apt package index

sudo apt-get update

Install packages to allow apt to use a repository over HTTPS

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

Add Docker’s official GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Check that the key fingerprint matches (see above link for value and exact command)

sudo apt-key fingerprint 0EBFCD88

Set up and install the stable repo

sudo add-apt-repository    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
sudo apt-get update
sudo apt-get install docker-ce

Check that it installed correctly

sudo docker run hello-world

If preferred, you can give your user access to docker instead of having to use root. Warning: this grants privileges equivalent to the root user. Log out and log back in if you do this.

sudo groupadd docker
sudo usermod -aG docker $USER
docker run hello-world

Install the Alphanet

Full instructions here but to run the michelson mode, you just need to:

Download the script

cd ~/tezos

(If you don’t have a tezos folder, you’ll need to create it with mkdir, but you should have it already from git cloning the tezos repo in the previous guide.)

wget https://raw.githubusercontent.com/tezos/tezos/alphanet/scripts/alphanet.sh
chmod +x alphanet.sh

Start up the docker container and run the node: ./alphanet.sh start

Set Up Emacs

This is going to seem painfully slow to people familiar with emacs. As a pycharm/webstorm/sublime text user, I’m an emacs noob, so if you are too, this is the guide for you. As an aside, the emacs tutorial on the opening page is actually quite helpful.

Note: You’re going to see references to emacs commands that look like C-[character] or M-[character]. C is the ctrl key on your tezbook. M is the alt key on your tezbook. You should hold the ctrl or alt key and then press whatever character the command uses. For example, to exit, press ctrl, then the letter x, then ctrl and the letter c (C-x, C-c).

The Emacs Initialization File

We’re going to be adding a bunch of stuff to the emacs initialization file.

Open up emacs, then open up the .emacs file with your newly-found shortcut skills: c-x c-f ~/.emacs

We need to install the deferred package and will be following the instructions here

Add the following to your .emacs file: (Note that you only need (require 'cl) if you are using emacs 24.)

(require 'cl)
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
                    (not (gnutls-available-p))))
       (url (concat (if no-ssl "http" "https") "://melpa.org/packages/")))
  (add-to-list 'package-archives (cons "melpa" url) t))
(when (< emacs-major-version 24)
  ;; For important compatibility libraries like cl-lib
  (add-to-list 'package-archives '("gnu" . "https://elpa.gnu.org/packages/")))
(package-initialize)

Paste in .emacs file with the yank command C-y

Save with C-x C-s and restart

Then just use M-x package-list-packages to browse and install deferred

Type c-S to search for ‘deferred’

Click on the link, hit the “Install” button in the new buffer

Hit yes

Open .emacs again, and yank (paste) the following (check that the file paths match your files):

(setq michelson-client-command "~/tezos/alphanet.sh client")
(setq michelson-alphanet t)

Note: your whole .emacs file should look like this now:

(require 'cl)

(put 'upcase-region 'disabled nil)

(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
                    (not (gnutls-available-p))))
       (url (concat (if no-ssl "http" "https") "://melpa.org/packages/")))
  (add-to-list 'package-archives (cons "melpa" url) t))
(when (< emacs-major-version 24)
  ;; For important compatibility libraries like cl-lib
  (add-to-list 'package-archives '("gnu" . "https://elpa.gnu.org/packages/")))
(package-initialize)

(load "~/tezos/emacs/michelson-mode.el" nil t)

(setq michelson-client-command "~/tezos/alphanet.sh client")
(setq michelson-alphanet t)

Save it and restart

Open up a smart contract, like ~/tezos/test/contracts/hardlimit.tz

Put your mouse over one of the commands, and view the stack!