Subject: bug#13115: prettier pretty-printing for eieio

From: Jonas Bernoulli <[email-masked]>
Date: Fri, 07 Dec 2012 17:54:51 +0100

Hello

The attached patch implements prettier pretty-printing in eieio.

* don't quote keywords and booleans
* don't insert needless newlines (empty lines, and before closing
  parents)
* align values that are objects with the slot keyword instead of putting
  it's beginning on the same line as the slot keyword.
* similar for lists of objects

Prettier pp:

,----
| (eieio-pp-demo "top"
|   :keyword :keyword
|   :boolean nil
|   :object
|   (eieio-pp-demo "subsub"
|     :list '(a b c))
|   :list '(a b c)
|   :object-list
|   (list
|     (eieio-pp-demo "sub"
|       :keyword :keyword
|       :object
|       (eieio-pp-demo "subsub"
|         :list '(a b c))
|       :list '(a b c))
|     'non-object
|     (eieio-pp-demo "sub"
|       :keyword :keyword
|       :object
|       (eieio-pp-demo "subsub"
|         :list '(a b c))
|       :list '(a b c))))
`----

Not so pretty pp:

,----
| (eieio-pp-demo "top"
|   :keyword ':keyword
|   :boolean 'nil
|   :object   (eieio-pp-demo "subsub"
|     :list '(a b c)
|     )
| 
|   :list '(a b c)
|   :object-list (list 
|    (eieio-pp-demo "sub"
|     :keyword ':keyword
|     :object     (eieio-pp-demo "subsub"
|       :list '(a b c)
|       )
| 
|     :list '(a b c)
|     )
|  'non-object   (eieio-pp-demo "sub"
|     :keyword ':keyword
|     :object     (eieio-pp-demo "subsub"
|       :list '(a b c)
|       )
| 
|     :list '(a b c)
|     )
|    )
|   )
`----

The demo code is also attached.

I have used this code to save the metadata extracted from all packages
on the Emacsmirror without any problems for several months now.

  Please consider merging this, thanks
  Jonas

>From 290b21ae3c416dc7b4a86ad256ec6cf3f08cc429 Mon Sep 17 00:00:00 2001
From: Jonas Bernoulli <[email-masked]>
Date: Fri, 7 Dec 2012 17:35:34 +0100
Subject: [PATCH] * lisp/emacs-lisp/eieio.el: Prettier object pretty-printing.
 (eieio-override-prin1): Don't quote kewords and booleans.
 (object-write(eieio-default-superclass): Don't put closing parens on   new
 line, avoid needless empty lines, align values that are objects   with the
 slot keyword (instead of beginning on the same line). (eieio-list-prin1):
 Align value with slot keyword, increase   eieio-print-depth before printing
 members of the list.

---
 eieio.el | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/eieio.el b/eieio.el
index 3f7b49b..f106f8c 100644
--- a/eieio.el
+++ b/eieio.el
@@ -2850,28 +2850,39 @@ this object."
 		(v (eieio-oref this (car publa)))
 		)
 	    (unless (or (not i) (equal v (car publd)))
+	      (unless (looking-back "\n")
+		(princ "\n"))
 	      (princ (make-string (* eieio-print-depth 2) ? ))
 	      (princ (symbol-name i))
-	      (princ " ")
 	      (if (car publp)
 		  ;; Use our public printer
-		  (funcall (car publp) v)
+		  (progn
+		    (princ " ")
+		    (funcall (car publp) v))
 		;; Use our generic override prin1 function.
-		(eieio-override-prin1 v))
-	      (princ "\n"))))
+		(if (or (eieio-object-p v)
+			(and (listp v)
+			     (eieio-object-p (car v))))
+		    (princ "\n")
+		  (princ " "))
+		(eieio-override-prin1 v)))))
 	(setq publa (cdr publa) publd (cdr publd)
-	      publp (cdr publp)))
-      (princ (make-string (* eieio-print-depth 2) ? )))
-    (princ ")\n")))
+	      publp (cdr publp))))
+    (princ ")")
+    (when (= eieio-print-depth 0)
+      (princ "\n"))))
 
 (defun eieio-override-prin1 (thing)
   "Perform a `prin1' on THING taking advantage of object knowledge."
   (cond ((eieio-object-p thing)
 	 (object-write thing))
-	((listp thing)
-	 (eieio-list-prin1 thing))
 	((class-p thing)
 	 (princ (class-name thing)))
+	((or (keywordp thing)
+	     (booleanp thing))
+	 (prin1 thing))
+	((listp thing)
+	 (eieio-list-prin1 thing))
 	((symbolp thing)
 	 (princ (concat "'" (symbol-name thing))))
 	(t (prin1 thing))))
@@ -2882,16 +2893,16 @@ this object."
       (progn
 	(princ "'")
 	(prin1 list))
-    (princ "(list ")
-    (if (eieio-object-p (car list)) (princ "\n "))
-    (while list
-      (if (eieio-object-p (car list))
-	  (object-write (car list))
-	(princ "'")
-	(prin1 (car list)))
-      (princ " ")
-      (setq list (cdr list)))
     (princ (make-string (* eieio-print-depth 2) ? ))
+    (princ "(list")
+    (let ((eieio-print-depth (1+ eieio-print-depth)))
+      (while list
+	(princ "\n")
+	(if (eieio-object-p (car list))
+	    (object-write (car list))
+	  (princ (make-string (* eieio-print-depth 2) ? ))
+	  (eieio-override-prin1 (car list)))
+	(setq list (cdr list))))
     (princ ")")))
 
 
-- 
1.8.0.1