forked from MX-Linux/mxfb-accessories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmxfb-edit
executable file
·162 lines (135 loc) · 3.69 KB
/
mxfb-edit
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
#!/bin/bash
# mx-edit : helper script to open user's "mime-default" text-editor
# fallback to featherpad, geany, nano, mcedit, vim, sensible-editor
# open terminal if needed
#
# author: fehlix at mxlinux dot org
# version:
VERSION="2021-08-04.01"
# need bash
if [ -z "$BASH_VERSION" ]; then
exec /bin/bash "$0" "$@"
fi
ME=$(readlink -e $0)
ME=${ME##*/}
usage() { cat<<USAGE
$ME - helper script to open users "mime-default" text-editor
Usage: $ME [options] [ text-file(s) ... ]
Options:
-e EDITOR set text-edit EDITOR to use
-d debug - print debug messages
-h help - show this help
-r edit as root
-s simulate - print commandline
-v version - show version
-V verbose - show what is done
-- pass options followed to text editor
USAGE
}
OPTSTRING=":e:dhrsvV"
while getopts $OPTSTRING arg; do
case $arg in
e) # set editor to use
USE_EDITOR="$OPTARG"
;;
d) # debug on
DEBUG=1
echo "DEBUG=on"
;;
r) # edit as root
EDIT_AS_ROOT=1
;;
s) # simulate on
SIMULATE=1
echo "SIMULATE=on"
;;
h) # help.
usage
exit
;;
V) # verbose on
VERBOSE=1
echo "VERBOSE=on"
;;
v) # version
echo "version $VERSION"
exit 0
;;
\?) # invalid option
echo "invalid option: -$OPTARG"
exit 1
;;
:) # missing argument
echo "option -$OPTARG : missing argument"
exit 1
;;
esac
done
((DEBUG)) && echo shift $((OPTIND -1))
shift $((OPTIND -1))
((DEBUG)) && echo '$@: '"$@"
FALLBACK=(
featherpad
geany
mousepad
nano
mcedit
vim
sensible-editor
)
[ -n "$USE_EDITOR" ] && E="$USE_EDITOR" || E=""
# avoid self-calling loop
[ -n "$E" ] && {
read -r c d <<<"$E"
if [[ ${c##*/} == $ME ]]; then
echo "ignored editor: $ME"
unset E
elif ! which "$c" >/dev/null; then
echo "ignored not found editor: '$c'"
unset E
fi
}
# check mime-default text editor
r=""
#((EUID)) || r="runuser -u $(logname)"
M=$($r xdg-mime query default text/plain)
[ -z "$E" ] && [ -n "$M" ] && {
((VERBOSE)) && echo "MIME_DEFAULT_TEXT_EDITOR:$M"
# set defaults
: ${XDG_DATA_DIRS:=/usr/local/share:/usr/share}
#XDD="$HOME/.local/share:$XDG_DATA_DIRS"
XDD="$(eval printf ~$(/usr/bin/logname))/.local/share:$XDG_DATA_DIRS"
readarray -t xdd <<<"${XDD//://applications$'\n'}/applications"
((DEBUG)) && declare -p xdd
# get first found with nullgob
read -r e _<<<$(shopt -s nullglob; echo ${xdd[@]/%//${M%.desktop}[.]desktop}; shopt -u nullglob)
grep -sq '^Terminal=true' "$e" && terminal="x-terminal-emulator -e" || terminal=
# get and tidy up first found Exec line
[ -n "$e" ] && E=$(grep -m1 -oP '^Exec[[:space:]]*=[[:space:]]*\K.*' "$e" |
sed -e 's:@::g; s:[[:space:]]%[a-zA-Z]::; s:"::g' -e "s:'::g")
# check found editor is available
read -r c d <<<"$E"
[ -n "$E" ] && which "$c" >/dev/null || unset E
}
# fallback
[ -z "$E" ] && \
for e in ${FALLBACK[@]}; do
E=$(which "$e" 2>/dev/null) && break
done
read -r c d <<<"$E"
[ -n "$E" ] && which "$c" >/dev/null || exit
case ${c##*/} in
nano|pico|vi|vim*|mcedit|sensible-editor)
terminal="x-terminal-emulator -e"
;;
*) : terminal="" ;;
esac
# have a terminal
tty >/dev/null && terminal=""
run="$terminal"
((EDIT_AS_ROOT)) && run="mx-pkexec $terminal"
##launch editor
((VERBOSE )) && echo $run "$E" "${@@Q}"
((SIMULATE)) && echo $run $E "$@" && exit
$run $E "$@"
exit