Skip to content

Instantly share code, notes, and snippets.

@lateau
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lateau/9d27fcf5b697483eef5a to your computer and use it in GitHub Desktop.
Save lateau/9d27fcf5b697483eef5a to your computer and use it in GitHub Desktop.
create-scratch-buffer.el
(defmacro create-scratch-buffer (name &optional comment body)
"Create a new custom scratch buffer. The name should be a valid major mode name without -mode suffix.
The body should be lambda block or a symbol of function that be invoked after a scratch buffer created. nil is accepted to turn it off."
`(defun ,(intern (concat "scratch-" name))
()
(interactive)
(let ((bufname (concat "*scratch-" ,name "*")))
(if (buffer-live-p (get-buffer bufname))
(switch-to-buffer bufname)
(with-current-buffer (get-buffer-create bufname)
(setq buffer-read-only nil)
(when ,comment
(insert-string (concat ,comment "\n\n")))
(,(intern (concat name "-mode")))
(when ,body
(funcall ,body))
(delete-other-windows)
(switch-to-buffer bufname))))))
;;; Examples
;; scratch-sql
;; C-c C-c to send a last line to *SQL* buffer.
(setenv "MYSQL_PS1" "")
(create-scratch-buffer
"sql"
"-- This buffer is for notes you don't want to save, and for SQL evaluation.
-- If you want to create a file, visit that file with C-x C-f,
-- then enter the text in that file's own buffer."
'(lambda ()
(sql-product-interactive
(completing-read
"Product: "
'(ansi db2 informix ingres interbase linter microsoft mysql oracle postgres solid sqlite sybase)))))
;; scratch-lisp
(setq inferior-lisp-program "clisp")
(create-scratch-buffer
"lisp"
";; This buffer is for notes you don't want to save, and for Common Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer."
#'slime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment