emacs/lisp/icejam-fonts.el
Maciej Szlosarczyk 0ec8f700af
Some checks failed
/ Test config on 20 (push) Failing after 48s
Fix bug in exec-path-from-shell hook, simplify font setup
2025-01-05 17:34:12 +02:00

140 lines
4.9 KiB
EmacsLisp

;;; icejam-fonts.el --- summary -*- lexical-binding: t; -*-
;; Author: Maciej Szlosarczyk
;; Maintainer: Maciej Szlosarczyk
;; Version: 0.1-snapshot
;;; Commentary:
;; Set font stuff
;;; Code:
(use-package dash :ensure t :defer t)
;; (defconst icejam-font "Monoid"
;; (defconst icejam-font "Fira Mono"
;; (defconst icejam-font "Fira Code"
;; (defconst icejam-font "Red Hat Mono"
;; (defconst icejam-font "Agave Nerd Font Mono"
;; (defconst icejam-font "Input Mono Condensed"
;; (defconst icejam-font "SF Mono"
;; (defconst icejam-font "Monaco"
;; (defconst icejam-font "JetBrains Mono" "Default font.")
;; (defconst icejam-font "JuliaMono"
;; (defconst icejam-font "Rec Mono Semicasual"
;; (defconst icejam-font "IBM Plex Mono"
;; (defconst icejam-font "Berkeley Mono Trial"
;; (defconst icejam-font "Inconsolata"
;; (defconst icejam-font "Victor Mono Medium" "Default font.")
;; (defconst icejam-font "Iosevka Term" "Default font.")
(defconst icejam-font-family "Iosevka Comfy Motion" "Default font.")
(defconst icejam-markdown-font-family "Iosevka Term" "Font used to render code blocks in markdown.")
(defconst icejam-font-height 14
"Default height of then font.
It is used to calculated the height in relation to the screen
in `icejam-set-font-to-screen`.")
(defcustom icejam-mut-font-family
icejam-font-family
"Current font, defaults to the one loaded in the beginning."
:type 'string
:group 'icejam)
(defcustom icejam-mut-markdown-font-family
icejam-markdown-font-family
"Current markdown font family, defaults to the one loaded in the beginning."
:type 'string
:group 'icejam)
(defcustom icejam-mut-font-height
icejam-font-height
"Current font height."
:type 'integer
:group 'icejam)
;;;;;;;;;;;;;;;;;;;;;; Font configuration ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; First, let's silence a warning about free variables
(defvar lsp-ui-doc-frame-hook)
(defun icejam-set-lsp-ui-font-hook ()
"Reset LSP IO font to specified `icejam-font` and `icejam-font-height`."
(setopt lsp-ui-doc-frame-hook nil)
(add-hook
'lsp-ui-doc-frame-hook
(lambda (frame _w)
(set-face-attribute 'default frame
:family icejam-mut-font-family
:height (-> icejam-mut-font-height (- 2) (* 10))))))
(defun icejam-set-font (family height)
"Set font to FAMILY and its HEIGHT to X.
Not all faces will be set to this value. Some of them look better with being
slightly smaller than the default face, by 1 point. Those are: `tooltip',
`company-tooltip', `company-tooltip-annotation', `company-tooltip-mouse'.
Modeline faces (`mode-line' and `mode-line-inactive') look better if they are
two points smaller."
(interactive "sNew font: \nnEnter height for %s: ")
(setopt icejam-mut-font-family family)
(setopt icejam-mut-font-height height)
;; Set default font.
(set-face-attribute 'default nil :family family :height (-> height (* 10)))
;; Some font faces look better when they are 1 point smaller.
(dolist (face '(tooltip
company-tooltip
company-tooltip-annotation
company-tooltip-mouse))
(set-face-attribute face nil :height (-> height (- 1) (* 10))))
;; And some, mainly in modeline with 2 points.
(dolist (face '(mode-line mode-line-inactive))
(set-face-attribute face nil :height (-> height (- 2) (* 10))))
;; Call LSP-UI hook
(icejam-set-lsp-ui-font-hook))
(defun icejam-set-font-to-screen ()
"Automatically set font height to suit the monitor."
(interactive)
;; Only do anything if there's a display at all.
(if (x-display-list)
(cond
;; LG 27" screen connected to a MacBook.
((>= 1080 (x-display-pixel-height))
(icejam-set-font icejam-font-family icejam-font-height))
;; MacBook 14" built-in screen.
((>= 1440 (x-display-pixel-height))
(icejam-set-font icejam-font-family (+ icejam-font-height 3)))
;; 4K screen on Windows or Linux
((>= 2160 (x-display-pixel-height))
(icejam-set-font icejam-font-family (- icejam-font-height 3))))))
;; Run the above function once, after elpaca finishes all downloads.
(add-hook 'elpaca-after-init-hook 'icejam-set-font-to-screen)
(defun icejam-set-font-height (height)
"Set font to a specified HEIGHT."
(interactive "nEnter height for font: ")
(icejam-set-font icejam-mut-font-family height))
(defun icejam-set-font-height-for-this-frame (new-height)
"Set font NEW-HEIGHT for this frame only."
(interactive "nEnter new height for font in this frame: ")
(set-frame-font (format "%s %d" icejam-mut-font-family new-height)))
;; Remove ugly black line
(set-face-attribute 'vertical-border nil :foreground
(face-attribute 'fringe :background))
;; Set fringe colors to default, so it does not bother you.
(set-face-attribute 'fringe nil
:foreground (face-foreground 'default)
:background (face-background 'default))
(provide 'icejam-fonts)
;;; icejam-fonts.el ends here