Skip to content

Instantly share code, notes, and snippets.

@suwatoh
Last active March 26, 2021 22:55
Show Gist options
  • Save suwatoh/6711c3c6c79873238eb13fc871763674 to your computer and use it in GitHub Desktop.
Save suwatoh/6711c3c6c79873238eb13fc871763674 to your computer and use it in GitHub Desktop.
;;; -*-coding: utf-8-with-signature; -*-
/*
AutoHotKey 1.1.27 以降必須
スクリプトをコンパイルしたファイルと同じ場所にStartEmacs.iniを作成。内容は次のとおり。
[path]
emacs=<runemacs.exeのパス>
複数起動を禁止する。既存のプロセスがあればアクティブにする。
引数はファイル名のみ受け付ける(+LINE:COLUMNなどのオプションには対応しない)。
ファイルのドラッグアンドドロップに対応する。
*/
;; 環境変数を無視する
#NoEnv
;; 既存のプロセスを終了する
#SingleInstance force
;; タスクトレイアイコンを非表示にする
#NoTrayIcon
;; Mouse系コマンドの処理方式(詳細はhttp://ahkwiki.net/SendMode)
SendMode Input
;; 作業ディレクトリをスクリプトのあるディレクトリパスとする
SetWorkingDir %A_ScriptDir%
;; iniファイル名を指定
ini_file = StartEmacs.ini
;; 起動後にファイルを送信するまでの待ち時間(ミリ秒)
delay = 1000
;;
;; Main
;;
;; iniファイルからファイルのパスを取得する
path_emacs := read_ini_file_path(ini_file)
;; 既存のプロセスをアクティブにする。起動していないなら起動
start_emacs(path_emacs)
;; 引数がある場合の処理
If (A_Args.length() > 0)
{
Process, Exist, emacs.exe
If (ErrorLevel = 0)
{
Sleep, delay
}
;; 引数はドラッグアンドドロップ方式で処理
For index, Arg in A_Args
{
If FIleExist(Arg)
{
;; WM_DROPFILES(0x233)をプロセスに送信
PostMessage, 0x233, HDrop(Arg), 0, , ahk_class Emacs
Sleep, 100
}
}
}
;;
;; Functions
;;
read_ini_file_path(ini_file)
{
Try
{
If not (FileExist(ini_file))
{
Throw "ini_file のパスが見つかりません`n" . "設定値:" . ini_file
}
IniRead, path_emacs, %ini_file%, path, emacs
If not (FileExist(path_emacs))
{
Throw "emacs起動用プログラムのパスが見つかりません`n" . "設定値:" . path_emacs
}
}
Catch e
{
MsgBox, ファイル指定のエラー`n%e%`nプログラムを終了します
ExitApp
}
return path_emacs
}
start_emacs(path_emacs)
{
hwnd := WinExist("ahk_class Emacs")
;; Emacs が既に起動している場合はアクティブ化
If (hwnd)
{
WinActivate, ahk_id %hwnd%
return
}
Run, % path_emacs
return
}
;; DROP構造体へのハンドルを返す
;; https://autohotkey.com/board/topic/41467-make-ahk-drop-files-into-other-applications/page-2
HDrop(fnames,x=0,y=0)
{
characterSize := 2
fns := RegExReplace(fnames,"\n$")
fns := RegExReplace(fns,"^\n")
hDrop := DllCall("GlobalAlloc", "UInt", 0x42, "UInt", 20+(StrLen(fns)*characterSize)+characterSize*2)
p := DllCall("GlobalLock", "UInt", hDrop)
NumPut(20, p+0) ; offset
NumPut(x, p+4) ; pt.x
NumPut(y, p+8) ; pt.y
NumPut(0, p+12) ; fNC
NumPut(1, p+16) ; fWide
p2 := p + 20
Loop, Parse, fns, `n, `r
{
DllCall("RtlMoveMemory", "UInt", p2, "Str", A_LoopField, "UInt", StrLen(A_LoopField)*characterSize)
p2 += StrLen(A_LoopField) * characterSize + characterSize
}
DllCall("GlobalUnlock", "UInt", hDrop)
return hDrop
}
;;; StartEmacs.ahk ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment