Subversion Repositories SvarDOS

Rev

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

Rev 1334 Rev 1335
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\bios.h"
32
#include "mdr\bios.h"
33
#include "mdr\cout.h"
33
#include "mdr\cout.h"
34
#include "mdr\dos.h"
34
#include "mdr\dos.h"
35
#include "mdr\keyb.h"
35
#include "mdr\keyb.h"
36
 
36
 
37
#include "svarlang\svarlang.h"
37
#include "svarlang\svarlang.h"
38
 
38
 
39
 
39
 
40
/*****************************************************************************
40
/*****************************************************************************
41
 * global variables and definitions                                          *
41
 * global variables and definitions                                          *
42
 *****************************************************************************/
42
 *****************************************************************************/
43
 
43
 
44
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
44
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
45
static unsigned char SCHEME_TEXT   = 0x07,
45
static unsigned char SCHEME_TEXT   = 0x07,
46
                     SCHEME_STBAR1 = 0x70,
46
                     SCHEME_STBAR1 = 0x70,
47
                     SCHEME_STBAR2 = 0x70,
47
                     SCHEME_STBAR2 = 0x70,
48
                     SCHEME_STBAR3 = 0xf0,
48
                     SCHEME_STBAR3 = 0xf0,
49
                     SCHEME_SCROLL = 0x70,
49
                     SCHEME_SCROLL = 0x70,
50
                     SCHEME_MSG    = 0x70,
50
                     SCHEME_MSG    = 0x70,
51
                     SCHEME_ERR    = 0xf0;
51
                     SCHEME_ERR    = 0xf0;
52
 
52
 
53
static unsigned char screenw, screenh;
53
static unsigned char screenw, screenh;
54
 
54
 
55
static struct {
55
static struct {
56
    unsigned char from;
56
    unsigned char from;
57
    unsigned char to;
57
    unsigned char to;
58
} uidirty = {0, 0xff}; /* make sure to redraw entire UI at first run */
58
} uidirty = {0, 0xff}; /* make sure to redraw entire UI at first run */
59
 
59
 
60
#define SCROLL_CURSOR 0xB1
60
#define SCROLL_CURSOR 0xB1
61
 
61
 
62
struct line {
62
struct line {
63
  struct line far *prev;
63
  struct line far *prev;
64
  struct line far *next;
64
  struct line far *next;
65
  unsigned short len;
65
  unsigned short len;
66
  char payload[1];
66
  char payload[1];
67
};
67
};
68
 
68
 
69
struct file {
69
struct file {
70
  int fd;
70
  int fd;
71
  struct line far *cursor;
71
  struct line far *cursor;
72
  unsigned short xoffset;
72
  unsigned short xoffset;
73
  unsigned char cursorposx;
73
  unsigned char cursorposx;
74
  unsigned char cursorposy;
74
  unsigned char cursorposy;
75
  unsigned short totlines;
75
  unsigned short totlines;
76
  unsigned short curline;
76
  unsigned short curline;
77
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
77
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
78
  char modflag;  /* non-zero if file has been modified since last save */
78
  char modflag;  /* non-zero if file has been modified since last save */
79
  char fname[128];
79
  char fname[128];
80
};
80
};
81
 
81
 
82
 
82
 
83
/*****************************************************************************
83
/*****************************************************************************
84
 * functions                                                                 *
84
 * functions                                                                 *
85
 *****************************************************************************/
85
 *****************************************************************************/
86
 
86
 
87
/* adds a new line at cursor position into file linked list and advance cursor
87
/* adds a new line at cursor position into file linked list and advance cursor
88
 * returns non-zero on error */
88
 * returns non-zero on error */
89
static int line_add(struct file *db, const char far *line, unsigned short slen) {
89
static int line_add(struct file *db, const char far *line, unsigned short slen) {
90
  struct line far *l;
90
  struct line far *l;
91
 
91
 
92
  l = _fcalloc(1, sizeof(struct line) + slen);
92
  l = _fcalloc(1, sizeof(struct line) + slen);
93
  if (l == NULL) return(-1);
93
  if (l == NULL) return(-1);
94
 
94
 
95
  l->prev = db->cursor;
95
  l->prev = db->cursor;
96
  if (db->cursor) {
96
  if (db->cursor) {
97
    l->next = db->cursor->next;
97
    l->next = db->cursor->next;
98
    db->cursor->next = l;
98
    db->cursor->next = l;
99
    l->next->prev = l;
99
    l->next->prev = l;
100
  }
100
  }
101
  db->cursor = l;
101
  db->cursor = l;
102
  _fmemcpy(l->payload, line, slen);
102
  _fmemcpy(l->payload, line, slen);
103
  l->len = slen;
103
  l->len = slen;
104
 
104
 
105
  db->totlines += 1;
105
  db->totlines += 1;
106
  db->curline += 1;
106
  db->curline += 1;
107
 
107
 
108
  return(0);
108
  return(0);
109
}
109
}
110
 
110
 
111
 
111
 
112
static void ui_getstring(const char *query, char *s, unsigned char maxlen) {
112
static void ui_getstring(const char *query, char *s, unsigned char maxlen) {
113
  unsigned char len = 0, y, x;
113
  unsigned char len = 0, y, x;
114
  int k;
114
  int k;
115
 
115
 
116
  if (maxlen == 0) return;
116
  if (maxlen == 0) return;
117
  maxlen--; /* make room for the nul terminator */
117
  maxlen--; /* make room for the nul terminator */
118
 
118
 
119
  y = screenh - 1;
119
  y = screenh - 1;
120
 
120
 
121
  /* print query string */
121
  /* print query string */
122
  x = mdr_cout_str(y, 0, query, SCHEME_STBAR3, 40);
122
  x = mdr_cout_str(y, 0, query, SCHEME_STBAR3, 40);
123
  mdr_cout_char_rep(y, x++, ' ', SCHEME_STBAR3, screenw - x);
123
  mdr_cout_char_rep(y, x++, ' ', SCHEME_STBAR3, screenw - x);
124
 
124
 
125
  for (;;) {
125
  for (;;) {
126
    mdr_cout_locate(y, x + len);
126
    mdr_cout_locate(y, x + len);
127
    k = keyb_getkey();
127
    k = keyb_getkey();
128
 
128
 
129
    if (k == 0x1b) return; /* ESC */
129
    if (k == 0x1b) return; /* ESC */
130
 
130
 
131
    if (k == '\r') {
131
    if (k == '\r') {
132
      s[len] = 0;
132
      s[len] = 0;
133
      return;
133
      return;
134
    }
134
    }
135
 
135
 
136
    if ((k == 0x08) && (len > 0)) { /* BKSPC */
136
    if ((k == 0x08) && (len > 0)) { /* BKSPC */
137
      len--;
137
      len--;
138
      mdr_cout_char(y, x + len, ' ', SCHEME_STBAR3);
138
      mdr_cout_char(y, x + len, ' ', SCHEME_STBAR3);
139
      continue;
139
      continue;
140
    }
140
    }
141
 
141
 
142
    if ((k <= 0xff) && (k >= ' ') && (len < maxlen)) {
142
    if ((k <= 0xff) && (k >= ' ') && (len < maxlen)) {
143
      mdr_cout_char(y, x + len, k, SCHEME_STBAR3);
143
      mdr_cout_char(y, x + len, k, SCHEME_STBAR3);
144
      s[len++] = k;
144
      s[len++] = k;
145
    }
145
    }
146
 
146
 
147
  }
147
  }
148
}
148
}
149
 
