Subversion Repositories SvarDOS

Rev

Rev 1314 | Rev 1316 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1314 Rev 1315
Line 51... Line 51...
51
  struct line far *next;
51
  struct line far *next;
52
  unsigned short len;
52
  unsigned short len;
53
  char payload[1];
53
  char payload[1];
54
};
54
};
55
 
55
 
56
struct linedb {
56
struct file {
57
  struct line far *cursor;
57
  struct line far *cursor;
58
  unsigned short xoffset;
58
  unsigned short xoffset;
59
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
59
  char lfonly; /* set if line endings are LF (CR/LF otherwise) */
60
};
60
};
61
 
61
 
62
 
62
 
63
/* returns non-zero on error */
63
/* returns non-zero on error */
64
static int line_add(struct linedb *db, const char far *line) {
64
static int line_add(struct file *db, const char far *line) {
65
  unsigned short slen;
65
  unsigned short slen;
66
  struct line far *l;
66
  struct line far *l;
67
 
67
 
68
  /* slen = strlen(line) (but for far pointer) */
68
  /* slen = strlen(line) (but for far pointer) */
69
  for (slen = 0; line[slen] != 0; slen++);
69
  for (slen = 0; line[slen] != 0; slen++);
Line 90... Line 90...
90
 
90
 
91
  return(0);
91
  return(0);
92
}
92
}
93
 
93
 
94
 
94
 
95
static void db_rewind(struct linedb *db) {
95
static void db_rewind(struct file *db) {
96
  if (db->cursor == NULL) return;
96
  if (db->cursor == NULL) return;
97
  while (db->cursor->prev) db->cursor = db->cursor->prev;
97
  while (db->cursor->prev) db->cursor = db->cursor->prev;
98
}
98
}
99
 
99
 
100
 
100
 
Line 104... Line 104...
104
  scheme[COL_STATUSBAR2] = 0x78;
104
  scheme[COL_STATUSBAR2] = 0x78;
105
  scheme[COL_SCROLLBAR] = 0x70;
105
  scheme[COL_SCROLLBAR] = 0x70;
106
}
106
}
107
 
107
 
108
 
108
 
109
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname, const struct linedb *db) {
109
static void ui_basic(unsigned char screenw, unsigned char screenh, const char *fname, const struct file *db) {
110
  unsigned char i;
110
  unsigned char i;
111
  const char *s = svarlang_strid(0); /* HELP */
111
  const char *s = svarlang_strid(0); /* HELP */
112
  unsigned char helpcol = screenw - (strlen(s) + 4);
112
  unsigned char helpcol = screenw - (strlen(s) + 4);
113
 
113
 
114
  /* fill status bar with background */
114
  /* fill status bar with background */
Line 167... Line 167...
167
  mdr_cout_cursor_show();
167
  mdr_cout_cursor_show();
168
#undef MAXLINLEN
168
#undef MAXLINLEN
169
}
169
}
170
 
170
 
171
 
171
 
172
static void ui_refresh(const struct linedb *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto, unsigned char y) {
172
static void ui_refresh(const struct file *db, unsigned char screenw, unsigned char screenh, unsigned char uidirtyfrom, unsigned char uidirtyto, unsigned char y) {
173
  unsigned char len;
173
  unsigned char len;
174
  const struct line far *l;
174
  const struct line far *l;
175
 
175
 
176
#ifdef DBG_REFRESH
176
#ifdef DBG_REFRESH
177
  static char m = 'a';
177
  static char m = 'a';
Line 204... Line 204...
204
  }
204
  }
205
 
205
 
206
}
206
}
207
 
207
 
208
 
208
 
209
static void check_cursor_not_after_eol(struct linedb *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
209
static void check_cursor_not_after_eol(struct file *db, unsigned char *cursorpos, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
210
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
210
  if (db->xoffset + *cursorpos <= db->cursor->len) return;
211
 
211
 
212
  if (db->cursor->len < db->xoffset) {
212
  if (db->cursor->len < db->xoffset) {
213
    *cursorpos = 0;
213
    *cursorpos = 0;
214
    db->xoffset = db->cursor->len;
214
    db->xoffset = db->cursor->len;
Line 218... Line 218...
218
    *cursorpos = db->cursor->len - db->xoffset;
218
    *cursorpos = db->cursor->len - db->xoffset;
219
  }
219
  }
220
}
220
}
221
 
