factor/library/ui/line-editor.factor

128 lines
3.0 KiB
Factor
Raw Normal View History

2005-01-03 02:55:54 -05:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-01-03 02:55:54 -05:00
IN: line-editor
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.
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.
line-text get dup empty? [
2005-01-04 00:41:14 -05:00
drop
] [
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.
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
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-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
] make-hash ;
2005-01-03 02:55:54 -05:00
: caret-insert ( str offset -- )
#! Call this in the line editor scope.
caret get <= [
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.
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 ;