Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init callback #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -7,17 +8,21 @@
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``
* the ``local current directory`` is changed to 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
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions autoload/project.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ command! -nargs=+ Project
command! -nargs=+ File
\ call project#config#title(<args>)

command! -nargs=+ Init
\ enew | call project#config#init(<args>)

command! -nargs=+ Callback
\ call project#config#callback(<args>)

Expand Down
27 changes: 23 additions & 4 deletions autoload/project/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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": [], "inits": [], "pos": s:pos}
let s:pos += 1
call s:setup()
endfunction
Expand All @@ -42,6 +42,19 @@ function! project#config#callback(title, callback) abort
call s:setup()
endfunction

function! project#config#init(title, callback) abort
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

function! project#config#title(filename, title) abort
let filename = s:full_path(a:filename)
if !filereadable(filename)
Expand All @@ -50,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": [], "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
Expand Down Expand Up @@ -144,10 +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)
execute 'nnoremap <silent><buffer> '. cnt .' :enew \| NERDTree '. s:escape(file).lcd."<cr>"
execute 'nnoremap <silent><buffer> '. cnt .' :enew \| NERDTree '. s:escape(file).inits.lcd."<cr>"
else
execute 'nnoremap <silent><buffer> '. cnt .' :edit '. s:escape(file).lcd."<cr>"
execute 'nnoremap <silent><buffer> '. cnt .' :edit '. s:escape(file).inits.lcd."<cr>"
endif
let cnt += 1
if cnt == 10
Expand Down