help.c 7.13 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
Thomas Roessler's avatar
Thomas Roessler committed
3 4 5 6 7 8 9 10 11 12 13 14 15
 * 
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
16
 *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
Thomas Roessler's avatar
Thomas Roessler committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
 */ 

#define HELP_C

#include "mutt.h"
#include "mutt_curses.h"
#include "keymap.h"
#include "pager.h"
#include "mapping.h"

#include <ctype.h>
#include <string.h>

static struct binding_t *help_lookupFunction (int op, int menu)
{
  int i;
  struct binding_t *map;

  if (menu != MENU_PAGER)
  {
    /* first look in the generic map for the function */
    for (i = 0; OpGeneric[i].name; i++)
      if (OpGeneric[i].op == op)
	return (&OpGeneric[i]);    
  }

  if ((map = km_get_table(menu)))
  {
    for (i = 0; map[i].name; i++)
      if (map[i].op == op)
	return (&map[i]);
  }
  
  return (NULL);
}

void mutt_make_help (char *d, size_t dlen, char *txt, int menu, int op)
{
  char buf[SHORT_STRING];

  if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
      km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
    snprintf (d, dlen, "%s:%s", buf, txt);
  else
    d[0] = 0;
}

char *
mutt_compile_help (char *buf, size_t buflen, int menu, struct mapping_t *items)
{
  int i;
  size_t len;
  char *pbuf = buf;
  
  for (i = 0; items[i].name && buflen > 2; i++)
  {
    if (i)
    {
      *pbuf++ = ' ';
      *pbuf++ = ' ';
      buflen -= 2;
    }
79
    mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
80
    len = mutt_strlen (pbuf);
Thomas Roessler's avatar
Thomas Roessler committed
81 82 83 84 85 86
    pbuf += len;
    buflen -= len;
  }
  return buf;
}

87
static int print_macro (FILE *f, int maxwidth, const char **macro)
Thomas Roessler's avatar
Thomas Roessler committed
88
{
89 90
  int n = maxwidth;
  wchar_t wc;
91 92 93 94 95 96 97 98
  int w;
  size_t k;
  size_t len = mutt_strlen (*macro);
  mbstate_t mbstate1, mbstate2;

  memset (&mbstate1, 0, sizeof (mbstate1));
  memset (&mbstate2, 0, sizeof (mbstate2));
  for (; (k = mbrtowc (&wc, *macro, len, &mbstate1)); *macro += k, len -= k)
Thomas Roessler's avatar
Thomas Roessler committed
99
  {
100
    if (k == (size_t)(-1) || k == (size_t)(-2))
101
      break;
102
    if ((w = wcwidth (wc)) >= 0)
103
    {
104 105 106
      if (w > n)
	break;
      n -= w;
107
      {
108 109 110 111 112
	char buf[MB_LEN_MAX*2];
	size_t n1, n2;
	if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t)(-1) &&
	    (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t)(-1))
	  fputs (buf, f);
113 114
      }
    }
115
    else if (wc < 0x20 || wc == 0x7f)
Thomas Roessler's avatar
Thomas Roessler committed
116
    {
117 118 119 120 121 122 123 124 125 126 127 128 129
      if (2 > n)
	break;
      n -= 2;
      if (wc == '\033')
	fprintf (f, "\\e");
      else if (wc == '\n')
	fprintf (f, "\\n");
      else if (wc == '\r')
	fprintf (f, "\\r");
      else if (wc == '\t')
	fprintf (f, "\\t");
      else
	fprintf (f, "^%c", (char)((wc + '@') & 0x7f));
Thomas Roessler's avatar
Thomas Roessler committed
130
    }
131
    else
132 133 134 135 136 137
    {
      if (1 > n)
	break;
      n -= 1;
      fprintf (f, "?");
    }
Thomas Roessler's avatar
Thomas Roessler committed
138
  }
139
  return (maxwidth - n);
Thomas Roessler's avatar
Thomas Roessler committed
140 141 142 143 144 145
}

static int pad (FILE *f, int col, int i)
{
  char fmt[8];

146
  if (col < i)
Thomas Roessler's avatar
Thomas Roessler committed
147
  {
148
    snprintf (fmt, sizeof(fmt), "%%-%ds", i - col);
Thomas Roessler's avatar
Thomas Roessler committed
149
    fprintf (f, fmt, "");
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    return (i);
  }
  fputc (' ', f);
  return (col + 1);
}

