
\- SETX  : SETX ( x -- )  GETXY NIP SETXY ;

[IFNDEF] MD
\+ MAKE-DIR : MD PARSE-NAME MAKE-DIR ; 
[THEN]

[IFNDEF] RM
\+ DELETE-FILE : RM PARSE-NAME ." delete " 2DUP TYPE ."  y/n"
\+ DELETE-FILE   KEY $20 OR 'y' <> IF 2DROP BREAK  DELETE-FILE DROP ; 
[THEN]

[IFNDEF] FMOVE
\ + DELETE-FILE : FMOVE PARSE-NAME 2DUP PARSE-NAME COPY_FILE DELETE-FILE ;  
[THEN]

[IFNDEF] REN
\+ RENAME-FILE : REN  PARSE-NAME PARSE-NAME RENAME-FILE ; 
[THEN]

\- CUR_DIR@ : CUR_DIR@  G_CLI_PATH ZCOUNT ;

1 VALUE CUR_NC
0 VALUE BIG_NC

1 VALUE CUR_NC_SAVE
0 VALUE BIG_NC_SAVE

0x1B VALUE NC_COLOR 

0 VALUE NC_X_BASE
2 VALUE NC_Y_BASE
2 VALUE NC_COLS
9 VALUE NC_ROWS
0 VALUE NC_CUR
0 VALUE NC_BASE
18 VALUE NC_NAMESIZE

: NCMOD3
3 TO NC_COLS
12 TO NC_NAMESIZE ;

: NCMOD2
2 TO NC_COLS
18 TO NC_NAMESIZE ;

: NCMOD1
1 TO NC_COLS
38 TO NC_NAMESIZE ;

\ 14 CONSTANT MAXFNM
55 CONSTANT MAXFNM

VARIABLE COUNT_NAME MAXFNM 1+ ALLOT
\ CREATE COUNT_NAME_S MAXFNM 1+ ALLOT

\- MAXDIR 0x101 CONSTANT MAXDIR
VARIABLE DIR_NAME_S MAXDIR 1+ ALLOT
DIR_NAME_S 0!

: |EMITS
 BEGIN DUP GETX U>
 WHILE OVER EMIT
 REPEAT 2DROP ;

: NCFFNAME. ( DirID n -- )
\  GETXY  NC_NAMESIZE SPACES
\  SETXY
  DIRI2NAME ZCOUNT TUCK TYPE \  ZTYPE
  NC_NAMESIZE - NEGATE SPACES
  
 ;

: ||EMITS NC_X_BASE  NC_NAMESIZE 1+ NC_COLS * +   |EMITS ;

0x30 VALUE NCSETCOLOR

0 VALUE ?COUNT_DIR

\- STATUS. : STATUS. ;
\- DIR_ID 0 VALUE DIR_ID


: NC_SETNAME  ( n -- )
  DUP>R  NC_CUR = 
  DUP	IF DIR_ID NC_CUR DIRI2NAME ZCOUNT COUNT_NAME $!
	   DIR_ID NC_CUR DIRI2TYPE 5 = TO ?COUNT_DIR
	THEN
 NCSETCOLOR AND
  NC_COLOR  XOR 
 DIR_ID R> DIRI2TYPE 5 =  IF 0x4 XOR THEN
 COLOR! ;

0 VALUE TST1V
0 VALUE TST2V

: NC_CAP_DRAW
  NC_COLOR COLOR!
  NC_X_BASE
  NC_Y_BASE SETXY
   '+' EMIT
 CUR_DIR@ COLS 1- 2/ 2- UMIN TYPE
 '-' ||EMITS
;

: EMITBK  >R  GETXY  R> EMIT  SETXY ;

: FRAME_DRAW
  COLOR@
\ CCOUT{ CR ." FRAME_DRAW@" DUP H. CCKWATE }CCOUT
 >R
  GETXY 2>R
  NC_CAP_DRAW  '+' EMITBK
  NC_ROWS 0 DO  CR NC_X_BASE SETX '|' EMIT bl ||EMITS '|' EMITBK LOOP
		CR NC_X_BASE SETX '+' EMIT '-' ||EMITS '+' EMIT
  2R> SETXY
  R>
\ CCOUT{  ." FRAME_DRAW!" DUP H. CCKWATE }CCOUT
  COLOR!
;