149
 
150
 
150
 
151
/* append a nul-terminated string to line at cursor position */
151
/* append a nul-terminated string to line at cursor position */
152
static int line_append(struct file *f, const char far *buf, unsigned short len) {
152
static int line_append(struct file *f, const char far *buf, unsigned short len) {
153
  struct line far *n;
153
  struct line far *n;
154
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
154
  if (sizeof(struct line) + f->cursor->len + len < len) return(-1); /* overflow check */
155
  n = _frealloc(f->cursor, sizeof(struct line) + f->cursor->len + len);
155
  n = _frealloc(f->cursor, sizeof(struct line) + f->cursor->len + len);
156
  if (n == NULL) return(-1);
156
  if (n == NULL) return(-1);
157
  f->cursor = n;
157
  f->cursor = n;
158
  _fmemcpy(f->cursor->payload + f->cursor->len, buf, len);
158
  _fmemcpy(f->cursor->payload + f->cursor->len, buf, len);
159
  f->cursor->len += len;
159
  f->cursor->len += len;
160
 
160
 
161
  /* rewire the linked list */
161
  /* rewire the linked list */
162
  if (f->cursor->next) f->cursor->next->prev = f->cursor;
162
  if (f->cursor->next) f->cursor->next->prev = f->cursor;
163
  if (f->cursor->prev) f->cursor->prev->next = f->cursor;
163
  if (f->cursor->prev) f->cursor->prev->next = f->cursor;
164
 
164
 
165
  return(0);
165
  return(0);
166
}
166
}
167
 
167
 
168
 
168
 
169
static void db_rewind(struct file *db) {
169
static void db_rewind(struct file *db) {
170
  if (db->cursor == NULL) return;
170
  if (db->cursor == NULL) return;
171
  while (db->cursor->prev) db->cursor = db->cursor->prev;
171
  while (db->cursor->prev) db->cursor = db->cursor->prev;
172
  db->curline = 0;
172
  db->curline = 0;
173
}
173
}
174
 
174
 
175
 
175
 
176
static void ui_basic(const struct file *db) {
176
static void ui_basic(const struct file *db) {
177
  const char *s = svarlang_strid(0); /* HELP */
177
  const char *s = svarlang_strid(0); /* HELP */
178
  unsigned char helpcol = screenw - (strlen(s) + 4);
178
  unsigned char helpcol = screenw - strlen(s);
179
 
179
 
180
  /* fill status bar with background (without modflag as it is refreshed by ui_refresh) */
180
  /* fill status bar with background (without modflag as it is refreshed by ui_refresh) */
181
  mdr_cout_char_rep(screenh - 1, 1, ' ', SCHEME_STBAR1, screenw - 1);
181
  mdr_cout_char_rep(screenh - 1, 1, ' ', SCHEME_STBAR1, screenw - 1);
182
 
182
 
183
  /* filename */
183
  /* filename */
184
  {
184
  {
185
    const char *fn = db->fname;
185
    const char *fn = db->fname;
186
    if (*fn == 0) fn = svarlang_str(0, 1);
186
    if (*fn == 0) fn = svarlang_str(0, 1);
187
    mdr_cout_str(screenh - 1, 1, fn, SCHEME_STBAR1, screenw);
187
    mdr_cout_str(screenh - 1, 1, fn, SCHEME_STBAR1, screenw);
188
  }
188
  }
189
 
189
 
190
  /* eol type */
190
  /* eol type */
191
  {
191
  {
192
    const char *eoltype = "CRLF";
192
    const char *eoltype = "CRLF";
193
    if (db->lfonly) eoltype = "LF";
193
    if (db->lfonly) eoltype = "LF";
194
    mdr_cout_str(screenh - 1, helpcol - 5, eoltype, SCHEME_STBAR1, 5);
194
    mdr_cout_str(screenh - 1, helpcol - 6, eoltype, SCHEME_STBAR1, 5);
195
  }
195
  }
196
 
196
 
197
  mdr_cout_str(screenh - 1, helpcol, " F1=", SCHEME_STBAR2, 40);
-
 
198
  mdr_cout_str(screenh - 1, helpcol + 4, s, SCHEME_STBAR2, 40);
197
  mdr_cout_str(screenh - 1, helpcol, s, SCHEME_STBAR2, 40);
199
}
198
}
200
 
199
 
201
 
200
 
202
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
201
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
203
  unsigned short x, y, msglen, i;
202
  unsigned short x, y, msglen, i;
204
  unsigned char msg2flag = 0;
203
  unsigned char msg2flag = 0;
205
 
204
 
206
  msglen = strlen(msg1);
205
  msglen = strlen(msg1);
207
  if (msg2) {
206
  if (msg2) {
208
    msg2flag = 1;
207
    msg2flag = 1;
209
    i = strlen(msg2);
208
    i = strlen(msg2);
210
    if (i > msglen) msglen = i;
209
    if (i > msglen) msglen = i;
211
  }
210
  }
212
 
211
 
213
  y = (screenh - 6) >> 1;
212
  y = (screenh - 6) >> 1;
214
  x = (screenw - msglen - 4) >> 1;
213
  x = (screenw - msglen - 4) >> 1;
215
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
214
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
216
  x++;
215
  x++;
217
  mdr_cout_str(y+1, x, msg1, attr, msglen);
216
  mdr_cout_str(y+1, x, msg1, attr, msglen);
218
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
217
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
219
 
218
 
220
  if (uidirty.from > y) uidirty.from = y;
219
  if (uidirty.from > y) uidirty.from = y;
221
  if (uidirty.to < y+4) uidirty.to = y+4;
220
  if (uidirty.to < y+4) uidirty.to = y+4;
222
}
221
}
223
 
222
 
224
 
223
 
