NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sort.c
Go to the documentation of this file.
1
24
34
35#include "config.h"
36#include <stdbool.h>
37#include <stdint.h>
38#include <string.h>
39#include "mutt/lib.h"
40#include "sort.h"
41#include "set.h"
42#include "types.h"
43
45#define PREFIX_REVERSE "reverse-"
47#define PREFIX_LAST "last-"
48
52static int sort_string_set(void *var, struct ConfigDef *cdef, const char *value,
53 struct Buffer *err)
54{
55 intptr_t id = -1;
56 uint16_t flags = 0;
57
58 if (!value || (value[0] == '\0'))
59 {
60 buf_printf(err, _("Option %s may not be empty"), cdef->name);
62 }
63
64 size_t plen = 0;
65
66 if (cdef->type & D_SORT_REVERSE)
67 {
69 if (plen != 0)
70 {
71 flags |= SORT_REVERSE;
72 value += plen;
73 }
74 }
75
76 if (cdef->type & D_SORT_LAST)
77 {
78 plen = mutt_str_startswith(value, PREFIX_LAST);
79 if (plen != 0)
80 {
81 flags |= SORT_LAST;
82 value += plen;
83 }
84 }
85
86 id = mutt_map_get_value(value, (struct Mapping *) cdef->data);
87
88 if (id < 0)
89 {
90 buf_printf(err, _("Invalid sort name: %s"), value);
92 }
93
94 id |= flags;
95
96 if (var)
97 {
98 if (id == (*(short *) var))
100
101 if (startup_only(cdef, err))
103
104 if (cdef->validator)
105 {
106 int rc = cdef->validator(cdef, (intptr_t) id, err);
107
108 if (CSR_RESULT(rc) != CSR_SUCCESS)
109 return rc | CSR_INV_VALIDATOR;
110 }
111
112 *(short *) var = id;
113 }
114 else
115 {
116 cdef->initial = id;
117 }
118
119 return CSR_SUCCESS;
120}
121
125static int sort_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
126{
127 int sort;
128
129 if (var)
130 sort = *(short *) var;
131 else
132 sort = (int) cdef->initial;
133
134 if (sort & SORT_REVERSE)
135 buf_addstr(result, PREFIX_REVERSE);
136 if (sort & SORT_LAST)
137 buf_addstr(result, PREFIX_LAST);
138
139 sort &= SORT_MASK;
140
141 const char *str = NULL;
142
143 str = mutt_map_get_name(sort, (struct Mapping *) cdef->data);
144
145 if (!str)
146 {
147 mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d/%d\n", cdef->type, sort);
149 }
150
151 buf_addstr(result, str);
152 return CSR_SUCCESS;
153}
154
158static int sort_native_set(void *var, const struct ConfigDef *cdef,
159 intptr_t value, struct Buffer *err)
160{
161 const char *str = NULL;
162
163 str = mutt_map_get_name((value & SORT_MASK), (struct Mapping *) cdef->data);
164
165 if (!str)
166 {
167 buf_printf(err, _("Invalid sort type: %ld"), (long) value);
169 }
170
171 if (value == (*(short *) var))
173
174 if (startup_only(cdef, err))
176
177 if (cdef->validator)
178 {
179 int rc = cdef->validator(cdef, value, err);
180
181 if (CSR_RESULT(rc) != CSR_SUCCESS)
182 return rc | CSR_INV_VALIDATOR;
183 }
184
185 *(short *) var = value;
186 return CSR_SUCCESS;
187}
188
192static intptr_t sort_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
193{
194 return *(short *) var;
195}
196
200static bool sort_has_been_set(void *var, const struct ConfigDef *cdef)
201{
202 return (cdef->initial != (*(short *) var));
203}
204
208static int sort_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
209{
210 if (cdef->initial == (*(short *) var))
212
213 if (startup_only(cdef, err))
215
216 if (cdef->validator)
217 {
218 int rc = cdef->validator(cdef, cdef->initial, err);
219
220 if (CSR_RESULT(rc) != CSR_SUCCESS)
221 return rc | CSR_INV_VALIDATOR;
222 }
223
224 *(short *) var = cdef->initial;
225 return CSR_SUCCESS;
226}
227
231const struct ConfigSetType CstSort = {
232 DT_SORT,
233 "sort",
238 NULL, // string_plus_equals
239 NULL, // string_minus_equals
242 NULL, // destroy
243};
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
const struct ConfigSetType CstSort
Config type representing a sort option.
Definition sort.c:231
A collection of config items.
static bool startup_only(const struct ConfigDef *cdef, struct Buffer *err)
Validator function for D_ON_STARTUP.
Definition set.h:295
#define CSR_ERR_INVALID
Value hasn't been set.
Definition set.h:36
#define CSR_INV_TYPE
Value is not valid for the type.
Definition set.h:45
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition set.h:46
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition set.h:42
#define CSR_RESULT(x)
Extract the result code from CSR_* flags.
Definition set.h:52
#define CSR_SUCCESS
Action completed successfully.
Definition set.h:33
#define PREFIX_REVERSE
Prefix for reverse sorting.
Definition sort.c:45
#define PREFIX_LAST
Prefix for last sorting (used in threading)
Definition sort.c:47
Type representing a sort option.
#define SORT_MASK
Mask for the sort id.
Definition sort.h:39
#define SORT_LAST
Sort thread by last-X, e.g. received date.
Definition sort.h:41
#define SORT_REVERSE
Reverse the order of the sort.
Definition sort.h:40
static bool sort_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value?
Definition sort.c:200
static intptr_t sort_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int from a Sort config item - Implements ConfigSetType::native_get() -.
Definition sort.c:192
static int sort_native_set(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:158
static int sort_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Sort to its initial value - Implements ConfigSetType::reset() -.
Definition sort.c:208
static int sort_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Sort as a string - Implements ConfigSetType::string_get() -.
Definition sort.c:125
static int sort_string_set(void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Sort by string - Implements ConfigSetType::string_set() -.
Definition sort.c:52
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
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:234
String manipulation buffer.
Definition buffer.h:36
const char * name
User-visible name.
Definition set.h:65
int(* validator)(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition set.h:81
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
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:43
#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