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

Re: enhancing hlsearch

Expand Messages
  • A.J.Mechelynck
    ... Well, I won t give you an answer, because I m too liable to stumble while writing it, but here is how it can be done: - Make a permanent mapping for / and
    Message 1 of 6 , Dec 6, 2006
      Mohsin wrote:
      > Mechelynck,
      >
      > It has to be done with one mapping, the reason being *simultaneous*
      > display, since
      > sometimes the same word is with wrong case, should be immediately visible
      > without having to make a mental switch to find it.
      >
      > I will try using with the syntax/highlight mappings.
      > thanks,
      > mohsin.

      Well, I won't give you an answer, because I'm too liable to stumble while
      writing it, but here is how it can be done:

      - Make a permanent mapping for / and ?. Each of them should, as part of the
      {rhs}, issue a cmap for <CR> (see below), and map the start of the search
      command, until but not including the start of the search pattern.

      - The cmap should not only finalize the search command but also issue :match
      and :2match with the variable part represented by <C-R>/ (Ctrl-R slash), and
      finally it should also unmap itself (and the next mapping) so the next use of
      <Enter> (or <Esc>) in command mode should not invoke these special cmaps.

      - If you want completeness, the maps for / and ? should also create a
      temporary cmap for <Esc> to cancel the search, and also unmap itself and the
      cmap for <CR>.

      I think it is doable, but it's not my province really. Some other Vim
      old-timers are much more versed than I in the creation of that kind of
      "self-undoing" mappings.


      Best regards,
      Tony.
    • Nikolai Weibull
      ... It can probably be simplified by mapping in command mode and check if getcmdtype() is / or ? and, if so, call :match as appropriate. However,
      Message 2 of 6 , Dec 6, 2006
        On 12/6/06, A.J.Mechelynck <antoine.mechelynck@...> wrote:

        > I think it is doable, but it's not my province really. Some other Vim
        > old-timers are much more versed than I in the creation of that kind of
        > "self-undoing" mappings.

        It can probably be simplified by mapping <CR> in command mode and
        check if getcmdtype() is '/' or '?' and, if so, call :match as
        appropriate.

        However, there's no good way of doing this type of highlighting while
        in 'incsearch' mode (i.e., while typing the pattern).

        Anyway, here's something I cooked up for you 'incsearch' users that
        actually thought this was sort of a neat idea, i.e., being able to
        quickly toggle between the word anywhere in the buffer, the complete
        word, and the case insensitive word. It definitely needs work to deal
        with anything but trivial patterns, e.g., only wrap the word under the
        cursor, but you probably only want to do this for simple, word-type
        patterns.

        cmap <C-I> <C-\>e<SID>toggle_word_style()<CR>

        let s:word_types = [
        \ [ '^\\<\|\\>$', "", 'g', '\c', "" ],
        \ [ '^\\c', "", "", "", "" ],
        \ [ '^', "", "", '\<', '\>' ]
        \ ]

        function! s:toggle_word_style()
        let line = getcmdline()
        let type = getcmdtype()

        if type != '/' && type != '?'
        return line
        endif

        for word_type in s:word_types
        if line =~ word_type[0]
        let line = word_type[3] .
        \ substitute(line, word_type[0], word_type[1], word_type[2]) .
        \ word_type[4]
        call setcmdpos(len(line) + 1)
        return line
        endif
        endfor

        return line
        endfunction

        Actually sort of neat, although it's not what Mohsin wants.

        nikolai
      • Nikolai Weibull
        ... Hm, you of course need to know how you got /into/ search mode, so that you only do this at the appropriate times. Setting a variable and checking for it
        Message 3 of 6 , Dec 6, 2006
          On 12/7/06, Nikolai Weibull <now@...> wrote:
          > On 12/6/06, A.J.Mechelynck <antoine.mechelynck@...> wrote:
          >
          > > I think it is doable, but it's not my province really. Some other Vim
          > > old-timers are much more versed than I in the creation of that kind of
          > > "self-undoing" mappings.
          >
          > It can probably be simplified by mapping <CR> in command mode and
          > check if getcmdtype() is '/' or '?' and, if so, call :match as
          > appropriate.

          Hm, you of course need to know how you got /into/ search mode, so that
          you only do this at the appropriate times. Setting a variable and
          checking for it would work, but you have to make sure to unset it if
          the command line is abandoned as well. We need
          CmdLineEnter/CmdLineLeave and v:cmdexecuted or some such, so that we
          can tell if the line was executed or not. Or perhaps even better:
          CmdLineEnter/CmdLineLeave/CmdLineExecutePre/CmdLineExecutePost. With
          the *Pre autocmd one can perhaps change the command line in some way
          before executing it. With *Post one can do stuff after it, like
          :matching. *Leave would be called when it is abandoned, although
          perhaps the name suggests other cases as well?

          Anyway, good night.

          nikolai
        Your message has been successfully submitted and would be delivered to recipients shortly.