Subversion Repositories SvarDOS

Rev

Rev 1308 | Rev 1310 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1308 Rev 1309
1
/* Sved, the SvarDOS editor
1
/* Sved, the SvarDOS editor
2
 *
2
 *
3
 * Copyright (C) 2023 Mateusz Viste
3
 * Copyright (C) 2023 Mateusz Viste
4
 *
4
 *
5
 * Sved is released under the terms of the MIT license.
5
 * Sved is released under the terms of the MIT license.
6
 *
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to
8
 * of this software and associated documentation files (the "Software"), to
9
 * deal in the Software without restriction, including without limitation the
9
 * deal in the Software without restriction, including without limitation the
10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
 * sell copies of the Software, and to permit persons to whom the Software is
11
 * sell copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
12
 * furnished to do so, subject to the following conditions:
13
 *
13
 *
14
 * The above copyright notice and this permission notice shall be included in
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
15
 * all copies or substantial portions of the Software.
16
 *
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
 * IN THE SOFTWARE.
23
 * IN THE SOFTWARE.
24
 */
24
 */
25
 
25
 
26
#include <dos.h>
26
#include <dos.h>
27
#include <fcntl.h>
27
#include <fcntl.h>
28
#include <stdlib.h>
28
#include <stdlib.h>
29
#include <string.h>
29
#include <string.h>
30
#include <malloc.h>   /* _fcalloc() */
30
#include <malloc.h>   /* _fcalloc() */
31
 
31
 
32
#include "mdr\cout.h"
32
#include "mdr\cout.h"
33
#include "mdr\dos.h"
33
#include "mdr\dos.h"
34
#include "mdr\keyb.h"
34
#include "mdr\keyb.h"
35
 
35
 
36
#include "svarlang\svarlang.h"
36
#include "svarlang\svarlang.h"
37
 
37
 
38
#define COL_TXT        0
38
#define COL_TXT        0
39
#define COL_STATUSBAR1 1
39
#define COL_STATUSBAR1 1
40
#define COL_STATUSBAR2 2
40
#define COL_STATUSBAR2 2
41
#define COL_SCROLLBAR  3
41
#define COL_SCROLLBAR  3
42
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
42
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
43
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
43
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
44
 
44
 
45
#define SCROLL_CURSOR 0xB1
45
#define SCROLL_CURSOR 0xB1
46
 
46
 
47
 
47
 
48
struct line {
48
struct line {
49
  struct line far *prev;
49
  struct line far *prev;
50
  struct line far *next;
50
  struct line far *next;
51
  unsigned short len;
51
  unsigned short len;
52
  char payload[1];
52
  char payload[1];
53
};
53
};
54
 
54
 
55
struct linedb {
55
struct linedb {
56
  struct line far *topscreen;
56
  struct line far *topscreen;
57
  struct line far *cursor;
57
  struct line far *cursor;
58
  unsigned short xoffset;
58
  unsigned short xoffset;
59
};
59
};
60
 
60
 
61
 
61
 
62
/* returns non-zero on error */
62
/* returns non-zero on error */
63
static int line_add(struct linedb *db, const char far *line) {
63
static int line_add(struct linedb *db, const char far *line) {
64
  unsigned short slen;
64
  unsigned short slen;
65
  struct line far *l;
65
  struct line far *l;
66
 
66
 
67
  /* slen = strlen(line) (but for far pointer) */
67
  /* slen = strlen(line) (but for far pointer) */
68
  for (slen = 0; line[slen] != 0; slen++);
68
  for (slen = 0; line[slen] != 0; slen++);
69
 
69
 
70
  /* trim out CR/LF line endings */
70
  /* trim out CR/LF line endings */
71
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
71
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
72
    slen -= 2;
72
    slen -= 2;
73
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
73
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
74
    slen--;
74
    slen--;
75
  }
75
  }
76
 
76
 
77
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
77
  l = _fcalloc(1, sizeof(struct line) + slen + 1);
