From 509fbb04751dd8dac786af9295be63e342989bb9 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Sat, 11 Dec 2021 08:38:04 +0200 Subject: [PATCH 1/5] Use a better variable name to prevent double load --- plugin/prj.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/prj.vim b/plugin/prj.vim index 13595ff..d78a022 100644 --- a/plugin/prj.vim +++ b/plugin/prj.vim @@ -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" From 6c3fdfc43fc64e32dcfea5c0e7d8a31148c57c6e Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Sat, 11 Dec 2021 08:38:34 +0200 Subject: [PATCH 2/5] Don't call get_config_files twice --- plugin/prj.vim | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/plugin/prj.vim b/plugin/prj.vim index d78a022..b060fcd 100644 --- a/plugin/prj.vim +++ b/plugin/prj.vim @@ -47,11 +47,10 @@ function! s:authorize(path) call system("sha256sum ".a:path." > ".auth_path) endfunction -function! s:load_config() +function! s:load_configs(configs) let unauth_configs = [] - let configs = s:get_config_files() - for config in configs + for config in a:configs if s:is_authorized(config) == 1 continue endif @@ -72,7 +71,7 @@ function! s:load_config() endfor endif - for config in configs + for config in a:configs exec "source ".config endfor endfunction @@ -83,8 +82,7 @@ function! prj#reload() echo "config not found!" return endif - - call s:load_config() + call s:load_configs(configs) endfunction function! prj#edit() From 12dce7231bbdbfa7970292b44c18af2c683272ca Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Sat, 11 Dec 2021 08:42:55 +0200 Subject: [PATCH 3/5] Move many functions to autoload --- autoload/prj.vim | 83 +++++++++++++++++++++++++++++++++++++++++++++++ plugin/prj.vim | 84 ------------------------------------------------ 2 files changed, 83 insertions(+), 84 deletions(-) create mode 100644 autoload/prj.vim diff --git a/autoload/prj.vim b/autoload/prj.vim new file mode 100644 index 0000000..167eac8 --- /dev/null +++ b/autoload/prj.vim @@ -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 diff --git a/plugin/prj.vim b/plugin/prj.vim index b060fcd..71edc03 100644 --- a/plugin/prj.vim +++ b/plugin/prj.vim @@ -10,90 +10,6 @@ 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_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) - echo "config not found!" - 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 augroup prj autocmd! From 3dc52f0f9ae382e432c96fd9c8b108d9284285a4 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Sat, 11 Dec 2021 08:51:53 +0200 Subject: [PATCH 4/5] Run prj#reload earlier then VimEnter --- plugin/prj.vim | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugin/prj.vim b/plugin/prj.vim index 71edc03..6268087 100644 --- a/plugin/prj.vim +++ b/plugin/prj.vim @@ -10,8 +10,4 @@ if !exists("g:prj_authorized_path") let g:prj_authorized_path = "$HOME/.cache/vim-prj" endif - -augroup prj - autocmd! - autocmd VimEnter * call s:load_config() -augroup END +call prj#reload() From e444457f3f46f3a3594071acecf91dfd9c3c06a8 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Sat, 11 Dec 2021 08:59:40 +0200 Subject: [PATCH 5/5] Small fixes to README --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 13dd80e..04437e5 100644 --- a/README.md +++ b/README.md @@ -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' ```