225
static void ui_help(void) {
224
static void ui_help(void) {
226
#define MAXLINLEN 35
225
#define MAXLINLEN 35
227
  unsigned short i, offset;
226
  unsigned short i, offset;
228
  offset = (screenw - MAXLINLEN + 2) >> 1;
227
  offset = (screenw - MAXLINLEN + 2) >> 1;
-
 
228
 
229
  mdr_cout_cursor_hide();
229
  mdr_cout_cursor_hide();
230
  for (i = 2; i <= 15; i++) {
230
  for (i = 2; i < 18; i++) {
231
    mdr_cout_char_rep(i, offset - 2, ' ', SCHEME_STBAR1, MAXLINLEN + 2);
231
    mdr_cout_char_rep(i, offset - 2, ' ', SCHEME_STBAR1, MAXLINLEN + 2);
232
  }
232
  }
233
 
233
 
-
 
234
  for (i = 0; i < 20; i++) {
234
  mdr_cout_str(3, offset, svarlang_str(0, 0), SCHEME_STBAR1, MAXLINLEN);
235
    const char *s = svarlang_str(8, i);
235
  for (i = 0; i <= 4; i++) {
236
    if (s[0] == 0) break;
-
 
237
    if (s[0] == '.') continue;
236
    mdr_cout_str(5 + i, offset, svarlang_str(8, i), SCHEME_STBAR1, MAXLINLEN);
238
    mdr_cout_str(3 + i, offset, s, SCHEME_STBAR1, MAXLINLEN);
237
  }
239
  }
238
  mdr_cout_str(5 + 1 + i, offset, svarlang_str(8, 10), SCHEME_STBAR1, MAXLINLEN);
-
 
239
 
-
 
240
  /* Press any key */
-
 
241
  mdr_cout_str(14, offset, svarlang_str(8, 11), SCHEME_STBAR1, MAXLINLEN);
-
 
242
 
240
 
243
  keyb_getkey();
241
  keyb_getkey();
244
  mdr_cout_cursor_show();
242
  mdr_cout_cursor_show();
245
#undef MAXLINLEN
243
#undef MAXLINLEN
246
}
244
}
247
 
245
 
248
 
246
 
249
static void ui_refresh(const struct file *db) {
247
static void ui_refresh(const struct file *db) {
250
  unsigned char x;
248
  unsigned char x;
251
  const struct line far *l;
249
  const struct line far *l;
252
  unsigned char y = db->cursorposy;
250
  unsigned char y = db->cursorposy;
253
 
251
 
254
#ifdef DBG_REFRESH
252
#ifdef DBG_REFRESH
255
  static char m = 'a';
253
  static char m = 'a';
256
  m++;
254
  m++;
257
  if (m > 'z') m = 'a';
255
  if (m > 'z') m = 'a';
258
#endif
256
#endif
259
 
257
 
260
  /* rewind cursor line to first line that needs redrawing */
258
  /* rewind cursor line to first line that needs redrawing */
261
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
259
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
262
 
260
 
263
  /* iterate over lines and redraw whatever needs to be redrawn */
261
  /* iterate over lines and redraw whatever needs to be redrawn */
264
  for (; l != NULL; l = l->next, y++) {
262
  for (; l != NULL; l = l->next, y++) {
265
 
263
 
266
    /* skip lines that do not need to be refreshed */
264
    /* skip lines that do not need to be refreshed */
267
    if (y < uidirty.from) continue;
265
    if (y < uidirty.from) continue;
268
    if (y > uidirty.to) break;
266
    if (y > uidirty.to) break;
269
 
267
 
270
    x = 0;
268
    x = 0;
271
    if (db->xoffset < l->len) {
269
    if (db->xoffset < l->len) {
272
      unsigned char i, limit;
270
      unsigned char i, limit;
273
      if (l->len - db->xoffset < screenw) {
271
      if (l->len - db->xoffset < screenw) {
274
        limit = l->len;
272
        limit = l->len;
275
      } else {
273
      } else {
276
        limit = db->xoffset + screenw - 1;
274
        limit = db->xoffset + screenw - 1;
277
      }
275
      }
278
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
276
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
279
    }
277
    }
280
 
278
 
281
    /* write empty spaces until end of line */
279
    /* write empty spaces until end of line */
282
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenw - 1 - x);
280
    if (x < screenw - 1) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenw - 1 - x);
283
 
281
 
284
#ifdef DBG_REFRESH
282
#ifdef DBG_REFRESH
285
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
283
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
286
#endif
284
#endif
287
 
285
 
288
    if (y == screenh - 2) break;
286
    if (y == screenh - 2) break;
289
  }
287
  }
290
 
288
 
291
  /* fill all lines below if empty (and they need to be redrawn) */
289
  /* fill all lines below if empty (and they need to be redrawn) */
292
  if (l == NULL) {
290
  if (l == NULL) {
293
    while ((y < screenh - 1) && (y < uidirty.to)) {
291
    while ((y < screenh - 1) && (y < uidirty.to)) {
294
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenw - 1);
292
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenw - 1);
295
    }
293
    }
296
  }
294
  }
297
 
295
 
298
  /* "file changed" flag */
296
  /* "file changed" flag */
299
  {
297
  {
300
    char flg = ' ';
298
    char flg = ' ';
301
    if (db->modflag) flg = '*';
299
    if (db->modflag) flg = '*';
302
    mdr_cout_char(screenh - 1, 0, flg, SCHEME_STBAR1);
300
    mdr_cout_char(screenh - 1, 0, flg, SCHEME_STBAR1);
303
  }
301
  }
304
 
302
 
305
  /* scroll bar */
303
  /* scroll bar */
306
  for (y = 0; y < (screenh - 1); y++) {
304
  for (y = 0; y < (screenh - 1); y++) {
307
    mdr_cout_char(y, screenw - 1, SCROLL_CURSOR, SCHEME_SCROLL);
305
    mdr_cout_char(y, screenw - 1, SCROLL_CURSOR, SCHEME_SCROLL);
308
  }
306
  }
309
 
307
 
310
  /* scroll cursor */
308
  /* scroll cursor */
311
  if (db->totlines >= screenh) {
309
  if (db->totlines >= screenh) {
312
    unsigned short topline = db->curline - db->cursorposy;
310
    unsigned short topline = db->curline - db->cursorposy;
313
    unsigned short col;
311
    unsigned short col;
314
    unsigned short totlines = db->totlines - screenh + 1;
312
    unsigned short totlines = db->totlines - screenh + 1;
315
    if (db->totlines - screenh > screenh) {
313
    if (db->totlines - screenh > screenh) {
316
      col = topline / (totlines / (screenh - 1));
314
      col = topline / (totlines / (screenh - 1));
317
    } else {
315
    } else {
318
      col = topline * (screenh - 1) / totlines;
316
      col = topline * (screenh - 1) / totlines;
319
    }
317
    }
320
    if (col >= screenh - 1) col = screenh - 2;
318
    if (col >= screenh - 1) col = screenh - 2;
321
    mdr_cout_char(col, screenw - 1, ' ', SCHEME_SCROLL);
319
    mdr_cout_char(col, screenw - 1, ' ', SCHEME_SCROLL);
322
  }
320
  }
