Using Unicode in Emacs for Perl 6
I use vi/vim
for quick edits and remote work, but I do most of my programming (and other work) in emacs
. To enter Unicode characters in emacs, you run the "insert-char
" command, which by default is tied to "C-x 8 [Enter]
", then type in the hex code for the character or its name.
Typing at least 5 characters to get one got old very fast, now that there are some Unicode characters that can be used as Perl 6 operators. So I wrote a lisp function which asks for a single character and looks it up in an alist (kinda similar to a Perl hash). That way I can enter any Unicode character I've put in the alist by hitting two keys: one key to run the function, then whatever key I assigned to that character. Here's the lisp, which I put in my .emacs file:
(defun ajb/common-unicode-chars (ch)
"Function to map single keys to some common unicode characters.
Run the function and then press a key. Non-mapped keys will default to themselves."
(interactive "cChar: ")
(insert
(or (cdr
(assoc-string
(char-to-string ch)
'(("[" . ?«)
("]" . ?»)
("a" . ?Α)
("d" . ?Δ)
("g" . ?Γ)
("o" . ?Ω)
("p" . ?π)
(">" . ?⊃)
("<" . ?⊂)
("|" . ?∪)
("&" . ?∩)
("1" . ?⚀)
("6" . ?⚅)))) ch)))
(global-set-key (kbd "<f6>") 'ajb/common-unicode-chars)
The first two characters in the list are guillemets, which Perl 6 uses as the "hyperoperators" (more on those another time), and they can also be used as double-quotes for strings. I keyed them to the square brackets, since they don't require the shift key. Then I put in a few Greek letters I might want to use, a few of the Set operators, and a couple of dice faces, just to show some possibilities. As I discover more Unicode characters that I want to use more than once in a great while, I'll pick a key for them and add them to the list.
If you put this in your .emacs
, it will be bound to the <f6>
key in all modes. So to get the left-pointing guillemet, you'd hit "<f6> [
". If you know lisp, the code is pretty simple. If you don't, I think it's fairly obvious how to add more entries, but please leave a comment if you have any questions.
Thanks to all the Unicode characters that are available, along with some other new features like "pointy blocks," you can write Perl 6 code that looks pretty strange to a Perl 5 guy, like this:
#!/usr/bin/env perl6
use v6;
my &Α = -> {
Set.new('a'..'n');
}
my &Ω = -> {
Set.new('m'..'z');
}
say [∩] Α, Ω;
I'll save explaining what that does for next time.
If you use X, there is a better solution: the Compose key. Since it is a basic X feature, it works in any place where you type text, not just Emacs. You get a huge number of predefined shortcuts, so you do not need to write a configuration yourself. The shortcuts are also more mnemonic. See https://superuser.com/a/78724 for instructions. Additional operators added from http://doc.perl6.org/routine.html
Compose, <, < → «
Compose, >, > → »
Compose, *, A → Α
Compose, *, D → Δ
Compose, *, G → Γ
Compose, *, V → Ω
Compose, *, p → π
Compose, {, ) → ⊃
Compose, {, ( → ⊂
Compose, {, U → ∪
Compose, {, ^ → ∩
Compose, [, 1, ] → ⚀
Compose, [, 6, ] → ⚅
Compose, i, n → ∈
Compose, !, i, n → ∉
Compose, n, i → ∋
Compose, !, n, i → ∌
Compose, !, {, ( → ⊄
Compose, !, {, ) → ⊅
Compose, {, =, ( → ⊆
Compose, {, =, ) → ⊇
Compose, (, -, ) → ⊖
Very nice, thanks. I just mapped my caps-lock key to be the Compose key with:
setxkbmap -option compose:caps
and also put it in the keyboard section of my xorg.conf for future runs of X:
Option "XkbOptions" "compose:caps"