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

macro wizard needed

Expand Messages
  • Schuttberg@aol.com
    Hello, I am trying to write macro which compares the existence of words within two buffers (buffer1 and buffer2) in 2 windows. If a word which exists in
    Message 1 of 5 , Jun 1, 2001
    • 0 Attachment
      Hello,

      I am trying to write macro which compares the existence of words within two
      buffers (buffer1 and buffer2) in 2 windows.
      If a word which exists in buffer1 also exists in buffer2, the word in buffer1
      has to be deleted.
      Otherwise I just want to move to the next word in buffer1.
      I tried a mapping like this:
      (treating a single word the cursor is on in buffer1):

      :map <F11> :let v:errmsg = ""<CR>|:normal yw|<c-w><c-w>:execute "/" .
      @"<CR><c-w><c-w>:if (v:errmsg != "")|:normal dw|else :normal w|endif<CR>

      I tried different variations but I cannot make it work. I guess something is
      wrong with the if..else or the :normal expression...(?)

      Any Ideas?

      Thank You

      Achim
    • Benji Fisher
      ... My first suggestion is that anything this complicated should be ... let v:errmsg = ... endfun If you do not like this approach, have a look at ... IIRC,
      Message 2 of 5 , Jun 3, 2001
      • 0 Attachment
        Schuttberg@... wrote:
        >
        > Hello,
        >
        > I am trying to write macro which compares the existence of words within two
        > buffers (buffer1 and buffer2) in 2 windows.
        > If a word which exists in buffer1 also exists in buffer2, the word in buffer1
        > has to be deleted.
        > Otherwise I just want to move to the next word in buffer1.
        > I tried a mapping like this:
        > (treating a single word the cursor is on in buffer1):
        >
        > :map <F11> :let v:errmsg = ""<CR>|:normal yw|<c-w><c-w>:execute "/" .
        > @"<CR><c-w><c-w>:if (v:errmsg != "")|:normal dw|else :normal w|endif<CR>
        >
        > I tried different variations but I cannot make it work. I guess something is
        > wrong with the if..else or the :normal expression...(?)

        My first suggestion is that anything this complicated should be
        written as a function, not a macro:

        :map <F11> :call Foo()<CR>

        :fun! Foo()
        let v:errmsg = ""
        ...
        endfun

        If you do not like this approach, have a look at

        :help :map
        :help :normal

        IIRC, a bar (|) ends the :map command and separates it from the next
        command. Try

        :map <F11> :echo "foo"<CR>|echo "bar"

        for example. The :normal command, on the other hand, takes | and
        everything following it (including things that you might think are
        comments) as part of its argument.

        To solve these problems, try using <Bar> or \| instead of |, and
        :execute "normal yw" instead of just :normal yw and so on.

        HTH --Benji Fisher
      • Michael Geddes
        fun! FindInBuffer(winn, word) let cur=winnr() exe norm .a:winn. let ret= (search( , ) ) echo input(a:word. : .ret) exe norm
        Message 3 of 5 , Jun 4, 2001
        • 0 Attachment
          fun! FindInBuffer(winn, word)
          let cur=winnr()
          exe "norm \<c-w>".a:winn."\<c-w>"
          let ret= (search('\<'.a:word.'\>','') )
          echo input(a:word.':'.ret)
          exe "norm \<c-w>".cur."\<c-w>"
          return ret > 0
          endfun

          fun! DeleteNotIn( winn )
          let line=1
          while line <= line('$')
          let text=getline(line)
          if text != "" && FindInBuffer(a:winn, text )
          exe line."d"
          else
          let line=line+1
          endif
          endwhile
          endfun

          eg
          call DeleteNotIn( 2)

          :)

          > -----Original Message-----
          > From: Schuttberg@... [SMTP:Schuttberg@...]
          > Sent: Saturday, 2 June 2001 5:19
          > To: vim@...
          > Subject: macro wizard needed
          >
          > Hello,
          >
          > I am trying to write macro which compares the existence of words within
          > two
          > buffers (buffer1 and buffer2) in 2 windows.
          > If a word which exists in buffer1 also exists in buffer2, the word in
          > buffer1
          > has to be deleted.
          > Otherwise I just want to move to the next word in buffer1.
          > I tried a mapping like this:
          > (treating a single word the cursor is on in buffer1):
          >
          > :map <F11> :let v:errmsg = ""<CR>|:normal yw|<c-w><c-w>:execute "/" .
          > @"<CR><c-w><c-w>:if (v:errmsg != "")|:normal dw|else :normal w|endif<CR>
          >
          > I tried different variations but I cannot make it work. I guess something
          > is
          > wrong with the if..else or the :normal expression...(?)
          >
          > Any Ideas?
          >
          > Thank You
          >
          > Achim
        • Benji Fisher
          ... Note that this requires Vim 6.0, for the search() function. --Benji Fisher
          Message 4 of 5 , Jun 4, 2001
          • 0 Attachment
            Michael Geddes wrote:
            >
            > fun! FindInBuffer(winn, word)
            > let cur=winnr()
            > exe "norm \<c-w>".a:winn."\<c-w>"
            > let ret= (search('\<'.a:word.'\>','') )
            > echo input(a:word.':'.ret)
            > exe "norm \<c-w>".cur."\<c-w>"
            > return ret > 0
            > endfun

            Note that this requires Vim 6.0, for the search() function.

            --Benji Fisher
          • Michael Geddes
            Ok for 5.7 fun! Search( expr ) exe / .a:expr if getline( . ) =~ expr return line( . ) else return 0 end if endfun (not tested) //.
            Message 5 of 5 , Jun 4, 2001
            • 0 Attachment
              Ok for 5.7

              fun! Search( expr )
              exe '/'.a:expr
              if getline('.') =~ expr
              return line('.')
              else
              return 0
              end if
              endfun

              (not tested)
              //.

              > -----Original Message-----
              > From: Benji Fisher [SMTP:benji@...]
              > Sent: Tuesday, 5 June 2001 11:16
              > To: Michael Geddes
              > Cc: 'Schuttberg@...'; vim@...
              > Subject: Re: macro wizard needed
              >
              > Michael Geddes wrote:
              > >
              > > fun! FindInBuffer(winn, word)
              > > let cur=winnr()
              > > exe "norm \<c-w>".a:winn."\<c-w>"
              > > let ret= (search('\<'.a:word.'\>','') )
              > > echo input(a:word.':'.ret)
              > > exe "norm \<c-w>".cur."\<c-w>"
              > > return ret > 0
              > > endfun
              >
              > Note that this requires Vim 6.0, for the search() function.
              >
              > --Benji Fisher
            Your message has been successfully submitted and would be delivered to recipients shortly.