```
(defun completing-read-in-region (start end collection &optional predicate)
"Prompt for completion of region in the minibuffer if non-unique.
Use as a value for `completion-in-region-function'."
(let* ((initial (buffer-substring-no-properties start end))
(all (completion-all-completions initial collection predicate
(length initial)))
(completion (cond
((atom all) nil)
((and (consp all) (atom (cdr all))) (car all))
(t (completing-read
"Completion: " collection predicate t initial)))))
(cond (completion (completion--replace start end completion) t)
(t (message "No completion") nil))))
(setq completion-in-region-function #'completing-read-in-region)
```
I have based this function at `consult-completion-in-region` but for some reason my version fails for some cases, like when you enter exact match into the minibuffer (it doesn't happen always which is especially annoying) or sometimes it removes wrong characters (eg it cuts the newline). I tried different variants with `delete-region` and `insert` but result was the same.
Any idea how to fix this?
EDIT:
Also for some reason it's completely broken in common lisp mode and doesn't allow to choose anything (enter doesn't do anything, it just completes like tab but doesn't quit, C-j works the same)
For reference I use eglot to provide completion sources (and sly for cl)