xref: /netbsd-src/external/gpl2/texinfo/dist/info/variables.c (revision 29619d2afe564e54d657b83e5a3ae89584f83720)
1*29619d2aSchristos /*	$NetBSD: variables.c,v 1.1.1.1 2016/01/14 00:11:29 christos Exp $	*/
2*29619d2aSchristos 
3*29619d2aSchristos /* variables.c -- how to manipulate user visible variables in Info.
4*29619d2aSchristos    Id: variables.c,v 1.3 2004/04/11 17:56:46 karl Exp
5*29619d2aSchristos 
6*29619d2aSchristos    Copyright (C) 1993, 1997, 2001, 2002, 2004 Free Software Foundation, Inc.
7*29619d2aSchristos 
8*29619d2aSchristos    This program is free software; you can redistribute it and/or modify
9*29619d2aSchristos    it under the terms of the GNU General Public License as published by
10*29619d2aSchristos    the Free Software Foundation; either version 2, or (at your option)
11*29619d2aSchristos    any later version.
12*29619d2aSchristos 
13*29619d2aSchristos    This program is distributed in the hope that it will be useful,
14*29619d2aSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*29619d2aSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*29619d2aSchristos    GNU General Public License for more details.
17*29619d2aSchristos 
18*29619d2aSchristos    You should have received a copy of the GNU General Public License
19*29619d2aSchristos    along with this program; if not, write to the Free Software
20*29619d2aSchristos    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21*29619d2aSchristos 
22*29619d2aSchristos    Written by Brian Fox (bfox@ai.mit.edu). */
23*29619d2aSchristos 
24*29619d2aSchristos #include "info.h"
25*29619d2aSchristos #include "variables.h"
26*29619d2aSchristos 
27*29619d2aSchristos /* **************************************************************** */
28*29619d2aSchristos /*                                                                  */
29*29619d2aSchristos /*                  User Visible Variables in Info                  */
30*29619d2aSchristos /*                                                                  */
31*29619d2aSchristos /* **************************************************************** */
32*29619d2aSchristos 
33*29619d2aSchristos /* Choices used by the completer when reading a zero/non-zero value for
34*29619d2aSchristos    a variable. */
35*29619d2aSchristos static char *on_off_choices[] = { "Off", "On", (char *)NULL };
36*29619d2aSchristos 
37*29619d2aSchristos VARIABLE_ALIST info_variables[] = {
38*29619d2aSchristos   { "automatic-footnotes",
39*29619d2aSchristos       N_("When \"On\", footnotes appear and disappear automatically"),
40*29619d2aSchristos       &auto_footnotes_p, (char **)on_off_choices },
41*29619d2aSchristos 
42*29619d2aSchristos   { "automatic-tiling",
43*29619d2aSchristos       N_("When \"On\", creating or deleting a window resizes other windows"),
44*29619d2aSchristos       &auto_tiling_p, (char **)on_off_choices },
45*29619d2aSchristos 
46*29619d2aSchristos   { "visible-bell",
47*29619d2aSchristos       N_("When \"On\", flash the screen instead of ringing the bell"),
48*29619d2aSchristos       &terminal_use_visible_bell_p, (char **)on_off_choices },
49*29619d2aSchristos 
50*29619d2aSchristos   { "errors-ring-bell",
51*29619d2aSchristos       N_("When \"On\", errors cause the bell to ring"),
52*29619d2aSchristos       &info_error_rings_bell_p, (char **)on_off_choices },
53*29619d2aSchristos 
54*29619d2aSchristos   { "gc-compressed-files",
55*29619d2aSchristos       N_("When \"On\", Info garbage collects files which had to be uncompressed"),
56*29619d2aSchristos       &gc_compressed_files, (char **)on_off_choices },
57*29619d2aSchristos   { "show-index-match",
58*29619d2aSchristos       N_("When \"On\", the portion of the matched search string is highlighted"),
59*29619d2aSchristos       &show_index_match, (char **)on_off_choices },
60*29619d2aSchristos 
61*29619d2aSchristos   { "scroll-behaviour",
62*29619d2aSchristos       N_("Controls what happens when scrolling is requested at the end of a node"),
63*29619d2aSchristos       &info_scroll_behaviour, (char **)info_scroll_choices },
64*29619d2aSchristos 
65*29619d2aSchristos   { "scroll-step",
66*29619d2aSchristos       N_("The number lines to scroll when the cursor moves out of the window"),
67*29619d2aSchristos       &window_scroll_step, (char **)NULL },
68*29619d2aSchristos 
69*29619d2aSchristos   { "ISO-Latin",
70*29619d2aSchristos       N_("When \"On\", Info accepts and displays ISO Latin characters"),
71*29619d2aSchristos       &ISO_Latin_p, (char **)on_off_choices },
72*29619d2aSchristos 
73*29619d2aSchristos   { (char *)NULL, (char *)NULL, (int *)NULL, (char **)NULL }
74*29619d2aSchristos };
75*29619d2aSchristos 
76*29619d2aSchristos DECLARE_INFO_COMMAND (describe_variable, _("Explain the use of a variable"))
77*29619d2aSchristos {
78*29619d2aSchristos   VARIABLE_ALIST *var;
79*29619d2aSchristos   char *description;
80*29619d2aSchristos 
81*29619d2aSchristos   /* Get the variable's name. */
82*29619d2aSchristos   var = read_variable_name ((char *) _("Describe variable: "), window);
83*29619d2aSchristos 
84*29619d2aSchristos   if (!var)
85*29619d2aSchristos     return;
86*29619d2aSchristos 
87*29619d2aSchristos   description = (char *)xmalloc (20 + strlen (var->name)
88*29619d2aSchristos 				 + strlen (_(var->doc)));
89*29619d2aSchristos 
90*29619d2aSchristos   if (var->choices)
91*29619d2aSchristos     sprintf (description, "%s (%s): %s.",
92*29619d2aSchristos              var->name, var->choices[*(var->value)], _(var->doc));
93*29619d2aSchristos   else
94*29619d2aSchristos     sprintf (description, "%s (%d): %s.",
95*29619d2aSchristos 	     var->name, *(var->value), _(var->doc));
96*29619d2aSchristos 
97*29619d2aSchristos   window_message_in_echo_area ("%s", description, NULL);
98*29619d2aSchristos   free (description);
99*29619d2aSchristos }
100*29619d2aSchristos 
101*29619d2aSchristos DECLARE_INFO_COMMAND (set_variable, _("Set the value of an Info variable"))
102*29619d2aSchristos {
103*29619d2aSchristos   VARIABLE_ALIST *var;
104*29619d2aSchristos   char *line;
105*29619d2aSchristos 
106*29619d2aSchristos   /* Get the variable's name and value. */
107*29619d2aSchristos   var = read_variable_name ((char *) _("Set variable: "), window);
108*29619d2aSchristos 
109*29619d2aSchristos   if (!var)
110*29619d2aSchristos     return;
111*29619d2aSchristos 
112*29619d2aSchristos   /* Read a new value for this variable. */
113*29619d2aSchristos   {
114*29619d2aSchristos     char prompt[100];
115*29619d2aSchristos 
116*29619d2aSchristos     if (!var->choices)
117*29619d2aSchristos       {
118*29619d2aSchristos         int potential_value;
119*29619d2aSchristos 
120*29619d2aSchristos         if (info_explicit_arg || count != 1)
121*29619d2aSchristos           potential_value = count;
122*29619d2aSchristos         else
123*29619d2aSchristos           potential_value = *(var->value);
124*29619d2aSchristos 
125*29619d2aSchristos         sprintf (prompt, _("Set %s to value (%d): "),
126*29619d2aSchristos                  var->name, potential_value);
127*29619d2aSchristos         line = info_read_in_echo_area (active_window, prompt);
128*29619d2aSchristos 
129*29619d2aSchristos         /* If no error was printed, clear the echo area. */
130*29619d2aSchristos         if (!info_error_was_printed)
131*29619d2aSchristos           window_clear_echo_area ();
132*29619d2aSchristos 
133*29619d2aSchristos         /* User aborted? */
134*29619d2aSchristos         if (!line)
135*29619d2aSchristos           return;
136*29619d2aSchristos 
137*29619d2aSchristos         /* If the user specified a value, get that, otherwise, we are done. */
138*29619d2aSchristos         canonicalize_whitespace (line);
139*29619d2aSchristos         if (*line)
140*29619d2aSchristos           *(var->value) = atoi (line);
141*29619d2aSchristos         else
142*29619d2aSchristos           *(var->value) = potential_value;
143*29619d2aSchristos 
144*29619d2aSchristos         free (line);
145*29619d2aSchristos       }
146*29619d2aSchristos     else
147*29619d2aSchristos       {
148*29619d2aSchristos         register int i;
149*29619d2aSchristos         REFERENCE **array = (REFERENCE **)NULL;
150*29619d2aSchristos         int array_index = 0;
151*29619d2aSchristos         int array_slots = 0;
152*29619d2aSchristos 
153*29619d2aSchristos         for (i = 0; var->choices[i]; i++)
154*29619d2aSchristos           {
155*29619d2aSchristos             REFERENCE *entry;
156*29619d2aSchristos 
157*29619d2aSchristos             entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
158*29619d2aSchristos             entry->label = xstrdup (var->choices[i]);
159*29619d2aSchristos             entry->nodename = (char *)NULL;
160*29619d2aSchristos             entry->filename = (char *)NULL;
161*29619d2aSchristos 
162*29619d2aSchristos             add_pointer_to_array
163*29619d2aSchristos               (entry, array_index, array, array_slots, 10, REFERENCE *);
164*29619d2aSchristos           }
165*29619d2aSchristos 
166*29619d2aSchristos         sprintf (prompt, _("Set %s to value (%s): "),
167*29619d2aSchristos                  var->name, var->choices[*(var->value)]);
168*29619d2aSchristos 
169*29619d2aSchristos         /* Ask the completer to read a variable value for us. */
170*29619d2aSchristos         line = info_read_completing_in_echo_area (window, prompt, array);
171*29619d2aSchristos 
172*29619d2aSchristos         info_free_references (array);
173*29619d2aSchristos 
174*29619d2aSchristos         if (!echo_area_is_active)
175*29619d2aSchristos           window_clear_echo_area ();
176*29619d2aSchristos 
177*29619d2aSchristos         /* User aborted? */
178*29619d2aSchristos         if (!line)
179*29619d2aSchristos           {
180*29619d2aSchristos             info_abort_key (active_window, 0, 0);
181*29619d2aSchristos             return;
182*29619d2aSchristos           }
183*29619d2aSchristos 
184*29619d2aSchristos         /* User accepted default choice?  If so, no change. */
185*29619d2aSchristos         if (!*line)
186*29619d2aSchristos           {
187*29619d2aSchristos             free (line);
188*29619d2aSchristos             return;
189*29619d2aSchristos           }
190*29619d2aSchristos 
191*29619d2aSchristos         /* Find the choice in our list of choices. */
192*29619d2aSchristos         for (i = 0; var->choices[i]; i++)
193*29619d2aSchristos           if (strcmp (var->choices[i], line) == 0)
194*29619d2aSchristos             break;
195*29619d2aSchristos 
196*29619d2aSchristos         if (var->choices[i])
197*29619d2aSchristos           *(var->value) = i;
198*29619d2aSchristos       }
199*29619d2aSchristos   }
200*29619d2aSchristos }
201*29619d2aSchristos 
202*29619d2aSchristos /* Read the name of an Info variable in the echo area and return the
203*29619d2aSchristos    address of a VARIABLE_ALIST member.  A return value of NULL indicates
204*29619d2aSchristos    that no variable could be read. */
205*29619d2aSchristos VARIABLE_ALIST *
read_variable_name(char * prompt,WINDOW * window)206*29619d2aSchristos read_variable_name (char *prompt, WINDOW *window)
207*29619d2aSchristos {
208*29619d2aSchristos   register int i;
209*29619d2aSchristos   char *line;
210*29619d2aSchristos   REFERENCE **variables;
211*29619d2aSchristos 
212*29619d2aSchristos   /* Get the completion array of variable names. */
213*29619d2aSchristos   variables = make_variable_completions_array ();
214*29619d2aSchristos 
215*29619d2aSchristos   /* Ask the completer to read a variable for us. */
216*29619d2aSchristos   line =
217*29619d2aSchristos     info_read_completing_in_echo_area (window, prompt, variables);
218*29619d2aSchristos 
219*29619d2aSchristos   info_free_references (variables);
220*29619d2aSchristos 
221*29619d2aSchristos   if (!echo_area_is_active)
222*29619d2aSchristos     window_clear_echo_area ();
223*29619d2aSchristos 
224*29619d2aSchristos   /* User aborted? */
225*29619d2aSchristos   if (!line)
226*29619d2aSchristos     {
227*29619d2aSchristos       info_abort_key (active_window, 0, 0);
228*29619d2aSchristos       return ((VARIABLE_ALIST *)NULL);
229*29619d2aSchristos     }
230*29619d2aSchristos 
231*29619d2aSchristos   /* User accepted "default"?  (There is none.) */
232*29619d2aSchristos   if (!*line)
233*29619d2aSchristos     {
234*29619d2aSchristos       free (line);
235*29619d2aSchristos       return ((VARIABLE_ALIST *)NULL);
236*29619d2aSchristos     }
237*29619d2aSchristos 
238*29619d2aSchristos   /* Find the variable in our list of variables. */
239*29619d2aSchristos   for (i = 0; info_variables[i].name; i++)
240*29619d2aSchristos     if (strcmp (info_variables[i].name, line) == 0)
241*29619d2aSchristos       break;
242*29619d2aSchristos 
243*29619d2aSchristos   if (!info_variables[i].name)
244*29619d2aSchristos     return ((VARIABLE_ALIST *)NULL);
245*29619d2aSchristos   else
246*29619d2aSchristos     return (&(info_variables[i]));
247*29619d2aSchristos }
248*29619d2aSchristos 
249*29619d2aSchristos /* Make an array of REFERENCE which actually contains the names of the
250*29619d2aSchristos    variables available in Info. */
251*29619d2aSchristos REFERENCE **
make_variable_completions_array(void)252*29619d2aSchristos make_variable_completions_array (void)
253*29619d2aSchristos {
254*29619d2aSchristos   register int i;
255*29619d2aSchristos   REFERENCE **array = (REFERENCE **)NULL;
256*29619d2aSchristos   int array_index = 0, array_slots = 0;
257*29619d2aSchristos 
258*29619d2aSchristos   for (i = 0; info_variables[i].name; i++)
259*29619d2aSchristos     {
260*29619d2aSchristos       REFERENCE *entry;
261*29619d2aSchristos 
262*29619d2aSchristos       entry = (REFERENCE *) xmalloc (sizeof (REFERENCE));
263*29619d2aSchristos       entry->label = xstrdup (info_variables[i].name);
264*29619d2aSchristos       entry->nodename = (char *)NULL;
265*29619d2aSchristos       entry->filename = (char *)NULL;
266*29619d2aSchristos 
267*29619d2aSchristos       add_pointer_to_array
268*29619d2aSchristos         (entry, array_index, array, array_slots, 200, REFERENCE *);
269*29619d2aSchristos     }
270*29619d2aSchristos 
271*29619d2aSchristos   return (array);
272*29619d2aSchristos }
273*29619d2aSchristos 
274*29619d2aSchristos #if defined(INFOKEY)
275*29619d2aSchristos 
276*29619d2aSchristos void
set_variable_to_value(char * name,char * value)277*29619d2aSchristos set_variable_to_value(char *name, char *value)
278*29619d2aSchristos {
279*29619d2aSchristos 	register int i;
280*29619d2aSchristos 
281*29619d2aSchristos 	/* Find the variable in our list of variables. */
282*29619d2aSchristos 	for (i = 0; info_variables[i].name; i++)
283*29619d2aSchristos 		if (strcmp(info_variables[i].name, name) == 0)
284*29619d2aSchristos 			break;
285*29619d2aSchristos 
286*29619d2aSchristos 	if (!info_variables[i].name)
287*29619d2aSchristos 		return;
288*29619d2aSchristos 
289*29619d2aSchristos 	if (info_variables[i].choices)
290*29619d2aSchristos 	{
291*29619d2aSchristos 		register int j;
292*29619d2aSchristos 
293*29619d2aSchristos 		/* Find the choice in our list of choices. */
294*29619d2aSchristos 		for (j = 0; info_variables[i].choices[j]; j++)
295*29619d2aSchristos 			if (strcmp (info_variables[i].choices[j], value) == 0)
296*29619d2aSchristos 				break;
297*29619d2aSchristos 
298*29619d2aSchristos 		if (info_variables[i].choices[j])
299*29619d2aSchristos 			*info_variables[i].value = j;
300*29619d2aSchristos 	}
301*29619d2aSchristos 	else
302*29619d2aSchristos 	{
303*29619d2aSchristos 		*info_variables[i].value = atoi(value);
304*29619d2aSchristos 	}
305*29619d2aSchristos }
306*29619d2aSchristos 
307*29619d2aSchristos #endif /* INFOKEY */
308