Subversion Repositories SvarDOS

Rev

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

Rev 1427 Rev 1428
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>      /* _dos_open(), _dos_read(), _dos_close(), ... */
26
#include <dos.h>      /* _dos_open(), _dos_read(), _dos_close(), ... */
27
#include <fcntl.h>    /* O_RDONLY, O_WRONLY */
27
#include <fcntl.h>    /* O_RDONLY, O_WRONLY */
28
#include <string.h>
28
#include <string.h>
29
 
29
 
30
#include "mdr\bios.h"
30
#include "mdr\bios.h"
31
#include "mdr\cout.h"
31
#include "mdr\cout.h"
32
#include "mdr\dos.h"
32
#include "mdr\dos.h"
33
 
33
 
34
#include "svarlang\svarlang.h"
34
#include "svarlang\svarlang.h"
35
 
35
 
36
 
36
 
37
#define PVER "2023.0"
37
#define PVER "2023.0"
38
#define PDATE "2023"
38
#define PDATE "2023"
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_MENU   = 0x70,
46
                     SCHEME_MENU   = 0x70,
47
                     SCHEME_MENU_CUR= 0x0f,
47
                     SCHEME_MENU_CUR= 0x0f,
48
                     SCHEME_MENU_SEL= 0x00,
48
                     SCHEME_MENU_SEL= 0x00,
49
                     SCHEME_STBAR1 = 0x70,
49
                     SCHEME_STBAR1 = 0x70,
50
                     SCHEME_STBAR2 = 0x70, /* greyed out information */
50
                     SCHEME_STBAR2 = 0x70, /* greyed out information */
51
                     SCHEME_STBAR3 = 0x70, /* query */
51
                     SCHEME_STBAR3 = 0x70, /* query */
52
                     SCHEME_SCROLL = 0x70,
52
                     SCHEME_SCROLL = 0x70,
53
                     SCHEME_MSG    = 0x70,
53
                     SCHEME_MSG    = 0x70,
54
                     SCHEME_ERR    = 0x70;
54
                     SCHEME_ERR    = 0x70;
55
 
55
 
56
static unsigned char screenw, screenh, screenlastrow, screenlastcol;
56
static unsigned char screenw, screenh, screenlastrow, screenlastcol;
57
static unsigned char glob_monomode, glob_tablessmode;
57
static unsigned char glob_monomode, glob_tablessmode;
58
 
58
 
59
static struct {
59
static struct {
60
    unsigned char from;
60
    unsigned char from;
61
    unsigned char to;
61
    unsigned char to;
62
    unsigned char statusbar;
62
    unsigned char statusbar;
63
} uidirty = {0, 0xff, 1}; /* make sure to redraw entire UI at first run */
63
} uidirty = {0, 0xff, 1}; /* make sure to redraw entire UI at first run */
64
 
64
 
65
#define SCROLL_CURSOR 0xB1
65
#define SCROLL_CURSOR 0xB1
66
 
66
 
67
struct line {
67
struct line {
68
  struct line far *prev;
68
  struct line far *prev;
69
  struct line far *next;
69
  struct line far *next;
70
  unsigned short len;
70
  unsigned short len;
71
  char payload[1];
71
  char payload[1];
72
};
72
};
73
 
73
 
74
struct file {
74
struct file {
75
  struct line far *cursor;
75
  struct line far *cursor;
76
  unsigned short xoffset;
76
  unsigned short xoffset;
77
  unsigned short cursorposx;
77
  unsigned short cursorposx;
78
  unsigned short cursorposy;
78
  unsigned short cursorposy;
79
  unsigned short totlines;
79
  unsigned short totlines;
80
  unsigned short curline;
80
  unsigned short curline;
81
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
81
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
82
  char modflag;  /* non-zero if file has been modified since last save */
82
  char modflag;  /* non-zero if file has been modified since last save */
83
  char modflagprev;
83
  char modflagprev;
84
  char fname[128];
84
  char fname[128];
85
};
85
};
86
 
86
 
87
 
87
 
88
/*****************************************************************************
88
/*****************************************************************************
89
 * functions                                                                 *
89
 * functions                                                                 *
90
 *****************************************************************************/
90
 *****************************************************************************/
91
 
91
 
92
static struct line far *line_calloc(unsigned short siz) {
92
static struct line far *line_calloc(unsigned short siz) {
93
  struct line far *res;
93
  struct line far *res;
94
  unsigned int seg;
94
  unsigned int seg;
95
  if (_dos_allocmem((sizeof(struct line) + siz + 15) / 16, &seg) != 0) return(NULL);
95
  if (_dos_allocmem((sizeof(struct line) + siz + 15) / 16, &seg) != 0) return(NULL);
96
  res = MK_FP(seg, 0);
96
  res = MK_FP(seg, 0);
97
  _fmemset(res, 0, sizeof(struct line) + siz);
97
  _fmemset(res, 0, sizeof(struct line) + siz);
98
  return(MK_FP(seg, 0));
98
  return(MK_FP(seg, 0));
99
}
99
}
100
 
100
 
101
 
101
 
102
static void line_free(struct line far *ptr) {
102
static void line_free(struct line far *ptr) {
103
  _dos_freemem(FP_SEG(ptr));
103
  _dos_freemem(FP_SEG(ptr));
104
}
104
}
105
 
105
 
106
 
106
 
107
static int curline_resize(struct file far *db, unsigned short newsiz) {
107
static int curline_resize(struct file far *db, unsigned short newsiz) {
108
  unsigned int maxavail;
108
  unsigned int maxavail;
109
  struct line far *newptr;
109
  struct line far *newptr;
110
 
110
 
111
  /* try resizing the block (much faster) */
111
  /* try resizing the block (much faster) */
112
  if (_dos_setblock((sizeof(struct line) + newsiz + 15) / 16, FP_SEG(db->cursor), &maxavail) == 0) return(0);
112
  if (_dos_setblock((sizeof(struct line) + newsiz + 15) / 16, FP_SEG(db->cursor), &maxavail) == 0) return(0);
113
 
113
 
114
  /* create a new block and copy data over */
114
  /* create a new block and copy data over */
115
  newptr = line_calloc(newsiz);
115
  newptr = line_calloc(newsiz);
116
  if (newptr == NULL) return(-1);
116
  if (newptr == NULL) return(-1);
117
  _fmemmove(newptr, db->cursor, sizeof(struct line) + db->cursor->len);
117
  _fmemmove(newptr, db->cursor, sizeof(struct line) + db->cursor->len);
118
 
118
 
119
  /* rewire the linked list */
119
  /* rewire the linked list */
120
  db->cursor = newptr;
120
  db->cursor = newptr;
121
  if (newptr->next) newptr->next->prev = newptr;
121
  if (newptr->next) newptr->next->prev = newptr;
122
  if (newptr->prev) newptr->prev->next = newptr;
122
  if (newptr->prev) newptr->prev->next = newptr;
123
 
123
 
124
  return(0);
124
  return(0);
125
}
125
}
126
 
126
 
127
 
127
 
128
/* adds a new line at cursor position into file linked list and advance cursor
128
/* adds a new line at cursor position into file linked list and advance cursor
129
 * returns non-zero on error */
129
 * returns non-zero on error */
130
static int line_add(struct file *db, const char far *line, unsigned short slen) {
130
static int line_add(struct file *db, const char far *line, unsigned short slen) {
131
  struct line far *l;
131
  struct line far *l;
132
 
132
 
133
  l = line_calloc(slen);
133
  l = line_calloc(slen);
134
  if (l == NULL) return(-1);
134
  if (l == NULL) return(-1);
135
 
135
 
136
  l->prev = db->cursor;
136
  l->prev = db->cursor;
137
  if (db->cursor) {
137
  if (db->cursor) {
138
    l->next = db->cursor->next;
138
    l->next = db->cursor->next;
139
    db->cursor->next = l;
139
    db->cursor->next = l;
140
    l->next->prev = l;
140
    l->next->prev = l;
141
  }
141
  }
142
  db->cursor = l;
142
  db->cursor = l;
143
  if (slen > 0) {
143
  if (slen > 0) {
144
    _fmemmove(l->payload, line, slen);
144
    _fmemmove(l->payload, line, slen);
145
    l->len = slen;
145
    l->len = slen;
146
  }
146
  }
147
 
147
 
148
  db->totlines += 1;
148
  db->totlines += 1;
149
  db->curline += 1;
149
  db->curline += 1;
150
 
150
 
151
  return(0);
151
  return(0);
152
}
152
}
153
 
153
 
154
 
154
 
155
static void ui_getstring(const char *query, char *s, unsigned short maxlen) {
155
static void ui_getstring(const char *query, char *s, unsigned short maxlen) {
156
  unsigned short len = 0;
156
  unsigned short len = 0;
157
  unsigned char y, x;
157
  unsigned char y, x;
158
  int k;
158
  int k;
159
 
159
 
160
  if (maxlen == 0) return;
160
  if (maxlen == 0) return;
161
  maxlen--; /* make room for the nul terminator */
161
  maxlen--; /* make room for the nul terminator */
162
 
162
 
163
  y = screenlastrow;
163
  y = screenlastrow;
164
 
164
 
165
  /* print query string */
165
  /* print query string */
166
  x = mdr_cout_str(y, 0, query, SCHEME_STBAR3, 40);
166
  x = mdr_cout_str(y, 0, query, SCHEME_STBAR3, 40);
167
  mdr_cout_char_rep(y, x++, ' ', SCHEME_STBAR3, screenw - x);
167
  mdr_cout_char_rep(y, x++, ' ', SCHEME_STBAR3, screenw - x);
168
 
168
 
169
  for (;;) {
169
  for (;;) {
170
    mdr_cout_locate(y, x + len);
170
    mdr_cout_locate(y, x + len);
171
    k = mdr_dos_getkey2();
171
    k = mdr_dos_getkey2();
172
 
172
 
173
    switch (k) {
173
    switch (k) {
174
      case 0x1b: /* ESC */
174
      case 0x1b: /* ESC */
175
        s[0] = 0;
175
        s[0] = 0;
176
        return;
176
        return;
177
      case '\r':
177
      case '\r':
178
        s[len] = 0;
178
        s[len] = 0;
179
        return;
179
        return;
180
      case 0x08: /* BKSPC */
180
      case 0x08: /* BKSPC */
181
        if (len > 0) {
181
        if (len > 0) {
182
          len--;
182
          len--;
183
          mdr_cout_char(y, x + len, ' ', SCHEME_STBAR3);
183
          mdr_cout_char(y, x + len, ' ', SCHEME_STBAR3);
184
        }
184
        }
185
        break;
185
        break;
186
      default:
186
      default:
187
        if ((k <= 0xff) && (k >= ' ') && (len < maxlen)) {
187
        if ((k <= 0xff) && (k >= ' ') && (len < maxlen)) {
188
          mdr_cout_char(y, x + len, k, SCHEME_STBAR3);
188
          mdr_cout_char(y, x + len, k, SCHEME_STBAR3);
189
          s[len++] = k;
189
          s[len++] = k;
190
        }
190
        }
191
    }
191
    }
192
  }
192
  }
193
 
193
 
194
}
194
}
195
 
