NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
quad.c
Go to the documentation of this file.
1
24
38
39#include "config.h"
40#include <limits.h>
41#include <stdbool.h>
42#include <stddef.h>
43#include <stdint.h>
44#include "mutt/lib.h"
45#include "quad.h"
46#include "set.h"
47#include "subset.h"
48#include "types.h"
49
55const char *QuadValues[] = {
56 "no", "yes", "ask-no", "ask-yes", NULL,
57};
58
62static int quad_string_set(void *var, struct ConfigDef *cdef, const char *value,
63 struct Buffer *err)
64{
65 if (!value)
66 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
67
68 int num = -1;
69 for (size_t i = 0; QuadValues[i]; i++)
70 {
71 if (mutt_istr_equal(QuadValues[i], value))
72 {
73 num = i;
74 break;
75 }
76 }
77
78 if (num < 0)
79 {
80 buf_printf(err, _("Invalid value for %s"), cdef->name);
81 buf_addch(err, '\n');
82 struct Buffer *list = buf_pool_get();
83 for (size_t i = 0; QuadValues[i]; i++)
84 {
85 if (i > 0)
86 buf_addstr(list, ", ");
87 buf_addstr(list, QuadValues[i]);
88 }
89 buf_add_printf(err, _("Choose from: %s"), buf_string(list));
90 buf_pool_release(&list);
92 }
93
94 if (var)
95 {
96 if (num == (*(char *) var))
98
99 if (startup_only(cdef, err))
101
102 if (cdef->validator)
103 {
104 int rc = cdef->validator(cdef, (intptr_t) num, err);
105
106 if (CSR_RESULT(rc) != CSR_SUCCESS)
107 return rc | CSR_INV_VALIDATOR;
108 }
109
110 *(char *) var = num;
111 }
112 else
113 {
114 cdef->initial = num;
115 }
116
117 return CSR_SUCCESS;
118}
119
123static int quad_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
124{
125 unsigned int value;
126
127 if (var)
128 value = *(char *) var;
129 else
130 value = (int) cdef->initial;
131
132 if (value >= (countof(QuadValues) - 1))
133 {
134 mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value); /* LCOV_EXCL_LINE */
135 return CSR_ERR_INVALID | CSR_INV_TYPE; /* LCOV_EXCL_LINE */
136 }
137
138 buf_addstr(result, QuadValues[value]);
139 return CSR_SUCCESS;
140}
141
145static int quad_native_set(void *var, const struct ConfigDef *cdef,
146 intptr_t value, struct Buffer *err)
147{
148 if ((value < 0) || (value >= (countof(QuadValues) - 1)))
149 {
150 buf_printf(err, _("Invalid quad value: %ld"), (long) value);
152 }
153
154 if (value == (*(char *) var))
156
157 if (startup_only(cdef, err))
159
160 if (cdef->validator)
161 {
162 int rc = cdef->validator(cdef, value, err);
163
164 if (CSR_RESULT(rc) != CSR_SUCCESS)
165 return rc | CSR_INV_VALIDATOR;
166 }
167
168 *(char *) var = value;
169 return CSR_SUCCESS;
170}
171
175static intptr_t quad_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
176{
177 return *(char *) var;
178}
179
183static bool quad_has_been_set(void *var, const struct ConfigDef *cdef)
184{
185 return (cdef->initial != (*(char *) var));
186}
187
191static int quad_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
192{
193 if (cdef->initial == (*(char *) var))
195
196 if (startup_only(cdef, err))
198
199 if (cdef->validator)
200 {
201 int rc = cdef->validator(cdef, cdef->initial, err);
202
203 if (CSR_RESULT(rc) != CSR_SUCCESS)
204 return rc | CSR_INV_VALIDATOR;
205 }
206
207 *(char *) var = cdef->initial;
208 return CSR_SUCCESS;
209}
210
220static int quad_toggle(int opt)
221{
222 return opt ^ 1;
223}
224
234int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
235{
236 if (!sub || !he || !he->data)
237 return CSR_ERR_CODE;
238
239 struct HashElem *he_base = cs_get_base(he);
240 if (CONFIG_TYPE(he_base->type) != DT_QUAD)
241 return CSR_ERR_CODE;
242
243 intptr_t value = cs_he_native_get(sub->cs, he, err);
244 if (value == INT_MIN)
245 return CSR_ERR_CODE;
246
247 value = quad_toggle(value);
248 int rc = cs_he_native_set(sub->cs, he, value, err);
249
250 if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
252
253 return rc;
254}
255
259const struct ConfigSetType CstQuad = {
260 DT_QUAD,
261 "quad",
266 NULL, // string_plus_equals
267 NULL, // string_minus_equals
270 NULL, // destroy
271};
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
const struct ConfigSetType CstQuad
Config type representing a quad-option.
Definition quad.c:259
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition set.c:161
int cs_he_native_set(const struct ConfigSet *cs, struct HashElem *he, intptr_t value, struct Buffer *err)
Natively set the value of a HashElem config item.
Definition set.c:737
intptr_t cs_he_native_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *err)
Natively get the value of a HashElem config item.
Definition set.c:842
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:296
#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_INV_WARNING
Report as a warning, not an error.
Definition set.h:48
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition set.h:42
#define CSR_ERR_CODE
Problem with the code.
Definition set.h:34
#define CSR_RESULT(x)
Extract the result code from CSR_* flags.
Definition set.h:53
#define CSR_SUCCESS
Action completed successfully.
Definition set.h:33
static bool quad_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value?
Definition quad.c:183
static intptr_t quad_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int object from a Quad-option config item - Implements ConfigSetType::native_get() -.
Definition quad.c:175
static int quad_native_set(void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Quad-option config item by int - Implements ConfigSetType::native_set() -.
Definition quad.c:145
static int quad_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Quad-option to its initial value - Implements ConfigSetType::reset() -.
Definition quad.c:191
static int quad_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Quad-option as a string - Implements ConfigSetType::string_get() -.
Definition quad.c:123
static int quad_string_set(void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Quad-option by string - Implements ConfigSetType::string_set() -.
Definition quad.c:62
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
#define countof(x)
Definition memory.h:49
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:677
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a quad.
Definition quad.c:234
static int quad_toggle(int opt)
Toggle (invert) the value of a quad option.
Definition quad.c:220
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition quad.c:55
Type representing a quad-option.
String manipulation buffer.
Definition buffer.h:36
const char * name
User-visible name.
Definition set.h:66
int(* validator)(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition set.h:82
intptr_t initial
Initial value.
Definition set.h:68
A set of inherited config items.
Definition subset.h:46
struct ConfigSet * cs
Parent ConfigSet.
Definition subset.h:50
The item stored in a Hash Table.
Definition hash.h:44
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition hash.h:45
void * data
User-supplied data.
Definition hash.h:47
void cs_subset_notify_observers(const struct ConfigSubset *sub, struct HashElem *he, enum NotifyConfig ev)
Notify all observers of an event.
Definition subset.c:243
Subset of Config Items.
@ NT_CONFIG_SET
Config item has been set.
Definition subset.h:61
Constants for all the config types.
#define CONFIG_TYPE(t)
Extract the type from the flags.
Definition types.h:50
@ DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition types.h:40