Subversion Repositories SvarDOS

Rev

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

Rev 1284 Rev 1286
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
 
30
 
31
#include "mdr\cout.h"
31
#include "mdr\cout.h"
32
#include "mdr\keyb.h"
32
#include "mdr\keyb.h"
33
 
33
 
34
#include "svarlang\svarlang.h"
34
#include "svarlang\svarlang.h"
35
 
35
 
36
#define COL_TXT        0
36
#define COL_TXT        0
37
#define COL_STATUSBAR1 1
37
#define COL_STATUSBAR1 1
38
#define COL_STATUSBAR2 2
38
#define COL_STATUSBAR2 2
39
#define COL_SCROLLBAR  3
39
#define COL_SCROLLBAR  3
40
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
40
/* preload the mono scheme (to be overloaded at runtime if color adapter present) */
41
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
41
static unsigned char scheme[] = {0x07, 0x70, 0x70, 0x70};
42
 
42
 
43
#define SCROLL_CURSOR 0xB1
43
#define SCROLL_CURSOR 0xB1
44
 
44
 
45
 
45
 
46
struct line {
46
struct line {
47
  struct line *prev;
47
  struct line *prev;
48
  struct line *next;
48
  struct line *next;
49
  unsigned short len;
49
  unsigned short len;
50
  char payload[1];
50
  char payload[1];
51
};
51
};
52
 
52
 
53
struct linedb {
53
struct linedb {
54
  struct line *topscreen;
54
  struct line *topscreen;
55
  struct line *cursor;
55
  struct line *cursor;
56
  unsigned short xoffset;
56
  unsigned short xoffset;
57
};
57
};
58
 
58
 
59
 
59
 
60
 
60
 
61
void line_add(struct linedb *db, const char *line) {
61
void line_add(struct linedb *db, const char *line) {
62
  unsigned short slen = strlen(line);
62
  unsigned short slen = strlen(line);
63
  struct line *l;
63
  struct line *l;
64
 
64
 
65
  /* trim out CR/LF line endings */
65
  /* trim out CR/LF line endings */
66
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
66
  if ((slen >= 2) && (line[slen - 2] == '\r')) {
67
    slen -= 2;
67
    slen -= 2;
68
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
68
  } else if ((slen >= 1) && (line[slen - 1] == '\n')) {
69
    slen--;
69
    slen--;
70
  }
70
  }
71
 
71
 
72
  l =  calloc(1, sizeof(struct line) + slen + 1);
72
  l =  calloc(1, sizeof(struct line) + slen + 1);
73
  l->prev = db->cursor;
73
  l->prev = db->cursor;
74
  if (db->cursor) {
74
  if (db->cursor) {
75
    l->next = db->cursor->next;
75
    l->next = db->cursor->next;
76
    db->cursor->next = l;
76
    db->cursor->next = l;
77
    l->next->prev = l;
77
    l->next->prev = l;
78
  }
78
  }
79
  db->cursor = l;
79
  db->cursor = l;
80
  memmove(l->payload, line, slen);
80
  memmove(l->payload, line, slen);
81
  l->len = slen;
81
  l->len = slen;
82
}
82
}
83
 
83
 
84
 
84
 
85
void db_rewind(struct linedb *db) {
85
void db_rewind(struct linedb *db) {
86
  if (db->cursor == NULL) return;
86
  if (db->cursor == NULL) return;
87
  while (db->cursor->prev) db->cursor = db->cursor->prev;
87
  while (db->cursor->prev) db->cursor = db->cursor->prev;
88
  db->topscreen = db->cursor;
88
  db->topscreen = db->cursor;
89
}
89
}
90
 
90
 
91
 
91
 
92
void load_colorscheme(void) {
92
void load_colorscheme(void) {
93
  scheme[COL_TXT] = 0x17;
93
  scheme[COL_TXT] = 0x17;
94
  scheme[COL_STATUSBAR1] = 0x70;
94
  scheme[COL_STATUSBAR1] = 0x70;
95
  scheme[COL_STATUSBAR2] = 0x78;
95
  scheme[COL_STATUSBAR2] = 0x78;
96
  scheme[COL_SCROLLBAR] = 0x70;
96
  scheme[COL_SCROLLBAR] = 0x70;
97
}
97
}
98
 
98
 
99
 
99
 
100
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
100
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname) {
101
  unsigned char i;
101
  unsigned char i;
102
  const char *s = "HELP";
102
  const char *s = "HELP";
103
  unsigned char helpcol = screenw - (strlen(s) + 4);
103
  unsigned char helpcol = screenw - (strlen(s) + 4);
104
 
104
 
105
  /* clear screen */
105
  /* clear screen */
106
  mdr_cout_cls(scheme[COL_TXT]);
106
  mdr_cout_cls(scheme[COL_TXT]);
107
 
107
 
108
  /* status bar */
108
  /* status bar */
109
  for (i = 0; i < helpcol; i++) {
109
  for (i = 0; i < helpcol; i++) {
110
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
110
    mdr_cout_char(screenh - 1, i, *fname, scheme[COL_STATUSBAR1]);
111
    if (*fname != 0) fname++;
111
    if (*fname != 0) fname++;
112
  }
112
  }
113
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
113
  mdr_cout_str(screenh - 1, helpcol, " F1=", scheme[COL_STATUSBAR2], 40);
114
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
114
  mdr_cout_str(screenh - 1, helpcol + 4, s, scheme[COL_STATUSBAR2], 40);
115
 
115
 
116
  /* scroll bar */
116
  /* scroll bar */
117
  for (i = 0; i < (screenh - 1); i++) {
117
  for (i = 0; i < (screenh - 1); i++) {
118
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
118
    mdr_cout_char(i, screenw - 1, SCROLL_CURSOR, scheme[COL_SCROLLBAR]);
119
  }
119
  }
120
}
120
}
121
 
121
 
122
 
122
 
123
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
123
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto) {
124
  unsigned char y = 0;
124
  unsigned char y = 0;
125
  unsigned char len;
125
  unsigned char len;
126
  struct line *l;
126
  struct line *l;
127
 
127
 
128
  /* DEBUG TODO FIXME */
128
  /* DEBUG TODO FIXME */
129
  static char m = 'a';
129
  static char m = 'a';
130
  m++;
130
  m++;
131
  if (m > 'z') m = 'a';
131
  if (m > 'z') m = 'a';
132
 
132
 
133
  for (l = db->topscreen; l != NULL; l = l->next) {
133
  for (l = db->topscreen; l != NULL; l = l->next) {
134
 
134
 
135
    /* skip lines that do not to be refreshed */
135
    /* skip lines that do not to be refreshed */
136
    if ((y < uidirtyfrom) || (y > uidirtyto)) continue;
136
    if ((y < uidirtyfrom) || (y > uidirtyto)) continue;
137
 
137
 
138
    if (db->xoffset < l->len) {
138
    if (db->xoffset < l->len) {
139
      len = mdr_cout_str(y, 0, l->payload + db->xoffset, scheme[COL_TXT], screenw - 1);
139
      len = mdr_cout_str(y, 0, l->payload + db->xoffset, scheme[COL_TXT], screenw - 1);
140
    } else {
140
    } else {
141
      len = 0;
141
      len = 0;
142
    }
142
    }
143
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
143
    while (len < screenw - 1) mdr_cout_char(y, len++, ' ', scheme[COL_TXT]);
144
 
144
 
145
    /* FIXME DEBUG */
145
    /* FIXME DEBUG */
146
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
146
    mdr_cout_char(y, 0, m, scheme[COL_STATUSBAR1]);
147
 
147
 
148
    if (y == screenh - 2) break;
148
    if (y == screenh - 2) break;
149
    y++;
149
    y++;
150
  }
150
  }
151
}
151
}
152
 
152
 
153
 
153
 
154
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
154
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
155
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
155
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
156
 
156
 
157
  if (db->cursor->len < db->xoffset) {
157
  if (db->cursor->len < db->xoffset) {
158
    *cursorpos = 0;
158
    *cursorpos = 0;
159
    db->xoffset = db->cursor->len;
159
    db->xoffset = db->cursor->len;
160
    *uidirtyfrom = 0;
160
    *uidirtyfrom = 0;
161
    *uidirtyto = 0xff;
161
    *uidirtyto = 0xff;
162
  } else {
162
  } else {
163
    *cursorpos = db->cursor->len - db->xoffset;
163
    *cursorpos = db->cursor->len - db->xoffset;
164
  }
164
  }
165
}
165
}
166
 
166
 
167
 
167
 
168
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
168
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
169
  if (db->cursor->prev != NULL) {
169
  if (db->cursor->prev != NULL) {
170
    db->cursor = db->cursor->prev;
170
    db->cursor = db->cursor->prev;
171
    if (*cursorposy == 0) {
171
    if (*cursorposy == 0) {
172
      db->topscreen = db->cursor;
172
      db->topscreen = db->cursor;
173
      *uidirtyfrom = 0;
173
      *uidirtyfrom = 0;
174
      *uidirtyto = 0xff;
174
      *uidirtyto = 0xff;
175
    } else {
175
    } else {
176
      *cursorposy -= 1;
176
      *cursorposy -= 1;
177
    }
177
    }
178
  }
178
  }
179
}
179
}
180
 
180
 
181
 
181
 
182
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
182
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
183
  /* adjust xoffset to make sure eol is visible on screen */
183
  /* adjust xoffset to make sure eol is visible on screen */
184
  if (db->xoffset > db->cursor->len) {
184
  if (db->xoffset > db->cursor->len) {
185
    db->xoffset = db->cursor->len - 1;
185
    db->xoffset = db->cursor->len - 1;
186
    *uidirtyfrom = 0;
186
    *uidirtyfrom = 0;
187
    *uidirtyto = 0xff;
187
    *uidirtyto = 0xff;
188
  }
188
  }
189
 
189
 
190
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
190
  if (db->xoffset + screenw - 1 <= db->cursor->len) {
191
    db->xoffset = db->cursor->len - screenw + 2;
191
    db->xoffset = db->cursor->len - screenw + 2;
192
    *uidirtyfrom = 0;
192
    *uidirtyfrom = 0;
193
    *uidirtyto = 0xff;
193
    *uidirtyto = 0xff;
194
  }
194
  }
195
  *cursorposx = db->cursor->len - db->xoffset;
195
  *cursorposx = db->cursor->len - db->xoffset;
196
}
196
}
197
 
197
 
198
 
198
 
199
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
199
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
200
  if (db->cursor->next != NULL) {
200
  if (db->cursor->next != NULL) {
201
    db->cursor = db->cursor->next;
201
    db->cursor = db->cursor->next;
202
    if (*cursorposy < screenh - 2) {
202
    if (*cursorposy < screenh - 2) {
203
      *cursorposy += 1;
203
      *cursorposy += 1;
204
    } else {
204
    } else {
205
      db->topscreen = db->topscreen->next;
205
      db->topscreen = db->topscreen->next;
206
      *uidirtyfrom = 0;
206
      *uidirtyfrom = 0;
207
      *uidirtyto = 0xff;
207
      *uidirtyto = 0xff;
208
    }
208
    }
209
  }
209
  }
210
}
210
}
211
 
211
 
212
 
212
 
213
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
213
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
214
  *cursorposx = 0;
214
  *cursorposx = 0;
215
  if (db->xoffset != 0) {
215
  if (db->xoffset != 0) {
216
    db->xoffset = 0;
216
    db->xoffset = 0;
217
    *uidirtyfrom = 0;
217
    *uidirtyfrom = 0;
218
    *uidirtyto = 0xff;
218
    *uidirtyto = 0xff;
219
  }
219
  }
220
}
220
}
221
 
221
 
222
 
222
 
-
 
223
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
-
 
224
 * of argc and argv, saves some 330 bytes of binary size */
223
int main(int argc, char **argv) {
225
static char *parseargv(void) {
-
 
226
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
-
 
227
  unsigned char count = 0;
-
 
228
  char *argv[4];
-
 
229
 
-
 
230
  mdr_coutraw_puts(tail);
-
 
231
 
-
 
232
  while (count < 4) {
-
 
233
    /* jump to nearest arg */
-
 
234
    while (*tail == ' ') {
-
 
235
      *tail = 0;
-
 
236
      tail++;
-
 
237
    }
-
 
238
 
-
 
239
    if (*tail == '\r') {
-
 
240
      *tail = 0;
-
 
241
      break;
-
 
242
    }
-
 
243
 
-
 
244
    argv[count++] = tail;
-
 
245
 
-
 
246
    /* jump to next delimiter */
-
 
247
    while ((*tail != ' ') && (*tail != '\r')) tail++;
-
 
248
  }
-
 
249
 
-
 
250
  /* check args now */
-
 
251
  if (count != 1) return(NULL);
-
 
252
 
-
 
253
  return(argv[0]);
-
 
254
}
-
 
255
 
-
 
256
 
-
 
257
int main(void) {
224
  int fd;
258
  int fd;
225
  const char *fname = NULL;
259
  const char *fname;
226
  char buff[1024];
260
  char buff[1024];
227
  struct linedb db;
261
  struct linedb db;
228
  unsigned char screenw = 0, screenh = 0;
262
  unsigned char screenw = 0, screenh = 0;
229
  unsigned char cursorposx = 0, cursorposy = 0;
263
  unsigned char cursorposx = 0, cursorposy = 0;
230
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
264
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
231
 
265
 
232
  bzero(&db, sizeof(db));
266
  bzero(&db, sizeof(db));
233
 
267
 
234
  svarlang_autoload_nlspath("sved");
268
  svarlang_autoload_nlspath("sved");
235
 
269
 
-
 
270
  fname = parseargv();
-
 
271
 
236
  if ((argc != 2) || (argv[1][0] == '/')) {
272
  if (fname == NULL) {
237
    mdr_coutraw_puts("usage: sved file.txt");
273
    mdr_coutraw_puts("usage: sved file.txt");
238
    return(0);
274
    return(0);
239
  }
275
  }
240
 
276
 
241
  fname = argv[1];
-
 
242
 
-
 
243
  mdr_coutraw_puts("");
277
  mdr_coutraw_puts("");
244
 
278
 
245
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
279
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
246
    mdr_coutraw_puts("Failed to open file:");
280
    mdr_coutraw_puts("Failed to open file:");
247
    mdr_coutraw_puts(fname);
281
    mdr_coutraw_puts(fname);
248
    return(1);
282
    return(1);
249
  }
283
  }
250
 
284
 
251
  /* load file */
285
  /* load file */
252
  {
286
  {
253
    unsigned int prevlen = 0, len, llen;
287
    unsigned int prevlen = 0, len, llen;
254
 
288
 
255
    do {
289
    do {
256
      if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) != 0) break;
290
      if (_dos_read(fd, buff + prevlen, sizeof(buff) - prevlen, &len) != 0) break;
257
      len += prevlen;
291
      len += prevlen;
258
 
292
 
259
      /* look for nearest \n and replace with 0*/
293
      /* look for nearest \n and replace with 0*/
260
      for (llen = 0; buff[llen] != '\n'; llen++) {
294
      for (llen = 0; buff[llen] != '\n'; llen++) {
261
        if (llen == sizeof(buff)) break;
295
        if (llen == sizeof(buff)) break;
262
      }
296
      }
263
      buff[llen] = 0;
297
      buff[llen] = 0;
264
      if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
298
      if ((llen > 0) && (buff[llen - 1])) buff[llen - 1] = 0; /* trim \r if line ending is cr/lf */
265
      line_add(&db, buff);
299
      line_add(&db, buff);
266
 
300
 
267
      len -= llen + 1;
301
      len -= llen + 1;
268
      memmove(buff, buff + llen + 1, len);
302
      memmove(buff, buff + llen + 1, len);
269
      prevlen = len;
303
      prevlen = len;
270
    } while (len > 0);
304
    } while (len > 0);
271
 
305
 
272
  }
306
  }
273
 
307
 
274
  /* add an empty line at end if not present already */
308
  /* add an empty line at end if not present already */
275
  if (db.cursor->len != 0) line_add(&db, "");
309
  if (db.cursor->len != 0) line_add(&db, "");
276
 
310
 
277
  _dos_close(fd);
311
  _dos_close(fd);
278
 
312
 
279
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
313
  if (mdr_cout_init(&screenw, &screenh)) load_colorscheme();
280
  ui_basic(screenw, screenh, fname);
314
  ui_basic(screenw, screenh, fname);
281
 
315
 
282
  db_rewind(&db);
316
  db_rewind(&db);
283
 
317
 
284
  for (;;) {
318
  for (;;) {
285
    int k;
319
    int k;
286
 
320
 
287
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
321
    check_cursor_not_after_eol(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
288
    mdr_cout_locate(cursorposy, cursorposx);
322
    mdr_cout_locate(cursorposy, cursorposx);
289
 
323
 
290
    if (uidirtyfrom != 0xff) {
324
    if (uidirtyfrom != 0xff) {
291
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
325
      ui_refresh(&db, screenw, screenh, uidirtyfrom, uidirtyto);
292
      uidirtyfrom = 0xff;
326
      uidirtyfrom = 0xff;
293
    }
327
    }
294
 
328
 
295
    k = keyb_getkey();
329
    k = keyb_getkey();
296
 
330
 
297
    if (k == 0x150) { /* down */
331
    if (k == 0x150) { /* down */
298
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
332
      cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
299
 
333
 
300
    } else if (k == 0x148) { /* up */
334
    } else if (k == 0x148) { /* up */
301
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
335
      cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
302
 
336
 
303
    } else if (k == 0x14D) { /* right */
337
    } else if (k == 0x14D) { /* right */
304
      if (db.cursor->len > db.xoffset + cursorposx) {
338
      if (db.cursor->len > db.xoffset + cursorposx) {
305
        if (cursorposx < screenw - 2) {
339
        if (cursorposx < screenw - 2) {
306
          cursorposx++;
340
          cursorposx++;
307
        } else {
341
        } else {
308
          db.xoffset++;
342
          db.xoffset++;
309
          uidirtyfrom = 0;
343
          uidirtyfrom = 0;
310
          uidirtyto = 0xff;
344
          uidirtyto = 0xff;
311
        }
345
        }
312
      } else {
346
      } else {
313
        cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
347
        cursor_down(&db, &cursorposy, screenh, &uidirtyfrom, &uidirtyto);
314
        cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
348
        cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
315
      }
349
      }
316
 
350
 
317
    } else if (k == 0x14B) { /* left */
351
    } else if (k == 0x14B) { /* left */
318
      if (cursorposx > 0) {
352
      if (cursorposx > 0) {
319
        cursorposx--;
353
        cursorposx--;
320
      } else if (db.xoffset > 0) {
354
      } else if (db.xoffset > 0) {
321
        db.xoffset--;
355
        db.xoffset--;
322
        uidirtyfrom = 0;
356
        uidirtyfrom = 0;
323
        uidirtyto = 0xff;
357
        uidirtyto = 0xff;
324
      } else if (db.cursor->prev != NULL) { /* jump to end of line above */
358
      } else if (db.cursor->prev != NULL) { /* jump to end of line above */
325
        cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
359
        cursor_up(&db, &cursorposy, &uidirtyfrom, &uidirtyto);
326
        cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
360
        cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
327
      }
361
      }
328
 
362
 
329
    } else if (k == 0x149) { /* pgup */
363
    } else if (k == 0x149) { /* pgup */
330
      // TODO
364
      // TODO
331
 
365
 
332
    } else if (k == 0x151) { /* pgdown */
366
    } else if (k == 0x151) { /* pgdown */
333
      // TODO
367
      // TODO
334
 
368
 
335
    } else if (k == 0x147) { /* home */
369
    } else if (k == 0x147) { /* home */
336
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
370
       cursor_home(&db, &cursorposx, &uidirtyfrom, &uidirtyto);
337
 
371
 
338
    } else if (k == 0x14F) { /* end */
372
    } else if (k == 0x14F) { /* end */
339
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
373
       cursor_eol(&db, &cursorposx, screenw, &uidirtyfrom, &uidirtyto);
340
 
374
 
341
    } else if (k == 0x1B) { /* ESC */
375
    } else if (k == 0x1B) { /* ESC */
342
      break;
376
      break;
343
 
377
 
344
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
378
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
345
      char buff[4];
379
      char buff[4];
346
      const char *HEX = "0123456789ABCDEF";
380
      const char *HEX = "0123456789ABCDEF";
347
      buff[0] = HEX[(k >> 8) & 15];
381
      buff[0] = HEX[(k >> 8) & 15];
348
      buff[1] = HEX[(k >> 4) & 15];
382
      buff[1] = HEX[(k >> 4) & 15];
349
      buff[2] = HEX[k & 15];
383
      buff[2] = HEX[k & 15];
350
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
384
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", scheme[COL_STATUSBAR1], 17);
351
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
385
      mdr_cout_str(screenh - 1, 17, buff, scheme[COL_STATUSBAR1], 3);
352
      keyb_getkey();
386
      keyb_getkey();
353
      break;
387
      break;
354
    }
388
    }
355
  }
389
  }
356
 
390
 
357
  mdr_cout_close();
391
  mdr_cout_close();
358
 
392
 
359
  /* TODO free memory */
393
  /* TODO free memory */
360
 
394
 
361
  return(0);
395
  return(0);
362
}
396
}
363
 
397