Subversion Repositories SvarDOS

Rev

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

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