Skip to content

Fix weird path problem with cygwin vim #1

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

Open
wants to merge 4 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
70 changes: 39 additions & 31 deletions autoload/vimprj.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
" current file (types :make ), then vimprj swithed to the
" 'default' settings.
" *) 1.10 - fixed issue with spaces in paths (thanks to Alexey Shevchenko)
" *) 1.11 - added $VIMPRJ_PROJECT_ROOT variable, which is set to the root of
" the currently active project.
" - default behavior changed: now, by default, Vimprj doesn't change
" your current working directory.
" Set g:vimprj_changeCurDirIfVimprjFound if you want it to do so.


" g:vimprj#dRoots - DICTIONARY with info about $INDEXER_PROJECT_ROOTs
Expand All @@ -35,7 +40,7 @@
" [0, 1, 2, ...] - LIST KEY. At this moment, only 0 is available
" ["file"] - key for s:dProjFilesParsed
" ["name"] - name of project
"
"


if v:version < 700
Expand Down Expand Up @@ -117,7 +122,7 @@ endif

" all dependencies is ok

let g:vimprj#version = 110
let g:vimprj#version = 111
let g:vimprj#loaded = 1

let s:boolInitialized = 0
Expand Down Expand Up @@ -158,8 +163,6 @@ function! vimprj#applyVimprjSettings(sVimprjKey)

call <SID>ChangeDirToVimprj(g:vimprj#dRoots[ a:sVimprjKey ]["cd_path"])

" для каждого проекта, в который входит файл, добавляем tags и path

call <SID>_AddToDebugLog(s:DEB_LEVEL__ALL, 'function end: __ApplyVimprjSettings__', {})
endfunction

Expand All @@ -168,7 +171,6 @@ function! vimprj#getVimprjKeyOfFile(iFileNum)
endfunction


" задаем пустые массивы с данными
function! vimprj#init()
if s:boolInitialized
return
Expand All @@ -190,15 +192,15 @@ function! vimprj#init()
endif

if !exists('g:vimprj_changeCurDirIfVimprjFound')
let g:vimprj_changeCurDirIfVimprjFound = 1
let g:vimprj_changeCurDirIfVimprjFound = 0
endif

if !exists('g:vimprj_sourceScriptsOnlyIfVimprjChanged')
let g:vimprj_sourceScriptsOnlyIfVimprjChanged = 1
endif


" задаем пустые массивы с данными
" initialize empty arrays
let g:vimprj#dRoots = {}
let g:vimprj#dFiles = {}
let g:vimprj#iCurFileNum = -1
Expand All @@ -224,20 +226,21 @@ function! vimprj#init()
\ 'ApplySettingsForFile' : {},
\ }

" указываем обработчик открытия нового файла: OnFileOpen
" OnFileOpen handler
augroup Vimprj_LoadFile
autocmd! Vimprj_LoadFile BufReadPost
autocmd! Vimprj_LoadFile BufNewFile
autocmd Vimprj_LoadFile BufReadPost * call <SID>OnFileOpen(bufnr(expand('<afile>')))
autocmd Vimprj_LoadFile BufNewFile * call <SID>OnFileOpen(bufnr(expand('<afile>')))
augroup END

" указываем обработчик входа в другой буфер: OnBufEnter
" OnBufEnter handler
augroup Vimprj_BufEnter
autocmd! Vimprj_BufEnter BufEnter
autocmd Vimprj_BufEnter BufEnter * call <SID>OnBufEnter(bufnr(expand('<afile>')))
augroup END

" OnBufSave handler
augroup Vimprj_BufWritePost
autocmd! Vimprj_BufWritePost BufWritePost
autocmd Vimprj_BufWritePost BufWritePost * call <SID>OnBufSave()
Expand All @@ -253,13 +256,11 @@ endfunction



" Парсит директорию проекта (директорию, в которой лежит директория .vimprj)
" Добавляет новый vimprj_root
" Parse the directory folder (the directory which contains `.vimprj` dir)
"
" @param lProjectRoots list with paths to projs dir
" (this is list instead of string because we need to support nested
" (this is a list instead of a string because we need to support nested
" projs)

function! <SID>ParseNewVimprjRoot(lProjectRoots)

let l:sLastVimprjFolder = ""
Expand Down Expand Up @@ -314,7 +315,7 @@ function! <SID>CreateDefaultProjectIfNotAlready()
\ 'sVimprjDirName' : ''
\ })

