0

Here is a simple example in which I would like to replace

(start-process "ls" "*temp*" "ls" "-l" "-a" "-t" "-r") ; this works

with

(setq some-var ????) ; <-- Need to figure this part out
(start-process "ls" "*temp*" "ls" some-var)

But I cannot figure out how to represent that some-var.

Below did not work:

;; Attempt 1
(setq some-var "-l -a -t -r")

;; Attempt 2
(setq some-var '("-l" "-a" "-t" "-r"))

I basically need a solution to be able to provide variable number of args (&rest args) to a function using a variable.

Drew
  • 75,699
  • 9
  • 109
  • 225
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
  • 1
    See also: http://stackoverflow.com/questions/13361819/how-to-give-a-list-as-arguments-to-a-function-in-elisp – PythonNut Feb 18 '16 at 06:50

1 Answers1

4

You are looking for the function apply. Use it as follows:

(setq some-var '("-l" "-a" "-t" "-r"))
(apply #'start-process "ls" "*temp*" "ls" some-var)
PythonNut
  • 10,243
  • 2
  • 29
  • 75
Erik Hetzner
  • 765
  • 3
  • 6
  • @PythonNut You don't need `function` as long as the byte-compiler does not complain. `start-process` is used as symbol here. There is nothing the byte-compiler can optimize. Otherwise `function` would also be used there: http://www.gnu.org/software/emacs/manual/html_node/elisp/Calling-Functions.html#Calling-Functions. `function` is useful for `lambda`-expressions and other evaluable lisp expressions. – Tobias Feb 18 '16 at 08:09
  • 2
    @Tobias I tend to think of the sharp quote as being an annotation for the reader as well, just so it's extra clear that `start-process` is intended to be interpreted as a function. With that in mind, I tend to sharp-quote all symbols referencing functions for consistency. – PythonNut Feb 18 '16 at 14:54