Skip to content

Commit 2440ccd

Browse files
Jorengarenarcxw42
authored andcommitted
Add basic support for [No Name] buffers (fix #55)
1 parent 39bd110 commit 2440ccd

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

doc/editorconfig.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ To set this option, add any of the following lines to your |vimrc| file:
153153
<
154154
The default value is "line".
155155

156+
*g:EditorConfig_enable_for_new_buf*
157+
Set this to 1 if you want EditorConfig plugin to set options
158+
for new empty buffers too.
159+
Path to .editorconfig will be determined based on CWD (see |getcwd()|)
160+
>
161+
let g:EditorConfig_enable_for_new_buf = 1
162+
<
163+
This option defaults to 0.
164+
156165
*g:EditorConfig_preserve_formatoptions*
157166
Set this to 1 if you don't want your formatoptions modified when
158167
max_line_length is set:

plugin/editorconfig.vim

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ if !exists('g:EditorConfig_disable_rules')
6060
let g:EditorConfig_disable_rules = []
6161
endif
6262

63+
if !exists('g:EditorConfig_enable_for_new_buf')
64+
let g:EditorConfig_enable_for_new_buf = 0
65+
endif
66+
6367
if !exists('g:EditorConfig_softtabstop_space')
6468
let g:EditorConfig_softtabstop_space = 1
6569
endif
@@ -203,9 +207,13 @@ endfunction " }}}1
203207
function! s:UseConfigFiles() abort " Apply config to the current buffer {{{1
204208
let b:editorconfig_tried = 1
205209
let l:buffer_name = expand('%:p')
206-
" ignore buffers without a name
210+
207211
if empty(l:buffer_name)
208-
return
212+
if g:EditorConfig_enable_for_new_buf
213+
let l:buffer_name = getcwd() . "/."
214+
else
215+
return
216+
endif
209217
endif
210218

211219
if exists("b:EditorConfig_disable") && b:EditorConfig_disable
@@ -247,11 +255,11 @@ function! s:UseConfigFiles() abort " Apply config to the current buffer {{{1
247255
endfor
248256

249257
if s:editorconfig_core_mode ==? 'vim_core'
250-
if s:UseConfigFiles_VimCore() == 0
258+
if s:UseConfigFiles_VimCore(l:buffer_name) == 0
251259
let b:editorconfig_applied = 1
252260
endif
253261
elseif s:editorconfig_core_mode ==? 'external_command'
254-
call s:UseConfigFiles_ExternalCommand()
262+
call s:UseConfigFiles_ExternalCommand(l:buffer_name)
255263
let b:editorconfig_applied = 1
256264
else
257265
echohl Error |
@@ -269,6 +277,7 @@ function! s:EditorConfigEnable(should_enable)
269277
autocmd!
270278
if a:should_enable
271279
autocmd BufNewFile,BufReadPost,BufFilePost * call s:UseConfigFiles()
280+
autocmd VimEnter,BufNew * call s:UseConfigFiles()
272281
endif
273282
augroup END
274283
endfunction
@@ -289,29 +298,29 @@ call s:EditorConfigEnable(1)
289298

290299
" UseConfigFiles function for different modes {{{1
291300

292-
function! s:UseConfigFiles_VimCore()
301+
function! s:UseConfigFiles_VimCore(target)
293302
" Use the vimscript EditorConfig core
294303
try
295304
let l:config = editorconfig_core#handler#get_configurations(
296-
\ { 'target': expand('%:p') } )
305+
\ { 'target': a:target } )
297306
call s:ApplyConfig(l:config)
298307
return 0 " success
299308
catch
300309
return 1 " failure
301310
endtry
302311
endfunction
303312

304-
function! s:UseConfigFiles_ExternalCommand()
313+
function! s:UseConfigFiles_ExternalCommand(target)
305314
" Use external EditorConfig core (e.g., the C core)
306315

307316
call s:DisableShellSlash()
308317
let l:exec_path = shellescape(s:editorconfig_exec_path)
309318
call s:ResetShellSlash()
310319

311-
call s:SpawnExternalParser(l:exec_path)
320+
call s:SpawnExternalParser(l:exec_path, a:target)
312321
endfunction
313322

314-
function! s:SpawnExternalParser(cmd) " {{{2
323+
function! s:SpawnExternalParser(cmd, target) " {{{2
315324
" Spawn external EditorConfig. Used by s:UseConfigFiles_ExternalCommand()
316325

317326
let l:cmd = a:cmd
@@ -323,7 +332,7 @@ function! s:SpawnExternalParser(cmd) " {{{2
323332
let l:config = {}
324333

325334
call s:DisableShellSlash()
326-
let l:cmd = l:cmd . ' ' . shellescape(expand('%:p'))
335+
let l:cmd = l:cmd . ' ' . shellescape(a:target)
327336
call s:ResetShellSlash()
328337

329338
let l:parsing_result = split(system(l:cmd), '\v[\r\n]+')

0 commit comments

Comments
 (0)