From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matthias Meulien Newsgroups: gmane.emacs.bugs Subject: bug#51809: 29.0.50; [PATCH] Support for outline default state in Diff buffers Date: Sat, 13 Nov 2021 19:41:17 +0100 Message-ID: <87a6i7x5iq.fsf@gmail.com> References: <87lf1sw6ji.fsf@gmail.com> <86h7cgdk4v.fsf@mail.linkov.net> <87ee7kvshn.fsf@gmail.com> 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="18839"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: 51809@debbugs.gnu.org To: Juri Linkov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 13 19:42:30 2021 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 1mlxyw-0004kp-FY for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 13 Nov 2021 19:42:30 +0100 Original-Received: from localhost ([::1]:52932 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mlxyv-0006M4-5d for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 13 Nov 2021 13:42:29 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:48686) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlxyU-0006KU-KR for bug-gnu-emacs@gnu.org; Sat, 13 Nov 2021 13:42:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36511) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mlxyU-0006vp-4I for bug-gnu-emacs@gnu.org; Sat, 13 Nov 2021 13:42:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mlxyU-0005Lv-1z for bug-gnu-emacs@gnu.org; Sat, 13 Nov 2021 13:42:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Matthias Meulien Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 13 Nov 2021 18:42:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51809 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 51809-submit@debbugs.gnu.org id=B51809.163682888720527 (code B ref 51809); Sat, 13 Nov 2021 18:42:02 +0000 Original-Received: (at 51809) by debbugs.gnu.org; 13 Nov 2021 18:41:27 +0000 Original-Received: from localhost ([127.0.0.1]:48057 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mlxxv-0005L1-65 for submit@debbugs.gnu.org; Sat, 13 Nov 2021 13:41:27 -0500 Original-Received: from mail-wr1-f49.google.com ([209.85.221.49]:44596) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mlxxs-0005Kg-W2 for 51809@debbugs.gnu.org; Sat, 13 Nov 2021 13:41:25 -0500 Original-Received: by mail-wr1-f49.google.com with SMTP id n29so21661490wra.11 for <51809@debbugs.gnu.org>; Sat, 13 Nov 2021 10:41:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=LuJoc0dVlPOnY+8INI9suDCuwMg/Uz5aQqdD+m8kFdk=; b=fR2vziQCZv4ziwivMDXIFTV4H25nehemRnkryEJ8desLIq7BDx2uw+vAYseqXek9KK rrH2gYloifFvzGs23SGsrTIKG/utiNsgpPJI/U5ZMpzXywuJrPh1sS2wTSELnFKWJyFv rLKha8N+EeSYHElwPEgH1LMAK5r+lwS70EQ/RcszGgiUpDw8rondCgpy2MF4Dj0lTmE2 FA+VX0ulsg9P6DnD46Tg2kPPNHiWocV+Yf4hnFmqw26R2aXhAvkNpZL6/AQyRBEHcvxt GjHRY5IoKcSkntHFsQMkF7WPQebJPsnr2aUHaNzyLXP/qaKdtq8YKRJ5YpM1rz7RSnUM QJAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=LuJoc0dVlPOnY+8INI9suDCuwMg/Uz5aQqdD+m8kFdk=; b=V7m905aBNPItJadsCwng3WD0gohp9pq4D0tIQcL4YhpYQAy91Q/EGqB7wGCrAIE3/X aH8ZAYLKHWCgA6IDtMk4elynTJLmZnAdx8Lu6nGAqc7ixqCqjMKrrq23UI5fJSgdNPxt cUTQidBOh1Fnp06gZmG3swUhRGLR1vMh0qRaXdpF8O3ufzg32336r2anXCi5k70vhP+y IEXOVovDcyyz2hmkpAw6EYrPg1pySzA1y/YHVPN6A+IVPMeR1cZwm39ZHy21xfCq47Bv yXwEZuGR1g0mjkKFxa6ykjSSAnUqFsxROznxB/MoByfIvjCifee6nkiqIsvjAuwWnFeT kdDA== X-Gm-Message-State: AOAM530AV365J6zzlfuUuhECO26uFExkef4xl+mmzVi7IdbecVrfD9TL U2/LxVvonNHM5+0IAZATj7bcdarEu9c= X-Google-Smtp-Source: ABdhPJzQlwpN0p4m85CiNsWjEeB4Cv7zlRt7KVKuMT976WDkeX+GedoDHPX4C62dZ+ypuk5FUjUX5g== X-Received: by 2002:a05:6000:4b:: with SMTP id k11mr29294527wrx.86.1636828879011; Sat, 13 Nov 2021 10:41:19 -0800 (PST) Original-Received: from carbon.localdomain ([2a01:e0a:245:c850:98f5:429a:aa8e:95bb]) by smtp.gmail.com with ESMTPSA id be3sm4840901wmb.1.2021.11.13.10.41.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Nov 2021 10:41:18 -0800 (PST) In-Reply-To: <87ee7kvshn.fsf@gmail.com> (Matthias Meulien's message of "Sat, 13 Nov 2021 19:08:04 +0100") 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" Xref: news.gmane.io gmane.emacs.bugs:219830 Archived-At: --=-=-= Content-Type: text/plain Matthias Meulien writes: >> (...) Could the above hook be replaced with customization of >> diff-outline-default-state? With the attached patch, Outline minor mode is automatically enabled when `diff-outline-default-state' is customized. Tested within "emacs -q". That said, I am not a proficient elisper and my use of the autoload, declare-function, eval-when-compile machinery may not be standard. Sorry for this. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Support-for-outline-default-state-in-Diff-buffers.patch >From 967df41ffc0bcaf74d4b2c8da4be9a354169498b Mon Sep 17 00:00:00 2001 From: Matthias Meulien Date: Sat, 13 Nov 2021 12:08:58 +0100 Subject: [PATCH] Support for outline default state in Diff buffers * lisp/vc/diff-mode.el (diff-outline-default-state): Add custom variable that defines an outline state and apply that state in Diff buffers. --- lisp/vc/diff-mode.el | 95 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index e68aa2257d..8751a9905c 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -50,7 +50,11 @@ ;; ;; - in diff-apply-hunk, strip context in replace-match to better ;; preserve markers and spacing. +;; ;; - Handle `diff -b' output in context->unified. +;; +;; - Support outlining files by name (eg to skip automatically +;; generated files like package-lock.json in Javascript projects). ;;; Code: (eval-when-compile (require 'cl-lib)) @@ -62,6 +66,9 @@ (defvar vc-find-revision-no-save) (defvar add-log-buffer-file-name-function) +(eval-when-compile (require 'outline)) +(autoload 'outline-minor-mode "outline") +(defvar outline-minor-mode) (defgroup diff-mode () "Major mode for viewing/editing diffs." @@ -147,6 +154,27 @@ diff-font-lock-syntax (const :tag "Highlight syntax" t) (const :tag "Allow hunk-based fallback" hunk-also))) +(defcustom diff-outline-default-state nil + "If non-nil, some files or hunk are outlined. +Outlining is performed by Outline minor mode. + +If `hide-body', only file and hunk headings are visible. + +If `size-threshold', files whose hunks cover more than +`diff-file-outline-threshold' lines are outlined." + :version "29.1" + :type '(choice (const :tag "Don't outline " nil) + (const :tag "Outline hunks" hide-body) + (const :tag "Outline files with long hunks" size-threshold) + (function :tag "Custom function"))) + +(defcustom diff-file-outline-threshold 50 + "Number of lines of hunks for a file to be outlined. + +Used by `diff-outline-file-according-to-size'." + :version "29.1" + :type '(natnum :tag "Number of lines")) + (defvar diff-vc-backend nil "The VC backend that created the current Diff buffer, if any.") @@ -1578,7 +1606,8 @@ diff-setup-whitespace (defun diff-setup-buffer-type () "Try to guess the `diff-buffer-type' from content of current Diff mode buffer. -`outline-regexp' is updated accordingly." +`outline-regexp' is updated accordingly and outline default state +applied." (save-excursion (goto-char (point-min)) (setq-local diff-buffer-type @@ -1589,7 +1618,8 @@ diff-setup-buffer-type (setq diff-outline-regexp (concat "\\(^diff --git.*\n\\|" diff-hunk-header-re "\\)")) (setq-local outline-level #'diff--outline-level)) - (setq-local outline-regexp diff-outline-regexp)) + (setq-local outline-regexp diff-outline-regexp) + (diff-outline-apply-default-state)) (defun diff-delete-if-empty () ;; An empty diff file means there's no more diffs to integrate, so we @@ -2143,6 +2173,67 @@ diff-refresh-hunk (delete-file file1) (delete-file file2)))) +(defun diff-outline-apply-default-state () + "Apply the outline state defined by `diff-outline-default-state'. + +When `diff-outline-default-state' is non-nil, Outline minor mode +is enabled." + (when diff-outline-default-state + (when (not outline-minor-mode) + (outline-minor-mode)) + (cond + ((eq diff-outline-default-state 'size-threshold) + (diff-outline-file-according-to-size)) + ((eq diff-outline-default-state 'hide-body) + (outline-hide-body)) + ((when (functionp diff-outline-default-state) + (funcall diff-outline-default-state)))))) + +(defun diff-outline-file-according-to-size () + "Outline file with long hunks. + +A file is outlined when its hunks cover more than +`diff-file-outline-threshold' lines. Does nothing when Outline +minor mode is not enabled. + +Inspired by `outline-hide-sublevels'." + (interactive) + (when outline-minor-mode + (save-excursion + (let* (outline-view-change-hook + (beg (progn + (goto-char (point-min)) + ;; Skip the prelude, if any. + (unless (outline-on-heading-p t) (outline-next-heading)) + (point))) + (end (progn + (goto-char (point-max)) + ;; Keep empty last line, if available. + (if (bolp) (1- (point)) (point))))) + (if (< end beg) + (setq beg (prog1 end (setq end beg)))) + ;; First hide sublevels + (outline-hide-sublevels 1) + ;; Then unhide short subtrees + (outline-map-region + (lambda () + (when (= (funcall outline-level) 1) + (goto-char (match-end 0)) + (let ((overlays (overlays-at (point)))) + (while overlays + (let ((overlay (car overlays))) + (progn + (when (eq (overlay-get overlay 'invisible) 'outline) + (let ((size (count-lines + (overlay-end overlay) + (overlay-start overlay)))) + (goto-char (match-beginning 0)) + (if (< size diff-file-outline-threshold) + (outline-show-subtree) + (outline-show-branches)))) + (setq overlays (cdr overlays)))))))) + beg end))))) + ;;; Fine change highlighting. (defface diff-refine-changed -- 2.30.2 --=-=-= Content-Type: text/plain -- Matthias --=-=-=--