unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@jurta.org>
To: rms@gnu.org
Cc: emacs-devel@gnu.org
Subject: Re: Convert a keyboard macro to equivalent Lisp code
Date: Mon, 07 Jun 2010 21:33:02 +0300	[thread overview]
Message-ID: <87mxv76k8h.fsf@mail.jurta.org> (raw)
In-Reply-To: <E1OLYby-0007gS-Qq@fencepost.gnu.org> (Richard Stallman's message of "Mon, 07 Jun 2010 05:27:14 -0400")

> Could you please add comments explaining the new things that
> the code in callint.c is doing?

Ok, will do.

> It is rather ugly that the conversion can only apply to the last
> macro, not to other macros that were saved under names.  How about
> saving the Lisp code when giving a name to a macro?

The following patch saves the Lisp code in the symbol property
of the keyboard macro in `name-last-kbd-macro', and uses it
in `insert-kbd-macro-commands' whose interactive spec now is
like the interactive spec of `insert-kbd-macro':

=== modified file 'lisp/macros.el'
--- lisp/macros.el	2010-03-03 17:31:50 +0000
+++ lisp/macros.el	2010-06-07 18:32:45 +0000
@@ -46,7 +46,8 @@ (defun name-last-kbd-macro (symbol)
 	      symbol))
   (if (string-equal symbol "")
       (error "No command name given"))
-  (fset symbol last-kbd-macro))
+  (fset symbol last-kbd-macro)
+  (put symbol 'kbd-macro-command-history last-kbd-macro-command-history))
 
 ;;;###autoload
 (defun insert-kbd-macro (macroname &optional keys)
@@ -280,6 +281,57 @@ (defun apply-macro-to-region-lines (top
 
 ;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query)
 
+;;; Convert a keyboard macro to Lisp code.
+
+;;;###autoload
+(defun insert-kbd-macro-commands (macroname)
+  "Insert in buffer the commands of kbd macro NAME, as Lisp code.
+
+To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
+use this command, and then save the file."
+  (interactive
+   (list (intern (completing-read
+		  "Insert Lisp commands of kbd macro (name): "
+		  obarray
+		  (lambda (elt)
+		    (and (fboundp elt)
+			 (get elt 'kbd-macro-command-history)))
+		  t))))
+  (let (definition)
+    (if (or (null macroname)
+	    (string= (symbol-name macroname) ""))
+	(setq macroname 'last-kbd-macro definition last-kbd-macro)
+      (setq definition (get macroname 'kbd-macro-command-history)))
+    (insert
+     "(defun " (format "%s" macroname) " ()\n"
+     "  \"Command created from the keyboard macro.\"\n")
+    (comment-region (prog1 (point)
+		      (insert "  Original keys: ")
+		      (insert-kbd-macro (intern "")))
+		    (point))
+    (insert "  (interactive)\n")
+    (dolist (s (kmacro-convert-command-history
+		last-kbd-macro-command-history))
+      (insert "  " (pp-to-string s)))
+    (insert ")\n")))
+
+(defun kmacro-convert-command-history (commands)
+  (let ((cmds commands) cmd name ret)
+    (while cmds
+      (setq cmd (car cmds))
+      (setq name (car cmd))
+      (cond
+       ;; Skip next commands.
+       ((memq name '(
+		     kmacro-start-macro kmacro-end-macro
+		     universal-argument universal-argument-other-key
+		     digit-argument
+		     execute-extended-command
+		     )))
+       (t (push cmd ret)))
+      (setq cmds (cdr cmds)))
+    (nreverse ret)))
+
 (provide 'macros)
 
 ;; arch-tag: 346ed1a5-1220-4bc8-b533-961ee704361f

-- 
Juri Linkov
http://www.jurta.org/emacs/



  parent reply	other threads:[~2010-06-07 18:33 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-05 19:57 Convert a keyboard macro to equivalent Lisp code Juri Linkov
2010-06-07  9:27 ` Richard Stallman
2010-06-07  9:27 ` Richard Stallman
2010-06-07 13:36   ` Stefan Monnier
2010-06-07 18:33   ` Juri Linkov [this message]
2010-06-07 18:35 ` Juri Linkov
2010-06-07 20:11   ` Stefan Monnier
2010-06-08  8:05     ` Juri Linkov
2010-06-09  1:36       ` Stefan Monnier
2010-06-09  8:34         ` Juri Linkov
2010-06-09 14:17           ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mxv76k8h.fsf@mail.jurta.org \
    --to=juri@jurta.org \
    --cc=emacs-devel@gnu.org \
    --cc=rms@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

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