Search code examples
emacselisp

Typewriter sounds for emacs


I would like to set up emacs so that it plays a typewriter sounds when typing text into the buffer, as well as a carriage return sound when hitting enter (similar to the Q10 editor on windows). Does anyone have any suggestions for how I might go about this? Is there a hook that I could use?

I currently use aquamacs and emacs 22, but am not averse to upgrading.

EDIT: In case anyone is interested, the vim version of this question was asked here: How can I make VIM play typewriter sound when I write a letter?


Solution

  • First you must establish some way to play sound:

        (defun play-typewriter-sound ()
          (let ((data-directory "~/Dowloads/Sounds"))
            (play-sound `(sound :file "key1.wav"))))
    

    ...doesn't work on Mac OSX Emacs for example since it's not compiled with sound support. There are workarounds though, see for example http://www.emacswiki.org/emacs/ErcSound

    • Then, you can use advice on any Emacsen

      (defadvice self-insert-command (after play-a-sound activate)
        (play-typewriter-sound))
      

      You could also advise newline-and-indent.

    • On Emacs24 you now have post-self-insert-hook

      (add-hook 'post-self-insert-hook 'play-typewriter-sound)
      
    • If you don't like defadvice you can use post-command-hook and check the name of this-command there:

      (add-hook 'post-command-hook #'play-typewriter-sound-maybe)
      
      (defun play-typewriter-sound-maybe ()
        (if (eq this-command 'self-insert-command)
            (play-typewriter-sound)))