;-*- coding: utf-8 -*- ; http://xahlee.org/emacs/ergonomic_emacs_keybinding.html ; This file is licensed under the GNU General Public License. ; First version (and date): 2007-08-01 ; version: 2008-06-22, added new-empty-buffer and modified close-current-buffer ; Author: Xah Lee ; ∑ http://xahlee.org/ (defun remove-hard-wrap-paragraph () "Replace line endings into single spaces on the current paragraph." (interactive) (let ((fill-column 90002000)) (fill-paragraph nil))) (defun remove-hard-wrap-region (start end) "Replace newline chars in region by single spaces." (interactive "r") (let ((fill-column 90002000)) (fill-region start end))) (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) ))) (defun next-emacs-buffer () "Switch to the next emacs buffer in cyclic order.\n Emacs buffers are those starting with *." (interactive) (next-buffer) (let ((i 0)) (while (and (not (string-match "^*" (buffer-name))) (< i 50)) (setq i (1+ i)) (next-buffer) ))) (defun previous-emacs-buffer () "Switch to the previous emacs buffer in cyclic order.\n Emacs buffers are those starting with *." (interactive) (previous-buffer) (let ((i 0)) (while (and (not (string-match "^*" (buffer-name))) (< i 50)) (setq i (1+ i)) (previous-buffer) ))) (defun new-empty-buffer () "Opens a new empty buffer." (interactive) (let ((buf (generate-new-buffer "untitled"))) (switch-to-buffer buf) (funcall (and initial-major-mode)) (setq buffer-offer-save t))) ;; note: emacs won't offer to save a buffer that's ;; not associated with a file, ;; even if buffer-modified-p is true. ;; One work around is to define your own my-kill-buffer function ;; that wraps around kill-buffer, and check on the buffer modification ;; status to offer save ;; This custome kill buffer is close-current-buffer. (defun close-current-buffer () "Close the current buffer. Similar to (kill-buffer (current-buffer)) but does some buffer switching after kill, and also offer save if the buffer has been modified, even if the buffer is not associated with a file. Also, make sure that after closing, the buffer shown is a user buffer. A emacs buffer is one who's name starts with *. Else it is a user buffer." (interactive) (let (isEmacsBufferBefore isEmacsBufferAfter) (if (string-match "^*" (buffer-name)) (setq isEmacsBufferBefore t) (setq isEmacsBufferBefore 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 isEmacsBufferBefore) (not (string-equal mode-name "Dired by name")) (not (string-equal mode-name "Dired by date")) (not (string-equal "" (save-restriction (widen) (buffer-string))))) (if (y-or-n-p (concat "Buffer " (buffer-name) " modified; Do you want to save?")) (save-buffer) (set-buffer-modified-p nil))) ;; close (kill-buffer (current-buffer)) ;; if emacs buffer, switch (if (string-match "^*" (buffer-name)) (setq isEmacsBufferAfter t) (setq isEmacsBufferAfter nil)) (when isEmacsBufferAfter (previous-user-buffer) ) ) ) (defun close-current-buffer2 () "Close the current buffer. Similar to (kill-buffer (current-buffer)) but does some buffer switching after kill, and also offer save if the buffer has been modified, even if the buffer is not associated with a file. If the current buffer is a user buffer, then make sure the buffer shown after kill is also a user buffer. If the current buffer is a emacs buffer, then make sure the buffer shown after kill is also a emacs buffer. A emacs buffer is one who's name starts with *. Else it is a user buffer." (interactive) (let (isEmacsBufferBefore isEmacsBufferAfter) (if (string-match "^*" (buffer-name)) (setq isEmacsBufferBefore t) (setq isEmacsBufferBefore 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 isEmacsBufferBefore) (not (string-equal mode-name "dired")) (not (string-equal "" (save-restriction (widen) (buffer-string))))) (if (y-or-n-p (concat "Buffer " (buffer-name) " modified; Do you want to save?")) (save-buffer) (set-buffer-modified-p nil))) ;; close (kill-buffer (current-buffer)) ;; switch (if (string-match "^*" (buffer-name)) (setq isEmacsBufferAfter t) (setq isEmacsBufferAfter nil)) (when (not (equal isEmacsBufferBefore isEmacsBufferAfter)) (if isEmacsBufferBefore (previous-emacs-buffer) (previous-user-buffer)) ) ) )