-
Notifications
You must be signed in to change notification settings - Fork 0
/
vim-session
executable file
·96 lines (86 loc) · 2.4 KB
/
vim-session
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash -e
SESSION_FILE_DEFAULT="./session"
DBG=false
VIM=vim
while [ ${#} -gt 0 ]
do
case "${1}" in
--) shift 1; break;;
-D) shift 1; DBG=true;;
-e) shift 1; VIM=${1}; shift 1;;
-*) echo "unknown option ${1}" >&1; exit 1;;
esac
done
main()
{
local session_files=("${@}")
[ ${#session_files[@]} -eq 0 ] && session_files=("${SESSION_FILE_DEFAULT}")
local vim_files
vim_files=(
$(parse_session_files "${session_files[@]}" || return ${?})
) || return ${?}
local cmd
cmd="TERM=screen-256color ${VIM} -- $(echo "${vim_files[@]}")" || return ${?}
${DBG} && echo "${cmd}" >&2 || eval "${cmd}"
return 0
}
#
# Parse multiple sessions files for file paths.
#
parse_session_files()
{
local files=("${@}")
${DBG} && echo "parse_session_files" >&2
${DBG} && echo "| files: ${files[@]}" >&2
local file
for file in "${files[@]}"
do
parse_session_file "${file}" || return ${?}
done
return 0
}
#
# Parse single sessions file (possibly nested) for file paths.
#
parse_session_file()
{
local file="${1}"
${DBG} && echo "parse_session_file" >&2
${DBG} && echo "| file: ${file}" >&2
[ ! -f "${file}" ] && { echo "no such file: '${file}'" >&2; return 1; }
local root="$(realpath --relative-to="." "$(dirname ${file})")"
${DBG} && echo "> root: ${root}" >&2
${DBG} && echo "> read lines from ${file}" >&2
local line
while read line
do
${DBG} && echo " '${line}'" >&2
line="$(echo ${line})" # strip whitespace
if [ "${line:0:1}" == '<' ]
then
local sub_file="${root}/$(echo ${line:1})" # strip whitespace
${DBG} && echo " - sub_file: ${sub_file}" >&2
parse_session_file "${sub_file}" "${root}/$(dirname "${sub_file}")" || return ${?}
else
parse_session_line "${line}" "${root}" || return ${?}
fi
done < "${file}"
return 0
}
#
# Parse a line in a sessions file for file paths.
#
parse_session_line()
{
local line="${1}"
local root="${2}"
${DBG} && echo "parse_session_line" >&2
${DBG} && echo "| line: ${line}" >&2
${DBG} && echo "| root: ${root}" >&2
line="$(echo "${line}" | sed -e 's/^ \+//' -e 's/#.*$//')"
[ "${line}" == '' ] && return 0
local file="$(echo "${root}/${line}" | sed -e 's@\./@@' -e 's@/./@/@g')"
echo "${file}"
return 0
}
main "$@"