Codebase list texinfo / scrub-obsolete/main info / variables.c
scrub-obsolete/main

Tree @scrub-obsolete/main (Download .tar.gz)

variables.c @scrub-obsolete/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/* variables.c -- how to manipulate user visible variables in Info.

   Copyright 1993-2019 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   Originally written by Brian Fox. */

#include "info.h"
#include "session.h"
#include "echo-area.h"
#include "variables.h"
#include "terminal.h"
#include "display.h"

static VARIABLE_ALIST *read_variable_name (char *prompt, WINDOW *window);

/* **************************************************************** */
/*                                                                  */
/*                  User Visible Variables in Info                  */
/*                                                                  */
/* **************************************************************** */

/* Choices used by the completer when reading a zero/non-zero value for
   a variable. */
static char *on_off_choices[] = { "Off", "On", NULL };

static char *mouse_choices[] = { "Off", "normal-tracking", NULL };

static char *follow_strategy_choices[] = { "remain", "path", NULL };

static char *nodeline_choices[] = { "no", "print", "pointers", NULL };

/* Choices used by the completer when reading a value for the user-visible
   variable "scroll-behaviour". */
static char *info_scroll_choices[] = { "Continuous", "Next Only",
    "Page Only", NULL };

/* Choices for the scroll-last-node variable */
static char *scroll_last_node_choices[] = { "Stop", "Top", NULL };

/* Choices for, and indicator of, rendition. */
static char *rendition_choices[] = { "black", "red", "green", "yellow", "blue",
    "magenta", "cyan", "white", "nocolour", "bgblack", "bgred", "bggreen",
    "bgyellow", "bgblue", "bgmagenta", "bgcyan", "bgwhite", "nobgcolour",
    "underline", "nounderline", "standout", "nostandout", "bold", "regular",
    "blink", "noblink", NULL };

/* Address of this indicates the 'highlight-searches' variable. */
static int *highlight_searches;

/* Note that the 'where_set' field of each element in the array is
   not given and defaults to 0. */
VARIABLE_ALIST info_variables[] = {
  { "automatic-footnotes",
      N_("When \"On\", footnotes appear and disappear automatically"),
      &auto_footnotes_p, (char **)on_off_choices },

  { "automatic-tiling",
      N_("When \"On\", creating or deleting a window resizes other windows"),
      &auto_tiling_p, (char **)on_off_choices },

  { "visible-bell",
      N_("When \"On\", flash the screen instead of ringing the bell"),
      &terminal_use_visible_bell_p, (char **)on_off_choices },

  { "errors-ring-bell",
      N_("When \"On\", errors cause the bell to ring"),
      &info_error_rings_bell_p, (char **)on_off_choices },

  { "gc-compressed-files",
      N_("When \"On\", Info garbage collects files which had to be uncompressed"),
      &gc_compressed_files, (char **)on_off_choices },
  { "show-index-match",
      N_("When \"On\", the portion of the matched search string is highlighted"),
      &show_index_match, (char **)on_off_choices },

  { "scroll-behaviour",
      N_("Controls what happens when scrolling is requested at the end of a node"),
      &info_scroll_behaviour, (char **)info_scroll_choices },

  /* Alternate spelling */
  { "scroll-behavior",
      N_("Same as scroll-behaviour"),
      &info_scroll_behaviour, (char **)info_scroll_choices },

  { "scroll-step",
      N_("The number lines to scroll when the cursor moves out of the window"),
      &window_scroll_step, NULL },

  { "cursor-movement-scrolls",
    N_("Controls whether scroll-behavior affects cursor movement commands"),
    &cursor_movement_scrolls_p, (char **)on_off_choices },
  
  { "ISO-Latin",
      N_("When \"On\", Info accepts and displays ISO Latin characters"),
      &ISO_Latin_p, (char **)on_off_choices },

  { "scroll-last-node",
    N_("What to do when a scrolling command is issued at the end of the "
       "last node"),
    &scroll_last_node, (char**)scroll_last_node_choices },
  
  { "min-search-length",
    N_("Minimal length of a search string"),
    &min_search_length, NULL },

  { "search-skip-screen",
      N_("Skip current window when searching"),
    &search_skip_screen_p, (char **)on_off_choices },

  { "infopath-no-defaults",
      N_("Exclude default directories from file search path"),
    &infopath_no_defaults_p, (char **)on_off_choices },

  { "hide-note-references",
      N_("Hide some Info file syntax in the text of nodes"),
    &preprocess_nodes_p, (char **)on_off_choices },

  { "key-time",
      N_("Length of time in milliseconds to wait for the next byte in a sequence indicating that a key has been pressed"),
    &key_time, NULL },

  { "mouse",
      N_("Method to use to track mouse events"),
    &mouse_protocol, (char **)mouse_choices },

  { "follow-strategy",
      N_("How to follow a cross-reference"),
    &follow_strategy, (char **)follow_strategy_choices },

  { "highlight-searches",
      N_("Highlight search matches"),
    &highlight_searches, (char **)on_off_choices },

  { "link-style",
      N_("Styles for links"),
    &ref_rendition, (char **)rendition_choices },

  { "active-link-style",
      N_("Styles for active links"),
    &hl_ref_rendition, (char **)rendition_choices },

  { "match-style",
      N_("Styles for search matches"),
    &match_rendition, (char **)rendition_choices },

  { "nodeline",
      N_("How to print the information line at the start of a node"),
    &nodeline_print, (char **)nodeline_choices },

  { NULL }
};