: DIR_DRAW
 NC_CUR NC_COLS NC_ROWS * +
 NC_BASE - NC_ROWS   NC_COLS *  /  1-  NC_ROWS *
 NC_BASE + 0 MAX TO NC_BASE
 NC_CUR    0 MAX TO NC_CUR

  S" "  OPEN-DIR DUP IF S" R:\" $CHDIR ( ." CD" KEY DROP) THEN
  THROW TO DIR_ID

    NC_X_BASE 1+ NC_Y_BASE 1+ SETXY

  NC_CUR DIR_ID DIR2COUNT 1- 0MAX UMIN TO NC_CUR

 NC_COLS NC_ROWS * 0
 DO
	I NC_ROWS /MOD  NC_NAMESIZE 1+ * NC_X_BASE + 1+
	SWAP  NC_Y_BASE + 1+ SETXY 
        I NC_BASE + DIR_ID DIR2COUNT U<
	IF 	I NC_BASE + NC_SETNAME
		DIR_ID I NC_BASE + NCFFNAME.
	ELSE  NC_NAMESIZE SPACES
	THEN
 LOOP
;

: NC_HOME  0 TO NC_CUR 0 TO NC_BASE ;

0 VALUE NC_CUR_S
0 VALUE NC_BASE_S
: FMTABDO
	0 TO NCSETCOLOR      DIR_DRAW
	0x30 TO NCSETCOLOR
	NC_CUR_S	NC_CUR
 TO	NC_CUR_S TO	NC_CUR
	NC_BASE_S	NC_BASE
 TO	NC_BASE_S TO	NC_BASE
\	COUNT_NAME COUNT COUNT_NAME_S $!
    DIR_NAME_S COUNT COUNT_NAME $!
	CUR_DIR@
 DUP MAXDIR U> IF CR ." YYY="  2DUP TYPE ABORT THEN
  DIR_NAME_S $!
    COUNT_NAME COUNT $CHDIR
  NC_X_BASE 0x28 XOR TO NC_X_BASE
         FRAME_DRAW
;

: NCDIR_DRAW
 2>R >R
 GETXY 2>R
 0 0 SETXY $50  SPACES $50  SPACES
 0 0 SETXY
  STATUS.
 2R> SETXY
 R> 2R> 

  COLOR@ >R
  GETXY 2>R

  DIR_DRAW

  2R> SETXY
  R>  COLOR!
;

\- $VIEW	DEFER $VIEW ' 2DROP TO $VIEW
\- $EDIT	DEFER $EDIT ' 2DROP TO $EDIT

: NCDDDD
  COLOR@ >R
  GETXY 2>R
   FMTABDO FMTABDO
  2R>  SETXY
  R>  COLOR! ;

: DO_NC_0KEY2_ ( c -- )
  CASE
	EXKEY_RIGHT OF NC_CUR  NC_ROWS + TO NC_CUR   ENDOF  \
	EXKEY_LEFT OF NC_CUR  NC_ROWS - TO NC_CUR   ENDOF  \ left
	EXKEY_DOWN OF NC_CUR  1+ TO NC_CUR
		    NC_CUR  NC_BASE -  NC_COLS NC_ROWS * =
			IF NC_BASE 1+ TO NC_BASE THEN
		 ENDOF
	EXKEY_UP OF NC_CUR  NC_BASE - 0=
			IF NC_BASE 1- 0 MAX  TO NC_BASE THEN
			NC_CUR  1- TO NC_CUR
		 ENDOF
	EXKEY_HOME OF  NC_HOME ENDOF \ HOME
	EXKEY_END OF	 ENDOF \ end
\+ CURSOR%	'R' OF CURSOR% @ 5 U< VSHRIFT@ AND 3 OR CURSOR% !  ENDOF \ Ins

	EXKEY_PAGE_UP OF NC_BASE NC_COLS NC_ROWS * - 0 MAX TO NC_BASE
		NC_CUR NC_COLS NC_ROWS * - 0 MAX TO NC_CUR
	     ENDOF \ PAGE UP
	EXKEY_PAGE_DOWN OF NC_BASE NC_COLS NC_ROWS * + TO NC_BASE
		NC_CUR	NC_COLS NC_ROWS * + TO NC_CUR
	     ENDOF \ PAGE DOWN
