From 6ad6dcd5d4ca2cbe28de8ea5ffe77cfee40a7004 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 17 Sep 2026 16:56:45 -0400 Subject: fix(filetype): empty 'filetype' skips :setfiletype #41958 Problem: After `:restart` a restored buffer can have an empty 'filetype'. Lazy LSP setup during the read replays FileType via `:doautoall`, which sets `did_filetype` even though `filetype=''`. :e foo.md :restart :set ft? " filetype= Solution: Set `did_filetype` only if 'filetype' is non-empty. --- src/nvim/autocmd.c | 4 ++-- src/nvim/eval.lua | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index 0b529f0757..909c8263bc 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -1710,8 +1710,8 @@ bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); nesting++; // see matching decrement below - // Remember that FileType was triggered. Used for did_filetype(). - if (event == EVENT_FILETYPE) { + // Remember that FileType was triggered, if 'filetype' was actually set. For :setf. + if (event == EVENT_FILETYPE && *curbuf->b_p_ft != NUL) { curbuf->b_did_filetype = true; } diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 2e37a8bc76..3e4e535970 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -1952,10 +1952,11 @@ M.funcs = { did_filetype = { desc = [=[ Returns |TRUE| when autocommands are being executed and the - FileType event has been triggered at least once. Can be used - to avoid triggering the FileType event again in the scripts - that detect the file type. |FileType| + FileType event triggered at least once for a buffer with + a non-empty 'filetype'. Can be used to avoid triggering + the FileType event again in the |ftdetect| scripts. |FileType| Returns |FALSE| when `:setf FALLBACK` was used. + When editing another file, the counter is reset, thus this really checks if the FileType event has been triggered for the current buffer. This allows an autocommand that starts -- cgit