* Re: Killing Buffers
2003-12-29 13:34 Edward Wijaya
@ 2003-12-29 6:27 ` Friedrich Dominicus
2003-12-29 6:55 ` Eli Zaretskii
2004-01-02 18:49 ` Bruce Ingalls
2 siblings, 0 replies; 12+ messages in thread
From: Friedrich Dominicus @ 2003-12-29 6:27 UTC (permalink / raw)
Edward Wijaya <ewijaya@singnet.com.sg> writes:
> Hi,
>
> I am looking for a key that:
> 1. Kill all the existing buffer without having have to close Emacs
> 2. Kill all the existing buffer "except" the active one.
>
> Does anybody know the shortcut key for that?
Something like that could do the job
(require 'cl)
(defun kill-all-buffers-except (prefix)
(interactive "P")
(let ((cur-buf-name (buffer-name nil))
(buf-list (buffer-list)))
(when prefix
(setf buf-list (delete-if #'(lambda (buf)
(string= (buffer-name buf) cur-buf-name))
buf-list)))
(map 'nil #'kill-buffer buf-list)))
You can bind it to whatever key you like.
If you give it prefix C-u than all but the current buffer is
killed. Otherwise all is killed but one buffer. One buffer is open all
the time.
Regards
Friedrich
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Killing Buffers
2003-12-29 13:34 Edward Wijaya
2003-12-29 6:27 ` Friedrich Dominicus
@ 2003-12-29 6:55 ` Eli Zaretskii
2004-01-02 18:49 ` Bruce Ingalls
2 siblings, 0 replies; 12+ messages in thread
From: Eli Zaretskii @ 2003-12-29 6:55 UTC (permalink / raw)
> From: Edward Wijaya <ewijaya@singnet.com.sg>
> Newsgroups: gnu.emacs.help
> Date: Mon, 29 Dec 2003 13:34:01 -0000
>
> I am looking for a key that:
> 1. Kill all the existing buffer without having have to close Emacs
> 2. Kill all the existing buffer "except" the active one.
One command that comes close is "M-x kill-some-buffers RET". See its
doc string for more details.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Killing Buffers
@ 2003-12-29 13:34 Edward Wijaya
2003-12-29 6:27 ` Friedrich Dominicus
` (2 more replies)
0 siblings, 3 replies; 12+ messages in thread
From: Edward Wijaya @ 2003-12-29 13:34 UTC (permalink / raw)
Hi,
I am looking for a key that:
1. Kill all the existing buffer without having have to close Emacs
2. Kill all the existing buffer "except" the active one.
Does anybody know the shortcut key for that?
Thanks so much for your time.
RegardS
Edward
SINGAPORE
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Killing Buffers
2003-12-29 13:34 Edward Wijaya
2003-12-29 6:27 ` Friedrich Dominicus
2003-12-29 6:55 ` Eli Zaretskii
@ 2004-01-02 18:49 ` Bruce Ingalls
2004-01-02 20:54 ` Brian Palmer
2 siblings, 1 reply; 12+ messages in thread
From: Bruce Ingalls @ 2004-01-02 18:49 UTC (permalink / raw)
;I believe I have merged recent posts of buffer killing code into the
;best practices of all, combined here.
;Since the code works, I am too lazy to re-use
;kill-all-other-buffer-frames() to call kill-buffer-frame() but will
;gladly accept patches. Happy Holidays, Bruce
;;__________________________________________________________________________
;;;;;; kill-buffer-frame
(defun kill-buffer-frame ()
"Tries `delete-frame', then `delete-window', then `kill-buffer' to close
the current file, only."
(interactive)
(condition-case err ;Handle any exceptions
(delete-frame)
(error
(let ((buffer (current-buffer)))
(or (one-window-p) (delete-window))
(kill-buffer buffer)))))
;;__________________________________________________________________________
;;;;;; kill-all-other-buffer-frames
(defun kill-all-other-buffer-frames (&optional prefix)
"Closes all open files, except current buffer, window and frame.
If prefixed with 'C-u', leaves only *scratch* buffer, instead."
(interactive "P")
(let ((cur-buf-name (buffer-name nil))
(buf-list (buffer-list)))
(mapcar (lambda (x) (delete-frame x)) ;close all frames but one, first
(cdr (visible-frame-list)))
(or (one-window-p) (delete-window)) ;close all windows but one, next
(if prefix ;C-u prefix: leave only *scratch*
(progn
(mapcar (lambda (x) (kill-buffer x)) (buffer-list))
(delete-other-windows))
;;else leave the current window
(setf buf-list (delete-if #'(lambda (buf)
(string= (buffer-name buf) cur-buf-name))
buf-list))
(map 'nil #'kill-buffer buf-list))))
;;__________________________________________________________________________
(global-set-key [(control f4)] 'kill-buffer-frame) ;cua binding
;;Note: C-u prefix leaves only the *scratch* buffer, instead of current
;;buffer
(global-set-key [(meta control f4)] 'kill-all-other-buffer-frames)
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Killing Buffers
2004-01-02 18:49 ` Bruce Ingalls
@ 2004-01-02 20:54 ` Brian Palmer
2004-01-03 3:46 ` Andi Kleen
2004-01-03 13:51 ` Bruce Ingalls
0 siblings, 2 replies; 12+ messages in thread
From: Brian Palmer @ 2004-01-02 20:54 UTC (permalink / raw)
Bruce Ingalls <bingalls@fit-zones.NO-SPAM.com> writes:
> ;I believe I have merged recent posts of buffer killing code into the
> ;best practices of all, combined here.
> ;Since the code works, I am too lazy to re-use
> ;kill-all-other-buffer-frames() to call kill-buffer-frame() but will
> ;gladly accept patches. Happy Holidays, Bruce
Interesting; I doubt I'll use these, since I'm relatively content with
just C-x C-b and marking all the buffers to delete, but here are some
revisions I've made to the code. Feel free to accept any changes you
like (I release any part belonging to me into the public domain, if
possible, and license it for any and all use if not).
The big changes I made were shortening the first sentence in the
docstring so that apropos kill.*buffer is meaningful, and eliminating
some of your lambdas (mapcar (lambda (x) (f x)) l) is the same as
(mapcar 'f l) in elisp, I believe, and even in cl it only makes a
difference if you're trying to capture existing variables). Similarly,
I replaced (map nil ....) with mapcar, and used delete instead of
delete-if (since string= is a special form of equal, which is what
delete uses).
At any rate, lightly tested on xemacs 21.4.8
(defun kill-buffer-frame ()
"Kill the current frame, or current buffer and delete its window."
(interactive)
(condition-case nil
(delete-frame)
(error
(let ((buffer (current-buffer)))
(or (one-window-p)
(delete-window))
(kill-buffer buffer)))))
(defun kill-all-other-buffer-frames (&optional prefix)
"Close other open files, and kill other frames and windows.
With prefix argument, kill all buffers, leaving only the default *scratch* buffer."
(interactive "P")
(let ((cur-buf-name (buffer-name))
(buffers-to-kill (buffer-list)))
(if (null prefix)
(setf buffers-to-kill (delete cur-buf-name buffers-to-kill)))
(mapcar 'delete-frame (cdr (visible-frame-list)))
(or (one-window-p)
(delete-window))
(mapcar 'kill-buffer buffers-to-kill)
(delete-other-windows)))
;;__________________________________________________________________________
(global-set-key [(control f4)] 'kill-buffer-frame) ;cua binding
(global-set-key [(meta control f4)] 'kill-all-other-buffer-frames)
--
I'm awfully glad I'm a Beta, because I don't work so hard.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Killing Buffers
@ 2004-01-02 22:28 Edward Wijaya
0 siblings, 0 replies; 12+ messages in thread
From: Edward Wijaya @ 2004-01-02 22:28 UTC (permalink / raw)
Courtesy of Jose Ruiz.
------- Forwarded message -------
From: "Jose A. Ortega Ruiz" <jao@gnu.org>
To: Edward Wijaya <ewijaya@singnet.com.sg>
Subject: Re: Killing Buffers
Date: Fri, 02 Jan 2004 14:04:55 +0100
> Edward Wijaya <ewijaya@singnet.com.sg> writes:
>
>> Hi,
>>
>> I am looking for a key that:
>> 1. Kill all the existing buffer without having have to close Emacs
>
> hi, i've got this code in my .emacs:
>
> ;;; kill all buffers
> (defun my-blank-slate-emacs ()
> "kill all buffers, leaving *scratch* only"
> (interactive)
> ;;close all frames but one, first
> (mapcar (lambda (x) (delete-frame x))
> (cdr (visible-frame-list)))
> ;;then, kill all buffers
> (mapcar (lambda (x) (kill-buffer x))
> (buffer-list))
> ;;make there be only one window holding buffer *scratch*
> (delete-other-windows))
> (global-set-key "\C-x!" 'my-blank-slate-emacs)
>
> hth,
> jao
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Killing Buffers
2004-01-02 20:54 ` Brian Palmer
@ 2004-01-03 3:46 ` Andi Kleen
2004-01-05 19:27 ` Kevin Rodgers
2004-01-03 13:51 ` Bruce Ingalls
1 sibling, 1 reply; 12+ messages in thread
From: Andi Kleen @ 2004-01-03 3:46 UTC (permalink / raw)
Brian Palmer <bpalmer@rescomp.Stanford.EDU> writes:
> Interesting; I doubt I'll use these, since I'm relatively content with
> just C-x C-b and marking all the buffers to delete, but here are some
I use this function. It is useful when you have multiple copies
of a source tree around and you first edit some files in one
copy and then switch to another copy and don't want C-x b to
get you to files from the other again.
It doesn't kill frames recently (mostly because I rarely use more than one),
but I guess that would be an interesting extension too.
;;; FIXME should handle dired buffers too.
(defun kill-buffer-tree (directory)
"Kill all buffers that visit files below a specific directory tree."
(interactive "DDirectory tree to kill from: ")
(let* ((dir (expand-file-name directory))
(dir-re (apply 'concat
"^"
(if (string-match "/$" dir)
(list dir)
(list dir "/")))))
(mapcar
'(lambda (buffer)
(let ((file-name (buffer-file-name buffer)))
(if (and file-name
(string-match dir-re file-name)
(y-or-n-p (concat "Kill buffer " file-name "? ")))
(kill-buffer buffer))))
(buffer-list))))
-Andi
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Killing Buffers
2004-01-02 20:54 ` Brian Palmer
2004-01-03 3:46 ` Andi Kleen
@ 2004-01-03 13:51 ` Bruce Ingalls
2004-01-12 15:02 ` Brian Palmer
1 sibling, 1 reply; 12+ messages in thread
From: Bruce Ingalls @ 2004-01-03 13:51 UTC (permalink / raw)
Brian Palmer wrote:
> Bruce Ingalls <bingalls@fit-zones.NO-SPAM.com> writes:
>>I have merged recent posts of buffer killing code
>
> Interesting; I doubt I'll use these, since I'm relatively content with
> just C-x C-b and marking all the buffers to delete
Nice, except that it takes more keystrokes, and won't close windows nor
frames. CUA compliance always helps.
> like (I release any part belonging to me into the public domain, if
> possible, and license it for any and all use if not).
This code alone is too trivial to copyright. Both ways you describe are
public domain. In any case, this code will be available via the GPL at
<url: http://emacro.sf.net/ >
> (defun kill-all-other-buffer-frames (&optional prefix)
Thanks for pointing out inelegancies in the code. Unfortunately, your
changes always leave the *scratch* buffer, and never the current buffer. :(
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Killing Buffers
2004-01-03 3:46 ` Andi Kleen
@ 2004-01-05 19:27 ` Kevin Rodgers
0 siblings, 0 replies; 12+ messages in thread
From: Kevin Rodgers @ 2004-01-05 19:27 UTC (permalink / raw)
Andi Kleen wrote:
> ;;; FIXME should handle dired buffers too.
Just change:
> (let ((file-name (buffer-file-name buffer)))
to:
(let ((file-name (save-excursion
(set-buffer buffer)
default-directory)))
--
Kevin Rodgers
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Killing Buffers
2004-01-03 13:51 ` Bruce Ingalls
@ 2004-01-12 15:02 ` Brian Palmer
2004-01-13 16:07 ` Kevin Rodgers
0 siblings, 1 reply; 12+ messages in thread
From: Brian Palmer @ 2004-01-12 15:02 UTC (permalink / raw)
Bruce Ingalls <bingalls@fit-zones.NO-SPAM.com> writes:
> > like (I release any part belonging to me into the public domain, if
> > possible, and license it for any and all use if not).
>
> This code alone is too trivial to copyright. Both ways you describe
> are public domain. In any case, this code will be available via the
> GPL at <url: http://emacro.sf.net/ >
Excellent. I just wanted to be compliant with whatever your code's
license was. (And there are differences between universal licensing
and public domain, aiui, but not worth getting into)
> > (defun kill-all-other-buffer-frames (&optional prefix)
>
> Thanks for pointing out inelegancies in the code. Unfortunately, your
> changes always leave the *scratch* buffer, and never the current
> buffer. :(
Sorry 'bout that. It's as simple as changing, in
kill-all-other-buffer-frames
(buffers-to-kill (buffer-list)))
to
(buffers-to-kill (mapcar 'buffer-name (buffer-list))))
(Tested and verified).
(defun kill-all-other-buffer-frames (&optional prefix)
"Close other open files, and kill other frames and windows. With
prefix argument, kill all buffers, leaving only the default *scratch*
buffer."
(interactive "P")
(let ((cur-buf-name (buffer-name))
(buffers-to-kill (mapcar 'buffer-name (buffer-list))))
(if (null prefix)
(setf buffers-to-kill (delete cur-buf-name buffers-to-kill)))
(mapcar 'delete-frame (cdr (visible-frame-list)))
(or (one-window-p)
(delete-window))
(mapcar 'kill-buffer buffers-to-kill)
(delete-other-windows)))
--
I'm awfully glad I'm a Beta, because I don't work so hard.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Killing Buffers
2004-01-12 15:02 ` Brian Palmer
@ 2004-01-13 16:07 ` Kevin Rodgers
2004-01-14 18:21 ` Brian Palmer
0 siblings, 1 reply; 12+ messages in thread
From: Kevin Rodgers @ 2004-01-13 16:07 UTC (permalink / raw)
Brian Palmer wrote:
> Bruce Ingalls <bingalls@fit-zones.NO-SPAM.com> writes:
>>Brian Palmer wrote:
>>>(defun kill-all-other-buffer-frames (&optional prefix)
>>
>>Thanks for pointing out inelegancies in the code. Unfortunately, your
>>changes always leave the *scratch* buffer, and never the current
>>buffer. :(
>
> Sorry 'bout that. It's as simple as changing, in
> kill-all-other-buffer-frames
> (buffers-to-kill (buffer-list)))
> to
> (buffers-to-kill (mapcar 'buffer-name (buffer-list))))
How could that have any effect? Each element of buffers-to-kill is
passed directly to kill-buffer, which handles a buffer or a buffer name
the same. And if you used buffers instead of names, you could use delq
instead of delete.
On a stylistic note, you use setf from the Common Lisp compatibility
library, but call mapcar without side effect instead of using the
(CL-inspired) mapc built-in function.
> (Tested and verified).
>
> (defun kill-all-other-buffer-frames (&optional prefix)
> "Close other open files, and kill other frames and windows. With
> prefix argument, kill all buffers, leaving only the default *scratch*
> buffer."
> (interactive "P")
> (let ((cur-buf-name (buffer-name))
> (buffers-to-kill (mapcar 'buffer-name (buffer-list))))
> (if (null prefix)
> (setf buffers-to-kill (delete cur-buf-name buffers-to-kill)))
>
> (mapcar 'delete-frame (cdr (visible-frame-list)))
> (or (one-window-p)
> (delete-window))
> (mapcar 'kill-buffer buffers-to-kill)
> (delete-other-windows)))
--
Kevin Rodgers
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Killing Buffers
2004-01-13 16:07 ` Kevin Rodgers
@ 2004-01-14 18:21 ` Brian Palmer
0 siblings, 0 replies; 12+ messages in thread
From: Brian Palmer @ 2004-01-14 18:21 UTC (permalink / raw)
Kevin Rodgers <ihs_4664@yahoo.com> writes:
> Brian Palmer wrote:
> > Sorry 'bout that. It's as simple as changing, in
> > kill-all-other-buffer-frames
> > (buffers-to-kill (buffer-list)))
> > to
> > (buffers-to-kill (mapcar 'buffer-name (buffer-list))))
>
> How could that have any effect? Each element of buffers-to-kill is
> passed directly to kill-buffer, which handles a buffer or a buffer name
> the same. And if you used buffers instead of names, you could use delq
> instead of delete.
The trouble was that cur-buf-name uses the buffer name, but
buffers-to-kill had been actual buffers. (I think I started with one,
and changed approach). Either using buffers throughout, or using names
throughout, is required. Personally, I prefer using names. Your point
about eq rather than equal is spot-on, however.
> On a stylistic note, you use setf from the Common Lisp compatibility
> library, but call mapcar without side effect instead of using the
> (CL-inspired) mapc built-in function.
Thanks! I'm always looking for stylistic feedback.
--
I'm awfully glad I'm a Beta, because I don't work so hard.
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2004-01-14 18:21 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-01-02 22:28 Killing Buffers Edward Wijaya
-- strict thread matches above, loose matches on Subject: below --
2003-12-29 13:34 Edward Wijaya
2003-12-29 6:27 ` Friedrich Dominicus
2003-12-29 6:55 ` Eli Zaretskii
2004-01-02 18:49 ` Bruce Ingalls
2004-01-02 20:54 ` Brian Palmer
2004-01-03 3:46 ` Andi Kleen
2004-01-05 19:27 ` Kevin Rodgers
2004-01-03 13:51 ` Bruce Ingalls
2004-01-12 15:02 ` Brian Palmer
2004-01-13 16:07 ` Kevin Rodgers
2004-01-14 18:21 ` Brian Palmer
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).