all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Is there a way to convert a keyboard macro to a string?
@ 2017-07-26  6:20 Marcin Borkowski
  2017-07-26  6:41 ` Emanuel Berg
                   ` (3 more replies)
  0 siblings, 4 replies; 18+ messages in thread
From: Marcin Borkowski @ 2017-07-26  6:20 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

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



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26  6:20 Is there a way to convert a keyboard macro to a string? Marcin Borkowski
@ 2017-07-26  6:41 ` Emanuel Berg
  2017-07-26  6:43 ` Emanuel Berg
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 18+ messages in thread
From: Emanuel Berg @ 2017-07-26  6:41 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26  6:20 Is there a way to convert a keyboard macro to a string? Marcin Borkowski
  2017-07-26  6:41 ` Emanuel Berg
@ 2017-07-26  6:43 ` Emanuel Berg
  2017-07-26  7:20 ` Héctor Lahoz
  2017-07-26 13:13 ` Stefan Monnier
  3 siblings, 0 replies; 18+ messages in thread
From: Emanuel Berg @ 2017-07-26  6:43 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26  6:20 Is there a way to convert a keyboard macro to a string? Marcin Borkowski
  2017-07-26  6:41 ` Emanuel Berg
  2017-07-26  6:43 ` Emanuel Berg
@ 2017-07-26  7:20 ` Héctor Lahoz
  2017-07-26  8:52   ` Emanuel Berg
  2017-07-26 13:13 ` Stefan Monnier
  3 siblings, 1 reply; 18+ messages in thread
From: Héctor Lahoz @ 2017-07-26  7:20 UTC (permalink / raw)
  To: help-gnu-emacs

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"



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26  7:20 ` Héctor Lahoz
@ 2017-07-26  8:52   ` Emanuel Berg
  0 siblings, 0 replies; 18+ messages in thread
From: Emanuel Berg @ 2017-07-26  8:52 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26  6:20 Is there a way to convert a keyboard macro to a string? Marcin Borkowski
                   ` (2 preceding siblings ...)
  2017-07-26  7:20 ` Héctor Lahoz
@ 2017-07-26 13:13 ` Stefan Monnier
  2017-07-26 16:55   ` Emanuel Berg
  2017-07-26 18:18   ` Marcin Borkowski
  3 siblings, 2 replies; 18+ messages in thread
From: Stefan Monnier @ 2017-07-26 13:13 UTC (permalink / raw)
  To: help-gnu-emacs

> 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




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26 13:13 ` Stefan Monnier
@ 2017-07-26 16:55   ` Emanuel Berg
  2017-07-26 20:22     ` tomas
  2017-07-26 18:18   ` Marcin Borkowski
  1 sibling, 1 reply; 18+ messages in thread
From: Emanuel Berg @ 2017-07-26 16:55 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26 13:13 ` Stefan Monnier
  2017-07-26 16:55   ` Emanuel Berg
@ 2017-07-26 18:18   ` Marcin Borkowski
  2017-07-26 18:40     ` Stefan Monnier
  1 sibling, 1 reply; 18+ messages in thread
From: Marcin Borkowski @ 2017-07-26 18:18 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2017-07-26, at 15:13, Stefan Monnier <monnier@iro.umontreal.ca> 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



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26 18:18   ` Marcin Borkowski
@ 2017-07-26 18:40     ` Stefan Monnier
  2017-07-26 20:38       ` Emanuel Berg
  2017-07-27  6:22       ` Marcin Borkowski
  0 siblings, 2 replies; 18+ messages in thread
From: Stefan Monnier @ 2017-07-26 18:40 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs

> 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



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26 16:55   ` Emanuel Berg
@ 2017-07-26 20:22     ` tomas
  2017-07-26 20:34       ` Emanuel Berg
  0 siblings, 1 reply; 18+ messages in thread
From: tomas @ 2017-07-26 20:22 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26 20:22     ` tomas
@ 2017-07-26 20:34       ` Emanuel Berg
  2017-07-26 20:51         ` John Mastro
  0 siblings, 1 reply; 18+ messages in thread
From: Emanuel Berg @ 2017-07-26 20:34 UTC (permalink / raw)
  To: help-gnu-emacs

> 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




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26 18:40     ` Stefan Monnier
@ 2017-07-26 20:38       ` Emanuel Berg
  2017-07-27  6:22       ` Marcin Borkowski
  1 sibling, 0 replies; 18+ messages in thread
From: Emanuel Berg @ 2017-07-26 20:38 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26 20:34       ` Emanuel Berg
@ 2017-07-26 20:51         ` John Mastro
  2017-07-26 23:32           ` Emanuel Berg
  0 siblings, 1 reply; 18+ messages in thread
From: John Mastro @ 2017-07-26 20:51 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org

Emanuel Berg <moasen@zoho.com> 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



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26 20:51         ` John Mastro
@ 2017-07-26 23:32           ` Emanuel Berg
  0 siblings, 0 replies; 18+ messages in thread
From: Emanuel Berg @ 2017-07-26 23:32 UTC (permalink / raw)
  To: help-gnu-emacs

John Mastro wrote:

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

Excellent, great answer. Thank you :)

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




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-26 18:40     ` Stefan Monnier
  2017-07-26 20:38       ` Emanuel Berg
@ 2017-07-27  6:22       ` Marcin Borkowski
  2017-07-27 12:54         ` Stefan Monnier
  1 sibling, 1 reply; 18+ messages in thread
From: Marcin Borkowski @ 2017-07-27  6:22 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2017-07-26, at 20:40, Stefan Monnier <monnier@IRO.UMontreal.CA> 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



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-27  6:22       ` Marcin Borkowski
@ 2017-07-27 12:54         ` Stefan Monnier
  2017-08-08  4:56           ` Marcin Borkowski
  0 siblings, 1 reply; 18+ messages in thread
From: Stefan Monnier @ 2017-07-27 12:54 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs

> 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



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-07-27 12:54         ` Stefan Monnier
@ 2017-08-08  4:56           ` Marcin Borkowski
  2017-08-08  6:32             ` John Ankarström
  0 siblings, 1 reply; 18+ messages in thread
From: Marcin Borkowski @ 2017-08-08  4:56 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2017-07-27, at 14:54, Stefan Monnier <monnier@IRO.UMontreal.CA> 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



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Is there a way to convert a keyboard macro to a string?
  2017-08-08  4:56           ` Marcin Borkowski
@ 2017-08-08  6:32             ` John Ankarström
  0 siblings, 0 replies; 18+ messages in thread
From: John Ankarström @ 2017-08-08  6:32 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2017-08-08  6:32 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-26  6:20 Is there a way to convert a keyboard macro to a string? Marcin Borkowski
2017-07-26  6:41 ` Emanuel Berg
2017-07-26  6:43 ` Emanuel Berg
2017-07-26  7:20 ` Héctor Lahoz
2017-07-26  8:52   ` Emanuel Berg
2017-07-26 13:13 ` Stefan Monnier
2017-07-26 16:55   ` Emanuel Berg
2017-07-26 20:22     ` tomas
2017-07-26 20:34       ` Emanuel Berg
2017-07-26 20:51         ` John Mastro
2017-07-26 23:32           ` Emanuel Berg
2017-07-26 18:18   ` Marcin Borkowski
2017-07-26 18:40     ` Stefan Monnier
2017-07-26 20:38       ` Emanuel Berg
2017-07-27  6:22       ` Marcin Borkowski
2017-07-27 12:54         ` Stefan Monnier
2017-08-08  4:56           ` Marcin Borkowski
2017-08-08  6:32             ` John Ankarström

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.