From eec25b0dc21559707c183c1d3ffbdfb9bdb9eb12 Mon Sep 17 00:00:00 2001 From: Nigel Date: Sun, 9 Aug 2026 15:36:25 +0100 Subject: [PATCH 1/3] Set Tk selection anchor when using Home/End keys When used without Shift, Home/End (Cmd-Left/Right on Macs) should set the selection anchor so that future adjustment of the selection works more predictably. Fixes #1920 --- src/guiguts/maintext.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/guiguts/maintext.py b/src/guiguts/maintext.py index 2fabe5cf..021e5b62 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("", 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 @@ -3920,6 +3913,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, _: tk.Event) -> 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 +3953,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(event) self.see(tk.INSERT) return "break" @@ -3986,6 +3985,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(event) self.see(tk.INSERT) return "break" From 858fea84416f632034dae5d2e5c315e6b918ae2f Mon Sep 17 00:00:00 2001 From: Nigel Date: Tue, 11 Aug 2026 11:42:42 +0100 Subject: [PATCH 2/3] Also fix Ctrl-a/e on Macs --- src/guiguts/maintext.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/guiguts/maintext.py b/src/guiguts/maintext.py index 021e5b62..8af0a59b 100644 --- a/src/guiguts/maintext.py +++ b/src/guiguts/maintext.py @@ -951,7 +951,7 @@ def text_peer_focus_track(event: tk.Event) -> None: self.bind_event( "", lambda _: self.cancel_drag_sel(), add=True, bind_peer=True ) - self.bind("", self.set_tk_selection_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 @@ -2689,6 +2689,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: @@ -3913,7 +3914,7 @@ 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, _: tk.Event) -> str: + 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 "" @@ -3953,7 +3954,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(event) + self.set_tk_selection_anchor() self.see(tk.INSERT) return "break" @@ -3985,7 +3986,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(event) + self.set_tk_selection_anchor() self.see(tk.INSERT) return "break" From a471a017af53787dfdb64117fdfd7f6aae0b2376 Mon Sep 17 00:00:00 2001 From: Nigel Date: Tue, 11 Aug 2026 20:34:02 +0100 Subject: [PATCH 3/3] Another small fix --- src/guiguts/maintext.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/guiguts/maintext.py b/src/guiguts/maintext.py index 8af0a59b..c8904452 100644 --- a/src/guiguts/maintext.py +++ b/src/guiguts/maintext.py @@ -2680,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: