NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
tags.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <stdbool.h>
34#include <stddef.h>
35#include <stdint.h>
36#include "mutt/lib.h"
37#include "config/lib.h"
38#include "core/lib.h"
39#include "tags.h"
40
41struct HashTable *TagTransforms = NULL;
42struct HashTable *TagFormats = NULL;
43
48void tag_free(struct Tag **ptr)
49{
50 if (!ptr || !*ptr)
51 return;
52
53 struct Tag *tag = *ptr;
54 FREE(&tag->name);
55 FREE(&tag->transformed);
56
57 FREE(ptr);
58}
59
64struct Tag *tag_new(void)
65{
66 return MUTT_MEM_CALLOC(1, struct Tag);
67}
68
77void driver_tags_getter(struct TagList *tl, bool show_hidden, bool show_transformed,
78 const char *filter, struct Buffer *tags)
79{
80 if (!tl)
81 return;
82
83 struct Tag *tag = NULL;
84 STAILQ_FOREACH(tag, tl, entries)
85 {
86 if (filter && !mutt_str_equal(tag->name, filter))
87 continue;
88 if (show_hidden || !tag->hidden)
89 {
90 if (show_transformed && tag->transformed)
91 buf_join_str(tags, tag->transformed, ' ');
92 else
93 buf_join_str(tags, tag->name, ' ');
94 }
95 }
96}
97
107void driver_tags_add(struct TagList *tl, char *new_tag)
108{
109 char *new_tag_transformed = mutt_hash_find(TagTransforms, new_tag);
110
111 struct Tag *tag = tag_new();
112 tag->name = new_tag;
113 tag->hidden = false;
114 tag->transformed = mutt_str_dup(new_tag_transformed);
115
116 /* filter out hidden tags */
117 const struct Slist *c_hidden_tags = cs_subset_slist(NeoMutt->sub, "hidden_tags");
118 if (c_hidden_tags)
119 if (mutt_list_find(&c_hidden_tags->head, new_tag))
120 tag->hidden = true;
121
122 STAILQ_INSERT_TAIL(tl, tag, entries);
123}
124
131void driver_tags_free(struct TagList *tl)
132{
133 if (!tl)
134 return;
135
136 struct Tag *tag = STAILQ_FIRST(tl);
137 struct Tag *next = NULL;
138 while (tag)
139 {
140 next = STAILQ_NEXT(tag, entries);
141 tag_free(&tag);
142 tag = next;
143 }
144 STAILQ_INIT(tl);
145}
146
152void driver_tags_get_transformed(struct TagList *tl, struct Buffer *tags)
153{
154 driver_tags_getter(tl, false, true, NULL, tags);
155}
156
164void driver_tags_get(struct TagList *tl, struct Buffer *tags)
165{
166 driver_tags_getter(tl, false, false, NULL, tags);
167}
168
174void driver_tags_get_with_hidden(struct TagList *tl, struct Buffer *tags)
175{
176 driver_tags_getter(tl, true, false, NULL, tags);
177}
178
187void driver_tags_get_transformed_for(struct TagList *tl, const char *name, struct Buffer *tags)
188{
189 driver_tags_getter(tl, true, true, name, tags);
190}
191
201bool driver_tags_replace(struct TagList *tl, const char *tags)
202{
203 if (!tl)
204 return false;
205
207
208 if (tags)
209 {
210 struct ListHead hsplit = STAILQ_HEAD_INITIALIZER(hsplit);
211 mutt_list_str_split(&hsplit, tags, ' ');
212 struct ListNode *np = NULL;
213 STAILQ_FOREACH(np, &hsplit, entries)
214 {
215 driver_tags_add(tl, np->data);
216 }
217 mutt_list_clear(&hsplit);
218 }
219 return true;
220}
221
225static void tags_deleter(int type, void *obj, intptr_t data)
226{
227 FREE(&obj);
228}
229
234{
237
240}
241
246{
249}
void buf_join_str(struct Buffer *buf, const char *str, char sep)
Join a buffer with a string separated by sep.
Definition: buffer.c:750
const struct Slist * cs_subset_slist(const struct ConfigSubset *sub, const char *name)
Get a string-list config item by name.
Definition: helpers.c:242
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
static void tags_deleter(int type, void *obj, intptr_t data)
Free our hash table data - Implements hash_hdata_free_t -.
Definition: tags.c:225
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition: hash.c:362
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:259
void mutt_hash_set_destructor(struct HashTable *table, hash_hdata_free_t fn, intptr_t fn_data)
Set the destructor for a Hash Table.
Definition: hash.c:301
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
#define MUTT_HASH_STRDUP_KEYS
make a copy of the keys
Definition: hash.h:111
#define MUTT_HASH_STRCASECMP
use strcasecmp() to compare keys
Definition: hash.h:110
struct ListNode * mutt_list_find(const struct ListHead *h, const char *data)
Find a string in a List.
Definition: list.c:103
void mutt_list_clear(struct ListHead *h)
Free a list, but NOT its strings.
Definition: list.c:166
size_t mutt_list_str_split(struct ListHead *head, const char *src, char sep)
Split a string into a list using a separator char.
Definition: list.c:246
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
#define STAILQ_INIT(head)
Definition: queue.h:372
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
#define STAILQ_NEXT(elm, field)
Definition: queue.h:400
String manipulation buffer.
Definition: buffer.h:36
A Hash Table.
Definition: hash.h:97
A List node for strings.
Definition: list.h:37
char * data
String.
Definition: list.h:38
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
String list.
Definition: slist.h:37
struct ListHead head
List containing values.
Definition: slist.h:38
LinkedList Tag Element.
Definition: tags.h:42
bool hidden
Tag should be hidden.
Definition: tags.h:45
char * transformed
Transformed name.
Definition: tags.h:44
char * name
Tag name.
Definition: tags.h:43
void driver_tags_getter(struct TagList *tl, bool show_hidden, bool show_transformed, const char *filter, struct Buffer *tags)
Get transformed tags separated by space.
Definition: tags.c:77
bool driver_tags_replace(struct TagList *tl, const char *tags)
Replace all tags.
Definition: tags.c:201
void driver_tags_cleanup(void)
Deinitialize structures used for tags.
Definition: tags.c:245
struct Tag * tag_new(void)
Create a new Tag.
Definition: tags.c:64
struct HashTable * TagFormats
Hash Table: "inbox" -> "GI" - Tag format strings.
Definition: tags.c:42
void driver_tags_get(struct TagList *tl, struct Buffer *tags)
Get tags all tags separated by space.
Definition: tags.c:164
void driver_tags_init(void)
Initialize structures used for tags.
Definition: tags.c:233
void driver_tags_add(struct TagList *tl, char *new_tag)
Add a tag to header.
Definition: tags.c:107
void driver_tags_get_with_hidden(struct TagList *tl, struct Buffer *tags)
Get all tags, also hidden ones, separated by space.
Definition: tags.c:174
struct HashTable * TagTransforms
Hash Table: "inbox" -> "i" - Alternative tag names.
Definition: tags.c:41
void driver_tags_free(struct TagList *tl)
Free tags from a header.
Definition: tags.c:131
void tag_free(struct Tag **ptr)
Free a Tag.
Definition: tags.c:48
void driver_tags_get_transformed(struct TagList *tl, struct Buffer *tags)
Get transformed tags separated by space.
Definition: tags.c:152
void driver_tags_get_transformed_for(struct TagList *tl, const char *name, struct Buffer *tags)
Get transformed tags for a tag name separated by space.
Definition: tags.c:187
Driver based email tags.