From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#63861: [PATCH] pp.el: New "pretty printing" code Date: Fri, 02 Jun 2023 18:50:57 -0400 Message-ID: Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26452"; mail-complaints-to="usenet@ciao.gmane.io" To: 63861@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jun 03 00:52:23 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q5Dd9-0006gB-1F for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 03 Jun 2023 00:52:23 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q5Dcr-0002EM-2U; Fri, 02 Jun 2023 18:52:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q5Dcp-0002E8-5s for bug-gnu-emacs@gnu.org; Fri, 02 Jun 2023 18:52:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q5Dco-0007Tl-UI for bug-gnu-emacs@gnu.org; Fri, 02 Jun 2023 18:52:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1q5Dco-0002G1-CA for bug-gnu-emacs@gnu.org; Fri, 02 Jun 2023 18:52:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 02 Jun 2023 22:52:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 63861 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16857462838625 (code B ref -1); Fri, 02 Jun 2023 22:52:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 2 Jun 2023 22:51:23 +0000 Original-Received: from localhost ([127.0.0.1]:40996 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q5DcA-0002F3-5Q for submit@debbugs.gnu.org; Fri, 02 Jun 2023 18:51:22 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:58746) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q5Dc5-0002Eq-CE for submit@debbugs.gnu.org; Fri, 02 Jun 2023 18:51:20 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q5Dc5-00029d-55 for bug-gnu-emacs@gnu.org; Fri, 02 Jun 2023 18:51:17 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q5Dc2-0007EX-Bz for bug-gnu-emacs@gnu.org; Fri, 02 Jun 2023 18:51:16 -0400 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 6325F44109D for ; Fri, 2 Jun 2023 18:51:12 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 7FDD344105D for ; Fri, 2 Jun 2023 18:51:09 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1685746269; bh=sxdHbJ65dSKDWDZ9YOQE6JJoMBCokSFgb3m5fwGXwLs=; h=From:To:Subject:Date:From; b=cG1aK65yftgiUQY3aRCs3/YKB9MlnGxHl7Ao5NtEixlKOc7cPUYIlzbMnWrTe+UxV HQ+iHOMGH4ZcHa8fXnvGXFiUNJxh3CjKVSvqoXOtJVySfmj+Qr9wKnEjXiXInOqBs8 EP53ej/l8q7EVVEJaeOTNavw0g/w7pl3p6pPyL+rZoVbrCLMCluP3Wk1sR2Ref6XuV woc1iy3AeF+4Mo95GOv7yRcxGMQnVV+XUgGUy/pJlWMAI5H1hsK9jtATezJ1d5HNX/ O5/6/7hNIXrCi6ORhMf6PZUsUSyAaR09ae70GxIKmOq8vnaxqPCVoJf2k7zwUhLWID 43X7a2BfvHYHw== Original-Received: from pastel (76-10-180-239.dsl.teksavvy.com [76.10.180.239]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 65A291202BC for ; Fri, 2 Jun 2023 18:51:09 -0400 (EDT) Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:262787 Archived-At: --=-=-= Content-Type: text/plain Tags: patch I've often been annoyed by the way `ielm` "pretty prints" data, so I wrote my own pretty printer, which has evolved over the years. I believe it has finally reached a state which may be acceptable to more than just myself. The new code is in a new function `pp-region`. The old code redirects to the new code if `pp-buffer-use-pp-region` is non-nil, tho I'm not sure we want to bother users with such a config var. Hopefully, the new code should be good enough that users don't need to choose. Maybe I should make it a `defvar` and have it default to t, so new users will complain if it's not good enough? Stefan In GNU Emacs 30.0.50 (build 2, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0, Xaw3d scroll bars) of 2023-05-24 built on pastel Repository revision: 41b6b907d4cf2f8c302647dc63c5d9c94f9f01d6 Repository branch: work Windowing system distributor 'The X.Org Foundation', version 11.0.12011000 System Description: Debian GNU/Linux 11 (bullseye) Configured using: 'configure -C --enable-checking --enable-check-lisp-object-type --with-modules --with-cairo --with-tiff=ifavailable 'CFLAGS=-Wall -g3 -Og -Wno-pointer-sign' PKG_CONFIG_PATH=/home/monnier/lib/pkgconfig' --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-pp.el-New-pretty-printing-code.patch >From f26455b6b36f02fcb0954d5a440e8e97a0070b26 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Fri, 2 Jun 2023 18:42:55 -0400 Subject: [PATCH] pp.el: New "pretty printing" code Provide a new pretty printing code which tries to generate code a bit more like what humans tend to write. The new code is driven by the assumption that we want the output to fit within `fill-column` (which clearly works poorly with deeply indented code which just can't fit within `fill-column`). * lisp/emacs-lisp/pp.el (pp--within-fill-column-p, pp-region): New funs. (pp-buffer-use-pp-region): New custom var. (pp-buffer): Use it. --- lisp/emacs-lisp/pp.el | 94 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/lisp/emacs-lisp/pp.el b/lisp/emacs-lisp/pp.el index e6e3cd6c6f4..3f9f4a5fbba 100644 --- a/lisp/emacs-lisp/pp.el +++ b/lisp/emacs-lisp/pp.el @@ -74,11 +74,103 @@ pp-to-string (pp-buffer) (buffer-string)))) +(defun pp--within-fill-column-p () + "Return non-nil if point is within `fill-column'." + ;; Try and make it O(fill-column) rather than O(current-column), + ;; so as to avoid major slowdowns on long lines. + ;; FIXME: This doesn't account for invisible text or `display' properties :-( + (and (save-excursion + (re-search-backward + "^\\|\n" (max (point-min) (- (point) fill-column)) t)) + (<= (current-column) fill-column))) + +(defun pp-region (beg end) + "Insert newlines in BEG..END to try and fit within `fill-column'. +Presumes the current buffer contains Lisp code and has indentation properly +configured for that. +Designed under the assumption that the region occupies a single line, +tho it should also work if that's not the case." + (interactive "r") + (goto-char beg) + (let ((end (copy-marker end t))) + (while (< (point) end) + (forward-comment (point-max)) + (let ((beg (point)) + ;; Whether we're in front of an element with paired delimiters. + ;; Can be something funky like #'(lambda ..) or ,'#s(...). + (paired (when (looking-at "['`,#]*[[:alpha:]]*\\([({[\"]\\)") + (match-beginning 1)))) + ;; Go to the end of the sexp. + (goto-char (or (scan-sexps (or paired (point)) 1) end)) + (unless + (and + ;; The sexp is all on a single line. + (save-excursion (not (search-backward "\n" beg t))) + ;; And its end is within `fill-column'. + (or (pp--within-fill-column-p) + ;; If the end of the sexp is beyond `fill-column', + ;; try to move the sexp to its own line. + (and + (save-excursion + (goto-char beg) + (if (save-excursion (skip-chars-backward " \t({[',") (bolp)) + ;; The sexp was already on its own line. + nil + (skip-chars-backward " \t") + (setq beg (copy-marker beg t)) + (if paired (setq paired (copy-marker paired t))) + ;; We could try to undo this insertion if it + ;; doesn't reduce the indentation depth, but I'm + ;; not sure it's worth the trouble. + (insert "\n") (indent-according-to-mode) + t)) + ;; Check again if we moved the whole exp to a new line. + (pp--within-fill-column-p)))) + ;; The sexp is spread over several lines, and/or its end is + ;; (still) beyond `fill-column'. + (when (and paired (not (eq ?\" (char-after paired)))) + ;; The sexp has sub-parts, so let's try and spread + ;; them over several lines. + (save-excursion + (goto-char beg) + (when (looking-at "(\\([^][()\" \t\n]+\\)") + ;; Inside an expression of the form (SYM ARG1 + ;; ARG2 ... ARGn) where SYM has a `lisp-indent-function' + ;; property that's a number, insert a newline after + ;; the corresponding ARGi, because it tends to lead to + ;; more natural and less indented code. + (let* ((sym (intern-soft (match-string 1))) + (lif (and sym (get sym 'lisp-indent-function)))) + (when (natnump lif) + (goto-char (match-end 0)) + (forward-sexp lif) + (insert "\n") + (indent-according-to-mode))))) + (save-excursion + (pp-region (1+ paired) (1- (point))))) + ;; Now the sexp either ends beyond `fill-column' or is + ;; spread over several lines (or both). Either way, the rest of the + ;; line should be moved to its own line. + (skip-chars-forward ")]}") + (unless (eolp) (insert "\n") (indent-according-to-mode))))))) + +(defcustom pp-buffer-use-pp-region nil + "If non-nil, `pp-buffer' uses the new `pp-region' code." + :type 'boolean) + ;;;###autoload (defun pp-buffer () "Prettify the current buffer with printed representation of a Lisp object." (interactive) (goto-char (point-min)) + (if pp-buffer-use-pp-region + (with-syntax-table emacs-lisp-mode-syntax-table + (let ((fill-column (max fill-column 70)) + (indent-line-function + (if (local-variable-p 'indent-line-function) + indent-line-function + #'lisp-indent-line))) + (pp-region (point-min) (point-max)))) (while (not (eobp)) (cond ((ignore-errors (down-list 1) t) @@ -98,7 +190,7 @@ pp-buffer (insert ?\n)) (t (goto-char (point-max))))) (goto-char (point-min)) - (indent-sexp)) + (indent-sexp))) ;;;###autoload (defun pp (object &optional stream) -- 2.30.2 --=-=-=--