Esempio n. 1
0
/* add the formatted text to the curses window 'w'.  The window 'w'
 * must already be initiatlized with curses
 */
static void _owl_fmtext_curs_waddstr(const owl_fmtext *f, WINDOW *w, int do_search)
{
  /* char *tmpbuff; */
  /* int position, trans1, trans2, trans3, len, lastsame; */
  char *s, *p;
  char attr;
  short fg, bg, pair = 0;
  
  if (w==NULL) {
    owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
    return;
  }

  s = f->textbuff;
  /* Set default attributes. */
  attr = f->default_attrs;
  fg = f->default_fgcolor;
  bg = f->default_bgcolor;
  _owl_fmtext_wattrset(w, attr);
  _owl_fmtext_update_colorpair(fg, bg, &pair);
  _owl_fmtext_wcolor_set(w, pair);

  /* Find next possible format character. */
  p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
  while(p) {
    if (owl_fmtext_is_format_char(g_utf8_get_char(p))) {
      /* Deal with all text from last insert to here. */
      char tmp;
   
      tmp = p[0];
      p[0] = '\0';
      if (owl_global_is_search_active(&g)) {
	/* Search is active, so highlight search results. */
	char tmp2;
	int start, end;
	while (owl_regex_compare(owl_global_get_search_re(&g), s, &start, &end) == 0) {
	  /* Prevent an infinite loop matching the empty string. */
	  if (end == 0)
	    break;

	  /* Found search string, highlight it. */

	  tmp2 = s[start];
	  s[start] = '\0';
	  waddstr(w, s);
	  s[start] = tmp2;

	  _owl_fmtext_wattrset(w, attr ^ OWL_FMTEXT_ATTR_REVERSE);
	  _owl_fmtext_wcolor_set(w, pair);
	  
	  tmp2 = s[end];
	  s[end] = '\0';
	  waddstr(w, s + start);
	  s[end] = tmp2;

	  _owl_fmtext_wattrset(w, attr);
	  _owl_fmtext_wcolor_set(w, pair);

	  s += end;
	}
      }
      /* Deal with remaining part of string. */
      waddstr(w, s);
      p[0] = tmp;

      /* Deal with new attributes. Initialize to defaults, then
	 process all consecutive formatting characters. */
      attr = f->default_attrs;
      fg = f->default_fgcolor;
      bg = f->default_bgcolor;
      while (owl_fmtext_is_format_char(g_utf8_get_char(p))) {
	_owl_fmtext_update_attributes(g_utf8_get_char(p), &attr, &fg, &bg);
	p = g_utf8_next_char(p);
      }
      _owl_fmtext_wattrset(w, attr | f->default_attrs);
      if (fg == OWL_COLOR_DEFAULT) fg = f->default_fgcolor;
      if (bg == OWL_COLOR_DEFAULT) bg = f->default_bgcolor;
      _owl_fmtext_update_colorpair(fg, bg, &pair);
      _owl_fmtext_wcolor_set(w, pair);

      /* Advance to next non-formatting character. */
      s = p;
      p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
    }
    else {
      p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8);
    }
  }
  if (s) {
    waddstr(w, s);
  }
  wbkgdset(w, 0);
}
Esempio n. 2
0
/* Truncate the message so that each line begins at column 'acol' and
 * ends at 'bcol' or sooner.  The first column is number 0.  The new
 * message is placed in 'out'.  The message is expected to end in a
 * new line for now.
 *
 * NOTE: This needs to be modified to deal with backing up if we find
 * a SPACING COMBINING MARK at the end of a line. If that happens, we
 * should back up to the last non-mark character and stop there.
 *
 * NOTE: If a line ends at bcol, we omit the newline. This is so printing
 * to ncurses works.
 */