323
}
321
}
324
 
322
 
325
 
323
 
326
static void check_cursor_not_after_eol(struct file *db) {
324
static void check_cursor_not_after_eol(struct file *db) {
327
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
325
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
328
 
326
 
329
  if (db->cursor->len < db->xoffset) {
327
  if (db->cursor->len < db->xoffset) {
330
    db->cursorposx = 0;
328
    db->cursorposx = 0;
331
    db->xoffset = db->cursor->len;
329
    db->xoffset = db->cursor->len;
332
    uidirty.from = 0;
330
    uidirty.from = 0;
333
    uidirty.to = 0xff;
331
    uidirty.to = 0xff;
334
  } else {
332
  } else {
335
    db->cursorposx = db->cursor->len - db->xoffset;
333
    db->cursorposx = db->cursor->len - db->xoffset;
336
  }
334
  }
337
}
335
}
338
 
336
 
339
 
337
 
340
static void cursor_up(struct file *db) {
338
static void cursor_up(struct file *db) {
341
  if (db->cursor->prev != NULL) {
339
  if (db->cursor->prev != NULL) {
342
    db->curline -= 1;
340
    db->curline -= 1;
343
    db->cursor = db->cursor->prev;
341
    db->cursor = db->cursor->prev;
344
    if (db->cursorposy == 0) {
342
    if (db->cursorposy == 0) {
345
      uidirty.from = 0;
343
      uidirty.from = 0;
346
      uidirty.to = 0xff;
344
      uidirty.to = 0xff;
347
    } else {
345
    } else {
348
      db->cursorposy -= 1;
346
      db->cursorposy -= 1;
349
    }
347
    }
350
  }
348
  }
351
}
349
}
352
 
350
 
353
 
351
 
354
static void cursor_eol(struct file *db) {
352
static void cursor_eol(struct file *db) {
355
  /* adjust xoffset to make sure eol is visible on screen */
353
  /* adjust xoffset to make sure eol is visible on screen */
356
  if (db->xoffset > db->cursor->len) {
354
  if (db->xoffset > db->cursor->len) {
357
    db->xoffset = db->cursor->len - 1;
355
    db->xoffset = db->cursor->len - 1;
358
    uidirty.from = 0;
356
    uidirty.from = 0;
359
    uidirty.to = 0xff;
357
    uidirty.to = 0xff;
360
  }
358
  }
361
 
359
 
362
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
360
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
363
    db->xoffset = db->cursor->len - screenw + 2;
361
    db->xoffset = db->cursor->len - screenw + 2;
364
    uidirty.from = 0;
362
    uidirty.from = 0;
365
    uidirty.to = 0xff;
363
    uidirty.to = 0xff;
366
  }
364
  }
367
  db->cursorposx = db->cursor->len - db->xoffset;
365
  db->cursorposx = db->cursor->len - db->xoffset;
368
}
366
}
369
 
367
 
370
 
368
 
371
static void cursor_down(struct file *db) {
369
static void cursor_down(struct file *db) {
372
  if (db->cursor->next != NULL) {
370
  if (db->cursor->next != NULL) {
373
    db->curline += 1;
371
    db->curline += 1;
374
    db->cursor = db->cursor->next;
372
    db->cursor = db->cursor->next;
375
    if (db->cursorposy < screenh - 2) {
373
    if (db->cursorposy < screenh - 2) {
376
      db->cursorposy += 1;
374
      db->cursorposy += 1;
377
    } else {
375
    } else {
378
      uidirty.from = 0;
376
      uidirty.from = 0;
379
      uidirty.to = 0xff;
377
      uidirty.to = 0xff;
380
    }
378
    }
381
  }
379
  }
382
}
380
}
383
 
381
 
384
 
382
 
385
static void cursor_left(struct file *db) {
383
static void cursor_left(struct file *db) {
386
  if (db->cursorposx > 0) {
384
  if (db->cursorposx > 0) {
387
    db->cursorposx -= 1;
385
    db->cursorposx -= 1;
388
  } else if (db->xoffset > 0) {
386
  } else if (db->xoffset > 0) {
389
    db->xoffset -= 1;
387
    db->xoffset -= 1;
390
    uidirty.from = 0;
388
    uidirty.from = 0;
391
    uidirty.to = 0xff;
389
    uidirty.to = 0xff;
392
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
390
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
393
    cursor_up(db);
391
    cursor_up(db);
394
    cursor_eol(db);
392
    cursor_eol(db);
395
  }
393
  }
396
}
394
}
397
 
395
 
398
 
396
 
399
static void cursor_home(struct file *db) {
397
static void cursor_home(struct file *db) {
400
  db->cursorposx = 0;
398
  db->cursorposx = 0;
401
  if (db->xoffset != 0) {
399
  if (db->xoffset != 0) {
402
    db->xoffset = 0;
400
    db->xoffset = 0;
403
    uidirty.from = 0;
401
    uidirty.from = 0;
404
    uidirty.to = 0xff;
402
    uidirty.to = 0xff;
405
  }
403
  }
406
}
404
}
407
 
405
 
408
 
406
 
409
static void cursor_right(struct file *db) {
407
static void cursor_right(struct file *db) {
410
  if (db->cursor->len > db->xoffset + db->cursorposx) {
408
  if (db->cursor->len > db->xoffset + db->cursorposx) {
411
    if (db->cursorposx < screenw - 2) {
409
    if (db->cursorposx < screenw - 2) {
412
      db->cursorposx += 1;
410
      db->cursorposx += 1;
413
    } else {
411
    } else {
414
      db->xoffset += 1;
412
      db->xoffset += 1;
415
      uidirty.from = 0;
413
      uidirty.from = 0;
416
      uidirty.to = 0xff;
414
      uidirty.to = 0xff;
417
    }
415
    }
418
  } else {
416
  } else {
419
    cursor_down(db);
417
    cursor_down(db);
420
    cursor_home(db);
418
    cursor_home(db);
421
  }
419
  }
422
}
420
}
423
 
421
 
424
 
422
 
