;; (C) Copyright Collin J. Doering 2014 ;; ;; 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 3 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, see . ;; File: scratches.el ;; Author: Collin J. Doering ;; Date: Oct 22, 2014 (defvar scratch-buffer-alist '((python-mode . "# This buffer is for notes you don't want to save, and for Lisp evaluation.\n# If you want to create a file, visit that file with C-x C-f,\n# then enter the text in that file's own buffer."))) (defun open-scratch-buffer (&optional buf-mode buf-name msg) "Opens a scratch buffer; if none exists creates one. When called with the universal argument (C-u) will ask what mode to use for the scratch buffer." (interactive (cond ((equal current-prefix-arg nil) ;; universal argument not called (list initial-major-mode "*scratch*" initial-scratch-message)) ((equal current-prefix-arg '(4)) ;; Universal argument called (C-u) (let* ((buf-mode (read-command "Mode: " initial-major-mode)) (buf-name (if (equal buf-mode initial-major-mode) "*scratch*" (concat "*scratch:" (symbol-name buf-mode) "*"))) (msg "")) (list buf-mode buf-name msg))))) (let* ((scratch-buffer (get-buffer buf-name))) ;; check if the scratchpad is open. If not create it, change its mode and insert message text at the top of the buffer (if (null scratch-buffer) (with-current-buffer (get-buffer-create buf-name) (funcall buf-mode) (insert msg))) (switch-to-buffer buf-name))) ;; Bind a key to grab a scratchpad (define-key ctl-x-4-map "s" 'open-scratch-buffer)