如何在 Emacs 中探测 Alacritty 的背景色?

在终端里跑 Emacs,想知道终端的背景色。

iTerm2 会在环境变量中记录 Profile,所以只要不乱起名字就行:

(getenv "ITERM_PROFILE")
;; => "Solarized Light"

Xterm 可以通过 stty 查询:Query background setting from terminal (xterm / rxvt-unicode) · GitHub

不知 Alacritty 下如何探测背景色?

读下 Alacritty 的配置文件(

读文件的开销有点大:

(benchmark-run
 1000
 (with-temp-buffer
   (insert-file-contents
    "~/.config/alacritty/alacritty.yml")
   (goto-char (point))
   (if (re-search-forward "^\s+background:\s'\\([^']+\\)'.*" nil t)
       (match-string 1)
     "black")))
;; => (0.422002 0 0.0)

(benchmark-run
 1000
 (getenv "ITEM_PROFILE"))
;; => (0.004543 0 0.0)

可以在终端下不设背景色

1 个赞

怎么设置?最近偶尔也用终端版本,感觉挺不错的;还没找到合适的主题。

https://git.sr.ht/~protesilaos/dotfiles/tree/master/item/alacritty/.config/alacritty

modus theme 的 alacritty 版本

1 个赞

我的 Emacs 在终端下向来不用 theme,但这样会被认为是 light 模式(即便终端是暗色): 导致有的界面可能显示异常。

但是我刚刚发现,与 iTerm2 不同,在 Alacritty 下可以直接探测到终端背景:

$ emacs -Q -nw --eval "\
  (add-hook 'window-setup-hook
            (lambda ()
              (insert (format \"%s\" (frame-parameter nil 'background-mode)))))"
  • Alacritty

    image

    image

    但是如果 Emacs 启动之后,再改变 Alacritty 的配色,(frame-parameter nil 'background-mode) 就不准了。

  • iTerm2

    image

    image