Possible to disable file change detection?

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

Possible to disable file change detection?

adah
When a file being edited is changed on the disk, Vim will prompt to load
the file from the disk.  This is, of course, very useful.  However, in
some cases when a command being executed can change the file on disk and
automatically reload it, the prompt is simply annoying.  Is there any
option to achieve this?

Best regards,

Yongwei
Reply | Threaded
Open this post in threaded view
|

Re: Possible to disable file change detection?

Gary Johnson
On 2005-09-22, [hidden email] wrote:
> When a file being edited is changed on the disk, Vim will prompt to load
> the file from the disk.  This is, of course, very useful.  However, in
> some cases when a command being executed can change the file on disk and
> automatically reload it, the prompt is simply annoying.  Is there any
> option to achieve this?

As far as I know, there is no option to disable this.  However,
there is at least one workaround, if you have access to the 'touch'
command, as on a Unix system.  The following is part of my plugin of
ClearCase commands.

-----------------------------------------------------------------

" Note on ClearCase Checkin and Checkout Functions
"
" The goal of these functions is to make checking files in to and out of
" ClearCase as painless and transparent as possible.  Doing this
" requires working around a number of protections vim has to help the
" user avoid shooting himself in the foot.
"
" When vim reads a file into a buffer, it records the file's
" modification time in some structure inaccessible to the user.  When
" vim writes a buffer to a file, it compares the file's modification
" time against the modification time when the file was read.  If the two
" differ, vim warns the user.  Vim does not provide a means to disable
" this warning.  Checking out a file changes the file's modification
" time.  Consequently, vim warns the user the first time the user writes
" a file after checking it out.
"
" To work around this, the checkout functions save the file's
" modification time before performing the checkout, then reset the
" file's modification time to the saved value.
"
" With version 6.0, vim started checking for changed file permissions
" as well as modification time following any shell command.  When vim
" detects such changes, it asks the user if this is OK or if the user
" wishes to reload the file.  This is annoying when it follows a
" ClearCase checkin or checkout command because the user knows that the
" file permissions and modification times have changed and that this is
" OK.  There is no straightforward way to turn these checks off, but
" Bram said in e-mail to the vim list on 7 Feb 2002 that the following
" will work, and it does, but I don't know why.
"
"     1. ":checktime"
"     2. Add a FileChangedShell autocommand that doesn't do anything.
"     3. Change the [file] attributes.
"     4. ":checktime"
"     5. Remove the autocommand again.
"
" So I have added the following prelude and postlude to the checkin and
" checkout functions:
"
"     Prelude:
"         checktime
"         exe "au FileChangedShell " . expand("%") . " let &cpo = &cpo"
"
"     Postlude:
"         checktime
"         exe "au! FileChangedShell " . expand("%")
"
" where "let &cpo = &cpo" is my command that does nothing.
"
" Update June 5, 2002:
" From J?rgen Kr?mer on the vim mailing list, here's another
" FileChangedShell command:
"
"     autocmd FileChangedShell *
"       \ echohl WarningMsg |
"       \ echo "File has been changed outside of vim." |
"       \ echohl None

" ClearCase checkout function and command:
"
" -  Save the file access and modification times.
" -  Check out the file.
" -  Restore the file access and modification times so that vim
"   doesn't know that it's been changed, thereby avoiding the
"   annoying warning and prompt when writing the file.
" -  Set the buffer's read-only status to match that of the file.
"
" Note that if some other command modifies the file, the warning and
" prompt will still work.
"
" To Do:
" Obtain file name from "system('cleartool des -s -cview "'...'"')"
" as ccase.vim:s:CtConsoleDiff() does so that files can be checked
" out from a vim instance spawned from the ClearCase Vtree Browser
" by the Open command.
"
function! Co()
    checktime
    exe "au FileChangedShell " . expand("%") . " let &cpo = &cpo"
    let mtime = strftime("%Y%m%d%H%M.%S", getftime(expand("%")))

    !cleartool co -nc %

    if filewritable(expand("%"))
        set noro
    endif

    exe "silent !touch -t " . mtime . " %"
    checktime
    exe "au! FileChangedShell " . expand("%")
endfunction
command! Co call Co()

