<commit_before>{ minneron
----------------------------------------------------------------
Copyright (c) 2012 <NAME>. All rights reserved.
----------------------------------------------------------------
Initial version forked from cedit.pas
---------------------------------------------------------------}
{$i xpc.inc}
program mn;
uses ll, li, fs, stri, num, cw, crt, buf, ui, kbd, cli;

  type
    editor = class
      buf : buf.buffer;
      filename : string;
      x, y, h, w : integer;
      topline, position : buf.buffer.cursor;
      led : ui.zinput;  // led = Line EDitor
      constructor create;
      function load( path : string ) : boolean;
      function save_as( path : string ) : boolean;
      function save : boolean;
      procedure show;
      procedure run;

     protected { cursor movement commands }
      procedure arrowup;
      procedure arrowdown;
      procedure home;
      procedure _end;
      procedure pageup;
      procedure pagedown;

     protected { line manipulation commands }
      procedure newline;
      procedure delete;
    end;


  constructor editor.create;
  begin
    inherited;
    x := 1;
    y := 1;
    w := crt.windMaxX;
    h := crt.windMaxY;
    self.buf := buffer.create;
    topline := self.buf.make_cursor;
    position := self.buf.make_cursor;
    filename := '';
  end;


  function editor.load( path : string ) : boolean;
    var txt : text; line : string;
  begin
    result := fs.exists( path );
    if result then begin
      //  need to check for io errors in here
      assign( txt, path );
      reset( txt );
      while not eof( txt ) do begin
	readln( txt, line );
	self.buf.append( stringtoken.create( line ));
      end;
      close( txt );
      self.filename := path;
    end;
  end; { editor.load }

  function editor.save : boolean;
    var txt: text; node : li.node;
  begin
    assign( txt, self.filename );
    rewrite( txt );
    for node in self.buf do begin
      if node.kind = KSTR then writeln( txt, (node as strnode).str );
    end;
    close( txt );
    result := true; // TODO error checking
  end;

  function editor.save_as( path : string ) : boolean;
    var oldname : string;
  begin
    oldname := self.filename;
    self.filename := path;
    result := self.save;
    if not result then self.filename := oldname
  end;


  procedure editor.show;
    var
      ypos : cardinal;
      cur  : buffer.cursor;

    procedure show_curpos;
    begin
      cwritexy( 1, 1,
                '|B[|C' + flushrt( n2s( self.position.index ), 6, '.' ) +
                '|w/|c' + flushrt( n2s( self.buf.count ), 6, '.' ) +
		'|B]' +
                '|%' );
    end;

    procedure show_nums;
    begin
      cwritexy( 1, ypos, '|Y|!m' );
      write( flushrt( n2s( cur.index ), 3, ' ' ));
      cwrite( '|!k|w' );
    end;

    procedure show_edit( line : string );
    begin
      { This simply positions the input widget. }
      with self.led do begin
	x := crt.wherex;
	y := crt.wherey;
	tcol := $0E; // bright yellow
	dlen := crt.windmaxx - crt.wherex;
      end;
      // debug: clear to eol w/blue bg to show where control should be
      // cwrite( '|!b|%' );
      led.show;
    end;

    procedure show_line( line : string );
    begin
      cwrite( stri.trunc( line, cw.scr.w - cw.cur.x ));
      cwrite( '|%' ); // clreol
    end;

  var line : string = '';

  begin
    // clrscr; //  fillbox( 1, 1, crt.windmaxx, crt.windmaxy, $0F20 );
    show_curpos;
    ypos := 2;
    cur := self.buf.make_cursor;
    cur.move_to( self.topline );
    repeat
      if cur.value.inheritsfrom( li.strnode ) then
      begin
	show_nums;
	line := li.strnode( cur.value ).str;
	if cur.index = position.index then show_edit( line )
	else show_line( line );
      end;
      inc( ypos )
    until ( ypos = self.h ) or ( not cur.move_next );
    while ypos < self.h do begin
      cwritexy( 1, ypos, '|%' );
      inc( ypos )
    end
  end;



  procedure editor.home;
  begin
    if self.buf.first = nil then exit;
    position.to_top;
    topline.to_top;
    if position.value.inheritsfrom( li.strnode ) then
      led.work := li.strnode( position.value ).str
    else
      led.work := '<<marker>>';
  end;

  procedure editor._end;
    var i : byte;
  begin
    self.position.to_end;
    self.topline.to_end;
    for i := crt.windmaxy div 2 downto 1 do
      self.topline.move_prev;
  end;

  procedure editor.pageup;
    var c : byte;
  begin for c := 1 to h do arrowup;
  end;

  procedure editor.pagedown;
    var c : byte;
  begin for c := 1 to h do arrowdown;
  end; { editor.pagedown }



  procedure editor.run;
    var done : boolean = false; ch : char;
  begin
    self.led := ui.zinput.create;
    self.home;
    repeat
      show; led.show;
      case kbd.readkey(ch) of
	^C, #27	: done := true;
	^N	: arrowdown;
	^P	: arrowup;
	^M	: newline;
	^D	: delete;
	^S	: save;
         ^V : pagedown;
         ^U : pageup;
	#0      : case kbd.readkey(ch) of<commit_msg>unbind escape key, show messages at top of screen, tweak colors<commit_after>{ minneron
----------------------------------------------------------------
Copyright (c) 2012 <NAME>. All rights reserved.
----------------------------------------------------------------
Initial version forked from cedit.pas
---------------------------------------------------------------}
{$i xpc.inc}
program mn;
uses ll, li, fs, stri, num, cw, crt, buf, ui, kbd, cli;

  type
    editor = class
      buf : buf.buffer;
      filename : string;
      message  : string;
      x, y, h, w : integer;
      topline, position : buf.buffer.cursor;
      led : ui.zinput;  // led = Line EDitor
      constructor create;
      function load( path : string ) : boolean;
      function save_as( path : string ) : boolean;
      function save : boolean;
      procedure show;
      procedure run;

     protected { cursor movement commands }
      procedure arrowup;
      procedure arrowdown;
      procedure home;
      procedure _end;
      procedure pageup;
      procedure pagedown;

     protected { line manipulation commands }
      procedure newline;
      procedure delete;
    end;


  constructor editor.create;
  begin
    inherited;
    x := 1;
    y := 1;
    w := crt.windMaxX;
    h := crt.windMaxY;
    self.buf := buffer.create;
    topline := self.buf.make_cursor;
    position := self.buf.make_cursor;
    filename := '';
    message  := 'welcome to minneron.';
  end;


  function editor.load( path : string ) : boolean;
    var txt : text; line : string;
  begin
    result := fs.exists( path );
    if result then begin
      //  need to check for io errors in here
      assign( txt, path );
      reset( txt );
      while not eof( txt ) do begin
	readln( txt, line );
	self.buf.append( stringtoken.create( line ));
      end;
      close( txt );
      self.filename := path;
    end;
  end; { editor.load }

  function editor.save : boolean;
    var txt: text; node : li.node;
  begin
    assign( txt, self.filename );
    rewrite( txt );
    for node in self.buf do begin
      if node.kind = KSTR then writeln( txt, (node as strnode).str );
    end;
    close( txt );
    result := true; // TODO error checking
    message := filename + ' saved.';
  end;

  function editor.save_as( path : string ) : boolean;
    var oldname : string;
  begin
    oldname := self.filename;
    self.filename := path;
    result := self.save;
    if not result then self.filename := oldname
  end;


  procedure editor.show;
    var
      ypos : cardinal;
      cur  : buffer.cursor;

    procedure show_curpos;
    begin
      cwritexy( 1, 1,
                '|B[|C' + flushrt( n2s( self.position.index ), 6, '.' ) +
                '|w/|c' + flushrt( n2s( self.buf.count ), 6, '.' ) +
                '|B] |G' + self.message +
             '|%' );
      self.message := '';
    end;

    procedure show_nums;
    begin
      cwritexy( 1, ypos, '|k|!c' );
      write( flushrt( n2s( cur.index ), 3, ' ' ));
      cwrite( '|!k|w' );
    end;

    procedure show_edit( line : string );
    begin
      { This simply positions the input widget. }
      with self.led do begin
	x := crt.wherex;
	y := crt.wherey;
	tcol := $0E; // bright yellow
	dlen := crt.windmaxx - crt.wherex;
      end;
      // debug: clear to eol w/blue bg to show where control should be
      // cwrite( '|!b|%' );
      led.show;
    end;

    procedure show_line( line : string );
    begin
      cwrite( stri.trunc( line, cw.scr.w - cw.cur.x ));
      cwrite( '|%' ); // clreol
    end;

  var line : string = '';

  begin
    // clrscr; //  fillbox( 1, 1, crt.windmaxx, crt.windmaxy, $0F20 );
    show_curpos;
    ypos := 2;
    cur := self.buf.make_cursor;
    cur.move_to( self.topline );
    repeat
      if cur.value.inheritsfrom( li.strnode ) then
      begin
	show_nums;
	line := li.strnode( cur.value ).str;
	if cur.index = position.index then show_edit( line )
	else show_line( line );
      end;
      inc( ypos )
    until ( ypos = self.h ) or ( not cur.move_next );
    while ypos < self.h do begin
      cwritexy( 1, ypos, '|%' );
      inc( ypos )
    end
  end;



  procedure editor.home;
  begin
    if self.buf.first = nil then exit;
    position.to_top;
    topline.to_top;
    if position.value.inheritsfrom( li.strnode ) then
      led.work := li.strnode( position.value ).str
    else
      led.work := '<<marker>>';
  end;

  procedure editor._end;
    var i : byte;
  begin
    self.position.to_end;
    self.topline.to_end;
    for i := crt.windmaxy div 2 downto 1 do
      self.topline.move_prev;
  end;

  procedure editor.pageup;
    var c : byte;
  begin for c := 1 to h do arrowup;
  end;

  procedure editor.pagedown;
    var c : byte;
  begin for c := 1 to h do arrowdown;
  end; { editor.pagedown }



  procedure editor.run;
    var done : boolean = false; ch : char;
  begin
    self.led := ui.zinput.create;
    self.home;
      repeat
      show; led.show;
      case kbd.readkey(ch) of
         ^C : done := true;
	^N	: arrowdown;
	^P	: arrowup;
	^M	: newline;
	^D	: delete;
	^S	: save;
         ^V : pagedown;
         ^U : pageup;
	#0      : case kbd.readkey(ch) of
{'fraction_non_alphanumeric': 0.15869565217391304, 'fraction_numerical': 0.005731225296442688, 'mean_word_length': 1.8744674808293098, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3235589', 'n_tokens_mistral': 3481, 'n_tokens_neox': 3366, 'n_words': 998}