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