From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Philip K." Newsgroups: gmane.emacs.bugs Subject: bug#42386: Acknowledgement ([PATCH] Handle symbols in project-kill-buffers-ignores) Date: Sat, 18 Jul 2020 14:48:43 +0200 Message-ID: <87mu3xdm50.fsf@warpmail.net> References: <878sfjkh8o.fsf@warpmail.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="33991"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 42386@debbugs.gnu.org To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jul 18 14:49:11 2020 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 1jwmH8-0008iD-4j for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Jul 2020 14:49:11 +0200 Original-Received: from localhost ([::1]:50634 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jwmH6-0007wG-SD for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Jul 2020 08:49:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60166) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jwmH0-0007w9-Co for bug-gnu-emacs@gnu.org; Sat, 18 Jul 2020 08:49:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:46958) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jwmH0-0000Ij-46 for bug-gnu-emacs@gnu.org; Sat, 18 Jul 2020 08:49:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jwmH0-0007r5-2D for bug-gnu-emacs@gnu.org; Sat, 18 Jul 2020 08:49:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: "Philip K." Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 18 Jul 2020 12:49:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 42386 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 42386-submit@debbugs.gnu.org id=B42386.159507653830185 (code B ref 42386); Sat, 18 Jul 2020 12:49:02 +0000 Original-Received: (at 42386) by debbugs.gnu.org; 18 Jul 2020 12:48:58 +0000 Original-Received: from localhost ([127.0.0.1]:58504 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jwmGv-0007qn-Hh for submit@debbugs.gnu.org; Sat, 18 Jul 2020 08:48:58 -0400 Original-Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:57667) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jwmGr-0007qY-U9 for 42386@debbugs.gnu.org; Sat, 18 Jul 2020 08:48:55 -0400 Original-Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id EC01D554; Sat, 18 Jul 2020 08:48:47 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Sat, 18 Jul 2020 08:48:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=warpmail.net; h= from:to:cc:subject:in-reply-to:date:message-id:mime-version :content-type; s=fm1; bh=ntK8XJn0Sy/pJbhBE8RfRA982u7iQbDkZZ2AK8C M0XY=; b=gfXR07jKRcvSdv9JdeCgShd45Gl3gVcQxU16yMk7eLHmfXz+QSJ6/cG iXHht/i7+xQWkg7ieYF+Qto67E6Xk96b2iqbj6yp5M3rtDfuy/5EQKVnAlHskgWf sVpypwiaZyx7+p6R4twIzhJeZTOfMv4EcWDL8dSZgKfnvRYtuOfaYKoeYr3zZqFK u5r8UGcBPfK/4dgzRmCDCVaES8TBHH5/htmqVlmyZdiBUmCO0JJIu+Gd0v5ZUYgN jV+ZCkTl4dhpERFjVxUWt/P9Fcglnv6OU4TpZgPEkeRWgj4YETARzSvS3gJE0h72 p2T2P0bNxcbZKNFM9JlwNi3GDT2tD8g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=ntK8XJn0Sy/pJbhBE 8RfRA982u7iQbDkZZ2AK8CM0XY=; b=LW9zWlVlKTMWVlIqLy09uenyjb5gDl1Cd 2d3XCA8WYXKJhQ739FcD4UJFvYj1o0NKwKSGfaKzjmnHEQFJ249N+3ea0AY3oj6b mK+60IrAg8ntfiXscT/+6Z+e0Mf5IVa/hRJPFWjandVRWCIuA9Elea+iYP4onzBl UXfme2ujz7gU9APVm/wBEnEF4C0ZNs1c3XF9moIrm1FERihhtlm/HCGDa2F2DDkX x76vXFbYLaGRJ9rEbk2+oFUIjrUwyTqhdCt59LA0h+gx1G5UYzeLRMJhnOHB1SkI 8j0S1PkP9xiiFRxFkJAH2OEDGbCI9ySJWpuZUT/9L9jAT+zdk0IxQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrfeelgdehjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvffujgffkfggtgesmhdtreertddttdenucfhrhhomhepfdfrhhhilhhiphcu mfdrfdcuoehphhhilhhiphesfigrrhhpmhgrihhlrdhnvghtqeenucggtffrrghtthgvrh hnpeejieeuvdellefffefgueetkeelkeegveffieeffffhgfeuueehvdelvdeuvefhgeen ucfkphepkeejrddugeehrddtrdduheeinecuvehluhhsthgvrhfuihiivgeptdenucfrrg hrrghmpehmrghilhhfrhhomhepphhhihhlihhpseifrghrphhmrghilhdrnhgvth X-ME-Proxy: Original-Received: from localhost (p5791009c.dip0.t-ipconnect.de [87.145.0.156]) by mail.messagingengine.com (Postfix) with ESMTPA id E205D328005A; Sat, 18 Jul 2020 08:48:46 -0400 (EDT) In-Reply-To: <0c296186-702e-576d-a609-b3028d093def@yandex.ru> (message from Dmitry Gutov on Sat, 18 Jul 2020 01:21:47 +0300) 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:183169 Archived-At: --=-=-= Content-Type: text/plain Dmitry Gutov writes: > On 17.07.2020 20:16, Philip K. wrote: >> Dmitry Gutov writes: >> >>> On 17.07.2020 18:30, Philip K. wrote: >>>> (defcustom project-kill-buffers-ignores >>>> '("\\*Help\\*") >>> >>> You might as well update the default value. >> >> So what should be added, besides ERC? > > Probably nothing, unless you have something else to propose. > > At this point, you are the main driver of this feature. I like it in > theory (a lot), but so far has done little to incorporate in my > workflow, so you're the foremost person to hit the edge cases. I also > don't do email/IRC/notes/etc in Emacs. > > Speaking of some ideas, though, if you are worried about more unknown > modes needing the same treatment, we could flip the meaning of this var > and go with the whitelist approach, like font-lock-global-modes does. It > can still serve as a blacklist if the first element is `not`. > > To give an example: > > (defcustom project-kill-buffers-conditions > '(buffer-file-name ; All file-visiting buffers are included. > (derived-mode . compilation-mode) > ;; Most of the temp buffers in the background: > (major-mode . fundamental-mode) > ;; A bit questionable (alternatively, include > ;; xref--xref-buffer-mode, occur-mode, > ;; vc-dir-mode, log-view-mode, log-edit-mode separately): > (derived-mode . special-mode)) > "Conditions for buffers `project-kill-buffers' should kill. > Each condition is either a regular expression matching a buffer > name, or a predicate function that takes a buffer object as > argument and returns non-nil if it matches, or a cons cell which <...>. > > Buffers that belong to the current project, and match any of the > conditions, will be killed. If the list starts with `not', > the meaning is negated." > :type '(repeat (choice regexp function)) > :version "28.1" > :package-version '(project . "0.6.0")) > > If this kind of list can be exhaustive enough, this can be a decent default. > > What do you think? If you're not sure, let's go with your patch now. I like this idea a lot, the patch below should implement this + backwards compatibility code. Thought this might be getting too complicated, I also went ahead and added "and" and "or". -- Philip K. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Replace-project-kill-buffers-ignores-with-.-kill-buf.patch >From 6a9c268a340025bca428b5ec7c35229a29b4a95f Mon Sep 17 00:00:00 2001 From: Philip K Date: Thu, 16 Jul 2020 10:03:35 +0200 Subject: [PATCH] Replace project-kill-buffers-ignores with ...-kill-buffer-conditions --- lisp/progmodes/project.el | 119 ++++++++++++++++++++++++++++++++------ 1 file changed, 100 insertions(+), 19 deletions(-) diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index 67ce3dc7d9..869401606a 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -843,16 +843,66 @@ project-switch-to-buffer nil predicate)))) -(defcustom project-kill-buffers-ignores - '("\\*Help\\*") - "Conditions for buffers `project-kill-buffers' should not kill. -Each condition is either a regular expression matching a buffer -name, or a predicate function that takes a buffer object as -argument and returns non-nil if it matches. Buffers that match -any of the conditions will not be killed." - :type '(repeat (choice regexp function)) +(defcustom project-kill-buffer-conditions + '(buffer-file-name ; All file-visiting buffers are included. + ;; Most of the temp buffers in the background: + (major-mode . fundamental-mode) + ;; non-text buffer such as xref, occur, vc, log, ... + (derived-mode . special-mode) + (derived-mode . compilation-mode) + (derived-mode . dired-mode) + (derived-mode . diff-mode)) + "Conditions for buffers `project-kill-buffers' should kill. +Each condition is either: +- a regular expression, to match a buffer name, +- a predicate function that takes a buffer object as argument + and returns non-nil if the buffer should be killed, +- a symbol, denoting a buffer local variable, where the buffer + is killed if it's value is non-nil. If the symbol also has a + function slot, it will be interpreted as a function first. +- a cons-cell, where the car describes how to interpret the cdr. + The car can be one of the following: + * `major-mode': the buffer is killed if the buffers major + mode is eq to the cons-cell's cdr + * `defived-mode': the buffer is killed if the buffers major + mode is derived from the major mode denoted by the cons-cell's + cdr + * `not': the cdr is interpreted as a negation of a condition. + * `and': the cdr is a list of recursive conditions, that all have + to be met. + * `or': the cdr is a list of recursive conditions, of which at + least one has to be met. + +Buffers that match any of the conditions will not be killed." + :type '(repeat (choice regexp function symbol + (cons :tag "Major mode" + (const major-mode) symbol) + (cons :tag "Derived mode" + (const derived-mode) symbol) + (cons :tag "Negation" + (const not) sexp) + (cons :tag "Conjunction" + (const and) sexp) + (cons :tag "Disjunction" + (const or) sexp))) :version "28.1" - :package-version '(project . "0.5.0")) + :group 'project + :package-version '(project . "0.6.0")) + +(defcustom project-kill-buffers-ignores nil + "Conditions for buffers `project-kill-buffers' should not kill." + :type '(repeat choice regexp function) + :set (lambda (var val) + (add-to-list 'project-kill-buffer-conditions + (cons 'not val)) + (custom-set-default var val)) + :version "28.1" + :group 'project + :package-version '(project . "0.6.0"))) + +(make-obsolete-variable 'project-kill-buffers-ignores + 'project-kill-buffer-conditions + "0.6.0") (defun project--buffer-list (pr) "Return the list of all buffers in project PR." @@ -864,6 +914,41 @@ project--buffer-list (push buf bufs))) (nreverse bufs))) +(defun project--kill-buffer-check (buf &optional conds) + "Throw" + (unless conds + (setq conds project-kill-buffer-conditions)) + (catch (if (eq project-kill-buffer-conditions conds) + 'kill 'other) + (dolist (c conds) + (when (cond + ((stringp c) + (string-match-p c (buffer-name buf))) + ((and (functionp c) + (ignore-errors (funcall c buf)))) + ((and (symbolp c) (boundp c)) + (buffer-local-value c buf)) + ((eq (car-safe c) 'major-mode) + (eq (buffer-local-value 'major-mode buf) + (cdr c))) + ((eq (car-safe c) 'derived-mode) + (provided-mode-derived-p + (buffer-local-value 'major-mode buf) + (cdr c))) + ((eq (car-safe c) 'not) + (not (project--kill-buffer-check buf (cdr c)))) + ((eq (car-safe c) 'and) + (seq-every-p + (apply-partially #'project--kill-buffer-check + buf) + (cdr c))) + ((eq (car-safe c) 'or) + (seq-some + (apply-partially #'project--kill-buffer-check + buf) + (cdr c)))) + (throw 'kill t))))) + ;;;###autoload (defun project-kill-buffers () "Kill all live buffers belonging to the current project. @@ -873,17 +958,13 @@ project-kill-buffers (interactive) (let ((pr (project-current t)) bufs) (dolist (buf (project--buffer-list pr)) - (unless (seq-some - (lambda (c) - (cond ((stringp c) - (string-match-p c (buffer-name buf))) - ((functionp c) - (funcall c buf)))) - project-kill-buffers-ignores) + (when (project--kill-buffer-check buf) (push buf bufs))) - (when (yes-or-no-p (format "Kill %d buffers in %s? " - (length bufs) (project-root pr))) - (mapc #'kill-buffer bufs)))) + (if (null bufs) + (message "No buffers to kill") + (when (yes-or-no-p (format "Kill %d buffers in %s? " + (length bufs) (project-root pr))) + (mapc #'kill-buffer bufs))))) ;;; Project list -- 2.20.1 --=-=-=--