添加或替换嵌套关联列表中的项目



来自文档:

变量window-system-default-frame-alist是 元素(WINDOW-SYSTEM . ALIST),其中WINDOW-SYSTEM是一个 窗口系统符号和ALIST是框架参数列表。

假设我想更改 X Window 系统的框架参数 alist,以便它包含元素(font . "Monospace-11"),但我不希望该 alist 中包含多个font元素。

那我该怎么办?基本上,我遍历了 alists,删除了我要替换/添加的元素,然后添加元素。 这是代码:

(let ((x-frame-alist (alist-get 'x window-system-default-frame-alist)))
(cl-acons 'x (cl-acons 'font "Monospace-11"
(cl-remove (cl-assoc 'font x-frame-alist)
x-frame-alist))
(cl-remove `(x . ,x-frame-alist)
window-system-default-frame-alist)))

然后必须将结果分配给符号window-system-default-frame-alist。 但不知何故,这一切看起来都过于复杂。 当然,必须有一个更简单的解决方案。

你可以做:

(push '(font . "Monospace-11")
(alist-get 'x window-system-default-frame-alist))

(setf (alist-get 'font (alist-get 'x window-system-default-frame-alist))
"Monospace-11")

您可以使用push来设置新字体,assq-delete-all来删除旧设置。

(progn
(setq frame-alist '((x . ((font . "remove")
(other . "d")
(font . "remove")
(other . "c")))
(w32 . ((font . "keep")))))
(setq x-change-font (assq-delete-all 'font (alist-get 'x frame-alist)))
(push '(font . "wanted") x-change-font)
(setq frame-alist
(mapcar
(lambda (elem) (if (eq (car elem) 'x) `(x . ,x-change-font) elem))
frame-alist ))
frame-alist)

返回:

((x   (font . "wanted") (other . "d") (other . "c"))
(w32 (font . "keep")))

最新更新