Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve word and file name completion with non-ASCII characters #946

Merged
merged 2 commits into from
Aug 14, 2023
Merged
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
2 changes: 1 addition & 1 deletion vis-complete
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fi
PATTERN="$1"

if [ $COMPLETE_WORD = 1 ]; then
tr -cs '[:alnum:]_' '\n' |
tr -s '[:blank:]_' '\n' |
grep "^$(basic_regex_quote "$PATTERN")." |
sort -u
else
Expand Down
40 changes: 37 additions & 3 deletions vis-menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,46 @@ appenditem(Item *item, Item **list, Item **last) {

static size_t
textwn(const char *s, int l) {
int b, c; /* bytes and UTF-8 characters */
int c;

for(b=c=0; s && s[b] && (l<0 || b<l); b++) if((s[b] & 0xc0) != 0x80) c++;
for (c=0; s && s[c] && (l<0 || c<l); c++);
return c+4; /* Accomodate for the leading and trailing spaces */
}

/*
* textvalidn returns the highest amount of bytes <= l of string s that
* only contains valid Unicode points. This is used to make sure we don't
* cut off any valid UTF-8-encoded unicode point in case there is not
* enough space to render the whole text string.
*/
static ssize_t
textvalidn(const char *s, int l) {
int c, utfcharbytes; /* byte count and UTF-8 codepoint length */

for (c=0; s && s[c] && (l<0 || c<l); ) {
utfcharbytes = 0;
if ((s[c] & 0x80) == 0) {
utfcharbytes = 1;
} else if ((s[c] & 0xf0) == 0xf0) {
utfcharbytes = 4;
} else if ((s[c] & 0xf0) == 0xe0) {
utfcharbytes = 3;
} else if ((s[c] & 0xe0) == 0xc0) {
utfcharbytes = 2;
} else {
return -1;
}

if ((l>0 && c + utfcharbytes >= l)) {
break;
}

c += utfcharbytes;
}

return c;
}

static size_t
textw(const char *s) {
return textwn(s, -1);
Expand Down Expand Up @@ -149,7 +183,7 @@ drawtext(const char *t, size_t w, Color col) {
buf[tw] = '\0';
memcpy(buf, t, MIN(strlen(t), tw));
if (textw(t) > w) /* Remember textw returns the width WITH padding */
for (i = MAX((tw-4), 0); i < tw; i++) buf[i] = '.';
for (i = MAX(textvalidn(t, w-4), 0); i < tw; i++) buf[i] = '.';

fprintf(stderr, "%s %s %s", prestr, buf, poststr);
free(buf);
Expand Down