ui/doom-dashboard: refactor & optimize

Potentially addresses crashing issues caused by the dashboard.

Relevant to #1170
This commit is contained in:
Henrik Lissner 2019-05-15 15:13:33 -04:00
parent b821bfca1f
commit 4dbf8f206b
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -70,7 +70,7 @@ Possible values:
("Open user manual" ("Open user manual"
:icon (all-the-icons-octicon "book" :face 'font-lock-keyword-face) :icon (all-the-icons-octicon "book" :face 'font-lock-keyword-face)
:when (file-exists-p (expand-file-name "index.org" doom-docs-dir)) :when (file-exists-p (expand-file-name "index.org" doom-docs-dir))
:action doom/open-manual)) :action doom/help-search))
"An alist of menu buttons used by `doom-dashboard-widget-shortmenu'. Each "An alist of menu buttons used by `doom-dashboard-widget-shortmenu'. Each
element is a cons cell (LABEL . PLIST). LABEL is a string to display after the element is a cons cell (LABEL . PLIST). LABEL is a string to display after the
icon and before the key string. icon and before the key string.
@ -93,6 +93,7 @@ PLIST can have the following properties:
(defvar +doom-dashboard--width 80) (defvar +doom-dashboard--width 80)
(defvar +doom-dashboard--old-fringe-indicator fringe-indicator-alist) (defvar +doom-dashboard--old-fringe-indicator fringe-indicator-alist)
(defvar +doom-dashboard--pwd-alist ()) (defvar +doom-dashboard--pwd-alist ())
(defvar +doom-dashboard--reload-timer nil)
(defvar all-the-icons-scale-factor) (defvar all-the-icons-scale-factor)
(defvar all-the-icons-default-adjust) (defvar all-the-icons-default-adjust)
@ -104,27 +105,24 @@ PLIST can have the following properties:
(defun +doom-dashboard|init () (defun +doom-dashboard|init ()
"Initializes Doom's dashboard." "Initializes Doom's dashboard."
(unless noninteractive (unless noninteractive
;; Ensure the dashboard is up-to-date whenever it is switched to or resized.
(add-hook 'window-configuration-change-hook #'+doom-dashboard|resize)
(add-hook 'window-size-change-functions #'+doom-dashboard|resize)
(add-hook 'kill-buffer-query-functions #'+doom-dashboard|reload-on-kill)
(add-hook 'doom-switch-buffer-hook #'+doom-dashboard|reload-on-kill)
(add-hook 'delete-frame-functions #'+doom-dashboard|reload-frame)
;; `persp-mode' integration: update `default-directory' when switching perspectives
(add-hook 'persp-created-functions #'+doom-dashboard|record-project)
(add-hook 'persp-activated-functions #'+doom-dashboard|detect-project)
(add-hook 'persp-before-switch-functions #'+doom-dashboard|record-project)
;; Ensure the dashboard becomes Emacs' go-to buffer when there's nothing ;; Ensure the dashboard becomes Emacs' go-to buffer when there's nothing
;; else to show. ;; else to show.
(setq doom-fallback-buffer-name +doom-dashboard-name (setq doom-fallback-buffer-name +doom-dashboard-name
initial-buffer-choice #'doom-fallback-buffer) initial-buffer-choice #'doom-fallback-buffer)
(when (equal (buffer-name) "*scratch*") (when (equal (buffer-name) "*scratch*")
(switch-to-buffer (doom-fallback-buffer)) (set-window-buffer nil (doom-fallback-buffer))
(if (daemonp) (if (daemonp)
(add-hook 'after-make-frame-functions #'+doom-dashboard|reload-frame) (add-hook 'after-make-frame-functions #'+doom-dashboard|reload-frame)
(+doom-dashboard-reload))))) (+doom-dashboard-reload)))
;; Ensure the dashboard is up-to-date whenever it is switched to or resized.
(add-hook 'window-configuration-change-hook #'+doom-dashboard|resize)
(add-hook 'window-size-change-functions #'+doom-dashboard|resize)
(add-hook 'doom-switch-buffer-hook #'+doom-dashboard|reload-maybe)
(add-hook 'delete-frame-functions #'+doom-dashboard|reload-frame)
;; `persp-mode' integration: update `default-directory' when switching perspectives
(add-hook 'persp-created-functions #'+doom-dashboard|record-project)
(add-hook 'persp-activated-functions #'+doom-dashboard|detect-project)
(add-hook 'persp-before-switch-functions #'+doom-dashboard|record-project)))
(add-hook 'doom-init-ui-hook #'+doom-dashboard|init) (add-hook 'doom-init-ui-hook #'+doom-dashboard|init)
@ -190,7 +188,7 @@ PLIST can have the following properties:
(defun +doom-dashboard|reposition-point () (defun +doom-dashboard|reposition-point ()
"Trap the point in the buttons." "Trap the point in the buttons."
(when (region-active-p) (when (region-active-p)
(deactivate-mark t) (setq deactivate-mark t)
(when (bound-and-true-p evil-local-mode) (when (bound-and-true-p evil-local-mode)
(evil-change-to-previous-state))) (evil-change-to-previous-state)))
(or (ignore-errors (or (ignore-errors
@ -200,54 +198,53 @@ PLIST can have the following properties:
(progn (goto-char (point-min)) (progn (goto-char (point-min))
(forward-button 1)))) (forward-button 1))))
(defun +doom-dashboard|reload-on-kill () (defun +doom-dashboard|reload-maybe ()
"A `kill-buffer-query-functions' hook. "Reload the dashboard or its state.
If this isn't a dashboard buffer, move along, but record its `default-directory' If this isn't a dashboard buffer, move along, but record its `default-directory'
if the buffer is real. See `doom-real-buffer-p' for an explanation for what if the buffer is real. See `doom-real-buffer-p' for an explanation for what
'real' means. 'real' means.
If this is the dashboard buffer, reload the dashboard." If this is the dashboard buffer, reload it completely."
(or (let ((buf (current-buffer))) (cond ((+doom-dashboard-p (current-buffer))
(unless (+doom-dashboard-p buf) (let (+doom-dashboard-inhibit-refresh)
(when (doom-real-buffer-p buf) (ignore-errors (+doom-dashboard-reload))))
(setq +doom-dashboard--last-cwd default-directory) ((doom-real-buffer-p (current-buffer))
(+doom-dashboard-update-pwd)) (setq +doom-dashboard--last-cwd default-directory)
t)) (+doom-dashboard-update-pwd))))
(ignore
(let (+doom-dashboard-inhibit-refresh)
(ignore-errors (+doom-dashboard-reload))))))
(defun +doom-dashboard|reload-frame (_frame) (defun +doom-dashboard|reload-frame (_frame)
"Reload the dashboard after a brief pause. This is necessary for new frames, "Reload the dashboard after a brief pause. This is necessary for new frames,
whose dimensions may not be fully initialized by the time this is run." whose dimensions may not be fully initialized by the time this is run."
(run-with-timer 0.1 nil #'+doom-dashboard-reload t)) (when (timerp +doom-dashboard--reload-timer)
(cancel-timer +doom-dashboard--reload-timer)) ; in case this function is run rapidly
(setq +doom-dashboard--reload-timer (run-with-timer 0.1 nil #'+doom-dashboard-reload t)))
(defun +doom-dashboard|resize (&rest _) (defun +doom-dashboard|resize (&rest _)
"Recenter the dashboard, and reset its margins and fringes." "Recenter the dashboard, and reset its margins and fringes."
(let ((windows (get-buffer-window-list (doom-fallback-buffer) nil t)) (let (buffer-list-update-hook
buffer-list-update-hook) window-configuration-change-hook
(let (window-configuration-change-hook window-size-change-functions)
window-size-change-functions) (let ((windows (get-buffer-window-list (doom-fallback-buffer) nil t)))
(dolist (win windows) (dolist (win windows)
(set-window-start win 0) (set-window-start win 0)
(set-window-fringes win 0 0) (set-window-fringes win 0 0)
(set-window-margins (set-window-margins
win (max 0 (/ (- (window-total-width win) +doom-dashboard--width) 2))))) win (max 0 (/ (- (window-total-width win) +doom-dashboard--width) 2))))
(when windows (when windows
(with-current-buffer (doom-fallback-buffer) (with-current-buffer (doom-fallback-buffer)
(save-excursion (save-excursion
(with-silent-modifications (with-silent-modifications
(goto-char (point-min)) (goto-char (point-min))
(delete-region (line-beginning-position) (delete-region (line-beginning-position)
(save-excursion (skip-chars-forward "\n") (save-excursion (skip-chars-forward "\n")
(point))) (point)))
(insert (make-string (insert (make-string
(max 0 (- (/ (window-height (get-buffer-window)) 2) (max 0 (- (/ (window-height (get-buffer-window)) 2)
(round (/ (+ (count-lines (point-min) (point-max)) (round (/ (+ (count-lines (point-min) (point-max))
(car +doom-dashboard-banner-padding)) (car +doom-dashboard-banner-padding))
2)))) 2))))
?\n)))))))) ?\n)))))))))
(defun +doom-dashboard|detect-project (&rest _) (defun +doom-dashboard|detect-project (&rest _)
"Check for a `last-project-root' parameter in the perspective, and set the "Check for a `last-project-root' parameter in the perspective, and set the
@ -283,6 +280,7 @@ project (which may be different across perspective)."
controlled by `+doom-dashboard-pwd-policy'." controlled by `+doom-dashboard-pwd-policy'."
(if pwd (if pwd
(with-current-buffer (doom-fallback-buffer) (with-current-buffer (doom-fallback-buffer)
(doom-log "Changed dashboard's PWD to %s" pwd)
(setq-local default-directory pwd)) (setq-local default-directory pwd))
(let ((new-pwd (+doom-dashboard--get-pwd))) (let ((new-pwd (+doom-dashboard--get-pwd)))
(when (and new-pwd (file-directory-p new-pwd)) (when (and new-pwd (file-directory-p new-pwd))
@ -298,6 +296,7 @@ controlled by `+doom-dashboard-pwd-policy'."
(not (run-hook-with-args-until-success '+doom-dashboard-inhibit-functions))) (not (run-hook-with-args-until-success '+doom-dashboard-inhibit-functions)))
force) force)
(with-current-buffer (doom-fallback-buffer) (with-current-buffer (doom-fallback-buffer)
(doom-log "Reloading dashboard at %s" (format-time-string "%T"))
(with-silent-modifications (with-silent-modifications
(let ((pt (point))) (let ((pt (point)))
(unless (eq major-mode '+doom-dashboard-mode) (unless (eq major-mode '+doom-dashboard-mode)
@ -441,12 +440,6 @@ controlled by `+doom-dashboard-pwd-policy'."
(+doom-dashboard--center (+doom-dashboard--center
(- +doom-dashboard--width 2) (- +doom-dashboard--width 2)
(with-temp-buffer (with-temp-buffer
;; TODO Publish the site!
;; (insert-text-button (propertize "doomemacs.org" 'face 'font-lock-keyword-face)
;; 'action (lambda (_) (browse-url "http://doomemacs.org"))
;; 'follow-link t
;; 'help-echo "Open home page")
;; (insert (propertize " x " 'face 'font-lock-comment-face))
(insert-text-button (or (all-the-icons-octicon "octoface" :face 'all-the-icons-green :height 1.3 :v-adjust -0.15) (insert-text-button (or (all-the-icons-octicon "octoface" :face 'all-the-icons-green :height 1.3 :v-adjust -0.15)
(propertize "github" 'face 'font-lock-keyword-face)) (propertize "github" 'face 'font-lock-keyword-face))
'action (lambda (_) (browse-url "https://github.com/hlissner/doom-emacs")) 'action (lambda (_) (browse-url "https://github.com/hlissner/doom-emacs"))