I have the following function in an autoload file:
function! closet#FindSkeletonForFile()
if exists('b:closet_skeleton')
return 0
endif
let b:closet_skeleton = s:closet.find_skeleton_for_file(expand('%:p'), &filetype)
if b:closet_skeleton !=# ''
let cpopts = &cpoptions
set cpoptions-=a
echom "Here goes the read:"
execute '0read '.fnameescape(b:closet_skeleton)
let &cpoptions = cpopts
return 1
endif
return 0
endfunction
This is triggered by a BufNewFile autocommand, and it appears to be working. The autocmd is in my plugin/closet.vim file, and looks like this:
augroup closet
autocmd!
autocmd BufNewFile * call closet#FindSkeletonForFile()
augroup END
When I invoke vi foo.vim (a file that does not exist) I get an empty buffer, some of my earlier code identifies valid skeletons, I get a confirm dialog, the filename is returned, and the s:closet.find_skeleton_for_file(...) function returns the correct value.
My echom outputs to the message log, and when I run :mess in the new vim window, I see this:
Messages maintainer: Bram Moolenaar <Bram@vim.org>
Here goes the read:
"~/Code/aghast/vim-closet/Closet/plugin.vim" 28L, 448C
Press ENTER or type command to continue
This tells me that the dummy plugin is being read somewhere, since those line/character numbers are what I see when I edit the dummy file.
But the foo.vim buffer doesn't get changed! Since I can see the code being run correctly (many, many :echom statements) I'm pretty sure it's just down to getting that 0read statement to ... well, read!
Can someone clue me in on the obvious thing I've missed?
foo.vim... if you run:execute '0read '.fnameescape(b:closet_skeleton)by hand, does it properly populate the file with the contents of the intended skeleton file? – filbranden Sep 24 '19 at 23:34b:closet_skeletonbuffer variable has been set, so at least my script is getting that right. – aghast Sep 25 '19 at 01:130readis actually reading but another hook is later erasing the buffer? Maybe try to test that without other plug-ins loaded to see if that works? – filbranden Sep 25 '19 at 01:43vim -u NONE, then did:set rtp+=~/.../vim-closet, then:e foo.vimand the template appeared. Now to slowly remove directories... – aghast Sep 25 '19 at 02:01line('$')pointed to, and quit if the buffer had contents, and now all is well! Thanks. – aghast Sep 25 '19 at 02:12