Subject: Re: bug#12638: 24.2.50; FR: Some suggestions for icomplete-mode

From: Jambunathan K <[email-masked]>
Date: Thu, 13 Dec 2012 19:21:06 +0530

Jambunathan K <[email-masked]> writes:

> Stefan Monnier <[email-masked]> writes:
>
>>> 2. M-x debbugs-gnu
>>>    There is no way I can C-s C-j to the second or third candidates.
>>>    I normally use debbbugs-gnu-search which is the second candidate.
>>
>> Better ask this elsewhere, I'm not up to speed on debbugs-gnu.
>
> I am attaching screenshot of minibuffer. 
>
>     1. M-x debbugs-gnu
>     2. Immediately cycle with C-s
>
> Cycling may happen internally but the UI presents the "most-is-exact"
> always at the head.  This is a definite bug.

The case to be handled is "Complete, but not unique".  

The attached patch fixes the issue by sneaking a Ctrl-M in to the try
try completion field.

Attachments: 
M-x debbugs-gnu RET with icomplete-mode on.

icomplete-1.png:  
        Note the ^M and the empty first entry

icomplete-2.png:  
        After a C-s.  Note the ^M indicator and empty entry pushed to
        the last.

icomplete-3.png:
        This is the second invocation of M-x debbugs-gnu RET.  Note that
        the entry from the recent history is at the head.  Also note ^M
        and the empty entries.

=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2012-12-13 05:29:15 +0000
+++ lisp/ChangeLog	2012-12-13 13:32:58 +0000
@@ -1,3 +1,10 @@
+2012-12-13  Jambunathan K  <[email-masked]>
+
+	* icomplete.el (icomplete-hide-common-prefix): New user variable.
+	(icomplete-first-match): New user face.
+	(icomplete-completions): Honor above settings.  Correctly handle
+	the case when NAME is complete but not unique (Bug#12638).
+
 2012-12-13  Alan Mackenzie  <[email-masked]>
 
 	Make CC Mode not hang when _some_ lines end in CRLF.  Bug #11841.

=== modified file 'lisp/icomplete.el'
--- lisp/icomplete.el	2012-11-30 17:09:55 +0000
+++ lisp/icomplete.el	2012-12-13 13:16:31 +0000
@@ -76,6 +76,18 @@
   :type 'string
   :version "24.4")
 
+(defcustom icomplete-hide-common-prefix t
+  "When non-nil, hide common prefix from completion candidates.
+When nil, show candidates in full."
+  :type 'boolean
+  :version "24.4"
+  :group 'icomplete)
+
+(defface icomplete-first-match  '((t :weight bold))
+  "Face used by icomplete for highlighting first match."
+  :version "24.4"
+  :group 'icomplete)
+
 ;;;_* User Customization variables
 (defcustom icomplete-prospects-height
   ;; 20 is an estimated common size for the prompt + minibuffer content, to
@@ -332,14 +344,12 @@ are exhibited within the square braces.)
 	     (determ (unless (or (eq t compare) (eq t most-try)
 				 (= (setq compare (1- (abs compare)))
 				    (length most)))
-		       (concat open-bracket
 			       (cond
 				((= compare (length name))
                                  ;; Typical case: name is a prefix.
 				 (substring most compare))
 				((< compare 5) most)
-				(t (concat "..." (substring most compare))))
-			       close-bracket)))
+			(t (concat "..." (substring most compare))))))
 	     ;;"-prospects" - more than one candidate
 	     (prospects-len (+ (length determ) 6 ;; take {,...} into account
                                (string-width (buffer-string))))
@@ -350,6 +360,8 @@ are exhibited within the square braces.)
                     ;; one line, increase the allowable space accordingly.
                     (/ prospects-len (window-width)))
                  (window-width)))
+	     (prefix (when icomplete-hide-common-prefix
+		       (try-completion "" comps)))
              (prefix-len
               ;; Find the common prefix among `comps'.
 	      ;; We can't use the optimization below because its assumptions
@@ -359,37 +371,58 @@ are exhibited within the square braces.)
 	      ;;     ;; Common case.
 	      ;;     (length most)
 	      ;; Else, use try-completion.
-	      (let ((comps-prefix (try-completion "" comps)))
-		(and (stringp comps-prefix)
-		     (length comps-prefix)))) ;;)
-
+	      (and (stringp prefix) (length prefix))) ;;)
 	     prospects most-is-exact comp limit)
 	(if (eq most-try t) ;; (or (null (cdr comps))
 	    (setq prospects nil)
+	  (when (and (cdr comps) (member name comps))
+	    ;; NAME is complete but not unique.  This scenario, poses
+	    ;; following UI issues:
+	    ;;
+	    ;; - When common prefix is hidden, NAME is stripped empty.
+	    ;;   This would make the entry inconspicuous.
+	    ;; - Due to sorting of completions, NAME may not be the
+	    ;;   first of the prospects and could be hidden deep in the
+	    ;;   displayed string.
+	    ;; - Because of truncation, NAME may not even be displayed
+	    ;;   to the user.
+	    ;;
+	    ;; To circumvent all the above problems, provide a visual
+	    ;; cue to the user via try completion field.
+	    (setq determ "
"))
 	  (while (and comps (not limit))
 	    (setq comp
 		  (if prefix-len (substring (car comps) prefix-len) (car comps))
 		  comps (cdr comps))
-	    (cond ((string-equal comp "") (setq most-is-exact t))
-		  ((member comp prospects))
-		  (t (setq prospects-len
+	    (setq prospects-len
                            (+ (string-width comp) 1 prospects-len))
 		     (if (< prospects-len prospects-max)
 			 (push comp prospects)
-		       (setq limit t))))))
+	      (setq limit t))))
+	(setq prospects (nreverse prospects))
+	;; Decorate the first of prospects.
+	(when prospects
+	  (let ((first (copy-sequence (pop prospects))))
+	    (put-text-property 0 (length first)
+			       'face 'icomplete-first-match first)
+	    (push first prospects)))
         ;; Restore the base-size info, since completion-all-sorted-completions
         ;; is cached.
         (if last (setcdr last base-size))
+	(when determ
+	  (setq determ (concat open-bracket determ close-bracket)))
 	(if prospects
 	    (concat determ
+		    (if (not (cdr prospects))
+			;; NAME is not complete but will complete to
+			;; an EXACT match on next try.
+			""
+		      (concat
 		    "{"
-		    (and most-is-exact
-                         (substring icomplete-separator
-                                    (string-match "[^ ]" icomplete-separator)))
-		    (mapconcat 'identity (nreverse prospects)
-                               icomplete-separator)
+		       (mapconcat 'identity prospects icomplete-separator)
 		    (and limit (concat icomplete-separator "…"))
-		    "}")
+		       "}"
+		       )))
 	  (concat determ " [Matched]"))))))
 
 ;;_* Local emacs vars.