195
 
196
 
196
 
197
/* append a nul-terminated string to line at cursor position */
197
/* append a nul-terminated string to line at cursor position */
198
static int line_append(struct file *f, const char far *buf, unsigned short len) {
198
static int line_append(struct file *f, const char far *buf, unsigned short len) {
199
  if (sizeof(struct line) + f->cursor->len + len < len) goto ERR; /* overflow check */
199
  if (sizeof(struct line) + f->cursor->len + len < len) goto ERR; /* overflow check */
200
  if (curline_resize(f, f->cursor->len + len) != 0) goto ERR;
200
  if (curline_resize(f, f->cursor->len + len) != 0) goto ERR;
201
 
201
 
202
  _fmemmove(f->cursor->payload + f->cursor->len, buf, len);
202
  _fmemmove(f->cursor->payload + f->cursor->len, buf, len);
203
  f->cursor->len += len;
203
  f->cursor->len += len;
204
 
204
 
205
  return(0);
205
  return(0);
206
  ERR:
206
  ERR:
207
  return(-1);
207
  return(-1);
208
}
208
}
209
 
209
 
210
 
210
 
211
static void db_rewind(struct file *db) {
211
static void db_rewind(struct file *db) {
212
  if (db->cursor == NULL) return;
212
  if (db->cursor == NULL) return;
213
  while (db->cursor->prev) db->cursor = db->cursor->prev;
213
  while (db->cursor->prev) db->cursor = db->cursor->prev;
214
  db->curline = 0;
214
  db->curline = 0;
215
}
215
}
216
 
216
 
217
 
217
 
218
static void ui_basic(const struct file *db, unsigned char slotnum) {
218
static void ui_basic(const struct file *db, unsigned char slotnum) {
219
  const char *s = svarlang_strid(0); /* ESC=MENU */
219
  const char *s = svarlang_strid(0); /* ESC=MENU */
220
  unsigned short helpcol = screenw - strlen(s);
220
  unsigned short helpcol = screenw - strlen(s);
221
  unsigned short maxfnlen = helpcol - 14;
221
  unsigned short maxfnlen = helpcol - 14;
222
 
222
 
223
  /* slot number (guaranteed to be 0-9) */
223
  /* slot number (guaranteed to be 0-9) */
224
  {
224
  {
225
    char slot[4] = "#00";
225
    char slot[4] = "#00";
226
    if (slotnum == 9) {
226
    if (slotnum == 9) {
227
      slot[1] = '1';
227
      slot[1] = '1';
228
    } else {
228
    } else {
229
      slotnum++;
229
      slotnum++;
230
      slot[2] += slotnum;
230
      slot[2] += slotnum;
231
    }
231
    }
232
    mdr_cout_str(screenlastrow, 0, slot, SCHEME_STBAR2, 3);
232
    mdr_cout_str(screenlastrow, 0, slot, SCHEME_STBAR2, 3);
233
  }
233
  }
234
 
234
 
235
  /* fill rest of status bar with background */
235
  /* fill rest of status bar with background */
236
  mdr_cout_char_rep(screenlastrow, 3, ' ', SCHEME_STBAR1, helpcol - 3);
236
  mdr_cout_char_rep(screenlastrow, 3, ' ', SCHEME_STBAR1, helpcol - 3);
237
 
237
 
238
  /* filename and modflag */
238
  /* filename and modflag */
239
  {
239
  {
240
    const char *fn;
240
    const char *fn;
241
    unsigned short x;
241
    unsigned short x;
242
    if (db->fname[0] == 0) {
242
    if (db->fname[0] == 0) {
243
      fn = svarlang_str(0, 1); /* "UNTITLED" */
243
      fn = svarlang_str(0, 1); /* "UNTITLED" */
244
    } else {
244
    } else {
245
      /* display filename up to maxfnlen chars */
245
      /* display filename up to maxfnlen chars */
246
      fn = db->fname;
246
      fn = db->fname;
247
      x = strlen(fn);
247
      x = strlen(fn);
248
      if (x > maxfnlen) fn += x - maxfnlen;
248
      if (x > maxfnlen) fn += x - maxfnlen;
249
    }
249
    }
250
    x = mdr_cout_str(screenlastrow, 4, fn, SCHEME_STBAR1, maxfnlen);
250
    x = mdr_cout_str(screenlastrow, 4, fn, SCHEME_STBAR1, maxfnlen);
251
    if (db->modflag) mdr_cout_char(screenlastrow, 5 + x, '!', SCHEME_STBAR2);
251
    if (db->modflag) mdr_cout_char(screenlastrow, 5 + x, '!', SCHEME_STBAR2);
252
  }
252
  }
253
 
253
 
254
  /* eol type */
254
  /* eol type */
255
  {
255
  {
256
    const char *eoltype = "CRLF";
256
    const char *eoltype = "CRLF";
257
    if (db->lfonly) eoltype += 2;
257
    if (db->lfonly) eoltype += 2;
258
    mdr_cout_str(screenlastrow, helpcol - 6, eoltype, SCHEME_STBAR1, 5);
258
    mdr_cout_str(screenlastrow, helpcol - 6, eoltype, SCHEME_STBAR1, 5);
259
  }
259
  }
260
 
260
 
261
  mdr_cout_str(screenlastrow, helpcol, s, SCHEME_STBAR2, 40);
261
  mdr_cout_str(screenlastrow, helpcol, s, SCHEME_STBAR2, 40);
262
}
262
}
263
 
263
 
264
 
264
 
265
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
265
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
266
  unsigned short x, y, msglen, i;
266
  unsigned short x, y, msglen, i;
267
  unsigned char msg2flag = 0;
267
  unsigned short msg2flag = 0;
268
 
268
 
269
  msglen = strlen(msg1);
269
  msglen = strlen(msg1);
270
  if (msg2) {
270
  if (msg2) {
271
    msg2flag = 1;
271
    msg2flag = 1;
272
    i = strlen(msg2);
272
    i = strlen(msg2);
273
    if (i > msglen) msglen = i;
273
    if (i > msglen) msglen = i;
274
  }
274
  }
275
 
275
 
276
  y = (screenh - 6) >> 1;
276
  y = (screenh - 6) >> 1;
277
  x = (screenw - msglen - 4) >> 1;
277
  x = (screenw - msglen - 4) >> 1;
278
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
278
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
279
  x++;
279
  x++;
280
 
280
 
281
  mdr_cout_str(y+1, x, msg1, attr, msglen);
281
  mdr_cout_str(y+1, x, msg1, attr, msglen);
282
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
282
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
283
 
283
 
284
  if (uidirty.from > y) uidirty.from = y;
284
  if (uidirty.from > y) uidirty.from = y;
285
  if (uidirty.to < y+4) uidirty.to = y+4;
285
  if (uidirty.to < y+4) uidirty.to = y+4;
286
}
286
}
287
 
287
 
288
 
288
 
289
static unsigned char ui_confirm_if_unsaved(const struct file *db) {
289
static unsigned char ui_confirm_if_unsaved(const struct file *db) {
290
  unsigned char r = 0;
290
  unsigned char r = 0;
291
  if (db->modflag == 0) return(0);
291
  if (db->modflag == 0) return(0);
292
 
292
 
293
  mdr_cout_cursor_hide();
293
  mdr_cout_cursor_hide();
294
 
294
 
295
  /* if file has been modified then ask for confirmation */
295
  /* if file has been modified then ask for confirmation */
296
  ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
296
  ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
297
  if (mdr_dos_getkey2() != '\r') r = 1;
297
  if (mdr_dos_getkey2() != '\r') r = 1;
298
 
298
 
299
  mdr_cout_cursor_show();
299
  mdr_cout_cursor_show();
300
 
300
 
301
  return(r);
301
  return(r);
302
}
302
}
303
 
303
 
304
 
304
 
305
static void ui_refresh(const struct file *db) {
305
static void ui_refresh(const struct file *db) {
306
  unsigned char x;
306
  unsigned char x;
307
  const struct line far *l;
307
  const struct line far *l;
308
  unsigned char y = db->cursorposy;
308
  unsigned char y = db->cursorposy;
309
 
309
 
310
  /* quit early if nothing to refresh */
310
  /* quit early if nothing to refresh */
311
  if (uidirty.from == 0xff) return;
311
  if (uidirty.from == 0xff) return;
312
 
312
 
313
#ifdef DBG_REFRESH
313
#ifdef DBG_REFRESH
314
  static char m = 'a';
314
  static char m = 'a';
315
  m++;
315
  m++;
316
  if (m > 'z') m = 'a';
316
  if (m > 'z') m = 'a';
317
#endif
317
#endif
318
 
318
 
319
  /* rewind cursor line to first line that needs redrawing */
319
  /* rewind cursor line to first line that needs redrawing */
320
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
320
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
321
 
321
 
322
  /* iterate over lines and redraw whatever needs to be redrawn */
322
  /* iterate over lines and redraw whatever needs to be redrawn */
323
  for (; l != NULL; l = l->next, y++) {
323
  for (; l != NULL; l = l->next, y++) {
324
 
324
 
325
    /* skip lines that do not need to be refreshed */
325
    /* skip lines that do not need to be refreshed */
326
    if (y < uidirty.from) continue;
326
    if (y < uidirty.from) continue;
327
    if (y > uidirty.to) break;
327
    if (y > uidirty.to) break;
328
 
328
 
329
    x = 0;
329
    x = 0;
330
    if (db->xoffset < l->len) {
330
    if (db->xoffset < l->len) {
331
      unsigned char i, limit;
331
      unsigned char i, limit;
332
      if (l->len - db->xoffset < screenw) {
332
      if (l->len - db->xoffset < screenw) {
333
        limit = l->len;
333
        limit = l->len;
334
      } else {
334
      } else {
335
        limit = db->xoffset + screenlastcol;
335
        limit = db->xoffset + screenlastcol;
336
      }
336
      }
337
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
337
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
338
    }
338
    }
339
 
339
 
340
    /* write empty spaces until end of line */
340
    /* write empty spaces until end of line */
341
    if (x < screenlastcol) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenlastcol - x);
341
    if (x < screenlastcol) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenlastcol - x);
342
 
342
 
343
#ifdef DBG_REFRESH
343
#ifdef DBG_REFRESH
344
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
344
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
345
#endif
345
#endif
346
 
346
 
347
    if (y == screenh - 2) break;
347
    if (y == screenh - 2) break;
348
  }
348
  }
349
 
349
 
350
  /* fill all lines below if empty (and they need to be redrawn) */
350
  /* fill all lines below if empty (and they need to be redrawn) */
351
  if (l == NULL) {
351
  if (l == NULL) {
352
    while ((y < screenlastrow) && (y < uidirty.to)) {
352
    while ((y < screenlastrow) && (y < uidirty.to)) {
353
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenlastcol);
353
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenlastcol);
354
    }
354
    }
355
  }
355
  }
356
 
356
 
357
  /* scroll bar */
357
  /* scroll bar */
358
  for (y = 0; y < screenlastrow; y++) {
358
  for (y = 0; y < screenlastrow; y++) {
359
    mdr_cout_char(y, screenlastcol, SCROLL_CURSOR, SCHEME_SCROLL);
359
    mdr_cout_char(y, screenlastcol, SCROLL_CURSOR, SCHEME_SCROLL);
360
  }
360
  }
361
 
361
 
362
  /* scroll cursor */
362
  /* scroll cursor */
363
  if (db->totlines >= screenh) {
363
  if (db->totlines >= screenh) {
364
    unsigned short topline = db->curline - db->cursorposy;
364
    unsigned short topline = db->curline - db->cursorposy;
365
    unsigned short col;
365
    unsigned short col;
366
    unsigned short totlines = db->totlines - screenh + 1;
366
    unsigned short totlines = db->totlines - screenh + 1;
367
    if (db->totlines - screenh > screenh) {
367
    if (db->totlines - screenh > screenh) {
368
      col = topline / (totlines / screenlastrow);
368
      col = topline / (totlines / screenlastrow);
369
    } else {
369
    } else {
370
      col = topline * screenlastrow / totlines;
370
      col = topline * screenlastrow / totlines;
371
    }
371
    }
372
    if (col >= screenlastrow) col = screenh - 2;
372
    if (col >= screenlastrow) col = screenh - 2;
373
    mdr_cout_char(col, screenlastcol, ' ', SCHEME_SCROLL);
373
    mdr_cout_char(col, screenlastcol, ' ', SCHEME_SCROLL);
374
  }
374
  }
375
 
375
 
376
  /* clear out the dirty flag */
376
  /* clear out the dirty flag */
377
  uidirty.from = 0xff;
377
  uidirty.from = 0xff;
378
}
378
}
379
 
379
 
380
 
380
 
381
static void check_cursor_not_after_eol(struct file *db) {
381
static void check_cursor_not_after_eol(struct file *db) {
382
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
382
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
383
 
383
 
384
  if (db->cursor->len < db->xoffset) {
384
  if (db->cursor->len < db->xoffset) {
385
    db->cursorposx = 0;
385
    db->cursorposx = 0;
386
    db->xoffset = db->cursor->len;
386
    db->xoffset = db->cursor->len;
387
    uidirty.from = 0;
387
    uidirty.from = 0;
388
    uidirty.to = 0xff;
388
    uidirty.to = 0xff;
389
  } else {
389
  } else {
390
    db->cursorposx = db->cursor->len - db->xoffset;
390
    db->cursorposx = db->cursor->len - db->xoffset;
391
  }
391
  }
392
}
392
}
393
 
393
 
394
 
394
 
395
static void cursor_up(struct file *db) {
395
static void cursor_up(struct file *db) {
396
 
-
 
397
  if (db->cursor->prev == NULL) return;
396
  if (db->cursor->prev == NULL) return;
398
 
397
 
399
  db->curline -= 1;
398
  db->curline -= 1;
400
  db->cursor = db->cursor->prev;
399
  db->cursor = db->cursor->prev;
401
  if (db->cursorposy == 0) {
400
  if (db->cursorposy == 0) {
402
    uidirty.from = 0;
401
    uidirty.from = 0;
403
    uidirty.to = 0xff;
402
    uidirty.to = 0xff;
404
  } else {
403
  } else {
405
    db->cursorposy -= 1;
404
    db->cursorposy -= 1;
406
  }
405
  }
407
}
406
}
408
 
407
 
409
 
408
 
410
static void cursor_eol(struct file *db) {
409
static void cursor_eol(struct file *db) {
411
  /* adjust xoffset to make sure eol is visible on screen */
410
  /* adjust xoffset to make sure eol is visible on screen */
412
  if (db->xoffset > db->cursor->len) {
411
  if (db->xoffset > db->cursor->len) {
413
    db->xoffset = db->cursor->len - 1;
412
    db->xoffset = db->cursor->len - 1;
414
    uidirty.from = 0;
413
    uidirty.from = 0;
415
    uidirty.to = 0xff;
414
    uidirty.to = 0xff;
416
  }
415
  }
417
 
416
 
418
  if (db->xoffset + screenlastcol <= db->cursor->len) {
417
  if (db->xoffset + screenlastcol <= db->cursor->len) {
419
    db->xoffset = db->cursor->len - screenw + 2;
418
    db->xoffset = db->cursor->len - screenw + 2;
420
    uidirty.from = 0;
419
    uidirty.from = 0;
421
    uidirty.to = 0xff;
420
    uidirty.to = 0xff;
422
  }
421
  }
423
  db->cursorposx = db->cursor->len - db->xoffset;
422
  db->cursorposx = db->cursor->len - db->xoffset;
424
}
423
}
425
 
424
 
426
 
425
 
427
static void cursor_down(struct file *db) {
426
static void cursor_down(struct file *db) {
428
 
-
 
429
  if (db->cursor->next == NULL) return;
427
  if (db->cursor->next == NULL) return;
430
 
428
 
431
  db->curline += 1;
429
  db->curline += 1;
432
  db->cursor = db->cursor->next;
430
  db->cursor = db->cursor->next;
433
  if (db->cursorposy < screenh - 2) {
431
  if (db->cursorposy < screenh - 2) {
434
    db->cursorposy += 1;
432
    db->cursorposy += 1;
435
  } else {
433
  } else {
436
    uidirty.from = 0;
434
    uidirty.from = 0;
437
    uidirty.to = 0xff;
435
    uidirty.to = 0xff;
438
  }
436
  }
439
}
437
}
440
 
438
 
441
 
439
 
442
static void cursor_left(struct file *db) {
440
static void cursor_left(struct file *db) {
443
  if (db->cursorposx > 0) {
441
  if (db->cursorposx > 0) {
444
    db->cursorposx -= 1;
442
    db->cursorposx -= 1;
445
  } else if (db->xoffset > 0) {
443
  } else if (db->xoffset > 0) {
446
    db->xoffset -= 1;
444
    db->xoffset -= 1;
447
    uidirty.from = 0;
445
    uidirty.from = 0;
448
    uidirty.to = 0xff;
446
    uidirty.to = 0xff;
449
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
447
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
450
    cursor_up(db);
448
    cursor_up(db);
451
    cursor_eol(db);
449
    cursor_eol(db);
452
  }
450
  }
453
}
451
}
454
 
452
 
455
 
453
 
456
static void cursor_home(struct file *db) {
454
static void cursor_home(struct file *db) {
457
  db->cursorposx = 0;
455
  db->cursorposx = 0;
458
  if (db->xoffset != 0) {
456
  if (db->xoffset != 0) {
459
    db->xoffset = 0;
457
    db->xoffset = 0;
460
    uidirty.from = 0;
458
    uidirty.from = 0;
461
    uidirty.to = 0xff;
459
    uidirty.to = 0xff;
462
  }
460
  }
463
}
461
}
464
 
462
 
465
 
463
 
466
static void cursor_right(struct file *db) {
464
static void cursor_right(struct file *db) {
467
  if (db->cursor->len > db->xoffset + db->cursorposx) {
465
  if (db->cursor->len > db->xoffset + db->cursorposx) {
468
    if (db->cursorposx < screenw - 2) {
466
    if (db->cursorposx < screenw - 2) {
469
      db->cursorposx += 1;
467
      db->cursorposx += 1;
470
    } else {
468
    } else {
471
      db->xoffset += 1;
469
      db->xoffset += 1;
472
      uidirty.from = 0;
470
      uidirty.from = 0;
473
      uidirty.to = 0xff;
471
      uidirty.to = 0xff;
474
    }
472
    }
475
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
473
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
476
    cursor_down(db);
474
    cursor_down(db);
477
    cursor_home(db);
475
    cursor_home(db);
478
  }
476
  }
479
}
477
}
480
 
478
 
481
 
479
 
482
static void del(struct file *db) {
480
static void del(struct file *db) {
483
  if (db->cursorposx + db->xoffset < db->cursor->len) {
481
  if (db->cursorposx + db->xoffset < db->cursor->len) {
484
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
482
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
485
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
483
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
486
    uidirty.from = db->cursorposy;
484
    uidirty.from = db->cursorposy;
487
    uidirty.to = db->cursorposy;
485
    uidirty.to = db->cursorposy;
488
    db->modflag = 1;
486
    db->modflag = 1;
489
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
487
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
490
    struct line far *nextline = db->cursor->next;
488
    struct line far *nextline = db->cursor->next;
491
    if (db->cursor->next->len > 0) {
489
    if (db->cursor->next->len > 0) {
492
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
490
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
493
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
491
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
494
        db->cursor->len += db->cursor->next->len;
492
        db->cursor->len += db->cursor->next->len;
495
      }
493
      }
496
    }
494
    }
497
 
495
 
498
    db->cursor->next = db->cursor->next->next;
496
    db->cursor->next = db->cursor->next->next;
499
    db->cursor->next->prev = db->cursor;
497
    db->cursor->next->prev = db->cursor;
500
 
498
 
501
    line_free(nextline);
499
    line_free(nextline);
502
    uidirty.from = db->cursorposy;
500
    uidirty.from = db->cursorposy;
503
    uidirty.to = 0xff;
501
    uidirty.to = 0xff;
504
    db->totlines -= 1;
502
    db->totlines -= 1;
505
    db->modflag = 1;
503
    db->modflag = 1;
506
  }
504
  }
