summaryrefslogtreecommitdiff
path: root/usr.bin/mg/macro.c
blob: 6b4396bf78a0ce84ecbfff22fc7215f412aac2dc (plain)
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
/* keyboard macros for Mg 2 */

#ifndef NO_MACRO
#include "def.h"
#include "key.h"
#define EXTERN
#define INIT(i) = (i)
#include "macro.h"

/* ARGSUSED */
definemacro(f, n)
	int             f, n;
{
	register LINE  *lp1;
	LINE           *lp2;

	macrocount = 0;
	if (macrodef) {
		ewprintf("already defining macro");
		return macrodef = FALSE;
	}
	/* free lines allocated for string arguments */
	if (maclhead != NULL) {
		for (lp1 = maclhead->l_fp; lp1 != maclhead; lp1 = lp2) {
			lp2 = lp1->l_fp;
			free((char *) lp1);
		}
		free((char *) lp1);
	}
	if ((maclhead = lp1 = lalloc(0)) == NULL)
		return FALSE;
	ewprintf("Defining Keyboard Macro...");
	maclcur = lp1->l_fp = lp1->l_bp = lp1;
	return macrodef = TRUE;
}

/* ARGSUSED */
finishmacro(f, n)
	int             f, n;
{
	macrodef = FALSE;
	ewprintf("End Keyboard Macro Definition");
	return TRUE;
}

/* ARGSUSED */
executemacro(f, n)
	int             f, n;
{
	int             i, j;
	PF              funct;
	int             universal_argument();
	int             flag, num;

	if (macrodef ||
	 (macrocount >= MAXMACRO && macro[MAXMACRO].m_funct != finishmacro))
		return FALSE;
	if (macrocount == 0)
		return TRUE;
	inmacro = TRUE;
	for (i = n; i > 0; i--) {
		maclcur = maclhead->l_fp;
		flag = 0;
		num = 1;
		for (j = 0; j < macrocount - 1; j++) {
			funct = macro[j].m_funct;
			if (funct == universal_argument) {
				flag = FFARG;
				num = macro[++j].m_count;
				continue;
			}
			if ((*funct) (flag, num) != TRUE) {
				inmacro = FALSE;
				return FALSE;
			}
			lastflag = thisflag;
			thisflag = 0;
			flag = 0;
			num = 1;
		}
	}
	inmacro = FALSE;
	return TRUE;
}
#endif