【问题标题】:Append to default value of a list in emacs-lisp: specifically, dotspacemacs-configuration-layers附加到 emacs-lisp 中列表的默认值:具体来说,dotspacemacs-configuration-layers
【发布时间】:2021-04-26 06:52:41
【问题描述】:

目标

我希望通过引用文件user-config.orguser-layers.org 来进一步扩展我的spacemacs 配置的模块化

模板

我通过将以下内容添加到我的 dotspacemacs/user-config 来实现前者:

(defun dotspacemacs/user-config ()
  (org-babel-load-file (expand-file-name
                        "user-config.org" dotspacemacs-directory)))

我可以安全地试验,克隆同步,organize,版本控制user-config.org

问题

现在, dotspacemacs/user-layers 是一个变量(默认)设置在一个函数中,方式如下:

(defun dotspacemacs/layers ()
  (setq-default
   dotspacemacs-distribution 'spacemacs  ;; not relevant
   ;;
   ;; many such default variable-value lines in between, skipped.
   ;;
   dotspacemacs-configuration-layers
   '(
     ;;
     ;; many layers auto-added/managed by spacemacs
     ;;
     git
     (python :variables python-backend 'lsp
           python-lsp-server 'pyright
           ;; other such python configurations
           python-formatter 'yapf))
   ;; other such default varialbe-value declarations
  )
)

尝试

我尝试通过以下方式在setq-default 的末尾添加类似的继承(如user-config),

  • 注意dotspacemacs-configuration-layers 不能在函数 dotspacemacs/layers 之外修改,因此必须在该函数中调用对文件的引用。
(defun dotspacemacs/layers ()
  (setq-default
    dotspacemacs-distribution 'spacemacs
    ;;
    ;; many such default variable-value lines in between, skipped.
    ;;
    dotspacemacs-configuration-layers
    '(
    ;; whatever spacemacs wants to auto-add
    )
    ;; other such default variable-value declarations
  )
  (org-babel-load-file (expand-file-name
    "user-layers.org" dotspacemacs-directory))
)

——这样user-layers.org 的格式如下:

引用org-mode文件:

  • dotspacemacs 层

#+BEGIN_SRC emacs-lisp

(append dotspacemacs-configuration-layers
  '(
    ;;
    ;; Other layers managed by the user
    ;;
    git
    (python :variables python-backend 'lsp
            python-lsp-server 'pyright
            ;; other such python configurations
            python-formatter 'yapf)))

#+END_SRC

失败

但是,append 似乎不会将元素添加到 seq-default 值。 我猜它附加到变量的本地 setq 值。 (我可能错了,因为 lisp 不是我的母语,python 才是。)

问题:

有没有办法通过添加另一个列表中的元素来扩展 emacs-lisp 中列表的 默认 值?

澄清:

——这样添加后,列表的默认值包含新添加的元素。

【问题讨论】:

标签: emacs append spacemacs


【解决方案1】:

这样的事情应该可以工作:

(setq-default dotspacemacs-configuration-layers
   (append (default-value 'dotspacemacs-configuration-layers)
           '(git ...)))

请注意,append 不会修改原始列表 - 它会创建一个添加了额外值的新列表。要将值添加到包含在变量中的列表,您需要重新分配新列表:

*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (setq x (list 1 2 3))
(1 2 3)

ELISP> (setq y (list 4 5 6))
(4 5 6)

ELISP> (append x y)
(1 2 3 4 5 6)

ELISP> x
(1 2 3)

ELISP> (setq x (append x y))
(1 2 3 4 5 6)

ELISP> x
(1 2 3 4 5 6)

【讨论】:

  • ... 或者您可以使用(add-to-list 'some-list new-element)。可选的第三个参数确定新元素是添加到列表的开头还是结尾。详情请C-h f add-to-list
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
相关资源
最近更新 更多