Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function from a string name in (Emacs) Lisp

Tags:

emacs

lisp

elisp

Given a string, built by various concatenation, "my-func-name", I would like to call the associated function.

As funcall expects a function object as parameter, I would like to know if there is a way to retrieve the function reference by its name, so I can execute it.

Hint: I am currently using the Emacs Lisp dialect.

Thank you very much

Bonus: Sample dummy code

(defun my-func-name ()
  "My function."
  (message "Hello"))

(setq mfname "my-func-name")

;; Not working, obviously
;; (funcall mfname)
like image 395
DCLaoij Avatar asked Dec 20 '22 05:12

DCLaoij


1 Answers

Get the symbol of that name with intern, and then funcall it:

(funcall (intern "my-func-name"))
like image 82
legoscia Avatar answered Dec 27 '22 16:12

legoscia