Commit 2d551de0 authored by hjk's avatar hjk
Browse files

fakevim: make recognition of ex command names similar to vi's

parent e7e0ead0
...@@ -302,6 +302,11 @@ ExCommand::ExCommand(const QString &c, const QString &a, const Range &r) ...@@ -302,6 +302,11 @@ ExCommand::ExCommand(const QString &c, const QString &a, const Range &r)
: cmd(c), hasBang(false), args(a), range(r), count(1) : cmd(c), hasBang(false), args(a), range(r), count(1)
{} {}
bool ExCommand::matches(const QString &min, const QString &full) const
{
return cmd.startsWith(min) && full.startsWith(cmd);
}
QDebug operator<<(QDebug ts, const ExCommand &cmd) QDebug operator<<(QDebug ts, const ExCommand &cmd)
{ {
return ts << cmd.cmd << ' ' << cmd.args << ' ' << cmd.range; return ts << cmd.cmd << ' ' << cmd.args << ' ' << cmd.range;
...@@ -3133,8 +3138,8 @@ bool FakeVimHandler::Private::handleExMapCommand(const ExCommand &cmd0) // :map ...@@ -3133,8 +3138,8 @@ bool FakeVimHandler::Private::handleExMapCommand(const ExCommand &cmd0) // :map
bool FakeVimHandler::Private::handleExHistoryCommand(const ExCommand &cmd) bool FakeVimHandler::Private::handleExHistoryCommand(const ExCommand &cmd)
{ {
// :history // :his[tory]
if (cmd.cmd != "his" && cmd.cmd != "history") if (!cmd.matches("his", "history"))
return false; return false;
if (cmd.args.isEmpty()) { if (cmd.args.isEmpty()) {
...@@ -3155,9 +3160,8 @@ bool FakeVimHandler::Private::handleExHistoryCommand(const ExCommand &cmd) ...@@ -3155,9 +3160,8 @@ bool FakeVimHandler::Private::handleExHistoryCommand(const ExCommand &cmd)
bool FakeVimHandler::Private::handleExRegisterCommand(const ExCommand &cmd) bool FakeVimHandler::Private::handleExRegisterCommand(const ExCommand &cmd)
{ {
// :reg and :di[splay] // :reg[isters] and :di[splay]
if (cmd.cmd != "reg" && cmd.cmd != "registers" if (!cmd.matches("reg", "registers") && !cmd.matches("di", "display"))
&& cmd.cmd != "di" && cmd.cmd != "display")
return false; return false;
QByteArray regs = cmd.args.toLatin1(); QByteArray regs = cmd.args.toLatin1();
...@@ -3183,8 +3187,8 @@ bool FakeVimHandler::Private::handleExRegisterCommand(const ExCommand &cmd) ...@@ -3183,8 +3187,8 @@ bool FakeVimHandler::Private::handleExRegisterCommand(const ExCommand &cmd)
bool FakeVimHandler::Private::handleExSetCommand(const ExCommand &cmd) bool FakeVimHandler::Private::handleExSetCommand(const ExCommand &cmd)
{ {
// :set // :se[t]
if (cmd.cmd != "se" && cmd.cmd != "set") if (!cmd.matches("se", "set"))
return false; return false;
showBlackMessage(QString()); showBlackMessage(QString());
...@@ -3224,8 +3228,8 @@ bool FakeVimHandler::Private::handleExSetCommand(const ExCommand &cmd) ...@@ -3224,8 +3228,8 @@ bool FakeVimHandler::Private::handleExSetCommand(const ExCommand &cmd)
bool FakeVimHandler::Private::handleExNormalCommand(const ExCommand &cmd) bool FakeVimHandler::Private::handleExNormalCommand(const ExCommand &cmd)
{ {
// :normal // :norm[al]
if (cmd.cmd != "norm" && cmd.cmd != "normal") if (!cmd.matches("norm", "normal"))
return false; return false;
//qDebug() << "REPLAY NORMAL: " << quoteUnprintable(reNormal.cap(3)); //qDebug() << "REPLAY NORMAL: " << quoteUnprintable(reNormal.cap(3));
replay(cmd.args, 1); replay(cmd.args, 1);
...@@ -3234,8 +3238,8 @@ bool FakeVimHandler::Private::handleExNormalCommand(const ExCommand &cmd) ...@@ -3234,8 +3238,8 @@ bool FakeVimHandler::Private::handleExNormalCommand(const ExCommand &cmd)
bool FakeVimHandler::Private::handleExDeleteCommand(const ExCommand &cmd) bool FakeVimHandler::Private::handleExDeleteCommand(const ExCommand &cmd)
{ {
// :delete // :d[elete]
if (cmd.cmd != "d" && cmd.cmd != "delete") if (!cmd.matches("d", "delete"))
return false; return false;
setCurrentRange(cmd.range); setCurrentRange(cmd.range);
...@@ -3314,8 +3318,8 @@ bool FakeVimHandler::Private::handleExWriteCommand(const ExCommand &cmd) ...@@ -3314,8 +3318,8 @@ bool FakeVimHandler::Private::handleExWriteCommand(const ExCommand &cmd)
bool FakeVimHandler::Private::handleExReadCommand(const ExCommand &cmd) bool FakeVimHandler::Private::handleExReadCommand(const ExCommand &cmd)
{ {
// :read // :r[ead]
if (cmd.cmd != "r" && cmd.cmd != "read") if (!cmd.matches("r", "read"))
return false; return false;
beginEditBlock(); beginEditBlock();
......
...@@ -64,6 +64,8 @@ struct ExCommand ...@@ -64,6 +64,8 @@ struct ExCommand
ExCommand(const QString &cmd, const QString &args = QString(), ExCommand(const QString &cmd, const QString &args = QString(),
const Range &range = Range()); const Range &range = Range());
bool matches(const QString &min, const QString &full) const;
QString cmd; QString cmd;
bool hasBang; bool hasBang;
QString args; QString args;
......
...@@ -910,7 +910,8 @@ void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd) ...@@ -910,7 +910,8 @@ void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd)
QTC_ASSERT(editorManager(), return); QTC_ASSERT(editorManager(), return);
*handled = true; *handled = true;
if (cmd.cmd == "w" || cmd.cmd == "write") { if (cmd.matches("w", "write")) {
// :w[rite]
Core::IEditor *editor = m_editorToHandler.key(handler); Core::IEditor *editor = m_editorToHandler.key(handler);
const QString fileName = handler->currentFileName(); const QString fileName = handler->currentFileName();
if (editor && editor->file()->fileName() == fileName) { if (editor && editor->file()->fileName() == fileName) {
...@@ -929,8 +930,8 @@ void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd) ...@@ -929,8 +930,8 @@ void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd)
} else { } else {
handler->showRedMessage(tr("File not saved")); handler->showRedMessage(tr("File not saved"));
} }
} else if (cmd.cmd == "wa" || cmd.cmd == "wall") { } else if (cmd.matches("wa", "wall")) {
// :wa // :w[all]
FileManager *fm = ICore::instance()->fileManager(); FileManager *fm = ICore::instance()->fileManager();
QList<IFile *> toSave = fm->modifiedFiles(); QList<IFile *> toSave = fm->modifiedFiles();
QList<IFile *> failed = fm->saveModifiedFilesSilently(toSave); QList<IFile *> failed = fm->saveModifiedFilesSilently(toSave);
...@@ -938,24 +939,24 @@ void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd) ...@@ -938,24 +939,24 @@ void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd)
handler->showBlackMessage(tr("Saving succeeded")); handler->showBlackMessage(tr("Saving succeeded"));
else else
handler->showRedMessage(tr("%n files not saved", 0, failed.size())); handler->showRedMessage(tr("%n files not saved", 0, failed.size()));
} else if (cmd.cmd == "q" || cmd.cmd == "quit") { } else if (cmd.matches("q", "quit")) {
// :q[uit] // :q[uit]
emit delayedQuitRequested(cmd.hasBang, m_editorToHandler.key(handler)); emit delayedQuitRequested(cmd.hasBang, m_editorToHandler.key(handler));
} else if (cmd.cmd == "qa" || cmd.cmd == "qall") { } else if (cmd.matches("qa", "qall")) {
// :qa // :qa[ll]
emit delayedQuitAllRequested(cmd.hasBang); emit delayedQuitAllRequested(cmd.hasBang);
} else if (cmd.cmd == "sp" || cmd.cmd == "split") { } else if (cmd.matches("sp", "split")) {
// :sp[lit] // :sp[lit]
triggerAction(Core::Constants::SPLIT); triggerAction(Core::Constants::SPLIT);
} else if (cmd.cmd == "vs" || cmd.cmd == "vsplit") { } else if (cmd.matches("vs", "vsplit")) {
// :vs[plit] // :vs[plit]
triggerAction(Core::Constants::SPLIT_SIDE_BY_SIDE); triggerAction(Core::Constants::SPLIT_SIDE_BY_SIDE);
} else if (cmd.cmd == "mak" || cmd.cmd == "make") { } else if (cmd.matches("mak", "make")) {
// :mak[e][!] [arguments] // :mak[e][!] [arguments]
triggerAction(ProjectExplorer::Constants::BUILD); triggerAction(ProjectExplorer::Constants::BUILD);
} else if (cmd.cmd == "se" || cmd.cmd == "set") { } else if (cmd.matches("se", "set")) {
if (cmd.args.isEmpty()) { if (cmd.args.isEmpty()) {
// :set // :se[t]
showSettingsDialog(); showSettingsDialog();
} else if (cmd.args == "ic" || cmd.args == "ignorecase") { } else if (cmd.args == "ic" || cmd.args == "ignorecase") {
// :set noic // :set noic
...@@ -966,21 +967,20 @@ void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd) ...@@ -966,21 +967,20 @@ void FakeVimPluginPrivate::handleExCommand(bool *handled, const ExCommand &cmd)
setActionChecked(Find::Constants::CASE_SENSITIVE, true); setActionChecked(Find::Constants::CASE_SENSITIVE, true);
*handled = false; // Let the handler see it as well. *handled = false; // Let the handler see it as well.
} }
} else if (cmd.cmd == "n" || cmd.cmd == "next") { } else if (cmd.matches("n", "next")) {
// :n[ext] // :n[ext]
switchToFile(currentFile() + cmd.count); switchToFile(currentFile() + cmd.count);
} else if (cmd.cmd == "prev" || cmd.cmd == "previous" } else if (cmd.matches("prev", "previous") || cmd.matches("N", "Next")) {
|| cmd.cmd == "N" || cmd.cmd == "Next") { // :prev[ious], :N[ext]
// :prev[ious]
switchToFile(currentFile() - cmd.count); switchToFile(currentFile() - cmd.count);
} else if (cmd.cmd == "bn" || cmd.cmd == "bnext") { } else if (cmd.matches("bn", "bnext")) {
// :bn[ext] // :bn[ext]
switchToFile(currentFile() + cmd.count); switchToFile(currentFile() + cmd.count);
} else if (cmd.cmd == "bp" || cmd.cmd == "bprevious" } else if (cmd.matches("bp", "bprevious") || cmd.matches("bN", "bNext")) {
|| cmd.cmd == "bN" || cmd.cmd == "bNext") { // :bp[revious], :bN[ext]
// :bp[revious]
switchToFile(currentFile() - cmd.count); switchToFile(currentFile() - cmd.count);
} else if (cmd.cmd == "on" || cmd.cmd == "only") { } else if (cmd.matches("on", "only")) {
// :on[ly]
//triggerAction(Core::Constants::REMOVE_ALL_SPLITS); //triggerAction(Core::Constants::REMOVE_ALL_SPLITS);
triggerAction(Core::Constants::REMOVE_CURRENT_SPLIT); triggerAction(Core::Constants::REMOVE_CURRENT_SPLIT);
} else { } else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment