Hi,

Yakov Lerner wrote:
>
> Now that my attempt to write unnamed buffer under
> name /tmp/N failed, I want to autoname empty buffer.
> My first attempt does not work. Autoevent is ot invoked.
> 
> function! TempName()
>     let x=1
>     while filereadable("/tmp/".x)
>         let x = x + 1
>     endwhile
>     return "/tmp/".x
> endfun
> 
> au BufNew * if(expand('<afile>') == '') | call input("AAA") | endif
> au BufNew * if(expand('<afile>') == '') | exe "file ".TempName() | endif

I checked it with this autocommand

  au! BufNew *
    \ if expand('<afile>') == '' |
    \   exe 'file ' . input('Enter file name: ') |
    \ else |
    \   echomsg 'File already has a name' |
    \ endif

It seems to be triggered, but when the 'file' command is executed, VIM
is still in the original buffer thus renaming this one instead of the
new one. One way to circumvent this problem I can think of is to use the
BufNew event to store the new file name in a variable and to use this
variable in a BufEnter event. The following code should do this, but it
is untested:

  au! BufNew *
    \ if expand('<afile>') == '' |
    \   let new_file_name = input('Enter file name: ') |
    \ else |
    \   echomsg 'File already has a name' |
    \ endif

  au! BufEnter *
    \ if expand('<afile>') == '' && exists('new_file_name') |
    \   exe 'file ' . new_file_name |
    \   unlet new_file_name |
    \ endif

Regards,
Jürgen

-- 
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

Reply via email to