Skip to content

Commit 0862272

Browse files
author
Caspar Gruijthuijsen
committed
added ahk support for sending keystrokes & faster paste commands
1 parent 5d01bf4 commit 0862272

File tree

4 files changed

+83
-18
lines changed

4 files changed

+83
-18
lines changed

AutoMatlab.sublime-settings

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@
4343
// help to extend the sleep time.
4444
"auto_hotkey_sleep_multiplier": 1,
4545

46+
// The AutoHotkey script to send commands to Matlab by default
47+
// uses the clipboard to paste commands into Matlab (without
48+
// affecting your current clipboard entry). While this is the
49+
// fastest method, updating the clipboard content can sometimes
50+
// take too long, making this method less reliable. To enhance
51+
// the reliability, the paste_commands option can be disabled,
52+
// making AutoHotkey simulate command typing (instead of
53+
// pasting). Alternatively, the reliability can be enhanced by
54+
// increasing the sleep_multiplier option.
55+
"auto_hotkey_paste_commands": true,
56+
4657
// Number of commands from the Matlab history to list in
4758
// the AutoMatlab command panel.
4859
"matlab_history_length": 200,

ahk/run_in_matlab.ahk

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,29 @@
33
; SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
44
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
55

6+
; inputs
7+
; %1% - command to run in matlab
8+
; %2% - command type: insert, paste, key
9+
; %3% - return focus to sublime: (0) no, (1) yes
10+
; %4% - sleep time multiplier (might vary across systems)
11+
612
; Get ID of the Sublime window and Matlab window
713
SublimeID := WinExist("A")
814
MatlabID := WinExist("ahk_exe matlab.exe")
915

10-
; Read sleep time multiplier
16+
; Read command type (as string)
17+
command_type = %2%
18+
19+
; Read sleep time multiplier (as double)
1120
sleep_multiplier = %3%
1221

22+
if (command_type = "paste") {
23+
; Copy command to clipboard
24+
; (do this first, as this might take some time to complete in the background)
25+
SavedClip := ClipboardAll
26+
Clipboard = %1%
27+
}
28+
1329
; Open Matlab window
1430
if WinExist("ahk_id" . MatlabID) {
1531
WinActivate, ahk_id %MatlabID%
@@ -18,27 +34,60 @@ if WinExist("ahk_id" . MatlabID) {
1834
}
1935
else {
2036
MsgBox,,Sublime AutoMatlab, AutoHotkey cannot find process matlab.exe.
37+
if (command_type = "paste") {
38+
Clipboard := SavedClip
39+
SavedClip := ""
40+
}
2141
Exit
2242
}
2343

24-
; Focus the Command window
25-
if WinActive("ahk_id" . MatlabID) {
26-
Send ^0
27-
slp := 200 * sleep_multiplier
28-
Sleep %slp%
44+
if (command_type = "key") {
45+
; Send keyboard command
46+
if WinActive("ahk_id" . MatlabID) {
47+
SendInput %1%
48+
slp := 100 * sleep_multiplier
49+
Sleep %slp%
50+
}
2951
}
52+
else {
53+
; Send text command
3054

31-
; Insert and submit command
32-
if WinActive("ahk_id" . MatlabID) {
33-
SendInput {Text}%1%
34-
SendInput {Enter}
35-
slp := 200 * sleep_multiplier
36-
Sleep %slp%
55+
; Focus the Matlab Command window
56+
if WinActive("ahk_id" . MatlabID) {
57+
Send ^0
58+
}
59+
60+
if (command_type = "paste") {
61+
; Paste and submit text command
62+
slp := 300 * sleep_multiplier
63+
if WinActive("ahk_id" . MatlabID) {
64+
Send ^v
65+
SendInput {Enter}
66+
slp := 100 * sleep_multiplier
67+
Sleep %slp%
68+
}
69+
}
70+
else {
71+
; Insert and submit text command
72+
slp := 200 * sleep_multiplier
73+
Sleep %slp%
74+
if WinActive("ahk_id" . MatlabID) {
75+
SendInput {Text}%1%
76+
SendInput {Enter}
77+
slp := 200 * sleep_multiplier
78+
Sleep %slp%
79+
}
80+
}
3781
}
3882

3983
; Return to Sublime window
4084
if WinActive("ahk_id" . MatlabID) {
41-
if %2% {
85+
if %3% {
4286
WinActivate, ahk_id %SublimeID%
4387
}
4488
}
89+
90+
if (command_type = "paste") {
91+
Clipboard := SavedClip
92+
SavedClip := ""
93+
}

auto_matlab_commands.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def run(self, edit):
9898
else:
9999
# show quick panel with 'empty history' message
100100
self.view.window().show_quick_panel(
101-
[config.EMPTY_MATLAB_HISTORY_MESSAGE], self.selected, 0,
101+
['Empty Matlab command history'], self.selected, 0,
102102
0, self.highlighted)
103103

104104
def selected(self, index):
@@ -238,7 +238,7 @@ class RunMatlabCommandCommand(sublime_plugin.WindowCommand):
238238
"""Run a command in Matlab, via AutoHotkey
239239
"""
240240

241-
def run(self, command):
241+
def run(self, command, type='text'):
242242
"""Run the provided command in Matlab, using AutoHotkey.
243243
Any sublime variable in the command will be replaced by its value.
244244
"""
@@ -276,6 +276,13 @@ def run(self, command):
276276
ahk_path = settings.get('auto_hotkey_path', 'default')
277277
if ahk_path == 'default':
278278
ahk_path = config.DEFAULT_AUTO_HOTKEY_PATH
279+
if type == 'key':
280+
command_type = type
281+
else:
282+
if settings.get('auto_hotkey_paste_commands', True):
283+
command_type = 'paste'
284+
else:
285+
command_type = 'insert'
279286

280287
# check ahk path
281288
if not isfile(ahk_path):
@@ -312,6 +319,7 @@ def run(self, command):
312319
subprocess.Popen([ahk_path,
313320
ahk_script_path,
314321
command,
322+
command_type,
315323
'1' if ahk_return_focus else '0',
316324
str(ahk_sleep_multiplier)])
317325

lib/config.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
DEFAULT_AUTO_HOTKEY_PATH (str): Default path to AutoHotkey executable
88
CONTENTS_NAME (str): Name of Matlab contents file
99
SIGNATURES_NAME (str): Name of Matlab signatures file
10-
EMPTY_MATLAB_HISTORY_MESSAGE (str): Message to show in command pael when
11-
no Matlab history was found.
1210
AUTO_HOTKEY_SCRIPT (str): Name of AutoHotkey script to run matlab commands
1311
MATLAB_COMPLETIONS_PATH (str): Path to AutoMatlab completions, generated
1412
from Matlab installation
@@ -43,7 +41,6 @@
4341
SIGNATURES_NAME = 'functionSignatures.json'
4442

4543
# AutoMatlab commands
46-
EMPTY_MATLAB_HISTORY_MESSAGE = 'Empty Matlab command history'
4744
AUTO_HOTKEY_SCRIPT = 'run_in_matlab.ahk'
4845

4946
# AutoMatlab completions

0 commit comments

Comments
 (0)