help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Execute a string as a command


From: Random832
Subject: Re: Execute a string as a command
Date: Thu, 05 Nov 2015 23:40:23 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Tim Johnson <tim@akwebsoft.com> writes:

> Example : A string has the value of :
> "toggle-truncate-lines"
>
> (setq string-wants-to-be-a-command "toggle-truncate-lines")
>
> How may I convert 'string-wants-to-be-a-command to a command:
> I.E. => (toggle-truncate-lines) to be used programmatically?
>
> thanks

If you want it to literally be interpreted as a symbol name and
damn the consequences if it's anything else, you can use intern
to get the symbol:

(intern "toggle-truncate-lines")
(intern string-wants-to-be-a-command)
(intern (symbol-value 'string-wants-to-be-a-command))

If it can be a lambda rather than just a symbol, then use
read-from-string instead:

(car (read-from-string "toggle-truncate-lines"))
(car (read-from-string "(lambda () (interactive))"))

But why are you storing a string instead of the command object
(symbol/function/lambda) anyway?

And then you can call it with funcall or call-interactively.

funcall requires you to specify the arguments, so only commands
that don't require an argument will work. To read the arguments
from the keyboard instead, use call-interactively.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]