On Thu, Feb 11, 2010 at 4:05 PM, epanda <[email protected]> wrote:
...
> this work well on first cursorhold but from the second one, I would
> like autocommand call updatefile only
> if cursor is hold on copen buffer.
>
> So I have to delete the current autocommand and define another one,but
> I don't know how.


I wrote this (quite some time ago):

" Tip #616: Have Vim check automatically if the file has changed
externally /*{{{*/
" http://www.vim.org/tips/tip.php?tip_id=616
" If you are using a console version of Vim, or dealing
" with a file that changes externally (ie a web server log)
" then Vim does not always check to see if the file has been changed.
" The GUI version of Vim will check more often (for example on Focus change),
" and prompt you if you want to reload the file.
"
" There can be cases where you can be working away, and Vim does not
" realize the file has changed.
"
" This function will force Vim to check more often.
"
" The function will turn on the :checktime command so that the
" file is checked based on the CursorHold event.
"
" Thanks to Jürgen Krämer, Antoine J. Mechelynck for the help.
"
" CheckForUpdates will toggle the behaviour on the current buffer.
"
" CheckForUpdates! will also turn on autoread for this buffer so that
" if the file is changed, it will reload automatically without
" prompting the user.
function! CheckForUpdates( autoload )

    " Save the current default register
    let saveB = @"
    let msg   = "\n"

    " Check to see if the checkforupdates autocommand exists
    redir @"
    silent! exec 'au CheckForUpdates'.bufnr('%')
    redir END

    if @" =~ 'E216'
        if a:autoload == 1
            let msg = msg . 'AutoRead enabled - '
            setlocal autoread
        endif
        let filename = expand("%:p")
        if has('win32')
            let filename = substitute(filename, '\\', '/', 'g')
        endif

        silent! exec 'augroup CheckForUpdates'.bufnr('%')
        exec "au Cursorhold " . filename . " :checktime"
        augroup END
        let msg = msg . 'Now checking buffer:'.bufnr('%').' for updates ...'
    else
        if a:autoload == 1
            let msg = msg . 'AutoRead disabled - '
            setlocal noautoread
        endif
        " Using a autogroup allows us to remove it easily with the following
        " command.  If we do not use an autogroup, we cannot remove this
        " single :checktime command
        " augroup! checkforupdates
        silent! exec 'au! CheckForUpdates'.bufnr('%')
        silent! exec 'augroup! CheckForUpdates'.bufnr('%')
        let msg = msg . 'No longer checking buffer:'.bufnr('%').' for updates'
    endif

    echo msg
    let @"=saveB
endfunction
command! -bang CheckForUpdates :call CheckForUpdates(<bang>0)
" }}}


Anyway, what it does is create an autocmd via an augroup.  Then
detects it is there, and removes it.

You need to do several of the same things.

So, in order to delete the autocmd you create, you wrap it in an
autocmd group and delete the group.

HTH,
Dave

-- 
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

Reply via email to