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: texinfo-eval-to-texi Date: Wed, 5 May 2004 04:45:20 +0200 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <16536.21824.435501.414059@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 1083863539 10804 80.91.224.253 (6 May 2004 17:12:19 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 6 May 2004 17:12:19 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Thu May 06 19:12: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 1BLmPn-0002Hd-00 for ; Thu, 06 May 2004 19:12:07 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1BLmPm-0004G3-00 for ; Thu, 06 May 2004 19:12:07 +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 1BLmKc-0007bR-P1 for emacs-devel@quimby.gnus.org; Thu, 06 May 2004 13:06:46 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.30) id 1BLmKO-0007Yg-Ff for emacs-devel@gnu.org; Thu, 06 May 2004 13:06:32 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.30) id 1BLmJr-0007PV-Oc for emacs-devel@gnu.org; Thu, 06 May 2004 13:06:30 -0400 Original-Received: from [138.100.152.2] (helo=fenix.eui.upm.es) by monty-python.gnu.org with esmtp (Exim 4.30) id 1BLmCN-0005rO-Cx for emacs-devel@gnu.org; Thu, 06 May 2004 12:58:15 -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 1335AB7016 for ; Thu, 6 May 2004 18:26:15 +0200 (CEST) Original-To: emacs-devel@gnu.org X-Mailer: VM 7.18 under Emacs 21.3.50.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:22870 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:22870 New patch taking care compact line spacing suggested by Adrian, the shell-command like interface suggested by Kevin and the tips on writing function documentation strings from the elisp user manual. 2004-04-30 Jose E. Marchesi * textmodes/texinfo.el (texinfo-eval-to-texi): New function. *** texinfo.el 21 Feb 2004 14:48:44 +0100 1.102 --- texinfo.el 05 May 2004 04:44:26 +0200 *************** *** 1050,1055 **** --- 1050,1120 ---- ;; job-number"\n")) (tex-recenter-output-buffer nil)) + + (defun texinfo-eval-to-texi (form &optional insert) + "Evaluate FORM, and display the result into a @lisp Texinfo command. + With a prefix argument, insert the Texinfo command in the current buffer. + + When called from Lisp, the command is returned as a string." + (interactive "sForm to evaluate: \nP") + + (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)) + (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)) + ;; 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)) ; macrop??? + (let ((met (prin1-to-string macroexp))) + ;; Escape texinfo special characters on the macro expansion text + (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)) + ;; 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 insert + (insert environment-text) + (with-output-to-temp-buffer "*Texinfo*" + (princ environment-text))) + + environment-text)) + + + (provide 'texinfo) ;;; arch-tag: 005d7c38-43b9-4b7d-aa1d-aea69bae73e1