home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / eel / storemod.zip / STORE.E
Text File  |  1990-09-19  |  6KB  |  279 lines

  1. /************************************************************************
  2. * "Epsilon", "EEL" and "Lugaru" are trademarks of Lugaru Software, Ltd. *
  3. *                                    *
  4. *  Copyright (C) 1985, 1990 Lugaru Software Ltd.  All rights reserved.    *
  5. *                                    *
  6. * Limited permission is hereby granted to reproduce and modify this    *
  7. * copyrighted material provided that the resulting code is used only in    *
  8. * conjunction with Lugaru products and that this notice is retained in    *
  9. * any such reproduction or modification.                *
  10. ************************************************************************/
  11.  
  12. /*
  13. * The modifications marked KSH below allow the following forms to be 
  14. * placed in files suitable for M-x load-file.  Enjoy, Shane Hartman.
  15. * (set-variable "beep-duration" 19)
  16. * (load-bytes "shane")
  17. *
  18. */
  19.  
  20. #include "eel.h"
  21.  
  22. jmp_buf jbuf;
  23.  
  24. do_load_file(fname)    /* load command file */
  25. char *fname;
  26. {
  27. int err;
  28. char *old = bufname, *temp_load;
  29.  
  30. temp_load = temp_buf();
  31. bufname = temp_load;
  32. filename = fname;
  33. if (err = do_file_read(fname, strip_returns.default)) {
  34.     file_error(err, fname, "read error");
  35.     maybe_ding(bell_on_read_error);
  36. } else if (parse_cmds()) {
  37.     bufname = old;
  38.     to_buffer(temp_load);        /* show bad commands in window */
  39.     quick_abort();
  40. }
  41. bufname = old;
  42. delete_buffer(temp_load);
  43. }
  44.  
  45. load_buffer_command()
  46. {
  47. char bname[80], *old = bufname;
  48. int err;
  49.  
  50. get_buf(bname, "Load definitions from buffer", bufname);
  51. if (exist(bname))
  52.     bufname = bname;
  53. else
  54.     error("No such buffer");
  55. err = parse_cmds();
  56. bufname = old;
  57. if (err) to_buffer(bname);        /* show offending command */
  58. }
  59.  
  60. parse_cmds()    /* load cmds in current buf, return 1 if error occured */
  61. {
  62. int c;
  63. char funcname[80];
  64.  
  65. point = 0;
  66. if (setjmp(&jbuf))            /* errors come here */
  67.     return 1;
  68. for (;;) {
  69.     re_search(1, "[ \n\t]*");        /* skip whitespace */
  70.     if (point == size())
  71.         return 0;
  72.     if (curchar() == ';') {            /* found comment */
  73.         nl_forward();
  74.         continue;
  75.     }
  76.     must_char('(');
  77.     re_search(1, "[ \t\n]*");
  78.     parse_string(1, "[^ \t\n]*", funcname);
  79.     if (!strcmp(funcname,"define-macro"))
  80.         cf_macro();
  81.     else if (!strcmp(funcname, "bind-to-key"))
  82.         cf_bind();
  83.     else if (!strcmp(funcname, "create-prefix-command"))
  84.         cf_prefix();
  85.     else if (!(strcmp(funcname, "load-bytes")))    /* KSH */
  86.         cf_load();                /* KSH */
  87.     else if (!(strcmp(funcname, "set-variable")))    /* KSH */
  88.         cf_set();                /* KSH */
  89.     else
  90.         init_error("bad function");
  91.     re_search(1, "[ \t\n]*");
  92.     must_char(')');
  93. }
  94. }
  95.  
  96. short *keyseq;        /* sequence of key codes, in format used by macros */
  97.  
  98. cf_macro()        /* handle define-macro */
  99. {
  100. char name[80];
  101.  
  102. get_str(name);
  103. get_keyseq();
  104. name_macro(name, keyseq);
  105. }
  106.  
  107. cf_bind()        /* handle bind-to-key */
  108. {
  109. int index;
  110. char name[80];
  111.  
  112. get_str(name);
  113. index = find_index(name);
  114. if (index)
  115.     put_on_key(index);
  116. else
  117.     init_error("no such command");
  118. }
  119.  
  120. cf_prefix()        /* handle create-prefix-command */
  121. {
  122.     put_on_key(make_anon_keytable());
  123. }
  124.  
  125. put_on_key(index)        /* parse key seq and bind it to index */
  126. {
  127. short *curtab;
  128. int c, i = 1;
  129.  
  130. get_keyseq();
  131. curtab = root_keys;
  132. for (;;) {
  133.     c = keyseq[i];
  134.     if (++i >= keyseq[0]) break;
  135.     if (name_type(curtab[c]) != NT_TABLE)
  136.         init_error("expected \"");
  137.     curtab = index_table(curtab[c]);
  138. }
  139. curtab[c] = index;
  140. }
  141.  
  142. get_keyseq()            /* parse key names, put keys in keyseq */
  143. {
  144. re_search(1, "[ \t\n]*");
  145. keyseq = get_keycode();
  146. if (!keyseq)
  147.     init_error("bad character");
  148. }
  149.  
  150. /* KSH */
  151. get_int(into)
  152. char *into;
  153. {
  154.     re_search(1, "[ \t\n]*");
  155.     if (!isdigit (character (point)))
  156.         error ("Not a number");
  157.     parse_string(1, "[012345679]*", into);
  158.     re_search(1, "[ \t\n]*");
  159. }
  160.  
  161. get_str(s)        /* get a quoted string, and put it in s */
  162. char *s;
  163. {
  164. re_search(1, "[ \t\n]*");
  165. must_char('"');
  166. parse_string(1, "[^\"]*", s);
  167. must_char('"');
  168. }
  169.  
  170. must_char(c)    /* move past current character, making sure that it's c */
  171. {
  172. char msg[80];
  173.  
  174. if (curchar() != c) {
  175.     sprintf(msg, "expected %c", c);
  176.     init_error(msg);
  177. } else
  178.     point++;
  179. }
  180.  
  181. have_char(c)    /* if this character is c, move past and return 1, else 0 */
  182. {
  183. int res = (curchar() == c);
  184.  
  185. if (res) point++;
  186. return res;
  187. }
  188.  
  189. init_error(s)        /* display error and abort loading */
  190. char *s;
  191. {
  192. say("%s", s);
  193. longjmp(&jbuf, 1);
  194. }
  195.  
  196. insert_macro_command()        /* insert command-file definition of macro */
  197. {
  198.     char name[80];
  199.     int i;
  200.  
  201.     get_macname(name, "Insert macro", "last-kbd-macro");
  202.     i = find_index(name);
  203.     if (i && name_type(i) == NT_MACRO) {
  204.         bprintf("(define-macro \"%s\" ", name);
  205.         stuff_macro(get_macro(i));
  206.         stuff(")\n");
  207.     } else
  208.         error("Not a macro");
  209. }
  210.  
  211. stuff_macro(m)        /* insert cmd-file format macro text in buffer */
  212. short *m;
  213. {
  214.     char tmp[20];
  215.     int i;
  216.  
  217.     stuff("\"");
  218.     for (i = 1; i < m[0]; i++)
  219.         switch (m[i]) {
  220.             case '\n':    insert('\n'); break;
  221.             case 'A':    /* "F-1" in text */
  222.             case 'C':    /* resembles special key */
  223.             case 'S':
  224.             case 'F':
  225.             case 'N':    if (i + 1 < m[0] && m[i + 1] == '-') {
  226.                         insert('\\');    /* quote it */
  227.                         insert(m[i]);
  228.                         break;
  229.                     }        /* fall through */
  230.             default:    tmp[0] = 0;
  231.                     show_char(tmp, m[i]);
  232.                     stuff(tmp);
  233.                     break;
  234.             case ALT('<'):
  235.             case ALT('"'):
  236.             case ALT('\\'):    stuff("A-");    /* fall through */
  237.             case '<':        /* quote these */
  238.             case '"':
  239.             case '\\':    insert('\\');
  240.                     insert(m[i] & 0x7f);
  241.                     break;
  242.         }
  243.     stuff("\"");
  244. }
  245.  
  246. /* KSH */
  247. cf_set()    /* handle set-variable */
  248. {
  249.     int index;
  250.     int type;
  251.     char name[80];
  252.     char arg[256];
  253.  
  254.     get_str(name);
  255.     re_search(1, "[ \n\t]*");               /* skip whitespace */
  256.     if (character(point) == '"')
  257.         get_str(arg);
  258.     else
  259.         get_int(arg);
  260.     index = find_index(name);
  261.     type = name_type(index);
  262.     if (type != NT_VAR && type != NT_BUFVAR && type != NT_WINVAR)
  263.         error("%s: not a variable", name);
  264.     set_var_val(index, arg, 1);
  265.     set_var_val(index, arg, 0);  
  266. }
  267.  
  268. /* KSH */
  269. cf_load()   /* handle load-bytes */
  270. {
  271.     char fname[80];
  272.  
  273.     get_str(fname);
  274.     strcpy(get_extension(fname), byte_extension);
  275.     say("Loading %s", fname);
  276.     load_commands(fname);
  277. }
  278.