Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ Plug 'emersonmx/vim-prj'
- Save the config file
- Run `call prj#reload()` to reload config files

## Commands
## Functions

```vim
call prj#reload " Reload config files (`.vim/vimrc` by default).
call prj#reload() " Reload config files (`.vim/vimrc` by default).
call prj#edit() " Edit the config file.
```

## Configuration

Default values are listed here:

```vim
let g:prj_config_path = ".vim/vimrc"
let g:prj_authorized_path = "$HOME/.cache/vim-prj"
let g:prj_config_path = '.prjrc'
let g:prj_authorized_path = '$HOME/.cache/vim-prj'
```
83 changes: 83 additions & 0 deletions autoload/prj.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
function! s:get_filename()
return g:prj_config_path
endfunction

function! s:base64(data)
return system("echo ".a:data. " | base64")
endfunction

function! s:setup_cache()
call system("mkdir -p ".g:prj_authorized_path)
endfunction

function! s:get_config_files()
let files = findfile(s:get_filename(), expand(".;$HOME/"), -1)
call map(files, "fnamemodify(v:val, ':p')")
call uniq(sort(files))
call filter(files, "v:val != '".expand("$HOME/".s:get_filename())."'")
return files
endfunction

function! s:get_auth_path(path)
let b64_path = s:base64(a:path)
return g:prj_authorized_path."/".b64_path
endfunction

function! s:is_authorized(config_path)
let auth_path = s:get_auth_path(a:config_path)
call system("sha256sum --quiet --check ".auth_path)
return v:shell_error == 0
endfunction

function! s:authorize(path)
call s:setup_cache()
let auth_path = s:get_auth_path(a:path)
call system("sha256sum ".a:path." > ".auth_path)
endfunction

function! s:load_configs(configs)
let unauth_configs = []

for config in a:configs
if s:is_authorized(config) == 1
continue
endif

call add(unauth_configs, config)
endfor

if !empty(unauth_configs)
let ss = "Unauthorized configs:\n"
let ss = ss.join(map(copy(unauth_configs), '"- ".v:val."\n"'), '')
let authorize = input(ss."Authorize all? (y/N) ")
if authorize != 'y'
return
endif

for c in unauth_configs
call s:authorize(c)
endfor
endif

for config in a:configs
exec "source ".config
endfor
endfunction

function! prj#reload()
let configs = s:get_config_files()
if empty(configs)
return
endif
call s:load_configs(configs)
endfunction

function! prj#edit()
let configs = s:get_config_files()
if empty(configs)
exec "edit ".s:get_filename()
return
endif

exec "edit ".configs[-1]
endfunction
96 changes: 3 additions & 93 deletions plugin/prj.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if exists('s:did_load')
if exists('s:loaded_prj')
finish
endif
let s:did_load = 1
let s:loaded_prj = 1

if !exists("g:prj_config_path")
let g:prj_config_path = ".prjrc"
Expand All @@ -10,94 +10,4 @@ if !exists("g:prj_authorized_path")
let g:prj_authorized_path = "$HOME/.cache/vim-prj"
endif

function! s:get_filename()
return g:prj_config_path
endfunction

function! s:base64(data)
return system("echo ".a:data. " | base64")
endfunction

function! s:setup_cache()
call system("mkdir -p ".g:prj_authorized_path)
endfunction

function! s:get_config_files()
let files = findfile(s:get_filename(), expand(".;$HOME/"), -1)
call map(files, "fnamemodify(v:val, ':p')")
call uniq(sort(files))
call filter(files, "v:val != '".expand("$HOME/".s:get_filename())."'")
return files
endfunction

function! s:get_auth_path(path)
let b64_path = s:base64(a:path)
return g:prj_authorized_path."/".b64_path
endfunction

function! s:is_authorized(config_path)
let auth_path = s:get_auth_path(a:config_path)
call system("sha256sum --quiet --check ".auth_path)
return v:shell_error == 0
endfunction

function! s:authorize(path)
call s:setup_cache()
let auth_path = s:get_auth_path(a:path)
call system("sha256sum ".a:path." > ".auth_path)
endfunction

function! s:load_config()
let unauth_configs = []
let configs = s:get_config_files()

for config in configs
if s:is_authorized(config) == 1
continue
endif

call add(unauth_configs, config)
endfor

if !empty(unauth_configs)
let ss = "Unauthorized configs:\n"
let ss = ss.join(map(copy(unauth_configs), '"- ".v:val."\n"'), '')
let authorize = input(ss."Authorize all? (y/N) ")
if authorize != 'y'
return
endif

for c in unauth_configs
call s:authorize(c)
endfor
endif

for config in configs
exec "source ".config
endfor
endfunction

function! prj#reload()
let configs = s:get_config_files()
if empty(configs)
echo "config not found!"
return
endif

call s:load_config()
endfunction

function! prj#edit()
let configs = s:get_config_files()
if empty(configs)
exec "edit ".s:get_filename()
return
endif

exec "edit ".configs[-1]
endfunction

augroup prj
autocmd!
autocmd VimEnter * call s:load_config()
augroup END
call prj#reload()