Subversion Repositories SvarDOS

Rev

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

Rev 1354 Rev 1355
Line 75... Line 75...
75
  unsigned short cursorposy;
75
  unsigned short cursorposy;
76
  unsigned short totlines;
76
  unsigned short totlines;
77
  unsigned short curline;
77
  unsigned short curline;
78
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
78
  char lfonly;   /* set if line endings are LF (CR/LF otherwise) */
79
  char modflag;  /* non-zero if file has been modified since last save */
79
  char modflag;  /* non-zero if file has been modified since last save */
-
 
80
  char modflagprev;
80
  char fname[128];
81
  char fname[128];
81
};
82
};
82
 
83
 
83
 
84
 
84
/*****************************************************************************
85
/*****************************************************************************
Line 206... Line 207...
206
  while (db->cursor->prev) db->cursor = db->cursor->prev;
207
  while (db->cursor->prev) db->cursor = db->cursor->prev;
207
  db->curline = 0;
208
  db->curline = 0;
208
}
209
}
209
 
210
 
210
 
211
 
211
static void ui_basic(const struct file *db) {
212
static void ui_basic(const struct file *db, unsigned short slotnum) {
212
  const char *s = svarlang_strid(0); /* HELP */
213
  const char *s = svarlang_strid(0); /* HELP */
213
  unsigned short helpcol = screenw - strlen(s);
214
  unsigned short helpcol = screenw - strlen(s);
214
 
215
 
-
 
216
  /* slot number */
-
 
217
  {
-
 
218
    char slot[4] = "#00";
-
 
219
    slotnum++;
-
 
220
    slot[1] += (slotnum / 10);
-
 
221
    slot[2] += (slotnum % 10);
-
 
222
    mdr_cout_str(screenh - 1, 0, slot, SCHEME_STBAR2, 3);
-
 
223
  }
-
 
224
 
215
  /* fill status bar with background (without modflag as it is refreshed by ui_refresh) */
225
  /* fill rest of status bar with background */
216
  mdr_cout_char_rep(screenh - 1, 1, ' ', SCHEME_STBAR1, screenw - 1);
226
  mdr_cout_char_rep(screenh - 1, 3, ' ', SCHEME_STBAR1, screenw - 1);
217
 
227
 
218
  /* filename */
228
  /* filename */
219
  {
229
  {
220
    const char *fn = db->fname;
230
    const char *fn = db->fname;
-
 
231
    unsigned short x;
221
    if (*fn == 0) fn = svarlang_str(0, 1);
232
    if (*fn == 0) fn = svarlang_str(0, 1);
222
    mdr_cout_str(screenh - 1, 1, fn, SCHEME_STBAR1, screenw);
233
    x = mdr_cout_str(screenh - 1, 4, fn, SCHEME_STBAR1, screenw);
-
 
234
    if (db->modflag) mdr_cout_char(screenh - 1, 5 + x, '*', SCHEME_STBAR1);
223
  }
235
  }
224
 
236
 
225
  /* eol type */
237
  /* eol type */
226
  {
238
  {
227
    const char *eoltype = "CRLF";
239
    const char *eoltype = "CRLF";
Line 256... Line 268...
256
  if (uidirty.to < y+4) uidirty.to = y+4;
268
  if (uidirty.to < y+4) uidirty.to = y+4;
257
}
269
}
258
 
270
 
259
 
271
 
260
static unsigned char ui_confirm_if_unsaved(struct file *db) {
272
static unsigned char ui_confirm_if_unsaved(struct file *db) {
-
 
273
  unsigned char r = 0;
261
  if (db->modflag == 0) return(0);
274
  if (db->modflag == 0) return(0);
262
 
275
 
-
 
276
  mdr_cout_cursor_hide();
-
 
277
 
263
  /* if file has been modified then ask for confirmation */
278
  /* if file has been modified then ask for confirmation */
264
  ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
279
  ui_msg(svarlang_str(0,4), svarlang_str(0,5), SCHEME_MSG);
265
  if (keyb_getkey() == '\r') return(0);
280
  if (keyb_getkey() != '\r') r = 1;
266
 
281
 
-
 
282
  mdr_cout_cursor_show();
-
 
283
 
267
  return(1);
284
  return(r);
268
}
285
}
269
 
