如何写插件 - kawvin/VimDesktop2 GitHub Wiki

如何写插件

一、内置函数说明

函数 参数 说明
ModeChange modeName 切换指定模式
SendKeyInput Vimd.ini内置时:
String/Integer/Float

外置时:
Array/String/Integer/Float
发送指定热键,在外置插件中,可使用Array,可指定delay,Array最后一项为数字,同时前面加“|”
如:[ "{F5}", "{F2}" ,"|500" ]
key| 键值序列 Vimd.ini内置使用,发送按键,也可使用SendKeyInput
如:key|{right}{down}
run| 命令 运行命令
如:run|D:\test\test.exe
dir| 命令 TC(如有)或资源管理器打开目录
如:dir|D:\test
tccmd| 命令 运行TC(如有)命令
如:tccmd|cm_CopyFullNamesToClip
wshkey| 命令 运行WScript.Shell的SendKeys
如:wshkey|我去过才怪

二、Vimd.ini内置【全局插件】

  1. 需设置窗体信息,直接在[global],写入以下键值;
    • default_Mode:设置默认模式
    • enabled:是否启用
  2. 设置热键,见 三、Vimd.ini内置【程序插件】设置热键

三、Vimd.ini内置【程序插件】

  1. 设置窗体信息,设置新的section,如[记事本],写入以下键值;
    • set_class:设置窗体class
    • set_file:设置窗体进程名
    • set_max_count:设置最大重复执行次数
    • set_time_out:设置超时时间
    • default_Mode:设置默认模式
    • enabled:是否启用
    • enable_show_info:设置显示提示
  2. 设置热键
  • 支持带参数,函数名称和参数之前以"||"分隔,如<c-u> = MsgBoxTest||aaaaa||bbb[=normal]
  • 参数可以0或多个,以"||"分隔,仅支持 String/Integer/Float,不支持Boolean/Array/Map/Object
  • 支持指定运行次数,在函数名称后添加<次数>,如运行3次,如<c-u> = MsgBoxTest<3>||aaaaa||bbb[=normal]
  • 以下为示例
    • VIM HotKey=FunctionName[=modeName]
    • VIM HotKey=FunctionName||Param1||Param2||Param3||...[=modeName]
    • VIM HotKey=FunctionName**<n>**||Param1||Param2||Param3||...[=modeName]
    • VIM热键=函数名称[=模式名称]
    • VIM热键=函数名称||参数1||参数2||参数3||...[=模式名称]
    • VIM热键=函数名称<n>||参数1||参数2||参数3||...[=模式名称]
  • 如:
    • <c-y> = MsgBoxTest||1111[=normal]
    • <c-u> = MsgBoxTest<2>||aaaaa[=normal]
    • h = key|{left}[=normal]
    • j = key|{down}[=normal]
    • k = key|{up}[=normal]
    • l = key|{right}[=normal]
    • <c-j> = run|notepad.exe[=VIM]
    • <c-k> = key|^o[=VIM]

以下为示例文件内容:

[记事本]
set_class = Notepad
;设置窗体class
set_file = Notepad.exe
;设置窗体进程名
set_max_count = 100
;设置最大重复执行次数
set_time_out = 800
;设置超时时间
default_Mode = normal
;默认模式
enable_show_info = 0
;设置显示提示
enabled = 1
;是否启用
<c-y> = MsgBoxTest||1111[=normal]
<c-u> = MsgBoxTest<2>||aaaaa[=normal]
;VIM HotKey=FunctionName[=modeName]
;VIM HotKey=FunctionName||Param1||Param2||Param3||...[=modeName]
;VIM HotKey=FunctionName<n>||Param1||Param2||Param3||...[=modeName]
;VIM热键=函数名称[=模式名称]
;VIM热键=函数名称||参数1||参数2||参数3||...[=模式名称]
;VIM热键=函数名称<n>||参数1||参数2||参数3||...[=模式名称]

h = key|{left}[=normal]
j = key|{down}[=normal]
k = key|{up}[=normal]
l = key|{right}[=normal]
<c-j> = run|notepad.exe[=VIM]
<c-k> = key|^o[=VIM]

