1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-05 01:03:14 +02:00

Add new option weechat.look.command_chars, add functions string_is_command_char and string_input_for_buffer in plugin and script API

This commit is contained in:
Sebastien Helleu
2010-03-02 17:34:49 +01:00
parent 282f786c1a
commit 0543b0ccc7
35 changed files with 935 additions and 235 deletions
+2 -2
View File
@@ -932,7 +932,7 @@ command_command (void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_ERROR;
}
}
if (argv_eol[2][0] == '/')
if (string_is_command_char (argv_eol[2]))
{
input_exec_command (buffer, 0, ptr_plugin, argv_eol[2]);
}
@@ -2291,7 +2291,7 @@ command_mute (void *data, struct t_gui_buffer *buffer,
gui_chat_mute = mute_mode;
gui_chat_mute_buffer = mute_buffer;
if (ptr_command[0] == '/')
if (string_is_command_char (ptr_command))
{
input_exec_command (buffer, 1, NULL, ptr_command);
}
+8
View File
@@ -74,6 +74,7 @@ struct t_config_option *config_look_buffer_notify_default;
struct t_config_option *config_look_buffer_time_format;
struct t_config_option *config_look_color_nicks_number;
struct t_config_option *config_look_color_real_white;
struct t_config_option *config_look_command_chars;
struct t_config_option *config_look_day_change;
struct t_config_option *config_look_day_change_time_format;
struct t_config_option *config_look_highlight;
@@ -1234,6 +1235,13 @@ config_weechat_init_options ()
"see real white instead of default term foreground "
"color)"),
NULL, 0, 0, "off", NULL, 0, NULL, NULL, &config_change_color, NULL, NULL, NULL);
config_look_command_chars = config_file_new_option (
weechat_config_file, ptr_section,
"command_chars", "string",
N_("chars used to determine if input string is a command or not: "
"input must start with one of these chars; the slash (\"/\") is "
"always considered as command prefix (example: \".$\")"),
NULL, 0, 0, "", NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
config_look_day_change = config_file_new_option (
weechat_config_file, ptr_section,
"day_change", "boolean",
+1
View File
@@ -87,6 +87,7 @@ extern struct t_config_option *config_startup_weechat_slogan;
extern struct t_config_option *config_look_buffer_notify_default;
extern struct t_config_option *config_look_buffer_time_format;
extern struct t_config_option *config_look_command_chars;
extern struct t_config_option *config_look_color_nicks_number;
extern struct t_config_option *config_look_color_real_white;
extern struct t_config_option *config_look_day_change;
+5 -3
View File
@@ -572,7 +572,7 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
{
struct t_hook *ptr_hook, *next_hook;
struct t_hook *hook_for_plugin, *hook_for_other_plugin;
char **argv, **argv_eol;
char **argv, **argv_eol, *ptr_command_name;
int argc, rc, number_for_other_plugin;
if (!buffer || !string || !string[0])
@@ -592,6 +592,8 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
}
argv_eol = string_split (string, " ", 1, 0, NULL);
ptr_command_name = utf8_next_char (argv[0]);
hook_exec_start ();
hook_for_plugin = NULL;
@@ -603,8 +605,8 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
next_hook = ptr_hook->next_hook;
if (!ptr_hook->deleted
&& ((argv[0][0] == '/') && (string_strcasecmp (argv[0] + 1,
HOOK_COMMAND(ptr_hook, command)) == 0)))
&& (string_strcasecmp (ptr_command_name,
HOOK_COMMAND(ptr_hook, command)) == 0))
{
if (ptr_hook->plugin == plugin)
{
+30 -33
View File
@@ -31,31 +31,12 @@
#include "wee-config.h"
#include "wee-hook.h"
#include "wee-string.h"
#include "wee-utf8.h"
#include "../gui/gui-buffer.h"
#include "../gui/gui-chat.h"
#include "../plugins/plugin.h"
/*
* input_is_command: return 1 if line is a command, 0 otherwise
*/
int
input_is_command (const char *line)
{
char *pos_slash, *pos_space;
if (strncmp (line, "/*", 2) == 0)
return 0;
pos_slash = strchr (line + 1, '/');
pos_space = strchr (line + 1, ' ');
return (line[0] == '/')
&& (!pos_slash || (pos_space && pos_slash > pos_space));
}
/*
* input_exec_data: send data to buffer input callbackr
*/
@@ -78,11 +59,9 @@ input_exec_data (struct t_gui_buffer *buffer, const char *data)
/*
* input_exec_command: execute a command (WeeChat internal or a plugin command)
* returns: 1 if command was executed succesfully
* 0 if error (command not executed)
*/
int
void
input_exec_command (struct t_gui_buffer *buffer,
int any_plugin,
struct t_weechat_plugin *plugin,
@@ -91,12 +70,12 @@ input_exec_command (struct t_gui_buffer *buffer,
int rc;
char *command, *pos, *ptr_args;
if ((!string) || (!string[0]) || (string[0] != '/'))
return 0;
if ((!string) || (!string[0]))
return;
command = strdup (string);
if (!command)
return 0;
return ;
/* look for end of command */
ptr_args = NULL;
@@ -166,7 +145,6 @@ input_exec_command (struct t_gui_buffer *buffer,
break;
}
free (command);
return 0;
}
/*
@@ -176,8 +154,9 @@ input_exec_command (struct t_gui_buffer *buffer,
void
input_data (struct t_gui_buffer *buffer, const char *data)
{
char *pos;
const char *ptr_data;
char *pos, *buf;
const char *ptr_data, *ptr_data_for_buffer;
int length, char_size;
if (!buffer || !data || !data[0] || (data[0] == '\r') || (data[0] == '\n'))
return;
@@ -190,14 +169,32 @@ input_data (struct t_gui_buffer *buffer, const char *data)
if (pos)
pos[0] = '\0';
if (input_is_command (ptr_data))
ptr_data_for_buffer = string_input_for_buffer (ptr_data);
if (ptr_data_for_buffer)
{
/* WeeChat or plugin command */
(void) input_exec_command (buffer, 1, buffer->plugin, ptr_data);
/* input string is NOT a command, send it to buffer input
callback */
if (string_is_command_char (ptr_data_for_buffer))
{
char_size = utf8_char_size (ptr_data_for_buffer);
length = strlen (ptr_data_for_buffer) + char_size + 1;
buf = malloc (length);
if (buf)
{
memcpy (buf, ptr_data_for_buffer, char_size);
snprintf (buf + char_size, length - char_size,
"%s", ptr_data_for_buffer);
input_exec_data (buffer, buf);
free (buf);
}
}
else
input_exec_data (buffer, ptr_data_for_buffer);
}
else
{
input_exec_data (buffer, ptr_data);
/* input string is a command */
input_exec_command (buffer, 1, buffer->plugin, ptr_data);
}
if (pos)
+4 -4
View File
@@ -23,10 +23,10 @@
struct t_gui_buffer;
struct t_weechat_plugin;
extern int input_exec_command (struct t_gui_buffer *buffer,
int any_plugin,
struct t_weechat_plugin *plugin,
const char *string);
extern void input_exec_command (struct t_gui_buffer *buffer,
int any_plugin,
struct t_weechat_plugin *plugin,
const char *string);
extern void input_data (struct t_gui_buffer *buffer, const char *data);
#endif /* wee-input.h */
+77
View File
@@ -50,6 +50,7 @@
#include "weechat.h"
#include "wee-string.h"
#include "wee-config.h"
#include "wee-utf8.h"
#include "../gui/gui-color.h"
@@ -1410,3 +1411,79 @@ string_decode_base64 (const char *from, char *to)
return to_length;
}
/*
* string_is_command_char: return 1 if first char of string is a command char,
* otherwise 0
*/
int
string_is_command_char (const char *string)
{
const char *ptr_command_chars;
if (!string)
return 0;
if (string[0] == '/')
return 1;
ptr_command_chars = CONFIG_STRING(config_look_command_chars);
if (!ptr_command_chars || !ptr_command_chars[0])
return 0;
while (ptr_command_chars && ptr_command_chars[0])
{
if (utf8_charcmp (ptr_command_chars, string) == 0)
return 1;
ptr_command_chars = utf8_next_char (ptr_command_chars);
}
return 0;
}
/*
* string_input_for_buffer: return pointer to input text for buffer (pointer
* inside "string" argument)
* or return NULL if it's a command
* (by default, a command starts with a single '/')
*/
const char *
string_input_for_buffer (const char *string)
{
char *pos_slash, *pos_space, *next_char;
/* special case for C comments pasted in input line */
if (strncmp (string, "/*", 2) == 0)
return string;
/* special case if string starts with '/': to allow to paste a path line
"/path/to/file.txt", we check if next '/' is after a space or not */
if (string[0] == '/')
{
pos_slash = strchr (string + 1, '/');
pos_space = strchr (string + 1, ' ');
/* if there's no other '/' of if '/' is after first space,
then it is a command, and return NULL */
if (!pos_slash || (pos_space && pos_slash > pos_space))
return NULL;
return (string[1] == '/') ? string + 1 : string;
}
/* if string does not start with a command char, then it's not command */
if (!string_is_command_char (string))
return string;
/* check if first char is doubled: if yes, then it's not a command */
next_char = utf8_next_char (string);
if (!next_char || !next_char[0])
return string;
if (utf8_charcmp (string, next_char) == 0)
return next_char;
/* string is a command */
return NULL;
}
+2
View File
@@ -58,5 +58,7 @@ extern void string_iconv_fprintf (FILE *file, const char *data, ...);
extern char *string_format_size (unsigned long size);
extern void string_encode_base64 (const char *from, int length, char *to);
extern int string_decode_base64 (const char *from, char *to);
extern int string_is_command_char (const char *string);
extern const char *string_input_for_buffer (const char *string);
#endif /* wee-string.h */