2

Say I have a defun with a dolist inside that I want to be able to pass a string that is then converted to a list with a length equal to the number of words in said string via the minibuffer, so something like:

minibuffer: list: arg1 or list: arg1 arg2 arg3 arg4

and just run the loop 3 more times

I have:

(defun some-function (&rest args)
  (interactive
    (list (some-function-to-read-multiple-arguments-as-a-list)))
  (dolist (arg args) 
    (some-function)))

I assume it's very trivial, but I've not been able to find anything in the docs about it.

Drew
  • 75,699
  • 9
  • 109
  • 225
Frede
  • 55
  • 5
  • You could just read this string from a minibuffer and then use `split-string` to turn it into a list. –  Aug 23 '18 at 19:05
  • Yea that works. there are so many functions in the standard library, that from the name could've been what I needed, `split-string` was the one! Thanks @DoMiNeLa10 – Frede Aug 23 '18 at 19:45

2 Answers2

1

You can use split-string to split a string into a list of strings. Here's a simple example of a function that uses it for a minibuffer prompt:

(defun test-function (&rest args)
  (interactive (split-string (read-from-minibuffer "Space separated list: ") " "))
  (mapc #'message args))
  • 2
    Note that, depending on your use-case, `split-string-and-unquote` *might* be preferable, as it means that users can include multi-word values by quoting them. – phils Aug 24 '18 at 03:22
  • Definitely a nice one to know as well! seeing as my application only needs single words, it's not needed in this case, but definitely will keep it in mind for future projects edit: grammar – Frede Aug 27 '18 at 17:39
0

I'd probably go with @DoMiNeLa10's answer, but just to mention another option: you could loop prompting for one thing at a time until, say, the user enters an empty string:

(defun test-function (args)
  (interactive
   (list
    (cl-loop for arg = (read-from-minibuffer "Next arg (leave empty to finish): ")
             until (string= arg "")
             collect arg)))
  (message "Read: %s" args))

(Also, instead of using &rest I made the arguments into a list, args; again just to show another option.)

Omar
  • 4,732
  • 1
  • 17
  • 32