Repository: microsoft/terminal
File: src/cascadia/TerminalCore/Terminal.cpp (C++)

Code before:
    _renderSettings.SetColorTableEntry(TextColor::CURSOR_COLOR, til::color{ colorScheme.CursorColor });

    _renderSettings.MakeAdjustedColorArray();

    // Tell the control that the scrollbar has somehow changed. Used as a
    // workaround to force the control to redraw any scrollbar marks whose color
    // may have changed.
    _NotifyScrollEvent();
}

bool Terminal::_inAltBuffer() const noexcept
{
    return _altBuffer != nullptr;
}

TextBuffer& Terminal::_activeBuffer() const noexcept
{
    return _inAltBuffer() ? *_altBuffer : *_mainBuffer;
}

void Terminal::_updateUrlDetection()
{
    if (_detectURLs)
    {
        // Add regex pattern recognizers to the buffer
        // For now, we only add the URI regex pattern
        _hyperlinkPatternId = _activeBuffer().AddPatternRecognizer(linkPattern);
        UpdatePatternsUnderLock();
    }
    else
    {
        ClearPatternTree();
    }
}

// NOTE: This is the version of AddMark that comes from the UI. The VT api call into this too.
void Terminal::AddMark(const Microsoft::Console::VirtualTerminal::DispatchTypes::ScrollMark& mark,
                       const til::point& start,
                       const til::point& end,
                       const bool fromUi)
{
    if (_inAltBuffer())
    {
        return;
    }

    DispatchTypes::ScrollMark m = mark;
    m.start = start;
    m.end = end;

    if (fromUi)

Review comment (bug):
I believe that using indices would be better than using `.back()` and adding `.insert()`.
Rust exposes this problem much more obviously, as you can't "borrow" foreign data easily there without introducing lifetime issues. A common solution is to instead save a numeric index (or similar) of where the data is stored at. In this case you could store a `size_t _currentPromptMarkIndex` which stores the index of the mark inside `_scrollMarks`, so that you can safely retrieve it from `_scrollMarks` at a later time.
It's basically exactly like the `_currentPrompt` pointer since it's still sort of a (relative) memory location, but unlike it, it's much easier to prove that it doesn't reference invalid data (by checking that `_currentPromptMarkIndex < .size()`). This way you don't need to `insert()` user marks at the beginning either, allowing you to remove this boolean parameter. Such a numeric index doesn't fix any "stateful" bugs of course, but it does prevent memory lifetime issues, as was the case with the raw pointer.

Code after:

TextBuffer& Terminal::_activeBuffer() const noexcept
{
    return _inAltBuffer() ? *_altBuffer : *_mainBuffer;
}

void Terminal::_updateUrlDetection()
{
    if (_detectURLs)
    {
        // Add regex pattern recognizers to the buffer
        // For now, we only add the URI regex pattern
        _hyperlinkPatternId = _activeBuffer().AddPatternRecognizer(linkPattern);
        UpdatePatternsUnderLock();
    }
    else
    {
        ClearPatternTree();
    }
}

// NOTE: This is the version of AddMark that comes from the UI. The VT api call into this too.
void Terminal::AddMark(const Microsoft::Console::VirtualTerminal::DispatchTypes::ScrollMark& mark,
                       const til::point& start,
                       const til::point& end,
                       const bool fromUi)
{
    if (_inAltBuffer())
    {
        return;
    }

    DispatchTypes::ScrollMark m = mark;
    m.start = start;
    m.end = end;

    if (fromUi)
    {
        _scrollMarks.insert(_scrollMarks.begin(), m);
    }
    else
    {
        _scrollMarks.push_back(m);
    }

    // Tell the control that the scrollbar has somehow changed. Used as a
    // workaround to force the control to redraw any scrollbar marks
    _NotifyScrollEvent();

    // DON'T set _currentPrompt. The VT impl will do that for you. We don't want
    // UI-driven marks to set that.