From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "Jose E. Marchesi" Newsgroups: gmane.emacs.devel Subject: eval to texinfo Date: Mon, 8 Mar 2004 12:43:57 +0100 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <16460.23677.301949.198404@gnues.es.gnu.org> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1079230812 30493 80.91.224.253 (14 Mar 2004 02:20:12 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 14 Mar 2004 02:20:12 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Sun Mar 14 03:20:07 2004 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1B2LEV-0007ET-00 for ; Sun, 14 Mar 2004 03:20:07 +0100 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1B2LEV-0007cS-00 for ; Sun, 14 Mar 2004 03:20:07 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1B1MTz-0003Qx-AK for emacs-devel@quimby.gnus.org; Thu, 11 Mar 2004 04:28:03 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.30) id 1B0JBZ-0002Fe-7Y for emacs-devel@gnu.org; Mon, 08 Mar 2004 06:44:41 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.30) id 1B0J8G-0001DV-7Y for emacs-devel@gnu.org; Mon, 08 Mar 2004 06:41:47 -0500 Original-Received: from [80.25.141.62] (helo=gnues.es.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1B0J8E-000181-Gu for emacs-devel@gnu.org; Mon, 08 Mar 2004 06:41:14 -0500 Original-Received: by gnues.es.gnu.org (Postfix, from userid 1000) id 5221113F40; Mon, 8 Mar 2004 12:43:57 +0100 (CET) Original-To: emacs-devel@gnu.org X-Mailer: VM 7.03 under Emacs 20.7.2 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:20425 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:20425 Hi. I wrote the following function for the writing of a book about emacs. It dump a @lisp texinfo environment that describes the action. It manages error conditions also. (defun eval-to-texi (form &optional tostring) "Evaluates FORM, dumping a @lisp texinfo environment that describes action. If TOSTRING is t, then the @lisp environment is returned into a string, rather than being inserted into the buffer." (interactive "sForm to evaluate: ") (let (environment-text) (setq environment-text (with-temp-buffer (condition-case error-description (let (expression result output) ;; Begin of the sample (insert "@lisp\n") ;; Dump the form itself into the sample (let ((tform form)) (setq tform (replace-regexp-in-string "@" "@@" tform)) (setq tform (replace-regexp-in-string "{" "@{" tform)) (setq tform (replace-regexp-in-string "}" "@}" tform)) (insert tform "\n")) ;; Parse the form to a valid expression (setq expression (read form)) ;; Get the result of the eval, and the output if there is one (setq output (with-output-to-string (setq result (prin1-to-string (eval expression))))) ;; If there is any output, dump a @print{} entry into the sample (if (not (equal output "")) (progn ;; Escape texinfo special characters on the output (setq output (replace-regexp-in-string "@" "@@" output)) (setq output (replace-regexp-in-string "{" "@{" output)) (setq output (replace-regexp-in-string "}" "@}" output)) ;; Indent multilines (setq output (replace-regexp-in-string "\n" "\n " output)) (insert " @print{} " output "\n"))) ;; If the expression is a macro, dump an @expansion{} (let ((macroexp (macroexpand expression))) (if (not (equal macroexp expression)) ; macro-p??? (let ((met (prin1-to-string macroexp))) ;; Escape texinfo special characters on the macro expansion text (setq met (replace-regexp-in-string "@" "@@" met)) (setq met (replace-regexp-in-string "{" "@{" met)) (setq met (replace-regexp-in-string "}" "@}" met)) ;; Indent multilines (setq met (replace-regexp-in-string "\n" "\n " met)) (insert " @expansion{} " met "\n")))) ;; Escape texinfo special characters on the result (setq result (replace-regexp-in-string "@" "@@" result)) (setq result (replace-regexp-in-string "{" "@{" result)) (setq result (replace-regexp-in-string "}" "@}" result)) ;; Indent multilines (setq result (replace-regexp-in-string "\n" "\n " result)) ;; Dump the @result{} entry into the sample (insert " @result{} " result "\n")) ;; Was an error => Dump an @error{} entry into the sample with the error ;; description from the interpreter (error (insert " @error{} " (error-message-string error-description) "\n"))) ;; End of the sample (insert "@end lisp") ;; Return buffer's contents (buffer-substring (point-min) (point-max)))) (if (not tostring) (insert environment-text) environment-text)))