From: Tino Calancha <tino.calancha@gmail.com>
To: Emacs developers <emacs-devel@gnu.org>
Subject: [ELPA] New package: kmb.el
Date: Wed, 24 May 2017 13:48:29 +0900 [thread overview]
Message-ID: <8737buoo6a.fsf@calancha-pc> (raw)
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 <tino.calancha@gmail.com>
;; 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 <http://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
\f
;;; 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:
\f
(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---
next reply other threads:[~2017-05-24 4:48 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-24 4:48 Tino Calancha [this message]
2017-05-25 5:38 ` [ELPA] New package: kmb.el John Wiegley
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8737buoo6a.fsf@calancha-pc \
--to=tino.calancha@gmail.com \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).