Skip to content

fix(search/macos): don't consume Return during IME composition on macOS - #2690

Merged
AucaCoyan merged 1 commit into
espanso:devfrom
znzryb:fix/search-bar-ime-enter-cjk
Jul 19, 2026
Merged

AucaCoyan merged 1 commit into
espanso:devfrom
znzryb:fix/search-bar-ime-enter-cjk

Conversation

@znzryb

@znzryb znzryb commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Stops the search bar from consuming Return while an IME composition is in progress on macOS.
  • Lets CJK users (Chinese / Japanese / Korean) press Return to land whatever the IME is currently holding — typically the raw Latin letters they just typed (e.g. lee, ka, gye) — into the search field, instead of triggering whichever match happens to be highlighted.
  • Fixes Search bar consumes Enter during IME composition (CJK input methods) #2687.

Demo

Both videos captured on macOS Tahoe 26 (Apple Silicon, Apple Pinyin IME, ~130 user-defined snippets).

Before (espanso 2.3.0, brew cask)

Typing the pinyin Poin under a CJK IME and pressing Return to commit Point to the search field — instead, espanso fires Submit() immediately, picking whichever match is currently highlighted (the first item 01trie, since the search field is still empty because the IME never got to commit). The wrong snippet then expands into vim:

BUG: wrong snippet 01trie expands under CJK IME

After (this PR, built from znzryb/espanso fix/search-bar-ime-enter-cjk)

Same exact keystrokes — first Return now lets the IME commit Point into the search field, the matches re-filter, and the second Return expands the intended Point class snippet into vim:

FIX: same keystrokes, IME commits then snippet expands correctly

Why this matters

Today SearchFrame::OnCharEvent unconditionally calls Submit() on WXK_RETURN / WXK_NUMPAD_ENTER. To understand why that's a hard blocker under a CJK IME, it helps to spell out how those IMEs work:

When a CJK IME is active, the keys you press do not go straight into the focused text field. They first land in the IME's own preedit buffer and are shown as underlined "marked text" inside the field. The user then has to actively tell the IME how to commit that buffer:

  • Space → convert to Chinese / kana candidates and pick the first one.
  • 1 / 2 / 3 / … → pick a numbered candidate.
  • Return → commit the raw Latin letters as-is (e.g. lee lands as the three ASCII characters l, e, e, not as 李/累/etc.). This is the only path that gets any text into the field if the user doesn't want IME conversion.

So Return here isn't an "optional submit shortcut" — it's the only way to land even ASCII letters into the search bar while a CJK IME layout is active. Espanso swallowing Return therefore has two failure modes stacked on top of each other:

  1. The search bar expands whatever match is highlighted at the moment the user is just trying to land a character (Chinese, kana, or even plain ASCII).
  2. Because Return never reaches the IME, no text at all — Chinese, kana, or raw Latin — can be typed into the search field while a CJK IME layout is the active one.

This makes the search bar effectively unusable for CJK users without first toggling the IME off to a plain US layout.

Approach

  • New espanso-modulo/src/sys/search/search_mac.mm exposes IsImeComposingInKeyWindow(), which asks the key window's first responder whether it has marked text via NSTextInputClient.hasMarkedText. NSTextField uses a shared field editor (an NSTextView), so the helper checks the responder directly, then falls back to currentEditor, then to the NSTextInputClient protocol.
  • The Return branch in OnCharEvent consults this on macOS before calling Submit(). If a composition is in progress, the event is passed through (event.Skip()) so the IME can finalize whatever it's holding (raw Latin letters or a converted candidate); otherwise the existing submit behavior is preserved.
  • build.rs is updated to compile the new .mm alongside the existing src/sys/common/mac.mm.
  • Windows and Linux code paths are untouched (guarded by #ifdef __WXOSX__). The same class of bug exists there but needs different APIs (IMM on Windows, IBus/preedit on Linux) — left as platform-specific follow-up.

This is the search-bar-UI counterpart of #1968, which is the same class of problem in the trigger engine.

Testing

Built and tested locally on macOS Tahoe 26 (Darwin 25.2.0), Apple Silicon:

  • CJK path: open search bar with ALT+SPACE while macOS Pinyin IME is active → type lee → Return lands lee (Latin literal) in the search field instead of triggering the highlight → matches re-filter on the new query → second Return (no marked text) submits the highlighted match into the target app, as before.
  • Non-IME path: with a plain US keyboard layout, Return still immediately submits the highlighted match. No behavior change vs. dev.
  • Verified end-to-end against ~/Library/Application Support/espanso/ with ~130 user-defined matches; expansion into the target app works correctly after a CJK-IME-mediated search session.

Follow-ups (intentionally out of scope)

Test plan

  • macOS Pinyin IME + search bar: Return lands Latin literal in field, doesn't fire highlight
  • macOS no-IME (US layout): Return still submits highlight (no regression)
  • End-to-end: search → CJK-IME-mediated query → snippet expansion into target app
  • Windows / Linux: not modified, not tested (out of scope)

The search bar's OnCharEvent unconditionally calls Submit() on Enter,
which "eats" the Enter that a user pressed to commit an IME candidate
(e.g. a Chinese / Japanese / Korean character selection). The result is
that the highlighted match is expanded the moment the user tries to
finalize their IME input, instead of the IME committing the candidate
into the search box.

Add a macOS-only helper (search_mac.mm) that asks the key window's
first responder whether it currently has marked text — that is the
canonical way to detect an in-progress IME composition on Cocoa
(NSTextInputClient.hasMarkedText). In OnCharEvent, if a composition
is in progress when Enter arrives, skip the event so the IME can
consume it, instead of submitting the search.

Behavior on Windows / Linux is unchanged; this path is guarded by
__WXOSX__ since the underlying detection is Cocoa-specific. Equivalent
detection on Windows (IMM API: ImmGetCompositionString) and on Linux
(IBus / fcitx via the IM module) can be added in follow-up commits.
@znzryb
znzryb requested a review from Archigos as a code owner May 17, 2026 16:17
@znzryb znzryb changed the title fix(search/macos): don't consume Return during IME composition fix(search/macos): don't consume Return during IME composition on macOS May 17, 2026
@AucaCoyan
AucaCoyan removed the request for review from Archigos July 19, 2026 11:55
@AucaCoyan
AucaCoyan merged commit a03ddea into espanso:dev Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Search bar consumes Enter during IME composition (CJK input methods)

2 participants