From: Stefan Monnier Date: Mon, 26 Aug 2024 15:26:11 +0000 (-0400) Subject: (pp): Indent lines right when starting in col > 0 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=ae48a8a388fa9bbda206e67b6c1b03c8990a92df;p=emacs.git (pp): Indent lines right when starting in col > 0 This refines the fix for bug#72561: commit 0a500193087e fixes the bug by changing `ert--pp-with-indentation-and-newline`, but it turns out that `pp` was inconsistent (it sometimes indented the subsequent lines correctly and sometimes not, depending on the current-buffer's major mode). So the fix really should be in `pp`, which is what this patch does. * lisp/emacs-lisp/pp.el (pp): Appropriately indent subsequent lines if the first line is not inserted in column 0 (tho only when printing into a buffer since otherwise it's somewhere between ill-defined and impossible to implement). * lisp/emacs-lisp/ert.el (ert--pp-with-indentation-and-newline): Remove the indentation code after `pp` which was just working around the bug in `pp`. Also remove the redundant addition of `\n` since `pp` always does it nowadays. * test/lisp/help-mode-tests.el (help-mode-tests-xref-on-pp): Fix thinko. (cherry picked from commit d3311499339fab0371cb7502a1e2481fbcd2c65d) --- diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el index 9ec23aec92e..686a3c5a65c 100644 --- a/lisp/emacs-lisp/ert.el +++ b/lisp/emacs-lisp/ert.el @@ -1316,13 +1316,9 @@ empty string." (defun ert--pp-with-indentation-and-newline (object) "Pretty-print OBJECT, indenting it to the current column of point. Ensures a final newline is inserted." - (let ((begin (point)) - (cols (current-column)) - (pp-escape-newlines t) + (let ((pp-escape-newlines t) (print-escape-control-characters t)) - (pp object (current-buffer)) - (unless (bolp) (insert "\n")) - (indent-rigidly begin (point) cols))) + (pp object (current-buffer)))) (defun ert--insert-infos (result) "Insert `ert-info' infos from RESULT into current buffer. diff --git a/lisp/emacs-lisp/pp.el b/lisp/emacs-lisp/pp.el index 7411e07b978..9323415f733 100644 --- a/lisp/emacs-lisp/pp.el +++ b/lisp/emacs-lisp/pp.el @@ -308,17 +308,24 @@ can handle, whenever this is possible. Uses the pretty-printing code specified in `pp-default-function'. Output stream is STREAM, or value of `standard-output' (which see)." - (cond - ((and (eq (or stream standard-output) (current-buffer)) - ;; Make sure the current buffer is setup sanely. - (eq (syntax-table) emacs-lisp-mode-syntax-table) - (eq indent-line-function #'lisp-indent-line)) - ;; Skip the buffer->string->buffer middle man. - (funcall pp-default-function object) - ;; Preserve old behavior of (usually) finishing with a newline. - (unless (bolp) (insert "\n"))) - (t - (princ (pp-to-string object) (or stream standard-output))))) + (let ((stream (or stream standard-output))) + (cond + ((and (eq stream (current-buffer)) + ;; Make sure the current buffer is setup sanely. + (eq (syntax-table) emacs-lisp-mode-syntax-table) + (eq indent-line-function #'lisp-indent-line)) + ;; Skip the buffer->string->buffer middle man. + (funcall pp-default-function object) + ;; Preserve old behavior of (usually) finishing with a newline. + (unless (bolp) (insert "\n"))) + (t + (save-current-buffer + (when (bufferp stream) (set-buffer stream)) + (let ((begin (point)) + (cols (current-column))) + (princ (pp-to-string object) (or stream standard-output)) + (when (and (> cols 0) (bufferp stream)) + (indent-rigidly begin (point) cols)))))))) ;;;###autoload (defun pp-display-expression (expression out-buffer-name &optional lisp) diff --git a/test/lisp/help-mode-tests.el b/test/lisp/help-mode-tests.el index 4c149ae7430..2e64b12732e 100644 --- a/test/lisp/help-mode-tests.el +++ b/test/lisp/help-mode-tests.el @@ -92,7 +92,7 @@ Lisp concepts such as car, cdr, cons cell and list.") (ert-deftest help-mode-tests-xref-on-pp () (with-temp-buffer - (insert (pp '(cons fill-column))) + (pp '(cons fill-column) (current-buffer)) (help-xref-on-pp (point-min) (point-max)) (goto-char (point-min)) (search-forward "co")