Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #27187
    QiaoJiao
    Participant

    Is it possible to select random line and set cursor here in the selection?

    I can do it for the whole file:

    var lines_total = document.GetLines();
    var line_random = Math.floor((Math.random()*lines_total)+1);
    document.selection.SetActivePoint(eePosLogical, 1, line_random);

    But I can’t wrap my mind how to do this for selection, if it possible at all.

    #27190
    Yutaka Emura
    Keymaster

    Do you want to select only one random line from the whole document? Then you can append this line:

    document.selection.EndOfLine(true,eeLineLogical);

    #27193
    QiaoJiao
    Participant

    I want to set cursor to a random line from selected lines. So if I select 3 lines, run script, it would set cursor to one of them randomly.

    My code above works for whole file, I wonder is it possible to do this for selected lines.

    Main problem is getting line number for selected lines, after that it is the same as in the previouse code.

    #27195
    LTT
    Participant

    Something like this?

    yTop = document.selection.GetTopPointY(eePosLogical);
    yBottom = document.selection.GetBottomPointY(eePosLogical);
    var line_random = Math.floor((Math.random()*(yBottom – yTop + 1)) + yTop);
    document.selection.SetActivePoint(eePosLogical, 1, line_random);

    #27200
    QiaoJiao
    Participant

    Yes, execty this. GetTopPointY is the magic I was looking for. Thanks a lot.

    “–” in code is messed up with formating, should be “-“.

    #27201
    QiaoJiao
    Participant

    My final super script: jump to random line in the file, if more than 1 line selected, jump to a random line from them.
    The quirky thing here is to remove last “\n”, otherwise next line will also be included which we don’t want.

    var text = document.selection.Text;
    var last_char = text.slice(-1);
    if ( last_char === "\n" )
    	text = text.slice(0, -1);
    
    if ( text.indexOf("\n") === -1 ) {
    	var lines_total = document.GetLines();
    	var line_random = Math.floor((Math.random()*lines_total)+1);
    }
    else {
    	var yTop = document.selection.GetTopPointY(eePosLogical);
    	var yBottom = document.selection.GetBottomPointY(eePosLogical);
    	if ( last_char === "\n" )
    		yBottom = yBottom - 1;
    	var line_random = Math.floor((Math.random()*(yBottom - yTop + 1)) + yTop);
    }
    document.selection.SetActivePoint(eePosLogical, 1, line_random);
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.