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