1114472Sru/* variables.c -- how to manipulate user visible variables in Info.
2146515Sru   $Id: variables.c,v 1.3 2004/04/11 17:56:46 karl Exp $
321495Sjmacd
4146515Sru   Copyright (C) 1993, 1997, 2001, 2002, 2004 Free Software Foundation, Inc.
521495Sjmacd
621495Sjmacd   This program is free software; you can redistribute it and/or modify
721495Sjmacd   it under the terms of the GNU General Public License as published by
821495Sjmacd   the Free Software Foundation; either version 2, or (at your option)
921495Sjmacd   any later version.
1021495Sjmacd
1121495Sjmacd   This program is distributed in the hope that it will be useful,
1221495Sjmacd   but WITHOUT ANY WARRANTY; without even the implied warranty of
1321495Sjmacd   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1421495Sjmacd   GNU General Public License for more details.
1521495Sjmacd
1621495Sjmacd   You should have received a copy of the GNU General Public License
1721495Sjmacd   along with this program; if not, write to the Free Software
1821495Sjmacd   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1921495Sjmacd
2021495Sjmacd   Written by Brian Fox (bfox@ai.mit.edu). */
2121495Sjmacd
2221495Sjmacd#include "info.h"
2321495Sjmacd#include "variables.h"
2421495Sjmacd
2521495Sjmacd/* **************************************************************** */
2642660Smarkm/*                                                                  */
2742660Smarkm/*                  User Visible Variables in Info                  */
2842660Smarkm/*                                                                  */
2921495Sjmacd/* **************************************************************** */
3021495Sjmacd
3121495Sjmacd/* Choices used by the completer when reading a zero/non-zero value for
3221495Sjmacd   a variable. */
3321495Sjmacdstatic char *on_off_choices[] = { "Off", "On", (char *)NULL };
3421495Sjmacd
3521495SjmacdVARIABLE_ALIST info_variables[] = {
3621495Sjmacd  { "automatic-footnotes",
3742660Smarkm      N_("When \"On\", footnotes appear and disappear automatically"),
3821495Sjmacd      &auto_footnotes_p, (char **)on_off_choices },
3921495Sjmacd
4021495Sjmacd  { "automatic-tiling",
4142660Smarkm      N_("When \"On\", creating or deleting a window resizes other windows"),
4221495Sjmacd      &auto_tiling_p, (char **)on_off_choices },
4321495Sjmacd
4421495Sjmacd  { "visible-bell",
4542660Smarkm      N_("When \"On\", flash the screen instead of ringing the bell"),
4621495Sjmacd      &terminal_use_visible_bell_p, (char **)on_off_choices },
4721495Sjmacd
4821495Sjmacd  { "errors-ring-bell",
4942660Smarkm      N_("When \"On\", errors cause the bell to ring"),
5021495Sjmacd      &info_error_rings_bell_p, (char **)on_off_choices },
5121495Sjmacd
5221495Sjmacd  { "gc-compressed-files",
5342660Smarkm      N_("When \"On\", Info garbage collects files which had to be uncompressed"),
5421495Sjmacd      &gc_compressed_files, (char **)on_off_choices },
5521495Sjmacd  { "show-index-match",
5642660Smarkm      N_("When \"On\", the portion of the matched search string is highlighted"),
5721495Sjmacd      &show_index_match, (char **)on_off_choices },
5821495Sjmacd
5921495Sjmacd  { "scroll-behaviour",
6042660Smarkm      N_("Controls what happens when scrolling is requested at the end of a node"),
6121495Sjmacd      &info_scroll_behaviour, (char **)info_scroll_choices },
6221495Sjmacd
6321495Sjmacd  { "scroll-step",
6442660Smarkm      N_("The number lines to scroll when the cursor moves out of the window"),
6521495Sjmacd      &window_scroll_step, (char **)NULL },
6621495Sjmacd
6721495Sjmacd  { "ISO-Latin",
6842660Smarkm      N_("When \"On\", Info accepts and displays ISO Latin characters"),
6921495Sjmacd      &ISO_Latin_p, (char **)on_off_choices },
7021495Sjmacd
7121495Sjmacd  { (char *)NULL, (char *)NULL, (int *)NULL, (char **)NULL }
7221495Sjmacd};
7321495Sjmacd
7442660SmarkmDECLARE_INFO_COMMAND (describe_variable, _("Explain the use of a variable"))
7521495Sjmacd{
7621495Sjmacd  VARIABLE_ALIST *var;
7721495Sjmacd  char *description;
7821495Sjmacd
7921495Sjmacd  /* Get the variable's name. */
80146515Sru  var = read_variable_name ((char *) _("Describe variable: "), window);
8121495Sjmacd
8221495Sjmacd  if (!var)
8321495Sjmacd    return;
8421495Sjmacd
8542660Smarkm  description = (char *)xmalloc (20 + strlen (var->name)
8642660Smarkm				 + strlen (_(var->doc)));
8721495Sjmacd
8821495Sjmacd  if (var->choices)
8921495Sjmacd    sprintf (description, "%s (%s): %s.",
9042660Smarkm             var->name, var->choices[*(var->value)], _(var->doc));
9121495Sjmacd  else
9242660Smarkm    sprintf (description, "%s (%d): %s.",
9342660Smarkm	     var->name, *(var->value), _(var->doc));
9421495Sjmacd
95146515Sru  window_message_in_echo_area ("%s", description, NULL);
9621495Sjmacd  free (description);
9721495Sjmacd}
9821495Sjmacd
9942660SmarkmDECLARE_INFO_COMMAND (set_variable, _("Set the value of an Info variable"))
10021495Sjmacd{
10121495Sjmacd  VARIABLE_ALIST *var;
10221495Sjmacd  char *line;
10321495Sjmacd
10421495Sjmacd  /* Get the variable's name and value. */
105146515Sru  var = read_variable_name ((char *) _("Set variable: "), window);
10621495Sjmacd
10721495Sjmacd  if (!var)
10821495Sjmacd    return;
10921495Sjmacd
11021495Sjmacd  /* Read a new value for this variable. */
11121495Sjmacd  {
11221495Sjmacd    char prompt[100];
11321495Sjmacd
11421495Sjmacd    if (!var->choices)
11521495Sjmacd      {
11642660Smarkm        int potential_value;
11721495Sjmacd
11842660Smarkm        if (info_explicit_arg || count != 1)
11942660Smarkm          potential_value = count;
12042660Smarkm        else
12142660Smarkm          potential_value = *(var->value);
12221495Sjmacd
12342660Smarkm        sprintf (prompt, _("Set %s to value (%d): "),
12442660Smarkm                 var->name, potential_value);
12542660Smarkm        line = info_read_in_echo_area (active_window, prompt);
12621495Sjmacd
12742660Smarkm        /* If no error was printed, clear the echo area. */
12842660Smarkm        if (!info_error_was_printed)
12942660Smarkm          window_clear_echo_area ();
13021495Sjmacd
13142660Smarkm        /* User aborted? */
13242660Smarkm        if (!line)
13342660Smarkm          return;
13421495Sjmacd
13542660Smarkm        /* If the user specified a value, get that, otherwise, we are done. */
13642660Smarkm        canonicalize_whitespace (line);
13742660Smarkm        if (*line)
13842660Smarkm          *(var->value) = atoi (line);
13942660Smarkm        else
14042660Smarkm          *(var->value) = potential_value;
14121495Sjmacd
14242660Smarkm        free (line);
14321495Sjmacd      }
14421495Sjmacd    else
14521495Sjmacd      {
14642660Smarkm        register int i;
14742660Smarkm        REFERENCE **array = (REFERENCE **)NULL;
14842660Smarkm        int array_index = 0;
14942660Smarkm        int array_slots = 0;
15021495Sjmacd
15142660Smarkm        for (i = 0; var->choices[i]; i++)
15242660Smarkm          {
15342660Smarkm            REFERENCE *entry;
15421495Sjmacd
15542660Smarkm            entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
15642660Smarkm            entry->label = xstrdup (var->choices[i]);
15742660Smarkm            entry->nodename = (char *)NULL;
15842660Smarkm            entry->filename = (char *)NULL;
15921495Sjmacd
16042660Smarkm            add_pointer_to_array
16142660Smarkm              (entry, array_index, array, array_slots, 10, REFERENCE *);
16242660Smarkm          }
16321495Sjmacd
16442660Smarkm        sprintf (prompt, _("Set %s to value (%s): "),
16542660Smarkm                 var->name, var->choices[*(var->value)]);
16621495Sjmacd
16742660Smarkm        /* Ask the completer to read a variable value for us. */
16842660Smarkm        line = info_read_completing_in_echo_area (window, prompt, array);
16921495Sjmacd
17042660Smarkm        info_free_references (array);
17121495Sjmacd
17242660Smarkm        if (!echo_area_is_active)
17342660Smarkm          window_clear_echo_area ();
17421495Sjmacd
17542660Smarkm        /* User aborted? */
17642660Smarkm        if (!line)
17742660Smarkm          {
17842660Smarkm            info_abort_key (active_window, 0, 0);
17942660Smarkm            return;
18042660Smarkm          }
18121495Sjmacd
18242660Smarkm        /* User accepted default choice?  If so, no change. */
18342660Smarkm        if (!*line)
18442660Smarkm          {
18542660Smarkm            free (line);
18642660Smarkm            return;
18742660Smarkm          }
18821495Sjmacd
18942660Smarkm        /* Find the choice in our list of choices. */
19042660Smarkm        for (i = 0; var->choices[i]; i++)
19142660Smarkm          if (strcmp (var->choices[i], line) == 0)
19242660Smarkm            break;
19321495Sjmacd
19442660Smarkm        if (var->choices[i])
19542660Smarkm          *(var->value) = i;
19621495Sjmacd      }
19721495Sjmacd  }
19821495Sjmacd}
19921495Sjmacd
20021495Sjmacd/* Read the name of an Info variable in the echo area and return the
20121495Sjmacd   address of a VARIABLE_ALIST member.  A return value of NULL indicates
20221495Sjmacd   that no variable could be read. */
20321495SjmacdVARIABLE_ALIST *
204146515Sruread_variable_name (char *prompt, WINDOW *window)
20521495Sjmacd{
20621495Sjmacd  register int i;
20721495Sjmacd  char *line;
20821495Sjmacd  REFERENCE **variables;
20921495Sjmacd
21021495Sjmacd  /* Get the completion array of variable names. */
21121495Sjmacd  variables = make_variable_completions_array ();
21221495Sjmacd
21321495Sjmacd  /* Ask the completer to read a variable for us. */
21421495Sjmacd  line =
21521495Sjmacd    info_read_completing_in_echo_area (window, prompt, variables);
21621495Sjmacd
21721495Sjmacd  info_free_references (variables);
21821495Sjmacd
21921495Sjmacd  if (!echo_area_is_active)
22021495Sjmacd    window_clear_echo_area ();
22121495Sjmacd
22221495Sjmacd  /* User aborted? */
22321495Sjmacd  if (!line)
22421495Sjmacd    {
22521495Sjmacd      info_abort_key (active_window, 0, 0);
22621495Sjmacd      return ((VARIABLE_ALIST *)NULL);
22721495Sjmacd    }
22821495Sjmacd
22921495Sjmacd  /* User accepted "default"?  (There is none.) */
23021495Sjmacd  if (!*line)
23121495Sjmacd    {
23221495Sjmacd      free (line);
23321495Sjmacd      return ((VARIABLE_ALIST *)NULL);
23421495Sjmacd    }
23521495Sjmacd
23621495Sjmacd  /* Find the variable in our list of variables. */
23721495Sjmacd  for (i = 0; info_variables[i].name; i++)
23821495Sjmacd    if (strcmp (info_variables[i].name, line) == 0)
23921495Sjmacd      break;
24021495Sjmacd
24121495Sjmacd  if (!info_variables[i].name)
24221495Sjmacd    return ((VARIABLE_ALIST *)NULL);
24321495Sjmacd  else
24421495Sjmacd    return (&(info_variables[i]));
24521495Sjmacd}
24621495Sjmacd
24721495Sjmacd/* Make an array of REFERENCE which actually contains the names of the
24821495Sjmacd   variables available in Info. */
24921495SjmacdREFERENCE **
250146515Srumake_variable_completions_array (void)
25121495Sjmacd{
25221495Sjmacd  register int i;
25321495Sjmacd  REFERENCE **array = (REFERENCE **)NULL;
25421495Sjmacd  int array_index = 0, array_slots = 0;
25521495Sjmacd
25621495Sjmacd  for (i = 0; info_variables[i].name; i++)
25721495Sjmacd    {
25821495Sjmacd      REFERENCE *entry;
25921495Sjmacd
26042660Smarkm      entry = (REFERENCE *) xmalloc (sizeof (REFERENCE));
26142660Smarkm      entry->label = xstrdup (info_variables[i].name);
26221495Sjmacd      entry->nodename = (char *)NULL;
26321495Sjmacd      entry->filename = (char *)NULL;
26421495Sjmacd
26521495Sjmacd      add_pointer_to_array
26642660Smarkm        (entry, array_index, array, array_slots, 200, REFERENCE *);
26721495Sjmacd    }
26821495Sjmacd
26921495Sjmacd  return (array);
27021495Sjmacd}
27193139Sru
27293139Sru#if defined(INFOKEY)
27393139Sru
27493139Sruvoid
275146515Sruset_variable_to_value(char *name, char *value)
27693139Sru{
27793139Sru	register int i;
27893139Sru
27993139Sru	/* Find the variable in our list of variables. */
28093139Sru	for (i = 0; info_variables[i].name; i++)
28193139Sru		if (strcmp(info_variables[i].name, name) == 0)
28293139Sru			break;
28393139Sru
28493139Sru	if (!info_variables[i].name)
28593139Sru		return;
28693139Sru
28793139Sru	if (info_variables[i].choices)
28893139Sru	{
28993139Sru		register int j;
29093139Sru
29193139Sru		/* Find the choice in our list of choices. */
29293139Sru		for (j = 0; info_variables[i].choices[j]; j++)
29393139Sru			if (strcmp (info_variables[i].choices[j], value) == 0)
29493139Sru				break;
29593139Sru
29693139Sru		if (info_variables[i].choices[j])
29793139Sru			*info_variables[i].value = j;
29893139Sru	}
29993139Sru	else
30093139Sru	{
30193139Sru		*info_variables[i].value = atoi(value);
30293139Sru	}
30393139Sru}
30493139Sru
30593139Sru#endif /* INFOKEY */
306