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

Problem with vim script function

Expand Messages
  • Tim Johnson
    Hello Vimmers: I have written a vim script function that I intend to use to write debugging script into C code. The function code is as follows: function!
    Message 1 of 2 , Aug 3 3:17 PM
      Hello Vimmers:
      I have written a vim script function that
      I intend to use to write debugging script into C code.
      The function code is as follows:
      function! Debug()
      let wrd=expand("<cWORD>")
      exe "norm! bdwaprintf(\"" . wrd . " = %\\n\"," . wrd . "); /* DEBUG!! */"
      exe "norm! bbbbbbbbbi"
      endfunction

      The intent of the code is that when I place the
      cursor in insert mode ahead of a word as in
      var
      ^
      I will "wrap" 'var in debugging code, seeing
      printf("var = %\n",var); /* DEBUG!! */
      in my editor.

      The third line of my function executes as I would like it
      to, but I also want to move the cursor back to the '%' to
      put in the correct format character.

      Alas, the cursor doesn't move, but stays at athe
      position following '/* DEBUG!! */'
      What is the correct approach to move the cursor
      back to the per cent (%) sign?

      Thanks
      tim
      (using vim 6.1 on RH 7.2)
      --
      Tim Johnson <tim@...>
      http://www.alaska-internet-solutions.com
      http://www.johnsons-web.com
    • Luc Hermitte
      Hello, ... May be: function! s:Debug() ... return f%a endfunction inoremap µ = Debug() ... This is a quite different approach,
      Message 2 of 2 , Aug 3 3:38 PM
        Hello,

        * On Sun, Aug 03, 2003 at 02:17:10PM -0800, Tim Johnson <tim@...> wrote:
        > I have written a vim script function that
        > I intend to use to write debugging script into C code.
        > [...]

        > var
        > ^
        > I will "wrap" 'var in debugging code, seeing
        > printf("var = %\n",var); /* DEBUG!! */
        > in my editor.
        >
        > The third line of my function executes as I would like it
        > to, but I also want to move the cursor back to the '%' to
        > put in the correct format character.

        May be:

        function! s:Debug()
        :s@\S\+\%#@printf("\0 = %\\n",\0); /* DEBUG!! */@
        return "\<esc>f%a"
        endfunction

        inoremap <buffer> µ <c-r>=<sid>Debug()<cr>


        :h i_CTRL-R
        :h f


        This is a quite different approach, but it solves your problem.

        HTH,
        --
        Luc Hermitte
        http://hermitte.free.fr/vim/
      Your message has been successfully submitted and would be delivered to recipients shortly.