Subversion Repositories SvarDOS

Rev

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

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