summaryrefslogtreecommitdiff
path: root/usr.bin/mg/match.c
blob: 3d2c528622af6b18110f76e951379369d1c3c23d (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*	$OpenBSD: match.c,v 1.7 2001/05/24 03:05:24 mickey Exp $	*/

/*
 *	Limited parenthesis matching routines
 *
 * The hacks in this file implement automatic matching * of (), [], {}, and
 * other characters.  It would be better to have a full-blown syntax table,
 * but there's enough overhead in the editor as it is.
 *
 * Since I often edit Scribe code, I've made it possible to blink arbitrary
 * characters -- just bind delimiter characters to "blink-matching-paren-hack"
 */

#include "def.h"
#include "key.h"

static int	balance		__P((void));
static void	displaymatch	__P((LINE *, int));

/*
 * Balance table. When balance() encounters a character that is to be
 * matched, it first searches this table for a balancing left-side character.
 * If the character is not in the table, the character is balanced by itself.
 * This is to allow delimiters in Scribe documents to be matched.
 */

static struct balance {
	char	left, right;
} bal[] = {
	{
		'(', ')'
	},
	{
		'[', ']'
	},
	{
		'{', '}'
	},
	{
		'<', '>'
	},
	{
		'\0', '\0'
	}
};

/*
 * Hack to show matching paren.  Self-insert character, then show matching
 * character, if any.  Bound to "blink-matching-paren-command".
 */
int
showmatch(f, n)
	int f, n;
{
	int	i, s;

	if (f & FFRAND)
		return FALSE;
	for (i = 0; i < n; i++) {
		if ((s = selfinsert(FFRAND, 1)) != TRUE)
			return s;
		/* unbalanced -- warn user */
		if (balance() != TRUE)
			ttbeep();
	}
	return TRUE;
}

/*
 * Search for and display a matching character.
 *
 * This routine does the real work of searching backward
 * for a balancing character.  If such a balancing character
 * is found, it uses displaymatch() to display the match.
 */

static int
balance()
{
	LINE	*clp;
	int	 cbo;
	int	 c;
	int	 i;
	int	 rbal;
	int	 lbal;
	int	 depth;

	rbal = key.k_chars[key.k_count - 1];

	/* See if there is a matching character -- default to the same */
	lbal = rbal;
	for (i = 0; bal[i].right != '\0'; i++)
		if (bal[i].right == rbal) {
			lbal = bal[i].left;
			break;
		}

	/*
	 * Move behind the inserted character.	We are always guaranteed
	 * that there is at least one character on the line, since one was
	 * just self-inserted by blinkparen.
	 */
	clp = curwp->w_dotp;
	cbo = curwp->w_doto - 1;

	/* init nesting depth */
	depth = 0;

	for (;;) {
		if (cbo == 0) {
			clp = lback(clp);	/* beginning of line	*/
			if (clp == curbp->b_linep)
				return (FALSE);
			cbo = llength(clp) + 1;
		}
		if (--cbo == llength(clp))
			c = '\n';		/* end of line		*/
		else
			c = lgetc(clp, cbo);	/* somewhere in middle	*/

		/*
		 * Check for a matching character.  If still in a nested
		 * level, pop out of it and continue search.  This check
		 * is done before the nesting check so single-character
		 * matches will work too.
		 */
		if (c == lbal) {
			if (depth == 0) {
				displaymatch(clp, cbo);
				return (TRUE);
			} else
				depth--;
		}
		/* Check for another level of nesting.	 */
		if (c == rbal)
			depth++;
	}
	/* NOTREACHED */
}


/*
 * Display matching character.  Matching characters that are not in the
 * current window are displayed in the echo line. If in the current window,
 * move dot to the matching character, sit there a while, then move back.
 */

static void
displaymatch(clp, cbo)
	LINE *clp;
	int cbo;
{
	LINE	*tlp;
	int	 tbo;
	int	 cp;
	int	 bufo;
	int	 c;
	int	 inwindow;
	char	 buf[NLINE];

	/*
	 * Figure out if matching char is in current window by
	 * searching from the top of the window to dot.
	 */
	inwindow = FALSE;
	for (tlp = curwp->w_linep; tlp != lforw(curwp->w_dotp);
	    tlp = lforw(tlp))
		if (tlp == clp)
			inwindow = TRUE;

	if (inwindow == TRUE) {
		tlp = curwp->w_dotp;	/* save current position */
		tbo = curwp->w_doto;

		curwp->w_dotp = clp;	/* move to new position */
		curwp->w_doto = cbo;
		curwp->w_flag |= WFMOVE;

		update();		/* show match */
		ttwait(1000);		/* wait for key or 1 second */

		curwp->w_dotp = tlp;	/* return to old position */
		curwp->w_doto = tbo;
		curwp->w_flag |= WFMOVE;
		update();
	} else {
		/* match is not in this window, so display line in echo area */
		bufo = 0;
		for (cp = 0; cp < llength(clp); cp++) {
			c = lgetc(clp, cp);
			if (c != '\t'
#ifdef NOTAB
			    || (curbp->b_flag & BFNOTAB)
#endif
				)
				if (ISCTRL(c)) {
					buf[bufo++] = '^';
					buf[bufo++] = CCHR(c);
				} else
					buf[bufo++] = c;
			else
				do {
					buf[bufo++] = ' ';
				} while (bufo & 7);
		}
		buf[bufo++] = '\0';
		ewprintf("Matches %s", buf);
	}
}