summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/texinfo/info/variables.c
blob: 7798701c124eb9327b2159014a24820ece4a009f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* variables.c -- How to manipulate user visible variables in Info. */

/* This file is part of GNU Info, a program for reading online documentation
   stored in Info format.

   Copyright (C) 1993 Free Software Foundation, Inc.

   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, 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
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   Written by Brian Fox (bfox@ai.mit.edu). */

#include "info.h"
#include "variables.h"

/* **************************************************************** */
/*								    */
/*		    User Visible Variables in Info		    */
/*								    */
/* **************************************************************** */

/* Choices used by the completer when reading a zero/non-zero value for
   a variable. */
static char *on_off_choices[] = { "Off", "On", (char *)NULL };

VARIABLE_ALIST info_variables[] = {
  { "automatic-footnotes",
      "When \"On\", footnotes appear and disappear automatically",
      &auto_footnotes_p, (char **)on_off_choices },

  { "automatic-tiling",
      "When \"On\", creating or deleting a window resizes other windows",
      &auto_tiling_p, (char **)on_off_choices },

  { "visible-bell",
      "When \"On\", flash the screen instead of ringing the bell",
      &terminal_use_visible_bell_p, (char **)on_off_choices },

  { "errors-ring-bell",
      "When \"On\", errors cause the bell to ring",
      &info_error_rings_bell_p, (char **)on_off_choices },

  { "gc-compressed-files",
      "When \"On\", Info garbage collects files which had to be uncompressed",
      &gc_compressed_files, (char **)on_off_choices },
  { "show-index-match",
      "When \"On\", the portion of the matched search string is highlighted",
      &show_index_match, (char **)on_off_choices },

  { "scroll-behaviour",
      "Controls what happens when scrolling is requested at the end of a node",
      &info_scroll_behaviour, (char **)info_scroll_choices },

  { "scroll-step",
      "The number lines to scroll when the cursor moves out of the window",
      &window_scroll_step, (char **)NULL },

  { "ISO-Latin",
      "When \"On\", Info accepts and displays ISO Latin characters",
      &ISO_Latin_p, (char **)on_off_choices },

  { (char *)NULL, (char *)NULL, (int *)NULL, (char **)NULL }
};

DECLARE_INFO_COMMAND (describe_variable, "Explain the use of a variable")
{
  VARIABLE_ALIST *var;
  char *description;

  /* Get the variable's name. */
  var = read_variable_name ("Describe variable: ", window);

  if (!var)
    return;

  description = (char *)xmalloc (20 + strlen (var->name) + strlen (var->doc));

  if (var->choices)
    sprintf (description, "%s (%s): %s.",
	     var->name, var->choices[*(var->value)], var->doc);
  else
    sprintf (description, "%s (%d): %s.", var->name, *(var->value), var->doc);

  window_message_in_echo_area ("%s", description);
  free (description);
}

DECLARE_INFO_COMMAND (set_variable, "Set the value of an Info variable")
{
  VARIABLE_ALIST *var;
  char *line;

  /* Get the variable's name and value. */
  var = read_variable_name ("Set variable: ", window);

  if (!var)
    return;

  /* Read a new value for this variable. */
  {
    char prompt[100];

    if (!var->choices)
      {
	int potential_value;

	if (info_explicit_arg || count != 1)
	  potential_value = count;
	else
	  potential_value = *(var->value);

	sprintf (prompt, "Set %s to value (%d): ",
		 var->name, potential_value);
	line = info_read_in_echo_area (active_window, prompt);

	/* If no error was printed, clear the echo area. */
	if (!info_error_was_printed)
	  window_clear_echo_area ();

	/* User aborted? */
	if (!line)
	  return;

	/* If the user specified a value, get that, otherwise, we are done. */
	canonicalize_whitespace (line);
	if (*line)
	  *(var->value) = atoi (line);
	else
	  *(var->value) = potential_value;

	free (line);
      }
    else
      {
	register int i;
	REFERENCE **array = (REFERENCE **)NULL;
	int array_index = 0;
	int array_slots = 0;

	for (i = 0; var->choices[i]; i++)
	  {
	    REFERENCE *entry;

	    entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
	    entry->label = strdup (var->choices[i]);
	    entry->nodename = (char *)NULL;
	    entry->filename = (char *)NULL;

	    add_pointer_to_array
	      (entry, array_index, array, array_slots, 10, REFERENCE *);
	  }

	sprintf (prompt, "Set %s to value (%s): ",
		 var->name, var->choices[*(var->value)]);

	/* Ask the completer to read a variable value for us. */
	line = info_read_completing_in_echo_area (window, prompt, array);

	info_free_references (array);

	if (!echo_area_is_active)
	  window_clear_echo_area ();

	/* User aborted? */
	if (!line)
	  {
	    info_abort_key (active_window, 0, 0);
	    return;
	  }

	/* User accepted default choice?  If so, no change. */
	if (!*line)
	  {
	    free (line);
	    return;
	  }

	/* Find the choice in our list of choices. */
	for (i = 0; var->choices[i]; i++)
	  if (strcmp (var->choices[i], line) == 0)
	    break;

	if (var->choices[i])
	  *(var->value) = i;
      }
  }
}

/* Read the name of an Info variable in the echo area and return the
   address of a VARIABLE_ALIST member.  A return value of NULL indicates
   that no variable could be read. */
VARIABLE_ALIST *
read_variable_name (prompt, window)
     char *prompt;
     WINDOW *window;
{
  register int i;
  char *line;
  REFERENCE **variables;

  /* Get the completion array of variable names. */
  variables = make_variable_completions_array ();

  /* Ask the completer to read a variable for us. */
  line =
    info_read_completing_in_echo_area (window, prompt, variables);

  info_free_references (variables);

  if (!echo_area_is_active)
    window_clear_echo_area ();

  /* User aborted? */
  if (!line)
    {
      info_abort_key (active_window, 0, 0);
      return ((VARIABLE_ALIST *)NULL);
    }

  /* User accepted "default"?  (There is none.) */
  if (!*line)
    {
      free (line);
      return ((VARIABLE_ALIST *)NULL);
    }

  /* Find the variable in our list of variables. */
  for (i = 0; info_variables[i].name; i++)
    if (strcmp (info_variables[i].name, line) == 0)
      break;

  if (!info_variables[i].name)
    return ((VARIABLE_ALIST *)NULL);
  else
    return (&(info_variables[i]));
}

/* Make an array of REFERENCE which actually contains the names of the
   variables available in Info. */
REFERENCE **
make_variable_completions_array ()
{
  register int i;
  REFERENCE **array = (REFERENCE **)NULL;
  int array_index = 0, array_slots = 0;

  for (i = 0; info_variables[i].name; i++)
    {
      REFERENCE *entry;

      entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
      entry->label = strdup (info_variables[i].name);
      entry->nodename = (char *)NULL;
      entry->filename = (char *)NULL;

      add_pointer_to_array
	(entry, array_index, array, array_slots, 200, REFERENCE *);
    }

  return (array);
}