78
  if (l == NULL) return(-1);
78
  if (l == NULL) return(-1);
79
 
79
 
80
  l->prev = db->cursor;
80
  l->prev = db->cursor;
81
  if (db->cursor) {
81
  if (db->cursor) {
82
    l->next = db->cursor->next;
82
    l->next = db->cursor->next;
83
    db->cursor->next = l;
83
    db->cursor->next = l;
84
    l->next->prev = l;
84
    l->next->prev = l;
85
  }
85
  }
86
  db->cursor = l;
86
  db->cursor = l;
87
  _fmemcpy(l->payload, line, slen);
87
  _fmemcpy(l->payload, line, slen);
88
  l->len = slen;
88
  l->len = slen;
89
 
89
 
90
  return(0);
90
  return(0);
91
}
91
}
92
 
92
 
93
 
93
 
94
static void db_rewind(struct linedb *db) {
94
static void db_rewind(struct linedb *db) {
95
  if (db->cursor == NULL) return;
95
  if (db->cursor == NULL) return;
96
  while (db->cursor->prev) db->cursor = db->cursor->prev;
96
  while (db->cursor->prev) db->cursor = db->cursor->prev;
97
  db->topscreen = db->cursor;
97
  db->topscreen = db->cursor;
98
}
98
}
99
 
99
 
100
 
100
 
101
static void load_colorscheme(void) {
101
static void load_colorscheme(void) {
102
  scheme[COL_TXT] = 0x17;
102
  scheme[COL_TXT] = 0x17;
103
  scheme[COL_STATUSBAR1] = 0x70;
103
  scheme[COL_STATUSBAR1] = 0x70;
104
  scheme[COL_STATUSBAR2] = 0x78;
104
  scheme[COL_STATUSBAR2] = 0x78;
105
  scheme[COL_SCROLLBAR] = 0x70;
105
  scheme[COL_SCROLLBAR] = 0x70;
106
}
106
}
107
 
107
 
108
 
108
 
109
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
109
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
110
  unsigned char i;
110
  unsigned char i;
111
  const char *s = svarlang_strid(0); /* HELP */
111
  const char *s = svarlang_strid(0); /* HELP */
112
  unsigned char helpcol = screenw - (strlen(s) + 4);
112
  unsigned char helpcol = screenw - (strlen(s) + 4);
113
 
113
 
114
  /* clear screen */
114
  /* clear screen */
115
  mdr_cout_cls(scheme[COL_TXT]);
115
  mdr_cout_cls(scheme[COL_TXT]);
116
 
116
 
117
  /* status bar */
117
  /* status bar */
118
  for (i = 0; i < helpcol; i++) {
118
  for (i = 0; i < helpcol; i++) {
119
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
119
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
120
    if (*fname != 0) fname++;
120
    if (*fname != 0) fname++;
121
  }
121
  }
122
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
122
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
123
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
123
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
124
 
124
 
125
  /* scroll bar */
125
  /* scroll bar */
126
  for (i = 0; i < (screenh - 1); i++) {
126
  for (i = 0; i < (screenh - 1); i++) {
127
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
127
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
128
  }
128
  }
129
}
129
}
130
 
130
 
131
 
131
 
-
 
132
static void ui_help(unsigned char screenw) {
-
 
133
#define MAXLINLEN 35
-
 
134
  unsigned short i, x, offset;
-
 
135
  offset = (screenw - MAXLINLEN + 1) >> 1;
-
 
136
  mdr_cout_cursor_hide();
-
 
137
  for (i = 2; i <= 12; i++) {
-
 
138
    for (x = offset - 2; x < offset + MAXLINLEN; x++) mdr_cout_char(i, x, ' ', scheme[COL_STATUSBAR1]);
-
 
139
  }
-
 
140
 
-
 
141
  mdr_cout_str(3, offset, svarlang_str(0, 0), scheme[COL_STATUSBAR1], MAXLINLEN);
-
 
142
  for (i = 0; i <= 2; i++) {
-
 
143
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), scheme[COL_STATUSBAR1], MAXLINLEN);
-
 
144
  }
-
 
145
  mdr_cout_str(9, offset, svarlang_str(8, 10), scheme[COL_STATUSBAR1], MAXLINLEN);
-
 
146
  mdr_cout_str(11, offset, svarlang_str(8, 11), scheme[COL_STATUSBAR1], MAXLINLEN);
-
 
147
 
-
 
148
  keyb_getkey();
-
 
149
  mdr_cout_cursor_show();
-
 
150
#undef MAXLINLEN
-
 
151
}
-
 
152
 
-
 
153
 
132
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
154
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
133
  unsigned char y = 0;
155
  unsigned char y = 0;
134
  unsigned char len;
156
  unsigned char len;
135
  struct line far *l;
157
  struct line far *l;
136
 
158
 
137
#ifdef DBG_REFRESH
159
#ifdef DBG_REFRESH
138
  static char m = 'a';
160
  static char m = 'a';
139
  m++;
161
  m++;
140
  if (m > 'z') m = 'a';
162
  if (m > 'z') m = 'a';
141
#endif
163
#endif
142
 
164
 
143
  for (l = db->topscreen; l != NULL; l = l->next, y++) {
165
  for (l = db->topscreen; l != NULL; l = l->next, y++) {
144
 
166
 
145
    /* skip lines that do not to be refreshed */
167
    /* skip lines that do not to be refreshed */
146
    if (y < uidirtyfrom) continue;
168
    if (y < uidirtyfrom) continue;
147
    if (y > uidirtyto) break;
169
    if (y > uidirtyto) break;
148
 
170
 
149
    if (db->xoffset < l->len) {
171
    if (db->xoffset < l->len) {
150
      for (len = 0; l->payload[len] != 0; len++) mdr_cout_char(y, len, l->payload[len], scheme[COL_TXT]);
172
      for (len = 0; l->payload[len] != 0; len++) mdr_cout_char(y, len, l->payload[len], scheme[COL_TXT]);
151
    } else {
173
    } else {
152
      len = 0;
174
      len = 0;
153
    }
175
    }
154
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
176
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
155
 
177
 
156
#ifdef DBG_REFRESH
178
#ifdef DBG_REFRESH
157
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
179
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
158
#endif
180
#endif
159
 
181
 
160
    if (y == screenh - 2) break;
182
    if (y == screenh - 2) break;
161
  }
183
  }
162
}
184
}
163
 
185
 
164
 
186
 
165
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
187
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
166
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
188
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
167
 
189
 
168
  if (db->cursor->len < db->xoffset) {
190
  if (db->cursor->len < db->xoffset) {
169
    *cursorpos = 0;
191
    *cursorpos = 0;
170
    db->xoffset = db->cursor->len;
192
    db->xoffset = db->cursor->len;
171
    *uidirtyfrom = 0;
193
    *uidirtyfrom = 0;
172
    *uidirtyto = 0xff;
194
    *uidirtyto = 0xff;
173
  } else {
195
  } else {
174
    *cursorpos = db->cursor->len - db->xoffset;
196
    *cursorpos = db->cursor->len - db->xoffset;
175
  }
197
  }
176
}
198
}
177
 
199
 
178
 
200
 
179
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
201
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
180
  if (db->cursor->prev != NULL) {
202
  if (db->cursor->prev != NULL) {
181
    db->cursor = db->cursor->prev;
203
    db->cursor = db->cursor->prev;
182
    if (*cursorposy == 0) {
204
    if (*cursorposy == 0) {
183
      db->topscreen = db->cursor;
205
      db->topscreen = db->cursor;
184
      *uidirtyfrom = 0;
206
      *uidirtyfrom = 0;
185
      *uidirtyto = 0xff;
207
      *uidirtyto = 0xff;
186
    } else {
208
    } else {
187
      *cursorposy -= 1;
209
      *cursorposy -= 1;
188
    }
210
    }
189
  }
211
  }