286
 
270
 
287
 
271
static void ui_refresh(const struct file *db) {
288
static void ui_refresh(const struct file *db) {
272
  unsigned char x;
289
  unsigned char x;
Line 315... Line 332...
315
    while ((y < screenh - 1) && (y < uidirty.to)) {
332
    while ((y < screenh - 1) && (y < uidirty.to)) {
316
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenw - 1);
333
      mdr_cout_char_rep(y++, 0, ' ', SCHEME_TEXT, screenw - 1);
317
    }
334
    }
318
  }
335
  }
319
 
336
 
320
  /* "file changed" flag */
-
 
321
  mdr_cout_char(screenh - 1, 0, db->modflag, SCHEME_STBAR1);
-
 
322
 
-
 
323
  /* scroll bar */
337
  /* scroll bar */
324
  for (y = 0; y < (screenh - 1); y++) {
338
  for (y = 0; y < (screenh - 1); y++) {
325
    mdr_cout_char(y, screenw - 1, SCROLL_CURSOR, SCHEME_SCROLL);
339
    mdr_cout_char(y, screenw - 1, SCROLL_CURSOR, SCHEME_SCROLL);
326
  }
340
  }
327
 
341
 
Line 517... Line 531...
517
  char *buffptr;
531
  char *buffptr;
518
  unsigned int len, llen;
532
  unsigned int len, llen;
519
  int fd;
533
  int fd;
520
  unsigned char eolfound;
534
  unsigned char eolfound;
521
 
535
 
-
 
536
  /* free the entire linked list of lines */
522
  bzero(db, sizeof(db));
537
  db_rewind(db);
-
 
538
  while (db->cursor) {
-
 
539
    struct line far *victim;
-
 
540
    victim = db->cursor;
-
 
541
    db->cursor = db->cursor->next;
-
 
542
    line_free(victim);
-
 
543
  }
-
 
544
 
-
 
545
  /* zero out the struct */
523
  memcpy(db->fname, fname, strlen(fname));
546
  bzero(db, sizeof(struct file));
524
 
547
 
525
  if (*fname == 0) goto SKIPLOADING;
548
  if (*fname == 0) goto SKIPLOADING;
526
 
549
 
-
 
550
  memcpy(db->fname, fname, strlen(fname));
-
 
551
 
527
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
552
  if (_dos_open(fname, O_RDONLY, &fd) != 0) {
528
    return(1);
553
    return(1);
529
  }
554
  }
530
 
555
 
531
  db->lfonly = 1;
556
  db->lfonly = 1;
Line 664... Line 689...
664
    }
689
    }
665
  }
690
  }
666
}
691
}
667
 
692
 
668
 
693
 
669
static void clear_file(struct file *db) {
-
 
670
 
-
 
671
  /* free the entire linked list of lines */
-
 
672
  db_rewind(db);
-
 
673
  while (db->cursor) {
-
 
674
    struct line far *victim;
-
 
675
    victim = db->cursor;
-
 
676
    db->cursor = db->cursor->next;
-
 
677
    line_free(victim);
-
 
678
  }
-
 
679
 
-
 
680
  /* zero out the struct */
-
 
681
  bzero(db, sizeof(struct file));
-
 
682
}
-
 
683
 
-
 
684
 
-
 
685
/* recompute db->curline by counting nodes in linked list */
694
/* recompute db->curline by counting nodes in linked list */
686
static void recompute_curline(struct file *db) {
695
static void recompute_curline(struct file *db) {
687
  const struct line far *l = db->cursor;
696
  const struct line far *l = db->cursor;
688
 
697
 
689
  db->curline = 0;
698
  db->curline = 0;
Line 694... Line 703...
694
}
703
}
695
 
