aboutsummaryrefslogtreecommitdiff
path: root/dwim
blob: 1ede16de00938eb8dd183f73902d0480894622af (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
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
163
164
165
166
167
168
169
#!/usr/bin/perl

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

use v5.24;
use warnings;
use strict;
use subs qw/arguments dir env fail handle path run/;

my ($DEBUG, $handler, $phrase);

while ($_ = shift @ARGV) {
	if (/^-d$/) {
		$DEBUG = 1;
	} elsif (/^-/) {
		die "usage: $0 [-d] [phrase]\n";
	} else {
		$phrase = $_;
	}
}

$phrase = `xsel -o` if not defined $phrase;

my @OPENER		= ('xtopen');
my @EDITOR		= (@OPENER, 'vi');
my @PDF_VIEWER		= ('xpdf');
my @MAN_VIEWER		= (@OPENER, 'man');
my @MAILER		= (@OPENER, 'mutt');
my @BROWSER		= (@OPENER, 'w3m -title');
my @FILE_BROWSER	= ('rox');
my @MAILDIR_VIEWER	= (@OPENER, 'mutt -f');
my $MAILROOT		= env MAILROOT => '/home/john/mail/';

for ($phrase) {
	if (/^(https?:\/?\/?\S+)$/) {
		handle 'web address';
		run @BROWSER, $1
	}

	if (/^(mailto:\S+)$/ or /^(\S+@.+\.\w+)$/) {
		handle 'e-mail address';
		run @MAILER, $1
	}

	if (/^(.+?):(\d+).*?$/) {
		handle 'file:line (like grep -n)';
		my $p = path $1;
		run @EDITOR, "+$2", $p
	}

	if (/^(.+) line (\d+)\.?$/) {
		handle 'FILE line LINE (like perl)';
		my $p = path $1;
		run @EDITOR, "+$2", $p
	}

	if (/^(.+):(.+)$/) {
		handle 'file:query (like grep)';
		my $p = path $1;
		run @EDITOR, "+/$2", $p if -e $p;
		fail 'file not found' if $DEBUG;
		# otherwise fall through
	}

	if (/^([-_A-Za-z]+)\((\d+)\)[):,.]*$/) {
		handle 'manual(section)';
		run @MAN_VIEWER, $2, $1;
	}

	if (/^([-_A-Za-z]+)\.([1-9])$/) {
		handle 'manual.section';
		my $p = path "$1.$2";
		run @MAN_VIEWER, $p if -e $p;
		run @MAN_VIEWER, $2, $1;
	}

	if (/^([A-Za-z_][A-Za-z0-9_]*)\(/) {
		handle 'function(call';
		my $dir = dir;
		s/\(.*//;
		for (`grep -n '^$_' "$dir"/*.c "$dir"/*.h`) {
			run @EDITOR, "+$2", $1 if /([^:]+):(\d+):/;
		}
	}

	if (/^<(\S+)>$/) {
		handle '<header.h>';
		open my $h, 'find / -maxdepth 3 -type d -name include 2>&-|'
			or fail 'could not search include directories';
		while (<$h>) {
			chomp;
			if (-e "$_/$1") {
				close $h;
				run @EDITOR, "$_/$1";
			}
		}
		close $h;
		fail 'header file not found';
	}

	if (/^(\S+)$/) {
		handle 'maildir / directory / file';
		my $p = path $1;
		run @MAILDIR_VIEWER, $p if $p =~ /^$MAILROOT/; # maildir
		run @FILE_BROWSER, $p if -d $p; # directory
		run @PDF_VIEWER, $p if -e $p and $p =~ /\.pdf$/; # pdf
		run @EDITOR, $p if -e $p; # file
		fail 'file not found' if $DEBUG;
		# otherwise fall through
	}

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

sub dir {
	for (`xtitle`) {
		chomp;
		s/.*\(([^(]+)\)$/$1/;
		s,^~([^/]+),/home/$1,;
		s,^~,/home/$ENV{USER},;
		die "couldn't retrieve current directory\n"
			if ! -d $_ and ! -d ($_ = dirname($_));
		return $_;
	}
	die "couldn't retrieve current directory: xtitle not installed\n";
}

sub path {
	my $n = shift;
	$n =~ s,^~([^/]+),/home/$1,;
	$n =~ s,^~,/home/$ENV{USER},;
	return $n if $n =~ /^\// or $n =~ /^~/;
	return dir . "/$n";
}

# take K => V and return environment variable K if defined, otherwise V
sub env {
	my %h = @_;
	my $k = (keys %h)[0];
	return $ENV{$k} if defined ${ENV}{$k};
	return $h{$k};
}

sub handle {
	$handler = shift;
	print STDERR "$handler MATCHED\n" if $DEBUG;
}

sub fail {
	my $msg = shift;
	print STDERR "$handler FAILED: $msg\n";
}

sub run {
	if ($DEBUG) {
		my @argv = @_;
		s/(\s)/\\$1/g for @argv; # escape whitespace
		print STDERR "@argv\n";
	}
	system "@_ &";
	exit;
}

sub dirname {
	my $path = shift;
	$path =~ s,/[^/]+,,;
	return $path;
}