-
Notifications
You must be signed in to change notification settings - Fork 3
/
markedit.zsh
177 lines (150 loc) · 3.58 KB
/
markedit.zsh
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/zsh
#----------------------#
# MAKE EDIT CACHE FILE #
#----------------------#{{{
_me_cache_="$HOME/.cache/markedit"
# }}}
#--------------#
# HELP DIALOG #
#--------------#{{{
_markedit_help_(){
\cat <<- HELP
[MARKEDIT] Mark your file To easy Access
OPTS :
ea Add Mark File
er Remove Mark File
es Show All Mark File's
em Edit Mark File
ee change or Edit Exist Mark
EXAMPLE :
ea bashrc ~/.bashrc ( add 'bashrc' Mark with out sudo Permission )
ea _ bashrc ~/.bashrc ( add 'bashrc' Mark with sudo Permission )
em bashrc ( Edit 'bashrc' Mark )
sudo em bashrc ( Edit 'bashrc' Mark with sudo )
er bashrc ... ( Remove 'bashrc' Mark and support multi Delete )
ee bashrc (Edit Exist mark like change the mark name or File )
HELP
}
#}}}
#--------------#
# ADD NEW MARK #
#--------------#{{{
ea(){
local mark file root
[ "$#" -lt "2" -o "$#" -gt "3" ] && { _markedit_help_ && return 0 ; }
if [ "$1" = "_" ]; then
root="$1" mark="$2" file="$3:P"
else
mark="$1" file="$2:P"
fi
# check if the mark alredy exist
check=$(\awk -F ";" '/^'"$mark"' /' $_me_cache_ 2> /dev/null)
if [ -n "$check" ]; then
echo -e "[X] $mark: mark already exist"
else
test -f "$file" || { echo -e "[X] $file: No such File" && return 0 ;}
if [ -n "$root" ]; then
echo -e "[+] $mark: Added"
echo "$mark ; $file ; root" >> $_me_cache_
else
echo -e "[+] $mark: Added"
echo "$mark ; $file " >> $_me_cache_
fi
fi
}
# }}}
#-------------#
# DELETE MARK #
#-------------#{{{
er(){
[ -z "$1" ] && { _markedit_help_ && return 0 ; }
local i check
for i in $@; do
check=$(\awk -F ";" '/^'"$i"' /' $_me_cache_ 2> /dev/null)
if [ -z "$check" ]; then
echo -e "[X] $i: none exist mark"
else
sed -i "/^$i /d" $_me_cache_
echo -e "[-] $i: Deleted "
fi
done
}
# }}}
#--------------#
# JUMB TO MARK #
#--------------#{{{
em(){
[ -z "$1" -o "$#" -gt 1 ] && { _markedit_help_ && return 0 ; }
local mark
read -A mark < <(\awk -F ";" '/^'"$1"' /{print $1" "$2" "$3}' $_me_cache_ 2> /dev/null)
EDITOR=${EDITOR:-vim}
if [ -z "${mark[1]}" ]; then
echo -e "[X] $1: none exist mark"
else
if [ -n "${mark[3]}" ]; then
if [ "$UID" -ne 0 ]; then
echo -e "[X] $1: Permission denied"
exit 1
else
eval "$EDITOR ${mark[2]}"
fi
else
eval "$EDITOR ${mark[2]}"
fi
fi
}
# }}}
#------------#
# SHOW MARKS #
#------------#{{{
es(){
if [ -z "$1" ]; then
column -t -s ';' "$_me_cache_"
else
for i in "$@"; do
local check=$(\awk -F ";" '/^'"$i"' /{print $0}' $_me_cache_ 2> /dev/null)
if [ -z "$check" ]; then
echo -e "[X] $i: None exist Mark"
else
echo -e "$check" | column -t -s ';'
fi
done
fi
}
# }}}
#-------------#
# EDIT A MARK #
#-------------#{{{
ee(){
[ -z "$1" -o "$#" -gt 1 ] && { _markedit_help_ && return 0 ; }
local check=$(\awk -F ";" '/^'"$1"' /' $_me_cache_ 2> /dev/null)
if [ -z "$check" ]; then
echo -e "[X] $1: None exist Mark"
else
# delete the mark
sed -i "/^$1 /d" $_me_cache_
# edit the mark with zle
vared check
# rewrite the mark
echo $check >> "$_me_cache_"
fi
}
# }}}
#---------------------------#
# AUTO COMPLITION FOR ZSH #
#---------------------------#{{{
function _complete_zsh {
if [[ "$(wc -l < $_me_cache_)" -gt 0 ]];then
reply=( $(\awk -F ';' '{print $1}' $_me_cache_) )
fi
}
# }}}
#----------------------#
# EXEC THE COMPLETION #
#----------------------#{{{
compctl -K _complete_zsh em
compctl -K _complete_zsh er
compctl -K _complete_zsh ee
compctl -K _complete_zsh es
#}}}
# vim: ft=sh