Subversion Repositories SvarDOS

Rev

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

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