[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | |
---|
[b2b0773] | 5 | /* initialize an fmtext with no data */ |
---|
[5789230] | 6 | void owl_fmtext_init_null(owl_fmtext *f) |
---|
| 7 | { |
---|
[7e111f4] | 8 | f->buff = g_string_new(""); |
---|
[7d4fbcd] | 9 | } |
---|
| 10 | |
---|
[a387d12e] | 11 | /* Clear the data from an fmtext, but don't deallocate memory. This |
---|
| 12 | fmtext can then be appended to again. */ |
---|
| 13 | void owl_fmtext_clear(owl_fmtext *f) |
---|
| 14 | { |
---|
[7e111f4] | 15 | g_string_truncate(f->buff, 0); |
---|
[7d4fbcd] | 16 | } |
---|
| 17 | |
---|
[c1522ec] | 18 | int owl_fmtext_is_format_char(gunichar c) |
---|
[9866c3a] | 19 | { |
---|
| 20 | if ((c & ~OWL_FMTEXT_UC_ATTR_MASK) == OWL_FMTEXT_UC_ATTR) return 1; |
---|
| 21 | if ((c & ~(OWL_FMTEXT_UC_ALLCOLOR_MASK)) == OWL_FMTEXT_UC_COLOR_BASE) return 1; |
---|
| 22 | return 0; |
---|
| 23 | } |
---|
[b2b0773] | 24 | /* append text to the end of 'f' with attribute 'attr' and color |
---|
| 25 | * 'color' |
---|
| 26 | */ |
---|
[e19eb97] | 27 | void owl_fmtext_append_attr(owl_fmtext *f, const char *text, char attr, short fgcolor, short bgcolor) |
---|
[5789230] | 28 | { |
---|
[7e111f4] | 29 | int a = 0, fg = 0, bg = 0; |
---|
[9866c3a] | 30 | |
---|
| 31 | if (attr != OWL_FMTEXT_ATTR_NONE) a=1; |
---|
| 32 | if (fgcolor != OWL_COLOR_DEFAULT) fg=1; |
---|
| 33 | if (bgcolor != OWL_COLOR_DEFAULT) bg=1; |
---|
| 34 | |
---|
| 35 | /* Set attributes */ |
---|
[6c171f1] | 36 | if (a) |
---|
[7e111f4] | 37 | g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | attr); |
---|
[6c171f1] | 38 | if (fg) |
---|
[7e111f4] | 39 | g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGCOLOR | fgcolor); |
---|
[6c171f1] | 40 | if (bg) |
---|
[7e111f4] | 41 | g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGCOLOR | bgcolor); |
---|
| 42 | |
---|
| 43 | g_string_append(f->buff, text); |
---|
[9866c3a] | 44 | |
---|
| 45 | /* Reset attributes */ |
---|
[7659079] | 46 | if (bg) g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGDEFAULT); |
---|
| 47 | if (fg) g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGDEFAULT); |
---|
| 48 | if (a) g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | OWL_FMTEXT_UC_ATTR); |
---|
[7d4fbcd] | 49 | } |
---|
| 50 | |
---|
[b2b0773] | 51 | /* Append normal, uncolored text 'text' to 'f' */ |
---|
[e19eb97] | 52 | void owl_fmtext_append_normal(owl_fmtext *f, const char *text) |
---|
[5789230] | 53 | { |
---|
[8fa9562] | 54 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
[7d4fbcd] | 55 | } |
---|
| 56 | |
---|
[1490327] | 57 | /* Append normal, uncolored text specified by format string to 'f' */ |
---|
[4479497] | 58 | void G_GNUC_PRINTF(2, 3) owl_fmtext_appendf_normal(owl_fmtext *f, const char *fmt, ...) |
---|
[1490327] | 59 | { |
---|
| 60 | va_list ap; |
---|
| 61 | char *buff; |
---|
| 62 | |
---|
| 63 | va_start(ap, fmt); |
---|
| 64 | buff = g_strdup_vprintf(fmt, ap); |
---|
| 65 | va_end(ap); |
---|
| 66 | if (!buff) |
---|
| 67 | return; |
---|
| 68 | owl_fmtext_append_attr(f, buff, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
[ddbbcffa] | 69 | g_free(buff); |
---|
[1490327] | 70 | } |
---|
| 71 | |
---|
[b2b0773] | 72 | /* Append normal text 'text' to 'f' with color 'color' */ |
---|
[e19eb97] | 73 | void owl_fmtext_append_normal_color(owl_fmtext *f, const char *text, int fgcolor, int bgcolor) |
---|
[5789230] | 74 | { |
---|
[8fa9562] | 75 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor); |
---|
[7d4fbcd] | 76 | } |
---|
| 77 | |
---|
[b2b0773] | 78 | /* Append bold text 'text' to 'f' */ |
---|
[e19eb97] | 79 | void owl_fmtext_append_bold(owl_fmtext *f, const char *text) |
---|
[5789230] | 80 | { |
---|
[8fa9562] | 81 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
[7d4fbcd] | 82 | } |
---|
| 83 | |
---|
[b2b0773] | 84 | /* Append reverse video text 'text' to 'f' */ |
---|
[e19eb97] | 85 | void owl_fmtext_append_reverse(owl_fmtext *f, const char *text) |
---|
[5789230] | 86 | { |
---|
[8fa9562] | 87 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
[7d4fbcd] | 88 | } |
---|
| 89 | |
---|
[b2b0773] | 90 | /* Append reversed and bold, uncolored text 'text' to 'f' */ |
---|
[e19eb97] | 91 | void owl_fmtext_append_reversebold(owl_fmtext *f, const char *text) |
---|
[5789230] | 92 | { |
---|
[8fa9562] | 93 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
[7d4fbcd] | 94 | } |
---|
| 95 | |
---|
[9866c3a] | 96 | /* Internal function. Parse attrbute character. */ |
---|
[e9c6fc8] | 97 | static void _owl_fmtext_update_attributes(gunichar c, char *attr, short *fgcolor, short *bgcolor) |
---|
[9866c3a] | 98 | { |
---|
| 99 | if ((c & OWL_FMTEXT_UC_ATTR) == OWL_FMTEXT_UC_ATTR) { |
---|
| 100 | *attr = c & OWL_FMTEXT_UC_ATTR_MASK; |
---|
| 101 | } |
---|
[6f6330b] | 102 | else if ((c & OWL_FMTEXT_UC_COLOR_BASE) == OWL_FMTEXT_UC_COLOR_BASE) { |
---|
| 103 | if ((c & OWL_FMTEXT_UC_BGCOLOR) == OWL_FMTEXT_UC_BGCOLOR) { |
---|
| 104 | *bgcolor = (c == OWL_FMTEXT_UC_BGDEFAULT |
---|
| 105 | ? OWL_COLOR_DEFAULT |
---|
| 106 | : c & OWL_FMTEXT_UC_COLOR_MASK); |
---|
| 107 | } |
---|
| 108 | else if ((c & OWL_FMTEXT_UC_FGCOLOR) == OWL_FMTEXT_UC_FGCOLOR) { |
---|
| 109 | *fgcolor = (c == OWL_FMTEXT_UC_FGDEFAULT |
---|
| 110 | ? OWL_COLOR_DEFAULT |
---|
| 111 | : c & OWL_FMTEXT_UC_COLOR_MASK); |
---|
| 112 | } |
---|
[7d4fbcd] | 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
[9866c3a] | 116 | /* Internal function. Scan for attribute characters. */ |
---|
[e9c6fc8] | 117 | static void _owl_fmtext_scan_attributes(const owl_fmtext *f, int start, char *attr, short *fgcolor, short *bgcolor) |
---|
[9866c3a] | 118 | { |
---|
[e19eb97] | 119 | const char *p; |
---|
[7e111f4] | 120 | p = strchr(f->buff->str, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
| 121 | while (p && p < f->buff->str + start) { |
---|
[9866c3a] | 122 | _owl_fmtext_update_attributes(g_utf8_get_char(p), attr, fgcolor, bgcolor); |
---|
| 123 | p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
[e0e0e5a] | 127 | /* Internal function. Append text from 'in' between index 'start' |
---|
| 128 | * inclusive and 'stop' exclusive, to the end of 'f'. This function |
---|
| 129 | * works with bytes. |
---|
[b2b0773] | 130 | */ |
---|
[e9c6fc8] | 131 | static void _owl_fmtext_append_fmtext(owl_fmtext *f, const owl_fmtext *in, int start, int stop) |
---|
[9866c3a] | 132 | { |
---|
| 133 | char attr = 0; |
---|
| 134 | short fgcolor = OWL_COLOR_DEFAULT; |
---|
| 135 | short bgcolor = OWL_COLOR_DEFAULT; |
---|
| 136 | |
---|
| 137 | _owl_fmtext_scan_attributes(in, start, &attr, &fgcolor, &bgcolor); |
---|
| 138 | |
---|
[cb6c9e1] | 139 | if (attr != OWL_FMTEXT_ATTR_NONE) |
---|
[7e111f4] | 140 | g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | attr); |
---|
[cb6c9e1] | 141 | if (fgcolor != OWL_COLOR_DEFAULT) |
---|
[7e111f4] | 142 | g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGCOLOR | fgcolor); |
---|
[cb6c9e1] | 143 | if (bgcolor != OWL_COLOR_DEFAULT) |
---|
[7e111f4] | 144 | g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGCOLOR | bgcolor); |
---|
[9866c3a] | 145 | |
---|
[7e111f4] | 146 | g_string_append_len(f->buff, in->buff->str+start, stop-start); |
---|
[9866c3a] | 147 | |
---|
| 148 | /* Reset attributes */ |
---|
[7659079] | 149 | g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGDEFAULT); |
---|
| 150 | g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGDEFAULT); |
---|
| 151 | g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | OWL_FMTEXT_UC_ATTR); |
---|
[7d4fbcd] | 152 | } |
---|
| 153 | |
---|
[b2b0773] | 154 | /* append fmtext 'in' to 'f' */ |
---|
[075ba92] | 155 | void owl_fmtext_append_fmtext(owl_fmtext *f, const owl_fmtext *in) |
---|
[5789230] | 156 | { |
---|
[7e111f4] | 157 | _owl_fmtext_append_fmtext(f, in, 0, in->buff->len); |
---|
[5789230] | 158 | |
---|
| 159 | } |
---|
| 160 | |
---|
[b2b0773] | 161 | /* Append 'nspaces' number of spaces to the end of 'f' */ |
---|
[5789230] | 162 | void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces) |
---|
| 163 | { |
---|
[7d4fbcd] | 164 | int i; |
---|
| 165 | for (i=0; i<nspaces; i++) { |
---|
| 166 | owl_fmtext_append_normal(f, " "); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
[b2b0773] | 170 | /* Return a plain version of the fmtext. Caller is responsible for |
---|
| 171 | * freeing the return |
---|
| 172 | */ |
---|
[2560529] | 173 | CALLER_OWN char *owl_fmtext_print_plain(const owl_fmtext *f) |
---|
[5789230] | 174 | { |
---|
[7e111f4] | 175 | return owl_strip_format_chars(f->buff->str); |
---|
[e1c4636] | 176 | } |
---|
| 177 | |
---|
[e9c6fc8] | 178 | static void _owl_fmtext_wattrset(WINDOW *w, int attrs) |
---|
[9866c3a] | 179 | { |
---|
| 180 | wattrset(w, A_NORMAL); |
---|
| 181 | if (attrs & OWL_FMTEXT_ATTR_BOLD) wattron(w, A_BOLD); |
---|
| 182 | if (attrs & OWL_FMTEXT_ATTR_REVERSE) wattron(w, A_REVERSE); |
---|
| 183 | if (attrs & OWL_FMTEXT_ATTR_UNDERLINE) wattron(w, A_UNDERLINE); |
---|
| 184 | } |
---|
[16c6cca] | 185 | |
---|
[e9c6fc8] | 186 | static void _owl_fmtext_wcolor_set(WINDOW *w, short pair) |
---|
[16c6cca] | 187 | { |
---|
[7abfcf2] | 188 | cchar_t background; |
---|
| 189 | wchar_t blank[2] = { ' ', 0 }; |
---|
[7b4f3be] | 190 | if (has_colors()) { |
---|
[7abfcf2] | 191 | wcolor_set(w, pair, NULL); |
---|
| 192 | /* Set the background with wbkgrndset so that we can handle color-pairs |
---|
| 193 | * past 256 on ncurses ABI 6 and later. */ |
---|
| 194 | setcchar(&background, blank, 0, pair, NULL); |
---|
| 195 | wbkgrndset(w, &background); |
---|
[16c6cca] | 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
[b2b0773] | 199 | /* add the formatted text to the curses window 'w'. The window 'w' |
---|
| 200 | * must already be initiatlized with curses |
---|
| 201 | */ |
---|
[237d02c] | 202 | static void _owl_fmtext_curs_waddstr(const owl_fmtext *f, WINDOW *w, int do_search, char default_attrs, short default_fgcolor, short default_bgcolor) |
---|
[9866c3a] | 203 | { |
---|
| 204 | /* char *tmpbuff; */ |
---|
| 205 | /* int position, trans1, trans2, trans3, len, lastsame; */ |
---|
| 206 | char *s, *p; |
---|
| 207 | char attr; |
---|
[18fb86ee] | 208 | short fg, bg, pair = 0; |
---|
[9866c3a] | 209 | |
---|
[f2f9314] | 210 | if (w==NULL) { |
---|
| 211 | owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr."); |
---|
| 212 | return; |
---|
| 213 | } |
---|
| 214 | |
---|
[7e111f4] | 215 | s = f->buff->str; |
---|
[9866c3a] | 216 | /* Set default attributes. */ |
---|
[237d02c] | 217 | attr = default_attrs; |
---|
| 218 | fg = default_fgcolor; |
---|
| 219 | bg = default_bgcolor; |
---|
[9866c3a] | 220 | _owl_fmtext_wattrset(w, attr); |
---|
[7b4f3be] | 221 | pair = owl_fmtext_get_colorpair(fg, bg); |
---|
[16c6cca] | 222 | _owl_fmtext_wcolor_set(w, pair); |
---|
[7d4fbcd] | 223 | |
---|
[9866c3a] | 224 | /* Find next possible format character. */ |
---|
| 225 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
| 226 | while(p) { |
---|
[c1522ec] | 227 | if (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
[9866c3a] | 228 | /* Deal with all text from last insert to here. */ |
---|
| 229 | char tmp; |
---|
| 230 | |
---|
| 231 | tmp = p[0]; |
---|
| 232 | p[0] = '\0'; |
---|
[449c682] | 233 | if (do_search && owl_global_is_search_active(&g)) { |
---|
[9866c3a] | 234 | /* Search is active, so highlight search results. */ |
---|
[41c9a96] | 235 | int start, end; |
---|
| 236 | while (owl_regex_compare(owl_global_get_search_re(&g), s, &start, &end) == 0) { |
---|
[0ed5c57] | 237 | /* Prevent an infinite loop matching the empty string. */ |
---|
| 238 | if (end == 0) |
---|
| 239 | break; |
---|
| 240 | |
---|
[9866c3a] | 241 | /* Found search string, highlight it. */ |
---|
| 242 | |
---|
[d3c318b] | 243 | waddnstr(w, s, start); |
---|
[9866c3a] | 244 | |
---|
[987a93c] | 245 | _owl_fmtext_wattrset(w, attr ^ OWL_FMTEXT_ATTR_REVERSE); |
---|
[16c6cca] | 246 | _owl_fmtext_wcolor_set(w, pair); |
---|
| 247 | |
---|
[d3c318b] | 248 | waddnstr(w, s + start, end - start); |
---|
[9866c3a] | 249 | |
---|
[16c6cca] | 250 | _owl_fmtext_wattrset(w, attr); |
---|
| 251 | _owl_fmtext_wcolor_set(w, pair); |
---|
[9866c3a] | 252 | |
---|
[41c9a96] | 253 | s += end; |
---|
[9866c3a] | 254 | } |
---|
| 255 | } |
---|
| 256 | /* Deal with remaining part of string. */ |
---|
| 257 | waddstr(w, s); |
---|
| 258 | p[0] = tmp; |
---|
| 259 | |
---|
[cb6c9e1] | 260 | /* Deal with new attributes. Process all consecutive formatting |
---|
| 261 | * characters, and then apply defaults where relevant. */ |
---|
[f119757] | 262 | while (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
[9866c3a] | 263 | _owl_fmtext_update_attributes(g_utf8_get_char(p), &attr, &fg, &bg); |
---|
| 264 | p = g_utf8_next_char(p); |
---|
[7d4fbcd] | 265 | } |
---|
[cb6c9e1] | 266 | attr |= default_attrs; |
---|
[237d02c] | 267 | if (fg == OWL_COLOR_DEFAULT) fg = default_fgcolor; |
---|
| 268 | if (bg == OWL_COLOR_DEFAULT) bg = default_bgcolor; |
---|
[cb6c9e1] | 269 | _owl_fmtext_wattrset(w, attr); |
---|
[7b4f3be] | 270 | pair = owl_fmtext_get_colorpair(fg, bg); |
---|
[16c6cca] | 271 | _owl_fmtext_wcolor_set(w, pair); |
---|
| 272 | |
---|
[9866c3a] | 273 | /* Advance to next non-formatting character. */ |
---|
| 274 | s = p; |
---|
| 275 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
| 276 | } |
---|
| 277 | else { |
---|
| 278 | p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
[7d4fbcd] | 279 | } |
---|
| 280 | } |
---|
[9866c3a] | 281 | if (s) { |
---|
| 282 | waddstr(w, s); |
---|
| 283 | } |
---|
[39cff48] | 284 | wbkgdset(w, 0); |
---|
[7d4fbcd] | 285 | } |
---|
| 286 | |
---|
[237d02c] | 287 | void owl_fmtext_curs_waddstr(const owl_fmtext *f, WINDOW *w, char default_attrs, short default_fgcolor, short default_bgcolor) |
---|
[47519e1b] | 288 | { |
---|
[237d02c] | 289 | _owl_fmtext_curs_waddstr(f, w, 1, default_attrs, default_fgcolor, default_bgcolor); |
---|
[47519e1b] | 290 | } |
---|
| 291 | |
---|
[237d02c] | 292 | void owl_fmtext_curs_waddstr_without_search(const owl_fmtext *f, WINDOW *w, char default_attrs, short default_fgcolor, short default_bgcolor) |
---|
[47519e1b] | 293 | { |
---|
[237d02c] | 294 | _owl_fmtext_curs_waddstr(f, w, 0, default_attrs, default_fgcolor, default_bgcolor); |
---|
[47519e1b] | 295 | } |
---|
[7d4fbcd] | 296 | |
---|
[2b83ad6] | 297 | /* Expands tabs. Tabs are expanded as if given an initial indent of start. */ |
---|
| 298 | void owl_fmtext_expand_tabs(const owl_fmtext *in, owl_fmtext *out, int start) { |
---|
| 299 | int col = start, numcopied = 0; |
---|
| 300 | char *ptr; |
---|
| 301 | |
---|
[7e111f4] | 302 | for (ptr = in->buff->str; |
---|
| 303 | ptr < in->buff->str + in->buff->len; |
---|
[2b83ad6] | 304 | ptr = g_utf8_next_char(ptr)) { |
---|
| 305 | gunichar c = g_utf8_get_char(ptr); |
---|
| 306 | int chwidth; |
---|
| 307 | if (c == '\t') { |
---|
| 308 | /* Copy up to this tab */ |
---|
[7e111f4] | 309 | _owl_fmtext_append_fmtext(out, in, numcopied, ptr - in->buff->str); |
---|
[2b83ad6] | 310 | /* and then copy spaces for the tab. */ |
---|
| 311 | chwidth = OWL_TAB_WIDTH - (col % OWL_TAB_WIDTH); |
---|
| 312 | owl_fmtext_append_spaces(out, chwidth); |
---|
| 313 | col += chwidth; |
---|
[7e111f4] | 314 | numcopied = g_utf8_next_char(ptr) - in->buff->str; |
---|
[2b83ad6] | 315 | } else { |
---|
| 316 | /* Just update col. We'll append later. */ |
---|
| 317 | if (c == '\n') { |
---|
| 318 | col = start; |
---|
| 319 | } else if (!owl_fmtext_is_format_char(c)) { |
---|
| 320 | col += mk_wcwidth(c); |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | /* Append anything we've missed. */ |
---|
[7e111f4] | 325 | if (numcopied < in->buff->len) |
---|
| 326 | _owl_fmtext_append_fmtext(out, in, numcopied, in->buff->len); |
---|
[2b83ad6] | 327 | } |
---|
| 328 | |
---|
[b2b0773] | 329 | /* start with line 'aline' (where the first line is 0) and print |
---|
| 330 | * 'lines' number of lines into 'out' |
---|
| 331 | */ |
---|
[075ba92] | 332 | int owl_fmtext_truncate_lines(const owl_fmtext *in, int aline, int lines, owl_fmtext *out) |
---|
[5789230] | 333 | { |
---|
[e19eb97] | 334 | const char *ptr1, *ptr2; |
---|
[7d4fbcd] | 335 | int i, offset; |
---|
| 336 | |
---|
| 337 | /* find the starting line */ |
---|
[7e111f4] | 338 | ptr1 = in->buff->str; |
---|
[9866c3a] | 339 | for (i = 0; i < aline; i++) { |
---|
| 340 | ptr1 = strchr(ptr1, '\n'); |
---|
| 341 | if (!ptr1) return(-1); |
---|
| 342 | ptr1++; |
---|
[7d4fbcd] | 343 | } |
---|
[9866c3a] | 344 | |
---|
[7d4fbcd] | 345 | /* ptr1 now holds the starting point */ |
---|
| 346 | |
---|
| 347 | /* copy in the next 'lines' lines */ |
---|
[9866c3a] | 348 | if (lines < 1) return(-1); |
---|
[7d4fbcd] | 349 | |
---|
[9866c3a] | 350 | for (i = 0; i < lines; i++) { |
---|
[7e111f4] | 351 | offset = ptr1 - in->buff->str; |
---|
[9866c3a] | 352 | ptr2 = strchr(ptr1, '\n'); |
---|
[7d4fbcd] | 353 | if (!ptr2) { |
---|
[e0e0e5a] | 354 | /* Copy to the end of the buffer. */ |
---|
[7e111f4] | 355 | _owl_fmtext_append_fmtext(out, in, offset, in->buff->len); |
---|
[7d4fbcd] | 356 | return(-1); |
---|
| 357 | } |
---|
[e0e0e5a] | 358 | /* Copy up to, and including, the new line. */ |
---|
| 359 | _owl_fmtext_append_fmtext(out, in, offset, (ptr2 - ptr1) + offset + 1); |
---|
[9866c3a] | 360 | ptr1 = ptr2 + 1; |
---|
[7d4fbcd] | 361 | } |
---|
| 362 | return(0); |
---|
| 363 | } |
---|
| 364 | |
---|
[e0022d2] | 365 | /* Implementation of owl_fmtext_truncate_cols. Does not support tabs in input. */ |
---|
| 366 | void _owl_fmtext_truncate_cols_internal(const owl_fmtext *in, int acol, int bcol, owl_fmtext *out) |
---|
[5789230] | 367 | { |
---|
[e19eb97] | 368 | const char *ptr_s, *ptr_e, *ptr_c, *last; |
---|
[9866c3a] | 369 | int col, st, padding, chwidth; |
---|
[7d4fbcd] | 370 | |
---|
[7e111f4] | 371 | last = in->buff->str + in->buff->len - 1; |
---|
| 372 | ptr_s = in->buff->str; |
---|
[9866c3a] | 373 | while (ptr_s <= last) { |
---|
[28ee32b] | 374 | ptr_e=strchr(ptr_s, '\n'); |
---|
| 375 | if (!ptr_e) { |
---|
[dd6af02] | 376 | /* but this shouldn't happen if we end in a \n */ |
---|
| 377 | break; |
---|
[7d4fbcd] | 378 | } |
---|
| 379 | |
---|
[9866c3a] | 380 | if (ptr_e == ptr_s) { |
---|
[7d4fbcd] | 381 | owl_fmtext_append_normal(out, "\n"); |
---|
[9866c3a] | 382 | ++ptr_s; |
---|
[7d4fbcd] | 383 | continue; |
---|
| 384 | } |
---|
| 385 | |
---|
[28ee32b] | 386 | col = 0; |
---|
[9866c3a] | 387 | st = 0; |
---|
[dd24b6a] | 388 | padding = 0; |
---|
[47519e1b] | 389 | chwidth = 0; |
---|
[28ee32b] | 390 | ptr_c = ptr_s; |
---|
[3a7cf49] | 391 | while(ptr_c < ptr_e) { |
---|
[28ee32b] | 392 | gunichar c = g_utf8_get_char(ptr_c); |
---|
[c1522ec] | 393 | if (!owl_fmtext_is_format_char(c)) { |
---|
[47519e1b] | 394 | chwidth = mk_wcwidth(c); |
---|
[3a7cf49] | 395 | if (col + chwidth > bcol) break; |
---|
| 396 | |
---|
[9866c3a] | 397 | if (col >= acol) { |
---|
| 398 | if (st == 0) { |
---|
| 399 | ptr_s = ptr_c; |
---|
| 400 | padding = col - acol; |
---|
| 401 | ++st; |
---|
| 402 | } |
---|
[dd24b6a] | 403 | } |
---|
[9866c3a] | 404 | col += chwidth; |
---|
| 405 | chwidth = 0; |
---|
[28ee32b] | 406 | } |
---|
[9866c3a] | 407 | ptr_c = g_utf8_next_char(ptr_c); |
---|
[dd24b6a] | 408 | } |
---|
[9866c3a] | 409 | if (st) { |
---|
| 410 | /* lead padding */ |
---|
| 411 | owl_fmtext_append_spaces(out, padding); |
---|
[3a7cf49] | 412 | if (ptr_c == ptr_e) { |
---|
[e0e0e5a] | 413 | /* We made it to the newline. Append up to, and including it. */ |
---|
[7e111f4] | 414 | _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str + 1); |
---|
[9866c3a] | 415 | } |
---|
[db1af5f] | 416 | else if (chwidth > 1) { |
---|
| 417 | /* Last char is wide, truncate. */ |
---|
[7e111f4] | 418 | _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str); |
---|
[db1af5f] | 419 | owl_fmtext_append_normal(out, "\n"); |
---|
| 420 | } |
---|
[9866c3a] | 421 | else { |
---|
[e0e0e5a] | 422 | /* Last char fits perfectly, We stop at the next char to make |
---|
| 423 | * sure we get it all. */ |
---|
[db1af5f] | 424 | ptr_c = g_utf8_next_char(ptr_c); |
---|
[7e111f4] | 425 | _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str); |
---|
[28ee32b] | 426 | } |
---|
[dd24b6a] | 427 | } |
---|
| 428 | else { |
---|
| 429 | owl_fmtext_append_normal(out, "\n"); |
---|
[28ee32b] | 430 | } |
---|
[dd6af02] | 431 | ptr_s = g_utf8_next_char(ptr_e); |
---|
[7d4fbcd] | 432 | } |
---|
| 433 | } |
---|
| 434 | |
---|
[e0022d2] | 435 | /* Truncate the message so that each line begins at column 'acol' and |
---|
| 436 | * ends at 'bcol' or sooner. The first column is number 0. The new |
---|
| 437 | * message is placed in 'out'. The message is expected to end in a |
---|
| 438 | * new line for now. |
---|
| 439 | * |
---|
| 440 | * NOTE: This needs to be modified to deal with backing up if we find |
---|
| 441 | * a SPACING COMBINING MARK at the end of a line. If that happens, we |
---|
| 442 | * should back up to the last non-mark character and stop there. |
---|
| 443 | * |
---|
| 444 | * NOTE: If a line ends at bcol, we omit the newline. This is so printing |
---|
| 445 | * to ncurses works. |
---|
| 446 | */ |
---|
| 447 | void owl_fmtext_truncate_cols(const owl_fmtext *in, int acol, int bcol, owl_fmtext *out) |
---|
| 448 | { |
---|
| 449 | owl_fmtext notabs; |
---|
| 450 | |
---|
| 451 | /* _owl_fmtext_truncate_cols_internal cannot handle tabs. */ |
---|
[7e111f4] | 452 | if (strchr(in->buff->str, '\t')) { |
---|
[e0022d2] | 453 | owl_fmtext_init_null(¬abs); |
---|
| 454 | owl_fmtext_expand_tabs(in, ¬abs, 0); |
---|
| 455 | _owl_fmtext_truncate_cols_internal(¬abs, acol, bcol, out); |
---|
| 456 | owl_fmtext_cleanup(¬abs); |
---|
| 457 | } else { |
---|
| 458 | _owl_fmtext_truncate_cols_internal(in, acol, bcol, out); |
---|
| 459 | } |
---|
| 460 | } |
---|
| 461 | |
---|
[b2b0773] | 462 | /* Return the number of lines in 'f' */ |
---|
[075ba92] | 463 | int owl_fmtext_num_lines(const owl_fmtext *f) |
---|
[5789230] | 464 | { |
---|
[7d4fbcd] | 465 | int lines, i; |
---|
[c93b8b5] | 466 | char *lastbreak, *p; |
---|
[7d4fbcd] | 467 | |
---|
| 468 | lines=0; |
---|
[7e111f4] | 469 | lastbreak = f->buff->str; |
---|
| 470 | for (i = 0; i < f->buff->len; i++) { |
---|
| 471 | if (f->buff->str[i]=='\n') { |
---|
| 472 | lastbreak = f->buff->str + i; |
---|
[c93b8b5] | 473 | lines++; |
---|
| 474 | } |
---|
[7d4fbcd] | 475 | } |
---|
| 476 | |
---|
[c93b8b5] | 477 | /* Check if there's a trailing line; formatting characters don't count. */ |
---|
| 478 | for (p = g_utf8_next_char(lastbreak); |
---|
[7e111f4] | 479 | p < f->buff->str + f->buff->len; |
---|
[c93b8b5] | 480 | p = g_utf8_next_char(p)) { |
---|
| 481 | if (!owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
| 482 | lines++; |
---|
| 483 | break; |
---|
| 484 | } |
---|
| 485 | } |
---|
[7d4fbcd] | 486 | |
---|
| 487 | return(lines); |
---|
| 488 | } |
---|
| 489 | |
---|
[f7456bc] | 490 | /* Returns the line number, starting at 0, of the character which |
---|
| 491 | * contains the byte at 'offset'. Note that a trailing newline is part |
---|
| 492 | * of the line it ends. Also, while a trailing line of formatting |
---|
| 493 | * characters does not contribute to owl_fmtext_num_lines, those |
---|
| 494 | * characters are considered on a new line. */ |
---|
| 495 | int owl_fmtext_line_number(const owl_fmtext *f, int offset) |
---|
| 496 | { |
---|
| 497 | int i, lineno = 0; |
---|
[7e111f4] | 498 | if (offset >= f->buff->len) |
---|
| 499 | offset = f->buff->len - 1; |
---|
[f7456bc] | 500 | for (i = 0; i < offset; i++) { |
---|
[7e111f4] | 501 | if (f->buff->str[i] == '\n') |
---|
[f7456bc] | 502 | lineno++; |
---|
| 503 | } |
---|
| 504 | return lineno; |
---|
| 505 | } |
---|
| 506 | |
---|
[7ba2ad4] | 507 | /* Searches for line 'lineno' in 'f'. The returned range, [start, |
---|
| 508 | * end), forms a half-open interval for the extent of the line. */ |
---|
| 509 | void owl_fmtext_line_extents(const owl_fmtext *f, int lineno, int *o_start, int *o_end) |
---|
| 510 | { |
---|
| 511 | int start, end; |
---|
| 512 | char *newline; |
---|
[7e111f4] | 513 | for (start = 0; lineno > 0 && start < f->buff->len; start++) { |
---|
| 514 | if (f->buff->str[start] == '\n') |
---|
[7ba2ad4] | 515 | lineno--; |
---|
| 516 | } |
---|
[7e111f4] | 517 | newline = strchr(f->buff->str + start, '\n'); |
---|
[7ba2ad4] | 518 | /* Include the newline, if it is there. */ |
---|
[7e111f4] | 519 | end = newline ? newline - f->buff->str + 1 : f->buff->len; |
---|
[7ba2ad4] | 520 | if (o_start) *o_start = start; |
---|
| 521 | if (o_end) *o_end = end; |
---|
| 522 | } |
---|
| 523 | |
---|
[075ba92] | 524 | const char *owl_fmtext_get_text(const owl_fmtext *f) |
---|
[5789230] | 525 | { |
---|
[7e111f4] | 526 | return f->buff->str; |
---|
[7d4fbcd] | 527 | } |
---|
| 528 | |
---|
[6772d19] | 529 | int owl_fmtext_num_bytes(const owl_fmtext *f) |
---|
| 530 | { |
---|
[7e111f4] | 531 | return f->buff->len; |
---|
[6772d19] | 532 | } |
---|
| 533 | |
---|
[b2b0773] | 534 | /* Make a copy of the fmtext 'src' into 'dst' */ |
---|
[075ba92] | 535 | void owl_fmtext_copy(owl_fmtext *dst, const owl_fmtext *src) |
---|
[5789230] | 536 | { |
---|
[7e111f4] | 537 | dst->buff = g_string_new(src->buff->str); |
---|
[1fd0b25] | 538 | } |
---|
| 539 | |
---|
[72f613a] | 540 | /* Search 'f' for the regex 're' for matches starting at |
---|
| 541 | * 'start'. Returns the offset of the first match, -1 if not |
---|
| 542 | * found. This is a case-insensitive search. |
---|
[5789230] | 543 | */ |
---|
[72f613a] | 544 | int owl_fmtext_search(const owl_fmtext *f, const owl_regex *re, int start) |
---|
[5789230] | 545 | { |
---|
[72f613a] | 546 | int offset; |
---|
[7e111f4] | 547 | if (start > f->buff->len || |
---|
| 548 | owl_regex_compare(re, f->buff->str + start, &offset, NULL) != 0) |
---|
[72f613a] | 549 | return -1; |
---|
| 550 | return offset + start; |
---|
[1fd0b25] | 551 | } |
---|
[12c35df] | 552 | |
---|
| 553 | |
---|
| 554 | /* Append the text 'text' to 'f' and interpret the zephyr style |
---|
| 555 | * formatting syntax to set appropriate attributes. |
---|
| 556 | */ |
---|
[e19eb97] | 557 | void owl_fmtext_append_ztext(owl_fmtext *f, const char *text) |
---|
[12c35df] | 558 | { |
---|
| 559 | int stacksize, curattrs, curcolor; |
---|
[e19eb97] | 560 | const char *ptr, *txtptr, *tmpptr; |
---|
[65b2173] | 561 | char *buff; |
---|
[d754b0a] | 562 | int attrstack[32], chrstack[32], colorstack[32]; |
---|
[12c35df] | 563 | |
---|
| 564 | curattrs=OWL_FMTEXT_ATTR_NONE; |
---|
| 565 | curcolor=OWL_COLOR_DEFAULT; |
---|
| 566 | stacksize=0; |
---|
| 567 | txtptr=text; |
---|
| 568 | while (1) { |
---|
| 569 | ptr=strpbrk(txtptr, "@{[<()>]}"); |
---|
| 570 | if (!ptr) { |
---|
| 571 | /* add all the rest of the text and exit */ |
---|
[8fa9562] | 572 | owl_fmtext_append_attr(f, txtptr, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[12c35df] | 573 | return; |
---|
| 574 | } else if (ptr[0]=='@') { |
---|
| 575 | /* add the text up to this point then deal with the stack */ |
---|
[96828e4] | 576 | buff=g_new(char, ptr-txtptr+20); |
---|
[12c35df] | 577 | strncpy(buff, txtptr, ptr-txtptr); |
---|
| 578 | buff[ptr-txtptr]='\0'; |
---|
[8fa9562] | 579 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[ddbbcffa] | 580 | g_free(buff); |
---|
[12c35df] | 581 | |
---|
| 582 | /* update pointer to point at the @ */ |
---|
| 583 | txtptr=ptr; |
---|
| 584 | |
---|
| 585 | /* now the stack */ |
---|
| 586 | |
---|
| 587 | /* if we've hit our max stack depth, print the @ and move on */ |
---|
| 588 | if (stacksize==32) { |
---|
[1bdffcb] | 589 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 590 | txtptr++; |
---|
| 591 | continue; |
---|
[12c35df] | 592 | } |
---|
| 593 | |
---|
| 594 | /* if it's an @@, print an @ and continue */ |
---|
| 595 | if (txtptr[1]=='@') { |
---|
[1bdffcb] | 596 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 597 | txtptr+=2; |
---|
| 598 | continue; |
---|
[12c35df] | 599 | } |
---|
[1bdffcb] | 600 | |
---|
[12c35df] | 601 | /* if there's no opener, print the @ and continue */ |
---|
| 602 | tmpptr=strpbrk(txtptr, "(<[{ "); |
---|
| 603 | if (!tmpptr || tmpptr[0]==' ') { |
---|
[1bdffcb] | 604 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 605 | txtptr++; |
---|
| 606 | continue; |
---|
[12c35df] | 607 | } |
---|
| 608 | |
---|
| 609 | /* check what command we've got, push it on the stack, start |
---|
[1bdffcb] | 610 | using it, and continue ... unless it's a color command */ |
---|
[96828e4] | 611 | buff=g_new(char, tmpptr-ptr+20); |
---|
[12c35df] | 612 | strncpy(buff, ptr, tmpptr-ptr); |
---|
| 613 | buff[tmpptr-ptr]='\0'; |
---|
| 614 | if (!strcasecmp(buff, "@bold")) { |
---|
[1bdffcb] | 615 | attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD; |
---|
| 616 | chrstack[stacksize]=tmpptr[0]; |
---|
[d754b0a] | 617 | colorstack[stacksize]=curcolor; |
---|
[1bdffcb] | 618 | stacksize++; |
---|
| 619 | curattrs|=OWL_FMTEXT_ATTR_BOLD; |
---|
| 620 | txtptr+=6; |
---|
[ddbbcffa] | 621 | g_free(buff); |
---|
[1bdffcb] | 622 | continue; |
---|
[12c35df] | 623 | } else if (!strcasecmp(buff, "@b")) { |
---|
[1bdffcb] | 624 | attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD; |
---|
| 625 | chrstack[stacksize]=tmpptr[0]; |
---|
[d754b0a] | 626 | colorstack[stacksize]=curcolor; |
---|
[1bdffcb] | 627 | stacksize++; |
---|
| 628 | curattrs|=OWL_FMTEXT_ATTR_BOLD; |
---|
| 629 | txtptr+=3; |
---|
[ddbbcffa] | 630 | g_free(buff); |
---|
[1bdffcb] | 631 | continue; |
---|
[12c35df] | 632 | } else if (!strcasecmp(buff, "@i")) { |
---|
[1bdffcb] | 633 | attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
| 634 | chrstack[stacksize]=tmpptr[0]; |
---|
[d754b0a] | 635 | colorstack[stacksize]=curcolor; |
---|
[1bdffcb] | 636 | stacksize++; |
---|
| 637 | curattrs|=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
| 638 | txtptr+=3; |
---|
[ddbbcffa] | 639 | g_free(buff); |
---|
[1bdffcb] | 640 | continue; |
---|
[12c35df] | 641 | } else if (!strcasecmp(buff, "@italic")) { |
---|
[1bdffcb] | 642 | attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
| 643 | chrstack[stacksize]=tmpptr[0]; |
---|
[d754b0a] | 644 | colorstack[stacksize]=curcolor; |
---|
[1bdffcb] | 645 | stacksize++; |
---|
| 646 | curattrs|=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
| 647 | txtptr+=8; |
---|
[ddbbcffa] | 648 | g_free(buff); |
---|
[1bdffcb] | 649 | continue; |
---|
[d754b0a] | 650 | } else if (!strcasecmp(buff, "@")) { |
---|
| 651 | attrstack[stacksize]=OWL_FMTEXT_ATTR_NONE; |
---|
| 652 | chrstack[stacksize]=tmpptr[0]; |
---|
| 653 | colorstack[stacksize]=curcolor; |
---|
| 654 | stacksize++; |
---|
| 655 | txtptr+=2; |
---|
[ddbbcffa] | 656 | g_free(buff); |
---|
[d754b0a] | 657 | continue; |
---|
[1bdffcb] | 658 | |
---|
| 659 | /* if it's a color read the color, set the current color and |
---|
[12c35df] | 660 | continue */ |
---|
| 661 | } else if (!strcasecmp(buff, "@color") |
---|
[1bdffcb] | 662 | && owl_global_is_colorztext(&g)) { |
---|
[ddbbcffa] | 663 | g_free(buff); |
---|
[1bdffcb] | 664 | txtptr+=7; |
---|
| 665 | tmpptr=strpbrk(txtptr, "@{[<()>]}"); |
---|
| 666 | if (tmpptr && |
---|
| 667 | ((txtptr[-1]=='(' && tmpptr[0]==')') || |
---|
| 668 | (txtptr[-1]=='<' && tmpptr[0]=='>') || |
---|
| 669 | (txtptr[-1]=='[' && tmpptr[0]==']') || |
---|
| 670 | (txtptr[-1]=='{' && tmpptr[0]=='}'))) { |
---|
| 671 | |
---|
| 672 | /* grab the color name */ |
---|
[96828e4] | 673 | buff=g_new(char, tmpptr-txtptr+20); |
---|
[1bdffcb] | 674 | strncpy(buff, txtptr, tmpptr-txtptr); |
---|
| 675 | buff[tmpptr-txtptr]='\0'; |
---|
| 676 | |
---|
| 677 | /* set it as the current color */ |
---|
| 678 | curcolor=owl_util_string_to_color(buff); |
---|
[1ee5c79] | 679 | if (curcolor == OWL_COLOR_INVALID) |
---|
| 680 | curcolor = OWL_COLOR_DEFAULT; |
---|
[ddbbcffa] | 681 | g_free(buff); |
---|
[1bdffcb] | 682 | txtptr=tmpptr+1; |
---|
| 683 | continue; |
---|
| 684 | |
---|
| 685 | } else { |
---|
| 686 | |
---|
| 687 | } |
---|
[12c35df] | 688 | |
---|
| 689 | } else { |
---|
[1bdffcb] | 690 | /* if we didn't understand it, we'll print it. This is different from zwgc |
---|
| 691 | * but zwgc seems to be smarter about some screw cases than I am |
---|
| 692 | */ |
---|
[d8f22b6] | 693 | g_free(buff); |
---|
[1bdffcb] | 694 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 695 | txtptr++; |
---|
| 696 | continue; |
---|
[12c35df] | 697 | } |
---|
| 698 | |
---|
| 699 | } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') { |
---|
| 700 | /* add the text up to this point first */ |
---|
[96828e4] | 701 | buff=g_new(char, ptr-txtptr+20); |
---|
[12c35df] | 702 | strncpy(buff, txtptr, ptr-txtptr); |
---|
| 703 | buff[ptr-txtptr]='\0'; |
---|
[8fa9562] | 704 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[ddbbcffa] | 705 | g_free(buff); |
---|
[12c35df] | 706 | |
---|
| 707 | /* now deal with the closer */ |
---|
| 708 | txtptr=ptr; |
---|
| 709 | |
---|
| 710 | /* first, if the stack is empty we must bail (just print and go) */ |
---|
| 711 | if (stacksize==0) { |
---|
[96828e4] | 712 | buff=g_new(char, 5); |
---|
[1bdffcb] | 713 | buff[0]=ptr[0]; |
---|
| 714 | buff[1]='\0'; |
---|
| 715 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[ddbbcffa] | 716 | g_free(buff); |
---|
[1bdffcb] | 717 | txtptr++; |
---|
| 718 | continue; |
---|
[12c35df] | 719 | } |
---|
| 720 | |
---|
| 721 | /* if the closing char is what's on the stack, turn off the |
---|
| 722 | attribue and pop the stack */ |
---|
| 723 | if ((ptr[0]==')' && chrstack[stacksize-1]=='(') || |
---|
[1bdffcb] | 724 | (ptr[0]=='>' && chrstack[stacksize-1]=='<') || |
---|
| 725 | (ptr[0]==']' && chrstack[stacksize-1]=='[') || |
---|
| 726 | (ptr[0]=='}' && chrstack[stacksize-1]=='{')) { |
---|
| 727 | int i; |
---|
| 728 | stacksize--; |
---|
| 729 | curattrs=OWL_FMTEXT_ATTR_NONE; |
---|
[d754b0a] | 730 | curcolor = colorstack[stacksize]; |
---|
[1bdffcb] | 731 | for (i=0; i<stacksize; i++) { |
---|
| 732 | curattrs|=attrstack[i]; |
---|
| 733 | } |
---|
| 734 | txtptr+=1; |
---|
| 735 | continue; |
---|
[12c35df] | 736 | } else { |
---|
[1bdffcb] | 737 | /* otherwise print and continue */ |
---|
[96828e4] | 738 | buff=g_new(char, 5); |
---|
[1bdffcb] | 739 | buff[0]=ptr[0]; |
---|
| 740 | buff[1]='\0'; |
---|
| 741 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[ddbbcffa] | 742 | g_free(buff); |
---|
[1bdffcb] | 743 | txtptr++; |
---|
| 744 | continue; |
---|
[12c35df] | 745 | } |
---|
| 746 | } else { |
---|
| 747 | /* we've found an unattached opener, print everything and move on */ |
---|
[96828e4] | 748 | buff=g_new(char, ptr-txtptr+20); |
---|
[12c35df] | 749 | strncpy(buff, txtptr, ptr-txtptr+1); |
---|
| 750 | buff[ptr-txtptr+1]='\0'; |
---|
[8fa9562] | 751 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[ddbbcffa] | 752 | g_free(buff); |
---|
[12c35df] | 753 | txtptr=ptr+1; |
---|
| 754 | continue; |
---|
| 755 | } |
---|
| 756 | } |
---|
| 757 | } |
---|
[a0a5179] | 758 | |
---|
| 759 | /* requires that the list values are strings or NULL. |
---|
| 760 | * joins the elements together with join_with. |
---|
| 761 | * If format_fn is specified, passes it the list element value |
---|
| 762 | * and it will return a string which this needs to free. */ |
---|
[ce68f23] | 763 | void owl_fmtext_append_list(owl_fmtext *f, const GPtrArray *l, const char *join_with, char *(format_fn)(const char *)) |
---|
[a0a5179] | 764 | { |
---|
[ce68f23] | 765 | int i; |
---|
[e19eb97] | 766 | const char *elem; |
---|
[a0a5179] | 767 | char *text; |
---|
| 768 | |
---|
[ce68f23] | 769 | for (i = 0; i < l->len; i++) { |
---|
| 770 | elem = l->pdata[i]; |
---|
[a0a5179] | 771 | if (elem && format_fn) { |
---|
| 772 | text = format_fn(elem); |
---|
| 773 | if (text) { |
---|
[1bdffcb] | 774 | owl_fmtext_append_normal(f, text); |
---|
[ddbbcffa] | 775 | g_free(text); |
---|
[a0a5179] | 776 | } |
---|
| 777 | } else if (elem) { |
---|
| 778 | owl_fmtext_append_normal(f, elem); |
---|
| 779 | } |
---|
[ce68f23] | 780 | if ((i < l->len - 1) && join_with) { |
---|
[a0a5179] | 781 | owl_fmtext_append_normal(f, join_with); |
---|
| 782 | } |
---|
| 783 | } |
---|
| 784 | } |
---|
| 785 | |
---|
| 786 | /* Free all memory allocated by the object */ |
---|
[7ab0020] | 787 | void owl_fmtext_cleanup(owl_fmtext *f) |
---|
[a0a5179] | 788 | { |
---|
[7e111f4] | 789 | if (f->buff) g_string_free(f->buff, true); |
---|
| 790 | f->buff = NULL; |
---|
[8fa9562] | 791 | } |
---|
| 792 | |
---|
| 793 | /*** Color Pair manager ***/ |
---|
| 794 | void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr) |
---|
| 795 | { |
---|
[af1920fd] | 796 | /* This could be a bitarray if we wanted to save memory. */ |
---|
[e062f97] | 797 | short i; |
---|
[af1920fd] | 798 | /* The test is <= because we allocate COLORS+1 entries. */ |
---|
[96828e4] | 799 | cpmgr->pairs = g_new(short *, COLORS + 1); |
---|
[c2c5c77] | 800 | for(i = 0; i <= COLORS; i++) { |
---|
[96828e4] | 801 | cpmgr->pairs[i] = g_new(short, COLORS + 1); |
---|
[c2c5c77] | 802 | } |
---|
[e062f97] | 803 | owl_fmtext_reset_colorpairs(cpmgr); |
---|
[a0a5179] | 804 | } |
---|
| 805 | |
---|
[8fa9562] | 806 | /* Reset used list */ |
---|
[e062f97] | 807 | void owl_fmtext_reset_colorpairs(owl_colorpair_mgr *cpmgr) |
---|
[8fa9562] | 808 | { |
---|
[e062f97] | 809 | short i, j; |
---|
| 810 | |
---|
[099597c] | 811 | cpmgr->overflow = false; |
---|
[e062f97] | 812 | cpmgr->next = 8; |
---|
[099597c] | 813 | /* The test is <= because we allocated COLORS+1 entries. */ |
---|
[e062f97] | 814 | for(i = 0; i <= COLORS; i++) { |
---|
| 815 | for(j = 0; j <= COLORS; j++) { |
---|
| 816 | cpmgr->pairs[i][j] = -1; |
---|
[fa3290d] | 817 | } |
---|
[e062f97] | 818 | } |
---|
[7b4f3be] | 819 | if (has_colors()) { |
---|
[fa3290d] | 820 | for(i = 0; i < 8; i++) { |
---|
| 821 | short fg, bg; |
---|
| 822 | if (i >= COLORS) continue; |
---|
| 823 | pair_content(i, &fg, &bg); |
---|
| 824 | cpmgr->pairs[fg+1][bg+1] = i; |
---|
[eeeef20] | 825 | } |
---|
[c2c5c77] | 826 | } |
---|
[8fa9562] | 827 | } |
---|
| 828 | |
---|
| 829 | /* Assign pairs by request */ |
---|
[1bdffcb] | 830 | short owl_fmtext_get_colorpair(int fg, int bg) |
---|
[8fa9562] | 831 | { |
---|
| 832 | owl_colorpair_mgr *cpmgr; |
---|
[423adcb] | 833 | short pair; |
---|
[1bdffcb] | 834 | |
---|
[7b4f3be] | 835 | if (!has_colors()) |
---|
| 836 | return 0; |
---|
| 837 | |
---|
[1b737a1] | 838 | /* Sanity (Bounds) Check */ |
---|
| 839 | if (fg > COLORS || fg < OWL_COLOR_DEFAULT) fg = OWL_COLOR_DEFAULT; |
---|
| 840 | if (bg > COLORS || bg < OWL_COLOR_DEFAULT) bg = OWL_COLOR_DEFAULT; |
---|
| 841 | |
---|
[1bdffcb] | 842 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
[8fa9562] | 843 | if (fg == OWL_COLOR_DEFAULT) fg = -1; |
---|
[1bdffcb] | 844 | #else |
---|
| 845 | if (fg == OWL_COLOR_DEFAULT) fg = 0; |
---|
| 846 | if (bg == OWL_COLOR_DEFAULT) bg = 0; |
---|
| 847 | #endif |
---|
| 848 | |
---|
[af1920fd] | 849 | /* looking for a pair we already set up for this draw. */ |
---|
[c2c5c77] | 850 | cpmgr = owl_global_get_colorpair_mgr(&g); |
---|
| 851 | pair = cpmgr->pairs[fg+1][bg+1]; |
---|
| 852 | if (!(pair != -1 && pair < cpmgr->next)) { |
---|
[af1920fd] | 853 | /* If we didn't find a pair, search for a free one to assign. */ |
---|
[9efa5bd] | 854 | pair = (cpmgr->next < owl_util_get_colorpairs()) ? cpmgr->next : -1; |
---|
[c2c5c77] | 855 | if (pair != -1) { |
---|
[af1920fd] | 856 | /* We found a free pair, initialize it. */ |
---|
[c2c5c77] | 857 | init_pair(pair, fg, bg); |
---|
| 858 | cpmgr->pairs[fg+1][bg+1] = pair; |
---|
| 859 | cpmgr->next++; |
---|
| 860 | } |
---|
| 861 | else if (bg != OWL_COLOR_DEFAULT) { |
---|
[af1920fd] | 862 | /* We still don't have a pair, drop the background color. Too bad. */ |
---|
[c2c5c77] | 863 | owl_function_debugmsg("colorpairs: color shortage - dropping background color."); |
---|
[099597c] | 864 | cpmgr->overflow = true; |
---|
[c2c5c77] | 865 | pair = owl_fmtext_get_colorpair(fg, OWL_COLOR_DEFAULT); |
---|
| 866 | } |
---|
| 867 | else { |
---|
[af1920fd] | 868 | /* We still don't have a pair, defaults all around. */ |
---|
[c2c5c77] | 869 | owl_function_debugmsg("colorpairs: color shortage - dropping foreground and background color."); |
---|
[099597c] | 870 | cpmgr->overflow = true; |
---|
[c2c5c77] | 871 | pair = 0; |
---|
[8fa9562] | 872 | } |
---|
| 873 | } |
---|
| 874 | return pair; |
---|
| 875 | } |
---|