本插件属于ftplugin,并且依赖于
shellinsidevim.vim
本插件使得你可以直接在vim编译和运行带有包的java类,并且如果改类属于一个java项目,它会自动检测项目环境(不要求你一定位于项目文件夹下),并且生成正确的编译参数。这些参数包括:classpath、sourcepath、输出目录、参数文件。以上参数的意义请自行参考javac文档。
如果上述参数不够,你可以直接在命令模式输入:Javac ,后跟参数。注意,这个参数是对上述的补充,你不用在额外指定上述参数。并且,每次输入的参数都将自动保存,直到重启vim或者在Javac命令后输入“:”再接参数。
Java命令同上。
此脚本可以方便的修改,使得应用于其他的语言环境,比如flex。
此插件的功能比较简单,更强大的脚本还在测试中。
本插件使得你可以直接在vim编译和运行带有包的java类,并且如果改类属于一个java项目,它会自动检测项目环境(不要求你一定位于项目文件夹下),并且生成正确的编译参数。这些参数包括:classpath、sourcepath、输出目录、参数文件。以上参数的意义请自行参考javac文档。
如果上述参数不够,你可以直接在命令模式输入:Javac ,后跟参数。注意,这个参数是对上述的补充,你不用在额外指定上述参数。并且,每次输入的参数都将自动保存,直到重启vim或者在Javac命令后输入“:”再接参数。
Java命令同上。
此脚本可以方便的修改,使得应用于其他的语言环境,比如flex。
此插件的功能比较简单,更强大的脚本还在测试中。
- "Vimglobalpluginforrunningjavaproject
- "Version:2.0
- "Maintainer:WarGrey<yoshua@gmail.com>
- "Lastchange:2008Dec22
- "
-
-
- "Thisgroupsofvariablesareusedtogeneratingtheoptionsforcompiling
- lets:LibraryOption="-classpathLO"
- lets:SourcepathOption="-sourcepathSO"
- lets:DistinationOption="-dDO"
- lets:ArgumentFileOption="@AFO"
- lets:ExtraCompileOptions=""
- lets:ExtraExecuteOptions=""
-
-
- "Commandsforcompilingandexecuting
- lets:Compiler="javac"
- lets:Interpretor="java"
- lets:Executor=""
-
-
- "Thisgroupsofvariablesarereferencingtothefoldsthatreallyexistsbaseonthenextgroups
- lets:ProjectRoot=''
- lets:SourcePath=''
- lets:DistinationPath=''
- lets:LibraryPath=''
- lets:ArgumentFile=''
-
-
- "Thisgroupsofvariablesareusedtodetectingtheenvironment
- if!exists("g:Java_Sources")
- letg:Java_Sources="src:java_src"
- endif
- if!exists("g:Java_Distinations")
- letg:Java_Distinations="classes:java_classes"
- endif
- if!exists("g:Java_Libraries")
- letg:Java_Libraries="lib:java_lib"
- endif
- if!exists("g:Java_ArgumentFiles")
- letg:Java_ArgumentFiles="classpath.javac:argument.javac:source.javac"
- endif
-
-
- "Excommandwhichtake0ormore(upto20)parameters
- command!-complete=file-nargs=*Javaccalls:CLCompile(<f-args>)
- command!-complete=file-nargs=*Javacalls:CLExecute(<f-args>)
-
-
- "Mapkeystofunctioncalls
- map<unique><buffer><F5>:call<SID>Compile()<CR>
- map<unique><buffer><M-F5>:Javac
- map<unique><buffer><F6>:call<SID>Execute()<CR>
- map<unique><buffer><M-F6>:Java
- map<unique><buffer><F9>:call<SID>ToggleExecutor()<CR>
-
-
- imap<unique><buffer><F5><ESC><F5>
- imap<unique><buffer><M-F5><ESC><M-F5>
- imap<unique><buffer><F6><ESC><F6>
- imap<unique><buffer><M-F6><ESC><M-F6>
- imap<unique><buffer><F9><ESC><F9>
-
-
- function!s:InitEnvironment()
- ifs:IsJavaFile()==0|return0|endif
-
- letcprt=s:ProjectRoot
- lets:Executor=s:GetFullClassName()
- lettail='/'.substitute(s:Executor,'\.','/','g')
- letsrc=fnamemodify(bufname("%"),":p:r")
- if(match(src,tail)+strlen(tail))==strlen(src)
- letsrc=substitute(src,tail,'','g')
- else
- callg:EchoErrorMsg("Thisclasshasanincorrectpackage!")
- endif
- lets:ProjectRoot=src
-
- letisProject=0
- letsrcs=split(g:Java_Sources,":")
- letdists=split(g:Java_Distinations,":")
- whilesrc!=fnamemodify(src,":h")
- forsdinsrcs
- if(strlen(sd)+match(src,sd))==strlen(src)
- letroot=substitute(src,"/".sd,"","g")
- fordidindists
- letdist=fnamemodify(root."/".did,":p")
- let csrc=fnamemodify(root."/".sd,":p")
- ifisdirectory(dist) && isdirectory(sd)
- lets:ProjectRoot=root
- lets:DistinationPath=dist
- letisProject=1
- break
- endif
- endfor
- ifisProject==1|break|endif
- endif
- endfor
- ifisProject==1|break|endif
- letsrc=fnamemodify(src,":h")
- endwhile
-
- ifisProject==1
- lets:SourcePath=s:PreparePath(g:Java_Sources,0)
- lets:LibraryPath=s:PreparePath(g:Java_Libraries,0)
- lets:ArgumentFile=s:PreparePath(g:Java_ArgumentFiles,1)
- else
- lets:DistinationPath=s:ProjectRoot
- lets:LibraryPath=s:ProjectRoot
- endif
-
- ifcprt!=s:ProjectRoot
- callg:EchoMoreMsg("Theprojectrootis".s:ProjectRoot)
- ifcprt!=''
- callg:EchoWarningMsg("Thisprojectisnottheoriginalone!")
- endif
- endif
- endfunction
-
-
- function!s:ToggleExecutor()
- ifs:IsJavaFile()==0|return0|endif
- letcer=s:Executor
- calls:InitEnvironment()
- ifs:Executor==cer
- callg:EchoWarningMsg(s:Executor.'isnolongertheexecutor.')
- lets:Executor=""
- else
- callg:EchoMoreMsg(s:Executor.'issetastheexecutor.')
- letmain="\\s*\\(\\(public\\|static\\)\\s\\+\\)\\{2}void\\s\\+main(String"
- letpara1=main."\\(\\(\\s*\\(\\.\\{3}\\|[]\\)\\s*\\)\\w\\+\\s*\\))"
- letpara2=main."\\(\\s\\+\\w\\+\\s*[]\\s*\\))"
- ifsearch(para1)+search(para2)==0
- callg:EchoWarningMsg(s:Executor.'doesnothavethemainmethod!')
- endif
- endif
- endfunction
-
-
- function!s:Compile()
- wall
- echo"Allopenfilesaresaved."
- execute"compiler!".s:Compiler
-
- letargfiles=s:GetArgumentFiles(s:ArgumentFile)
- ifstrlen(argfiles)>0
- callg:ExecuteCommand(&makeprg,argfiles,";")
- else
- calls:InitEnvironment()
- letdist=substitute(s:DistinationOption,"DO",s:DistinationPath,"")
- letcp=substitute(s:LibraryOption,"LO",s:GetClassPath(s:LibraryPath,s:DistinationPath),"")
- letsp=substitute(s:SourcepathOption,"SO",s:SourcePath,"")
- callg:ExecuteCommand(&makeprg,dist,cp,sp,s:ExtraCompileOptions,bufname("%"),";")
- endif
- endfunction
-
-
- function!s:Execute()
- ifs:Executor==""|calls:ToggleExecutor()|endif
-
- letcp=substitute(s:LibraryOption,"LO",s:GetClassPath(s:LibraryPath,s:DistinationPath),"g")
- if!filereadable(g:VIM_STD_IN_FILE)
- callwritefile([""],g:VIM_STD_IN_FILE)
- endif
- callg:ExecuteCommand(">",s:Interpretor,cp,s:ExtraExecuteOptions,s:Executor)
- endfunction
-
-
- function!s:CLCompile(...)
- letindex=1
- leteoption=""
- whileindex<=a:0
- execute'leteoption=eoption."".g:Trim(a:'.index.')'
- letindex=index+1
- endwhile
- ifmatch(eoption,"^:")==-1
- lets:ExtraCompileOptions=s:ExtraCompileOptions.eoption
- else
- lets:ExtraCompileOptions=strpart(eoption,2)
- endif
- calls:Compile()
- endfunction
-
-
- function!s:CLExecute(...)
- letindex=1
- leteoption=""
- whileindex<=a:0
- execute'leteoption=eoption."".g:Trim(a:'.index.')'
- letindex=index+1
- endwhile
- ifmatch(eoption,"^:")==-1
- lets:ExtraExecuteOptions=s:ExtraExecuteOptions.eoption
- else
- lets:ExtraExecuteOptions=strpart(eoption,2)
- endif
- calls:Execute()
- endfunction
- "Otherusefulfunctions
- function!s:PreparePath(srcs,isfile)
- letresult=""
- forsrcinsplit(a:srcs,":")
- letsource=s:ProjectRoot."/".src
- if(a:isfile==0&&isdirectory(source))||(a:isfile==1&&filereadable(source))
- letresult=s:AddPathTo(result,source,":","after")
- endif
- endfor
- returnresult
- endfunction
-
- function!s:AddPathTo(src,dir,sep,pos)
- letpath=s:CleanPath(a:dir)
- ifa:src==""
- returnpath
- elseifmatch(a:src,path)>=0
- returns:src
- else
- ifa:pos=='after'
- returna:src.a:sep.path
- else
- returnpath.a:sep.a:src
- endif
- endif
- endfunction
-
- function!s:CleanPath(path)
- letpaths=split(fnamemodify(a:path,":p"),"/")
- letresult=[]
- letn=len(paths)-1
- letskip=0
- whilen>=0
- ifpaths[n]==".."
- letskip=skip+1
- elseifskip==0
- callinsert(result,paths[n],0)
- else
- letskip=skip-1
- endif
- letn=n-1
- endwhile
- letpath="/".join(result,"/")
- ifmatch(path,"")>0
- letpath='"'.path.'"'
- endif
- returnpath
- endfunction
-
- function!s:IsJavaFile()
- letfilename=bufname("%")
- iffnamemodify(filename,":e")!="java"
- calls:EchoWarningMsg(filename."isnottheexpectone!")
- return0
- endif
- return1
- endfunction
-
- function!s:GetPackage()
- letcur=getpos(".")
- letpackage=""
- callsetpos(".",[0,1,1,0])
- letline=search('package','')
- ifline>0
- try
- letpackage=split(split(getline(line))[1],';')[0]
- catch/.*/
- callg:EchoErrorMsg("Thepackagedefinitionmayincorrect!")
- endtry
- endif
- callsetpos(".",cur)
- returnpackage
- endfunction
-
- function!s:GetFullClassName()
- ifs:IsJavaFile()==0|return""|endif
- letpackage=s:GetPackage()
- letclass=fnamemodify(bufname("%"),":t:r")
- ifstrlen(package)>0
- returnpackage.'.'.class
- else
- returnclass
- endif
- endfunction
-
- function!s:GetClassPath(libraries,distination)
- letclasspath=substitute($CLASSPATH,"\\.\\/:","","g")
- letlibffixes=['jar','JAR','zip','ZIP']
- forlibinsplit(a:libraries,":")
- forsuffixinlibffixes
- letfindlib=substitute(glob(lib."/*".suffix),"\n",":","g")
- ifstrlen(findlib)>0
- letclasspath=s:AddPathTo(classpath,findlib,":","before")
- endif
- endfor
- endfor
- returns:AddPathTo(classpath,a:distination,":","before")
- endfunction
-
- function!s:GetArgumentFiles(ArgumentFile)
- letargfiles=""
- forafinsplit(a:ArgumentFile,":")
- letargfiles=argfiles."".substitute(s:ArgumentFileOption,"AFO",af,"g")
- endfor
- returnargfiles
- endfunction