From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.devel Subject: [ELPA] New package: kmb.el Date: Wed, 24 May 2017 13:48:29 +0900 Message-ID: <8737buoo6a.fsf@calancha-pc> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1495601475 18543 195.159.176.226 (24 May 2017 04:51:15 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 24 May 2017 04:51:15 +0000 (UTC) To: Emacs developers Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed May 24 06:51:11 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dDOGM-0004iY-Kn for ged-emacs-devel@m.gmane.org; Wed, 24 May 2017 06:51:10 +0200 Original-Received: from localhost ([::1]:52705 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dDOGS-0007un-3D for ged-emacs-devel@m.gmane.org; Wed, 24 May 2017 00:51:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50246) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dDODu-0006in-D5 for emacs-devel@gnu.org; Wed, 24 May 2017 00:48:40 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dDODr-00053J-8A for emacs-devel@gnu.org; Wed, 24 May 2017 00:48:38 -0400 Original-Received: from mail-pg0-x22e.google.com ([2607:f8b0:400e:c05::22e]:34587) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dDODr-00052v-0i for emacs-devel@gnu.org; Wed, 24 May 2017 00:48:35 -0400 Original-Received: by mail-pg0-x22e.google.com with SMTP id u28so59743242pgn.1 for ; Tue, 23 May 2017 21:48:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version; bh=BTfONtfF2coL1OrKPQpEuz9aM2tRgJG3qJkQ0B/MyGg=; b=OOaKaqnZrwUjTL737WH01GpVCCD++rDGbdxYfRUaWovkQLxVGUn6gMNF5i0LF1gW8W w2hUmorg3KFIuWpuAFnckMB7qz78c3LLZaulVrQ0fRPYbh96SiJgRmBVVMRforvq+z7i djl/+sC7PeW/CVPq0lIZu2dH81N6TnBLpcfCFEwiSenUs5ldnoQ+R77BGxGi4WTWGLQt a0R4D44ojjKn3KYw/T9zIA6CmGD16WGKDEnkWI66JlgZVMtVmoiLcRWw2EiBvfIXYaLI RNA0ah1yu7Vok19hzruTLfNJcnCqwhsbK70ZNfKEC0wdcmZaRkbwuBDWljnvz0oZQPYP cX8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=BTfONtfF2coL1OrKPQpEuz9aM2tRgJG3qJkQ0B/MyGg=; b=Q8UWh70oMM1RYh0RpgjTHsVRitPxBSTOCuXs6G3DqFuhBEXMLanx5b0xsfG/6LGsIX obhtZ3zsvRUTAkkWljbsJiYF4FRaBjcc+yso+5mZPqcUUofKU5EoETexHaDdoz2O6Ol+ XY63ZjMhnpP5vRPezTOYCMZinb6mYOdacl1a5KiucoFnKSQqOcjOLbaGiBWPuVBovTdw Iff823qQ98vqvB1ZEKldv5T43xAGCId6093zu3+iicGf22Jx7MDsZDhoj17fj6tvy15c jbLGHe0R7mRibpBYdY7E8+wpk8V+vCSAVkf/ZfpVQSPVz55sem2DbrA02nG/mQVK0p+G f1KQ== X-Gm-Message-State: AODbwcCsSUCcAgfEENfnevlLHSpbemXwIlzddyeS0deSNxc7oGwD0DVH cS2mXYRhw+SxwzZH X-Received: by 10.98.98.6 with SMTP id w6mr14825654pfb.67.1495601313610; Tue, 23 May 2017 21:48:33 -0700 (PDT) Original-Received: from calancha-pc (222.139.137.133.dy.bbexcite.jp. [133.137.139.222]) by smtp.gmail.com with ESMTPSA id h62sm4915107pfj.0.2017.05.23.21.48.32 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 23 May 2017 21:48:32 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::22e X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:215153 Archived-At: This library provides the commands `kmb-kill-matching-buffers-no-ask' and `kmb-delete-process-and-kill-buffer-no-ask'. The former kills buffers whose name matches a regular expression. The latter, interactively kills the current buffer and if called from Lisp, then accepts a list of buffers to kill. Any of these commands ask for confirmation to kill the buffers. If one of the buffers is running a process, then the process is deleted before kill the buffer. This file also defines the commands `kmb-list-matching-buffers' and `kmb-list-buffers-matching-content' to list the buffers whose name or content match a regexp. ------------------------------------------------------------------------------- I've being using this library last couple of years. After reading this thread https://lists.gnu.org/archive/html/emacs-devel/2017-05/msg00625.html i think it might be useful for other people as well. --8<-----------------------------cut here---------------start------------->8--- ;;; kmb.el --- Kill buffers matching a regexp w/o confirmation -*- lexical-binding: t -*- ;; Copyright (C) 2017 Free Software Foundation, Inc. ;; Author: Tino Calancha ;; Keywords: lisp, convenience ;; Maintainer: Tino Calancha ;; Created: Wed May 24 13:19:18 JST 2017 ;; Version: 0.1 ;; Package-Requires: ((emacs "24.1")) ;; Last-Updated: Wed May 24 13:19:18 JST 2017 ;; By: calancha ;; Update #: 0 ;; Compatibility: GNU Emacs 24.x ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; This file is NOT part of GNU Emacs. ;; ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Commentary: ;; ;; This library provides the commands `kmb-kill-matching-buffers-no-ask' ;; and `kmb-delete-process-and-kill-buffer-no-ask'. The former kills ;; buffers whose name matches a regular expression. The latter, ;; interactively kills the current buffer and if called from Lisp, ;; then accepts a list of buffers to kill. ;; Any of these commands ask for confirmation to kill the buffers. ;; If one of the buffers is running a process, then the process is ;; deleted before kill the buffer. ;; ;; This file also defines the commands `kmb-list-matching-buffers' and ;; `kmb-list-buffers-matching-content' to list the buffers whose name ;; or content match a regexp. ;; ;; ;; Commands defined here: ;; ;; `kmb-delete-process-and-kill-buffer-no-ask', ;; `kmb-kill-matching-buffers-no-ask', `kmb-list-buffers-matching-content', ;; `kmb-list-matching-buffers'. ;; ;; Non-interactive functions defined here: ;; ;; `kmb--show-matches'. ;; ;; ;;; Code: (defun kmb--show-matches (buffers &optional count) "Show the name of BUFFERS in the echo area. BUFFERS is a list of buffers. If optional arg COUNT is non-nil, then it's the length of BUFFERS." (if (null buffers) (message "No buffers matching regexp") (message "Found %d match%s: %s" (or count (length buffers)) (if (cdr buffers) "es" "") (mapconcat #'buffer-name buffers ", ")))) (defun kmb-list-matching-buffers (regexp &optional with-process) "Return list of buffers whose name matching REGEXP. If optional arg WITH-PROCESS is non-nil, then list just buffers running a process." (interactive (let* ((prefix current-prefix-arg) (regexp (if prefix (read-string "List buffers running a process \ and matching regexp: ") (read-string "List buffers matching regexp: ")))) (list regexp prefix))) (let ((buffers (delq nil (mapcar (lambda (x) (when (string-match regexp (buffer-name x)) (cond (with-process (and (get-buffer-process x) x)) (t x)))) (buffer-list))))) (kmb--show-matches buffers) buffers)) (defun kmb-kill-matching-buffers-no-ask (regexp) "Kill all buffers whose name matching REGEXP without confirmation. If a buffer is running a process, then delete the process before kill the buffer." (interactive "sKill buffers matching regexp: ") (dolist (b (buffer-list)) (when (string-match regexp (buffer-name b)) (kmb-delete-process-and-kill-buffer-no-ask b)))) (defalias 'kmb-kill-matching-buffers 'kmb-kill-matching-buffers-no-ask) (defun kmb-list-buffers-matching-content (regexp) "Return list of buffers whose content match REGEXP." (interactive "sList buffers whose content matches regexp: ") (let* ((count 0) (buffers (delq nil (mapcar (lambda (b) (let (str) (with-current-buffer b (setq str (buffer-substring-no-properties (point-min) (point-max)))) (when (string-match regexp str) (setq count (1+ count)) b))) (buffer-list))))) (kmb--show-matches buffers count) buffers)) (defun kmb-delete-process-and-kill-buffer-no-ask (&optional buffer) "Delete BUFFER without confirmation. BUFFER is a buffer or a list of buffers. If the buffer is running a process, then delete the processes before kill the buffer. Interactivelly, delete the current buffer." (interactive "i") (let* ((def (or buffer (current-buffer))) (buffers (delq nil (mapcar #'get-buffer (if (nlistp def) (list def) def)))) (processes (process-list))) (dolist (buf buffers) (when (get-buffer-process buf) (dolist (proc processes) (when (eq buf (process-buffer proc)) (set-process-query-on-exit-flag proc nil)))) (when (buffer-modified-p buf) (with-current-buffer buf (set-buffer-modified-p nil))) (kill-buffer buf)))) (defalias 'kmb-kill-buffer 'kmb-delete-process-and-kill-buffer-no-ask) (provide 'kmb) ;;; kmb.el ends here --8<-----------------------------cut here---------------end--------------->8---