about summary refs log tree commit diff
path: root/Completion/Commands/_correct_filename
diff options
context:
space:
mode:
Diffstat (limited to 'Completion/Commands/_correct_filename')
-rw-r--r--Completion/Commands/_correct_filename37
1 files changed, 37 insertions, 0 deletions
diff --git a/Completion/Commands/_correct_filename b/Completion/Commands/_correct_filename
new file mode 100644
index 000000000..edf1c65c2
--- /dev/null
+++ b/Completion/Commands/_correct_filename
@@ -0,0 +1,37 @@
+#defkeycomp complete-word \C-xc
+
+# Function to correct a filename.  Can be used as a completion widget,
+# or as a function in its own right, in which case it will print the
+# corrected filename to standard output.
+#
+# You can adapt max_approx to the maximum number of mistakes
+# which are allowed in total.
+
+emulate -LR zsh
+setopt extendedglob
+
+local file="$PREFIX$SUFFIX" trylist
+integer approx max_approx=6
+
+[[ -z $WIDGET ]] && file=$1 
+
+if [[ -e "$file" ]]; then
+  if [[ -n $WIDGET ]]; then
+    compadd "$file"
+  else
+    print "$file"
+  fi
+  return
+fi
+
+for (( approx = 1; approx <= max_approx; approx++ )); do
+  trylist=( (#a$approx)"$file"(N) )
+  (( $#trylist )) && break
+done
+(( $#trylist )) || return 1
+
+if [[ -n $WIDGET ]]; then
+  compadd -U "${trylist[@]}"
+else
+  print "${trylist[@]}"
+fi