\+ $VIEW	EXKEY_F3 OF >R 2>R COUNT_NAME COUNT $VIEW	2R> R> FMTABDO FMTABDO ENDOF
\+ $EDIT	EXKEY_F4 OF >R 2>R COUNT_NAME COUNT $EDIT	2R> R> FMTABDO FMTABDO ENDOF
  EXKEY_F4 CTL+ OF >R 2>R S" REE" EVALUATE		2R> R> NCDDDD ENDOF
  EXKEY_F1 OF 
  CR ." ctrl-o - nc/console switch"
  CR ." ctrl-e - previous line"
  CR ." ctrl-x - ctrl-e undo"
  CR ." f4 - edit"
  CR ." f4-ctrl - last file edit continue"
  CR ." f5 - file copy"
  CR ." f8 - file delete"
  CR NCDDDD
 ENDOF

 ENDCASE
;

: GEN_2PARAM
	ACC_TYPE
	BL  ACC_EMIT
	COUNT_NAME COUNT ACC_TYPE
	BL  ACC_EMIT
	DIR_NAME_S	COUNT ACC_TYPE
	COUNT_NAME COUNT ACC_TYPE
;

: GEN_1PARAM  ACC_TYPE
	BL  ACC_EMIT
	COUNT_NAME COUNT ACC_TYPE
;

\- FCOPY \+ COPY : FCOPY COPY ;

: ACC_DELETE1 ( ADDR ADDR1 -- ADDR ADDR1 )
  2DUP U> 0= IF BREAK
   GETXY 2>R
  2DUP - >R DUP 1+ OVER R@ MOVE
   DUP R>
\   GETXY 2>R
\ 1 1 SETXY DUP . 2DUP TYPE ." $"
\  2R> SETXY
 TYPE 2 EMIT 4 EMIT
  2R> SETXY
;

: NCDDD
  COLOR@ >R
  GETXY 2>R
   FMTABDO
  2R>  SETXY
  R>  COLOR! ;

: NCDO1B
  CASE
\	EXKEY_F10 OF ( S"  0 TO NCACCEPT?" ACC_TYPE) ENDOF \ F10
	EXKEY_F5 OF
		?COUNT_DIR
		IF	S"  DIRCP " ACC_TYPE
			COUNT_NAME COUNT ACC_TYPE
			BL  ACC_EMIT
			DIR_NAME_S COUNT ACC_TYPE
			S" \"  ACC_TYPE
		ELSE  S"  FCOPY" GEN_2PARAM 
		THEN ENDOF \ F5
	EXKEY_F6 OF  S"  RENAME" GEN_1PARAM S"  NEW"  ACC_TYPE  ENDOF \ F6
	EXKEY_F7 OF  S"  MKDIR NEWDIR" ACC_TYPE ENDOF \ F7
	EXKEY_F8 OF  S"  RM" GEN_1PARAM  ENDOF \ F8
	EXKEY_F9 OF  S"  EFICALL" GEN_1PARAM  ENDOF \ F8
	0x1C OF  COUNT_NAME COUNT ACC_TYPE ENDOF \ Alt-Ener

	[CHAR] S OF  ACC_DELETE ENDOF
	$92 OF          \  CTRL INS
[IFDEF] CLIPBOARD!
 2 PICK \ SA EA A SA
 2DUP - \ SA EA A SA A-SA 
 CLIPBOARD!
[THEN]
  ENDOF
[IFDEF] ID_SHIFT@
	[CHAR] R OF  \ imsert 
			ID_SHIFT@
			IF CLIPBOARD? IF CLIPBOARD@ ( 80 UMIN ) ACC_TYPE THEN
			ELSE \+ CURSOR%	CURSOR% @ 5 U< VSHRIFT@ AND 3 OR CURSOR% ! 
			THEN
		 ENDOF
[THEN]
\+ EVALUATE	0xFE OF >R 2>R S" REE" EVALUATE		2R> R> NCDDDD ENDOF \ Ctl F4

  COLOR@ >R
  GETXY 2>R
	DO_NC_0KEY2_
  2R> SETXY
  R>  COLOR! 0

 ENDCASE ;

0 VALUE NCACCEPT?

DEFER NCACCEPT_STEP

0 VALUE 'NC_NCACCEPT_STEP

0 VALUE 'NCACCEPT0
0 VALUE 'NCACCEPT
\- NCACCEPT DEFER NCACCEPT

: COACCEPT0 ( c-addr +n1 -- +n2 ) \ 94
 GETXY 2>R
 2>R  
 0 0 SETXY  STATUS.
 2R>  \ c-addr +n1
 2R> SETXY

   COLS 2- UMIN
   2DUP BLANK
   OVER + 1- OVER      \ SA EA A
   BEGIN   KEY          \ SA EA A C
