jump to navigation

VimからAutoHotkeyリファレンス検索

2009-11-01 13:06 Posted by
nase
in : プログラミング
red_plant.jpg

AutoHotkeyでウィンドウリサイズ他のときにリファレンス検索が欲しくなったのでvimrcへのruby埋め込みで実装してみました。chm形式ヘルプもahkスクリプト経由で検索できます。

対応するhtmlファイルを開いたり、chmから検索しているだけなので、他の用途でも応用が効くかも知れません。

日本語AutoHotkeyリファレンスの検索

日本語リファレンスはAutoHotkeyを流行らせるページのダウンロード版リファレンスを使います。vimrcに埋め込んだrubyスクリプトに引数を渡す方法が分からなかったんですが、どうやらrubyの関数を作って:rubyで呼び出せばvim側から引数を渡せるようです。

"AutoHotkey日本語リファレンスを検索
function! SearchAHKRefJa()
ruby << EOF
def searchAHKRefJa(cword)
  if cword != "" then
    found = false
    browser_path = 'C:/path/to/chrome.exe'
    Dir.glob("D:/path/to/AutoHotkey_ref_ja/commands/*.html") do |f|
      if /^_{0,2}#{cword}\.html$/i =~ File.basename(f) then
        VIM.command(%Q{silent !start "#{browser_path}" --enable-user-scripts "#{f}"})
        found = true
        break
      end
    end
    unless found then
      puts "「#{cword}」のドキュメントは見つかりませんでした"
    end
  end
end
EOF
execute ":ruby searchAHKRefJa('" . expand('<cword>') . "')"
endfunction
au BufRead *.ahk nnoremap <silent> <buffer> K :call SearchAHKRefJa()<CR>

これでコマンド名上でShift-kすると対応するhtmlファイルがブラウザ(この例だとchrome)で開きます。

英語AutoHotkeyリファレンスの検索

AutoHotkeyにはIntelliSense.ahkというおまけスクリプトが付いており、これを使うと普通のエディタでインテリセンスやchmヘルプ検索ができたりします。このchmヘルプ検索部分だけを抜き出してコマンドライン引数から実行できるようにしてみます。

cword = %1%
ahk_help_file = %A_ProgramFiles%\AutoHotkey\AutoHotkey.chm

SetTitleMatchMode, 1
IfWinNotExist, AutoHotkey Help
{
    IfNotExist, %ahk_help_file%
    {
        MsgBox, Could not find the help file: %ahk_help_file%.
            return
    }
    Run, %ahk_help_file%
        WinWait, AutoHotkey Help
}

WinActivate
WinWaitActive
Send, !n{home}+{end}%cword%{enter}
return

vimrc側で以下のように設定して呼び出します。

function! SearchAHKRefEn()
    let cword = expand('<cword>')
    if (cword != "")
        silent execute ':! ' . $HOME . '\path\to\SearchAHKRefEn.ahk ' . cword
    endif
endfunction
au BufRead *.ahk nnoremap <silent> <buffer> <C-K> :call SearchAHKRefEn()<CR>

Ctrl-Shift-kするとchmが起動して該当キーワードが検索できます。

Comments»

no comments yet


*Comments and trackbacks will appear after it is approved by the administrator.