Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactively select multiple items

Tags:

emacs

elisp

How can I let the user select multiple items from a list instead of just one? Like in the C-x b menu in helm.

Currently I can only select a single item instead of getting a complete list:

(defun test-main ()
  (interactive)
  (let ((choice (completing-read "Select: " '("item1 item2 item3"))))
    (message choice)))
like image 969
Kiwi Avatar asked Nov 15 '15 18:11

Kiwi


2 Answers

You can do that with completing-read-multiple:

(defun test-main ()
  (interactive)
  (let ((choice (completing-read-multiple "Select: " '("item1" "item2" "item3"))))
    (message "%S" choice)))

It returns the selected items as a list, so if you type item2,item3 at the prompt, it returns ("item2" "item3").

like image 80
legoscia Avatar answered Nov 15 '22 03:11

legoscia


Because that's what vanilla completing-read does. It reads and returns a single choice, providing completion to help you choose.

You can do what you are asking for with Icicles. It redefines function completing-read when Icicle mode is on.

Many Icicles commands are multi-commands, which means that you can make multiple input choices in a single command execution: a multiple-choice command. You can define your own multi-commands, using any actions.

And for any completion (not just for a multi-command), you can manipulate, save, and restore sets of completion candidates.

(You can also enter multiple inputs in the minibuffer. This is useful even for commands that read input without completion.)

like image 29
Drew Avatar answered Nov 15 '22 05:11

Drew