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