507
}
505
}
508
 
506
 
509
 
507
 
510
static void bkspc(struct file *db) {
508
static void bkspc(struct file *db) {
511
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
509
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
512
  if ((db->cursorposx + db->xoffset == 0) && (db->cursor->prev == NULL)) return;
510
  if ((db->cursorposx + db->xoffset == 0) && (db->cursor->prev == NULL)) return;
513
 
511
 
514
  cursor_left(db);
512
  cursor_left(db);
515
  del(db);
513
  del(db);
516
}
514
}
517
 
515
 
518
 
516
 
519
/* returns 0 on success, 1 on file not found, 2 on other error */
517
/* returns 0 on success, 1 on file not found, 2 on other error */
520
static int loadfile(struct file *db, const char *fname) {
518
static int loadfile(struct file *db, const char *fname) {
521
  char buff[512]; /* read one entire sector at a time (faster) */
519
  char buff[512]; /* read one entire sector at a time (faster) */
522
  char *buffptr;
520
  char *buffptr;
523
  unsigned int len, llen;
521
  unsigned int len, llen;
524
  int fd;
522
  int fd;
525
  unsigned char eolfound;
523
  unsigned char eolfound;
526
 
524
 
527
  /* free the entire linked list of lines */
525
  /* free the entire linked list of lines */
528
  db_rewind(db);
526
  db_rewind(db);
529
  while (db->cursor) {
527
  while (db->cursor) {
530
    struct line far *victim;
528
    struct line far *victim;
531
    victim = db->cursor;
529
    victim = db->cursor;
532
    db->cursor = db->cursor->next;
530
    db->cursor = db->cursor->next;
533
    line_free(victim);
531
    line_free(victim);
534
  }
532
  }
535
 
533
 
536
  /* zero out the struct */
534
  /* zero out the struct */
537
  bzero(db, sizeof(struct file));
535
  bzero(db, sizeof(struct file));
538
 
536
 
539
  if (fname == NULL) goto SKIPLOADING;
537
  if (fname == NULL) goto SKIPLOADING;
540
 
538
 
541
  memcpy(db->fname, fname, strlen(fname));
539
  memcpy(db->fname, fname, strlen(fname));
542
 
540
 
543
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
541
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
544
    return(1);
542
    return(1);
545
  }
543
  }
546
 
544
 
547
  db->lfonly = 1;
545
  db->lfonly = 1;
548
 
546
 
549
  /* start by adding an empty line */
547
  /* start by adding an empty line */
550
  if (line_add(db, NULL, 0) != 0) {
548
  if (line_add(db, NULL, 0) != 0) {
551
    /* TODO ERROR HANDLING */
549
    /* TODO ERROR HANDLING */
552
  }
550
  }
553
 
551
 
554
  for (eolfound = 0;;) {
552
  for (eolfound = 0;;) {
555
    unsigned short consumedbytes;
553
    unsigned short consumedbytes;
556
 
554
 
557
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
555
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
558
    buffptr = buff;
556
    buffptr = buff;
559
 
557
 
560
    FINDLINE:
558
    FINDLINE:
561
 
559
 
562
    /* look for nearest \n (also expand tabs) */
560
    /* look for nearest \n (also expand tabs) */
563
    for (consumedbytes = 0;; consumedbytes++) {
561
    for (consumedbytes = 0;; consumedbytes++) {
564
 
562
 
565
      if (buffptr[consumedbytes] == '\t') {
563
      if (buffptr[consumedbytes] == '\t') {
566
        llen = consumedbytes;
564
        llen = consumedbytes;
567
        break;
565
        break;
568
      }
566
      }
569
 
567
 
570
      if (consumedbytes == len) {
568
      if (consumedbytes == len) {
571
        llen = consumedbytes;
569
        llen = consumedbytes;
572
        break;
570
        break;
573
      }
571
      }
574
      if (buffptr[consumedbytes] == '\r') {
572
      if (buffptr[consumedbytes] == '\r') {
575
        llen = consumedbytes;
573
        llen = consumedbytes;
576
        consumedbytes++;
574
        consumedbytes++;
577
        db->lfonly = 0;
575
        db->lfonly = 0;
578
        break;
576
        break;
579
      }
577
      }
580
      if (buffptr[consumedbytes] == '\n') {
578
      if (buffptr[consumedbytes] == '\n') {
581
        eolfound = 1;
579
        eolfound = 1;
582
        llen = consumedbytes;
580
        llen = consumedbytes;
583
        consumedbytes++;
581
        consumedbytes++;
584
        break;
582
        break;
585
      }
583
      }
586
    }
584
    }
587
 
585
 
588
    /* consumedbytes is the amount of bytes processed from buffptr,
586
    /* consumedbytes is the amount of bytes processed from buffptr,
589
     * llen is the length of line's payload (without its line terminator) */
587
     * llen is the length of line's payload (without its line terminator) */
590
 
588
 
591
    /* append content, if line is non-empty */
589
    /* append content, if line is non-empty */
592
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
590
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
593
      goto IOERR;
591
      goto IOERR;
594
    }
592
    }
595
 
593
 
596
    /* allocate the next line if current line ended */
594
    /* allocate the next line if current line ended */
597
    if (eolfound) {
595
    if (eolfound) {
598
      if (line_add(db, NULL, 0) != 0) {
596
      if (line_add(db, NULL, 0) != 0) {
599
        goto IOERR;
597
        goto IOERR;
600
      }
598
      }
601
      eolfound = 0;
599
      eolfound = 0;
602
    }
600
    }
603
 
601
 
604
    /* append 8 spaces if tab char found */
602
    /* append 8 spaces if tab char found */
605
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t') && (glob_tablessmode == 0)) {
603
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t') && (glob_tablessmode == 0)) {
606
      consumedbytes++;
604
      consumedbytes++;
607
      if (line_append(db, "        ", 8) != 0) {
605
      if (line_append(db, "        ", 8) != 0) {
608
        goto IOERR;
606
        goto IOERR;
609
      }
607
      }
610
    }
608
    }
611
 
609
 
612
    /* anything left? process the buffer leftover again */
610
    /* anything left? process the buffer leftover again */
613
    if (consumedbytes < len) {
611
    if (consumedbytes < len) {
614
      len -= consumedbytes;
612
      len -= consumedbytes;
615
      buffptr += consumedbytes;
613
      buffptr += consumedbytes;
616
      goto FINDLINE;
614
      goto FINDLINE;
617
    }
615
    }
618
 
616
 
619
  }
617
  }
620
 
618
 
621
  _dos_close(fd);
619
  _dos_close(fd);
622
 
620
 
623
  SKIPLOADING:
621
  SKIPLOADING:
624
 
622
 
625
  /* add an empty line at end if not present already, also rewind cursor to top of file */
623
  /* add an empty line at end if not present already, also rewind cursor to top of file */
626
  if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
624
  if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
627
  db_rewind(db);
625
  db_rewind(db);
628
 
626
 
629
  return(0);
627
  return(0);
630
 
628
 
631
  IOERR:
629
  IOERR:
632
  _dos_close(fd);
630
  _dos_close(fd);
633
  return(2);
631
  return(2);
634
}
632
}
635
 
633
 
636
 
634
 
637
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
635
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
638
 * of argc and argv, saves some 330 bytes of binary size
636
 * of argc and argv, saves some 330 bytes of binary size
639
 * returns non-zero on error */
637
 * returns non-zero on error */
