;(*---------------------------------------------------------------------------
;    :Program.     TabConversions.asm
;    :Contents.	  Replace spaces with tabs and delete trailing blanks.
;    :Author.      Bernd Preusing
;    :Address.	  Gerhardstr. 16  D-2200 Elmshorn
;    :Phone.	  04121/22486
;    :Copyright.	  Public Domain
;    :Language.	  68000 Assembler
;    :Translator.  DevPac 2.0
;    :History.	  V1.0 26-Aug-89 Bernd Preusing
;    :Bugs.	  Strings go across lines. This will not be corrected!
;    :Remark.      DEF-Module is TabConversions.def
;---------------------------------------------------------------------------*)
;
	IDNT	TabConversions
	SECTION	CODE,CODE
	XDEF	TabConversions
	XDEF	SpacesToTabs

; Column	EQUR	D0
; Spaces	EQUR	D1
; TermQuote	EQUR	D2
; nt		EQUR	D3
; npos		EQUR	D4
; ch		EQUR	D5
; space		EQUR	D6 ; const
; tab		EQUR	D7 ; const
; src		EQUR	A0
; dst		EQUR	A1
; OldDst	EQUR	A2
;

; In der M2Amiga-Umgebung ist die Rettung der hier benutzten Register Unsinn!

SpacesToTabs:
	MOVE.L	A1,A2		; OldDst:=dst
	MOVEQ	#0,D0		; Column:=0
	MOVEQ	#0,D1		; Spaces:=0
	MOVEQ	#' ',D6		; D6=space-konstante
	MOVEQ	#9,D7		; D7=tab-konstante

L000001:			; repeat
	MOVE.B	(A0)+,D5	; ch:=src^; inc(src)
	CMP.B	D6,D5		; if ch=' ' then
	BNE.S	L000002
	ADDQ.W	#1,D1		; inc(Spaces)
	BRA.S	L000014
L000002:
	CMP.B	D7,D5		; elsif	ch=ht
	BNE.S	L000003
	ADD.W	D0,D1		; Spaces:=(Column+Spaces)or 7 +1-Column
	ORI.W	#$0007,D1
	ADDQ.W	#1,D1
	SUB.W	D0,D1
	BRA.S	L000014
L000003:
	CMPI.B	#$0A,D5		; if ch=eol or ch=cr then
	BEQ.S	L000004
	CMPI.B	#$0D,D5
	BNE.S	L000005
L000004:
	MOVEQ	#-1,D0		; Column:=-1
	MOVEQ	#0,D1		; Spaces:=0
	BRA.S	L000013
L000005:
	TST.W	D1		; while	Spaces>0 do
	BLE.S	L000009
	MOVE.W	D0,D3
	ORI.W	#$0007,D3
	ADDQ.W	#1,D3		; nt:=col or 7 +1
	MOVE.W	D0,D4
	ADD.W	D1,D4		; npos:=Column+Spaces
	CMP.W	D4,D3
	BLE.S	L000007		; if nt>npos then
L000006:			; repeat
	MOVE.B	D6,(A1)+	; dst^:=' '; inc(dst)
	ADDQ.W	#1,D0		; inc(Column)
	CMP.W	D0,D4
	BNE.S	L000006		; until	Column=npos
	MOVEQ	#0,D1		; Spaces:=0
	BRA.S	L000008		; else
L000007:
	MOVE.B	D7,(A1)+	; dst^:=ht; inc(dst)
	SUB.W	D3,D1
	ADD.W	D0,D1		; dec(Spaces,nt-Column)
	MOVE.W	D3,D0		; Column:=nt
L000008:
	BRA.S	L000005		; end while putSpaces

L000009:
	CMPI.B	#$22,D5		; if ch="'" or ch='"' then
	BEQ.S	L000010
	CMPI.B	#$27,D5
	BNE.S	L000013
L000010:
	MOVE.B	D5,D2		; TermQuote:=ch
L000011:			; repeat
	MOVE.B	D5,(A1)+	; dst^:=ch; inc(dst)
	CMP.B	D7,D5		; if ch=ht then
	BNE.S	L000012
	ORI.W	#$0007,D0	; col:=col or 7
L000012:			; end
	ADDQ.W	#1,D0		; inc(Column)
	MOVE.B	(A0)+,D5	; ch:=src^;inc(src)
	BEQ.S	L000013		; until	ch=0 or	ch=TermQuote
	CMP.B	D2,D5
	BNE.S	L000011
L000013:
	ADDQ.W	#1,D0		; inc(Column)
	MOVE.B	D5,(A1)+	; dst^:=ch; inc(dst)
L000014:
	BNE.S	L000001		; until	ch=nul

	SUBQ.L	#2,A1		; dec(dst,2) vor	nul
L000015:			; das geht sicher noch schneller v !!!
	CMPA.L	A2,A1
	BLT.S	L000017
	CMP.B	(A1),D6		; ch=' ' ?
	BEQ.S	L000016
	CMP.B	(A1),D7		; ch=ht?
	BNE.S	L000017
L000016:
	CLR.B	(A1)		; dst^:=nul
	SUBQ.L	#1,A1		; dec(dst)
	BRA.S	L000015
L000017:
	MOVE.L	A1,D0		; return dst+1-OldDst
	ADDQ.L	#1,D0
	SUB.L	A2,D0

TabConversions: ; in Zukunft:'TabConversionsInit:'
	RTS

	END
