Subversion Repositories SvarDOS

Rev

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

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