2008-12-01

running some external program only if it exists

You can easily run external programs from emacs; one way to do it is by using the shell-command-function. However, as discussed here, if you are using emacs in different environments, you might want to check first if the program is available, and give a warning if it's not there.

One simple helper function you can use in your .emacs for this, could be something like:

(defun djcb-shell-command-maybe (exe &optional paramstr)
  "run executable EXE with PARAMSTR, or warn if EXE's not available; eg. "
  " (djcb-shell-command-maybe \"ls\" \"-l -a\")"
  (if (executable-find exe)
    (shell-command (concat exe " " paramstr))
    (message (concat "'" exe "' not found found; please install"))))
Of course, this function is not very useful by itself, but we'll use it in later entries, and of you can use it in your own scripts.

No comments: