Back to index

tetex-bin  3.0
variables.c
Go to the documentation of this file.
00001 /* variables.c -- how to manipulate user visible variables in Info.
00002    $Id: variables.c,v 1.3 2004/04/11 17:56:46 karl Exp $
00003 
00004    Copyright (C) 1993, 1997, 2001, 2002, 2004 Free Software Foundation, Inc.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2, or (at your option)
00009    any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019 
00020    Written by Brian Fox (bfox@ai.mit.edu). */
00021 
00022 #include "info.h"
00023 #include "variables.h"
00024 
00025 /* **************************************************************** */
00026 /*                                                                  */
00027 /*                  User Visible Variables in Info                  */
00028 /*                                                                  */
00029 /* **************************************************************** */
00030 
00031 /* Choices used by the completer when reading a zero/non-zero value for
00032    a variable. */
00033 static char *on_off_choices[] = { "Off", "On", (char *)NULL };
00034 
00035 VARIABLE_ALIST info_variables[] = {
00036   { "automatic-footnotes",
00037       N_("When \"On\", footnotes appear and disappear automatically"),
00038       &auto_footnotes_p, (char **)on_off_choices },
00039 
00040   { "automatic-tiling",
00041       N_("When \"On\", creating or deleting a window resizes other windows"),
00042       &auto_tiling_p, (char **)on_off_choices },
00043 
00044   { "visible-bell",
00045       N_("When \"On\", flash the screen instead of ringing the bell"),
00046       &terminal_use_visible_bell_p, (char **)on_off_choices },
00047 
00048   { "errors-ring-bell",
00049       N_("When \"On\", errors cause the bell to ring"),
00050       &info_error_rings_bell_p, (char **)on_off_choices },
00051 
00052   { "gc-compressed-files",
00053       N_("When \"On\", Info garbage collects files which had to be uncompressed"),
00054       &gc_compressed_files, (char **)on_off_choices },
00055   { "show-index-match",
00056       N_("When \"On\", the portion of the matched search string is highlighted"),
00057       &show_index_match, (char **)on_off_choices },
00058 
00059   { "scroll-behaviour",
00060       N_("Controls what happens when scrolling is requested at the end of a node"),
00061       &info_scroll_behaviour, (char **)info_scroll_choices },
00062 
00063   { "scroll-step",
00064       N_("The number lines to scroll when the cursor moves out of the window"),
00065       &window_scroll_step, (char **)NULL },
00066 
00067   { "ISO-Latin",
00068       N_("When \"On\", Info accepts and displays ISO Latin characters"),
00069       &ISO_Latin_p, (char **)on_off_choices },
00070 
00071   { (char *)NULL, (char *)NULL, (int *)NULL, (char **)NULL }
00072 };
00073 
00074 DECLARE_INFO_COMMAND (describe_variable, _("Explain the use of a variable"))
00075 {
00076   VARIABLE_ALIST *var;
00077   char *description;
00078 
00079   /* Get the variable's name. */
00080   var = read_variable_name ((char *) _("Describe variable: "), window);
00081 
00082   if (!var)
00083     return;
00084 
00085   description = (char *)xmalloc (20 + strlen (var->name)
00086                              + strlen (_(var->doc)));
00087 
00088   if (var->choices)
00089     sprintf (description, "%s (%s): %s.",
00090              var->name, var->choices[*(var->value)], _(var->doc));
00091   else
00092     sprintf (description, "%s (%d): %s.",
00093             var->name, *(var->value), _(var->doc));
00094 
00095   window_message_in_echo_area ("%s", description, NULL);
00096   free (description);
00097 }
00098 
00099 DECLARE_INFO_COMMAND (set_variable, _("Set the value of an Info variable"))
00100 {
00101   VARIABLE_ALIST *var;
00102   char *line;
00103 
00104   /* Get the variable's name and value. */
00105   var = read_variable_name ((char *) _("Set variable: "), window);
00106 
00107   if (!var)
00108     return;
00109 
00110   /* Read a new value for this variable. */
00111   {
00112     char prompt[100];
00113 
00114     if (!var->choices)
00115       {
00116         int potential_value;
00117 
00118         if (info_explicit_arg || count != 1)
00119           potential_value = count;
00120         else
00121           potential_value = *(var->value);
00122 
00123         sprintf (prompt, _("Set %s to value (%d): "),
00124                  var->name, potential_value);
00125         line = info_read_in_echo_area (active_window, prompt);
00126 
00127         /* If no error was printed, clear the echo area. */
00128         if (!info_error_was_printed)
00129           window_clear_echo_area ();
00130 
00131         /* User aborted? */
00132         if (!line)
00133           return;
00134 
00135         /* If the user specified a value, get that, otherwise, we are done. */
00136         canonicalize_whitespace (line);
00137         if (*line)
00138           *(var->value) = atoi (line);
00139         else
00140           *(var->value) = potential_value;
00141 
00142         free (line);
00143       }
00144     else
00145       {
00146         register int i;
00147         REFERENCE **array = (REFERENCE **)NULL;
00148         int array_index = 0;
00149         int array_slots = 0;
00150 
00151         for (i = 0; var->choices[i]; i++)
00152           {
00153             REFERENCE *entry;
00154 
00155             entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
00156             entry->label = xstrdup (var->choices[i]);
00157             entry->nodename = (char *)NULL;
00158             entry->filename = (char *)NULL;
00159 
00160             add_pointer_to_array
00161               (entry, array_index, array, array_slots, 10, REFERENCE *);
00162           }
00163 
00164         sprintf (prompt, _("Set %s to value (%s): "),
00165                  var->name, var->choices[*(var->value)]);
00166 
00167         /* Ask the completer to read a variable value for us. */
00168         line = info_read_completing_in_echo_area (window, prompt, array);
00169 
00170         info_free_references (array);
00171 
00172         if (!echo_area_is_active)
00173           window_clear_echo_area ();
00174 
00175         /* User aborted? */
00176         if (!line)
00177           {
00178             info_abort_key (active_window, 0, 0);
00179             return;
00180           }
00181 
00182         /* User accepted default choice?  If so, no change. */
00183         if (!*line)
00184           {
00185             free (line);
00186             return;
00187           }
00188 
00189         /* Find the choice in our list of choices. */
00190         for (i = 0; var->choices[i]; i++)
00191           if (strcmp (var->choices[i], line) == 0)
00192             break;
00193 
00194         if (var->choices[i])
00195           *(var->value) = i;
00196       }
00197   }
00198 }
00199 
00200 /* Read the name of an Info variable in the echo area and return the
00201    address of a VARIABLE_ALIST member.  A return value of NULL indicates
00202    that no variable could be read. */
00203 VARIABLE_ALIST *
00204 read_variable_name (char *prompt, WINDOW *window)
00205 {
00206   register int i;
00207   char *line;
00208   REFERENCE **variables;
00209 
00210   /* Get the completion array of variable names. */
00211   variables = make_variable_completions_array ();
00212 
00213   /* Ask the completer to read a variable for us. */
00214   line =
00215     info_read_completing_in_echo_area (window, prompt, variables);
00216 
00217   info_free_references (variables);
00218 
00219   if (!echo_area_is_active)
00220     window_clear_echo_area ();
00221 
00222   /* User aborted? */
00223   if (!line)
00224     {
00225       info_abort_key (active_window, 0, 0);
00226       return ((VARIABLE_ALIST *)NULL);
00227     }
00228 
00229   /* User accepted "default"?  (There is none.) */
00230   if (!*line)
00231     {
00232       free (line);
00233       return ((VARIABLE_ALIST *)NULL);
00234     }
00235 
00236   /* Find the variable in our list of variables. */
00237   for (i = 0; info_variables[i].name; i++)
00238     if (strcmp (info_variables[i].name, line) == 0)
00239       break;
00240 
00241   if (!info_variables[i].name)
00242     return ((VARIABLE_ALIST *)NULL);
00243   else
00244     return (&(info_variables[i]));
00245 }
00246 
00247 /* Make an array of REFERENCE which actually contains the names of the
00248    variables available in Info. */
00249 REFERENCE **
00250 make_variable_completions_array (void)
00251 {
00252   register int i;
00253   REFERENCE **array = (REFERENCE **)NULL;
00254   int array_index = 0, array_slots = 0;
00255 
00256   for (i = 0; info_variables[i].name; i++)
00257     {
00258       REFERENCE *entry;
00259 
00260       entry = (REFERENCE *) xmalloc (sizeof (REFERENCE));
00261       entry->label = xstrdup (info_variables[i].name);
00262       entry->nodename = (char *)NULL;
00263       entry->filename = (char *)NULL;
00264 
00265       add_pointer_to_array
00266         (entry, array_index, array, array_slots, 200, REFERENCE *);
00267     }
00268 
00269   return (array);
00270 }
00271 
00272 #if defined(INFOKEY)
00273 
00274 void
00275 set_variable_to_value(char *name, char *value)
00276 {
00277        register int i;
00278 
00279        /* Find the variable in our list of variables. */
00280        for (i = 0; info_variables[i].name; i++)
00281               if (strcmp(info_variables[i].name, name) == 0)
00282                      break;
00283 
00284        if (!info_variables[i].name)
00285               return;
00286 
00287        if (info_variables[i].choices)
00288        {
00289               register int j;
00290 
00291               /* Find the choice in our list of choices. */
00292               for (j = 0; info_variables[i].choices[j]; j++)
00293                      if (strcmp (info_variables[i].choices[j], value) == 0)
00294                             break;
00295 
00296               if (info_variables[i].choices[j])
00297                      *info_variables[i].value = j;
00298        }
00299        else
00300        {
00301               *info_variables[i].value = atoi(value);
00302        }
00303 }
00304 
00305 #endif /* INFOKEY */