vis: perform more renames cursor -> selection
[vis.git] / vis-complete
blob398244f485c77ee10c097807b4712f76800273e1
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 = 0 ]; then
32 case $PATTERN in
33 /*)
34 # An absolute path. This is fine.
36 '~'|'~/'*)
37 # Expand tilde to $HOME
38 PATTERN=$HOME$(echo $PATTERN | tail -c +2)
41 # A relaive path. Let's make it absolute.
42 PATTERN=$PWD/$PATTERN
44 esac
47 if [ $COMPLETE_WORD = 1 ]; then
48 tr -cs '[:alnum:]_' '\n' |
49 grep "^$(basic_regex_quote "$PATTERN")." |
50 sort -u
51 else
52 START=$(dirname "$PATTERN")
53 # The first path condition rules out paths that start with "." unless
54 # they start with "..". That way, hidden paths should stay hidden, but
55 # non-normalised paths should still show up.
56 find "$START" \
57 -name '.*' -prune \
58 -o \( \
59 ! -name '.*' \
60 -a -path "$(glob_quote "$PATTERN")*" \
61 -print \
62 \) 2>/dev/null |
63 head -n $FIND_FILE_LIMIT |
64 sort
65 fi |
66 vis-menu -b |
67 sed "s/^$(printf "%s" "$PATTERN" | sed 's:/:\\/:g' )//" |
68 tr -d '\n'