ui/doom-dashboard: refactor

This commit is contained in:
Henrik Lissner 2017-06-25 02:01:05 +02:00
parent e595f20cd0
commit c493ad80f4
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 49 additions and 40 deletions

View file

@ -0,0 +1,24 @@
;;; ui/doom-dashboard/autoload.el -*- lexical-binding: t; -*-
;;;###autoload
(defun +doom-dashboard/next-button ()
(interactive)
(ignore-errors (goto-char (next-button (point)))))
;;;###autoload
(defun +doom-dashboard/previous-button ()
(interactive)
(ignore-errors (goto-char (previous-button (point)))))
;;;###autoload
(defun +doom-dashboard/first-button ()
(interactive)
(goto-char (point-min))
(+doom-dashboard/next-button))
;;;###autoload
(defun +doom-dashboard/last-button ()
(interactive)
(goto-char (point-max))
(+doom-dashboard/previous-button)
(beginning-of-line-text))

View file

@ -12,9 +12,16 @@
(defvar +doom-dashboard-widgets '(banner shortmenu loaded) (defvar +doom-dashboard-widgets '(banner shortmenu loaded)
"List of widgets to display in a blank scratch buffer.") "List of widgets to display in a blank scratch buffer.")
(define-derived-mode +doom-dashboard-mode fundamental-mode (define-derived-mode +doom-dashboard-mode special-mode
(concat "v" doom-version) (concat "v" doom-version)
"Major mode for the DOOM dashboard buffer.") "Major mode for the DOOM dashboard buffer."
(read-only-mode +1)
(setq truncate-lines t
mode-line-format +doom-dashboard-modeline)
(cl-loop for (car . _cdr) in fringe-indicator-alist
collect (cons car nil) into alist
finally do (setq fringe-indicator-alist alist)))
(defvar +doom-dashboard--width 0) (defvar +doom-dashboard--width 0)
(defvar +doom-dashboard--height 0) (defvar +doom-dashboard--height 0)
@ -41,28 +48,6 @@
[remap evil-delete] #'evil-normal-state [remap evil-delete] #'evil-normal-state
[remap evil-delete-char] #'evil-normal-state) [remap evil-delete-char] #'evil-normal-state)
(defun +doom-dashboard/next-button ()
(interactive)
(ignore-errors
(goto-char (next-button (point)))))
(defun +doom-dashboard/previous-button ()
(interactive)
(ignore-errors
(goto-char (previous-button (point)))))
(defun +doom-dashboard/first-button ()
(interactive)
(goto-char (point-min))
(+doom-dashboard/next-button))
(defun +doom-dashboard/last-button ()
(interactive)
(goto-char (point-max))
(+doom-dashboard/previous-button)
(beginning-of-line-text))
;; ;;
(defun +doom-dashboard|init () (defun +doom-dashboard|init ()
"Initialize doom-dashboard and set up its hooks; possibly open the dashboard "Initialize doom-dashboard and set up its hooks; possibly open the dashboard
@ -110,6 +95,10 @@ whose dimensions may not be fully initialized by the time this is run."
(and (buffer-live-p buffer) (and (buffer-live-p buffer)
(eq buffer (doom-fallback-buffer))))) (eq buffer (doom-fallback-buffer)))))
(defun +doom-dashboard-center (len s)
(concat (make-string (ceiling (max 0 (- len (length s))) 2) ? )
s))
(defun +doom-dashboard-deferred-reload (frame) (defun +doom-dashboard-deferred-reload (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."
@ -125,36 +114,31 @@ whose dimensions may not be fully initialized by the time this is run."
(not (window-minibuffer-p (frame-selected-window))) (not (window-minibuffer-p (frame-selected-window)))
(get-buffer-window (doom-fallback-buffer))) (get-buffer-window (doom-fallback-buffer)))
(unless +doom-dashboard-modeline (unless +doom-dashboard-modeline
(setq +doom-dashboard--old-modeline mode-line-format) (setq +doom-dashboard--old-modeline mode-line-format
(setq +doom-dashboard-modeline +doom-dashboard-modeline
(or (and (featurep! :ui doom-modeline) (or (and (featurep! :ui doom-modeline)
(doom-modeline 'project)) (doom-modeline 'project))
mode-line-format))) mode-line-format)))
(let ((old-pwd (or dir default-directory))) (let ((old-pwd (or dir default-directory)))
(with-current-buffer (doom-fallback-buffer) (with-current-buffer (doom-fallback-buffer)
(with-silent-modifications (with-silent-modifications
(read-only-mode +1)
(+doom-dashboard-mode) (+doom-dashboard-mode)
(cl-loop for (car . _cdr) in fringe-indicator-alist
collect (cons car nil) into alist
finally do (setq fringe-indicator-alist alist))
(erase-buffer) (erase-buffer)
(setq default-directory old-pwd)
(let ((+doom-dashboard--width (window-width)) (let ((+doom-dashboard--width (window-width))
(+doom-dashboard--height (window-height))) (+doom-dashboard--height (window-height)))
(insert (make-string (max 0 (- (truncate (/ +doom-dashboard--height 2)) 16)) ?\n)) (insert (make-string (max 0 (- (truncate (/ +doom-dashboard--height 2)) 16)) ?\n))
(dolist (widget-name +doom-dashboard-widgets) (dolist (widget-name +doom-dashboard-widgets)
(funcall (intern (format "doom-dashboard-widget--%s" widget-name))) (funcall (intern (format "doom-dashboard-widget--%s" widget-name)))
(insert "\n"))) (insert "\n")))
(setq default-directory old-pwd
mode-line-format +doom-dashboard-modeline)
(unless (button-at (point)) (unless (button-at (point))
(goto-char (point-min)) (goto-char (next-button (point-min))))))))
(goto-char (next-button (point))))))))
t) t)
;; widgets
(defun doom-dashboard-widget--banner () (defun doom-dashboard-widget--banner ()
(mapc (lambda (line) (mapc (lambda (line)
(insert (propertize (s-center +doom-dashboard--width line) (insert (propertize (+doom-dashboard-center +doom-dashboard--width line)
'face 'font-lock-comment-face) " ") 'face 'font-lock-comment-face) " ")
(insert "\n")) (insert "\n"))
'("================= =============== =============== ======== ========" '("================= =============== =============== ======== ========"
@ -180,10 +164,11 @@ whose dimensions may not be fully initialized by the time this is run."
(defun doom-dashboard-widget--loaded () (defun doom-dashboard-widget--loaded ()
(insert (insert
(propertize (propertize
(s-center +doom-dashboard--width (+doom-dashboard-center
(format "Loaded %d packages in %.03fs " +doom-dashboard--width
(- (length load-path) (length doom--base-load-path)) (format "Loaded %d packages in %.03fs "
(if (floatp doom-init-time) doom-init-time 0.0))) (- (length load-path) (length doom--base-load-path))
(if (floatp doom-init-time) doom-init-time 0.0)))
'face 'font-lock-comment-face) 'face 'font-lock-comment-face)
"\n")) "\n"))
@ -208,7 +193,7 @@ whose dimensions may not be fully initialized by the time this is run."
(propertize (concat " " label) 'face 'font-lock-keyword-face)) (propertize (concat " " label) 'face 'font-lock-keyword-face))
'action fn 'action fn
'follow-link t) 'follow-link t)
(s-center (1- +doom-dashboard--width) (buffer-string)))) (+doom-dashboard-center (1- +doom-dashboard--width) (buffer-string))))
(insert "\n\n")))) (insert "\n\n"))))
`(("Homepage" "mark-github" `(("Homepage" "mark-github"
(lambda (_) (browse-url "https://github.com/hlissner/.emacs.d"))) (lambda (_) (browse-url "https://github.com/hlissner/.emacs.d")))