]> pd.if.org Git - pd_readline/blob - mg/macro.c
Added mg from an OpenBSD mirror site. Many thanks to the OpenBSD team and the mg...
[pd_readline] / mg / macro.c
1 /*      $OpenBSD: macro.c,v 1.14 2012/04/12 04:47:59 lum Exp $  */
2
3 /* This file is in the public domain. */
4
5 /*
6  *      Keyboard macros.
7  */
8
9 #include "def.h"
10 #include "key.h"
11 #include "macro.h"
12
13 int inmacro = FALSE;    /* Macro playback in progess */
14 int macrodef = FALSE;   /* Macro recording in progress */
15 int macrocount = 0;
16
17 struct line *maclhead = NULL;
18 struct line *maclcur;
19
20 union macrodef macro[MAXMACRO];
21
22 /* ARGSUSED */
23 int
24 definemacro(int f, int n)
25 {
26         struct line     *lp1, *lp2;
27
28         macrocount = 0;
29
30         if (macrodef) {
31                 ewprintf("already defining macro");
32                 return (macrodef = FALSE);
33         }
34
35         /* free lines allocated for string arguments */
36         if (maclhead != NULL) {
37                 for (lp1 = maclhead->l_fp; lp1 != maclhead; lp1 = lp2) {
38                         lp2 = lp1->l_fp;
39                         free(lp1);
40                 }
41                 free(lp1);
42         }
43
44         if ((maclhead = lp1 = lalloc(0)) == NULL)
45                 return (FALSE);
46
47         ewprintf("Defining Keyboard Macro...");
48         maclcur = lp1->l_fp = lp1->l_bp = lp1;
49         return (macrodef = TRUE);
50 }
51
52 /* ARGSUSED */
53 int
54 finishmacro(int f, int n)
55 {
56         if (macrodef == TRUE) {
57                 macrodef = FALSE;
58                 ewprintf("End Keyboard Macro Definition");
59                 return (TRUE);
60         }
61         return (FALSE);
62 }
63
64 /* ARGSUSED */
65 int
66 executemacro(int f, int n)
67 {
68         int      i, j, flag, num;
69         PF       funct;
70
71         if (macrodef ||
72             (macrocount >= MAXMACRO && macro[MAXMACRO - 1].m_funct
73             != finishmacro)) {
74                 ewprintf("Macro too long. Aborting.");
75                 return (FALSE);
76         }
77
78         if (macrocount == 0)
79                 return (TRUE);
80
81         inmacro = TRUE;
82
83         for (i = n; i > 0; i--) {
84                 maclcur = maclhead->l_fp;
85                 flag = 0;
86                 num = 1;
87                 for (j = 0; j < macrocount - 1; j++) {
88                         funct = macro[j].m_funct;
89                         if (funct == universal_argument) {
90                                 flag = FFARG;
91                                 num = macro[++j].m_count;
92                                 continue;
93                         }
94                         if ((*funct)(flag, num) != TRUE) {
95                                 inmacro = FALSE;
96                                 return (FALSE);
97                         }
98                         lastflag = thisflag;
99                         thisflag = 0;
100                         flag = 0;
101                         num = 1;
102                 }
103         }
104         inmacro = FALSE;
105         return (TRUE);
106 }