NeoMutt  2021-02-05-89-gabe350
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 
82 void driver_tags_add(struct TagList *list, char *new_tag)
83 {
84  char *new_tag_transformed = mutt_hash_find(TagTransforms, new_tag);
85 
86  struct Tag *tn = mutt_mem_calloc(1, sizeof(struct Tag));
87  tn->name = new_tag;
88  tn->hidden = false;
89  if (new_tag_transformed)
90  tn->transformed = mutt_str_dup(new_tag_transformed);
91 
92  /* filter out hidden tags */
93  if (C_HiddenTags)
94  if (mutt_list_find(&C_HiddenTags->head, new_tag))
95  tn->hidden = true;
96 
97  STAILQ_INSERT_TAIL(list, tn, entries);
98 }
99 
106 void driver_tags_free(struct TagList *list)
107 {
108  if (!list)
109  return;
110 
111  struct Tag *np = STAILQ_FIRST(list);
112  struct Tag *next = NULL;
113  while (np)
114  {
115  next = STAILQ_NEXT(np, entries);
116  FREE(&np->name);
117  FREE(&np->transformed);
118  FREE(&np);
119  np = next;
120  }
121  STAILQ_INIT(list);
122 }
123 
132 char *driver_tags_get_transformed(struct TagList *list)
133 {
134  return driver_tags_getter(list, false, true, NULL);
135 }
136 
144 char *driver_tags_get(struct TagList *list)
145 {
146  return driver_tags_getter(list, false, false, NULL);
147 }
148 
157 char *driver_tags_get_with_hidden(struct TagList *list)
158 {
159  return driver_tags_getter(list, true, false, NULL);
160 }
161 
171 char *driver_tags_get_transformed_for(struct TagList *head, const char *name)
172 {
173  return driver_tags_getter(head, true, true, name);
174 }
175 
185 bool driver_tags_replace(struct TagList *head, char *tags)
186 {
187  if (!head)
188  return false;
189 
190  driver_tags_free(head);
191 
192  if (tags)
193  {
194  struct ListHead hsplit = STAILQ_HEAD_INITIALIZER(hsplit);
195  mutt_list_str_split(&hsplit, tags, ' ');
196  struct ListNode *np = NULL;
197  STAILQ_FOREACH(np, &hsplit, entries)
198  {
199  driver_tags_add(head, np->data);
200  }
201  mutt_list_clear(&hsplit);
202  }
203  return true;
204 }
mutt_str_append_item
void mutt_str_append_item(char **str, const char *item, char sep)
Add string to another separated by sep.
Definition: string.c:466
TagTransforms
struct HashTable * TagTransforms
Lookup table of alternative tag names.
Definition: tags.c:38
STAILQ_INIT
#define STAILQ_INIT(head)
Definition: queue.h:369
mutt_mem_calloc
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
C_HiddenTags
struct Slist * C_HiddenTags
Config: Tags that shouldn't be displayed on screen.
Definition: tags.c:36
ListNode
A List node for strings.
Definition: list.h:34
mutt_list_find
struct ListNode * mutt_list_find(const struct ListHead *h, const char *data)
Find a string in a List.
Definition: list.c:102
driver_tags_replace
bool driver_tags_replace(struct TagList *head, char *tags)
Replace all tags.
Definition: tags.c:185
driver_tags_free
void driver_tags_free(struct TagList *list)
Free tags from a header.
Definition: tags.c:106
tags.h
Slist::head
struct ListHead head
Definition: slist.h:48
mutt_str_dup
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
FREE
#define FREE(x)
Definition: memory.h:40
STAILQ_FIRST
#define STAILQ_FIRST(head)
Definition: queue.h:347
Tag::hidden
bool hidden
Tag should be hidden.
Definition: tags.h:44
STAILQ_FOREACH
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
Tag::name
char * name
Tag name.
Definition: tags.h:42
mutt_hash_find
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
mutt_str_equal
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
driver_tags_get_transformed
char * driver_tags_get_transformed(struct TagList *list)
Get transformed tags.
Definition: tags.c:132
driver_tags_add
void driver_tags_add(struct TagList *list, char *new_tag)
Add a tag to header.
Definition: tags.c:82
lib.h
Tag
LinkedList Tag Element.
Definition: tags.h:40
STAILQ_HEAD_INITIALIZER
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:321
driver_tags_get_transformed_for
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:171
driver_tags_get
char * driver_tags_get(struct TagList *list)
Get tags.
Definition: tags.c:144
STAILQ_INSERT_TAIL
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:386
mutt_list_clear
void mutt_list_clear(struct ListHead *h)
Free a list, but NOT its strings.
Definition: list.c:167
mutt_list_str_split
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
ListNode::data
char * data
String.
Definition: list.h:36
Tag::transformed
char * transformed
Transformed name.
Definition: tags.h:43
STAILQ_NEXT
#define STAILQ_NEXT(elm, field)
Definition: queue.h:397
HashTable
A Hash Table.
Definition: hash.h:84
driver_tags_getter
static char * driver_tags_getter(struct TagList *head, bool show_hidden, bool show_transformed, const char *filter)
Get transformed tags.
Definition: tags.c:50
driver_tags_get_with_hidden
char * driver_tags_get_with_hidden(struct TagList *list)
Get tags with hiddens.
Definition: tags.c:157
Slist
String list.
Definition: slist.h:46