Evaluating a string of elisp code is a two-stage process: you need to parse the string using read-from-string and then evaluate the resulting Lisp expression with eval.
(defun my-eval-string (string)
"Evaluate elisp code stored in a string."
(eval (car (read-from-string string))))
Now (my-eval-string "(+ 1 2)") evaluates to 3.
As pointed out by ...
It looks like Emacs simply reads (. 123) as 123, what happened?
That's exactly what happened. To back it up with sources:
if (ch == '.')
if (!NILP (tail))
XSETCDR (tail, read0 (readcharfun));
val = read0 (readcharfun);
read1 (readcharfun, &ch, 0);
if (ch == ')')
if (doc_reference == 1)
That is a struct of elfeed-entry (defined by elfeed). The #s here means struct. The first one is for elfeed-entry, the second is for elfeed-ref.
(cl-defstruct website name shortname url shorturl)
(make-website :name "StackOverflow"
;; => #s(website "StackOverflow" nil "https://stackoverflow.com/" nil)
This is a better approach I think.
(with-current-buffer (find-file-noselect "foo.el")
If you want to wrap this as you describe below, and excecute it, try this:
(defmacro my-read (fname)
,(with-current-buffer (find-file-noselect fname)
The answer of Constantine is okay.
Just to provide a slight modification:
(defun my-eval-string (str)
"Read and evaluate all forms in str.
Return the results of all forms as a list."
(let ((next 0)
(setq ret (cons (funcall (lambda (ret)
(setq next (cdr ...
See Drew's answer to a related question.
If you do not mind ignoring all errors (I do not know which errors read might signal), you should use ignore-errors. Otherwise, your error handler should probably be specific to end-of-file errors.
Personally, I take (sometimes guilty) pleasure in K&R-style and functional brevity, so I would write your function ...
You apparently want to read a character and immediately dispatch behavior based on what it is.
Help > Search Documentation > Find Any Object by Name (command apropos) tells you, for read plus char tells you about function read-char.
C-h f read-char:
read-char is a built-in function in `C source code'.
(read-char &optional PROMPT INHERIT-INPUT-...
If your file contains just one lisp form, all you need to do is
(setq v (read (f-read-text "s.el")))
(see Input Functions).
If there are several forms, like (a b) (c d), you will need to read in cycle using read-from-string or do
(read (concat "(" my-string ")"))
Based on John Kitchin's solution, I come up with the following solution which inserts the needed text before and after the file contents:
If you want evaluation to return nil when it would normally raise an error, wrap the sexp to be evaluated in ignore-errors:
(ignore-errors (read-from-string contents pos))
See also with-demoted-errors.