Skip to content

Commit

Permalink
Add support for quickly reading text pasted into the terminal
Browse files Browse the repository at this point in the history
Fixes #1.
  • Loading branch information
magiblot committed Nov 1, 2020
1 parent 941fc34 commit 125e749
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
44 changes: 43 additions & 1 deletion src/docview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ void DocumentView::handleEvent(TEvent &ev)
case evKeyDown:
if (ev.keyDown.keyCode == kbIns)
setState(sfCursorIns, Boolean(!getState(sfCursorIns)));
editor.KeyDownWithModifiers(ev.keyDown, nullptr);
// If we always began reading events in consumeInputText,
// we would never autoindent newlines.
if (!ev.keyDown.textLength)
editor.KeyDownWithModifiers(ev.keyDown, nullptr);
else
consumeInputText(ev);
handled = true;
break;
case evMouseDown:
Expand Down Expand Up @@ -75,6 +80,43 @@ void DocumentView::handleEvent(TEvent &ev)
}
}

void DocumentView::consumeInputText(TEvent &ev)
{
size_t count = 0;
std::vector<char> buf;
auto push = [&buf] (std::string_view text) {
buf.insert(buf.end(), text.data(), text.data()+text.size());
};

do {
if (ev.what == evKeyDown) {
if (ev.keyDown.keyCode == kbEnter)
push("\n");
else if (ev.keyDown.keyCode == kbTab)
push("\t");
else if (ev.keyDown.textLength)
push(ev.keyDown.asText());
else
break;
} else
break;
++count;
getImmediateEvent(ev);
} while (true);
// Put non-text event back into the queue.
if (count && ev.what != evNothing)
putEvent(ev);

if (buf.size()) {
if (count > 2) // 1 may be too easy to trigger just by typing fast.
editor.WndProc(SCI_BEGINUNDOACTION, 0U, 0U);
editor.pasteText({buf.data(), buf.size()});
editor.WndProc(SCI_SCROLLCARET, 0U, 0U);
if (count > 2)
editor.WndProc(SCI_ENDUNDOACTION, 0U, 0U);
}
}

void DocumentView::draw()
{
auto [x, y] = editor.getCaretPosition();
Expand Down
2 changes: 2 additions & 0 deletions src/docview.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct DocumentView : public TSurfaceView {
void handleEvent(TEvent &ev) override;
void draw() override;

void consumeInputText(TEvent &ev);

};

#endif
1 change: 1 addition & 0 deletions src/editwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ void EditorWindow::notify(SCNotification scn)
case SCN_CHARADDED:
if (scn.ch == '\n')
indent.autoIndentCurrentLine(editor);
break;
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/tscintilla.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <tvision/tv.h>

#include <ScintillaHeaders.h>
#include <string_view>

struct DocumentView;
class TDrawSurface;
Expand Down Expand Up @@ -55,6 +56,7 @@ struct TScintillaEditor : public ScintillaBase {
void setWindow(TDrawSurface *wid);
void setParent(TScintillaWindow *parent_);
void changeSize();
void pasteText(std::string_view text);
Sci::Line getFirstVisibleDocumentLine();
TPoint getCaretPosition();
TPoint getDelta();
Expand All @@ -76,6 +78,11 @@ inline void TScintillaEditor::changeSize()
ScintillaBase::ChangeSize();
}

inline void TScintillaEditor::pasteText(std::string_view text)
{
InsertPasteShape(text.data(), text.size(), pasteStream);
}

inline Sci::Line TScintillaEditor::getFirstVisibleDocumentLine()
{
return pcs->DocFromDisplay(topLine);
Expand Down

0 comments on commit 125e749

Please sign in to comment.