static char *
rendition_to_string (RENDITION *rendition)
{
  static char string[8 /* nocolour */ + 1 /* comma */
                     + 10 /* nobgcolour */ + 1
                     + 11 /* nounderline */ + 1
                     + 10 /* nostandout */ + 1
                     + 7 /* regular */ + 1
                     + 7 /* noblink */ + 1];
  unsigned long style;
  static const char* fg[] = { "black", "red", "green", "yellow", "blue",
      "magenta", "cyan", "white" };
  static const char* bg[] = { "bgblack", "bgred", "bggreen", "bgyellow",
      "bgblue", "bgmagenta", "bgcyan", "bgwhite" };

  *string = '\0';

  if (rendition->mask & BLINK_MASK)
    strcat (string, rendition->value & BLINK_MASK ? "blink" : "noblink");
  if (rendition->mask & BOLD_MASK)
    {
      if (*string != '\0')
        strcat (string, ",");
      strcat (string, rendition->value & BOLD_MASK ? "bold" : "nobold");
    }
  if (rendition->mask & STANDOUT_MASK)
    {
      if (*string != '\0')
        strcat (string, ",");
      strcat (string, rendition->value & STANDOUT_MASK ? "standout" : "nostandout");
    }
  if (rendition->mask & UNDERLINE_MASK)
    {
      if (*string != '\0')
        strcat (string, ",");
      strcat (string, rendition->value & UNDERLINE_MASK ? "underline" : "nounderline");
    }
  if (rendition->mask & COLOUR_MASK)
    {
      if (*string != '\0')
        strcat (string, ",");
      style = rendition->value & COLOUR_MASK;
      strcat (string, style >= 8 ? fg[style - 8] : "nocolour");
    }
  if (rendition->mask & BGCOLOUR_MASK)
    {
      if (*string != '\0')
        strcat (string, ",");
      style = (rendition->value & BGCOLOUR_MASK) >> 9;
      strcat (string, style >= 8 ? bg[style - 8] : "nobgcolour");
    }

  return string;
}

DECLARE_INFO_COMMAND (describe_variable, _("Explain the use of a variable"))
{
  VARIABLE_ALIST *var;
  char *description;

  /* Get the variable's name. */
  var = read_variable_name (_("Describe variable: "), window);
  if (!var)
    return;

  if (var->choices)
    asprintf (&description, "%s (%s): %s.",
             var->name,
             var->value == &highlight_searches
             ? on_off_choices[match_rendition.mask != 0]
             : var->choices == (char **) &rendition_choices
             ? rendition_to_string (var->value)
             : var->choices[*(int *)var->value], _(var->doc));
  else
    asprintf (&description, "%s (%d): %s.",
             var->name, *(int *)var->value, _(var->doc));

  window_message_in_echo_area ("%s", description);
  free (description);
}

