--- gforth/history.fs 2005/12/31 15:46:08 1.43 +++ gforth/history.fs 2007/06/07 20:57:13 1.54 @@ -1,6 +1,6 @@ \ command line edit and history support 16oct94py -\ Copyright (C) 1995,2000,2003,2004,2005 Free Software Foundation, Inc. +\ Copyright (C) 1995,2000,2003,2004,2005,2006 Free Software Foundation, Inc. \ This file is part of Gforth. @@ -69,9 +69,16 @@ s" os-class" environment? [IF] s" unix" \ moving in history file 16oct94py -: clear-line ( max span addr pos1 -- max addr ) - backspaces over spaces swap backspaces ; +defer back-restore ( u -- ) +' backspaces is back-restore +[IFDEF] x-width +: clear-line ( max span addr pos1 -- max addr ) + back-restore over over swap x-width spaces swap back-restore ; +[ELSE] +: clear-line ( max span addr pos1 -- max addr ) + back-restore over spaces swap back-restore ; +[THEN] \ : clear-tib ( max span addr pos -- max 0 addr 0 false ) \ clear-line 0 tuck dup ; @@ -191,12 +198,40 @@ require utf-8.fs [IFUNDEF] #esc 27 Constant #esc [THEN] -: save-cursor ( -- ) #esc emit '7 emit ; -: restore-cursor ( -- ) #esc emit '8 emit ; +Variable curpos + +s" os-type" environment? [IF] s" cygwin" str= [IF] +: save-cursor ( -- ) #esc emit '7 emit ; +: restore-cursor ( -- ) #esc emit '8 emit ; +: cur-correct ( addr u -- ) 2drop ; +[ELSE] +: at-xy? ( -- x y ) + key? drop + #esc emit ." [6n" 0 0 + BEGIN key dup 'R <> WHILE + dup '; = IF drop swap ELSE + dup '0 '9 1+ within IF '0 - swap 10 * + ELSE + drop THEN THEN + REPEAT drop 1- swap 1- ; +: cursor@ ( -- n ) at-xy? form nip * + ; +: cursor! ( n -- ) form nip /mod at-xy ; +: cur-correct ( addr u -- ) x-width curpos @ + cursor@ - + form nip >r r@ 2/ + r@ / r> * negate curpos +! ; +: save-cursor ( -- ) cursor@ curpos ! ; +: restore-cursor ( -- ) curpos @ cursor! ; +[THEN] : .rest ( addr pos1 -- addr pos1 ) - restore-cursor 2dup type ; + key? ?EXIT + restore-cursor 2dup type 2dup cur-correct ; : .all ( span addr pos1 -- span addr pos1 ) - restore-cursor >r 2dup swap type r> ; + key? ?EXIT + restore-cursor >r 2dup swap type 2dup swap cur-correct r> ; +: xback-restore ( u -- ) + drop key? ?EXIT + restore-cursor ; + +\ In the following, addr max is the buffer, addr span is the current +\ string in the buffer, and pos1 is the cursor position in the buffer. : ( max span addr pos1 xc -- max span addr pos2 ) >r 2over r@ xc-size + u< IF ( max span addr pos1 R:xc ) @@ -269,12 +304,14 @@ require utf-8.fs ['] xclear-tib ctrl K bindkey ['] xfirst-pos ctrl A bindkey ['] xend-pos ctrl E bindkey - ['] (xenter) #lf bindkey - ['] (xenter) #cr bindkey + history IF ['] (xenter) #lf bindkey THEN + history IF ['] (xenter) #cr bindkey THEN ['] xtab-expand #tab bindkey ['] (xins) IS insert-char ['] kill-prefix IS everychar - ['] save-cursor IS everyline ; + ['] save-cursor IS everyline + ['] xback-restore IS back-restore +; xchar-history @@ -285,7 +322,7 @@ xchar-history ?dup-if \ !! >stderr \ history-file type ." : " .error cr - drop 2drop + drop 2drop 0 to history ['] false ['] false ['] (ret) else to history @@ -302,6 +339,10 @@ xchar-history : history-cold ( -- ) history-file get-history xchar-history ; -' history-cold INIT8 chained +:noname ( -- ) + defers 'cold + history-cold +; is 'cold + history-cold