diff --git a/src/guiguts/maintext.py b/src/guiguts/maintext.py index 2fabe5cf..c8904452 100644 --- a/src/guiguts/maintext.py +++ b/src/guiguts/maintext.py @@ -951,14 +951,7 @@ def text_peer_focus_track(event: tk.Event) -> None: self.bind_event( "", lambda _: self.cancel_drag_sel(), add=True, bind_peer=True ) - - def set_anchor(_: tk.Event) -> str: - """Set anchor point at current position, then allow Shift-click to - execute its default behavior.""" - self.mark_set(TK_ANCHOR_MARK, f"{self.index(tk.INSERT)}") - return "" - - self.bind("", set_anchor) + self.bind("", lambda _event: self.set_tk_selection_anchor()) # Override default left/right/up/down arrow key behavior if there is a selection # Above behavior would affect Shift-Left/Right/Up/Down, so also bind those to @@ -2687,7 +2680,14 @@ def _move_to_selection_edge(self, end: bool, force_line: bool = False) -> str: """ sel_ranges = self.selected_ranges() if not sel_ranges: - return "" + if force_line: + sel_ranges.append( + IndexRange( + maintext().get_insert_index(), maintext().get_insert_index() + ) + ) + else: + return "" pos = sel_ranges[-1].end if end else sel_ranges[0].start idx = pos.index() if force_line: @@ -2696,6 +2696,7 @@ def _move_to_selection_edge(self, end: bool, force_line: bool = False) -> str: self.focus_widget().mark_set(tk.INSERT, idx) self.focus_widget().see(tk.INSERT) self.clear_selection() + self.set_tk_selection_anchor() return "break" def move_to_start(self) -> None: @@ -3920,6 +3921,11 @@ def strip_end_of_line_spaces(self) -> None: while start := self.search(" +$", start, regexp=True): self.delete(start, f"{start} lineend") + def set_tk_selection_anchor(self) -> str: + """Set anchor point at current position.""" + self.mark_set(TK_ANCHOR_MARK, f"{self.index(tk.INSERT)}") + return "" + def go_home(self, event: tk.Event) -> str: """Handle the Home key. Either move to start of line, or if already there, move to first non-space on line.""" @@ -3955,6 +3961,7 @@ def go_home(self, event: tk.Event) -> str: # If no Shift, just moving, so clear selection else: self.clear_selection() + self.set_tk_selection_anchor() self.see(tk.INSERT) return "break" @@ -3986,6 +3993,7 @@ def go_end(self, event: tk.Event) -> str: # If no Shift, just moving, so clear selection else: self.clear_selection() + self.set_tk_selection_anchor() self.see(tk.INSERT) return "break"