NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sort.c
Go to the documentation of this file.
1
35#include "config.h"
36#include <stdint.h>
37#include <string.h>
38#include "mutt/lib.h"
39#include "set.h"
40#include "sort2.h"
41#include "types.h"
42
43#define PREFIX_REVERSE "reverse-"
44#define PREFIX_LAST "last-"
45
49static int sort_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
50 const char *value, struct Buffer *err)
51{
52 intptr_t id = -1;
53 uint16_t flags = 0;
54
55 if (!value || (value[0] == '\0'))
56 {
57 buf_printf(err, _("Option %s may not be empty"), cdef->name);
59 }
60
61 size_t plen = 0;
62
63 if (cdef->type & D_SORT_REVERSE)
64 {
66 if (plen != 0)
67 {
68 flags |= SORT_REVERSE;
69 value += plen;
70 }
71 }
72
73 if (cdef->type & D_SORT_LAST)
74 {
75 plen = mutt_str_startswith(value, PREFIX_LAST);
76 if (plen != 0)
77 {
78 flags |= SORT_LAST;
79 value += plen;
80 }
81 }
82
83 id = mutt_map_get_value(value, (struct Mapping *) cdef->data);
84
85 if (id < 0)
86 {
87 buf_printf(err, _("Invalid sort name: %s"), value);
89 }
90
91 id |= flags;
92
93 if (var)
94 {
95 if (id == (*(short *) var))
97
98 if (startup_only(cdef, err))
100
101 if (cdef->validator)
102 {
103 int rc = cdef->validator(cs, cdef, (intptr_t) id, err);
104
105 if (CSR_RESULT(rc) != CSR_SUCCESS)
106 return rc | CSR_INV_VALIDATOR;
107 }
108
109 *(short *) var = id;
110 }
111 else
112 {
113 cdef->initial = id;
114 }
115
116 return CSR_SUCCESS;
117}
118
122static int sort_string_get(const struct ConfigSet *cs, void *var,
123 const struct ConfigDef *cdef, struct Buffer *result)
124{
125 int sort;
126
127 if (var)
128 sort = *(short *) var;
129 else
130 sort = (int) cdef->initial;
131
132 if (sort & SORT_REVERSE)
133 buf_addstr(result, PREFIX_REVERSE);
134 if (sort & SORT_LAST)
135 buf_addstr(result, PREFIX_LAST);
136
137 sort &= SORT_MASK;
138
139 const char *str = NULL;
140
141 str = mutt_map_get_name(sort, (struct Mapping *) cdef->data);
142
143 if (!str)
144 {
145 mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d/%d\n", cdef->type, sort);
147 }
148
149 buf_addstr(result, str);
150 return CSR_SUCCESS;
151}
152
156static int sort_native_set(const struct ConfigSet *cs, void *var,
157 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
158{
159 const char *str = NULL;
160
161 str = mutt_map_get_name((value & SORT_MASK), (struct Mapping *) cdef->data);
162
163 if (!str)
164 {
165 buf_printf(err, _("Invalid sort type: %ld"), (long) value);
167 }
168
169 if (value == (*(short *) var))
171
172 if (startup_only(cdef, err))
174
175 if (cdef->validator)
176 {
177 int rc = cdef->validator(cs, cdef, value, err);
178
179 if (CSR_RESULT(rc) != CSR_SUCCESS)
180 return rc | CSR_INV_VALIDATOR;
181 }
182
183 *(short *) var = value;
184 return CSR_SUCCESS;
185}
186
190static intptr_t sort_native_get(const struct ConfigSet *cs, void *var,
191 const struct ConfigDef *cdef, struct Buffer *err)
192{
193 return *(short *) var;
194}
195
199static int sort_reset(const struct ConfigSet *cs, void *var,
200 const struct ConfigDef *cdef, struct Buffer *err)
201{
202 if (cdef->initial == (*(short *) var))
204
205 if (startup_only(cdef, err))
207
208 if (cdef->validator)
209 {
210 int rc = cdef->validator(cs, cdef, cdef->initial, err);
211
212 if (CSR_RESULT(rc) != CSR_SUCCESS)
213 return rc | CSR_INV_VALIDATOR;
214 }
215
216 *(short *) var = cdef->initial;
217 return CSR_SUCCESS;
218}
219
223const struct ConfigSetType CstSort = {
224 DT_SORT,
225 "sort",
230 NULL, // string_plus_equals
231 NULL, // string_minus_equals
233 NULL, // destroy
234};
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
static bool startup_only(const struct ConfigDef *cdef, struct Buffer *err)
Validator function for D_ON_STARTUP.
Definition: set.h:296
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:38
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition: set.h:44
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define PREFIX_REVERSE
Definition: sort.c:43
#define PREFIX_LAST
Definition: sort.c:44
const struct ConfigSetType CstSort
Config type representing a sort option.
Definition: sort.c:223
static intptr_t sort_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int from a Sort config item - Implements ConfigSetType::native_get() -.
Definition: sort.c:190
static int sort_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Sort config item by int - Implements ConfigSetType::native_set() -.
Definition: sort.c:156
static int sort_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Sort to its initial value - Implements ConfigSetType::reset() -.
Definition: sort.c:199
static int sort_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Sort as a string - Implements ConfigSetType::string_get() -.
Definition: sort.c:122
static int sort_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Sort by string - Implements ConfigSetType::string_set() -.
Definition: sort.c:49
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:85
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:230
Parse the 'set' command.
Type representing a sort option.
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:70
#define SORT_LAST
Sort thread by last-X, e.g. received date.
Definition: sort2.h:72
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:71
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:64
const char * name
User-visible name.
Definition: set.h:65
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
intptr_t data
Extra variable data.
Definition: set.h:68
intptr_t initial
Initial value.
Definition: set.h:67
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
Container for lots of config items.
Definition: set.h:252
Mapping between user-readable string and a constant.
Definition: mapping.h:33
Constants for all the config types.
@ DT_SORT
sorting methods
Definition: types.h:44
#define D_SORT_LAST
Sort flag for -last prefix.
Definition: types.h:119
#define D_SORT_REVERSE
Sort flag for -reverse prefix.
Definition: types.h:120