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