The Trick with Perl6 and Linux: Staying Composed

5 minute read

During Andrew Shitov’s talk about Perl6 one-liners at the recent German Perl Workshop in Munich I followed along on the Perl6 REPL trying out the examples as he presented them. However, I got stuck on his example of calculating the area of a circle. Specifically, I couldn’t enter the Greek lower-case letter pi (π) directly on my keyboard.

One of the many beauties of Perl6 is that one can write mathematical expressions in code almost exactly like one would write them by hand on paper. And this was the point of Andrew’s one-liner: there is something elegant and wonderful about

say π×$r²

because that’s readable and executable code1.

My locale is UTF-8-based (en_NZ.UTF-8; just because I can) so theoretically I should be able to enter and display UTF-8 characters such as a Greek lower-case pi. Where to turn to first?

vim to the rescue?

Well, I know that vim has a (very long) list of special characters, which, if one knows the special combination of two standard keyboard characters (i.e. digraphs), it’s as simple as <Ctrl>-k <digraph> in insert mode to insert the required character. For instance, lower-case pi is <Ctrl>-k p*. To see the list of possible characters, one can use the :digraphs command in command mode. Often, the vim combination is the same as what one would use with the compose key in, say, the shell.

An initial composition

The compose key? What’s that he’s talking about? Well, in boring languages such as English, there’s no need for any special characters (except maybe in the word naïve, but I digress…), so the ASCII character set is completely sufficient. It’s only in “fancy foreign languages” (ahem) where you need accents. The thing is, most of the people on the planet speak (and write) in fancy foreign languages, and for them the ASCII character set isn’t sufficient. Anyways, in order to create (a.k.a. compose) special characters, some bright people came up with the idea of a compose key with which one could enter interesting characters while still using a boring keyboard (anyone remember the “Compose” key on Sun keyboards?).

While some systems back in the 80’s and 90’s still had keyboards with an explicit “Compose” key, unfortunately most modern systems don’t. They have a Caps-Lock key instead, which, everyone uses all the time, right?

Mapping mods in X

It turns out that Linux (well, X-Windows actually), already has a default set of compose key mappings right out of the box. The only thing one needs to do is remap one of the keys to be “compose” instead of something else. In my case I remapped Caps-Lock to Compose. This was a simple change in my ~/.xmodmap file:

keycode 66 = Multi_key
clear Lock

How did I know that the keycode to edit was number 66? I used xev and pressed the Caps-Lock key and that’s what xev said its keycode was. Note that we have to clear out the Lock functionality so that we don’t get Compose and Caps-Lock at the same time, which would get confusing.

And how did I get the ~/.xmodmap file in the first place? Via the xmodmap command:

xmodmap -pke > ~/.xmodmap

Sweet, so we’ve mapped Caps-Lock to Compose and we know that p* is the digraph-sort-of-like-compose-sequence in vim. Let’s try that.

No such luck.

Urm, what about *p, someone on “the internet” mentioned that.

Also no such luck.

Hrm.

It turns out that there aren’t any compose key combinations to create Greek characters when using the default out-of-the-box compose key mappings.

Surely, there must be a way!2

xcompose to the rescue!

It turns out that there is a way, and it’s as simple as creating an .XCompose file in your home directory (see also man XCompose). As is often the case, someone else has already had this problem, solved it and has published the solution online. In this instance the solution comes from the xcompose project on GitHub. This project defines an .XCompose file which has compose combinations you didn’t even know you needed! Fortunately, it has the combination that we need right now, namely one that will produce π.

The installation is easy, first, clone the project

git clone git@github.com:kragen/xcompose.git

and then run the install script in the project root directory.

cd xcompose
./install

This script simply sets a symlink from the project’s dotXCompose file to .XCompose in your home directory.

The new compose key sequences will be active in either the next X11 application you start, or will be active everywhere after closing and restarting your X windows session. In my case I only had to start a new alacritty terminal for the new bindings to be active, and voilà!3 <compose> * p produces π as we wanted. \o/

Only the beginning of a composition is hard

It turns out that we’ve solved the hard problem already; the remainder of the expression can be entered with the standard compose key bindings:

  • the multiplication sign is <compose> x x: i.e. × (yes, that’s an actual operator in Perl6, cool eh?).
  • the power of two on the $r variable is <compose> ^ 2: i.e. $r² (yes, that’s not just a pretty squiggle next to $r, that actually performs the power of two operation).

Thus, to produce

say π×$r²

one has to enter these keys:

s a y <space> <compose> * p <compose> x x $ r <compose> ^ 2

Sure, I could have just used the Texas versions of the constant and operators

say pi * $r**2

but where would be the fun in that? ☻4

You have reached your destination

Finally, we can try out the expression in Perl6. If you don’t have perl6 installed, you just need to run sudo apt install perl6.

perl6 -e 'my $r = 5; say π×$r²'
78.5398163397448

Yay, it works! I love it when a plan comes together.

  1. BTW: π a.k.a. pi is the mathematical constant 3.14159… in Perl6. 

  2. yes there is, and don’t call me Shirley. 

  3. that’s v o i l <compose> <backtick> a for those following along at home. 

  4. just in case you’re wondering: that’s <compose> ( :

Support

If you liked this post and want to see more like this, please buy me a coffee!

buy me a coffee logo