Subversion Repositories SvarDOS

Rev

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

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