void owl_fmtext_truncate_cols(const owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
{
  const char *ptr_s, *ptr_e, *ptr_c, *last;
  int col, st, padding, chwidth;

  /* copy the default attributes */
  out->default_attrs = in->default_attrs;
  out->default_fgcolor = in->default_fgcolor;
  out->default_bgcolor = in->default_bgcolor;

  last=in->textbuff+in->textlen-1;
  ptr_s=in->textbuff;
  while (ptr_s <= last) {
    ptr_e=strchr(ptr_s, '\n');
    if (!ptr_e) {
      /* but this shouldn't happen if we end in a \n */
      break;
    }
    
    if (ptr_e == ptr_s) {
      owl_fmtext_append_normal(out, "\n");
      ++ptr_s;
      continue;
    }

    col = 0;
    st = 0;
    padding = 0;
    chwidth = 0;
    ptr_c = ptr_s;
    while(ptr_c < ptr_e) {
      gunichar c = g_utf8_get_char(ptr_c);
      if (!owl_fmtext_is_format_char(c)) {
	chwidth = mk_wcwidth(c);
	if (col + chwidth > bcol) break;
	
	if (col >= acol) {
	  if (st == 0) {
	    ptr_s = ptr_c;
	    padding = col - acol;
	    ++st;
	  }
	}
	col += chwidth;
	chwidth = 0;
      }
      ptr_c = g_utf8_next_char(ptr_c);
    }
    if (st) {
      /* lead padding */
      owl_fmtext_append_spaces(out, padding);
      if (ptr_c == ptr_e) {
	/* We made it to the newline. */
	_owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff);
      }
      else if (chwidth > 1) {
        /* Last char is wide, truncate. */
        _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff - 1);
        owl_fmtext_append_normal(out, "\n");
      }
      else {
        /* Last char fits perfectly, We skip to the next char and back
         * up a byte to make sure we get it all.
         */
        ptr_c = g_utf8_next_char(ptr_c);
        _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff - 1);
      }
    }
    else {
      owl_fmtext_append_normal(out, "\n");
    }
    ptr_s = g_utf8_next_char(ptr_e);
  }
}
Esempio n. 3
0
/* add the formatted text to the curses window 'w'.  The window 'w'
 * must already be initiatlized with curses
 */
void _owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w, int do_search) /*noproto*/
{
  /* char *tmpbuff; */
  /* int position, trans1, trans2, trans3, len, lastsame; */
  char *s, *p;
  char attr;
  short fg, bg, pair;
  int search_results, search_len;
  
  if (w==NULL) {
    owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
    return;
  }

  search_results = (do_search
		    ? owl_fmtext_search(f, owl_global_get_search_string(&g))
		    : 0);
  search_len = (search_results
		? strlen(owl_global_get_search_string(&g))
		: 0);
  s = f->textbuff;
  /* Set default attributes. */
  attr = f->default_attrs;
  fg = f->default_fgcolor;
  bg = f->default_bgcolor;
  _owl_fmtext_wattrset(w, attr);
  _owl_fmtext_update_colorpair(fg, bg, &pair);
  _owl_fmtext_wcolor_set(w, pair);

  /* Find next possible format character. */
  p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
  while(p) {
    if (owl_fmtext_is_format_char(g_utf8_get_char(p))) {
      /* Deal with all text from last insert to here. */
      char tmp;
   
      tmp = p[0];
      p[0] = '\0';
      if (search_results) {
	/* Search is active, so highlight search results. */
	char tmp2, *ss;
	ss = stristr(s, owl_global_get_search_string(&g));
	while (ss) {
	  /* Found search string, highlight it. */

	  tmp2 = ss[0];
	  ss[0] = '\0';
	  waddstr(w, s);
	  ss[0] = tmp2;

	  _owl_fmtext_wattrset(w, attr ^ OWL_FMTEXT_ATTR_REVERSE);
	  _owl_fmtext_wcolor_set(w, pair);
	  
	  tmp2 = ss[search_len];
	  ss[search_len] = '\0';
	  waddstr(w, ss);
	  ss[search_len] = tmp2;

	  _owl_fmtext_wattrset(w, attr);
	  _owl_fmtext_wcolor_set(w, pair);

	  s = ss + search_len;
	  ss = stristr(s, owl_global_get_search_string(&g));
	}
      }
      /* Deal with remaining part of string. */
      waddstr(w, s);
      p[0] = tmp;

      /* Deal with new attributes. Initialize to defaults, then
	 process all consecutive formatting characters. */
      attr = f->default_attrs;
      fg = f->default_fgcolor;
      bg = f->default_bgcolor;
      while (p && owl_fmtext_is_format_char(g_utf8_get_char(p))) {
	_owl_fmtext_update_attributes(g_utf8_get_char(p), &attr, &fg, &bg);
	p = g_utf8_next_char(p);
      }
      _owl_fmtext_wattrset(w, attr | f->default_attrs);
      if (fg == OWL_COLOR_DEFAULT) fg = f->default_fgcolor;
      if (bg == OWL_COLOR_DEFAULT) bg = f->default_bgcolor;
      _owl_fmtext_update_colorpair(fg, bg, &pair);
      _owl_fmtext_wcolor_set(w, pair);

      /* Advance to next non-formatting character. */
      s = p;
      p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
    }
    else {
      p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8);
    }
  }
  if (s) {
    waddstr(w, s);
  }
}