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