aboutsummaryrefslogtreecommitdiff
path: root/dwim.pl
blob: 6d2c445140d61972a97b2e0c31a42d9b5a30ce5f (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
66
67
68
69
70
71
#!/usr/bin/perl

# dwim (do what i mean) is my own plan9-like plumber

use v5.24;
use warnings;
use strict;
use Path::ExpandTilde;

die "usage: $0 phrase\n" if scalar @ARGV > 1;
my $p;
$p = $ARGV[0] if defined $ARGV[0];
$p = `xsel -o` if not defined $ARGV[0];

my $OPENER = env (OPENER => "u");
my $EDITOR = env (EDITOR => "vi");
my $MAILER = env (MAILER => "mutt");
my $MAILROOT = env (MAILROOT => "/home/john/mail/");

for ($p) {
	# web address
	if (/^(https?:\/\/.+)$/) {
		exec "firefox", "$1"
	}

	# e-mail address
	if (/^(mailto:\/\/.+)$/ or /^(.+@.+\.\w+)$/) {
		exec $MAILER, "$1"
	}

	# file:line
	if (/^(.+):(\d+)(:.*?)?$/) {
		my $f = path($1);
		exec $OPENER, $EDITOR, "-c", ":$2", "$f"
	}

	# file:query (if file exists)
	if (/^(.+):(.+)$/) {
		my $f = path($1);
		exec $OPENER, $EDITOR, "-c", "/$2", "$f" if -e $f;
		# otherwise fall through
	}

	# maildir (if it matches) or file (if it exists)
	if (/^([^\s]+)$/) {
		my $f = path($1);
		exec $OPENER, $MAILER, "-f", "$f" if $f =~ /^$MAILROOT/; # maildir
		exec $OPENER, $EDITOR, "$f" if -e $f; #file
		# otherwise fall through
	}

	# otherwise
	die "no handler matched by: $p\n"
}

sub path {
	my $f = shift;
	$f = expand_tilde($f);
	return $f if $f =~ /^\// or $f =~ /^~/;
	my $t = `xtitle`;
	chomp $t;
	die "couldn't retrieve directory\n" if ! -d $t and ! -d ($t = dirname $t);
	return "$t/$f";
}

sub env {
	my %h = @_;
	my $k = (keys %h)[0];
	return $ENV{$k} if defined ${ENV}{$k};
	return $h{$k};
}