0

I want to set the foreground colours for outlines using a list stored in pigment-darkbg. How can I do this in a neat and short way ?

(defun outline-tyface ()
  (interactive)

  (let ( (pigment-darkbg
            [ "#4fafff" "#4fe42f" "#fe6060" "#f0dd60"
              "#ff62d4" "#3fdfd0" "#fba849" "#9f80ff" ]) )

    (set-face-foreground 'outline-1 "#4fafff")
    (set-face-foreground 'outline-2 )
    (set-face-foreground 'outline-3 )
    (set-face-foreground 'outline-4 )
    (set-face-foreground 'outline-5 )
    (set-face-foreground 'outline-6 )
    (set-face-foreground 'outline-7 )
    (set-face-foreground 'outline-8 )))
6
  • 1
    I see no particular reason to use a vector there. In Lisp it's usually simpler and better to use lists. But if you really want to use a vector then instead of just dolist (as @db48x showed) you'll need to use while or equivalent, and use aref to access the iterated indexed vector element.
    – Drew
    Jun 17, 2023 at 2:13
  • 1
    In a case like this, where you can tell you want to iterate, it's a good idea to get in the habit of asking Emacs: look for iteration in the Elisp manual: i iteration. And i iteration over vector or string tells you about seq-doseq, which is like dolist but works also for a vector or string.
    – Drew
    Jun 17, 2023 at 2:15
  • @Drew: Perhaps because vector is faster to access and takes up fewer bytes than list?
    – shynur
    Jun 17, 2023 at 5:17
  • 1
    I have no problem using a list instead.
    – Dilna
    Jun 17, 2023 at 12:41
  • @shynur: Relevant in the example? I don't expect so. It happens that people used to other languages sometimes use vectors/arrays/strings more than is common with Lisp.
    – Drew
    Jun 17, 2023 at 22:21

1 Answer 1

1

Chapter 11.5 Iteration of the Emacs Lisp Manual will tell you how to write several kinds of loops. I recommend using dolist. To quote from the documentation:

   The ‘dolist’ and ‘dotimes’ macros provide convenient ways to write
two common kinds of loops.

 -- Macro: dolist (var list [result]) body...
     This construct executes BODY once for each element of LIST, binding
     the variable VAR locally to hold the current element.  Then it
     returns the value of evaluating RESULT, or ‘nil’ if RESULT is
     omitted.  For example, here is how you could use ‘dolist’ to define
     the ‘reverse’ function:

          (defun reverse (list)
            (let (value)
              (dolist (elt list value)
                (setq value (cons elt value)))))

I’m sure you can take it from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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