Rewrite custom scratch buffer module

This commit is contained in:
Henrik Lissner 2017-01-06 18:42:46 -05:00
parent 75265be961
commit cf8efe91da
2 changed files with 168 additions and 155 deletions

View file

@ -8,128 +8,136 @@
(defvar doom-buffer nil (defvar doom-buffer nil
"The global and persistent scratch buffer for doom.") "The global and persistent scratch buffer for doom.")
(defvar doom-buffer-name " *doom*"
(defvar doom-scratch-name " *doom*"
"The name of the doom scratch buffer.") "The name of the doom scratch buffer.")
(defvar doom-buffer-edited nil
(defvar doom-scratch-edited nil
"If non-nil, the scratch buffer has been edited.") "If non-nil, the scratch buffer has been edited.")
(defvar doom-buffer-inhibit-refresh nil
(defvar doom-scratch-inhibit-refresh nil
"If non-nil, the doom buffer won't be refreshed.") "If non-nil, the doom buffer won't be refreshed.")
(defvar doom-scratch-modeline (doom-modeline 'scratch)
"Modeline format for doom scratch buffer.")
(defvar doom-scratch-widgets '(banner shortmenu loaded)
"List of widgets to display in a blank scratch buffer.")
(define-derived-mode doom-mode fundamental-mode (define-derived-mode doom-mode fundamental-mode
(concat "v" doom-version) (concat "v" doom-version)
"Major mode for special DOOM buffers.") "Major mode for the DOOM scratch buffer.")
;; Don't kill the scratch buffer (defvar doom-scratch--width 0)
(add-hook! 'kill-buffer-query-functions (defvar doom-scratch--height 0)
(not (eq doom-buffer (current-buffer))))
(add-hook 'emacs-startup-hook 'doom--reload-scratch-buffer)
;; Don't rename these buffers. That could cause problems. ;;
(after! uniquify (add-hook 'emacs-startup-hook 'doom-scratch)
(setq uniquify-ignore-buffers-re (regexp-quote doom-buffer-name))) (add-hook! 'kill-buffer-query-functions (not (doom-scratch-buffer-p)))
(add-hook! window-setup
(add-hook 'window-configuration-change-hook 'doom-scratch-reload)
(doom-scratch-reload))
(defun doom*scratch-split-hack (&rest _)
"Removes the window margins before attempting a vertical-split on the scratch
buffer. Without this, it would refuse to split, saying 'too small to split'."
(when (eq (current-buffer) doom-buffer)
(set-window-margins nil 0 0)))
(advice-add 'split-window :before 'doom*scratch-split-hack)
(defun doom|mode-erase-on-insert () ;;
"Erase the buffer and prepare it to be used like a normal buffer." (defun doom-scratch-buffer-p (&optional buffer)
(erase-buffer) (let ((buffer (or buffer (current-buffer))))
(set-window-margins (get-buffer-window doom-buffer) 0 0) (and (buffer-live-p buffer)
(setq doom-buffer-edited t (eq buffer doom-buffer))))
mode-line-format (doom-modeline)
doom--scratch-width nil)
(remove-hook 'evil-insert-state-entry-hook 'doom|mode-erase-on-insert t))
(defun doom-reload-scratch-buffer (&optional dir) (defun doom-scratch-buffer ()
"Update the DOOM scratch buffer (or create it, if it doesn't exist)." "Ensure the scratch buffer exists and is alive (otherwise create it)."
(when (and (not doom-buffer-inhibit-refresh)
(get-buffer-window-list doom-buffer nil t)
(or (not doom-buffer-edited) dir)
(not (minibuffer-window-active-p (minibuffer-window))))
(doom--reload-scratch-buffer dir)))
(defvar doom--scratch-width nil)
(defvar doom--scratch-height nil)
(defun doom--reload-scratch-buffer (&optional dir)
;; Rename the old scratch buffer, if it exists. ;; Rename the old scratch buffer, if it exists.
(let ((old-scratch (get-buffer "*scratch*"))) (let ((old-scratch (get-buffer "*scratch*")))
(when old-scratch (when old-scratch (kill-buffer old-scratch)))
(with-current-buffer old-scratch
(rename-buffer doom-buffer-name)
(setq doom-buffer old-scratch))))
;; Ensure the doom buffer is alive! ;; Ensure the doom buffer is alive!
(unless (buffer-live-p doom-buffer) (unless (buffer-live-p doom-buffer)
(setq doom-buffer nil)) (setq doom-buffer nil))
(unless doom-buffer (unless doom-buffer
(setq doom-buffer (get-buffer-create doom-buffer-name))) (setq doom-buffer (get-buffer-create doom-scratch-name)))
;; Fill it with the splash screen content doom-buffer)
(with-current-buffer doom-buffer
(doom-mode)
(add-hook 'evil-insert-state-entry-hook 'doom|mode-erase-on-insert nil t)
(add-hook 'after-change-major-mode-hook 'doom|mode-erase-on-insert nil t)
(setq doom-buffer-edited nil)
(let ((width 78)
updates-p height)
(mapc (lambda (window)
(set-window-margins window 0 0)
(let ((pad (max 0 (- (truncate (/ (window-width window) 2)) (truncate (/ width 2))))))
(set-window-margins window pad pad)
(setq height (max 0
(min (or height 9999)
(- (truncate (/ (window-height window) 2)) 12))))))
(get-buffer-window-list doom-buffer nil t))
(when (or (not doom--scratch-width)
(not doom--scratch-height)
(/= doom--scratch-width width)
(/= doom--scratch-height height))
(erase-buffer)
(insert (make-string (if height (max 0 height) 0) ?\n)
(propertize
(concat
"================= =============== =============== ======== ========\n"
"\\\\ . . . . . . .\\\\ //. . . . . . .\\\\ //. . . . . . .\\\\ \\\\. . .\\\\// . . //\n"
"||. . ._____. . .|| ||. . ._____. . .|| ||. . ._____. . .|| || . . .\\/ . . .||\n"
"|| . .|| ||. . || || . .|| ||. . || || . .|| ||. . || ||. . . . . . . ||\n"
"||. . || || . .|| ||. . || || . .|| ||. . || || . .|| || . | . . . . .||\n"
"|| . .|| ||. _-|| ||-_ .|| ||. . || || . .|| ||. _-|| ||-_.|\\ . . . . ||\n"
"||. . || ||-' || || `-|| || . .|| ||. . || ||-' || || `|\\_ . .|. .||\n"
"|| . _|| || || || || ||_ . || || . _|| || || || |\\ `-_/| . ||\n"
"||_-' || .|/ || || \\|. || `-_|| ||_-' || .|/ || || | \\ / |-_.||\n"
"|| ||_-' || || `-_|| || || ||_-' || || | \\ / | `||\n"
"|| `' || || `' || || `' || || | \\ / | ||\n"
"|| .===' `===. .==='.`===. .===' /==. | \\/ | ||\n"
"|| .==' \\_|-_ `===. .===' _|_ `===. .===' _-|/ `== \\/ | ||\n"
"|| .==' _-' `-_ `=' _-' `-_ `=' _-' `-_ /| \\/ | ||\n"
"|| .==' _-' '-__\\._-' '-_./__-' `' |. /| | ||\n"
"||.==' _-' `' | /==.||\n"
"==' _-' E M A C S \\/ `==\n"
"\\ _-' `-_ /\n"
" `'' ``'")
'face 'font-lock-comment-face)
"\n\n"
(s-center 73 (doom--scratch-info))
"\n\n"
(s-center
78 (propertize (format "Loaded %d packages in %s"
(length doom-packages)
(emacs-init-time))
'face '(:inherit font-lock-comment-face
:height 0.9))))
(setq doom--scratch-width width
doom--scratch-height height)))
(goto-char 1521)
(when dir (setq default-directory dir))
(setq mode-line-format (doom-modeline 'scratch))
;; Readjust the scratch buffer if it is visible, when the frame changes.
(add-hook 'window-configuration-change-hook 'doom-reload-scratch-buffer)))
;; TODO Less hard-coded (defun doom-scratch ()
(defun doom--scratch-info () (interactive)
(doom-scratch-reload)
(switch-to-buffer doom-buffer)
nil)
(defun doom-scratch-force-reload ()
(setq doom-scratch-edited nil)
(doom-scratch-reload))
(defun doom|scratch-clear-on-insert ()
"Erase the buffer and prepare it to be used like a normal buffer."
(erase-buffer)
;; (set-window-margins (get-buffer-window doom-buffer) 0 0)
(setq doom-scratch-edited t
mode-line-format (doom-modeline))
(remove-hook 'evil-insert-state-entry-hook 'doom|mode-erase-on-insert t))
(defun doom-scratch-reload (&optional dir)
"Update the DOOM scratch buffer (or create it, if it doesn't exist)."
(when (and (not doom-scratch-inhibit-refresh)
(not (minibuffer-window-active-p (minibuffer-window)))
(get-buffer-window-list doom-buffer nil t)
(or (not doom-scratch-edited) dir))
(let ((old-pwd (or dir default-directory)))
(with-current-buffer (doom-scratch-buffer)
(doom-mode)
(add-hook 'evil-insert-state-entry-hook 'doom|scratch-clear-on-insert nil t)
(add-hook 'after-change-major-mode-hook 'doom|scratch-clear-on-insert nil t)
(setq doom-scratch-edited nil)
(erase-buffer)
(let ((doom-scratch--width (1- (window-width (get-buffer-window doom-buffer))))
(doom-scratch--height (window-height (get-buffer-window doom-buffer))))
(insert (make-string (max 0 (- (truncate (/ doom-scratch--height 2)) 12)) ?\n))
(mapc (lambda (widget-name)
(funcall (intern (format "doom-scratch-widget-%s" widget-name)))
(insert "\n\n"))
doom-scratch-widgets))
(setq default-directory old-pwd)
(setq mode-line-format (doom-modeline 'scratch)))))
t)
(defun doom-scratch-widget-banner ()
(mapc (lambda (line)
(insert "\n")
(insert (propertize (s-center doom-scratch--width line)
'face 'font-lock-comment-face) " "))
'("================= =============== =============== ======== ========"
"\\\\ . . . . . . .\\\\ //. . . . . . .\\\\ //. . . . . . .\\\\ \\\\. . .\\\\// . . //"
"||. . ._____. . .|| ||. . ._____. . .|| ||. . ._____. . .|| || . . .\\/ . . .||"
"|| . .|| ||. . || || . .|| ||. . || || . .|| ||. . || ||. . . . . . . ||"
"||. . || || . .|| ||. . || || . .|| ||. . || || . .|| || . | . . . . .||"
"|| . .|| ||. _-|| ||-_ .|| ||. . || || . .|| ||. _-|| ||-_.|\\ . . . . ||"
"||. . || ||-' || || `-|| || . .|| ||. . || ||-' || || `|\\_ . .|. .||"
"|| . _|| || || || || ||_ . || || . _|| || || || |\\ `-_/| . ||"
"||_-' || .|/ || || \\|. || `-_|| ||_-' || .|/ || || | \\ / |-_.||"
"|| ||_-' || || `-_|| || || ||_-' || || | \\ / | `||"
"|| `' || || `' || || `' || || | \\ / | ||"
"|| .===' `===. .==='.`===. .===' /==. | \\/ | ||"
"|| .==' \\_|-_ `===. .===' _|_ `===. .===' _-|/ `== \\/ | ||"
"|| .==' _-' `-_ `=' _-' `-_ `=' _-' `-_ /| \\/ | ||"
"|| .==' _-' '-__\\._-' '-_./__-' `' |. /| | ||"
"||.==' _-' `' | /==.||"
"==' _-' E M A C S \\/ `=="
"\\ _-' `-_ /"
" `'' ``'")))
(defun doom-scratch-widget-loaded ()
(insert
(s-center (1- doom-scratch--width)
(propertize
(format "Loaded %d packages in %s"
(length doom-packages)
(emacs-init-time))
'face '(:inherit font-lock-comment-face
:height 0.9)))))
(defun doom-scratch-widget-shortmenu ()
(let ((all-the-icons-scale-factor 1.3) (let ((all-the-icons-scale-factor 1.3)
(all-the-icons-default-adjust -0.05) (all-the-icons-default-adjust -0.05)
(start (point)) (start (point))
@ -139,48 +147,50 @@ buffer. Without this, it would refuse to split, saying 'too small to split'."
end) end)
(unless last-session-p (unless last-session-p
(setq sep " ")) (setq sep " "))
(with-temp-buffer (insert
(insert-text-button (s-center (- doom-scratch--width 5)
(concat (all-the-icons-octicon (with-temp-buffer
"mark-github" (insert-text-button
:face 'font-lock-keyword-face) (concat (all-the-icons-octicon
(propertize " Homepage" 'face 'font-lock-keyword-face)) "mark-github"
'action '(lambda (_) (browse-url "https://github.com/hlissner/.emacs.d")) :face 'font-lock-keyword-face)
'follow-link t) (propertize " Homepage" 'face 'font-lock-keyword-face))
'action '(lambda (_) (browse-url "https://github.com/hlissner/.emacs.d"))
'follow-link t)
(insert sep " ") (insert sep " ")
(insert-text-button (insert-text-button
(concat (all-the-icons-octicon (concat (all-the-icons-octicon
"file-text" "file-text"
:face 'font-lock-keyword-face) :face 'font-lock-keyword-face)
(propertize " Recent files" 'face 'font-lock-keyword-face)) (propertize " Recent files" 'face 'font-lock-keyword-face))
'action '(lambda (_) (call-interactively 'counsel-recentf)) 'action '(lambda (_) (call-interactively 'counsel-recentf))
'follow-link t) 'follow-link t)
(insert sep) (insert sep)
(insert-text-button (insert-text-button
(concat (all-the-icons-octicon (concat (all-the-icons-octicon
"tools" "tools"
:face 'font-lock-keyword-face) :face 'font-lock-keyword-face)
(propertize " Edit emacs.d" 'face 'font-lock-keyword-face)) (propertize " Edit emacs.d" 'face 'font-lock-keyword-face))
'action '(lambda (_) (find-file (f-expand "init.el" doom-emacs-dir))) 'action '(lambda (_) (find-file (f-expand "init.el" doom-emacs-dir)))
'follow-link t) 'follow-link t)
(when last-session-p (when last-session-p
(insert sep) (insert sep)
(insert-text-button (insert-text-button
(concat (all-the-icons-octicon (concat (all-the-icons-octicon
"history" "history"
:face 'font-lock-keyword-face) :face 'font-lock-keyword-face)
(propertize " Reload last session" 'face 'font-lock-keyword-face)) (propertize " Reload last session" 'face 'font-lock-keyword-face))
'action '(lambda (_) (doom:workgroup-load)) 'action '(lambda (_) (doom:workgroup-load))
'follow-link t)) 'follow-link t))
(setq end (point)) (setq end (point))
(buffer-string)))) (buffer-string))))))
(provide 'core-scratch) (provide 'core-scratch)
;;; core-scratch.el ends here ;;; core-scratch.el ends here

View file

@ -122,23 +122,24 @@ NOTE: only buries scratch buffer.
See `doom/real-buffer-p' for what 'real' means." See `doom/real-buffer-p' for what 'real' means."
(interactive (list t)) (interactive (list t))
(let* ((scratch-p (eq (current-buffer) doom-buffer)) (let* ((scratch-p (doom-scratch-buffer-p))
(old-project (doom/project-root)) (old-project (doom/project-root))
(buffer (current-buffer)) (buffer (current-buffer))
(only-buffer-window-p (= (length (get-buffer-window-list buffer nil t)) 1))) (only-buffer-window-p (= (length (get-buffer-window-list buffer nil t)) 1)))
(unless scratch-p (unless scratch-p
(when (and only-buffer-window-p buffer-file-name (buffer-modified-p)) (when (and buffer-file-name only-buffer-window-p (buffer-modified-p))
(if (yes-or-no-p "Buffer is unsaved, save it?") (if (yes-or-no-p "Buffer is unsaved, save it?")
(save-buffer) (save-buffer)
(set-buffer-modified-p nil))) (set-buffer-modified-p nil)))
(when arg (when arg
(doom/previous-real-buffer) (doom/previous-real-buffer)
(when (eq buffer (current-buffer)) (unless (eq (current-buffer) buffer)
(switch-to-buffer doom-buffer t t)) (when only-buffer-window-p
(when only-buffer-window-p (kill-buffer buffer)
(kill-buffer buffer)))) (unless (doom/real-buffer-p)
(when (eq (current-buffer) doom-buffer) (doom/previous-real-buffer)))))))
(doom-reload-scratch-buffer old-project))) (when (doom-scratch-buffer-p)
(doom-scratch-force-reload))
t) t)
;;;###autoload ;;;###autoload
@ -194,23 +195,26 @@ nothing left, create a scratch buffer."
(i 0) (i 0)
(continue t) (continue t)
(buffers (doom/get-real-buffers (doom/get-buffers t))) (buffers (doom/get-real-buffers (doom/get-buffers t)))
(fail-buffer (if (> (length (get-buffer-window-list doom-buffer nil t)) 1)
start-buffer
doom-buffer))
destbuf) destbuf)
(setq destbuf (setq destbuf
(catch 'goto (catch 'goto
(if (or (not buffers) (if (or (not buffers)
(= (length buffers) 1)) (= (length buffers) 1))
(progn (message "No other buffers in workgroup") (progn (message "No other buffers in workgroup")
(throw 'goto (current-buffer))) (throw 'goto fail-buffer))
(funcall move-func) (funcall move-func)
(while (not (memq (current-buffer) buffers)) (while (not (memq (current-buffer) buffers))
(if (or (eq (current-buffer) start-buffer) (if (or (eq (current-buffer) start-buffer)
(>= i max)) (>= i max))
(throw 'goto doom-buffer) (throw 'goto fail-buffer)
(funcall move-func)) (funcall move-func))
(cl-incf i)) (cl-incf i))
(current-buffer)))) (current-buffer))))
(when (eq destbuf doom-buffer) (when (eq destbuf doom-buffer)
(doom-reload-scratch-buffer) (doom-scratch-reload)
(message "Nowhere to go")) (message "Nowhere to go"))
(switch-to-buffer destbuf))) (switch-to-buffer destbuf)))
@ -222,12 +226,11 @@ popup (or temporary) window and b) it isn't a special buffer (e.g. scratch or
(setq buffer (or (and (bufferp buffer) buffer) (setq buffer (or (and (bufferp buffer) buffer)
(and (stringp buffer) (get-buffer buffer)) (and (stringp buffer) (get-buffer buffer))
(current-buffer))) (current-buffer)))
(or (eq buffer doom-buffer) (when (buffer-live-p buffer)
(when (buffer-live-p buffer) (with-current-buffer buffer
(with-current-buffer buffer (not (or (apply #'derived-mode-p (-filter 'symbolp doom-unreal-buffers))
(not (or (apply #'derived-mode-p (-filter 'symbolp doom-unreal-buffers)) (--any? (string-match-p it (buffer-name buffer))
(--any? (string-match-p it (buffer-name buffer)) (-filter 'stringp doom-unreal-buffers)))))))
(-filter 'stringp doom-unreal-buffers))))))))
;;;###autoload ;;;###autoload
(defun doom/next-real-buffer () (defun doom/next-real-buffer ()