1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-12-25 17:48:06 +11:00

ed: allow inserting in empty file

This commit is contained in:
Virgil Dupras 2019-07-21 15:06:03 -04:00
parent 34f499184d
commit 421d881fae
2 changed files with 32 additions and 26 deletions

View File

@ -29,6 +29,8 @@ bufInit:
ld de, BUF_PAD ; points to beginning of current line ld de, BUF_PAD ; points to beginning of current line
ld ix, BUF_LINES ; points to current line index ld ix, BUF_LINES ; points to current line index
ld bc, 0 ; line count ld bc, 0 ; line count
; init pad end in case we have an empty file.
ld (BUF_PADEND), hl
.loop: .loop:
call ioGetC call ioGetC
jr nz, .loopend jr nz, .loopend

View File

@ -69,38 +69,44 @@ edMain:
jr nz, .error jr nz, .error
ld a, (CMD_TYPE) ld a, (CMD_TYPE)
cp 'q' cp 'q'
jr z, .doQuit jr z, .doQ
cp 'w' cp 'w'
jr z, .doWrite jr z, .doW
; The rest of the commands need an address ; The rest of the commands need an address
call edReadAddrs call edReadAddrs
jr nz, .error jr nz, .error
ld a, (CMD_TYPE) ld a, (CMD_TYPE)
cp 'd'
jr z, .doDel
cp 'a'
jr z, .doAppend
cp 'i' cp 'i'
jr z, .doInsert jr z, .doI
jr .doPrint ; The rest of the commands don't allow addr == cnt
push hl ; --> lvl 1
ld hl, (BUF_LINECNT)
call cpHLDE
pop hl ; <-- lvl 1
jr z, .error
cp 'd'
jr z, .doD
cp 'a'
jr z, .doA
jr .doP
.doQuit: .doQ:
xor a xor a
ret ret
.doWrite: .doW:
ld a, 3 ; seek beginning ld a, 3 ; seek beginning
call ioSeek call ioSeek
ld de, 0 ; cur line ld de, 0 ; cur line
.writeLoop: .wLoop:
push de \ pop hl push de \ pop hl
call bufGetLine ; --> buffer in (HL) call bufGetLine ; --> buffer in (HL)
jr nz, .writeEnd jr nz, .wEnd
call ioPutLine call ioPutLine
jr nz, .error jr nz, .error
inc de inc de
jr .writeLoop jr .wLoop
.writeEnd: .wEnd:
; Set new file size ; Set new file size
call ioTell call ioTell
call ioSetSize call ioSetSize
@ -108,14 +114,14 @@ edMain:
; TODO: reload buffer ; TODO: reload buffer
xor a xor a
ret ret
.doDel: .doD:
; bufDelLines expects an exclusive upper bound, which is why we inc DE. ; bufDelLines expects an exclusive upper bound, which is why we inc DE.
inc de inc de
call bufDelLines call bufDelLines
jr .mainLoop jr .mainLoop
.doAppend: .doA:
inc de inc de
.doInsert: .doI:
call stdioReadLine ; --> HL call stdioReadLine ; --> HL
call bufScratchpadAdd ; --> HL call bufScratchpadAdd ; --> HL
; insert index in DE, line offset in HL. We want the opposite. ; insert index in DE, line offset in HL. We want the opposite.
@ -124,7 +130,7 @@ edMain:
call printcrlf call printcrlf
jr .mainLoop jr .mainLoop
.doPrint: .doP:
push hl push hl
call bufGetLine call bufGetLine
jr nz, .error jr nz, .error
@ -132,10 +138,10 @@ edMain:
call printcrlf call printcrlf
pop hl pop hl
call cpHLDE call cpHLDE
jr z, .doPrintEnd jr z, .doPEnd
inc hl inc hl
jr .doPrint jr .doP
.doPrintEnd: .doPEnd:
ld (ED_CURLINE), hl ld (ED_CURLINE), hl
jp .mainLoop jp .mainLoop
.error: .error:
@ -168,12 +174,10 @@ edResolveAddr:
edReadAddrs: edReadAddrs:
ld ix, CMD_ADDR2 ld ix, CMD_ADDR2
call edResolveAddr call edResolveAddr
ex de, hl ld de, (BUF_LINECNT)
ld hl, (BUF_LINECNT) ex de, hl ; HL: cnt DE: addr2
ex de, hl ; HL: addr2 DE: cnt
call cpHLDE call cpHLDE
jp nc, unsetZ ; HL (addr2) >= DE (cnt). no good jp c, unsetZ ; HL (cnt) < DE (addr2). no good
ex de, hl ; DE: addr2
ld ix, CMD_ADDR1 ld ix, CMD_ADDR1
call edResolveAddr call edResolveAddr
ex de, hl ; HL: addr2, DE: addr1 ex de, hl ; HL: addr2, DE: addr1