Skip to content

Commit

Permalink
Make startup banner scalable (from dotfile)
Browse files Browse the repository at this point in the history
The default logo is rather big. Users might prefer to use the screen size for
other information (while still showing a banner).

This commit adds a configuration variable `dotspacemacs-startup-banner-scale`
to the dotfile
  • Loading branch information
dalanicolai authored and smile13241324 committed Jun 6, 2022
1 parent 51686e5 commit a485b5a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
64 changes: 55 additions & 9 deletions core/core-spacemacs-buffer.el
Expand Up @@ -27,6 +27,7 @@
(defvar dotspacemacs-filepath)
(defvar dotspacemacs-show-startup-list-numbers)
(defvar dotspacemacs-startup-banner)
(defvar dotspacemacs-startup-banner-scale)
(defvar dotspacemacs-startup-buffer-show-icons)
(defvar spacemacs-badge-official-png)
(defvar spacemacs-banner-directory)
Expand Down Expand Up @@ -261,15 +262,52 @@ If ALL is non-nil then truly all banners can be selected."
"Return the full path to banner with index INDEX."
(concat spacemacs-banner-directory (format "%03d-banner.txt" index)))

(defun spacemacs-buffer//banner-fit-height-size ()
"Calculate height of startup banner to fit buffer contents.
Returns height in units of line height with a minimum of 1."
;; first determine number of lines occupied by startup list
(let* ((startup-list-line-height
;; the all-the-icons package is not available here yet, but we don't
;; require icons for just counting the lines in the
;; `dotspacemacs-startup-lists'
(let ((icons dotspacemacs-startup-buffer-show-icons)
lines)
(setq dotspacemacs-startup-buffer-show-icons nil)
(setq lines (with-temp-buffer
(spacemacs-buffer//do-insert-startupify-lists)
(line-number-at-pos)))
;; (count-lines (point-min) (point-max)))
(setq dotspacemacs-startup-buffer-show-icons icons)
lines))
;; We determine the maximum available banner height by subtracting the
;; number of lines in the home buffer contents (excl. logo and
;; startup-list), i.e. `26', and the number of lines in the startup
;; list from the total available text lines
(image-height (- (window-text-height) 26 startup-list-line-height)))
;; return image-height with minimum of 3 line heights
(max image-height 3)))

(defun spacemacs-buffer//insert-image-banner (banner)
"Display an image banner.
BANNER: the path to an ascii banner file."
(when (file-exists-p banner)
(let* ((title spacemacs-buffer-logo-title)
(spec (create-image banner))
(size (image-size spec))
;; we must use the scaled size for determining the correct
;; left-margin size
(unscaled-size (image-size spec)) ;; size in 'canonical character units'
(height (cdr unscaled-size)) ;; return size in units of line heights
(scale (pcase dotspacemacs-startup-banner-scale
('auto (let ((factor (/ (float (spacemacs-buffer//banner-fit-height-size))
height)))
;; return factor with maximum of 1
(min factor 1)))
(factor factor)))
(size (cons (* scale (car unscaled-size)) (* scale (cdr unscaled-size))))
(width (car size))
(left-margin (max 0 (floor (- spacemacs-buffer--window-width width) 2))))
;; we scale the image by simply setting the scale property in the image-spec
(plist-put (cdr spec) :scale scale)
(insert (make-string left-margin ?\s))
(insert-image spec)
(insert "\n\n")
Expand Down Expand Up @@ -1219,7 +1257,8 @@ SEQ, START and END are the same arguments as for `cl-subseq'"
(defun spacemacs-buffer//insert-errors ()
(when (spacemacs-buffer//insert-string-list
(spacemacs-buffer||propertize-heading
(all-the-icons-material "error" :face 'font-lock-keyword-face)
(when dotspacemacs-startup-buffer-show-icons
(all-the-icons-material "error" :face 'font-lock-keyword-face))
"Errors:" "e")
spacemacs-buffer--errors)
(spacemacs-buffer||add-shortcut "e" "Errors:")
Expand All @@ -1228,7 +1267,8 @@ SEQ, START and END are the same arguments as for `cl-subseq'"
(defun spacemacs-buffer//insert-warnings ()
(when (spacemacs-buffer//insert-string-list
(spacemacs-buffer||propertize-heading
(all-the-icons-material "warning" :face 'font-lock-keyword-face)
(when dotspacemacs-startup-buffer-show-icons
(all-the-icons-material "warning" :face 'font-lock-keyword-face))
"Warnings:" "w")
spacemacs-buffer--warnings)
(spacemacs-buffer||add-shortcut "w" "Warnings:")
Expand All @@ -1249,7 +1289,8 @@ SEQ, START and END are the same arguments as for `cl-subseq'"
(spacemacs//subseq spacemacs-buffer//recent-files-list 0 list-size))
(when (spacemacs-buffer//insert-file-list
(spacemacs-buffer||propertize-heading
(all-the-icons-octicon "history" :face 'font-lock-keyword-face :v-adjust -0.05)
(when dotspacemacs-startup-buffer-show-icons
(all-the-icons-octicon "history" :face 'font-lock-keyword-face :v-adjust -0.05))
"Recent Files:" "r")
spacemacs-buffer//recent-files-list)
(spacemacs-buffer||add-shortcut "r" "Recent Files:"))
Expand All @@ -1260,7 +1301,8 @@ SEQ, START and END are the same arguments as for `cl-subseq'"
(unless projectile-mode (projectile-mode))
(when (spacemacs-buffer//insert-files-by-dir-list
(spacemacs-buffer||propertize-heading
(all-the-icons-octicon "rocket" :face 'font-lock-keyword-face :v-adjust -0.05)
(when dotspacemacs-startup-buffer-show-icons
(all-the-icons-octicon "rocket" :face 'font-lock-keyword-face :v-adjust -0.05))
"Recent Files by Project:" "R")
(mapcar (lambda (group)
(cons (car group)
Expand All @@ -1276,7 +1318,8 @@ SEQ, START and END are the same arguments as for `cl-subseq'"
(defun spacemacs-buffer//insert-todos (list-size)
(when (spacemacs-buffer//insert-todo-list
(spacemacs-buffer||propertize-heading
(all-the-icons-octicon "check" :face 'font-lock-keyword-face :v-adjust -0.05)
(when dotspacemacs-startup-buffer-show-icons
(all-the-icons-octicon "check" :face 'font-lock-keyword-face :v-adjust -0.05))
"To-Do:" "d")
(spacemacs//subseq (spacemacs-buffer//todo-list)
0 list-size))
Expand All @@ -1286,7 +1329,8 @@ SEQ, START and END are the same arguments as for `cl-subseq'"
(defun spacemacs-buffer//insert-agenda (list-size)
(when (spacemacs-buffer//insert-todo-list
(spacemacs-buffer||propertize-heading
(all-the-icons-octicon "calendar" :face 'font-lock-keyword-face :v-adjust -0.05)
(when dotspacemacs-startup-buffer-show-icons
(all-the-icons-octicon "calendar" :face 'font-lock-keyword-face :v-adjust -0.05))
"Agenda:" "c")
(spacemacs//subseq (spacemacs-buffer//agenda-list)
0 list-size))
Expand All @@ -1299,7 +1343,8 @@ SEQ, START and END are the same arguments as for `cl-subseq'"
(require 'bookmark)
(when (spacemacs-buffer//insert-bookmark-list
(spacemacs-buffer||propertize-heading
(all-the-icons-octicon "bookmark" :face 'font-lock-keyword-face :v-adjust -0.05)
(when dotspacemacs-startup-buffer-show-icons
(all-the-icons-octicon "bookmark" :face 'font-lock-keyword-face :v-adjust -0.05))
"Bookmarks:" "b")
(spacemacs//subseq (bookmark-all-names)
0 list-size))
Expand All @@ -1310,7 +1355,8 @@ SEQ, START and END are the same arguments as for `cl-subseq'"
(unless projectile-mode (projectile-mode))
(when (spacemacs-buffer//insert-file-list
(spacemacs-buffer||propertize-heading
(all-the-icons-octicon "rocket" :face 'font-lock-keyword-face :v-adjust -0.05)
(when dotspacemacs-startup-buffer-show-icons
(all-the-icons-octicon "rocket" :face 'font-lock-keyword-face :v-adjust -0.05))
"Projects:" "p")
(spacemacs//subseq (projectile-relevant-known-projects)
0 list-size))
Expand Down
7 changes: 7 additions & 0 deletions core/templates/.spacemacs.template
Expand Up @@ -180,6 +180,13 @@ It should only modify the values of Spacemacs settings."
;; If the value is nil then no banner is displayed. (default 'official)
dotspacemacs-startup-banner 'official

;; Scale factor controls the scaling (size) of the startup banner. Default
;; value is `auto' for scaling the logo automatically to fit all buffer
;; contents, to a maximum of the full image height and a minimum of 3 line
;; heights. If set to a number (int or float) it is used as a constant
;; scaling factor for the default logo size.
dotspacemacs-startup-banner-scale 'auto

;; List of items to show in startup buffer or an association list of
;; the form `(list-type . list-size)`. If nil then it is disabled.
;; Possible values for list-type are:
Expand Down

2 comments on commit a485b5a

@winsphinx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this commit, every time when start Emacs, the most recent file disappears.

@dalanicolai
Copy link
Contributor Author

@dalanicolai dalanicolai commented on a485b5a Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, github did not notify me about this message so that I only see this now.
Anyway, this commit was working fine, until I've added the function
spacemacs-buffer//banner-fit-height-size at the last moment. That function makes the
scaling depend on the real 'startup list' size, instead of the configured possible maximum
'startup list'. Although that function is responsible for the faulty recentf behavior, I do not
quickly see how the code is even distantly related to the recentf behavior (although I guess
it has something to do with the use of that spacemacs-buffer//do-insert-startupify-lists
function). Very weird...

Please sign in to comment.