From: Amélia Coutard-Sander Date: Sat, 4 Jan 2025 21:40:59 +0000 (+0100) Subject: Complétion instantanée du préfixe commun si plusieurs completions possibles X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=608898f4dd423b73c06a35c91c4296c9ce80f688;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 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 =