From: Amélia Coutard-Sander Date: Wed, 7 Jan 2026 13:38:53 +0000 (+0100) Subject: Complétion instantanée du préfixe commun si plusieurs completions possibles X-Git-Url: https://git.f4mnq.fr/?a=commitdiff_plain;h=3d8e86da0516bb0dc257d158f55940c705a71ed7;p=cells.git Complétion instantanée du préfixe commun si plusieurs completions possibles --- diff --git a/bin/modes.ml b/bin/modes.ml index a4eb173..5cded68 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 =