Skip to content

Commit

Permalink
fix(tmp buf): Set tmp buf name to target file name
Browse files Browse the repository at this point in the history
Set name of a tmp buffer to the chezmoi target file name before
running the filetype detection because Neovim v0.10 has changed the
builtin filetype detection behavior. It has used a buffer name instead
of a matched path name in an autocmd callback of the builtin filetype
detection since the change was applied on 2024-03-23. So calling only
`:doau filetypedetect BufRead <VIRTUAL FIXED PATH> ...` is no longer
working correcly and it needs to use `:file <VIRTUAL FIXED PATH>` for
setting a buffer name to a virtual fixed path before `:doau ...`.

Additionally, when using a command that creates or switches buffers,
use `keepalt` command modifier for keeping the alternative buffer name.

See the followings for more details of the related change:
Issue link: neovim/neovim#27914
Commit No: 2955c921ceaf5764e8d1592a78370d9ca3a268e2
  • Loading branch information
alker0 committed May 7, 2024
1 parent 2c78807 commit 286c2b5
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions autoload/chezmoi/filetype.vim
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ function! s:run_default_detect(detect_target) abort

let b:chezmoi_detecting_fixed = 1

if exists('g:chezmoi#use_tmp_buffer') && g:chezmoi#use_tmp_buffer == v:true
let bufnr_org = bufnr()
if bufexists(a:detect_target)
" Copy filetype to original buffer from an existant buffer.
call setbufvar(bufnr_org, '&filetype', getbufvar(a:detect_target, '&filetype'))
elseif exists('g:chezmoi#use_tmp_buffer') && g:chezmoi#use_tmp_buffer == v:true
" Save current status.
let evignore_save = &eventignore
let bufhidden_save = &bufhidden
Expand All @@ -183,7 +187,8 @@ function! s:run_default_detect(detect_target) abort
let l:reg_content_save = getreg('"')
let l:reg_type_save = getregtype('"')

let bufnr_org = bufnr()
let bufname_tmp = 'CHEZMOI_DETECT_' . bufnr_org
let escaped_target_filename = fnameescape(a:detect_target)

try
set eventignore=all
Expand All @@ -197,24 +202,34 @@ function! s:run_default_detect(detect_target) abort
" Avoid inheritance options on entering tmp buffer.
set cpo-=S

execute bufnr('CHEZMOI_DETECT_' . bufnr_org, 1) . 'buffer'
silent execute 'keepalt ' . bufnr(bufname_tmp, 1) . 'buffer'
set buftype=nofile
set bufhidden=wipe

" Set the current buffer name to the target file name
" of chezmoi because Neovim v0.10 uses a buffer name instead
" of a matched path name when the filetype detection in
" `filetypedetect` autocmd.
" See https://github.com/neovim/neovim/issues/27914
silent execute 'keepalt file ' . escaped_target_filename

" Delete another tmp buffer that running `:file` creates.
silent execute 'bwipeout ' . bufname_tmp

" Copy contents from original buffer.
silent put = getbufline(bufnr_org, 1, '$')
silent 1delete

set eventignore=FileType,Syntax
execute 'doau filetypedetect BufRead ' . fnameescape(a:detect_target)
execute 'doau filetypedetect BufRead ' . escaped_target_filename
set eventignore=all

" Copy filetype to original buffer.
call setbufvar(bufnr_org, '&filetype', &filetype)

" Return to original buffer and also cleanup
" tmp buffer automatically because `bufhidden=wipe`.
silent execute bufnr_org . 'buffer'
silent execute 'keepalt ' . bufnr_org . 'buffer'
finally
" Restore status.
let &eventignore = evignore_save
Expand Down

0 comments on commit 286c2b5

Please sign in to comment.