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.

  • You could just read this string from a minibuffer and then use split-string to turn it into a list. – DoMiNeLa10 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
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.)

  • Very neat option @Omar! – Frede Sep 18 '18 at 21:45
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 3: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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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