From 608898f4dd423b73c06a35c91c4296c9ce80f688 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= <git@ameliathe1st.gay>
Date: Sat, 4 Jan 2025 22:40:59 +0100
Subject: [PATCH] =?utf8?q?Compl=C3=A9tion=20instantan=C3=A9e=20du=20pr?=
 =?utf8?q?=C3=A9fixe=20commun=20si=20plusieurs=20completions=20possibles?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

---
 bin/modes.ml | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/bin/modes.ml b/bin/modes.ml
index b1f0d46..8362f62 100644
--- a/bin/modes.ml
+++ b/bin/modes.ml
@@ -73,6 +73,13 @@ module rec Command : (Mode with type initer = string) = struct
           | x :: t -> x :: ch_last v t
           | [] -> assert false
 
+  let common_prefix s s' =
+          let rec aux i m =
+                  if i < 0 then String.sub s 0 m else if s.[i] = s'.[i] then aux (i - 1) m else aux (i - 1) i
+          in
+          let len = min (String.length s) (String.length s') in
+          aux (len - 1) len
+
   let real_complete cmd completions =
           let last = last cmd in
           match List.filter (String.starts_with ~prefix:last) completions with
@@ -80,10 +87,10 @@ module rec Command : (Mode with type initer = string) = struct
                   Printf.fprintf stderr "No completions !\n%!";
                   String.concat " " (List.map Parse.shquote cmd)
           | [possibility] -> String.concat " " (List.map Parse.shquote (ch_last possibility cmd)) ^ " "
-          | possibilities ->
+          | p :: ps ->
                   Printf.fprintf stderr "Completions:\n%!";
-                  List.iter (Printf.fprintf stderr "\t%s\n%!") possibilities;
-                  String.concat " " (List.map Parse.shquote cmd)
+                  List.iter (Printf.fprintf stderr "\t%s\n%!") (p :: ps);
+                  String.concat " " (List.map Parse.shquote (ch_last (List.fold_left common_prefix p ps) cmd))
 
   let complete_file cmd f =
           let dir_files dir =
-- 
2.46.0