• How do I select an entire line of text?

    From Luc@21:1/5 to All on Fri Oct 7 18:59:46 2022
    I should be able to do this, but I am struggling and that is sucking the
    fun out of my project.

    How do I select entire lines in a text widget programatically?
    I mean, the current line must be selected at all times.

    I'm doing it like this:

    --------------------
    set ::currindex [[focus] index insert]
    lassign [split $::currindex "."] ::CURRLINE ::CURRCOL

    $::textw tag configure "selected" -foreground #ffffff -background #000000

    $::textw tag remove "selected" $::oldline.0 "$::oldline.0 lineend"
    $::textw tag add "selected" $::CURRLINE.0 "$::CURRLINE.0 lineend"
    set ::oldline $::CURRLINE
    --------------------

    Great. The current line is always selected. Problems though:

    1) The line is only "selected" (I probably should say "highlighted" because that's what it really is) until the end of the text. I want the "selection"
    or highlight to extend up to the end of the line, I mean, across the void
    and into the "wall" of the text widget, as if the entire line were a single cell in a table.

    2) I want to be able to select multiple lines holding Shift. Even if I
    use the "sel" tag, I'm not too sure of how to achieve that. I understand
    the text widget will select and apply the "sel" tag by design, which
    would do the job, but I don't know how to make the current line become
    a selection automatically, without using the mouse or the Shift key.
    I just want to have to use the Shift key when selecting multiple consecutive lines.

    3) Bonus if I can select multiple non-consecutive lines by holding the
    Ctrl key.

    Many thanks for any help.

    --
    Luc


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to Luc on Sat Oct 8 04:47:44 2022
    Luc <[email protected]> wrote:
    How do I select entire lines in a text widget programatically?

    ... I want the "selection" or highlight to extend up to the end of the
    line, I mean, across the void and into the "wall" of the text widget,
    as if the entire line were a single cell in a table.

    $ rlwrap wish
    % text .t
    .t
    % pack .t
    % .t insert end "Mary had a little lamb\n"
    % .t insert end "It's fleece was white as snow\n"
    % .t insert end "The quick brown fox\n"
    % .t tag add sel "2.5 linestart" "2.5 lineend + 1 chars"

    2) I want to be able to select multiple lines holding Shift. Even if I
    use the "sel" tag, I'm not too sure of how to achieve that. I understand
    the text widget will select and apply the "sel" tag by design, which
    would do the job, but I don't know how to make the current line become
    a selection automatically, without using the mouse or the Shift key.

    $ rlwrap wish
    % text .t
    .t
    % pack .t
    % .t insert end "Mary had a little lamb\n"
    % .t insert end "It's fleece was white as snow\n"
    % .t insert end "The quick brown fox\n"
    % .t tag add sel "1.5 linestart" "1.5 lineend + 1 chars"
    % .t tag add sel "3.5 linestart" "3.5 lineend + 1 chars"

    Making all this work with various combinations of keys will likely
    require you to replace the existing selection handling bindings for the
    text with your own custom versions to do the "selection magic" you
    want. Or use your own tag rather than the built in sel tag to do the highlighting you want. Which will still require custom bindings, but
    you won't have to override the existing sel bindings.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)