emacsappendspacemacs

Append to default value of a list in emacs-lisp: specifically, dotspacemacs-configuration-layers


Goal

I wish to further extend modularity of my spacemacs configuration by referencing files user-config.org and user-layers.org

Template

I have achieved the former by adding the following to my dotspacemacs/user-config:

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

I can safely experiment with, clone-sync, organize, version-control user-config.org

Problem

Now, dotspacemacs/user-layers is a variable (default-)set in a function in the following manner:

(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
  )
)

Attempt

I tried adding a similar inheretance (as with user-config) at the end of the setq-default in the following manner,

(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))
)

—such that user-layers.org is of the following form:

Referenced org-mode file:

#+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

Failure

However, append does not appear to add elements to the seq-default value. I guess it is appending to the variable's local setq value. (I may be wrong at this, as lisp isn't my motherbored tongue, python is.)

Question:

Is there a way to extend the default value of a list in emacs-lisp by adding elements from another list?

clarification:

—such that after addition, the default value of the list contains the newly added elements.


Solution

  • Something like this should work:

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

    Note that append doesn't modify the original list - it creates a new list with the extra values added. To add values to a list contained in a variable you need to assign back the new list:

    *** 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)