四、外置插件

插件名和目录名一致,插件要放到 plugins/PluginName/PluginName.ahk 位置。

放入插件后,重新运行 vimd 会自动启用插件。

函数名请添加 PluginName_ 前缀,避免和其他插件冲突。

如在外置插件中注册全局热键,winName请填写“global”,如:

vim.map("<w-o>", "global", "VIM", "MsgBoxTest", "aaBBBcc", "", "这是全局热键示例")

;该函数名需要和插件名一致
;PluginName(){
PotPlayer(){
    ;热键映射数组
    KeyArray:=Array()

    ;ModeChange为内置函数,用于进行模式的切换
	KeyArray.push({Key:"<insert>", Mode: "普通模式", Group: "模式", Func: "ModeChange", Param: "VIM模式", Comment: "切换到【VIM模式】"})
	KeyArray.push({Key:"<insert>", Mode: "VIM模式", Group: "模式", Func: "ModeChange", Param: "普通模式", Comment: "切换到【普通模式】"})

    ;SendKeyInput为内置函数,用于send指定键盘输入
	KeyArray.push({Key:"z", Mode: "VIM模式", Group: "音量", Func: "SendKeyInput", Param: "{up}", Comment: "声音增大"})
	KeyArray.push({Key:"Z", Mode: "VIM模式", Group: "音量", Func: "SendKeyInput", Param: "{down}", Comment: "声音减小"})
	KeyArray.push({Key:"x", Mode: "VIM模式", Group: "音量", Func: "SendKeyInput", Param: "{Volume_Up 2}", Comment: "系统声音增大"})
	
    ;以下为自定义函数,需要在插件里定义
    KeyArray.push({Key:"a", Mode: "VIM模式", Group: "控制", Func: "PotPlayer_隐藏程序", Param: "", Comment: "隐藏程序"})
	KeyArray.push({Key:"A", Mode: "VIM模式", Group: "控制", Func: "PotPlayer_显示程序", Param: "", Comment: "显示程序"})
	KeyArray.push({Key:"C", Mode: "VIM模式", Group: "存档", Func: "PotPlayer_打开存档1", Param: "", Comment: "打开存档1"})
	KeyArray.push({Key:"B", Mode: "VIM模式", Group: "存档", Func: "PotPlayer_保存存档1", Param: "", Comment: "保存存档1"})


    ;注册窗体,请务必保证 PluginName 和文件名一致,以避免名称混乱影响使用
    ;如果 class 和 exe 同时填写,以 exe 为准
    ;vim.SetWin("PluginName", "ahk_class名")
    ;vim.SetWin("PluginName", "ahk_class名", "PluginName.exe")
    vim.SetWin("PotPlayer", "", "PotPlayerMini64.exe")
    ;设置超时
    ;vim.SetTimeOut(300, "PluginName")
    vim.SetTimeOut(300, "PotPlayer")
	for k, v in KeyArray{
        if (v.Key!="")  ;方便类似TC类全功能,仅启用部分热键的情况
            ;vim.map(v.Key, "PluginName", v.Mode, v.Func, v.Param, v.Group, v.Comment)
            vim.map(v.Key, "PotPlayer", v.Mode, v.Func, v.Param, v.Group, v.Comment)
	}
    ;设置全局热键
    vim.map("<w-o>", "global", "VIM", "MsgBoxTest", "aaBBBcc", "", "这是全局热键示例")
}

;PluginName_Before() ;如有,值=true时,直接发送键值,不执行命令
;PluginName_After() ;如有,值=true时,在执行命令后,再发送键值

;对符合条件的控件使用【normal模式】,而不是【Vim模式】
PotPlayer_Before() {
    ctrl:=ControlGetClassNN(ControlGetFocus("ahk_exe PotPlayerMini64.exe"), "ahk_exe PotPlayerMini64.exe")
    if RegExMatch(ctrl, "Edit2")
        return true
    return false
}

PotPlayer_隐藏程序(*){
    WinMinimize "ahk_class PotPlayer"
    sleep 50
    WinHide "ahk_class PotPlayer"
}

PotPlayer_显示程序(*){
    WinShow "ahk_class PotPlayer"
}

PotPlayer_打开存档1(aFile){
    run aFile
}

PotPlayer_保存存档1(*){
    MsgBox "已保存"
}

以下为Potplayer插件示例:

PotPlayer(){
	KeyArray:=Array()
	KeyArray.push({Key:"<insert>", Mode: "普通模式", Group: "模式", Func: "ModeChange", Param: "VIM模式", Comment: "切换到【VIM模式】"})

	KeyArray.push({Key:"<insert>", Mode: "VIM模式", Group: "模式", Func: "ModeChange", Param: "普通模式", Comment: "切换到【普通模式】"})
	KeyArray.push({Key:"z", Mode: "VIM模式", Group: "音量", Func: "SendKeyInput", Param: "{up}", Comment: "声音增大"})
	KeyArray.push({Key:"Z", Mode: "VIM模式", Group: "音量", Func: "SendKeyInput", Param: "{down}", Comment: "声音减小"})
	KeyArray.push({Key:"x", Mode: "VIM模式", Group: "音量", Func: "SendKeyInput", Param: "{Volume_Up 2}", Comment: "系统声音增大"})
	KeyArray.push({Key:"X", Mode: "VIM模式", Group: "音量", Func: "SendKeyInput", Param: "{Volume_Down 2}", Comment: "系统声音减小"})
	KeyArray.push({Key:"<capslock>", Mode: "VIM模式", Group: "音量", Func: "SendKeyInput", Param: "{Volume_Mute}", Comment: "系统声音静音"})
    
	KeyArray.push({Key:"g", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "{space}", Comment: "播放_暂停"})
	KeyArray.push({Key:"w", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "{PgDn}", Comment: "播放下一个"})
	KeyArray.push({Key:"W", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "{PgUp}", Comment: "播放上一个"})
	KeyArray.push({Key:"s", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "{right}", Comment: "快进2s"})
	KeyArray.push({Key:"S", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "{left}", Comment: "快退2s"})
	KeyArray.push({Key:"<space>", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "^{right}", Comment: "快进15s"})
	KeyArray.push({Key:"v", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "^{left}", Comment: "快退15s"})
	KeyArray.push({Key:"f", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "+{right}", Comment: "快进1m"})
	KeyArray.push({Key:"F", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "+{left}", Comment: "快退1m"})
	KeyArray.push({Key:"d", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "^!{right}", Comment: "快进5m"})
	KeyArray.push({Key:"D", Mode: "VIM模式", Group: "播放", Func: "SendKeyInput", Param: "^!{left}", Comment: "快退5m"})
    
	KeyArray.push({Key:"r", Mode: "VIM模式", Group: "倍速", Func: "SendKeyInput", Param: "{c}", Comment: "倍速_加0.1"})
	KeyArray.push({Key:"R", Mode: "VIM模式", Group: "倍速", Func: "SendKeyInput", Param: "{x}", Comment: "倍速_减0.1"})
	KeyArray.push({Key:"t", Mode: "VIM模式", Group: "倍速", Func: "SendKeyInput", Param: "{c 5}", Comment: "倍速_加0.5"})
	KeyArray.push({Key:"T", Mode: "VIM模式", Group: "倍速", Func: "SendKeyInput", Param: "{x 5}", Comment: "倍速_减0.5"})
	KeyArray.push({Key:"E", Mode: "VIM模式", Group: "倍速", Func: "SendKeyInput", Param: "{c 10}", Comment: "倍速_加1"})
	KeyArray.push({Key:"e", Mode: "VIM模式", Group: "倍速", Func: "SendKeyInput", Param: "{z}", Comment: "倍速_正常"})
    
	KeyArray.push({Key:"a", Mode: "VIM模式", Group: "删除", Func: "PotPlayer_标记", Param: "", Comment: "标记"})
	KeyArray.push({Key:"A", Mode: "VIM模式", Group: "删除", Func: "PotPlayer_删除标记", Param: "", Comment: "删除标记"})
	KeyArray.push({Key:"C", Mode: "VIM模式", Group: "删除", Func: "PotPlayer_删除当前", Param: "", Comment: "删除当前"})
	KeyArray.push({Key:"B", Mode: "VIM模式", Group: "删除", Func: "PotPlayer_删除当前所在文件夹", Param: "", Comment: "删除当前所在文件夹"})

	KeyArray.push({Key:"Q", Mode: "VIM模式", Group: "控制", Func: "SendKeyInput", Param: "!{f4}", Comment: "关闭程序"})
	KeyArray.push({Key:"b", Mode: "VIM模式", Group: "控制", Func: "SendKeyInput", Param: "{Enter}", Comment: "全屏"})
	KeyArray.push({Key:"<esc>", Mode: "VIM模式", Group: "控制", Func: "PotPlayer_隐藏程序", Param: "", Comment: "隐藏程序"})
	; KeyArray.push({Key:"*SS", Mode: "VIM模式", Group: "控制", Func: "PotPlayer_显示程序", Param: "", Comment: "显示程序"})
	KeyArray.push({Key:"q", Mode: "VIM模式", Group: "控制", Func: "SendKeyInput", Param: "!k", Comment: "旋转"})
	KeyArray.push({Key:"l", Mode: "VIM模式", Group: "控制", Func: "SendKeyInput", Param: "l", Comment: "打开关闭列表"})

	KeyArray.push({Key:"<F1>", Mode: "VIM模式", Group: "存档", Func: "PotPlayer_打开存档1", Param: "", Comment: "打开存档1"})
	KeyArray.push({Key:"<F2>", Mode: "VIM模式", Group: "存档", Func: "PotPlayer_保存存档1", Param: "", Comment: "保存存档1"})
	KeyArray.push({Key:"<F3>", Mode: "VIM模式", Group: "存档", Func: "PotPlayer_打开存档2", Param: "", Comment: "打开存档2"})
	KeyArray.push({Key:"<F4>", Mode: "VIM模式", Group: "存档", Func: "PotPlayer_保存存档2", Param: "", Comment: "保存存档2"})

    KeyArray.push({Key:"1", Mode: "VIM模式", Group: "控制", Func: "SendKeyInput", Param: "1", Comment: "缩放50%"})
    KeyArray.push({Key:"2", Mode: "VIM模式", Group: "控制", Func: "SendKeyInput", Param: "2", Comment: "缩放100%"})
    KeyArray.push({Key:"3", Mode: "VIM模式", Group: "控制", Func: "SendKeyInput", Param: "4", Comment: "缩放200%"})
    KeyArray.push({Key:"4", Mode: "VIM模式", Group: "控制", Func: "SendKeyInput", Param: "!4", Comment: "缩放25%"})
    
    vim.SetWin("PotPlayer", "", "PotPlayerMini64.exe")
    vim.SetTimeOut(300, "PotPlayer")
	for k, v in KeyArray{
        if (v.Key!="")
            vim.map(v.Key, "PotPlayer", v.Mode, v.Func, v.Param, v.Group, v.Comment)
	}
}

PotPlayer_隐藏程序(){
    try
        WinMinimize "ahk_class PotPlayer"
    try
        WinMinimize "ahk_class PotPlayer64"
    sleep 50
    try
        WinHide "ahk_class PotPlayer"
    try
        WinHide "ahk_class PotPlayer64"
}

PotPlayer_显示程序(){
    try
        WinShow "ahk_class PotPlayer"
    try
        WinShow "ahk_class PotPlayer64"
}

PotPlayer_删除当前(){}

PotPlayer_删除当前所在文件夹(){}

PotPlayer_删除标记(){}

PotPlayer_删除(){}

PotPlayer_标记(){}

PotPlayer_打开存档1(){}

PotPlayer_打开存档2(){}

PotPlayer_保存存档1(){}

PotPlayer_保存存档2(){}

PotPlayer_GetCurFilePath(){}
⚠️ **GitHub.com Fallback** ⚠️