2003-06-24 10:22:42 +00:00
|
|
|
// FbRun.cc
|
2006-02-16 06:53:05 +00:00
|
|
|
// Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
|
2002-08-20 02:05:17 +00:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#include "FbRun.hh"
|
|
|
|
|
2009-05-25 06:04:41 +00:00
|
|
|
#include "FbTk/App.hh"
|
|
|
|
#include "FbTk/EventManager.hh"
|
|
|
|
#include "FbTk/Color.hh"
|
|
|
|
#include "FbTk/KeyUtil.hh"
|
|
|
|
#include "FbTk/FileUtil.hh"
|
2002-11-12 17:10:13 +00:00
|
|
|
|
2003-08-25 01:18:00 +00:00
|
|
|
#ifdef HAVE_XPM
|
|
|
|
#include <X11/xpm.h>
|
|
|
|
#include "fbrun.xpm"
|
|
|
|
#endif // HAVE_XPM
|
|
|
|
|
2002-08-20 02:05:17 +00:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/keysym.h>
|
|
|
|
#include <X11/Xutil.h>
|
2003-03-22 11:33:04 +00:00
|
|
|
#include <X11/cursorfont.h>
|
2004-04-21 14:58:44 +00:00
|
|
|
#include <unistd.h>
|
2002-08-20 02:05:17 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
2003-08-25 01:18:00 +00:00
|
|
|
#include <iterator>
|
2002-11-12 19:20:31 +00:00
|
|
|
#include <fstream>
|
2004-04-18 14:16:09 +00:00
|
|
|
#include <algorithm>
|
2002-08-20 02:05:17 +00:00
|
|
|
|
2011-10-28 19:07:49 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <cstring>
|
|
|
|
#endif
|
|
|
|
|
2006-10-30 19:31:15 +00:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
using std::string;
|
|
|
|
using std::fstream;
|
|
|
|
using std::ifstream;
|
|
|
|
using std::ofstream;
|
|
|
|
using std::ios;
|
2006-04-25 06:46:06 +00:00
|
|
|
|
2002-11-12 17:10:13 +00:00
|
|
|
FbRun::FbRun(int x, int y, size_t width):
|
2003-08-27 00:20:19 +00:00
|
|
|
FbTk::TextBox(DefaultScreen(FbTk::App::instance()->display()),
|
|
|
|
m_font, ""),
|
2008-10-04 21:01:31 +00:00
|
|
|
m_print(false),
|
2002-12-05 00:07:39 +00:00
|
|
|
m_font("fixed"),
|
2003-07-25 11:17:41 +00:00
|
|
|
m_display(FbTk::App::instance()->display()),
|
2002-12-05 00:07:39 +00:00
|
|
|
m_bevel(4),
|
2003-08-27 14:04:12 +00:00
|
|
|
m_gc(*this),
|
2002-12-05 00:07:39 +00:00
|
|
|
m_end(false),
|
2003-03-22 11:33:04 +00:00
|
|
|
m_current_history_item(0),
|
2006-04-25 02:42:05 +00:00
|
|
|
m_last_completion_prefix(""),
|
2004-04-18 14:16:09 +00:00
|
|
|
m_current_apps_item(0),
|
2003-08-27 14:04:12 +00:00
|
|
|
m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)) {
|
2006-04-25 06:46:06 +00:00
|
|
|
|
2003-08-27 14:04:12 +00:00
|
|
|
setGC(m_gc.gc());
|
2003-08-25 01:18:00 +00:00
|
|
|
setCursor(m_cursor);
|
2002-12-05 00:07:39 +00:00
|
|
|
// setting nomaximize in local resize
|
2003-08-27 00:20:19 +00:00
|
|
|
resize(width, font().height() + m_bevel);
|
|
|
|
|
2003-06-25 12:01:23 +00:00
|
|
|
// setup class name
|
2012-10-22 15:28:35 +00:00
|
|
|
XClassHint ch;
|
|
|
|
ch.res_name = const_cast<char *>("fbrun");
|
|
|
|
ch.res_class = const_cast<char *>("FbRun");
|
|
|
|
XSetClassHint(m_display, window(), &ch);
|
2006-04-25 06:46:06 +00:00
|
|
|
|
2003-08-25 01:18:00 +00:00
|
|
|
#ifdef HAVE_XPM
|
|
|
|
Pixmap mask = 0;
|
2003-08-27 14:04:12 +00:00
|
|
|
Pixmap pm;
|
2003-08-25 01:18:00 +00:00
|
|
|
XpmCreatePixmapFromData(m_display,
|
|
|
|
window(),
|
2007-07-31 23:16:05 +00:00
|
|
|
const_cast<char **>(fbrun_xpm),
|
2003-08-27 14:04:12 +00:00
|
|
|
&pm,
|
2003-08-25 01:18:00 +00:00
|
|
|
&mask,
|
|
|
|
0); // attribs
|
|
|
|
if (mask != 0)
|
|
|
|
XFreePixmap(m_display, mask);
|
2003-08-27 14:04:12 +00:00
|
|
|
|
|
|
|
m_pixmap = pm;
|
2003-08-25 01:18:00 +00:00
|
|
|
#endif // HAVE_XPM
|
|
|
|
|
2003-08-27 14:04:12 +00:00
|
|
|
if (m_pixmap.drawable()) {
|
2003-08-27 00:20:19 +00:00
|
|
|
XWMHints wmhints;
|
|
|
|
wmhints.flags = IconPixmapHint;
|
2003-08-27 14:04:12 +00:00
|
|
|
wmhints.icon_pixmap = m_pixmap.drawable();
|
2003-08-27 00:20:19 +00:00
|
|
|
XSetWMHints(m_display, window(), &wmhints);
|
|
|
|
}
|
2004-04-18 14:16:09 +00:00
|
|
|
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
|
|
|
|
2003-06-24 10:22:42 +00:00
|
|
|
|
2002-08-20 02:05:17 +00:00
|
|
|
FbRun::~FbRun() {
|
2002-12-05 00:07:39 +00:00
|
|
|
hide();
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FbRun::run(const std::string &command) {
|
2003-08-25 01:18:00 +00:00
|
|
|
FbTk::App::instance()->end(); // end application
|
|
|
|
m_end = true; // mark end of processing
|
|
|
|
|
2008-10-04 21:01:31 +00:00
|
|
|
if (m_print) {
|
|
|
|
std::cout << command;
|
|
|
|
hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-28 19:07:49 +00:00
|
|
|
#ifdef HAVE_FORK
|
2003-08-25 01:18:00 +00:00
|
|
|
// fork and execute program
|
2002-12-05 00:07:39 +00:00
|
|
|
if (!fork()) {
|
2007-06-06 06:22:37 +00:00
|
|
|
|
2007-07-31 23:16:05 +00:00
|
|
|
const char *shell = getenv("SHELL");
|
2007-06-06 06:22:37 +00:00
|
|
|
if (!shell)
|
|
|
|
shell = "/bin/sh";
|
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
setsid();
|
2007-06-09 17:44:27 +00:00
|
|
|
execl(shell, shell, "-c", command.c_str(), static_cast<void*>(NULL));
|
2002-12-05 00:07:39 +00:00
|
|
|
exit(0); //exit child
|
|
|
|
}
|
2011-10-28 19:07:49 +00:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
/// @todo - unduplicate from FbCommands.cc
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 1024
|
|
|
|
#endif
|
|
|
|
char comspec[PATH_MAX] = {0};
|
|
|
|
char * env_var = getenv("COMSPEC");
|
|
|
|
if (env_var != NULL) {
|
|
|
|
strncpy(comspec, env_var, PATH_MAX - 1);
|
|
|
|
comspec[PATH_MAX - 1] = '\0';
|
|
|
|
} else {
|
|
|
|
strncpy(comspec, "cmd.exe", 7);
|
|
|
|
comspec[7] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
spawnlp(P_NOWAIT, comspec, comspec, "/c", command.c_str(), static_cast<void*>(NULL));
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "Can't build FbRun - don't know how to launch without fork on your platform"
|
|
|
|
#endif
|
2002-12-05 00:07:39 +00:00
|
|
|
|
|
|
|
hide(); // hide gui
|
2006-04-25 06:46:06 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
// save command history to file
|
2003-08-27 00:20:19 +00:00
|
|
|
if (text().size() != 0) { // no need to save empty command
|
2003-07-10 10:18:08 +00:00
|
|
|
|
2003-08-24 23:47:31 +00:00
|
|
|
// don't allow duplicates into the history file, first
|
2003-07-10 10:18:08 +00:00
|
|
|
// look for a duplicate
|
2003-08-24 23:47:31 +00:00
|
|
|
if (m_current_history_item < m_history.size()
|
2003-08-27 00:20:19 +00:00
|
|
|
&& text() == m_history[m_current_history_item]) {
|
2003-08-24 23:47:31 +00:00
|
|
|
// m_current_history_item is the duplicate
|
|
|
|
} else {
|
2003-08-25 01:18:00 +00:00
|
|
|
m_current_history_item = 0;
|
2006-04-25 06:46:06 +00:00
|
|
|
for (; m_current_history_item < m_history.size();
|
2003-08-25 01:18:00 +00:00
|
|
|
++m_current_history_item) {
|
2003-08-27 00:20:19 +00:00
|
|
|
if (m_history[m_current_history_item] == text())
|
2003-08-25 01:18:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-08-24 23:47:31 +00:00
|
|
|
}
|
2003-07-10 10:18:08 +00:00
|
|
|
|
2003-08-24 23:47:31 +00:00
|
|
|
fstream inoutfile(m_history_file.c_str(), ios::in|ios::out);
|
|
|
|
if (inoutfile) {
|
2004-12-24 03:02:48 +00:00
|
|
|
// now m_current_history_item points at the duplicate, or
|
|
|
|
// at m_history.size() if no duplicate
|
|
|
|
if (m_current_history_item != m_history.size()) {
|
2006-04-17 08:04:12 +00:00
|
|
|
unsigned int i = 0;
|
2004-12-24 03:02:48 +00:00
|
|
|
// read past history items before current
|
|
|
|
for (; inoutfile.good() && i < m_current_history_item; i++)
|
|
|
|
inoutfile.ignore(1, '\n');
|
|
|
|
|
|
|
|
// write the history items that come after current
|
|
|
|
for (i++; i < m_history.size(); i++)
|
|
|
|
inoutfile<<m_history[i]<<endl;
|
2006-04-25 06:46:06 +00:00
|
|
|
|
2004-12-24 03:02:48 +00:00
|
|
|
} else {
|
|
|
|
// set put-pointer at end of file
|
|
|
|
inoutfile.seekp(0, ios::end);
|
|
|
|
}
|
|
|
|
// append current command to the file
|
|
|
|
inoutfile<<command<<endl;
|
|
|
|
|
2003-08-24 23:47:31 +00:00
|
|
|
} else
|
|
|
|
cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
|
2002-12-05 00:07:39 +00:00
|
|
|
}
|
2003-08-25 01:18:00 +00:00
|
|
|
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
|
|
|
|
2002-11-12 19:20:31 +00:00
|
|
|
bool FbRun::loadHistory(const char *filename) {
|
2002-12-05 00:07:39 +00:00
|
|
|
if (filename == 0)
|
|
|
|
return false;
|
|
|
|
ifstream infile(filename);
|
|
|
|
if (!infile) {
|
|
|
|
//even though we fail to load file, we should try save to it
|
2004-09-29 00:51:29 +00:00
|
|
|
ofstream outfile(filename);
|
|
|
|
if (outfile) {
|
|
|
|
m_history_file = filename;
|
|
|
|
return true;
|
|
|
|
}
|
2002-12-05 00:07:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-02 15:45:39 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
m_history.clear();
|
|
|
|
string line;
|
2015-01-02 15:45:39 +00:00
|
|
|
while (getline(infile, line)) {
|
|
|
|
if (!line.empty()) // don't add empty lines
|
2002-12-05 00:07:39 +00:00
|
|
|
m_history.push_back(line);
|
|
|
|
}
|
|
|
|
// set no current histor to display
|
|
|
|
m_current_history_item = m_history.size();
|
|
|
|
// set history file
|
|
|
|
m_history_file = filename;
|
|
|
|
return true;
|
2002-11-12 19:20:31 +00:00
|
|
|
}
|
|
|
|
|
2002-08-20 02:05:17 +00:00
|
|
|
bool FbRun::loadFont(const string &fontname) {
|
2002-12-05 00:07:39 +00:00
|
|
|
if (!m_font.load(fontname.c_str()))
|
|
|
|
return false;
|
2002-08-20 02:05:17 +00:00
|
|
|
|
2002-12-05 00:07:39 +00:00
|
|
|
// resize to fit new font height
|
2003-08-27 00:20:19 +00:00
|
|
|
resize(width(), font().height() + m_bevel);
|
2002-12-05 00:07:39 +00:00
|
|
|
return true;
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
|
|
|
|
2003-08-25 01:18:00 +00:00
|
|
|
void FbRun::setForegroundColor(const FbTk::Color &color) {
|
2003-08-27 14:04:12 +00:00
|
|
|
m_gc.setForeground(color);
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FbRun::setTitle(const string &title) {
|
2003-08-25 01:18:00 +00:00
|
|
|
setName(title.c_str());
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
|
|
|
|
2003-09-16 14:49:49 +00:00
|
|
|
void FbRun::resize(unsigned int width, unsigned int height) {
|
2006-04-25 06:46:06 +00:00
|
|
|
FbTk::TextBox::resize(width, height);
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FbRun::redrawLabel() {
|
2003-08-25 01:18:00 +00:00
|
|
|
clear();
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
|
|
|
|
2002-11-27 21:56:56 +00:00
|
|
|
void FbRun::keyPressEvent(XKeyEvent &ke) {
|
2006-04-25 02:42:05 +00:00
|
|
|
// reset last completion prefix if we don't do a tab completion thing
|
|
|
|
bool did_tab_complete = false;
|
|
|
|
|
2003-12-31 01:34:33 +00:00
|
|
|
ke.state = FbTk::KeyUtil::instance().cleanMods(ke.state);
|
2003-09-06 15:50:25 +00:00
|
|
|
|
2003-08-27 00:20:19 +00:00
|
|
|
FbTk::TextBox::keyPressEvent(ke);
|
2002-12-05 00:07:39 +00:00
|
|
|
KeySym ks;
|
|
|
|
char keychar[1];
|
|
|
|
XLookupString(&ke, keychar, 1, &ks, 0);
|
2003-06-24 10:22:42 +00:00
|
|
|
// a modifier key by itself doesn't do anything
|
2006-04-25 06:46:06 +00:00
|
|
|
if (IsModifierKey(ks))
|
2005-05-06 09:22:53 +00:00
|
|
|
return;
|
2003-06-24 10:22:42 +00:00
|
|
|
|
2005-05-06 09:22:53 +00:00
|
|
|
if (FbTk::KeyUtil::instance().isolateModifierMask(ke.state)) { // a modifier key is down
|
|
|
|
if ((ke.state & ControlMask) == ControlMask) {
|
2003-06-24 10:22:42 +00:00
|
|
|
switch (ks) {
|
|
|
|
case XK_p:
|
2006-04-25 02:42:05 +00:00
|
|
|
did_tab_complete = true;
|
2003-06-24 10:22:42 +00:00
|
|
|
prevHistoryItem();
|
|
|
|
break;
|
|
|
|
case XK_n:
|
2006-04-25 02:42:05 +00:00
|
|
|
did_tab_complete = true;
|
2003-06-24 10:22:42 +00:00
|
|
|
nextHistoryItem();
|
|
|
|
break;
|
2004-04-18 14:16:09 +00:00
|
|
|
case XK_Tab:
|
2006-04-25 02:42:05 +00:00
|
|
|
did_tab_complete = true;
|
2004-04-18 14:16:09 +00:00
|
|
|
tabCompleteHistory();
|
|
|
|
break;
|
2003-06-24 10:22:42 +00:00
|
|
|
}
|
2005-05-06 09:22:53 +00:00
|
|
|
} else if ((ke.state & (Mod1Mask|ShiftMask)) == (Mod1Mask | ShiftMask)) {
|
2003-06-24 10:22:42 +00:00
|
|
|
switch (ks) {
|
|
|
|
case XK_less:
|
2006-04-25 02:42:05 +00:00
|
|
|
did_tab_complete = true;
|
2003-06-24 10:22:42 +00:00
|
|
|
firstHistoryItem();
|
|
|
|
break;
|
|
|
|
case XK_greater:
|
2006-04-25 02:42:05 +00:00
|
|
|
did_tab_complete = true;
|
2003-06-24 10:22:42 +00:00
|
|
|
lastHistoryItem();
|
|
|
|
break;
|
|
|
|
}
|
2002-12-05 00:07:39 +00:00
|
|
|
}
|
2003-06-24 10:22:42 +00:00
|
|
|
} else { // no modifier key
|
2002-12-05 00:07:39 +00:00
|
|
|
switch (ks) {
|
2003-06-24 10:22:42 +00:00
|
|
|
case XK_Escape:
|
|
|
|
m_end = true;
|
|
|
|
hide();
|
|
|
|
FbTk::App::instance()->end(); // end program
|
|
|
|
break;
|
2004-11-18 03:25:08 +00:00
|
|
|
case XK_KP_Enter:
|
2003-06-24 10:22:42 +00:00
|
|
|
case XK_Return:
|
2003-08-27 00:20:19 +00:00
|
|
|
run(text());
|
2003-06-24 10:22:42 +00:00
|
|
|
break;
|
2002-12-05 00:07:39 +00:00
|
|
|
case XK_Up:
|
|
|
|
prevHistoryItem();
|
|
|
|
break;
|
|
|
|
case XK_Down:
|
|
|
|
nextHistoryItem();
|
|
|
|
break;
|
2003-06-24 10:22:42 +00:00
|
|
|
case XK_Tab:
|
2006-04-25 02:42:05 +00:00
|
|
|
did_tab_complete = true;
|
2004-04-18 14:16:09 +00:00
|
|
|
tabCompleteApps();
|
2003-06-24 10:22:42 +00:00
|
|
|
break;
|
2002-12-05 00:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-27 00:20:19 +00:00
|
|
|
clear();
|
2006-04-25 02:42:05 +00:00
|
|
|
if (!did_tab_complete)
|
|
|
|
m_last_completion_prefix = "";
|
2002-11-27 21:56:56 +00:00
|
|
|
}
|
|
|
|
|
2006-04-17 14:00:28 +00:00
|
|
|
void FbRun::lockPosition(bool size_too) {
|
2002-12-05 00:07:39 +00:00
|
|
|
// we don't need to maximize this window
|
|
|
|
XSizeHints sh;
|
|
|
|
sh.flags = PMaxSize | PMinSize;
|
2003-08-25 01:18:00 +00:00
|
|
|
sh.max_width = width();
|
|
|
|
sh.max_height = height();
|
|
|
|
sh.min_width = width();
|
|
|
|
sh.min_height = height();
|
2006-04-17 14:00:28 +00:00
|
|
|
if (size_too) {
|
|
|
|
sh.flags |= USPosition;
|
|
|
|
sh.x = x();
|
|
|
|
sh.y = y();
|
|
|
|
}
|
2003-08-25 01:18:00 +00:00
|
|
|
XSetWMNormalHints(m_display, window(), &sh);
|
2002-08-20 02:05:17 +00:00
|
|
|
}
|
2002-11-12 19:20:31 +00:00
|
|
|
|
|
|
|
void FbRun::prevHistoryItem() {
|
2011-11-02 17:33:39 +00:00
|
|
|
if (m_history.empty() || m_current_history_item == 0) {
|
2003-06-24 10:22:42 +00:00
|
|
|
XBell(m_display, 0);
|
|
|
|
} else {
|
2002-12-05 00:07:39 +00:00
|
|
|
m_current_history_item--;
|
2003-08-27 00:20:19 +00:00
|
|
|
setText(m_history[m_current_history_item]);
|
2003-06-24 10:22:42 +00:00
|
|
|
}
|
2002-11-12 19:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FbRun::nextHistoryItem() {
|
2003-06-24 10:22:42 +00:00
|
|
|
if (m_current_history_item == m_history.size()) {
|
|
|
|
XBell(m_display, 0);
|
|
|
|
} else {
|
|
|
|
m_current_history_item++;
|
2010-09-08 18:17:21 +00:00
|
|
|
FbTk::BiDiString text("");
|
2003-06-24 10:22:42 +00:00
|
|
|
if (m_current_history_item == m_history.size()) {
|
|
|
|
m_current_history_item = m_history.size();
|
2010-09-08 18:17:21 +00:00
|
|
|
} else
|
|
|
|
text.setLogical((m_history[m_current_history_item]));
|
|
|
|
|
|
|
|
setText(text);
|
2003-06-24 10:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FbRun::firstHistoryItem() {
|
2011-11-02 17:33:39 +00:00
|
|
|
if (m_history.empty() || m_current_history_item == 0) {
|
2003-06-24 10:22:42 +00:00
|
|
|
XBell(m_display, 0);
|
|
|
|
} else {
|
|
|
|
m_current_history_item = 0;
|
2010-09-08 18:17:21 +00:00
|
|
|
setText(FbTk::BiDiString(m_history[m_current_history_item]));
|
2003-06-24 10:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-12 19:20:31 +00:00
|
|
|
|
2003-06-24 10:22:42 +00:00
|
|
|
void FbRun::lastHistoryItem() {
|
|
|
|
// actually one past the end
|
2011-11-02 17:33:39 +00:00
|
|
|
if (m_history.empty()) {
|
2003-06-24 10:22:42 +00:00
|
|
|
XBell(m_display, 0);
|
|
|
|
} else {
|
|
|
|
m_current_history_item = m_history.size();
|
2010-09-08 18:17:21 +00:00
|
|
|
setText(FbTk::BiDiString(""));
|
2003-06-24 10:22:42 +00:00
|
|
|
}
|
2002-11-12 19:20:31 +00:00
|
|
|
}
|
2003-03-22 11:33:04 +00:00
|
|
|
|
2003-06-24 10:22:42 +00:00
|
|
|
void FbRun::tabCompleteHistory() {
|
2004-02-28 10:43:20 +00:00
|
|
|
if (m_current_history_item == 0 || m_history.empty() ) {
|
2003-06-24 10:22:42 +00:00
|
|
|
XBell(m_display, 0);
|
|
|
|
} else {
|
2004-02-28 10:43:20 +00:00
|
|
|
unsigned int nr= 0;
|
2006-04-17 08:04:12 +00:00
|
|
|
unsigned int history_item = m_current_history_item - 1;
|
2006-04-25 02:42:05 +00:00
|
|
|
if (m_last_completion_prefix.empty())
|
|
|
|
m_last_completion_prefix = text().substr(0, textStartPos() + cursorPosition());
|
|
|
|
|
2004-02-28 10:43:20 +00:00
|
|
|
while (history_item != m_current_history_item && nr++ < m_history.size()) {
|
2006-04-25 02:42:05 +00:00
|
|
|
if (m_history[history_item].find(m_last_completion_prefix) == 0) {
|
2003-06-24 10:22:42 +00:00
|
|
|
m_current_history_item = history_item;
|
2010-09-08 18:17:21 +00:00
|
|
|
setText(FbTk::BiDiString(m_history[m_current_history_item]));
|
2006-04-25 02:42:05 +00:00
|
|
|
cursorEnd();
|
2003-06-24 10:22:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-04-17 08:04:12 +00:00
|
|
|
if (history_item == 0) // loop
|
|
|
|
history_item = m_history.size();
|
2003-06-24 10:22:42 +00:00
|
|
|
history_item--;
|
|
|
|
}
|
2004-02-25 18:37:47 +00:00
|
|
|
if (history_item == m_current_history_item) XBell(m_display, 0);
|
2003-06-24 10:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-22 11:33:04 +00:00
|
|
|
|
2004-04-18 14:16:09 +00:00
|
|
|
void FbRun::tabCompleteApps() {
|
2006-04-25 06:46:06 +00:00
|
|
|
|
2004-04-22 21:01:58 +00:00
|
|
|
static bool first_run= true;
|
2006-04-25 02:42:05 +00:00
|
|
|
if (m_last_completion_prefix.empty())
|
|
|
|
m_last_completion_prefix = text().substr(0, textStartPos() + cursorPosition());
|
|
|
|
string prefix = m_last_completion_prefix;
|
2004-04-22 21:01:58 +00:00
|
|
|
FbTk::Directory dir;
|
|
|
|
|
|
|
|
bool add_dirs= false;
|
|
|
|
bool changed_prefix= false;
|
|
|
|
|
|
|
|
// (re)build m_apps-container
|
2006-04-25 02:42:05 +00:00
|
|
|
if (first_run || m_last_completion_prefix != prefix) {
|
2004-04-22 21:01:58 +00:00
|
|
|
first_run= false;
|
2006-04-25 06:46:06 +00:00
|
|
|
|
2004-04-22 21:01:58 +00:00
|
|
|
string path;
|
2006-04-25 06:46:06 +00:00
|
|
|
|
|
|
|
if(!prefix.empty() &&
|
2004-10-10 16:35:29 +00:00
|
|
|
string("/.~").find_first_of(prefix[0]) != string::npos) {
|
2004-04-22 21:01:58 +00:00
|
|
|
size_t rseparator= prefix.find_last_of("/");
|
|
|
|
path= prefix.substr(0, rseparator + 1) + ":";
|
|
|
|
add_dirs= true;
|
2006-04-25 06:46:06 +00:00
|
|
|
} else {
|
|
|
|
char* tmp_path = getenv("PATH");
|
|
|
|
if (tmp_path)
|
|
|
|
path = tmp_path;
|
|
|
|
}
|
2004-04-22 21:01:58 +00:00
|
|
|
m_apps.clear();
|
2006-04-25 06:46:06 +00:00
|
|
|
|
2004-04-22 21:01:58 +00:00
|
|
|
unsigned int l;
|
|
|
|
unsigned int r;
|
|
|
|
|
|
|
|
for(l= 0, r= 0; r < path.size(); r++) {
|
|
|
|
if ((path[r]==':' || r == path.size() - 1) && r - l > 0) {
|
|
|
|
string filename;
|
|
|
|
string fncomplete;
|
|
|
|
dir.open(path.substr(l, r - l).c_str());
|
|
|
|
int n= dir.entries();
|
|
|
|
if (n >= 0) {
|
|
|
|
while(n--) {
|
|
|
|
filename= dir.readFilename();
|
2006-04-25 06:46:06 +00:00
|
|
|
fncomplete= dir.name() +
|
|
|
|
(*dir.name().rbegin() != '/' ? "/" : "") +
|
2004-04-22 21:01:58 +00:00
|
|
|
filename;
|
|
|
|
|
|
|
|
// directories in dirmode ?
|
2004-12-18 01:29:22 +00:00
|
|
|
if (add_dirs && FbTk::FileUtil::isDirectory(fncomplete.c_str()) &&
|
2004-04-22 21:01:58 +00:00
|
|
|
filename != ".." && filename != ".") {
|
2006-04-25 06:46:06 +00:00
|
|
|
m_apps.push_back(fncomplete);
|
2004-04-22 21:01:58 +00:00
|
|
|
// executables in dirmode ?
|
2006-04-25 06:46:06 +00:00
|
|
|
} else if (add_dirs && FbTk::FileUtil::isRegularFile(fncomplete.c_str()) &&
|
|
|
|
FbTk::FileUtil::isExecutable(fncomplete.c_str()) &&
|
|
|
|
(prefix == "" ||
|
2004-04-22 21:01:58 +00:00
|
|
|
fncomplete.substr(0, prefix.size()) == prefix)) {
|
|
|
|
m_apps.push_back(fncomplete);
|
|
|
|
// executables in $PATH ?
|
2006-04-25 06:46:06 +00:00
|
|
|
} else if (FbTk::FileUtil::isRegularFile(fncomplete.c_str()) &&
|
|
|
|
FbTk::FileUtil::isExecutable(fncomplete.c_str()) &&
|
|
|
|
(prefix == "" ||
|
2004-04-22 21:01:58 +00:00
|
|
|
filename.substr(0, prefix.size()) == prefix)) {
|
|
|
|
m_apps.push_back(filename);
|
2006-04-25 06:46:06 +00:00
|
|
|
}
|
2004-04-22 21:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
l= r + 1;
|
|
|
|
dir.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort(m_apps.begin(), m_apps.end());
|
|
|
|
unique(m_apps.begin(), m_apps.end());
|
|
|
|
|
2006-04-25 02:42:05 +00:00
|
|
|
m_last_completion_prefix = prefix;
|
2004-04-22 21:01:58 +00:00
|
|
|
changed_prefix= true;
|
|
|
|
m_current_apps_item= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_apps.empty() ) {
|
2004-04-18 14:16:09 +00:00
|
|
|
XBell(m_display, 0);
|
|
|
|
} else {
|
2004-04-22 21:01:58 +00:00
|
|
|
size_t apps_item = m_current_apps_item + (changed_prefix ? 0 : 1);
|
|
|
|
bool loop= false;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (apps_item >= m_apps.size() ) {
|
|
|
|
loop = true;
|
|
|
|
apps_item = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!changed_prefix || loop) && apps_item == m_current_apps_item) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (m_apps[apps_item].find(prefix) == 0) {
|
2004-04-18 14:16:09 +00:00
|
|
|
m_current_apps_item = apps_item;
|
2004-12-18 01:29:22 +00:00
|
|
|
if (add_dirs && FbTk::FileUtil::isDirectory(m_apps[m_current_apps_item].c_str()))
|
2004-04-22 21:01:58 +00:00
|
|
|
setText(m_apps[m_current_apps_item] + "/");
|
|
|
|
else
|
|
|
|
setText(m_apps[m_current_apps_item]);
|
2006-04-25 02:42:05 +00:00
|
|
|
cursorEnd();
|
2004-04-18 14:16:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-04-18 18:57:24 +00:00
|
|
|
apps_item++;
|
2004-04-18 14:16:09 +00:00
|
|
|
}
|
2006-04-25 06:46:06 +00:00
|
|
|
if (!changed_prefix && apps_item == m_current_apps_item)
|
2004-04-18 18:57:24 +00:00
|
|
|
XBell(m_display, 0);
|
2004-04-18 14:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-27 00:20:19 +00:00
|
|
|
void FbRun::insertCharacter(char keychar) {
|
|
|
|
char val[2] = {keychar, 0};
|
|
|
|
insertText(val);
|
2003-06-24 10:22:42 +00:00
|
|
|
}
|
|
|
|
|