640
static int parseargv(struct file *dbarr) {
638
static int parseargv(struct file *dbarr) {
641
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
639
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
642
  unsigned short count = 0;
640
  unsigned short count = 0;
643
  char *arg;
641
  char *arg;
644
  unsigned short lastarg = 0;
642
  unsigned short lastarg = 0;
645
  int err;
643
  int err;
646
 
644
 
647
  while (!lastarg) {
645
  while (!lastarg) {
648
    /* jump to nearest arg */
646
    /* jump to nearest arg */
649
    while (*tail == ' ') {
647
    while (*tail == ' ') {
650
      *tail = 0;
648
      *tail = 0;
651
      tail++;
649
      tail++;
652
    }
650
    }
653
 
651
 
654
    if (*tail == '\r') {
652
    if (*tail == '\r') {
655
      *tail = 0;
653
      *tail = 0;
656
      break;
654
      break;
657
    }
655
    }
658
 
656
 
659
    arg = tail;
657
    arg = tail;
660
 
658
 
661
    /* jump to next delimiter */
659
    /* jump to next delimiter */
662
    while ((*tail != ' ') && (*tail != '\r')) tail++;
660
    while ((*tail != ' ') && (*tail != '\r')) tail++;
663
 
661
 
664
    /* if \r then remember this is the last arg */
662
    /* if \r then remember this is the last arg */
665
    if (*tail == '\r') lastarg = 1;
663
    if (*tail == '\r') lastarg = 1;
666
 
664
 
667
    *tail = 0;
665
    *tail = 0;
668
    tail++;
666
    tail++;
669
 
667
 
670
    /* look at the arg now */
668
    /* look at the arg now */
671
    if (*arg == '/') {
669
    if (*arg == '/') {
672
      if (arg[1] == 't') { /* /t = do not expand tabs */
670
      if (arg[1] == 't') { /* /t = do not expand tabs */
673
        glob_tablessmode = 1;
671
        glob_tablessmode = 1;
674
 
672
 
675
      } else if (arg[1] == 'm') { /* /m = force mono mode */
673
      } else if (arg[1] == 'm') { /* /m = force mono mode */
676
        glob_monomode = 1;
674
        glob_monomode = 1;
677
 
675
 
678
      } else {  /* help screen */
676
      } else {  /* help screen */
679
        const char far *self = mdr_dos_selfexe();
677
        const char far *self = mdr_dos_selfexe();
680
        unsigned short i;
678
        unsigned short i;
681
        if (self == NULL) self = "sved";
679
        if (self == NULL) self = "sved";
682
        for (i = 0; self[i] != 0; i++) {
680
        for (i = 0; self[i] != 0; i++) {
683
          if (self[i] == '\\') {
681
          if (self[i] == '\\') {
684
            self += i + 1;
682
            self += i + 1;
685
            i = 0;
683
            i = 0;
686
          }
684
          }
687
        }
685
        }
688
        mdr_coutraw_str(svarlang_str(1,3)); /* Sved, the SvarDOS editor */
686
        mdr_coutraw_str(svarlang_str(1,3)); /* Sved, the SvarDOS editor */
689
        mdr_coutraw_str(" [");
687
        mdr_coutraw_str(" [");
690
        mdr_coutraw_str(svarlang_str(1,4)); /* ver */
688
        mdr_coutraw_str(svarlang_str(1,4)); /* ver */
691
        mdr_coutraw_puts(" " PVER "]");
689
        mdr_coutraw_puts(" " PVER "]");
692
        mdr_coutraw_puts("Copyright (C) " PDATE " Mateusz Viste");
690
        mdr_coutraw_puts("Copyright (C) " PDATE " Mateusz Viste");
693
        mdr_coutraw_crlf();
691
        mdr_coutraw_crlf();
694
        mdr_coutraw_str(svarlang_str(1,0)); /* usage: */
692
        mdr_coutraw_str(svarlang_str(1,0)); /* usage: */
695
        mdr_coutraw_char(' ');
693
        mdr_coutraw_char(' ');
696
        while (*self != 0) {
694
        while (*self != 0) {
697
          mdr_coutraw_char(*self);
695
          mdr_coutraw_char(*self);
698
          self++;
696
          self++;
699
        }
697
        }
700
        mdr_coutraw_char(' ');
698
        mdr_coutraw_char(' ');
701
        mdr_coutraw_puts(svarlang_str(1,1)); /* args syntax */
699
        mdr_coutraw_puts(svarlang_str(1,1)); /* args syntax */
702
        mdr_coutraw_crlf();
700
        mdr_coutraw_crlf();
703
        mdr_coutraw_puts(svarlang_str(1,10)); /* /m */
701
        mdr_coutraw_puts(svarlang_str(1,10)); /* /m */
704
        mdr_coutraw_puts(svarlang_str(1,11)); /* /t */
702
        mdr_coutraw_puts(svarlang_str(1,11)); /* /t */
705
        return(-1);
703
        return(-1);
706
      }
704
      }
707
      continue;
705
      continue;
708
    }
706
    }
709
 
707
 
710
    /* looks to be a filename */
708
    /* looks to be a filename */
711
    if (count == 10) {
709
    if (count == 10) {
712
      mdr_coutraw_puts(svarlang_str(0,12)); /* too many files */
710
      mdr_coutraw_puts(svarlang_str(0,12)); /* too many files */
713
      return(-1);
711
      return(-1);
714
    }
712
    }
715
 
713
 
716
    /* try loading it */
714
    /* try loading it */
717
    mdr_coutraw_str(svarlang_str(1,2));
715
    mdr_coutraw_str(svarlang_str(1,2));
718
    mdr_coutraw_char(' ');
716
    mdr_coutraw_char(' ');
719
    mdr_coutraw_puts(arg);
717
    mdr_coutraw_puts(arg);
720
    err = loadfile(&(dbarr[count]), arg);
718
    err = loadfile(&(dbarr[count]), arg);
721
    if (err) {
719
    if (err) {
722
      if (err == 1) { /* file not found */
720
      if (err == 1) { /* file not found */
723
        if ((count == 0) && (lastarg != 0)) { /* a 'file not found' is fine if only one file was given */
721
        if ((count == 0) && (lastarg != 0)) { /* a 'file not found' is fine if only one file was given */
724
          memcpy(dbarr[count].fname, arg, strlen(arg) + 1);
722
          memcpy(dbarr[count].fname, arg, strlen(arg) + 1);
725
          err = 0;
723
          err = 0;
726
        } else {
724
        } else {
727
          err = 11;
725
          err = 11;
728
        }
726
        }
729
      } else { /* general error */
727
      } else { /* general error */
730
        err = 10;
728
        err = 10;
731
      }
729
      }
732
      if (err) {
730
      if (err) {
733
        mdr_coutraw_puts(svarlang_str(0,err));
731
        mdr_coutraw_puts(svarlang_str(0,err));
734
        return(-1);
732
        return(-1);
735
      }
733
      }
736
    }
734
    }
737
    count++;
735
    count++;
738
  }
736
  }
739
 
737
 
740
  return(0);
738
  return(0);
741
}
739
}
742
 
740
 
743
 
741
 
744
static int savefile(const struct file *db, const char *newfname) {
742
static int savefile(const struct file *db, const char *newfname) {
745
  int fd;
743
  int fd;
746
  const struct line far *l;
744
  const struct line far *l;
747
  unsigned int bytes;
745
  unsigned int bytes;
748
  unsigned char eollen;
746
  unsigned char eollen;
749
  unsigned char eolbuf[2];
747
  unsigned char eolbuf[2];
750
  int errflag = 0;
748
  int errflag = 0;
751
 
749
 
752
  /* either create a new file if newfname provided, or... */
750
  /* either create a new file if newfname provided, or... */
753
  if (newfname) {
751
  if (newfname) {
754
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
752
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
755
  } else { /* ...open db->fname */
753
  } else { /* ...open db->fname */
756
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
754
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
757
  }
755
  }
758
 
756
 
759
  l = db->cursor;
757
  l = db->cursor;
760
  while (l->prev) l = l->prev;
758
  while (l->prev) l = l->prev;
761
 
759
 
762
  /* preset line terminators */
760
  /* preset line terminators */
763
  if (db->lfonly) {
761
  if (db->lfonly) {
764
    eolbuf[0] = '\n';
762
    eolbuf[0] = '\n';
765
    eollen = 1;
763
    eollen = 1;
766
  } else {
764
  } else {
767
    eolbuf[0] = '\r';
765
    eolbuf[0] = '\r';
768
    eolbuf[1] = '\n';
766
    eolbuf[1] = '\n';
769
    eollen = 2;
767
    eollen = 2;
770
  }
768
  }
771
 
769
 
772
  while (l) {
770
  while (l) {
773
    /* do not write the last empty line, it is only useful for edition */
771
    /* do not write the last empty line, it is only useful for edition */
774
    if (l->len != 0) {
772
    if (l->len != 0) {
775
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
773
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
776
    } else if (l->next == NULL) {
774
    } else if (l->next == NULL) {
777
      break;
775
      break;
778
    }
776
    }
779
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
777
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
780
    l = l->next;
778
    l = l->next;
781
  }
779
  }
782
 
780
 
783
  errflag |= _dos_close(fd);
781
  errflag |= _dos_close(fd);
784
 
782
 
785
  return(errflag);
783
  return(errflag);
786
}
784
}
787
 
785
 
788
 
786
 
789
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
787
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
790
  if (curline_resize(db, db->cursor->len + len) == 0) {
788
  if (curline_resize(db, db->cursor->len + len) == 0) {
791
    unsigned short off = db->xoffset + db->cursorposx;
789
    unsigned short off = db->xoffset + db->cursorposx;
792
    db->modflag = 1;
790
    db->modflag = 1;
793
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
791
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
794
    db->cursor->len += len;
792
    db->cursor->len += len;
795
    uidirty.from = db->cursorposy;
793
    uidirty.from = db->cursorposy;
796
    uidirty.to = db->cursorposy;
794
    uidirty.to = db->cursorposy;
797
    while (len--) {
795
    while (len--) {
798
      db->cursor->payload[off++] = *databuf;
796
      db->cursor->payload[off++] = *databuf;
799
      databuf++;
797
      databuf++;
800
      cursor_right(db);
798
      cursor_right(db);
801
    }
799
    }
802
  }
800
  }
803
}
801
}
804
 
802
 
805
 
803
 
806
/* recompute db->curline by counting nodes in linked list */
804
/* recompute db->curline by counting nodes in linked list */
807
static void recompute_curline(struct file *db) {
805
static void recompute_curline(struct file *db) {
808
  const struct line far *l = db->cursor;
806
  const struct line far *l = db->cursor;
809
 
807
 
810
  db->curline = 0;
808
  db->curline = 0;
811
  while (l->prev != NULL) {
809
  while (l->prev != NULL) {
812
    db->curline += 1;
810
    db->curline += 1;
813
    l = l->prev;
811
    l = l->prev;
814
  }
812
  }
815
}
813
}
816
 
814
 
817
 
815
 
818
enum MENU_ACTION {
816
enum MENU_ACTION {
819
  MENU_NONE   = 0,
817
  MENU_NONE   = 0,
820
  MENU_OPEN   = 1,
818
  MENU_OPEN   = 1,
821
  MENU_SAVE   = 2,
819
  MENU_SAVE   = 2,
822
  MENU_SAVEAS = 3,
820
  MENU_SAVEAS = 3,
823
  MENU_CLOSE  = 4,
821
  MENU_CLOSE  = 4,
824
  MENU_CHGEOL = 5,
822
  MENU_CHGEOL = 5,
825
  MENU_QUIT   = 6
823
  MENU_QUIT   = 6
826
};
824
};
827
 
825
 
828
static enum MENU_ACTION ui_menu(void) {
826
static enum MENU_ACTION ui_menu(void) {
829
  unsigned short i, curchoice, attr, x, slen;
827
  unsigned short i, curchoice, attr, x, slen;
830
  unsigned short xorigin, yorigin;
828
  unsigned short xorigin, yorigin;
831
 
829
 
832
  /* find out the longest string */
830
  /* find out the longest string */
833
  slen = 0;
831
  slen = 0;
834
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
832
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
835
    x = strlen(svarlang_str(8, i));
833
    x = strlen(svarlang_str(8, i));
836
    if (x > slen) slen = x;
834
    if (x > slen) slen = x;
837
  }
835
  }
838
 
836
 
839
  /* calculate where to draw the menu on screen */
837
  /* calculate where to draw the menu on screen */
840
  xorigin = (screenw - (slen + 5)) / 2;
838
  xorigin = (screenw - (slen + 5)) / 2;
841
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 6)) / 2;
839
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 6)) / 2;
842
 
