1 /* $OpenBSD: macro.c,v 1.6 2002/07/01 14:33:44 vincent Exp $ */ 2 3 /* 4 * Keyboard macros. 5 */ 6 7 #ifndef NO_MACRO 8 #include "def.h" 9 #include "key.h" 10 #define EXTERN 11 #define INIT(i) = (i) 12 #include "macro.h" 13 14 /* ARGSUSED */ 15 int 16 definemacro(int f, int n) 17 { 18 LINE *lp1, *lp2; 19 20 macrocount = 0; 21 22 if (macrodef) { 23 ewprintf("already defining macro"); 24 return macrodef = FALSE; 25 } 26 27 /* free lines allocated for string arguments */ 28 if (maclhead != NULL) { 29 for (lp1 = maclhead->l_fp; lp1 != maclhead; lp1 = lp2) { 30 lp2 = lp1->l_fp; 31 free((char *)lp1); 32 } 33 free((char *)lp1); 34 } 35 36 if ((maclhead = lp1 = lalloc(0)) == NULL) 37 return FALSE; 38 39 ewprintf("Defining Keyboard Macro..."); 40 maclcur = lp1->l_fp = lp1->l_bp = lp1; 41 return macrodef = TRUE; 42 } 43 44 /* ARGSUSED */ 45 int 46 finishmacro(int f, int n) 47 { 48 macrodef = FALSE; 49 ewprintf("End Keyboard Macro Definition"); 50 return TRUE; 51 } 52 53 /* ARGSUSED */ 54 int 55 executemacro(int f, int n) 56 { 57 int i, j, flag, num; 58 PF funct; 59 60 if (macrodef || 61 (macrocount >= MAXMACRO && macro[MAXMACRO].m_funct != finishmacro)) 62 return FALSE; 63 64 if (macrocount == 0) 65 return TRUE; 66 67 inmacro = TRUE; 68 69 for (i = n; i > 0; i--) { 70 maclcur = maclhead->l_fp; 71 flag = 0; 72 num = 1; 73 for (j = 0; j < macrocount - 1; j++) { 74 funct = macro[j].m_funct; 75 if (funct == universal_argument) { 76 flag = FFARG; 77 num = macro[++j].m_count; 78 continue; 79 } 80 if ((*funct)(flag, num) != TRUE) { 81 inmacro = FALSE; 82 return FALSE; 83 } 84 lastflag = thisflag; 85 thisflag = 0; 86 flag = 0; 87 num = 1; 88 } 89 } 90 inmacro = FALSE; 91 return TRUE; 92 } 93 #endif /* NO_MACRO */ 94