diff options
author | Sebastian Thorarensen <sebth@naju.se> | 2014-10-11 18:47:39 +0200 |
---|---|---|
committer | Sebastian Thorarensen <sebth@naju.se> | 2014-10-19 17:03:20 +0200 |
commit | f81a54b937b16ebabc05a01d3053a49b3341ac8c (patch) | |
tree | b89ee9fe7577e08ca40fef26204c862d20425015 /src/core | |
parent | 31ee20e559bea19ed77376c4758e44c457eb9fb4 (diff) | |
download | irssi-f81a54b937b16ebabc05a01d3053a49b3341ac8c.zip |
Try to split long lines on spaces
Try to split long lines on spaces to avoid words being splitted. This
can be turned off with the option `split_line_on_space'. The code
assumes that the terminal encoding has ASCII spaces.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/misc.c | 37 | ||||
-rw-r--r-- | src/core/misc.h | 2 | ||||
-rw-r--r-- | src/core/recode.c | 20 | ||||
-rw-r--r-- | src/core/recode.h | 2 |
4 files changed, 43 insertions, 18 deletions
diff --git a/src/core/misc.c b/src/core/misc.c index 5e6087cb..586e4f7c 100644 --- a/src/core/misc.c +++ b/src/core/misc.c @@ -967,19 +967,30 @@ char *ascii_strdown(char *str) return str; } -char **strsplit_len(const char *str, int len) -{ - char **ret; - size_t total_len = strlen(str); - int n = total_len / len; - int i; - - if (total_len % len) - n++; - - ret = g_new(char *, n + 1); - for (i = 0; i < n; i++, str += len) - ret[i] = g_strndup(str, len); +char **strsplit_len(const char *str, int len, gboolean onspace) +{ + char **ret = g_new(char *, 1); + int n; + int offset; + + for (n = 0; *str != '\0'; n++, str += MIN(len - offset, strlen(str))) { + offset = 0; + if (onspace) { + /* + * Try to find a space to split on and leave + * the space on the previous line. + */ + int i; + for (i = 0; i < len; i++) { + if (str[len-1-i] == ' ') { + offset = i; + break; + } + } + } + ret[n] = g_strndup(str, len - offset); + ret = g_renew(char *, ret, n + 2); + } ret[n] = NULL; return ret; diff --git a/src/core/misc.h b/src/core/misc.h index 8fb5078f..c6369489 100644 --- a/src/core/misc.h +++ b/src/core/misc.h @@ -116,6 +116,6 @@ uoff_t str_to_uofft(const char *str); int find_substr(const char *list, const char *item); /* split `str' into `len' sized substrings */ -char **strsplit_len(const char *str, int len); +char **strsplit_len(const char *str, int len, gboolean onspace); #endif diff --git a/src/core/recode.c b/src/core/recode.c index 029d7ff1..d001a46a 100644 --- a/src/core/recode.c +++ b/src/core/recode.c @@ -183,7 +183,7 @@ char *recode_out(const SERVER_REC *server, const char *str, const char *target) } char **recode_split(const SERVER_REC *server, const char *str, - const char *target, int len) + const char *target, int len, gboolean onspace) { GIConv cd = (GIConv)-1; const char *from = translit_charset; @@ -219,7 +219,7 @@ char **recode_split(const SERVER_REC *server, const char *str, cd = g_iconv_open(to, from); if (cd == (GIConv)-1) { /* Fall back to splitting by byte. */ - ret = strsplit_len(str, len); + ret = strsplit_len(str, len, onspace); goto out; } @@ -235,11 +235,25 @@ char **recode_split(const SERVER_REC *server, const char *str, */ ret[n] = NULL; g_strfreev(ret); - ret = strsplit_len(str, len); + ret = strsplit_len(str, len, onspace); goto out; } /* Outbuf overflowed, split the input string. */ + if (onspace) { + /* + * Try to find a space to split on and leave + * the space on the previous line. + */ + int i; + for (i = 0; i < inbuf - previnbuf; i++) { + if (previnbuf[inbuf-previnbuf-1-i] == ' ') { + inbuf -= i; + inbytesleft += i; + break; + } + } + } ret[n++] = g_strndup(previnbuf, inbuf - previnbuf); ret = g_renew(char *, ret, n + 1); previnbuf = inbuf; diff --git a/src/core/recode.h b/src/core/recode.h index b70ec630..719e8f54 100644 --- a/src/core/recode.h +++ b/src/core/recode.h @@ -4,7 +4,7 @@ char *recode_in (const SERVER_REC *server, const char *str, const char *target); char *recode_out (const SERVER_REC *server, const char *str, const char *target); char **recode_split(const SERVER_REC *server, const char *str, - const char *target, int len); + const char *target, int len, gboolean onspace); gboolean is_valid_charset(const char *charset); gboolean is_utf8(void); void recode_update_charset(void); |