/* * 編集中の Ruby スクリプトを実行する秀丸マクロ(RSpec に対応) * * Ver.3.4 (2017-07-24) * * 機能 * - このマクロを実行すると、編集中の内容が Ruby スクリプトとして実行されます。 * - マクロの先頭で、使用する Ruby インタプリタとコマンドラインオプションを指定できます。 * - 出力先を以下から選択できます。 * - コマンドプロンプト * - アウトプット枠 * - 新しいウィンドウ * - Ruby スクリプトの終了まで待たなくても実行中に出力が逐次表示されていきます。 * (アウトプット枠と新しいウィンドウへの出力時は、行単位でバッファリングされます。) * - 無限ループなどでスクリプトが終了しない場合、簡単に強制終了させることが出来ます。 * - (無題) のまま実行することが出来ます。 * - コマンドプロンプトへの出力時は、スクリプトの終了後、キー入力を待ってからコマンドプロンプトを閉じます。 * - スクリプトの文字コードや出力先によらず、結果はほとんどの場合文字化けせずに表示されます。 * * - ファイル名が "_spec.rb" で終わっている場合は RSpec で実行します。 * - この場合も、コマンドライン引数と出力先をマクロの先頭で設定できます。 * - 出力先がアウトプット枠の場合には、実行結果に応じてアウトプット枠の文字と背景の色を一時的に変更します。 * * 注意 * このマクロの初回起動時、および、コマンドプロンプトへの初回出力時に、このマクロが内部的に使用する * Rubyスクリプトとバッチファイルが、このマクロと同じフォルダにそれぞれ作成されます。 * * Yasunori Miyamoto * http://tipszone.jp/20121203_exec_ruby_mac/ * mailto: nori@tipszone.jp */ // Rubyインタプリタ $ruby = "ruby"; // コマンドラインオプション $opt = "-W"; // 出力先 // 0: コマンドプロンプト // 1: アウトプット枠 // 2: 新しいウィンドウ #output = 1; // RSpec 用の設定 $rspec = "rspec.bat"; $rspec_opt = "-c"; #rspec_output = 1; #exec_rspec = rightstr(basename, 8) == "_spec.rb"; // アウトプット枠に結果を表示するときの文字と背景の色 // RRGGBB 形式で指定 $success_color = "000000"; $success_background_color = "D0FFD6"; $failure_color = "000000"; $failure_background_color = "FBE0FB"; // 空のファイルを保存しようとしたとき、保存するかどうかの // 確認ダイアログが表示されないようにする。 seterrormode 3, 0x00000200; // Rubyスクリプトにロードするファイルを作成 $required_file = currentmacrodirectory + "\\exec_ruby_required_v3.rb"; if (existfile($required_file) == no) { runsync2 "\"" + $ruby + "\" -x -- \"" + currentmacrofilename + "\" \"" + $required_file + "\""; if (existfile($required_file) == no) endmacro; } // (無題)の場合にもそのまま実行できるようにする if (filetype == "new") { // 編集内容を取得 disabledraw; call SAVE_CURSOR; gofileend; $$current_contents = gettext(0, 0, x, y); ##encode = charset; call RESTORE_CURSOR; enabledraw screentopy; // ステルスモードのウィンドウを利用し、編集内容をファイルに保存 openfile "/h"; // 現在の編集内容を保存するテンポラリファイル $$temp_file = getenv("TMP") + "\\current_contents.rb"; setencode ##encode; insert $$current_contents; saveas $$temp_file; setactivehidemaru 1; closehidemaruforced 1; call EXEC, $$temp_file; } else if (leftstr(filetype, 1) == ".") { // 現在のタブが「grep結果」や「実行結果」でなければ、上書き保存してから実行 save; call EXEC, "%f"; } ##result = result; if (#exec_rspec) { if (##result == 0) { message "'" + $rspec + "' が見つかりません。"; endmacro; } if (#output == 1) { if (getresultex(9)) { call SET_OUTPUT_PANE_COLOR_TEMPORARILY, $failure_color, $failure_background_color; } else { call SET_OUTPUT_PANE_COLOR_TEMPORARILY, $success_color, $success_background_color; } } } endmacro; // カーソル位置と範囲選択の状態をグローバル変数 #_cursor_info に保存する。 SAVE_CURSOR: #_cursor_info[0] = selecting; #_cursor_info[1] = rectselecting; #_cursor_info[2] = selopenx; #_cursor_info[3] = selopeny; #_cursor_info[4] = x; #_cursor_info[5] = y; return; // #_cursor_info の値から、カーソル位置と範囲選択の状態を復元する。 RESTORE_CURSOR: ##selecting = #_cursor_info[0]; ##rectselecting = #_cursor_info[1]; ##selopenx = #_cursor_info[2]; ##selopeny = #_cursor_info[3]; ##x = #_cursor_info[4]; ##y = #_cursor_info[5]; escape; if (##selecting) { moveto ##selopenx, ##selopeny; if (##rectselecting) beginrect; else beginsel; moveto ##x, ##y; endsel; } else { moveto ##x, ##y; } return; EXEC: $$cmd = $ruby; if (#exec_rspec) { $$cmd = $rspec; $opt = $rspec_opt; #output = #rspec_output; } if (#output == 0) { $$batch_file = currentmacrodirectory + "\\exec_ruby_v2.bat"; if (existfile($$batch_file) == no) { // Rubyスクリプトを起動するバッチファイルを作成 runsync2 "\"" + $ruby + "\" -x -- \"" + currentmacrofilename + "\" \"" + $$batch_file + "\""; if (existfile($$batch_file) == no) endmacro; } run "\"" + $$batch_file + "\" \"" + $$cmd + "\" \"" + $opt + "\" \"" + $required_file + "\" \"" + $$1 + "\""; } else { ##stdout = 4; if (#output == 1) { ##stdout = 7; call REFLECT; // アウトプット枠の文字・背景色をリセット } if (! #exec_rspec) { $$require = " -r \"" + $required_file + "\""; } runex "\"" + $$cmd + "\" " + $opt + $$require + " -- \"" + $$1 + "\"", #exec_rspec, //sync 0:async 1:sync 0, "", //stdin 0:none 1:auto 2:file 3:(reserve) 4:all 5:select ##stdout, "", //stdout 0:none 1:auto 2:file 3:add file 4:new 5:insert 6:replace 7:output frame 1, "", //stderr 0:none 1:=out 2:file 3:add file 4:new 5:insert 6:replace 7:output frame 1, "", //folder 0:none 1:current 2:specify 3:(reserve) 4:exe's folder 2, //show 0:auto 1:show 2:hide 0, //nodraw 0:draw 1:no draw 0; //unicode 0:ansi 2:utf-16 6:utf-8 } return result; // レジストリ変更を元にアウトプット枠の色を更新 REFLECT: loaddll "HmOutputPane.dll"; ##h = dllfunc("GetWindowHandle", hidemaruhandle(0)); ##_ = sendmessage(##h, 0x111, 1014, 0); freedll; return; // RRGGBB 形式の文字列を BBGGRR に変換する // Param: string RGB2BGR: return rightstr($$1, 2) + midstr($$1, 2, 2) + leftstr($$1, 2); // アウトプット枠の色を表すレジストリの設定値を作成する。 // (HKEY_CURRENT_USER\Software\Hidemaruo\Hidemaru\HmOutputPane の Color 属性の値) // Param: string 文字の色 ("RRGGBB" 形式) // Param: string 背景の色 ("RRGGBB" 形式) // Return: string レジストリに設定する値 MAKE_OUTPUT_PANE_COLOR_STRING: call RGB2BGR, $$1; $$rslt = "00" + $$return; call RGB2BGR, $$2; return $$rslt + "00" + $$return; // アウトプット枠の文字と背景の色を一時的に変更する。 // Param: string 文字の色 ("RRGGBB" 形式) // Param: string 背景の色 ("RRGGBB" 形式) SET_OUTPUT_PANE_COLOR_TEMPORARILY: openreg "CURRENTUSER", "Software\\Hidemaruo\\Hidemaru\\HmOutputPane"; $original_color = getregstr("Color"); call MAKE_OUTPUT_PANE_COLOR_STRING, $$1, $$2; writeregstr "Color", $$return; call REFLECT; writeregstr "Color", $original_color; closereg; return; /* #!ruby # coding: SJIS # Rubyスクリプトにロードするファイルを作成する。 # ARGV[0] : 作成するファイル名 begin File.write ARGV[0], (ARGV[0].end_with?('.rb') ? <<-'EOR' : <<-'EOB' # coding: SJIS require 'nkf' # バッファリング抑制 STDOUT.sync = true # 文字化けを防ぐため、文字コードを Shift-JIS に変換 def STDERR.write(str) super(str.to_s.encode(Encoding::SJIS)) end def STDOUT.write(str) super(str.to_s.encode(Encoding::SJIS)) end EOR @echo off set cmd="%~f$PATH:1" if %cmd% == "" set cmd=%1 call %cmd% %~2 -r %3 -- %4 echo. set /p =Press any key to exit . . . < nul pause > nul EOB ).gsub(/^\t\t/, '') rescue puts ARGV[0].end_with?('.rb') ? "Rubyスクリプトにロードするファイルを作成できませんでした。" : "Rubyスクリプトを起動するバッチファイルを作成できませんでした。" puts "\n#{$!.backtrace.shift}: #{$!} (#{$!.class})" $@.each{|s| puts "\tfrom " + s} `pause` end __END__ */