2017-09-27 01:21:10 +02:00
|
|
|
;;; core/autoload/scratch.el -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
(defvar doom-scratch-files-dir (concat doom-etc-dir "scratch/")
|
|
|
|
"Where to store project scratch files, created by
|
|
|
|
`doom/open-project-scratch-buffer'.")
|
|
|
|
|
|
|
|
(defvar doom-scratch-buffer-hook ()
|
|
|
|
"The hooks to run after a scratch buffer is made.")
|
|
|
|
|
2018-02-02 19:10:43 -05:00
|
|
|
(defun doom--create-scratch-buffer ()
|
|
|
|
(let* ((project-p (doom-project-p 'nocache))
|
|
|
|
(text (and (region-active-p)
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
(region-beginning) (region-end))))
|
|
|
|
(mode major-mode)
|
|
|
|
(derived-p (derived-mode-p 'prog-mode 'text-mode))
|
|
|
|
(project-root (if project-p (doom-project-root 'nocache)))
|
|
|
|
(id (projectile-project-name)))
|
|
|
|
(when (and id (string-empty-p id))
|
|
|
|
(user-error "Invalid id for a scratch buffer (%s)" id))
|
2017-09-27 01:21:10 +02:00
|
|
|
(unless (file-directory-p doom-scratch-files-dir)
|
2018-02-02 19:10:43 -05:00
|
|
|
(make-directory doom-scratch-files-dir t))
|
2017-09-27 01:21:10 +02:00
|
|
|
(with-current-buffer
|
|
|
|
(if project-p
|
|
|
|
(find-file-noselect
|
|
|
|
(expand-file-name (replace-regexp-in-string
|
2018-02-02 19:10:43 -05:00
|
|
|
"\\." "_" id
|
2017-09-27 01:21:10 +02:00
|
|
|
t t)
|
|
|
|
doom-scratch-files-dir)
|
|
|
|
nil t)
|
|
|
|
(get-buffer-create "*doom:scratch*"))
|
|
|
|
(when project-p
|
2018-02-02 19:10:43 -05:00
|
|
|
(rename-buffer (format "*doom:scratch (%s)*" id))
|
|
|
|
(setq default-directory project-root))
|
2017-09-27 01:21:10 +02:00
|
|
|
(when (and (not (eq major-mode mode))
|
|
|
|
derived-p
|
|
|
|
(functionp mode))
|
|
|
|
(funcall mode))
|
|
|
|
(if text (insert text))
|
|
|
|
(run-hooks 'doom-scratch-buffer-hook)
|
|
|
|
(current-buffer))))
|
|
|
|
|
|
|
|
;;;###autoload
|
2018-02-02 19:10:43 -05:00
|
|
|
(defun doom/open-scratch-buffer (&optional arg)
|
|
|
|
"Opens a persistent scratch buffer in the same major-mode, at the current
|
|
|
|
project root. If no project is found, scratch buffer will not be persistent. If
|
|
|
|
a selection is active, copy it to the scratch buffer.
|
2017-09-27 01:21:10 +02:00
|
|
|
|
2018-02-02 19:10:43 -05:00
|
|
|
If ARG (universal argument) is non-nil, display the buffer in the current
|
|
|
|
window. Otherwise, display it in a pop up window.
|
|
|
|
|
|
|
|
Persistent scratch buffers are stored in `doom-scratch-files-dir'."
|
|
|
|
(interactive "P")
|
|
|
|
(funcall (if arg #'switch-to-buffer #'display-buffer)
|
|
|
|
(doom--create-scratch-buffer)))
|
2017-09-27 01:21:10 +02:00
|
|
|
|