summaryrefslogtreecommitdiffstats
path: root/vim/plugin/dircreate.vim
blob: 11da7f909f12ad48d54ee337e4b4ec54bc80ec7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function! s:ensure_dir_exists()
    let l:required_dir = expand("%:h")
    if !isdirectory(l:required_dir)
        if <SID>ask_quit("Directory '" . l:required_dir . "' doesn't exist.", "&Create it?")
            return
        endif

        try
            call mkdir(l:required_dir, 'p')
        catch
            call <SID>ask_quit("Can't create '" . l:required_dir . "'", "&Continue anyway?")
        endtry
    endif
endfunction

function! s:ask_quit(msg, proposed_action)
    if confirm(a:msg, "&Quit?\n" . a:proposed_action) == 1
        if len(getbufinfo()) > 1
            silent bd
            return 1
        else
            exit
        end
    endif
    return 0
endfunction

augroup dircreate
    autocmd!
    autocmd BufNewFile * call <SID>ensure_dir_exists()
augroup END