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: Tue, 28 Dec 2021 22:45:37 +0100 Message-ID: <87v8z8v1xa.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> <878rxrmy7q.fsf@gmail.com> <86y256uc0v.fsf@mail.linkov.net> <87r1ajknsr.fsf@gmail.com> <86ilvu6w6k.fsf@mail.linkov.net> <871r2hc51a.fsf@gmail.com> <87r19zs662.fsf@gmail.com> <86zgokpoll.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="15621"; 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 Tue Dec 28 22:46:51 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 1n2KJ0-0003oO-Jp for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 28 Dec 2021 22:46:50 +0100 Original-Received: from localhost ([::1]:57630 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n2KIz-000805-Fq for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 28 Dec 2021 16:46:49 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:42842) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n2KIE-0007z7-R2 for bug-gnu-emacs@gnu.org; Tue, 28 Dec 2021 16:46:04 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36081) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n2KID-0003dT-Rk for bug-gnu-emacs@gnu.org; Tue, 28 Dec 2021 16:46:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n2KID-0005yg-MQ for bug-gnu-emacs@gnu.org; Tue, 28 Dec 2021 16:46:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Matthias Meulien Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 28 Dec 2021 21:46:01 +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.164072794722958 (code B ref 51809); Tue, 28 Dec 2021 21:46:01 +0000 Original-Received: (at 51809) by debbugs.gnu.org; 28 Dec 2021 21:45:47 +0000 Original-Received: from localhost ([127.0.0.1]:47627 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n2KHy-0005yD-Np for submit@debbugs.gnu.org; Tue, 28 Dec 2021 16:45:47 -0500 Original-Received: from mail-wr1-f48.google.com ([209.85.221.48]:44843) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n2KHw-0005y0-Q1 for 51809@debbugs.gnu.org; Tue, 28 Dec 2021 16:45:45 -0500 Original-Received: by mail-wr1-f48.google.com with SMTP id k18so3918406wrg.11 for <51809@debbugs.gnu.org>; Tue, 28 Dec 2021 13:45:44 -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=gdcoaAJlJe61MxWe6/P2oQhuVBFVxmnEgfoyzaVxCFQ=; b=mqmAGsOkGXBwso1HwH6VM1qnK5Ctuzjkz5mIZcbRC9oLSKUGx347auQNPDzw4CGLCP 9ltW1kIfFz/XZqtPNIJdhXhKjEM/GbE68nN3GdH1AUfdLg1hGSq/i6D82V2tsC5qRJGk aA1oIFS6GJFQH+VYHKnAUsU+9I0/dfS6HrzFQCuAtd6p4ZU5iNTLswp/TOvfa8CPk0ZT Fz7t38OtP+saP/EAPLOVHWOdfHId28PSCdLXaRPGfJHqbLjTo4eX3y9/Prqbx2UhBcPg Qs7QVf55Xqg+VeQCZd3xDe13fY6bT7t13+AJB8ERTKSqtne6rAVlOa+j3czGd2DIyBhs eJlw== 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=gdcoaAJlJe61MxWe6/P2oQhuVBFVxmnEgfoyzaVxCFQ=; b=OaX6Du8CaJiskASMbyzPKFK9hSYEEtikVb6L6GDFiX+RazWson1mKPWmid50T/5a8S MbCtvHAfzgnBkEnYTuFYtDJYvVMiObIY3MyZjJ2JZgkrYz7QkCar4kc6SmKLo78X2ids x4mU2z+6HpsBlTsX54O19vv43cU0ZAn72yvUIBPWls1YrYb53aO9SQ/H+9ErWfF6hdT3 /8SNBuhaM0glmu06P67Mym5HcPa3l4RSGlEszmLFqiiGWI8kA1ww8QdiIaGLDi82gnxP AaTT+CbpmK9Y5bZnbeug4h5efQLvlbFsKRymBDAJFWvy7Jx0sYFa2d03e5r6hcNQ5dbd PB3Q== X-Gm-Message-State: AOAM532HDBjMhIbb9vRDqjjZsmE0ukEAWDlkU2+z7QWASm+5Cu0jxAUL bWGTPsnukcdXaT1No5Yo5WyOZhePn+g= X-Google-Smtp-Source: ABdhPJwx2cI6J/1w9i3o0w81llGYfAgW1bWLsvsg8cQ2vBuu+gnE+v5FqjnQJkdxV2ceUqwa10+cAA== X-Received: by 2002:adf:f907:: with SMTP id b7mr17822161wrr.5.1640727938747; Tue, 28 Dec 2021 13:45:38 -0800 (PST) Original-Received: from carbon.localdomain ([2a01:e0a:245:c850:98f5:429a:aa8e:95bb]) by smtp.gmail.com with ESMTPSA id k19sm21669773wmo.29.2021.12.28.13.45.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Dec 2021 13:45:37 -0800 (PST) In-Reply-To: <86zgokpoll.fsf@mail.linkov.net> (Juri Linkov's message of "Tue, 28 Dec 2021 20:32:22 +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:223292 Archived-At: --=-=-= Content-Type: text/plain Juri Linkov writes: >> @@ -353,7 +353,9 @@ outline-mode >> + (add-hook 'change-major-mode-hook #'outline-show-all nil t) >> + (add-hook 'hack-local-variables-hook >> + #'outline-apply-default-state)) >> >> @@ -436,7 +438,9 @@ outline-minor-mode >> + (add-hook 'hack-local-variables-hook >> + #'outline-apply-default-state)) > > Are you sure about modifying the global value of hack-local-variables-hook > instead of the buffer-local hook with `nil t' at the end? Ah, ah, my bad. Many thanks! > (...) When this feature will be used a lot, even variable names will > affect usability - with longer names usability deteriorates, and 40 > characters of 'outline-default-state-subtree-visibility' takes half of > the standard window width. Would it be possible to find a shorter > name? Good point. > Since it defines the rules, how about 'outline-default-rules'? LGTM. > (...) Also outline-long-line-threshold and > outline-line-count-threshold could share the same prefix, maybe: > > # outline-default-long-line: 200 > # outline-default-line-count: 100 Adopted! --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Extend-Outline-mode-with-default-visibility-state.patch >From 6f8fb8b142c913405d9e00732ffbdecb8331ddb7 Mon Sep 17 00:00:00 2001 From: Matthias Meulien Date: Wed, 8 Dec 2021 22:35:42 +0100 Subject: [PATCH] Extend Outline mode with default visibility state * etc/NEWS: Announce support for default visibility state. * lisp/outline.el (outline-mode, outline-minor-mode): Ensure default visibility state is applied. (outline-hide-sublevels): Add optional argument for function to call on each heading. (outline-default-state): Define the default visibility state. (outline-apply-default-state): Apply default visibility state. --- etc/NEWS | 10 +++ lisp/outline.el | 181 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 188 insertions(+), 3 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index cfea513cca..9a49ff8379 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -215,6 +215,16 @@ These will take you (respectively) to the next and previous "page". --- *** 'describe-char' now also outputs the name of emoji combinations. +** Outline Mode + +*** Support for a default visibility state. +Customize the option 'outline-default-state' to define what headings +are visible when the mode is set. When equal to a number, the option +'outline-default-state-subtree-visibility' determines the visibility +of the subtree starting at the corresponding level. Values are +provided to show a heading subtree unless the heading match a regexp, +or its subtree has long lines or is long. + ** Outline Minor Mode +++ diff --git a/lisp/outline.el b/lisp/outline.el index 5e3d4e0e00..1a878dee04 100644 --- a/lisp/outline.el +++ b/lisp/outline.el @@ -354,7 +354,8 @@ outline-mode '(outline-font-lock-keywords t nil nil backward-paragraph)) (setq-local imenu-generic-expression (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0))) - (add-hook 'change-major-mode-hook #'outline-show-all nil t)) + (add-hook 'change-major-mode-hook #'outline-show-all nil t) + (add-hook 'hack-local-variables-hook #'outline-apply-default-state nil t)) (defvar outline-minor-mode-map) @@ -437,7 +438,9 @@ outline-minor-mode nil t) (setq-local line-move-ignore-invisible t) ;; Cause use of ellipses for invisible text. - (add-to-invisibility-spec '(outline . t))) + (add-to-invisibility-spec '(outline . t)) + (add-hook 'hack-local-variables-hook + #'outline-apply-default-state nil t)) (when (or outline-minor-mode-cycle outline-minor-mode-highlight) (if font-lock-fontified (font-lock-remove-keywords nil outline-font-lock-keywords)) @@ -1094,7 +1097,7 @@ outline-hide-sublevels (outline-map-region (lambda () (if (<= (funcall outline-level) levels) - (outline-show-heading))) + (outline-show-heading))) beg end) ;; Finally unhide any trailing newline. (goto-char (point-max)) @@ -1308,6 +1311,178 @@ outline-headers-as-kill (insert "\n\n")))))) (kill-new (buffer-string))))))) +(defcustom outline-default-state nil + "If non-nil, some headings are initially outlined. + +Note that the default state is applied when the major mode is set +or when the command `outline-apply-default-state' is called +interactively. + +When nil, headings visibility is left unchanged. + +If equal to `outline-show-all', all text of buffer is shown. + +If equal to `outline-show-only-headings', only headings are shown. + +If equal to a number, show only headings up to and including the +corresponding level. See `outline-default-rules' to customize +visibility of the subtree at the choosen level. + +If equal to a lambda function or function name, this function is +expected to toggle headings visibility, and will be called after +the mode is enabled." + :version "29.1" + :type '(choice (const :tag "Disabled" nil) + (const :tag "Show all" outline-show-all) + (const :tag "Only headings" outline-show-only-headings) + (natnum :tag "Show headings up to level" :value 1) + (function :tag "Custom function"))) + +(defcustom outline-default-rules nil + "Determines visibility of subtree starting at `outline-default-state' level. + +When nil, the subtree is hidden unconditionally. + +When equal to a list, each element should be one of the following: + +- A cons cell with CAR `match-regexp' and CDR a regexp, the + subtree will be hidden when the outline heading match the + regexp. + +- `subtree-has-long-lines' to only show the heading branches when + long lines are detected in its subtree (see + `outline-default-long-line' for the definition of long lines). + +- `subtree-is-long' to only show the heading branches when its + subtree contains more than `outline-default-line-count' lines. + +- A lambda function or function name which will be evaluated with + point at the beginning of the heading and the match data set + appropriately, the function being expected to toggle the + heading visibility." + :version "29.1" + :type '(choice (const :tag "Hide subtree" nil) + (set :tag "Show subtree unless" + (cons :tag "Heading match regexp" + (const match-regexp) string) + (const :tag "Subtree has long lines" + subtree-has-long-lines) + (const :tag "Subtree is long" + subtree-is-long) + (cons :tag "Custom function" + (const custom-function) function)))) + +(defcustom outline-default-long-line 1000 + "Minimal number of characters in a line for a heading to be outlined." + :version "29.1" + :type '(natnum :tag "Number of characters")) + +(defcustom outline-default-line-count 50 + "Minimal number of lines for a heading to be outlined." + :version "29.1" + :type '(natnum :tag "Number of lines")) + +(defun outline-apply-default-state () + "Apply the outline state defined by `outline-default-state'." + (interactive) + (cond + ((integerp outline-default-state) + (outline--show-headings-up-to-level outline-default-state)) + ((when (functionp outline-default-state) + (funcall outline-default-state))))) + +(defun outline-show-only-headings () + "Show only headings." + (interactive) + (outline-show-all) + (outline-hide-region-body (point-min) (point-max))) + +(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) + +(defun outline--show-headings-up-to-level (level) + "Show only headings up to a LEVEL level. + +Like `outline-hide-sublevels' but, for each heading at level +LEVEL, decides of subtree visibility according to +`outline-default-rules'." + (if (not outline-default-rules) + (outline-hide-sublevels level) + (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)))) + (heading-regexp + (cdr-safe + (assoc 'match-regexp outline-default-rules))) + (check-line-count + (memq 'subtree-is-long outline-default-rules)) + (check-long-lines + (memq 'subtree-has-long-lines outline-default-rules)) + (custom-function + (cdr-safe + (assoc 'custom-function outline-default-rules)))) + (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 () + (let ((current-level (funcall outline-level))) + (when (< current-level level) + (outline-show-heading) + (outline-show-entry)) + (when (= current-level level) + (cond + ((and heading-regexp + (let ((beg (point)) + (end (progn (outline-end-of-heading) (point)))) + (string-match-p heading-regexp (buffer-substring beg end)))) + ;; hide entry when heading match regexp + (outline-hide-entry)) + ((and check-line-count + (save-excursion + (let ((beg (point)) + (end (progn (outline-end-of-subtree) (point)))) + (<= outline-default-line-count (count-lines beg end))))) + ;; show only branches when line count of subtree > + ;; threshold + (outline-show-branches)) + ((and check-long-lines + (save-excursion + (let ((beg (point)) + (end (progn (outline-end-of-subtree) (point)))) + (save-restriction + (narrow-to-region beg end) + (let ((so-long-skip-leading-comments nil) + (so-long-threshold outline-default-long-line) + (so-long-max-lines nil)) + (so-long-detected-long-line-p)))))) + ;; show only branches when long lines are detected + ;; in subtree + (outline-show-branches)) + (custom-function + ;; call custom function if defined + (funcall custom-function)) + (t + ;; if no previous clause succeeds, show subtree + (outline-show-subtree)))))) + beg end))) + (run-hooks 'outline-view-change-hook))) + (defun outline--cycle-state () "Return the cycle state of current heading. Return either 'hide-all, 'headings-only, or 'show-all." -- 2.30.2 --=-=-= Content-Type: text/plain -- Matthias --=-=-=--