Simplify close buffer

Hi,

this is a bunch of patches which simplifies the `close_buffer()` function in `src/buffer.c` by outsourcing code chunks to own functions and replacing a bit of duplicated code with a macro.

I would like to try to build my changes, but currently I'm stuck. I'll notify you as soon as possible if I was able to get my build environment running.