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