Repository: microsoft/terminal
File: src/cascadia/TerminalApp/AppActionHandlers.cpp (C++)

Code before:
        _renamerPressedEnter = false;
        WindowRenamer().IsOpen(true);

        args.Handled(true);
    }

    void TerminalPage::_HandleSearchForText(const IInspectable& /*sender*/,
                                            const ActionEventArgs& args)
    {
        if (args)
        {
            if (const auto& realArgs = args.ActionArgs().try_as<SearchForTextArgs>())
            {
                if (const auto termControl{ _GetActiveControl() })
                {
                    if (termControl.HasSelection())
                    {
                        const auto selections{ termControl.SelectedText(true) };

                        // concatenate the selection into a single line
                        auto searchText = std::accumulate(selections.begin(), selections.end(), std::wstring());

                        // make it compact by replacing consecutive whitespaces with a single space
                        searchText = std::regex_replace(searchText, std::wregex(LR"(\s+)"), L" ");

                        if (realArgs.WrapWithQuotes())
                        {
                            searchText.insert(searchText.begin(), L'"');
                            searchText.push_back(L'"');
                        }

                        const auto queryUrl = realArgs.QueryUrl().empty() ? _settings.GlobalSettings().SearchWebQueryUrlDefault() : realArgs.QueryUrl();
                        const auto finalString = queryUrl + Windows::Foundation::Uri::EscapeComponent(searchText);
                        winrt::Microsoft::Terminal::Control::OpenHyperlinkEventArgs shortcut{ finalString };
                        _OpenHyperlinkHandler(termControl, shortcut);
                        args.Handled(true);
                    }
                }
            }
        }
    }

    void TerminalPage::_HandleGlobalSummon(const IInspectable& /*sender*/,
                                           const ActionEventArgs& args)
    {
        // Manually return false. These shouldn't ever get here, except for when
        // we fail to register for the global hotkey. In that case, returning
        // false here will let the underlying terminal still process the key, as
        // if it wasn't bound at all.
        args.Handled(false);
    }

Review comment (suggestion):
alright, so I've got an idea here. It kills two of my concerns!

When you set up a search engine shortcut in ~~Firefox~~ Microsoft Edge (:wink:) it gives you a special string, `%s`, that you can insert into the query.

<img width="320" alt="image" src="https://github.com/microsoft/terminal/assets/189190/c5bfce28-94fd-4d76-9d8d-4875686fb119">

If we use `%s` just like them, we get some benefits:

1. we don't have to append, so people can put the query argument anywhere
2. we don't need `wrapWithQuotes`, because people can put quotes directly in the URL!

Code after:
        _renamerPressedEnter = false;
        WindowRenamer().IsOpen(true);

        args.Handled(true);
    }

    void TerminalPage::_HandleSearchForText(const IInspectable& /*sender*/,
                                            const ActionEventArgs& args)
    {
        if (const auto termControl{ _GetActiveControl() })
        {
            if (termControl.HasSelection())
            {
                const auto selections{ termControl.SelectedText(true) };

                // concatenate the selection into a single line
                auto searchText = std::accumulate(selections.begin(), selections.end(), std::wstring());

                // make it compact by replacing consecutive whitespaces with a single space
                searchText = std::regex_replace(searchText, std::wregex(LR"(\s+)"), L" ");

                std::wstring queryUrl;
                if (args)
                {
                    if (const auto& realArgs = args.ActionArgs().try_as<SearchForTextArgs>())
                    {
                        queryUrl = realArgs.QueryUrl().c_str();
                    }
                }

                // use global default if query URL is unspecified
                if (queryUrl.empty())
                {
                    queryUrl = _settings.GlobalSettings().SearchWebDefaultQueryUrl().c_str();
                }

                constexpr std::wstring_view queryToken{ L"%s" };
                if (const auto pos{ queryUrl.find(queryToken) }; pos != std::wstring_view::npos)
                {
                    queryUrl.replace(pos, queryToken.length(), Windows::Foundation::Uri::EscapeComponent(searchText));
                }

                winrt::Microsoft::Terminal::Control::OpenHyperlinkEventArgs shortcut{ queryUrl };
                _OpenHyperlinkHandler(termControl, shortcut);
                args.Handled(true);
            }
        }
    }

    void TerminalPage::_HandleGlobalSummon(const IInspectable& /*sender*/,
                                           const ActionEventArgs& args)