1 #ifndef CMDH
2 #define CMDH
3 
4 /*
5  * $Id: cmd.h,v 1.9 2001/02/14 19:22:57 danny Exp $
6  *
7  * Copyright � 1992, 1993, 2001 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 software; see the file COPYING.  If not, write to
21  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 
24 /*
25  * This file explains the generic interface to interactive functions.
26  * This covers how C functions are made available to the user, how
27  * keymaps are structured.  This also describes the variables that
28  * hold the user's interaction state .
29  */
30 #include "global.h"
31 #include "obstack.h"
32 #include "line.h"
33 #include "key.h"
34 #include "args.h"
35 #include "funcs.h"
36 #include "info.h"
37 
38 typedef void (*alarm_fn)(void);
39 
40 struct alarm_entry
41 {
42   alarm_fn fn;
43   int freq;			/* in seconds. */
44   time_t last_time;
45 };
46 
47 #define	SPECIAL_CODE_A	0x80 + 'a'
48 #define	SPECIAL_CODE_B	0x80 + 'b'
49 #define	SPECIAL_CODE_C	0x80 + 'c'
50 
51 extern struct alarm_entry alarm_table[];
52 
53 #define cell_timer_seconds  (alarm_table[0].freq)
54 
55 /* Fields prefixed by _ should normally be accessed via the macros
56  * defined further on.
57  */
58 
59 struct command_frame;
60 struct macro;
61 
62 struct input_stream
63 {
64   /* The currently executing macro. */
65   struct macro *_rmac;
66 
67   unsigned char * _last_macro;		/* The last anonymous macro. */
68 
69   /* If a macro is being exectuted, arguments to a command
70    * are read from this string.
71    */
72   char *_func_arg;
73 
74   /* Call stack for macros. */
75   struct obstack _macro_stack;
76 
77   /* The macro being recorded, if any. */
78   unsigned char *_macro;
79   unsigned char *_macro_start;
80   unsigned int _macro_size;
81 
82   /* If this input stream was created only to execute a macro,
83    * this will point to the input_stream it suspended.
84    * The purpose of this stack is to give command_loop the ability to
85    * execute exactly one macro and then return.
86    *
87    * Note that within an input stream there is another macro stack.
88    * That stack is used internally to command_loop.
89    */
90   struct input_stream * prev_stream;
91 
92   int _pushed_back_char;
93 };
94 
95 struct macro
96 {
97   struct macro *mac_prev;
98   unsigned char *mac_exe;
99   CELLREF mac_row, mac_col;
100   struct rng mac_rng;
101 
102   int count;			/* Repeat count for this macro. */
103   unsigned char * mac_start;	/* Beginning the current cell's string (as */
104 				/* copied to the macro stack). */
105 };
106 
107 /* When a key is bound to a range, that range is stored here and
108  * the CODE field of the binding is an index.  This is bogus.
109  * Variables should be used.
110  */
111 extern int n_bound_macros;
112 extern struct rng *bound_macros;
113 extern int bound_macro_vec;
114 
115 /* The pattern of interaction is:
116  *   the user selects an interactive function
117  *   a list of arguments to that function are assembled
118  *   the function is called
119  *
120  * This type is a union of the types that arguments to interactive
121  * functions can have.
122  */
123 union command_arg_val
124 {
125   char character;
126   FILE * fp;
127   int integer;
128   double floating;
129   struct key_sequence key;	/* Passed as (struct keyseq *). */
130   struct rng range;		/* Passed as (struct rng *). */
131   char * string;
132 };
133 
134 
135 #ifdef __STDC__
136 typedef void (*direction_function) (int magic, int repeat);
137 #else
138 typedef void (*direction_function) ();
139 #endif
140 
141 
142 struct command_arg
143 {
144   int do_prompt;		/* If true, the user gets to edit this. */
145   int is_set;			/* If true, a valid value is stored here. */
146   struct prompt_style * style;	/* The editting mode for this argument. */
147   char * arg_desc;		/* Pointer into FUNC_ARGS of CUR_CMD. */
148   char * prompt;		/* Unexpanded prompt */
149   char * expanded_prompt;
150 
151   struct info_buffer * prompt_info;/* Info that should be displayed while */
152 				/* prompting for this argument. */
153   int info_line;		/* First line visible in prompt_info */
154 
155   struct line text;		/* A buffer for the user to edit this value. */
156   int cursor;			/* cursor position of this buffer. */
157   int overwrite;		/* Is overwrite mode on? */
158 
159   /* For incremental commands. */
160   direction_function inc_cmd;
161 
162   /* For reading a character with timeout. */
163   int timeout_seconds;
164 
165   /* The value as it will be passed to the cmd function. */
166   union command_arg_val val;
167 };
168 
169 #define MAX_COMMAND_ARGS	10
170 
171 /* These declarations make up the state of the command interpreter. */
172 
173 struct command_frame
174 {
175   /* If `recursive' edits are enabled, there can be more than one of these. */
176   struct command_frame * next;
177   struct command_frame * prev;
178 
179   struct input_stream * input;
180 
181   /* The cell being editted (if any). */
182   CELLREF _setrow;
183   CELLREF _setcol;
184 
185   /* The current cell and the mark. */
186   long	buf1;
187   CELLREF _curow;
188   long	buf2;
189   CELLREF _cucol;
190   long	buf3;
191   CELLREF _mkrow;
192   long	buf4;
193   CELLREF _mkcol;
194   long	buf5;
195 
196   /* What passes for a window configuration, for now. */
197   /*
198    * When the input area is active, it appears to be just another window,
199    * reachable by other-window.  These values must be maintained by any
200    * implementation of io_get_line.
201    */
202   int _window_after_input;	/* Id of the window prior to the input area. */
203   int _input_active;	/* Bool: is the input area selected? */
204 
205 
206   /* The current top level keymap. */
207   int top_keymap;
208 
209   /* Current position in the keymaps. */
210   int _cur_keymap;
211 
212   int saved_cur_keymap;		/* used when building a prefix arg */
213 
214   /* The about-to-begin executing command (if any). */
215   struct cmd_func *_cur_cmd;
216   short _cur_vector;
217 
218   /* The last character processed .*/
219   int _cur_chr;
220 
221   /* The prefix argument */
222   int _how_many;
223   struct line _raw_prefix;
224 
225   /* This becomes true if the user is ever prompted for arguments
226    * for this frame.
227    */
228   int complex_to_user;
229 
230   int _cmd_argc;
231   int _cur_arg;
232   struct cmd_func * cmd;
233 
234   /* The arguments to the current function.
235    * This is used only if the current function prompts for arguments.
236    */
237   struct command_arg argv[MAX_COMMAND_ARGS];
238 };
239 
240 /* When a command is executing, this points to the frame it should operate
241  * on:
242  */
243 
244 extern struct command_frame * the_cmd_frame;
245 extern struct command_frame * running_frames;
246 
247 /* For most code, the structure of command loops and input streams
248  * is unimportant.  To that code, we make it appear that there is just
249  * a set of global variables.
250  */
251 
252 #define setrow			the_cmd_frame->_setrow
253 #define setcol			the_cmd_frame->_setcol
254 #define curow			the_cmd_frame->_curow
255 #define cucol			the_cmd_frame->_cucol
256 #define mkrow			the_cmd_frame->_mkrow
257 #define mkcol			the_cmd_frame->_mkcol
258 
259 #define window_after_input	the_cmd_frame->_window_after_input
260 #define input_active		the_cmd_frame->_input_active
261 
262 #define cur_keymap		the_cmd_frame->_cur_keymap
263 #define cur_cmd			the_cmd_frame->_cur_cmd
264 #define cur_vector		the_cmd_frame->_cur_vector
265 #define cur_chr			the_cmd_frame->_cur_chr
266 #define cur_arg			the_cmd_frame->_cur_arg
267 #define cmd_argc		the_cmd_frame->_cmd_argc
268 
269 #define how_many		the_cmd_frame->_how_many
270 #define raw_prefix		the_cmd_frame->_raw_prefix
271 
272 #define cur_input		the_cmd_frame->input
273 #define rmac			cur_input->_rmac
274 #define pushed_back_char	cur_input->_pushed_back_char
275 #define last_macro		cur_input->_last_macro
276 #define macro_func_arg		cur_input->_func_arg
277 #define macro_stack		cur_input->_macro_stack
278 #define making_macro		cur_input->_macro
279 #define making_macro_start	cur_input->_macro_start
280 #define making_macro_size	cur_input->_macro_size
281 
282 #define the_cmd_arg  		the_cmd_frame->argv[cur_arg]
283 
284 
285 
286 #ifdef FD_SET
287 
288 #define SELECT_TYPE fd_set
289 #define SELECT_SET_SIZE FD_SETSIZE
290 
291 #else /* no FD_SET */
292 
293 /* Define the macros to access a single-int bitmap of descriptors.  */
294 #define SELECT_SET_SIZE 32
295 #define SELECT_TYPE int
296 #define FD_SET(n, p) (*(p) |= (1 << (n)))
297 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
298 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
299 #define FD_ZERO(p) (*(p) = 0)
300 
301 #endif /* no FD_SET */
302 
303 /* The fd's that are selected on in the interact loop. */
304 extern SELECT_TYPE read_fd_set;
305 extern SELECT_TYPE exception_fd_set;
306 extern SELECT_TYPE write_fd_set;
307 extern SELECT_TYPE read_pending_fd_set; /* These are the output of select. */
308 extern SELECT_TYPE exception_pending_fd_set;
309 extern SELECT_TYPE write_pending_fd_set;
310 
311 #ifdef __STDC__
312 typedef void (*select_hook_fn) (int fd);
313 #else
314 typedef void (*select_hook_fn) ();
315 #endif
316 
317 struct select_hook
318 {
319   select_hook_fn hook_fn;
320   void * jrandom;
321 };
322 
323 extern struct select_hook file_read_hooks[SELECT_SET_SIZE];
324 extern struct select_hook file_exception_hooks[SELECT_SET_SIZE];
325 extern struct select_hook file_write_hooks[SELECT_SET_SIZE];
326 
327 
328 extern void free_input_stream (struct input_stream * stream);
329 extern void pop_input_stream (void);
330 extern void start_entering_macro (void);
331 extern void bound_macro (int num);
332 extern void run_string_as_macro (char * macro);
333 extern void call_last_kbd_macro (int count);
334 extern void end_macro (void);
335 extern void stop_entering_macro (void);
336 extern void store_last_macro (struct rng * rng);
337 extern int real_get_chr (void);
338 extern void block_until_excitement(struct timeval *tv);
339 extern void push_command_frame (struct rng * rng, char * first_line, int len);
340 extern void remove_cmd_frame (struct command_frame * frame);
341 extern void free_cmd_frame (struct command_frame * frame);
342 extern void pop_unfinished_command (void);
343 extern void recover_from_error (void);
344 extern void exit_minibuffer (void);
345 extern void setn_arg_text (struct command_arg * arg, char * text, int len);
346 extern void init_arg_text (struct command_arg * arg, char * text);
347 extern void set_default_arg (struct command_arg * arg, char * text, int len);
348 extern void command_loop (int prefix, int iscmd);
349 extern void execute_command (char *str);
350 extern int get_chr (void);
351 extern void display_msg (char * msg, int c);
352 extern void pushback_keystroke (int c);
353 extern void io_error_msg (char *str,...);
354 extern void io_info_msg (char *str,...);
355 extern char * expand_prompt (char * str);
356 extern void set_info (char * name);
357 extern void page_info_backwards (int rep);
358 extern void page_info (int rep);
359 extern void view_info (char * name, int ignore);
360 extern void with_keymap (char * mapname);
361 extern void one_cmd_with_keymap (char * mapname, struct key_sequence * keyseq);
362 
363 #endif
364