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

Re: BufReadPost: surprising behavior?

Expand Messages
  • Bram Moolenaar
    ... The reason is that reloading the file is done by reading the new file contents into the existing buffer and then deleting the old lines. This avoids that
    Message 1 of 4 , Jun 2, 2002
    • 0 Attachment
      Alan G Isaac wrote:

      > I'm going to try again to get some help on this by
      > showing that e! does not produce the same effect as
      > Vim loading a file.
      >
      > Save the following five lines as temp.vim
      > function! Protect()
      > %s@^\s*save\s\+.\+@save\ from\ old\ file@e
      > g/save/co 0
      > endfunction
      > au BufReadPost temp9.txt call Protect()
      >
      > Save the following single line of text as temp9.txt
      > why?
      >
      > Source your file temp.vim.
      > :so temp.vim
      >
      > Go to the buffer for temp9.txt and
      > i. change the single line to
      > save me
      > ii. do *not* save this buffer
      >
      > Give the command
      > e!
      >
      > As expected the buffer now contains the single line:
      > why?
      >
      > Now for my problem.
      > Change this single line in the buffer back to
      > save me
      > but again do *not* save this buffer.
      >
      > This time open another instance of Vim (or any other editor).
      > In a new buffer put in the single line
      > why?
      > and then save this as temp9.txt
      > (Ignore the resulting warnings.)
      >
      > Now return to the first instance of Vim.
      > It notices that temp9.txt has been changed and offers to load it.
      > Load it.
      > Note the unexpected result!
      > This time there are two lines in the buffer instead of one.
      >
      > The function operates on the text that
      > was in the buffer *before* the load (as well as on the
      > new text that is loaded).
      >
      > Why?

      The reason is that reloading the file is done by reading the new file
      contents into the existing buffer and then deleting the old lines. This
      avoids that the current buffer contents is lost when reading the file
      fails for some reason.

      Obviously your BufRead autocommands change the old text unexpectedly.
      The code isn't prepared for this. I'll think about a solution to avoid
      this problem.

      --
      From "know your smileys":
      O:-) Saint

      /// Bram Moolenaar -- Bram@... -- http://www.moolenaar.net \\\
      /// Creator of Vim -- http://vim.sf.net -- ftp://ftp.vim.org/pub/vim \\\
      \\\ Project leader for A-A-P -- http://www.a-a-p.org ///
      \\\ Help me helping AIDS orphans in Uganda - http://iccf-holland.org ///
    • Alan G Isaac
      ... Thanks! Alan Isaac
      Message 2 of 4 , Jun 2, 2002
      • 0 Attachment
        On Sun, 02 Jun 2002 20:25:29 +0200 Bram Moolenaar <Bram@...> wrote:
        > The reason is that reloading the file is done by reading the new file
        > contents into the existing buffer and then deleting the old lines. This
        > avoids that the current buffer contents is lost when reading the file
        > fails for some reason.

        > Obviously your BufRead autocommands change the old text unexpectedly.
        > The code isn't prepared for this. I'll think about a solution to avoid
        > this problem.



        Thanks!
        Alan Isaac
      Your message has been successfully submitted and would be delivered to recipients shortly.