Translate to Vim 7 script

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Translate to Vim 7 script

Suresh Govindachar`
Hello,

I made the following "single-line" command, :Ls, whose
output is the same as that of :ls but sorted by buffer name.
The main work is done in perl.  I gather Vim 7 script has
features that facilitate achieveing such functionality.  

With a veiw to learning Vim 7 script features, I would like
to see this example translated to Vim 7 script.  If there
is a simple way to do it in Vim 6.x script without using
any "array-" or other plugins, I would like to see that too.

command!  Ls     redir @" | silent ls | redir END | echo "\n" |
              \  perl {
              \          my $foo=VIM::Eval('@"');
              \          my %list=();
              \          my $boo;
              \          while($foo =~ m/(.*?line\s+\d+)/g)
              \          {
              \             $boo = $1;
              \             $boo =~ m/"([^"]+)"/;
              \             $list{$1} = $boo;
              \          }
              \          VIM::Msg("\n");
              \          if ($^O =~ /mswin/i)
              \          {
              \             for $boo (sort { lc($a) cmp lc($b) } keys %list)
              \             {
              \                VIM::Msg("$list{$boo}\n");
              \             }
              \          }
              \          else
              \          {
              \             for $boo (sort keys %list)
              \             {
              \                VIM::Msg("$list{$boo}\n");
              \             }
              \          }
              \       }  
              \  <cr>

Enhancement 1:  one could let :Ls take an argument;  when the
                argument is the character '!', one could make it
                display the output of :ls! sorted by buffer name

Enhancement 2:  although the output of :ls does not clobber the
                message-history, the output of :Ls does do so.
                The enhancement would be to prevent this from
                happening.

Thanks,

--Suresh

Reply | Threaded
Open this post in threaded view
|

Re: Translate to Vim 7 script

Suresh Govindachar`
In regard to the message I sent a little while ago
on Sun, 21 Aug 2005 16:30:58 +0000 and appended below,
the Enhancement 1 was easy, very easy -- just change
the first line to the following:
 
     command!  -bang Ls     redir @" | silent ls<bang> | redir END | echo "\n" |

--Suresh

Hello,

I made the following "single-line" command, :Ls, whose
output is the same as that of :ls but sorted by buffer name.
The main work is done in perl.  I gather Vim 7 script has
features that facilitate achieveing such functionality.  

With a veiw to learning Vim 7 script features, I would like
to see this example translated to Vim 7 script.  If there
is a simple way to do it in Vim 6.x script without using
any "array-" or other plugins, I would like to see that too.

command!  Ls     redir @" | silent ls | redir END | echo "\n" |
              \  perl {
              \          my $foo=VIM::Eval('@"');
              \          my %list=();
              \          my $boo;
              \          while($foo =~ m/(.*?line\s+\d+)/g)
              \          {
              \             $boo = $1;
              \             $boo =~ m/"([^"]+)"/;
              \             $list{$1} = $boo;
              \          }
              \          VIM::Msg("\n");
              \          if ($^O =~ /mswin/i)
              \          {
              \             for $boo (sort { lc($a) cmp lc($b) } keys %list)
              \             {
              \                VIM::Msg("$list{$boo}\n");
              \             }
              \          }
              \          else
              \          {
              \             for $boo (sort keys %list)
              \             {
              \                VIM::Msg("$list{$boo}\n");
              \             }
              \          }
              \       }  
              \  <cr>

Enhancement 1:  one could let :Ls take an argument;  when the
                argument is the character '!', one could make it
                display the output of :ls! sorted by buffer name

Enhancement 2:  although the output of :ls does not clobber the
                message-history, the output of :Ls does do so.
                The enhancement would be to prevent this from
                happening.

Thanks,

--Suresh

Reply | Threaded
Open this post in threaded view
|

Re: Translate to Vim 7 script

Mikołaj Machowski
In reply to this post by Suresh Govindachar`
Dnia niedziela 21 sierpie? 2005 18:30, Suresh Govindachar napisa?:
>
>
function! Ls()
        redir @a | silent ls | redir END

        " Now we have buffers in array
        " Everything put in g: vars to track values
        let g:array = split(@a, '\n')

        " We have to initialize new datatypes
        let g:hash = {}

        " OK, split entry in two parts: preamble, and rest beginning with
        " " starting file name - put this two parts in hash. In that way they will
        " be connected after sorting. Number of buffer and flags are
        " 8 chars wide
        for i in g:array
                let g:hash[strpart(i, 9)] = strpart(i, 1, 8)
        endfor

        " Now create one string:
        let g:string = "\n"         " <-- beginning \n may be necessary for output
        for i in sort(keys(g:hash))
                let g:string .= g:hash[i] . i . "\n"
        endfor

        " And echo it:
        echo g:string

endfunction

HTH

m.