static void format_line (FILE *f, int ismacro,
			 const char *t1, const char *t2, const char *t3)
{
  int col;
  int col_a, col_b;
  int split;
  int n;

  fputs (t1, f);

  /* don't try to press string into one line with less than 40 characters.
     The double paranthesis avoid a gcc warning, sigh ... */
  if ((split = COLS < 40))
  {
    col_a = col = 0;
    col_b = LONG_STRING;
    fputc ('\n', f);
Thomas Roessler's avatar
Thomas Roessler committed
173 174 175
  }
  else
  {
176 177
    col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
    col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
178
    col = pad (f, mutt_strlen(t1), col_a);
Thomas Roessler's avatar
Thomas Roessler committed
179
  }
180 181 182

  if (ismacro > 0)
  {
183
    if (!mutt_strcmp (Pager, "builtin"))
184 185 186 187 188 189 190
      fputs ("_\010", f);
    fputs ("M ", f);
    col += 2;

    if (!split)
    {
      col += print_macro (f, col_b - col - 4, &t2);
191
      if (mutt_strlen (t2) > col_b - col)
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	t2 = "...";
    }
  }

  col += print_macro (f, col_b - col - 1, &t2);
  if (split)
    fputc ('\n', f);
  else
    col = pad (f, col, col_b);

  if (split)
  {
    print_macro (f, LONG_STRING, &t3);
    fputc ('\n', f);
  }
  else
  {
    while (*t3)
    {
      n = COLS - col;

      if (ismacro >= 0)
      {
	SKIPWS(t3);

217
	/* FIXME: this is completely wrong */
218
	if ((n = mutt_strlen (t3)) > COLS - col)
219 220 221 222 223 224 225 226 227 228 229 230
	{
	  n = COLS - col;
	  for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--) ;
	  if (col_a)
	    n = col_a;
	}
      }

      print_macro (f, n, &t3);

      if (*t3)
      {
231
        if (mutt_strcmp (Pager, "builtin"))
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	{
	  fputc ('\n', f);
	  n = 0;
	}
	else
	{
	  n += col - COLS;
	  if (option (OPTMARKERS))
	    ++n;
	}
	col = pad (f, n, col_b);
      }
    }
  }

  fputc ('\n', f);
Thomas Roessler's avatar
Thomas Roessler committed
248 249 250 251 252 253 254 255 256 257 258
}

static void dump_menu (FILE *f, int menu)
{
  struct keymap_t *map;
  struct binding_t *b;
  char buf[SHORT_STRING];

  /* browse through the keymap table */
  for (map = Keymaps[menu]; map; map = map->next)
  {
Thomas Roessler's avatar
Thomas Roessler committed
259
    if (map->op != OP_NULL)
Thomas Roessler's avatar
Thomas Roessler committed
260
    {
Thomas Roessler's avatar
Thomas Roessler committed
261 262 263 264 265
      km_expand_key (buf, sizeof (buf), map);

      if (map->op == OP_MACRO)
      {
	if (map->descr == NULL)
266 267 268
	  format_line (f, -1, buf, "macro", map->macro);
        else
	  format_line (f, 1, buf, map->macro, map->descr);
Thomas Roessler's avatar
Thomas Roessler committed
269 270 271 272
      }
      else
      {
	b = help_lookupFunction (map->op, menu);
273
	format_line (f, 0, buf, b ? b->name : "UNKNOWN",
274
	      b ? _(HelpStrings[b->op]) : _("ERROR: please report this bug"));
Thomas Roessler's avatar
Thomas Roessler committed
275
      }
Thomas Roessler's avatar
Thomas Roessler committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    }
  }
}

static int is_bound (struct keymap_t *map, int op)
{
  for (; map; map = map->next)
    if (map->op == op)
      return 1;
  return 0;
}

static void dump_unbound (FILE *f,
			  struct binding_t *funcs,
			  struct keymap_t *map,
			  struct keymap_t *aux)
{
  int i;

  for (i = 0; funcs[i].name; i++)
  {
    if (! is_bound (map, funcs[i].op) &&
	(!aux || ! is_bound (aux, funcs[i].op)))
299
      format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
Thomas Roessler's avatar
Thomas Roessler committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  }
}

void mutt_help (int menu)
{
  char t[_POSIX_PATH_MAX];
  char buf[SHORT_STRING];
  char *desc;
  FILE *f;
  struct binding_t *funcs;

  mutt_mktemp (t);

  funcs = km_get_table (menu);
  desc = mutt_getnamebyvalue (menu, Menus);
  if (!desc)
316
    desc = _("<UNKNOWN>");
Thomas Roessler's avatar
Thomas Roessler committed
317
  
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  do {
    if ((f = safe_fopen (t, "w")) == NULL)
    {
      mutt_perror (t);
      return;
    }
  
    dump_menu (f, menu);
    if (menu != MENU_EDITOR && menu != MENU_PAGER)
    {
      fputs (_("\nGeneric bindings:\n\n"), f);
      dump_menu (f, MENU_GENERIC);
    }
  
    fputs (_("\nUnbound functions:\n\n"), f);
    if (funcs)
      dump_unbound (f, funcs, Keymaps[menu], NULL);
    if (menu != MENU_PAGER)
      dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
  
    fclose (f);
  
    snprintf (buf, sizeof (buf), _("Help for %s"), desc);
Thomas Roessler's avatar
Thomas Roessler committed
341
  }
342 343 344 345 346
  while
    (mutt_do_pager (buf, t,
		    M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP,
		    NULL)
     == OP_REFORMAT_WINCH);
Thomas Roessler's avatar
Thomas Roessler committed
347
}