summaryrefslogtreecommitdiff
path: root/usr.bin/mg/cmode.c
blob: e2660f96337f94632bea1f89616e83a3b47672a6 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* $OpenBSD: cmode.c,v 1.1 2008/06/12 01:58:44 kjell Exp $ */
/*
 * This file is in the public domain.
 *
 * Author: Kjell Wooding <kjell@openbsd.org>
 */

/*
 * Implement an non-irritating KNF-compliant mode for editing
 * C code.
 */
#include <ctype.h>

#include "def.h"
#include "kbd.h"
#include "funmap.h"

/* Pull in from modes.c */
extern int changemode(int, int, char *);

static int cc_strip_trailp = TRUE;	/* Delete Trailing space? */
static int cc_basic_indent = 8;		/* Basic Indent multiple */
static int cc_cont_indent = 4;		/* Continued line indent */
static int cc_colon_indent = -8;	/* Label / case indent */

static int getmatch(int, int);
static int getindent(const struct line *, int *);

/* Keymaps */

static PF cmode_brace[] = {
	cc_char,	/* } */
};

static PF cmode_ci[] = {
	cc_tab,		/* ^I */
	rescan,		/* ^J */
	rescan,		/* ^K */
	rescan,		/* ^L */
	cc_lfindent,	/* ^M */
};

static PF cmode_colon[] = {
	cc_char,	/* : */
	rescan,		/* ; */
};

static struct KEYMAPE (3 + IMAPEXT) cmodemap = {
	3,
	3 + IMAPEXT,
	rescan,
	{
		{ CCHR('I'), CCHR('M'), cmode_ci, NULL },
		{ ':', ';', cmode_colon, NULL },
		{ '}', '}', cmode_brace, NULL }
	}
};

/* Funtion, Mode hooks */

void
cmode_init(void)
{
	funmap_add(cmode, "c-mode");
	funmap_add(cc_char, "c-handle-special-char");
	funmap_add(cc_tab, "c-tab-or-indent");
	funmap_add(cc_indent, "c-indent");
	funmap_add(cc_lfindent, "c-indent-and-newline");
	maps_add((KEYMAP *)&cmodemap, "c-mode");
}

/*
 * Enable/toggle c-mode
 */
int
cmode(int f, int n)
{
	return(changemode(f, n, "c-mode"));
}

/*
 * Handle special C character - selfinsert then indent.
 */
int
cc_char(int f, int n)
{
	if (n < 0)
		return (FALSE);
	if (selfinsert(FFRAND, n) == FALSE)
		return (FALSE);
	return (cc_indent(FFRAND, n));
}

/*
 * If we are in the whitespace at the beginning of the line,
 * simply act as a regular tab. If we are not, indent
 * current line according to whitespace rules.
 */
int
cc_tab(int f, int n)
{
	int lo;
	int inwhitep = FALSE;	/* In leading whitespace? */
	
	for (lo = 0; lo < llength(curwp->w_dotp); lo++) {
		if (!isspace(lgetc(curwp->w_dotp, lo)))
			break;
		if (lo == curwp->w_doto)
			inwhitep = TRUE;
	}

	/* Special case: we could be at the end of a whitespace line */
	if (lo == llength(curwp->w_dotp) && curwp->w_doto == lo)
		inwhitep = TRUE;
	
	/* If empty line, or in whitespace */
	if (llength(curwp->w_dotp) == 0 || inwhitep)
		return (selfinsert(f, n));

	return (cc_indent(FFRAND, 1));
}

/*
 * Attempt to indent current line according to KNF rules.
 */
int
cc_indent(int f, int n)
{
	int pi, mi;			/* Previous indents */
	int ci, dci;			/* current indent, don't care */
	int lo;
	int c;
	int nonblankp;
	struct line *lp;

	if (n < 0)
		return (FALSE);

	if (cc_strip_trailp)
		deltrailwhite(FFRAND, 1);

	/* Search backwards for a nonblank, non preprocessor line */
	lp = curwp->w_dotp;
	nonblankp = FALSE;
	while (lback(lp) != curbp->b_headp && !nonblankp) {
		lp = lback(lp);
		for (lo = 0; lo < llength(lp); lo++) {
			if (!isspace(c = lgetc(lp, lo))) {
				/* Leading # is a blank */
				if (c != '#')	
					nonblankp = TRUE;
				break;
			}
		}
	}

	pi = getindent(lp, &mi);

	/* Strip leading space on current line */
	delleadwhite(FFRAND, 1);
	/* current indent is computed only to current position */
	dci = getindent(curwp->w_dotp, &ci);
	
	if (pi + ci < 0)
		return(indent(FFOTHARG, 0));
	else
		return(indent(FFOTHARG, pi + ci));
}

/*
 * Indent-and-newline (technically, newline then indent)
 */
int
cc_lfindent(int f, int n)
{
	if (n < 0)
		return (FALSE);
	if (newline(FFRAND, 1) == FALSE)
		return (FALSE);
	return (cc_indent(FFRAND, n));
}

/*
 * Get the level of indention after line lp is processed
 * Note getindent has two returns: curi, nexti.
 * curi = value if indenting current line.
 * return value = value affecting subsequent lines.
 * note, we only process up to offset op.
 * set to llength(lp) for the whole line.
 */
static int
getindent(const struct line *lp, int *curi)
{
	int lo, co;		/* leading space,  current offset*/
	int nicol = 0;		/* position count */
	int c = '\0';		/* current char */
	int newind = 0;		/* new index value */
	int stringp = FALSE;	/* in string? */
	int escp = FALSE;	/* Escape char? */
	int lastc = '\0';	/* Last matched string delimeter */
	int nparen = 0;		/* paren count */
	int obrace = 0;		/* open brace count */
	int cbrace = 0;		/* close brace count */
	int contp = FALSE;	/* Continue? */
	int firstseenp = FALSE;	/* First nonspace encountered? */
	int colonp = FALSE;	/* Did we see a colon? */
	int questionp = FALSE;	/* Did we see a question mark? */
	
	*curi = 0;

	/* Compute leading space */
	for (lo = 0; lo < llength(lp); lo++) {
		if (!isspace(c = lgetc(lp, lo)))
			break;
		if (c == '\t'
#ifdef NOTAB
		    && !(curbp-b_flag & BFNOTAB)
#endif /* NOTAB */
		    ) {
			nicol |= 0x07;
		}
		nicol++;
	}

	/* If last line was blank, choose 0 */
	if (lo == llength(lp))
		nicol = 0;

	if (c == '#')
		return (0);

	newind = 0;
	/* Compute modifiers */
	for (co = lo; co < llength(lp); co++) {
		c = lgetc(lp, co);
		/* We have a non-whitespace char */
		if (!firstseenp && !isspace(c)) {
			contp = TRUE;
			firstseenp = TRUE; 
		}
		if (c == '\\')
			escp = !escp;
		else if (stringp) {
			if (!escp && (c == '"' || c == '\'')) {
				/* unescaped string char */
				if (getmatch(c, lastc))
					stringp = FALSE;
			}
		} else if (c == '"' || c == '\'') {
			stringp = TRUE;
			lastc = c;
		} else if (c == '(') {
			nparen++;
		} else if (c == ')') {
			nparen--;
		} else if (c == '{') {
			obrace++;
			firstseenp = FALSE;
			contp = FALSE;
		} else if (c == '}') {
			cbrace++;
		} else if (c == '?') {
			questionp = TRUE;
		} else if (c == ':') {
			/* ignore (foo ? bar : baz) construct */
			if (!questionp)
				colonp = TRUE;
		} else if (c == ';') {
			if (nparen > 0)
				contp = FALSE;
		}

		/* Reset escape character match */
		if (c != '\\')
			escp = FALSE;
	}
	/*
	 * If not terminated with a semicolon, and brace or paren open.
	 * we continue
	 */
	if (colonp) {
		*curi += cc_colon_indent;
		newind -= cc_colon_indent;
	}

	*curi -= (cbrace) * cc_basic_indent;
	newind += obrace * cc_basic_indent;

	if (nparen < 0)
		newind -= cc_cont_indent;
	else if (nparen > 0)
		newind += cc_cont_indent;
	newind += nicol;
	*curi += nicol;

	return (newind);
}

/*
 * Given a delimeter and its purported mate, tell us if they
 * match.
 */
static int
getmatch(int c, int mc)
{
	int match = FALSE;

	switch (c) {
	case '"':
		match = (mc == '"');
		break;
	case '\'':
		match = (mc == '\'');
		break;
	case '(':
		match = (mc == ')');
		break;
	case '[':
		match = (mc == ']');
		break;
	case '{':
		match = (mc == '}');
		break;
	}

	return (match);
}