[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Convert a keyboard macro to equivalent Lisp code
From: |
Juri Linkov |
Subject: |
Re: Convert a keyboard macro to equivalent Lisp code |
Date: |
Mon, 07 Jun 2010 21:33:02 +0300 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (x86_64-pc-linux-gnu) |
> 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/