Subversion Repositories SvarDOS

Rev

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

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