Skip to content

Instantly share code, notes, and snippets.

@troysandal
Last active December 20, 2015 07:19
Show Gist options
  • Save troysandal/6092629 to your computer and use it in GitHub Desktop.
Save troysandal/6092629 to your computer and use it in GitHub Desktop.
Remap of Cmd-Left and Cmd-Right in Zero Brane Studio to match that of Home/End.
return {
name = "Modified Cmd-Arrow",
description = "Modified Cmd-Arrow keys to behave as Sublime Text 2",
author = "Paul Kulchenko",
onEditorKeyDown = function(self, editor, event)
local keycode = event:GetKeyCode()
local mod = event:GetModifiers()
local result
if (keycode == wx.WXK_LEFT or keycode == wx.WXK_RIGHT) and mod == wx.wxMOD_CMD then
local line = editor:GetCurrentLine()
local text = editor:GetLine(line)
local firstNonWS = text:find("[^%s]")
if #text > 0 then
local pos
firstNonWS = firstNonWS or #text
if (keycode == wx.WXK_LEFT) then
local offset = (editor:GetCurrentPos() - editor:PositionFromLine(line)) + 1
if offset == firstNonWS then
-- caret is at leftmost non-WS, move to start of line
-- BUG: +1 seems right but I'm always winding up at the
-- end of the previous line. It seems like some other code is
-- either catching this keystroke and moving us back a line
-- or positioning the cursor in whitespace is doing it.
-- Not sure how to kill the event, event.stop() etc?
pos = editor:PositionFromLine(line)
result = false
else
-- move caret to first non-WS
pos = editor:PositionFromLine(line) + firstNonWS
end
else
-- move to caret to end of line
pos = editor:GetLineEndPosition(line)-1
end
editor:GotoPos(pos)
end
elseif keycode == wx.WXK_UP and mod == wx.wxMOD_CMD then
editor:GotoPos(0)
elseif keycode == wx.WXK_DOWN and mod == wx.wxMOD_CMD then
editor:GotoPos(editor:GetLength())
end
return result
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment