From 7e82152b6ac16a2573549c202aa70d2d289f7119 Mon Sep 17 00:00:00 2001 From: Maciej Watras Date: Sat, 11 Apr 2020 14:15:23 +0200 Subject: [PATCH 1/3] Add init command --- autoload/project.vim | 3 +++ autoload/project/config.vim | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/autoload/project.vim b/autoload/project.vim index 7846188..bd79219 100644 --- a/autoload/project.vim +++ b/autoload/project.vim @@ -4,6 +4,9 @@ command! -nargs=+ Project command! -nargs=+ File \ call project#config#title() +command! -nargs=+ Init +\ enew | call project#config#init() + command! -nargs=+ Callback \ call project#config#callback() diff --git a/autoload/project/config.vim b/autoload/project/config.vim index 43c890a..6aae502 100644 --- a/autoload/project/config.vim +++ b/autoload/project/config.vim @@ -24,7 +24,7 @@ function! project#config#project(arg, ...) abort if !isdirectory(project) return endif - let s:projects[title] = { "type": "project", "event": event, "project": project, "title": title, "callbacks": [], "pos": s:pos} + let s:projects[title] = { "type": "project", "event": event, "project": project, "title": title, "callbacks": [], "init": "", "pos": s:pos} let s:pos += 1 call s:setup() endfunction @@ -42,6 +42,13 @@ function! project#config#callback(title, callback) abort call s:setup() endfunction +function! project#config#init(title, callback) abort + " TODO allow many init functions? (this would be consistent with callbacks + " behavior) + let s:projects[a:title]["init"] = a:callback + call s:setup() +endfunction + function! project#config#title(filename, title) abort let filename = s:full_path(a:filename) if !filereadable(filename) @@ -50,7 +57,7 @@ function! project#config#title(filename, title) abort if !filereadable(filename) return else - let s:projects[a:title] = { "type": "filename", "event": filename, "title": a:title, "callbacks": [], "pos": s:pos } + let s:projects[a:title] = { "type": "filename", "event": filename, "title": a:title, "callbacks": [], "init": "", "pos": s:pos } let s:pos += 1 call s:setup() endif @@ -145,7 +152,12 @@ function! project#config#welcome() abort let line = printf(printf(' ['. cnt .']'.padding.'%s '.file, '%-'.max_title_length.'s'), v["title"]) call append('$', line) if get(g:, 'project_use_nerdtree', 0) && isdirectory(file) - execute 'nnoremap '. cnt .' :enew \| NERDTree '. s:escape(file).lcd."" + if (len(v['init']) > 0) + let inits = '\| call '.v['init'].'("'.v["title"].'")' + else + let inits = '' + endif + execute 'nnoremap '. cnt .' :enew '.inits.' \| NERDTree '. s:escape(file).lcd."" else execute 'nnoremap '. cnt .' :edit '. s:escape(file).lcd."" endif From dcc8ebd143cff6c53ed00fe979fcc74c54185246 Mon Sep 17 00:00:00 2001 From: Maciej Watras Date: Sat, 11 Apr 2020 20:39:52 +0200 Subject: [PATCH 2/3] Allow many init callbacks User can now define any number of init functions for a project, by writing multiple Init commands, just like Callback works. They will be called sequentially in the same order they are defined: Project 'path', 'ProjectName' Init 'ProjectName', 'FirstFunction' Init 'ProjectName', 'SecondFunction' will result in (...) | call FirstFunction('ProjectName') | call SecondFunction('ProjectName') (...) execution when project is opened. This only works if g:project_enable_welcome is set to 1. --- autoload/project/config.vim | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/autoload/project/config.vim b/autoload/project/config.vim index 6aae502..e36330e 100644 --- a/autoload/project/config.vim +++ b/autoload/project/config.vim @@ -24,7 +24,7 @@ function! project#config#project(arg, ...) abort if !isdirectory(project) return endif - let s:projects[title] = { "type": "project", "event": event, "project": project, "title": title, "callbacks": [], "init": "", "pos": s:pos} + let s:projects[title] = { "type": "project", "event": event, "project": project, "title": title, "callbacks": [], "inits": [], "pos": s:pos} let s:pos += 1 call s:setup() endfunction @@ -43,9 +43,15 @@ function! project#config#callback(title, callback) abort endfunction function! project#config#init(title, callback) abort - " TODO allow many init functions? (this would be consistent with callbacks - " behavior) - let s:projects[a:title]["init"] = a:callback + if type(a:callback) == type([]) + let callbacks = a:callback + else + let callbacks = [a:callback] + endif + let project_or_filename = s:projects[a:title] + for callback in callbacks + call add(project_or_filename["inits"], callback) + endfor call s:setup() endfunction @@ -57,7 +63,7 @@ function! project#config#title(filename, title) abort if !filereadable(filename) return else - let s:projects[a:title] = { "type": "filename", "event": filename, "title": a:title, "callbacks": [], "init": "", "pos": s:pos } + let s:projects[a:title] = { "type": "filename", "event": filename, "title": a:title, "callbacks": [], "inits": [], "pos": s:pos } let s:pos += 1 call s:setup() endif @@ -151,15 +157,16 @@ function! project#config#welcome() abort endif let line = printf(printf(' ['. cnt .']'.padding.'%s '.file, '%-'.max_title_length.'s'), v["title"]) call append('$', line) + let inits = '' + if (len(v['inits']) > 0) + for i in v['inits'] + let inits = inits . ' \| call '.i.'("'.v["title"].'") ' + endfor + endif if get(g:, 'project_use_nerdtree', 0) && isdirectory(file) - if (len(v['init']) > 0) - let inits = '\| call '.v['init'].'("'.v["title"].'")' - else - let inits = '' - endif - execute 'nnoremap '. cnt .' :enew '.inits.' \| NERDTree '. s:escape(file).lcd."" + execute 'nnoremap '. cnt .' :enew \| NERDTree '. s:escape(file).inits.lcd."" else - execute 'nnoremap '. cnt .' :edit '. s:escape(file).lcd."" + execute 'nnoremap '. cnt .' :edit '. s:escape(file).inits.lcd."" endif let cnt += 1 if cnt == 10 From 781bff73f51c12b1e3960e62a58cf41495a134e9 Mon Sep 17 00:00:00 2001 From: Maciej Watras Date: Wed, 15 Apr 2020 21:55:11 +0200 Subject: [PATCH 3/3] Update README file --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8c8773a..dd8502d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ ## News +* New command Init * [New feature](#callbacks) ``project#utils#alternate``. To alternate between `file.ext` and ``file_suffix.ext`` or ``fileSuffix.ext`` with the command ``:A`` * [Windows support added](https://twitter.com/amiorin/status/336003331984076800) @@ -7,6 +8,7 @@ A Project is made of : * One directory (the ``root of the project``) * One title (by default the last part of the the ``root of the project``) +* One or more ``inits`` * One or more ``callbacks`` Everytime you open a file nested in the ``root of the project`` @@ -14,10 +16,13 @@ Everytime you open a file nested in the ``root of the project`` * the ``guitablabel`` is set to the ``title`` of the project * the ``callbacks`` of the project are executed +Every time you choose project on the welcome screen, the ``inits`` of the +project are executed. + ![img][0] ## Commands -There are four commands : +There are five commands : * ``Project`` It's used inside the ``.vimrc``. The first parameter is the ``path`` to the project. The second parameter is optional and it is the ``title`` of the @@ -51,6 +56,11 @@ the name a function or an array of function names. This function or these functions are callbacks and they are executed everytime a file nested in the ``root of the project`` is opened with **one parameter** that is the ``title`` of the project. +* ``Init`` +It's used inside the ``.vimrc``. It works just as the ``Callback`` command, except +it is executed _once_ when project is selected on the welcome screen. If welcome +screen is not used (that is, ``g:project_enable_welcome`` is set to 0), this +command does nothing. * ``Welcome`` It's the [``Startify``](https://github.com/mhinz/vim-startify) equivalent. If you don't want ``Welcome`` to appear when you start vim: @@ -145,8 +155,13 @@ Project 'glib' Project 'reloadlive' Project 'flashcards' Project 'leitner' +Init 'leitner' , 'Murphy' Callback 'leitner' , ['AddSpecToPath', 'RemoveTextWidth'] +function! Murphy(title) + colorscheme murphy +endfunction + function! AddSpecToPath(tile) abort setlocal path+=spec endfunction