File Coverage

blib/lib/TextMate/JumpTo.pm
Criterion Covered Total %
statement 28 32 87.5
branch 7 14 50.0
condition n/a
subroutine 8 9 88.8
pod 2 2 100.0
total 45 57 78.9


line stmt bran cond sub pod time code
1             package TextMate::JumpTo;
2              
3 3     3   292984 use warnings;
  3         9  
  3         137  
4 3     3   19 use strict;
  3         6  
  3         110  
5 3     3   1100 use HTML::Tiny;
  3         3518  
  3         310  
6 3     3   23 use File::Spec;
  3         5  
  3         238  
7 3     3   18 use Carp;
  3         6  
  3         248  
8              
9 3     3   17 use base qw(Exporter);
  3         6  
  3         3180  
10              
11             our @EXPORT_OK = qw(jumpto tm_location);
12              
13             =head1 NAME
14              
15             TextMate::JumpTo - Tell TextMate to jump to a particular file, line
16              
17             =head1 VERSION
18              
19             This document describes TextMate::JumpTo version 0.07
20              
21             =cut
22              
23             our $VERSION = '0.07';
24              
25             =head1 SYNOPSIS
26              
27             use TextMate::JumpTo qw(jumpto tm_location);
28              
29             jumpto( file => 'mysrc.pl', line => 123 );
30              
31             my $textmate_link = tm_location( file => 'foo.t', line => 12 );
32              
33             =head1 DESCRIPTION
34              
35             On Mac OS The TextMate editor handles urls of the form
36              
37             txmt://open?url=file://somefile.pl&line=100
38              
39             which cause it to jump to the file, line and column specified by the
40             arguments. This module is a simple wrapper which uses the Mac OS 'open'
41             command to send TextMate to the specified location.
42              
43             I use it in my F<~/.perldb> to have TextMate track the current debugger
44             position. Here's what it looks like:
45              
46             $ cat ~/.perldb
47             use TextMate::JumpTo qw(jumpto);
48             use File::Spec;
49              
50             sub afterinit {
51             $trace |= 4; # Enable watchfunction
52              
53             # Needed to work out where filenames are relative to
54             chomp( $base_dir = `pwd` );
55              
56             $option{animate} = 0;
57             push @options, 'animate';
58             }
59              
60             sub watchfunction {
61             my ( $package, $file, $line ) = @_;
62             return unless $DB::single || $option{animate};
63             local $trace = 0;
64             if ( $file =~ /^\(eval\s+\d+\)\[(.+?):(\d+)\]/ ) {
65             $file = $1;
66             $line += $2 - 1;
67             }
68             $file = File::Spec->rel2abs( $file, $base_dir );
69             jumpto( file => $file, line => $line, bg => 1 );
70             return 1;
71             }
72              
73             =head1 INTERFACE
74              
75             =head2 C<< jumpto >>
76              
77             Instruct TextMate to jump to the specified file, line and column. The
78             arguments are a list of key, value pairs:
79              
80             jumpto( file => 'splendid.pl', line => 12, column => 3 );
81              
82             Possible arguments are:
83              
84             =over
85              
86             =item C
87              
88             The path to the file to go to.
89              
90             =item C
91              
92             The (one based) line number to go to.
93              
94             =item C
95              
96             The (one based) column to go to.
97              
98             =item C
99              
100             True to leave TextMate in the background. By default a call to C
101             will bring TextMate to the foreground.
102              
103             =back
104              
105             =cut
106              
107             sub jumpto {
108 5 100   5 1 4904 croak "Odd number of args, needs a list of key => value pairs"
109             if @_ % 2;
110 4         13 my %args = @_;
111 4         17 my $bg = delete $args{bg};
112 4         15 _open( tm_location( %args ), $bg );
113             }
114              
115             # Open a URL on Mac OS.
116             sub _open {
117 0     0   0 my ( $url, $bg ) = @_;
118 0 0       0 croak "TextMate only runs on Mac OS"
119             unless $^O =~ /darwin/;
120 0 0       0 my @cmd = ( '/usr/bin/open', ( $bg ? ( '-g' ) : () ), $url );
121 0 0       0 system @cmd and croak "Can't open $url ($?)";
122             }
123              
124             =head2 C
125              
126             Get a URL using the C scheme that jumps to the specified
127             location. Arguments as for C with the exeception of the C
128             switch which makes no sense in this context.
129              
130             my $loc = tm_location( file => 'humbile.pm', line => 42 );
131              
132             =cut
133              
134             sub tm_location {
135 8 100   8 1 3314 croak "Odd number of args, needs a list of key => value pairs"
136             if @_ % 2;
137 7         19 my %args = @_;
138 7 100       465 croak "You must supply one or more of file, line, column"
139             unless grep defined $args{$_}, qw(file line column);
140 5 50       30 if ( my $file = delete $args{file} ) {
141 5         136 $args{url} = "file://" . File::Spec->rel2abs( $file );
142             }
143 5         26 return 'txmt://open?' . HTML::Tiny->new->query_encode( \%args );
144             }
145              
146             1;
147             __END__