Subversion Repositories SvarDOS

Rev

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

Rev 1438 Rev 1439
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 numbers */
262
  /* line numbers */
263
  {
263
  {
264
    unsigned short x;
264
    unsigned short x;
265
    unsigned char count = 0;
265
    unsigned char count = 0;
266
    unsigned short coloffset = 9;
266
    unsigned short col = helpcol - 9;
267
 
267
 
268
    x = 1 + db->totlines;
268
    x = db->totlines;
269
    AGAIN:
269
    AGAIN:
270
    do {
270
    do {
271
      mdr_cout_char(screenlastrow, helpcol - coloffset, '0' + (x % 10), SCHEME_STBAR1);
271
      mdr_cout_char(screenlastrow, col--, '0' + (x % 10), SCHEME_STBAR1);
272
      x /= 10;
272
      x /= 10;
273
      coloffset++;
-
 
274
    } while (x);
273
    } while (x);
275
    /* redo same exercise, but printing the current line now */
274
    /* redo same exercise, but printing the current line now */
276
    if (count == 0) {
275
    if (count == 0) {
277
      count = 1;
276
      count = 1;
278
      mdr_cout_char(screenlastrow, helpcol - coloffset, '/', SCHEME_STBAR1);
277
      mdr_cout_char(screenlastrow, col--, '/', SCHEME_STBAR1);
279
      coloffset++;
-
 
280
      x = 1 + db->curline;
278
      x = 1 + db->curline;
281
      goto AGAIN;
279
      goto AGAIN;
282
    }
280
    }
283
 
281
 
284
  }
282
  }
285
 
283
 
286
  mdr_cout_str(screenlastrow, helpcol, s, SCHEME_STBAR2, 40);
284
  mdr_cout_str(screenlastrow, helpcol, s, SCHEME_STBAR2, 40);
287
}
285
}
288
 
286
 
289
 
287
 
290
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
288
static void ui_msg(const char *msg1, const char *msg2, unsigned char attr) {
291
  unsigned short x, y, msglen, i;
289
  unsigned short x, y, msglen, i;
292
  unsigned short msg2flag = 0;
290
  unsigned short msg2flag = 0;
293
 
291
 
294
  msglen = strlen(msg1);
292
  msglen = strlen(msg1);
295
  if (msg2) {
293
  if (msg2) {
296
    msg2flag = 1;
294
    msg2flag = 1;
297
    i = strlen(msg2);
295
    i = strlen(msg2);
298
    if (i > msglen) msglen = i;
296
    if (i > msglen) msglen = i;
299
  }
297
  }
300
 
298
 
301
  y = (screenh - 6) >> 1;
299
  y = (screenh - 6) >> 1;
302
  x = (screenw - msglen - 4) >> 1;
300
  x = (screenw - msglen - 4) >> 1;
303
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
301
  for (i = y+2+msg2flag; i >= y; i--) mdr_cout_char_rep(i, x, ' ', attr, msglen + 2);
304
  x++;
302
  x++;
305
 
303
 
306
  mdr_cout_str(y+1, x, msg1, attr, msglen);
304
  mdr_cout_str(y+1, x, msg1, attr, msglen);
307
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
305
  if (msg2) mdr_cout_str(y+2, x, msg2, attr, msglen);
308
 
306
 
309
  if (uidirty.from > y) uidirty.from = y;
307
  if (uidirty.from > y) uidirty.from = y;
310
  if (uidirty.to < y+4) uidirty.to = y+4;
308
  if (uidirty.to < y+4) uidirty.to = y+4;
311
}
309
}
312
 
310
 
313
 
311
 
314
static unsigned char ui_confirm_if_unsaved(const struct file *db) {
312
static unsigned char ui_confirm_if_unsaved(const struct file *db) {
315
  unsigned char r = 0;
313
  unsigned char r = 0;
316
  if (db->modflag == 0) return(0);
314
  if (db->modflag == 0) return(0);
317
 
315
 
318
  mdr_cout_cursor_hide();
316
  mdr_cout_cursor_hide();
319
 
317
 
320
  /* if file has been modified then ask for confirmation */
318
  /* if file has been modified then ask for confirmation */
321
  ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
319
  ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
322
  if (mdr_dos_getkey2() != '\r') r = 1;
320
  if (mdr_dos_getkey2() != '\r') r = 1;
323
 
321
 
324
  mdr_cout_cursor_show();
322
  mdr_cout_cursor_show();
325
 
323
 
326
  return(r);
324
  return(r);
327
}
325
}
328
 
326
 
329
 
327
 
330
static void ui_refresh(const struct file *db) {
328
static void ui_refresh(const struct file *db) {
331
  unsigned char x;
329
  unsigned char x;
332
  const struct line far *l;
330
  const struct line far *l;
333
  unsigned char y = db->cursorposy;
331
  unsigned char y = db->cursorposy;
334
 
332
 
335
  /* quit early if nothing to refresh */
333
  /* quit early if nothing to refresh */
336
  if (uidirty.from == 0xff) return;
334
  if (uidirty.from == 0xff) return;
337
 
335
 
338
#ifdef DBG_REFRESH
336
#ifdef DBG_REFRESH
339
  static char m = 'a';
337
  static char m = 'a';
340
  m++;
338
  m++;
341
  if (m > 'z') m = 'a';
339
  if (m > 'z') m = 'a';
342
#endif
340
#endif
343
 
341
 
344
  /* rewind cursor line to first line that needs redrawing */
342
  /* rewind cursor line to first line that needs redrawing */
345
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
343
  for (l = db->cursor; y > uidirty.from; y--) l = l->prev;
346
 
344
 
347
  /* iterate over lines and redraw whatever needs to be redrawn */
345
  /* iterate over lines and redraw whatever needs to be redrawn */
348
  for (; l != NULL; l = l->next, y++) {
346
  for (; l != NULL; l = l->next, y++) {
349
 
347
 
350
    /* skip lines that do not need to be refreshed */
348
    /* skip lines that do not need to be refreshed */
351
    if (y < uidirty.from) continue;
349
    if (y < uidirty.from) continue;
352
    if (y > uidirty.to) break;
350
    if (y > uidirty.to) break;
353
 
351
 
354
    x = 0;
352
    x = 0;
355
    if (db->xoffset < l->len) {
353
    if (db->xoffset < l->len) {
356
      unsigned char i, limit;
354
      unsigned char i, limit;
357
      if (l->len - db->xoffset < screenw) {
355
      if (l->len - db->xoffset < screenw) {
358
        limit = l->len;
356
        limit = l->len;
359
      } else {
357
      } else {
360
        limit = db->xoffset + screenlastcol;
358
        limit = db->xoffset + screenlastcol;
361
      }
359
      }
362
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
360
      for (i = db->xoffset; i < limit; i++) mdr_cout_char(y, x++, l->payload[i], SCHEME_TEXT);
363
    }
361
    }
364
 
362
 
365
    /* write empty spaces until end of line */
363
    /* write empty spaces until end of line */
366
    if (x < screenlastcol) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenlastcol - x);
364
    if (x < screenlastcol) mdr_cout_char_rep(y, x, ' ', SCHEME_TEXT, screenlastcol - x);
367
 
365
 
368
#ifdef DBG_REFRESH
366
#ifdef DBG_REFRESH
369
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
367
    mdr_cout_char(y, 0, m, SCHEME_STBAR1);
370
#endif
368
#endif
371
 
369
 
372
    if (y == screenh - 2) break;
370
    if (y == screenh - 2) break;
373
  }
371
  }
374
 
372
 
375
  /* fill all lines below if empty (and they need to be redrawn) */
373
  /* fill all lines below if empty (and they need to be redrawn) */
376
  if (l == NULL) {
374
  if (l == NULL) {
377
    while ((y < screenlastrow) && (y < uidirty.to)) {
375
    while ((y < screenlastrow) && (y < uidirty.to)) {
378
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenlastcol);
376
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenlastcol);
379
    }
377
    }
380
  }
378
  }
381
 
379
 
382
  /* scroll bar */
380
  /* scroll bar */
383
  for (y = 0; y < screenlastrow; y++) {
381
  for (y = 0; y < screenlastrow; y++) {
384
    mdr_cout_char(y, screenlastcol, SCROLL_CURSOR, SCHEME_SCROLL);
382
    mdr_cout_char(y, screenlastcol, SCROLL_CURSOR, SCHEME_SCROLL);
385
  }
383
  }
386
 
384
 
387
  /* scroll cursor */
385
  /* scroll cursor */
