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