Subject: 27.2; [PATCH] ecomplete.el: Auto-select when there is only a single option

From: Phil Sainty <[email-masked]>
Date: Fri, 11 Feb 2022 15:47:14 +1300

I tried out ecomplete for email address completion today(*),
and I found it slightly annoying that when there was only a
single match for the text, I still have to firstly type M-n
to access the completion 'list', and then RET to select the
only available option.

Would the attached patch be a reasonable enhancement?

-Phil

(*) On account of 
https://www.reddit.com/r/emacs/comments/sl33w6/ecomplete_the_emacs_contact_manager_you_were/

In GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, X toolkit, Xaw scroll 
bars)
  of 2021-07-12 built on phil-lp
Windowing system distributor 'The X.Org Foundation', version 
11.0.12008000
System Description: Ubuntu 18.04.6 LTS
diff --git a/lisp/ecomplete.el b/lisp/ecomplete.el
index 260657e0f7..e011c3210e 100644
--- a/lisp/ecomplete.el
+++ b/lisp/ecomplete.el
@@ -80,6 +80,10 @@ ecomplete-sort-predicate
 		(function-item :tag "Sort by newness" ecomplete-newness)
 		(function :tag "Other")))
 
+(defcustom ecomplete-message-display-abbrev-auto-select t
+  "Whether `message-display-abbrev' should automatically select a sole option."
+  :type 'boolean)
+
 ;;; Internal variables.
 
 (defvar ecomplete-database nil)
@@ -174,6 +178,12 @@ ecomplete-display-matches
 	  (define-key local-map (kbd "<down>") next-func)
 	  (define-key local-map (kbd "M-p") prev-func)
 	  (define-key local-map (kbd "<up>") prev-func)
+          ;; Auto-select when there is only a single option.
+          (when ecomplete-message-display-abbrev-auto-select
+            (save-match-data
+              (when-let ((match (string-match "\\`\\(.+\\)\n" matches)))
+                (when (string= (match-string 0 matches) matches)
+                  (setq selected (match-string 1 matches))))))
 	  (let ((overriding-local-map local-map))
 	    (while (and (null selected)
 			(setq command (read-key-sequence highlight))