all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Random832 <random832@fastmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Execute a string as a command
Date: Thu, 05 Nov 2015 23:40:23 -0500	[thread overview]
Message-ID: <87611fg2q0.fsf@fastmail.com> (raw)
In-Reply-To: 20151106032357.GF3301@mail.akwebsoft.com

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.




  parent reply	other threads:[~2015-11-06  4:40 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-06  3:23 Execute a string as a command Tim Johnson
2015-11-06  3:53 ` Emanuel Berg
2015-11-06  4:06   ` Tim Johnson
2015-11-06 21:47     ` Emanuel Berg
2015-11-07  1:32       ` Tim Johnson
2015-11-07  3:57         ` Emanuel Berg
     [not found]       ` <mailman.1860.1446859974.7904.help-gnu-emacs@gnu.org>
2015-11-07 18:32         ` Barry Margolin
2015-11-07 19:36           ` Tim Johnson
2015-11-06  4:40 ` Random832 [this message]
2015-11-06  5:52   ` Tim Johnson
     [not found]   ` <mailman.1818.1446789151.7904.help-gnu-emacs@gnu.org>
2015-11-06  9:43     ` Joost Kremers
2015-11-06 16:17       ` Tim Johnson
2015-11-06 16:49         ` Tim Johnson
     [not found] <mailman.1811.1446780244.7904.help-gnu-emacs@gnu.org>
2015-11-06  3:36 ` Barry Margolin
2015-11-06  3:59   ` Tim Johnson
2015-11-06  3:59   ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87611fg2q0.fsf@fastmail.com \
    --to=random832@fastmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.