groovy.vim(ftplugin)はGROOVY_HOMEを設定しないと使えない。なので直してみた。

多分quickrun.vimを使っている人の方が多いと思うので、需要は無いだろうけど書いておく。

vimでGroovyのコードを書く為のプラグインとしてGroovyの公式サイトでも触れられている「groovy.vim : Runs or compiles Groovy scripts.」というものがある*1

最近のvimはデフォルトでGroovyのシンタックスハイライトに対応している一方で自動インデントの類は無効のままなのだけど、このgroovy.vimを入れると自動インデントが利くようになる(不完全ではあるけど)。

ただ惜しいことに、編集中のスクリプトファイルを実行したりコンパイルしたりする機能があるのだけど、環境変数GROOVY_HOMEを設定していないと使えない。最近はGROOVY_HOMEを設定しないのが定説なので、非常に残念な感じだ。

しかしここで諦めるのも嫌なので、「GROOVY_HOMEが未定義 == GROOVY_HOME/binに相当する場所を環境変数PATHに通してある」と決め打ちして直してみることにした(GROOVY_HOMEが定義されている場合はそれを使う)。

ついでに編集中のスクリプトのファイル名にスペースが含まれていると残念な結果になることにも気づいたので、それも直してみた。

という訳でパッチ。ライセンスは……元コードに準じる、で良いのかなあ? 実は元のコードのライセンスが分からないのだけど。

*** groovy.vim	2012-07-27 19:35:42.859375000 +0900
--- groovy.vim.new	2012-07-28 00:48:34.140625000 +0900
***************
*** 14,23 ****
  "
  " Date Created:	April 20, 2008
  "
! " Version:			0.1.2
  "
  " Modification History:
  " 
  " 						April 24, 2008: Properly reset modified indent when leaving 
  " 						buffer.
  "
--- 14,28 ----
  "
  " Date Created:	April 20, 2008
  "
! " Version:			0.1.2a
  "
  " Modification History:
  " 
+ " 						July 28, 2012: <eel3 @ TRASH BOX> Fixed to be enabled to runs or
+ " 						compiles scripts:
+ " 						- if $GROOVY_HOME is not defined.
+ " 						- its filename containing spaces.
+ " 
  " 						April 24, 2008: Properly reset modified indent when leaving 
  " 						buffer.
  "
***************
*** 49,64 ****
  
  " Script variables
  "
! " Replace Windows backslashes, that would be swallowed in Cygwin, with slashes
! let s:GROOVY_HOME = substitute($GROOVY_HOME, "\\", "/", "g") 
! let s:GROOVY_PATH = s:GROOVY_HOME . "/bin/groovy"
! let s:GROOVYC_PATH = s:GROOVY_HOME . "/bin/groovyc"
  let s:CLASSPATH_PROMPT = "Specify classpath, or hit Enter for none: "
  let s:ARG_PROMPT = "Specify arg(s), or hit Enter for none: "
  " Buffer variables
  let b:classpath = ""
  let b:args = ""
  
  " Prompt for classpath/args and run 
  if !exists("<SID>RunPrompt()")
  	function! <SID>RunPrompt()
--- 54,90 ----
  
  " Script variables
  "
! if $GROOVY_HOME == ""
! 	" XXX Assume that $GROOVY_HOME/bin is in $PATH
! 	let s:GROOVY_PATH = "groovy"
! 	let s:GROOVYC_PATH = "groovyc"
! else
! 	" Replace Windows backslashes, that would be swallowed in Cygwin, with slashes
! 	let s:GROOVY_HOME = substitute($GROOVY_HOME, "\\", "/", "g") 
! 	let s:GROOVY_PATH = s:GROOVY_HOME . "/bin/groovy"
! 	let s:GROOVYC_PATH = s:GROOVY_HOME . "/bin/groovyc"
! 	if has("win32") || has("win64")
! 		let s:GROOVY_PATH = '"' . s:GROOVY_PATH . '"'
! 		let s:GROOVYC_PATH = '"' . s:GROOVYC_PATH . '"'
! 	endif
! endif
  let s:CLASSPATH_PROMPT = "Specify classpath, or hit Enter for none: "
  let s:ARG_PROMPT = "Specify arg(s), or hit Enter for none: "
  " Buffer variables
  let b:classpath = ""
  let b:args = ""
  
+ " Helper function
+ if has("win32") || has("win64")
+ 	function! s:currentfile()
+ 		return &shellslash ? '"' . expand("%") . '"' : shellescape(expand("%"))
+ 	endfunction
+ else
+ 	function! s:currentfile()
+ 		return shellescape(expand("%"))
+ 	endfunction
+ endif
+ 
  " Prompt for classpath/args and run 
  if !exists("<SID>RunPrompt()")
  	function! <SID>RunPrompt()
