diff --git a/layers/+spacemacs/spacemacs-defaults/funcs.el b/layers/+spacemacs/spacemacs-defaults/funcs.el index 82b6ab951..76581df95 100644 --- a/layers/+spacemacs/spacemacs-defaults/funcs.el +++ b/layers/+spacemacs/spacemacs-defaults/funcs.el @@ -285,7 +285,6 @@ Dedicated (locked) windows are left untouched." (interactive "p") (spacemacs/rotate-windows-forward (* -1 count))) - (if (configuration-layer/package-used-p 'winum) (progn (defun spacemacs/move-buffer-to-window (windownum follow-focus-p) @@ -668,6 +667,19 @@ If the universal prefix argument is used then kill the windows too." (when (equal '(4) arg) (delete-other-windows)) (message "Buffers deleted!"))) +(defun spacemacs//confirm-kill-buffer () + "Prompt the user to save a buffer to a file before killing it. +This skips the following buffers: +- A buffer with non-nil value of variable `buffer-file-name'. + Or in other words, a buffer who has a file associated with. + Emacs by default prompts the user to save it if it's modified. +- A buffer derived from `special-mode'." + (when (and (not buffer-file-name) + (buffer-modified-p) + (not (derived-mode-p 'special-mode)) + (not (yes-or-no-p (format "Buffer %S modified; kill anyway? " (buffer-name))))) + (save-buffer))) + ;; from http://dfan.org/blog/2009/02/19/emacs-dedicated-windows/ (defun spacemacs/toggle-current-window-dedication () "Toggle dedication state of a window. Commands that change the buffer that a @@ -837,13 +849,12 @@ variable." (concat dotspacemacs-template-directory ".spacemacs.template"))) (defun spacemacs/new-empty-buffer (&optional split) - "Create a new buffer called: untitled + "Create a new buffer called: \"untitled\". -The SPLIT argument decides where the buffer opens: -Value Buffer -`nil' current window -`left', `below', `above' or `right' split window -`frame' new frame +SPLIT decides where the buffer opens: +- nil, open in current window. +- `left', `below', `above' or `right', split the window in the given direction. +- `frame', open in new frame. If the variable `dotspacemacs-new-empty-buffer-major-mode' has been set, then apply that major mode to the new buffer." @@ -858,6 +869,9 @@ then apply that major mode to the new buffer." ;; Prompt to save on `save-some-buffers' with positive PRED (with-current-buffer newbuf (setq-local buffer-offer-save t) + (add-hook 'kill-buffer-hook + #'spacemacs//confirm-kill-buffer + nil t) (when dotspacemacs-new-empty-buffer-major-mode (funcall dotspacemacs-new-empty-buffer-major-mode))) ;; pass non-nil force-same-window to prevent `switch-to-buffer' from @@ -1659,11 +1673,14 @@ if prefix argument ARG is given, switch to it in an other, possibly new window." (if arg (switch-to-buffer-other-window (get-buffer-create "*scratch*")) (switch-to-buffer (get-buffer-create "*scratch*"))) - (when (and (not exists) - (not (eq major-mode dotspacemacs-scratch-mode)) - (fboundp dotspacemacs-scratch-mode)) - (funcall dotspacemacs-scratch-mode) - (run-hooks 'spacemacs-scratch-mode-hook)))) + (when (not exists) + (add-hook 'kill-buffer-hook + #'spacemacs//confirm-kill-buffer + nil t) + (when (and (not (eq major-mode dotspacemacs-scratch-mode)) + (fboundp dotspacemacs-scratch-mode)) + (funcall dotspacemacs-scratch-mode) + (run-hooks 'spacemacs-scratch-mode-hook))))) (defvar spacemacs--killed-buffer-list nil "List of recently killed buffers.")