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

How to search from inside a function

Expand Messages
  • Asis Hallab
    Merry Christmas to all! Hopefully anyone of you can help me with my latest problem: I want to write a function that extends the behaviour of t.T.f and F, so
    Message 1 of 3 , Dec 25, 2011
      Merry Christmas to all!

      Hopefully anyone of you can help me with my latest problem:

      I want to write a function that extends the behaviour of t.T.f and F,
      so that the character-search does not stop at the end of the current line.

      I just could not get it to work with the built in function search(),
      because in order to emulate t<char> and T<char>,
      I need to call /char/e-1, which I cant do using search().

      So far I tried:

      function! ExtendT(searchChar)
           " search does not accept the 'e-1':
           " search('/'.a:searchChar.'/e-1')
           " can't be passed as "flag" to search:
           " search(a:searchChar, "/e-1")
           " eval does not work:
           " eval '/'.a:searchChar.'/e-1'
           " exe doesn't work either:
           " execute '/'.a:searchChar.'/e-1'
      endfunction

      Anyway, I am unsure, if getting the above function to work and mapping it to e.g. t,
      this will work as a movement-command in Vim?
      So will I be able to do ct#
      on the following (cursor-position is indicated by *):

      This is l*ine one
      and I want to change until here #.

      Help will be much appreciated.
      Santa Claus says, he provides the one presenting a solution
      to this problem with a chocolate Vim-Statuette.
      ;-)

      Have a nice Christmas and a Happy new year!
      Kind regards!
      Asis

      --
      You received this message from the "vim_use" 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
    • Tony Mechelynck
      ... will bring you to the next x You could then use (untested) ... if you wanted to go to the preceding character, wrapping to the previous line if at the
      Message 2 of 3 , Dec 25, 2011
        On 25/12/11 15:49, Asis Hallab wrote:
        > Merry Christmas to all!
        >
        > Hopefully anyone of you can help me with my latest problem:
        >
        > I want to write a function that extends the behaviour of t.T.f and F,
        > so that the character-search does not stop at the end of the current line.
        >
        > I just could not get it to work with the built in function search(),
        > because in order to emulate t<char> and T<char>,
        > I need to call /char/e-1, which I cant do using search().
        >
        > So far I tried:
        >
        > function! ExtendT(searchChar)
        > " search does not accept the 'e-1':
        > " search('/'.a:searchChar.'/e-1')
        > " can't be passed as "flag" to search:
        > " search(a:searchChar, "/e-1")
        > " eval does not work:
        > " eval '/'.a:searchChar.'/e-1'
        > " exe doesn't work either:
        > " execute '/'.a:searchChar.'/e-1'
        > endfunction
        >
        > Anyway, I am unsure, if getting the above function to work and mapping
        > it to e.g. t,
        > this will work as a movement-command in Vim?
        > So will I be able to do ct#
        > on the following (cursor-position is indicated by *):
        >
        > This is l*ine one
        > and I want to change until here #.
        >
        > Help will be much appreciated.
        > Santa Claus says, he provides the one presenting a solution
        > to this problem with a chocolate Vim-Statuette.
        > ;-)
        >
        > Have a nice Christmas and a Happy new year!
        > Kind regards!
        > Asis

        :call search('x')

        will bring you to the next x

        You could then use (untested)

        :if col('.') == 1
        : if line('.') != 1
        : norm! k$
        : endif
        :else
        : norm! h
        :endif

        if you wanted to go to the preceding character, wrapping to the previous
        line if at the start of a line.

        To map it to just ct# -- now, you can get that # as v:char in an :abbr
        <expr> expression, but any of the following cannot be done while
        evaluating the expression, only by "typing" the _result_ of the expression:

        - changing the text
        - switching to a different buffer
        - the :normal command
        and
        - moving the cursor
        is allowed, but the cursor will be returned once the expression has been
        evaluated

        so it is not as simple.

        Maybe (untested)

        function ToChar(char, fwd, exclusive)
        call search(a:char, a:fwd ? "e" : "b")
        if a:exclusive
        if a:fwd
        if col('.') == col('$')
        if line('.') != line('$')
        norm! j0
        endif
        else
        norm! l
        endif
        else
        if col('.') <= 1
        if line('.') > 1
        norm! k$
        endif
        else
        norm! h
        endif
        endif
        endif
        endfunc
        function GetUnmodChar()
        let char = getchar()
        while getcharmod()
        let char = getchar()
        endwhile
        return char
        endfunc

        omap t :call ToChar(GetUnmodChar(),1,1)<CR>
        omap T :call ToChar(GetUnmodChar(),0,1)<CR>
        omap f :call ToChar(GetUnmodChar(),1,0)<CR>
        omap F :call ToChar(GetUnmodChar(),0,0)<CR>


        Best regards,
        Tony.
        --
        "I don't care who does the electing as long as I get to do the
        nominating"
        -- Boss Tweed

        --
        You received this message from the "vim_use" 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
      • Christian Brabandt
        Hi Asis! ... onoremap t / .nr2char(getchar()). /e-1 n Mit freundlichen Grüßen Christian -- Sicherheit ist das Irrlicht, dem Wünschende
        Message 3 of 3 , Dec 27, 2011
          Hi Asis!

          On So, 25 Dez 2011, Asis Hallab wrote:

          > Merry Christmas to all!
          >
          > Hopefully anyone of you can help me with my latest problem:
          >
          > I want to write a function that extends the behaviour of t.T.f and F,
          > so that the character-search does not stop at the end of the current line.
          >
          > I just could not get it to work with the built in function search(),
          > because in order to emulate t<char> and T<char>,
          > I need to call /char/e-1, which I cant do using search().
          >
          > So far I tried:
          >
          > function! ExtendT(searchChar)
          > " search does not accept the 'e-1':
          > " search('/'.a:searchChar.'/e-1')
          > " can't be passed as "flag" to search:
          > " search(a:searchChar, "/e-1")
          > " eval does not work:
          > " eval '/'.a:searchChar.'/e-1'
          > " exe doesn't work either:
          > " execute '/'.a:searchChar.'/e-1'
          > endfunction
          >
          > Anyway, I am unsure, if getting the above function to work and mapping it
          > to e.g. t,
          > this will work as a movement-command in Vim?
          > So will I be able to do ct#
          > on the following (cursor-position is indicated by *):
          >
          > This is l*ine one
          > and I want to change until here #.

          onoremap <silent> <expr> t '/'.nr2char(getchar())."/e-1\n"


          Mit freundlichen Grüßen
          Christian
          --
          Sicherheit ist das Irrlicht, dem Wünschende nachjagen.
          -- Walter Heiby

          --
          You received this message from the "vim_use" 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
        Your message has been successfully submitted and would be delivered to recipients shortly.