text: mark text_size argument as const
[vis.git] / vis-complete
blob2e664b07ad5713b53a266aa029de7d0ec4a0ff39
1 #!/bin/sh
2 set -e
4 basic_regex_quote() { printf "%s" "$1" | sed 's|[\\.*^$[]|\\&|g'; }
5 glob_quote () { printf "%s" "$1" | sed 's|[\\?*[]]|\\&|g'; }
7 PATTERN=""
8 COMPLETE_WORD=0
9 FIND_FILE_LIMIT=1000
11 while [ $# -gt 0 ]; do
12 case "$1" in
13 -h|--help)
14 echo "usage: $(basename "$0") [-h] [--file|--word] [pattern]"
15 exit 0;
17 --file)
18 shift
20 --word)
21 COMPLETE_WORD=1
22 shift
25 PATTERN="$1"
26 break
28 esac
29 done
31 if [ $COMPLETE_WORD = 1 ]; then
32 tr -cs '[:alnum:]_' '\n' |
33 grep "^$(basic_regex_quote "$PATTERN")." |
34 sort -u
35 else
36 # Expand to absolute path because of the -path option below.
37 case $PATTERN in
38 /*)
39 XPATTERN=$PATTERN
41 '~'|'~/'*)
42 XPATTERN=$HOME$(echo $PATTERN | tail -c +2)
45 XPATTERN=$PWD/$PATTERN
47 esac
49 # The first path condition rules out paths that start with "." unless
50 # they start with "..". That way, hidden paths should stay hidden, but
51 # non-normalised paths should still show up.
52 find $(dirname "$XPATTERN") \
53 -name '.*' -prune \
54 -o \( \
55 ! -name '.*' \
56 -a -path "$(glob_quote "$XPATTERN")*" \
57 -print \
58 \) 2>/dev/null |
59 head -n $FIND_FILE_LIMIT |
60 sort |
61 sed "s|^$(dirname $XPATTERN)/||"
62 fi |
63 vis-menu -b |
64 sed "s|^$(basename $PATTERN)$(echo $PATTERN | tail -c 2 | fgrep /)||" |
65 tr -d '\n'