-----------------------------------------------------------------

HTH,
Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

Re: Possible to disable file change detection?

A.J.Mechelynck
In reply to this post by adah
----- Original Message -----
From: <[hidden email]>
To: <[hidden email]>
Sent: Thursday, September 22, 2005 5:37 AM
Subject: Possible to disable file change detection?


> When a file being edited is changed on the disk, Vim will prompt to load
> the file from the disk.  This is, of course, very useful.  However, in
> some cases when a command being executed can change the file on disk and
> automatically reload it, the prompt is simply annoying.  Is there any
> option to achieve this?
>
> Best regards,
>
> Yongwei

Method 1:
This message is never given if there is a FileChangedShell autocommand. So,
if you want to disable the message and prompt for file foobar.txt

    :autocmd FileChangedShell foobar.txt echon ""

To disable it for all files, replace foobar.txt by *

To reload the current file without the prompt and then re-enable the prompt:
hit F2 after sourcing the following code snippet:

    function! NoPromptReload()
        augroup temp
            autocmd FileChangedShell * echon ""
        augroup END
        edit
        autocmd! temp
        augroup! temp
    endfunction
    map <F2> call NoPromptReload()

The above will fail if the file is modified, unless 'autowriteall' has been
set (in which case a write will occur, silently discarding the external
changes).

Method 2:
To automatically reload any file whenever it is detected that it has been
changed outside of Vim, and that is wasn't changed inside of Vim:

    :set autoread


Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: Possible to disable file change detection?

Hari Krishna Dara
In reply to this post by adah

On Thu, 22 Sep 2005 at 11:37am, [hidden email] wrote:

> When a file being edited is changed on the disk, Vim will prompt to load
> the file from the disk.  This is, of course, very useful.  However, in
> some cases when a command being executed can change the file on disk and
> automatically reload it, the prompt is simply annoying.  Is there any
> option to achieve this?
>
> Best regards,
>
> Yongwei

The only way you can disable the default behavior is by setting your own
handler for the FileChangedShell autocommand. I had to deal with exactly
the same problem in my perforce plugin as I checkout a file for edit. I
reload the file anyway, so didn't want to see the prompt from Vim. I
have converted my functions into generic utility functions and included
them in genutils.vim. Take a look at the DefFileChangedShell() and
DefFCShellInstall() functions from genutils.vim and their usage in
perforce.vim, if you are interested.

The simplest solution I think is to simply define this autocommand which
does nothing.

--
HTH,
Hari

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
Reply | Threaded
Open this post in threaded view
|

RE: Possible to disable file change detection?

Keith W. Roberts
 

> -----Original Message-----
> From: Hari Krishna Dara [mailto:[hidden email]]
> Sent: Thursday, September 22, 2005 10:59 AM
> To: [hidden email]
> Cc: [hidden email]
> Subject: Re: Possible to disable file change detection?
>
>
> On Thu, 22 Sep 2005 at 11:37am, [hidden email] wrote:
>
> > When a file being edited is changed on the disk, Vim will
> prompt to load
> > the file from the disk.  This is, of course, very useful.  
> However, in
> > some cases when a command being executed can change the
> file on disk and
> > automatically reload it, the prompt is simply annoying.  Is
> there any
> > option to achieve this?
> >
> > Best regards,
> >
> > Yongwei
>
> The only way you can disable the default behavior is by
> setting your own
> handler for the FileChangedShell autocommand. I had to deal
> with exactly
> the same problem in my perforce plugin as I checkout a file
> for edit. I
> reload the file anyway, so didn't want to see the prompt from Vim. I
> have converted my functions into generic utility functions
> and included
> them in genutils.vim. Take a look at the DefFileChangedShell() and
> DefFCShellInstall() functions from genutils.vim and their usage in
> perforce.vim, if you are interested.
>
> The simplest solution I think is to simply define this
> autocommand which
> does nothing.

Hari,

I've seen mentions of your perforce plugin for a few years and ignored them
as not applicable to me.  I changed jobs a couple months ago, and it wasn't
until this very moment that I realized it's now applicable, since we use
Perforce as SCC!

So I'll thank you in advance for your work in making my life easier. :)
I'll have to install and start using it post-haste!

