Subversion Repositories SvarDOS

Rev

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

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