home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / gnuinfo / Source / c / variables < prev    next >
Encoding:
Text File  |  1994-10-01  |  7.3 KB  |  274 lines

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