Subversion Repositories SvarDOS

Rev

Rev 1325 | Rev 1327 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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