425
static void del(struct file *db) {
423
static void del(struct file *db) {
426
  if (db->cursorposx + db->xoffset < db->cursor->len) {
424
  if (db->cursorposx + db->xoffset < db->cursor->len) {
427
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
425
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
428
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
426
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
429
    uidirty.from = db->cursorposy;
427
    uidirty.from = db->cursorposy;
430
    uidirty.to = db->cursorposy;
428
    uidirty.to = db->cursorposy;
431
    db->modflag = 1;
429
    db->modflag = 1;
432
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
430
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
433
    struct line far *nextline = db->cursor->next;
431
    struct line far *nextline = db->cursor->next;
434
    if (db->cursor->next->len > 0) {
432
    if (db->cursor->next->len > 0) {
435
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
433
      void far *newptr = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + db->cursor->next->len + 1);
436
      if (newptr != NULL) {
434
      if (newptr != NULL) {
437
        db->cursor = newptr;
435
        db->cursor = newptr;
438
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
436
        _fmemcpy(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
439
        db->cursor->len += db->cursor->next->len;
437
        db->cursor->len += db->cursor->next->len;
440
      }
438
      }
441
    }
439
    }
442
    db->cursor->next = db->cursor->next->next;
440
    db->cursor->next = db->cursor->next->next;
443
    db->cursor->next->prev = db->cursor;
441
    db->cursor->next->prev = db->cursor;
444
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
442
    if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor; /* in case realloc changed my pointer */
445
    _ffree(nextline);
443
    _ffree(nextline);
446
    uidirty.from = db->cursorposy;
444
    uidirty.from = db->cursorposy;
447
    uidirty.to = 0xff;
445
    uidirty.to = 0xff;
448
    db->totlines -= 1;
446
    db->totlines -= 1;
449
    db->modflag = 1;
447
    db->modflag = 1;
450
  }
448
  }
451
}
449
}
452
 
450
 
453
 
451
 
454
static void bkspc(struct file *db) {
452
static void bkspc(struct file *db) {
455
 
453
 
456
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
454
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
457
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
455
  if ((db->cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
458
 
456
 
459
  cursor_left(db);
457
  cursor_left(db);
460
  del(db);
458
  del(db);
461
}
459
}
462
 
460
 
463
 
461
 
464
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
462
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
465
 * of argc and argv, saves some 330 bytes of binary size */
463
 * of argc and argv, saves some 330 bytes of binary size */
466
static char *parseargv(void) {
464
static char *parseargv(void) {
467
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
465
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
468
  unsigned char count = 0;
466
  unsigned char count = 0;
469
  char *argv[4];
467
  char *argv[4];
470
 
468
 
471
  while (count < 4) {
469
  while (count < 4) {
472
    /* jump to nearest arg */
470
    /* jump to nearest arg */
473
    while (*tail == ' ') {
471
    while (*tail == ' ') {
474
      *tail = 0;
472
      *tail = 0;
475
      tail++;
473
      tail++;
476
    }
474
    }
477
 
475
 
478
    if (*tail == '\r') {
476
    if (*tail == '\r') {
479
      *tail = 0;
477
      *tail = 0;
480
      break;
478
      break;
481
    }
479
    }
482
 
480
 
483
    argv[count++] = tail;
481
    argv[count++] = tail;
484
 
482
 
485
    /* jump to next delimiter */
483
    /* jump to next delimiter */
486
    while ((*tail != ' ') && (*tail != '\r')) tail++;
484
    while ((*tail != ' ') && (*tail != '\r')) tail++;
487
  }
485
  }
488
 
486
 
489
  /* check args now */
487
  /* check args now */
490
  if (count == 0) return("");
488
  if (count == 0) return("");
491
 
489
 
492
  return(argv[0]);
490
  return(argv[0]);
493
}
491
}
494
 
492
 
495
 
493
 
496
static struct file *loadfile(const char *fname) {
494
static struct file *loadfile(const char *fname) {
497
  char buff[512]; /* read one entire sector at a time (faster) */
495
  char buff[512]; /* read one entire sector at a time (faster) */
498
  char *buffptr;
496
  char *buffptr;
499
  unsigned int len, llen;
497
  unsigned int len, llen;
500
  int fd;
498
  int fd;
501
  unsigned char eolfound;
499
  unsigned char eolfound;
502
  struct file *db;
500
  struct file *db;
503
 
501
 
504
  len = strlen(fname) + 1;
502
  len = strlen(fname) + 1;
505
  db = calloc(1, sizeof(struct file));
503
  db = calloc(1, sizeof(struct file));
506
  if (db == NULL) return(NULL);
504
  if (db == NULL) return(NULL);
507
  memcpy(db->fname, fname, len);
505
  memcpy(db->fname, fname, len);
508
 
506
 
509
  if (*fname == 0) goto SKIPLOADING;
507
  if (*fname == 0) goto SKIPLOADING;
510
 
508
 
511
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
509
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
512
    mdr_coutraw_puts("Failed to open file:");
510
    mdr_coutraw_puts("Failed to open file:");
513
    mdr_coutraw_puts(fname);
511
    mdr_coutraw_puts(fname);
514
    free(db);
512
    free(db);
515
    return(NULL);
513
    return(NULL);
516
  }
514
  }
517
 
515
 
518
  db->lfonly = 1;
516
  db->lfonly = 1;
519
 
517
 
520
  /* start by adding an empty line */
518
  /* start by adding an empty line */
521
  if (line_add(db, NULL, 0) != 0) {
519
  if (line_add(db, NULL, 0) != 0) {
522
    /* TODO ERROR HANDLING */
520
    /* TODO ERROR HANDLING */
523
  }
521
  }
524
 
522
 
525
  for (eolfound = 0;;) {
523
  for (eolfound = 0;;) {
526
    unsigned short consumedbytes;
524
    unsigned short consumedbytes;
527
 
525
 
528
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
526
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
529
    buffptr = buff;
527
    buffptr = buff;
530
 
528
 
531
    FINDLINE:
529
    FINDLINE:
532
 
530
 
533
    /* look for nearest \n */
531
    /* look for nearest \n */
534
    for (consumedbytes = 0;; consumedbytes++) {
532
    for (consumedbytes = 0;; consumedbytes++) {
535
      if (consumedbytes == len) {
533
      if (consumedbytes == len) {
536
        llen = consumedbytes;
534
        llen = consumedbytes;
537
        break;
535
        break;
538
      }
536
      }
539
      if (buffptr[consumedbytes] == '\r') {
537
      if (buffptr[consumedbytes] == '\r') {
540
        llen = consumedbytes;
538
        llen = consumedbytes;
541
        consumedbytes++;
539
        consumedbytes++;
542
        db->lfonly = 0;
540
        db->lfonly = 0;
543
        break;
541
        break;
544
      }
542
      }
545
      if (buffptr[consumedbytes] == '\n') {
543
      if (buffptr[consumedbytes] == '\n') {
546
        eolfound = 1;
544
        eolfound = 1;
547
        llen = consumedbytes;
545
        llen = consumedbytes;
548
        consumedbytes++;
546
        consumedbytes++;
549
        break;
547
        break;
550
      }
548
      }
551
    }
549
    }
552
 
550
 
553
    /* consumedbytes is the amount of bytes processed from buffptr,
551
    /* consumedbytes is the amount of bytes processed from buffptr,
554
     * llen is the length of line's payload (without its line terminator) */
552
     * llen is the length of line's payload (without its line terminator) */
555
 
553
 
556
    /* append content, if line is non-empty */
554
    /* append content, if line is non-empty */
557
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
555
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
558
      mdr_coutraw_puts("out of memory");
556
      mdr_coutraw_puts("out of memory");
559
      free(db);
557
      free(db);
560
      db = NULL;
558
      db = NULL;
561
      break;
559
      break;
562
    }
560
    }
563
 
561
 
564
    /* add a new line if necessary */
562
    /* add a new line if necessary */
565
    if (eolfound) {
563
    if (eolfound) {
566
      if (line_add(db, NULL, 0) != 0) {
564
      if (line_add(db, NULL, 0) != 0) {
567
      /* TODO ERROR HANDLING */
565
      /* TODO ERROR HANDLING */
568
        mdr_coutraw_puts("out of memory");
566
        mdr_coutraw_puts("out of memory");
569
        free(db);
567
        free(db);
570
        db = NULL;
568
        db = NULL;
571
        break;
569
        break;
572
      }
570
      }
573
      eolfound = 0;
571
      eolfound = 0;
574
    }
572
    }
575
 
573
 
576
    /* anything left? process the buffer leftover again */
574
    /* anything left? process the buffer leftover again */
577
    if (consumedbytes < len) {
575
    if (consumedbytes < len) {
578
      len -= consumedbytes;
576
      len -= consumedbytes;
579
      buffptr += consumedbytes;
577
      buffptr += consumedbytes;
580
      goto FINDLINE;
578
      goto FINDLINE;
581
    }
579
    }
582
 
580
 
583
  }
581
  }