840
 
843
  /* */
841
  /* */
844
  uidirty.from = yorigin;
842
  uidirty.from = yorigin;
845
  uidirty.to = 0xff;
843
  uidirty.to = 0xff;
846
  uidirty.statusbar = 1;
844
  uidirty.statusbar = 1;
847
 
845
 
848
  /* hide the cursor */
846
  /* hide the cursor */
849
  mdr_cout_cursor_hide();
847
  mdr_cout_cursor_hide();
850
 
848
 
851
  curchoice = MENU_OPEN;
849
  curchoice = MENU_OPEN;
852
  for (;;) {
850
  for (;;) {
853
    /* render menu */
851
    /* render menu */
854
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
852
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
855
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
853
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
856
      if (i == curchoice) {
854
      if (i == curchoice) {
857
        attr = SCHEME_MENU_CUR;
855
        attr = SCHEME_MENU_CUR;
858
        mdr_cout_char_rep(yorigin + i, xorigin + 1, ' ', SCHEME_MENU_SEL, slen + 2);
856
        mdr_cout_char_rep(yorigin + i, xorigin + 1, ' ', SCHEME_MENU_SEL, slen + 2);
859
      } else {
857
      } else {
860
        attr = SCHEME_MENU;
858
        attr = SCHEME_MENU;
861
      }
859
      }
862
      mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
860
      mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
863
    }
861
    }
864
    /* wait for key */
862
    /* wait for key */
865
    switch (mdr_dos_getkey2()) {
863
    switch (mdr_dos_getkey2()) {
866
      case 0x150: /* down */
864
      case 0x150: /* down */
867
        if (curchoice == MENU_QUIT) {
865
        if (curchoice == MENU_QUIT) {
868
          curchoice = MENU_OPEN;
866
          curchoice = MENU_OPEN;
869
        } else {
867
        } else {
870
          curchoice++;
868
          curchoice++;
871
        }
869
        }
872
        break;
870
        break;
873
      case 0x148: /* up */
871
      case 0x148: /* up */
874
        if (curchoice == MENU_OPEN) {
872
        if (curchoice == MENU_OPEN) {
875
          curchoice = MENU_QUIT;
873
          curchoice = MENU_QUIT;
876
        } else {
874
        } else {
877
          curchoice--;
875
          curchoice--;
878
        }
876
        }
879
        break;
877
        break;
880
      default:
878
      default:
881
        curchoice = MENU_NONE;
879
        curchoice = MENU_NONE;
882
        /* FALLTHRU */
880
        /* FALLTHRU */
883
      case '\r': /* ENTER */
881
      case '\r': /* ENTER */
884
        mdr_cout_cursor_show();
882
        mdr_cout_cursor_show();
885
        return(curchoice);
883
        return(curchoice);
886
    }
884
    }
887
  }
885
  }
888
}
886
}
889
 
887
 
890
 
888
 
891
static struct file *select_slot(struct file *dbarr, unsigned char curfile) {
889
static struct file *select_slot(struct file *dbarr, unsigned char curfile) {
892
  uidirty.from = 0;
890
  uidirty.from = 0;
893
  uidirty.to = 0xff;
891
  uidirty.to = 0xff;
894
  uidirty.statusbar = 1;
892
  uidirty.statusbar = 1;
895
 
893
 
896
  dbarr = &(dbarr[curfile]);
894
  dbarr = &(dbarr[curfile]);
897
  /* force redraw now, because the main() routine might not if this is exit
895
  /* force redraw now, because the main() routine might not if this is exit
898
   * time and we want to show the user which file has unsaved changes */
896
   * time and we want to show the user which file has unsaved changes */
899
  ui_basic(dbarr, curfile);
897
  ui_basic(dbarr, curfile);
900
  ui_refresh(dbarr);
898
  ui_refresh(dbarr);
901
  return(dbarr);
899
  return(dbarr);
902
}
900
}
903
 
901
 
904
 
902
 
905
/* main returns nothing, ie. sved always exits with a zero exit code
903
/* main returns nothing, ie. sved always exits with a zero exit code
906
 * (this saves 20 bytes of executable footprint) */
904
 * (this saves 20 bytes of executable footprint) */
907
void main(void) {
905
void main(void) {
908
  static struct file dbarr[10];
906
  static struct file dbarr[10];
909
  unsigned char curfile;
907
  unsigned char curfile;
910
  struct file *db = dbarr; /* visible file is the first slot by default */
908
  struct file *db = dbarr; /* visible file is the first slot by default */
911
  struct line far *clipboard = NULL;
909
  struct line far *clipboard = NULL;
912
  unsigned char original_breakflag;
910
  unsigned char original_breakflag;
913
 
911
 
914
  { /* load NLS resource */
912
  { /* load NLS resource */
915
    unsigned short i = 0;
913
    unsigned short i = 0;
916
    const char far *selfptr;
914
    const char far *selfptr;
917
    char self[128], lang[8];
915
    char self[128], lang[8];
918
    selfptr = mdr_dos_selfexe();
916
    selfptr = mdr_dos_selfexe();
919
    if (selfptr != NULL) {
917
    if (selfptr != NULL) {
920
      do {
918
      do {
921
        self[i] = selfptr[i];
919
        self[i] = selfptr[i];
922
      } while (self[i++] != 0);
920
      } while (self[i++] != 0);
923
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
921
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
924
    }
922
    }
925
  }
923
  }
926
 
924
 
927
  /* preload all slots with empty files */
925
  /* preload all slots with empty files */
928
  for (curfile = 9;; curfile--) {
926
  for (curfile = 9;; curfile--) {
929
    loadfile(&(dbarr[curfile]), NULL);
927
    loadfile(&(dbarr[curfile]), NULL);
930
    if (curfile == 0) break;
928
    if (curfile == 0) break;
931
  }
929
  }
932
 
930
 
933
  /* parse argv (and load files, if any passed on) */
931
  /* parse argv (and load files, if any passed on) */
934
  if (parseargv(dbarr) != 0) return;
932
  if (parseargv(dbarr) != 0) return;
935
 
933
 
936
  if ((mdr_cout_init(&screenw, &screenh) != 0) && (glob_monomode == 0)) {
934
  if ((mdr_cout_init(&screenw, &screenh) != 0) && (glob_monomode == 0)) {
937
    /* load color scheme if mdr_cout_init returns a color flag */
935
    /* load color scheme if mdr_cout_init returns a color flag */
938
    SCHEME_TEXT = 0x17;
936
    SCHEME_TEXT = 0x17;
939
    SCHEME_MENU = 0x70;
937
    SCHEME_MENU = 0x70;
940
    SCHEME_MENU_CUR = 0x6f;
938
    SCHEME_MENU_CUR = 0x6f;
941
    SCHEME_MENU_SEL = 0x66;
939
    SCHEME_MENU_SEL = 0x66;
942
    SCHEME_STBAR1 = 0x70;
940
    SCHEME_STBAR1 = 0x70;
943
    SCHEME_STBAR2 = 0x78;
941
    SCHEME_STBAR2 = 0x78;
944
    SCHEME_STBAR3 = 0x3f;
942
    SCHEME_STBAR3 = 0x3f;
945
    SCHEME_SCROLL = 0x70;
943
    SCHEME_SCROLL = 0x70;
946
    SCHEME_MSG = 0x6f;
944
    SCHEME_MSG = 0x6f;
947
    SCHEME_ERR = 0x4f;
945
    SCHEME_ERR = 0x4f;
948
  }
946
  }
949
  screenlastrow = screenh - 1;
947
  screenlastrow = screenh - 1;
950
  screenlastcol = screenw - 1;
948
  screenlastcol = screenw - 1;
951
 
949
 
952
  /* instruct DOS to stop detecting CTRL+C because user needs it for
950
  /* instruct DOS to stop detecting CTRL+C because user needs it for
953
   * copy/paste operations. also remember the original status of the BREAK
951
   * copy/paste operations. also remember the original status of the BREAK
954
   * flag so I can restore it as it was before quitting. */
952
   * flag so I can restore it as it was before quitting. */
955
  original_breakflag = mdr_dos_ctrlc_disable();
953
  original_breakflag = mdr_dos_ctrlc_disable();
956
 
954
 
957
  for (;;) {
955
  for (;;) {
958
    int k;
956
    int k;
959
 
957
 
960
    /* add an extra empty line if cursor is on last line and this line is not empty */
958
    /* add an extra empty line if cursor is on last line and this line is not empty */
961
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
959
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
962
      if (line_add(db, NULL, 0) == 0) {
960
      if (line_add(db, NULL, 0) == 0) {
963
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
961
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
964
      }
962
      }
965
    }
963
    }
966
 
964
 
967
    check_cursor_not_after_eol(db);
965
    check_cursor_not_after_eol(db);
968
    mdr_cout_locate(db->cursorposy, db->cursorposx);
966
    mdr_cout_locate(db->cursorposy, db->cursorposx);
969
 
967
 
970
    ui_refresh(db);
968
    ui_refresh(db);
971
 
969
 
972
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag)) {
970
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag)) {
973
      ui_basic(db, curfile);
971
      ui_basic(db, curfile);
974
      uidirty.statusbar = 0;
972
      uidirty.statusbar = 0;
975
      db->modflagprev = db->modflag;
973
      db->modflagprev = db->modflag;
976
    }
974
    }
977
#ifdef DBG_LINENUM
975
#ifdef DBG_LINENUM
978
      {
976
      {
979
        char ddd[10];
977
        char ddd[10];
980
        db->curline += 1;
978
        db->curline += 1;
981
        ddd[0] = '0' + db->curline / 100;
979
        ddd[0] = '0' + db->curline / 100;
982
        ddd[1] = '0' + (db->curline % 100) / 10;
980
        ddd[1] = '0' + (db->curline % 100) / 10;
983
        ddd[2] = '0' + (db->curline % 10);
981
        ddd[2] = '0' + (db->curline % 10);
984
        db->curline -= 1;
982
        db->curline -= 1;
985
        ddd[3] = '/';
983
        ddd[3] = '/';
986
        ddd[4] = '0' + db->totlines / 100;
984
        ddd[4] = '0' + db->totlines / 100;
987
        ddd[5] = '0' + (db->totlines % 100) / 10;
985
        ddd[5] = '0' + (db->totlines % 100) / 10;
988
        ddd[6] = '0' + (db->totlines % 10);
986
        ddd[6] = '0' + (db->totlines % 10);
989
        ddd[7] = 0;
987
        ddd[7] = 0;
990
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
988
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
991
      }
989
      }
