Skip to content

Commit

Permalink
Ignore certain prefix commands
Browse files Browse the repository at this point in the history
For #297
  • Loading branch information
Code7R committed Oct 20, 2024
1 parent 3ab123c commit bf8f57c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
7 changes: 7 additions & 0 deletions man/icewm-preferences.pod
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,13 @@ Dimension of the large icons.

Dimension of the large icons.

=item B<InputIgnorePrefix>="nohup|(nice[[:space:]]+-n[[:space:]]*[[:digit:]]+)|sudo|(LANG|LC_[[:alnum:]]]*)(=|=[^[:space:]]+)"

Regular expression (POSIX Extended flavor) which marks the begin of a
command line in command input fields. Such prefixes are ignored while
the auto-completion action is performed, typically for commands acting
as pass-through wrapper, executing the specified command.

=item B<QuickSwitchHorzMargin>=3 [0-64]

Horizontal margin of the quickswitch window.
Expand Down
4 changes: 4 additions & 0 deletions src/default.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ XSV(const char *, fmtTimeAlt, NULL)
XSV(const char *, fmtDate, "%Y-%m-%d %H:%M:%S %z %B %A")
#endif

XSV(const char *, inputIgnorePfx, "nohup|(nice[[:space:]]+-n[[:space:]]*[[:digit:]]+)|sudo|(LANG|LC_[[:alnum:]]]*)(=|=[^[:space:]]+)")

#ifdef CFGDEF

cfoption icewm_preferences[] = {
Expand Down Expand Up @@ -574,6 +576,8 @@ cfoption icewm_preferences[] = {
OKF("WorkspaceNames", addWorkspace, "Add a workspace"),
OKF("KeyboardLayouts", addKeyboard, "Add a keyboard layout"),
OSV("WinMenuItems", &winMenuItems, "The list of items to be supported in the menu window (rmsnxfhualytieckw)"),

OSV("InputIgnorePrefix", &inputIgnorePfx, "Prefix to ignore while tab-completing (POSIX Extended Regular Expression)"),
OK0()
};

Expand Down
38 changes: 31 additions & 7 deletions src/yinputline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "yxapp.h"
#include "prefs.h"
#include "intl.h"
#include "regex.h"
#include <X11/keysym.h>

class YInputMenu: public YMenu {
Expand Down Expand Up @@ -46,16 +47,30 @@ YInputLine::YInputLine(YWindow *parent, YInputListener *listener):
inputBg(&clrInput),
inputFg(&clrInputText),
inputSelectionBg(&clrInputSelection),
inputSelectionFg(&clrInputSelectionText)
inputSelectionFg(&clrInputSelectionText),
prefixRegex(nullptr)
{
addStyle(wsNoExpose);
if (inputFont != null)
setSize(width(), inputFont->height() + 2);
if (inputIgnorePfx && *inputIgnorePfx) {
prefixRegex = new regex_t;
mstring reFullPrefix("^(", inputIgnorePfx, ")[[:space:]]+");
if (0 != regcomp(prefixRegex, reFullPrefix, REG_EXTENDED)) {
delete prefixRegex;
prefixRegex = nullptr;
}
}
}

YInputLine::~YInputLine() {
if (inputContext)
XDestroyIC(inputContext);
if (prefixRegex) {
regfree(prefixRegex);
delete prefixRegex;
prefixRegex = nullptr;
}
}

void YInputLine::setText(mstring text, bool asMarked) {
Expand Down Expand Up @@ -762,12 +777,21 @@ void YInputLine::complete() {
}
}
csmart res;

mstring ignoredPfx;
if (prefixRegex) {
regmatch_t full_match;
if (0 == regexec(prefixRegex, mstr, 1, &full_match, 0)) {
ignoredPfx = mstr.substring(0, full_match.rm_eo);
mstr = mstr.substring(full_match.rm_eo);
}
}
int res_count = globit_best(mstr, &res, nullptr, nullptr);
// directory is not a final match
if (res_count == 1 && upath(res).dirExists())
res_count++;
if (1 <= res_count)
setText(mstring(res), res_count == 1);
setText(ignoredPfx + mstring(res), res_count == 1);
else {
int i = mstr.lastIndexOf(' ');
if (i > 0 && size_t(i + 1) < mstr.length()) {
Expand All @@ -776,7 +800,7 @@ void YInputLine::complete() {
if (sub[0] == '$' || sub[0] == '~') {
mstring exp = upath(sub).expand();
if (exp != sub && exp != null) {
setText(pre + exp, false);
setText(ignoredPfx + pre + exp, false);
}
else if (sub.indexOf('/') == -1) {
mstring var = sub.substring(1);
Expand All @@ -788,7 +812,7 @@ void YInputLine::complete() {
}
if (exp != var) {
char doti[] = { char(sub[0]), '\0' };
setText(pre + doti + exp, false);
setText(ignoredPfx + pre + doti + exp, false);
}
}
}
Expand All @@ -799,7 +823,7 @@ void YInputLine::complete() {
if (upath::glob(sub + "*", list, "/S") && list.nonempty()) {
if (list.getCount() == 1) {
mstring found(mstr.substring(0, i + 1) + list[0]);
setText(found, true);
setText(ignoredPfx + found, true);
} else {
int len = 0;
for (; list[0][len]; ++len) {
Expand All @@ -813,7 +837,7 @@ void YInputLine::complete() {
if (len) {
mstring common(list[0], len);
mstring found(mstr.substring(0, i + 1) + common);
setText(found, false);
setText(ignoredPfx + found, false);
}
}
}
Expand All @@ -822,7 +846,7 @@ void YInputLine::complete() {
if (mstr[0] == '$') {
mstring var = completeVariable(mstr.substring(1));
if (var != mstr.substring(1))
setText(mstr.substring(0, 1) + var, false);
setText(ignoredPfx + mstr.substring(0, 1) + var, false);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/yinputline.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "ypopup.h"
#include "ystring.h"

#include <regex.h>

class YMenu;
class YInputLine;
class YInputMenu;
Expand Down Expand Up @@ -103,6 +105,7 @@ class YInputLine:
YColorName inputSelectionFg;
lazy<YTimer> cursorBlinkTimer;
lazy<YInputMenu> inputMenu;
regex_t* prefixRegex;

private: // not-used
YInputLine(const YInputLine &);
Expand Down

0 comments on commit bf8f57c

Please sign in to comment.