-
Notifications
You must be signed in to change notification settings - Fork 1
/
langmode_c.c
473 lines (407 loc) · 9.75 KB
/
langmode_c.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
/* $OpenBSD: cmode.c,v 1.8 2012/05/18 02:13:44 lum Exp $ */
/*
* This file is in the public domain.
*
* Author: Kjell Wooding <[email protected]>
*
* Modified for Mg3a by Bengt Larsson and Pedro Andres Aranda
* Gutierrez. Still public domain.
*/
/*
* Implement an non-irritating KNF-compliant mode for editing
* C code.
*
* paaguti: TODO: integrate comment_begin and comment_end variables
*/
#include "def.h"
#ifdef LANGMODE_C
INT cc_strip_trailp = TRUE; /* Delete Trailing space? */
INT cc_basic_indent = 8; /* Basic Indent multiple */
INT cc_cont_indent = 4; /* Continued line indent */
INT cc_colon_indent = -8; /* Label / case indent */
static INT getmatch(INT, INT);
static INT cc_getindent(LINE *, INT *);
static INT in_whitespace(LINE *, INT);
static INT findcolpos(BUFFER *, LINE *, INT);
static LINE *findnonblank(LINE *);
static INT isnonblank(LINE *, INT);
INT cc_comment(INT, INT);
INT cc_indent(INT, INT);
INT selfinsert(INT, INT);
INT showmatch(INT, INT);
/* Mg3a: begin compatibility functions */
static int
is_space(INT c)
{
return c == ' ' || c == '\t';
}
/* Mg3a: end compatibility functions */
/*
* Enable/toggle c-mode
*/
INT cmode(INT f, INT n)
{
return changemode(curbp, f, n, "c");
}
/*
* Handle preprocessor char '#'
* if the line is empty (no chars or all blanks),
* insert at the beginning of the line
*/
INT
cc_preproc(INT f, INT n)
{
if (n < 0) return FALSE;
if (isblankline(curwp->w_dotp)) {
if (delleadwhite() == FALSE) return FALSE;
}
return linsert(n, '#');
}
/*
* Handle special C character - selfinsert then indent.
*/
INT
cc_char(INT f, INT n)
{
if (selfinsert(f|FFRAND, n) == FALSE) return (FALSE);
return (cc_indent(FFRAND, n));
}
/*
* Handle special C character - selfinsert, show matching char, 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|FFRAND, 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;
// INT dci; /* current indent, don't care */ // Not used
LINE *lp;
INT ret;
if (n < 0)
return (FALSE);
// undo_boundary_enable(FFRAND, 0);
if (cc_strip_trailp)
deltrailwhite();
/*
* Search backwards for a non-blank, non-preprocessor,
* non-comment line
*/
lp = findnonblank(curwp->w_dotp);
pi = cc_getindent(lp, &mi);
/* Strip leading space on current line */
if (delleadwhite() == FALSE) return FALSE;
/* current indent is computed only to current position */
// dci = cc_getindent(curwp->w_dotp, &ci); // Not used
cc_getindent(curwp->w_dotp, &ci);
if (pi + ci < 0)
ret = setindent(0);
else
ret = setindent(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 (lnewline() == FALSE)
return (FALSE);
return (cc_indent(FFRAND, n));
}
/*
* Get the level of indention after line lp is processed
* Note cc_getindent has two returns:
* curi = value if indenting current line.
* return value = value affecting subsequent lines.
*/
static INT
cc_getindent(LINE *lp, INT *curi)
{
INT lo, co; /* leading space, current offset*/
INT nicol = 0; /* position count */
// INT ccol = 0; /* current column */ // Not used
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? */ // Not used
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 (!is_space((c = lgetc(lp, lo))))
break;
if (c == '\t') {
nicol |= 0x07;
}
nicol++;
}
/* If last line was blank, choose 0 */
if (lo == llength(lp))
nicol = 0;
newind = 0;
// ccol = nicol; /* current column */ // Not used
/* Compute modifiers */
for (co = lo; co < llength(lp); co++) {
c = lgetc(lp, co);
/* We have a non-whitespace char */
if (!firstnwsp && !is_space(c)) {
// contp = TRUE; // Not used
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;
// contp = FALSE; // Not used
} 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; // Not used
} 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 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);
}
static INT
in_whitespace(LINE *lp, INT len)
{
INT lo;
INT inwhitep = FALSE;
for (lo = 0; lo < len; lo++) {
if (!is_space(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(BUFFER *bp, LINE *lp, INT lo)
{
return getcolumn(curwp, lp, lo); // Only called with curbp
}
/*
* 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 LINE *
findnonblank(LINE *lp)
{
INT lo;
INT nonblankp = FALSE;
INT commentp = FALSE;
INT slashp;
INT astp;
INT c;
while (lback(lp) != curbp->b_linep && (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.
*/
slashp = FALSE;
/* Scan backwards from end to find C-style comment */
for (lo = llength(lp) - 1; lo >= 0; lo--) {
if (!is_space((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_linep && !nonblankp)
return (curbp->b_linep);
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(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 (!is_space((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);
}
#endif /* LANGMODE_C */