2005-01-03 02:55:54 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
2005-07-12 20:30:05 -04:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-01-03 02:55:54 -05:00
|
|
|
IN: line-editor
|
2005-07-12 20:30:05 -04:00
|
|
|
USING: kernel math namespaces sequences strings vectors ;
|
2005-01-03 02:55:54 -05:00
|
|
|
|
|
|
|
SYMBOL: line-text
|
|
|
|
SYMBOL: caret
|
|
|
|
|
2005-01-04 00:41:14 -05:00
|
|
|
! History stuff
|
|
|
|
SYMBOL: history
|
|
|
|
SYMBOL: history-index
|
|
|
|
|
|
|
|
: history-length ( -- n )
|
|
|
|
#! Call this in the line editor scope.
|
2005-04-26 00:35:55 -04:00
|
|
|
history get length ;
|
2005-01-04 00:41:14 -05:00
|
|
|
|
|
|
|
: reset-history ( -- )
|
|
|
|
#! Call this in the line editor scope. After user input,
|
|
|
|
#! resets the history index.
|
|
|
|
history-length history-index set ;
|
|
|
|
|
|
|
|
: commit-history ( -- )
|
|
|
|
#! Call this in the line editor scope. Adds the currently
|
|
|
|
#! entered text to the history.
|
2005-07-12 20:30:05 -04:00
|
|
|
line-text get dup empty? [
|
2005-01-04 00:41:14 -05:00
|
|
|
drop
|
|
|
|
] [
|
2005-04-26 00:35:55 -04:00
|
|
|
history-index get history get set-nth
|
2005-01-04 00:41:14 -05:00
|
|
|
reset-history
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: set-line-text ( text -- )
|
|
|
|
#! Call this in the line editor scope.
|
2005-04-29 02:37:12 -04:00
|
|
|
dup line-text set length caret set ;
|
2005-01-04 00:41:14 -05:00
|
|
|
|
|
|
|
: goto-history ( n -- )
|
|
|
|
#! Call this in the line editor scope.
|
|
|
|
dup history-index set
|
2005-04-26 00:35:55 -04:00
|
|
|
history get nth set-line-text ;
|
2005-01-04 00:41:14 -05:00
|
|
|
|
|
|
|
: history-prev ( -- )
|
|
|
|
#! Call this in the line editor scope.
|
|
|
|
history-index get dup 0 = [
|
|
|
|
drop
|
|
|
|
] [
|
|
|
|
dup history-length = [ commit-history ] when
|
|
|
|
1 - goto-history
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: history-next ( -- )
|
|
|
|
#! Call this in the line editor scope.
|
2005-09-16 22:47:28 -04:00
|
|
|
history-index get dup 1+ history-length >= [
|
2005-01-04 00:41:14 -05:00
|
|
|
drop
|
|
|
|
] [
|
2005-09-16 22:47:28 -04:00
|
|
|
1+ goto-history
|
2005-01-04 00:41:14 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2005-01-03 02:55:54 -05:00
|
|
|
: line-clear ( -- )
|
|
|
|
#! Call this in the line editor scope.
|
2005-01-04 00:41:14 -05:00
|
|
|
0 caret set
|
|
|
|
"" line-text set ;
|
2005-01-03 02:55:54 -05:00
|
|
|
|
|
|
|
: <line-editor> ( -- editor )
|
2005-08-22 02:06:32 -04:00
|
|
|
[
|
2005-01-04 00:41:14 -05:00
|
|
|
line-clear
|
2005-08-25 15:27:38 -04:00
|
|
|
{ } clone history set
|
2005-01-04 00:41:14 -05:00
|
|
|
0 history-index set
|
2005-08-22 02:06:32 -04:00
|
|
|
] make-hash ;
|
2005-01-03 02:55:54 -05:00
|
|
|
|
|
|
|
: caret-insert ( str offset -- )
|
|
|
|
#! Call this in the line editor scope.
|
|
|
|
caret get <= [
|
2005-04-29 02:37:12 -04:00
|
|
|
length caret [ + ] change
|
2005-01-03 02:55:54 -05:00
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: line-insert ( str offset -- )
|
|
|
|
#! Call this in the line editor scope.
|
2005-01-04 00:41:14 -05:00
|
|
|
reset-history
|
2005-01-03 02:55:54 -05:00
|
|
|
2dup caret-insert
|
2005-08-25 15:27:38 -04:00
|
|
|
line-text get [ head ] 2keep tail
|
2005-05-18 16:26:22 -04:00
|
|
|
swapd append3 line-text set ;
|
2005-01-03 02:55:54 -05:00
|
|
|
|
|
|
|
: insert-char ( ch -- )
|
|
|
|
#! Call this in the line editor scope.
|
2005-03-05 16:33:40 -05:00
|
|
|
ch>string caret get line-insert ;
|
2005-01-03 02:55:54 -05:00
|
|
|
|
|
|
|
: caret-remove ( offset length -- )
|
|
|
|
#! Call this in the line editor scope.
|
|
|
|
2dup + caret get <= [
|
|
|
|
nip caret [ swap - ] change
|
|
|
|
] [
|
|
|
|
caret get pick pick dupd + between? [
|
|
|
|
drop caret set
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] ifte
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: line-remove ( offset length -- )
|
|
|
|
#! Call this in the line editor scope.
|
2005-01-04 00:41:14 -05:00
|
|
|
reset-history
|
2005-01-03 02:55:54 -05:00
|
|
|
2dup caret-remove
|
2005-05-18 16:26:22 -04:00
|
|
|
dupd + line-text get tail
|
|
|
|
>r line-text get head r> append
|
2005-01-03 02:55:54 -05:00
|
|
|
line-text set ;
|
|
|
|
|
|
|
|
: backspace ( -- )
|
|
|
|
#! Call this in the line editor scope.
|
2005-09-16 22:47:28 -04:00
|
|
|
caret get dup 0 = [ drop ] [ 1- 1 line-remove ] ifte ;
|
2005-01-03 02:55:54 -05:00
|
|
|
|
|
|
|
: left ( -- )
|
|
|
|
#! Call this in the line editor scope.
|
2005-09-16 22:47:28 -04:00
|
|
|
caret [ 1- 0 max ] change ;
|
2005-01-03 02:55:54 -05:00
|
|
|
|
|
|
|
: right ( -- )
|
|
|
|
#! Call this in the line editor scope.
|
2005-09-16 22:47:28 -04:00
|
|
|
caret [ 1+ line-text get length min ] change ;
|
2005-07-27 01:46:06 -04:00
|
|
|
|
|
|
|
: home ( -- )
|
|
|
|
#! Call this in the line editor scope.
|
|
|
|
0 caret set ;
|
|
|
|
|
|
|
|
: end ( -- )
|
|
|
|
#! Call this in the line editor scope.
|
|
|
|
line-text get length caret set ;
|