\     DUP 0xA = OVER 0xD = OR 0= 
 NCACCEPT_STEP
   UNTIL

  ACC_END NIP
  OVER - DUP IF 2DUP

  LAST_STP $!  1 COUNT_STP +! THEN
  NIP CR
;

: CO_NCACCEPT_STEP   ( -- flg )
        CASE
        DUP $FFFF ANDC
	IF DO1B   ENDOF
        8       OF ACC_LEFT ACC_DELETE ENDOF
\+ STS.	$13	OF 2>R >R STS. R> 2R> ENDOF
	DUP $20 OR 
	'o' CTL+ OF DROP 'NC_NCACCEPT_STEP TO NCACCEPT_STEP 
 NCDDDD NCDIR_DRAW
                'NCACCEPT0 TO NCACCEPT
  ENDOF  \ ctr-o
	DROP

        0xA     OF -1 EXIT ENDOF
\+ CURSOR%	CURSOR% @ 5 U>	IF >R ACC_DELETE1 R> THEN
               ACC_EMIT
              0 ENDCASE  OVER UMIN \ SA EA A
 0
;

: NC_NCACCEPT_STEP ( c -- flg )
 CASE
	DUP $FFFF ANDC

	IF  NCDO1B NCDIR_DRAW  ENDOF
	8	OF ACC_LEFT ACC_DELETE ENDOF
\+ STS.	$13	OF 2>R >R STS. R> 2R> ENDOF
	DUP $20 OR
	'o' CTL+ OF DROP ['] CO_NCACCEPT_STEP TO NCACCEPT_STEP
                ['] COACCEPT0 TO NCACCEPT  ENDOF \ Ctr-O
	'e' CTL+ OF DROP -1 COUNT_STP +! GET_LAST_STP    ENDOF  \ ctr-e
	'x' CTL+ OF DROP  1 COUNT_STP +! GET_LAST_STP    ENDOF  \ ctr-x

        'u' CTL+  OF DROP  NC_X_BASE 0x28 XOR TO NC_X_BASE  COLOR@ >R  GETXY 2>R
			FMTABDO FMTABDO   DIR_DRAW	2R> SETXY  R>  COLOR!  ENDOF \ Ctr-U

	'f' CTL+  OF DROP	CUR_DIR@  ACC_TYPE '\' ACC_EMIT
			COUNT_NAME COUNT ACC_TYPE ENDOF \ Ctr-F
	DROP
	0x1B OF ( S"  0 TO NCACCEPT?" ACC_TYPE) ENDOF \ esc
	9	OF
  COLOR@ >R  GETXY 2>R  FMTABDO DIR_DRAW
  2R> NC_ROWS 2+ UMAX SETXY  R>  COLOR!  ENDOF  \ tab
	0xA CTL+ OF  COUNT_NAME COUNT ACC_TYPE ENDOF \ Ctr-Enter
	0xA	OF   >R OVER R>  TUCK U< IF -1 BREAK
		  ?COUNT_DIR
		  IF	COUNT_NAME COUNT $CHDIR
		COLOR@ >R  GETXY 2>R NC_HOME NC_CAP_DRAW NCDIR_DRAW
		2R> SETXY  R>  COLOR!
		  THEN
		ENDOF
\+ CURSOR%		CURSOR% @ 5 U>	IF >R ACC_DELETE1 R> THEN
               ACC_EMIT
              0 ENDCASE  OVER UMIN \ SA EA A
             0
;

' NC_NCACCEPT_STEP TO 'NC_NCACCEPT_STEP
'NC_NCACCEPT_STEP TO NCACCEPT_STEP

: NCACCEPT0 ( c-addr +n1 -- +n2 ) \ 94
  NCDDDD

   COLS 2- UMIN
   2DUP BLANK
   OVER + 1- OVER      \ SA EA A
  NCDIR_DRAW
   BEGIN
   KEY          \ SA EA A C
 NCACCEPT_STEP
   UNTIL

  ACC_END NIP
  OVER - DUP IF 2DUP

  LAST_STP $!  1 COUNT_STP +! THEN
  NIP CR
;

' NCACCEPT0 TO 'NCACCEPT0
' NCACCEPT0 TO NCACCEPT

: NC \ TOGGG
 ['] NCACCEPT TO ACCEPT
 GETXY   NC_ROWS 2 + MAX
 SETXY ;