" создаем дефолтный "проект"
" create default "project"
call <SID>ChangeDirToVimprj(substitute(s:sStartCwd, ' ', '\\ ', 'g'))
call <SID>AddNewVimprjRoot("default", [], s:sStartCwd)
"call <SID>TakeAccountOfFile(0, 'default')
Expand Down Expand Up @@ -416,10 +417,10 @@ function! <SID>ExecHooks(sHooksgroup, dParams)
endfunction


" добавляет новый vimprj root, заполняет его текущими параметрами
" Add new vimprj root, fill it with current params
"
" ВНИМАНИЕ! "текущими" параметрами - это означает, что на момент вызова
" этого метода все .vim файлы из .vimprj уже должны быть выполнены!
" NOTE: "current params" means that when this function is called, all
" .vimprj/*.vim files should be sourced already!
function! <SID>AddNewVimprjRoot(sVimprjKey, lPaths, sCdPath)

if !has_key(g:vimprj#dRoots, a:sVimprjKey)
Expand Down Expand Up @@ -462,13 +463,13 @@ endfunction



" returns if we should to skip this buffer ('skipped' buffers are not handled
" by vimprj plugin at all)
" returns if we should skip this buffer ('skipped' buffers are not handled by
" vimprj plugin at all)
function! <SID>NeedSkipBuffer(iFileNum)

" COMMENTED!! file should be readable
" commented because of we should parse creation of new files,
" which isn't readable at BufNewFile.
" commented because we should parse creation of new files, which isn't
" readable at BufNewFile.
"
"if !filereadable(bufname(a:iFileNum))
"return 1
Expand Down Expand Up @@ -536,10 +537,10 @@ function! <SID>SourceVimprjFiles(sPath)
endfunction

function! <SID>ChangeDirToVimprj(sPath)
" переключаем рабочую директорию
" switch working dir
if (g:vimprj_changeCurDirIfVimprjFound)
exec "cd ".a:sPath
" ???? иначе не работает
" ???? very strange, but sometimes it doesn't work otherwise
exec "cd ".a:sPath
endif
endfunction
Expand All @@ -558,6 +559,9 @@ function! <SID>GetVimprjRootOfFile(iFileNum)
let l:sCurPath = l:sDirname
while (l:i < g:vimprj_recurseUpCount)
let l:sTmp = simplify(l:sCurPath.'/'.g:vimprj_dirNameForSearch)
if has("win32unix")
let l:sTmp = substitute(l:sTmp, '//', '/', 'g')
endif
if isdirectory(l:sTmp) || filereadable(l:sTmp)

" directory or file with needed name found
Expand All @@ -568,9 +572,12 @@ function! <SID>GetVimprjRootOfFile(iFileNum)
"break

endif

" get upper path
let l:sNextCurPath = simplify(l:sCurPath.'/..')
if has("win32unix")
let l:sNextCurPath = substitute(l:sNextCurPath, '//', '/', 'g')
endif
if (l:sNextCurPath == l:sCurPath)
" we reached root of filesystem. break now
break
Expand All @@ -583,16 +590,15 @@ function! <SID>GetVimprjRootOfFile(iFileNum)
if !empty(l:sProjectRoot)

" .vimprj directory or file is found.
" проверяем, не открыли ли мы файл из директории .vimprj (или, если это
" файл, то не открыли ли мы этот файл)
"
" Let's check if the file in question is a file from .vimprj
" directory

let l:sPathToDirNameForSearch = l:sProjectRoot.'/'.g:vimprj_dirNameForSearch
"let l:iPathToDNFSlen = strlen(l:sPathToDirNameForSearch)

"if strpart(l:sFilename, 0, l:iPathToDNFSlen) == l:sPathToDirNameForSearch " открытый файл - из директории .vimprj, так что для него

if dfrank#util#IsFileInSubdir(l:sFilename, l:sPathToDirNameForSearch)
" НЕ будем применять настройки из этой директории.
" Yes, we opened a file from .vimprj dir. So, we're not going to
" apply settings from .vimprj dir.
let l:sProjectRoot = ''
endif

Expand Down Expand Up @@ -673,7 +679,7 @@ function! <SID>OnFileOpen(iFileNum)
\ 'sVimprjDirName' : ''
\ })

