crux icon indicating copy to clipboard operation
crux copied to clipboard

including dired in crux-kill-other-buffers

Open mambolevis opened this issue 7 years ago • 6 comments

Hi,

I think it is good idea to consider dired buffers in crux-kill-other-buffers function: Something like this:

(defun kill-dired-buffers ()
     (interactive)
     (mapc (lambda (buffer) 
           (when (eq 'dired-mode (buffer-local-value 'major-mode buffer)) 
             (kill-buffer buffer))) 
         (buffer-list)))

Thanks Levis

mambolevis avatar Sep 01 '17 19:09 mambolevis

You want a command to kill just buffers with a particular major mode or what?

bbatsov avatar Dec 30 '17 19:12 bbatsov

Let me explain it with one example. Consider that your current file is main.cpp and you have a total of 6 buffers open: mean.cpp, header.h, src, project, *scratch*, *Messsages* If you call crux-kill-other-buffers only header.h will be killed. I think it is also good idea to kill src and project which are dired buffers.

One possible solution is like you said, considering the setup of a particular major mode in crux-kill-other-buffers

mambolevis avatar Dec 30 '17 22:12 mambolevis

I also need crux command. something like crux-kill-other-dirs

azzamsa avatar May 14 '18 19:05 azzamsa

any updates regarding this feature request ?

azzamsa avatar Aug 19 '18 22:08 azzamsa

For now I can use

  1. list buffer from helm

  2. select all buffer

  3. delete

  4. C-x b

  5. C-u C-SPACE

  6. M-S-D

azzamsa avatar Aug 22 '18 07:08 azzamsa

@mambolevis maybe you need this

(defun kill-other-buffers ()
  "Kill all other buffers."
  (interactive)
  (mapc 'kill-buffer (delq (current-buffer) (buffer-list))))

Taken from Kill Other Buffers

But it will kill everything. crux-kill-other-buffers doesn't kill everything. Because it use (seq-filter #'buffer-file-name (buffer-list)) which will only return 'visited-buffer'. So that the dired buffer doesn't get kill.

The above code will kill everything. Because it just kill whatever (buffer-list) return. I come with my own solution:

(require 'cl)

;;;###autoload
(defun noprompt-kill-buffers ()
  "Kill buffers matching REGEXP without asking for confirmation."
  (interactive)
  (flet ((kill-buffer-ask (buffer) (kill-buffer buffer)))
    (kill-matching-buffers "^[^\*]"))) ;;all buffers that doesn't start with *

I think I can improve the regex value to make it more robust.

Update:

Fresh new version :)

;;;###autoload
(defun aza-kill-other-buffers ()
  "Kill all buffers but current buffer and special buffers"
  (interactive)
  (dolist (buffer (delq (current-buffer) (buffer-list)))
    (let ((name (buffer-name buffer)))
      (when (string-match "^[^\*]" name)
        (funcall 'kill-buffer buffer)))))

azzamsa avatar Sep 22 '18 05:09 azzamsa