Skip to content

Instantly share code, notes, and snippets.

@sim642
Last active July 18, 2020 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sim642/545500a9c76550e7178f6094aa5ea3ae to your computer and use it in GitHub Desktop.
Save sim642/545500a9c76550e7178f6094aa5ea3ae to your computer and use it in GitHub Desktop.
WeeChat WIP patch for rendering \n in messages
diff --git a/src/gui/curses/gui-curses-chat.c b/src/gui/curses/gui-curses-chat.c
index 7fb229e88..c65db3302 100644
--- a/src/gui/curses/gui-curses-chat.c
+++ b/src/gui/curses/gui-curses-chat.c
@@ -1244,8 +1244,10 @@ gui_chat_display_line (struct t_gui_window *window, struct t_gui_line *line,
int read_marker_x, read_marker_y;
int word_start_offset, word_end_offset;
int word_length_with_spaces, word_length;
+ int num_split_lines, i;
char *message_with_tags, *message_with_search;
const char *ptr_data, *ptr_end_offset, *ptr_style, *next_char;
+ char **split_lines;
struct t_gui_line *ptr_prev_line, *ptr_next_line;
struct tm local_time, local_time2;
struct timeval tv_time;
@@ -1375,95 +1377,105 @@ gui_chat_display_line (struct t_gui_window *window, struct t_gui_line *line,
if (ptr_data && ptr_data[0])
{
- while (ptr_data && ptr_data[0])
+ split_lines = string_split (ptr_data, "\n", 0, 0, &num_split_lines);
+ for (i = 0; i < num_split_lines; i++)
{
- gui_chat_get_word_info (window,
- ptr_data,
- &word_start_offset,
- &word_end_offset,
- &word_length_with_spaces, &word_length);
+ ptr_data = split_lines[i];
- ptr_end_offset = ptr_data + word_end_offset;
-
- /* if message ends with spaces, display them */
- if ((word_length <= 0) && (word_length_with_spaces > 0)
- && !ptr_data[word_end_offset + 1])
+ while (ptr_data && ptr_data[0])
{
- word_length = word_length_with_spaces;
- }
+ gui_chat_get_word_info (window,
+ ptr_data,
+ &word_start_offset,
+ &word_end_offset,
+ &word_length_with_spaces, &word_length);
- if (word_length >= 0)
- {
- line_align = gui_line_get_align (window->buffer, line, 1,
- (lines_displayed == 0) ? 1 : 0);
- if ((window->win_chat_cursor_x + word_length_with_spaces > gui_chat_get_real_width (window))
- && (word_length <= gui_chat_get_real_width (window) - line_align))
+ ptr_end_offset = ptr_data + word_end_offset;
+
+ /* if message ends with spaces, display them */
+ if ((word_length <= 0) && (word_length_with_spaces > 0)
+ && !ptr_data[word_end_offset + 1])
{
- /* spaces + word too long for current line but OK for next line */
- gui_chat_display_new_line (window, num_lines, count,
- &lines_displayed, simulate);
- /* apply styles before jumping to start of word */
- if (!simulate && (word_start_offset > 0))
+ word_length = word_length_with_spaces;
+ }
+
+ if (word_length >= 0)
+ {
+ line_align = gui_line_get_align (window->buffer, line, 1,
+ (lines_displayed == 0) ? 1 : 0);
+ if ((window->win_chat_cursor_x + word_length_with_spaces > gui_chat_get_real_width (window))
+ && (word_length <= gui_chat_get_real_width (window) - line_align))
{
- ptr_style = ptr_data;
- while (ptr_style < ptr_data + word_start_offset)
+ /* spaces + word too long for current line but OK for next line */
+ gui_chat_display_new_line (window, num_lines, count,
+ &lines_displayed, simulate);
+ /* apply styles before jumping to start of word */
+ if (!simulate && (word_start_offset > 0))
{
- /* loop until no style/char available */
- ptr_style = gui_chat_string_next_char (window, line,
- (unsigned char *)ptr_style,
- 1,
- CONFIG_BOOLEAN(config_look_color_inactive_message),
- 0);
- if (!ptr_style)
- break;
- ptr_style = utf8_next_char (ptr_style);
+ ptr_style = ptr_data;
+ while (ptr_style < ptr_data + word_start_offset)
+ {
+ /* loop until no style/char available */
+ ptr_style = gui_chat_string_next_char (window, line,
+ (unsigned char *) ptr_style,
+ 1,
+ CONFIG_BOOLEAN(
+ config_look_color_inactive_message),
+ 0);
+ if (!ptr_style)
+ break;
+ ptr_style = utf8_next_char (ptr_style);
+ }
}
+ /* jump to start of word */
+ ptr_data += word_start_offset;
}
- /* jump to start of word */
- ptr_data += word_start_offset;
- }
- /* display word */
- gui_chat_display_word (window, line, ptr_data,
- ptr_end_offset + 1,
- 0, num_lines, count,
- pre_lines_displayed, &lines_displayed,
- simulate,
- CONFIG_BOOLEAN(config_look_color_inactive_message),
- 0);
+ /* display word */
+ gui_chat_display_word (window, line, ptr_data,
+ ptr_end_offset + 1,
+ 0, num_lines, count,
+ pre_lines_displayed, &lines_displayed,
+ simulate,
+ CONFIG_BOOLEAN(config_look_color_inactive_message),
+ 0);
- if ((!simulate) && (window->win_chat_cursor_y >= window->win_chat_height))
- ptr_data = NULL;
- else
- {
- /* move pointer after end of word */
- ptr_data = ptr_end_offset + 1;
- if (*(ptr_data - 1) == '\0')
+ if ((!simulate) && (window->win_chat_cursor_y >= window->win_chat_height))
ptr_data = NULL;
-
- if (window->win_chat_cursor_x == 0)
+ else
{
- while (ptr_data && (ptr_data[0] == ' '))
+ /* move pointer after end of word */
+ ptr_data = ptr_end_offset + 1;
+ if (*(ptr_data - 1) == '\0')
+ ptr_data = NULL;
+
+ if (window->win_chat_cursor_x == 0)
{
- next_char = utf8_next_char (ptr_data);
- if (!next_char)
- break;
- ptr_data = gui_chat_string_next_char (window, line,
- (unsigned char *)next_char,
- 1,
- CONFIG_BOOLEAN(config_look_color_inactive_message),
- 0);
+ while (ptr_data && (ptr_data[0] == ' '))
+ {
+ next_char = utf8_next_char (ptr_data);
+ if (!next_char)
+ break;
+ ptr_data = gui_chat_string_next_char (window, line,
+ (unsigned char *) next_char,
+ 1,
+ CONFIG_BOOLEAN(
+ config_look_color_inactive_message),
+ 0);
+ }
}
}
}
- }
- else
- {
- gui_chat_display_new_line (window, num_lines, count,
- &lines_displayed, simulate);
- ptr_data = NULL;
+ else
+ {
+ gui_chat_display_new_line (window, num_lines, count,
+ &lines_displayed, simulate);
+ ptr_data = NULL;
+ }
}
}
+
+ string_free_split (split_lines);
}
else
{
diff --git a/src/gui/gui-chat.c b/src/gui/gui-chat.c
index a766262a1..cc1ecc9e3 100644
--- a/src/gui/gui-chat.c
+++ b/src/gui/gui-chat.c
@@ -974,9 +974,10 @@ gui_chat_printf_date_tags (struct t_gui_buffer *buffer, time_t date,
while (pos)
{
/* display until next end of line */
- pos_end = strchr (pos, '\n');
+ /*pos_end = strchr (pos, '\n');
if (pos_end)
- pos_end[0] = '\0';
+ pos_end[0] = '\0';*/
+ pos_end = NULL;
if (gui_init_ok)
{
# -*- coding: utf-8 -*-
from __future__ import print_function
SCRIPT_NAME = "newline"
SCRIPT_AUTHOR = "Simmo Saan <simmo.saan@gmail.com>"
SCRIPT_VERSION = "1.0"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = ""
SCRIPT_COMMAND = SCRIPT_NAME
IMPORT_OK = True
try:
import weechat
except ImportError:
print("This script must be run under WeeChat.")
print("Get WeeChat now at: https://weechat.org/")
IMPORT_OK = False
def command_cb(data, buffer, args):
weechat.prnt(buffer, "pref1\tfirst\nsecond")
weechat.prnt(buffer, "pref2\tthird\nfourth")
hdata_buffer = weechat.hdata_get("buffer")
hdata_lines = weechat.hdata_get("lines")
hdata_line = weechat.hdata_get("line")
hdata_line_data = weechat.hdata_get("line_data")
lines = weechat.hdata_pointer(hdata_buffer, buffer, "lines")
last_line = weechat.hdata_pointer(hdata_lines, lines, "last_line")
last_line_data = weechat.hdata_pointer(hdata_line, last_line, "data")
weechat.hdata_update(hdata_line_data, last_line_data, {
"prefix": "pref3",
"message": "fifth\nsixth"
})
return weechat.WEECHAT_RC_OK
if __name__ == "__main__" and IMPORT_OK:
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
weechat.hook_command(SCRIPT_COMMAND, SCRIPT_DESC, "", "", "", "command_cb", "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment