;; Copyright (C) 2003 Erik Johansson ;; This program is free software; you can redistribute it and/or ;; modify it under the terms of the GNU General Public License as ;; published by the Free Software Foundation; either version 2 of ;; the License, or (at your option) any later version. ;; This program is distributed in the hope that it will be ;; useful, but WITHOUT ANY WARRANTY; without even the implied ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ;; PURPOSE. See the GNU General Public License for more details. ;; You should have received a copy of the GNU General Public ;; License along with this program; if not, write to the Free ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, ;; MA 02111-1307 USA ;; Changelog: ;; 2003-05-03 [0.1.4]: ;; * Added switch-current-buffer-nr-string to support bs. ;; 2003-05-03 [0.1.3]: ;; * Made switch-buffer-list a var and cleaned up the code after ;; suggestion from Kalle Svensson. ;; * Made the buffer-list-size configurable. ;; 2003-04-11 [0.1.2]: ;; * Documentation. ;; 2003-04-09 [0.1.1]: ;; * Cleaned up the code. ;; * Added switch-auto-set-buffer. ;; 2003-04-09 [0.1]: ;; * Created and first release. ;; The purpose of this script is to make it easier to switch ;; between several buffers. This is done by pressing a "bindkey" ;; (default: C-M-<0-9>) which associates the current buffer ;; with a "setkey" (default: M-<0-9>). The buffer can then ;; become the active one by pressing the "setkey". ;; ;; If you don't want to press a "bindkey" every time you open ;; a file, activate auto-set (default: on) and every file you ;; open is automatically associated with the first free "setkey". ;; ;; To load this script at startup add this to your .emacs. ;; (load "/path/to/switch-buffer.el") ;; If you want the buffer list to show the key associated with a buffer ;; ddd this to your .emacs. This will change the default buffer list to ;; bs and add a K column to the output showing the key. ;; (global-set-key (kbd "C-x C-b") 'bs-show) ;; (setq bs-attributes-list ;; '(("" 1 1 left bs--get-marked-string) ;; ("M" 1 1 left bs--get-modified-string) ;; ("R" 1 1 left bs--get-readonly-string) ;; ("K" 3 3 left switch-current-buffer-nr-string) ;; ("Buffer" bs--get-name-length 10 left bs--get-name) ;; ("" 1 1 left " ") ;; ("Size" 8 8 right bs--get-size-string) ;; ("" 1 1 left " ") ;; ("Mode" 12 12 right bs--get-mode-name) ;; ("" 2 2 left " ") ;; ("File" 12 12 left bs--get-file-name) ;; ("" 2 2 left " "))) ;; ========== Settings ========== ;; Comment out this if you don't want auto-set. (add-hook 'find-file-hooks '(lambda () (switch-auto-set-buffer (current-buffer)))) ;; Numbers of buffers. You need to add/remove keybindings if you change ;; this or else you'll get an error. (defconst switch-buffer-list-size 10 "Number of buffers.") ;; Setup "setkeys" for accessing buffers. (global-set-key (kbd "M-1") (lambda () (interactive) (switch-get-buffer 1))) (global-set-key (kbd "M-2") (lambda () (interactive) (switch-get-buffer 2))) (global-set-key (kbd "M-3") (lambda () (interactive) (switch-get-buffer 3))) (global-set-key (kbd "M-4") (lambda () (interactive) (switch-get-buffer 4))) (global-set-key (kbd "M-5") (lambda () (interactive) (switch-get-buffer 5))) (global-set-key (kbd "M-6") (lambda () (interactive) (switch-get-buffer 6))) (global-set-key (kbd "M-7") (lambda () (interactive) (switch-get-buffer 7))) (global-set-key (kbd "M-8") (lambda () (interactive) (switch-get-buffer 8))) (global-set-key (kbd "M-9") (lambda () (interactive) (switch-get-buffer 9))) (global-set-key (kbd "M-0") (lambda () (interactive) (switch-get-buffer 10))) ;; Setup "bindkeys" for associate buffer with key. (global-set-key (kbd "C-M-1") (lambda () (interactive) (switch-set-buffer (current-buffer) 1))) (global-set-key (kbd "C-M-2") (lambda () (interactive) (switch-set-buffer (current-buffer) 2))) (global-set-key (kbd "C-M-3") (lambda () (interactive) (switch-set-buffer (current-buffer) 3))) (global-set-key (kbd "C-M-4") (lambda () (interactive) (switch-set-buffer (current-buffer) 4))) (global-set-key (kbd "C-M-5") (lambda () (interactive) (switch-set-buffer (current-buffer) 5))) (global-set-key (kbd "C-M-6") (lambda () (interactive) (switch-set-buffer (current-buffer) 6))) (global-set-key (kbd "C-M-7") (lambda () (interactive) (switch-set-buffer (current-buffer) 7))) (global-set-key (kbd "C-M-8") (lambda () (interactive) (switch-set-buffer (current-buffer) 8))) (global-set-key (kbd "C-M-9") (lambda () (interactive) (switch-set-buffer (current-buffer) 9))) (global-set-key (kbd "C-M-0") (lambda () (interactive) (switch-set-buffer (current-buffer) 10))) ;; ======== End settings ======== (defvar switch-buffer-list (make-vector switch-buffer-list-size 'nil) "The variable which keeps all the saved buffers.") ;; Saves buffer as nr - 1. (defun switch-set-buffer (buffer nr) "Saves BUFFER as NR." (if (and (> nr 0) (<= nr switch-buffer-list-size)) (progn (aset switch-buffer-list (1- nr) buffer) (message "Buffer added as no. %i." nr)) (message "Key (%i) out of range (1-%i)." nr switch-buffer-list-size))) ;; Activates buffer nr - 1. (defun switch-get-buffer (nr) "Sets current buffer to saved buffer NR or displays error message if no such buffer exists." (if (and (> nr 0) (<= nr switch-buffer-list-size)) (let ((buffer (aref switch-buffer-list (1- nr)))) (if (and (bufferp buffer) (buffer-name buffer)) (switch-to-buffer buffer) (message "No such buffer."))) (message "Key (%i) out of range (1-%i)." nr switch-buffer-list-size))) ;; Handles auto-set. (defun switch-auto-set-buffer (buffer) "Add BUFFER to first free slot in switch-buffer-list. If no free slots exists, display error message." (catch 'loop (dotimes (i switch-buffer-list-size) (let ((buf (aref switch-buffer-list i))) (unless (and (bufferp buf) (buffer-name buf)) (progn (switch-set-buffer buffer (1+ i)) (throw 'loop t))))) (message "No free slots."))) (defun switch-current-buffer-nr-string (start-buffer all-buffers) "Returns the position in switch-buffer-list for current buffer as a string." (catch 'found (let ((buf (current-buffer))) (dotimes (i switch-buffer-list-size) (when (eq buf (aref switch-buffer-list i)) (throw 'found (int-to-string (1+ i)))))) ""))