From 2b6208917e92d93f94ad6620c5135d1bcd237ea8 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 9 Feb 2008 10:08:30 +0000 Subject: command completion (single match) --- src/core/commandbuffer.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ src/core/commandbuffer.h | 3 +++ src/core/cvar.h | 4 ++++ src/core/func.h | 5 +++++ 4 files changed, 55 insertions(+) (limited to 'src/core') diff --git a/src/core/commandbuffer.cc b/src/core/commandbuffer.cc index eaa8504..a46863c 100644 --- a/src/core/commandbuffer.cc +++ b/src/core/commandbuffer.cc @@ -10,6 +10,7 @@ // C++ headers #include #include +#include namespace core { @@ -73,6 +74,48 @@ void clear() while (core::cmd.getline(line, MAXCMDSIZE-1)); } +void complete(std::string &input, size_t &pos) +{ + std::list match; + + std::string partial = input.substr(0, pos); + if (!partial.size()) + return; + + // search function registry for matches + std::map::iterator f; + for (f = func::registry.begin(); f != func::registry.end(); f++) { + if (partial == (*f).first.substr(0, partial.size())) { + match.push_back((*f).first); + //con_print << " " << (*f).first << std::endl; + } + } + + // search cvar registry for matches + std::map::iterator c; + for (c = cvar::registry.begin(); c != cvar::registry.end(); c++) { + if (partial == (*c).first.substr(0, partial.size())) { + match.push_back((*c).first); + //con_print << " " << (*c).first << std::endl; + } + } + + if (!match.size()) + return; + + if (match.size() == 1) { + std::list::iterator l; + l = match.begin(); + input.replace(0, pos, (*l)); + pos = (*l).size(); + } else { + std::list::iterator l; + for (l = match.begin(); l !=match.end(); l++) + con_print << " " << (*l) << std::endl; + } + +} + } // namespace commandbuffer } // namespace core diff --git a/src/core/commandbuffer.h b/src/core/commandbuffer.h index c18acd0..6f5b324 100644 --- a/src/core/commandbuffer.h +++ b/src/core/commandbuffer.h @@ -26,6 +26,9 @@ void execute(); /// flush the command buffer void clear(); +/// tab completion +void complete(std::string &input, size_t &pos); + } } diff --git a/src/core/cvar.h b/src/core/cvar.h index 660f03e..205a416 100644 --- a/src/core/cvar.h +++ b/src/core/cvar.h @@ -8,6 +8,7 @@ #define __INCLUDED_CORE_CVAR_H__ #include +#include namespace core { @@ -77,6 +78,9 @@ Cvar find(const char *name); /// list the cvar registry void list(); +/// the Cvar registry +extern std::map registry; + } // namespace cvar } // namespace core diff --git a/src/core/func.h b/src/core/func.h index ad189b8..3a58295 100644 --- a/src/core/func.h +++ b/src/core/func.h @@ -10,6 +10,8 @@ #include "sys/sys.h" #include +#include +#include namespace core { @@ -34,6 +36,9 @@ Func find(const std::string &functionname); /// list the function registry void list(); +/// the function registry +extern std::map registry; + } // namespace func } // namespace core -- cgit v1.2.3