selecting multiple item from completion in emacs

Table of Contents

1. vanilla completing-read-multiple

completing-read-multiple is one simple solution, with a few caveats:

  • all selected items are in your minibuffer, looking like this:

    emacs,anotther item, item 3333, this, and that
    

    When the list get long, it gets even more annoying, and impossible to read

  • deselecting is not quite convenient deleting the entire “,…,” item, which can be a handful as you typically don’t have evil or any edit util in minibuffer
  • CRM-seperator can be in your candidate text, which results in information loss in output The default CRM-seperator(completing read multiple seperator, which concludes an item. in item1,item2, it is ,). is ,, and if your candidate looks like "the past, present and future of emacs completion", it will be returned as ("the past" "present and future of emacs completion)

In all, it could be used if you won’t select too much items, they are short, and they don’t contain ,

2. citar--select-multiple   ATTACH

citar have implemented a wonderful front-end of multiple selection(link). _20240310_150252screenshot.png this completing-read front-end is awesome, and the only requirement to have multiple selection working is the candidate being a hashtable.

All you need to do is putting all strings you have from the candidate list to a hashtable, and:

(defun hermanhel-strings-to-hash (strings)
  "Convert a list of STRINGS to a hash table with the strings as keys."
  (let ((hash (make-hash-table :test 'equal)))
    (dolist (str strings)
      (puthash str t hash))
    hash))
(setq candidates (hermanhel-strings-to-hash '("item 1" "item ,2" "item 3")))

(citar--select-multiple "References: " candidates)

_20240310_151505screenshot.png In this way you get:

  • search and completion
  • TAB to select
  • return a nice list of string ("item 1" "item 2")

with the following caveat:

  • TAB or ENT on selected item will result in error Debugger entered--Lisp error: (setting-constant nil)

To solve the caveat, as well as making this more friendly to use without all other parts of citar, there’s certainly a lot more to do.

Backlinks

emacs

Emacs is an editor featuring:

Author: Linfeng He

Created: 2024-04-03 Wed 23:24