[comp.emacs] pretty-printer

merlyn@intelob.intel.com (Randal L. Schwartz @ Stonehenge) (09/15/88)

In article <8809122124.AA06496@nav.icst.nbs.gov>, rbj@nav (Root Boy Jim) writes:
| Why, a pretty printer, of course!
| 
[his source deleted...]

Here's one I've been messing with for quite a while.  I have a place
to hook in a conversion from (quote (a b c)) to '(a b c), but haven't
done it yet (I'm worried about the pathological case of (quote a b
c)).

Thanks to weemba for the inspiration on this one, mostly out of a
challenge while reading the Gnews code of "could it be done better
than that?".

enable shar mode...  (only signature at the end...)

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  pp.el
# Wrapped by merlyn@intelob on Wed Sep 14 10:17:18 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'pp.el' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pp.el'\"
else
echo shar: Extracting \"'pp.el'\" \(1778 characters\)
sed "s/^X//" >'pp.el' <<'END_OF_FILE'
X;;; original version by merlyn
X;;; LastEditDate="Fri Sep  9 13:58:42 1988"
X
X(defun pp-to-string (object)
X  "Return a string containing the pretty-printed representation of OBJECT,
Xany Lisp object.  Quoting characters are used when needed to make output
Xthat `read' can handle, whenever this is possible."
X  (save-excursion
X    (set-buffer (generate-new-buffer " pp-to-string"))
X    (unwind-protect
X	(progn
X	  (emacs-lisp-mode)
X	  (let ((print-escape-newlines t))
X	    (prin1 object (current-buffer)))
X	  (goto-char (point-min))
X	  (while (not (eobp))
X	    ;; (message "%06d" (- (point-max) (point)))
X	    (cond
X	     ((looking-at "\\s(")
X	      (while (looking-at "\\s(")
X		(forward-char 1)))
X	     ;; (looking-at "quote")
X	     ((condition-case err-var
X		  (prog1 t (down-list 1))
X		(error nil))
X	      (backward-char 1)
X	      (newline-and-indent))
X	     ((condition-case err-var
X		  (prog1 t (up-list 1))
X		(error nil))
X	      (while (looking-at "\\s)")
X		(forward-char 1))
X	      (newline-and-indent))
X	     (t (goto-char (point-max)))))
X	  (buffer-string))
X      (kill-buffer (current-buffer)))))
X
X(defun pp (object &optional stream)
X  "Output the pretty-printed representation of OBJECT, any Lisp object.
XQuoting characters are printed when needed to make output that `read'
Xcan handle, whenever this is possible.
XOutput stream is STREAM, or value of `standard-output' (which see)."
X  (princ (pp-to-string object) (or stream standard-output)))
X
X(defun pp-eval-expression (expression)
X  "Evaluate EXPRESSION and pretty-print value into a new display buffer.
XValue is also consed on to front of variable  values  's value."
X  (interactive "xPp-eval: ")
X  (setq values (cons (eval expression) values))
X  (with-output-to-temp-buffer "*Pp Eval Output*"
X    (pp (car values))))
END_OF_FILE
if test 1778 -ne `wc -c <'pp.el'`; then
    echo shar: \"'pp.el'\" unpacked with wrong size!
fi
# end of 'pp.el'
fi
echo shar: End of shell archive.
exit 0
-- 
Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095
on contract to BiiN Technical Information Services (for now :-),
in a former Intel building in Hillsboro, Oregon, USA
<merlyn@intelob.intel.com> or ...!tektronix!inteloa[!intelob]!merlyn
Standard disclaimer: I *am* my employer!