-Keith

Reply | Threaded
Open this post in threaded view
|

RE: Possible to disable file change detection?

Hari Krishna Dara
In reply to this post by adah

On Thu, 22 Sep 2005 at 11:10am, Keith W. Roberts wrote:

>
>
> > -----Original Message-----
> > From: Hari Krishna Dara [mailto:[hidden email]]
> > Sent: Thursday, September 22, 2005 10:59 AM
> > To: [hidden email]
> > Cc: [hidden email]
> > Subject: Re: Possible to disable file change detection?
> >
> >
> > On Thu, 22 Sep 2005 at 11:37am, [hidden email] wrote:
> >
> > > When a file being edited is changed on the disk, Vim will
> > prompt to load
> > > the file from the disk.  This is, of course, very useful.
> > However, in
> > > some cases when a command being executed can change the
> > file on disk and
> > > automatically reload it, the prompt is simply annoying.  Is
> > there any
> > > option to achieve this?
> > >
> > > Best regards,
> > >
> > > Yongwei
> >
> > The only way you can disable the default behavior is by
> > setting your own
> > handler for the FileChangedShell autocommand. I had to deal
> > with exactly
> > the same problem in my perforce plugin as I checkout a file
> > for edit. I
> > reload the file anyway, so didn't want to see the prompt from Vim. I
> > have converted my functions into generic utility functions
> > and included
> > them in genutils.vim. Take a look at the DefFileChangedShell() and
> > DefFCShellInstall() functions from genutils.vim and their usage in
> > perforce.vim, if you are interested.
> >
> > The simplest solution I think is to simply define this
> > autocommand which
> > does nothing.
>
> Hari,
>
> I've seen mentions of your perforce plugin for a few years and ignored them
> as not applicable to me.  I changed jobs a couple months ago, and it wasn't
> until this very moment that I realized it's now applicable, since we use
> Perforce as SCC!
>
> So I'll thank you in advance for your work in making my life easier. :)
> I'll have to install and start using it post-haste!
>
> -Keith

I am glad for picking up an expert user like you, I am sure it will be
benefitial for the plugin in general.

--
Thanks,
Hari


               
__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
Reply | Threaded
Open this post in threaded view
|

Re: Possible to disable file change detection?

adah
In reply to this post by adah
Thank you all, Gary, Tony, and Hari, for your suggestions.  I have
uploaded a new version of CVS integration plugin at

  http://vim.sourceforge.net/scripts/script.php?script_id=1245

that incorporated the FileChangedShell trick.

Gary Johnson wrote:

> function! Co()
>     checktime
>     exe "au FileChangedShell " . expand("%") . " let &cpo = &cpo"
>     let mtime = strftime("%Y%m%d%H%M.%S", getftime(expand("%")))
>
>     !cleartool co -nc %
>
>     if filewritable(expand("%"))
>                set noro
>     endif
>
>     exe "silent !touch -t " . mtime . " %"
>     checktime
>     exe "au! FileChangedShell " . expand("%")
> endfunction

Why touch necessary?  I just used something like:

  checktime
  exec 'au FileChangedShell ' . expand('%') . ' let $CVSOPT=$CVSOPT'
  call CVSDoCommand('commit -m "'.message.'" '.rev)
  checktime
  exec 'au! FileChangedShell ' . expand('%')
  edit

and it works all right.

Best regards,

Yongwei
Reply | Threaded
Open this post in threaded view
|

Re: Possible to disable file change detection?

Gary Johnson
On 2005-09-23, [hidden email] wrote:

> Gary Johnson wrote:
> > function! Co()
> >     checktime
> >     exe "au FileChangedShell " . expand("%") . " let &cpo = &cpo"
> >     let mtime = strftime("%Y%m%d%H%M.%S", getftime(expand("%")))
> >
> >     !cleartool co -nc %
> >
> >     if filewritable(expand("%"))
> >                set noro
> >     endif
> >
> >     exe "silent !touch -t " . mtime . " %"
> >     checktime
> >     exe "au! FileChangedShell " . expand("%")
> > endfunction
>
> Why touch necessary?  I just used something like:
>
>   checktime
>   exec 'au FileChangedShell ' . expand('%') . ' let $CVSOPT=$CVSOPT'
>   call CVSDoCommand('commit -m "'.message.'" '.rev)
>   checktime
>   exec 'au! FileChangedShell ' . expand('%')
>   edit
>
> and it works all right.