190
}
212
}
191
 
213
 
192
 
214
 
193
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
215
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
194
  /* adjust xoffset to make sure eol is visible on screen */
216
  /* adjust xoffset to make sure eol is visible on screen */
195
  if (db->xoffset > db->cursor->len) {
217
  if (db->xoffset > db->cursor->len) {
196
    db->xoffset = db->cursor->len - 1;
218
    db->xoffset = db->cursor->len - 1;
197
    *uidirtyfrom = 0;
219
    *uidirtyfrom = 0;
198
    *uidirtyto = 0xff;
220
    *uidirtyto = 0xff;
199
  }
221
  }
200
 
222
 
201
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
223
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
202
    db->xoffset = db->cursor->len - screenw + 2;
224
    db->xoffset = db->cursor->len - screenw + 2;
203
    *uidirtyfrom = 0;
225
    *uidirtyfrom = 0;
204
    *uidirtyto = 0xff;
226
    *uidirtyto = 0xff;
205
  }
227
  }
206
  *cursorposx = db->cursor->len - db->xoffset;
228
  *cursorposx = db->cursor->len - db->xoffset;
207
}
229
}
208
 
230
 
209
 
231
 
210
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
232
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
211
  if (db->cursor->next != NULL) {
233
  if (db->cursor->next != NULL) {
212
    db->cursor = db->cursor->next;
234
    db->cursor = db->cursor->next;
213
    if (*cursorposy < screenh - 2) {
235
    if (*cursorposy < screenh - 2) {
214
      *cursorposy += 1;
236
      *cursorposy += 1;
215
    } else {
237
    } else {
216
      db->topscreen = db->topscreen->next;
238
      db->topscreen = db->topscreen->next;
217
      *uidirtyfrom = 0;
239
      *uidirtyfrom = 0;
218
      *uidirtyto = 0xff;
240
      *uidirtyto = 0xff;
219
    }
241
    }
220
  }
242
  }
221
}
243
}
222
 
244
 
223
 
245
 
224
static void cursor_left(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
246
static void cursor_left(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
225
  if (*cursorposx > 0) {
247
  if (*cursorposx > 0) {
226
    *cursorposx -= 1;
248
    *cursorposx -= 1;
227
  } else if (db->xoffset > 0) {
249
  } else if (db->xoffset > 0) {
228
    db->xoffset -= 1;
250
    db->xoffset -= 1;
229
    *uidirtyfrom = 0;
251
    *uidirtyfrom = 0;
230
    *uidirtyto = 0xff;
252
    *uidirtyto = 0xff;
231
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
253
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
232
    cursor_up(db, cursorposy, uidirtyfrom, uidirtyto);
254
    cursor_up(db, cursorposy, uidirtyfrom, uidirtyto);
233
    cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
255
    cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
234
  }
256
  }
235
}
257
}
236
 
258
 
237
 
259
 
238
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
260
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
239
  *cursorposx = 0;
261
  *cursorposx = 0;
240
  if (db->xoffset != 0) {
262
  if (db->xoffset != 0) {
241
    db->xoffset = 0;
263
    db->xoffset = 0;
242
    *uidirtyfrom = 0;
264
    *uidirtyfrom = 0;
243
    *uidirtyto = 0xff;
265
    *uidirtyto = 0xff;
244
  }
266
  }
245
}
267
}
246
 
268
 
247
 
269
 
