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: Tue, 21 Jul 2020 11:11:56 +0200 Message-ID: <87eep59qqr.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="15445"; 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 Tue Jul 21 11:13: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 1jxoKk-0003sb-N2 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 21 Jul 2020 11:13:11 +0200 Original-Received: from localhost ([::1]:37426 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jxoKj-00044h-EQ for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 21 Jul 2020 05:13:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58790) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jxoKc-00044I-2E for bug-gnu-emacs@gnu.org; Tue, 21 Jul 2020 05:13:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54731) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jxoKb-0007Fi-Pd for bug-gnu-emacs@gnu.org; Tue, 21 Jul 2020 05:13:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jxoKb-00040u-KK for bug-gnu-emacs@gnu.org; Tue, 21 Jul 2020 05:13:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: "Philip K." Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 21 Jul 2020 09:13:01 +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.159532272915339 (code B ref 42386); Tue, 21 Jul 2020 09:13:01 +0000 Original-Received: (at 42386) by debbugs.gnu.org; 21 Jul 2020 09:12:09 +0000 Original-Received: from localhost ([127.0.0.1]:38044 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jxoJk-0003zI-M1 for submit@debbugs.gnu.org; Tue, 21 Jul 2020 05:12:09 -0400 Original-Received: from wout3-smtp.messagingengine.com ([64.147.123.19]:51031) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jxoJi-0003yj-AQ for 42386@debbugs.gnu.org; Tue, 21 Jul 2020 05:12:07 -0400 Original-Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id 540E1300; Tue, 21 Jul 2020 05:12:00 -0400 (EDT) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Tue, 21 Jul 2020 05:12:00 -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=EtNOBIVpykVvYDQGb+L78bhhHFWAbNCJvr59mHk bEPM=; b=HRytqp5LMIfEkFomenvi97pGQZTYolN7173G3gCwbQqHKWpUK9qVSS2 KfSPoYiJbQE1B2/WAyy3FQ49JtI+2vZt17fsJc7NtpPu6GrhVgefoIGDMDuCW7Cs Jxysz7SMC0Xn/+vAM7JKQmmYQPgZMsHGy1V3Omlr6n+sznEwcfVV8PRyXV19FT2D YbhN3X/fxaD0jyWOLR8nZfu7CxzKlxTSXq+DbKUD7oL+cHcxDIZe+3Yuoj/SqAsy /Lcrh07d739hwSMwkHlQnYo9KnGxVhLzdjYtRdQ+0DcSxauKfOOJoOwVXbz4FrmE 7Kr2OVns2RsVcE3Gbjma48eJX9Fo7QA== 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=EtNOBIVpykVvYDQGb +L78bhhHFWAbNCJvr59mHkbEPM=; b=F/IxIrQY8EduoKvQbuDI+bvvwN6vojvNP PHjeElU2ReNw2i32P5ZMmpSO3sBXrz1VI58IloM9urxdYb6vVfyHATZqLeoe7mtP Tjjxojom2YS6nQhRzuZuomXWnmTXHJp91xyBpsDVxUxEnK/D7mQDo/gB9WLp0+EC qg0CuaV14qNgn69UDRHIJzvQKVOqRqKzORnkXGWGPB5e/ATLid0lEc60srSWGXwP RFhoTo8rUhJpb4Gwde+BoCbcDo4Rc1/IdWTIby/D/1NUAKXPQ1t+UeLfCUn7FVNU 55jjNruWam8I/kKBerAAWhiUy2SHcs/0iFMcjhAyUVoJz6myfRBmA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrgeeigddtkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvffujgffkfggtgesmhdtreertddttdenucfhrhhomhepfdfrhhhilhhiphcu mfdrfdcuoehphhhilhhiphesfigrrhhpmhgrihhlrdhnvghtqeenucggtffrrghtthgvrh hnpeejieeuvdellefffefgueetkeelkeegveffieeffffhgfeuueehvdelvdeuvefhgeen ucfkphepkeejrddugeehrddtrddutdeinecuvehluhhsthgvrhfuihiivgeptdenucfrrg hrrghmpehmrghilhhfrhhomhepphhhihhlihhpseifrghrphhmrghilhdrnhgvth X-ME-Proxy: Original-Received: from localhost (p5791006a.dip0.t-ipconnect.de [87.145.0.106]) by mail.messagingengine.com (Postfix) with ESMTPA id 5F8FF3060067; Tue, 21 Jul 2020 05:11:59 -0400 (EDT) In-Reply-To: <920f6e5b-2ef6-3385-73cc-9e344e621c69@yandex.ru> (message from Dmitry Gutov on Mon, 20 Jul 2020 16:39:59 +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:183346 Archived-At: --=-=-= Content-Type: text/plain Dmitry Gutov writes: > On 20.07.2020 15:07, Philip K. wrote: >>>> + ((eq (car-safe c) 'or) >>>> + (seq-some >>>> + (apply-partially #'project--kill-buffer-check >>>> + buf) >>> I think we can simply recurse in this case. >> I feel stupid for asking, but what do you mean? Am I not recursively >> calling project--kill-buffer-check? > > Just > > (project--kill-buffer-check buf (cdr c)) > > instead of using apply-partially since we use the 'or' semantics by > default already in there. > > It's not a big thing, and can be changed later anyway. Oh yes, I missed that. Anyways, the updated patch is attached below. -- Philip K. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Add-project-kill-buffer-conditions.patch >From f054c4983f9a90d8bf411f6d06c327a3fa382f4b Mon Sep 17 00:00:00 2001 From: Philip K Date: Mon, 20 Jul 2020 21:20:34 +0200 Subject: [PATCH] Add project-kill-buffer-conditions This replaces its negation, project-kill-buffers-ignores, from the previous version. --- lisp/progmodes/project.el | 102 +++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 19 deletions(-) diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index 67ce3dc7d9..50c164ebc7 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el @@ -843,16 +843,52 @@ 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, ... + (and (derived-mode . special-mode) + (not (major-mode . help-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")) (defun project--buffer-list (pr) "Return the list of all buffers in project PR." @@ -864,6 +900,38 @@ project--buffer-list (push buf bufs))) (nreverse bufs))) +(defun project--kill-buffer-check (buf conds) + "Return non-nil, if buffer BUF matches any CONDS. +CONDS is a list of conditions. See +`project-kill-buffer-conditions' for more details." + (catch 'kill + (dolist (c conds) + (when (cond + ((stringp c) + (string-match-p c (buffer-name buf))) + ((symbolp c) + (funcall 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 +941,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 project-kill-buffer-conditions) (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 --=-=-=--