704
 
696
 
705
 
697
enum MENU_ACTION {
706
enum MENU_ACTION {
698
  MENU_NONE = 0,
707
  MENU_NONE = 0,
699
  MENU_NEW = 1,
708
  MENU_OPEN = 1,
700
  MENU_OPEN = 2,
709
  MENU_SAVE = 2,
701
  MENU_SAVE = 3,
710
  MENU_SAVEAS = 3,
702
  MENU_SAVEAS = 4,
711
  MENU_CLOSE = 4,
703
  MENU_CHGEOL = 5,
712
  MENU_CHGEOL = 5,
704
  MENU_QUIT = 6
713
  MENU_QUIT = 6
705
};
714
};
706
 
715
 
707
static enum MENU_ACTION ui_menu(void) {
716
static enum MENU_ACTION ui_menu(void) {
Line 710... Line 719...
710
  uidirty.to = 0xff;
719
  uidirty.to = 0xff;
711
  uidirty.statusbar = 1;
720
  uidirty.statusbar = 1;
712
 
721
 
713
  /* find out the longest string */
722
  /* find out the longest string */
714
  slen = 0;
723
  slen = 0;
715
  for (i = MENU_NEW; i <= MENU_QUIT; i++) {
724
  for (i = MENU_OPEN; i <= MENU_QUIT; i++) {
716
    x = strlen(svarlang_str(8, i));
725
    x = strlen(svarlang_str(8, i));
717
    if (x > slen) slen = x;
726
    if (x > slen) slen = x;
718
  }
727
  }
719
 
728
 
720
  curchoice = MENU_NEW;
729
  curchoice = MENU_OPEN;
721
  for (;;) {
730
  for (;;) {
722
    /* render menu */
731
    /* render menu */
723
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
732
    for (i = MENU_NONE; i <= MENU_QUIT + 1; i++) {
724
      mdr_cout_char_rep(i, 0, ' ', SCHEME_MENU, slen+4);
733
      mdr_cout_char_rep(i, 0, ' ', SCHEME_MENU, slen+4);
725
      if (i == curchoice) {
734
      if (i == curchoice) {
Line 737... Line 746...
737
    /* wait for key */
746
    /* wait for key */
738
    switch (keyb_getkey()) {
747
    switch (keyb_getkey()) {
739
      case '\r': return(curchoice); /* ENTER */
748
      case '\r': return(curchoice); /* ENTER */
740
      case 0x150: /* down */
749
      case 0x150: /* down */
741
        if (curchoice == MENU_QUIT) {
750
        if (curchoice == MENU_QUIT) {
742
          curchoice = MENU_NEW;
751
          curchoice = MENU_OPEN;
743
        } else {
752
        } else {
744
          curchoice++;
753
          curchoice++;
745
        }
754
        }
746
        break;
755
        break;
747
      case 0x148: /* up */
756
      case 0x148: /* up */
748
        if (curchoice == MENU_NEW) {
757
        if (curchoice == MENU_OPEN) {
749
          curchoice = MENU_QUIT;
758
          curchoice = MENU_QUIT;
750
        } else {
759
        } else {
751
          curchoice--;
760
          curchoice--;
752
        }
761
        }
753
        break;
762
        break;
Line 755... Line 764...
755
    }
764
    }
756
  }
765
  }
757
}
766
}
758
 
767
 
759
 
768
 
-
 
769
static struct file *select_slot(struct file *dbarr, unsigned short curfile) {
-
 
770
  uidirty.from = 0;
-
 
771
  uidirty.to = 0xff;
-
 
772
  uidirty.statusbar = 1;
-
 
773
 
-
 
774
  dbarr = &(dbarr[curfile]);
-
 
775
  ui_basic(dbarr, curfile);
-
 
776
  ui_refresh(dbarr);
-
 
777
  return(dbarr);
-
 
778
}
-
 
779
 
-
 
780
 
760
/* main returns nothing, ie. sved always exits with a zero exit code
781
/* main returns nothing, ie. sved always exits with a zero exit code
761
 * (this saves 20 bytes of executable footprint) */
782
 * (this saves 20 bytes of executable footprint) */
762
void main(void) {
783
void main(void) {
763
  static struct file dbarr[12];
784
  static struct file dbarr[10];
764
  const char *fname;
785
  const char *fname;
765
  struct file *db = dbarr;
786
  unsigned short curfile;
-
 
787
  struct file *db = dbarr; /* visible file is the first slot by default */
766
 
788
 
767
  {
789
  {
768
    char nlspath[128], lang[8];
790
    char nlspath[128], lang[8];
769
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
791
    svarlang_autoload_pathlist("sved", mdr_dos_getenv(nlspath, "NLSPATH", sizeof(nlspath)), mdr_dos_getenv(lang, "LANG", sizeof(lang)));
770
  }
792
  }
Line 774... Line 796...
774
  if ((fname == NULL) || (*fname == '/')) {
796
  if ((fname == NULL) || (*fname == '/')) {
775
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
797
    mdr_coutraw_puts(svarlang_str(1,0)); /* usage: sved file.txt */
776
    return;
798
    return;
777
  }
799
  }
778
 
800
 
779
  /* load file */
801
  /* preload all slots with empty files */
-
 
802
  for (curfile = 9;; curfile--) {
-
 
803
    loadfile(&(dbarr[curfile]), "");
-
 
804
    if (curfile == 0) break;
780
  {
805
  }
-
 
806
 
-
 
807
  /* load file, if any given */
-
 
808
  if (*fname != 0) {
781
    unsigned char err = loadfile(db, fname);
809
    unsigned char err = loadfile(db, fname);
782
    if (err == 1) {
810
    if (err == 1) {
783
      mdr_coutraw_puts(svarlang_str(0,11)); /* file not found */
811
      mdr_coutraw_puts(svarlang_str(0,11)); /* file not found */
784
      return;
812
      return;
785
    } else if (err != 0) {
813
    } else if (err != 0) {
Line 810... Line 838...
810
 
838
 
811
    if (uidirty.from != 0xff) {
839
    if (uidirty.from != 0xff) {
812
      ui_refresh(db);
840
      ui_refresh(db);
813
      uidirty.from = 0xff;
841
      uidirty.from = 0xff;
814
    }
842
    }
815
    if (uidirty.statusbar) {
843
    if ((uidirty.statusbar != 0) || (db->modflagprev != db->modflag)) {
816
      ui_basic(db);
844
      ui_basic(db, curfile);
817
      uidirty.statusbar = 0;
845
      uidirty.statusbar = 0;
-
 
846
      db->modflagprev = db->modflag;
818
    }
847
    }
819
#ifdef DBG_LINENUM
848
#ifdef DBG_LINENUM
820
      {
849
      {
821
        char ddd[10];
850
        char ddd[10];
822
        db->curline += 1;
851
        db->curline += 1;
Line 896... Line 925...
896
      switch (ui_menu()) {
925
      switch (ui_menu()) {
897
 
926
 
898
        case MENU_NONE:
927
        case MENU_NONE:
899
          break;
928
          break;
900
 
929
 
901
        case MENU_NEW:
-
 
902
          if (ui_confirm_if_unsaved(db) == 0) {
-
 
903
            clear_file(db);
-
 
904
            /* add a single empty line */
-
 
905
            line_add(db, NULL, 0);
-
 
906
          }
-
 
907
          uidirty.statusbar = 1;
-
 
908
          break;
-
 
909
 
-
 
910
        case MENU_OPEN:
930
        case MENU_OPEN:
911
          /* display a warning if unsaved changes are pending */
931
          /* display a warning if unsaved changes are pending */
912
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
932
          if (db->modflag != 0) ui_msg(svarlang_str(0,4), svarlang_str(0,8), SCHEME_MSG);
913
 
933
 
914
          /* ask for filename */
934
          /* ask for filename */
915
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
935
          ui_getstring(svarlang_str(0,7), fname, sizeof(fname));
916
          if (fname[0] != 0) {
936
          if (fname[0] != 0) {
917
            unsigned char err;
937
            unsigned char err;
918
            clear_file(db);
-
 
919
            err = loadfile(db, fname);
938
            err = loadfile(db, fname);
920
            if (err != 0) {
939
            if (err != 0) {
921
              if (err == 1) {
940
              if (err == 1) {
922
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
941
                ui_msg(svarlang_str(0,11), NULL, SCHEME_ERR); /* file not found */
923
              } else {
942
              } else {
924
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
943
                ui_msg(svarlang_str(0,10), NULL, SCHEME_ERR);  /* ERROR */
925
              }
944
              }
926
              mdr_bios_tickswait(44); /* 3s */
945
              mdr_bios_tickswait(44); /* 3s */
927
              clear_file(db);
946
              loadfile(db, "");
928
            }
947
            }
929
          }
948
          }
930
          break;
949
          break;
931
 
950
 
932
        case MENU_SAVEAS:
951
        case MENU_SAVEAS:
Line 950... Line 969...
950
            ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
969
            ui_msg(svarlang_str(0, 3), NULL, SCHEME_ERR);
951
            mdr_bios_tickswait(36); /* 2s */
970
            mdr_bios_tickswait(36); /* 2s */
952
          }
971
          }
953
          break;
972
          break;
954
 
973
 
-
 
974
        case MENU_CLOSE:
-
 
975
          if (ui_confirm_if_unsaved(db) == 0) {
-
 
976
            loadfile(db, "");
-
 
977
          }
-
 
978
          uidirty.from = 0;
-
 
979
          uidirty.to = 0xff;
-
 
980
          uidirty.statusbar = 1;
-
 
981
          break;
-
 
982
 
955
        case MENU_CHGEOL:
983
        case MENU_CHGEOL:
956
          db->modflag = '*';
984
          db->modflag = '*';
957
          db->lfonly ^= 1;
985
          db->lfonly ^= 1;
958
          break;
986
          break;
959
 
987
 
960
        case MENU_QUIT:
988
        case MENU_QUIT:
-
 
989
          quitnow = 1;
-
 
990
          for (curfile = 0; curfile < 10; curfile++) {
-
 
991
            if (dbarr[curfile].modflag) {
-
 
992
              db = select_slot(dbarr, curfile);
961
          if (ui_confirm_if_unsaved(db) == 0) quitnow = 1;
993
              if (ui_confirm_if_unsaved(db) != 0) quitnow = 0;
-
 
994
            }
-
 
995
          }
962
          break;
996
          break;
963
      }
997
      }
964
 
998
 
965
      if (quitnow) break;
999
      if (quitnow) break;
966
 
1000
 
Line 993... Line 1027...
993
      insert_in_line(db, &c, 1);
1027
      insert_in_line(db, &c, 1);
994
 
1028
 
995
    } else if (k == 0x009) { /* TAB */
1029
    } else if (k == 0x009) { /* TAB */
996
      insert_in_line(db, "        ", 8);
1030
      insert_in_line(db, "        ", 8);
997
 
1031
 
998
    } else if ((k >= 0x13b) && (k <= 0x146)) { /* F1..F12 */
1032
    } else if ((k >= 0x13b) && (k <= 0x144)) { /* F1..F10 */
999
      uidirty.from = 0;
1033
      curfile = k - 0x13b;
1000
      uidirty.to = 0xff;
1034
      db = select_slot(dbarr, curfile);
1001
 
1035
 
1002
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1036
    } else if (k == 0x174) { /* CTRL+ArrRight - jump to next word */
1003
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1037
      /* if currently cursor is on a non-space, then fast-forward to nearest space or EOL */
1004
      for (;;) {
1038
      for (;;) {
1005
        if (db->xoffset + db->cursorposx == db->cursor->len) break;
1039
        if (db->xoffset + db->cursorposx == db->cursor->len) break;