388
  if (db->totlines >= screenh) {
386
  if (db->totlines >= screenh) {
389
    unsigned short topline = db->curline - db->cursorposy;
387
    unsigned short topline = db->curline - db->cursorposy;
390
    unsigned short col;
388
    unsigned short col;
391
    unsigned short totlines = db->totlines - screenh + 1;
389
    unsigned short totlines = db->totlines - screenh + 1;
392
    if (db->totlines - screenh > screenh) {
390
    if (db->totlines - screenh > screenh) {
393
      col = topline / (totlines / screenlastrow);
391
      col = topline / (totlines / screenlastrow);
394
    } else {
392
    } else {
395
      col = topline * screenlastrow / totlines;
393
      col = topline * screenlastrow / totlines;
396
    }
394
    }
397
    if (col >= screenlastrow) col = screenh - 2;
395
    if (col >= screenlastrow) col = screenh - 2;
398
    mdr_cout_char(col, screenlastcol, ' ', SCHEME_SCROLL);
396
    mdr_cout_char(col, screenlastcol, ' ', SCHEME_SCROLL);
399
  }
397
  }
400
 
398
 
401
  /* clear out the dirty flag */
399
  /* clear out the dirty flag */
402
  uidirty.from = 0xff;
400
  uidirty.from = 0xff;
403
}
401
}
404
 
402
 
405
 
403
 
406
static void check_cursor_not_after_eol(struct file *db) {
404
static void check_cursor_not_after_eol(struct file *db) {
407
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
405
  if (db->xoffset + db->cursorposx <= db->cursor->len) return;
408
 
406
 
409
  if (db->cursor->len < db->xoffset) {
407
  if (db->cursor->len < db->xoffset) {
410
    db->cursorposx = 0;
408
    db->cursorposx = 0;
411
    db->xoffset = db->cursor->len;
409
    db->xoffset = db->cursor->len;
412
    uidirty.from = 0;
410
    uidirty.from = 0;
413
    uidirty.to = 0xff;
411
    uidirty.to = 0xff;
414
  } else {
412
  } else {
415
    db->cursorposx = db->cursor->len - db->xoffset;
413
    db->cursorposx = db->cursor->len - db->xoffset;
416
  }
414
  }
417
}
415
}
418
 
416
 
419
 
417
 
420
static void cursor_up(struct file *db) {
418
static void cursor_up(struct file *db) {
421
  if (db->cursor->prev == NULL) return;
419
  if (db->cursor->prev == NULL) return;
422
 
420
 
423
  db->curline -= 1;
421
  db->curline -= 1;
424
  db->cursor = db->cursor->prev;
422
  db->cursor = db->cursor->prev;
425
  if (db->cursorposy == 0) {
423
  if (db->cursorposy == 0) {
426
    uidirty.from = 0;
424
    uidirty.from = 0;
427
    uidirty.to = 0xff;
425
    uidirty.to = 0xff;
428
  } else {
426
  } else {
429
    db->cursorposy -= 1;
427
    db->cursorposy -= 1;
430
  }
428
  }
431
}
429
}
432
 
430
 
433
 
431
 
434
static void cursor_eol(struct file *db) {
432
static void cursor_eol(struct file *db) {
435
  /* adjust xoffset to make sure eol is visible on screen */
433
  /* adjust xoffset to make sure eol is visible on screen */
436
  if (db->xoffset > db->cursor->len) {
434
  if (db->xoffset > db->cursor->len) {
437
    db->xoffset = db->cursor->len - 1;
435
    db->xoffset = db->cursor->len - 1;
438
    uidirty.from = 0;
436
    uidirty.from = 0;
439
    uidirty.to = 0xff;
437
    uidirty.to = 0xff;
440
  }
438
  }
441
 
439
 
442
  if (db->xoffset + screenlastcol <= db->cursor->len) {
440
  if (db->xoffset + screenlastcol <= db->cursor->len) {
443
    db->xoffset = db->cursor->len - screenw + 2;
441
    db->xoffset = db->cursor->len - screenw + 2;
444
    uidirty.from = 0;
442
    uidirty.from = 0;
445
    uidirty.to = 0xff;
443
    uidirty.to = 0xff;
446
  }
444
  }
447
  db->cursorposx = db->cursor->len - db->xoffset;
445
  db->cursorposx = db->cursor->len - db->xoffset;
448
}
446
}
449
 
447
 
450
 
448
 
451
static void cursor_down(struct file *db) {
449
static void cursor_down(struct file *db) {
452
  if (db->cursor->next == NULL) return;
450
  if (db->cursor->next == NULL) return;
453
 
451
 
454
  db->curline += 1;
452
  db->curline += 1;
455
  db->cursor = db->cursor->next;
453
  db->cursor = db->cursor->next;
456
  if (db->cursorposy < screenh - 2) {
454
  if (db->cursorposy < screenh - 2) {
457
    db->cursorposy += 1;
455
    db->cursorposy += 1;
458
  } else {
456
  } else {
459
    uidirty.from = 0;
457
    uidirty.from = 0;
460
    uidirty.to = 0xff;
458
    uidirty.to = 0xff;
461
  }
459
  }
462
}
460
}
463
 
461
 
464
 
462
 
465
static void cursor_left(struct file *db) {
463
static void cursor_left(struct file *db) {
466
  if (db->cursorposx > 0) {
464
  if (db->cursorposx > 0) {
467
    db->cursorposx -= 1;
465
    db->cursorposx -= 1;
468
  } else if (db->xoffset > 0) {
466
  } else if (db->xoffset > 0) {
469
    db->xoffset -= 1;
467
    db->xoffset -= 1;
470
    uidirty.from = 0;
468
    uidirty.from = 0;
471
    uidirty.to = 0xff;
469
    uidirty.to = 0xff;
472
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
470
  } else if (db->cursor->prev != NULL) { /* jump to end of line above */
473
    cursor_up(db);
471
    cursor_up(db);
474
    cursor_eol(db);
472
    cursor_eol(db);
475
  }
473
  }
476
}
474
}
477
 
475
 
478
 
476
 
479
static void cursor_home(struct file *db) {
477
static void cursor_home(struct file *db) {
480
  db->cursorposx = 0;
478
  db->cursorposx = 0;
481
  if (db->xoffset != 0) {
479
  if (db->xoffset != 0) {
482
    db->xoffset = 0;
480
    db->xoffset = 0;
483
    uidirty.from = 0;
481
    uidirty.from = 0;
484
    uidirty.to = 0xff;
482
    uidirty.to = 0xff;
485
  }
483
  }
486
}
484
}
487
 
485
 
488
 
486
 
489
static void cursor_right(struct file *db) {
487
static void cursor_right(struct file *db) {
490
  if (db->cursor->len > db->xoffset + db->cursorposx) {
488
  if (db->cursor->len > db->xoffset + db->cursorposx) {
491
    if (db->cursorposx < screenw - 2) {
489
    if (db->cursorposx < screenw - 2) {
492
      db->cursorposx += 1;
490
      db->cursorposx += 1;
493
    } else {
491
    } else {
494
      db->xoffset += 1;
492
      db->xoffset += 1;
495
      uidirty.from = 0;
493
      uidirty.from = 0;
496
      uidirty.to = 0xff;
494
      uidirty.to = 0xff;
497
    }
495
    }
498
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
496
  } else if (db->cursor->next != NULL) { /* jump to start of next line */
499
    cursor_down(db);
497
    cursor_down(db);
500
    cursor_home(db);
498
    cursor_home(db);
501
  }
499
  }
502
}
500
}
503
 
501
 
504
 
502
 
505
static void del(struct file *db) {
503
static void del(struct file *db) {
506
  if (db->cursorposx + db->xoffset < db->cursor->len) {
504
  if (db->cursorposx + db->xoffset < db->cursor->len) {
507
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
505
    _fmemmove(db->cursor->payload + db->cursorposx + db->xoffset, db->cursor->payload + db->cursorposx + db->xoffset + 1, db->cursor->len - db->cursorposx - db->xoffset);
508
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
506
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
509
    uidirty.from = db->cursorposy;
507
    uidirty.from = db->cursorposy;
510
    uidirty.to = db->cursorposy;
508
    uidirty.to = db->cursorposy;
511
    db->modflag = 1;
509
    db->modflag = 1;
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) */
510
  } else if (db->cursor->next != NULL) { /* cursor is at end of line: merge current line with next one (if there is a next one) */
513
    struct line far *nextline = db->cursor->next;
511
    struct line far *nextline = db->cursor->next;
514
    if (db->cursor->next->len > 0) {
512
    if (db->cursor->next->len > 0) {
515
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
513
      if (curline_resize(db, db->cursor->len + db->cursor->next->len + 1) == 0) {
516
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
514
        _fmemmove(db->cursor->payload + db->cursor->len, db->cursor->next->payload, db->cursor->next->len + 1);
517
        db->cursor->len += db->cursor->next->len;
515
        db->cursor->len += db->cursor->next->len;
518
      }
516
      }
519
    }
517
    }
520
 
518
 
521
    db->cursor->next = db->cursor->next->next;
519
    db->cursor->next = db->cursor->next->next;
522
    db->cursor->next->prev = db->cursor;
520
    db->cursor->next->prev = db->cursor;
523
 
521
 
524
    line_free(nextline);
522
    line_free(nextline);
525
    uidirty.from = db->cursorposy;
523
    uidirty.from = db->cursorposy;
526
    uidirty.to = 0xff;
524
    uidirty.to = 0xff;
527
    db->totlines -= 1;
525
    db->totlines -= 1;
528
    db->modflag = 1;
526
    db->modflag = 1;
529
  }
527
  }
