]> git.eshelyaron.com Git - emacs.git/commitdiff
pp.el: Try and fix bug#70039
authorStefan Monnier <monnier@iro.umontreal.ca>
Thu, 28 Mar 2024 19:31:04 +0000 (15:31 -0400)
committerEshel Yaron <me@eshelyaron.com>
Fri, 29 Mar 2024 07:34:20 +0000 (08:34 +0100)
* lisp/emacs-lisp/pp.el (pp-fill): Avoid splitting `#N#` or `#[`.
* test/lisp/emacs-lisp/pp-tests.el (pp-tests--sanity): New test.

(cherry picked from commit 4cee95815b9d7d56f6f77abb1cc17e346c038685)

lisp/emacs-lisp/pp.el
test/lisp/emacs-lisp/pp-tests.el

index 26c77d6b047658aeebc415b743b81221a2902da8..d586fc59939091d355bb3409eb18f8e7db94c849 100644 (file)
@@ -166,12 +166,19 @@ it inserts and pretty-prints that arg at point."
   (interactive "r")
   (if (null end) (pp--object beg #'pp-fill)
     (goto-char beg)
-    (let ((end (copy-marker end t))
-          (newline (lambda ()
-                     (skip-chars-forward ")]}")
-                     (unless (save-excursion (skip-chars-forward " \t") (eolp))
-                       (insert "\n")
-                       (indent-according-to-mode)))))
+    (let* ((end (copy-marker end t))
+           (avoid-unbreakable
+            (lambda ()
+              (and (memq (char-before) '(?# ?s ?f))
+                   (memq (char-after) '(?\[ ?\())
+                   (looking-back "#[sf]?" (- (point) 2))
+                   (goto-char (match-beginning 0)))))
+           (newline (lambda ()
+                      (skip-chars-forward ")]}")
+                      (unless (save-excursion (skip-chars-forward " \t") (eolp))
+                        (funcall avoid-unbreakable)
+                        (insert "\n")
+                        (indent-according-to-mode)))))
       (while (progn (forward-comment (point-max))
                     (< (point) end))
         (let ((beg (point))
@@ -198,10 +205,10 @@ it inserts and pretty-prints that arg at point."
                       ;; reduce the indentation depth.
                       ;; Similarly, we prefer to cut before a "." than after
                       ;; it because it reduces the indentation depth.
-                      (while (not (zerop (skip-chars-backward " \t({[',.")))
-                        (and (memq (char-before) '(?# ?s ?f))
-                             (looking-back "#[sf]?" (- (point) 2))
-                             (goto-char (match-beginning 0))))
+                      (while
+                          (progn
+                            (funcall avoid-unbreakable)
+                            (not (zerop (skip-chars-backward " \t({[',.")))))
                       (if (bolp)
                           ;; The sexp already starts on its own line.
                           (progn (goto-char beg) nil)
index 7f7c798cde886635c8c8eff1f3212132d58920b9..7606183d64522cae13a30bb80a9fef1c2ab091a2 100644 (file)
     (while (search-forward "." nil t)
       (should (not (eolp))))))
 
+(ert-deftest pp-tests--sanity ()
+  (with-temp-buffer
+    (lisp-data-mode)
+    (let ((testdata "(a b c #1=#[0 \"\" [] 0] #s(foo #1# bar))"))
+      (let ((res (car (read-from-string testdata))))
+      (dotimes (i (length testdata))
+        (erase-buffer)
+        (insert testdata)
+        (let ((fill-column i))
+          (pp-fill (point-min) (point-max))
+          (goto-char (point-min))
+          (condition-case err
+              (should (equal (read (current-buffer)) res))
+            (invalid-read-syntax
+             (message "Invalid fill result with i=%d:\n%s"
+                      i (buffer-string))
+             (signal (car err) (cdr err))
+             ))))))))
+
 ;;; pp-tests.el ends here.