Subversion Repositories SvarDOS

Rev

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

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