summaryrefslogtreecommitdiffstats
path: root/headers.c
diff options
context:
space:
mode:
authorDavid Champion <dgc@bikeshed.us>2017-01-28 18:47:57 -0800
committerDavid Champion <dgc@bikeshed.us>2017-01-28 18:47:57 -0800
commitf58e89c8466b36d11ecabddc8baf9727f59e6d69 (patch)
treed933009c275687b7155702c6233e39ddb53aa0af /headers.c
parentf538c3ca16b2ba9682c6c9799120d379ea4afd68 (diff)
Adds label completion.
A global label hash is added, to which labels are added as they're parsed from a mailbox file or edited manually by the user. Reference counts are kept in the hash table so that unused labels are removed from available completions. Completion is available in the label editor only, but it may be feasible to add for search expressions if the preceding text ends with '~y'.
Diffstat (limited to 'headers.c')
-rw-r--r--headers.c42
1 files changed, 41 insertions, 1 deletions
diff --git a/headers.c b/headers.c
index ada4aad4..0cf3a6cc 100644
--- a/headers.c
+++ b/headers.c
@@ -212,6 +212,31 @@ void mutt_edit_headers (const char *editor,
}
}
+static void label_ref_dec(char *label)
+{
+ uintptr_t count;
+
+ count = (uintptr_t)hash_find(Labels, label);
+ if (count)
+ {
+ hash_delete(Labels, label, NULL, NULL);
+ count--;
+ if (count > 0)
+ hash_insert(Labels, label, (void *)count, 0);
+ }
+}
+
+static void label_ref_inc(char *label)
+{
+ uintptr_t count;
+
+ count = (uintptr_t)hash_find(Labels, label);
+ if (count)
+ hash_delete(Labels, label, NULL, NULL);
+ count++; /* was zero if not found */
+ hash_insert(Labels, label, (void *)count, 0);
+}
+
/*
* add an X-Label: field.
*/
@@ -221,7 +246,11 @@ static int label_message(HEADER *hdr, char *new)
return 0;
if (mutt_strcmp (hdr->env->x_label, new) == 0)
return 0;
+ if (hdr->env->x_label != NULL)
+ label_ref_dec(hdr->env->x_label);
mutt_str_replace (&hdr->env->x_label, new);
+ if (hdr->env->x_label != NULL)
+ label_ref_inc(hdr->env->x_label);
return hdr->changed = hdr->xlabel_changed = 1;
}
@@ -236,7 +265,7 @@ int mutt_label_message(HEADER *hdr)
strncpy(buf, hdr->env->x_label, LONG_STRING);
}
- if (mutt_get_field("Label: ", buf, sizeof(buf), 0 /* | MUTT_CLEAR */) != 0)
+ if (mutt_get_field("Label: ", buf, sizeof(buf), MUTT_LABEL /* | MUTT_CLEAR */) != 0)
return 0;
new = buf;
@@ -261,3 +290,14 @@ int mutt_label_message(HEADER *hdr)
return changed;
}
+
+/* scan a context (mailbox) and hash all labels we find */
+void mutt_scan_labels(CONTEXT *ctx)
+{
+ int i;
+
+ for (i = 0; i < ctx->msgcount; i++)
+ if (ctx->hdrs[i]->env->x_label)
+ label_ref_inc(ctx->hdrs[i]->env->x_label);
+}
+