This post is about the niche intersection of Emacs and Magic: the Gathering.

I considered not writing this because I figured, surely if you multiply the proportion of people who play Magic by the proportion of people who use Emacs, you get a very small number. But then I thought, those two variables are probably not independent. And the intersection of Magic players x Emacs users x people who read my blog might actually be greater than zero. So if you’re out there, this post is for you.

Do you like how MTG websites like magic.gg and mtg.wiki let you mouse over a card name to see a picture of the card? Well, I wrote an Emacs extension that replicates that functionality.

Here is the code: https://github.com/michaeldickens/emacs-mtg

The README on GitHub pretty much explains how it works, so the rest of this post is just gonna repeat what it says in the README.

Usage

(add-to-list 'load-path /path/to/mtg.el)
(require 'mtg)

This module allows you to refer to Magic cards in Org Mode using a new type of link prefixed with mtg:. For example:

[[mtg:Black Lotus]] might be the strongest card in my collection, but my personal favorite is [[mtg:Grizzly Bears]].

When Org Mode sees a link to an MTG card, it will do the following:

  1. If the card is not downloaded yet, download the card by querying the Scryfall API for a card with the given name.
  2. When you open the link (using org-open-at-point or C-c C-o), Emacs displays an image of the card in the minibuffer.

Here’s how it looks:

By default, card images and data are downloaded to ~/.emacs.d/mtg-cards/, but you can change this by customizing the variable mtg/db-path.

Scryfall’s API has fuzzy name matching, so for example [[mtg:blac lotus]] will display Black Lotus.

Card legality

Cards are displayed with a red tint if they are illegal in the preferred format. It looks like this:

When checking legality, this module uses Standard format by default, but you can customize it by setting the variable mtg/default-format. You can also set file-local or heading-local formats in Org Mode using the :MTG_FORMAT: property. For example:

:PROPERTIES:
:MTG_FORMAT: standard
:END:
If you open this link --> [[mtg:Black Lotus]], the card will appear
with a red tint because it's illegal in Standard.

** My vintage cards
  :PROPERTIES:
  :MTG_FORMAT: vintage
  :END:
  [[mtg:Black Lotus]] is legal in Vintage, so here it will
  appear with no tint.

Note: Adding a red tint requires ImageMagick. If you don’t have ImageMagick installed, all cards will be displayed as if they’re legal.

Exporting to HTML

If you export Org Mode files to HTML, you can make the MTG card links display images on hover. For this to work, you must include some custom CSS in your Org Mode file.

On GitHub there is a file called export-style.setup that includes some custom CSS. To include this custom CSS in Org Mode, put this line at the top of your Org Mode file:

#+SETUPFILE: /path/to/export-style.setup

Then call org-export-dispatch to export the Org file to HTML.

Table utilities

mtg.el comes with functions for working with Org Mode tables. The functions assume you have a table where one column contains links to MTG cards, like this:

[[mtg:Black Lotus]]
[[mtg:Grizzly Bears]]
[[mtg:Colossal Dreadmaw]]

mtg/table-sort-by-property takes a property as a string (such as “name”, “rarity”, or “color”) and sorts the table by looking up that property for each card. This only works if you’ve already downloaded the card info (which happens when you view the card or export the whole file).

mtg/table-insert-column takes a property as a string and inserts a new column containing that property for each card. For example, calling (mtg/table-insert-column "rarity") on the table above produces this:

[[mtg:Black Lotus]] bonus
[[mtg:Grizzly Bears]] common
[[mtg:Colossal Dreadmaw]] common

If a property is missing, the cell will be left blank. For example, calling (mtg/table-insert-column "power") produces

[[mtg:Black Lotus]]  
[[mtg:Grizzly Bears]] 2
[[mtg:Colossal Dreadmaw]] 6

You can also call mtg/get-property to return a property for the card at point.

Posted on