Because if I don't restore the modification time on the file after I
check it out, when I later write the file vim complains:

    WARNING: The file has been changed since reading it!!!
    Do you really want to write to it (y/n)?

Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

Re: Possible to disable file change detection?

adah
In reply to this post by adah
Gary Johnson wrote:

>> Why touch necessary?  I just used something like:
>>
>>  checktime
>>  exec 'au FileChangedShell ' . expand('%') . ' let $CVSOPT=$CVSOPT'
>>  call CVSDoCommand('commit -m "'.message.'" '.rev)
>>  checktime
>>  exec 'au! FileChangedShell ' . expand('%')
>>  edit
>>
>> and it works all right.
>
> Because if I don't restore the modification time on the file after I
> check it out, when I later write the file vim complains:
>
>     WARNING: The file has been changed since reading it!!!
>     Do you really want to write to it (y/n)?
>
> Gary

Ah, I did not realize that you were doing a check-out, while I focused
on doing a check-in.

(However, if the file was read-only and the buffer was not modified,
using `edit' to force a reload and to recognize the attribute change
might be a good idea too.)

Best regards,

Yongwei
Reply | Threaded
Open this post in threaded view
|

Re: Possible to disable file change detection?

Gary Johnson
On 2005-09-23, [hidden email] wrote:

> Gary Johnson wrote:
> >> Why touch necessary?  I just used something like:
> >>
> >>  checktime
> >>  exec 'au FileChangedShell ' . expand('%') . ' let $CVSOPT=$CVSOPT'
> >>  call CVSDoCommand('commit -m "'.message.'" '.rev)
> >>  checktime
> >>  exec 'au! FileChangedShell ' . expand('%')
> >>  edit
> >>
> >> and it works all right.
> >
> > Because if I don't restore the modification time on the file after I
> > check it out, when I later write the file vim complains:
> >
> >     WARNING: The file has been changed since reading it!!!
> >     Do you really want to write to it (y/n)?
> >
> > Gary
>
> Ah, I did not realize that you were doing a check-out, while I focused
> on doing a check-in.
>
> (However, if the file was read-only and the buffer was not modified,
> using `edit' to force a reload and to recognize the attribute change
> might be a good idea too.)

Yeah, I've noticed that some people's scripts reload the file after
a checkout.  However, I often found myself beginning to edit a file
before realizing that I hadn't yet checked it out.  I wanted to be
able to check out the file without losing my changes.  That's also
the reason for this part of the function,

    if filewritable(expand("%"))
        set noro
    endif

to remove the readonly status of the buffer if the checkout
succeeded.

Regards,
Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

RE: Possible to disable file change detection?

Keith W. Roberts
In reply to this post by Hari Krishna Dara
 

> -----Original Message-----
> From: Hari Krishna Dara [mailto:[hidden email]]
> Sent: Thursday, September 22, 2005 2:03 PM
> To: Keith W. Roberts
> Cc: [hidden email]; [hidden email]
> Subject: RE: Possible to disable file change detection?
>
>
> On Thu, 22 Sep 2005 at 11:10am, Keith W. Roberts wrote:
> >
> > Hari,
> >
> > I've seen mentions of your perforce plugin for a few years
> and ignored them
> > as not applicable to me.  I changed jobs a couple months
> ago, and it wasn't
> > until this very moment that I realized it's now applicable,
> since we use
> > Perforce as SCC!
> >
> > So I'll thank you in advance for your work in making my
> life easier. :)
> > I'll have to install and start using it post-haste!
> >
> > -Keith
>
> I am glad for picking up an expert user like you, I am sure it will be
> benefitial for the plugin in general.
>
> --
> Thanks,
> Hari

Thanks, I hope to be able to.  Right now I'm using Perforce, Textpad and Vim
simultaneously and independently.  Everyone else here uses Textpad.  I'll
have to spend some time upgrading Vim and customizing it for use here; then
maybe others will like what I've done.  The only problem is one of time. :)

-Keith