blib/lib/Vim/X.pm

filter: Statementstmt 56.4% 57 / 101 Branchbran 14.2% 2 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 51.4% 18 / 35 Podpod 94.1% 16 / 17 totaltotal 52.5% 93 / 177 CC 11 SCAR 13.3
CC
11
Cov
49%
source_function_dir
12.0
vim_range
12.0
load_function_dir
6.0
CRAP 3.8
« blib/lib/Vim/X/Buffer.pm ↑ Summary blib/lib/Vim/X/Window.pm »
1 package Vim::X;
2 BEGIN {
3 3 ▶ $Vim::X::AUTHORITY = 'cpan:YANICK';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 # ABSTRACT: Candy for Perl programming in Vim
6 3 $Vim::X::VERSION = '1.0.1';
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Sub::Attribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our @EXPORT = qw/
16 vim_func vim_prefix vim_msg vim_buffer vim_cursor vim_window
17 vim_command
18 vim_call
19 vim_lines
20 vim_append
21 vim_eval
22 vim_range
23 vim_line
24 vim_delete /;
25  
26 3 ▶use Vim::X::Window;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Vim::X::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use Vim::X::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub import {
31 2 ▶ __PACKAGE__->export_to_level(1, @_);
Subroutine import: called CC=1 SCAR=0.0
32 2 my $target_class = caller;
33  
34  
35 2 ▶ eval <<"END";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
36 package $target_class;
37 use Sub::Attribute;
38 sub Vim :ATTR_SUB { goto &Vim::X::Vim; }
39 END
40  
41 }
42  
43 sub Vim :ATTR_SUB {
44 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 2 ▶ my( $class, $sym_ref, undef, undef, $attr_data ) = @_;
Subroutine Vim: called CC=2 SCAR=6.9
Pod documented
47  
48 1 my $name = *{$sym_ref}{NAME};
49  
50 1 ▶ my $args = $attr_data =~ 'args' ? '...' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$attr_data =~ /args/ ? : 1 0 1
51  
52 1 my $range = 'range' x ( $attr_data =~ /range/ );
53  
54 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 VIM::DoCommand(<<END);
56 function $name($args) $range
57 perl ${class}::$name( split "\\n", scalar VIM::Eval('a:000'))
58 endfunction
59 END
60  
61 1 return;
62 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64  
65 sub load_function_dir {
66 0 ▶ my $dir = shift;
Subroutine load_function_dir: not called CC=2 SCAR=17.9
Pod documented
67  
68 0 my @files = <$dir/*.pl>;
69  
70 0 for my $f ( @files ) {
71 0 my $name = _func_name($f);
72 0 vim_command(
73 "au FuncUndefined $name perl Vim::X::load_function_file('$f')"
74 );
75 }
76 }
77  
78  
79 sub source_function_dir {
80 0 ▶ my $dir = shift;
Subroutine source_function_dir: not called CC=3 SCAR=24.8
Pod documented
81  
82 0 my @files = ( <$dir/*.pl>, <$dir/*.pvim> );
83  
84 0 for my $f ( @files ) {
85 0 my $name = _func_name($f);
86 0 ▶ vim_command( "source $f" ) if $f =~ /\.pvim$/;
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /\.pvim$/ 0 0 0
87 0 vim_command(
88 "au FuncUndefined $name perl Vim::X::load_function_file('$f')"
89 );
90 }
91 }
92  
93 sub _func_name {
94 0 ▶ my $name = shift;
Subroutine _func_name: not called CC=1 SCAR=6.9
95 0 $name =~ s#^.*/##;
96 0 $name =~ s#\.p(?:l|vim)$##;
97 0 return $name;
98 }
99  
100  
101 sub load_function_file {
102 0 ▶ my $file = shift;
Subroutine load_function_file: not called CC=2 SCAR=17.9
Pod documented
103  
104 0 my $name = _func_name($file);
105  
106 0 eval "{ package Vim::X::Function::$name;\n"
107 . "no warnings;\n"
108 . Path::Tiny::path($file)->slurp
109 . "\n}"
110 ;
111  
112 0 ▶ vim_msg( "ERROR: $@" ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
113  
114 0 return '';
115  
116 }
117  
118 3 ▶unless ( $main::curbuf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($main::curbuf) 3 0 3
119 package
120 VIM;
121 3 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
123 # warn "calling $AUTOLOAD";
124 }
125 }
126  
127  
128 sub vim_msg {
129 0 ▶ VIM::Msg( join " ", @_ );
Subroutine vim_msg: not called CC=1 SCAR=6.9
Pod documented
130 }
131  
132 sub vim_prefix {
133 0 ▶ my( $prefix ) = @_;
Subroutine vim_prefix: not called CC=1 SCAR=6.9
Pod undocumented
134  
135 0 $Vim::X::PREFIX = $prefix;
136 }
137  
138  
139 sub vim_buffer {
140 0 ▶ my $buf = shift // $::curbuf->Number;
Subroutine vim_buffer: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
shift() // $main::curbuf->Number
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $main::curbuf->Number
shift() $main::curbuf->Number
Truth table0%0/3
A // B
A= shift()B= $main::curbuf->Number
ABresult
1X1
011
000
141  
142 0 return Vim::X::Buffer->new( index => $buf, _buffer => $::curbuf );
143 }
144  
145  
146 sub vim_lines {
147 0 ▶ vim_buffer->lines(@_);
Subroutine vim_lines: not called CC=1 SCAR=6.9
Pod documented
148 }
149  
150  
151 sub vim_line {
152 0 ▶ @_ ? vim_buffer->line(shift) : vim_cursor();
Subroutine vim_line: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
153 }
154  
155  
156 sub vim_append {
157 0 ▶ vim_cursor()->append(@_);
Subroutine vim_append: not called CC=1 SCAR=6.9
Pod documented
158 }
159  
160  
161 sub vim_eval {
162 0 ▶ return map { scalar VIM::Eval($_) } @_;
Subroutine vim_eval: not called CC=1 SCAR=6.9
Pod documented
163 }
164  
165  
166 sub vim_range {
167 0 ▶ my @range = map { 0 + $_ } @_ == 2 ? @_
Branch: 2 branches
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
@_ == 1 ? : 0 0 0
168 : @_ == 1 ? ( @_ ) x 2
169 0 ▶ : map { vim_eval($_) } qw/ a:firstline a:lastline /;
Subroutine vim_range: not called CC=3 SCAR=24.8
Pod documented
170  
171 0 return vim_buffer->range( @range );
172 }
173  
174  
175 sub vim_command {
176 0 ▶ return map { VIM::DoCommand($_) } @_;
Subroutine vim_command: not called CC=1 SCAR=6.9
Pod documented
177 }
178  
179  
180 sub vim_call {
181 0 ▶ my( $func, @args ) = @_;
Subroutine vim_call: not called CC=1 SCAR=6.9
Pod documented
182 0 my $cmd = join ' ', 'call', $func . '(', map( { "'$_'" } @args ), ')';
183 0 vim_command( $cmd );
184 }
185  
186  
187 sub vim_window {
188 0 ▶ return Vim::X::Window->new( _window => shift || $::curwin);
Subroutine vim_window: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
shift() || $main::curwin
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $main::curwin
shift() $main::curwin
Truth table0%0/3
A || B
A= shift()B= $main::curwin
ABresult
1X1
011
000
189 }
190  
191  
192 sub vim_cursor {
193 0 ▶ my $w = vim_window();
Subroutine vim_cursor: not called CC=1 SCAR=6.9
Pod documented
194 0 return $w->cursor;
195 }
196  
197  
198 sub vim_delete {
199 0 ▶ vim_buffer->delete(@_);
Subroutine vim_delete: not called CC=1 SCAR=6.9
Pod documented
200 }
201  
202 3 1;
203  
204 __END__
205  
206 =pod
207  
208 =encoding UTF-8
209  
210 =head1 NAME
211  
212 Vim::X - Candy for Perl programming in Vim
213  
214 =head1 VERSION
215  
216 version 1.0.1
217  
218 =head1 SYNOPSIS
219  
220 package Vim::X::Plugin::MostUsedVariable;
221  
222 use strict;
223 use warnings;
224  
225 use Vim::X;
226  
227 sub MostUsedVariable :Vim {
228 my %var;
229  
230 for my $line ( vim_lines ) {
231 $var{$1}++ while $line =~ /[$@%](\w+)/g;
232 }
233  
234 my ( $most_used ) = reverse sort { $var{$a} <=> $var{$b} } keys %var;
235  
236 vim_msg "variable name $most_used used $var{$most_used} times";
237 }
238  
239 and then in your C<.vimrc>:
240  
241 perl push @INC, '/path/to/plugin/lib';
242 perl use Vim::X::Plugin::MostUsedVariable;
243  
244 map <leader>m :call MostUsedVariable()
245  
246 =head1 DESCRIPTION
247  
248 I<Vim::X> provides two tools to make writing Perl functions for Vim a little
249 easier: it auto-exports functions tagged by the attribute C<:Vim> in
250 Vim-space, and it defines a slew of helper functions and objects that are a
251 little more I<Do What I Mean> than the I<VIM> API module that comes with Vim
252 itself.
253  
254 Obviously, for this module to work, Vim has to be compiled with Perl interpreter
255 support.
256  
257 =head2 Import Perl function in Vim-space
258  
259 Function labeled with the C<:Vim> attribute are automatically exported to Vim.
260  
261 The C<:Vim> attribute accepts two optional parameters: C<args> and C<range>.
262  
263 =head3 :Vim(args)
264  
265 If C<args> is present, the function will be exported expecting arguments, that
266 will be passed to the function via the usual C<@_> way.
267  
268 sub Howdie :Vim(args) {
269 vim_msg( "Hi there, ", $_[0] );
270 }
271  
272 # and then in vim:
273 call Howdie("buddy")
274  
275 =head3 :Vim(range)
276  
277 If C<range> is present, the function will be called only once when invoked
278 over a range, instead than once per line (which is the default behavior).
279  
280 sub ReverseLines :Vim(range) {
281 my @lines = reverse map { "$_" } vim_range();
282 for my $line ( vim_range ) {
283 $line <<= pop @lines;
284 }
285 }
286  
287 # and then in vim:
288 :5,15 call ReverseLines()
289  
290 =head3 Loading libraries
291  
292 If your collection of functions is growing,
293 C<load_function_dir()> can help with their management. See that function below
294 for more details.
295  
296 =head1 FUNCTIONS
297  
298 =head2 load_function_dir( $library_dir)
299  
300 Looks into the given I<$library_dir> and imports the functions in all
301 files with the extension C<.pl> (non-recursively).
302 Each file must have the name of its main
303 function to be imported to Vim-space.
304  
305 To have good start-up time and to avoid loading all dependencies for
306 all functions, the different files aren't sourced at start-up, but are
307 rather using the C<autocmd> function of Vim to trigger the loading
308 of those files only if used.
309  
310 E.g.,
311  
312 # in ~/.vim/vimx/perlweekly/PWGetInfo.pl
313 use Vim::X;
314  
315 use LWP::UserAgent;
316 use Web::Query;
317 use Escape::Houdini;
318  
319 sub PWGetInfo :Vim() {
320 ...;
321 }
322  
323 # in .vimrc
324 perl use Vim::X;
325  
326 autocmd BufNewFile,BufRead **/perlweekly/src/*.mkd
327 \ perl Vim::X::load_function_dir('~/.vim/vimx/perlweekly')
328 autocmd BufNewFile,BufRead **/perlweekly/src/*.mkd
329 \ map <leader>pw :call PWGetInfo()<CR>
330  
331 =head2 source_function_dir( $library_dir )
332  
333 Like C<load_function_dir>, but if it finds files with the exension C<.pvim>,
334 it'll also source them as C<vimL> files at
335 load-time, allowing to define both the Perl bindings and the vim macros in the
336 same file. Note that, magically, the Perl code will still only be compiled if the function
337 is invoked.
338  
339 For that special type of magic to happen, the C<.pvim> files must follow a certain pattern to
340 be able to live their double-life as Perl scripts and vim file:
341  
342 ""; <<'finish';
343  
344 " your vim code goes here
345  
346 finish
347  
348 # the Perl code goes here
349  
350 When sourced as a vim script, the first line is considered a comment and
351 ignored, and the rest is read until it hits C<finish>, which cause Vim to
352 stop reading the file. When read as a Perl file, the first line contains a
353 heredoc that makes all the Vim code into an unused string, so basically ignore
354 it in a fancy way.
355  
356 For example, the snippet for C<load_function_dir> could be rewritten as such:
357  
358 # in ~/.vim/vimx/perlweekly/PWGetInfo.pvim
359 ""; <<'finish';
360  
361 map <leader>pw :call PWGetInfo()<CR>
362  
363 finish
364  
365 use Vim::X;
366  
367 use LWP::UserAgent;
368 use Web::Query;
369 use Escape::Houdini;
370  
371 sub PWGetInfo :Vim() {
372 ...;
373 }
374  
375 # in .vimrc
376 perl use Vim::X;
377  
378 autocmd BufNewFile,BufRead **/perlweekly/src/*.mkd
379 \ perl Vim::X::source_function_dir('~/.vim/vimx/perlweekly')
380  
381 =head2 load_function_file( $file_path )
382  
383 Loads the code within I<$file_path> under the namespace
384 I<Vim::X::Function::$name>, where name is the basename of the I<$file_path>,
385 minus the C<.pl>/C<.pvim> extension. Not that useful by itself, but used by
386 C<load_function_dir>.
387  
388 =head2 vim_msg( @text )
389  
390 Display the strings of I<@text> concatenated as a vim message.
391  
392 vim_msg "Hello from Perl";
393  
394 =head2 vim_buffer( $i )
395  
396 Returns the L<Vim::X::Buffer> object associated with the I<$i>th buffer. If
397 I<$i> is not given or set to '0', it returns the current buffer.
398  
399 =head2 vim_lines( @indexes )
400  
401 Returns the L<Vim::X::Line> objects for the lines in I<@indexes> of the
402 current buffer. If no index is given, returns all the lines of the buffer.
403  
404 =head2 vim_line($index)
405  
406 Returns the L<Vim::X::Line> object for line I<$index> of the current buffer.
407 If I<$index> is not given, returns the line at the cursor.
408  
409 =head2 vim_append(@lines)
410  
411 Appends the given lines after the line under the cursor.
412  
413 If carriage returns are present in the lines, they will be split in
414 consequence.
415  
416 =head2 vim_eval(@expressions)
417  
418 Evals the given C<@expressions> and returns their results.
419  
420 =head2 vim_range($from, $to)
421  
422 =head2 vim_range($line)
423  
424 =head2 vim_range()
425  
426 Returns a L<Vim::X::Range> object for the given lines, or single line,
427 in the current buffer. The lines can be passed as indexes, or L<Vim::X::Line>
428 objects.
429  
430 If no line whatsoever is passed, the range will be the one on
431 which the command has been called (i.e.: C<:afirstline> and C<a:lastline>).
432  
433 =head2 vim_command( @commands )
434  
435 Run the given 'ex' commands and return their results.
436  
437 vim_command 'normal 10G', 'normal iHi there!';
438  
439 =head2 vim_call( $function, @args )
440  
441 Calls the vim-space function I<$function> with the
442 provided arguments.
443  
444 vim_call( 'SetVersion', '1.23' )
445  
446 # equivalent of doing
447 # :call SetVersion( '1.23' )
448 # in vim
449  
450 =head2 vim_window( $i )
451  
452 Returns the L<Vim::X::Window> associated with the I<$i>th window. If I<$i>
453 is not provided or is zero, returns the object for the current window.
454  
455 =head2 vim_cursor
456  
457 Returns the L<Vim::X::Line> associated with the position of the cursor
458 in the current window.
459  
460 =head2 vim_delete( @lines )
461  
462 Deletes the given lines from the current buffer.
463  
464 =head1 SEE ALSO
465  
466 The original blog entry: L<http://techblog.babyl.ca/entry/vim-x>
467  
468 =head3 CONTRIBUTORS
469  
470 Hernan Lopes
471  
472 =head1 AUTHOR
473  
474 Yanick Champoux <yanick@cpan.org>
475  
476 =head1 COPYRIGHT AND LICENSE
477  
478 This software is copyright (c) 2014 by Yanick Champoux.
479  
480 This is free software; you can redistribute it and/or modify it under
481 the same terms as the Perl 5 programming language system itself.
482  
483 =cut
« blib/lib/Vim/X/Buffer.pm ↑ Summary blib/lib/Vim/X/Window.pm »