Debian Codegraph @

plugins/xdgdefault

raw
1 #!/usr/bin/env sh
2
3 # Description: Sets the xdg-open's default application for the current entry's file
4 # type. ${XDG_DATA_DIRS} and ${XDG_DATA_HOME} are set to the recommended
5 # defaults if unset, as specified in XDG Base Directory Specification
6 # - http://specifications.freedesktop.org/basedir-spec/.
7 #
8 # Dependencies: xdg-utils, fzf or dmenu (GUI)
9 #
10 # Shell: POSIX compliant
11 # Author: lwnctd
12
13 # set to 1 to enable GUI apps
14 GUI="${GUI:-0}"
15
16 if [ "$GUI" -ne 0 ] && command -v dmenu > /dev/null 2>& 1; then
17 menu="dmenu -i -l 7"
18 elif command -v fzf > /dev/null 2>& 1; then
19 menu="fzf -e --tiebreak=begin"
20 fi
21
22 if [ -z "$1" ] || [ -z "$menu" ] > /dev/null 2>& 1; then
23 exit 1
24 fi
25
26 ftype=$(xdg-mime query filetype "$2/$1")
27
28 if [ -z "$ftype" ]; then
29 exit 1
30 fi
31
32 dirs=${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
33 dirs=${dirs}:${XDG_DATA_HOME:-$HOME/.local/share}:
34
35 while [ -n "$dirs" ]; do
36 d=${dirs%%:*}
37 if [ -n "$d" ] && [ -d "$d"/applications ]; then
38 set -- "$@" "$d"/applications
39 fi
40 dirs=${dirs#*:}
41 done
42
43 # see PR #2102
44 # shellcheck disable=SC2038
45 app=$(find "$@" -iname '*.desktop' -exec grep -l "MimeType=.*$ftype" {} + 2>/dev/null \
46 | xargs -I {} grep '^Name=' {} /dev/null \
47 | sort -u -t ':' -k 1,1 \
48 | sed -e 's;..*/\(..*desktop\):Name=\(..*\);\2:\1;' \
49 | sort -t ':' -k 1,1 \
50 | column -t -s ':' -o "$(printf '\t')" \
51 | $menu \
52 | cut -f 2)
53
54 if [ -n "$app" ]; then
55 xdg-mime default "${app%%[[:blank:]]*}" "$ftype"
56 fi