Long ago, I added a key to Emacs to quickly close a buffer. I used to use Alt+F3, because that's what I used with Turbo Pascal. These days I use Cmd+W to match the Macintosh key for closing windows. The trouble is that when it's easy to close buffers, I do it often, and I occasionally close a buffer I shouldn't have closed. The solution is to make Emacs ask me for confirmation if the buffer hasn't been saved.

In both GNU Emacs and XEmacs, the answer is in a variable named kill-buffer-query-functions:

kill-buffer-query-functions is a variable defined
in `C source code'.
Documentation:
List of functions called with no args to query before
killing a buffer. The buffer being killed will be current
while the functions are running. If any of them returns nil,
the buffer is not killed.

Emacs will call each function listed in kill-buffer-query-functions before killing a buffer. If any of these functions returns nil, Emacs will not kill the buffer. So I defined a function that would ask for confirmation if the buffer hadn't been saved: