-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmode.c
515 lines (448 loc) · 10.3 KB
/
cmode.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* $OpenBSD: cmode.c,v 1.22 2023/04/21 13:39:37 op Exp $ */
/*
* This file is in the public domain.
*
* Author: Kjell Wooding <[email protected]>
*/
/*
* Implement an non-irritating KNF-compliant mode for editing
* C code.
*/
#include <sys/queue.h>
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include "def.h"
#include "funmap.h"
#include "kbd.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 *);
static int in_whitespace(struct line *, int);
static int findcolpos(const struct buffer *, const struct line *, int);
static struct line *findnonblank(struct line *);
static int isnonblank(const struct line *, int);
void cmode_init(void);
int cc_comment(int, int);
/* Keymaps */
static PF cmode_brace[] = {
cc_brace, /* } */
};
static PF cmode_cCP[] = {
compile, /* C-c P */
};
static PF cmode_cc[] = {
NULL, /* ^C */
rescan, /* ^D */
rescan, /* ^E */
rescan, /* ^F */
rescan, /* ^G */
rescan, /* ^H */
cc_tab, /* ^I */
rescan, /* ^J */
rescan, /* ^K */
rescan, /* ^L */
cc_lfindent, /* ^M */
};
static PF cmode_spec[] = {
cc_char, /* : */
};
static struct KEYMAPE (1) cmode_cmap = {
1,
1,
rescan,
{
{ 'P', 'P', cmode_cCP, NULL }
}
};
static struct KEYMAPE (3) cmodemap = {
3,
3,
rescan,
{
{ CCHR('C'), CCHR('M'), cmode_cc, (KEYMAP *) &cmode_cmap },
{ ':', ':', cmode_spec, NULL },
{ '}', '}', cmode_brace, NULL }
}
};
/* Function, Mode hooks */
void
cmode_init(void)
{
funmap_add(cmode, "c-mode", 0);
funmap_add(cc_char, "c-handle-special-char", 0);
funmap_add(cc_brace, "c-handle-special-brace", 0);
funmap_add(cc_tab, "c-tab-or-indent", 0);
funmap_add(cc_indent, "c-indent", 0);
funmap_add(cc_lfindent, "c-indent-and-newline", 0);
maps_add((KEYMAP *)&cmodemap, "c");
}
/*
* Enable/toggle c-mode
*/
int
cmode(int f, int n)
{
return(changemode(f, n, "c"));
}
/*
* 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));
}
/*
* Handle special C character - selfinsert then indent.
*/
int
cc_brace(int f, int n)
{
if (n < 0)
return (FALSE);
if (showmatch(FFRAND, 1) == 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 inwhitep = FALSE; /* In leading whitespace? */
inwhitep = in_whitespace(curwp->w_dotp, llength(curwp->w_dotp));
/* 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 (mi is ignored) */
int ci; /* current indent */
struct line *lp;
int ret;
if (n < 0)
return (FALSE);
undo_boundary_enable(FFRAND, 0);
if (cc_strip_trailp)
deltrailwhite(FFRAND, 1);
/*
* Search backwards for a non-blank, non-preprocessor,
* non-comment line
*/
lp = findnonblank(curwp->w_dotp);
pi = getindent(lp, &mi);
/* Strip leading space on current line */
delleadwhite(FFRAND, 1);
/* current indent is computed only to current position */
(void)getindent(curwp->w_dotp, &ci);
if (pi + ci < 0)
ret = indent(FFOTHARG, 0);
else
ret = indent(FFOTHARG, pi + ci);
undo_boundary_enable(FFRAND, 1);
return (ret);
}
/*
* Indent-and-newline (technically, newline then indent)
*/
int
cc_lfindent(int f, int n)
{
if (n < 0)
return (FALSE);
if (cc_strip_trailp)
(void)delwhite(FFRAND, 1);
if (enewline(FFRAND, 1) == FALSE)
return (FALSE);
return (cc_indent(FFRAND, n));
}
/*
* Get the level of indentation after line lp is processed
* Note getindent has two returns:
* curi = value if indenting current line.
* return value = value affecting subsequent lines.
*/
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 delimiter */
int nparen = 0; /* paren count */
int obrace = 0; /* open brace count */
int cbrace = 0; /* close brace count */
int firstnwsp = FALSE; /* First nonspace encountered? */
int colonp = FALSE; /* Did we see a colon? */
int questionp = FALSE; /* Did we see a question mark? */
int slashp = FALSE; /* Slash? */
int astp = FALSE; /* Asterisk? */
int cpos = -1; /* comment position */
int cppp = FALSE; /* Preprocessor command? */
*curi = 0;
/* Compute leading space */
for (lo = 0; lo < llength(lp); lo++) {
if (!isspace(c = lgetc(lp, lo)))
break;
if (c == '\t')
nicol = ntabstop(nicol, curbp->b_tabw);
else
nicol++;
}
/* If last line was blank, choose 0 */
if (lo == llength(lp))
nicol = 0;
newind = 0;
/* Compute modifiers */
for (co = lo; co < llength(lp); co++) {
c = lgetc(lp, co);
/* We have a non-whitespace char */
if (!firstnwsp && !isspace(c)) {
if (c == '#')
cppp = TRUE;
firstnwsp = 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++;
firstnwsp = 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 == '/') {
/* first nonwhitespace? -> indent */
if (firstnwsp) {
/* If previous char asterisk -> close */
if (astp)
cpos = -1;
else
slashp = TRUE;
}
} else if (c == '*') {
/* If previous char slash -> open */
if (slashp)
cpos = co;
else
astp = TRUE;
} else if (firstnwsp) {
firstnwsp = FALSE;
}
/* Reset matches that apply to next character only */
if (c != '\\')
escp = FALSE;
if (c != '*')
astp = FALSE;
if (c != '/')
slashp = 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;
*curi += nicol;
/* Ignore preprocessor. Otherwise, add current column */
if (cppp) {
newind = nicol;
*curi = 0;
} else {
newind += nicol;
}
if (cpos != -1)
newind = findcolpos(curbp, lp, cpos);
return (newind);
}
/*
* Given a delimiter 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);
}
static int
in_whitespace(struct line *lp, int len)
{
int lo;
int inwhitep = FALSE;
for (lo = 0; lo < len; lo++) {
if (!isspace(lgetc(lp, lo)))
break;
if (lo == len - 1)
inwhitep = TRUE;
}
return (inwhitep);
}
/* convert a line/offset pair to a column position (for indenting) */
static int
findcolpos(const struct buffer *bp, const struct line *lp, int lo)
{
int col, i, c;
char tmp[5];
/* determine column */
col = 0;
for (i = 0; i < lo; ++i) {
c = lgetc(lp, i);
if (c == '\t') {
col = ntabstop(col, curbp->b_tabw);
} else if (ISCTRL(c) != FALSE)
col += 2;
else if (isprint(c)) {
col++;
} else {
col += snprintf(tmp, sizeof(tmp), "\\%o", c);
}
}
return (col);
}
/*
* Find a non-blank line, searching backwards from the supplied line pointer.
* For C, nonblank is non-preprocessor, non C++, and accounts
* for complete C-style comments.
*/
static struct line *
findnonblank(struct line *lp)
{
int lo;
int nonblankp = FALSE;
int commentp = FALSE;
int slashp;
int astp;
int c;
while (lback(lp) != curbp->b_headp && (commentp || !nonblankp)) {
lp = lback(lp);
slashp = FALSE;
astp = FALSE;
/* Potential nonblank? */
nonblankp = isnonblank(lp, llength(lp));
/*
* Search from end, removing complete C-style
* comments. If one is found, ignore it and
* test for nonblankness from where it starts.
*/
for (lo = llength(lp) - 1; lo >= 0; lo--) {
if (!isspace(c = lgetc(lp, lo))) {
if (commentp) { /* find comment "open" */
if (c == '*')
astp = TRUE;
else if (astp && c == '/') {
commentp = FALSE;
/* whitespace to here? */
nonblankp = isnonblank(lp, lo);
}
} else { /* find comment "close" */
if (c == '/')
slashp = TRUE;
else if (slashp && c == '*')
/* found a comment */
commentp = TRUE;
}
}
}
}
/* Rewound to start of file? */
if (lback(lp) == curbp->b_headp && !nonblankp)
return (curbp->b_headp);
return (lp);
}
/*
* Given a line, scan forward to 'omax' and determine if we
* are all C whitespace.
* Note that preprocessor directives and C++-style comments
* count as whitespace. C-style comments do not, and must
* be handled elsewhere.
*/
static int
isnonblank(const struct line *lp, int omax)
{
int nonblankp = FALSE; /* Return value */
int slashp = FALSE; /* Encountered slash */
int lo; /* Loop index */
int c; /* char being read */
/* Scan from front for preprocessor, C++ comments */
for (lo = 0; lo < omax; lo++) {
if (!isspace(c = lgetc(lp, lo))) {
/* Possible nonblank line */
nonblankp = TRUE;
/* skip // and # starts */
if (c == '#' || (slashp && c == '/')) {
nonblankp = FALSE;
break;
} else if (!slashp && c == '/') {
slashp = TRUE;
continue;
}
}
slashp = FALSE;
}
return (nonblankp);
}