Browse Source

ed: allow inserting in empty file

pull/10/head
Virgil Dupras 5 years ago
parent
commit
421d881fae
2 changed files with 31 additions and 25 deletions
  1. +2
    -0
      apps/ed/buf.asm
  2. +29
    -25
      apps/ed/main.asm

+ 2
- 0
apps/ed/buf.asm 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


+ 29
- 25
apps/ed/main.asm 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 'i'
jr z, .doI
; 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' cp 'd'
jr z, .doDel
jr z, .doD
cp 'a' cp 'a'
jr z, .doAppend
cp 'i'
jr z, .doInsert
jr .doPrint
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
.writeEnd:
jr .wLoop
.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
.doPrintEnd:
jr .doP
.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 hl, (BUF_LINECNT)
ex de, hl ; HL: addr2 DE: cnt
ld de, (BUF_LINECNT)
ex de, hl ; HL: cnt DE: addr2
call cpHLDE call cpHLDE
jp nc, unsetZ ; HL (addr2) >= DE (cnt). no good
ex de, hl ; DE: addr2
jp c, unsetZ ; HL (cnt) < DE (addr2). no good
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


Loading…
Cancel
Save