magit-save-some-buffers: add optional TOPDIR argument
magit-buffer-switch: add optional SWITCH-FUNCTION argument

This allows using this function in `magit-status' which until now
had to duplicate some of the functionality of this function.
magit-buffer-switch: detect magit buffers using derived-mode-p

Previously a regular expression ("magit") that matched to
much ("^\*magit" would have been more appropriate) was used to detect
such buffers based on their names.

This changes how the current buffer effects switching to a magit-mode
buffer:

- Buffers whose name begins with "*magit" but do not derive from
  `magit-mode' (e.g. "*magit-process*") are now treated as non-magit
  buffers.

- Buffers visiting magit libraries (e.g. "magit.el") are now treated
  as what they are "non-magit" buffers.
fix handling of magit-restore-window-configuration

When option `magit-restore-window-configuration' is non-nil
`magit-quit-window' restores the "previous window configuration" after
quitting the window and burying the buffer.  That configuration is
stored by `magit-buffer-switch'.

Until now the previous configuration was stored everytime
`magit-buffer-switch' was called, even if the buffer already existed
or even was already visible in the current frame.  This could lead to
`magit-quit-window' being unable to actually quit the window.

Now the previous configuration is only saved when it makes sense and
as an additional precaution `magit-quit-window' sets it to nil after
using it.
Merge branch 'buffer-switch'