Is there a way to convert a keyboard macro to a string?

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Is there a way to convert a keyboard macro to a string?

Marcin Borkowski-3
Hi all,

execute-kbd-macro accepts a string as MACRO.  However, if I record
a macro using F3 ... F4 and look at last-kbd-macro, it is a vector and
not a string.  How do I convert it to a string representing the same key
sequence?

TIA,

--
Marcin Borkowski

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Emanuel Berg-4
Marcin Borkowski wrote:

> execute-kbd-macro accepts a string as MACRO.
> However, if I record a macro using F3 ... F4
> and look at last-kbd-macro, it is a vector
> and not a string. How do I convert it to
> a string representing the same key sequence?

(setq *test-vector* #[1 2 3 4])

(string-join
 (cl-loop for e
          across *test-vector*
          collecting (format "%s" e))) ; "1234"

Battlecruiser operational?

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Emanuel Berg-4
In reply to this post by Marcin Borkowski-3
Or complete:

    (require 'cl-lib)
    (require 'subr-x)

    (defvar *test-vector* #[1 2 3 4])

    (string-join
     (cl-loop for e
              across *test-vector*
              collecting (format "%s" e))) ; "1234"

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Héctor Lahoz
In reply to this post by Marcin Borkowski-3
Marcin Borkowski wrote:
> Hi all,
>
> execute-kbd-macro accepts a string as MACRO.  However, if I record
> a macro using F3 ... F4 and look at last-kbd-macro, it is a vector and
> not a string.  How do I convert it to a string representing the same key
> sequence?

This might be helpful. Section of the manual:
info elisp "Strings of events"

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Emanuel Berg-4
Héctor Lahoz wrote:

> This might be helpful. Section of the manual:
> info elisp "Strings of events"

You mean

    (info "(elisp) Strings of events") ; eval me

:)

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Stefan Monnier
In reply to this post by Marcin Borkowski-3
> execute-kbd-macro accepts a string as MACRO.  However, if I record
> a macro using F3 ... F4 and look at last-kbd-macro, it is a vector and
> not a string.  How do I convert it to a string representing the same key
> sequence?

Depending on the purpose, you can do:

    (seq-into VEC 'string)
or
    (key-description VEC)

The first will fail if your key sequence includes non-character events,
or events with non-trivial modifiers.  The second will give you a string
that's human readable but needs to be passed through `kbd` before it can
be used as a "key sequence" (e.g. for define-key).


        Stefan


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Emanuel Berg-4
Stefan Monnier wrote:

> (seq-into VEC 'string) or (key-description
> VEC)

Yeah, I was just about to say that. The OP can
do that. I mean, it's not wrong or anything!

While we have the Supreme Techno-Pope out of
the paleo-Metabunker, let me ask him to explain
the reason behind vectors.

They are not vectors in the math sense because
they don't come with operators such as the
cross product and what have you from accursed
Linear Algebra.

They are one dimensional arrays.

But why can't you use lists for that?
Because you can!

Implementation and optimization derived
details? If so, which ones and how?

The answer may be supplied in anyone's literary
style, including that of yours truly. If he
still got the skills, that is :)

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Marcin Borkowski-3
In reply to this post by Stefan Monnier

On 2017-07-26, at 15:13, Stefan Monnier <[hidden email]> wrote:

>> execute-kbd-macro accepts a string as MACRO.  However, if I record
>> a macro using F3 ... F4 and look at last-kbd-macro, it is a vector and
>> not a string.  How do I convert it to a string representing the same key
>> sequence?
>
> Depending on the purpose, you can do:
>
>     (seq-into VEC 'string)
> or
>     (key-description VEC)
>
> The first will fail if your key sequence includes non-character events,
> or events with non-trivial modifiers.  The second will give you a string
> that's human readable but needs to be passed through `kbd` before it can
> be used as a "key sequence" (e.g. for define-key).

Thanks, that was _exactly_ what I needed!  This means that what I want
is to first use seq-into (wrapped in condition-case), and if that fails,
key-description.  Emacs did not disappoint! ;-)

Best,

--
Marcin Borkowski

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Stefan Monnier
> Thanks, that was _exactly_ what I needed!  This means that what I want
> is to first use seq-into (wrapped in condition-case), and if that fails,
> key-description.  Emacs did not disappoint! ;-)

I think that's the wrong approach: the kinds of strings returned
are different.

E.g for a vector like [?\C-a], seq-into will return a string with
a single char (the ASCII code 1), whereas key-description will return
the string "C-a".

The most likely situation is that you always want to use
`key-description` because the reason for wanting a string is to
have a human-readable representation of the sequence of events.


        Stefan

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Tomas Zerolo
In reply to this post by Emanuel Berg-4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Jul 26, 2017 at 06:55:01PM +0200, Emanuel Berg wrote:

[...]

> While we have the Supreme Techno-Pope out of
> the paleo-Metabunker, let me ask him to explain
> the reason behind vectors.

Not the Pope here, by a long stretch, but the reason
is in the (Elisp) doc ("6 Sequences, Arrays, Vectors"):
a vector is constant access time for the nth element,
while on a list you need time proportional to the
number n (i.e. you have to "walk down" the list to
access its nth element (or rather, the little Lisp
gremlin has to do it for you).

OTOH, prepending an element to a list is quick & easy,
whereas for a vector, you have to copy the whole kaboodle
to get another vector.

Similar, yet different tools.

- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAll4+h0ACgkQBcgs9XrR2kZjqwCfYgHLA+wDxWWX0tFOdZK4rjS6
WrEAn2emSrFf1VisUvlyOWQ2LSrDJB0R
=WiyE
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Emanuel Berg-4
> Not the Pope here, by a long stretch, but the
> reason is in the (Elisp) doc ("6 Sequences,
> Arrays, Vectors"): a vector is constant access
> time for the nth element, while on a list you
> need time proportional to the number n (i.e.
> you have to "walk down" the list to access its
> nth element (or rather, the little Lisp gremlin
> has to do it for you).

Yes, constant access time instead of linear.
Different data structures.

Temporal complexity... or perhaps spacial?
I don't remember.

But *how* does this happen?

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Emanuel Berg-4
In reply to this post by Stefan Monnier
Stefan Monnier wrote:

> The most likely situation is that you always
> want to use `key-description` because the
> reason for wanting a string is to have
> a human-readable representation of the
> sequence of events.

Seriously, how much money would you ask to
write your computer memoirs starting with
seeing your first computer ever?

And you wouldn't have to bother with anything,
I do all the LaTeX and groff and gnuplot and
website companion and appendix timelines and
glossary and GNU knows what...

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

John Mastro
In reply to this post by Emanuel Berg-4
Emanuel Berg <[hidden email]> wrote:
> Yes, constant access time instead of linear.
> Different data structures.
>
> Temporal complexity... or perhaps spacial?
> I don't remember.
>
> But *how* does this happen?

This is off course simplified and just for illustration, but think of
the respective structures like this:

struct vector {
    Lisp_Value *elts;
};

struct cons {
    Lisp_Value car, cdr;
};

A call like (nth n seq) then boils down to something like:

/* Vector - a single array access by index */
return vector->elts[n];

/* Cons */
while (n-- > 0) /* This loop is what makes it O(N) */
    cons = cdr(cons);
return car(cons);

That's how it happens :)

        John

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Emanuel Berg-4
John Mastro wrote:

> ... That's how it happens :)

Excellent, great answer. Thank you :)

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Marcin Borkowski-3
In reply to this post by Stefan Monnier

On 2017-07-26, at 20:40, Stefan Monnier <[hidden email]> wrote:

>> Thanks, that was _exactly_ what I needed!  This means that what I want
>> is to first use seq-into (wrapped in condition-case), and if that fails,
>> key-description.  Emacs did not disappoint! ;-)
>
> I think that's the wrong approach: the kinds of strings returned
> are different.
>
> E.g for a vector like [?\C-a], seq-into will return a string with
> a single char (the ASCII code 1), whereas key-description will return
> the string "C-a".
>
> The most likely situation is that you always want to use
> `key-description` because the reason for wanting a string is to
> have a human-readable representation of the sequence of events.

Well, I was too concise, sorry.  I mean that if seq-into works, its
result would be fed to insert, and if not, the result of key-description
would be fed into execute-kbd-macro.  This _should_ work, no?

Best,

--
Marcin Borkowski

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Stefan Monnier
> Well, I was too concise, sorry.  I mean that if seq-into works, its
> result would be fed to insert, and if not, the result of key-description
> would be fed into execute-kbd-macro.  This _should_ work, no?

I don't think this will work, but then I don't know what you're trying
to do.


        Stefan

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

Marcin Borkowski-3

On 2017-07-27, at 14:54, Stefan Monnier <[hidden email]> wrote:

>> Well, I was too concise, sorry.  I mean that if seq-into works, its
>> result would be fed to insert, and if not, the result of key-description
>> would be fed into execute-kbd-macro.  This _should_ work, no?
>
> I don't think this will work, but then I don't know what you're trying
> to do.

Well, you were right and wrong at the same time.  It didn't "just work",
but after some modifications it did what I wanted it to do.  Here's the
thing described: http://mbork.pl/2017-08-07_demo-it_recording

Thanks,

--
Marcin Borkowski

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there a way to convert a keyboard macro to a string?

John Ankarström
2017-08-08 06:56 Marcin Borkowski wrote:
> Well, you were right and wrong at the same time.  It didn't "just
> work",
> but after some modifications it did what I wanted it to do.  Here's the
> thing described: http://mbork.pl/2017-08-07_demo-it_recording

Wow, that's really cool, and a very good idea.
Looking at the code, though, I have to ask if
it's a good idea to cancel the macro whenever
`keyboard-quit' is called, as users might have
a reason to press C-g without wanting to cancel
the macro.

Otherwise, great work, and I hope you refine
your code such that it is merged into the
`demo-it' package.

- John

Loading...