std::exception(const char*) seems to be an msvc extension...
[supertux.git] / src / console.cpp
index fba096f..ead9128 100644 (file)
 #include "main.hpp"
 #include "log.hpp"
 #include "resources.hpp"
+#include "gameconfig.hpp"
 
 /// speed (pixels/s) the console closes
 static const float FADE_SPEED = 1;
 
 Console::Console()
-  : history_position(history.end()), vm(NULL), backgroundOffset(0), 
+  : history_position(history.end()), vm(NULL), backgroundOffset(0),
     height(0), alpha(1.0), offset(0), focused(false), stayOpen(0) {
   fontheight = 8;
 }
 
-Console::~Console() 
+Console::~Console()
 {
   if(vm != NULL) {
     sq_release(Scripting::global_vm, &vm_object);
@@ -56,8 +57,8 @@ Console::init_graphics()
   background2.reset(new Surface("images/engine/console2.png"));
 }
 
-void 
-Console::flush(ConsoleStreamBuffer* buffer) 
+void
+Console::flush(ConsoleStreamBuffer* buffer)
 {
   if (buffer == &outputBuffer) {
     std::string s = outputBuffer.str();
@@ -93,7 +94,7 @@ Console::ready_vm()
       throw Scripting::SquirrelError(vm, "Couldn't get vm object for console");
     sq_addref(vm, &vm_object);
     sq_pop(vm, 1);
-    
+
     // create new roottable for thread
     sq_newtable(new_vm);
     sq_pushroottable(new_vm);
@@ -103,7 +104,7 @@ Console::ready_vm()
     sq_setroottable(new_vm);
 
     vm = new_vm;
-    
+
     try {
       std::string filename = "scripts/console.nut";
       IFileStream stream(filename);
@@ -119,15 +120,15 @@ Console::execute_script(const std::string& command)
 {
   using namespace Scripting;
 
-  ready_vm(); 
+  ready_vm();
 
-  SQInteger oldtop = sq_gettop(vm); 
+  SQInteger oldtop = sq_gettop(vm);
   try {
     if(SQ_FAILED(sq_compilebuffer(vm, command.c_str(), command.length(),
                  "", SQTrue)))
       throw SquirrelError(vm, "Couldn't compile command");
 
-    sq_pushroottable(vm); 
+    sq_pushroottable(vm);
     if(SQ_FAILED(sq_call(vm, 1, SQTrue, SQTrue)))
       throw SquirrelError(vm, "Problem while executing command");
 
@@ -244,7 +245,7 @@ sq_insert_commands(std::list<std::string>& cmds, HSQUIRRELVM vm, std::string tab
 }
 
 
-} 
+}
 // End of Console::autocomplete helper functions
 
 void
@@ -255,14 +256,6 @@ Console::autocomplete()
 
   std::list<std::string> cmds;
 
-  // append all known CCRs to list
-  for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
-    std::string cmdKnown = i->first;
-    if (cmdKnown.substr(0, prefix.length()) == prefix) {
-      cmds.push_back(cmdKnown);
-    }
-  }
-
   ready_vm();
 
   // append all keys of the current root table to list
@@ -283,27 +276,36 @@ Console::autocomplete()
   // depending on number of hits, show matches or autocomplete
   if (cmds.size() == 0) addLines("No known command starts with \""+prefix+"\"");
   if (cmds.size() == 1) {
+    // one match: just replace input buffer with full command
     inputBuffer.str(cmds.front());
     inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
   }
   if (cmds.size() > 1) {
+    // multiple matches: show all matches and set input buffer to longest common prefix
+    std::string commonPrefix = cmds.front();
     while (cmds.begin() != cmds.end()) {
-      addLines(cmds.front());
+      std::string cmd = cmds.front();
       cmds.pop_front();
+      addLines(cmd);
+      for (int n = commonPrefix.length(); n >= 1; n--) {
+        if (cmd.compare(0, n, commonPrefix) != 0) commonPrefix.resize(n-1); else break;
+      }
     }
+    inputBuffer.str(commonPrefix);
+    inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
   }
 }
 
-void 
-Console::addLines(std::string s) 
+void
+Console::addLines(std::string s)
 {
   std::istringstream iss(s);
   std::string line;
   while (std::getline(iss, line, '\n')) addLine(line);
 }
 
-void 
-Console::addLine(std::string s) 
+void
+Console::addLine(std::string s)
 {
   // output line to stderr
   std::cerr << s << std::endl;
@@ -318,7 +320,7 @@ Console::addLine(std::string s)
   // trim scrollback buffer
   while (lines.size() >= 1000)
     lines.pop_back();
+
   // increase console height if necessary
   if (height < 64) {
     if(height < 4)
@@ -335,7 +337,7 @@ Console::addLine(std::string s)
 }
 
 void
-Console::parse(std::string s) 
+Console::parse(std::string s)
 {
   // make sure we actually have something to parse
   if (s.length() == 0) return;
@@ -363,53 +365,22 @@ Console::parse(std::string s)
   // ignore if it's an internal command
   if (consoleCommand(command,args)) return;
 
-  // look up registered ccr
-  std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
-  if ((i == commands.end()) || (i->second.size() == 0)) {
-    try {
-      execute_script(s);
-    } catch(std::exception& e) {
-      addLines(e.what());
-    }
-    return;
+  try {
+    execute_script(s);
+  } catch(std::exception& e) {
+    addLines(e.what());
   }
 
-  // send command to the most recently registered ccr
-  ConsoleCommandReceiver* ccr = i->second.front();
-  if (ccr->consoleCommand(command, args) != true) log_warning << "Sent command to registered ccr, but command was unhandled" << std::endl;
 }
 
 bool
-Console::consoleCommand(std::string command, std::vector<std::string> arguments) 
+Console::consoleCommand(std::string /*command*/, std::vector<std::string> /*arguments*/)
 {
-  if (command == "ccrs") {
-    if (arguments.size() != 1) {
-      log_info << "Usage: ccrs <command>" << std::endl;
-      return true;
-    }
-    std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(arguments[0]);
-    if ((i == commands.end()) || (i->second.size() == 0)) {
-      log_info << "unknown command: \"" << arguments[0] << "\"" << std::endl;
-      return true;
-    }
-
-    std::ostringstream ccr_list;
-    std::list<ConsoleCommandReceiver*> &ccrs = i->second;
-    std::list<ConsoleCommandReceiver*>::iterator j;
-    for (j = ccrs.begin(); j != ccrs.end(); j++) {
-      if (j != ccrs.begin()) ccr_list << ", ";
-      ccr_list << "[" << *j << "]";
-    }
-
-    log_info << "registered ccrs for \"" << arguments[0] << "\": " << ccr_list.str() << std::endl;
-    return true;
-  }
-
   return false;
 }
 
 bool
-Console::hasFocus() 
+Console::hasFocus()
 {
   return focused;
 }
@@ -417,12 +388,16 @@ Console::hasFocus()
 void
 Console::show()
 {
+  if(!config->console_enabled)
+    return;
+
   focused = true;
   height = 256;
   alpha = 1.0;
+  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 }
 
-void 
+void
 Console::hide()
 {
   focused = false;
@@ -431,15 +406,16 @@ Console::hide()
 
   // clear input buffer
   inputBuffer.str(std::string());
+  SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
 }
 
-void 
+void
 Console::toggle()
 {
   if (Console::hasFocus()) {
-    Console::hide(); 
-  } 
-  else { 
+    Console::hide();
+  }
+  else {
     Console::show();
   }
 }
@@ -460,7 +436,7 @@ Console::update(float elapsed_time)
   }
 }
 
-void 
+void
 Console::draw(DrawingContext& context)
 {
   if (height == 0)
@@ -492,47 +468,11 @@ Console::draw(DrawingContext& context)
     if (py < -9) break;
     context.draw_text(font.get(), *i, Vector(4, py), LEFT_ALLIGN, layer);
   }
-
   context.pop_transform();
 }
 
-void 
-Console::registerCommand(std::string command, ConsoleCommandReceiver* ccr)
-{
-  commands[command].push_front(ccr);
-}
-
-void 
-Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr)
-{
-  std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
-  if ((i == commands.end()) || (i->second.size() == 0)) {
-    log_warning << "Command \"" << command << "\" not associated with a command receiver. Not dissociated." << std::endl;
-    return;
-  }
-  std::list<ConsoleCommandReceiver*>::iterator j = find(i->second.begin(), i->second.end(), ccr);
-  if (j == i->second.end()) {
-    log_warning << "Command \"" << command << "\" not associated with given command receiver. Not dissociated." << std::endl;
-    return;
-  }
-  i->second.erase(j);
-}
-
-void 
-Console::unregisterCommands(ConsoleCommandReceiver* ccr)
-{
-  for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
-    std::list<ConsoleCommandReceiver*> &ccrs = i->second;
-    std::list<ConsoleCommandReceiver*>::iterator j;
-    while ((j = find(ccrs.begin(), ccrs.end(), ccr)) != ccrs.end()) {
-      ccrs.erase(j);
-    }
-  }
-}
-
 Console* Console::instance = NULL;
 ConsoleStreamBuffer Console::inputBuffer;
 ConsoleStreamBuffer Console::outputBuffer;
 std::ostream Console::input(&Console::inputBuffer);
 std::ostream Console::output(&Console::outputBuffer);
-