From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: jemarch@gnu.org Newsgroups: gmane.emacs.devel Subject: eval-to-texi Date: Fri, 30 Apr 2004 16:45:25 +0200 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <16530.26245.472519.384112@termi.eui.upm.es> 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 1083601720 9025 80.91.224.253 (3 May 2004 16:28:40 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 3 May 2004 16:28:40 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Mon May 03 18:28:28 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 1BKgIt-0007y2-00 for ; Mon, 03 May 2004 18:28:27 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1BKgIt-0002dY-00 for ; Mon, 03 May 2004 18:28:27 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BKgAS-0001jA-BN for emacs-devel@quimby.gnus.org; Mon, 03 May 2004 12:19:44 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.30) id 1BKg9B-0001ID-8Z for emacs-devel@gnu.org; Mon, 03 May 2004 12:18:25 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.30) id 1BKg8b-00015K-8u for emacs-devel@gnu.org; Mon, 03 May 2004 12:18:20 -0400 Original-Received: from [199.232.41.8] (helo=mx20.gnu.org) by monty-python.gnu.org with esmtp (TLSv1:DES-CBC3-SHA:168) (Exim 4.30) id 1BKg8a-00015C-RA for emacs-devel@gnu.org; Mon, 03 May 2004 12:17:48 -0400 Original-Received: from [138.100.152.2] (helo=fenix.eui.upm.es) by mx20.gnu.org with esmtp (Exim 4.30) id 1BKek0-0005ir-Hz for emacs-devel@gnu.org; Mon, 03 May 2004 10:48:20 -0400 Original-Received: from termi.eui.upm.es.gnu.org (zorzal.eui.upm.es [138.100.158.159]) by fenix.eui.upm.es (Postfix) with ESMTP id D56A9B6F38 for ; Mon, 3 May 2004 16:44:53 +0200 (CEST) Original-To: emacs-devel@gnu.org X-Mailer: VM 7.17 under Emacs 21.2.1 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:22634 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:22634 2004-04-30 Jose E. Marchesi * textmodes/texinfo.el (texinfo-eval-to-texi): New function. *** texinfo.el.~1.102.~ Sat Feb 21 14:48:44 2004 --- texinfo.el Fri Apr 30 16:42:38 2004 *************** *** 1050,1055 **** --- 1050,1155 ---- ;; job-number"\n")) (tex-recenter-output-buffer nil)) + + (defun texinfo-eval-to-texi (form &optional tostring) + "Evaluates FORM, dumping a @lisp texinfo environment with the evaluation + description. + + If TOSTRING is t, then the @lisp environment is returned into a string, + rather than being inserted into the current 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))) + + + (provide 'texinfo) ;;; arch-tag: 005d7c38-43b9-4b7d-aa1d-aea69bae73e1