1

I want to build a ring of symbols in my init.el for the purpose of rotating themes. I could do it like this:

(let* ((themes '(doom-one zenburn leuven))
       (ring (make-ring (length themes))))
  (mapc (lambda (x) (ring-insert ring x))
        themes)
  (setq my-theme-ring ring))

which results in the following data-structure

(0 3 . [doom-one zenburn leuven])

Or, I could build it more directly:

(let* ((vector [doom-one zenburn leuven])
       (ring `(0 ,(length vector) . ,vector)))
  (setq my-theme-ring ring)

The first approach goes through the ring.el public API and makes no assumptions about its implementation.

The second approach does less work, but could break if ring.el changes its implementation.

I think I can use eval-when-compile to get the best of both worlds:

(setq my-theme-ring
      (eval-when-compile
        (let* ((themes '(doom-one zenburn leuven))
               (ring (make-ring (length themes))))
          (mapc (lambda (x) (ring-insert ring x))
                themes)
          ring)))

Or maybe even:

(eval-when-compile
  (let* ((themes '(doom-one zenburn leuven))
         (ring (make-ring (length themes))))
    (mapc (lambda (x) (ring-insert ring x))
          themes)
    (setq my-theme-ring ring)))

When I compile my init file, it will iterate over the themes and add them one-by-one to the ring. When I start Emacs and load my already compiled init file, however, it will just read the already populated ring that resulted during compilation.

Is that right? And if so, can I nest the setq call within the eval-when-compile body too, as I've done in the last code snippet, or can I only use the compiled result for the value it returns?

1 Answer 1

2

Moving the setq into the eval-when-compile means that the variable will be assigned during byte-compilation but not when you load init.elc, so it's probably not what you want.

BTW, I'd recommend (dolist (x '(doom-one zenburn leuven)) (ring-insert ring x)) over using mapc, tho it's largely a question of taste.

1
  • I suspected as much about putting the setq in the body of eval-when-compile -- good to know, thanks!
    – ivan
    Mar 13, 2017 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.