vis: improve C-n behavior in visual mode
[vis.git] / vis-open
blob6d2532690cfeb2a9af360d904751daa397920737
1 #!/bin/sh
2 set -e
4 # Later, we're going to want to set $IFS to a single newline, so let's prepare one.
5 NL='
8 VIS_MENU_PROMPT=""
9 ALLOW_AUTO_SELECT=1
11 wrap_dirs() {
12 local o
14 while read o
16 [ -d "$o" ] && printf "%s/\n" "$o" || printf "%s\n" "$o"
17 done
20 while [ $# -gt 0 ]; do
21 case "$1" in
22 -h|--help)
23 echo "usage: $(basename $0) [-h] [-p prompt] [-f] [--] [file-pattern]"
24 exit 0;
26 -p)
27 VIS_MENU_PROMPT=$2
28 shift
29 shift
31 -f)
32 ALLOW_AUTO_SELECT=""
33 shift
35 --)
36 shift
37 break
40 break
42 esac
43 done
45 # At this point, all the remaining arguments should be the expansion of
46 # any globs that were passed on the command line.
48 if [ $# -eq 1 -a "$ALLOW_AUTO_SELECT" = 1 ]; then
49 # If there were globs on the command-line, they've expanded to
50 # a single item, so we can just process it.
52 if [ -d "$1" ]; then
53 # Recurse and show the contents of the named directory,
54 # We pass -f to force the next iteration to present the
55 # full list, even if it's just an empty directory.
56 cd "$1"
57 IFS=$NL # Don't split ls output on tabs or spaces.
58 exec "$0" -p "$VIS_MENU_PROMPT" -f .. $(ls -1)
59 else
60 # We've found a single item, and it's not a directory,
61 # so it must be a filename (or file-like thing) to open,
62 # unless the parent directory does not exist.
63 if [ -d "$(dirname "$1")" ]; then
64 cd "$(dirname "$1")"
65 echo "$(pwd -P)"/"$(basename "$1" | sed 's/\*$//')"
66 exit 0
67 else
68 exit 1
73 # At this point, we have a bunch of options we need to present to the
74 # user so they can pick one.
75 CHOICE=$(printf "%s\n" "$@" | wrap_dirs | vis-menu -b -p "$VIS_MENU_PROMPT")
77 # Did they pick a file or directory? Who knows, let's let the next iteration figure it out.
78 exec "$0" -p "$VIS_MENU_PROMPT" -- "$CHOICE"