-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help please: Swap anchor and caret using Lua #2583
Comments
Hi Still trying combinations. My latest as follows:
generates an interesting error message:
Could someone help me by explaining the difference between The reason I like Lua is the simplicity, trying to drive GO using Lua is proving a great challenge. Kind Regards Gavin Holt |
Hi, Still plugging away at this problem with no success. Latest iteration tries to move away from Go editor = {}
function editor.HasSelection(Current)
return Current.Cursor:HasSelection() or nil
end
function editor.Selection(Current)
-- Get coordinates of current cursor +- selection
if editor.HasSelection(Current) then
return { CursorX = Current.Cursor.Loc.X+1,
CursorY = Current.Cursor.Loc.Y+1,
SelStartX = Current.Cursor.CurSelection[1].X+1,
SelStartY = Current.Cursor.CurSelection[1].Y+1,
SelEndX = Current.Cursor.CurSelection[2].X+1,
SelEndY = Current.Cursor.CurSelection[2].Y+1
}
else
return { CursorX = Current.Cursor.Loc.X+1,
CursorY = Current.Cursor.Loc.Y+1
}
end
end
function editor.SetSelection(Current,SelStartX,SelEndX,SelStartY,SelEndY)
-- Set the current selection
Current.Cursor:SetSelectionStart({X = SelStartX,Y = SelEndX})
Current.Cursor:SetSelectionEnd({X = SelStartY,Y = SelEndY})
Current.Cursor:ResetSelection()
end
function editor.SetLocation(Current,CursorX,CursorY)
-- Set the cursor position
Current.Cursor:GotoLoc({X = CursorX,Y = CursorY})
Current.Cursor:Relocate()
end
function editor.SwapAnchor(Current)
-- Move active insertion point to the other end of the selection
-- Allow me to extend selection from either end
if editor.HasSelection(Current) then
local Selection= editor.Selection(Current) -- Simple numbers
if Caret.CursorX==Caret.SelEndX and Caret.CursorY==Caret.SelEndY then
editor.SetSelection(Current,Caret.SelEndX,Caret.SelEndY,Caret.SelStartX,Caret.SelStartY)
editor.SetLocation(Current,Caret.SelStartX,Caret.SelStartY)
end
if Caret.CursorX==Caret.SelStartX and Caret.CursorY==Caret.SelStartY then
editor.SetSelection(Current,Caret.SelStartX,Caret.SelStartY,Caret.SelEndX,Caret.SelEndY)
editor.SetLocation(Current,Caret.SelEndX,Caret.SelEndY)
end
end
end My SetSelection function does not seem to work! Not sure if I am making this too complicated, but this problem does highlight some simple patterns that I would like to be able to use in my scripts:
How do I pass data back into Go from Lua? As before, any help welcome. Kind Regards Gavin Holt |
I have given up. G |
The use of Go structs from Lua was correct in your initial code. The problem is with the logic. The direction of a text selection is determined not by the order of start and end locations in (From micro source code, e.g. from SelectTo function it looks like in most cases micro resets the order to "left, right" regardless of the direction of selection. I'd say this is counter-intuitive and even inconsistent, since in some other cases, namely when selecting via mouse, micro does set the order to either "left, right" or "right, left" depending on the direction, as seen in MousePress function.) This seems to work fine:
|
Hi, Many thanks, this is a great help. Kind Regards Gavin Holt |
Hi,
As part of developing some keyboard shortcuts for text selection, I want a Lua function to change the direction of the current selection. i.e. Keep the same text selection and swap the position of the anchor and the insertion point (this would allow me to extend my selection at whichever end is required).
I have tried the following (and thought it would work: "Swap anchor and caret using Lua #2542") :
The logic seems sound but, the reselection of the text is not working.
I do not have a good grasp of GO and I am finding the documentation difficult.
If anyone could point me in the right direction, I would be grateful.
Kind Regards Gavin Holt
The text was updated successfully, but these errors were encountered: