Subversion Repositories SvarDOS

Rev

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

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