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: Sun, 14 Nov 2021 00:29:13 +0100 Message-ID: <878rxrmy7q.fsf@gmail.com> References: <87lf1sw6ji.fsf@gmail.com> <86h7cgdk4v.fsf@mail.linkov.net> <87ee7kvshn.fsf@gmail.com> <87a6i7x5iq.fsf@gmail.com> <86k0hbam7r.fsf@mail.linkov.net> 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="25680"; 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 Sun Nov 14 00:30:38 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 1mm2Tj-0006UY-Sp for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 14 Nov 2021 00:30:35 +0100 Original-Received: from localhost ([::1]:54814 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mm2Ti-0005WC-9s for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 13 Nov 2021 18:30:34 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:58838) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mm2TE-0005Uo-TI for bug-gnu-emacs@gnu.org; Sat, 13 Nov 2021 18:30:04 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36930) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mm2TE-0004uO-HS for bug-gnu-emacs@gnu.org; Sat, 13 Nov 2021 18:30:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mm2TE-0000dt-7Q for bug-gnu-emacs@gnu.org; Sat, 13 Nov 2021 18:30:04 -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 23:30:04 +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.16368461642329 (code B ref 51809); Sat, 13 Nov 2021 23:30:04 +0000 Original-Received: (at 51809) by debbugs.gnu.org; 13 Nov 2021 23:29:24 +0000 Original-Received: from localhost ([127.0.0.1]:48473 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mm2SZ-0000bV-Me for submit@debbugs.gnu.org; Sat, 13 Nov 2021 18:29:24 -0500 Original-Received: from mail-wr1-f54.google.com ([209.85.221.54]:44721) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mm2SW-0000bH-PH for 51809@debbugs.gnu.org; Sat, 13 Nov 2021 18:29:21 -0500 Original-Received: by mail-wr1-f54.google.com with SMTP id n29so22425490wra.11 for <51809@debbugs.gnu.org>; Sat, 13 Nov 2021 15:29:20 -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=aPw/dq/+E/risRZKWfFRccNB1qFIVEkLoHFTdhBplqU=; b=XS4/sNkTJ0EdvpOh9yJhLhtviSeI1EhVXA50C4q1gBKTMt0H+3XEIIvgEc0fIWn3lk 3bCMZ7qeoi2LADef2+lhlXuEZH2QG97D9J/p5opizLi5VgAaUZiUdBCRCISEjtS0rXa4 EGHxjHciL13Umk8EuaYiuoTCwGWYhU8Yz77XDFPHnPN+mFbwY7svcB71qQ2DE0OdNR1m dWqraEV10m0JVPpo1k+MrqmPSZh3r+zG91WSrxaRRHNs/lF3eTFWXdvgMCrCRYivMllh znvMTZcxaw24683utiW3AQqS4hr+Szs2gPScnPJjzXGUKQh4mdnfink+TGi0zg1zi8u8 CH3w== 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=aPw/dq/+E/risRZKWfFRccNB1qFIVEkLoHFTdhBplqU=; b=xgQVHESqzgMIUfLJqw7WOL2p3ApDnnRWWeB2y3lmqIDerah8rRPL3Mt8DGgf4f3ca9 WMaPUhjfnu2xLGk6kghfVuaNSM68Pa2dLwksjGPyDrl+/YX2AYQujak4+n4Kzdwq+Edj So8LJBZUascOUorxO478vXSTDtgR86R62rQRXFw3KHIiesvu4ZxrWD2/rr/9WVAgIzh0 RBiFGZ1bn7bRxXbx1kCsUefUfo5nXLLTEoBA1leebW8TB5KM0/na43OBgr00y8ltWTWQ ApX+G58Sn7UO9vJmvuAuIwMEA0l/TAQdO7zpnMNCfnXpi/pVo6K19sMzEiVwtMxPTPGL pWkA== X-Gm-Message-State: AOAM533ZZBZeOYOfWmXE2BYxuX5/6M3WkA608JUq62OFS6ZHcBUaEKYy 5ky5LhBpcsOQVkSquyoIgWWn76RjOjk= X-Google-Smtp-Source: ABdhPJxYx7l27Orhquzh25P7XWQDcNdwxugH/AWoJsA2aXX8iPJNrRwoJEdns+niMwqoXPm60B/BSQ== X-Received: by 2002:adf:f708:: with SMTP id r8mr32039536wrp.198.1636846154940; Sat, 13 Nov 2021 15:29:14 -0800 (PST) Original-Received: from carbon.localdomain ([2a01:e0a:245:c850:98f5:429a:aa8e:95bb]) by smtp.gmail.com with ESMTPSA id x1sm9604441wmc.22.2021.11.13.15.29.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Nov 2021 15:29:14 -0800 (PST) In-Reply-To: <86k0hbam7r.fsf@mail.linkov.net> (Juri Linkov's message of "Sat, 13 Nov 2021 21:29:12 +0200") 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:219851 Archived-At: --=-=-= Content-Type: text/plain Updated patch that takes Juri comments into account. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Support-for-outline-default-state-in-Diff-buffers.patch >From 4a9f53e73dcbcd77df339bca012ee72aec343f18 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/outline.el (outline-map-sublevel-overlay): * lisp/vc/diff-mode.el (diff-outline-default-state): Variable that defines an outline state. (diff-outline-apply-default-state) (diff--outline-set-file-heading-visibility): Apply outline state defined in `diff-outline-default-state'. --- lisp/outline.el | 40 ++++++++++++++ lisp/vc/diff-mode.el | 128 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 166 insertions(+), 2 deletions(-) diff --git a/lisp/outline.el b/lisp/outline.el index cefb811703..77de31f785 100644 --- a/lisp/outline.el +++ b/lisp/outline.el @@ -1100,6 +1100,46 @@ outline-hide-sublevels (define-obsolete-function-alias 'hide-sublevels #'outline-hide-sublevels "25.1") +(defun outline-map-sublevel-overlay (level fun) + "Hide everything and call FUN on the LEVEL sublevels of headers . + +When FUN is called, point is at the beginning of the heading, the +match data is set appropriately, and FUN receives one argument, +the outline overlay for the heading entry. + +This also unhides the top heading-less body, if any." + (if (< level 1) + (error "Must keep at least one level of headers")) + (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 everything. + (outline-hide-sublevels level) + ;; Then unhide the top level headers. + (outline-map-region + (lambda () + (when (= (funcall outline-level) level) + (goto-char (match-end 0)) + (let ((overlays (overlays-at (point)))) + (while overlays + (let ((overlay (car overlays))) + (when (eq (overlay-get overlay 'invisible) 'outline) + (goto-char (match-beginning 0)) + (funcall fun overlay)) + (setq overlays (cdr overlays))))))) + beg end))) + (run-hooks 'outline-view-change-hook)) + (defun outline-hide-other () "Hide everything except current body and parent and top-level headings. This also unhides the top heading-less body, if any." diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index e68aa2257d..9617e6ceee 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -50,6 +50,7 @@ ;; ;; - in diff-apply-hunk, strip context in replace-match to better ;; preserve markers and spacing. +;; ;; - Handle `diff -b' output in context->unified. ;;; Code: @@ -62,6 +63,18 @@ (defvar vc-find-revision-no-save) (defvar add-log-buffer-file-name-function) +(eval-when-compile (require 'outline)) +(defvar outline-minor-mode) +(declare-function outline-minor-mode "outline" (&optional args)) +(declare-function outline-hide-entry "outline") +(declare-function outline-show-branches "outline") +(declare-function outline-show-subtree "outline" (&optional event)) + +(eval-when-compile (require 'so-long)) +(autoload 'so-long-detected-long-line-p "so-long") +(defvar so-long-skip-leading-comments) +(defvar so-long-threshold) +(defvar so-long-max-lines) (defgroup diff-mode () "Major mode for viewing/editing diffs." @@ -147,6 +160,61 @@ 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 equal to `outline-hunks', only file and hunk headings are +visibles. + +If equal to a lambda function or function name, this function is +expected to toggle file or hunks visibility, and will be called +after the mode is enabled. + +If equal to a list of symbols, hunk headings will be outlined +depending on the conditions defined for each symbol: + +- If `line-count-threshold', when hunks under a given file + heading cover more than `diff-outline-line-count-threshold' + lines, they are outlined; + +- If `file-heading-regexp', file headings which match the regexp + `diff-outline-file-heading-regexp' are outlined; + +- If `long-line-threshold', when a hunk under a given file + heading have a line with more than + `diff-outline-long-line-threshold' characters, all hunks for + that file heading are outlined." + :version "29.1" + :type '(choice (const :tag "Don't outline " nil) + (const :tag "Outline hunks" outline-hunks) + (set :tag "Outline some files" + (const + :tag "Outline files with long hunks" + line-count-threshold) + (const + :tag "Outline files by name" + file-heading-regexp) + (const + :tag "Outline files whose hunks involve long lines" + long-line-threshold)) + (function :tag "Custom function"))) + +(defcustom diff-outline-line-count-threshold 50 + "Minimal number of lines of hunks for a file to be outlined." + :version "29.1" + :type '(natnum :tag "Number of lines")) + +(defcustom diff-outline-file-heading-regexp "ChangeLog\\|package-lock\\.json" + "Regexp to match file headings to be outlined." + :version "29.1" + :type '(regexp :tag "Files to outline")) + +(defcustom diff-outline-long-line-threshold 1000 + "Minimal number of characters in a line for a file to be outlined." + :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 +1646,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 +1658,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 +2213,60 @@ 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." + (interactive) + (when diff-outline-default-state + (when (not outline-minor-mode) + (outline-minor-mode)) + (cond + ((eq diff-outline-default-state 'outline-hunks) + (outline-hide-body)) + ((listp diff-outline-default-state) + (outline-map-sublevel-overlay + 1 #'diff--outline-set-file-heading-visibility)) + ((when (functionp diff-outline-default-state) + (funcall diff-outline-default-state)))))) + +(defun diff--outline-set-file-heading-visibility (overlay) + (cond + ;; hide entry when file heading match + ;; `diff-outline-file-heading-regexp' + ((and + (memq 'file-heading-regexp + diff-outline-default-state) + (string-match-p + diff-outline-file-heading-regexp + (match-string 0))) + (outline-hide-entry)) + ;; show only branches when line count > threshold + ((and + (memq 'line-count-threshold + diff-outline-default-state) + (let ((line-count (count-lines + (overlay-end overlay) + (overlay-start overlay)))) + (< diff-outline-line-count-threshold line-count))) + (outline-show-branches)) + ;; show only branches when a long line is detected + ((and + (memq 'long-line-threshold + diff-outline-default-state) + (save-restriction + (narrow-to-region (overlay-start overlay) + (overlay-end overlay)) + (let ((so-long-skip-leading-comments nil) + (so-long-threshold + diff-outline-long-line-threshold) + (so-long-max-lines nil)) + (so-long-detected-long-line-p)))) + (outline-show-branches)) + ;; otherwise show subtree + (t (outline-show-subtree)))) + ;;; Fine change highlighting. (defface diff-refine-changed -- 2.30.2 --=-=-= Content-Type: text/plain -- Matthias --=-=-=--