584
 
582
 
585
  _dos_close(fd);
583
  _dos_close(fd);
586
 
584
 
587
  SKIPLOADING:
585
  SKIPLOADING:
588
 
586
 
589
  /* add an empty line at end if not present already, also rewind cursor to top of file */
587
  /* add an empty line at end if not present already, also rewind cursor to top of file */
590
  if (db != NULL) {
588
  if (db != NULL) {
591
    if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
589
    if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
592
    db_rewind(db);
590
    db_rewind(db);
593
  }
591
  }
594
 
592
 
595
  return(db);
593
  return(db);
596
}
594
}
597
 
595
 
598
 
596
 
599
static int savefile(const struct file *db, const char *newfname) {
597
static int savefile(const struct file *db, const char *newfname) {
600
  int fd;
598
  int fd;
601
  const struct line far *l;
599
  const struct line far *l;
602
  unsigned bytes;
600
  unsigned bytes;
603
  unsigned char eollen;
601
  unsigned char eollen;
604
  unsigned char eolbuf[2];
602
  unsigned char eolbuf[2];
605
  int errflag = 0;
603
  int errflag = 0;
606
 
604
 
607
  /* either create a new file if newfname provided, or... */
605
  /* either create a new file if newfname provided, or... */
608
  if (newfname) {
606
  if (newfname) {
609
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
607
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
610
  } else { /* ...open db->fname */
608
  } else { /* ...open db->fname */
611
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
609
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
612
  }
610
  }
613
 
611
 
614
  l = db->cursor;
612
  l = db->cursor;
615
  while (l->prev) l = l->prev;
613
  while (l->prev) l = l->prev;
616
 
614
 
617
  /* preset line terminators */
615
  /* preset line terminators */
618
  if (db->lfonly) {
616
  if (db->lfonly) {
619
    eolbuf[0] = '\n';
617
    eolbuf[0] = '\n';
620
    eollen = 1;
618
    eollen = 1;
621
  } else {
619
  } else {
622
    eolbuf[0] = '\r';
620
    eolbuf[0] = '\r';
623
    eolbuf[1] = '\n';
621
    eolbuf[1] = '\n';
624
    eollen = 2;
622
    eollen = 2;
625
  }
623
  }
626
 
624
 
627
  while (l) {
625
  while (l) {
628
    /* do not write the last empty line, it is only useful for edition */
626
    /* do not write the last empty line, it is only useful for edition */
629
    if (l->len != 0) {
627
    if (l->len != 0) {
630
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
628
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
631
    } else if (l->next == NULL) {
629
    } else if (l->next == NULL) {
632
      break;
630
      break;
633
    }
631
    }
634
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
632
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
635
    l = l->next;
633
    l = l->next;
636
  }
634
  }
637
 
635
 
638
  errflag |= _dos_close(fd);
636
  errflag |= _dos_close(fd);
639
 
637
 
640
  return(errflag);
638
  return(errflag);
641
}
639
}
642
 
640
 
643
 
641
 
644
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
642
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
645
  struct line far *n;
643
  struct line far *n;
646
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
644
  n = _frealloc(db->cursor, sizeof(struct line) + db->cursor->len + len);
647
  if (n != NULL) {
645
  if (n != NULL) {
648
    unsigned short off = db->xoffset + db->cursorposx;
646
    unsigned short off = db->xoffset + db->cursorposx;
649
    db->modflag = 1;
647
    db->modflag = 1;
650
    if (n->prev) n->prev->next = n;
648
    if (n->prev) n->prev->next = n;
651
    if (n->next) n->next->prev = n;
649
    if (n->next) n->next->prev = n;
652
    db->cursor = n;
650
    db->cursor = n;
653
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
651
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
654
    db->cursor->len += len;
652
    db->cursor->len += len;
655
    uidirty.from = db->cursorposy;
653
    uidirty.from = db->cursorposy;
656
    uidirty.to = db->cursorposy;
654
    uidirty.to = db->cursorposy;
657
    while (len--) {
655
    while (len--) {
658
      db->cursor->payload[off++] = *databuf;
656
      db->cursor->payload[off++] = *databuf;
659
      databuf++;
657
      databuf++;
660
      cursor_right(db);
658
      cursor_right(db);
661
    }
659
    }
662
  }
660
  }
663
}
661
}
664
 
662
 
665
 
663
 