221
 
222
 
222
 
223
static void cursor_up(struct linedb *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
223
static void cursor_up(struct file *db, unsigned char *cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
224
  if (db->cursor->prev != NULL) {
224
  if (db->cursor->prev != NULL) {
225
    db->cursor = db->cursor->prev;
225
    db->cursor = db->cursor->prev;
226
    if (*cursorposy == 0) {
226
    if (*cursorposy == 0) {
227
      *uidirtyfrom = 0;
227
      *uidirtyfrom = 0;
228
      *uidirtyto = 0xff;
228
      *uidirtyto = 0xff;
Line 231... Line 231...
231
    }
231
    }
232
  }
232
  }
233
}
233
}
234
 
234
 
235
 
235
 
236
static void cursor_eol(struct linedb *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
236
static void cursor_eol(struct file *db, unsigned char *cursorposx, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
237
  /* adjust xoffset to make sure eol is visible on screen */
237
  /* adjust xoffset to make sure eol is visible on screen */
238
  if (db->xoffset > db->cursor->len) {
238
  if (db->xoffset > db->cursor->len) {
239
    db->xoffset = db->cursor->len - 1;
239
    db->xoffset = db->cursor->len - 1;
240
    *uidirtyfrom = 0;
240
    *uidirtyfrom = 0;
241
    *uidirtyto = 0xff;
241
    *uidirtyto = 0xff;
Line 248... Line 248...
248
  }
248
  }
249
  *cursorposx = db->cursor->len - db->xoffset;
249
  *cursorposx = db->cursor->len - db->xoffset;
250
}
250
}
251
 
251
 
252
 
252
 
253
static void cursor_down(struct linedb *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
253
static void cursor_down(struct file *db, unsigned char *cursorposy, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
254
  if (db->cursor->next != NULL) {
254
  if (db->cursor->next != NULL) {
255
    db->cursor = db->cursor->next;
255
    db->cursor = db->cursor->next;
256
    if (*cursorposy < screenh - 2) {
256
    if (*cursorposy < screenh - 2) {
257
      *cursorposy += 1;
257
      *cursorposy += 1;
258
    } else {
258
    } else {
Line 261... Line 261...
261
    }
261
    }
262
  }
262
  }
263
}
263
}
264
 
264
 
265
 
265
 
266
static void cursor_left(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
266
static void cursor_left(struct file *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
267
  if (*cursorposx > 0) {
267
  if (*cursorposx > 0) {
268
    *cursorposx -= 1;
268
    *cursorposx -= 1;
269
  } else if (db->xoffset > 0) {
269
  } else if (db->xoffset > 0) {
270
    db->xoffset -= 1;
270
    db->xoffset -= 1;
271
    *uidirtyfrom = 0;
271
    *uidirtyfrom = 0;
Line 275... Line 275...
275
    cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
275
    cursor_eol(db, cursorposx, screenw, uidirtyfrom, uidirtyto);
276
  }
276
  }
277
}
277
}
278
 
278
 
279
 
279
 
280
static void cursor_home(struct linedb *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
280
static void cursor_home(struct file *db, unsigned char *cursorposx, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
281
  *cursorposx = 0;
281
  *cursorposx = 0;
282
  if (db->xoffset != 0) {
282
  if (db->xoffset != 0) {
283
    db->xoffset = 0;
283
    db->xoffset = 0;
284
    *uidirtyfrom = 0;
284
    *uidirtyfrom = 0;
285
    *uidirtyto = 0xff;
285
    *uidirtyto = 0xff;
286
  }
286
  }
287
}
287
}
288
 
288
 
289
 
289
 
