From e98699564b8dc81f7fe3d98446e1e967b9f2cb70 Mon Sep 17 00:00:00 2001 From: RadAd Date: Fri, 24 May 2019 16:26:06 +1000 Subject: Trim space from the end of lines when copying to clipboard --- RadTerminal.cpp | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/RadTerminal.cpp b/RadTerminal.cpp index 1d5e812..7d91526 100644 --- a/RadTerminal.cpp +++ b/RadTerminal.cpp @@ -686,8 +686,40 @@ void RadTerminalWindowOnRButtonDown(HWND hWnd, BOOL fDoubleClick, int x, int y, int len = tsm_screen_selection_copy(data->screen, &buf); if (len > 0) { - HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len); - memcpy(GlobalLock(hMem), buf, len); + { // trim empty space from end of lines + char* lastnonnull = nullptr; + for (int i = 0; i < len; ++i) + { + switch (buf[i]) + { + case ' ': + case '\0': + //buf[i] = ' '; + break; + + case '\n': + if (lastnonnull != nullptr) + { + strncpy_s(lastnonnull, len - (lastnonnull - buf), buf + i, len - i); // TODO len should be len - (lastnonnull - buf) I think + int cut = (int) ((buf + i) - lastnonnull); + len -= cut; + i -= cut; + } + // fallthrough + + default: + lastnonnull = buf + i + 1; + break; + } + } + + *lastnonnull = '\0'; + int cut = (int) ((buf + len) - lastnonnull); + len -= cut; + } + + HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len + 1); + memcpy(GlobalLock(hMem), buf, len + 1); GlobalUnlock(hMem); while (!OpenClipboard(hWnd)) ; -- cgit v1.2.3