aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Allen <screwtape@froup.com>2016-10-08 16:05:21 +1100
committerTim Allen <screwtape@froup.com>2016-10-08 16:05:21 +1100
commit9532165df3f7e055d8e4e8fee021fdda96593f46 (patch)
tree45b709d436311346207565e79923ebb79dce8fb1
parent55666d0f67bb94d5654a9389031fa9269cf512a5 (diff)
downloadvis-9532165df3f7e055d8e4e8fee021fdda96593f46.tar.gz
vis-9532165df3f7e055d8e4e8fee021fdda96593f46.tar.xz
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.
-rwxr-xr-xvis-complete9
1 files changed, 7 insertions, 2 deletions
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 |