Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typewriter sounds for emacs

Tags:

emacs

elisp

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?

like image 436
Simon Byrne Avatar asked Jun 26 '12 11:06

Simon Byrne


3 Answers

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)))
    
like image 90
Joao Tavora Avatar answered Oct 18 '22 04:10

Joao Tavora


If someone need this using afplay here whats I use

(defun play (audio-name)
    (interactive)
    (let* (buf (get-buffer-create "playnoise"))
        (start-process-shell-command 
         "play" buf (concat (format "afplay /Users/foo/audios/%s" audio-name) ".mp3"))))

(play "wrong")
like image 2
Édipo Féderle Avatar answered Oct 18 '22 05:10

Édipo Féderle


This is quite an old question now, but in case somebody stumbles into this, there's selectric-mode for emacs in MELPA now.

like image 1
wringles Avatar answered Oct 18 '22 03:10

wringles