NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
ifdef.c
Go to the documentation of this file.
1
30
36
37#include "config.h"
38#include <stdbool.h>
39#include "mutt/lib.h"
40#include "config/lib.h"
41#include "core/lib.h"
42#include "gui/lib.h"
43#include "ifdef.h"
44#include "color/lib.h"
45#include "key/lib.h"
46#include "parse/lib.h"
47#include "store/lib.h"
48#include "version.h"
49
56static bool is_function(const char *name)
57{
58 int op = km_get_op(name);
59
60 return (op != OP_NULL);
61}
62
69static bool is_color_object(const char *name)
70{
71 int cid = mutt_map_get_value(name, ColorFields);
72
73 return (cid > 0);
74}
75
89enum CommandResult parse_ifdef(const struct Command *cmd, struct Buffer *line,
90 const struct ParseContext *pc, struct ParseError *pe)
91{
92 struct Buffer *err = pe->message;
93
94 if (!MoreArgs(line))
95 {
96 buf_printf(err, _("%s: too few arguments"), cmd->name);
97 return MUTT_CMD_WARNING;
98 }
99
100 struct Buffer *token = buf_pool_get();
102
104
105 // is the item defined as:
106 bool res = cs_subset_lookup(NeoMutt->sub, buf_string(token)) // a variable?
107 || feature_enabled(buf_string(token)) // a compiled-in feature?
108 || is_function(buf_string(token)) // a function?
109 || commands_get(&NeoMutt->commands, buf_string(token)) // a command?
110 || is_color_object(buf_string(token)) // a color?
111#ifdef USE_HCACHE
112 || store_is_valid_backend(buf_string(token)) // a store? (database)
113#endif
114 || mutt_str_getenv(buf_string(token)); // an environment variable?
115
116 if (!MoreArgs(line))
117 {
118 buf_printf(err, _("%s: too few arguments"), cmd->name);
119 rc = MUTT_CMD_WARNING;
120 goto done;
121 }
122 parse_extract_token(token, line, TOKEN_SPACE);
123
124 /* ifdef KNOWN_SYMBOL or ifndef UNKNOWN_SYMBOL */
125 if ((res && (cmd->id == CMD_IFDEF)) || (!res && (cmd->id == CMD_IFNDEF)))
126 {
127 // Cheat: Remove the `const` so we can recurse
128 rc = parse_rc_line(token, (struct ParseContext *) pc, pe);
129 if (rc == MUTT_CMD_ERROR)
130 mutt_error(_("Error: %s"), buf_string(err));
131
132 goto done;
133 }
134
135done:
136 buf_pool_release(&token);
137 return rc;
138}
139
150enum CommandResult parse_finish(const struct Command *cmd, struct Buffer *line,
151 const struct ParseContext *pc, struct ParseError *pe)
152{
153 struct Buffer *err = pe->message;
154
155 if (MoreArgs(line))
156 {
157 buf_printf(err, _("%s: too many arguments"), cmd->name);
158 return MUTT_CMD_WARNING;
159 }
160
161 return MUTT_CMD_FINISH;
162}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
Color and attribute parsing.
const struct Mapping ColorFields[]
Mapping of colour names to their IDs.
Definition commands.c:54
@ CMD_IFNDEF
:ifndef
Definition command.h:84
@ CMD_IFDEF
:ifdef
Definition command.h:83
CommandResult
Error codes for command_t parse functions.
Definition command.h:37
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:40
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:38
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:39
@ MUTT_CMD_FINISH
Finish: Stop processing this file.
Definition command.h:41
Convenience wrapper for the config headers.
const struct Command * commands_get(struct CommandArray *ca, const char *name)
Get a Command by its name.
Definition command.c:82
Convenience wrapper for the core headers.
int parse_extract_token(struct Buffer *dest, struct Buffer *line, TokenFlags flags)
Extract one token from a string.
Definition extract.c:49
#define TOKEN_SPACE
Don't treat whitespace as a term.
Definition extract.h:48
#define MoreArgs(buf)
Definition extract.h:31
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:45
enum CommandResult parse_finish(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'finish' command - Implements Command::parse() -.
Definition ifdef.c:150
enum CommandResult parse_ifdef(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'ifdef' and 'ifndef' commands - Implements Command::parse() -.
Definition ifdef.c:89
#define mutt_error(...)
Definition logging2.h:94
Convenience wrapper for the gui headers.
static bool is_color_object(const char *name)
Is the argument a neomutt colour?
Definition ifdef.c:69
static bool is_function(const char *name)
Is the argument a neomutt function?
Definition ifdef.c:56
Parse Ifdef Commands.
Manage keymappings.
int km_get_op(const char *func)
Get the OpCode for a Function.
Definition menu.c:184
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition mapping.c:85
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
const char * mutt_str_getenv(const char *name)
Get an environment variable.
Definition string.c:728
Text parsing functions.
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
enum CommandResult parse_rc_line(struct Buffer *line, struct ParseContext *pc, struct ParseError *pe)
Parse a line of user config.
Definition rc.c:45
Key value store.
bool store_is_valid_backend(const char *str)
Is the string a valid Store backend.
Definition store.c:126
String manipulation buffer.
Definition buffer.h:36
const char * name
Name of the Command.
Definition command.h:159
enum CommandId id
ID of the Command.
Definition command.h:160
Container for Accounts, Notifications.
Definition neomutt.h:41
struct CommandArray commands
NeoMutt commands.
Definition neomutt.h:53
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Context for config parsing (history/backtrace)
Definition pcontext.h:34
Detailed error information from config parsing.
Definition perror.h:34
struct Buffer * message
Error message.
Definition perror.h:35
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition subset.c:193
bool feature_enabled(const char *name)
Test if a compile-time feature is enabled.
Definition version.c:728
Display version and copyright about NeoMutt.