Extending ERC with Emacs Lisp

ERC is an IRC client written in Emacs Lisp. This makes ERC very easy to extend, customize and otherwise adapt to your personal style.

A nice features of the standard ERC distribution is that you can extend the set of commands available on your IRC prompt by writing short Emacs Lisp functions. When you define a function called “erc-cmd-XXX” it instantly becomes available as an IRC command.

One of the first custom commands that I wrote using this feature of ERC was a “/WII” command. The “/whois” command is one of the commands that I use a lot, so writing a shorter command is nice. The Emacs Lisp code that adds this command to ERC is quite small too:

(defun erc-cmd-WII (nick &rest ignore)
  "`/WHOIS' command with extra user information."
  (erc-send-command (mapconcat #'identity
                       (list "WHOIS" nick nick) " ")))

After testing this and saving it to my ERC startup code, I added another one. An “/identify” command that sends my password to NickServ:

(defun erc-cmd-IDENTIFY (password &rest ignore)
  "Short-hand alias for `/msg NickServ identify PASS'."
  (erc-send-command (mapconcat #'identity
                       (list "identify" password) " ")))

The next few commands were a tiny bit more “complex” Emacs Lisp code, because they should accept a variable number of arguments. They are my short-hand aliases “/CS“, “/MS“, and “/NS“, for the “/CHANSERV“, “/MEMOSERV“, and “/NICKSERV” service-related commands:

(defun erc-cmd-CS (&rest args)
  "Short alias for `/chanserv ARGS'."
  (let ((command-args (append (list "CHANSERV") args)))
    (let ((chanserv-command (mapconcat #'identity command-args " ")))
      (erc-send-command chanserv-command))))

(defun erc-cmd-MS (&rest args)
  "Short alias for `/memoserv ARGS'."
  (let ((command-args (append (list "MEMOSERV") args)))
    (let ((memoserv-command (mapconcat #'identity command-args " ")))
      (erc-send-command memoserv-command))))

(defun erc-cmd-NS (&rest args)
  "Short alias for `/nickserv ARGS'."
  (let ((command-args (append (list "NICKSERV") args)))
    (let ((nickserv-command (mapconcat #'identity command-args " ")))
      (erc-send-command nickserv-command))))

Typing these new commands in a *scratch* buffer and evaluating them while ERC is running in another buffer of the same session makes them immediately accessible as IRC commands. Testing variations of these commands is much much easier when you can develop, test and save the commands right there, within a single Emacs session :-)

About these ads

One thought on “Extending ERC with Emacs Lisp

  1. Chris

    Wow, I never knew that you could do that, but it really shouldn’t surprise me.

Comments are closed.