I want to build a list step by step, adding or not adding certain strings to the list based on the values of some variables.
I am writing a package that runs an executable and it has variables that can be used to configure behavior by deciding whether or not I add some arguments to the command.
When I'm about to call the tool, I want to build the command as a list (list argv[0] argv[1]...) which I use in (make-process :command (repos--create-command). I got it to work and went on to write the rest of my package but this seems wrong
(defun repos--create-command () ;; Add-to-list adds to the front ;; Also some guy who looks like he gets LISP says add-to-list isn't good ;; for building a list the way I want. (let ((args (list))) (when repos-overview-all (add-to-list 'args "-all")) (when repos-overview-n-jobs (add-to-list 'args (number-to-string repos-overview-n-jobs)) (add-to-list 'args "-j")) (when (not repos-overview-ignore) (add-to-list 'args "-noignore")) (unless repos-overview-fetch (add-to-list 'args "--no-fetch")) (if (boundp 'other-config-file) (add-to-list 'args other-config-file) (add-to-list 'args repos-config-file)) (add-to-list 'args "-F") (add-to-list 'args repos-command) args)) The idea is that when some variable is true, I will add --all to the arguments, if repos-overview-njobs is non-nil, I will add -j N where N is the value of repos-overview-n-jobs.
(defvar arg), makingarga special (dynamic) variable. Theletbinding would then affect that dynamic variable instead of a lexical variable, soadd-to-listwould work. But the right approach is, as Shynur said, to usepushorcl-pushnewinstead ofadd-to-list.