mmbasic:line_editor_for_interactive_editing_of_text_lines_pages_lineedit_bas
Line editor for interactive editing of text lines/pages - LINEEDIT.BAS
lineedit.bas.v2.zip lineedit.bas.v3.zip lineedit.bas.zip
See here for some more recent options courtesy @twofingers
An alternative for Picomites courtesy @matherp
The LineEdit function defined in the attached LINEEDIT.BAS is an MMBasic 4.5 function to edit lines of text or even pages of text, as these five examples demonstrate:
' ' Edit a string until ENTER with LineEdit: ' Sub Example1 a$ = "text to edit" Do: k = LineEdit(a$): Loop Until k = 13 ' until ENTER End Sub
'
' Edit a string until ENTER or ESC with ESC to restore the old string value:
'
Sub Example2
a$ = "text to edit"
b$ = a$ ' save old string value
Do: k = LineEdit(a$): Loop Until k = 13 Or k = 27 ' until ENTER or ESC
If k = 27 Then ' ESC pressed
Print b$;
If Len(a$) > Len(b$) Then Print Space$(Len(a$) - Len(b$));
a$ = b$ ' restore old value on ESC
EndIf
End Sub
'
' Interactive editing of 10 lines with up/down screen navigation:
'
Sub Example3
Local a$(10) length 40
Cls
Print "Tiny edit | ESC to quit"
For row = 1 To 10
Print @(0,12*row) a$(row);Tab(11);"|"
Next row
row = 1 ' first row to edit
cur = 0 ' cursor at begin of the line
max = 40 ' max size of a$()
win = 10 ' edit in window of 10 chars
Do
Print @(0,12*row); ' place cursor where we want to edit the line
k = LineEdit(a$(row), cur, -1, max, win) ' -1: get next key press
If k = 128 Then
If row > 1 Then row = row - 1 ' UP key: move up
ElseIf k = 13 Or k = 129 Then
If row < 10 Then row = row + 1 ' DOWN key: move down
EndIf
Print @(0,132) "row:";row;" col:";cur;" key:";k;" ";
Loop Until k = 27 ' until ESC
End Sub
'
' Interactive editing with screen navigation while continuously updating the screen with the current time:
'
Sub Example4
Local a$(10) length 120
Cls
Print " | Editor | ESC to quit"
For row = 1 To 10
Print @(0,12*row) a$(row);
Next row
row = 1 ' first row to edit
cur = 0 ' cursor at begin of the line
max = 120 ' max size of a$()
win = MM.HRes/6 ' edit in window up to window width
' show cursor
Font 1,,1: Print @(0,12*row) Mid$(a$(row)+" ", 1, 1);Chr$(8);: Font 1,,0
Do
Print @(0,0) Time$; ' continuously show the current time
k = Asc(Inkey$)
If k Then
Print @(0,12*row);
k = LineEdit(a$(row), cur, k, max, win) ' pass in key to edit line
If k = 128 Then ' UP key: move up
If row > 1 Then row = row - 1
Font 1,,1: Print @(0,12*row) Mid$(a$(row)+" ", 1, 1);Chr$(8);: Font 1,,0
cur = 0
ElseIf k = 13 Or k = 129 Then ' DOWN key: move down
If row < 10 Then row = row + 1
Font 1,,1: Print @(0,12*row) Mid$(a$(row)+" ", 1, 1);Chr$(8);: Font 1,,0
cur = 0
EndIf
EndIf
Loop Until k = 27 ' until ESC
End Sub
'
' Edit a string of digits, masking key presses to digits
'
Sub Example5
a$ = "1234"
Font 1,,1: Print Mid$(a$, 1, 1);: Font 1,,0: Print Mid$(a$, 2);Chr$(13);
Do
k = Asc(Inkey$)
If k Then
If k < 32 Or k > 126 Or Instr("0123456789", Chr$(k)) Then
k = LineEdit(a$, cur, k)
EndIf
EndIf
Loop Until k = 13 Or k = 27 ' until ENTER or ESC
End Sub
Source code of LineEdit:
' LineEdit(a$, [cur [, key [, max [, win ]]]]) edit string a$ at position cur,
' use key parameter (when provided) to make the next edit of a$ and update cur, and
' return new key press, or if key press not provided (zero) keep editing until
' ESC, ENTER or special key such as F1, UP/DOWN etc. where max is the maximum
' string length and win is the "window" size. Use LEFT/RIGHT/HOME/END to
' navigate the line, BACKSPACE/DEL to delete a character.
'
' Supports three editing modes: key=0 (default) edit until ENTER or other
' special key, key=-1 wait for key and make one edit, returning the key value,
' key=X to pass a key press to LineEdit to make one edit, returning the key
' value.
'
' Cursor position cur parameter is passed in and updated by LineEdit
'
' Use the max parameter when the string has a maximum length smaller than 255
'
' To edit within a smaller window, use the win parameter
'
Function LineEdit(a$, cur, key, max, win)
Local x, y, size, from, done
x = MM.HPos: y = MM.VPos
If max <= 0 Then max = 255
If win <= 0 Then win = 255
size = Len(a$)
If cur < 0 Then
cur = 0
ElseIf cur > size Then
cur = size
EndIf
done = key
Do
If key >= 32 And key < 127 Then
If size < max Then
a$ = Left$(a$, cur) + Chr$(key) + Mid$(a$, cur + 1)
cur = cur + 1
size = size + 1
EndIf
ElseIf key = 8 Then
If cur > 0 Then
cur = cur - 1
a$ = Left$(a$, cur) + Mid$(a$, cur + 2)
size = size - 1
EndIf
ElseIf key = 127 Then
If cur < size Then
a$ = Left$(a$, cur) + Mid$(a$, cur + 2)
size = size - 1
EndIf
ElseIf key = 130 Then
If cur > 0 Then cur = cur - 1
ElseIf key = 131 Then
If cur < size Then cur = cur + 1
ElseIf key = 134 Then
cur = 0
ElseIf key = 135 Then
cur = size
ElseIf key > 0 Then
' done editing, so print a clean line
Print @(x,y) Left$(a$, win);
' remove the cursor from the end of the line
If cur = size And size < win Then Print " ";
Exit
EndIf
' cursor cannot move beyond string max length
If cur >= max Then cur = max - 1
' cursor is offset from window start
If cur >= win Then from = cur - win + 2 Else from = 1
If cur = size Then
' print the line constrained to the window size
Print @(x,y) Mid$(a$, from);
' put the cursor at the end
Font 1,,1: Print " ";: Font 1,,0
' BACKSPACE may require removing the cursor from the end of the line
If key = 8 Then
If cur + 1 < win And size < win Then Print " ";
EndIf
Else
' print the line constrained to window size
Print @(x,y) Mid$(a$, from, cur - from + 1);
' invert character at the cursor
Font 1,,1: Print Mid$(a$, cur + 1, 1);: Font 1,,0
' print the rest of the line
Print Mid$(a$, cur + 2, win - cur + from - 2);
If cur < win And size < win Then Print " ";
EndIf
' if this is a non-blocking call then exit
If done > 0 Then Exit
' wait on key press
Do: key = Asc(Inkey$): Loop Until key
done = -done
Loop
' move cursor back to the start of the line before returning
Print @(x,y);
LineEdit = key
End Function
Line editor for Picomite
courtesy @matherp
MM.DISPLAY - tells you if there is a connected physical display VGA/HDMI or TFT MM.HEIGHT gives the number of characters down the physical display with the current font MM.WIDTH gives the number of characters across the physical display with the current font
Option explicit setcolour RGB(red),RGB(blue) clearscreen writeat 30,24,"Hello World" ' Sub resize 'Set the serial console to the same size as the screen Option console serial Print Chr$(27)+"[8;"+Str$(MM.HEIGHT)+";"+Str$(MM.WIDTH+1)+"t"; Option console both End Sub ' Sub cursor x%,y% ' move the cursor to the x%,y% character positions (not pixel) Option console serial Print Chr$(27)+"["+Str$(y%+1)+";"+Str$(x%+1)+"H"; If MM.DISPLAY Then Print @(x%*MM.FONTWIDTH,y%*MM.FONTHEIGHT)""; EndIf Option console both End Sub ' Sub writeat x%,y%,t$ 'write text at the character position specified Option console serial Print Chr$(27)+"["+Str$(y%+1)+";"+Str$(x%+1)+"H"+t$; If MM.DISPLAY Then Text x%*MM.FONTWIDTH,y%*MM.FONTHEIGHT,t$ EndIf Option console both End Sub ' Sub write t$ 'write text at the current cursor position Option console serial Print t$; If MM.DISPLAY Then Text MM.HPOS*MM.FONTWIDTH,MM.VPOS*MM.FONTHEIGHT,t$ EndIf Option console both End Sub ' Sub setcolour f%, b% ' set the colour of both the screen and the console Local integer fr=f%>>16,br=b%>>16,fg=(f%>>8) And &HFF,bg=(b%>>8) And &HFF,fb=f% And &HFF,bb=b% And &HFF Option console serial Print Chr$(27)+"[38;2;"+Str$(fr)+";"+Str$(fg)+";"+Str$(fb)+"m"; Print Chr$(27)+"[48;2;"+Str$(br)+";"+Str$(bg)+";"+Str$(bb)+"m"; Option console both Colour f%,b% End Sub ' Sub clearscreen 'clear both the display and the console Option console serial Print Chr$(27)+"[7"+Chr$(27)+"[2J"+Chr$(27)+"[H"; If MM.DISPLAY Then CLS EndIf Option console both End Sub
mmbasic/line_editor_for_interactive_editing_of_text_lines_pages_lineedit_bas.txt · Last modified: by Gerry
