A few days ago I wanted to write to a colleague about accessibility – or, as it is often called, a11y. As you probably know, the 11 comes from the fact that there are 11 letter beteen “a” and “y” in the word “accessibility”. Of course, I didn’t want to count the letters manually – I just marked the word, pressed M-= (count-words-region) and subtracted 2 from the result. Then it occurred to me that it is Emacs who should be doing things like that, not me. Also, I wanted to know how much time I’d need to write a command to make a word into an abbreviation (or a10n
). And here it is, written in about 5 minutes:
(defun make-letter-count-abbreviation ()
"Convert the word before point to an a10n."
(interactive)
(save-excursion
(let* ((begin (progn (backward-word) (point)))
(end (progn (forward-word) (point)))
(count (- end begin 2)))
(delete-region (1+ begin) (1- end))
(backward-char)
(insert (format "%s" count)))))
And that’s pretty much it for today. Happy Elisp hacking!