From 9532165df3f7e055d8e4e8fee021fdda96593f46 Mon Sep 17 00:00:00 2001 From: Tim Allen Date: Sat, 8 Oct 2016 16:05:21 +1100 Subject: Quote meta-characters in the completion pattern. Because we're completing text from the document, we can't assume it's going to be a sensible regex pattern, or glob pattern, let alone both, so we should quote the pattern before we hand it off to helper tools like grep and find. --- vis-complete | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'vis-complete') diff --git a/vis-complete b/vis-complete index 52dd464..fa5bbf0 100755 --- a/vis-complete +++ b/vis-complete @@ -1,6 +1,9 @@ #!/bin/sh set -e +basic_regex_quote() { printf "%s" "$1" | sed 's|[\\.*^$[]|\\&|g'; } +glob_quote () { printf "%s" "$1" | sed 's|[\\?*[]]|\\&|g'; } + PATTERN="" COMPLETE_WORD=0 FIND_FILE_LIMIT=1000 @@ -26,7 +29,9 @@ while [ $# -gt 0 ]; do done if [ $COMPLETE_WORD = 1 ]; then - tr -cs '[:alnum:]_' '\n' | grep "^$PATTERN." | sort -u + tr -cs '[:alnum:]_' '\n' | + grep "^$(basic_regex_quote "$PATTERN")." | + sort -u else case $PATTERN in /*) @@ -41,7 +46,7 @@ else START=$(dirname "$PATTERN") find "$START" \ ! -path '*/\.*' \ - -a -path "$PATTERN*" 2>/dev/null | + -a -path "$(glob_quote "$PATTERN")*" 2>/dev/null | head -n $FIND_FILE_LIMIT | sort fi | -- cgit v1.2.3