Subversion Repositories SvarDOS

Rev

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

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