emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shouldn't emacs print long lists with newlines?


From: Stefan Monnier
Subject: Re: Shouldn't emacs print long lists with newlines?
Date: Wed, 07 Aug 2019 06:51:25 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

>> Shouldn't the printing code be smarter and print long lists with
>> newlines, every item in a separate line, to avoid the long line
>> issue?

[ My minibuffer is exactly 1-line tall (and it's a separate frame, so
  Emacs is not allowed to grow it), so spreading the output on several
  lines is not always a good solution.  ]

> If it should depends on the use case.  It should be able to do so when
> the user wants that - especially pretty printing (pp).  So far this has
> been discussed here and there, but so far nobody has implemented it.

I don't think Edebug tries to pretty print this, currently.

But if we want to improve the pretty printer to fold such "parenthesis
deserts", we can try a patch like the one below,


        Stefan


diff --git a/lisp/emacs-lisp/pp.el b/lisp/emacs-lisp/pp.el
index de4cbfc0e1..21de326421 100644
--- a/lisp/emacs-lisp/pp.el
+++ b/lisp/emacs-lisp/pp.el
@@ -50,6 +50,33 @@ pp-to-string
     (pp-buffer)
     (buffer-string)))
 
+(defun pp--long-line ()
+  "Cut the line before point and before `fill-column' if needed."
+  ;; FIXME: This still leaves lines longer than fill-column because
+  ;; When pp-buffer calls us, the text is flush-left rather than
+  ;; properly indented, so (current-column) is not the real column at
+  ;; which the text will end up.
+  (let ((end (point)))
+    (when (> (current-column) fill-column)
+      (forward-line 0)
+      (let ((last nil))
+        (while (progn
+                 (skip-syntax-forward "^\"< \\")
+                 (skip-syntax-forward " ")
+                 (and (< (point) end)
+                      (or (< (current-column) fill-column)
+                          (null last))))
+          (setq last (point))
+          (pcase (char-syntax (char-after))
+            (?\\ (forward-char 2))
+            (?\" (forward-sexp 1))
+            (?\< (end-of-line))))
+        (when last
+          (goto-char last)
+          (skip-syntax-backward " ")
+          (delete-region (point) last)
+          (insert "\n"))))))
+
 ;;;###autoload
 (defun pp-buffer ()
   "Prettify the current buffer with printed representation of a Lisp object."
@@ -58,6 +85,7 @@ pp-buffer
     ;; (message "%06d" (- (point-max) (point)))
     (cond
      ((ignore-errors (down-list 1) t)
+      (pp--long-line)
       (save-excursion
         (backward-char 1)
         (skip-chars-backward "'`#^")
@@ -67,6 +95,7 @@ pp-buffer
            (progn (skip-chars-backward " \t\n") (point)))
           (insert "\n"))))
      ((ignore-errors (up-list 1) t)
+      (pp--long-line)
       (skip-syntax-forward ")")
       (delete-region
        (point)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]