Ismo Puustinen mentioned:
>
> I recently wrote an implementation of the optimal line breaking
> algorithm (in Ruby). If you are interested, the file is located
> here, with a small piece of test code at the end:
> http://lissujaismo.net/rbpar/rbpar_engine.rb
>
> You can run the test code by saying "ruby rbpar_engine.rb".
>
> The library is actually part of a (yet unreleased) email text
> processing framework that I've been writing for Vim and other
> plain text editors. I think it shouldn't be too difficult to use
> Vim Ruby bindings to run the algorithm for a range of text
> lines, but I haven't looked at that yet. One possible problem
> could be maintaining the cursor position.
Attached below are a few ways of using the Perl module
Text::Autoformat to format text in Vim (warning: the email
transport mechanism is likely to wrap some long lines). I
suspect these techniques can be adapted for a ruby module.
Please provide me a precise reference to "the optimal line
breaking algorithm" you mention.
Thanks,
--Suresh
"Some mappings related to use of Text::Autoformat:
"
" Although there are several maps below, I almost
" exclusively use maps related to the '=' command.
"
" Note how textwidth and filetype (mail) options are
" used at the time of invocation of the command!
"Using external (rather than embedded) perl:
"------------------------------------------
"Via the ={motion} command:
nnoremap = :execute 'setlocal equalprg=perl\
\-MText::Autoformat\ -e\
\\"{autoformat{all=>1'.
\',\ right=>'
\.(&tw?&tw:72).
\',\ mail=>'.
\(&ft=='mail'?1:0).
\'};}\"'
\<cr>=
"Shortcut for formatting the range .,$:
nnoremap <F3> :execute '.,$!perl -MText::Autoformat -e
\ "{autoformat{all=>1, right=>' .
\ (&tw ? &tw : 72) .
\ ', mail=>'.
\ (&ft=='mail'?1:0).
\ '};}"'<cr>
"Using embedded perl:
"-------------------
if(has('perl'))
command! -range=% Format :
\ perl {
\ use Text::Autoformat;
\ my $n_line1 = VIM::Eval('<line1>');
\ my $n_line2 = VIM::Eval('<line2>');
\ my $foo= join "\n",
$curbuf->Get($n_line1 .. $n_line2);
\ my $tw=VIM::Eval('&textwidth');
\ $tw or $tw = 72;
\ my $ft=VIM::Eval('&filetype');
\ $ft = ($ft eq 'mail') ? 1 : 0;
\ my $formatted = autoformat($foo,
{right=>$tw, all=>1, mail=>$ft});
\ my @formatted_lines = split '\n',
$formatted;
\ $curbuf->Delete($n_line1, $n_line2);
\ $curbuf->Append($n_line1-1,
@formatted_lines, '');
\ }
"Shortcut for formatting the paragraph around the cursor:
"nnoremap =p {}:'`,Format<cr>:silent!w<cr>
nnoremap =p {}:'`,Format<cr>
"Format the stuff over the _previous_ motion:
"nnoremap <F4> :'`,Format<cr>y<esc>:silent!w<cr>
nnoremap <F4> :'`,Format<cr>y<esc>
"Format the visual selection:
"vnoremap <F4> :Format<cr>:silent!w<cr>
vnoremap <F4> :Format<cr>
endif
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---