help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ELISP] How do you turn an array of chars into a string?


From: Joseph Brenner
Subject: Re: [ELISP] How do you turn an array of chars into a string?
Date: Wed, 08 Dec 2010 15:22:15 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> Joseph Brenner <doom@kzsu.stanford.edu> writes:
>
>> The elisp manual has this example, using "kbd" to convert a (relatively)
>> readable string into the "internal Emacs key representation":
>>
>>      (global-set-key (kbd "C-x C-\\") 'next-line)
>>
>>      (global-set-key [?\C-x ?\C-\\] 'next-line)
>>
>> What's the inverse of kbd?
>
>> What if you want to convert an array-of-chars
>> into a string?
>
> These are two radically different things.
>
> The inverse of kbd doesn't convert an array of characters into a
> string, it would produce a string containing a text describing in a
> human readable form the keychoard sequence.

Which is indeed, a kind of string, and from context, I would hope it's
clear that that's the kind of string I was talking about.

> To convert a vector of characters to a string you could use:
>
> (require 'cl) ; all the good stuff is always in there!
>
> (coerce [?c ?a ?t] 'string) --> "cat"
>
> (concatenate 'string  "A " [?c ?a ?t] '(?  ?e ?a ?t ?s) " a mouse.")
> --> "A cat eats a mouse."

Yes, looks good, but then I'd figured out ways to do that sort of job...

>> Things like this seem to work, but only for very simple chars:
>>
>>   (mapconcat 'string [?c ?a ?t] "")  ;; => "cat"
>
> What is a non-simple character???

Well, for example, it doesn't work for:

  (control ?c)

But then it does work for:

  ?\C-c

SO it could be I was wrong.

> To convert a vector of key chords into a human readable description of
> it, I don't know.  But the command where-is seems to be knowing how to
> do it, so let's read the source of where-is!  Here, we find a:
> (mapconcat 'key-description keys ", ") therefore key-description might
> be the right function.  Read the documentation.  Yes!  Notice how it
> says nothing about converting vectors to string!!!
>
> (key-description (kbd "C-x C-\\"))
>  -->  "C-x C-\\"
>
> (key-description (kbd "C-M-A-s-Z C-u 123 H-S-A-é"))
>  --> "A-C-M-s-z C-u 1 2 3 A-H-S-é"
>
> Looks good...

Yes, thanks much.  That does indeed look like the solution.



reply via email to

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