Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing buffers whose names start with a particular string

Tags:

emacs

lisp

elisp

Here's my problem: I use Emacs and get lots of buffers that are pretty useless all the time, like *Messages* or *Completions*.

I want to bind \C-y to close all buffers that start with * except for *shell* (and *shell* < k >) buffers.

To do that, I'd like to add some Emacs-Lisp in my .emacs file:

(defun string-prefix s1 s2
  (if (> (string-length s1) (string-length s2)) nil
    (string=? s1 (substring s2 0 (string-length s1))) ))

(defun curry2
  (lambda (f)
    (lambda (x)
      (lambda (y)
    (f x y) ))))

(defun filter
  (lambda (f l)
    (if (null? l) '()
      (let ((rest (cdr l)))
    (if (f (car l)) (cons (car l) rest)
      rest) ))))


(defun kill-useless (arg)
  (interactive "p")
  (map 'kill-buffer
       (filter
    (not ((curry2 string-prefix) "*shell*"))
    (list-buffers)
    ) ))

(global-set-key "\C-y" 'kill-useless)

I've already tested string-prefix and curry2 using Scheme and filter seems pretty straightforward. Sadly I just can't get kill-useless to work properly.

It says filter: Invalid function: (curry2 string-prefix).

Now, the thing is I kind of suck at Emacs-Lisp, I don't really use any Lisp except Scheme, and in Scheme (MIT), this works:

(filter ((curry2 string-prefix?) "*shell") '("*shell*" "*sh22" "eel"))
;Value 5: ("*shell*")

I'd like:

  1. a way to fix my code
  2. suggestions on how to do this in a different way

Thanks!

like image 836
Dan Filimon Avatar asked Feb 23 '11 21:02

Dan Filimon


1 Answers

C-h f kill-matching-buffers RET

kill-matching-buffers is an interactive compiled Lisp function in `files.el'.

(kill-matching-buffers REGEXP &optional INTERNAL-TOO)

Kill buffers whose name matches the specified REGEXP. The optional second argument indicates whether to kill internal buffers too.

like image 159
phils Avatar answered Nov 09 '22 21:11

phils