Terminal: support bracketed paste.

Fixes #18029: Pasting into nano etc. eats newlines and indentation

Change-Id: I935696bb97f4d82b30f73cc488f6317cf847beae
Reviewed-on: https://review.haiku-os.org/c/haiku/+/5987
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
This commit is contained in:
Jessica Hamilton 2023-01-09 12:08:52 +13:00 committed by waddlesplash
parent 7003a4cb1d
commit 7a772a5586
7 changed files with 46 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2015, Haiku.
* Copyright 2001-2023, Haiku, Inc. All rights reserved.
* Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
* Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
*
@ -81,6 +81,7 @@ static const uint32 MSG_SET_TERMINAL_COLORS = 'setc';
static const uint32 MSG_RESET_TERMINAL_COLORS = 'rstc';
static const uint32 MSG_QUIT_TERMNAL = 'qutt';
static const uint32 MSG_ENABLE_META_KEY = 'emtk';
static const uint32 MSG_ENABLE_BRACKETED_PASTE = 'ebkp';
static const uint32 MSG_REPORT_MOUSE_EVENT = 'mous';
static const uint32 MSG_SAVE_WINDOW_POSITION = 'swps';
static const uint32 MSG_MOVE_TAB_LEFT = 'mvtl';

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2013, Haiku, Inc.
* Copyright 2001-2023, Haiku, Inc. All rights reserved.
* Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
* Distributed under the terms of the MIT license.
@ -1420,6 +1420,10 @@ TermParse::_DecPrivateModeSet(int value)
fBuffer->SaveCursor();
fBuffer->UseAlternateScreenBuffer(true);
break;
case 2004:
// Enable bracketed paste mode
fBuffer->EnableBracketedPasteMode(true);
break;
}
}
@ -1498,6 +1502,10 @@ TermParse::_DecPrivateModeReset(int value)
fBuffer->UseNormalScreenBuffer();
fBuffer->RestoreCursor();
break;
case 2004:
// Disable bracketed paste mode
fBuffer->EnableBracketedPasteMode(false);
break;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2014, Haiku, Inc.
* Copyright 2001-2023, Haiku, Inc. All rights reserved.
* Copyright 2003-2004 Kian Duffy, myob@users.sourceforge.net
* Parts Copyright 1998-1999 Kazuho Okui and Takashi Murai.
* All rights reserved. Distributed under the terms of the MIT license.
@ -316,6 +316,7 @@ TermView::_InitObject(const ShellParameters& shellParameters)
fUseOptionAsMetaKey = false;
fInterpretMetaKey = true;
fMetaKeySendsEscape = true;
fUseBracketedPaste = false;
fReportX10MouseEvent = false;
fReportNormalMouseEvent = false;
fReportButtonMouseEvent = false;
@ -922,7 +923,13 @@ TermView::Paste(BClipboard *clipboard)
ssize_t numBytes;
if (clipMsg->FindData("text/plain", B_MIME_TYPE,
(const void**)&text, &numBytes) == B_OK ) {
if (fUseBracketedPaste)
fShell->Write(BEGIN_BRACKETED_PASTE_CODE, strlen(BEGIN_BRACKETED_PASTE_CODE));
_WritePTY(text, numBytes);
if (fUseBracketedPaste)
fShell->Write(END_BRACKETED_PASTE_CODE, strlen(END_BRACKETED_PASTE_CODE));
}
clipboard->Unlock();
@ -1892,6 +1899,13 @@ TermView::MessageReceived(BMessage *message)
fMetaKeySendsEscape = enable;
break;
}
case MSG_ENABLE_BRACKETED_PASTE:
{
bool enable;
if (message->FindBool("enableBracketedPaste", &enable) == B_OK)
fUseBracketedPaste = enable;
break;
}
case MSG_REPORT_MOUSE_EVENT:
{
bool value;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2013, Haiku, Inc.
* Copyright 2001-2023, Haiku, Inc. All rights reserved.
* Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
*
@ -353,6 +353,7 @@ private:
bool fUseOptionAsMetaKey;
bool fInterpretMetaKey;
bool fMetaKeySendsEscape;
bool fUseBracketedPaste;
// mouse
int32 fMouseButtons;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013, Haiku, Inc. All rights reserved.
* Copyright 2013-2023, Haiku, Inc. All rights reserved.
* Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*
@ -113,6 +113,17 @@ TerminalBuffer::EnableMetaKeySendsEscape(bool enable)
}
void
TerminalBuffer::EnableBracketedPasteMode(bool enable)
{
if (fListenerValid) {
BMessage message(MSG_ENABLE_BRACKETED_PASTE);
message.AddBool("enableBracketedPaste", enable);
fListener.SendMessage(&message);
}
}
void
TerminalBuffer::ReportX10MouseEvent(bool reportX10MouseEvent)
{

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013, Haiku, Inc. All rights reserved.
* Copyright 2013-2023, Haiku, Inc. All rights reserved.
* Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*
@ -56,6 +56,7 @@ public:
void EnableInterpretMetaKey(bool enable);
void EnableMetaKeySendsEscape(bool enable);
void EnableBracketedPasteMode(bool enable);
void ReportX10MouseEvent(bool report);
void ReportNormalMouseEvent(bool report);

View File

@ -1,6 +1,6 @@
/*
* Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files or portions
@ -83,6 +83,9 @@
#define PAGE_UP_KEY_CODE "\033[5~"
#define PAGE_DOWN_KEY_CODE "\033[6~"
#define BEGIN_BRACKETED_PASTE_CODE "\033[200~"
#define END_BRACKETED_PASTE_CODE "\033[201~"
//#define IS_DOWN_KEY(x) (info.key_states[(x) / 8] & key_state_table[(x) % 8])
#define IS_DOWN_KEY(x) \
(info.key_states[(x) >> 3] & (1 << (7 - ((x) % 8))))