1

I am using the following Vim autocommand in my .gvimrc file:

augroup MyAuGroup
  autocmd MyAuGroup FileChangedShell * call FileChanedEvent_BuffUpdate()
augroup END

function FileChanedEvent_BuffUpdate()
  let MyBn  = bufname("%")
  let MyStr = "Warning: File \"".MyBn."\" has changed since editing started\nSee \":help W11\" for more info."
  let MyTest = confirm(MyStr, "&OK\n&Load File", 2, "W")
  if MyTest == 2
    edit
  else
  endif
endfunction

with the intention to replace the default gVim behaviour when a file is changed externally (see this question). However, if multiple windows are opened, showing multiple buffers, the edit command works on the last active window, and not on the window containing the buffer that was changed.

How can I determine which buffer caused the FileChangedShell event, and apply the edit commant to that buffer?

ysap
  • 2,620
  • 12
  • 50
  • 74

2 Answers2

4

From :help FileChangedShell:

NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer that was changed "<afile>".

You need to locate the window where the corresponding file is being edited. For that, the buffer number (in <abuf>) is even easier:

let winNr = bufwinnr(0 + expand('<abuf>'))
execute winNr . 'wincmd w'
edit

The same applies to the buffer name; replace

let MyBn  = bufname("%")

with

let MyBn  = expand('<afile>')
Ingo Karkat
  • 22,638
  • 2
  • 45
  • 58
  • I used `edit MyBn` instead of the `edit` command. But, when selecting the `load File` button, I get error `E811` and the buffer won't update. I think my alternative answer is more direct. – ysap Mar 21 '14 at 19:52
  • I wonder why there is a difference between the two `edit` forms. The `edit` (no args) version works when the last active buffer was the one with the changed file. – ysap Mar 21 '14 at 19:56
  • tried also `edit ` but got the same response – ysap Mar 21 '14 at 20:03
  • You would need `execute 'edit' fnameescape(MyBn)`, but I see you've found a more straightforward alternative. – Ingo Karkat Mar 21 '14 at 20:15
  • Unfortunately, this variation fails either. – ysap Mar 21 '14 at 20:31
0

Thanks to @IngoKarkat's answer. I found an alternative solution. Replace the if block in the function with:

  if MyTest == 2
    let v:fcs_choice = "reload"
  else
    let v:fcs_choice = ""
  endif

This seems to do the trick.

ysap
  • 2,620
  • 12
  • 50
  • 74