290
static void cursor_right(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
290
static void cursor_right(struct file *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char screenh, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
291
  if (db->cursor->len > db->xoffset + *cursorposx) {
291
  if (db->cursor->len > db->xoffset + *cursorposx) {
292
    if (*cursorposx < screenw - 2) {
292
    if (*cursorposx < screenw - 2) {
293
      *cursorposx += 1;
293
      *cursorposx += 1;
294
    } else {
294
    } else {
295
      db->xoffset += 1;
295
      db->xoffset += 1;
Line 301... Line 301...
301
    cursor_home(db, cursorposx, uidirtyfrom, uidirtyto);
301
    cursor_home(db, cursorposx, uidirtyfrom, uidirtyto);
302
  }
302
  }
303
}
303
}
304
 
304
 
305
 
305
 
306
static void del(struct linedb *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
306
static void del(struct file *db, unsigned char cursorposx, unsigned char cursorposy, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
307
  if (cursorposx + db->xoffset < db->cursor->len) {
307
  if (cursorposx + db->xoffset < db->cursor->len) {
308
    _fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
308
    _fmemmove(db->cursor->payload + cursorposx + db->xoffset, db->cursor->payload + cursorposx + db->xoffset + 1, db->cursor->len - cursorposx - db->xoffset);
309
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
309
    db->cursor->len -= 1; /* do this AFTER memmove so the copy includes the nul terminator */
310
    *uidirtyfrom = cursorposy;
310
    *uidirtyfrom = cursorposy;
311
    *uidirtyto = cursorposy;
311
    *uidirtyto = cursorposy;
Line 327... Line 327...
327
    *uidirtyto = 0xff;
327
    *uidirtyto = 0xff;
328
  }
328
  }
329
}
329
}
330
 
330
 
331
 
331
 
332
static void bkspc(struct linedb *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
332
static void bkspc(struct file *db, unsigned char *cursorposx, unsigned char *cursorposy, unsigned char screenw, unsigned char *uidirtyfrom, unsigned char *uidirtyto) {
333
 
333
 
334
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
334
  /* backspace is basically "left + del", not applicable only if cursor is on 1st byte of the file */
335
  if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
335
  if ((*cursorposx == 0) && (db->xoffset == 0) && (db->cursor->prev == NULL)) return;
336
 
336
 
337
  cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
337
  cursor_left(db, cursorposx, cursorposy, screenw, uidirtyfrom, uidirtyto);
Line 369... Line 369...
369
 
369
 
370
  return(argv[0]);
370
  return(argv[0]);
371
}
371
}
372
 
372
 
373
 
373
 
374
static int loadfile(struct linedb *db, const char *fname) {
374
static int loadfile(struct file *db, const char *fname) {
375
  char buff[1024];
375
  char buff[1024];
376
  unsigned int prevlen = 0, len, llen;
376
  unsigned int prevlen = 0, len, llen;
377
  int fd;
377
  int fd;
378
  int r = 0;
378
  int r = 0;
379
 
379
 
Line 416... Line 416...
416
 
416
 
417
  return(r);
417
  return(r);
418
}
418
}
419
 
419
 
420
 
420
 
421
static int savefile(const struct linedb *db, const char *fname) {
421
static int savefile(const struct file *db, const char *fname) {
422
  int fd;
422
  int fd;
423
  const struct line far *l;
423
  const struct line far *l;
424
  unsigned bytes;
424
  unsigned bytes;
425
  unsigned char eollen;
425
  unsigned char eollen;
426
  unsigned char eolbuf[2];
426
  unsigned char eolbuf[2];
Line 453... Line 453...
453
}
453
}
454
 
454
 
455
 
455
 
456
int main(void) {
456
int main(void) {
457
  const char *fname;
457
  const char *fname;
458
  struct linedb db;
458
  struct file db;
459
  unsigned char screenw = 0, screenh = 0;
459
  unsigned char screenw = 0, screenh = 0;
460
  unsigned char cursorposx = 0, cursorposy = 0;
460
  unsigned char cursorposx = 0, cursorposy = 0;
461
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
461
  unsigned char uidirtyfrom = 0, uidirtyto = 0xff; /* make sure to redraw entire UI at first run */
462
 
462
 
463
  bzero(&db, sizeof(db));
463
  bzero(&db, sizeof(db));