Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs lisp; how to make a string from a variable of any type?

Tags:

elisp

Like error messages for wrongly called functions show, eg.:

(message (file-attributes "."))

Produces the message:

"eval: Wrong type argument: stringp, ("/home14/tjones" 1 0 0 (20415 35598) (20211 19255) (20211 19255) 14 "lrwxrwxrwx" t ...)"

How do you do this type of translation intentionally, eg.:

(message (thing-to-string (file-attributes ".")))

To message something like:

("/home14/tjones" 1 0 0 (20415 35598) (20211 19255) (20211 19255) 14 "lrwxrwxrwx" t ...)

This is for debugging/info only. I'm assuming there's a way as message is doing it, but is this exposed to us users?

like image 479
mrtimdog Avatar asked May 25 '12 13:05

mrtimdog


People also ask

Is Emacs Lisp the same as Lisp?

Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.

What does #' mean in Emacs Lisp?

function (aka #' ) is used to quote functions, whereas quote (aka ' ) is used to quote data.

How do you declare variables in Emacs?

To define a customizable variable, you should use defcustom (which calls defvar as a subroutine). See Variable Definitions. This special form defines symbol as a variable. Note that symbol is not evaluated; the symbol to be defined should appear explicitly in the defvar form.

Should I learn Emacs Lisp?

Learning a little Emacs Lisp will help you use Emacs more effectively: You will better understand the documentation and online help for functions and variables. You will be able to consult the Lisp source code for a function or variable, in order to understand it still better.


3 Answers

Look into prin1-to-string and related functions (prin1, princ, etc). And do try the manual! http://www.gnu.org/software/emacs/manual/html_node/elisp/Output-Functions.html

like image 51
tripleee Avatar answered Nov 09 '22 18:11

tripleee


In your example, message did not do anything (it just refused to run), so the translation to string was done by the read-eval-print loop which caught the error and turned it into a text message. But yes, message can also do that, and it does that by calling format, which internally uses things like prin1-to-string. So (format "%S" <foo>) would do your thing-to-string.

like image 28
Stefan Avatar answered Nov 09 '22 19:11

Stefan


The first argument to message is supposed to be a format string (same as the one you pass to the format function. If you give it the format "%s" (or "%S" as in Stefan's answer.) it will stringify anything you give it as the next argument.

The capital S version will escape characters in the string so that it can be read again as an s-expression. In this case, I think that is what you want. So, you don't need to change your code very much to get what you are looking for:

(message "%S" (file-attributes "."))
like image 25
A. Levy Avatar answered Nov 09 '22 19:11

A. Levy