530
}
528
}
531
 
529
 
532
 
530
 
533
static void bkspc(struct file *db) {
531
static void bkspc(struct file *db) {
534
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
532
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
535
  if ((db->cursorposx + db->xoffset == 0) && (db->cursor->prev == NULL)) return;
533
  if ((db->cursorposx + db->xoffset == 0) && (db->cursor->prev == NULL)) return;
536
 
534
 
537
  cursor_left(db);
535
  cursor_left(db);
538
  del(db);
536
  del(db);
539
}
537
}
540
 
538
 
541
 
539
 
542
/* returns 0 on success, 1 on file not found, 2 on other error */
540
/* returns 0 on success, 1 on file not found, 2 on other error */
543
static int loadfile(struct file *db, const char *fname) {
541
static int loadfile(struct file *db, const char *fname) {
544
  char buff[512]; /* read one entire sector at a time (faster) */
542
  char buff[512]; /* read one entire sector at a time (faster) */
545
  char *buffptr;
543
  char *buffptr;
546
  unsigned int len, llen;
544
  unsigned int len, llen;
547
  int fd;
545
  int fd;
548
  unsigned char eolfound;
546
  unsigned char eolfound;
549
 
547
 
550
  /* free the entire linked list of lines */
548
  /* free the entire linked list of lines */
551
  db_rewind(db);
549
  db_rewind(db);
552
  while (db->cursor) {
550
  while (db->cursor) {
553
    struct line far *victim;
551
    struct line far *victim;
554
    victim = db->cursor;
552
    victim = db->cursor;
555
    db->cursor = db->cursor->next;
553
    db->cursor = db->cursor->next;
556
    line_free(victim);
554
    line_free(victim);
557
  }
555
  }
558
 
556
 
559
  /* zero out the struct */
557
  /* zero out the struct */
560
  bzero(db, sizeof(struct file));
558
  bzero(db, sizeof(struct file));
561
 
559
 
562
  if (fname == NULL) goto SKIPLOADING;
560
  if (fname == NULL) goto SKIPLOADING;
563
 
561
 
564
  memcpy(db->fname, fname, strlen(fname));
562
  memcpy(db->fname, fname, strlen(fname));
565
 
563
 
566
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
564
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
567
    return(1);
565
    return(1);
568
  }
566
  }
569
 
567
 
570
  db->lfonly = 1;
568
  db->lfonly = 1;
571
 
569
 
572
  /* start by adding an empty line */
570
  /* start by adding an empty line */
573
  if (line_add(db, NULL, 0) != 0) {
571
  if (line_add(db, NULL, 0) != 0) {
574
    /* TODO ERROR HANDLING */
572
    /* TODO ERROR HANDLING */
575
  }
573
  }
576
 
574
 
577
  for (eolfound = 0;;) {
575
  for (eolfound = 0;;) {
578
    unsigned short consumedbytes;
576
    unsigned short consumedbytes;
579
 
577
 
580
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
578
    if ((_dos_read(fd, buff, sizeof(buff), &len) != 0) || (len == 0)) break;
581
    buffptr = buff;
579
    buffptr = buff;
582
 
580
 
583
    FINDLINE:
581
    FINDLINE:
584
 
582
 
585
    /* look for nearest \n (also expand tabs) */
583
    /* look for nearest \n (also expand tabs) */
586
    for (consumedbytes = 0;; consumedbytes++) {
584
    for (consumedbytes = 0;; consumedbytes++) {
587
 
585
 
588
      if (buffptr[consumedbytes] == '\t') {
586
      if (buffptr[consumedbytes] == '\t') {
589
        llen = consumedbytes;
587
        llen = consumedbytes;
590
        break;
588
        break;
591
      }
589
      }
592
 
590
 
593
      if (consumedbytes == len) {
591
      if (consumedbytes == len) {
594
        llen = consumedbytes;
592
        llen = consumedbytes;
595
        break;
593
        break;
596
      }
594
      }
597
      if (buffptr[consumedbytes] == '\r') {
595
      if (buffptr[consumedbytes] == '\r') {
598
        llen = consumedbytes;
596
        llen = consumedbytes;
599
        consumedbytes++;
597
        consumedbytes++;
600
        db->lfonly = 0;
598
        db->lfonly = 0;
601
        break;
599
        break;
602
      }
600
      }
603
      if (buffptr[consumedbytes] == '\n') {
601
      if (buffptr[consumedbytes] == '\n') {
604
        eolfound = 1;
602
        eolfound = 1;
605
        llen = consumedbytes;
603
        llen = consumedbytes;
606
        consumedbytes++;
604
        consumedbytes++;
607
        break;
605
        break;
608
      }
606
      }
609
    }
607
    }
610
 
608
 
611
    /* consumedbytes is the amount of bytes processed from buffptr,
609
    /* consumedbytes is the amount of bytes processed from buffptr,
612
     * llen is the length of line's payload (without its line terminator) */
610
     * llen is the length of line's payload (without its line terminator) */
613
 
611
 
614
    /* append content, if line is non-empty */
612
    /* append content, if line is non-empty */
615
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
613
    if ((llen > 0) && (line_append(db, buffptr, llen) != 0)) {
616
      goto IOERR;
614
      goto IOERR;
617
    }
615
    }
618
 
616
 
619
    /* allocate the next line if current line ended */
617
    /* allocate the next line if current line ended */
620
    if (eolfound) {
618
    if (eolfound) {
621
      if (line_add(db, NULL, 0) != 0) {
619
      if (line_add(db, NULL, 0) != 0) {
622
        goto IOERR;
620
        goto IOERR;
623
      }
621
      }
624
      eolfound = 0;
622
      eolfound = 0;
625
    }
623
    }
626
 
624
 
627
    /* append 8 spaces if tab char found */
625
    /* append 8 spaces if tab char found */
628
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t') && (glob_tablessmode == 0)) {
626
    if ((consumedbytes < len) && (buffptr[consumedbytes] == '\t') && (glob_tablessmode == 0)) {
629
      consumedbytes++;
627
      consumedbytes++;
630
      if (line_append(db, "        ", 8) != 0) {
628
      if (line_append(db, "        ", 8) != 0) {
631
        goto IOERR;
629
        goto IOERR;
632
      }
630
      }
633
    }
631
    }
634
 
632
 
635
    /* anything left? process the buffer leftover again */
633
    /* anything left? process the buffer leftover again */
636
    if (consumedbytes < len) {
634
    if (consumedbytes < len) {
637
      len -= consumedbytes;
635
      len -= consumedbytes;
638
      buffptr += consumedbytes;
636
      buffptr += consumedbytes;
639
      goto FINDLINE;
637
      goto FINDLINE;
640
    }
638
    }
641
 
639
 
642
  }
640
  }
643
 
641
 
644
  _dos_close(fd);
642
  _dos_close(fd);
645
 
643
 
646
  SKIPLOADING:
644
  SKIPLOADING:
647
 
645
 
648
  /* add an empty line at end if not present already, also rewind cursor to top of file */
646
  /* add an empty line at end if not present already, also rewind cursor to top of file */
649
  if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
647
  if ((db->cursor == NULL) || (db->cursor->len != 0)) line_add(db, NULL, 0);
650
  db_rewind(db);
648
  db_rewind(db);
651
 
649
 
652
  return(0);
650
  return(0);
653
 
651
 
654
  IOERR:
652
  IOERR:
655
  _dos_close(fd);
653
  _dos_close(fd);
656
  return(2);
654
  return(2);
657
}
655
}
658
 
656
 
659
 
657
 
660
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
658
/* a custom argv-parsing routine that looks directly inside the PSP, avoids the need
661
 * of argc and argv, saves some 330 bytes of binary size
659
 * of argc and argv, saves some 330 bytes of binary size
662
 * returns non-zero on error */
660
 * returns non-zero on error */
663
static int parseargv(struct file *dbarr) {
661
static int parseargv(struct file *dbarr) {
664
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
662
  char *tail = (void *)0x81; /* THIS WORKS ONLY IN SMALL MEMORY MODEL */
665
  unsigned short count = 0;
663
  unsigned short count = 0;
666
  char *arg;
664
  char *arg;
667
  unsigned short lastarg = 0;
665
  unsigned short lastarg = 0;
668
  int err;
666
  int err;
669
 
667
 
670
  while (!lastarg) {
668
  while (!lastarg) {
671
    /* jump to nearest arg */
669
    /* jump to nearest arg */
672
    while (*tail == ' ') {
670
    while (*tail == ' ') {
673
      *tail = 0;
671
      *tail = 0;
674
      tail++;
672
      tail++;
675
    }
673
    }
676
 
674
 
677
    if (*tail == '\r') {
675
    if (*tail == '\r') {
678
      *tail = 0;
676
      *tail = 0;
679
      break;
677
      break;
680
    }
678
    }
681
 
679
 
682
    arg = tail;
680
    arg = tail;
683
 
681
 
684
    /* jump to next delimiter */
682
    /* jump to next delimiter */
685
    while ((*tail != ' ') && (*tail != '\r')) tail++;
683
    while ((*tail != ' ') && (*tail != '\r')) tail++;
686
 
684
 
687
    /* if \r then remember this is the last arg */
685
    /* if \r then remember this is the last arg */
688
    if (*tail == '\r') lastarg = 1;
686
    if (*tail == '\r') lastarg = 1;
689
 
687
 
690
    *tail = 0;
688
    *tail = 0;
691
    tail++;
689
    tail++;
692
 
690
 
693
    /* look at the arg now */
691
    /* look at the arg now */
694
    if (*arg == '/') {
692
    if (*arg == '/') {
695
      if (arg[1] == 't') { /* /t = do not expand tabs */
693
      if (arg[1] == 't') { /* /t = do not expand tabs */
696
        glob_tablessmode = 1;
694
        glob_tablessmode = 1;
697
 
695
 
698
      } else if (arg[1] == 'm') { /* /m = force mono mode */
696
      } else if (arg[1] == 'm') { /* /m = force mono mode */
699
        glob_monomode = 1;
697
        glob_monomode = 1;
700
 
698
 
701
      } else {  /* help screen */
699
      } else {  /* help screen */
702
        mdr_coutraw_str(svarlang_str(1,3)); /* Sved, the SvarDOS editor */
700
        mdr_coutraw_str(svarlang_str(1,3)); /* Sved, the SvarDOS editor */
703
        mdr_coutraw_str(" [");
701
        mdr_coutraw_str(" [");
704
        mdr_coutraw_str(svarlang_str(1,4)); /* ver */
702
        mdr_coutraw_str(svarlang_str(1,4)); /* ver */
705
        mdr_coutraw_puts(" " PVER "]");
703
        mdr_coutraw_puts(" " PVER "]");
706
        mdr_coutraw_puts("Copyright (C) " PDATE " Mateusz Viste");
704
        mdr_coutraw_puts("Copyright (C) " PDATE " Mateusz Viste");
707
        mdr_coutraw_crlf();
705
        mdr_coutraw_crlf();
708
        mdr_coutraw_str("sved [/m] [/t] ");
706
        mdr_coutraw_str("sved [/m] [/t] ");
709
        mdr_coutraw_puts(svarlang_str(1,1)); /* args syntax */
707
        mdr_coutraw_puts(svarlang_str(1,1)); /* args syntax */
710
        mdr_coutraw_crlf();
708
        mdr_coutraw_crlf();
711
        mdr_coutraw_puts(svarlang_str(1,10)); /* /m */
709
        mdr_coutraw_puts(svarlang_str(1,10)); /* /m */
712
        mdr_coutraw_puts(svarlang_str(1,11)); /* /t */
710
        mdr_coutraw_puts(svarlang_str(1,11)); /* /t */
713
        return(-1);
711
        return(-1);
714
      }
712
      }
715
      continue;
713
      continue;
716
    }
714
    }
717
 
715
 
718
    /* looks to be a filename */
716
    /* looks to be a filename */
719
    if (count == 10) {
717
    if (count == 10) {
720
      mdr_coutraw_puts(svarlang_str(0,12)); /* too many files */
718
      mdr_coutraw_puts(svarlang_str(0,12)); /* too many files */
721
      return(-1);
719
      return(-1);
722
    }
720
    }
723
 
721
 
724
    /* try loading it */
722
    /* try loading it */
725
    mdr_coutraw_str(svarlang_str(1,2));
723
    mdr_coutraw_str(svarlang_str(1,2));
726
    mdr_coutraw_char(' ');
724
    mdr_coutraw_char(' ');
727
    mdr_coutraw_puts(arg);
725
    mdr_coutraw_puts(arg);
728
    err = loadfile(&(dbarr[count]), arg);
726
    err = loadfile(&(dbarr[count]), arg);
729
    if (err) {
727
    if (err) {
730
      if (err == 1) { /* file not found */
728
      if (err == 1) { /* file not found */
731
        if ((count == 0) && (lastarg != 0)) { /* a 'file not found' is fine if only one file was given */
729
        if ((count == 0) && (lastarg != 0)) { /* a 'file not found' is fine if only one file was given */
732
          memcpy(dbarr[count].fname, arg, strlen(arg) + 1);
730
          memcpy(dbarr[count].fname, arg, strlen(arg) + 1);
733
          err = 0;
731
          err = 0;
734
        } else {
732
        } else {
735
          err = 11;
733
          err = 11;
736
        }
734
        }
737
      } else { /* general error */
735
      } else { /* general error */
738
        err = 10;
736
        err = 10;
739
      }
737
      }
740
      if (err) {
738
      if (err) {
741
        mdr_coutraw_puts(svarlang_str(0,err));
739
        mdr_coutraw_puts(svarlang_str(0,err));
742
        return(-1);
740
        return(-1);
743
      }
741
      }
744
    }
742
    }
745
    count++;
743
    count++;
746
  }
744
  }
747
 
745
 
748
  return(0);
746
  return(0);
749
}
747
}
750
 
748
 
751
 
749
 
752
static int savefile(const struct file *db, const char *newfname) {
750
static int savefile(const struct file *db, const char *newfname) {
753
  int fd;
751
  int fd;
754
  const struct line far *l;
752
  const struct line far *l;
755
  unsigned int bytes;
753
  unsigned int bytes;
756
  unsigned char eollen;
754
  unsigned char eollen;
757
  unsigned char eolbuf[2];
755
  unsigned char eolbuf[2];
758
  int errflag = 0;
756
  int errflag = 0;
759
 
757
 
760
  /* either create a new file if newfname provided, or... */
758
  /* either create a new file if newfname provided, or... */
761
  if (newfname) {
759
  if (newfname) {
762
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
760
    if (_dos_creatnew(newfname, _A_NORMAL, &fd) != 0) return(-1);
763
  } else { /* ...open db->fname */
761
  } else { /* ...open db->fname */
764
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
762
    if (_dos_open(db->fname, O_WRONLY, &fd) != 0) return(-1);
765
  }
763
  }
766
 
764
 
767
  l = db->cursor;
765
  l = db->cursor;
768
  while (l->prev) l = l->prev;
766
  while (l->prev) l = l->prev;
769
 
767
 
770
  /* preset line terminators */
768
  /* preset line terminators */
771
  if (db->lfonly) {
769
  if (db->lfonly) {
772
    eolbuf[0] = '\n';
770
    eolbuf[0] = '\n';
773
    eollen = 1;
771
    eollen = 1;
774
  } else {
772
  } else {
775
    eolbuf[0] = '\r';
773
    eolbuf[0] = '\r';
776
    eolbuf[1] = '\n';
774
    eolbuf[1] = '\n';
777
    eollen = 2;
775
    eollen = 2;
778
  }
776
  }
779
 
777
 
780
  while (l) {
778
  while (l) {
781
    /* do not write the last empty line, it is only useful for edition */
779
    /* do not write the last empty line, it is only useful for edition */
782
    if (l->len != 0) {
780
    if (l->len != 0) {
783
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
781
      errflag |= _dos_write(fd, l->payload, l->len, &bytes);
784
    } else if (l->next == NULL) {
782
    } else if (l->next == NULL) {
785
      break;
783
      break;
786
    }
784
    }
787
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
785
    errflag |= _dos_write(fd, eolbuf, eollen, &bytes);
788
    l = l->next;
786
    l = l->next;
789
  }
787
  }
790
 
788
 
791
  errflag |= _dos_close(fd);
789
  errflag |= _dos_close(fd);
792
 
790
 
793
  return(errflag);
791
  return(errflag);
794
}
792
}
795
 
793
 
796
 
794
 
797
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
795
static void insert_in_line(struct file *db, const char *databuf, unsigned short len) {
798
  if (curline_resize(db, db->cursor->len + len) == 0) {
796
  if (curline_resize(db, db->cursor->len + len) == 0) {
799
    unsigned short off = db->xoffset + db->cursorposx;
797
    unsigned short off = db->xoffset + db->cursorposx;
800
    db->modflag = 1;
798
    db->modflag = 1;
801
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
799
    _fmemmove(db->cursor->payload + off + len, db->cursor->payload + off, db->cursor->len - off + 1);
802
    db->cursor->len += len;
800
    db->cursor->len += len;
803
    uidirty.from = db->cursorposy;
801
    uidirty.from = db->cursorposy;
804
    uidirty.to = db->cursorposy;
802
    uidirty.to = db->cursorposy;
805
    while (len--) {
803
    while (len--) {
806
      db->cursor->payload[off++] = *databuf;
804
      db->cursor->payload[off++] = *databuf;
807
      databuf++;
805
      databuf++;
808
      cursor_right(db);
806
      cursor_right(db);
809
    }
807
    }
810
  }
808
  }
811
}
809
}
812
 
810
 
813
 
811
 
814
/* recompute db->curline by counting nodes in linked list */
812
/* recompute db->curline by counting nodes in linked list */
815
static void recompute_curline(struct file *db) {
813
static void recompute_curline(struct file *db) {
816
  const struct line far *l = db->cursor;
814
  const struct line far *l = db->cursor;
817
 
815
 
818
  db->curline = 0;
816
  db->curline = 0;
819
  while (l->prev != NULL) {
817
  while (l->prev != NULL) {
820
    db->curline += 1;
818
    db->curline += 1;
821
    l = l->prev;
819
    l = l->prev;
822
  }
820
  }
823
}
821
}
824
 
822
 
825
 
823
 
826
enum MENU_ACTION {
824
enum MENU_ACTION {
827
  MENU_NONE   = 0,
825
  MENU_NONE   = 0,
828
  MENU_OPEN   = 1,
826
  MENU_OPEN   = 1,
829
  MENU_SAVE   = 2,
827
  MENU_SAVE   = 2,
830
  MENU_SAVEAS = 3,
828
  MENU_SAVEAS = 3,
831
  MENU_CLOSE  = 4,
829
  MENU_CLOSE  = 4,
832
  MENU_CHGEOL = 5,
830
  MENU_CHGEOL = 5,
833
  MENU_QUIT   = 6
831
  MENU_QUIT   = 6
834
};
832
};
835
 
833
 
836
static enum MENU_ACTION ui_menu(void) {
834
static enum MENU_ACTION ui_menu(void) {
837
  unsigned short i, curchoice, attr, x, slen;
835
  unsigned short i, curchoice, attr, x, slen;
838
  unsigned short xorigin, yorigin;
836
  unsigned short xorigin, yorigin;
839
 
837
 
840
  /* find out the longest string */
838
  /* find out the longest string */
841
  slen = 0;
839
  slen = 0;
842
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
840
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
843
    x = strlen(svarlang_str(8, i));
841
    x = strlen(svarlang_str(8, i));
844
    if (x > slen) slen = x;
842
    if (x > slen) slen = x;
845
  }
843
  }
846
 
844
 
847
  /* calculate where to draw the menu on screen */
845
  /* calculate where to draw the menu on screen */
848
  xorigin = (screenw - (slen + 5)) / 2;
846
  xorigin = (screenw - (slen + 5)) / 2;
849
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 6)) / 2;
847
  yorigin = (screenh - (MENU_QUIT - MENU_OPEN + 6)) / 2;
850
 
848
 
851
  /* */
849
  /* */
852
  uidirty.from = yorigin;
850
  uidirty.from = yorigin;
853
  uidirty.to = 0xff;
851
  uidirty.to = 0xff;
854
  uidirty.statusbar = 1;
852
  uidirty.statusbar = 1;
855
 
853
 
856
  /* hide the cursor */
854
  /* hide the cursor */
857
  mdr_cout_cursor_hide();
855
  mdr_cout_cursor_hide();
858
 
856
 
859
  curchoice = MENU_OPEN;
857
  curchoice = MENU_OPEN;
860
  for (;;) {
858
  for (;;) {
861
    /* render menu */
859
    /* render menu */
862
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
860
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
863
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
861
      mdr_cout_char_rep(yorigin + i, xorigin, ' ', SCHEME_MENU, slen+4);
864
      if (i == curchoice) {
862
      if (i == curchoice) {
865
        attr = SCHEME_MENU_CUR;
863
        attr = SCHEME_MENU_CUR;
866
        mdr_cout_char_rep(yorigin + i, xorigin + 1, ' ', SCHEME_MENU_SEL, slen + 2);
864
        mdr_cout_char_rep(yorigin + i, xorigin + 1, ' ', SCHEME_MENU_SEL, slen + 2);
867
      } else {
865
      } else {
868
        attr = SCHEME_MENU;
866
        attr = SCHEME_MENU;
869
      }
867
      }
870
      mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
868
      mdr_cout_str(yorigin + i, xorigin + 2, svarlang_str(8, i), attr, slen);
871
    }
869
    }
872
    /* wait for key */
870
    /* wait for key */
873
    switch (mdr_dos_getkey2()) {
871
    switch (mdr_dos_getkey2()) {
874
      case 0x150: /* down */
872
      case 0x150: /* down */
875
        if (curchoice == MENU_QUIT) {
873
        if (curchoice == MENU_QUIT) {
876
          curchoice = MENU_OPEN;
874
          curchoice = MENU_OPEN;
877
        } else {
875
        } else {
878
          curchoice++;
876
          curchoice++;
879
        }
877
        }
880
        break;
878
        break;
881
      case 0x148: /* up */
879
      case 0x148: /* up */
882
        if (curchoice == MENU_OPEN) {
880
        if (curchoice == MENU_OPEN) {
883
          curchoice = MENU_QUIT;
881
          curchoice = MENU_QUIT;
884
        } else {
882
        } else {
885
          curchoice--;
883
          curchoice--;
886
        }
884
        }
887
        break;
885
        break;
888
      default:
886
      default:
889
        curchoice = MENU_NONE;
887
        curchoice = MENU_NONE;
890
        /* FALLTHRU */
888
        /* FALLTHRU */
891
      case '\r': /* ENTER */
889
      case '\r': /* ENTER */
892
        mdr_cout_cursor_show();
890
        mdr_cout_cursor_show();
893
        return(curchoice);
891
        return(curchoice);
894
    }
892
    }
895
  }
893
  }
896
}
894
}
897
 
895
 
898
 
896
 
899
static struct file *select_slot(struct file *dbarr, unsigned char curfile) {
897
static struct file *select_slot(struct file *dbarr, unsigned char curfile) {
900
  uidirty.from = 0;
898
  uidirty.from = 0;
901
  uidirty.to = 0xff;
899
  uidirty.to = 0xff;
902
  uidirty.statusbar = 1;
900
  uidirty.statusbar = 1;
903
 
901
 
904
  dbarr = &(dbarr[curfile]);
902
  dbarr = &(dbarr[curfile]);
905
  /* force redraw now, because the main() routine might not if this is exit
903
  /* force redraw now, because the main() routine might not if this is exit
906
   * time and we want to show the user which file has unsaved changes */
904
   * time and we want to show the user which file has unsaved changes */
907
  ui_statusbar(dbarr, curfile);
905
  ui_statusbar(dbarr, curfile);
908
  ui_refresh(dbarr);
906
  ui_refresh(dbarr);
909
  return(dbarr);
907
  return(dbarr);
910
}
908
}
911
 
909
 
912
 
910
 
913
/* main returns nothing, ie. sved always exits with a zero exit code
911
/* main returns nothing, ie. sved always exits with a zero exit code
914
 * (this saves 20 bytes of executable footprint) */
912
 * (this saves 20 bytes of executable footprint) */
915
void main(void) {
913
void main(void) {
916
  static struct file dbarr[10];
914
  static struct file dbarr[10];
917
  unsigned char curfile;
915
  unsigned char curfile;
918
  struct file *db = dbarr; /* visible file is the first slot by default */
916
  struct file *db = dbarr; /* visible file is the first slot by default */
919
  struct line far *clipboard = NULL;
917
  struct line far *clipboard = NULL;
920
  unsigned char original_breakflag;
918
  unsigned char original_breakflag;
921
 
919
 
922
  { /* load NLS resource */
920
  { /* load NLS resource */
923
    unsigned short i = 0;
921
    unsigned short i = 0;
924
    const char far *selfptr;
922
    const char far *selfptr;
925
    char self[128], lang[8];
923
    char self[128], lang[8];
926
    selfptr = mdr_dos_selfexe();
924
    selfptr = mdr_dos_selfexe();
927
    if (selfptr != NULL) {
925
    if (selfptr != NULL) {
928
      do {
926
      do {
929
        self[i] = selfptr[i];
927
        self[i] = selfptr[i];
930
      } while (self[i++] != 0);
928
      } while (self[i++] != 0);
931
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
929
      svarlang_autoload_exepath(self, mdr_dos_getenv(lang, "LANG", sizeof(lang)));
932
    }
930
    }
933
  }
931
  }
934
 
932
 
935
  /* preload all slots with empty files */
933
  /* preload all slots with empty files */
936
  for (curfile = 9;; curfile--) {
934
  for (curfile = 9;; curfile--) {
937
    loadfile(&(dbarr[curfile]), NULL);
935
    loadfile(&(dbarr[curfile]), NULL);
938
    if (curfile == 0) break;
936
    if (curfile == 0) break;
939
  }
937
  }
940
 
938
 
941
  /* parse argv (and load files, if any passed on) */
939
  /* parse argv (and load files, if any passed on) */
942
  if (parseargv(dbarr) != 0) return;
940
  if (parseargv(dbarr) != 0) return;
943
 
941
 
944
  if ((mdr_cout_init(&screenw, &screenh) != 0) && (glob_monomode == 0)) {
942
  if ((mdr_cout_init(&screenw, &screenh) != 0) && (glob_monomode == 0)) {
945
    /* load color scheme if mdr_cout_init returns a color flag */
943
    /* load color scheme if mdr_cout_init returns a color flag */
946
    SCHEME_TEXT = 0x17;
944
    SCHEME_TEXT = 0x17;
947
    SCHEME_MENU = 0x70;
945
    SCHEME_MENU = 0x70;
948
    SCHEME_MENU_CUR = 0x6f;
946
    SCHEME_MENU_CUR = 0x6f;
949
    SCHEME_MENU_SEL = 0x66;
947
    SCHEME_MENU_SEL = 0x66;
950
    SCHEME_STBAR1 = 0x70;
948
    SCHEME_STBAR1 = 0x70;
951
    SCHEME_STBAR2 = 0x78;
949
    SCHEME_STBAR2 = 0x78;
952
    SCHEME_STBAR3 = 0x3f;
950
    SCHEME_STBAR3 = 0x3f;
953
    SCHEME_SCROLL = 0x70;
951
    SCHEME_SCROLL = 0x70;
954
    SCHEME_MSG = 0x6f;
952
    SCHEME_MSG = 0x6f;
955
    SCHEME_ERR = 0x4f;
953
    SCHEME_ERR = 0x4f;
956
  }
954
  }
957
  screenlastrow = screenh - 1;
955
  screenlastrow = screenh - 1;
958
  screenlastcol = screenw - 1;
956
  screenlastcol = screenw - 1;
959
 
957
 
960
  /* instruct DOS to stop detecting CTRL+C because user needs it for
958
  /* instruct DOS to stop detecting CTRL+C because user needs it for
961
   * copy/paste operations. also remember the original status of the BREAK
959
   * copy/paste operations. also remember the original status of the BREAK
962
   * flag so I can restore it as it was before quitting. */
960
   * flag so I can restore it as it was before quitting. */
963
  original_breakflag = mdr_dos_ctrlc_disable();
961
  original_breakflag = mdr_dos_ctrlc_disable();
964
 
962
 
965
  for (;;) {
963
  for (;;) {
966
    int k;
964
    int k;
967
 
965
 
968
    /* add an extra empty line if cursor is on last line and this line is not empty */
966
    /* add an extra empty line if cursor is on last line and this line is not empty */
969
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
967
    if ((db->cursor->next == NULL) && (db->cursor->len != 0)) {
970
      if (line_add(db, NULL, 0) == 0) {
968
      if (line_add(db, NULL, 0) == 0) {
971
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
969
        db->cursor = db->cursor->prev; /* line_add() changes the cursor pointer */
972
        db->curline -= 1;
970
        db->curline -= 1;
973
      }
971
      }
974
    }
972
    }
975
 
973
 
976
    check_cursor_not_after_eol(db);
974
    check_cursor_not_after_eol(db);
977
    mdr_cout_locate(db->cursorposy, db->cursorposx);
975
    mdr_cout_locate(db->cursorposy, db->cursorposx);
978
 
976
 
979
    ui_refresh(db);
977
    ui_refresh(db);
980
 
978
 
981
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag) || (db->curline_prev != db->curline)) {
979
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag) || (db->curline_prev != db->curline)) {
982
      ui_statusbar(db, curfile);
980
      ui_statusbar(db, curfile);
983
      uidirty.statusbar = 0;
981
      uidirty.statusbar = 0;
984
      db->modflagprev = db->modflag;
982
      db->modflagprev = db->modflag;
985
      db->curline_prev = db->curline;
983
      db->curline_prev = db->curline;
986
    }
984
    }
987
#ifdef DBG_LINENUM
985
#ifdef DBG_LINENUM
988
      {
986
      {
989
        char ddd[10];
987
        char ddd[10];
990
        db->curline += 1;
988
        db->curline += 1;
991
        ddd[0] = '0' + db->curline / 100;
989
        ddd[0] = '0' + db->curline / 100;
992
        ddd[1] = '0' + (db->curline % 100) / 10;
990
        ddd[1] = '0' + (db->curline % 100) / 10;
993
        ddd[2] = '0' + (db->curline % 10);
991
        ddd[2] = '0' + (db->curline % 10);
994
        db->curline -= 1;
992
        db->curline -= 1;
995
        ddd[3] = '/';
993
        ddd[3] = '/';
996
        ddd[4] = '0' + db->totlines / 100;
994
        ddd[4] = '0' + db->totlines / 100;
997
        ddd[5] = '0' + (db->totlines % 100) / 10;
995
        ddd[5] = '0' + (db->totlines % 100) / 10;
998
        ddd[6] = '0' + (db->totlines % 10);
996
        ddd[6] = '0' + (db->totlines % 10);
999
        ddd[7] = 0;
997
        ddd[7] = 0;
1000
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
998
        mdr_cout_str(screenh - 1, 40, ddd, SCHEME_STBAR1, sizeof(ddd));
1001
      }
999
      }
1002
#endif
1000
#endif
1003
 
1001
 
1004
    k = mdr_dos_getkey2();
1002
    k = mdr_dos_getkey2();
1005
 
1003
 
1006
    if (k == 0x150) { /* down */
1004
    if (k == 0x150) { /* down */
1007
      cursor_down(db);
1005
      cursor_down(db);
1008
 
1006
 
1009
    } else if (k == 0x148) { /* up */
1007
    } else if (k == 0x148) { /* up */
1010
      cursor_up(db);
1008
      cursor_up(db);
1011
 
1009
 
1012
    } else if (k == 0x14D) { /* right */
1010
    } else if (k == 0x14D) { /* right */
1013
      cursor_right(db);
1011
      cursor_right(db);
1014
 
1012
 
1015
    } else if (k == 0x14B) { /* left */
1013
    } else if (k == 0x14B) { /* left */
1016
      cursor_left(db);
1014
      cursor_left(db);
1017
 
1015
 
1018
    } else if (k == 0x149) { /* pgup */
1016
    } else if (k == 0x149) { /* pgup */
1019
      unsigned char dist = db->cursorposy + screenh - 1;
1017
      unsigned char dist = db->cursorposy + screenh - 1;
1020
      while ((dist != 0) && (db->cursor->prev != NULL)) {
1018
      while ((dist != 0) && (db->cursor->prev != NULL)) {
1021
        db->cursor = db->cursor->prev;
1019
        db->cursor = db->cursor->prev;
1022
        dist--;
1020
        dist--;
1023
      }
1021
      }
1024
      if (dist != 0) {
1022
      if (dist != 0) {
1025
        db->cursorposy = 0;
1023
        db->cursorposy = 0;
1026
        db->cursorposx = 0;
1024
        db->cursorposx = 0;
1027
      } else {
1025
      } else {
1028
        dist = db->cursorposy;
1026
        dist = db->cursorposy;
1029
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
1027
        while ((dist--) && (db->cursor->next)) db->cursor = db->cursor->next;
1030
      }
1028
      }
1031
      uidirty.from = 0;
1029
      uidirty.from = 0;
1032
      uidirty.to = 0xff;
1030
      uidirty.to = 0xff;
1033
      recompute_curline(db);
1031
      recompute_curline(db);
1034
 
1032
 
1035
    } else if (k == 0x151) { /* pgdown */
1033
    } else if (k == 0x151) { /* pgdown */
1036
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
1034
      unsigned char dist = screenh + screenh - db->cursorposy - 3;
1037
      while ((dist != 0) && (db->cursor->next != NULL)) {
1035
      while ((dist != 0) && (db->cursor->next != NULL)) {
1038
        db->cursor = db->cursor->next;
1036
        db->cursor = db->cursor->next;
1039
        dist--;
1037
        dist--;
1040
      }
1038
      }
1041
      if (dist != 0) {
1039
      if (dist != 0) {
1042
        db->cursorposy = screenh - 2;
1040
        db->cursorposy = screenh - 2;
1043
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
1041
        if (db->totlines <= db->cursorposy) db->cursorposy = db->totlines - 1;
1044
        db->cursorposx = 0;
1042
        db->cursorposx = 0;
1045
      } else {
1043
      } else {
1046
        dist = screenh - 2 - db->cursorposy;
1044
        dist = screenh - 2 - db->cursorposy;
1047
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
1045
        while ((dist--) && (db->cursor->prev)) db->cursor = db->cursor->prev;
1048
      }
1046
      }
1049
      uidirty.from = 0;
1047
      uidirty.from = 0;
1050
      uidirty.to = 0xff;
1048
      uidirty.to = 0xff;
1051
      recompute_curline(db);
1049
      recompute_curline(db);
1052
 
1050
 
1053
    } else if (k == 0x147) { /* home */
1051
    } else if (k == 0x147) { /* home */
1054
       cursor_home(db);
1052
       cursor_home(db);
1055
 
1053
 
1056
    } else if (k == 0x14F) { /* end */
1054
    } else if (k == 0x14F) { /* end */
1057
       cursor_eol(db);
1055
       cursor_eol(db);
1058
 
1056
 
1059
    } else if (k == 0x1B) { /* ESC */
1057
    } else if (k == 0x1B) { /* ESC */
1060
      int quitnow = 0;
1058
      int quitnow = 0;
1061
      char fname[64];
1059
      char fname[64];
1062
      int saveflag = 0;
1060
      int saveflag = 0;
1063
      enum MENU_ACTION ui_action;
1061
      enum MENU_ACTION ui_action;
1064
 
1062
 
1065
      /* collect the exact menu action and clear the screen */
1063
      /* collect the exact menu action and clear the screen */
1066
      ui_action = ui_menu();
1064
      ui_action = ui_menu();
1067
      ui_refresh(db);
1065
      ui_refresh(db);
1068
 
1066
 
1069
      switch (ui_action) {
1067
      switch (ui_action) {
1070
 
1068
 
1071
        case MENU_NONE:
1069
        case MENU_NONE:
1072
          break;
1070
          break;
1073
 
1071
 
1074
        case MENU_OPEN:
1072
        case MENU_OPEN:
1075
          /* display a warning if unsaved changes are pending */
1073
          /* display a warning if unsaved changes are pending */
1076
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
1074
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
1077
 
1075
 
1078
          /* ask for filename */
1076
          /* ask for filename */
1079
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1077
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
1080
          if (fname[0] != 0) {
1078
          if (fname[0] != 0) {
1081
            int err;
1079
            int err;
1082
            err = loadfile(db, fname);
1080
            err = loadfile(db, fname);
1083
            if (err != 0) {
1081
            if (err != 0) {
1084
              if (err == 1) {
1082
              if (err == 1) {
1085
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1083
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
1086
              } else {
1084
              } else {
1087
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1085
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
1088
              }
1086
              }
1089
              mdr_bios_tickswait(44); /* 3s */
1087
              mdr_bios_tickswait(44); /* 3s */
1090
              loadfile(db, NULL);
1088
              loadfile(db, NULL);
1091
            }
1089
            }
1092
          }
1090
          }
1093
          uidirty.from = 0;
1091
          uidirty.from = 0;
1094
          uidirty.to = 0xff;
1092
          uidirty.to = 0xff;
1095
          uidirty.statusbar = 1;
1093
          uidirty.statusbar = 1;
1096
          break;
1094
          break;
1097
 
1095
 
1098
        case MENU_SAVEAS:
1096
        case MENU_SAVEAS:
1099
          saveflag = 1;
1097
          saveflag = 1;
1100
          /* FALLTHRU */
1098
          /* FALLTHRU */
1101
        case MENU_SAVE:
1099
        case MENU_SAVE:
1102
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1100
          if ((saveflag != 0) || (db->fname[0] == 0)) { /* save as... */
1103
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1101
            ui_getstring(svarlang_str(0,6), fname, sizeof(fname));
1104
            if (*fname == 0) break;
1102
            if (*fname == 0) break;
1105
            saveflag = savefile(db, fname);
1103
            saveflag = savefile(db, fname);
1106
            if (saveflag == 0) memcpy(db->fname, fname, sizeof(fname));
1104
            if (saveflag == 0) memcpy(db->fname, fname, sizeof(fname));
1107
          } else {
1105
          } else {
1108
            saveflag = savefile(db, NULL);
1106
            saveflag = savefile(db, NULL);
1109
          }
1107
          }
1110
 
1108
 
1111
          mdr_cout_cursor_hide();
1109
          mdr_cout_cursor_hide();
1112
 
1110
 
1113
          if (saveflag == 0) {
1111
          if (saveflag == 0) {
1114
            db->modflag = 0;
1112
            db->modflag = 0;
1115
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1113
            ui_msg(svarlang_str(0, 2), NULL, SCHEME_MSG);
1116
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1114
            mdr_bios_tickswait(11); /* 11 ticks is about 600 ms */
1117
          } else {
1115
          } else {
1118
            ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR);
1116
            ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR);
1119
            mdr_bios_tickswait(36); /* 2s */
1117
            mdr_bios_tickswait(36); /* 2s */
1120
          }
1118
          }
1121
          mdr_cout_cursor_show();
1119
          mdr_cout_cursor_show();
1122
          break;
1120
          break;
1123
 
1121
 
1124
        case MENU_CLOSE:
1122
        case MENU_CLOSE:
1125
          if (ui_confirm_if_unsaved(db) == 0) {
1123
          if (ui_confirm_if_unsaved(db) == 0) {
1126
            loadfile(db, NULL);
1124
            loadfile(db, NULL);
1127
          }
1125
          }
1128
          uidirty.from = 0;
1126
          uidirty.from = 0;
1129
          uidirty.to = 0xff;
1127
          uidirty.to = 0xff;
1130
          uidirty.statusbar = 1;
1128
          uidirty.statusbar = 1;
1131
          break;
1129
          break;
1132
 
1130
 
1133
        case MENU_CHGEOL:
1131
        case MENU_CHGEOL:
1134
          db->modflag = 1;
1132
          db->modflag = 1;
1135
          db->lfonly ^= 1;
1133
          db->lfonly ^= 1;
1136
          break;
1134
          break;
1137
 
1135
 
1138
        case MENU_QUIT:
1136
        case MENU_QUIT:
1139
          quitnow = 1;
1137
          quitnow = 1;
1140
          for (curfile = 0; curfile < 10; curfile++) {
1138
          for (curfile = 0; curfile < 10; curfile++) {
1141
            if (dbarr[curfile].modflag) {
1139
            if (dbarr[curfile].modflag) {
1142
              db = select_slot(dbarr, curfile);
1140
              db = select_slot(dbarr, curfile);
1143
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
1141
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
1144
            }
1142
            }
1145
          }
1143
          }
1146
          break;
1144
          break;
1147
      }
1145
      }
1148
 
1146
 
1149
      if (quitnow) break;
1147
      if (quitnow) break;
1150
 
1148
 
1151
    } else if (k == 0x0D) { /* ENTER */
1149
    } else if (k == 0x0D) { /* ENTER */
1152
      unsigned short off = db->xoffset + db->cursorposx;
1150
      unsigned short off = db->xoffset + db->cursorposx;
1153
      /* add a new line */
1151
      /* add a new line */
1154
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1152
      if (line_add(db, db->cursor->payload + off, db->cursor->len - off) == 0) {
1155
        db->modflag = 1;
1153
        db->modflag = 1;
1156
        db->cursor = db->cursor->prev; /* back to original line */
1154
        db->cursor = db->cursor->prev; /* back to original line */
1157
        db->curline -= 1;
1155
        db->curline -= 1;
1158
        /* trim the line above */
1156
        /* trim the line above */
1159
        db->cursor->len = off;
1157
        db->cursor->len = off;
1160
        /* move cursor to the (new) line below */
1158
        /* move cursor to the (new) line below */
1161
        uidirty.from = db->cursorposy;
1159
        uidirty.from = db->cursorposy;
1162
        uidirty.to = 0xff;
1160
        uidirty.to = 0xff;
1163
        cursor_down(db);
1161
        cursor_down(db);
1164
        cursor_home(db);
1162
        cursor_home(db);
1165
      } else {
1163
      } else {
1166
        /* ERROR: OUT OF MEMORY */
1164
        /* ERROR: OUT OF MEMORY */
1167
      }
1165
      }
1168
 
1166
 
1169
    } else if (k == 0x153) {  /* DEL */
1167
    } else if (k == 0x153) {  /* DEL */
1170
      del(db);
1168
      del(db);
1171
 
1169
 
1172
    } else if (k == 0x008) { /* BKSPC */
1170
    } else if (k == 0x008) { /* BKSPC */
1173
      bkspc(db);
1171
      bkspc(db);
1174
 
1172
 
1175
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1173
    } else if ((k >= 0x20) && (k <= 0xff)) { /* "normal" character */
1176
      char c = k;
1174
      char c = k;
1177
      insert_in_line(db, &c, 1);
1175
      insert_in_line(db, &c, 1);
1178
 
1176
 
1179
    } else if (k == 0x009) { /* TAB */
1177
    } else if (k == 0x009) { /* TAB */
1180
      if (glob_tablessmode == 0) {
1178
      if (glob_tablessmode == 0) {
1181
        insert_in_line(db, "        ", 8);
1179
        insert_in_line(db, "        ", 8);
1182
      } else {
1180
      } else {
1183
        insert_in_line(db, "\t", 1);
1181
        insert_in_line(db, "\t", 1);
1184
      }
1182
      }
1185
 
1183
 
1186
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1184
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
1187
      curfile = k - 0x13b;
1185
      curfile = k - 0x13b;
1188
      db = select_slot(dbarr, curfile);
1186
      db = select_slot(dbarr, curfile);
1189
 
1187
 
1190
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1188
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1191
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1189
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1192
      for (;;) {
1190
      for (;;) {
1193
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1191
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1194
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1192
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') break;
1195
        cursor_right(db);
1193
        cursor_right(db);
1196
      }
1194
      }
1197
      /* now skip to next non-space or end of file */
1195
      /* now skip to next non-space or end of file */
1198
      for (;;) {
1196
      for (;;) {
1199
        cursor_right(db);
1197
        cursor_right(db);
1200
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1198
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1201
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1199
        if ((db->cursor->next == NULL) && (db->cursorposx + db->xoffset == db->cursor->len)) break;
1202
      }
1200
      }
1203
 
1201
 
1204
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1202
    } else if (k == 0x173) { /* CTRL+ArrLeft - jump to prev word */
1205
      cursor_left(db);
1203
      cursor_left(db);
1206
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1204
      /* if currently cursor is on a space, then fast-forward to nearest non-space or start of line */
1207
      for (;;) {
1205
      for (;;) {
1208
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1206
        if ((db->xoffset == 0) && (db->cursorposx == 0)) break;
1209
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1207
        if (db->cursor->payload[db->xoffset + db->cursorposx] != ' ') break;
1210
        cursor_left(db);
1208
        cursor_left(db);
1211
      }
1209
      }
1212
      /* now skip to next space or start of file */
1210
      /* now skip to next space or start of file */
1213
      for (;;) {
1211
      for (;;) {
1214
        cursor_left(db);
1212
        cursor_left(db);
1215
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1213
        if (db->cursor->payload[db->xoffset + db->cursorposx] == ' ') {
1216
          cursor_right(db);
1214
          cursor_right(db);
1217
          break;
1215
          break;
1218
        }
1216
        }
1219
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1217
        if ((db->cursorposx == 0) && (db->xoffset == 0)) break;
1220
      }
1218
      }