248
static void cursor_right(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
270
static void cursor_right(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
249
  if (db->cursor->len > db->xoffset + *cursorposx) {
271
  if (db->cursor->len > db->xoffset + *cursorposx) {
250
    if (*cursorposx < screenw - 2) {
272
    if (*cursorposx < screenw - 2) {
251
      *cursorposx += 1;
273
      *cursorposx += 1;
252
    } else {
274
    } else {
253
      db->xoffset += 1;
275
      db->xoffset += 1;
254
      *uidirtyfrom = 0;
276
      *uidirtyfrom = 0;
255
      *uidirtyto = 0xff;
277
      *uidirtyto = 0xff;
256
    }
278
    }
257
  } else {
279
  } else {
258
    cursor_down(db, cursorposy, screenh, uidirtyfrom, uidirtyto);
280
    cursor_down(db, cursorposy, screenh, uidirtyfrom, uidirtyto);
259
    cursor_home(db, cursorposx, uidirtyfrom, uidirtyto);
281
    cursor_home(db, cursorposx, uidirtyfrom, uidirtyto);
260
  }
282
  }
261
}
283
}
262
 
284
 
263
 
285
 
264
static void del(struct linedb *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
286
static void del(struct linedb *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
265
  if (cursorposx + db->xoffset < db->cursor->len) {
287
  if (cursorposx + db->xoffset < db->cursor->len) {
266
    _fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
288
    _fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
267
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
289
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
268
    *uidirtyfrom = cursorposy;
290
    *uidirtyfrom = cursorposy;
269
    *uidirtyto = cursorposy;
291
    *uidirtyto = cursorposy;
270
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
292
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
271
    struct line far *nextline = db->cursor->next;
293
    struct line far *nextline = db->cursor->next;
272
    if (db->cursor->next->len > 0) {
294
    if (db->cursor->next->len > 0) {
273
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
295
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
274
      if (newptr != NULL) {
296
      if (newptr != NULL) {
275
        db->cursor = newptr;
297
        db->cursor = newptr;
276
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
298
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
277
        db->cursor->len += db->cursor->next->len;
299
        db->cursor->len += db->cursor->next->len;
278
        /* update db->topscreen if needed */
300
        /* update db->topscreen if needed */
279
        if (cursorposy == 0) db->topscreen = db->cursor;
301
        if (cursorposy == 0) db->topscreen = db->cursor;
280
      }
302
      }
281
    }
303
    }
282
    db->cursor->next = db->cursor->next->next;
304
    db->cursor->next = db->cursor->next->next;
283
    db->cursor->next->prev = db->cursor;
305
    db->cursor->next->prev = db->cursor;
284
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
306
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
285
    _ffree(nextline);
307
    _ffree(nextline);
286
    *uidirtyfrom = cursorposy;
308
    *uidirtyfrom = cursorposy;
287
    *uidirtyto = 0xff;
309
    *uidirtyto = 0xff;
288
  }
310
  }
289
}
311
}
290
 
312
 
291
 
313
 
292
static void bkspc(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
314
static void bkspc(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
293
 
315
 
294
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
316
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
295
  if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
317
  if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
296
 
318
 
297
  cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
319
  cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
298
  del(db, *cursorposx, *cursorposy, uidirtyfrom, uidirtyto);
320
  del(db, *cursorposx, *cursorposy, uidirtyfrom, uidirtyto);
299
}
321
}
300
 
322
 
301
 
323
 
302
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
324
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
303
 * of argc and argv, saves some 330 bytes of binary size */
325
 * of argc and argv, saves some 330 bytes of binary size */
304
static char *parseargv(void) {
326
static char *parseargv(void) {
305
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
327
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
306
  unsigned char count = 0;
328
  unsigned char count = 0;
307
  char *argv[4];
329
  char *argv[4];
308
 
330
 
309
  while (count < 4) {
331
  while (count < 4) {
310
    /* jump to nearest arg */
332
    /* jump to nearest arg */
311
    while (*tail == ' ') {
333
    while (*tail == ' ') {
312
      *tail = 0;
334
      *tail = 0;
313
      tail++;
335
      tail++;
314
    }
336
    }
315
 
337
 
316
    if (*tail == '\r') {
338
    if (*tail == '\r') {
317
      *tail = 0;
339
      *tail = 0;
318
      break;
340
      break;
319
    }
341
    }
320
 
342
 
321
    argv[count++] = tail;
343
    argv[count++] = tail;
322
 
344
 
323
    /* jump to next delimiter */
345
    /* jump to next delimiter */
324
    while ((*tail != ' ') && (*tail != '\r')) tail++;
346
    while ((*tail != ' ') && (*tail != '\r')) tail++;
325
  }
347
  }
326
 
348
 
327
  /* check args now */
349
  /* check args now */
328
  if (count != 1) return(NULL);
350
  if (count != 1) return(NULL);
329
 
351
 
330
  return(argv[0]);
352
  return(argv[0]);
331
}
353
}
332
 
354
 
333
 
355
 
334
static int loadfile(struct linedb *db, const char *fname) {
356
static int loadfile(struct linedb *db, const char *fname) {
335
  char buff[1024];
357
  char buff[1024];
336
  unsigned int prevlen = 0, len, llen;
358
  unsigned int prevlen = 0, len, llen;
337
  int fd;
359
  int fd;
338
  int r = 0;
360
  int r = 0;
339
 
361
 
340
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
362
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
341
    mdr_coutraw_puts("Failed to open file:");
363
    mdr_coutraw_puts("Failed to open file:");
342
    mdr_coutraw_puts(fname);
364
    mdr_coutraw_puts(fname);
343
    return(-1);
365
    return(-1);
344
  }
366
  }
345
 
367
 
346
  do {
368
  do {
347
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
369
    if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) == 0) {
348
      len += prevlen;
370
      len += prevlen;
349
    } else {
371
    } else {
350
      len = prevlen;
372
      len = prevlen;
351
    }
373
    }
352
 
374
 
353
    /* look for nearest \n and replace with 0*/
375
    /* look for nearest \n and replace with 0*/
354
    for (llen = 0; buff[llen] != '\n'; llen++) {
376
    for (llen = 0; buff[llen] != '\n'; llen++) {
355
      if (llen == sizeof(buff)) break;
377
      if (llen == sizeof(buff)) break;
356
    }
378
    }
357
    buff[llen] = 0;
379
    buff[llen] = 0;
358
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
380
    if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
359
    if (line_add(db, buff) != 0) {
381
    if (line_add(db, buff) != 0) {
360
      mdr_coutraw_puts("out of memory");
382
      mdr_coutraw_puts("out of memory");
361
      r = -1;
383
      r = -1;
362
      break;
384
      break;
363
    }
385
    }
364
 
386
 
365
    len -= llen + 1;
387
    len -= llen + 1;
366
    memmove(buff, buff + llen + 1, len);
388
    memmove(buff, buff + llen + 1, len);
367
    prevlen = len;
389
    prevlen = len;
368
  } while (len > 0);
390
  } while (len > 0);
369
 
391
 
370
  _dos_close(fd);
392
  _dos_close(fd);
371
 
393
 
372
  return(r);
394
  return(r);
373
}
395
}
374
 
396
 
375
 
397
 
