Subversion Repositories SvarDOS

Rev

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

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