Skip to content

Commit e014708

Browse files
committed
charset: if we changed fenc from '' to enc, set nomodified
If the buffer had no fileencoding, and we changed fenc to match the Vim-wide encoding, we haven't actually changed the file. However, we have set modified on the buffer by virtue of assigning fenc. In this case, set nomodified since we didn't actually change anything. This affects new buffers created during the VimEnter autocmd.
1 parent 345a5a3 commit e014708

1 file changed

Lines changed: 40 additions & 17 deletions

File tree

plugin/editorconfig.vim

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,45 @@ endfunction " }}}2
403403

404404
" }}}1
405405

406-
function! s:ApplyConfig(bufnr, config) abort " Set the buffer options {{{1
406+
" Set the buffer options {{{1
407+
function! s:SetCharset(bufnr, charset) abort " apply config['charset']
408+
409+
" Remember the buffer's state so we can set `nomodifed` at the end
410+
" if appropriate.
411+
let l:orig_fenc = getbufvar(a:bufnr, "&fileencoding")
412+
let l:orig_enc = getbufvar(a:bufnr, "&encoding")
413+
let l:orig_modified = getbufvar(a:bufnr, "&modified")
414+
415+
if a:charset == "utf-8"
416+
call setbufvar(a:bufnr, '&fileencoding', 'utf-8')
417+
call setbufvar(a:bufnr, '&bomb', 0)
418+
elseif a:charset == "utf-8-bom"
419+
call setbufvar(a:bufnr, '&fileencoding', 'utf-8')
420+
call setbufvar(a:bufnr, '&bomb', 1)
421+
elseif a:charset == "latin1"
422+
call setbufvar(a:bufnr, '&fileencoding', 'latin1')
423+
call setbufvar(a:bufnr, '&bomb', 0)
424+
elseif a:charset == "utf-16be"
425+
call setbufvar(a:bufnr, '&fileencoding', 'utf-16be')
426+
call setbufvar(a:bufnr, '&bomb', 1)
427+
elseif a:charset == "utf-16le"
428+
call setbufvar(a:bufnr, '&fileencoding', 'utf-16le')
429+
call setbufvar(a:bufnr, '&bomb', 1)
430+
endif
431+
432+
let l:new_fenc = getbufvar(a:bufnr, "&fileencoding")
433+
434+
" If all we did was change the fileencoding from the default to a copy
435+
" of the default, we didn't actually modify the file.
436+
if !l:orig_modified && (l:orig_fenc ==# '') && (l:new_fenc ==# l:orig_enc)
437+
if g:EditorConfig_verbose
438+
echo 'Setting nomodified on buffer ' . a:bufnr
439+
endif
440+
call setbufvar(a:bufnr, '&modified', 0)
441+
endif
442+
endfunction
443+
444+
function! s:ApplyConfig(bufnr, config) abort
407445
if g:EditorConfig_verbose
408446
echo 'Options: ' . string(a:config)
409447
endif
@@ -462,22 +500,7 @@ function! s:ApplyConfig(bufnr, config) abort " Set the buffer options {{{1
462500

463501
if s:IsRuleActive('charset', a:config) &&
464502
\ getbufvar(a:bufnr, '&modifiable')
465-
if a:config["charset"] == "utf-8"
466-
call setbufvar(a:bufnr, '&fileencoding', 'utf-8')
467-
call setbufvar(a:bufnr, '&bomb', 0)
468-
elseif a:config["charset"] == "utf-8-bom"
469-
call setbufvar(a:bufnr, '&fileencoding', 'utf-8')
470-
call setbufvar(a:bufnr, '&bomb', 1)
471-
elseif a:config["charset"] == "latin1"
472-
call setbufvar(a:bufnr, '&fileencoding', 'latin1')
473-
call setbufvar(a:bufnr, '&bomb', 0)
474-
elseif a:config["charset"] == "utf-16be"
475-
call setbufvar(a:bufnr, '&fileencoding', 'utf-16be')
476-
call setbufvar(a:bufnr, '&bomb', 1)
477-
elseif a:config["charset"] == "utf-16le"
478-
call setbufvar(a:bufnr, '&fileencoding', 'utf-16le')
479-
call setbufvar(a:bufnr, '&bomb', 1)
480-
endif
503+
call s:SetCharset(a:bufnr, a:config["charset"])
481504
endif
482505

483506
augroup editorconfig_trim_trailing_whitespace

0 commit comments

Comments
 (0)