make C-s and C-q available in key mappings
[vis/gkirilov.git] / vis-open
blob28d1b1a91f31a14a8983eae7ac0e5ff8852a2796
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 if [ -z "$VIS_OPEN_LINES" ]; then
9 VIS_OPEN_LINES='0'
12 VIS_MENU_PROMPT=''
13 ALLOW_AUTO_SELECT='1'
15 wrap_dirs() {
16 while read -r filename
18 if [ -d "$filename" ]; then
19 printf '%s/\n' "$filename"
20 else
21 printf '%s\n' "$filename"
23 done
26 while getopts fhp: opt; do
27 case "$opt" in
29 ALLOW_AUTO_SELECT=''
32 VIS_MENU_PROMPT="$OPTARG"
34 h|?)
35 printf 'usage: %s [-f] [-h] [-p prompt] [--] [file-pattern]\n' "${0##*/}"
36 exit 0
38 esac
39 done
40 shift "$((OPTIND - 1))"
42 # At this point, all the remaining arguments should be the expansion of
43 # any globs that were passed on the command line.
45 if [ "$#" -eq 1 ] && [ "$ALLOW_AUTO_SELECT" = '1' ]; then
46 # If there were globs on the command-line, they've expanded to
47 # a single item, so we can just process it.
49 if [ -d "$1" ]; then
50 # Recurse and show the contents of the named directory,
51 # We pass -f to force the next iteration to present the
52 # full list, even if it's just an empty directory.
53 cd "$1"
54 IFS="$NL" # Don't split ls output on tabs or spaces.
55 exec "$0" -p "$VIS_MENU_PROMPT" -f "$(ls -1)"
56 else
57 # We've found a single item, and it's not a directory,
58 # so it must be a filename (or file-like thing) to open,
59 # unless the parent directory does not exist.
60 parentdir="$(dirname -- "$1")"
61 if [ -d "$parentdir" ]; then
62 cd "$parentdir"
63 printf '%s/%s\n' "$(pwd -P)" "$(basename -- "${1%\*}")"
64 exit 0
65 else
66 exit 1
71 # At this point, we have a bunch of options we need to present to the
72 # user so they can pick one.
73 CHOICE="$(printf '%s\n' '..' "$@" | wrap_dirs | vis-menu -b -l "$VIS_OPEN_LINES" -p "$VIS_MENU_PROMPT")"
75 # Did they pick a file or directory? Who knows, let's let the next iteration figure it out.
76 exec "$0" -p "$VIS_MENU_PROMPT" -- "$CHOICE"