aboutsummaryrefslogtreecommitdiffstats
path: root/dmenu/plumb
blob: 13cfca8780be3fa1eda852b8240df8ce0a5ec4a8 (plain) (blame)
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
#!/bin/bash

WEB_BROWSER=brave
EMAIL_CLIENT=claws-mail
if test -x /usr/bin/terminator; then
	TERMINAL=terminator
elif test -x /usr/bin/st; then
	TERMINAL=st
else
	TERMINAL=xterm
fi

sel="$(xclip -o)"
url="$(printf '%s' "$sel" | tr -d '\n')"
file="$(printf '%s' "$sel" | tr -d '\n')"

if test -z "$sel"; then
    exit 0
fi

cd -- "$(readlink "/proc/$(pstree -lpA "$(xprop -id "$(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')" | grep PID | head -n 1 | cut -d ' ' -f 3)" | awk -F--- '{print $NF}' | tail -n1 | tr -Cd '[0-9]')/cwd")"

functions=""

if test -d "$sel" || test -d "$(printf '%s\n' "$sel" | sed 's|^~/|'"${HOME}|")"; then
	functions="$functions caja terminal"
	caja () {
		exec c "$sel";
	}
	terminal () {
		cd -- "$sel"
		exec $TERMINAL;
	}
elif test -e "$sel"; then
	functions="$functions open"
	open () {
		exec xdg-open -- "$url";
	}
fi

if printf '%s\n' "$url" | grep '^\(https\?://\|www\.\)[^[:space:]./]\+\.[^[:space:]/]*\(/\S*\)\?$' > /dev/null; then
	if printf '%s\n' "$url" | grep '^\(https\?://\)\?\(www\.\)\?spreaker\.com/.*/.*/.*' > /dev/null; then
		functions="$functions spreaker-dl"
		spreaker-dl () {
			cd ~/next/new
			exec spreaker-dl "$url";
		}
	fi
	functions="$functions web-browser"
	web-browser () {
		exec $WEB_BROWSER -- "$url";
	}
fi

if printf '%s\n' "$url" | grep '^\(mailto:\)\?\("[^"]\+"\|[a-zA-Z0-9!#$%&'\''*+/=?^_`{|}~-]\+\(\.[a-zA-Z0-9!#$%&'\''*+/=?^_`{|}~-]\)*\)@[^[:space:]./]\+\.[^[:space:]/]*$' > /dev/null; then
	functions="$functions email-client"
	email-client () {
		exec $EMAIL_CLIENT -- "$url";
	}
fi

if test -r "$file" && file "$file" | grep '\(PNG\|JPEG\) image' > /dev/null; then
	functions="$functions xwallpaper"
	xwallpaper () {
		exec xwallpaper --zoom "$file";
	}
fi

functions="$functions wiktionary.en"
wiktionary.en () {
	exec $WEB_BROWSER "https://en.wiktionary.org/wiki/$sel";
}

functions="$functions wiktionary.sv"
wiktionary.sv () {
	exec $WEB_BROWSER "https://sv.wiktionary.org/wiki/$sel";
}

functions="$functions wikipedia.en"
wikipedia.en () {
	exec $WEB_BROWSER "https://en.wikipedia.org/wiki/$sel";
}

functions="$functions wikipedia.sv"
wikipedia.sv () {
	exec $WEB_BROWSER "https://sv.wikipedia.org/wiki/$sel";
}

functions="$functions duckduckgo"
duckduckgo () {
	exec $WEB_BROWSER "https://duckduckgo.com/?q=$sel";
}

function="$(printf '%s\n' $functions | dmenu -i -p "Plumb $sel to:" -l 50)"

if test -n "$function"; then
	"$function"
fi