Rewrite scratch buffer commands
+ No longer open persistent scratch buffers by default. Supply the universal argument to do that. SPC x = open throw-away scratch buffer SPC u SPC x = open persistent scratch buffer (prompted for file) + Added doom/delete-scratch-files
This commit is contained in:
parent
1aa108ce02
commit
7afc62fb1d
1 changed files with 56 additions and 35 deletions
|
@ -4,53 +4,74 @@
|
||||||
"Where to store project scratch files, created by
|
"Where to store project scratch files, created by
|
||||||
`doom/open-project-scratch-buffer'.")
|
`doom/open-project-scratch-buffer'.")
|
||||||
|
|
||||||
|
(defvar doom-scratch-buffer-display-fn #'display-buffer
|
||||||
|
"TODO")
|
||||||
|
|
||||||
(defvar doom-scratch-buffer-hook ()
|
(defvar doom-scratch-buffer-hook ()
|
||||||
"The hooks to run after a scratch buffer is made.")
|
"The hooks to run after a scratch buffer is made.")
|
||||||
|
|
||||||
(defun doom--create-scratch-buffer ()
|
|
||||||
(let* ((project-p (doom-project-p 'nocache))
|
;;
|
||||||
(text (and (region-active-p)
|
;; Library
|
||||||
(buffer-substring-no-properties
|
;;
|
||||||
(region-beginning) (region-end))))
|
|
||||||
(mode major-mode)
|
;;;###autoload
|
||||||
(derived-p (derived-mode-p 'prog-mode 'text-mode))
|
(defun doom-scratch-buffer (&optional file mode text)
|
||||||
(project-root (if project-p (doom-project-root 'nocache)))
|
"Return a scratchpad buffer in major MODE with TEXT in it.
|
||||||
(id (projectile-project-name)))
|
|
||||||
(when (and id (string-empty-p id))
|
If FILE is a valid path, open it as if it were a persistent scratchpad."
|
||||||
(user-error "Invalid id for a scratch buffer (%s)" id))
|
(if file (setq file (file-truename file)))
|
||||||
(unless (file-directory-p doom-scratch-files-dir)
|
(let ((buffer
|
||||||
(make-directory doom-scratch-files-dir t))
|
(if file
|
||||||
(with-current-buffer
|
(with-current-buffer (find-file-noselect file)
|
||||||
(if project-p
|
(rename-buffer (format "*doom:scratch (%s)*" (file-name-nondirectory file)))
|
||||||
(find-file-noselect
|
(current-buffer))
|
||||||
(expand-file-name (replace-regexp-in-string
|
(get-buffer-create "*doom:scratch*"))))
|
||||||
"\\." "_" id
|
(with-current-buffer buffer
|
||||||
t t)
|
|
||||||
doom-scratch-files-dir)
|
|
||||||
nil t)
|
|
||||||
(get-buffer-create "*doom:scratch*"))
|
|
||||||
(when project-p
|
|
||||||
(rename-buffer (format "*doom:scratch (%s)*" id))
|
|
||||||
(setq default-directory project-root))
|
|
||||||
(when (and (not (eq major-mode mode))
|
(when (and (not (eq major-mode mode))
|
||||||
derived-p
|
|
||||||
(functionp mode))
|
(functionp mode))
|
||||||
(funcall mode))
|
(funcall mode))
|
||||||
(if text (insert text))
|
(when text
|
||||||
|
(insert text))
|
||||||
(run-hooks 'doom-scratch-buffer-hook)
|
(run-hooks 'doom-scratch-buffer-hook)
|
||||||
(current-buffer))))
|
(current-buffer))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom/open-scratch-buffer (&optional arg)
|
(defun doom/open-scratch-buffer (&optional arg)
|
||||||
"Opens a persistent scratch buffer in the same major-mode, at the current
|
"Opens a scratch pad window in the same major-mode.
|
||||||
project root. If no project is found, scratch buffer will not be persistent. If
|
|
||||||
a selection is active, copy it to the scratch buffer.
|
|
||||||
|
|
||||||
If ARG (universal argument) is non-nil, display the buffer in the current
|
If ARG (universal argument), then open a persistent scratch pad buffer. You'll
|
||||||
window. Otherwise, display it in a pop up window.
|
be prompted for its name, or to open a previously created. These are stored in
|
||||||
|
`doom-scratch-files-dir'.
|
||||||
|
|
||||||
Persistent scratch buffers are stored in `doom-scratch-files-dir'."
|
If a region is active, copy its contents to the scratch pad."
|
||||||
(interactive "P")
|
(interactive "P")
|
||||||
(funcall (if arg #'switch-to-buffer #'display-buffer)
|
(let (projectile-enable-caching)
|
||||||
(doom--create-scratch-buffer)))
|
(funcall
|
||||||
|
doom-scratch-buffer-display-fn
|
||||||
|
(doom-scratch-buffer
|
||||||
|
(when arg
|
||||||
|
(if-let* ((file (read-file-name "Open scratch file > " doom-scratch-files-dir "scratch")))
|
||||||
|
file
|
||||||
|
(user-error "Aborting")))
|
||||||
|
major-mode
|
||||||
|
(and (region-active-p)
|
||||||
|
(buffer-substring-no-properties
|
||||||
|
(region-beginning) (region-end)))))))
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun doom/switch-to-scratch-buffer (&optional arg)
|
||||||
|
"Switches to a scratch pad buffer in the current window.
|
||||||
|
|
||||||
|
Otherwise, does exactly what `doom/open-scratch-buffer' does."
|
||||||
|
(interactive "P")
|
||||||
|
(let ((doom-scratch-buffer-display-fn #'switch-to-buffer))
|
||||||
|
(doom/open-scratch-buffer arg)))
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun doom/delete-scratch-files ()
|
||||||
|
"Deletes all scratch buffers in `doom-scratch-files-dir'."
|
||||||
|
(interactive)
|
||||||
|
(dolist (file (directory-files doom-scratch-files-dir t "^[^.]" t))
|
||||||
|
(delete-file file)
|
||||||
|
(message "Deleted '%s'" (file-name-nondirectory file))))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue