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: Emanuel Berg
Subject: Re: Execute a string as a command
Date: Fri, 06 Nov 2015 04:53:22 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (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?

(Why do you want this? Is there a better way to
do it?)

Do you mean you want to parenthesize the string? - in
what case this is an editing issue, at least for
uncomplicated commands.

Or do you want to store commands in strings and have
a function execute the commands that the string
contain? - if so, take a look at something I wrote
several years ago:

;; This file: http://user.it.uu.se/~embe8573/conf/emacs-init/shell-cli.el

(defun string-to-cmd (str)
  (interactive (list (read-string " $> ")))
  (let*((cmd  (read (car (split-string str " "))))
        (args (cdr (make-arg-list (string-to-list str)))) )
    (dolist (arg (nreverse args))
      (push 13 unread-command-events) ; 13 is RET
      (dolist (n (reverse (string-to-list arg)))
        (push n unread-command-events) ))
    (call-interactively cmd) ))

;; test: (string-to-cmd "goto-char 0")

(defun make-arg-list (chars)
  (interactive)
  (if chars
      (let ((WS  39) ; whitespace ( )
            (SQM 32) ;      quote (')
            (c  (car chars))
            (cs (cdr chars)) )
        (if (eq c WS) (make-word cs '() WS)
          (if (eq c SQM) (make-arg-list cs)
            (make-word chars '() SQM)) ))
  '() ))

(defun make-word (chars wd sep)
  (interactive)
  (if chars
      (let ((c  (car chars))
            (cs (cdr chars)))
        (if (eq c sep) (cons wd (make-arg-list cs))
          (make-word cs (append wd (list c)) sep)))
    (list wd) ))

(defun zow (string symbol number)
  "Test: shell-cli.el"
   (interactive "sstring: \nSsymbol: \nnnumber: ")
   (message "got: %S" (list string symbol number)) )

(provide 'shell-cli)

-- 
underground experts united
http://user.it.uu.se/~embe8573




reply via email to

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