NeoMutt  2021-10-22-8-g9cb437
Teaching an old dog new tricks
DOXYGEN
tags.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stddef.h>
31 #include <stdbool.h>
32 #include "mutt/lib.h"
33 #include "config/lib.h"
34 #include "core/lib.h"
35 #include "tags.h"
36 
38 
49 static char *driver_tags_getter(struct TagList *head, bool show_hidden,
50  bool show_transformed, const char *filter)
51 {
52  if (!head)
53  return NULL;
54 
55  char *tags = NULL;
56  struct Tag *np = NULL;
57  STAILQ_FOREACH(np, head, entries)
58  {
59  if (filter && !mutt_str_equal(np->name, filter))
60  continue;
61  if (show_hidden || !np->hidden)
62  {
63  if (show_transformed && np->transformed)
64  mutt_str_append_item(&tags, np->transformed, ' ');
65  else
66  mutt_str_append_item(&tags, np->name, ' ');
67  }
68  }
69  return tags;
70 }
71 
81 void driver_tags_add(struct TagList *list, char *new_tag)
82 {
83  char *new_tag_transformed = mutt_hash_find(TagTransforms, new_tag);
84 
85  struct Tag *tn = mutt_mem_calloc(1, sizeof(struct Tag));
86  tn->name = new_tag;
87  tn->hidden = false;
88  if (new_tag_transformed)
89  tn->transformed = mutt_str_dup(new_tag_transformed);
90 
91  /* filter out hidden tags */
92  const struct Slist *c_hidden_tags =
93  cs_subset_slist(NeoMutt->sub, "hidden_tags");
94  if (c_hidden_tags)
95  if (mutt_list_find(&c_hidden_tags->head, new_tag))
96  tn->hidden = true;
97 
98  STAILQ_INSERT_TAIL(list, tn, entries);
99 }
100 
107 void driver_tags_free(struct TagList *list)
108 {
109  if (!list)
110  return;
111 
112  struct Tag *np = STAILQ_FIRST(list);
113  struct Tag *next = NULL;
114  while (np)
115  {
116  next = STAILQ_NEXT(np, entries);
117  FREE(&np->name);
118  FREE(&np->transformed);
119  FREE(&np);
120  np = next;
121  }
122  STAILQ_INIT(list);
123 }
124 
133 char *driver_tags_get_transformed(struct TagList *list)
134 {
135  return driver_tags_getter(list, false, true, NULL);
136 }
137 
145 char *driver_tags_get(struct TagList *list)
146 {
147  return driver_tags_getter(list, false, false, NULL);
148 }
149 
158 char *driver_tags_get_with_hidden(struct TagList *list)
159 {
160  return driver_tags_getter(list, true, false, NULL);
161 }
162 
172 char *driver_tags_get_transformed_for(struct TagList *head, const char *name)
173 {
174  return driver_tags_getter(head, true, true, name);
175 }
176 
186 bool driver_tags_replace(struct TagList *head, char *tags)
187 {
188  if (!head)
189  return false;
190 
191  driver_tags_free(head);
192 
193  if (tags)
194  {
195  struct ListHead hsplit = STAILQ_HEAD_INITIALIZER(hsplit);
196  mutt_list_str_split(&hsplit, tags, ' ');
197  struct ListNode *np = NULL;
198  STAILQ_FOREACH(np, &hsplit, entries)
199  {
200  driver_tags_add(head, np->data);
201  }
202  mutt_list_clear(&hsplit);
203  }
204  return true;
205 }
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
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:354
const struct Slist * cs_subset_slist(const struct ConfigSubset *sub, const char *name)
Get a string-list config item by name.
Definition: helpers.c:268
struct ListNode * mutt_list_find(const struct ListHead *h, const char *data)
Find a string in a List.
Definition: list.c:102
void mutt_list_clear(struct ListHead *h)
Free a list, but NOT its strings.
Definition: list.c:167
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:247
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:40
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
void mutt_str_append_item(char **str, const char *item, char sep)
Add string to another separated by sep.
Definition: string.c:466
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
#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
A Hash Table.
Definition: hash.h:97
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
String list.
Definition: slist.h:47
struct ListHead head
List containing values.
Definition: slist.h:48
LinkedList Tag Element.
Definition: tags.h:38
bool hidden
Tag should be hidden.
Definition: tags.h:41
char * transformed
Transformed name.
Definition: tags.h:40
char * name
Tag name.
Definition: tags.h:39
static char * driver_tags_getter(struct TagList *head, bool show_hidden, bool show_transformed, const char *filter)
Get transformed tags.
Definition: tags.c:49
char * driver_tags_get_transformed_for(struct TagList *head, const char *name)
Get transformed tag for a tag name from a header.
Definition: tags.c:172
void driver_tags_free(struct TagList *list)
Free tags from a header.
Definition: tags.c:107
char * driver_tags_get_transformed(struct TagList *list)
Get transformed tags.
Definition: tags.c:133
bool driver_tags_replace(struct TagList *head, char *tags)
Replace all tags.
Definition: tags.c:186
char * driver_tags_get_with_hidden(struct TagList *list)
Get tags with hiddens.
Definition: tags.c:158
struct HashTable * TagTransforms
Lookup table of alternative tag names.
Definition: tags.c:37
void driver_tags_add(struct TagList *list, char *new_tag)
Add a tag to header.
Definition: tags.c:81
char * driver_tags_get(struct TagList *list)
Get tags.
Definition: tags.c:145
Driver based email tags.