Vim frontend.



Hi all.

I've created a frontend for vim.  Since I spend most of the time I'm
awake within vim it seamed like a good idea. :)

Some info.

It uses the perl-interface so you need a vim with built in perl-support.

It sends cluepackages when you are in insert mode and hit <space>,
<return> or when you hit <esc> to go out of insert mode

It sends the following cluepackages to dashboard
word_at_point with the relevance 10
sentence_at_point with the relevance 8 (currently same as line_at_point)
line_at_point with relevance 6
textblock with relevance 4
identifier with relevance 10

textblock is 10 lines before and 10 lines after current line.
identifier is there to make sure it uses the manpage backend 

Please try and let me know what you think.  Also read the info inside
the file if your vim crashes (which it will do in the default rh vim :)
)

Regards,
Erik
" Dashboard frontend for vim.
"
" Author Erik Bågfors <erik bagfors nu>
"
" Just dump this file and Dashboard.pm into ~/.vim/plugin and everything
" should work, if you have perl-support in your vim.
"
" You cannot use this in the default redhat vim.  That's because of a bug in
" that version of vim.  To see if you have the bug type ":perl $curbuf->Name()"
" if that kills your vim you have the bug. Fix your vim. 
"
" It sends cluepackages when you are in insert mode and hit <space>, 
" <return> or when you hit <esc> to go out of insert mode
"
" It sends the following cluepackages to dashboard
" word_at_point with the relevance 10
" sentence_at_point with the relevance 8 (currently same as line_at_point)
" line_at_point with relevance 6
" textblock with relevance 4
" identifier with relevance 10
"
" textblock is 10 lines before and 10 lines after current line.
" identifier is there to make sure it uses the manpage backend 
"

if has('perl')

fun! SendDash()
	perl <<EOF
        # For some reason I cannot move the "use blaha" code
	# Above the function, this means that this runs very time which is 
	# unnecessary
	#
	use lib ("$ENV{HOME}/.vim/plugin");
	use Dashboard;
	use Encode;

	sub to_utf8 { $convert?encode("utf8", $_[0]):$_[0]; }

	unless(defined $dash) { # set these global variables
	    $frontend = "vim";
	    $focused = 1; # Vim never does anything unless it's in focus..
	    $dash = new Dashboard($frontend);
	    my ($success, $enc) = VIM::Eval('&encoding');
	    $convert = 1 unless ($enc eq "utf-8");
	} 
	my $context = $curbuf->Name();
	my ($row, $col) = $curwin->Cursor();
	if ($col == 0) {
		$col=9999;
		$row--;
	}

	my $from = $row - 10 > 0?$row - 10:1; 
	my $to = $row + 10;
	my @lines = $curbuf->Get($from .. $to);
	my $spaceposs = 0;
	my $i = $row - $from;
	my $line_at_point = $lines[$row - $from];
	my $sentence_at_point = "";
        
	my $c = 0; 
	for(my $l = $row - $from + 1; $l--; $l >= 0) {
	    $sentence_at_point = $lines[$l] . "\n" . $sentence_at_point;
	    $c = $c?length($lines[$l])+$c:$col - 3;
	    # find start of a sentence, remove everything before that
	    last if ($sentence_at_point =~ s/^.{0,$c}[\.!?][\s\n]//s);
	    last if ($sentence_at_point =~ s/^.*\n\n//s);
	    $c = "*";
	}
	$sentence_at_point .= join ("\n",@lines[$row - $from + 1 .. $#lines]);
	# We found the start of the sentence, let's find the end
	$sentence_at_point =~ s/(^.*)[\.!?][\s\n].*$/$1/s;
	$sentence_at_point =~ s/(\s)\n/$1/sg;
	$sentence_at_point =~ s/\n/ /sg;
	$sentence_at_point =~ s/^\s*//sg;

	my $textblock = join "\n", @lines;
	$textblock = &to_utf8($textblock);
	$sentence_at_point = &to_utf8($sentence_at_point);

	my ($inp, $outp);
	$inp = rindex $line_at_point, " ", $col-2;
	$outp = index $line_at_point, " ", $col-2;

	my $word_at_point;
	if ($outp == -1) {
		$word_at_point = &to_utf8(substr $line_at_point, $inp+1);
	} else {
		$word_at_point = &to_utf8(substr $line_at_point, $inp+1, $outp - $inp);
	}
	$line_at_point = &to_utf8($line_at_point);

	#my $sentence_at_point = $line_at_point;

	my $clue = [
	   {'type' => 'word_at_point', 'relevance' => 10, 'data' => $word_at_point}, 
	   {'type' => 'sentence_at_point', 'relevance' => 8, 'data' => $sentence_at_point}, 
	   {'type' => 'line_at_point', 'relevance' => 6, 'data' => $line_at_point},
	   {'type' => 'textblock', 'relevance' => 4, 'data' => $textblock},
	   {'type' => 'identifier', 'relevance' => 10, 'data' => $word_at_point}
	];
	$dash->send_cluepacket($context, $focused, $clue);
EOF
	return ""
endfun

:inoremap <Esc> <c-r>= SendDash()<CR><Esc>
:inoremap <space> <space><c-r>=SendDash()<CR>
:inoremap <Return> <Return><c-r>=SendDash()<CR>

endif "has('perl')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]