1221
 
1219
 
1222
    } else if ((k == 0x003) || (k == 0x018)) { /* CTRL+C or CTRL+X */
1220
    } else if ((k == 0x003) || (k == 0x018)) { /* CTRL+C or CTRL+X */
1223
      /* free clipboard if anything in it */
1221
      /* free clipboard if anything in it */
1224
      if (clipboard != NULL) line_free(clipboard);
1222
      if (clipboard != NULL) line_free(clipboard);
1225
 
1223
 
1226
      /* copy cursor line to clipboard */
1224
      /* copy cursor line to clipboard */
1227
      clipboard = line_calloc(db->cursor->len);
1225
      clipboard = line_calloc(db->cursor->len);
1228
      if (clipboard == NULL) {
1226
      if (clipboard == NULL) {
1229
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1227
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1230
        mdr_bios_tickswait(18); /* 1s */
1228
        mdr_bios_tickswait(18); /* 1s */
1231
      } else {
1229
      } else {
1232
        mdr_cout_char_rep(db->cursorposy, 0, ' ', ((SCHEME_TEXT >> 4) | (SCHEME_TEXT << 4)) & 0xff, screenlastcol);
1230
        mdr_cout_char_rep(db->cursorposy, 0, ' ', ((SCHEME_TEXT >> 4) | (SCHEME_TEXT << 4)) & 0xff, screenlastcol);
1233
        uidirty.from = db->cursorposy;
1231
        uidirty.from = db->cursorposy;
1234
        uidirty.to = db->cursorposy;
1232
        uidirty.to = db->cursorposy;
1235
        if (db->cursor->len != 0) {
1233
        if (db->cursor->len != 0) {
1236
          _fmemmove(clipboard->payload, db->cursor->payload, db->cursor->len);
1234
          _fmemmove(clipboard->payload, db->cursor->payload, db->cursor->len);
1237
          clipboard->len = db->cursor->len;
1235
          clipboard->len = db->cursor->len;
1238
        }
1236
        }
1239
        mdr_bios_tickswait(2); /* ca 100ms */
1237
        mdr_bios_tickswait(2); /* ca 100ms */
1240
 
1238
 
1241
        /* if this is about cutting the line (CTRL+X) then delete cur line */
1239
        /* if this is about cutting the line (CTRL+X) then delete cur line */
1242
        if ((k == 0x018) && ((db->cursor->next != NULL) || (db->cursor->prev != NULL))) {
1240
        if ((k == 0x018) && ((db->cursor->next != NULL) || (db->cursor->prev != NULL))) {
1243
          if (db->cursor->next) db->cursor->next->prev = db->cursor->prev;
1241
          if (db->cursor->next) db->cursor->next->prev = db->cursor->prev;
1244
          if (db->cursor->prev) db->cursor->prev->next = db->cursor->next;
1242
          if (db->cursor->prev) db->cursor->prev->next = db->cursor->next;
1245
          clipboard->prev = db->cursor;
1243
          clipboard->prev = db->cursor;
1246
          if (db->cursor->next) {
1244
          if (db->cursor->next) {
1247
            db->cursor = db->cursor->next;
1245
            db->cursor = db->cursor->next;
1248
          } else {
1246
          } else {
1249
            cursor_up(db);
1247
            cursor_up(db);
1250
          }
1248
          }
1251
          line_free(clipboard->prev);
1249
          line_free(clipboard->prev);
1252
          db->totlines -= 1;
1250
          db->totlines -= 1;
1253
          uidirty.from = 0;
1251
          uidirty.from = 0;
1254
          uidirty.to = 0xff;
1252
          uidirty.to = 0xff;
1255
          recompute_curline(db);
1253
          recompute_curline(db);
1256
        }
1254
        }
1257
      }
1255
      }
1258
 
1256
 
1259
    } else if ((k == 0x016) && (clipboard != NULL)) { /* CTRL+V */
1257
    } else if ((k == 0x016) && (clipboard != NULL)) { /* CTRL+V */
1260
      if (line_add(db, clipboard->payload, clipboard->len) != 0) {
1258
      if (line_add(db, clipboard->payload, clipboard->len) != 0) {
1261
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1259
        ui_msg(svarlang_str(0, 10), NULL, SCHEME_ERR); /* ERROR */
1262
        mdr_bios_tickswait(18); /* 1s */
1260
        mdr_bios_tickswait(18); /* 1s */
1263
      } else {
1261
      } else {
1264
        /* rewire the linked list so the new line is on top of the previous one */
1262
        /* rewire the linked list so the new line is on top of the previous one */
1265
        clipboard->prev = db->cursor->prev;
1263
        clipboard->prev = db->cursor->prev;
1266
        /* remove prev node from list */
1264
        /* remove prev node from list */
1267
        db->cursor->prev = db->cursor->prev->prev;
1265
        db->cursor->prev = db->cursor->prev->prev;
1268
        if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor;
1266
        if (db->cursor->prev != NULL) db->cursor->prev->next = db->cursor;
1269
        /* insert the node after cursor now */
1267
        /* insert the node after cursor now */
1270
        clipboard->prev->next = db->cursor->next;
1268
        clipboard->prev->next = db->cursor->next;
1271
        if (db->cursor->next != NULL) db->cursor->next->prev = clipboard->prev;
1269
        if (db->cursor->next != NULL) db->cursor->next->prev = clipboard->prev;
1272
        clipboard->prev->prev = db->cursor;
1270
        clipboard->prev->prev = db->cursor;
1273
        db->cursor->next = clipboard->prev;
1271
        db->cursor->next = clipboard->prev;
1274
        cursor_down(db);
1272
        cursor_down(db);
1275
      }
1273
      }
1276
      uidirty.from = 0;
1274
      uidirty.from = 0;
1277
      uidirty.to = 0xff;
1275
      uidirty.to = 0xff;
1278
      recompute_curline(db);
1276
      recompute_curline(db);
1279
 
1277
 
1280
#ifdef DBG_UNHKEYS
1278
#ifdef DBG_UNHKEYS
1281
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1279
    } else { /* UNHANDLED KEY - TODO IGNORE THIS IN PRODUCTION RELEASE */
1282
      char buff[4];
1280
      char buff[4];
1283
      const char *HEX = "0123456789ABCDEF";
1281
      const char *HEX = "0123456789ABCDEF";
1284
      buff[0] = HEX[(k >> 8) & 15];
1282
      buff[0] = HEX[(k >> 8) & 15];
1285
      buff[1] = HEX[(k >> 4) & 15];
1283
      buff[1] = HEX[(k >> 4) & 15];
1286
      buff[2] = HEX[k & 15];
1284
      buff[2] = HEX[k & 15];
1287
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1285
      mdr_cout_str(screenh - 1, 0, "UNHANDLED KEY: 0x", SCHEME_STBAR1, 17);
1288
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1286
      mdr_cout_str(screenh - 1, 17, buff, SCHEME_STBAR1, 3);
1289
      mdr_dos_getkey2();
1287
      mdr_dos_getkey2();
1290
      break;
1288
      break;
1291
#endif
1289
#endif
1292
    }
1290
    }
1293
  }
1291
  }
1294
 
1292
 
1295
  mdr_cout_close();
1293
  mdr_cout_close();
1296
 
1294
 
1297
  /* restore the DOS BREAK flag if it was originally set */
1295
  /* restore the DOS BREAK flag if it was originally set */
1298
  if (original_breakflag != 0) mdr_dos_ctrlc_enable();
1296
  if (original_breakflag != 0) mdr_dos_ctrlc_enable();
1299
 
1297
 
1300
  /* no need to free memory, DOS will do it for me */
1298
  /* no need to free memory, DOS will do it for me */
1301
 
1299
 
1302
  return;
1300
  return;
1303
}
1301
}
1304
 
1302