Skip to content

Commit

Permalink
Quote meta-characters in the completion pattern.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Screwtapello committed Oct 8, 2016
1 parent 55666d0 commit 9532165
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions vis-complete
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
/*)
Expand All @@ -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 |
Expand Down

0 comments on commit 9532165

Please sign in to comment.