Search Edit multi-level Undo/Redo without RichEdit formatting overhead (EM_SETTEXTMODE + TM_PLAINTEXT)

Have a suggestion for "Everything"? Please post it here.
minicom365
Posts: 1
Joined: Mon Nov 16, 2026 4:26 am

Search Edit multi-level Undo/Redo without RichEdit formatting overhead (EM_SETTEXTMODE + TM_PLAINTEXT)

Post by minicom365 »

Hi void, thank you as always for Everything.

Because Everything searches instantly as you type, I often refine complex queries iteratively without pressing Enter. When I press Ctrl+Z repeatedly in the search box to step back through my edits, it only toggles back and forth between the last two states (standard Win32
"Edit"
1-level
EM_UNDO
behavior) rather than rewinding step by step.
(Using
search_history_add_delay
with Alt+Left helps to some extent, but setting a short delay tends to saturate the 64-entry persistent search history dropdown with intermediate typing fragments.)

While reading the earlier threads on search edit multiple undo (t=10577) and syntax highlighting (t=16625), I noticed your comment regarding why switching from the stock Windows edit control to RichEdit was put off:
void wrote: The stock Windows edit control is rather limited. ...
I need to explore what is possible with a RichEdit control. This is also on my TODO list.
I don't want users to be able to format text in the search box so it would have to be trimmed down a lot.
Before writing a full custom edit control from scratch, I wanted to share a small Win32 detail that might save you from having to manually strip out formatting features:
EM_SETTEXTMODE
with
TM_PLAINTEXT | TM_MULTILEVELUNDO
on
MSFTEDIT_CLASS
(
Msftedit.dll
).

How it works:
By default,
MSFTEDIT_CLASS
(
L"RichEdit50W"
) starts in
TM_RICHTEXT
mode. However, if you send
EM_SETTEXTMODE
immediately after creating the control (while the text is still empty), the control locks itself into pure plain-text mode at the OS level while keeping the native multi-level undo/redo queue:

Code: Select all

#include <richedit.h>

// 1. Load system Msftedit.dll and create MSFTEDIT_CLASS (L"RichEdit50W") instead of L"Edit"
LoadLibraryW(L"Msftedit.dll");
HWND hwndSearchEdit = CreateWindowExW(
    0, MSFTEDIT_CLASS, L"",
    WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
    x, y, w, h, hwndParent, (HMENU)id, hInstance, NULL
);

// 2. Immediately after creation (while empty), lock into Plain-Text + Multi-Level Undo mode
SendMessageW(
    hwndSearchEdit,
    EM_SETTEXTMODE,
    TM_PLAINTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE,
    0
);

// 3. Enable EN_CHANGE / EN_UPDATE notifications to the parent window (disabled by default in RichEdit)
SendMessageW(hwndSearchEdit, EM_SETEVENTMASK, 0, ENM_CHANGE | ENM_UPDATE);
Why this avoids the "trimming down" work:
  • No RTF / formatting to strip (
    TM_PLAINTEXT
    ): Pasting formatted text from a browser or Word via Ctrl+V is automatically coerced to plain Unicode text (
    CF_UNICODETEXT
    ). Formatting shortcuts (Ctrl+B, Ctrl+I, Ctrl+U), font/size changes, and OLE object drops are natively disabled by the control itself.
  • Native Windows non-cycling multi-level Undo/Redo (
    TM_MULTILEVELUNDO
    ): Instead of cycling between two states on Ctrl+Z, it provides standard Windows typing coalescing (grouping consecutive characters/deletions naturally), stops cleanly at the initial empty state without wrapping around, and supports Ctrl+Y (
    EM_REDO
    ). It also uses the control's own isolated undo stack (configurable via
    EM_SETUNDOLIMIT
    ) so it never pollutes the persistent Search History.
  • Drop-in message compatibility: Standard edit messages (
    WM_GETTEXT
    ,
    WM_SETTEXT
    ,
    EM_GETSEL
    ,
    EM_SETSEL
    ,
    EM_REPLACESEL
    ) work the same way as the stock
    "Edit"
    control.
Two minor differences compared to stock
"Edit"
:
1.
EM_SETEVENTMASK
with
ENM_CHANGE
must be set once after creation so the parent window receives
WM_COMMAND
(
EN_CHANGE
) on each keystroke.
2. For dark mode colors,
MSFTEDIT_CLASS
uses
EM_SETBKGNDCOLOR
and
EM_SETCHARFORMAT
(
SCF_ALL
) rather than
WM_CTLCOLOREDIT
.

Hope this is useful whenever you get around to looking at the search box control. Thanks again!