~ubuntu-branches/ubuntu/lucid/mutt/lucid-updates

« back to all changes in this revision

Viewing changes to curs_lib.c

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2007-11-03 23:00:04 UTC
  • mto: (16.1.1 sid) (1.1.11 upstream) (2.3.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20071103230004-l98yi0j504i7yjjl
Tags: upstream-1.5.17
ImportĀ upstreamĀ versionĀ 1.5.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
    mutt_query_exit ();
100
100
 
101
101
  if(ch == ERR)
 
102
  {
 
103
    /* either timeout or the terminal has been lost */
 
104
    if (!isatty (0))
 
105
    {
 
106
      endwin ();
 
107
      exit (1);
 
108
    }
102
109
    return err;
103
 
  
 
110
  }
 
111
 
104
112
  if ((ch & 0x80) && option (OPTMETAKEY))
105
113
  {
106
114
    /* send ALT-x as ESC-x */
292
300
  
293
301
  dprint (1, (debugfile, "%s\n", scratch));
294
302
  mutt_format_string (Errorbuf, sizeof (Errorbuf),
295
 
                      0, COLS-2, 0, 0, scratch, sizeof (scratch), 0);
 
303
                      0, COLS-2, FMT_LEFT, 0, scratch, sizeof (scratch), 0);
296
304
 
297
305
  if (!option (OPTKEEPQUIET))
298
306
  {
317
325
  va_end (ap);
318
326
 
319
327
  mutt_format_string (Errorbuf, sizeof (Errorbuf),
320
 
                      0, COLS-2, 0, 0, scratch, sizeof (scratch), 0);
 
328
                      0, COLS-2, FMT_LEFT, 0, scratch, sizeof (scratch), 0);
321
329
 
322
330
  if (!option (OPTKEEPQUIET))
323
331
  {
641
649
 
642
650
void mutt_format_string (char *dest, size_t destlen,
643
651
                         int min_width, int max_width,
644
 
                         int right_justify, char m_pad_char,
 
652
                         int justify, char m_pad_char,
645
653
                         const char *s, size_t n,
646
654
                         int arboreal)
647
655
{
688
696
  w = (int)destlen < min_width ? destlen : min_width;
689
697
  if (w <= 0)
690
698
    *p = '\0';
691
 
  else if (right_justify)
 
699
  else if (justify == FMT_RIGHT)        /* right justify */
692
700
  {
693
701
    p[w] = '\0';
694
702
    while (--p >= dest)
696
704
    while (--w >= 0)
697
705
      dest[w] = m_pad_char;
698
706
  }
699
 
  else
 
707
  else if (justify == FMT_CENTER)       /* center */
 
708
  {
 
709
    char *savedp = p;
 
710
    int half = (w+1) / 2; /* half of cushion space */
 
711
 
 
712
    p[w] = '\0';
 
713
 
 
714
    /* move str to center of buffer */
 
715
    while (--p >= dest)
 
716
      p[half] = *p;
 
717
 
 
718
    /* fill rhs */
 
719
    p = savedp + half;
 
720
    while (--w >= half)
 
721
      *p++ = m_pad_char;
 
722
 
 
723
    /* fill lhs */
 
724
    while (half--)
 
725
      dest[half] = m_pad_char;
 
726
  }
 
727
  else                                  /* left justify */
700
728
  {
701
729
    while (--w >= 0)
702
730
      *p++ = m_pad_char;
718
746
                             const char *s,
719
747
                             int arboreal)
720
748
{
721
 
  int right_justify = 1;
 
749
  int justify = FMT_RIGHT;
722
750
  char *p;
723
751
  int min_width;
724
752
  int max_width = INT_MAX;
725
753
 
726
754
  if (*prefix == '-')
727
 
    ++prefix, right_justify = 0;
 
755
    ++prefix, justify = FMT_LEFT;
 
756
  else if (*prefix == '=')
 
757
    ++prefix, justify = FMT_CENTER;
728
758
  min_width = strtol (prefix, &p, 10);
729
759
  if (*p == '.')
730
760
  {
735
765
  }
736
766
 
737
767
  mutt_format_string (dest, destlen, min_width, max_width,
738
 
                      right_justify, ' ', s, mutt_strlen (s), arboreal);
 
768
                      justify, ' ', s, mutt_strlen (s), arboreal);
739
769
}
740
770
 
741
771
void mutt_format_s (char *dest,
756
786
 
757
787
/*
758
788
 * mutt_paddstr (n, s) is almost equivalent to
759
 
 * mutt_format_string (bigbuf, big, n, n, 0, ' ', s, big, 0), addstr (bigbuf)
 
789
 * mutt_format_string (bigbuf, big, n, n, FMT_LEFT, ' ', s, big, 0), addstr (bigbuf)
760
790
 */
761
791
 
762
792
void mutt_paddstr (int n, const char *s)
792
822
    addch (' ');
793
823
}
794
824
 
 
825
/* See how many bytes to copy from string so its at most maxlen bytes
 
826
 * long and maxwid columns wide */
 
827
int mutt_wstr_trunc (const char *src, size_t maxlen, size_t maxwid, size_t *width)
 
828
{
 
829
  wchar_t wc;
 
830
  int w = 0, l = 0, cl;
 
831
  size_t cw, n;
 
832
  mbstate_t mbstate;
 
833
 
 
834
  if (!src)
 
835
    goto out;
 
836
 
 
837
  n = mutt_strlen (src);
 
838
 
 
839
  memset (&mbstate, 0, sizeof (mbstate));
 
840
  for (w = 0; n && (cl = mbrtowc (&wc, src, n, &mbstate)); src += cl, n -= cl)
 
841
  {
 
842
    if (cl == (size_t)(-1) || cl == (size_t)(-2))
 
843
      cw = cl = 1;
 
844
    else
 
845
      cw = wcwidth (wc);
 
846
    if (cl + l > maxlen || cw + w > maxwid)
 
847
      break;
 
848
    l += cl;
 
849
    w += cw;
 
850
  }
 
851
out:
 
852
  if (width)
 
853
    *width = w;
 
854
  return l;
 
855
}
 
856
 
 
857
/*
 
858
 * returns the number of bytes the first (multibyte) character
 
859
 * of input consumes:
 
860
 *      < 0 ... conversion error
 
861
 *      = 0 ... end of input
 
862
 *      > 0 ... length (bytes)
 
863
 */
 
864
int mutt_charlen (const char *s, int *width)
 
865
{
 
866
  wchar_t wc;
 
867
  mbstate_t mbstate;
 
868
  size_t k, n;
 
869
 
 
870
  if (!s || !*s)
 
871
    return 0;
 
872
 
 
873
  n = mutt_strlen (s);
 
874
  memset (&mbstate, 0, sizeof (mbstate));
 
875
  k = mbrtowc (&wc, s, n, &mbstate);
 
876
  if (width)
 
877
    *width = wcwidth (wc);
 
878
  return (k == (size_t)(-1) || k == (size_t)(-2)) ? -1 : k;
 
879
}
 
880
 
795
881
/*
796
882
 * mutt_strwidth is like mutt_strlen except that it returns the width
797
883
 * refering to the number of characters cells.