Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Workaround for incorrect center text calculation #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions dashboard-widgets.el
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ is displayed."
(integer :tag "index of a text banner")
(string :tag "a path to an image or text banner")
(cons :tag "an image and text banner"
(string :tag "image banner path")
(string :tag "text banner path")))
(string :tag "image banner path")
(string :tag "text banner path")))
:group 'dashboard)

(defcustom dashboard-item-generators
Expand Down Expand Up @@ -497,8 +497,7 @@ If MESSAGEBUF is not nil then MSG is also written in message buffer."
(let ((width 0))
(while (< (point) end)
(let ((line-length (- (line-end-position) (line-beginning-position))))
(if (< width line-length)
(setq width line-length)))
(setq width (max width line-length)))
(forward-line 1))
(let ((prefix (propertize " " 'display `(space . (:align-to (- center ,(/ width 2)))))))
(add-text-properties start end `(line-prefix ,prefix indent-prefix ,prefix))))))
Expand Down Expand Up @@ -526,45 +525,45 @@ If MESSAGEBUF is not nil then MSG is also written in message buffer."

(defun dashboard-choose-banner ()
"Return a plist specifying the chosen banner based on `dashboard-startup-banner'."
(pcase dashboard-startup-banner
('nil nil)
('official
(append (when (image-type-available-p 'png)
(list :image dashboard-banner-official-png))
(list :text (dashboard-get-banner-path 1))))
('logo
(append (when (image-type-available-p 'png)
(list :image dashboard-banner-logo-png))
(list :text (dashboard-get-banner-path 1))))
((pred integerp)
(list :text (dashboard-get-banner-path dashboard-startup-banner)))
((pred stringp)
(pcase dashboard-startup-banner
((pred (lambda (f) (not (file-exists-p f))))
(message "could not find banner %s, use default instead" dashboard-startup-banner)
(list :text (dashboard-get-banner-path 1)))
((pred (string-suffix-p ".txt"))
(list :text (if (file-exists-p dashboard-startup-banner)
dashboard-startup-banner
(message "could not find banner %s, use default instead" dashboard-startup-banner)
(dashboard-get-banner-path 1))))
((pred dashboard--image-supported-p)
(list :image dashboard-startup-banner
:text (dashboard-get-banner-path 1)))
(_
(message "unsupported file type %s" (file-name-nondirectory dashboard-startup-banner))
(list :text (dashboard-get-banner-path 1)))))
(`(,img . ,txt)
(list :image (if (dashboard--image-supported-p img)
img
(message "could not find banner %s, use default instead" img)
dashboard-banner-official-png)
:text (if (and (file-exists-p txt) (string-suffix-p ".txt" txt))
txt
(message "could not find banner %s, use default instead" txt)
(dashboard-get-banner-path 1))))
(_
(message "unsupported banner config %s" dashboard-startup-banner))))
(pcase dashboard-startup-banner
('nil nil)
('official
(append (when (image-type-available-p 'png)
(list :image dashboard-banner-official-png))
(list :text (dashboard-get-banner-path 1))))
('logo
(append (when (image-type-available-p 'png)
(list :image dashboard-banner-logo-png))
(list :text (dashboard-get-banner-path 1))))
((pred integerp)
(list :text (dashboard-get-banner-path dashboard-startup-banner)))
((pred stringp)
(pcase dashboard-startup-banner
((pred (lambda (f) (not (file-exists-p f))))
(message "could not find banner %s, use default instead" dashboard-startup-banner)
(list :text (dashboard-get-banner-path 1)))
((pred (string-suffix-p ".txt"))
(list :text (if (file-exists-p dashboard-startup-banner)
dashboard-startup-banner
(message "could not find banner %s, use default instead" dashboard-startup-banner)
(dashboard-get-banner-path 1))))
((pred dashboard--image-supported-p)
(list :image dashboard-startup-banner
:text (dashboard-get-banner-path 1)))
(_
(message "unsupported file type %s" (file-name-nondirectory dashboard-startup-banner))
(list :text (dashboard-get-banner-path 1)))))
(`(,img . ,txt)
(list :image (if (dashboard--image-supported-p img)
img
(message "could not find banner %s, use default instead" img)
dashboard-banner-official-png)
:text (if (and (file-exists-p txt) (string-suffix-p ".txt" txt))
txt
(message "could not find banner %s, use default instead" txt)
(dashboard-get-banner-path 1))))
(_
(message "unsupported banner config %s" dashboard-startup-banner))))

(defun dashboard--type-is-gif-p (image-path)
"Return if image is a gif.
Expand Down Expand Up @@ -633,7 +632,8 @@ Argument IMAGE-PATH path to the image."
(insert "\n\n")
(add-text-properties start (point) '(cursor-intangible t inhibit-isearch t))))
(when dashboard-banner-logo-title
(dashboard-insert-center (propertize dashboard-banner-logo-title 'face 'dashboard-banner-logo-title) "\n\n"))
(dashboard-insert-center (propertize dashboard-banner-logo-title 'face 'dashboard-banner-logo-title))
(insert "\n\n"))
(dashboard-insert-navigator)
(dashboard-insert-init-info))

Expand Down
94 changes: 51 additions & 43 deletions dashboard.el
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ Optional prefix ARG says how many lines to move; default is one line."
(defun dashboard--section-list (section)
"Return the list from SECTION."
(cl-case section
(`recents recentf-list)
(`bookmarks (bookmark-all-names))
(`projects (dashboard-projects-backend-load-projects))
(`recents recentf-list)
(`bookmarks (bookmark-all-names))
(`projects (dashboard-projects-backend-load-projects))
(`ls-directories (dashboard-ls--dirs))
(`ls-files (dashboard-ls--files))
(`ls-files (dashboard-ls--files))
(t (user-error "Unknown section for search: %s" section))))

(defun dashboard--current-item-in-path ()
Expand Down Expand Up @@ -367,6 +367,13 @@ Optional argument ARGS adviced function arguments."
;;
;; Insertion
;;
(defmacro dashboard--with-buffer (&rest body)
"Execute BODY in dashboard buffer."
(declare (indent 0))
`(with-current-buffer (get-buffer-create dashboard-buffer-name)
(let (buffer-read-only) ,@body)
(current-buffer)))

(defun dashboard-maximum-section-length ()
"For the just-inserted section, calculate the length of the longest line."
(let ((max-line-length 0))
Expand All @@ -388,45 +395,46 @@ Optional argument ARGS adviced function arguments."
(max-line-length 0))
(when recentf-is-on
(setq recentf-list (dashboard-subseq recentf-list dashboard-num-recents)))
(prog1
(with-current-buffer (get-buffer-create dashboard-buffer-name)
(when (or dashboard-force-refresh (not (eq major-mode 'dashboard-mode)))
(let (buffer-read-only)
(erase-buffer)
(dashboard-insert-banner)
(setq dashboard--section-starts nil)
(mapc (lambda (els)
(let* ((el (or (car-safe els) els))
(list-size
(or (cdr-safe els)
dashboard-items-default-length))
(item-generator
(cdr-safe (assoc el dashboard-item-generators))))
(push (point) dashboard--section-starts)
(funcall item-generator list-size)
(goto-char (point-max))
(when recentf-is-on
(setq recentf-list origial-recentf-list))
(setq max-line-length
(max max-line-length (dashboard-maximum-section-length)))))
dashboard-items)
(when dashboard-center-content
(dashboard-center-text
(if dashboard--section-starts
(car (last dashboard--section-starts))
(point))
(point-max)))
(insert dashboard-page-separator)
(save-excursion
(dolist (start dashboard--section-starts)
(goto-char start)
(insert dashboard-page-separator)))
(dashboard-insert-footer))
(goto-char (point-min))
(dashboard-mode))
(current-buffer))
(when recentf-is-on
(setq recentf-list origial-recentf-list)))))
(dashboard--with-buffer
(when (or dashboard-force-refresh (not (eq major-mode 'dashboard-mode)))
(erase-buffer)
(dashboard-insert-banner)
(setq dashboard--section-starts nil)
(mapc (lambda (els)
(let* ((el (or (car-safe els) els))
(list-size
(or (cdr-safe els)
dashboard-items-default-length))
(item-generator
(cdr-safe (assoc el dashboard-item-generators))))
(push (point) dashboard--section-starts)
(funcall item-generator list-size)
(goto-char (point-max))
;; add a newline so the next section-name doesn't get include
;; on the same line.
(insert "\n")
(when recentf-is-on
(setq recentf-list origial-recentf-list))
(setq max-line-length
(max max-line-length (dashboard-maximum-section-length)))))
dashboard-items)
(when dashboard-center-content
(dashboard-center-text
(if dashboard--section-starts
(car (last dashboard--section-starts))
(point))
(point-max)))
(insert dashboard-page-separator)
(save-excursion
(dolist (start dashboard--section-starts)
(goto-char start)
(backward-delete-char 1) ; delete the newline we added previously
(insert dashboard-page-separator)))
(dashboard-insert-footer)
(goto-char (point-min))
(dashboard-mode)))
(when recentf-is-on
(setq recentf-list origial-recentf-list))))

(defun dashboard-refresh-buffer (&rest _)
"Refresh buffer."
Expand Down