Skip to content

Commit

Permalink
allow += modification of my_ variables
Browse files Browse the repository at this point in the history
Fixes: neomutt#2937

- myvar: add myvar_append function
- command_parse: use it in parse_set for 'set my_var += text'
- command_parse: diagnose 'set my_var-=text' instead of silently
  treating it like =
- docs: tweak docs to match
  • Loading branch information
ebblake authored and flatcap committed Jun 18, 2021
1 parent f169e97 commit 6ad8de3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
16 changes: 15 additions & 1 deletion command_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

#include "config.h"
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
Expand Down Expand Up @@ -789,6 +790,11 @@ enum CommandResult parse_set(struct Buffer *buf, struct Buffer *s,
else
decrement = true;

if (my && decrement)
{
mutt_buffer_printf(err, _("Can't decrement a my_ variable"), set_commands[data]);
return MUTT_CMD_WARNING;
}
s->dptr++;
if (*s->dptr == '=')
{
Expand Down Expand Up @@ -894,7 +900,15 @@ enum CommandResult parse_set(struct Buffer *buf, struct Buffer *s,
mutt_extract_token(buf, s, MUTT_TOKEN_BACKTICK_VARS);
if (my)
{
myvar_set(name, buf->data);
assert(!decrement);
if (increment)
{
myvar_append(name, buf->data);
}
else
{
myvar_set(name, buf->data);
}
FREE(&name);
}
else
Expand Down
21 changes: 14 additions & 7 deletions docs/manual.xml.head
Original file line number Diff line number Diff line change
Expand Up @@ -6829,15 +6829,15 @@ set spam_separator=", "
<para>
This command is used to set (and unset)
<link linkend="variables">configuration variables</link>. There are
four basic types of variables: boolean, number, string, string list
several basic types of variables: boolean, number, string, string list
and quadoption. <emphasis>boolean</emphasis> variables can be
<emphasis>set</emphasis> (true) or <emphasis>unset</emphasis>
(false). <emphasis>number</emphasis> variables can be assigned
a positive integer value. Value of number variables can be
a positive integer value. The value of numeric variables can be
incremented <emphasis>+=</emphasis> and decremented
<emphasis>-=</emphasis>. <emphasis>String list</emphasis> variables
use <emphasis>+=</emphasis> for appending increment to the string list
and <emphasis>-=</emphasis> for removal decrement from the string list.
use <emphasis>+=</emphasis> for appending to the string list
and <emphasis>-=</emphasis> for removal from the string list.
<emphasis>string</emphasis> variables
consist of any number of printable characters and must be enclosed in
quotes if they contain spaces or tabs. You may also use the escape
Expand Down Expand Up @@ -6911,9 +6911,16 @@ set spam_separator=", "
</para>
<para>
The <command>set</command> command either creates a custom
<literal>my_</literal> variable or changes its value if it does
exist already. The <command>unset</command> and
<command>reset</command> commands remove the variable entirely.
<literal>my_</literal> variable or changes its value if it
exists already. Use of <emphasis>+=</emphasis> will adjust
a custom variable using the same behavior as a string
variable, by appending additional characters (this is true
even if the current contents of the variable resemble an
integer, which is different than the behavior of
<emphasis>+=</emphasis> on built-in numeric
variables). The <command>unset</command> and
<command>reset</command> commands remove the variable
entirely.
</para>
<para>
Since user-defined variables are expanded in the same way that
Expand Down
19 changes: 19 additions & 0 deletions myvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ void myvar_set(const char *var, const char *val)
TAILQ_INSERT_TAIL(&MyVars, myv, entries);
}

/**
* myvar_append - Append to the value of a "my_" variable
* @param var Variable name
* @param val Value to append
*/
void myvar_append(const char *var, const char *val)
{
struct MyVar *myv = myvar_find(var);

if (myv)
{
mutt_str_append_item(&myv->value, val, '\0');
return;
}

myv = myvar_new(var, val);
TAILQ_INSERT_TAIL(&MyVars, myv, entries);
}

/**
* myvar_del - Unset the value of a "my_" variable
* @param var Variable name
Expand Down
1 change: 1 addition & 0 deletions myvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern struct MyVarList MyVars; ///< List of all the user's custom config variab
void myvar_del(const char *var);
const char *myvar_get(const char *var);
void myvar_set(const char *var, const char *val);
void myvar_append(const char *var, const char *val);

void myvarlist_free(struct MyVarList *list);

Expand Down

0 comments on commit 6ad8de3

Please sign in to comment.