Mastering Emacs!

2022-12-11 16:46
tags:
emacs

Well, it has finally happened. I've decided I'm actually going to try and learn vanilla emacs, and learn it properly. My employer kindly gives us a budget to spend on learning materials and I have just purchased Mastering Emacs.

So, what brought me to this point (other than looking for a way to spend part of my education stipend)?

I've been using Doom emacs for probably... 5 years now! It's kind of wild, when I think about it. I found a PR I made adding some updates to Clojure mode way back in 2017. At the time, I basically had enough knowledge of elisp to be dangerous (as they say)... and to be honest, that is still pretty much the case.

Doom has done all the heavy lifting for me, save for a few snippets of elisp I've found in other places that I've copied and pasted. I don't think I'll move away from Doom now that I'm going to intentionally try and understand emacs sans-framework. For the most part, I'd like to be a bit more debugging when things break, or customizing things.

How about an example: currently at work I'm in emacs writing elixir most of the day. Now, elixir+liveview is similar to rjsx in the sense that it has embedded html inside source elixir files. The problem with that is that when emacs encounters anything between the H sigil, it has trouble highlighting the html syntax:

def render(assigns) do
  ~H"""
    <div> <%= @stuff %> </div>
  """
end

In order to get the above text highlighting properly, I googled around and pilfered this code:

(use-package!
   polymode
  :ensure t
  :mode ("\\.ex\\'" . poly-elixir-web-mode)
  :init (setq web-mode-engines-alist '(("elixir" . "\\.ex\\'")))
  :config
  (define-hostmode poly-elixir-hostmode :mode 'elixir-mode)
  (define-innermode poly-surface-expr-elixir-innermode
    :mode 'web-mode
    :head-matcher (rx line-start (* space) "~H" (= 3 (char "\"'")) line-end)
    :tail-matcher (rx line-start (* space) (= 3 (char "\"'")) line-end)
    :head-mode 'host
    :tail-mode 'host
    :allow-nested nil
    :keep-in-mode 'host
    :fallback-mode 'host)
  (define-polymode poly-elixir-web-mode
    :hostmode 'poly-elixir-hostmode
    :innermodes '(poly-surface-expr-elixir-innermode)))

The above does some magic that handles the highlighting of heex expressions within .ex files. If I stop and read it, I guess at what it's doing (although to be honest, I just copied and pasted and went about my happy day).

However!

The problem is that for some reason when I save my file, emacs will sometimes ... delete everything between the triple quotes of the ~H sigil. It's pretty annoying.

So, what am I doing about it? Nothing really. If I notice it, I undo the deletion. If I don't, the compiler fails and tells me something's wrong. I don't even know where to debug this problem 1


These kinds of things are what I'm hoping to avoid in the future. But I'm also hoping that Mastering Emacs will help really kickstart my journey into the real depths of emacs, so that I can explore things like:

  • being able to navigate in vanilla emacs (so that I can see if I at least like it! ... and navigate my config when it's broken and doesn't load)
  • customize the UI and style of emacs to fit my tastes 2
  • more confidently hack together packages for my needs
  • write more powerful macros/scripts that are beyond, say, simple find and replace.

Thanks for reading and wish me luck!

Footnotes

1

I'm optimistic that treesitter will solve this problem for me once it's setup in the latest emacs + has an elixir syntax available for it. But still—my point stands. 2: I would also love to get Nicolas Rougier's emacs theme working