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