ui/doom-dashboard: general refactor

Hopefully addresses #1170
This commit is contained in:
Henrik Lissner 2019-04-08 22:58:10 -04:00
parent 5f09c9c634
commit f934f38627
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 58 additions and 57 deletions

View file

@ -10,7 +10,7 @@
"Switch to the dashboard in the current window, of the current FRAME." "Switch to the dashboard in the current window, of the current FRAME."
(interactive (list (selected-frame))) (interactive (list (selected-frame)))
(with-selected-frame frame (with-selected-frame frame
(switch-to-buffer (+doom-dashboard-initial-buffer)) (switch-to-buffer (doom-fallback-buffer))
(+doom-dashboard-reload t))) (+doom-dashboard-reload t)))
;;;###autoload ;;;###autoload

View file

@ -99,40 +99,56 @@ PLIST can have the following properties:
;; ;;
;; Bootstrap ;;; Bootstrap
(setq doom-fallback-buffer-name +doom-dashboard-name (defun +doom-dashboard|init ()
;; Fixes #850: `emacs file.txt' opens two windows, one for file.txt and "Initializes Doom's dashboard."
;; one for `initial-buffer-choice' (in `command-line-1'). We want one or (unless noninteractive
;; the other, not both. ;; Ensure the dashboard is up-to-date whenever it is switched to or resized.
initial-buffer-choice (add-hook 'window-configuration-change-hook #'+doom-dashboard|resize)
(when (or (daemonp) (add-hook 'window-size-change-functions #'+doom-dashboard|resize)
(not (cl-loop for arg in (cdr command-line-args) (add-hook 'kill-buffer-query-functions #'+doom-dashboard|reload-on-kill)
if (or (equal arg "--restore") (add-hook 'doom-switch-buffer-hook #'+doom-dashboard|reload-on-kill)
(and (string-match-p "^[^-]" arg) (add-hook 'delete-frame-functions #'+doom-dashboard|reload-frame)
(file-exists-p arg)))
return t)))
#'+doom-dashboard-initial-buffer))
(add-hook 'doom-init-ui-hook #'+doom-dashboard|init 'append) ;; `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
;; else to show.
(setq doom-fallback-buffer-name +doom-dashboard-name
initial-buffer-choice #'doom-fallback-buffer)
(when (equal (buffer-name) "*scratch*")
(switch-to-buffer (doom-fallback-buffer))
(if (daemonp)
(add-hook 'after-make-frame-functions #'+doom-dashboard|reload-frame)
(+doom-dashboard-reload)))))
(add-hook 'doom-init-ui-hook #'+doom-dashboard|init)
;; ;;
;; Major mode ;;; Major mode
(define-derived-mode +doom-dashboard-mode special-mode (define-derived-mode +doom-dashboard-mode special-mode
(format "DOOM v%s" doom-version) (format "DOOM v%s" doom-version)
"Major mode for the DOOM dashboard buffer."
:syntax-table nil :syntax-table nil
:abbrev-table nil :abbrev-table nil
"Major mode for the DOOM dashboard buffer."
(setq truncate-lines t) (setq truncate-lines t)
(setq-local whitespace-style nil) (setq-local whitespace-style nil)
(setq-local show-trailing-whitespace nil) (setq-local show-trailing-whitespace nil)
(setq-local hscroll-margin 0) (setq-local hscroll-margin 0)
(setq-local tab-width 2) (setq-local tab-width 2)
;; Don't scroll to follow cursor
(setq-local scroll-preserve-screen-position nil)
(setq-local auto-hscroll-mode nil)
(cl-loop for (car . _cdr) in fringe-indicator-alist (cl-loop for (car . _cdr) in fringe-indicator-alist
collect (cons car nil) into alist collect (cons car nil) into alist
finally do (setq fringe-indicator-alist alist)) finally do (setq fringe-indicator-alist alist))
;; Ensure point is always on a button
(add-hook 'post-command-hook #'+doom-dashboard|reposition-point nil t)) (add-hook 'post-command-hook #'+doom-dashboard|reposition-point nil t))
(define-key! +doom-dashboard-mode-map (define-key! +doom-dashboard-mode-map
@ -175,13 +191,13 @@ PLIST can have the following properties:
;; ;;
;; Hooks ;;; Hooks
(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) (deactivate-mark t)
(when (bound-and-true-p evil-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
(if (button-at (point)) (if (button-at (point))
@ -190,25 +206,12 @@ 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|init ()
"Initializes Doom's dashboard."
(unless noninteractive
(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)
;; `persp-mode' integration: update `default-directory' when switching
(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))
(if (daemonp)
(add-hook 'after-make-frame-functions #'+doom-dashboard|reload-frame)
(+doom-dashboard-reload t)))
(defun +doom-dashboard|reload-on-kill () (defun +doom-dashboard|reload-on-kill ()
"A `kill-buffer-query-functions' hook. If this isn't a dashboard buffer, move "A `kill-buffer-query-functions' hook.
along, but record its `default-directory' if the buffer is real. See
`doom-real-buffer-p' for an explanation for what 'real' means. 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
'real' means.
If this is the dashboard buffer, reload the dashboard." If this is the dashboard buffer, reload the dashboard."
(or (let ((buf (current-buffer))) (or (let ((buf (current-buffer)))
@ -230,11 +233,13 @@ whose dimensions may not be fully initialized by the time this is run."
"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 ((windows (get-buffer-window-list (doom-fallback-buffer) nil t))
buffer-list-update-hook) buffer-list-update-hook)
(dolist (win windows) (let (window-configuration-change-hook
(set-window-start win 0) window-size-change-functions)
(set-window-fringes win 0 0) (dolist (win windows)
(set-window-margins (set-window-start win 0)
win (max 0 (/ (- (window-total-width win) +doom-dashboard--width) 2)))) (set-window-fringes win 0 0)
(set-window-margins
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
@ -271,12 +276,7 @@ project (which may be different across perspective)."
;; ;;
;; Library ;;; Library
(defun +doom-dashboard-initial-buffer ()
"Returns buffer to display on startup. Designed for `initial-buffer-choice'."
(let (buffer-list-update-hook)
(get-buffer-create +doom-dashboard-name)))
(defun +doom-dashboard-p (buffer) (defun +doom-dashboard-p (buffer)
"Returns t if BUFFER is the dashboard buffer." "Returns t if BUFFER is the dashboard buffer."
@ -303,15 +303,17 @@ controlled by `+doom-dashboard-pwd-policy'."
force) force)
(with-current-buffer (doom-fallback-buffer) (with-current-buffer (doom-fallback-buffer)
(with-silent-modifications (with-silent-modifications
(save-excursion (let ((pt (point)))
(unless (eq major-mode '+doom-dashboard-mode) (unless (eq major-mode '+doom-dashboard-mode)
(+doom-dashboard-mode)) (+doom-dashboard-mode))
(erase-buffer) (erase-buffer)
(run-hooks '+doom-dashboard-functions))) (run-hooks '+doom-dashboard-functions)
(+doom-dashboard|resize) (goto-char pt)
(+doom-dashboard|detect-project) (+doom-dashboard|reposition-point))
(+doom-dashboard-update-pwd) (+doom-dashboard|resize)
(current-buffer)))) (+doom-dashboard|detect-project)
(+doom-dashboard-update-pwd)
(current-buffer)))))
;; helpers ;; helpers
(defun +doom-dashboard--center (len s) (defun +doom-dashboard--center (len s)
@ -337,13 +339,12 @@ controlled by `+doom-dashboard-pwd-policy'."
cwd))) cwd)))
((eq policy 'last) ((eq policy 'last)
lastcwd) lastcwd)
(t ((warn "`+doom-dashboard-pwd-policy' has an invalid value of '%s'"
(warn "`+doom-dashboard-pwd-policy' has an invalid value of '%s'"
policy))))) policy)))))
;; ;;
;; Widgets ;;; Widgets
(defun doom-dashboard-widget-banner () (defun doom-dashboard-widget-banner ()
(let ((point (point))) (let ((point (point)))