Refactoring
to
maintainability

Changes on Emacs

2021-04-26
Tooling Emacs

The setup

As I have started working on my new job, I got given a Mac laptop. So after four years, a Linux system is not my main development environment (some pfffffffft here). I am still using GNU Emacs, as Aquamacs seems to be quite behind, but maybe I should give it a look.

With the new setup I also have a new external monitor for it, though, unlike the one that I was using as primary on my desktpop, is “only” 24” 1920x1080

The shell change

So it seems macosx has a very weird behaviour. Zsh is the default shell (which is the one I use, anyway). But for some reason, when starting Emacs in GUI mode it uses sh. Which has the annoying effect of not setting up my path or other environment variables.

There is an easy solution for this:

(if (memq window-system '(mac ns))
    (setenv "SHELL" "/bin/zsh"))

But!!! a wrinkle. For some reason, if I shutdown the mac with Emacs open and then start it again, it fails to execute the code above. Not sure why yet, I have to investigate a bit further. For now, I have to close Emacs before I shutdown the mac and then re-open it again.

The font change

I have written in a previous post about some code to quickly switch fonts on Emacs for when I am sharing my screen. My default font size is 10. But that font size is not very nice on my new monitor, and the “sharing” font is too big for normal use. So I decided to finally setup a bit of code that I mused then about adding.

(defun set-size-font (size)
  (set-face-attribute 'default nil :font (concat "Fira Code-" (number-to-string size))))

(defun switch-font (universal)
  "Switches the font between my normal one and the one used to share screen"
  (interactive "P")
  (if (equal universal nil)
      (progn
        (if using-sharing-font
            (set-standard-font)
          (set-sharing-font))
        (setq using-sharing-font (not using-sharing-font)))
    (progn
      (set-size-font universal)
      (setq using-sharing-font t))))

(interactive "P") is a special command that allows to pass whatever is typed as universal argument (C-u) as a parameter to the function.

So, if, for example, I use the keybindings C-u 13 M-x switch-font then 13 will be the parameter passed into switch-font.

When you don’t use the universal argument then nil is passed. And with that, if I have Emacs on the external monitor, I can switch to a font size of 12.

Refactoring

There are a couple of ideas for refactoring the above code.

First, we can use the universal argument without passing a value (which for historical reasons, I expect, gives you a value of '(4)). This would allow me to completely skip the boolean state that I am using, making the code a bit simpler and idempotent (this is good).

Second, now I have three functions that deal with the resizing of my font (set-standard-font, set-sharing-font and set-size-font). It seems to me that the right thing will be use the last one, and call that where the other two are called, passing the corresponding size (probably in declared variable).


Return to Index

Unless otherwise stated, all posts are my own and not associated with any other particular person or company.

For all code - MIT license
All other text - Creative Commons Attribution-ShareAlike 4.0 International License
Creative Commons BY-SA license image