Subversion Repositories SvarDOS

Rev

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

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