1/* m-x.c -- Meta-x minibuffer reader.
2   $Id: m-x.c,v 1.3 2004/04/11 17:56:46 karl Exp $
3
4   Copyright (C) 1993, 1997, 1998, 2001, 2002, 2004 Free Software
5   Foundation, Inc.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21   Originally written by Brian Fox (bfox@ai.mit.edu). */
22
23#include "info.h"
24#include "funs.h"
25
26/* **************************************************************** */
27/*                                                                  */
28/*                     Reading Named Commands                       */
29/*                                                                  */
30/* **************************************************************** */
31
32/* Read the name of an Info function in the echo area and return the
33   name.  A return value of NULL indicates that no function name could
34   be read. */
35char *
36read_function_name (char *prompt, WINDOW *window)
37{
38  register int i;
39  char *line;
40  REFERENCE **array = (REFERENCE **)NULL;
41  int array_index = 0, array_slots = 0;
42
43  /* Make an array of REFERENCE which actually contains the names of
44     the functions available in Info. */
45  for (i = 0; function_doc_array[i].func; i++)
46    {
47      REFERENCE *entry;
48
49      entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
50      entry->label = xstrdup (function_doc_array[i].func_name);
51      entry->nodename = (char *)NULL;
52      entry->filename = (char *)NULL;
53
54      add_pointer_to_array
55        (entry, array_index, array, array_slots, 200, REFERENCE *);
56    }
57
58  line = info_read_completing_in_echo_area (window, prompt, array);
59
60  info_free_references (array);
61
62  if (!echo_area_is_active)
63    window_clear_echo_area ();
64
65  return (line);
66}
67
68DECLARE_INFO_COMMAND (describe_command,
69   _("Read the name of an Info command and describe it"))
70{
71  char *line;
72
73  line = read_function_name ((char *) _("Describe command: "), window);
74
75  if (!line)
76    {
77      info_abort_key (active_window, count, key);
78      return;
79    }
80
81  /* Describe the function named in "LINE". */
82  if (*line)
83    {
84      InfoCommand *cmd = named_function (line);
85
86      if (!cmd)
87        return;
88
89      window_message_in_echo_area ("%s: %s.",
90                                   line, function_documentation (cmd));
91    }
92  free (line);
93}
94
95DECLARE_INFO_COMMAND (info_execute_command,
96   _("Read a command name in the echo area and execute it"))
97{
98  char *line;
99  char *keys;
100  char *prompt;
101
102  prompt = (char *)xmalloc (20);
103
104  keys = where_is (info_keymap, InfoCmd(info_execute_command));
105  /* If the where_is () function thinks that this command doesn't exist,
106     there's something very wrong!  */
107  if (!keys)
108    abort();
109
110  if (info_explicit_arg || count != 1)
111    sprintf (prompt, "%d %s ", count, keys);
112  else
113    sprintf (prompt, "%s ", keys);
114
115  /* Ask the completer to read a reference for us. */
116  line = read_function_name (prompt, window);
117
118  /* User aborted? */
119  if (!line)
120    {
121      info_abort_key (active_window, count, key);
122      return;
123    }
124
125  /* User accepted "default"?  (There is none.) */
126  if (!*line)
127    {
128      free (line);
129      return;
130    }
131
132  /* User wants to execute a named command.  Do it. */
133  {
134    InfoCommand *command;
135
136    if ((active_window != the_echo_area) &&
137        (strncmp (line, "echo-area-", 10) == 0))
138      {
139        free (line);
140        info_error ((char *) _("Cannot execute an `echo-area' command here."),
141            NULL, NULL);
142        return;
143      }
144
145    command = named_function (line);
146    free (line);
147
148    if (!command)
149      return;
150
151    if (InfoFunction(command))
152      (*InfoFunction(command)) (active_window, count, 0);
153    else
154      info_error ((char *) _("Undefined command: %s"), line, NULL);
155  }
156}
157
158/* Okay, now that we have M-x, let the user set the screen height. */
159DECLARE_INFO_COMMAND (set_screen_height,
160  _("Set the height of the displayed window"))
161{
162  int new_height, old_height = screenheight;
163
164  if (info_explicit_arg || count != 1)
165    new_height = count;
166  else
167    {
168      char prompt[80];
169      char *line;
170
171      new_height = screenheight;
172
173      sprintf (prompt, _("Set screen height to (%d): "), new_height);
174
175      line = info_read_in_echo_area (window, prompt);
176
177      /* If the user aborted, do that now. */
178      if (!line)
179        {
180          info_abort_key (active_window, count, 0);
181          return;
182        }
183
184      /* Find out what the new height is supposed to be. */
185      if (*line)
186        new_height = atoi (line);
187
188      /* Clear the echo area if it isn't active. */
189      if (!echo_area_is_active)
190        window_clear_echo_area ();
191
192      free (line);
193    }
194
195  terminal_clear_screen ();
196  display_clear_display (the_display);
197  screenheight = new_height;
198#ifdef SET_SCREEN_SIZE_HELPER
199  SET_SCREEN_SIZE_HELPER;
200#endif
201  if (screenheight == old_height)
202    {
203      /* Display dimensions didn't actually change, so
204	 window_new_screen_size won't do anything, but we've
205	 already cleared the display above.  Undo the damage.  */
206      window_mark_chain (windows, W_UpdateWindow);
207      display_update_display (windows);
208    }
209  else
210    {
211      display_initialize_display (screenwidth, screenheight);
212      window_new_screen_size (screenwidth, screenheight);
213    }
214}
215