Interactive scripting

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

Interactive scripting

Todd Strader
Does anyone know how to move to and highlight a word/line from a
script like ":%s/old_pattern/new_pattern/gc" would do?  I've tried
"call cursor(lineNo, 1)" to at least get me to the line.  But this
doesn't seem to move the buffer to that line until the script
finishes.  I'm looping through a range of lines and testing each one.
If it matches a regexp, then I want to move the buffer there,
highlight the line, and prompt the user.  Can someone help me figure
this out?  Thanks.

 Todd
Reply | Threaded
Open this post in threaded view
|

Re: Interactive scripting

Tim Chase-2
> Does anyone know how to move to and highlight a word/line from
> a script like ":%s/old_pattern/new_pattern/gc" would do?  I've
> tried "call cursor(lineNo, 1)" to at least get me to the line.
> But this doesn't seem to move the buffer to that line until
> the script finishes.

Sounds like you're describing the search() function

        :help search()

"When a match has been found its line number is returned, and the
cursor will be positioned at the match.  If there is no match a 0
is returned and the cursor doesn't move."

If you want to use your previous search, you can exploit the
search register, calling this function as

        search(@/)

HTH,

-tim





Reply | Threaded
Open this post in threaded view
|

Re: Interactive scripting

Todd Strader
Tim,

Thanks for the response.  search() doesn't seem to be exactly what I
want to do.  I want to search over a set range of lines, and search
just finds the next occurrence.  Regardless, search() still suffers
from the same problems I was having before.  That is, when the cursor
is moved and I'm still running a script, the buffer (or window, or
whatever you want to call it) doesn't move to that position.  So, if I
move the cursor to 50 different locations, I only see the last one
after the script exits.  Here's my function as it is now, perhaps that
will help explain what is going on here.

:function Outsig_func(...) range
  let lineNo = a:firstline

  while lineNo <= a:lastline
    let line = getline(lineNo)
    if line =~ "LMARX_H0RX"
      call cursor(lineNo, 1)
      call input("found one on line " . lineNo)
    endif
    let lineNo = lineNo + 1
  endwhile
:endfunction

Try this out.  Make the regexp something that will match in a file and
see what happens.  I can't seem to get vim to display where the cursor
is at while I'm still in the script.  Thanks again for the comments.

Todd

On 9/21/05, Tim Chase <[hidden email]> wrote:

> > Does anyone know how to move to and highlight a word/line from
> > a script like ":%s/old_pattern/new_pattern/gc" would do?  I've
> > tried "call cursor(lineNo, 1)" to at least get me to the line.
> > But this doesn't seem to move the buffer to that line until
> > the script finishes.
>
> Sounds like you're describing the search() function
>
>         :help search()
>
> "When a match has been found its line number is returned, and the
> cursor will be positioned at the match.  If there is no match a 0
> is returned and the cursor doesn't move."
>
> If you want to use your previous search, you can exploit the
> search register, calling this function as
>
>         search(@/)
>
> HTH,
>
> -tim
>
>
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Interactive scripting

Antony Scriven
On Sep 22, Todd Strader wrote:

 > [...] That is, when the cursor is moved and I'm still
 > running a script, the buffer (or window, or whatever you
 > want to call it) doesn't move to that position.  So, if I
 > move the cursor to 50 different locations, I only see the
 > last one after the script exits.  Here's my function as
 > it is now, perhaps that will help explain what is going
 > on here.

