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