992
#endif
990
#endif
993
 
991
 
994
    k = mdr_dos_getkey2();
992
    k = mdr_dos_getkey2();
995
 
993
 
996
    if (k == 0x150) { /* down */
994
    if (k == 0x150) { /* down */
997
      cursor_down(db);
995
      cursor_down(db);
998
 
996
 
999
    } else if (k == 0x148) { /* up */
997
    } else if (k == 0x148) { /* up */
1000
      cursor_up(db);
998
      cursor_up(db);
1001
 
999
 
1002
    } else if (k == 0x14D) { /* right */
1000
    } else if (k == 0x14D) { /* right */
1003
      cursor_right(db);
1001
      cursor_right(db);
1004
 
1002
 
1005
    } else if (k == 0x14B) { /* left */
1003
    } else if (k == 0x14B) { /* left */
1006
      cursor_left(db);
1004
      cursor_left(db);
1007
 
1005
 
1008
    } else if (k == 0x149) { /* pgup */
1006
    } else if (k == 0x149) { /* pgup */
1009
      unsigned char dist = db->cursorposy + screenh - 1;
1007
      unsigned char dist = db->cursorposy + screenh - 1;
1010
      while ((dist != 0) && (db->cursor->prev != NULL)) {
1008
      while ((dist != 0) && (db->cursor->prev != NULL)) {
1011
        db->cursor = db->cursor->prev;
1009
        db->cursor = db->cursor->prev;
1012
        dist--;
1010
        dist--;
1013
      }
1011
      }
1014
      if (dist != 0) {
1012
      if (dist != 0) {
1015
        db->cursorposy = 0;
1013
        db->cursorposy = 0;
1016
        db->cursorposx = 0;
1014
        db->cursorposx = 0;
1017
      } else {
1015
      } else {
1018
        dist = db->cursorposy;
1016
        dist = db->cursorposy;
1019
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
1017
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
1020
      }
1018
      }
1021
      uidirty.from = 0;
1019
      uidirty.from = 0;
1022
      uidirty.to = 0xff;
1020
      uidirty.to = 0xff;
1023
      recompute_curline(db);
1021
      recompute_curline(db);
1024
 
1022
 
1025
    } else if (k == 0x151) { /* pgdown */
1023
    } else if (k == 0x151) { /* pgdown */
1026
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
1024
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
1027
      while ((dist != 0) && (db->cursor->next != NULL)) {
1025
      while ((dist != 0) && (db->cursor->next != NULL)) {
1028
        db->cursor = db->cursor->next;
1026
        db->cursor = db->cursor->next;
1029
        dist--;
1027
        dist--;
1030
      }
1028
      }
1031
      if (dist != 0) {
1029
      if (dist != 0) {
1032
        db->cursorposy = screenh - 2;
1030
        db->cursorposy = screenh - 2;
1033
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
1031
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
1034
        db->cursorposx = 0;
1032
        db->cursorposx = 0;
1035
      } else {
1033
      } else {
1036
        dist = screenh - 2 - db->cursorposy;
1034
        dist = screenh - 2 - db->cursorposy;
1037
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
1035
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
1038
      }
1036
      }
1039
      uidirty.from = 0;
1037
      uidirty.from = 0;
1040
      uidirty.to = 0xff;
1038
      uidirty.to = 0xff;
1041
      recompute_curline(db);
1039
      recompute_curline(db);
1042
 
1040
 
1043
    } else if (k == 0x147) { /* home */
1041
    } else if (k == 0x147) { /* home */
1044
       cursor_home(db);
1042
       cursor_home(db);
1045
 
1043
 
1046
    } else if (k == 0x14F) { /* end */
1044
    } else if (k == 0x14F) { /* end */
1047
       cursor_eol(db);
1045
       cursor_eol(db);
1048
 
1046
 
1049
    } else if (k == 0x1B) { /* ESC */
1047
    } else if (k == 0x1B) { /* ESC */
1050
      int quitnow = 0;
1048
      int quitnow = 0;
1051
      char fname[64];
1049
      char fname[64];
1052
      int saveflag = 0;
1050
      int saveflag = 0;
1053
      enum MENU_ACTION ui_action;
1051
      enum MENU_ACTION ui_action;
1054
 
1052
 
1055
      /* collect the exact menu action and clear the screen */
1053
      /* collect the exact menu action and clear the screen */
1056
      ui_action = ui_menu();
1054
      ui_action = ui_menu();
1057
      ui_refresh(db);
1055
      ui_refresh(db);
1058
 
1056
 
1059
      switch (ui_action) {
1057
      switch (ui_action) {
1060
 
1058
 
1061
        case MENU_NONE:
1059
        case MENU_NONE:
1062
          break;
1060
          break;
1063
 
1061
 
1064
        case MENU_OPEN:
1062
        case MENU_OPEN:
1065
          /* display a warning if unsaved changes are pending */
1063
          /* display a warning if unsaved changes are pending */
1066
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
1064
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
1067
 
1065
 
1068
          /* ask for filename */
1066
          /* ask for filename */
1069
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1067
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1070
          if (fname[0] != 0) {
1068
          if (fname[0] != 0) {
1071
            int err;
1069
            int err;
1072
            err = loadfile(db, fname);
1070
            err = loadfile(db, fname);
1073
            if (err != 0) {
1071
            if (err != 0) {
1074
              if (err == 1) {
1072
              if (err == 1) {
1075
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1073
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1076
              } else {
1074
              } else {
1077
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1075
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1078
              }
1076
              }
1079
              mdr_bios_tickswait(44); /* 3s */
1077
              mdr_bios_tickswait(44); /* 3s */
1080
              loadfile(db, NULL);
1078
              loadfile(db, NULL);
1081
            }
1079
            }
1082
          }
1080
          }
1083
          uidirty.from = 0;
1081
          uidirty.from = 0;
1084
          uidirty.to = 0xff;
1082
          uidirty.to = 0xff;
1085
          uidirty.statusbar = 1;
1083
          uidirty.statusbar = 1;
1086
          break;
1084
          break;
1087
 
1085
 
1088
        case MENU_SAVEAS:
1086
        case MENU_SAVEAS:
1089
          saveflag = 1;
1087
          saveflag = 1;
1090
          /* FALLTHRU */
1088
          /* FALLTHRU */
1091
        case MENU_SAVE:
1089
        case MENU_SAVE:
1092
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1090
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1093
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1091
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1094
            if (*fname == 0) break;
1092
            if (*fname == 0) break;
1095
            saveflag = savefile(db, fname);
1093
            saveflag = savefile(db, fname);
1096
            if (saveflag == 0) memcpy(db->fname, fname, sizeof(fname));
1094
            if (saveflag == 0) memcpy(db->fname, fname, sizeof(fname));
1097
          } else {
1095
          } else {
1098
            saveflag = savefile(db, NULL);
1096
            saveflag = savefile(db, NULL);
1099
          }
1097
          }
1100
 
1098
 
1101
          mdr_cout_cursor_hide();
1099
          mdr_cout_cursor_hide();
1102
 
1100
 
1103
          if (saveflag == 0) {
1101
          if (saveflag == 0) {
1104
            db->modflag = 0;
1102
            db->modflag = 0;
1105
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1103
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1106
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1104
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1107
          } else {
1105
          } else {
1108
            ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
1106
            ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
1109
            mdr_bios_tickswait(36); /* 2s */
1107
            mdr_bios_tickswait(36); /* 2s */
1110
          }
1108
          }
1111
          mdr_cout_cursor_show();
1109
          mdr_cout_cursor_show();
1112
          break;
1110
          break;
1113
 
1111
 
1114
        case MENU_CLOSE:
1112
        case MENU_CLOSE:
1115
          if (ui_confirm_if_unsaved(db) == 0) {
1113
          if (ui_confirm_if_unsaved(db) == 0) {
1116
            loadfile(db, NULL);
1114
            loadfile(db, NULL);
1117
          }
1115
          }
1118
          uidirty.from = 0;
1116
          uidirty.from = 0;
1119
          uidirty.to = 0xff;
1117
          uidirty.to = 0xff;
1120
          uidirty.statusbar = 1;
1118
          uidirty.statusbar = 1;
1121
          break;
1119
          break;
1122
 
1120
 
1123
        case MENU_CHGEOL:
1121
        case MENU_CHGEOL:
1124
          db->modflag = 1;
1122
          db->modflag = 1;
1125
          db->lfonly ^= 1;
1123
          db->lfonly ^= 1;
1126
          break;
1124
          break;
1127
 
1125
 
1128
        case MENU_QUIT:
1126
        case MENU_QUIT:
1129
          quitnow = 1;
1127
          quitnow = 1;
1130
          for (curfile = 0; curfile < 10; curfile++) {
1128
          for (curfile = 0; curfile < 10; curfile++) {
1131
            if (dbarr[curfile].modflag) {
1129
            if (dbarr[curfile].modflag) {
1132
              db = select_slot(dbarr, curfile);
1130
              db = select_slot(dbarr, curfile);
1133
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
1131
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
1134
            }
1132
            }
1135
          }
1133
          }
1136
          break;
1134
          break;
1137
      }
1135
      }
1138
 
1136
 
1139
      if (quitnow) break;
1137
      if (quitnow) break;
1140
 
1138
 
1141
    } else if (k == 0x0D) { /* ENTER */
1139
    } else if (k == 0x0D) { /* ENTER */
1142
      unsigned short off = db->xoffset + db->cursorposx;
1140
      unsigned short off = db->xoffset + db->cursorposx;
1143
      /* add a new line */
1141
      /* add a new line */
1144
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1142
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1145
        db->modflag = 1;
1143
        db->modflag = 1;
1146
        db->cursor = db->cursor->prev; /* back to original line */
1144
        db->cursor = db->cursor->prev; /* back to original line */
1147
        db->curline -= 1;
1145
        db->curline -= 1;
1148
        /* trim the line above */
1146
        /* trim the line above */
1149
        db->cursor->len = off;
1147
        db->cursor->len = off;
1150
        /* move cursor to the (new) line below */
1148
        /* move cursor to the (new) line below */
1151
        uidirty.from = db->cursorposy;
1149
        uidirty.from = db->cursorposy;
1152
        uidirty.to = 0xff;
1150
        uidirty.to = 0xff;
1153
        cursor_down(db);
1151
        cursor_down(db);
1154
        cursor_home(db);
1152
        cursor_home(db);
1155
      } else {
1153
      } else {
1156
        /* ERROR: OUT OF MEMORY */
1154
        /* ERROR: OUT OF MEMORY */
1157
      }