376
int main(void) {
398
int main(void) {
377
  const char *fname;
399
  const char *fname;
378
  struct linedb db;
400
  struct linedb db;
379
  unsigned char screenw = 0, screenh = 0;
401
  unsigned char screenw = 0, screenh = 0;
380
  unsigned char cursorposx = 0, cursorposy = 0;
402
  unsigned char cursorposx = 0, cursorposy = 0;
381
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
403
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
382
 
404
 
383
  bzero(&db, sizeof(db));
405
  bzero(&db, sizeof(db));
384
 
406
 
385
  {
407
  {
386
    char nlspath[128], lang[8];
408
    char nlspath[128], lang[8];
387
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
409
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
388
  }
410
  }
389
 
411
 
390
  fname = parseargv();
412
  fname = parseargv();
391
 
413
 
392
  if (fname == NULL) {
414
  if (fname == NULL) {
393
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
415
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
394
    return(0);
416
    return(0);
395
  }
417
  }
396
 
418
 
397
  /* load file */
419
  /* load file */
398
  if (loadfile(&db, fname) != 0) return(1);
420
  if (loadfile(&db, fname) != 0) return(1);
399
 
421
 
400
  /* add an empty line at end if not present already */
422
  /* add an empty line at end if not present already */
401
  if (db.cursor->len != 0) line_add(&db, "");
423
  if (db.cursor->len != 0) line_add(&db, "");
402
 
424
 
403
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
425
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
404
  ui_basic(screenw, screenh, fname);
426
  ui_basic(screenw, screenh, fname);
405
 
427
 
406
  db_rewind(&db);
428
  db_rewind(&db);
407
 
429
 
408
  for (;;) {
430
  for (;;) {
409
    int k;
431
    int k;
410
 
432
 
411
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
433
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
412
    mdr_cout_locate(cursorposy, cursorposx);
434
    mdr_cout_locate(cursorposy, cursorposx);
413
 
435
 
414
    if (uidirtyfrom != 0xff) {
436
    if (uidirtyfrom != 0xff) {
415
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
437
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
416
      uidirtyfrom = 0xff;
438
      uidirtyfrom = 0xff;
417
    }
439
    }
418
 
440
 
419
    k = keyb_getkey();
441
    k = keyb_getkey();
420
 
442
 
421
    if (k == 0x150) { /* down */
443
    if (k == 0x150) { /* down */
422
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
444
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
423
 
445
 
424
    } else if (k == 0x148) { /* up */
446
    } else if (k == 0x148) { /* up */
425
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
447
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
426
 
448
 
427
    } else if (k == 0x14D) { /* right */
449
    } else if (k == 0x14D) { /* right */
428
      cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
450
      cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
429
 
451
 
430
    } else if (k == 0x14B) { /* left */
452
    } else if (k == 0x14B) { /* left */
431
      cursor_left(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
453
      cursor_left(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
432
 
454
 
433
    } else if (k == 0x149) { /* pgup */
455
    } else if (k == 0x149) { /* pgup */
434
      // TODO
456
      // TODO
435
 
457
 
436
    } else if (k == 0x151) { /* pgdown */
458
    } else if (k == 0x151) { /* pgdown */
437
      // TODO
459
      // TODO
438
 
460
 
439
    } else if (k == 0x147) { /* home */
461
    } else if (k == 0x147) { /* home */
440
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
462
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
441
 
463
 
442
    } else if (k == 0x14F) { /* end */
464
    } else if (k == 0x14F) { /* end */
443
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
465
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
444
 
466
 
445
    } else if (k == 0x1B) { /* ESC */
467
    } else if (k == 0x1B) { /* ESC */
446
      break;
468
      break;
447
 
469
 
448
    } else if (k == 0x0D) { /* ENTER */
470
    } else if (k == 0x0D) { /* ENTER */
449
      /* add a new line */
471
      /* add a new line */
450
      if (line_add(&db, db.cursor->payload + db.xoffset + cursorposx) == 0) {
472
      if (line_add(&db, db.cursor->payload + db.xoffset + cursorposx) == 0) {
451
        /* trim the line above */
473
        /* trim the line above */
452
        db.cursor->prev->len = db.xoffset + cursorposx;
474
        db.cursor->prev->len = db.xoffset + cursorposx;
453
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
475
        db.cursor->prev->payload[db.cursor->prev->len] = 0;
454
        /* move cursor to the (new) line below */
476
        /* move cursor to the (new) line below */
455
        cursorposx = 0;
477
        cursorposx = 0;
456
        if (cursorposy < screenh - 2) {
478
        if (cursorposy < screenh - 2) {
457
          uidirtyfrom = cursorposy;
479
          uidirtyfrom = cursorposy;
458
          cursorposy++;
480
          cursorposy++;
459
        } else {
481
        } else {
460
          db.topscreen = db.topscreen->next;
482
          db.topscreen = db.topscreen->next;
461
          uidirtyfrom = 0;
483
          uidirtyfrom = 0;
462
        }
484
        }
463
        uidirtyto = 0xff;
485
        uidirtyto = 0xff;
464
      } else {
486
      } else {
465
        /* ERROR: OUT OF MEMORY */
487
        /* ERROR: OUT OF MEMORY */
466
      }
488
      }
467
 
489
 
468
    } else if (k == 0x153) {  /* DEL */
490
    } else if (k == 0x153) {  /* DEL */
469
      del(&db, cursorposx, cursorposy, &uidirtyfrom, &uidirtyto);
491
      del(&db, cursorposx, cursorposy, &uidirtyfrom, &uidirtyto);
470
 
492
 
471
    } else if (k == 0x008) { /* BKSPC */
493
    } else if (k == 0x008) { /* BKSPC */
472
      bkspc(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
494
      bkspc(&db, &cursorposx, &cursorposy, screenw, &uidirtyfrom, &uidirtyto);
473
 
495
 
474
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
496
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
475
      struct line far *n;
497
      struct line far *n;
476
      n = _frealloc(db.cursor, sizeof(struct line) + db.cursor->len);
498
      n = _frealloc(db.cursor, sizeof(struct line) + db.cursor->len);
477
      if (n != NULL) {
499
      if (n != NULL) {
478
        unsigned short off = db.xoffset + cursorposx;
500
        unsigned short off = db.xoffset + cursorposx;
479
        if (n->prev) n->prev->next = n;
501
        if (n->prev) n->prev->next = n;
480
        if (n->next) n->next->prev = n;
502
        if (n->next) n->next->prev = n;
481
        if (db.topscreen == db.cursor) db.topscreen = n;
503
        if (db.topscreen == db.cursor) db.topscreen = n;
482
        db.cursor = n;
504
        db.cursor = n;
483
        _fmemmove(db.cursor->payload + off + 1, db.cursor->payload + off, db.cursor->len - off + 1);
505
        _fmemmove(db.cursor->payload + off + 1, db.cursor->payload + off, db.cursor->len - off + 1);
484
        db.cursor->len += 1;
506
        db.cursor->len += 1;
485
        uidirtyfrom = cursorposy;
507
        uidirtyfrom = cursorposy;
486
        uidirtyto = cursorposy;
508
        uidirtyto = cursorposy;
487
        db.cursor->payload[off] = k;
509
        db.cursor->payload[off] = k;
488
        cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
510
        cursor_right(&db, &cursorposx, &cursorposy, screenw, screenh, &uidirtyfrom, &uidirtyto);
489
      }
511
      }
490
 
512
 
-
 
513
    } else if (k == 0x13b) { /* F1 */
-
 
514
      ui_help(screenw);
-
 
515
      uidirtyfrom = 0;
-
 
516
      uidirtyto = 0xff;
-
 
517
 
491
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
518
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
492
      char buff[4];
519
      char buff[4];
493
      const char *HEX = "0123456789ABCDEF";
520
      const char *HEX = "0123456789ABCDEF";
494
      buff[0] = HEX[(k >> 8) & 15];
521
      buff[0] = HEX[(k >> 8) & 15];
495
      buff[1] = HEX[(k >> 4) & 15];
522
      buff[1] = HEX[(k >> 4) & 15];
496
      buff[2] = HEX[k & 15];
523
      buff[2] = HEX[k & 15];
497
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
524
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
498
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
525
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
499
      keyb_getkey();
526
      keyb_getkey();
500
      break;
527
      break;
501
    }
528
    }
502
  }
529
  }
503
 
530
 
504
  mdr_cout_close();
531
  mdr_cout_close();
505
 
532
 
506
  /* TODO free memory */
533
  /* TODO free memory */
507
 
534
 
508
  return(0);
535
  return(0);
509
}
536
}
510
 
537