From 9554bdd04dced89c677a3a1f6b230fa4f6e05eb5 Mon Sep 17 00:00:00 2001 From: Calle Helmertz Date: Fri, 31 Jul 2026 08:32:02 +0200 Subject: [PATCH] feat: smart case search (#7385) Add "smart-case" into the cycling of already existing search modes. Fixes #6018 --- config/src/keyassignment.rs | 8 ++++--- .../keyassignment/CopyMode/CycleMatchType.md | 7 +++--- docs/config/lua/keyassignment/Search.md | 22 ++++++++++++++----- docs/scrollback.md | 5 +++-- mux/src/localpane.rs | 8 +++++++ mux/src/pane.rs | 5 +++++ wezterm-gui/src/overlay/copy.rs | 6 ++++- wezterm-gui/src/termwindow/mod.rs | 1 + 8 files changed, 47 insertions(+), 15 deletions(-) diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index 0609de145..e26b5563e 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -115,6 +115,7 @@ pub enum SelectionMode { pub enum Pattern { CaseSensitiveString(String), CaseInSensitiveString(String), + CaseSmartString(String), Regex(String), CurrentSelectionOrEmptyString, } @@ -122,9 +123,10 @@ pub enum Pattern { impl Pattern { pub fn is_empty(&self) -> bool { match self { - Self::CaseSensitiveString(s) | Self::CaseInSensitiveString(s) | Self::Regex(s) => { - s.is_empty() - } + Self::CaseSensitiveString(s) + | Self::CaseInSensitiveString(s) + | Self::CaseSmartString(s) + | Self::Regex(s) => s.is_empty(), Self::CurrentSelectionOrEmptyString => true, } } diff --git a/docs/config/lua/keyassignment/CopyMode/CycleMatchType.md b/docs/config/lua/keyassignment/CopyMode/CycleMatchType.md index 562ac36a5..99a04ca10 100644 --- a/docs/config/lua/keyassignment/CopyMode/CycleMatchType.md +++ b/docs/config/lua/keyassignment/CopyMode/CycleMatchType.md @@ -2,8 +2,10 @@ {{since('20220624-141144-bd1b7c5d')}} -Move the CopyMode/SearchMode cycle between case-sensitive, case-insensitive -and regular expression match types. +Move the CopyMode/SearchMode cycle between case-sensitive, case-insensitive, +smart-case and regular expression match types. + +See [`Search` key assignment](../Search.md) for details about each match types. ```lua local wezterm = require 'wezterm' @@ -17,4 +19,3 @@ return { }, } ``` - diff --git a/docs/config/lua/keyassignment/Search.md b/docs/config/lua/keyassignment/Search.md index 9a9e74892..4f2569d38 100644 --- a/docs/config/lua/keyassignment/Search.md +++ b/docs/config/lua/keyassignment/Search.md @@ -2,14 +2,17 @@ {{since('20200607-144723-74889cd4')}} -This action will trigger the search overlay for the current tab. -It accepts a typed pattern string as its parameter, allowing for -`Regex`, `CaseSensitiveString` and `CaseInSensitiveString` as -pattern matching types. +This action will trigger the search overlay for the current tab. It accepts a +typed pattern string as its parameter, allowing for `Regex`, +`CaseSensitiveString`, `CaseInSensitiveString` and `CaseSmartString` as pattern +matching types. -The supported [regular expression syntax is described -here](https://docs.rs/regex/1.3.9/regex/#syntax). +For `Regex` matching, the supported [regular expression syntax is described here](https://docs.rs/regex/1.3.9/regex/#syntax). +{{since('nightly')}} + +`CaseSmartString` was added, to make the search case-_insensitive_ until the search term +includes any uppercase character, which triggers the search to become case-_sensitive_. ```lua local act = wezterm.action @@ -35,6 +38,13 @@ config.keys = { mods = 'SHIFT|CTRL', action = act.Search { CaseInSensitiveString = 'hash' }, }, + -- search for the string "hash" matching regardless of case, until the search string is edited + -- with an uppercase and the matching becomes case sensitive. + { + key = 'H', + mods = 'SHIFT|CTRL', + action = act.Search { CaseSmartString = 'hash' }, + }, } ``` diff --git a/docs/scrollback.md b/docs/scrollback.md index 6a8c08998..3a1973f8d 100644 --- a/docs/scrollback.md +++ b/docs/scrollback.md @@ -71,8 +71,9 @@ When the search overlay is active the behavior of wezterm changes: * `CTRL-N` and `DownArrow` will cause the selection to move to any next matching text. * `PageDown` will traverse to the next match one page at a time. * `CTRL-R` will cycle through the pattern matching mode; the initial mode is case-sensitive - text matching, the next will match ignoring case and the last will match using the - [regular expression syntax described here](https://docs.rs/regex/1.3.9/regex/#syntax). + text matching, the next will match ignoring case, the next is "smart" and will be + case-sensitive if any input is uppercase, otherwise it will match ignoring case, and the + last will match using the [regular expression syntax described here](https://docs.rs/regex/1.3.9/regex/#syntax). The matching mode is indicated in the search bar. * `CTRL-U` will clear the *search pattern* so you can start over. * `CTRL-SHIFT-C` will copy the selected text to the clipboard. diff --git a/mux/src/localpane.rs b/mux/src/localpane.rs index 0c85661c3..bffc5213c 100644 --- a/mux/src/localpane.rs +++ b/mux/src/localpane.rs @@ -664,6 +664,14 @@ impl Pane for LocalPane { // normalize the case so we match everything lowercase CompiledPattern::CaseInSensitiveString(s.to_lowercase()) } + Pattern::CaseSmartString(s) => { + if s.chars().any(|c| c.is_uppercase()) { + CompiledPattern::CaseSensitiveString(s) + } else { + // normalize the case so we match everything lowercase + CompiledPattern::CaseInSensitiveString(s.to_lowercase()) + } + } Pattern::Regex(r) => CompiledPattern::Regex(Regex::new(&r)?), }; diff --git a/mux/src/pane.rs b/mux/src/pane.rs index 4679b73c3..efa32c9db 100644 --- a/mux/src/pane.rs +++ b/mux/src/pane.rs @@ -57,6 +57,7 @@ pub struct SearchResult { pub enum Pattern { CaseSensitiveString(String), CaseInSensitiveString(String), + CaseSmartString(String), Regex(String), } @@ -72,6 +73,7 @@ impl std::ops::Deref for Pattern { match self { Pattern::CaseSensitiveString(s) => s, Pattern::CaseInSensitiveString(s) => s, + Pattern::CaseSmartString(s) => s, Pattern::Regex(s) => s, } } @@ -82,6 +84,7 @@ impl std::ops::DerefMut for Pattern { match self { Pattern::CaseSensitiveString(s) => s, Pattern::CaseInSensitiveString(s) => s, + Pattern::CaseSmartString(s) => s, Pattern::Regex(s) => s, } } @@ -91,6 +94,7 @@ impl std::ops::DerefMut for Pattern { pub enum PatternType { CaseSensitiveString, CaseInSensitiveString, + CaseSmartString, Regex, } @@ -99,6 +103,7 @@ impl From<&Pattern> for PatternType { match value { Pattern::CaseSensitiveString(_) => PatternType::CaseSensitiveString, Pattern::CaseInSensitiveString(_) => PatternType::CaseInSensitiveString, + Pattern::CaseSmartString(_) => PatternType::CaseSmartString, Pattern::Regex(_) => PatternType::Regex, } } diff --git a/wezterm-gui/src/overlay/copy.rs b/wezterm-gui/src/overlay/copy.rs index e74f338c5..327cb4baf 100644 --- a/wezterm-gui/src/overlay/copy.rs +++ b/wezterm-gui/src/overlay/copy.rs @@ -645,6 +645,7 @@ impl CopyRenderable { match self.pattern_type { PatternType::CaseSensitiveString => Pattern::CaseSensitiveString(pattern), PatternType::CaseInSensitiveString => Pattern::CaseInSensitiveString(pattern), + PatternType::CaseSmartString => Pattern::CaseSmartString(pattern), PatternType::Regex => Pattern::Regex(pattern), } } @@ -694,7 +695,8 @@ impl CopyRenderable { fn cycle_match_type(&mut self) { let pattern_type = match &self.pattern_type { PatternType::CaseSensitiveString => PatternType::CaseInSensitiveString, - PatternType::CaseInSensitiveString => PatternType::Regex, + PatternType::CaseInSensitiveString => PatternType::CaseSmartString, + PatternType::CaseSmartString => PatternType::Regex, PatternType::Regex => PatternType::CaseSensitiveString, }; self.pattern_type = pattern_type; @@ -1433,6 +1435,7 @@ impl Pane for CopyOverlay { let mode = &match pattern { Pattern::CaseSensitiveString(_) => "case-sensitive", Pattern::CaseInSensitiveString(_) => "ignore-case", + Pattern::CaseSmartString(_) => "smart-case", Pattern::Regex(_) => "regex", }; @@ -1534,6 +1537,7 @@ impl Pane for CopyOverlay { let mode = &match pattern { Pattern::CaseSensitiveString(_) => "case-sensitive", Pattern::CaseInSensitiveString(_) => "ignore-case", + Pattern::CaseSmartString(_) => "smart-case", Pattern::Regex(_) => "regex", }; line.overlay_text_with_attribute( diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 61e3ff2d2..bfdbc4bee 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -3603,6 +3603,7 @@ impl TermWindow { match pattern { Pattern::CaseSensitiveString(s) => MuxPattern::CaseSensitiveString(s), Pattern::CaseInSensitiveString(s) => MuxPattern::CaseInSensitiveString(s), + Pattern::CaseSmartString(s) => MuxPattern::CaseSmartString(s), Pattern::Regex(s) => MuxPattern::Regex(s), Pattern::CurrentSelectionOrEmptyString => { let text = self.selection_text(pane);