NeoMutt  2020-09-25
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 "tags.h"
34 
35 /* These Config Variables are only used in email/tags.c */
37 
39 
50 static char *driver_tags_getter(struct TagList *head, bool show_hidden,
51  bool show_transformed, const char *filter)
52 {
53  if (!head)
54  return NULL;
55 
56  char *tags = NULL;
57  struct Tag *np = NULL;
58  STAILQ_FOREACH(np, head, entries)
59  {
60  if (filter && !mutt_str_equal(np->name, filter))
61  continue;
62  if (show_hidden || !np->hidden)
63  {
64  if (show_transformed && np->transformed)
65  mutt_str_append_item(&tags, np->transformed, ' ');
66  else
67  mutt_str_append_item(&tags, np->name, ' ');
68  }
69  }
70  return tags;
71 }
72 
80 static void driver_tags_add(struct TagList *list, char *new_tag)
81 {
82  char *new_tag_transformed = mutt_hash_find(TagTransforms, new_tag);
83 
84  struct Tag *tn = mutt_mem_calloc(1, sizeof(struct Tag));
85  tn->name = mutt_str_dup(new_tag);
86  tn->hidden = false;
87  if (new_tag_transformed)
88  tn->transformed = mutt_str_dup(new_tag_transformed);
89 
90  /* filter out hidden tags */
91  if (C_HiddenTags)
92  if (mutt_list_find(&C_HiddenTags->head, new_tag))
93  tn->hidden = true;
94 
95  STAILQ_INSERT_TAIL(list, tn, entries);
96 }
97 
104 void driver_tags_free(struct TagList *list)
105 {
106  if (!list)
107  return;
108 
109  struct Tag *np = STAILQ_FIRST(list);
110  struct Tag *next = NULL;
111  while (np)
112  {
113  next = STAILQ_NEXT(np, entries);
114  FREE(&np->name);
115  FREE(&np->transformed);
116  FREE(&np);
117  np = next;
118  }
119  STAILQ_INIT(list);
120 }
121 
130 char *driver_tags_get_transformed(struct TagList *list)
131 {
132  return driver_tags_getter(list, false, true, NULL);
133 }
134 
142 char *driver_tags_get(struct TagList *list)
143 {
144  return driver_tags_getter(list, false, false, NULL);
145 }
146 
155 char *driver_tags_get_with_hidden(struct TagList *list)
156 {
157  return driver_tags_getter(list, true, false, NULL);
158 }
159 
169 char *driver_tags_get_transformed_for(struct TagList *head, const char *name)
170 {
171  return driver_tags_getter(head, true, true, name);
172 }
173 
183 bool driver_tags_replace(struct TagList *head, char *tags)
184 {
185  if (!head)
186  return false;
187 
188  driver_tags_free(head);
189 
190  if (tags)
191  {
192  struct ListHead hsplit = STAILQ_HEAD_INITIALIZER(hsplit);
193  mutt_list_str_split(&hsplit, tags, ' ');
194  struct ListNode *np = NULL;
195  STAILQ_FOREACH(np, &hsplit, entries)
196  {
197  driver_tags_add(head, np->data);
198  }
199  mutt_list_free(&hsplit);
200  }
201  return true;
202 }
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
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
A Hash Table.
Definition: hash.h:84
LinkedList Tag Element.
Definition: tags.h:40
char * name
Tag name.
Definition: tags.h:42
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:386
String list.
Definition: slist.h:45
void driver_tags_free(struct TagList *list)
Free tags from a header.
Definition: tags.c:104
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
#define STAILQ_INIT(head)
Definition: queue.h:369
struct Slist * C_HiddenTags
Config: Tags that shouldn&#39;t be displayed on screen.
Definition: tags.c:36
struct ListHead head
Definition: slist.h:47
struct ListNode * mutt_list_find(const struct ListHead *h, const char *data)
Find a string in a List.
Definition: list.c:102
bool hidden
Tag should be hidden.
Definition: tags.h:44
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
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:169
char * driver_tags_get(struct TagList *list)
Get tags.
Definition: tags.c:142
struct HashTable * TagTransforms
Lookup table of alternative tag names.
Definition: tags.c:38
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
#define STAILQ_NEXT(elm, field)
Definition: queue.h:397
char * driver_tags_get_with_hidden(struct TagList *list)
Get tags with hiddens.
Definition: tags.c:155
char * data
String.
Definition: list.h:36
char * driver_tags_get_transformed(struct TagList *list)
Get transformed tags.
Definition: tags.c:130
static char * driver_tags_getter(struct TagList *head, bool show_hidden, bool show_transformed, const char *filter)
Get transformed tags.
Definition: tags.c:50
#define FREE(x)
Definition: memory.h:40
static void driver_tags_add(struct TagList *list, char *new_tag)
Add a tag to header.
Definition: tags.c:80
Driver based email tags.
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:321
bool driver_tags_replace(struct TagList *head, char *tags)
Replace all tags.
Definition: tags.c:183
Convenience wrapper for the library headers.
void mutt_str_append_item(char **str, const char *item, char sep)
Add string to another separated by sep.
Definition: string.c:466
A List node for strings.
Definition: list.h:34
char * transformed
Transformed name.
Definition: tags.h:43
#define STAILQ_FIRST(head)
Definition: queue.h:347