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