DECLARE_INFO_COMMAND (set_variable, _("Set the value of an Info variable"))
{
  VARIABLE_ALIST *var;
  char *line;
  char prompt[100];

  /* Get the variable's name and value. */
  var = read_variable_name (_("Set variable: "), window);

  if (!var)
    return;

  /* Read a new value for this variable. */

  if (!var->choices)
    {
      int potential_value;

      if (info_explicit_arg || count != 1)
        potential_value = count;
      else
        potential_value = *(int *)(var->value);

      sprintf (prompt, _("Set %s to value (%d): "),
               var->name, potential_value);
      line = info_read_in_echo_area (prompt);

      /* User aborted? */
      if (!line)
        return;

      /* If the user specified a value, get that, otherwise, we are done. */
      canonicalize_whitespace (line);

      set_variable_to_value (var, line, SET_IN_SESSION);

      free (line);
    }
  else
    {
      register int i;
      REFERENCE **array = NULL;
      size_t array_index = 0;
      size_t array_slots = 0;

      for (i = 0; var->choices[i]; i++)
        {
          REFERENCE *entry;

          entry = xmalloc (sizeof (REFERENCE));
          entry->label = xstrdup (var->choices[i]);
          entry->nodename = NULL;
          entry->filename = NULL;

          add_pointer_to_array (entry, array_index, array, array_slots, 10);
        }

      sprintf (prompt, _("Set %s to value (%s): "),
               var->name,
               var->value == &highlight_searches
               ? on_off_choices[match_rendition.mask != 0]
               : var->choices == (char **) &rendition_choices
               ? rendition_to_string (var->value)
               : var->choices[*(int *)(var->value)]);

      /* Ask the completer to read a variable value for us. */
      if (var->choices == (char **) &rendition_choices)
        line = info_read_maybe_completing (prompt, array);
      else
        line = info_read_completing_in_echo_area (prompt, array);

      info_free_references (array);

      /* User aborted? */
      if (!line)
        {
          info_abort_key (active_window, 0);
          return;
        }

      /* User accepted default choice?  If so, no change. */
      if (!*line)
        {
          free (line);
          return;
        }

      set_variable_to_value (var, line, SET_IN_SESSION);
    }
}

VARIABLE_ALIST *
variable_by_name (char *name)
{
  int i;

  /* Find the variable in our list of variables. */
  for (i = 0; info_variables[i].name; i++)
    if (strcmp (info_variables[i].name, name) == 0)
      break;

  if (!info_variables[i].name)
    return NULL;
  else
    return &info_variables[i];
}

/* Read the name of an Info variable in the echo area and return the
   address of a VARIABLE_ALIST member.  A return value of NULL indicates
   that no variable could be read. */
static VARIABLE_ALIST *
read_variable_name (char *prompt, WINDOW *window)
{
  char *line;
  REFERENCE **variables;

  /* Get the completion array of variable names. */
  variables = make_variable_completions_array ();

  /* Ask the completer to read a variable for us. */
  line = info_read_completing_in_echo_area (prompt, variables);

  info_free_references (variables);

  /* User aborted? */
  if (!line)
    {
      info_abort_key (active_window, 0);
      return NULL;
    }

  /* User accepted "default"?  (There is none.) */
  if (!*line)
    {
      free (line);
      return NULL;
    }

  return variable_by_name (line);
}

/* Make an array of REFERENCE which actually contains the names of the
   variables available in Info. */
REFERENCE **
make_variable_completions_array (void)
{
  register int i;
  REFERENCE **array = NULL;
  size_t array_index = 0, array_slots = 0;

  for (i = 0; info_variables[i].name; i++)
    {
      REFERENCE *entry;

      entry = xmalloc (sizeof (REFERENCE));
      entry->label = xstrdup (info_variables[i].name);
      entry->nodename = NULL;
      entry->filename = NULL;

      add_pointer_to_array (entry, array_index, array, array_slots, 200);
    }

  return array;
}

/* VALUE is a string that is the value of the variable specified
   by the user.  Update our internal data structure VAR using this
   information. */
