From e7363f430957b8c0be233562703532089e8ed86e Mon Sep 17 00:00:00 2001 From: John Campion Jr Date: Thu, 5 Nov 2026 20:35:32 -0500 Subject: [PATCH] Control: public hovered-link API, platform click modifier, hover off --- src/XTerm.NET.Tests/OscTerminatorTests.cs | 55 +++++++ src/XTerm.NET.Tests/WordSeparatorsTests.cs | 158 ++++++++++++++++++++ src/XTerm.NET/Events/TerminalEvents.cs | 15 ++ src/XTerm.NET/Options/TerminalOptions.cs | 18 +++ src/XTerm.NET/Selection/SelectionManager.cs | 66 +++++++- src/XTerm.NET/Terminal.cs | 2 +- 6 files changed, 306 insertions(+), 8 deletions(-) create mode 100644 src/XTerm.NET.Tests/OscTerminatorTests.cs create mode 100644 src/XTerm.NET.Tests/WordSeparatorsTests.cs diff --git a/src/XTerm.NET.Tests/OscTerminatorTests.cs b/src/XTerm.NET.Tests/OscTerminatorTests.cs new file mode 100644 index 00000000..f57dae33 --- /dev/null +++ b/src/XTerm.NET.Tests/OscTerminatorTests.cs @@ -0,0 +1,55 @@ +using XTerm; +using XTerm.Events; +using XTerm.Options; +using Xunit; + +namespace XTerm.Tests; + +/// +/// : which terminator the program used, +/// so a handler can answer in kind. +/// +public class OscTerminatorTests +{ + private static readonly string Esc = ((char)0x1B).ToString(); + + private static TerminalEvents.OscReceivedEventArgs Receive(string sequence) + { + var terminal = new Terminal(new TerminalOptions { Cols = 20, Rows = 5 }); + var seen = new List(); + terminal.OscReceived += (_, e) => seen.Add(e); + terminal.Write(sequence); + return Assert.Single(seen); + } + + [Fact] + public void A_sequence_ended_with_BEL_says_so() + { + Assert.True(Receive($"{Esc}]1337;SetMark\u0007").EndedWithBel); + } + + [Fact] + public void A_sequence_ended_with_ST_says_not() + { + Assert.False(Receive($"{Esc}]1337;SetMark{Esc}\\").EndedWithBel); + } + + [Fact] + public void Each_sequence_reports_its_own_terminator() + { + var terminal = new Terminal(new TerminalOptions { Cols = 20, Rows = 5 }); + var seen = new List(); + terminal.OscReceived += (_, e) => seen.Add(e.EndedWithBel); + + terminal.Write($"{Esc}]1337;A\u0007{Esc}]1337;B{Esc}\\{Esc}]1337;C\u0007"); + + Assert.Equal(new[] { true, false, true }, seen); + } + + [Fact] + public void The_old_constructor_still_builds_one_that_ended_with_ST() + { + var e = new TerminalEvents.OscReceivedEventArgs("1", 1, "", "1", recognized: false); + Assert.False(e.EndedWithBel); + } +} diff --git a/src/XTerm.NET.Tests/WordSeparatorsTests.cs b/src/XTerm.NET.Tests/WordSeparatorsTests.cs new file mode 100644 index 00000000..9b1a1bdb --- /dev/null +++ b/src/XTerm.NET.Tests/WordSeparatorsTests.cs @@ -0,0 +1,158 @@ +using XTerm; +using XTerm.Options; +using XTerm.Selection; +using Xunit; + +namespace XTerm.Tests; + +/// +/// : where a double-click word ends. Null keeps the +/// built-in rule; a string lists the separators, everything else being part of a word. +/// +public class WordSeparatorsTests +{ + private static Terminal Write(string text, string? separators = null) + { + var t = new Terminal(new TerminalOptions { Cols = 40, Rows = 3, WordSeparators = separators }); + t.Write(text); + return t; + } + + private static string DoubleClick(Terminal t, int col) + { + t.Selection.StartSelection(col, 0, SelectionMode.Word); + return t.Selection.GetSelectionText(); + } + + [Fact] + public void By_default_punctuation_ends_a_word() + { + var t = Write("cat ~/src/foo-bar.txt"); + Assert.Equal("foo", DoubleClick(t, 11)); + } + + [Fact] + public void Separators_listed_end_a_word_and_nothing_else_does() + { + var t = Write("cat ~/src/foo-bar.txt", " "); + Assert.Equal("~/src/foo-bar.txt", DoubleClick(t, 11)); + } + + [Fact] + public void Ghosttys_default_list_keeps_a_path_whole_and_stops_at_a_colon() + { + var t = Write("at src/foo.cs:42 here", "\t '\"│`|:;,()[]{}<>$"); + Assert.Equal("src/foo.cs", DoubleClick(t, 5)); + } + + [Fact] + public void A_separator_outside_the_basic_plane_counts_as_one_code_point() + { + var t = Write("ab\U0001F600cd", "\U0001F600"); + Assert.Equal("ab", DoubleClick(t, 0)); + } + + /// + /// A wide glyph is two cells, the second a width-0 continuation with no content. Expansion walks the + /// glyph whole, from either side and from a click on either half. + /// + [Theory] + [InlineData("a\U0001F600b", 0)] + [InlineData("a\U0001F600b", 3)] + [InlineData("a\U0001F600b", 2)] + [InlineData("a\u7532b", 0)] + [InlineData("a\u7532b", 3)] + public void A_wide_character_that_is_not_a_separator_stays_inside_the_word(string text, int col) + { + var t = Write(text, " "); + Assert.Equal(text, DoubleClick(t, col)); + } + + [Fact] + public void A_wide_letter_stays_inside_the_word_under_the_default_rule_too() + { + // The built-in rule counts a CJK letter as a word character; its continuation cell has to agree. + var t = Write("a\u7532b"); + Assert.Equal("a\u7532b", DoubleClick(t, 0)); + } + + [Fact] + public void A_wide_separator_still_ends_the_word() + { + // The space too, so the blank cells after "cd" end it on the right; the emoji ends it on the left. + var t = Write("ab\U0001F600cd", " \U0001F600"); + Assert.Equal("cd", DoubleClick(t, 4)); + } + + [Fact] + public void A_wide_separators_second_cell_is_not_taken_into_the_word_beside_it() + { + // Its continuation adds no text, so only the range shows it: the word starts at "c", not one + // cell earlier on the separator's second half. + var t = Write("ab\U0001F600cd", " \U0001F600"); + t.Selection.StartSelection(4, 0, SelectionMode.Word); + + Assert.True(t.Selection.TryGetSelection(out var range)); + Assert.Equal(4, range.StartX); + } + + [Fact] + public void An_empty_cell_that_follows_no_wide_glyph_still_ends_a_word() + { + // Width 0 and no content, but not a continuation: nothing wide comes before it. + var t = Write("ab cd", ""); + t.Buffer.Lines[0]![2] = XTerm.Buffer.BufferCell.Empty; + + Assert.Equal("ab", DoubleClick(t, 0).TrimEnd()); + } + + /// + /// Blank cells hold a space, not nothing, so a gap the cursor skipped is spaces like any other: a + /// list without the space joins across it, and one with it stops there. Ghostty's default has it. + /// + [Fact] + public void Blank_cells_are_spaces_so_a_list_needs_the_space_to_stop_at_a_gap() + { + var joined = Write("one", ""); + joined.Write("\u001b[1;6Htwo"); + Assert.StartsWith("one two", DoubleClick(joined, 1)); + + var stopped = Write("one", " "); + stopped.Write("\u001b[1;6Htwo"); + Assert.Equal("one", DoubleClick(stopped, 1)); + } + + [Fact] + public void A_change_applies_to_the_next_double_click() + { + var t = Write("foo-bar"); + Assert.Equal("foo", DoubleClick(t, 1)); + + t.Options.WordSeparators = " "; + Assert.Equal("foo-bar", DoubleClick(t, 1)); + + // One list to another, not only to and from null: the parsed set has to follow the string. + t.Options.WordSeparators = "-"; + Assert.Equal("foo", DoubleClick(t, 1)); + + t.Options.WordSeparators = null; + Assert.Equal("foo", DoubleClick(t, 1)); + } + + [Fact] + public void The_rule_is_public_for_a_host_to_share() + { + var t = Write("", "-"); + Assert.True(t.Selection.IsWordChar(".")); + Assert.False(t.Selection.IsWordChar("-")); + Assert.False(t.Selection.IsWordChar("")); + Assert.False(t.Selection.IsWordChar(null)); + } + + [Fact] + public void A_copy_of_the_options_keeps_it() + { + var options = new TerminalOptions { WordSeparators = " :" }; + Assert.Equal(" :", options.Clone().WordSeparators); + } +} diff --git a/src/XTerm.NET/Events/TerminalEvents.cs b/src/XTerm.NET/Events/TerminalEvents.cs index 743cb18a..3f761aae 100644 --- a/src/XTerm.NET/Events/TerminalEvents.cs +++ b/src/XTerm.NET/Events/TerminalEvents.cs @@ -427,12 +427,18 @@ public AttentionRequestedEventArgs(string action) public class OscReceivedEventArgs : EventArgs { public OscReceivedEventArgs(string identifier, int code, string data, string raw, bool recognized) + : this(identifier, code, data, raw, recognized, endedWithBel: false) + { + } + + public OscReceivedEventArgs(string identifier, int code, string data, string raw, bool recognized, bool endedWithBel) { Identifier = identifier; Code = code; Data = data; Raw = raw; Recognized = recognized; + EndedWithBel = endedWithBel; } /// @@ -460,6 +466,15 @@ public OscReceivedEventArgs(string identifier, int code, string data, string raw /// handler is the only thing that will act on it. /// public bool Recognized { get; } + + /// + /// Whether the program ended the sequence with BEL (0x07) rather than ST (ESC \). + /// + /// + /// For a handler that replies: answering in the terminator the program used is what xterm + /// does, and what the terminal's own replies do. + /// + public bool EndedWithBel { get; } } /// diff --git a/src/XTerm.NET/Options/TerminalOptions.cs b/src/XTerm.NET/Options/TerminalOptions.cs index 11d04928..982ea2d2 100644 --- a/src/XTerm.NET/Options/TerminalOptions.cs +++ b/src/XTerm.NET/Options/TerminalOptions.cs @@ -311,6 +311,23 @@ public int TabStopWidth /// public string EnquiryResponse { get; set; } = string.Empty; + /// + /// The characters that end a word when a double-click selects one. Null, the default, keeps the + /// built-in rule: letters, digits and _ are word characters and everything else ends a word. + /// + /// + /// Given a string, every character NOT in it is part of a word, so ., - and + /// / join a path or a URL unless listed. This is Ghostty's selection-word-chars, whose + /// default is "\t '\"│`|:;,()[]{}<>$". Each code point counts on its own, so characters + /// outside the Basic Multilingual Plane work; a sequence of several code points (an emoji with a + /// modifier, say) cannot be one separator. + /// A cell with no content (code point 0, as NUL is in Ghostty) is always a boundary. Blank + /// cells hold a space rather than nothing, though, so a list without the space joins words across a + /// gap. A cell is judged by its first code point. + /// Read on every expansion, so a change applies to the next double-click. + /// + public string? WordSeparators { get; set; } + /// /// How OSC colour queries (OSC 4, 5, 10, 11 and 12 with ?) are answered. /// @@ -552,6 +569,7 @@ public TerminalOptions(TerminalOptions other) KittyKeyboardEnabled = other.KittyKeyboardEnabled; PointerShapesEnabled = other.PointerShapesEnabled; EnquiryResponse = other.EnquiryResponse; + WordSeparators = other.WordSeparators; ColorReportFormat = other.ColorReportFormat; KeyboardActionModeAllowed = other.KeyboardActionModeAllowed; MaxImageRegistryBytes = other.MaxImageRegistryBytes; diff --git a/src/XTerm.NET/Selection/SelectionManager.cs b/src/XTerm.NET/Selection/SelectionManager.cs index 270a040c..d502af85 100644 --- a/src/XTerm.NET/Selection/SelectionManager.cs +++ b/src/XTerm.NET/Selection/SelectionManager.cs @@ -1,5 +1,6 @@ using XTerm.Buffer; using XTerm.Common; +using XTerm.Options; namespace XTerm.Selection; @@ -289,7 +290,7 @@ private void ExpandSelectionToWord() var firstLine = first.y >= 0 && first.y < buffer.Lines.Length ? buffer.Lines[first.y] : null; if (firstLine != null) { - while (first.x > 0 && IsWordChar(firstLine[first.x - 1].Content)) + while (first.x > 0 && IsWordCell(firstLine, first.x - 1)) { first.x--; } @@ -298,7 +299,7 @@ private void ExpandSelectionToWord() var lastLine = last.y >= 0 && last.y < buffer.Lines.Length ? buffer.Lines[last.y] : null; if (lastLine != null) { - while (last.x < _terminal.Cols - 1 && IsWordChar(lastLine[last.x + 1].Content)) + while (last.x < _terminal.Cols - 1 && IsWordCell(lastLine, last.x + 1)) { last.x++; } @@ -333,15 +334,66 @@ private void ExpandSelectionToLine() } /// - /// Checks if a character is a word character. + /// Whether the cell at is part of a word, whole glyphs at a time: a wide glyph's + /// second cell is a width-0 continuation with no content of its own, and belongs to the glyph before + /// it. Without that, expansion stopped halfway through a CJK character or an emoji. A cell that is + /// empty without following a wide glyph is still a boundary. /// - private bool IsWordChar(string ch) + private bool IsWordCell(BufferLine line, int x) { - if (string.IsNullOrEmpty(ch)) + if (x > 0 && line.GetWidth(x) == 0 && line.GetWidth(x - 1) == 2) + return IsWordChar(line[x - 1].Content); + return IsWordChar(line[x].Content); + } + + /// + /// Whether a cell's content is part of a word, by : what + /// double-click expansion stops at. Public so a host's own word motion agrees with the mouse. + /// + /// A cell's content. Null or empty, an empty cell, is never part of a word. + public bool IsWordChar(string? content) + { + if (string.IsNullOrEmpty(content)) return false; - var c = ch[0]; - return char.IsLetterOrDigit(c) || c == '_'; + var separators = _terminal.Options.WordSeparators; + if (separators is null) + { + var c = content[0]; + return char.IsLetterOrDigit(c) || c == '_'; + } + + int first = char.IsHighSurrogate(content[0]) && content.Length > 1 && char.IsLowSurrogate(content[1]) + ? char.ConvertToUtf32(content[0], content[1]) + : content[0]; + return first != 0 && !SeparatorSet(separators).Contains(first); + } + + // The separators as code points, rebuilt only when the option is set to a different string. + private string? _separatorSource; + private HashSet _separatorSet = new(); + + private HashSet SeparatorSet(string separators) + { + if (!ReferenceEquals(separators, _separatorSource)) + { + var set = new HashSet(); + for (var i = 0; i < separators.Length; i++) + { + if (char.IsHighSurrogate(separators[i]) && i + 1 < separators.Length && char.IsLowSurrogate(separators[i + 1])) + { + set.Add(char.ConvertToUtf32(separators[i], separators[i + 1])); + i++; + } + else + { + set.Add(separators[i]); + } + } + _separatorSet = set; + _separatorSource = separators; + } + return _separatorSet; } private int ToAbsoluteY(int viewportY) diff --git a/src/XTerm.NET/Terminal.cs b/src/XTerm.NET/Terminal.cs index c4ebf419..0eb76552 100644 --- a/src/XTerm.NET/Terminal.cs +++ b/src/XTerm.NET/Terminal.cs @@ -2212,7 +2212,7 @@ internal void RaiseAttentionRequested(string action) => internal void RaiseKittyNotificationReceived(string? identifier, string? title, string? body, int? urgency, string? icon) => NotificationReceived?.Invoke(this, new TerminalEvents.NotificationEventArgs(identifier, title, body, urgency, icon)); internal void RaiseOscReceived(string identifier, int code, string data, string raw, bool recognized) => - OscReceived?.Invoke(this, new TerminalEvents.OscReceivedEventArgs(identifier, code, data, raw, recognized)); + OscReceived?.Invoke(this, new TerminalEvents.OscReceivedEventArgs(identifier, code, data, raw, recognized, _parser.LastOscEndedWithBel)); internal void RaiseWindowMoved(int x, int y) => WindowMoved?.Invoke(this, new TerminalEvents.WindowMovedEventArgs(x, y));