summaryrefslogtreecommitdiff
path: root/usr.bin/mg/macro.c
blob: 83e8851f0433e97d7b3d8472ab35dcabc8b2531e (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
86
87
88
89
90
91
92
93
94
95
96
/*	$OpenBSD: macro.c,v 1.4 2001/01/29 01:58:08 niklas Exp $	*/

/*
 *	Keyboard macros.
 */

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

/* ARGSUSED */
int
definemacro(f, n)
	int	f, n;
{
	LINE	*lp1, *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 */
int
finishmacro(f, n)
	int	f, n;
{
	macrodef = FALSE;
	ewprintf("End Keyboard Macro Definition");
	return TRUE;
}

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

	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	/* NO_MACRO */