Subversion Repositories SvarDOS

Rev

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

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