From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.devel Subject: Re: Convert a keyboard macro to equivalent Lisp code Date: Mon, 07 Jun 2010 21:33:02 +0300 Organization: JURTA Message-ID: <87mxv76k8h.fsf@mail.jurta.org> References: <87fx11mfd3.fsf@mail.jurta.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1275936745 6598 80.91.229.12 (7 Jun 2010 18:52:25 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 7 Jun 2010 18:52:25 +0000 (UTC) Cc: emacs-devel@gnu.org To: rms@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jun 07 20:52:24 2010 connect(): No such file or directory Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1OLhQt-0003qS-It for ged-emacs-devel@m.gmane.org; Mon, 07 Jun 2010 20:52:23 +0200 Original-Received: from localhost ([127.0.0.1]:34730 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OLhQs-0005PD-OH for ged-emacs-devel@m.gmane.org; Mon, 07 Jun 2010 14:52:22 -0400 Original-Received: from [140.186.70.92] (port=44936 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OLhQf-0005N2-UU for emacs-devel@gnu.org; Mon, 07 Jun 2010 14:52:11 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OLhQe-0003Hm-Lj for emacs-devel@gnu.org; Mon, 07 Jun 2010 14:52:09 -0400 Original-Received: from smtp-out1.starman.ee ([85.253.0.3]:44909 helo=mx1.starman.ee) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OLhQe-0003HI-9p; Mon, 07 Jun 2010 14:52:08 -0400 X-Virus-Scanned: by Amavisd-New at mx1.starman.ee Original-Received: from mail.starman.ee (82.131.35.211.cable.starman.ee [82.131.35.211]) by mx1.starman.ee (Postfix) with ESMTP id AA3D93F4198; Mon, 7 Jun 2010 21:52:04 +0300 (EEST) In-Reply-To: (Richard Stallman's message of "Mon, 07 Jun 2010 05:27:14 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (x86_64-pc-linux-gnu) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:125602 Archived-At: > 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/