***************
*** 81,97 ****
  			endif
  		endif
  		if b:classpath == ""
! 			if has("win32") || has("win64")
! 				execute '!"' . s:GROOVY_PATH . '" ' . expand('%') . ' ' . b:args			
! 			else
! 				execute "!" . s:GROOVY_PATH . " " . expand("%") . " " . b:args			
! 			endif
  		else
! 			if has("win32") || has("win64")
! 				execute '!"' . s:GROOVY_PATH . '" -cp ' . b:classpath  . ' ' . expand('%') . ' ' . b:args			
! 			else
! 				execute "!" . s:GROOVY_PATH . " -cp " . b:classpath  . " " . expand("%") . " " . b:args			
! 			endif
  		endif
  		silent cd -
  	endfunction
--- 107,115 ----
  			endif
  		endif
  		if b:classpath == ""
! 			execute "!" . s:GROOVY_PATH s:currentfile() b:args
  		else
! 			execute "!" . s:GROOVY_PATH "-cp" b:classpath s:currentfile() b:args
  		endif
  		silent cd -
  	endfunction
***************
*** 111,127 ****
  			endif
  		endif
  		if b:classpath == ""
! 			if has("win32") || has("win64")
! 				execute '!"' . s:GROOVYC_PATH . '" ' . expand('%')
! 			else
! 				execute "!" . s:GROOVYC_PATH . " " . expand("%")
! 			endif
  		else
! 			if has("win32") || has("win64")
! 				execute '!"' . s:GROOVYC_PATH . '" -cp ' . b:classpath  . ' ' . expand('%')
! 			else
! 				execute "!" . s:GROOVYC_PATH . " -cp " . b:classpath  . " " . expand("%")
! 			endif
  		endif
  		silent cd -
  	endfunction
--- 129,137 ----
  			endif
  		endif
  		if b:classpath == ""
! 			execute "!" . s:GROOVYC_PATH s:currentfile()
  		else
! 			execute "!" . s:GROOVYC_PATH "-cp" b:classpath s:currentfile()
  		endif
  		silent cd -
  	endfunction
***************
*** 133,149 ****
  		update
  		silent cd %:p:h
  		if b:classpath == ""
! 			if has("win32") || has("win64")
! 				execute '!"' . s:GROOVY_PATH . '" ' . expand("%") . ' ' . b:args			
! 			else
! 				execute "!" . s:GROOVY_PATH . " " . expand("%") . " " . b:args			
! 			endif
  		else
! 			if has("win32") || has("win64")
! 				execute '!"' . s:GROOVY_PATH . '" -cp ' . b:classpath  . ' ' . expand('%') . ' ' . b:args			
! 			else
! 				execute "!" . s:GROOVY_PATH . " -cp " . b:classpath  . " " . expand("%") . " " . b:args			
! 			endif	
  		endif
  		silent cd -
  	endfunction
--- 143,151 ----
  		update
  		silent cd %:p:h
  		if b:classpath == ""
! 			execute "!" . s:GROOVY_PATH s:currentfile() b:args
  		else
! 			execute "!" . s:GROOVY_PATH "-cp" b:classpath s:currentfile() b:args
  		endif
  		silent cd -
  	endfunction
***************
*** 155,171 ****
  		update
  		silent cd %:p:h
  		if b:classpath == ""
! 			if has("win32") || has("win64")
! 				execute '!"' . s:GROOVYC_PATH . '" ' . expand('%')
! 			else
! 				execute "!" . s:GROOVYC_PATH . " " . expand("%")
! 			endif
  		else
! 			if has("win32") || has("win64")
! 				execute '!"' . s:GROOVYC_PATH . '" -cp ' . b:classpath  . ' ' . expand('%')
! 			else
! 				execute "!" . s:GROOVYC_PATH . " -cp " . b:classpath  . " " . expand("%")
! 			endif
  		endif
  		silent cd -
  	endfunction
--- 157,165 ----
  		update
  		silent cd %:p:h
  		if b:classpath == ""
! 			execute "!" . s:GROOVYC_PATH s:currentfile()
  		else
! 			execute "!" . s:GROOVYC_PATH "-cp" b:classpath s:currentfile()
  		endif
  		silent cd -
  	endfunction

直していて気づいたのだけど、インデント周りの設定って本来ならindent/groovy.vimに分けて記述するべきではないだろうか? quickrun.vimを使う人の場合、自動インデントは欲しいけどスクリプトの実行機能は不要な訳で、groovy.vimの全機能だとオーバースペックというか機能の重複があるというか。

*1:もう一つsyntaxの方も掲載されているけど、こちらは最近のvimなら同梱されているので入れる必要はない。