When vim is processing the `call input ...' then the cursor
is in the command line, not in the editing window. So you
need to highlight the text manually, perhaps, something like
this.

   hi link Outsig_foundline cursor

 > :function Outsig_func(...) range
 >   let lineNo = a:firstline
 >
 >   while lineNo <= a:lastline
 >     let line = getline(lineNo)
 >     if line =~ "LMARX_H0RX"
 >       call cursor(lineNo, 1)

         match Outsig_foundline /\%#.*/
         redraw
         match none

 >       call input("found one on line " . lineNo)
 >     endif
 >     let lineNo = lineNo + 1
 >   endwhile
 > :endfunction

Antony
Reply | Threaded
Open this post in threaded view
|

Re: Interactive scripting

Todd Strader
Antony,

Thanks for you reply.  OK, this is really close now.  The updated
function below now highlights each line, but the screen still does not
move until the script exits.  I think the redraw command is just
redrawing the screen and not moving it to the current cursor position.
 To see what I mean, you'll have to have a file that's multiple
screens big and position it at the bottom of the file.  The script
will find, but not show, the lines at the top of the file.  When it
makes its way down to the bottom of the file, you'll see any lines
that are currently in the window being highlighted.

:function Outsig_func(...) range
  let lineNo = a:firstline

  while lineNo <= a:lastline
    let line = getline(lineNo)
    if line =~ "LMARX_H0RX"
      call cursor(lineNo, 1)
      hi link Outsig_foundline cursor
      match Outsig_foundline /\%#.*/
      redraw
      match none
      call input("found one on line " . lineNo)
    endif
    let lineNo = lineNo + 1
  endwhile

:endfunction

Is there some way to force the window to move to where the cursor is?
I thought this would be automatic, but I guess not.  Thanks for any
help.

Todd

On 9/22/05, Antony Scriven <[hidden email]> wrote:

> On Sep 22, Todd Strader wrote:
>
>  > [...] That is, when the cursor is moved and I'm still
>  > running a script, the buffer (or window, or whatever you
>  > want to call it) doesn't move to that position.  So, if I
>  > move the cursor to 50 different locations, I only see the
>  > last one after the script exits.  Here's my function as
>  > it is now, perhaps that will help explain what is going
>  > on here.
>
> When vim is processing the `call input ...' then the cursor
> is in the command line, not in the editing window. So you
> need to highlight the text manually, perhaps, something like
> this.
>
>    hi link Outsig_foundline cursor
>
>  > :function Outsig_func(...) range
>  >   let lineNo = a:firstline
>  >
>  >   while lineNo <= a:lastline
>  >     let line = getline(lineNo)
>  >     if line =~ "LMARX_H0RX"
>  >       call cursor(lineNo, 1)
>
>          match Outsig_foundline /\%#.*/
>          redraw
>          match none
>
>  >       call input("found one on line " . lineNo)
>  >     endif
>  >     let lineNo = lineNo + 1
>  >   endwhile
>  > :endfunction
>
> Antony
>
Reply | Threaded
Open this post in threaded view
|

Re: Interactive scripting

Antony Scriven
On Sep 22, Todd Strader wrote:

 > Thanks for you reply.  OK, this is really close now.  The
 > updated function below now highlights each line, but the
 > screen still does not move until the script exits.  I
 > think the redraw command is just redrawing the screen and
 > not moving it to the current cursor position. To see what
 > I mean, you'll have to have a file that's multiple
 > screens big and position it at the bottom of the file.
 > The script will find, but not show, the lines at the top
 > of the file.  When it makes its way down to the bottom of
 > the file, you'll see any lines that are currently in the
 > window being highlighted.

Sorry, I don't see this problem. Which version of vim are
you using? Start vim with `vim -u NONE' and see if you can
reproduce the problem.

 > :function Outsig_func(...) range
 >   let lineNo = a:firstline
 >
 >   while lineNo <= a:lastline
 >     let line = getline(lineNo)
 >     if line =~ "LMARX_H0RX"
 >       call cursor(lineNo, 1)
 >       hi link Outsig_foundline cursor

Btw this last line doesn't need to be in the function: it
only needs to be executed once.

Antony
Reply | Threaded
Open this post in threaded view
|

Re: Interactive scripting

Todd Strader
Antony,

Good call.  I am running 6.1 on my workstation (installed by the
sysadmins), but I checked it out on my own machine which has 6.3.
That worked.  I guess it's time to make a local install.  Thanks for
the help.

Todd

On 9/22/05, Antony Scriven <[hidden email]> wrote:

> On Sep 22, Todd Strader wrote:
>
>  > Thanks for you reply.  OK, this is really close now.  The
>  > updated function below now highlights each line, but the
>  > screen still does not move until the script exits.  I
>  > think the redraw command is just redrawing the screen and
>  > not moving it to the current cursor position. To see what
>  > I mean, you'll have to have a file that's multiple
>  > screens big and position it at the bottom of the file.
>  > The script will find, but not show, the lines at the top
>  > of the file.  When it makes its way down to the bottom of
>  > the file, you'll see any lines that are currently in the
>  > window being highlighted.
>
> Sorry, I don't see this problem. Which version of vim are
> you using? Start vim with `vim -u NONE' and see if you can
> reproduce the problem.
>
>  > :function Outsig_func(...) range
>  >   let lineNo = a:firstline
>  >
>  >   while lineNo <= a:lastline
>  >     let line = getline(lineNo)
>  >     if line =~ "LMARX_H0RX"
>  >       call cursor(lineNo, 1)
>  >       hi link Outsig_foundline cursor
>
> Btw this last line doesn't need to be in the function: it
> only needs to be executed once.
>
> Antony
>
Reply | Threaded
Open this post in threaded view
|

RE: Interactive scripting

Keith W. Roberts
 

> -----Original Message-----
> From: Todd Strader [mailto:[hidden email]]
> Sent: Thursday, September 22, 2005 9:14 AM
> To: [hidden email]
> Subject: Re: Interactive scripting
>
> Antony,
>
> Good call.  I am running 6.1 on my workstation (installed by the
> sysadmins), but I checked it out on my own machine which has 6.3.
> That worked.  I guess it's time to make a local install.  Thanks for
> the help.
>
> Todd

Also, sometimes :redraw won't work (maybe when &lazyredraw is set?), but
redraw! will.

-Keith