-
Notifications
You must be signed in to change notification settings - Fork 1
/
langmode_python.c
119 lines (90 loc) · 1.55 KB
/
langmode_python.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
/*
* This file is in the public domain.
*
* Author: Pedro A. Aranda <[email protected]>
*
* Modified by: Bengt Larsson.
*
*/
/*
* Implement a mode for editing Python code.
*/
#include "def.h"
#ifdef LANGMODE_PYTHON
static int
is_blank(INT c)
{
return (c == ' ') || (c == '\t');
}
static LINE *
findnonblank(LINE *lp)
{
while (lback(lp) != curbp->b_linep) {
INT curchar, offs;
lp = lback(lp);
for (offs = 0; offs < llength(lp); offs++) {
curchar = lgetc(lp,offs);
if (is_blank(curchar))
continue;
if (curchar != '#')
return lp;
break;
}
}
return lp;
}
/*
* Get the level of indention after line lp is processed
* return value = value affecting subsequent lines.
*/
static INT
_getpyindent(LINE *lp)
{
INT col, i, c;
if (getindent(lp, NULL, &col) == FALSE) return -1;
for (i = llength(lp) - 1; i >= 0; i--) {
c = lgetc(lp, i);
if (c == ':') {
if (col < MAXINT - 100) col += gettabsize(curbp);
break;
} else if (!is_blank(c)) {
break;
}
}
return col;
}
static INT
getpyindent(LINE *lp)
{
return _getpyindent(findnonblank(lp));
}
/*
* Enable/toggle python-mode
*/
INT
pymode(INT f, INT n)
{
return changemode(curbp, f, n, "python");
}
/*
* Attempt to indent current line
*/
INT
py_indent(INT f, INT n)
{
return setindent(getpyindent(curwp->w_dotp));
}
/*
* Python newline and indent
*/
INT
py_lfindent(INT f, INT n)
{
if (n < 0) return FALSE;
while (n-- > 0) {
if (lnewline() == FALSE) return FALSE;
if (py_indent(FFRAND, 1) == FALSE) return FALSE;
}
return TRUE;
}
#endif