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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
* alias-command.c - alias commands
*
* Copyright (C) 2003-2018 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "alias.h"
#include "alias-config.h"
/*
* Adds a new alias.
*/
void
alias_command_add (const char *alias_name, const char *command,
const char *completion)
{
struct t_config_option *ptr_option;
/* define new alias */
if (!alias_new (alias_name, command, completion))
{
weechat_printf (NULL,
_("%s%s: error creating alias \"%s\" "
"=> \"%s\""),
weechat_prefix ("error"), ALIAS_PLUGIN_NAME,
alias_name, command);
return;
}
/* create configuration option for command */
ptr_option = weechat_config_search_option (alias_config_file,
alias_config_section_cmd,
alias_name);
if (ptr_option)
weechat_config_option_free (ptr_option);
alias_config_cmd_new_option (alias_name, command);
/* create configuration option for completion */
ptr_option = weechat_config_search_option (alias_config_file,
alias_config_section_completion,
alias_name);
if (ptr_option)
weechat_config_option_free (ptr_option);
if (completion)
alias_config_completion_new_option (alias_name, completion);
/* display message */
weechat_printf (NULL,
_("Alias \"%s\" => \"%s\" created"),
alias_name, command);
}
/*
* Callback for command "/alias": displays or creates alias.
*/
int
alias_command_cb (const void *pointer, void *data,
struct t_gui_buffer *buffer, int argc,
char **argv, char **argv_eol)
{
char *ptr_alias_name;
struct t_alias *ptr_alias;
struct t_config_option *ptr_option;
int alias_found, i;
/* make C compiler happy */
(void) pointer;
(void) data;
(void) buffer;
/* List all aliases */
if ((argc == 1) || (weechat_strcasecmp (argv[1], "list") == 0))
{
if (alias_list)
{
/* get pointer to alias name */
ptr_alias_name = NULL;
if (argc > 1)
{
ptr_alias_name = (weechat_string_is_command_char (argv[2])) ?
(char *)weechat_utf8_next_char (argv[2]) : argv[2];
}
/* display list of aliases */
alias_found = 0;
for (ptr_alias = alias_list; ptr_alias;
ptr_alias = ptr_alias->next_alias)
{
if (!ptr_alias_name
|| weechat_strcasestr (ptr_alias->name, ptr_alias_name))
{
if (!alias_found)
{
weechat_printf (NULL, "");
if (ptr_alias_name)
{
weechat_printf (NULL,
_("Aliases with \"%s\":"),
ptr_alias_name);
}
else
{
weechat_printf (NULL, _("All aliases:"));
}
}
ptr_option = weechat_config_search_option (
alias_config_file,
alias_config_section_completion,
ptr_alias->name);
if (ptr_option)
{
weechat_printf (
NULL,
" %s %s=>%s %s %s(%s%s %s%s)%s",
ptr_alias->name,
weechat_color ("chat_delimiters"),
weechat_color ("chat"),
ptr_alias->command,
weechat_color ("chat_delimiters"),
weechat_color ("chat"),
_("completion:"),
weechat_config_string (ptr_option),
weechat_color ("chat_delimiters"),
weechat_color ("chat"));
}
else
{
weechat_printf (
NULL,
" %s %s=>%s %s",
ptr_alias->name,
weechat_color ("chat_delimiters"),
weechat_color ("chat"),
ptr_alias->command);
}
alias_found = 1;
}
}
if (!alias_found)
{
weechat_printf (NULL, _("No alias found matching \"%s\""),
(ptr_alias_name) ? ptr_alias_name : "");
}
}
else
{
weechat_printf (NULL, _("No alias defined"));
}
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp (argv[1], "add") == 0)
{
WEECHAT_COMMAND_MIN_ARGS(4, "add");
alias_command_add (
(weechat_string_is_command_char (argv[2])) ?
(char *)weechat_utf8_next_char (argv[2]) : argv[2],
argv_eol[3],
NULL);
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp (argv[1], "addcompletion") == 0)
{
WEECHAT_COMMAND_MIN_ARGS(5, "addcompletion");
alias_command_add (
(weechat_string_is_command_char (argv[3])) ?
(char *)weechat_utf8_next_char (argv[3]) : argv[3],
argv_eol[4],
argv[2]);
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp (argv[1], "del") == 0)
{
WEECHAT_COMMAND_MIN_ARGS(3, "del");
for (i = 2; i < argc; i++)
{
ptr_alias_name = (weechat_string_is_command_char (argv[i])) ?
(char *)weechat_utf8_next_char (argv[i]) : argv[i];
ptr_alias = alias_search (ptr_alias_name);
if (!ptr_alias)
{
weechat_printf (NULL,
_("%sAlias \"%s\" not found"),
weechat_prefix ("error"),
ptr_alias_name);
}
else
{
/* remove alias */
alias_free (ptr_alias);
/* remove options */
ptr_option = weechat_config_search_option (
alias_config_file,
alias_config_section_cmd,
ptr_alias_name);
if (ptr_option)
weechat_config_option_free (ptr_option);
ptr_option = weechat_config_search_option (
alias_config_file,
alias_config_section_completion,
ptr_alias_name);
if (ptr_option)
weechat_config_option_free (ptr_option);
weechat_printf (NULL,
_("Alias \"%s\" removed"),
ptr_alias_name);
}
}
return WEECHAT_RC_OK;
}
WEECHAT_COMMAND_ERROR;
}
/*
* Hooks alias command.
*/
void
alias_command_init ()
{
weechat_hook_command (
"alias",
N_("list, add or remove command aliases"),
N_("list [<alias>]"
" || add <alias> [<command>[;<command>...]]"
" || addcompletion <completion> <alias> [<command>[;<command>...]]"
" || del <alias> [<alias>...]"),
/* xgettext:no-c-format */
N_(" list: list aliases (without argument, this list is "
"displayed)\n"
" add: add an alias\n"
"addcompletion: add an alias with a custom completion\n"
" del: delete an alias\n"
" completion: completion for alias: by default completion is "
"done with target command\n"
" note: you can use %%command to use completion of "
"an existing command\n"
" alias: name of alias\n"
" command: command name with arguments (many commands can be "
"separated by semicolons)\n"
"\n"
"Note: in command, special variables are replaced:\n"
" $n: argument 'n' (between 1 and 9)\n"
" $-m: arguments from 1 to 'm'\n"
" $n-: arguments from 'n' to last\n"
" $n-m: arguments from 'n' to 'm'\n"
" $*: all arguments\n"
" $~: last argument\n"
" $var: where \"var\" is a local variable of buffer (see "
"/buffer localvar)\n"
" examples: $nick, $channel, $server, $plugin, $name\n"
"\n"
"Examples:\n"
" alias /split to split window horizontally:\n"
" /alias add split /window splith\n"
" alias /hello to say \"hello\" on all channels but not on "
"#weechat:\n"
" /alias add hello /allchan -exclude=#weechat msg * hello\n"
" alias /forcejoin to send IRC command \"forcejoin\" with "
"completion of /sajoin:\n"
" /alias addcompletion %%sajoin forcejoin /quote forcejoin"),
"list %(alias)"
" || add %(alias) %(commands:/)|%(alias_value)"
" || addcompletion %- %(alias) %(commands:/)|%(alias_value)"
" || del %(alias)|%*",
&alias_command_cb, NULL, NULL);
}
|