Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Basic bindkey functionality #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/linenoise.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
extern "C" {
#endif

typedef void (lineNoiseBindkeyFunction)(void);

typedef struct linenoiseCompletions linenoiseCompletions;

typedef void(linenoiseCompletionCallback)(const char*, linenoiseCompletions*);
Expand All @@ -61,6 +63,8 @@ void linenoiseHistoryFree(void);
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
int linenoiseBindkeyAdd(int key, lineNoiseBindkeyFunction *fn);
int linenoiseBindkeyRemove(int key);
/* the following is extension to the original linenoise API */
int linenoiseInstallWindowChangeHandler(void);

Expand Down
28 changes: 28 additions & 0 deletions src/linenoise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@

#include <string>
#include <vector>
#include <unordered_map>
#include <memory>

using std::string;
using std::vector;
using std::unordered_map;
using std::unique_ptr;
using namespace linenoise_ng;

Expand Down Expand Up @@ -913,6 +915,7 @@ static const int PAGE_DOWN_KEY = 0x11200000;

static const char* unsupported_term[] = {"dumb", "cons25", "emacs", NULL};
static linenoiseCompletionCallback* completionCallback = NULL;
static unordered_map<int, lineNoiseBindkeyFunction *> bindKeyFunctions;

#ifdef _WIN32
static HANDLE console_in, console_out;
Expand Down Expand Up @@ -1766,6 +1769,7 @@ static char32_t linenoiseReadChar(void) {
} else if (rec.Event.KeyEvent.uChar.UnicodeChar ==
ctrlChar('[')) { // ESC, set flag for later
escSeen = true;
return modifierKeys | rec.Event.KeyEvent.uChar.UnicodeChar;
continue;
} else {
// we got a real character, return it
Expand Down Expand Up @@ -1875,6 +1879,10 @@ static int cleanupCtrl(int c) {
c = c & ~CTRL;
}
}
auto found = bindKeyFunctions.find(c);
if (found != bindKeyFunctions.end()){
found->second();
}
return c;
}

Expand Down Expand Up @@ -3364,3 +3372,23 @@ int linenoiseInstallWindowChangeHandler(void) {
#endif
return 0;
}

int linenoiseBindkeyAdd(int key, lineNoiseBindkeyFunction *fn)
{
if (bindKeyFunctions.find(key) != bindKeyFunctions.end()){
// the key is already bound
return -1;
}
bindKeyFunctions.insert(std::make_pair(key, fn));
return 0;
}

int linenoiseBindkeyRemove(int key)
{
if (bindKeyFunctions.find(key) == bindKeyFunctions.end()){
// Key is not bound
return -1;
}
bindKeyFunctions.erase(key);
return 0;
}