一套配置在多台电脑上同步使用的可能?

我的台式机与笔记本电脑共用的是同一套emacs配置(doom emacs),通过github的私有仓库同步,但我现在遇到了一个问题,就是我的台式机分辨率低但屏幕大,笔记本分辨率高但屏幕小,如果采取同样的字体设置,笔记本上的字就小得瞎眼。

我想知道,emacs-lisp有没有内置的函数,类似于doom中通过判断if: IS_MAC那样的判断逻辑,读取当前设备的hostname,然后这样就可以依据不同的hostname,分别进行不同的配置了?

直接写个macro,case一下

我对elisp的掌握程度还很肤浅,不知道用啥函数来获取hostname……

system-type是判断操作系统的

system-name是hostname

标题有点误导性 我差点就把我的视频链接贴出来了……

emacs有一些变量和函数来读取hostname 用(shell-command-to-string)也是可以的

我用 display-pixel-width 判断的。详见链接。

不知道是不是我理解不对,为什么看您的配置,display-pixel-width不管是大于还是小于1400,好像对字体的设置都是一样的14号啊……

参照我这边的做法https://github.com/ztlevi/doom-config/blob/master/+ui.el#L14-L17。

hostname是个次优解。

精彩的做法!

还可以通过分辨率 / PPI 进行判断。

分辨率

(list
 (display-pixel-width)
 (display-pixel-height))
;; => (1280 800)

尺寸(mm)

;; 1
(list
 (display-mm-width)
 (display-mm-height))
;; => (353 220)

;; 2
(cdr (assoc 'mm-size (frame-monitor-attributes)))
;; => (285 178)

(方法 2 看起来更接近现实)

PPI

(defun x-display-ppi ()
  "Return the PPI of the display.

Modified from `org--get-display-dpi'.

The function assumes that the display has the same pixel width in
the horizontal and vertical directions."
  (if (display-graphic-p)
      (let ((mm-h (cl-caddr (assoc 'mm-size (frame-monitor-attributes)))))
        (round (/ (display-pixel-height)
    	          (/ mm-h 25.4))))
    (error "Attempt to calculate the dpi of a non-graphic display")))

(x-display-ppi)
;; => 114
5 个赞

神乎其技!请问这个frame-monitor-attributes是哪里提供的呢?emacs可以直接获取到显示器的物理参数吗?

嗯,可以调整的。我那天不知为什么,在 WSL高分辨率下14也够了,就临时改了下。

在国外网站上搜到的,如何获取屏幕分辨率,在Win下的操作见这篇文章:

https://qiita.com/y-takano/items/cb752ad6a10e550ec92f

同一个网站上的,一个在Emacs下使用这种方法的范例,用于最大化WSL下打开的Emacs窗口:

https://qiita.com/fujimotok/items/b29c541b4387ed332bad

1 个赞
(if (display-graphic-p)
    (cond ((string= (system-name) "arch-mini");;Mini PC
           (progn (set-font "Sarasa Term SC" "Sarasa Term SC" 40 40)
                  (setq desktop-dirname "~/.emacs.d/arch-mini")
                  (setq desktop-path '("~/.emacs.d/arch-mini"))))

可以的:

;; > frame-monitor-attributes
;; >> display-monitor-attributes-list
    (cond
     ((eq frame-type 'x)
      (x-display-monitor-attributes-list display))
     ((eq frame-type 'w32)
      (w32-display-monitor-attributes-list display))
     ((eq frame-type 'ns)
      (ns-display-monitor-attributes-list display))

再往下就是针对各平台的具体实现了。

也可以通过命令行获取:

  • Linux (xrandr & xdpyinfo)
$ xdpyinfo | grep dimensions
dimensions:    1366x768 pixels (361x203 millimeters)
  • macOS
⋊> ./brightness -l -v | grep physical
      physical size 285 x 178 mm

当然也有跨平台的方式(准确度有待提高):

#+BEGIN_SRC python :results output :preamble "# -*- coding: utf-8 -*-"
import tkinter as tk

root = tk.Tk()

width_mm = root.winfo_screenmmwidth()
height_mm = root.winfo_screenmmheight()

print(f'Width: {width_mm} mm, Height: {height_mm} mm')
#+END_SRC

#+RESULTS:
: Width: 452 mm, Height: 282 mm
3 个赞

可以了解一下 chezmoi,一个专门用来管理配置文件的工具。它里面包含有 if 语句,可以实现不同设备中生成不同的实际调用配置文件。

该软件大概的原理是用户编辑 chezmoi 目录下的配置文件,然后由 chezmoi 负责生成正确的配置文件并应用。用户利用 git 管理 chezmoi 目录即可。

顺便提一下,chezmoi是法语的在我家的意思。

2 个赞