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