sábado, 29 de diciembre de 2012

using ido-completing-read to get input from users


Lately, ido is my shortcut to many commands that I used to execute in shell. It's great to have a flexible matching with autocompletion, inside your development environment. The function to call from your elisp code is ido-completing-read which has the following signature:

(ido-completing-read PROMPT CHOICES &optional PREDICATE REQUIRE-MATCH
INITIAL-INPUT HIST DEF INHERIT-INPUT-METHOD)

Let's see an example of using ido to collect a parameter from the user. I have my favourite streaming radios stored in pls or asx files, and this little function allows me to type M-x radio RET to listen any of those.
 

 (defun radio ()
   (interactive)
   (let ((filename
         (ido-completing-read "which radio?: "
                              (directory-files
                               "/home/rgrau/bin/radios/"
                               nil
                               "\\.pls$\\|\\.asx$"))))
     (async-shell-command
      (concat "mplayer -playlist /home/rgrau/bin/radios/" filename) "*mplayer*" )
     (message "choosen: %s" filename)))

Now you can easily port many commands you used to run on terminal to be executed inside your favourite editor.


EDIT: A copypasta fail made this post appear like without body in planet emacsen. sorry :(

No hay comentarios: