Loading ...
Sorry, an error occurred while loading the content.

69380Re: capture() function to get output of command

Expand Messages
  • Christian Brabandt
    May 2 7:29 AM
    • 0 Attachment
      Hi Ingo!

      On Do, 02 Mai 2013, Ingo Karkat wrote:

      > On 02-May-2013 15:34 +0200, Christian Brabandt wrote:
      >
      > > Hi mattn!
      > >
      > > On Mi, 01 Mai 2013, mattn wrote:
      > >
      > >> I wrote a patch to add new function 'capture()'.
      > >> When you want to get a list of all commands/functions/mappings with using vim script, probably, you'll do like follow.
      > >>
      > >> ---------------------------------
      > >> let funcs = ''
      > >> redir => funcs
      > >> silent! function
      > >> redir END
      > >> ---------------------------------
      > >>
      > >> I guess, most of vim script programmers want to get the result as expression. not command, want to get more easily.
      > >> If vim have 'capture()' which allow to getting those command line 'function', I guess this is very useful.
      > >>
      > >> For example, getting :Command list as array.
      > >> ---------------------------------
      > >> :echo map(split(capture("command"), "\n")[1:], 'split(v:val[4:])[0]')
      > >> ---------------------------------
      > >>
      > >> One more thing. vim doesn't support nest of :redir.
      > >> ---------------------------------
      > >> function! s:foo()
      > >> let a = ''
      > >> redir => a
      > >> silent echo "foo"
      > >> redir END
      > >> return a
      > >> endfunction
      > >>
      > >> function! s:bar()
      > >> let a = ''
      > >> redir => a
      > >> call s:foo()
      > >> redir END
      > >> return a
      > >> endfunction
      > >>
      > >> echo s:bar()
      > >> ---------------------------------
      > >>
      > >> This 'echo s:bar()' doesn't show anything. But capture() can do it.
      > >
      > > Perhaps we should first fix redir before we introduce another similar
      > > function?
      >
      > That would work, too. On the other hand, here's an idea from a
      > practitioner that might justify a separate function: Almost always, the
      > captured text is split() into a list of lines (mattn's example does
      > this, too). Why not have capture() return a list of lines in the first
      > place?! We obviously cannot change the original :redir interface, but a
      > new function could offer this and spare all plugins from doing that.

      The patch throws out Warnings:

      eval.c: In function 'f_capture':
      eval.c:9305:16: warning: assignment from incompatible pointer type [enabled by default]
      gcc -c -I. -Iproto -DHAVE_CONFIG_H -I/usr/local/include -g -DDEBUG -Wall -Wshadow -Wmissing-prototypes -Wmaybe-uninitialized -o objects/main.o main.c
      In file included from vim.h:1965:0,
      from main.c:11:
      globals.h:1098:18: warning: 'capture_ga' initialized and declared 'extern' [enabled by default]


      This patch seems to fix those warnings:
      iff --git a/src/eval.c b/src/eval.c
      --- a/src/eval.c
      +++ b/src/eval.c
      @@ -9302,7 +9302,7 @@
      if (check_secure())
      return;

      - capture_ga = alloc(sizeof(garray_T));
      + capture_ga = (garray_T *) alloc(sizeof(garray_T));
      ga_init2(capture_ga, (int)sizeof(char), 80);

      ++msg_silent;
      diff --git a/src/globals.h b/src/globals.h
      --- a/src/globals.h
      +++ b/src/globals.h
      @@ -1095,7 +1095,11 @@
      EXTERN int redir_reg INIT(= 0); /* message redirection register */
      EXTERN int redir_vname INIT(= 0); /* message redirection variable */
      #endif
      -extern garray_T *capture_ga INIT(= NULL); /* capture() buffer */
      +EXTERN garray_T *capture_ga /* capture() buffer */
      +# ifdef DO_INIT
      + = NULL
      +# endif
      + ;

      I haven't played yet much with the capture() function, so I am not sure,
      this works recursively.

      It looks like a useful addition, but I am not sure, whether a new
      function is the right way to go, instead of fixing redir.

      Mit freundlichen Grüßen
      Christian
      --
      Auf der Verpackung steht: Win95, NT oder besser, folglich ist Linux
      eine unterstützte Plattform.

      --
      --
      You received this message from the "vim_dev" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_dev" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Show all 26 messages in this topic