## Clickable email addresses in emacs

| categories: | tags: | View Comments

There are clickable mailto:jkitchin@andrew.cmu.edu links in org-mode, but standalone email addresses like jkitchin@cmu.edu are just ordinary text. Here, I want to explore using clickable text instead. I will use the button-lock package for this. I borrowed an email regexp from EmacsWiki: Regular Expression for this. The idea is to define a regular expression for an email address, and use it to make the emails clickable.

I am still not sure what the canonical way to get the value of the text that was highlighted. Here we use the :additional-property feature to set a property to t, and then use that property to get the characters that have a non-nil "email-address" property. It seems clunky, but it works. The main action is to compose an email in mu4e (my preferred email program in emacs). You could also put a call to helm here, or to a hydra for other options.

I make the email addresses stand out a little by giving them a gray background, and a tooltip so you can see why they are highlighted. I also bind RET so I don't have to use the mouse. Don't forget you can type C-h . to see the local help instead of mousing over it! Finally, we add a text-mode hook so this will get loaded when we open a text file (or one with a mode derived from text-mode like org-mode).

(defun highlight-email-addresses ()
"Add button to email address. Clicking or RET will open a compose email window."
(button-lock-set-button
"\\w+\$$\\.\\w+\$$?@\$$\\w\\|\\.\$$+"
(lambda ()
(interactive)
(backward-char))
(forward-char)
(setq start (point))
(forward-char))
(setq end (point))
:face '((:background "gray80") (:underline t))
:help-echo "click to send mu4e email"
:keyboard-binding (kbd "RET")