forked from bloovis/microemacs.mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextend.c
More file actions
378 lines (354 loc) · 9.21 KB
/
extend.c
File metadata and controls
378 lines (354 loc) · 9.21 KB
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
/*
Copyright (C) 2008 Mark Alexander
This file is part of MicroEMACS, a small text editor.
MicroEMACS 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/>.
*/
/*
* Name: MicroEMACS
* Extended (M-X) commands.
* Version: 29
* By: rex::conroy
* decvax!decwrl!dec-rhea!dec-rex!conroy
* Last edit: 24-Mar-88
* By: Mark Alexander
* drivax!alexande
*/
#include "def.h"
/*
* This function modifies the keyboard
* binding table, by adjusting the entries in the
* big "bindings" array. Most of the grief deals with the
* prompting for additional arguments.
*/
int
bindtokey (int f, int n, int k)
{
register int s;
register SYMBOL *sp;
register int c;
char xname[NXNAME];
if ((s = ereadv ("Function: ", xname, NXNAME, EFAUTO)) != TRUE)
return (s);
if ((sp = symlookup (xname)) == NULL)
{
eprintf ("Unknown function for binding");
return (FALSE);
}
if (kbdmop != NULL) /* Are we in a macro? */
c = *kbdmop++; /* Get key from macro. */
else
{
eputc (' '); /* Prompt for the key. */
eputc ('K');
eputc ('e');
eputc ('y');
eputc (':');
eputc (' ');
ttflush ();
c = getkey (); /* Read key. */
ekeyname (xname, c); /* Display ekeyname. */
eputs (xname);
ttflush ();
}
if (kbdmip != NULL)
{ /* Save key in macro. */
if (kbdmip > &kbdm[NKBDM - 2])
{
ctrlg (FALSE, 0, KRANDOM);
return (FALSE);
}
*kbdmip++ = c;
}
setbinding (c, sp);
return (TRUE);
}
/*
* Extended command. Call the message line
* routine to read in the command name and apply autocompletion
* to it. When it comes back, look the name up in the symbol table
* and run the command if it is found and has the right type.
* Print an error if there is anything wrong.
*/
int
extend (int f, int n, int k)
{
register SYMBOL *sp;
register int s;
char xname[NXNAME];
if ((s = ereadv (": ", xname, NXNAME, EFNEW | EFAUTO)) != TRUE)
return (s);
if ((sp = symlookup (xname)) != NULL)
{
if (sp->s_macro)
{
if (kbdmip != NULL || kbdmop != NULL)
{
eprintf ("Not now");
return (FALSE);
}
return (domacro (sp->s_macro, n));
}
#if USE_RUBY
else if (sp->s_funcp == NULL)
return rubycall (sp->s_name, f, n);
#endif
else
return ((*sp->s_funcp) (f, n, KRANDOM));
}
eprintf ("Unknown extended command");
return (ABORT);
}
/*
* Read a key from the keyboard, and look it
* up in the binding table. Display the name of the function
* currently bound to the key. Say that the key is not bound
* if it is indeed not bound, or if the type is not a
* "builtin". This is a bit of overkill, because this is the
* only kind of function there is.
*/
int
help (int f, int n, int k)
{
register SYMBOL *sp;
register int c;
char b[20];
c = getkey ();
ekeyname (b, c);
if ((sp = getbinding (c)) == NULL)
eprintf ("[%s is unbound]", b);
else
eprintf ("[%s is bound to %s]", b, sp->s_name);
return (TRUE);
}
/*
* Variables used by the insert-macro command.
* Save is a buffer of characters to be inserted
* into the current buffer; sindex is the current
* buffer index. Stringlen is the length of a
* quoted string that is currently being inserted
* into the buffer.
*/
static char save[32]; /* Saved string. */
static int sindex; /* Index into save. */
static int stringlen; /* Length of quoted string */
/*
* Flush the save buffer to the current buffer.
*/
static int
flush (void)
{
if (sindex == 0)
return (TRUE);
if (linsert (sindex, 0, save) == FALSE)
return (FALSE);
sindex = 0;
return (TRUE);
}
/*
* Write a character to the save buffer, and
* flush it if the buffer is full.
*/
static int
outchar (char c)
{
if (sindex == sizeof (save)) /* Buffer full? */
if (flush () == FALSE) /* Insert the whole lot */
return (FALSE);
save[sindex++] = c;
return (TRUE);
}
/*
* Write a null-terminated string to the save buffer.
*/
static int
outbuf (const char *s)
{
register char c;
while ((c = *s++) != 0)
if (outchar (c) == FALSE)
return (FALSE);
return (TRUE);
}
/*
* Write a space character to the current buffer,
* do a new line if we're past the fill column.
* Also, flush the buffer.
*/
static int
outspace (void)
{
if (flush () == FALSE)
return (FALSE);
if (getcolpos () >= fillcol)
return (lnewline ());
else
return (linsert (1, ' ', NULLPTR));
}
/*
* Write a decimal number to the save buffer.
*/
static int
outdec (int n)
{
static char buf[5];
register int i;
if (n < 0)
{
n = -n;
if (outchar ('-') == FALSE)
return (FALSE);
}
for (i = 6; i > 0;)
{
buf[--i] = (n % 10) + '0';
if ((n /= 10) == 0)
break;
}
while (i < 6)
if (outchar (buf[i++]) == FALSE)
return (FALSE);
return (outspace ());
}
/*
* Write one character of a quoted string to
* the save buffer.
*/
static int
outstrchr (char c)
{
if (stringlen == 0) /* empty string? */
if (outchar ('"') == FALSE) /* send opening quote */
return (FALSE);
stringlen++;
if (c == '"') /* prefix a quote with an escape */
if (outchar ('\\') == FALSE)
return (FALSE);
return (outchar (c));
}
/*
* Flush a quoted string to the current buffer.
*/
static int
flushstring (void)
{
if (stringlen == 0)
return (TRUE); /* empty string */
stringlen = 0;
if (outchar ('"') == FALSE) /* closing quote */
return (FALSE);
if (flush () == FALSE)
return (FALSE);
return (outspace ());
}
/*
* Prompt for a name, and insert the text of the specifed macro into the
* current buffer. Return FALSE if an error occurs; otherwise TRUE.
*/
int
insertmacro (int f, int n, int k)
{
register int *mp; /* Macro pointer. */
char xname[NXNAME + 2]; /* Symbol name. */
register SYMBOL *sp = NULL;
register int named;
/* Read the name of the symbol to use, store the name in
* a local array.
*/
if ((named = ereadv ("Macro name: ", xname, NXNAME, EFAUTO)) == ABORT)
return (named);
if (named == FALSE) /* no name given? */
mp = &kbdm[0]; /* use current macro */
else
{
/* See if the symbol exists, and if it is a named macro.
*/
if ((sp = symlookup (xname)) == NULL)
{
eprintf ("No such symbol.");
return (FALSE);
}
if ((mp = sp->s_macro) == NULL)
{
eprintf ("Not a named macro.");
return (FALSE);
}
}
/* Insert the text of each character in the macro into the
* the current buffer. Put strings of printable ascii
* characters into quotes; convert other characters
* into their appropriate names in brackets.
*/
sindex = 0;
for (k = KCTLX | '(';; k = *mp++)
{
if (k > 0x00 && k <= 0x1F) /* Relocate control. */
k = KCTRL | (k + '@');
ekeyname (xname, k); /* Get key name. */
if ((k >= 0x20 && k <= 0x7e) || (strlen (xname) <= 1))
{ /* Simple ASCII? */
if (k == '\\') /* Escape backslash */
if (outstrchr (k) == FALSE)
return (FALSE);
if (outstrchr (k) == FALSE)
return (FALSE);
}
else if (k == 0)
{ /* Null: end of string */
if (outstrchr ('\\') == FALSE)
return (FALSE);
if (outstrchr ('r') == FALSE)
return (FALSE);
if (flushstring () == FALSE)
return (FALSE);
}
else if (k == (KCTRL | 'U'))
{ /* Control-U arg prefix */
if (flushstring () == FALSE)
return (FALSE); /* Flush prev. string. */
if (outdec (*mp++) == FALSE)
return (FALSE); /* Output argument */
}
else
{ /* Not a simple key. */
if (flushstring () == FALSE)
return (FALSE); /* Flush prev. string. */
if (outchar ('[') == FALSE)
return (FALSE); /* Opening bracket. */
if (outbuf (xname) == FALSE)
return (FALSE); /* Write out key name, */
if (outchar (']') == FALSE)
return (FALSE); /* Closing bracket. */
if (outspace () == FALSE)
return (FALSE); /* followed by a space. */
}
if (k == (KCTLX | ')')) /* End of macro? */
break;
}
if (flushstring () == FALSE) /* Flush last string. */
return (FALSE);
if (named)
{ /* A named macro? */
if (outbuf ("name-macro") == FALSE)
return (FALSE); /* Bind macro to name. */
if (outspace () == FALSE)
return (FALSE); /* followed by a space. */
if (outchar ('"') == FALSE) /* Opening quote. */
return (FALSE);
if (outbuf (sp->s_name) == FALSE) /* Output symbol name. */
return (FALSE);
if (outbuf ("\\r\"") == FALSE) /* Append carriage ret. */
return (FALSE);
if (flush () == FALSE) /* Flush symbol name. */
return (FALSE);
}
return (lnewline ()); /* Blank line. */
}