Skip to content
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

Closed
Gavin-Holt opened this issue Oct 5, 2022 · 5 comments
Closed

Help please: Swap anchor and caret using Lua #2583

Gavin-Holt opened this issue Oct 5, 2022 · 5 comments

Comments

@Gavin-Holt
Copy link

Gavin-Holt commented Oct 5, 2022

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") :

function editor_SwapAnchor(Current)
    if Current.Cursor:HasSelection()  then
       if Current.Cursor.CurSelection[1]:GreaterThan(-Current.Cursor.CurSelection[2]) then
            local a, b = Current.Cursor.CurSelection[2], Current.Cursor.CurSelection[1]
            Current.Cursor:SetSelectionStart(-a)
            Current.Cursor:SetSelectionEnd(-b)
        else
            local a, b = Current.Cursor.CurSelection[1], Current.Cursor.CurSelection[2]
            Current.Cursor:SetSelectionStart(-a)
            Current.Cursor:SetSelectionEnd(-b)
        end
    end
end

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

@Gavin-Holt
Copy link
Author

Hi

Still trying combinations. My latest as follows:

function editor_SwapAnchor(Current) 
    if editor.HasSelection(Current) then
        local a  = Current.Cursor.CurSelection[1]
        local b = Current.Cursor.CurSelection[2]

        if Current.Cursor.CurSelection[1]:GreaterThan(-Current.Cursor.CurSelection[2]) then
            -- a>b
            Current.Cursor:SetSelectionStart(a)
            Current.Cursor:SetSelectionEnd(b)
        else
            -- a<b
            Current.Cursor:SetSelectionStart(b)
            Current.Cursor:SetSelectionEnd(a)
        end
    end
end

generates an interesting error message:

init:338: bad argument #2 to SetSelectionStart (cannot use &{38 321} (type *buffer.Loc) as type buffer.L
oc)
stack traceback:
        [G]: in function 'SetSelectionStart'
        init:338: in function 'editor_SwapAnchor'
        init:521: in main chunk
        [G]: ?

Press enter to continue

Could someone help me by explaining the difference between type *buffer.Loc and type buffer.Loc

The reason I like Lua is the simplicity, trying to drive GO using Lua is proving a great challenge.

Kind Regards Gavin Holt

@Gavin-Holt
Copy link
Author

Hi,

Still plugging away at this problem with no success. Latest iteration tries to move away from Go strucs and get things into Lua types/tables:

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:

  1. Check for a selection
  2. Get the start and end coordinates
  3. Get the location of the insertion point
  4. Get the direction of the selection (Right to left or left to right)
  5. Set all of the above using Lua data types/tables

How do I pass data back into Go from Lua?

As before, any help welcome.

Kind Regards Gavin Holt

@Gavin-Holt
Copy link
Author

I have given up. G

@dmaluka
Copy link
Collaborator

dmaluka commented Dec 30, 2022

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 CurSelection but by the current cursor position.

(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:

function editor_SwapAnchor(Current)
    if Current.Cursor:HasSelection()  then
        local a, b = -Current.Cursor.CurSelection[1], -Current.Cursor.CurSelection[2]
        if -Current.Cursor.Loc == a then
            -- backward -> forward
            Current.Cursor:GotoLoc(b)
            Current.Cursor.OrigSelection = -Current.Cursor.CurSelection
        else
            -- forward -> backward
            Current:Deselect()
            Current.Cursor:GotoLoc(b)
            Current.Cursor.OrigSelection[1] = -Current.Cursor.Loc
            Current.Cursor:GotoLoc(a)
            Current.Cursor:SelectTo(a)
        end
    end
end

@Gavin-Holt
Copy link
Author

Hi,

Many thanks, this is a great help.

Kind Regards Gavin Holt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants