Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cut/Copy/Paste Context Menu #539

Merged
merged 4 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ void MainWindow::init(NeovimConnector *c)
m_tabline_bar->addWidget(m_tabline);
m_tabline_bar->setVisible(m_shell_options.enable_ext_tabline);

// Context menu and actions for right-click
m_contextMenu = new QMenu();
m_actCut = new QAction(QIcon::fromTheme("edit-cut"), QString("Cut"), nullptr /*parent*/);
m_actCopy = new QAction(QIcon::fromTheme("edit-copy"), QString("Copy"), nullptr /*parent*/);
m_actPaste = new QAction(QIcon::fromTheme("edit-paste"), QString("Paste"), nullptr /*parent*/);
m_actSelectAll = new QAction(QIcon::fromTheme("edit-select-all"), QString("Select All"),
nullptr /*parent*/);
m_contextMenu->addAction(m_actCut);
m_contextMenu->addAction(m_actCopy);
m_contextMenu->addAction(m_actPaste);
m_contextMenu->addSeparator();
m_contextMenu->addAction(m_actSelectAll);

m_nvim = c;

m_tree = new TreeView(c);
Expand Down Expand Up @@ -88,6 +101,16 @@ void MainWindow::init(NeovimConnector *c)
this, &MainWindow::neovimTablineUpdate);
connect(m_shell, &Shell::neovimShowtablineSet,
this, &MainWindow::neovimShowtablineSet);
connect(m_shell, &Shell::neovimShowContextMenu,
this, &MainWindow::neovimShowContextMenu);
connect(m_actCut, &QAction::triggered,
this, &MainWindow::neovimSendCut);
connect(m_actCopy, &QAction::triggered,
this, &MainWindow::neovimSendCopy);
connect(m_actPaste, &QAction::triggered,
this, &MainWindow::neovimSendPaste);
connect(m_actSelectAll, &QAction::triggered,
this, &MainWindow::neovimSendSelectAll);
m_shell->setFocus(Qt::OtherFocusReason);

if (m_nvim->errorCause()) {
Expand Down Expand Up @@ -338,6 +361,31 @@ void MainWindow::neovimTablineUpdate(int64_t curtab, QList<Tab> tabs)
Q_ASSERT(tabs.size() == m_tabline->count());
}

void MainWindow::neovimShowContextMenu()
{
m_contextMenu->popup(QCursor::pos());
}

void MainWindow::neovimSendCut()
{
m_nvim->api0()->vim_command_output(R"(normal! "+x)");
}

void MainWindow::neovimSendCopy()
{
m_nvim->api0()->vim_command(R"(normal! "+y)");
}

void MainWindow::neovimSendPaste()
{
m_nvim->api0()->vim_command(R"(normal! "+gP)");
}

void MainWindow::neovimSendSelectAll()
{
m_nvim->api0()->vim_command("normal! ggVG");
}

void MainWindow::changeTab(int index)
{
if (!m_shell_options.enable_ext_tabline) {
Expand Down
12 changes: 11 additions & 1 deletion src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ private slots:
void neovimIsUnsupported();
void neovimShowtablineSet(int);
void neovimTablineUpdate(int64_t curtab, QList<Tab> tabs);
void neovimShowContextMenu();
void neovimSendCut();
void neovimSendCopy();
void neovimSendPaste();
void neovimSendSelectAll();
void extTablineSet(bool);
void changeTab(int index);
private:
void init(NeovimConnector *);
NeovimConnector *m_nvim;
NeovimConnector *m_nvim;
ErrorWidget *m_errorWidget;
QSplitter *m_window;
TreeView *m_tree;
Expand All @@ -63,6 +68,11 @@ private slots:
QToolBar *m_tabline_bar;
ShellOptions m_shell_options;
bool m_neovim_requested_close;
QMenu *m_contextMenu;
QAction *m_actCut;
QAction *m_actCopy;
QAction *m_actPaste;
QAction *m_actSelectAll;
};

} // Namespace
Expand Down
8 changes: 8 additions & 0 deletions src/gui/runtime/doc/nvim_gui_shim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ This can be used to check for a specific GUI in ginit.vim, e.g.
This is a wrapper around |nvim_get_chan_info()|.

*GuiShowContextMenu()*
Displays a Cut-Copy-Paste context menu at the current cursor position. This
menu can be mapped to right click events in ginit.vim, e.g.
>
nnoremap <silent><RightMouse> :call GuiShowContextMenu()<CR>
inoremap <silent><RightMouse> <Esc>:call GuiShowContextMenu()<CR>
vnoremap <silent><RightMouse> :call GuiShowContextMenu()<CR>gv
==============================================================================
4. Internals

Expand Down
5 changes: 5 additions & 0 deletions src/gui/runtime/plugin/nvim_gui_shim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,8 @@ endfunction
command! GuiTreeviewToggle call <SID>TreeViewToggle()
noremap <script> <Plug>GuiTreeviewToggle :call <SID>TreeViewToggle()
anoremenu <script> Gui.Treeview.Toggle :call <SID>TreeViewShowToggle()

" Show Right-Click ContextMenu
function GuiShowContextMenu() range
call rpcnotify(0, 'Gui', 'ShowContextMenu')
endfunction
2 changes: 2 additions & 0 deletions src/gui/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,8 @@ void Shell::handleNeovimNotification(const QByteArray &name, const QVariantList&

qDebug() << "Neovim changed clipboard" << data << type << reg_name << clipboard;
QGuiApplication::clipboard()->setMimeData(clipData, clipboard);
} else if (guiEvName == "ShowContextMenu") {
emit neovimShowContextMenu();
}
return;
} else if (name != "redraw") {
Expand Down
1 change: 1 addition & 0 deletions src/gui/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Shell: public ShellWidget
/// as seen in Tab::tab.
void neovimTablineUpdate(int64_t curtab, QList<Tab> tabs);
void neovimShowtablineSet(int);
void neovimShowContextMenu();
void fontChanged();

public slots:
Expand Down