From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Johannes Weiner Newsgroups: gmane.emacs.devel Subject: [PATCH] pp-macroexpansion Date: Mon, 20 Aug 2007 17:25:14 +0200 Message-ID: <20070820152514.GA3936@saeurebad.de> References: <20070819220806.GA14139@saeurebad.de> <20070820013126.GA24021@saeurebad.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="zYM0uCDKw75PZbzx" X-Trace: sea.gmane.org 1187623531 14074 80.91.229.12 (20 Aug 2007 15:25:31 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 20 Aug 2007 15:25:31 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Aug 20 17:25:28 2007 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.50) id 1IN98B-0003aI-G6 for ged-emacs-devel@m.gmane.org; Mon, 20 Aug 2007 17:25:27 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IN98B-0005Js-Dd for ged-emacs-devel@m.gmane.org; Mon, 20 Aug 2007 11:25:27 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1IN986-0005JM-WA for emacs-devel@gnu.org; Mon, 20 Aug 2007 11:25:23 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1IN985-0005Iq-Qt for emacs-devel@gnu.org; Mon, 20 Aug 2007 11:25:22 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IN985-0005Ig-No for emacs-devel@gnu.org; Mon, 20 Aug 2007 11:25:21 -0400 Original-Received: from saeurebad.de ([85.214.36.134]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1IN983-0002wl-Uw for emacs-devel@gnu.org; Mon, 20 Aug 2007 11:25:20 -0400 Original-Received: by saeurebad.de (Postfix, from userid 1000) id 90A882F03B2; Mon, 20 Aug 2007 17:25:14 +0200 (CEST) Mail-Followup-To: emacs-devel@gnu.org Content-Disposition: inline In-Reply-To: <20070820013126.GA24021@saeurebad.de> User-Agent: Mutt/1.5.16 (2007-06-11) X-Detected-Kernel: 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:76840 Archived-At: --zYM0uCDKw75PZbzx Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi again, On Mon, Aug 20, 2007 at 03:31:26AM +0200, Johannes Weiner wrote: > > here is a patch that extracts last-sexp from already existing code so that one > > can use last-sexp for other purposes too. > > My real goals where of course, interactive macro expansion while editing lisp > code. So I implemented that also. The result is now a more generalized > operation functionality on the sexp before point. I realized now that macroexpansion really should not be done without pretty-printing, the resulting forms are unreadable when they are more than just a setf -> setq expansion. So please forget about the above patches, here is one that extends the code in pp.el by `pp-macroexpand-expression' and `pp-macroexpand-last-sexp'. The code is now a lot shorter, more readable and the results more usable. Diff attached, have fun! Hannes --zYM0uCDKw75PZbzx Content-Type: text/x-diff; charset=us-ascii Content-Disposition: inline; filename="emacs-pp-macroexpand-last-sexp.patch" diff -Naur emacs.orig/lisp/emacs-lisp/pp.el emacs/lisp/emacs-lisp/pp.el --- emacs.orig/lisp/emacs-lisp/pp.el 2007-08-20 16:51:28.000000000 +0200 +++ emacs/lisp/emacs-lisp/pp.el 2007-08-20 17:18:48.000000000 +0200 @@ -97,14 +97,10 @@ (princ (pp-to-string object) (or stream standard-output))) ;;;###autoload -(defun pp-eval-expression (expression) - "Evaluate EXPRESSION and pretty-print its value. -Also add the value to the front of the list in the variable `values'." - (interactive - (list (read-from-minibuffer "Eval: " nil read-expression-map t - 'read-expression-history))) - (message "Evaluating...") - (setq values (cons (eval expression) values)) +(defun pp-display-expression (expression out-buffer-name) + "Prettify and display EXPRESSION in an appropriate way, +depending on its printed length. If a temporary buffer is needed +for representation, it will be named after OUT-BUFFER-NAME." (let* ((old-show-function temp-buffer-show-function) ;; Use this function to display the buffer. ;; This function either decides not to display it at all @@ -128,23 +124,37 @@ (select-window window) (run-hooks 'temp-buffer-show-hook)) (select-window old-selected) - (message "Evaluating...done. \ -See buffer *Pp Eval Output*."))) + (message "See buffer %s." out-buffer-name))) (message "%s" (buffer-substring (point-min) (point))) )))))) - (with-output-to-temp-buffer "*Pp Eval Output*" - (pp (car values)) + (with-output-to-temp-buffer out-buffer-name + (pp expression) (with-current-buffer standard-output (emacs-lisp-mode) (setq buffer-read-only nil) (set (make-local-variable 'font-lock-verbose) nil))))) ;;;###autoload -(defun pp-eval-last-sexp (arg) - "Run `pp-eval-expression' on sexp before point (which see). -With argument, pretty-print output into current buffer. -Ignores leading comment characters." - (interactive "P") +(defun pp-eval-expression (expression) + "Evaluate EXPRESSION and pretty-print its value. +Also add the value to the front of the list in the variable `values'." + (interactive + (list (read-from-minibuffer "Eval: " nil read-expression-map t + 'read-expression-history))) + (message "Evaluating...") + (setq values (cons (eval expression) values)) + (pp-display-expression (car values) "*Pp Eval Output*")) + +;;;###autoload +(defun pp-macroexpand-expression (expression) + "Macroexpand EXPRESSION and pretty-print its value." + (interactive + (list (read-from-minibuffer "Macroexpand: " nil read-expression-map t + 'read-expression-history))) + (pp-display-expression (macroexpand expression) "*Pp Macroexpand Output*")) + +(defun pp-last-sexp () + "Read sexp before point. Ignores leading comment characters." (let ((stab (syntax-table)) (pt (point)) start exp) (set-syntax-table emacs-lisp-mode-syntax-table) (save-excursion @@ -160,9 +170,27 @@ (setq exp (read exp))) (setq exp (read (current-buffer))))) (set-syntax-table stab) - (if arg - (insert (pp-to-string (eval exp))) - (pp-eval-expression exp)))) + exp)) + +;;;###autoload +(defun pp-eval-last-sexp (arg) + "Run `pp-eval-expression' on sexp before point (which see). +With argument, pretty-print output into current buffer. +Ignores leading comment characters." + (interactive "P") + (if arg + (insert (pp-to-string (eval (pp-last-sexp)))) + (pp-eval-expression (pp-last-sexp)))) + +;;;###autoload +(defun pp-macroexpand-last-sexp (arg) + "Run `pp-macroexpand-expression' on sexp before point (which see). +With argument, pretty-print output into current buffer. +Ignores leading comment characters." + (interactive "P") + (if arg + (insert (pp-to-string (macroexpand (pp-last-sexp)))) + (pp-macroexpand-expression (pp-last-sexp)))) ;;; Test cases for quote ;; (pp-eval-expression ''(quote quote)) --zYM0uCDKw75PZbzx Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-devel mailing list Emacs-devel@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-devel --zYM0uCDKw75PZbzx--