Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs Lisp - declare functions with a variable/varying name

Tags:

emacs

I'm working on an Emacs Lisp package and one particular feature I would like to add is ability to define functions on the fly - they would follow the same naming convention, but it would help me not having to declare every single one of them manually.

To give an example, I have a basic function called exec, which takes an argument that is the name of executable to launch:

(def exec (cmd)
    (async-shell-command cmd "buffer"))

At the same time, in this particular case, I know the list of the executables that I will want to use - or more precisely, I know how to get a list of them, as it can change over time. So what I would like to do, given the following list of executables:

("a" "b" "c")

is to iterate over them and for each one to create a function with a name exec-[executable] - exec-a, exec-b, exec-c.

Unfortunately, defun does not evaluate the NAME argument so I cannot create the function name dynamically.

PS. The exec command is good enough in itself - it uses completing-read with the list of executables supplied, but I thought the above would be nice addition.

like image 809
ppb Avatar asked Oct 03 '12 17:10

ppb


1 Answers

How 'bout

(dolist (name name-list)
  (defalias (intern (concat "exec-" name))
   `(lambda () ,(format "Run %s via `exec'." name) (interactive) (exec ,name))))
like image 71
Stefan Avatar answered Nov 18 '22 22:11

Stefan