   (add-to-list 'desktop-buffer-mode-handlers '(w3m-mode . w3m-restore-desktop-buffer)))
 
 
-;;;; Editor behavior
-
-;;; We don't need to see the startup message.
-(setq inhibit-startup-message t)
-
-;;; Turn off the toolbar.
-(tool-bar-mode -1)
-
-;;; Turn off blinking cursor.
-(blink-cursor-mode 0)
+;;;; Buffer manipulation
+(defun close-current-buffer ()
+  "Close the current buffer.
+
+Similar to (kill-buffer (current-buffer)) with the following additions:
+
+* Prompt user to save if the buffer has been modified even if the buffer is not associated with a file.
+* Make sure the buffer shown after closing is a user buffer.
+
+A special buffer is one who's name starts with *.
+Else it is a user buffer."
+  (interactive)
+  (let (special-buffer-p is-special-buffer-after)
+    (if (string-match "^*" (buffer-name))
+        (setq special-buffer-p t)
+      (setq special-buffer-p nil))
+
+    ;; Offer to save buffers that are non-empty and modified, even for non-file visiting buffer.
+    ;; Because kill-buffer does not offer to save buffers that are not associated with files.
+    (when (and (buffer-modified-p)
+               (not special-buffer-p)
+               (not (string-equal major-mode "dired-mode"))
+               (if (equal (buffer-file-name) nil)
+                   (if (string-equal "" (save-restriction (widen) (buffer-string))) nil t)
+                 t
+                 )
+               )
+      (if (yes-or-no-p
+           (concat "Buffer " (buffer-name) " modified; kill anyway? "))
+          (save-buffer)
+        (set-buffer-modified-p nil)))
+
+    ;; close
+    (kill-buffer (current-buffer))
+
+    ;; if emacs buffer, switch to a user buffer
+    (if (string-match "^*" (buffer-name))
+        (setq is-special-buffer-after t)
+      (setq is-special-buffer-after nil))
+    (when is-special-buffer-after
+      (next-user-buffer))
+    ))
+
+(defun next-user-buffer ()
+  "Switch to the next user buffer in cyclic order.\n
+User buffers are those not starting with *."
+  (interactive)
+  (next-buffer)
+  (let ((i 0))
+    (while (and (string-match "^*" (buffer-name)) (< i 50))
+      (setq i (1+ i)) (next-buffer))))
+
+(defun previous-user-buffer ()
+  "Switch to the previous user buffer in cyclic order.\n
+User buffers are those not starting with *."
+  (interactive)
+  (previous-buffer)
+  (let ((i 0))
+    (while (and (string-match "^*" (buffer-name)) (< i 50))
+      (setq i (1+ i)) (previous-buffer))))
+
+(global-set-key (kbd "<kp-divide>") 'previous-user-buffer)
+(global-set-key (kbd "<kp-multiply>") 'next-user-buffer)
+(global-set-key (kbd "<kp-subtract>") 'close-current-buffer)
 
 ;;; Respawn the scratch buffer if it is killed (C-x k).
 (defun kill-scratch-buffer ()

(setq desktop-load-locked-desktop nil)  ;do not load desktop if locked
(add-to-list 'desktop-globals-to-save 'query-replace-history)  ;C-%
(add-to-list 'desktop-globals-to-save 'log-edit-comment-ring)  ;*VC-log*


;;;; Web browsing

;;; Key bindings.
(global-set-key (kbd "C-z") 'undo)   ;overrides suspend-frame
(global-set-key (kbd "C-S-z") 'redo)
(global-set-key (kbd "<kp-7>") (lambda () "" (interactive) (find-file "~/.plan")))

;;; Mouse yank commands yank at point instead of at click.
(setq mouse-yank-at-point t)
