NeoMutt  2025-01-09-117-gace867
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 <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
44#define PREFIX_REVERSE "reverse-"
45#define PREFIX_LAST "last-"
46
50static int sort_string_set(void *var, struct ConfigDef *cdef, const char *value,
51 struct Buffer *err)
52{
53 intptr_t id = -1;
54 uint16_t flags = 0;
55
56 if (!value || (value[0] == '\0'))
57 {
58 buf_printf(err, _("Option %s may not be empty"), cdef->name);
60 }
61
62 size_t plen = 0;
63
64 if (cdef->type & D_SORT_REVERSE)
65 {
67 if (plen != 0)
68 {
69 flags |= SORT_REVERSE;
70 value += plen;
71 }
72 }
73
74 if (cdef->type & D_SORT_LAST)
75 {
76 plen = mutt_str_startswith(value, PREFIX_LAST);
77 if (plen != 0)
78 {
79 flags |= SORT_LAST;
80 value += plen;
81 }
82 }
83
84 id = mutt_map_get_value(value, (struct Mapping *) cdef->data);
85
86 if (id < 0)
87 {
88 buf_printf(err, _("Invalid sort name: %s"), value);
90 }
91
92 id |= flags;
93
94 if (var)
95 {
96 if (id == (*(short *) var))
98
99 if (startup_only(cdef, err))
101
102 if (cdef->validator)
103 {
104 int rc = cdef->validator(cdef, (intptr_t) id, err);
105
106 if (CSR_RESULT(rc) != CSR_SUCCESS)
107 return rc | CSR_INV_VALIDATOR;
108 }
109
110 *(short *) var = id;
111 }
112 else
113 {
114 cdef->initial = id;
115 }
116
117 return CSR_SUCCESS;
118}
119
123static int sort_string_get(void *var, 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(void *var, const struct ConfigDef *cdef,
157 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(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(void *var, const struct ConfigDef *cdef, struct Buffer *err)
191{
192 return *(short *) var;
193}
194
198static bool sort_has_been_set(void *var, const struct ConfigDef *cdef)
199{
200 return (cdef->initial != (*(short *) var));
201}
202
206static int sort_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
207{
208 if (cdef->initial == (*(short *) var))
210
211 if (startup_only(cdef, err))
213
214 if (cdef->validator)
215 {
216 int rc = cdef->validator(cdef, cdef->initial, err);
217
218 if (CSR_RESULT(rc) != CSR_SUCCESS)
219 return rc | CSR_INV_VALIDATOR;
220 }
221
222 *(short *) var = cdef->initial;
223 return CSR_SUCCESS;
224}
225
229const struct ConfigSetType CstSort = {
230 DT_SORT,
231 "sort",
236 NULL, // string_plus_equals
237 NULL, // string_minus_equals
240 NULL, // destroy
241};
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:293
#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)
Definition: set.h:50
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:33
#define PREFIX_REVERSE
Definition: sort.c:44
#define PREFIX_LAST
Definition: sort.c:45
const struct ConfigSetType CstSort
Config type representing a sort option.
Definition: sort.c:229
#define SORT_MASK
Mask for the sort id.
Definition: sort.h:38
#define SORT_LAST
Sort thread by last-X, e.g. received date.
Definition: sort.h:40
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort.h:39
static bool sort_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value? - Implements ConfigSetType::has_been_set() -.
Definition: sort.c:198
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:190
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:156
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:206
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:123
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:50
#define mutt_debug(LEVEL,...)
Definition: logging2.h:90
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:44
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:231
Parse the 'set' command.
Sidebar sorting functions.
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:62
const char * name
User-visible name.
Definition: set.h:63
int(* validator)(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:79
intptr_t data
Extra variable data.
Definition: set.h:66
intptr_t initial
Initial value.
Definition: set.h:65
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:64
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:118
#define D_SORT_REVERSE
Sort flag for -reverse prefix.
Definition: types.h:119