aboutsummaryrefslogtreecommitdiff
path: root/xplumb
blob: 1d897964f2767a6440a7ba3a5ba0baa512481a37 (plain)
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
#!/bin/sh

# My interpretation of a plumber (plan9 style).
# It scans the selected text (xsel) and decides what to do with it among
# some possibilities (open file, open url, man page...). If none is matched,
# a choice is prompted.

# Requires: dmenu (or similar), mail-compose, open-file, terminal, websearch,
#           xedit, xsel

# TODO:
#	send mail to email address;
#	calendar if it is a date
#	choice: pipe to another program

browser=${BROWSER:-firefox}
menu=${MENU:-dmenu}
menuopts="-l 10"

text=$(xsel | sed 's/\n/ /g')
if [ "${#text}" -ge 11 ]; then
	shorttext=$(echo "$text" | cut -b 1-10)...
else
	shorttext="$text"
fi

choice() {
	chosen=$(printf "websearch\nedit" | \
	         $menu $menuopts -p "What to do with \"$shorttext\"?")

	if [ "$chosen" = "websearch" ]; then
		websearch "$text"
	elif [ "$chosen" = "edit" ]; then
		f=$(mktemp)
		xsel > "$f"
		xedit "$f"
	fi
}

trymail() {
	addr=$(echo "$1" | addressgrep)
	( [ -n "$addr" ] && mail-compose "$addr" ) || return 1
}

tryman() {
	number=$(echo "$1" | sed 's/[^1-8]//g')
	name=$(echo "$1" | sed 's/([1-8])//g' | sed 's/ //g')
	if [ "$(man -w "$number" "$name" | wc -l)" = 1 ]; then
		terminal -e man "$number" "$name"
		return 0
	else
		return 1
	fi
}

tryurl() {
	url=$(echo "$1" | urlgrep)
	( [ -n "$url" ] && "$browser" "$url" ) || return 1
}

#open-file -s spawn "$text" || \
tryurl    "$text" || \
trymail   "$text" || \
tryman    "$text" || \
choice

Generated with cgit - Back to sebastiano.tronto.net