1155
      }
1158
 
1156
 
1159
    } else if (k == 0x153) {  /* DEL */
1157
    } else if (k == 0x153) {  /* DEL */
1160
      del(db);
1158
      del(db);
1161
 
1159
 
1162
    } else if (k == 0x008) { /* BKSPC */
1160
    } else if (k == 0x008) { /* BKSPC */
1163
      bkspc(db);
1161
      bkspc(db);
1164
 
1162
 
1165
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1163
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1166
      char c = k;
1164
      char c = k;
1167
      insert_in_line(db, &c, 1);
1165
      insert_in_line(db, &c, 1);
1168
 
1166
 
1169
    } else if (k == 0x009) { /* TAB */
1167
    } else if (k == 0x009) { /* TAB */
1170
      if (glob_tablessmode == 0) {
1168
      if (glob_tablessmode == 0) {
1171
        insert_in_line(db, "        ", 8);
1169
        insert_in_line(db, "        ", 8);
1172
      } else {
1170
      } else {
1173
        insert_in_line(db, "\t", 1);
1171
        insert_in_line(db, "\t", 1);
1174
      }
1172
      }
1175
 
1173
 
1176
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1174
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1177
      curfile = k - 0x13b;
1175
      curfile = k - 0x13b;
1178
      db = select_slot(dbarr, curfile);
1176
      db = select_slot(dbarr, curfile);
1179
 
1177
 
1180
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1178
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1181
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1179
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1182
      for (;;) {
1180
      for (;;) {
1183
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1181
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1184
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1182
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1185
        cursor_right(db);
1183
        cursor_right(db);
1186
      }
1184
      }
1187
      /* now skip to next non-space or end of file */
1185
      /* now skip to next non-space or end of file */
1188
      for (;;) {
1186
      for (;;) {
1189
        cursor_right(db);
1187
        cursor_right(db);
1190
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1188
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1191
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1189
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1192
      }
1190
      }
1193
 
1191
 
1194
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1192
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1195
      cursor_left(db);
1193
      cursor_left(db);
1196
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1194
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1197
      for (;;) {
1195
      for (;;) {
1198
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1196
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1199
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1197
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1200
        cursor_left(db);
1198
        cursor_left(db);
1201
      }
1199
      }
1202
      /* now skip to next space or start of file */
1200
      /* now skip to next space or start of file */
1203
      for (;;) {
1201
      for (;;) {
1204
        cursor_left(db);
1202
        cursor_left(db);
1205
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1203
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1206
          cursor_right(db);
1204
          cursor_right(db);
1207
          break;
1205
          break;
1208
        }
1206
        }
1209
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1207
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1210
      }
1208
      }
1211
 
1209
 
1212
    } else if ((k == 0x003) || (k == 0x018)) { /* CTRL+C or CTRL+X */
1210
    } else if ((k == 0x003) || (k == 0x018)) { /* CTRL+C or CTRL+X */
1213
      /* free clipboard if anything in it */
1211
      /* free clipboard if anything in it */
1214
      if (clipboard != NULL) line_free(clipboard);
1212
      if (clipboard != NULL) line_free(clipboard);
1215
 
1213
 
1216
      /* copy cursor line to clipboard */
1214
      /* copy cursor line to clipboard */
1217
      clipboard = line_calloc(db->cursor->len);
1215
      clipboard = line_calloc(db->cursor->len);
1218
      if (clipboard == NULL) {
1216
      if (clipboard == NULL) {
1219
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1217
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1220
        mdr_bios_tickswait(18); /* 1s */
1218
        mdr_bios_tickswait(18); /* 1s */
1221
      } else {
1219
      } else {
1222
        mdr_cout_char_rep(db->cursorposy, 0, ' ', ((SCHEME_TEXT >> 4) | (SCHEME_TEXT << 4)) & 0xff, screenlastcol);
1220
        mdr_cout_char_rep(db->cursorposy, 0, ' ', ((SCHEME_TEXT >> 4) | (SCHEME_TEXT << 4)) & 0xff, screenlastcol);
1223
        uidirty.from = db->cursorposy;
1221
        uidirty.from = db->cursorposy;
1224
        uidirty.to = db->cursorposy;
1222
        uidirty.to = db->cursorposy;
1225
        if (db->cursor->len != 0) {
1223
        if (db->cursor->len != 0) {
1226
          _fmemmove(clipboard->payload, db->cursor->payload, db->cursor->len);
1224
          _fmemmove(clipboard->payload, db->cursor->payload, db->cursor->len);
1227
          clipboard->len = db->cursor->len;
1225
          clipboard->len = db->cursor->len;
1228
        }
1226
        }
1229
        mdr_bios_tickswait(2); /* ca 100ms */
1227
        mdr_bios_tickswait(2); /* ca 100ms */
1230
 
1228
 
1231
        /* if this is about cutting the line (CTRL+X) then delete cur line */
1229
        /* if this is about cutting the line (CTRL+X) then delete cur line */
1232
        if ((k == 0x018) && ((db->cursor->next != NULL) || (db->cursor->prev != NULL))) {
1230
        if ((k == 0x018) && ((db->cursor->next != NULL) || (db->cursor->prev != NULL))) {
1233
          if (db->cursor->next) db->cursor->next->prev = db->cursor->prev;
1231
          if (db->cursor->next) db->cursor->next->prev = db->cursor->prev;
1234
          if (db->cursor->prev) db->cursor->prev->next = db->cursor->next;
1232
          if (db->cursor->prev) db->cursor->prev->next = db->cursor->next;
1235
          clipboard->prev = db->cursor;
1233
          clipboard->prev = db->cursor;
1236
          if (db->cursor->next) {
1234
          if (db->cursor->next) {
1237
            db->cursor = db->cursor->next;
1235
            db->cursor = db->cursor->next;
1238
          } else {
1236
          } else {
1239
            cursor_up(db);
1237
            cursor_up(db);
1240
          }
1238
          }
1241
          line_free(clipboard->prev);
1239
          line_free(clipboard->prev);
1242
          db->totlines -= 1;
1240
          db->totlines -= 1;
1243
          uidirty.from = 0;
1241
          uidirty.from = 0;
1244
          uidirty.to = 0xff;
1242
          uidirty.to = 0xff;
1245
          recompute_curline(db);
1243
          recompute_curline(db);
1246
        }
1244
        }
1247
      }
1245
      }
1248
 
1246
 
1249
    } else if ((k == 0x016) && (clipboard != NULL)) { /* CTRL+V */
1247
    } else if ((k == 0x016) && (clipboard != NULL)) { /* CTRL+V */
1250
      if (line_add(db, clipboard->payload, clipboard->len) != 0) {
1248
      if (line_add(db, clipboard->payload, clipboard->len) != 0) {
1251
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1249
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1252
        mdr_bios_tickswait(18); /* 1s */
1250
        mdr_bios_tickswait(18); /* 1s */
1253
      } else {
1251
      } else {
1254
        /* rewire the linked list so the new line is on top of the previous one */
1252
        /* rewire the linked list so the new line is on top of the previous one */
1255
        clipboard->prev = db->cursor->prev;
1253
        clipboard->prev = db->cursor->prev;
1256
        /* remove prev node from list */
1254
        /* remove prev node from list */
1257
        db->cursor->prev = db->cursor->prev->prev;
1255
        db->cursor->prev = db->cursor->prev->prev;
1258
        if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor;
1256
        if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor;
1259
        /* insert the node after cursor now */
1257
        /* insert the node after cursor now */
1260
        clipboard->prev->next = db->cursor->next;
1258
        clipboard->prev->next = db->cursor->next;
1261
        if (db->cursor->next != NULL) db->cursor->next->prev = clipboard->prev;
1259
        if (db->cursor->next != NULL) db->cursor->next->prev = clipboard->prev;
1262
        clipboard->prev->prev = db->cursor;
1260
        clipboard->prev->prev = db->cursor;
1263
        db->cursor->next = clipboard->prev;
1261
        db->cursor->next = clipboard->prev;
1264
        cursor_down(db);
1262
        cursor_down(db);
1265
      }
1263
      }
1266
      uidirty.from = 0;
1264
      uidirty.from = 0;
1267
      uidirty.to = 0xff;
1265
      uidirty.to = 0xff;
1268
      recompute_curline(db);
1266
      recompute_curline(db);
1269
 
1267
 
1270
#ifdef DBG_UNHKEYS
1268
#ifdef DBG_UNHKEYS
1271
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1269
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1272
      char buff[4];
1270
      char buff[4];
1273
      const char *HEX = "0123456789ABCDEF";
1271
      const char *HEX = "0123456789ABCDEF";
1274
      buff[0] = HEX[(k >> 8) & 15];
1272
      buff[0] = HEX[(k >> 8) & 15];
1275
      buff[1] = HEX[(k >> 4) & 15];
1273
      buff[1] = HEX[(k >> 4) & 15];
1276
      buff[2] = HEX[k & 15];
1274
      buff[2] = HEX[k & 15];
1277
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1275
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1278
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1276
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1279
      mdr_dos_getkey2();
1277
      mdr_dos_getkey2();
1280
      break;
1278
      break;
1281
#endif
1279
#endif
1282
    }
1280
    }
1283
  }
1281
  }
1284
 
1282
 
1285
  mdr_cout_close();
1283
  mdr_cout_close();
1286
 
1284
 
1287
  /* restore the DOS BREAK flag if it was originally set */
1285
  /* restore the DOS BREAK flag if it was originally set */
1288
  if (original_breakflag != 0) mdr_dos_ctrlc_enable();
1286
  if (original_breakflag != 0) mdr_dos_ctrlc_enable();
1289
 
1287
 
1290
  /* no need to free memory, DOS will do it for me */
1288
  /* no need to free memory, DOS will do it for me */
1291
 
1289
 
1292
  return;
1290
  return;
1293
}
1291
}
1294
 
1292