Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This sample is for File.InsertLine, File.ReadLine, and File.WriteLine.
Usage
Enter the text file name to edit. H command shows the help.
Code
' Line Editor
' Version 0.1
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Known Issues
' [1] Can't display Unicode characters.
' [2] Can't delete lines.
Init()
While cont
ShowPrompt()
ReadCommand()
DoCommand()
EndWhile
Program.End()
Sub DoCommand
If cmd = "h" Or cmd = "?" Then
TextWindow.WriteLine("?|h - help")
TextWindow.WriteLine("i[<l1>,<l2>] - insert line[s] to file")
TextWindow.WriteLine("l[<l1>,<l2>|*] - list lines")
TextWindow.WriteLine("m<l> - move to a line")
TextWindow.WriteLine("q - quit")
TextWindow.WriteLine("r[<l1>,<l2>] - replace line[s] to file")
ElseIf cmd = "i" Then
If maxlno + 1 < l1 Then
For l = maxlno + 1 To l1 - 1
File.WriteLine(filename, l, "")
EndFor
EndIf
For l = l1 To l2
line = TextWindow.Read()
File.InsertLine(filename, l, line)
EndFor
maxlno = maxlno + l2 - l1 + 1
ElseIf cmd = "l" Then
For l = l1 To l2
sp = Text.GetSubText(" ", 1, Text.GetLength(l2) - Text.GetLength(l))
line = File.ReadLine(filename, l)
TextWindow.WriteLine(sp + l + " " + line)
EndFor
ElseIf cmd = "m" Then
If maxlno < l1 Then
curlno = maxlno
Else
curlno = l1
EndIf
ElseIf cmd = "q" Then
cont = "False"
ElseIf cmd = "r" Then
If 1 <= l1 And l1 <= l2 And l2 <= maxlno Then
For l = l1 To l2
line = TextWindow.Read()
File.WriteLine(filename, l, line)
EndFor
Else
msg = "Invalid lines: " + l1 + "," + l2
ShowError()
EndIf
Else
msg = "Unknown cmd: " + cmd
ShowError()
EndIf
EndSub
Sub GetCommand
' param cmdline - command line
' param p - pointer to command line
' return p - updated pointer to command line
' return cmd - command
cmd = ""
c = Text.ConvertToLowerCase(Text.GetSubText(cmdline, p, 1))
If Text.IsSubText("abcdefghijklmnopqrstuvwxyz?", c) Then
cmd = c
p = p + 1
EndIf
EndSub
Sub GetDelim
' param cmdline - command line
' param p - pointer to command line
' return p - updated pointer to command line
' return delim - delimiter
delim = ""
c = Text.GetSubText(cmdline, p, 1)
If c = "," Then
delim = c
p = p + 1
EndIf
EndSub
Sub GetLineNo
' param cmdline - command line
' param p - pointer to command line
' return p - updated pointer to command line
' return l1 - start line number
' return l2 - end line number
c = Text.GetSubText(cmdline, p, 1)
If c = "*" Then
l1 = 1
l2 = maxlno
Else
GetNum()
If n = "" Then
l1 = curlno
Else
l1 = n
EndIf
SkipSpace()
GetDelim()
If delim = "" Then
l2 = l1
Else
SkipSpace()
GetNum()
If n = "" Then
l2 = l1
Else
l2 = n
EndIf
EndIf
EndIf
EndSub
Sub GetMaxLineNo
' param filename
' return maxlno - max line number
maxlno = 0
buf = File.ReadContents(filename)
p = 1
len = Text.GetLength(buf)
While p < len
maxlno = maxlno + 1
eol = Text.GetIndexOf(Text.GetSubTextToEnd(buf, p), LF)
If eol = 0 Then
p = len
Else
p = p + eol
EndIf
EndWhile
EndSub
Sub GetNum
' param cmdline - command line
' param p - pointer to command line
' return p - updated pointer to command line
' return n - number
n = ""
c = Text.GetSubText(cmdline, p, 1)
While Text.IsSubText("0123456789", c)
n = Text.Append(n, c)
p = p + 1
c = Text.GetSubText(cmdline, p, 1)
EndWhile
EndSub
Sub Init
Not = "False=True;True=False;"
cont = "True"
LF = Text.GetCharacter(10)
If 0 < Program.ArgumentCount Then
filename = Program.GetArgument(1)
EndIf
While filename = ""
TextWindow.Write("Filename? ")
filename = TextWindow.Read()
EndWhile
If Not[Text.IsSubText(filename, "\")] Then
filename = Program.Directory + "\" + filename
EndIf
GetMaxLineNo()
TextWindow.WriteLine(maxlno + " lines")
curlno = 1
EndSub
Sub ReadCommand
cmdline = TextWindow.Read()
p = 1
SkipSpace()
GetCommand()
SkipSpace()
GetLineNo()
EndSub
Sub ShowError
' param msg - error message
TextWindow.ForegroundColor = "Red"
TextWindow.WriteLine(msg)
TextWindow.ForegroundColor = "Gray"
EndSub
Sub ShowPrompt
TextWindow.Write("> ")
EndSub
Sub SkipSpace
' param cmdline - command line
' param p - pointer to command line
' return p - updated pointer to command line
c = Text.GetSubText(cmdline, p, 1)
While c = " "
p = p + 1
c = Text.GetSubText(cmdline, p, 1)
EndWhile
EndSub