unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* eval to texinfo
@ 2004-03-08 11:43 Jose E. Marchesi
  0 siblings, 0 replies; only message in thread
From: Jose E. Marchesi @ 2004-03-08 11:43 UTC (permalink / raw)



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)))    

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-03-08 11:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-08 11:43 eval to texinfo Jose E. Marchesi

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).