emacs keybindings and tab completion, thanks David J Burger
This commit is contained in:
parent
fc5de0455e
commit
489af9787c
2 changed files with 245 additions and 66 deletions
|
@ -1,5 +1,5 @@
|
||||||
// FbRun.hh
|
// FbRun.cc
|
||||||
// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
|
// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen<at>users.sourceforge.net)
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
// copy of this software and associated documentation files (the "Software"),
|
||||||
|
@ -19,7 +19,7 @@
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
// $Id: FbRun.cc,v 1.11 2003/04/27 02:26:21 rathnor Exp $
|
// $Id: FbRun.cc,v 1.12 2003/06/24 10:22:42 fluxgen Exp $
|
||||||
|
|
||||||
#include "FbRun.hh"
|
#include "FbRun.hh"
|
||||||
|
|
||||||
|
@ -50,13 +50,17 @@ FbRun::FbRun(int x, int y, size_t width):
|
||||||
m_end(false),
|
m_end(false),
|
||||||
m_current_history_item(0),
|
m_current_history_item(0),
|
||||||
m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)),
|
m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)),
|
||||||
m_cursor_pos(0) {
|
m_start_pos(0),
|
||||||
XDefineCursor(FbTk::App::instance()->display(), m_win.window(), m_cursor);
|
m_end_pos(0),
|
||||||
|
m_cursor_pos(0)
|
||||||
|
{
|
||||||
|
m_win.setCursor(m_cursor);
|
||||||
// setting nomaximize in local resize
|
// setting nomaximize in local resize
|
||||||
resize(width, m_font.height());
|
resize(width, m_font.height());
|
||||||
FbTk::EventManager::instance()->registerEventHandler(*this, m_win.window());
|
FbTk::EventManager::instance()->registerEventHandler(*this, m_win.window());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FbRun::~FbRun() {
|
FbRun::~FbRun() {
|
||||||
hide();
|
hide();
|
||||||
FbTk::EventManager::instance()->unregisterEventHandler(m_win.window());
|
FbTk::EventManager::instance()->unregisterEventHandler(m_win.window());
|
||||||
|
@ -129,7 +133,6 @@ void FbRun::setBackground(const FbTk::Color &color) {
|
||||||
redrawLabel();
|
redrawLabel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void FbRun::setText(const string &text) {
|
void FbRun::setText(const string &text) {
|
||||||
m_runtext = text;
|
m_runtext = text;
|
||||||
redrawLabel();
|
redrawLabel();
|
||||||
|
@ -167,49 +170,79 @@ void FbRun::drawString(int x, int y,
|
||||||
const char *text, size_t len) {
|
const char *text, size_t len) {
|
||||||
assert(m_gc);
|
assert(m_gc);
|
||||||
|
|
||||||
// check right boundary and adjust text drawing
|
m_font.drawText(m_win.window(), DefaultScreen(m_display), m_gc, text + m_start_pos, m_end_pos - m_start_pos, x, y - 2);
|
||||||
size_t text_width = m_font.textWidth(text, len);
|
|
||||||
size_t startpos = 0;
|
|
||||||
if (text_width > m_win.width()) {
|
|
||||||
for (; startpos < len; ++startpos) {
|
|
||||||
if (m_font.textWidth(text+startpos, len-startpos) < m_win.width())
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_font.drawText(m_win.window(), DefaultScreen(m_display), m_gc, text + startpos, len-startpos, x, y - 2);
|
|
||||||
int cursor_pos = m_font.textWidth(text + m_cursor_pos, len - startpos) + 1;
|
|
||||||
// draw cursor position
|
// draw cursor position
|
||||||
XDrawLine(FbTk::App::instance()->display(), m_win.window(), m_gc,
|
int cursor_pos = m_font.textWidth(text + m_start_pos, m_cursor_pos) + 1;
|
||||||
cursor_pos, 0,
|
m_win.drawLine(m_gc, cursor_pos, 0, cursor_pos, m_font.height());
|
||||||
cursor_pos, m_font.height());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FbRun::keyPressEvent(XKeyEvent &ke) {
|
void FbRun::keyPressEvent(XKeyEvent &ke) {
|
||||||
KeySym ks;
|
KeySym ks;
|
||||||
char keychar[1];
|
char keychar[1];
|
||||||
XLookupString(&ke, keychar, 1, &ks, 0);
|
XLookupString(&ke, keychar, 1, &ks, 0);
|
||||||
if (ks == XK_Escape) {
|
// a modifier key by itself doesn't do anything
|
||||||
|
if (IsModifierKey(ks)) return;
|
||||||
|
|
||||||
|
if (ke.state) { // a modifier key is down
|
||||||
|
if (ke.state == ControlMask) {
|
||||||
|
switch (ks) {
|
||||||
|
case XK_b:
|
||||||
|
cursorLeft();
|
||||||
|
break;
|
||||||
|
case XK_f:
|
||||||
|
cursorRight();
|
||||||
|
break;
|
||||||
|
case XK_p:
|
||||||
|
prevHistoryItem();
|
||||||
|
break;
|
||||||
|
case XK_n:
|
||||||
|
nextHistoryItem();
|
||||||
|
break;
|
||||||
|
case XK_a:
|
||||||
|
cursorHome();
|
||||||
|
break;
|
||||||
|
case XK_e:
|
||||||
|
cursorEnd();
|
||||||
|
break;
|
||||||
|
case XK_d:
|
||||||
|
deleteForward();
|
||||||
|
break;
|
||||||
|
case XK_k:
|
||||||
|
killToEnd();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (ke.state == (Mod1Mask | ShiftMask)) {
|
||||||
|
switch (ks) {
|
||||||
|
case XK_less:
|
||||||
|
firstHistoryItem();
|
||||||
|
break;
|
||||||
|
case XK_greater:
|
||||||
|
lastHistoryItem();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (ke.state == ShiftMask) {
|
||||||
|
if (isprint(keychar[0]))insertCharacter(ks, keychar);
|
||||||
|
}
|
||||||
|
} else { // no modifier key
|
||||||
|
switch (ks) {
|
||||||
|
case XK_Escape:
|
||||||
m_end = true;
|
m_end = true;
|
||||||
hide();
|
hide();
|
||||||
FbTk::App::instance()->end(); // end program
|
FbTk::App::instance()->end(); // end program
|
||||||
} else if (ks == XK_Return) {
|
break;
|
||||||
|
case XK_Return:
|
||||||
run(m_runtext);
|
run(m_runtext);
|
||||||
m_runtext = ""; // clear text
|
m_runtext = ""; // clear text
|
||||||
} else if (ks == XK_BackSpace) {
|
break;
|
||||||
if (m_runtext.size() != 0) { // we can't erase what we don't have ;)
|
case XK_BackSpace:
|
||||||
m_runtext.erase(m_runtext.size()-1);
|
backspace();
|
||||||
redrawLabel();
|
break;
|
||||||
m_cursor_pos--;
|
case XK_Home:
|
||||||
}
|
cursorHome();
|
||||||
} else if (! IsModifierKey(ks) && !IsCursorKey(ks)) { // insert normal character at cursor pos
|
break;
|
||||||
char in_char[2] = {keychar[0], 0};
|
case XK_End:
|
||||||
m_runtext.insert(m_cursor_pos, in_char);
|
cursorEnd();
|
||||||
m_cursor_pos++;
|
break;
|
||||||
redrawLabel();
|
|
||||||
} else if (IsCursorKey(ks)) {
|
|
||||||
|
|
||||||
switch (ks) {
|
|
||||||
case XK_Up:
|
case XK_Up:
|
||||||
prevHistoryItem();
|
prevHistoryItem();
|
||||||
break;
|
break;
|
||||||
|
@ -222,18 +255,20 @@ void FbRun::keyPressEvent(XKeyEvent &ke) {
|
||||||
case XK_Right:
|
case XK_Right:
|
||||||
cursorRight();
|
cursorRight();
|
||||||
break;
|
break;
|
||||||
|
case XK_Tab:
|
||||||
|
tabCompleteHistory();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (isprint(keychar[0])) insertCharacter(ks, keychar);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
redrawLabel();
|
redrawLabel();
|
||||||
} else if (ks == XK_End) {
|
|
||||||
m_cursor_pos = m_runtext.size() - 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FbRun::exposeEvent(XExposeEvent &ev) {
|
void FbRun::exposeEvent(XExposeEvent &ev) {
|
||||||
redrawLabel();
|
redrawLabel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void FbRun::setNoMaximize() {
|
void FbRun::setNoMaximize() {
|
||||||
// we don't need to maximize this window
|
// we don't need to maximize this window
|
||||||
XSizeHints sh;
|
XSizeHints sh;
|
||||||
|
@ -246,32 +281,160 @@ void FbRun::setNoMaximize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void FbRun::prevHistoryItem() {
|
void FbRun::prevHistoryItem() {
|
||||||
|
if (m_history.size() == 0 || m_current_history_item == 0) {
|
||||||
if (m_current_history_item > 0 && m_history.size() > 0)
|
XBell(m_display, 0);
|
||||||
|
} else {
|
||||||
m_current_history_item--;
|
m_current_history_item--;
|
||||||
if (m_current_history_item < m_history.size())
|
|
||||||
m_runtext = m_history[m_current_history_item];
|
m_runtext = m_history[m_current_history_item];
|
||||||
|
m_cursor_pos = m_end_pos = m_runtext.size();
|
||||||
|
adjustStartPos();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FbRun::nextHistoryItem() {
|
void FbRun::nextHistoryItem() {
|
||||||
|
if (m_current_history_item == m_history.size()) {
|
||||||
|
XBell(m_display, 0);
|
||||||
|
} else {
|
||||||
m_current_history_item++;
|
m_current_history_item++;
|
||||||
if (m_current_history_item >= m_history.size()) {
|
if (m_current_history_item == m_history.size()) {
|
||||||
m_current_history_item = m_history.size();
|
m_current_history_item = m_history.size();
|
||||||
m_runtext = "";
|
m_runtext = "";
|
||||||
return;
|
m_start_pos = m_cursor_pos = m_end_pos = 0;
|
||||||
} else
|
} else {
|
||||||
m_runtext = m_history[m_current_history_item];
|
m_runtext = m_history[m_current_history_item];
|
||||||
|
m_cursor_pos = m_end_pos = m_runtext.size();
|
||||||
|
adjustStartPos();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FbRun::firstHistoryItem() {
|
||||||
|
if (m_history.size() == 0 || m_current_history_item == 0) {
|
||||||
|
XBell(m_display, 0);
|
||||||
|
} else {
|
||||||
|
m_current_history_item = 0;
|
||||||
|
m_runtext = m_history[m_current_history_item];
|
||||||
|
m_cursor_pos = m_end_pos = m_runtext.size();
|
||||||
|
adjustStartPos();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::lastHistoryItem() {
|
||||||
|
// actually one past the end
|
||||||
|
if (m_history.size() == 0) {
|
||||||
|
XBell(m_display, 0);
|
||||||
|
} else {
|
||||||
|
m_current_history_item = m_history.size();
|
||||||
|
m_runtext = "";
|
||||||
|
m_start_pos = m_cursor_pos = m_end_pos = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::tabCompleteHistory() {
|
||||||
|
if (m_current_history_item == 0) {
|
||||||
|
XBell(m_display, 0);
|
||||||
|
} else {
|
||||||
|
int history_item = m_current_history_item - 1;
|
||||||
|
string prefix = m_runtext.substr(0, m_cursor_pos);
|
||||||
|
while (history_item > - 1) {
|
||||||
|
if (m_history[history_item].find(prefix) == 0) {
|
||||||
|
m_current_history_item = history_item;
|
||||||
|
m_runtext = m_history[m_current_history_item];
|
||||||
|
adjustEndPos();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
history_item--;
|
||||||
|
}
|
||||||
|
if (history_item == -1) XBell(m_display, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void FbRun::cursorLeft() {
|
void FbRun::cursorLeft() {
|
||||||
if (m_cursor_pos > 0)
|
if (m_cursor_pos)
|
||||||
m_cursor_pos--;
|
m_cursor_pos--;
|
||||||
|
else if (m_start_pos) {
|
||||||
|
m_start_pos--;
|
||||||
|
adjustEndPos();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FbRun::cursorRight() {
|
void FbRun::cursorRight() {
|
||||||
|
if (m_start_pos + m_cursor_pos < m_end_pos)
|
||||||
m_cursor_pos++;
|
m_cursor_pos++;
|
||||||
if (m_cursor_pos >= m_runtext.size())
|
else if (m_end_pos < m_runtext.size()) {
|
||||||
m_cursor_pos = m_runtext.size() - 1;
|
m_cursor_pos++;
|
||||||
|
m_end_pos++;
|
||||||
|
adjustStartPos();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::cursorHome() {
|
||||||
|
m_start_pos = m_cursor_pos = 0;
|
||||||
|
adjustEndPos();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::cursorEnd() {
|
||||||
|
m_cursor_pos = m_end_pos = m_runtext.size();
|
||||||
|
adjustStartPos();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::backspace() {
|
||||||
|
if (m_start_pos || m_cursor_pos) {
|
||||||
|
m_runtext.erase(m_start_pos + m_cursor_pos - 1, 1);
|
||||||
|
if (m_cursor_pos)
|
||||||
|
m_cursor_pos--;
|
||||||
|
else
|
||||||
|
m_start_pos--;
|
||||||
|
adjustEndPos();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::deleteForward() {
|
||||||
|
if (m_start_pos + m_cursor_pos < m_end_pos) {
|
||||||
|
m_runtext.erase(m_start_pos + m_cursor_pos, 1);
|
||||||
|
adjustEndPos();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::killToEnd() {
|
||||||
|
if (m_start_pos + m_cursor_pos < m_end_pos) {
|
||||||
|
m_runtext.erase(m_start_pos + m_cursor_pos);
|
||||||
|
adjustEndPos();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::insertCharacter(KeySym ks, char *keychar) {
|
||||||
|
char in_char[2] = {keychar[0], 0};
|
||||||
|
m_runtext.insert(m_start_pos + m_cursor_pos, in_char);
|
||||||
|
m_cursor_pos++;
|
||||||
|
m_end_pos++;
|
||||||
|
if (m_start_pos + m_cursor_pos < m_end_pos)
|
||||||
|
adjustEndPos();
|
||||||
|
else
|
||||||
|
adjustStartPos();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::adjustEndPos() {
|
||||||
|
m_end_pos = m_runtext.size();
|
||||||
|
const char *text = m_runtext.c_str();
|
||||||
|
int text_width = m_font.textWidth(text + m_start_pos, m_end_pos - m_start_pos);
|
||||||
|
while (text_width > m_win.width()) {
|
||||||
|
m_end_pos--;
|
||||||
|
text_width = m_font.textWidth(text + m_start_pos, m_end_pos - m_start_pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FbRun::adjustStartPos() {
|
||||||
|
const char *text = m_runtext.c_str();
|
||||||
|
int text_width = m_font.textWidth(text + m_start_pos, m_end_pos - m_start_pos);
|
||||||
|
if (text_width < m_win.width()) return;
|
||||||
|
int start_pos = 0;
|
||||||
|
text_width = m_font.textWidth(text + start_pos, m_end_pos - start_pos);
|
||||||
|
while (text_width > m_win.width()) {
|
||||||
|
start_pos++;
|
||||||
|
text_width = m_font.textWidth(text + start_pos, m_end_pos - start_pos);
|
||||||
|
}
|
||||||
|
// adjust m_cursor_pos according relative to change to m_start_pos
|
||||||
|
m_cursor_pos -= start_pos - m_start_pos;
|
||||||
|
m_start_pos = start_pos;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
// $Id: FbRun.hh,v 1.9 2003/03/22 11:31:43 fluxgen Exp $
|
// $Id: FbRun.hh,v 1.10 2003/06/24 10:22:42 fluxgen Exp $
|
||||||
|
|
||||||
#ifndef FBRUN_HH
|
#ifndef FBRUN_HH
|
||||||
#define FBRUN_HH
|
#define FBRUN_HH
|
||||||
|
@ -84,6 +84,18 @@ private:
|
||||||
/// set no maximizable for this window
|
/// set no maximizable for this window
|
||||||
void setNoMaximize();
|
void setNoMaximize();
|
||||||
|
|
||||||
|
void cursorHome();
|
||||||
|
void cursorEnd();
|
||||||
|
void backspace();
|
||||||
|
void deleteForward();
|
||||||
|
void killToEnd();
|
||||||
|
void insertCharacter(KeySym ks, char *keychar);
|
||||||
|
void adjustStartPos();
|
||||||
|
void adjustEndPos();
|
||||||
|
void firstHistoryItem();
|
||||||
|
void lastHistoryItem();
|
||||||
|
void tabCompleteHistory();
|
||||||
|
|
||||||
FbTk::Font m_font; ///< font used to draw command text
|
FbTk::Font m_font; ///< font used to draw command text
|
||||||
FbTk::FbWindow m_win; ///< toplevel window
|
FbTk::FbWindow m_win; ///< toplevel window
|
||||||
Display *m_display; ///< display connection
|
Display *m_display; ///< display connection
|
||||||
|
@ -95,7 +107,11 @@ private:
|
||||||
size_t m_current_history_item; ///< holds current position in command history
|
size_t m_current_history_item; ///< holds current position in command history
|
||||||
std::string m_history_file; ///< holds filename for command history file
|
std::string m_history_file; ///< holds filename for command history file
|
||||||
Cursor m_cursor;
|
Cursor m_cursor;
|
||||||
int m_cursor_pos;
|
|
||||||
|
int m_start_pos; ///< start position of portion of text to display
|
||||||
|
int m_cursor_pos; ///< relative to m_start_pos
|
||||||
|
int m_end_pos; ///< end postition of portion of text to display
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FBRUN_HH
|
#endif // FBRUN_HH
|
||||||
|
|
Loading…
Reference in a new issue