11-24-2023, 05:51 PM
(This post was last modified: 11-24-2023, 05:52 PM by Ed Davis.
Edit Reason: Adding stuff
)
(11-19-2023, 09:04 PM)bplus Wrote: Anyone got anything close to bare bones editor only a few c's of lines ?How about this? Based on an old FreeBasic editor I found a while back. It used linked lists, had a few issues.
I converted it to QB64, used an array of strings instead of the linked list, fixed a few bugs, probably added a few more. Available commands are in the main sub below.
Very bare bones! But less than 200 lines, and pretty easy to follow
Does not currently handle horizontal scrolling.
Code: (Select All)
option _explicit
declare function down&
declare function up&
declare function pgdn&
declare function pgup&
declare function cmleft&
declare function cmright&
const c_nlines = 5000, true = -1, false = 0
redim shared linelist(1 to c_nlines) as string
dim shared as long curline, numlines, cx, cy, sy, rows, cols, junk
numlines = 0: cx = 0: cy = 0: sy = 0
rows = _height: cols = _width
rows = rows - 1 ' allow one for a status line
loadfile command$
locate , , 0
main
locate rows + 1, 1, 1
end
sub main
dim thekey as string
dim refresh as integer
redraw
do
refresh = true
thekey = inkey$
select case thekey
case chr$(0)+"P": junk = down
case chr$(0)+"H": junk = up
case chr$(0)+"M": junk = cmright
case chr$(0)+"K": junk = cmleft
case chr$(0)+"Q": junk = pgdn
case chr$(0)+"I": junk = pgup
case chr$(0)+"G": cx = 0 'home
case chr$(0)+"O": cx = len(linelist(curline)) 'end
case chr$(0)+chr$(118): while pgdn: wend 'ctrl-pgdn
case chr$(0)+chr$(132): while pgup: wend 'ctrl-pgup
case chr$(13): enter
case chr$(8): backspace
case chr$(0)+"S": cdelete
case chr$(32) to chr$(128): selfinsert thekey
case else: refresh = false
end select
if refresh then redraw
loop until thekey = chr$(27)
end sub
sub loadfile(f as string)
linelist(1) = ""
curline = 1
if not _fileexists(f) then numlines = 1: exit sub
open f for input as #1
do
numlines = numlines + 1
if numlines > ubound(linelist) then redim _preserve linelist(ubound(linelist) + c_nlines)
line input #1, linelist(numlines)
loop until eof(1)
close
end sub
function down&
if curline >= numlines then down = false: exit function
if cy = rows - 1 then sy = sy + 1 else cy = cy + 1
curline = curline + 1
if len(linelist(curline)) < cx then cx = len(linelist(curline))
down = true
end function
function up&
if curline <= 1 then up = false: exit function
if cy = 0 then sy = sy - 1 else cy = cy - 1
curline = curline - 1
if len(linelist(curline)) < cx then cx = len(linelist(curline))
up = true
end function
function cmleft&
if cx = 0 then
if curline <= 1 then cmleft = false: exit function
junk = up: cx = len(linelist(curline))
else
cx = cx - 1
end if
cmleft = true
end function
function cmright&
if cx = len(linelist(curline)) then
if curline >= numlines then cmright = false: exit function
junk = down: cx = 0
else
cx = cx + 1
end if
cmright = true
end function
function pgdn&
dim i as long
if curline >= numlines then pgdn = false: exit function
for i = 1 to rows - 1
if not down then exit for
next
pgdn = true
end function
function pgup&
dim i as long
if curline <= 1 then pgup = false: exit function
for i = 1 to rows - 1
if not up then exit for
next
pgup = true
end function
sub enter
dim as long n
dim as string s
numlines = numlines + 1
if numlines > ubound(linelist) then redim _preserve linelist(ubound(linelist) + c_nlines)
s = mid$(linelist(curline), cx + 1)
linelist(curline) = left$(linelist(curline), cx)
' move everything after curline down one
for n = numlines to curline + 1 step -1
linelist(n) = linelist(n - 1)
next n
curline = curline + 1
linelist(curline) = s
if cy = rows - 1 then sy = sy + 1 else cy = cy + 1
cx = 0
end sub
sub backspace
dim as long line_len, n
if cx = 0 then
if curline <= 1 then exit sub
line_len = len(linelist(curline - 1))
linelist(curline - 1) = linelist(curline - 1) + linelist(curline)
' move everything after curline up one
for n = curline to numlines - 1
linelist(n) = linelist(n + 1)
next n
numlines = numlines - 1
cx = line_len
if cy = 0 then sy = sy - 1 else cy = cy - 1
curline = curline - 1
else
linelist(curline) = left$(linelist(curline), cx - 1) + mid$(linelist(curline), cx + 1)
cx = cx - 1
end if
end sub
sub cdelete
if cx = len(linelist(curline)) then if curline >= numlines then exit sub
junk = cmright
backspace
end sub
sub selfinsert(thekey as string)
linelist(curline) = left$(linelist(curline), cx) + thekey + mid$(linelist(curline), cx + 1)
cx = cx + 1
end sub
sub redraw
dim as long y, n
cls
n = curline - cy
if n < 1 then n = 1
for y = 1 to rows
locate y, 1
? left$(linelist(n), cols);
if n = numlines then exit for
n = n + 1
next
locate cy + 1, cx + 1
color 0, 7
? chr$(screen(cy + 1, cx + 1));
locate rows + 1, 1
? "Line: "; cy + sy + 1; " of "; numlines; " Col: "; cx + 1;
color 7, 0
end sub