| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
#include "jove.h" |
| 9 |
#include "jctype.h" |
| 10 |
#include "fp.h" |
| 11 |
#include "chars.h" |
| 12 |
#include "disp.h" |
| 13 |
#include "ask.h" |
| 14 |
#include "commands.h" |
| 15 |
#include "macros.h" |
| 16 |
#include "extend.h" |
| 17 |
#include "fmt.h" |
| 18 |
/* #include "util.h" */ |
| 19 |
|
| 20 |
private void |
| 21 |
pop_macro_stack(void); |
| 22 |
|
| 23 |
private struct macro |
| 24 |
*ask_macname(const char *, int); |
| 25 |
|
| 26 |
private jbool |
| 27 |
UnsavedMacros = NO; /* are there any macros that need saving to a file? */ |
| 28 |
|
| 29 |
struct macro |
| 30 |
*macros = NULL; /* macros */ |
| 31 |
|
| 32 |
jbool InMacDefine = NO; |
| 33 |
|
| 34 |
private void |
| 35 |
add_mac(struct macro *new) |
| 36 |
{ |
| 37 |
struct macro *mp, |
| 38 |
*prev = NULL; |
| 39 |
|
| 40 |
for (mp = macros; mp != NULL; prev = mp, mp = mp->m_nextm) |
| 41 |
if (mp == new) |
| 42 |
return; |
| 43 |
|
| 44 |
if (prev) |
| 45 |
prev->m_nextm = new; |
| 46 |
else |
| 47 |
macros = new; |
| 48 |
new->m_nextm = NULL; |
| 49 |
new->Type = MACRO; |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
* Jove keeps a "stack" of running macros. Whenever we |
| 54 |
* execute a macro, we push it on the stack. The main event |
| 55 |
* loops (getch(), joverc(), do_find()) call mac_getc() and |
| 56 |
* dispatch() to actually run whatever is on the macro stack. |
| 57 |
*/ |
| 58 |
struct m_thread { |
| 59 |
struct m_thread *mt_prev; |
| 60 |
struct macro *mt_mp; |
| 61 |
int mt_offset, |
| 62 |
mt_count; |
| 63 |
}; |
| 64 |
|
| 65 |
private struct m_thread *mac_stack = NULL; |
| 66 |
|
| 67 |
private struct m_thread * |
| 68 |
alloc_mthread(void) |
| 69 |
{ |
| 70 |
return (struct m_thread *) emalloc(sizeof (struct m_thread)); |
| 71 |
} |
| 72 |
|
| 73 |
private void |
| 74 |
free_mthread(struct m_thread *t) |
| 75 |
{ |
| 76 |
free(t); |
| 77 |
} |
| 78 |
|
| 79 |
/* abandon/drain any running macros */ |
| 80 |
void |
| 81 |
unwind_macro_stack(void) |
| 82 |
{ |
| 83 |
while (mac_stack != NULL) |
| 84 |
pop_macro_stack(); |
| 85 |
} |
| 86 |
|
| 87 |
private void |
| 88 |
pop_macro_stack(void) |
| 89 |
{ |
| 90 |
struct m_thread *m; |
| 91 |
|
| 92 |
if ((m = mac_stack) == NULL) |
| 93 |
return; |
| 94 |
|
| 95 |
mac_stack = m->mt_prev; |
| 96 |
jdprintf("pop_macro_stack \"%s\" newtop=\"%s\"\n", m->mt_mp->Name, |
| 97 |
mac_stack?mac_stack->mt_mp->Name : ""); |
| 98 |
free_mthread(m); |
| 99 |
} |
| 100 |
|
| 101 |
private void |
| 102 |
push_macro_stack(struct macro *m, int count) |
| 103 |
{ |
| 104 |
struct m_thread *t; |
| 105 |
|
| 106 |
jdprintf("push_macro_stack \"%s\" %d\n", m->Name, count); |
| 107 |
for (t = mac_stack; t != NULL; t = t->mt_prev) { |
| 108 |
jdprintf("stack \"%s\"\n", t->mt_mp->Name); |
| 109 |
if (t->mt_mp == m) { |
| 110 |
complain("[Cannot execute macro recursively]"); |
| 111 |
/* NOTREACHED */ |
| 112 |
} |
| 113 |
} |
| 114 |
if (count <= 0) { |
| 115 |
complain("[Cannot execute macro a negative number of times]"); |
| 116 |
/* NOTREACHED */ |
| 117 |
} |
| 118 |
t = alloc_mthread(); |
| 119 |
t->mt_prev = mac_stack; |
| 120 |
mac_stack = t; |
| 121 |
t->mt_offset = 0; |
| 122 |
t->mt_mp = m; |
| 123 |
t->mt_count = count; |
| 124 |
jdprintf("push_macro_stack added \"%s\"\n", m->Name); |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
do_macro(struct macro *mac) |
| 129 |
{ |
| 130 |
push_macro_stack(mac, arg_value()); |
| 131 |
} |
| 132 |
|
| 133 |
private struct macro KeyMacro = { /* Macro used for defining */ |
| 134 |
MACRO, "keyboard-macro", 0, NULL, NULL |
| 135 |
}; |
| 136 |
|
| 137 |
private int kmac_len; |
| 138 |
private int kmac_buflen = 0; |
| 139 |
|
| 140 |
void |
| 141 |
mac_init(void) |
| 142 |
{ |
| 143 |
add_mac(&KeyMacro); |
| 144 |
} |
| 145 |
|
| 146 |
void |
| 147 |
mac_putc(DAPchar c) |
| 148 |
{ |
| 149 |
if (kmac_len >= kmac_buflen) { |
| 150 |
KeyMacro.m_body = erealloc(KeyMacro.m_body, (size_t) kmac_buflen + 16); |
| 151 |
kmac_buflen += 16; |
| 152 |
} |
| 153 |
KeyMacro.m_body[kmac_len++] = c; |
| 154 |
} |
| 155 |
|
| 156 |
void |
| 157 |
note_dispatch(void) |
| 158 |
{ |
| 159 |
if (kmac_len > 0) |
| 160 |
KeyMacro.m_len = kmac_len - 1; |
| 161 |
} |
| 162 |
|
| 163 |
jbool |
| 164 |
in_macro(void) |
| 165 |
{ |
| 166 |
return (mac_stack != NULL); |
| 167 |
} |
| 168 |
|
| 169 |
ZXchar |
| 170 |
mac_getc(void) |
| 171 |
{ |
| 172 |
struct m_thread *mthread; |
| 173 |
struct macro *m; |
| 174 |
ZXchar zc; |
| 175 |
|
| 176 |
if ((mthread = mac_stack) == NULL) |
| 177 |
return EOF; |
| 178 |
|
| 179 |
m = mthread->mt_mp; |
| 180 |
jdprintf("mac_getc \"%s\" len=%d off=%d count=%d \n", m->Name, m->m_len, |
| 181 |
mthread->mt_offset, mthread->mt_count); |
| 182 |
if (mthread->mt_offset == m->m_len) { |
| 183 |
mthread->mt_offset = 0; |
| 184 |
if (--mthread->mt_count == 0) |
| 185 |
pop_macro_stack(); |
| 186 |
return mac_getc(); |
| 187 |
} |
| 188 |
zc = ZXC(m->m_body[mthread->mt_offset++]); |
| 189 |
jdbg("mac_getc -> %d %c\n", zc, jisprint(zc)?zc:'~'); |
| 190 |
return zc; |
| 191 |
} |
| 192 |
|
| 193 |
private void |
| 194 |
MacDef(struct macro *m, /* NULL, or def to overwrite */ |
| 195 |
const char *name, /* must be stable if m isn't NULL */ |
| 196 |
int len, char *body) |
| 197 |
{ |
| 198 |
if (m == NULL) { |
| 199 |
m = (struct macro *) emalloc(sizeof *m); |
| 200 |
m->Name = name; |
| 201 |
} else { |
| 202 |
if (m->m_body != NULL) |
| 203 |
free(m->m_body); |
| 204 |
} |
| 205 |
m->m_len = len; |
| 206 |
if (len == 0) { |
| 207 |
m->m_body = NULL; |
| 208 |
} else { |
| 209 |
m->m_body = emalloc((size_t) len); |
| 210 |
byte_copy(body, m->m_body, (size_t) len); |
| 211 |
} |
| 212 |
add_mac(m); |
| 213 |
if (!InJoverc) |
| 214 |
UnsavedMacros = YES; |
| 215 |
} |
| 216 |
|
| 217 |
void |
| 218 |
NameMac(void) |
| 219 |
{ |
| 220 |
char *name = NULL; |
| 221 |
struct macro *m; |
| 222 |
|
| 223 |
if (KeyMacro.m_len == 0) { |
| 224 |
complain("[No keyboard macro to name!]"); |
| 225 |
/* NOTREACHED */ |
| 226 |
} |
| 227 |
if (in_macro() || InMacDefine) { |
| 228 |
complain("[Can't name while defining/executing]"); |
| 229 |
/* NOTREACHED */ |
| 230 |
} |
| 231 |
if ((m = ask_macname(ProcFmt, ALLOW_OLD | ALLOW_INDEX | ALLOW_NEW)) == NULL) |
| 232 |
name = copystr(Minibuf); |
| 233 |
if (m == &KeyMacro) { |
| 234 |
complain("[Can't name it that!]"); |
| 235 |
/* NOTREACHED */ |
| 236 |
} |
| 237 |
MacDef(m, name, KeyMacro.m_len, KeyMacro.m_body); |
| 238 |
} |
| 239 |
|
| 240 |
void |
| 241 |
RunMacro(void) |
| 242 |
{ |
| 243 |
do_macro((struct macro *) findmac(ProcFmt)); |
| 244 |
} |
| 245 |
|
| 246 |
private void |
| 247 |
pr_putc(ZXchar c, File *fp) |
| 248 |
{ |
| 249 |
if (c == '\\' || c == '^') { |
| 250 |
f_putc('\\', fp); |
| 251 |
f_putc(c, fp); |
| 252 |
} else { |
| 253 |
char buf[PPWIDTH]; |
| 254 |
char *p; |
| 255 |
|
| 256 |
PPchar(c, buf); |
| 257 |
for (p = buf; *p != '\0'; p++) |
| 258 |
f_putc(*p, fp); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
void |
| 263 |
WriteMacs(void) |
| 264 |
{ |
| 265 |
struct macro *m; |
| 266 |
char |
| 267 |
fnamebuf[FILESIZE]; |
| 268 |
File *fp; |
| 269 |
int i; |
| 270 |
|
| 271 |
(void) ask_file((char *)NULL, (char *)NULL, fnamebuf); |
| 272 |
fp = open_file(fnamebuf, iobuff, F_WRITE, YES); |
| 273 |
|
| 274 |
/* Don't write the keyboard macro which is always the first */ |
| 275 |
for (m = macros->m_nextm; m != NULL; m = m->m_nextm) { |
| 276 |
fwritef(fp, "define-macro %s ", m->Name); |
| 277 |
for (i = 0; i < m->m_len; i++) |
| 278 |
pr_putc(ZXC(m->m_body[i]), fp); |
| 279 |
#ifdef USE_CRLF |
| 280 |
f_putc('\r', fp); |
| 281 |
#endif /* USE_CRLF */ |
| 282 |
f_putc(EOL, fp); |
| 283 |
} |
| 284 |
close_file(fp); |
| 285 |
UnsavedMacros = NO; |
| 286 |
} |
| 287 |
|
| 288 |
void |
| 289 |
DefKBDMac(void) |
| 290 |
{ |
| 291 |
struct macro *m = ask_macname(ProcFmt, |
| 292 |
ALLOW_OLD | ALLOW_INDEX | ALLOW_NEW); |
| 293 |
ZXchar c; |
| 294 |
const char |
| 295 |
*macro_name = m == NULL? copystr(Minibuf) : m->Name, |
| 296 |
*macro_body; |
| 297 |
char |
| 298 |
macro_buffer[LBSIZE]; |
| 299 |
int len; |
| 300 |
|
| 301 |
if (m == &KeyMacro) { |
| 302 |
complain("[Can't name it that!]"); |
| 303 |
/* NOTREACHED */ |
| 304 |
} |
| 305 |
/* ??? I hope that this ask doesn't change *m! */ |
| 306 |
macro_body = ask(NullStr, ": %f %s enter body: ", macro_name); |
| 307 |
len = 0; |
| 308 |
while ((c = ZXC(*macro_body++)) != '\0') { |
| 309 |
if (c == '\\' || c == '^') |
| 310 |
c = DecodePair(c, ZXC(*macro_body++)); |
| 311 |
if (len >= LBSIZE) { |
| 312 |
complain("Macro too large"); |
| 313 |
/* NOTREACHED */ |
| 314 |
} |
| 315 |
macro_buffer[len++] = c; |
| 316 |
} |
| 317 |
MacDef(m, macro_name, len, macro_buffer); |
| 318 |
} |
| 319 |
|
| 320 |
void |
| 321 |
Remember(void) |
| 322 |
{ |
| 323 |
/* If we are already executing macros or in a joverc, disallow any |
| 324 |
* attempts to define the keyboard macro |
| 325 |
*/ |
| 326 |
if (in_macro() || InJoverc) { |
| 327 |
complain("Cannot define keyboard macro from macro or joverc"); |
| 328 |
/* NOTREACHED */ |
| 329 |
} |
| 330 |
if (InMacDefine) |
| 331 |
message("[Already defining ... continue with definition]"); |
| 332 |
else { |
| 333 |
UpdModLine = YES; |
| 334 |
InMacDefine = YES; |
| 335 |
kmac_len = KeyMacro.m_len = 0; |
| 336 |
message("Defining..."); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
void |
| 341 |
Forget(void) |
| 342 |
{ |
| 343 |
UpdModLine = YES; |
| 344 |
if (InMacDefine) { |
| 345 |
message("Keyboard macro defined."); |
| 346 |
InMacDefine = NO; |
| 347 |
} else { |
| 348 |
complain("[end-kbd-macro: not currently defining macro!]"); |
| 349 |
/* NOTREACHED */ |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
void |
| 354 |
ExecMacro(void) |
| 355 |
{ |
| 356 |
do_macro(&KeyMacro); |
| 357 |
} |
| 358 |
|
| 359 |
void |
| 360 |
MacInter(void) |
| 361 |
{ |
| 362 |
if (Asking) |
| 363 |
Interactive = YES; |
| 364 |
} |
| 365 |
|
| 366 |
jbool |
| 367 |
ModMacs(void) |
| 368 |
{ |
| 369 |
return UnsavedMacros; |
| 370 |
} |
| 371 |
|
| 372 |
/* Ask for macro name, with completion. |
| 373 |
* Flags is passed directly to complete. If ALLOW_NEW is on, |
| 374 |
* the name might be new, in which case NULL is returned and the |
| 375 |
* actual name in in Minibuf. |
| 376 |
*/ |
| 377 |
|
| 378 |
private struct macro * |
| 379 |
ask_macname(const char *prompt, int flags) |
| 380 |
{ |
| 381 |
const char *strings[100]; |
| 382 |
const char **strs = strings; |
| 383 |
int com; |
| 384 |
struct macro *m; |
| 385 |
|
| 386 |
for (m = macros; m != NULL; m = m->m_nextm) { |
| 387 |
if (strs == &strings[elemsof(strings)-1]) { |
| 388 |
complain("[too many macros]"); |
| 389 |
/* NOTREACHED */ |
| 390 |
} |
| 391 |
*strs++ = m->Name; |
| 392 |
} |
| 393 |
*strs = NULL; |
| 394 |
|
| 395 |
if ((com = complete(strings, (char *)NULL, prompt, flags)) < 0) |
| 396 |
return NULL; |
| 397 |
|
| 398 |
m = macros; |
| 399 |
while (--com >= 0) |
| 400 |
m = m->m_nextm; |
| 401 |
return m; |
| 402 |
} |
| 403 |
|
| 404 |
const data_obj * |
| 405 |
findmac(const char *prompt) |
| 406 |
{ |
| 407 |
return (data_obj *)ask_macname(prompt, ALLOW_OLD | ALLOW_INDEX); |
| 408 |
} |