簡體   English   中英

Emacs:如何根據 window 大小或顯示器大小調整字體大小?

[英]Emacs: How can I adjust the font size according to window size or monitor size?

我目前正在使用 doom-emacs 並且我遇到的問題是我使用我的 4K (28") 顯示器設置了我的字體大小,但我還需要 emacs 在我的筆記本電腦 1920x1080 (15.6") 上用於學校,我想更改字體尺寸根據 emacs window 尺寸或當前顯示器尺寸。 我的字體配置如下所示。 如果重要的話,我正在使用 (K)Ubuntu 和 dwm。

(setq doom-font (font-spec :family "SauceCodePro Nerd Font Mono" :size 26 :weight 'semi-light)
      doom-variable-pitch-font (font-spec :family "Merriweather" :size 28)
      doom-big-font (font-spec :family "SauceCodePro Nerd Font Mono" :size 36))

(after! doom-themes
  (setq doom-themes-enable-bold t)
  (setq doom-themes-enable-italic t))
(custom-set-faces!
  '(font-lock-comment-face :slant italic)
  '(font-lock-keyword-face :slant italic))

我使用下面的 function 來確定字體大小。 這個想法是大小是最大的,這樣一個 integer fill-columns (在我的情況下通常是 2 個)並排放置。

(defun aadcg-auto-size-font (arg font)
  "Set FONT such that ARG FILL-COLUMN(s) fit in a frame.
Example: M-2 M-x aadcg-auto-size-font RET iosevka"
  (interactive "p\nsFont: ")
  (let ((size 1.0))
    (set-frame-font (concat font "-" (number-to-string size)) nil t)
    (while (>= (save-window-excursion
                (delete-other-windows)
                (window-max-chars-per-line))
              (* arg (+ fill-column 15)))
      (setq size (+ size 0.5))
      (set-frame-font (concat font "-" (number-to-string size)) nil t))
    (message "The suggested font size is %f" size)
    (number-to-string size))

在確定在不同情況下需要哪種字體大小后,您可以創建一個變量,例如external-monitor-p ,用於檢查您正在使用的監視器(如果您正在運行 Xorg,則利用 xrandr)並設置 fonts。

大致如下。

(if external-monitor-p
  (add-to-list 'default-frame-alist '(font . "FontName-Size"))
  ;; something else
)

我使用下面的 function dmg-font-size 來改變它(我不使用doom-emacs)

根據您的操作系統/窗口管理器,檢測顯示器分辨率的變化,並使用 emacsclient 調用 function。 否則手動設置。 當分辨率發生變化(Linux)時,我通過鋸魚中的回調來做到這一點。

當 window 框架改變大小時,我認為你不想改變字體大小。 我想這會很煩人。

;;; scale fonts
(defvar dmg-typeface (car default-frame-alist)
  "What is the typeface to use")

(when (member "DejaVu Sans Mono" (font-family-list))
  (setq dmg-typeface "DejaVu Sans Mono")
  )
(when (member "Andale Mono" (font-family-list))
  (setq dmg-typeface "Andale Mono")
)

(defun dmg-font-size (size)
  (interactive "NPoint size:")
  (set-frame-font (concat dmg-typeface "-" (number-to-string size)) t t)
  )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM