Subversion Repositories SvarDOS

Rev

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

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