Don't clobber tab order on /who

weechat-hashes
Curtis McEnroe 2018-08-20 19:11:44 -04:00
parent 3e66bcf50a
commit b4ca3a5dfc
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
3 changed files with 26 additions and 9 deletions

1
chat.h
View File

@ -142,6 +142,7 @@ const wchar_t *editHead(void);
const wchar_t *editTail(void); const wchar_t *editTail(void);
void tabTouch(struct Tag tag, const char *word); void tabTouch(struct Tag tag, const char *word);
void tabAdd(struct Tag tag, const char *word);
void tabRemove(struct Tag tag, const char *word); void tabRemove(struct Tag tag, const char *word);
void tabReplace(struct Tag tag, const char *prev, const char *next); void tabReplace(struct Tag tag, const char *prev, const char *next);
void tabClear(struct Tag tag); void tabClear(struct Tag tag);

View File

@ -308,8 +308,7 @@ static void handleReplyWho(char *prefix, char *params) {
); );
struct Tag tag = tagFor(chan); struct Tag tag = tagFor(chan);
// FIXME: Don't clobber order if they already exist. tabAdd(tag, nick);
tabTouch(tag, nick);
size_t cap = sizeof(who.buf) - who.len; size_t cap = sizeof(who.buf) - who.len;
int len = snprintf( int len = snprintf(

31
tab.c
View File

@ -47,14 +47,16 @@ static void touch(struct Entry *entry) {
prepend(entry); prepend(entry);
} }
void tabTouch(struct Tag tag, const char *word) { static struct Entry *find(struct Tag tag, const char *word) {
for (struct Entry *entry = head; entry; entry = entry->next) { for (struct Entry *entry = head; entry; entry = entry->next) {
if (entry->tag.id != tag.id) continue; if (entry->tag.id != tag.id) continue;
if (strcmp(entry->word, word)) continue; if (strcmp(entry->word, word)) continue;
touch(entry); return entry;
return;
} }
return NULL;
}
static void add(struct Tag tag, const char *word) {
struct Entry *entry = malloc(sizeof(*entry)); struct Entry *entry = malloc(sizeof(*entry));
if (!entry) err(EX_OSERR, "malloc"); if (!entry) err(EX_OSERR, "malloc");
@ -65,11 +67,26 @@ void tabTouch(struct Tag tag, const char *word) {
prepend(entry); prepend(entry);
} }
void tabTouch(struct Tag tag, const char *word) {
struct Entry *entry = find(tag, word);
if (entry) {
touch(entry);
} else {
add(tag, word);
}
}
void tabAdd(struct Tag tag, const char *word) {
if (!find(tag, word)) add(tag, word);
}
void tabReplace(struct Tag tag, const char *prev, const char *next) { void tabReplace(struct Tag tag, const char *prev, const char *next) {
tabTouch(tag, prev); struct Entry *entry = find(tag, prev);
free(head->word); if (!entry) return;
head->word = strdup(next); touch(entry);
if (!head->word) err(EX_OSERR, "strdup"); free(entry->word);
entry->word = strdup(next);
if (!entry->word) err(EX_OSERR, "strdup");
} }
static struct Entry *iter; static struct Entry *iter;