" создаем дефолтный "проект"
" create default "project"
call <SID>ChangeDirToVimprj(substitute(s:sStartCwd, ' ', '\\ ', 'g'))
call <SID>AddNewVimprjRoot("default", [], s:sStartCwd)
"call <SID>TakeAccountOfFile(0, 'default')
Expand All @@ -695,6 +701,8 @@ function! <SID>OnFileOpen(iFileNum)

call <SID>SetCurrentFile(l:iFileNum)

let $VIMPRJ_PROJECT_ROOT = g:vimprj#dRoots[ l:sVimprjKey ].proj_root

call <SID>ExecHooks('OnFileOpen', {
\ 'iFileNum' : l:iFileNum,
\ })
Expand Down
3 changes: 3 additions & 0 deletions doc/auxiliary/cur_changes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

*) Refactor. Added new dependency: script-library DfrankUtil (vimscript #3884)

104 changes: 104 additions & 0 deletions doc/auxiliary/vim_org.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
Please note: there's dependency DfrankUtil (vimscript #3884)

* * *

Many times i faced a problem. That's the story:

I'd like to use 3-space indent in my projects.
No tabs, especially three spaces. One day i need to work on another project
written by someone else, and there's 4 spaces indent. Or maybe tabs.

I need to work on this project too, and i need to leave existing formatting
options.

I strongly dislike that i have to switch these options by hand: &shiftwidth,
&tabstop, &expandtab.

And then i decided to write this plugin to make my life easier.

Using this plugin is quite easy. You need to create in the root directory of
your project new directory ".vimprj" and put any number of files "*.vim" in it.
Every time you open new file in Vim, plugin looks for ".vimprj" directory up
by tree, and if it is found, then all *.vim files from it will be sourced.

OR: you can just put file ".vimprj", then this file will be sourced.
Name ".vimprj" can be changed by editing option g:vimprj_dirNameForSearch .

Usually for my own projects i create just one file .vimprj/my.vim with the
following contents: >

let &tabstop = 3
let &shiftwidth = 3
set expandtab

(my .vimprj directory also contain tags and other project-specific files,
so, i'd prefer to use directory instead of just one file.)

and for other projects i create file with appropriate options.

Now, when i open file from my project, file my.vim is sourced. When i open
file from some another project, its own file with options is sourced too. Of
course, when i switch buffer, this files are sourced too if project is
changed.

So, i always have correct options, and simultaneus work on several projects
with different formatting options is not painful anymore for me.

And, of course, you can use it not only to manage formatting options. For
instance, in some my projects i use mapping <F9> to open project window
(from plugin project.tar.gz), but for some java projects i use Eclim, and <F9>
should be mapped to another command (to open Eclim's project).
All these options can also be stated in .vimprj dir.

There is an issue: as i already said, i would like to use shiftwidth=3. Say,
i open some file from my project with shiftwidth=3. All ok here. Then i open
file from another project with shiftwidth=4. All is still ok. Then i open some
file not from any project (there's no ".vimprj" directory). Of course, i would
like to use my favorite options (e.g. shiftwidth=3), but now shiftwidth is 4,
because it was changed when i opened last project. It leads us to the fact
that we should define default options. But here let me make a digression.

Plugin provides some hooks for any other plugin. For instance, plugin
Indexer (since version 4.0) uses these hooks to provide correct behavior
when user works on different project simultaneusly.

Right now i have not documented yet all these hooks, because of lack of
free time, but i just tell you about one hook, to let you specify your
default options.

This is a hook "SetDefaultOptions". Now i show you an example. Please insert
in your .vimrc the following, just specify your own default options instead of
mine ones: >

function! <SID>SetMainDefaults()

" your default options goes here!
set tabstop=3
set shiftwidth=3
set expandtab

endfunction

call <SID>SetMainDefaults()

" initialize vimprj plugin
call vimprj#init()

" define a hook
function! g:vimprj#dHooks['SetDefaultOptions']['main_options'](dParams)
call <SID>SetMainDefaults()
endfunction

Now vimprj plugin will call your function SetMainDefaults just before sourcing
all *.vim files from .vimprj directory, and when you open file not from any
project.

If you are interesting about another hooks, you can just look how is they used
in Indexer plugin (since version 4.0). I hope one day i will write all this here. =)

Please NOTE: by default this plugin will change your current dir
to project's dir if ".vimprj" is found.
More detailed read in help: type :help vimprj-options , look for an option
g:vimprj_changeCurDirIfVimprjFound

Development repository (Mercurial) : http://hg.dfrank.ru/vim/bundle/vimprj
Loading