int
set_variable_to_value (VARIABLE_ALIST *var, char *value, int where)
{
  /* If variable was set elsewhere with a higher priority, don't do
     anything, but don't indicate an error. */
  if (var->where_set > where)
    return 1;

  if (var->choices)
    {
      register int j;

      /* "highlight-searches=On" is equivalent to
         "match-rendition=standout". */
      if (var->value == &highlight_searches)
        {
          if (strcmp (on_off_choices[0], value) == 0)
            {
              match_rendition.mask = 0;
              match_rendition.value = 0;
            }
          else
            {
              match_rendition.mask = STANDOUT_MASK;
              match_rendition.value = STANDOUT_MASK;
            }
        }
      else if (var->choices != (char **) &rendition_choices)
        {
          /* Find the choice in our list of choices. */
          for (j = 0; var->choices[j]; j++)
            if (strcmp (var->choices[j], value) == 0)
              {
                *(int *)var->value = j;
                var->where_set = where;
                return 1;
              }
        }
      else
        {
          static struct {
              unsigned long mask;
              unsigned long value;
              char *name;
          } styles[] = {
              COLOUR_MASK, COLOUR_BLACK,   "black",
              COLOUR_MASK, COLOUR_RED,     "red",
              COLOUR_MASK, COLOUR_GREEN,   "green",
              COLOUR_MASK, COLOUR_YELLOW,  "yellow",
              COLOUR_MASK, COLOUR_BLUE,    "blue",
              COLOUR_MASK, COLOUR_MAGENTA, "magenta",
              COLOUR_MASK, COLOUR_CYAN,    "cyan",
              COLOUR_MASK, COLOUR_WHITE,   "white",
              COLOUR_MASK, 0,           "nocolour",
              COLOUR_MASK, 0,           "nocolor",
              BGCOLOUR_MASK, BGCOLOUR_BLACK,   "bgblack",
              BGCOLOUR_MASK, BGCOLOUR_RED,     "bgred",
              BGCOLOUR_MASK, BGCOLOUR_GREEN,   "bggreen",
              BGCOLOUR_MASK, BGCOLOUR_YELLOW,  "bgyellow",
              BGCOLOUR_MASK, BGCOLOUR_BLUE,    "bgblue",
              BGCOLOUR_MASK, BGCOLOUR_MAGENTA, "bgmagenta",
              BGCOLOUR_MASK, BGCOLOUR_CYAN,    "bgcyan",
              BGCOLOUR_MASK, BGCOLOUR_WHITE,   "bgwhite",
              BGCOLOUR_MASK, 0,           "nobgcolour",
              BGCOLOUR_MASK, 0,           "nobgcolor",
              UNDERLINE_MASK, UNDERLINE_MASK, "underline",
              UNDERLINE_MASK, 0,              "nounderline",
              STANDOUT_MASK, STANDOUT_MASK, "standout",
              STANDOUT_MASK, 0,             "nostandout",
              BOLD_MASK, BOLD_MASK,         "bold",
              BOLD_MASK, 0,                 "regular",
              BOLD_MASK, 0,                 "nobold",
              BLINK_MASK, BLINK_MASK,       "blink",
              BLINK_MASK, 0,                "noblink",
          };
          int i;
          char *component;
          unsigned long rendition_mask = 0;
          unsigned long rendition_value = 0;

          component = strtok (value, ",");
          while (component)
            {
              for (i = 0; (styles[i].name); i++)
                {
                  if (!strcmp (styles[i].name, component))
                    break;
                }
              if (styles[i].name)
                {
                  rendition_mask |= styles[i].mask;
                  rendition_value &= ~styles[i].mask;
                  rendition_value |= styles[i].value;
                }
              /* If not found, silently ignore, in case more options are
                 added in the future. */

              component = strtok (0, ",");
            }

          /* Now all the specified styles are recorded in rendition_value. */
          ((RENDITION *)var->value)->mask = rendition_mask;
          ((RENDITION *)var->value)->value = rendition_value;
        }
      return 1;
    }
  else
    {
      char *p;
      long n = strtol (value, &p, 10);
      if (*p == 0 && INT_MIN <= n && n <= INT_MAX)
	{
          *(int *)var->value = n;
	  return 1;
	}
    }

  return 0;
}