Subversion Repositories SvarDOS

Rev

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

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