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