Subversion Repositories SvarDOS

Rev

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

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