Yakov Lerner wrote:
On 8/21/06, Bob Hiestand <[EMAIL PROTECTED]> wrote:
On 8/21/06, Jürgen Krämer <[EMAIL PROTECTED]> wrote:
>
> 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

Why not change to the new buffer?  Something like:

:au BufAdd * if expand('<afile>')==''|execute 'buffer' expand('<abuf>')|execute
'file' tempname()|endif

This is nice, but. This works for :new (I tried BufNew),
but still does not work
for that empty buffer #1 that is created when vim is invoked without
commandline arguments.

I want it to work for the initial  empty buffer, too.

Yakov




IIUC, that buffer is created before sourcing the vimrc (at step 2 under ":help startup"), so apparently you're out of luck there. Try either BufWriteCmd (q.v.) or BufWinEnter (but the latter will be triggered too often, and your "if" will return FALSE except maybe the first time).


Best regards,
Tony.

Reply via email to