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