From 975d325a924a5a55d87d1ed4c3fd1cfb1de9e876 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 3 May 2003 21:49:19 +0000 Subject: Patch #731613: debugger tab-completion (thanks, Willem!) svn-id: r7293 --- gui/console.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- gui/console.h | 10 ++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) (limited to 'gui') diff --git a/gui/console.cpp b/gui/console.cpp index f531629d7d..c52c54a823 100644 --- a/gui/console.cpp +++ b/gui/console.cpp @@ -159,7 +159,7 @@ void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers) { // comply to the C++ standard, so we can't use a dynamic sized stack array. char *str = new char[len + 1]; - // Copy the user intput to str + // Copy the user input to str for (i = 0; i < len; i++) str[i] = _buffer[(_promptStartPos + i) % kBufferSize]; str[len] = '\0'; @@ -197,6 +197,33 @@ void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers) { scrollToCurrent(); draw(); // FIXME - not nice to redraw the full console just for one char! break; + case 9: // tab + { + if (_completionCallbackProc) { + int len = _currentPos - _promptStartPos; + assert(len >= 0); + char *str = new char[len + 1]; + + // Copy the user input to str + for (i = 0; i < len; i++) + str[i] = _buffer[(_promptStartPos + i) % kBufferSize]; + str[len] = '\0'; + + char *completion = 0; + if ((*_completionCallbackProc)(this, str, completion, + _callbackRefCon)) + { + if (_caretVisible) + drawCaret(true); + insertIntoPrompt(completion); + scrollToCurrent(); + draw(); + delete[] completion; + } + delete[] str; + } + break; + } case 127: killChar(); draw(); @@ -255,6 +282,18 @@ void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers) { } } +void ConsoleDialog::insertIntoPrompt(const char* str) +{ + unsigned int l = strlen(str); + for (int i = _promptEndPos-1; i >= _currentPos; i--) + _buffer[(i + l) % kBufferSize] = + _buffer[i % kBufferSize]; + for (unsigned int j = 0; j < l; ++j) { + _promptEndPos++; + putcharIntern(str[j]); + } +} + void ConsoleDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) { switch (cmd) { case kSetPositionCmd: diff --git a/gui/console.h b/gui/console.h index c0c806a9b1..4af9eafd2e 100644 --- a/gui/console.h +++ b/gui/console.h @@ -39,6 +39,7 @@ class ScrollBarWidget; class ConsoleDialog : public Dialog { public: typedef bool (*InputCallbackProc)(ConsoleDialog *console, const char *input, void *refCon); + typedef bool (*CompletionCallbackProc)(ConsoleDialog* console, const char *input, char*& completion, void *refCon); protected: char _buffer[kBufferSize]; @@ -63,6 +64,10 @@ protected: InputCallbackProc _callbackProc; void *_callbackRefCon; + // _completionCallbackProc is called when tab is pressed + CompletionCallbackProc _completionCallbackProc; + void *_completionCallbackRefCon; + char _history[kHistorySize][kLineBufferSize]; int _historySize; int _historyIndex; @@ -88,10 +93,15 @@ public: _callbackProc = proc; _callbackRefCon = refCon; } + void setCompletionCallback(CompletionCallbackProc proc, void *refCon) { + _completionCallbackProc = proc; + _completionCallbackRefCon = refCon; + } protected: void drawCaret(bool erase); void putcharIntern(int c); + void insertIntoPrompt(const char *str); void print(const char *str); void nextLine(); void updateScrollBar(); -- cgit v1.2.3