summarylogtreecommitdiffstats
path: root/ifdef.patch
blob: 1a760ef090ab5d7d3b86fd7aa4a0e6e003a10ef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
From: Cedric Duval <cedricduval@free.fr>
Date: Thu, 27 Feb 2014 12:06:21 +0100
Subject: ifdef

This command allows to test if a feature has been compiled in before
actually attempting to configure / use it.

Syntax:

ifdef <item> <command>

where <item> can be the name of a variable, function, or command.

Examples:

ifdef  imap-fetch-mail  'source ~/.mutt/imap_setup'
ifdef  trash  set trash=~/Mail/trash

* Patch last synced with upstream:
  - Date: 2007-02-15
  - File:
    http://cedricduval.free.fr/mutt/patches/download/patch-1.5.4.cd.ifdef.1

* Changes made:
  - Updated to 1.5.13
  - Also look for commands
  - Use mutt_strcmp in favor of ascii_strncasecmp to compare strings.

Signed-off-by: Matteo F. Vescovi <mfvescovi@gmail.com>

Gbp-Pq: Topic features
---
 doc/manual.xml.head | 22 ++++++++++++++++++++
 init.c              | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 init.h              |  2 ++
 3 files changed, 83 insertions(+)

diff --git a/doc/manual.xml.head b/doc/manual.xml.head
index baeddac..8948459 100644
--- a/doc/manual.xml.head
+++ b/doc/manual.xml.head
@@ -4411,6 +4411,28 @@ from which to read input (e.g.  <literal><command>source</command>
 
 </sect1>
 
+<sect1 id="ifdef">
+
+<title>Configuring features conditionally</title>
+
+<para>
+Usage: <literal>ifdef</literal> <emphasis>item</emphasis> <emphasis>command</emphasis>
+</para>
+
+<para>
+This command allows to test if a feature has been compiled in, before
+actually executing the command. Item can be either the name of a
+function, variable, or command. Example:
+</para>
+
+<para>
+<screen>
+ifdef imap_keepalive 'source ~/.mutt/imap_setup'
+</screen>
+</para>
+
+</sect1>
+
 <sect1 id="unhook">
 <title>Removing Hooks</title>
 
diff --git a/init.c b/init.c
index 118f06f..d95c9bc 100644
--- a/init.c
+++ b/init.c
@@ -601,6 +601,65 @@ static void remove_from_list (LIST **l, const char *str)
   }
 }
 
+static int parse_ifdef (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
+{
+  int i, j, res = 0;
+  BUFFER token;
+
+  memset (&token, 0, sizeof (token));
+  mutt_extract_token (tmp, s, 0);
+
+  /* is the item defined as a variable? */
+  res = (mutt_option_index (tmp->data) != -1);
+
+  /* a function? */
+  if (!res)
+    for (i = 0; !res && i < MENU_MAX; i++)
+    {
+      struct binding_t *b = km_get_table (Menus[i].value);
+
+      if (!b)
+	continue;
+
+      for (j = 0; b[j].name; j++)
+	if (!mutt_strcmp (tmp->data, b[j].name))
+	{
+	  res = 1;
+	  break;
+	}
+    }
+
+  /* a command? */
+  if (!res)
+    for (i = 0; Commands[i].name; i++)
+    {
+      if (!mutt_strcmp (tmp->data, Commands[i].name))
+      {
+	res = 1;
+	break;
+      }
+    }
+
+  if (!MoreArgs (s))
+  {
+    snprintf (err->data, err->dsize, _("ifdef: too few arguments"));
+    return (-1);
+  }
+  mutt_extract_token (tmp, s, M_TOKEN_SPACE);
+
+  if (res)
+  {
+    if (mutt_parse_rc_line (tmp->data, &token, err) == -1)
+    {
+      mutt_error ("Erreur: %s", err->data);
+      FREE (&token.data);
+      return (-1);
+    }
+    FREE (&token.data);
+  }
+  return 0;
+}
+
 static int parse_unignore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
 {
   do
diff --git a/init.h b/init.h
index 569f91e..6b49341 100644
--- a/init.h
+++ b/init.h
@@ -3596,6 +3596,7 @@ static int parse_lists (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_unlists (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_alias (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_unalias (BUFFER *, BUFFER *, unsigned long, BUFFER *);
+static int parse_ifdef (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_ignore (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_unignore (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_source (BUFFER *, BUFFER *, unsigned long, BUFFER *);
@@ -3646,6 +3647,7 @@ const struct command_t Commands[] = {
   { "group",		parse_group,		M_GROUP },
   { "ungroup",		parse_group,		M_UNGROUP },
   { "hdr_order",	parse_list,		UL &HeaderOrderList },
+  { "ifdef",		parse_ifdef,		0 },
 #ifdef HAVE_ICONV
   { "iconv-hook",	mutt_parse_hook,	M_ICONVHOOK },
 #endif