File: editor.c

package info (click to toggle)
dosemu-freedos 1%3A0.0.b9r5a%2Betch.1-0etch1
  • links: PTS
  • area: contrib
  • in suites: etch
  • size: 19,744 kB
  • ctags: 23,279
  • sloc: ansic: 143,864; asm: 20,397; makefile: 3,868; perl: 1,106; yacc: 690; sh: 553; pascal: 297; xml: 150; cpp: 67
file content (245 lines) | stat: -rw-r--r-- 5,876 bytes parent folder | download | duplicates (2)
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
/* ------------- editor.c ------------ */
#include "dflat.h"

#define pTab ('\t' + 0x80)
#define sTab ('\f' + 0x80)

/* ---------- SETTEXT Message ------------ */
static int SetTextMsg(WINDOW wnd, char *Buf)
{
    unsigned char *tp,*ep,*ttp;
    int x=0,sz=0,rtn;

    tp=Buf;

    /* Compute the buffer size based on tabs in the text */
    while (*tp)
        {
        if (*tp == '\t')
            {
            /* Tab, adjust the buffer length */
            int sps=cfg.Tabs-(x % cfg.Tabs);

            sz+=sps;
            x+=sps;
            }
        else
            {
            /* Not a tab, count the character */
            sz++;
            x++;
            }

        if (*tp == '\n')
            x=0;                        /* Newline, reset x */

        tp++;
        }

    ep=DFcalloc(1, sz+1);               /* Allocate a buffer */

    /* Detab the input file */
    tp=Buf;
    ttp=ep;
    x=0;
    while (*tp)
        {
        x++;                            /* Put the character (\t, too) into the buffer */
        /* Expand tab into subst tab (\f + 0x80) and expansions (\t + 0x80) */
        if (*tp == '\t')
            {
            *ttp++ = sTab;              /* Substitute tab character */
            while ((x % cfg.Tabs) != 0)
                *ttp++ = pTab, x++;

            }
        else
            {
            *ttp++ = *tp;
            if (*tp == '\n')
                x=0;
            }

        tp++;
        }

    *ttp='\0';
    rtn=BaseWndProc(EDITOR, wnd, SETTEXT, (PARAM) ep, 0);
    free(ep);
    return rtn;

}
void CollapseTabs(WINDOW wnd)
{
    unsigned char *cp = wnd->text, *cp2;

    while (*cp)
        {
        if (*cp == pTab)
            {
            *cp='\t';
            cp2=cp;
            while (*++cp2 == sTab);
                memmove(cp+1, cp2, strlen(cp2)+1);

            }

    	cp++;
        }

}

void ExpandTabs(WINDOW wnd)
{
    int Holdwtop = wnd->wtop, Holdwleft = wnd->wleft, HoldRow = wnd->CurrLine, HoldCol = wnd->CurrCol, HoldwRow = wnd->WndRow;

    SendMessage(wnd, SETTEXT, (PARAM) wnd->text, 0);
    wnd->wtop=Holdwtop;
    wnd->wleft=Holdwleft;
    wnd->CurrLine=HoldRow;
    wnd->CurrCol=HoldCol;
    wnd->WndRow=HoldwRow;
    SendMessage(wnd, PAINT, 0, 0);
    SendMessage(wnd, KEYBOARD_CURSOR, 0, wnd->WndRow);

}

/* --- When inserting or deleting, adjust next following tab, same line --- */
static void AdjustTab(WINDOW wnd)
{
    int col=wnd->CurrCol;

    while (*CurrChar && *CurrChar != '\n')
        {
        if (*CurrChar == sTab)
            {
            int exp=(cfg.Tabs-1)-(wnd->CurrCol % cfg.Tabs);

            wnd->CurrCol++;
            while (*CurrChar == pTab)
                BaseWndProc(EDITOR, wnd, KEYBOARD, DEL, 0);

            while (exp--)
                BaseWndProc(EDITOR, wnd, KEYBOARD, pTab, 0);

            break;
            }

        wnd->CurrCol++;
        }

    wnd->CurrCol=col;

}

static void TurnOffDisplay(WINDOW wnd)
{
    SendMessage(NULL, HIDE_CURSOR, 0, 0);
    ClearVisible(wnd);

}

static void TurnOnDisplay(WINDOW wnd)
{
    SetVisible(wnd);
    SendMessage(NULL, SHOW_CURSOR, 0, 0);

}

static void RepaintLine(WINDOW wnd)
{
    SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
    WriteTextLine(wnd, NULL, wnd->CurrLine, FALSE);
}

/* --------- KEYBOARD Message ---------- */
static int KeyboardMsg(WINDOW wnd, PARAM p1, PARAM p2)
{
    int c=(int) p1;
    BOOL delnl;
    PARAM pn=p1;

    if (WindowMoving || WindowSizing || ((int)p2 & ALTKEY))
        return FALSE;

    switch (c)
        {
        case PGUP:
        case PGDN:
        case UP:
        case DN:
            pn=(PARAM) BS;
        case FWD:
        case LARROW:
            TurnOffDisplay(wnd);
            BaseWndProc(EDITOR, wnd, KEYBOARD, p1, p2);
            while (*CurrChar == pTab)
                BaseWndProc(EDITOR, wnd, KEYBOARD, pn, p2);

            TurnOnDisplay(wnd);
            return TRUE;
        case DEL:
            TurnOffDisplay(wnd);
            delnl = *CurrChar == '\n' || TextBlockMarked(wnd);
            BaseWndProc(EDITOR, wnd, KEYBOARD, p1, p2);
            while (*CurrChar == pTab)
                BaseWndProc(EDITOR, wnd, KEYBOARD, p1, p2);

            AdjustTab(wnd);
            TurnOnDisplay(wnd);
            RepaintLine(wnd);
            if (delnl)
                SendMessage(wnd, PAINT, 0, 0);

            return TRUE;
        case '\t':
            TurnOffDisplay(wnd);
            BaseWndProc(EDITOR, wnd, KEYBOARD, (PARAM) sTab, p2);
            while ((wnd->CurrCol % cfg.Tabs) != 0)
                BaseWndProc(EDITOR, wnd, KEYBOARD, pTab, p2);

            TurnOnDisplay(wnd);
            RepaintLine(wnd);
            return TRUE;
        default:
            if (((c & FKEY) == 0) && (isprint(c) || c == '\r'))
                {
                TurnOffDisplay(wnd);
                BaseWndProc(EDITOR, wnd, KEYBOARD, p1, p2);
                AdjustTab(wnd);
                TurnOnDisplay(wnd);
                RepaintLine(wnd);
                if (c == '\r')
                    SendMessage(wnd, PAINT, 0, 0);

                return TRUE;
                }

            break;

        }

    return FALSE;

}

/* ------- Window processing module for EDITBOX class ------ */
int EditorProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
    switch (msg)
        {
        case KEYBOARD:
            if (KeyboardMsg(wnd, p1, p2))
                return TRUE;

            break;
        case SETTEXT:
            return SetTextMsg(wnd, (char *) p1);
        default:
            break;

        }

    return BaseWndProc(EDITOR, wnd, msg, p1, p2);

}