From f633e42597bff0f57b5efbf7f83bbe3d9d13f8bc Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 4 Jul 2009 00:35:50 -0400 Subject: [PATCH] Enhancement - _commands - Added undocumented _commands command to support completion scripts. --- src/Cmd.cpp | 11 +++++++++++ src/Cmd.h | 1 + src/Context.cpp | 1 + src/command.cpp | 14 ++++++++++++++ src/main.h | 1 + 5 files changed, 28 insertions(+) diff --git a/src/Cmd.cpp b/src/Cmd.cpp index 63f019c14..abadc60e1 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -108,6 +108,7 @@ void Cmd::load () { commands.push_back ("_projects"); commands.push_back ("_tags"); + commands.push_back ("_commands"); commands.push_back (context.stringtable.get (CMD_ADD, "add")); commands.push_back (context.stringtable.get (CMD_APPEND, "append")); commands.push_back (context.stringtable.get (CMD_ANNOTATE, "annotate")); @@ -180,12 +181,22 @@ void Cmd::allCustomReports (std::vector & all) const all = customReports; } +//////////////////////////////////////////////////////////////////////////////// +void Cmd::allCommands (std::vector & all) const +{ + all.clear (); + foreach (command, commands) + if (command->substr (0, 1) != "_") + all.push_back (*command); +} + //////////////////////////////////////////////////////////////////////////////// // Commands that do not directly modify the data files. bool Cmd::isReadOnlyCommand () { if (command == "_projects" || command == "_tags" || + command == "_commands" || command == context.stringtable.get (CMD_CALENDAR, "calendar") || command == context.stringtable.get (CMD_COLORS, "colors") || command == context.stringtable.get (CMD_EXPORT, "export") || diff --git a/src/Cmd.h b/src/Cmd.h index 314a6802d..07889417f 100644 --- a/src/Cmd.h +++ b/src/Cmd.h @@ -44,6 +44,7 @@ public: bool validCustom (const std::string&); void parse (const std::string&); void allCustomReports (std::vector &) const; + void allCommands (std::vector &) const; bool isReadOnlyCommand (); bool isWriteCommand (); diff --git a/src/Context.cpp b/src/Context.cpp index efac49918..a779ba8ec 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -198,6 +198,7 @@ std::string Context::dispatch () else if (cmd.command == "undo") { handleUndo (); } else if (cmd.command == "_projects") { out = handleCompletionProjects (); } else if (cmd.command == "_tags") { out = handleCompletionTags (); } + else if (cmd.command == "_commands") { out = handleCompletionCommands (); } else if (cmd.command == "" && sequence.size ()) { out = handleModify (); } diff --git a/src/command.cpp b/src/command.cpp index 3281cffcb..70f0f422a 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -315,6 +315,20 @@ std::string handleCompletionTags () return out.str (); } +//////////////////////////////////////////////////////////////////////////////// +std::string handleCompletionCommands () +{ + std::vector commands; + context.cmd.allCommands (commands); + std::sort (commands.begin (), commands.end ()); + + std::stringstream out; + foreach (command, commands) + out << *command << std::endl; + + return out.str (); +} + //////////////////////////////////////////////////////////////////////////////// void handleUndo () { diff --git a/src/main.h b/src/main.h index 7993cae51..95a98a710 100644 --- a/src/main.h +++ b/src/main.h @@ -63,6 +63,7 @@ std::string handleProjects (); std::string handleCompletionProjects (); std::string handleTags (); std::string handleCompletionTags (); +std::string handleCompletionCommands (); std::string handleVersion (); std::string handleDelete (); std::string handleStart ();