Hot answers tagged

26

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. Edit: As pointed out by ...


15

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)); else val = read0 (readcharfun); read1 (readcharfun, &ch, 0); if (ch == ')') { if (doc_reference == 1) ...


7

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" :url "https://stackoverflow.com/") ;; => #s(website "StackOverflow" nil "https://stackoverflow.com/" nil) (...


6

This is a better approach I think. (with-current-buffer (find-file-noselect "foo.el") (goto-char (point-min)) (read (current-buffer))) If you want to wrap this as you describe below, and excecute it, try this: (defmacro my-read (fname) `(progn ,(with-current-buffer (find-file-noselect fname) (goto-char (point-min)) (read (current-buffer)...


5

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) ret) (condition-case err (while t (setq ret (cons (funcall (lambda (ret) (setq next (cdr ...


4

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 ...


4

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-...


3

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 ")"))


3

Try this: (with-temp-buffer (insert-file-contents "config.dat") (keep-lines "contexts" (point-min) (point-max)) (setq ctx (when (string-match "contexts: \\(.*\\)" (buffer-string)) (match-string 1 (buffer-string)))))


2

Based on John Kitchin's solution, I come up with the following solution which inserts the needed text before and after the file contents: (with-temp-buffer (save-excursion (insert "(progn\n") (insert-file-contents "foo.el") (goto-char (point-max)) (insert "\n)\n")) (read (current-buffer)))


2

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.


1

Use read-number directly: (interactive (list (read-number "Input increment number: " 1)))


Only top voted, non community-wiki answers of a minimum length are eligible