sub tr_perl_getcolor {
my( $char ) = @_;
$char =~ tr/xrgybpcwXRGYBPCW/0123456701234567/
or return 255;
return $char;
}
I don't think there's anything much faster you can do than a single array lookup and return, except either unrolling it from a subroutine so the code is inline where you need it, or making sure you use $_[ 0 ] rather than creating a variable with my.
I'd also consider ditching the whole unpack() algorithm and looking at just using regexps to do the colour replacement, is there a reason for not doing that?
]]>my $codes_regex = join '|', map quotemeta, keys %codes_hash;
so far this is all precomputation. Then boil the entire ansify function down to
(my $output = $input) =~ s/($codes_regex)/$codes_hash{$1}/g; return $output;
and let the regex trie-izer handle optimization for you. Unless there's more complexity to the codes than what you've shown, the job is done, and I bet it's lightning fast. :)
]]>Do you mean as in IO::Async? Perhaps you could suggest some more examples you'd like to see?
]]>
my @document = map { $curbuf->Get($_) } 0 .. $curbuf->Count; my ( $line_number, $column ) = $curwin->Cursor;
can become:
my ( $line_number, $column ) = $curwin->Cursor; $line = $curbuf->Get($line_number--) while $line_number >=0 && !/^\s* sub \s+ (\w+)/x;
.. so you can avoid ->Get-ting all 8k lines when you need "only" 200 ;)
]]>
my @document = map { $curbuf->Get($_) } 0 .. $curbuf->Count; my ( $line_number, $column ) = $curwin->Cursor;
could become:
my ( $line_number, $column ) = $curwin->Cursor; $line = $curbuf->Get($line_number--) while $line_number >=0 && !/^\s* sub \s+ (\w+)/x;
.. so you can avoid ->Get-ting all 8k lines when you need "only" 200 ;)
]]>Corrections after having used this a bit:
b:did_perl_statusline
makes the variable local to the buffer instead of g: which is global
my $line_number = ($curwin->Cursor)[0]; my $line = ''; $line = $curbuf->Get( $line_number ) while $line_number-- > 0 && $line !~ /^\s*sub\s+(\w+)\b/; my $sub_name = $1 || '(not in sub)'; VIM::DoCommand("let subName='$sub_name'");
I do believe this is faster than getting all lines for each cursor movement :)
]]>function! PerlCurrentSubName() let s:currline = line('.') let s:currcol = col('.') normal $ let [s:line, s:column] = searchpos('^\s*sub\s\+\zs\(\w\+\)','bcW') if (s:line != 0) let s:subname = expand('') call cursor(s:currline, s:currcol) return s:subname else return '(not in sub)' endif endfunction]]>
function! PerlCurrentSubName() let s:currline = line('.') let s:currcol = col('.') normal $ let [s:line, s:column] = searchpos('^\s*sub\s\+\zs\(\w\+\)','bcW') if (s:line != 0) let s:subname = expand('') else let s:subname = '(not in sub)' endif call cursor(s:currline, s:currcol) return s:subname endfunction
There may be a better way of doing this without "normal $", and maybe there's a way of getting the result of the subpattern in search() directly without having to jump to the location and expanding cword, but my vim-fu is weak...
]]>.vimrc syntax on setlocal laststatus=2 setlocal statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ \ Position:\ %p%%\ %l/%L,%c if has("autocmd") autocmd BufReadPost * if &syntax == 'perl' | source ~/.vim/perl_current_subroutine | endif endif
.vim/perl_current_subroutine if ! exists("b:did_perl_statusline") && &syntax == 'perl' setlocal statusline+=%(\ \ \ Subroutine:\ %{StatusLineIndexLine()}%) let b:did_perl_statusline = 1 endif if has('perl') perl Cursor)[0]; my $line = $curbuf->Get($line_number); my $indent = ''; if ($line !~ /^(\s*)sub\s+(\w+)\b/) { $line = $curbuf->Get($line_number) while ($line_number-- > 0 && $line !~ /^(\s*)sub\s+(\w+)\b/); ($indent, $sub_name) = ($1, $2); } else { $line_number--; ($indent, $sub_name) = ($1, $2); } #if found, (try to) find the end of the subroutine if ($sub_name ne 'N/A') { my $end = $curbuf->Count(); $line = $curbuf->Get($line_number); if ($line !~ /}\s*$/) { $line = $curbuf->Get($line_number) while ($line_number++ Cursor)[0] > $line_number); } VIM::DoCommand("let current_perl_subroutine_name='$sub_name'"); } EOP function! StatusLineIndexLine() perl current_perl_subroutine() return current_perl_subroutine_name endfunction endif]]>