-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrain_functions.sh
More file actions
99 lines (96 loc) · 2.67 KB
/
brain_functions.sh
File metadata and controls
99 lines (96 loc) · 2.67 KB
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
97
98
99
#!/usr/bin/zsh
cwd=$0:A:h
source "$cwd/conf.sh"
source "$cwd/grep.sh"
source "$cwd/find.sh"
# LANGUAGE
__brain_root_edit () {
if [[ $# -eq 0 ]]; then
echo "brain: no search term provided"
return 1
fi
local f=$(__brain_find_file "$@")
local multiple=0
[[ "$f" =~ '.*:.*' ]] && multiple=1
if [[ "$f" == "" ]]; then
echo "brain: no information available about '$@'"
return 1
fi
if [[ $multiple == 1 ]]; then
local first=$(echo $f|sed "s,:,\n,g"|head -n1)
local all=$(echo $f|sed "s,:,\n,g")
echo -e "brain: ambiguous query result {\n$all\n}, choosing '$first'"
f="$first"
fi
"$EDITOR" "$f"
}
__brain_pw_edit () {
local __brain_roots=$__brain_pw_roots __brain_suffix=$__brain_pw_suffix
__brain_root_edit "$@"
}
__brain_session () {
local sess="$__brain_session_dir/$1.vim"
[[ -f "$sess" ]] || return 1
$EDITOR -S "$sess"
}
__brain_human_files () {
for f in $(find $__brain_human_root -type f -not \( -name '*~' -or -name '*.vcf' \) ); do
echo "${f#$__brain_human_root/}"
done
}
__brain_human_edit () {
local file="$HOME/z/priv/misc/contact/$1"
$EDITOR "$file"
}
__brain_human () {
__brain_human_edit "$@"
}
brain () {
if [[ $# -eq 0 ]]; then
echo "brain"
echo " e|edit <file> search brain for lan.<file>/<file.lang an open it"
echo " n|new <file> ???"
echo " c|contact <name> search brain for humans"
echo " pw <file> search brain for <file>.pw"
return 0
fi
local arg1="$1"
shift
if [[ "$arg1" =~ '^(e|edit)$' ]]; then
__brain_root_edit "$@"
elif [[ "$arg1" =~ '^(g|grep)$' ]]; then
greplang "$@"
elif [[ "$arg1" =~ '^(pw)$' ]]; then
__brain_pw_edit "$@"
elif [[ "$arg1" =~ '^(session)$' ]]; then
__brain_session "$@"
elif [[ "$arg1" =~ '^(n|new)$' ]]; then
echo "hippocampus: long term memory failure" # XXX: implement? no need right now
elif [[ "$arg1" =~ '^(c|contact)$' ]]; then
__brain_human "$@"
fi
}
# completion!
__brain_echo_line () {
echo "ctx=$context state=$state statedescr=$state_descr line=$line"
}
_brain_2nd () {
if [[ "$line" =~ '.*(pw).*' ]]; then
_values 'pw files' $(ls $__brain_pw_roots/*.pw\
|sed -e "s,$__brain_pw_roots/,,g"\
|sed -e "s,\(.*\).pw,\1,g")
elif [[ "$line" =~ '.*(session).*' ]]; then
_values 'sessions' $(ls $__brain_session_dir/*.vim\
|sed -e "s,$__brain_session_dir/,,g"\
|sed -e "s,\(.*\).vim,\1,g")
elif [[ "$line" =~ '.*(contact).*' ]]; then
_values 'contact humans' $(__brain_human_files)
else
#__brain_echo_line
fi
}
_brain () {
local context state state_descr line
typeset -A opt_args
_arguments ":operation:(edit grep pw session contact)" ":subject:_brain_2nd"
}