666
int main(void) {
664
int main(void) {
667
  const char *fname;
665
  const char *fname;
668
  struct file *db;
666
  struct file *db;
669
 
667
 
670
  {
668
  {
671
    char nlspath[128], lang[8];
669
    char nlspath[128], lang[8];
672
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
670
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
673
  }
671
  }
674
 
672
 
675
  fname = parseargv();
673
  fname = parseargv();
676
 
674
 
677
  if (fname == NULL) {
675
  if (fname == NULL) {
678
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
676
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
679
    return(0);
677
    return(0);
680
  }
678
  }
681
 
679
 
682
  /* load file */
680
  /* load file */
683
  db = loadfile(fname);
681
  db = loadfile(fname);
684
  if (db == NULL) return(1);
682
  if (db == NULL) return(1);
685
 
683
 
686
  if (mdr_cout_init(&screenw, &screenh)) {
684
  if (mdr_cout_init(&screenw, &screenh)) {
687
    /* load color scheme if mdr_cout_init returns a color flag */
685
    /* load color scheme if mdr_cout_init returns a color flag */
688
    SCHEME_TEXT = 0x17;
686
    SCHEME_TEXT = 0x17;
689
    SCHEME_STBAR1 = 0x70;
687
    SCHEME_STBAR1 = 0x70;
690
    SCHEME_STBAR2 = 0x78;
688
    SCHEME_STBAR2 = 0x78;
691
    SCHEME_STBAR3 = 0xf0;
689
    SCHEME_STBAR3 = 0xf0;
692
    SCHEME_SCROLL = 0x70;
690
    SCHEME_SCROLL = 0x70;
693
    SCHEME_MSG = 0xf0;
691
    SCHEME_MSG = 0xf0;
694
    SCHEME_ERR = 0x4f;
692
    SCHEME_ERR = 0x4f;
695
  }
693
  }
696
  ui_basic(db);
694
  ui_basic(db);
697
 
695
 
698
  for (;;) {
696
  for (;;) {
699
    int k;
697
    int k;
700
 
698
 
701
    check_cursor_not_after_eol(db);
699
    check_cursor_not_after_eol(db);
702
    mdr_cout_locate(db->cursorposy, db->cursorposx);
700
    mdr_cout_locate(db->cursorposy, db->cursorposx);
703
 
701
 
704
    if (uidirty.from != 0xff) {
702
    if (uidirty.from != 0xff) {
705
      ui_refresh(db);
703
      ui_refresh(db);
706
      uidirty.from = 0xff;
704
      uidirty.from = 0xff;
707
    }
705
    }
708
#ifdef DBG_LINENUM
706
#ifdef DBG_LINENUM
709
      {
707
      {
710
        char ddd[10];
708
        char ddd[10];
711
        db->curline += 1;
709
        db->curline += 1;
712
        ddd[0] = '0' + db->curline / 100;
710
        ddd[0] = '0' + db->curline / 100;
713
        ddd[1] = '0' + (db->curline % 100) / 10;
711
        ddd[1] = '0' + (db->curline % 100) / 10;
714
        ddd[2] = '0' + (db->curline % 10);
712
        ddd[2] = '0' + (db->curline % 10);
715
        db->curline -= 1;
713
        db->curline -= 1;
716
        ddd[3] = '/';
714
        ddd[3] = '/';
717
        ddd[4] = '0' + db->totlines / 100;
715
        ddd[4] = '0' + db->totlines / 100;
718
        ddd[5] = '0' + (db->totlines % 100) / 10;
716
        ddd[5] = '0' + (db->totlines % 100) / 10;
719
        ddd[6] = '0' + (db->totlines % 10);
717
        ddd[6] = '0' + (db->totlines % 10);
720
        ddd[7] = 0;
718
        ddd[7] = 0;
721
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
719
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
722
      }
720
      }
723
#endif
721
#endif
724
 
722
 
725
    k = keyb_getkey();
723
    k = keyb_getkey();
726
 
724
 
727
    if (k == 0x150) { /* down */
725
    if (k == 0x150) { /* down */
728
      cursor_down(db);
726
      cursor_down(db);
729
 
727
 
730
    } else if (k == 0x148) { /* up */
728
    } else if (k == 0x148) { /* up */
731
      cursor_up(db);
729
      cursor_up(db);
732
 
730
 
733
    } else if (k == 0x14D) { /* right */
731
    } else if (k == 0x14D) { /* right */
734
      cursor_right(db);
732
      cursor_right(db);
735
 
733
 
736
    } else if (k == 0x14B) { /* left */
734
    } else if (k == 0x14B) { /* left */
737
      cursor_left(db);
735
      cursor_left(db);
738
 
736
 
739
    } else if (k == 0x149) { /* pgup */
737
    } else if (k == 0x149) { /* pgup */
740
      unsigned char dist = db->cursorposy + screenh - 1;
738
      unsigned char dist = db->cursorposy + screenh - 1;
741
      while ((dist != 0) && (db->cursor->prev != NULL)) {
739
      while ((dist != 0) && (db->cursor->prev != NULL)) {
742
        db->cursor = db->cursor->prev;
740
        db->cursor = db->cursor->prev;
743
        dist--;
741
        dist--;
744
      }
742
      }
745
      if (dist != 0) {
743
      if (dist != 0) {
746
        db->cursorposy = 0;
744
        db->cursorposy = 0;
747
        db->cursorposx = 0;
745
        db->cursorposx = 0;
748
      } else {
746
      } else {
749
        dist = db->cursorposy;
747
        dist = db->cursorposy;
750
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
748
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
751
      }
749
      }
752
      uidirty.from = 0;
750
      uidirty.from = 0;
753
      uidirty.to = 0xff;
751
      uidirty.to = 0xff;
754
 
752
 
755
    } else if (k == 0x151) { /* pgdown */
753
    } else if (k == 0x151) { /* pgdown */
756
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
754
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
757
      while ((dist != 0) && (db->cursor->next != NULL)) {
755
      while ((dist != 0) && (db->cursor->next != NULL)) {
758
        db->cursor = db->cursor->next;
756
        db->cursor = db->cursor->next;
759
        dist--;
757
        dist--;
760
      }
758
      }
761
      if (dist != 0) {
759
      if (dist != 0) {
762
        db->cursorposy = screenh - 2;
760
        db->cursorposy = screenh - 2;
763
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
761
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
764
        db->cursorposx = 0;
762
        db->cursorposx = 0;
765
      } else {
763
      } else {
766
        dist = screenh - 2 - db->cursorposy;
764
        dist = screenh - 2 - db->cursorposy;
767
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
765
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
768
      }
766
      }
769
      uidirty.from = 0;
767
      uidirty.from = 0;
770
      uidirty.to = 0xff;
768
      uidirty.to = 0xff;
771
 
769
 
772
    } else if (k == 0x147) { /* home */
770
    } else if (k == 0x147) { /* home */
773
       cursor_home(db);
771
       cursor_home(db);
774
 
772
 
775
    } else if (k == 0x14F) { /* end */
773
    } else if (k == 0x14F) { /* end */
776
       cursor_eol(db);
774
       cursor_eol(db);
777
 
775
 
778
    } else if (k == 0x1B) { /* ESC */
776
    } else if (k == 0x1B) { /* ESC */
779
      if (db->modflag == 0) break;
777
      if (db->modflag == 0) break;
780
      /* if file has been modified then ask for confirmation */
778
      /* if file has been modified then ask for confirmation */
781
      ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
779
      ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
782
      if (keyb_getkey() == '\r') break;
780
      if (keyb_getkey() == '\r') break;
783
 
781
 
784
    } else if (k == 0x0D) { /* ENTER */
782
    } else if (k == 0x0D) { /* ENTER */
785
      unsigned short off = db->xoffset + db->cursorposx;
783
      unsigned short off = db->xoffset + db->cursorposx;
786
      /* add a new line */
784
      /* add a new line */
787
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
785
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
788
        db->modflag = 1;
786
        db->modflag = 1;
789
        db->cursor = db->cursor->prev; /* back to original line */
787
        db->cursor = db->cursor->prev; /* back to original line */
790
        db->curline -= 1;
788
        db->curline -= 1;
791
        /* trim the line above */
789
        /* trim the line above */
792
        db->cursor->len = off;
790
        db->cursor->len = off;
793
        /* move cursor to the (new) line below */
791
        /* move cursor to the (new) line below */
794
        uidirty.from = db->cursorposy;
792
        uidirty.from = db->cursorposy;
795
        uidirty.to = 0xff;
793
        uidirty.to = 0xff;
796
        cursor_down(db);
794
        cursor_down(db);
797
        cursor_home(db);
795
        cursor_home(db);
798
      } else {
796
      } else {
799
        /* ERROR: OUT OF MEMORY */
797
        /* ERROR: OUT OF MEMORY */
800
      }
798
      }
801
 
799
 
802
    } else if (k == 0x153) {  /* DEL */
800
    } else if (k == 0x153) {  /* DEL */
803
      del(db);
801
      del(db);
804
 
802
 
805
    } else if (k == 0x008) { /* BKSPC */
803
    } else if (k == 0x008) { /* BKSPC */
806
      bkspc(db);
804
      bkspc(db);
807
 
805
 
808
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
806
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
809
      char c = k;
807
      char c = k;
810
      insert_in_line(db, &c, 1);
808
      insert_in_line(db, &c, 1);
811
 
809
 
812
    } else if (k == 0x009) { /* TAB */
810
    } else if (k == 0x009) { /* TAB */
813
      const char *tab = "        ";
811
      const char *tab = "        ";
814
      insert_in_line(db, tab, 8);
812
      insert_in_line(db, tab, 8);
815
 
813
 
816
    } else if (k == 0x13b) { /* F1 */
814
    } else if (k == 0x13b) { /* F1 */
817
      ui_help();
815
      ui_help();
818
      uidirty.from = 0;
816
      uidirty.from = 0;
819
      uidirty.to = 0xff;
817
      uidirty.to = 0xff;
820
 
818
 
821
    } else if ((k == 0x13f) || (k == 0x140)) { /* F5 or F6 */
819
    } else if ((k == 0x13f) || (k == 0x140)) { /* F5 or F6 */
822
      int saveres;
820
      int saveres;
823
 
821
 
824
      if ((k == 0x140) || (db->fname[0] == 0)) { /* save as... */
822
      if ((k == 0x140) || (db->fname[0] == 0)) { /* save as... */
825
        char fname[25];
823
        char fname[25];
826
        ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
824
        ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
827
        if (*fname == 0) continue;
825
        if (*fname == 0) continue;
828
        saveres = savefile(db, fname);
826
        saveres = savefile(db, fname);
829
        if (saveres == 0) memcpy(db->fname, fname, sizeof(fname));
827
        if (saveres == 0) memcpy(db->fname, fname, sizeof(fname));
830
      } else {
828
      } else {
831
        saveres = savefile(db, NULL);
829
        saveres = savefile(db, NULL);
832
      }
830
      }
833
 
831
 
834
      if (saveres == 0) {
832
      if (saveres == 0) {
835
        db->modflag = 0;
833
        db->modflag = 0;
836
        ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
834
        ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
837
        mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
835
        mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
838
      } else {
836
      } else {
839
        ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
837
        ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
840
        mdr_bios_tickswait(36); /* 2s */
838
        mdr_bios_tickswait(36); /* 2s */
841
      }
839
      }
842
 
840
 
843
      ui_basic(db);
841
      ui_basic(db);
844
      ui_refresh(db);
842
      ui_refresh(db);
845
 
843
 
846
    } else if (k == 0x144) { /* F10 */
844
    } else if (k == 0x144) { /* F10 */
847
      db->modflag = 1;
845
      db->modflag = 1;
848
      db->lfonly ^= 1;
846
      db->lfonly ^= 1;
849
      ui_basic(db);
847
      ui_basic(db);
850
 
848
 
851
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
849
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
852
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
850
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
853
      for (;;) {
851
      for (;;) {
854
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
852
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
855
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
853
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
856
        cursor_right(db);
854
        cursor_right(db);
857
      }
855
      }
858
      /* now skip to next non-space or end of file */
856
      /* now skip to next non-space or end of file */
859
      for (;;) {
857
      for (;;) {
860
        cursor_right(db);
858
        cursor_right(db);
861
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
859
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
862
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
860
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
863
      }
861
      }
864
 
862
 
865
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
863
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
866
      cursor_left(db);
864
      cursor_left(db);
867
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
865
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
868
      for (;;) {
866
      for (;;) {
869
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
867
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
870
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
868
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
871
        cursor_left(db);
869
        cursor_left(db);
872
      }
870
      }
873
      /* now skip to next space or start of file */
871
      /* now skip to next space or start of file */
874
      for (;;) {
872
      for (;;) {
875
        cursor_left(db);
873
        cursor_left(db);
876
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
874
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
877
          cursor_right(db);
875
          cursor_right(db);
878
          break;
876
          break;
879
        }
877
        }
880
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
878
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
881
      }
879
      }
882
 
880
 
883
#ifdef DBG_UNHKEYS
881
#ifdef DBG_UNHKEYS
884
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
882
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
885
      char buff[4];
883
      char buff[4];
886
      const char *HEX = "0123456789ABCDEF";
884
      const char *HEX = "0123456789ABCDEF";
887
      buff[0] = HEX[(k >> 8) & 15];
885
      buff[0] = HEX[(k >> 8) & 15];
888
      buff[1] = HEX[(k >> 4) & 15];
886
      buff[1] = HEX[(k >> 4) & 15];
889
      buff[2] = HEX[k & 15];
887
      buff[2] = HEX[k & 15];
890
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
888
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
891
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
889
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
892
      keyb_getkey();
890
      keyb_getkey();
893
      break;
891
      break;
894
#endif
892
#endif
895
    }
893
    }
896
  }
894
  }
897
 
895
 
898
  mdr_cout_close();
896
  mdr_cout_close();
899
 
897
 
900
  /* no need to free memory, DOS will do it for me */
898
  /* no need to free memory, DOS will do it for me */
901
 
899
 
902
  return(0);
900
  return(0);
903
}
901
}
904
 
902