* [ELISP] How do you turn an array of chars into a string?
@ 2010-08-01 17:01 Joseph Brenner
2010-08-01 21:52 ` Pascal J. Bourguignon
2010-08-02 21:22 ` Stefan Monnier
0 siblings, 2 replies; 10+ messages in thread
From: Joseph Brenner @ 2010-08-01 17:01 UTC (permalink / raw)
To: help-gnu-emacs
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?
Things like this seem to work, but only for very simple chars:
(mapconcat 'string [?c ?a ?t] "") ;; => "cat"
Motivation: see the first FIXME in footnote.el, on footnote-prefix.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [ELISP] How do you turn an array of chars into a string?
2010-08-01 17:01 [ELISP] How do you turn an array of chars into a string? Joseph Brenner
@ 2010-08-01 21:52 ` Pascal J. Bourguignon
2010-08-01 23:07 ` Joseph Brenner
2010-08-02 21:22 ` Stefan Monnier
1 sibling, 1 reply; 10+ messages in thread
From: Pascal J. Bourguignon @ 2010-08-01 21:52 UTC (permalink / raw)
To: help-gnu-emacs
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.
So what do you really want?
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."
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...
> Things like this seem to work, but only for very simple chars:
>
> (mapconcat 'string [?c ?a ?t] "") ;; => "cat"
What is a non-simple character???
--
__Pascal Bourguignon__ http://www.informatimago.com/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [ELISP] How do you turn an array of chars into a string?
2010-08-01 21:52 ` Pascal J. Bourguignon
@ 2010-08-01 23:07 ` Joseph Brenner
0 siblings, 0 replies; 10+ messages in thread
From: Joseph Brenner @ 2010-08-01 23:07 UTC (permalink / raw)
To: help-gnu-emacs
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.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [ELISP] How do you turn an array of chars into a string?
2010-08-01 17:01 [ELISP] How do you turn an array of chars into a string? Joseph Brenner
2010-08-01 21:52 ` Pascal J. Bourguignon
@ 2010-08-02 21:22 ` Stefan Monnier
2010-08-02 21:55 ` Pascal J. Bourguignon
2010-08-03 20:57 ` Joseph Brenner
1 sibling, 2 replies; 10+ messages in thread
From: Stefan Monnier @ 2010-08-02 21:22 UTC (permalink / raw)
To: help-gnu-emacs
> 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?
key-description
> What if you want to convert an array-of-chars into a string?
A string *is* an array of chars. If you want to convert a vector
(i.e. one of those arrays that contains arbitrary Lisp values) to
a string, you can use `concat': (concat (vector ?a ?b ?c)) => "abc".
Stefan
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [ELISP] How do you turn an array of chars into a string?
2010-08-02 21:22 ` Stefan Monnier
@ 2010-08-02 21:55 ` Pascal J. Bourguignon
2010-08-02 22:41 ` Johan Bockgård
2010-08-03 9:33 ` Stefan Monnier
2010-08-03 20:57 ` Joseph Brenner
1 sibling, 2 replies; 10+ messages in thread
From: Pascal J. Bourguignon @ 2010-08-02 21:55 UTC (permalink / raw)
To: help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> 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?
>
> key-description
>
>> What if you want to convert an array-of-chars into a string?
>
> A string *is* an array of chars.
Not in scheme or emacs lisp. (vectorp "abc") --> nil
> If you want to convert a vector
> (i.e. one of those arrays that contains arbitrary Lisp values) to
> a string, you can use `concat': (concat (vector ?a ?b ?c)) => "abc".
>
>
> Stefan
--
__Pascal Bourguignon__ http://www.informatimago.com/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [ELISP] How do you turn an array of chars into a string?
2010-08-02 21:55 ` Pascal J. Bourguignon
@ 2010-08-02 22:41 ` Johan Bockgård
2010-08-02 22:57 ` Pascal J. Bourguignon
2010-08-03 9:33 ` Stefan Monnier
1 sibling, 1 reply; 10+ messages in thread
From: Johan Bockgård @ 2010-08-02 22:41 UTC (permalink / raw)
To: help-gnu-emacs
pjb@informatimago.com (Pascal J. Bourguignon) writes:
> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>
>> A string *is* an array of chars.
>
> Not in scheme or emacs lisp. (vectorp "abc") --> nil
(arrayp "abc") => t
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [ELISP] How do you turn an array of chars into a string?
2010-08-02 22:41 ` Johan Bockgård
@ 2010-08-02 22:57 ` Pascal J. Bourguignon
0 siblings, 0 replies; 10+ messages in thread
From: Pascal J. Bourguignon @ 2010-08-02 22:57 UTC (permalink / raw)
To: help-gnu-emacs
Johan Bockgård <bojohan+news@gnu.org> writes:
> pjb@informatimago.com (Pascal J. Bourguignon) writes:
>
>> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>>
>>> A string *is* an array of chars.
>>
>> Not in scheme or emacs lisp. (vectorp "abc") --> nil
>
> (arrayp "abc") => t
Ah, right, in emacs array is a supertype of string and vector.
--
__Pascal Bourguignon__ http://www.informatimago.com/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [ELISP] How do you turn an array of chars into a string?
2010-08-02 21:55 ` Pascal J. Bourguignon
2010-08-02 22:41 ` Johan Bockgård
@ 2010-08-03 9:33 ` Stefan Monnier
1 sibling, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2010-08-03 9:33 UTC (permalink / raw)
To: help-gnu-emacs
>> A string *is* an array of chars.
> Not in scheme or emacs lisp. (vectorp "abc") --> nil
(arrayp "abc") => t
Hence my subsequent:
>> If you want to convert a vector
>> (i.e. one of those arrays that contains arbitrary Lisp values) to
Stefan
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [ELISP] How do you turn an array of chars into a string?
2010-08-02 21:22 ` Stefan Monnier
2010-08-02 21:55 ` Pascal J. Bourguignon
@ 2010-08-03 20:57 ` Joseph Brenner
2010-08-04 9:27 ` Stefan Monnier
1 sibling, 1 reply; 10+ messages in thread
From: Joseph Brenner @ 2010-08-03 20:57 UTC (permalink / raw)
To: help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> What if you want to convert an array-of-chars into a string?
>
> A string *is* an array of chars.
Yes, I've heard that, but:
(let ((mah-array [?c ?a ?t])
(mah-string "cat"))
(equal mah-array mah-string)) ;; ==> nil
(let ((mah-array [?c ?a ?t])
(mah-string "cat"))
(string= mah-array mah-string))
;; Lisp error: (wrong-type-argument stringp [99 97 116])
Though on the other hand:
(equal
(aref [?c ?a ?t] 2)
(aref "cat" 2)) ;; ==> t
In any case, it doesn't exactly make sense to me to just say
that "a string is an array of chars".
I suppose you might say "a string is an array of chars, with a type
of 'string'".
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [ELISP] How do you turn an array of chars into a string?
2010-08-03 20:57 ` Joseph Brenner
@ 2010-08-04 9:27 ` Stefan Monnier
0 siblings, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2010-08-04 9:27 UTC (permalink / raw)
To: help-gnu-emacs
>>> What if you want to convert an array-of-chars into a string?
>> A string *is* an array of chars.
[...]
> In any case, it doesn't exactly make sense to me to just say
> that "a string is an array of chars".
Then I guess a better way to say it is:
a string is one of the various kinds of arrays supported by Elisp
(other types are vectors, char-tables, and bool-vectors).
Stefan
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2010-08-04 9:27 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-01 17:01 [ELISP] How do you turn an array of chars into a string? Joseph Brenner
2010-08-01 21:52 ` Pascal J. Bourguignon
2010-08-01 23:07 ` Joseph Brenner
2010-08-02 21:22 ` Stefan Monnier
2010-08-02 21:55 ` Pascal J. Bourguignon
2010-08-02 22:41 ` Johan Bockgård
2010-08-02 22:57 ` Pascal J. Bourguignon
2010-08-03 9:33 ` Stefan Monnier
2010-08-03 20:57 ` Joseph Brenner
2010-08-04 9:27 ` Stefan Monnier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).