;TOSAAABIAKOAAAAAAAAAAABNLGCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPPPMKDPA
*** ScR ***

final	=1
setnamelen		=256
release			=113
releasestr	MACRO
		dc.b	"1.13"
		ENDM
version			=38
revision		=82
date		MACRO
		dc.b	"3.8.95"
		ENDM
	
versionstr	MACRO
		dc.b	version/10+'0',(version-version/10*10)+'0'
		ENDM
revisionstr	MACRO
		dc.b	revision/10+'0',(revision-revision/10*10)+'0'
		ENDM

;TIPS: Ha en flagga för varje buffert, en för ladda, en för countlines.
;      multi_loadrest kan då heta multi_update, och går igenom alla buffertar
;      och kollar om dom ska ladda in en fil, eller räkna om raderna. (nja..)


	ifeq	final-1
	PRINTT	"Final version...  :-)"
	auto	wo sys:cp
	endif

	incdir	Asminclude:
	include	devices/printer.i
	include	devices/prtbase.i
	include	dos/datetime.i
	include	dos/dos.i
	include	dos/dos_lib.i
	include	dos/dosextens.i
	include	dos/dostags.i
	include	exec/alerts.i
	include	exec/exec.i
	include	exec/exec_lib.i
	include	graphics/rastport.i
	include	graphics/graphics_lib.i
	include	graphics/gfxbase.i
	include	graphics/displayinfo.i
	include	intuition/gadgetclass.i
	include	intuition/preferences.i
	include	intuition/intuition.i
	include	intuition/intuition_lib.i
	include	libraries/asl.i
	include	libraries/asl_lib.i
	include	libraries/diskfont_lib.i
	include	libraries/gadtools.i
	include	libraries/gadtools_lib.i
	include	utility/utility.i
	include	utility/utility_lib.i
	include	utility/tagitem.i
	include	workbench/workbench.i
	include workbench/wb_lib.i
	include workbench/icon_lib.i
	include workbench/startup.i

NM_COMMANDSTRING        EQU     COMMSEQ
GTMN_NewLookMenus	EQU	GT_TagBase+67
GTLV_MakeVisible	EQU	GT_TagBase+78	; Make this item visible
WA_NewLookMenus		EQU	$80000093
WA_BusyPointer		EQU	WA_MenuHelp+6
SA_MinimizeISG		EQU	$80000049
BMA_FLAGS		EQU	12
BMF_STANDARD		EQU	8

_LVOSetWindowPointerA	EQU	-$330
_LVOLoadRGB32		EQU	-882
_LVOGetRGB32		EQU	-900
_LVOGetUniqueID		EQU	-270
_LVOGetBitMapAttr	EQU	-960

	include	libraries/ppbase.i
	include	libraries/powerpacker_lib.i
	include	libraries/xpk.i

	incdir	include:
	include	hardware/custom.i
	include	hardware/intbits.i
	include	macros.i

Message	MACRO
	base	intui
	move.l	windowptr(pc),a0
	move.l	#-1,a1
	lea	\1,a2
	call	SetWindowTitles
	ENDM
wm	macro
	btst	#6,$bfe001
	bne.s	*-8
	btst	#6,$bfe001
	beq.s	*-8
	endm
fl	macro
	move.w	#$ffff,d0
	move.w	d0,$dff180
	dbf	d0,*-6
	endm

 STRUCTURE cm,MN_SIZE
   UWORD   cm_command
   ULONG   cm_data
   LABEL   cm_SIZE

cmc_open	=1	;data: pek till lws: filnamn, setname, tabsize
cmc_opennew	=2	; -"-
cmc_quit	=3	;N/A

err_exitnow	=-1
err_noscreen	=-2
err_nowin	=-3
err_nomenu	=-4
err_nogadget	=-5
err_nofontreq	=-6
err_nofilereq	=-7
err_nomem	=-8
err_nochipmem	=-9
err_nofont	=-10


;SHIFTMENU	macro
;	and.w	#$1F,\1
;	endm
;SHIFTITEM	macro
;	and.w	#$3F,\1
;	lsl.w	#5,\1
;	emdm
;SHIFTSUB	macro
;	and.w	#$1F,\1
;	lsl.w	#11,\1
;	emdm

s_exit		=0
TAB		=9

start:
		ifeq final-1
	include	startup.i
	move.l	a0,wbmessage
	move.l	d0,boolcli
		endif
	jmp	realstart

stayalive:				;Only exec may be used here!
	base	exec

		ifeq final-1
	lea	dosname,a1
	moveq	#0,d0
	call	OpenLibrary
	move.l	d0,a6
	lea	start-4(pc),a0
	move.l	(a0),d1			;Other segments
	clr.l	(a0)			;Detach
	call	UnLoadSeg		;Free
	move.l	a6,a1
	base	exec
	call	CloseLibrary
		endif

	move.l	comport(pc),a0
	call	WaitPort
.getallcommsgs
	move.l	comport(pc),a0
	call	GetMsg
	tst.l	d0
	beq.w	.lastmess
	move.l	d0,a1
	move.w	cm_command(a1),d2
	call	ReplyMsg
	cmp.w	#cmc_quit,d2
	beq.b	terminate
	bra.s	.getallcommsgs
.lastmess

terminate:
	base	exec
	move.l	commessage(pc),d0
	move.l	d0,a1
	beq.b	.nocommessage
	moveq	#cm_SIZE,d0
	call	FreeMem
.nocommessage
	tst.w	comportispublic
	beq.b	.comnotadded
	move.l	comport(pc),d0
	move.l	d0,a1
	beq.b	.nocomport
	call	RemPort
.comnotadded
	move.l	comport(pc),d0
	move.l	d0,a0
	beq.b	.nocomport
	call	DeleteMsgPort
.nocomport

	moveq	#0,d0
	rts


comport:	dc.l	0
commessage:	dc.l	0
comportispublic:dc.w	0

		RSRESET
coma_version:	rs.l	1	;cli/wb
coma_setname:	rs.l	1	;cli/wb
coma_boolcli:	rs.l	1	;cli/wb
coma_tabsize:	rs.l	0	;cli
coma_numargs:	rs.l	1	;wb
coma_argnames:	rs.l	0	;cli
coma_arglist:	rs.l	1	;wb
coma_currdir:	rs.l	1	;cli, LOCK!
coma_SIZE:	rs.l	0


	section	realcode,code
realstart:
	jsr	openlibs
	tst.l	d0
	bmi.w	exit_nolibs

	jsr	fixwinmenu		;if not V39+, remove "Esc", "Tab" etc
	jsr	multi_init
	tst.l	d0
	beq	exit

		ifeq final-1
	jsr	getargs
	tst.l	d0
	bmi.w	exit_argerr
		endif

	base	exec
	call	CreateMsgPort
	lea	comport,a0
	move.l	d0,(a0)
	beq.w	exit

	tst.l	argnew
	bne.w	.startnewcopy

	base	exec
	moveq	#cm_SIZE,d0
	move.l	#MEMF_PUBLIC!MEMF_CLEAR,d1
	call	AllocMem
	lea	commessage,a0
	move.l	d0,(a0)
	beq.w	exit

	move.l	commessage,a1
	move.l	comport,MN_REPLYPORT(a1)
	move.w	#cm_SIZE,MN_LENGTH(a1)
	move.w	#cmc_opennew,cm_command(a1)
	lea	com_array,a0
	move.l	a0,cm_data(a1)

	suba.l	a1,a1
	call	FindTask
	move.l	d0,a1

	lea	com_array,a0
	move.l	#release,coma_version(a0)
	move.l	pr_CurrentDir(a1),coma_currdir(a0)
	move.l	argset(pc),coma_setname(a0)
	move.l	boolcli(pc),coma_boolcli(a0)
	bne.w	.cli
	move.l	wbmessage(pc),a1
	move.l	sm_NumArgs(a1),coma_numargs(a0)
	move.l	sm_ArgList(a1),coma_arglist(a0)
	bra.b	.messageinited
.cli
	move.l	tabsize(pc),coma_tabsize(a0)
	move.l	argnames(pc),coma_argnames(a0)
.messageinited
	base	exec
	call	Forbid
	lea	programname(pc),a1
	call	FindPort
	tst.l	d0
	beq.b	.noprevcomport

	move.l	d0,a0
	move.l	commessage,a1
	call	PutMsg
	call	Permit

	move.l	comport,a0
	call	WaitPort
.getallcommsgs
	move.l	comport,a0
	call	GetMsg
	tst.l	d0
	bne.s	.getallcommsgs

	bsr.w	cleanup
	jmp	stayalive

.noprevcomport
	call	Permit

.startnewcopy
	move.l	comport,a1
	lea	programname(pc),a0
	move.l	a0,LN_NAME(a1)
	move.b	#1,LN_PRI(a1)
	call	AddPort
	move.w	#1,comportispublic

	lea	bufflist(pc),a0
	TSTNODE	a0,a0
	beq.w	exit
	lea	thisbuff(pc),a1
	move.l	a0,(a1)
	bsr	multi_buff2curr

	lea	bufflist(pc),a5
	IFEMPTY	a5,.listisempty
	move.l	MLH_HEAD(a5),a5
	bsr	multi_loadrest
	tst.l	d0
	bne.b	exit
.listisempty

*** int server
	base	exec
	moveq	#-1,d0
	call	AllocSignal
	lea	intsignal(pc),a0
	move.l	d0,(a0)
	cmp.l	#-1,d0
	beq.w	exit

	lea	int,a1
	move.b	#NT_INTERRUPT,LN_TYPE(a1)
	move.b	#5,LN_PRI(a1)
	lea	intname(pc),a0
	move.l	a0,LN_NAME(a1)
	lea	intsignal(pc),a0
	move.l	a0,IS_DATA(a1)
	lea	interruptet(pc),a0
	move.l	a0,IS_CODE(a1)
	moveq	#INTB_VERTB,d0
	call	AddIntServer


	bsr.w	handlesignals
	bsr	mopenwb

*******************************************************************************

exit:
	bsr.b	cleanup
	jmp	terminate

cleanup:
	base	exec
	move.l	intsignal(pc),d0
	cmp.l	#-1,d0
	beq.b	.nointsignal
	call	FreeSignal
	lea	int,a1
	moveq	#INTB_VERTB,d0
	call	RemIntServer
.nointsignal

	bsr	multi_quit
	bsr.w	closeenvironment
exit_argerr:
	base	dos
	move.l	rastruct(pc),d1
	beq.b	.noargs
	call	FreeArgs
.noargs
	lea	pipeused(pc),a0
	tst.b	(a0)
	bne.b	.nopipe

	base	dos
	lea	pipename(pc),a0
	move.l	a0,d1
	call	DeleteFile
.nopipe

exit_nolibs:
	bsr.w	closelibs
	rts

*******************************************************************************

handlesignals:

;*** slav!
;	base	dos
;	lea	.dostags(pc),a0
;	move.l	a0,d1
;	call	CreateNewProc
;	lea	slave(pc),a0
;	move.l	d0,(a0)
;	beq.w	.noslave
.loop
	base	exec
	moveq	#0,d0
	move.l	intsignal(pc),d1
	bset	d1,d0
	call	Wait			;the intserver signals each vblank..

	move.l	windowptr(pc),a0
	move.l	wd_UserPort(a0),a0
	move.b	MP_SIGBIT(a0),d1
	moveq	#0,d0
	bset	d1,d0
	lea	.usersignal(pc),a0
	move.l	d0,(a0)

	move.l	comport,a0
	move.b	MP_SIGBIT(a0),d1
	moveq	#0,d0
	bset	d1,d0
	lea	.comsignal(pc),a0
	move.l	d0,(a0)

	or.l	.usersignal(pc),d0
	bset	#SIGBREAKB_CTRL_C,d0

	lea	.signal(pc),a0
	move.l	d0,(a0)

	moveq	#0,d0
	moveq	#0,d1
	call	SetSignal
	and.l	.signal(pc),d0
	beq.s	.scrolla		;no signals

	base	exec
	lea	.signal(pc),a0
	move.l	d0,(a0)
	call	Wait			;fetch and clear
	lea	.signal(pc),a0
	move.l	d0,(a0)

	move.l	.signal(pc),d1
	and.l	.usersignal(pc),d1	;userport signal?
	beq.B	.nouserport
	bsr.w	handleIDCMP		;yep
	tst.l	d0
	bmi.b	.exit
.nouserport
	move.l	.signal(pc),d1
	and.l	.comsignal(pc),d1	;comport signal?
	beq.B	.nocomport
	bsr	handleCOM		;yep
.nocomport
	move.l	.signal(pc),d1
	and.l	#SIGBREAKF_CTRL_C,d1
	beq.B	.nobreak
	lea	status(pc),a0
	bset	#s_exit,(a0)		;if ctrl_c from cli, quit!
.nobreak
.scrolla
	move.w	scrolla(pc),d0
	beq.s	.noscroll
	cmp.w	#scrolla_mouse,d0
	bne.s	.dontupdatespeed
	moveq	#0,d0
	move.w	mousey(pc),d0
	mulu.w	#9,d0
	divu.w	pheight+2(pc),d0
	subq.w	#4,d0
	cmp.w	#4,d0
	ble.s	.speedok
	moveq	#4,d0
.speedok
	lea	scrollfart(pc),a0
	move.w	d0,(a0)
	bra.s	.scrollit
.dontupdatespeed
	cmp.w	#scrolla_crsr,d0
	bne.s	.noscroll
	clr.w	scrolla			;key pressed, clear.
.scrollit
	move.w	scrollfart(pc),d0
	bsr	scrollsmooth
.noscroll

	lea	status(pc),a0
	btst	#s_exit,(a0)
	beq.w	.loop

.exit

;	lea	slavecmd+slavemessage(pc),a0
;	move.w	#-1,(a0)		;tala om att avsluta!
;	bsr	slavesendmsg
;.noslave
	rts

.signal		dc.l	0
.usersignal	dc.l	0
.comsignal	dc.l	0

;.dostags
;	dc.l	NP_Entry,slavefunc
;	dc.l	NP_Name,.slavename
;	dc.l	TAG_DONE
;.slavename
;	dc.b	"Textread slave process",0

scrolla_mouse:	equ	1
scrolla_crsr:	equ	2
scrolla:	dc.w	0
scrollfart:	dc.w	0
mousey:		dc.w	0

intsignal:	dc.l	-1

interruptet:
	base	exec
	move.l	(a1),d1
	moveq	#0,d0
	bset	d1,d0
	move.l	thistask(pc),a1
	call	Signal
	moveq	#0,d0
	rts

*******************************************************************************
********************** C O M  *************************************************
*******************************************************************************
handleCOM:
.loop
	base	exec
	move.l	comport,a0
	call	GetMsg
	tst.l	d0
	beq.w	.exit
	move.l	d0,.mess
	move.l	d0,a0
	cmp.w	#cmc_opennew,cm_command(a0)
	bne.b	.notopennew
	pushm	a0
	bsr	mnew
	popm	a0
	bra.b	.open
.notopennew
	cmp.w	#cmc_open,cm_command(a0)
	bne.b	.notopen
.open
	lea	.array(pc),a1
	move.l	cm_data(a0),(a1)

	lea	.thisbuff(pc),a0
	move.l	thisbuff(pc),(a0)

	move.l	.array(pc),a0
	cmp.l	#release,coma_version(a0)
	bne.b	.notopen
	move.l	coma_setname(a0),a0
	bsr	loadsettings
	bsr	prefs2curr
	move.l	.array(pc),a0
	tst.l	coma_boolcli(a0)
	bne.b	.cli
	move.l	coma_numargs(a0),d1
	move.l	coma_arglist(a0),a2
	bsr.w	parsewbarg
	bra.b	.open_buffscreated
.cli
	lea	tabsize(pc),a1
	move.l	coma_tabsize(a0),(a1)
	move.l	coma_argnames(a0),a2
	move.l	.array(pc),a0
	move.l	coma_currdir(a0),d0
	bsr.w	parsefilenamearray
.open_buffscreated
	move.l	.thisbuff(pc),a5
	bsr	multi_loadrest
	move.l	.mess(pc),a0
	;remeber who signalled us, send a quit message when the buffer
	;is closed later on (multi_freebuff)
	move.l	MN_REPLYPORT(a0),buff_commessageport(a5)
.notopen
	base	exec
	move.l	.mess(pc),a1
	call	ReplyMsg
	bra.w	.loop
.exit
	rts
.mess		dc.l	0
.array		dc.l	0
.thisbuff	dc.l	0

*******************************************************************************
********************** I D C M P **********************************************
*******************************************************************************
handleIDCMP:
	base	gt
	move.l	windowptr(pc),a0
	move.l	wd_UserPort(a0),a0
	call	GT_GetIMsg
	tst.l	d0
	beq.w	.exit
	moveq	#0,d3
	moveq	#0,d4
	moveq	#0,d5
	moveq	#0,d6
	move.l	d0,a0
	move.l	im_IAddress(a0),a2
	move.l	im_Class(a0),d2
	move.w	im_Code(a0),d3
	move.w	im_Qualifier(a0),d4
	move.w	im_MouseX(a0),d5
	move.w	im_MouseY(a0),d6
		movem.l	a0/a6,-(sp)
		lea	mousey(pc),a1
		move.w	d6,(a1)
		cmp.l	#IDCMP_MENUVERIFY,d2
		bne.w	.notmenuverify
		lea	scrolla(pc),a0
		clr.w	(a0)
.notmenuverify
		movem.l	(sp)+,a0/a6

	move.l	a0,a1
	call	GT_ReplyIMsg

	pea	.return(pc)		;-<>-

	cmp.l	#IDCMP_MENUPICK,d2
	beq.B	menupick
	cmp.l	#IDCMP_RAWKEY,d2
	beq.w	rawkey
	cmp.l	#IDCMP_VANILLAKEY,d2
	beq.w	vanillakey
	cmp.l	#IDCMP_REFRESHWINDOW,d2
	beq.w	refreshwindow
	cmp.l	#IDCMP_MOUSEMOVE,d2
	beq.w	handlegadgets
	cmp.l	#IDCMP_GADGETUP,d2
	beq.w	handlegadgets
	cmp.l	#IDCMP_MOUSEBUTTONS,d2
	beq.w	handlebuttons
	cmp.l	#IDCMP_INACTIVEWINDOW,d2
	bne.w	.notinactive
	lea	scrolla(pc),a0
	clr.w	(a0)
.notinactive

	addq.l	#4,sp			;-<>-	clean up!

	bra.w	handleIDCMP
.return
	tst.l	d0
	bne.b	.exit
	bra.w	handleIDCMP
.exit
	rts

*********************************************************************** project

menupick:
.loop
	base	intui
	moveq	#0,d0
	move.w	d3,d0			;code=menu number
	move.l	winmenuptr(pc),a0
	call	ItemAddress
	lea	.item(pc),a0
	move.l	d0,(a0)
	move.l	d0,a0
	move.l	d0,a1
	cmp.w	#MENUNULL,d3
	beq.b	.end
	pea	.cont(pc)
	GTMENUITEM_USERDATA	a1,a1
	move.l	a1,d0			;test!
	beq.b	.end
	jmp	(a1)			;a0: pointer to menu structure
.end
	moveq	#0,d0
.exit
	rts
.cont
	tst.l	d0
	bne.b	.exit
	move.l	.item(pc),a0
	move.w	mi_NextSelect(a0),d3
	bra.B	.loop
.item	dc.l	0

mnew:
	bsr	multi_new
	tst.l	d0
	bne.b	.error
	moveq	#1,d0
.error
	rts
mopen:
	moveq	#FALSE,d0			;Open!
	bsr.w	askfile
	tst.l	d0
	beq.b	.cancel
	
	bsr.w	openfile
.cancel
	moveq	#0,d0
	rts

;	lea	.thisbuff(pc),a0
;	move.l	thisbuff(pc),(a0)
;	move.l	coma_numargs(a0),d1
;	move.l	coma_arglist(a0),a2
;	bsr.w	parsewbarg
;	move.l	.thisbuff(pc),a5
;	bsr	multi_loadrest


mopennew:
	bsr	mnew
	cmp.l	#1,d0
	bne.b	.error
	bsr.b	mopen
	moveq	#1,d0
.error
	rts

msave:
	lea	savetext(pc),a1
	lea	savegadget(pc),a2
	lea	.args(pc),a4
	bsr	requester

	tst.l	d0
	beq.b	.exit
	lea	filereqdir(pc),a0
	lea	filename(pc),a1
	lea	temp,a2
	move.w	#tempsize,d0
	bsr.w	pathnfiletobuff

	move.l	#temp,d0
	bsr	savefile
.exit
	moveq	#0,d0
	rts
.args
	dc.l	filename

msaveas:
	moveq	#TRUE,d0		;Save!
	bsr.w	askfile
	tst.l	d0
	beq.b	.cancel

	bsr.w	savefile
.cancel
	moveq	#0,d0
	rts
mprint:
	jmp	printfile
mprintas:
	jsr	requestprintopts
	subq.l	#1,d0
	beq.b	mprint
	moveq	#0,d0
	rts

mreveal:
	move.l	thisbuff(pc),a0
	bsr	multi_curr2buff		;update list!
	jsr	requestreveal
	tst.l	d0
	bmi.s	.exit
	beq.s	.cancel
	move.l	bnum(pc),d0
	subq.l	#1,d0
	bsr	multi_change
	bmi.b	.exit			;can't happen.. (?) :)
.cancel
	moveq	#1,d0
.exit
	rts

mclose:
	lea	bufflist(pc),a0
	move.l	MLH_HEAD(a0),d0
	cmp.l	MLH_TAILPRED(a0),d0
	beq.w	mquit			;quit last time

	bsr	multi_close
	tst.l	d0
	bne.b	.exit
	bsr	updatemenu
	moveq	#1,d0
.exit
	rts

miconify:
	bsr.b	mopenwb
	bra.w	iconify

mopenwb:
	base	intui
	call	OpenWorkBench
	moveq	#0,d0
	rts

mclosewb:
	base	intui
	call	CloseWorkBench
	moveq	#0,d0
	rts

mabout:
	lea	.fill(pc),a4
	move.l	filelen(pc),(a4)+
	base	exec
	moveq	#0,d1
	call	AvailMem
	move.l	d0,(a4)+
	move.l	#MEMF_LARGEST,d1
	call	AvailMem
	move.l	d0,(a4)+
	lea	abouttext(pc),a1
	lea	oktext(pc),a2
	lea	.args(pc),a4
	bsr.w	requester
	moveq	#0,d0
	rts
.args	dc.l	.releasestr
	dc.l	.versionstr
	dc.l	filename
.fill	dc.l	0,0,0

.releasestr
	releasestr
	dc.b	0
.versionstr	
	versionstr
	dc.b	"."
	revisionstr
	dc.b	0

	even
mquit:
	lea	status(pc),a0
	bset	#s_exit,(a0)
	moveq	#1,d0
.exit
	rts

********************************************************************** settings

wordwrap:
	dc.w	0
mwordwrap:
	lea	wordwrap(pc),a1
	bsr.w	checkmark
	tst.l	d0
	beq.b	.nochange
	bsr.w	countlines
	tst.l	d0
	bne.b	.exit
	bsr.w	textredraw
.nochange
	moveq	#0,d0
.exit
	rts
sysfont:
	dc.w	0
msysfont:
	lea	sysfont(pc),a1
	bsr.w	checkmark
	tst.l	d0
	beq.b	.nochange
	bsr.w	changevideomode
	tst.l	d0
	bne.b	.exit
	moveq	#1,d0
.exit
	rts
.nochange
	moveq	#0,d0
	rts
alwaysremansi:
	dc.w	0
malwaysremansi:
	lea	alwaysremansi(pc),a1
	bsr.w	checkmark
	moveq	#0,d0
	rts
alwaysremcr:
	dc.w	0
malwaysremcr:
	lea	alwaysremcr(pc),a1
	bsr.w	checkmark
	moveq	#0,d0
	rts
nofontfake:
	dc.w	0
mnofontfake:
	lea	nofontfake(pc),a1
	bsr.w	checkmark
	tst.l	d0
	beq.b	.nochange
	moveq	#TRUE,d0
	bsr.w	fixafonten
	bsr	recountlines
	bsr.w	textredraw
.nochange
	moveq	#0,d0
	rts
legalprint:
	dc.w	0
mlegalprint: 
	lea	legalprint(pc),a1
	bsr.w	checkmark
	tst.l	d0
	beq.b	.nochange
	clr.w	oldfontsize		;force update of font
	tst.w	(a1)
	beq.b	.fast
	lea	nofontfake(pc),a0
	move.w	#TRUE,(a0)
.fast
	bsr.w	updatemenu
	bsr.w	updatecheckmarks

	moveq	#TRUE,d0
	bsr.w	fixafonten
	bsr	recountlines
	bsr.w	textredraw
.nochange
	moveq	#0,d0
.error
	rts
mtabsize:
	jsr	requesttabsize
	tst.l	d0
	bmi.s	.exit
	beq.s	.cancel
	lea	tabbar,a0
	bsr.w	fixatabbar
	bsr.w	countlines
	tst.l	d0
	bne.b	.exit
.ok
	bsr.w	textredraw
.cancel
	moveq	#0,d0
.exit
	rts
;in:
* a0	pek på (word) strukt: MinX, MinY, MaxX, MaxY
getfontlimits:
	move.l	a0,a1
	moveq	#8,d0
	move.w	d0,(a0)+
	move.w	d0,(a0)+
	move.w	d0,(a0)+
	move.w	d0,(a0)+

	lea	legalprint(pc),a0
	tst.w	(a0)
	beq.b	.exit

	move.l	pwidth(pc),d0
	divu.w	#254,d0			;max chars/row
	cmp.w	#5,d0
	bpl.b	.xgt5
	moveq	#5,d0
.xgt5
	move.w	d0,(a1)			;MinX

	move.l	pwidth(pc),d0
	divu.w	#80,d0			;min chars/row
	move.w	d0,4(a1)		;MaxX

	move.l	pheight(pc),d0
	sub.l	barheight(pc),d0
	sub.l	fonty(pc),d0
	subq.l	#4,d0			;space for bevelbox
	move.l	d0,d1

	divu.w	#254,d1			;max rows
	cmp.w	#5,d1
	bpl.b	.ygt5
	moveq	#5,d1
.ygt5
	move.w	d1,2(a1)		;MinY

	divu.w	#20,d0			;min rows
	move.w	d0,6(a1)		;MaxY
.exit
	rts

mscreen:
	base	asl
	cmp.w	#38,LIB_VERSION(a6)
	bhs.w	.v38
	lea	smodetext(pc),a1
	lea	smodegadget(pc),a2
	suba.l	a4,a4
	bsr	requester
	tst.l	d0
	beq.w	.error
	lea	displayid(pc),a0
	cmp.w	#1,d0
	bne.w	.not_pal
	move.l	#PAL_MONITOR_ID|HIRES_KEY,(a0)
.not_pal
	cmp.w	#2,d0
	bne.w	.not_pal_lace
	move.l	#PAL_MONITOR_ID|HIRESLACE_KEY,(a0)
.not_pal_lace
	cmp.w	#3,d0
	bne.w	.not_ntsc
	move.l	#NTSC_MONITOR_ID|HIRES_KEY,(a0)
.not_ntsc
	cmp.w	#4,d0
	bne.w	.not_ntsc_lace
	move.l	#NTSC_MONITOR_ID|HIRESLACE_KEY,(a0)
.not_ntsc_lace
	cmp.w	#5,d0
	bne.w	.not_vga_prod
	move.l	#VGA_MONITOR_ID|VGAPRODUCT_KEY,(a0)
.not_vga_prod
	bra.w	.changeit
.v38
	lea	.tagswin(pc),a0
	move.l	windowptr(pc),(a0)
	lea	.tagsdispid(pc),a0
	move.l	displayid(pc),(a0)

	lea	.scrnreqhgt(pc),a0
	move.l	pheight(pc),d0
	mulu	#7,d0
	lsr.l	#3,d0
	move.l	d0,(a0)

	moveq	#ASL_ScreenModeRequest,d0
	lea	.tags(pc),a0
	call	AllocAslRequest
	lea	.screenreq(pc),a0
	move.l	d0,(a0)
	beq.w	.error

	move.l	.screenreq(pc),a0
	lea	tag_done(pc),a1
	call	AslRequest
	tst.l	d0
	beq.b	.cancel

	move.l	.screenreq(pc),a0
	lea	displayid(pc),a1
	move.l	sm_DisplayID(a0),(a1)
	base	asl
	move.l	.screenreq(pc),a0
	call	FreeAslRequest
.changeit
	bsr.w	changevideomode
	tst.l	d0
	bne.b	.exit
	moveq	#1,d0			;process no more menus
.exit
	rts
.cancel
	base	asl
	move.l	.screenreq(pc),a0
	call	FreeAslRequest
.error
	moveq	#0,d0
	rts
.screenreq
	dc.l	0
.tags
	dc.l	ASLSM_Window
.tagswin
	dc.l	0
	dc.l	ASLSM_SleepWindow,TRUE
	dc.l	ASLSM_InitialDisplayID
.tagsdispid
	dc.l	0
	dc.l	ASLSM_PropertyFlags,DIPF_IS_WB
	dc.l	ASLSM_PropertyMask,DIPF_IS_WB
	dc.l	ASLSM_FilterFunc,.hook
	dc.l	ASLSM_InitialHeight
.scrnreqhgt
	dc.l	100
	dc.l	TAG_DONE

.entry
	move.l	h_SubEntry(a0),a0
	jsr	(a0)
	rts
.subentry
	move.l	a6,-(sp)
	base	intui
	move.l	a1,a0			;displayid
	lea	temp,a1
	clr.l	(a1)
	clr.l	4(a1)			;just some cleanup
	moveq	#OSCAN_TEXT,d0
	call	QueryOverscan

	lea	temp,a0
	move.w	ra_MaxX(a0),d0
	sub.w	ra_MinX(a0),d0
	cmp.w	#639,d0
	blo.s	.false
	move.w	ra_MaxY(a0),d0
	sub.w	ra_MinY(a0),d0
	cmp.w	#199,d0
	blo.s	.false

	move.l	(sp)+,a6
	moveq	#TRUE,d0
	rts
.false
	move.l	(sp)+,a6
	moveq	#FALSE,d0
	rts
.hook
	ds.b	MLN_SIZE
	dc.l	.entry
	dc.l	.subentry
	dc.l	NULL

mscreencol:
	bsr	lockwindow
	base	dos
	lea	.command(pc),a0
	move.l	a0,d1
	lea	.tags(pc),a0
	move.l	a0,d2
	call	SystemTagList
	tst.l	d0
	beq.b	.exit
	bsr	unlockwindow
	lea	colerrortext(pc),a1
	lea	oktext(pc),a2
	suba.l	a4,a4
	bsr	requester
	bsr	lockwindow
.exit
	lea	palette(pc),a1
	bsr	getpalette
	bsr	unlockwindow
	moveq	#0,d0
	rts

.command
	dc.b	"SYS:Tools/Colors",0
	even
.tags	
	dc.l	SYS_UserShell,TRUE
	dc.l	TAG_DONE

mdefcol:
	lea	defpalette,a1
	bsr	changepalette
	moveq	#0,d0
	rts

mtextfont:
	lea	.minx(pc),a0
	bsr	getfontlimits

	lea	.freqminheight+2(pc),a0
	move.w	.miny(pc),(a0)
	lea	.freqmaxheight+2(pc),a0
	move.w	.maxy(pc),(a0)

	lea	savedfont,a0
	lea	savedfontname,a1
	bsr.w	savefont

	lea	.freqdefheight(pc),a0
	move.w	thisfont+ta_YSize(pc),2(a0)
	lea	.window(pc),a0
	move.l	windowptr(pc),(a0)
	lea	.fontreqhgt(pc),a0
	move.l	pheight(pc),d0
	mulu	#7,d0
	lsr.l	#3,d0
	move.l	d0,(a0)

	base	asl
	moveq	#ASL_FontRequest,d0
	lea	.tags(pc),a0
	call	AllocAslRequest
	lea	.fontreqptr(pc),a0
	move.l	d0,(a0)
	beq.s	.exit
.selectagain
	base	asl
	move.l	.fontreqptr(pc),a0
	lea	tag_done(pc),a1
	call	AslRequest
	tst.l	d0
	beq.B	.cancel

	move.l	.fontreqptr(pc),a0
	lea	fo_Attr(a0),a0

	bsr.w	changefont
	tst.l	d0
	bne.b	.selectagain

	move.l	myfont(pc),a1

	move.w	.miny(pc),d0
	cmp.w	tf_YSize(a1),d0
	bhi.b	.selectagain		;<min?
	move.w	.maxy(pc),d0
	cmp.w	tf_YSize(a1),d0
	blo.b	.selectagain		;>max?

	move.w	.minx(pc),d0
	cmp.w	tf_XSize(a1),d0
	bhi.b	.selectagain		;<min?
	move.w	.maxx(pc),d0
	cmp.w	tf_XSize(a1),d0
	blo.b	.selectagain		;>max?
	bra.s	.exit
.cancel
	lea	savedfont,a0
	bsr.w	changefont
.exit
	base	asl
	move.l	.fontreqptr(pc),a0
	clr.l	.fontreqptr
	call	FreeAslRequest

	moveq	#FALSE,d0
	bsr.w	fixafonten
	bsr	recountlines
	bsr.w	textredraw
	moveq	#0,d0
.error
	rts

.minx	dc.w	0
.miny	dc.w	0
.maxx	dc.w	0
.maxy	dc.w	0

.entry
	base	diskfont
	move.l	a6,d0
	beq.b	.hookrettrue		;If no diskfont.library, return(1)

	btst.b	#FPB_PROPORTIONAL,ta_Flags(a1)
	bne.b	.hookfelfont

	move.w	.miny(pc),d0
	cmp.w	ta_YSize(a1),d0
	bhi.b	.hookfelfont		;<min?
	move.w	.maxy(pc),d0
	cmp.w	ta_YSize(a1),d0
	blo.b	.hookfelfont		;>max?

	move.l	a1,a0
	call	OpenDiskFont
	tst.l	d0
	beq.b	.hookrettrue		;If error, return(1)
	move.l	d0,a0

	move.w	tf_XSize(a0),d2

	base	gfx
	move.l	d0,a1
	call	CloseFont

	move.w	.minx(pc),d0
	cmp.w	d2,d0
	bhi.b	.hookfelfont		;<min?
	move.w	.maxx(pc),d0
	cmp.w	d2,d0
	blo.b	.hookfelfont		;>max?

.hookrettrue
	moveq	#TRUE,d0
	rts
.hookfelfont
	moveq	#FALSE,d0
	rts
.hook
	ds.b	MLN_SIZE
	dc.l	.entry
	dc.l	NULL
	dc.l	NULL
.tags
	dc.l	ASLFO_Window
.window	dc.l	0
	dc.l	ASLFO_SleepWindow,TRUE
	dc.l	ASLFO_FixedWidthOnly,TRUE
	dc.l	ASLFO_InitialSize
.freqdefheight
	dc.l	8
	dc.l	ASLFO_InitialName,thisfontname
	dc.l	ASLFO_MinHeight
.freqminheight
	dc.l	8
	dc.l	ASLFO_MaxHeight
.freqmaxheight
	dc.l	8
	dc.l	ASLFO_FilterFunc,.hook
	dc.l	ASLFO_InitialHeight
.fontreqhgt
	dc.l	100
	dc.l	TAG_DONE
.fontreqptr	dc.l	0
	even

mdeffont:
	lea	legalprint(pc),a0
	tst.w	(a0)
	bne.b	.legal

	lea	topaz8font(pc),a0
	bsr.w	changefont
	bra.b	.fontok
.legal
	lea	deffont,a0
	bsr.w	changefont
.fontok
	moveq	#FALSE,d0
	bsr.w	fixafonten
	bsr	recountlines
	bsr.w	textredraw
	moveq	#0,d0
.error
	rts

mloadset:
.again
	lea	setfreqtitle(pc),a0
	lea	loadsettitle(pc),a1
	move.l	a1,(a0)
	move.l	#FALSE,setfreqsave
	base	asl
	move.l	setfreqptr(pc),a0
	lea	setfreqtags(pc),a1
	call	AslRequest
	tst.l	d0
	beq.s	.error

	move.l	setfreqptr(pc),a0
	move.l	fr_File(a0),a1
	move.l	fr_Drawer(a0),a0
	lea	temp,a2
	move.w	#tempsize,d0
	bsr.w	pathnfiletobuff

	lea	temp,a0
	bsr	loadsettings
	tst.l	d0
	bne.b	.again			;error!
	bsr.w	mloadset_changeenv
	tst.l	d0
	bne.b	.error
.exit
	moveq	#0,d0
.error
	rts

;in:
* d1	gammal sysfont
* d2	gammal screenmode
mloadset_changeenv:
	bsr	prefs2curr

	bsr	changeenv
	tst.l	d0
	bne.b	.error
	bsr.w	recountlines
	tst.l	d0
	bne.b	.error

	moveq	#1,d0
.error
	rts

msaveset:
	lea	setname,a0
	tst.b	(a0)
	bne.b	.saveit

	base	dos
	lea	defsetname(pc),a2
	move.l	a2,d1
	call	PathPart
	lea	temp,a0
	move.l	a2,a1
.loop
	move.b	(a1)+,(a0)+
	cmp.l	a1,d0
	bne.b	.loop
	clr.b	(a0)
	move.l	#temp,d1
	moveq	#ACCESS_READ,d2
	call	Lock
	tst.l	d0
	bne.b	.unlock
	move.l	#temp,d1
	call	CreateDir
.unlock
	move.l	d0,d1
	call	UnLock				;NULL is ok

	move.l	a2,a0
.saveit
	bsr.w	savesettings
	tst.l	d0
	beq.b	.exit
	lea	savesetmessage(pc),a1
	lea	oktext(pc),a2
	suba.l	a4,a4
	bsr	requester
.exit
	moveq	#0,d0
	rts
msavesetas:
.again
	lea	setfreqtitle(pc),a0
	lea	savesettitle(pc),a1
	move.l	a1,(a0)
	move.l	#TRUE,setfreqsave
	base	asl
	move.l	setfreqptr(pc),a0
	lea	setfreqtags(pc),a1
	call	AslRequest
	tst.l	d0
	beq.s	.error

	move.l	setfreqptr(pc),a0
	move.l	fr_File(a0),a1
	move.l	fr_Drawer(a0),a0
	lea	temp,a2
	move.w	#tempsize,d0
	bsr.w	pathnfiletobuff

	lea	temp,a0
	bsr.W	savesettings
	tst.l	d0
	bmi.b	.again
.exit
	moveq	#0,d0
.error
	rts

;in:
* a0	filename
;ut:
* d0	0 if no error, else -1
loadsettings:
	pushm	d1-a6

	lea	.filename(pc),a1
	move.l	a0,(a1)

	bsr	curr2prefs

	move.l	.filename(pc),a0
	bsr	loadfile
	tst.l	d0
	bne.w	.error
	lea	.addr(pc),a1
	move.l	a0,(a1)
	lea	.memlen(pc),a1
	move.l	d1,(a1)
	lea	.len(pc),a1
	move.l	d2,(a1)

	lea	prefs_id(pc),a1
.testloop
	move.b	(a1)+,d0
	beq.b	.testok
	cmp.b	(a0)+,d0
	bne.w	.error
	bra.b	.testloop
.testok

	base	dos

	lea	prefs_longs(pc),a2
.longloop
	move.l	(a2)+,d0
	beq.b	.nomorelongs
	move.l	d0,a1
	move.l	.addr(pc),a0
	move.l	.len(pc),d0
	bsr	.getarg
	move.l	(a2)+,d2
	move.l	d0,d1
	beq.b	.longloop
	call	StrToLong
	bra.b	.longloop
.nomorelongs

	lea	prefs_words(pc),a2
.wordloop
	move.l	(a2)+,d0
	beq.b	.nomorewords
	move.l	d0,a1
	move.l	.addr(pc),a0
	move.l	.len(pc),d0
	bsr	.getarg
	move.l	(a2)+,a3
	lea	.templong(pc),a0
	move.l	a0,d2
	move.l	d0,d1
	beq.b	.wordloop
	call	StrToLong
	move.w	.templong+2(pc),(a3)
	bra.b	.wordloop
.nomorewords

	lea	prefs_strings(pc),a2
.stringloop
	move.l	(a2)+,d0
	beq.b	.nomorestrings
	move.l	d0,a1
	move.l	.addr(pc),a0
	move.l	.len(pc),d0
	bsr	.getarg
	move.l	(a2)+,a1
	tst.l	d0
	beq.b	.stringloop
	move.l	d0,a0
.stringcopy
	move.b	(a0)+,d0
	cmp.b	#LF,d0
	bne.b	.stringnolf
	moveq	#0,d0
.stringnolf
	move.b	d0,(a1)+
	bne.b	.stringcopy
	bra.b	.stringloop
.nomorestrings


	lea	prefs_palette(pc),a1
	move.l	.addr(pc),a0
	move.l	.len(pc),d0
	bsr	.getarg
	move.l	d0,d3
	beq.b	.nopalette
	lea	palette(pc),a2
	moveq	#3*4-1,d4
.paletteloop
	move.l	a2,d2
	move.l	d3,d1
	bsr	strtoulong
	addq.l	#1,d0
	beq.b	.nopalette
	addq.l	#1,d0
	add.l	d0,d3
	addq.l	#4,a2
	dbf	d4,.paletteloop
.nopalette
	lea	thisfont(pc),a0
	lea	thisfontname(pc),a1
	move.l	a1,(a0)

	move.w	#setnamelen-1,d0	;save the name
	lea	setname,a1
	move.l	.filename(pc),a0
.copysetname
	move.b	(a0)+,(a1)+
	dbeq	d0,.copysetname

	moveq	#0,d2
.exit
	base	exec
	move.l	.addr(pc),a1
	move.l	.memlen(pc),d0
	beq.b	.nofileloaded
	call	FreeMem
.nofileloaded
	move.l	d2,d0
	popm	d1-a6
	rts
.error
	moveq	#-1,d2
	bra.b	.exit
.filehandle	dc.l	0
.filename	dc.l	0
.addr		dc.l	0
.memlen		dc.l	0
.len		dc.l	0
.templong	dc.l	0
;in:
* a0	buffer to search
* a1	NULL-terminated keyword
* d0	length of buffer
;ut:
* d0	pointer to argument or NULL if not found
.getarg
	pushm	d1/a0-a3
	subq.w	#1,d0
.getarg_loop2
	move.b	(a1),d1
.getarg_loop1
	cmp.b	(a0)+,d1
	dbeq	d0,.getarg_loop1
	tst.w	d0
	bmi.b	.getarg_notfound

	move.l	a0,a2
	move.l	a1,a3
	addq.l	#1,a3
.getarg_loop3
	move.b	(a3)+,d1
	beq.b	.getarg_found
	cmp.b	(a2)+,d1
	beq.b	.getarg_loop3
	bra.b	.getarg_loop2
.getarg_found
	move.l	a2,d0
	addq.l	#1,d0
	bra.b	.getarg_exit
.getarg_notfound
	moveq	#0,d0
.getarg_exit
	popm	d1/a0-a3
	rts

in:
* a0	filename
;ut:
* d0	0 if no error, else -1
savesettings:
	bsr	curr2prefs
	base	dos
	move.l	a0,d1
	lea	.filename(pc),a1
	move.l	a0,(a1)
	move.l	#MODE_NEWFILE,d2
	call	Open
	lea	.filehandle(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d3
	lea	prefs_id(pc),a0
	move.l	a0,d2
	move.l	d3,d1
	call	FPuts
	lea	prefs_version(pc),a0
	move.l	a0,d2
	move.l	d3,d1
	call	FPuts

	lea	prefs_longs(pc),a2
.longloop
	move.l	(a2)+,d2
	beq.b	.nomorelongs
	move.l	d3,d1
	call	FPuts
	move.l	d3,d1
	moveq	#"=",d2
	call	FPutC
	move.l	(a2)+,a0
	move.l	(a0),d0
	bsr	numdec
	move.l	d0,d2
	move.l	d3,d1
	call	FPuts
	move.l	d3,d1
	moveq	#LF,d2
	call	FPutC
	bra.b	.longloop
.nomorelongs

	lea	prefs_words(pc),a2
.wordloop
	move.l	(a2)+,d2
	beq.b	.nomorewords
	move.l	d3,d1
	call	FPuts
	move.l	d3,d1
	moveq	#"=",d2
	call	FPutC
	move.l	(a2)+,a0
	moveq	#0,d0
	move.w	(a0),d0
	bsr	numdec
	move.l	d0,d2
	move.l	d3,d1
	call	FPuts
	move.l	d3,d1
	moveq	#LF,d2
	call	FPutC
	bra.b	.wordloop
.nomorewords

	lea	prefs_strings(pc),a2
.stringloop
	move.l	(a2)+,d2
	beq.b	.nomorestrings
	move.l	d3,d1
	call	FPuts
	move.l	d3,d1
	moveq	#"=",d2
	call	FPutC
	move.l	(a2)+,d2
	move.l	d3,d1
	call	FPuts
	move.l	d3,d1
	moveq	#LF,d2
	call	FPutC
	bra.b	.stringloop
.nomorestrings

	lea	prefs_palette(pc),a0
	move.l	d3,d1
	move.l	a0,d2
	call	FPuts
	move.l	d3,d1
	moveq	#"=",d2
	call	FPutC
	lea	palette(pc),a2
	move.l	(a2)+,d0
	bsr	numdec
	move.l	d0,d2
	move.l	d3,d1
	call	FPuts
	moveq	#4*3-1-1,d4
.paletteloop
	move.l	d3,d1
	moveq	#",",d2
	call	FPutC
	move.l	(a2)+,d0
	bsr	numdec
	move.l	d0,d2
	move.l	d3,d1
	call	FPuts
	dbf	d4,.paletteloop
	move.l	d3,d1
	moveq	#LF,d2
	call	FPutC

	lea	createicons(pc),a0
	tst.w	(a0)
	beq.b	.noicon
	base	icon
	lea	.defprefsiconname(pc),a0
	call	GetDiskObject
	move.l	d0,d2
	move.l	d0,a1
	bne.b	.nodefprefsicon
	lea	Textread_prefs,a1	;builtin icon
.nodefprefsicon
	move.l	.filename(pc),a0
	call	PutDiskObject
	tst.l	d2
	beq.b	.nodefprefsicon2
	move.l	d2,a0
	call	FreeDiskObject
.nodefprefsicon2
.noicon
	base	dos
	move.l	.filehandle(pc),d1
	call	Close

	move.l	.filename(pc),a0
	lea	setname,a1
.copysetname
	move.b	(a0)+,(a1)+
	bne.b	.copysetname

	moveq	#0,d0
	rts
.error
	moveq	#-1,d0
	rts

.filehandle	dc.l	0
.filename	dc.l	0
.defprefsiconname
	dc.b	"ENV:Textread/def_prefs",0
	even

prefs_longs
	dc.l	prefs_tabsize,tabsize
	dc.l	prefs_displayid,setdisplayid
	dc.l	0
prefs_words
	dc.l	prefs_fontsize,thisfontsize
	dc.l	prefs_createicons,setcreateicons
	dc.l	prefs_wordwrap,setwordwrap
	dc.l	prefs_sysfont,setsysfont
	dc.l	prefs_remansi,setalwaysremansi
	dc.l	prefs_remcr,setalwaysremcr
	dc.l	prefs_nofontfake,setnofontfake
	dc.l	prefs_legalprint,setlegalprint
	dc.l	0
prefs_strings
	dc.l	prefs_fontname,thisfontname
	dc.l	prefs_filereqdir,filereqdir
	dc.l	prefs_filereqpat,filereqpat
	dc.l	0

prefs_id	dc.b	"Textread settings",10,0
prefs_id_len	=*-prefs_id

prefs_version
	dc.b	"$VER: Textread "
	versionstr
	dc.b	"."
	revisionstr
	dc.b	" ("
	date
	dc.b	")",10,10,0

prefs_tabsize		dc.b	"TABSIZE",0
prefs_fontname		dc.b	"FONTNAME",0
prefs_fontsize		dc.b	"FONTSIZE",0
prefs_filereqdir	dc.b	"REQDIR",0
prefs_filereqpat	dc.b	"REQPATTERN",0
prefs_createicons	dc.b	"CREATEICONS",0
prefs_wordwrap		dc.b	"WORDWRAP",0
prefs_sysfont		dc.b	"SYSFONT",0
prefs_remansi		dc.b	"REMANSI",0
prefs_remcr		dc.b	"REMCR",0
prefs_nofontfake	dc.b	"NOFONTFAKE",0
prefs_legalprint	dc.b	"LEGALPRINT",0
prefs_displayid		dc.b	"DISPLAYID",0
prefs_palette		dc.b	"PALETTE",0
	even

createicons:
	dc.w	0
mcreateicons:
	lea	createicons(pc),a1
	bsr.w	checkmark
	moveq	#0,d0
	rts

mprefs2all:
	bsr	multi_prefs2all
	moveq	#0,d0
	rts

********************************************************************** commands

msearch:
	bra.w	search
msearchnext:
	bra.w	searchnext
msearchprev:
	bra.w	searchprev

medittext:
	bra.w	vedittext
mremansi:
	bsr.b	RemoveANSI
	bsr.w	countlines
	tst.l	d0
	bne.b	.error
	bsr.w	textredraw
	moveq	#0,d0
.error
	rts
mremcr:
	bsr.w	RemoveCR
	bsr.w	countlines
	tst.l	d0
	bne.b	.error
	bsr.w	textredraw
	moveq	#0,d0
.error
	rts

RemoveANSI:
	tst.l	filememlen
	beq.b	.exit
	Message	remansitext
	move.l	fileaddr(pc),a0
	move.l	a0,a1
	add.l	filelen(pc),a1
	bsr.b	.RemoveANSI
	move.l	fileaddr(pc),d1
	sub.l	d1,d0
	lea	filelen(pc),a0
	move.l	d0,(a0)
	Message	screentitle
.exit
	moveq	#0,d0
	rts
	even
;in:
* a0	text
* a1	textend
;ut:
* d0	nytt textend
.RemoveANSI
	move.l	a0,a2
	move.l	a0,a4			;save beginning
	moveq	#$1b,d7			;ESC
	moveq	#$9b-256,d6		;CSI	(moveq.b #$9b,d6)
	moveq	#8,d5			;BS
.loop
	move.b	(a0)+,d0
	cmp.b	d7,d0			;Esc
	bne.b	.inteESC

	move.l	a0,a3

	move.b	(a3)+,d1

	cmp.b	#"[",d1
	beq.b	.csi_loop		;*NOT* ISO/ANSI, but often used.

	cmp.b	#" ",d1
	blo.b	.dontremove
	cmp.b	#"/",d1
	bhi.b	.dontremove

.esc_loop
	move.b	(a3)+,d1
	cmp.b	#" ",d1
	blo.b	.esc_last
	cmp.b	#"/",d1
	bhi.b	.esc_last
	cmp.l	a1,a3
	blo.b	.esc_loop
	bra.b	.dontremove
.esc_last
	move.b	-1(a3),d1
	cmp.b	#"0",d1
	blo.b	.dontremove
	cmp.b	#"~",d1
	bhi.b	.dontremove

	move.l	a3,a0			;Remove!
	bra.b	.nextchar
.inteESC
	cmp.b	d6,d0			;CSI
	bne.b	.inteCSI

	move.l	a0,a3

	move.b	(a3)+,d1
	cmp.b	#" ",d1
	blo.b	.dontremove
	cmp.b	#"?",d1
	bhi.b	.dontremove

.csi_loop
	move.b	(a3)+,d1
	cmp.b	#" ",d1
	blo.b	.csi_last
	cmp.b	#"?",d1
	bhi.b	.csi_last
	cmp.l	a1,a3
	blo.b	.csi_loop
	bra.b	.dontremove
.csi_last
	move.b	-1(a3),d1
	cmp.b	#"@",d1
	blo.b	.dontremove
	cmp.b	#"~",d1
	bhi.b	.dontremove
	move.l	a3,a0			;Remove!
	bra.b	.nextchar
.inteCSI
	cmp.b	d5,d0			;BS
	bne.s	.inteBS
	cmp.l	a2,a4
	beq.s	.nextchar
	subq.l	#1,a2			;remove prev char
	bra.s	.nextchar
.inteBS
.dontremove
	move.b	d0,(a2)+
.nextchar
	cmp.l	a1,a0
	blo.w	.loop
	move.l	a2,d0
	rts

RemoveCR:
	tst.l	filememlen
	beq.b	.exit
	Message	remcrtext
	move.l	fileaddr(pc),a0
	move.l	a0,a1
	add.l	filelen(pc),a1
	bsr.b	.RemoveCR
	move.l	fileaddr(pc),d1
	sub.l	d1,d0
	lea	filelen(pc),a0
	move.l	d0,(a0)
	Message	screentitle
.exit
	moveq	#0,d0
	rts
;in:
* a0	text
* a1	textend
;ut:
* d0	nytt textend
.RemoveCR
	move.l	a0,a2
	moveq	#13,d2
.loop
	move.b	(a0)+,d0
	cmp.b	d2,d0			; ascii 13
	beq.b	.CR
	move.b	d0,(a2)+
.CR
	cmp.l	a0,a1
	bhi.b	.loop
	move.l	a2,d0
	rts

*******************************************************************************
rawkey:
	cmp.w	#$5f,d3			;help
	beq.w	printhelp
	cmp.w	#$4c,d3
	beq.W	.crsrup
	cmp.w	#$4d,d3
	beq.W	.crsrdown
	cmp.w	#$4e,d3
	beq.s	.crsrright
	cmp.w	#$4f,d3
	beq.s	.crsrleft
	cmp.w	#$50,d3
	blo.b	.notf
	cmp.w	#$59,d3
	bls.w	.fkeys
.notf
	moveq	#0,d0
	rts
.crsrleft
	move.w	d4,d0
	and.w	#IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,d0
	bne.w	prevpage3
	move.w	d4,d0
	and.w	#IEQUALIFIER_LALT!IEQUALIFIER_RALT,d0
	bne.w	prevbuff
	bra.w	prevpage
.crsrright
	move.w	d4,d0
	and.w	#IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,d0
	bne.w	nextpage3
	move.w	d4,d0
	and.w	#IEQUALIFIER_LALT!IEQUALIFIER_RALT,d0
	bne.w	nextbuff
	bra.w	nextpage
.crsrup
	move.w	d4,d0
	and.w	#IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,d0
	bne.w	prevpage
	move.w	d4,d0
	and.w	#IEQUALIFIER_CONTROL,d0
	bne.w	gotop
	move.w	d4,d0
	and.w	#IEQUALIFIER_LALT!IEQUALIFIER_RALT,d0
	bne.w	prevpage3
	bra.w	scrolldown1
.crsrdown
	move.w	d4,d0
	and.w	#IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,d0
	bne.w	nextpage
	move.w	d4,d0
	and.w	#IEQUALIFIER_CONTROL,d0
	bne.w	gobottom
	move.w	d4,d0
	and.w	#IEQUALIFIER_LALT!IEQUALIFIER_RALT,d0
	bne.w	nextpage3
	bra.w	scrollup1
.fkeys
	sub.w	#$50,d3
	lsl.w	#2,d3
	lea	fkeys_lines(pc),a0
	and.w	#IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,d4
	beq.b	.fkeys_goto
	move.l	linepos(pc),(a0,d3.w)
	moveq	#0,d0
	rts
.fkeys_goto
	lea	linepos(pc),a1
	move.l	(a0,d3.w),d0
	cmp.l	#-1,d0
	beq.b	.fkeys_exit
	move.l	d0,(a1)
	bsr.w	textredraw
.fkeys_exit
	moveq	#0,d0
	rts
fkeys_lines:
	dc.l	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
fkeys_lines_size	=*-fkeys_lines
*******************************************************************************
vanillakey:
	cmp.w	#$0003,d3
	beq.w	vctrl_c
	cmp.w	#$001b,d3
	beq.w	vesc
	cmp.w	#"q",d3
	beq.w	vquit

	cmp.w	#"?",d3
	beq.w	vhelp
	cmp.w	#"h",d3
	beq.w	vhelp

	cmp.w	#"w",d3
	beq.w	vwordwrap
	cmp.w	#TAB,d3
	beq.w	mtabsize

	cmp.w	#"A",d3
	beq.w	mremansi
	cmp.w	#"C",d3
	beq.w	mremcr

	cmp.w	#"#",d3
	beq.w	vnummer
	cmp.w	#"l",d3
	beq.w	vl
	cmp.w	#"g",d3
	beq.w	vg
	cmp.w	#"%",d3
	beq.w	vprosenten

	cmp.w	#"t",d3
	beq.w	gotop
	cmp.w	#"<",d3
	beq.w	gotop

	cmp.w	#"b",d3
	beq.w	gobottom
	cmp.w	#">",d3
	beq.w	gobottom

	btst	#IEQUALIFIERB_NUMERICPAD,d4
	beq.b	.nonumpad
	cmp.w	#"7",d3
	beq.w	gotop

	cmp.w	#"1",d3
	beq.w	gobottom

	cmp.w	#"9",d3
	beq.w	prevpagescroll
	cmp.w	#"3",d3
	beq.w	nextpagescroll

	cmp.w	#"8",d3
	beq.w	.smoothdown

	cmp.w	#"2",d3
	beq.w	.smoothup
	bra.b	.numpadok
.nonumpad
	cmp.w	#"0",d3
	blo.b	.not1to0
	cmp.w	#"9",d3
	bhi.b	.not1to0
	sub.w	#"0"+1,d3
	bpl.b	.buffnumok
	moveq	#9,d3
.buffnumok
	moveq	#0,d0
	move.w	d3,d0
	bra	multi_change
.not1to0
.numpadok
	cmp.w	#"+",d3
	beq	nextbuff
	cmp.w	#"-",d3
	beq	prevbuff

	cmp.w	#" ",d3
	beq.w	nextpage
	cmp.w	#8,d3			;bs
	beq.w	prevpage
	cmp.w	#13,d3
	beq.w	scrollup1

	cmp.w	#"E",d3
	beq.w	vedittext

	cmp.w	#"s",d3
	beq.w	vsearchnocase
	cmp.w	#".",d3
	beq.w	vsearchnocase
	cmp.w	#"S",d3
	beq.w	vsearchcase
	cmp.w	#"/",d3
	beq.w	vsearchcase

	cmp.w	#"n",d3
	beq.w	vsearchnext
	cmp.w	#"p",d3
	beq.w	searchprev

	moveq	#0,d0
	rts

.smoothup
	moveq	#0,d1			;->
	bra.b	.smooth
.smoothdown
	moveq	#1,d1			;->
.smooth
	lea	scrollfart(pc),a0
	lea	scrolla(pc),a1

	move.w	d4,d0
	and.w	#IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,d0
	beq.s	.smoothup_noshift
	move.w	#3,(a0)
	move.w	#scrolla_crsr,(a1)
	bra.s	.smoothup_exit
.smoothup_noshift
	move.w	d4,d0
	and.w	#IEQUALIFIER_CONTROL,d0
	beq.s	.smoothup_nocontrol
	move.w	#1,(a0)
	move.w	#scrolla_crsr,(a1)
	bra.s	.smoothup_exit
.smoothup_nocontrol
	move.w	d4,d0
	and.w	#IEQUALIFIER_LALT!IEQUALIFIER_RALT,d0
	beq.s	.smoothup_noalt
	move.w	#4,(a0)
	move.w	#scrolla_crsr,(a1)
	bra.s	.smoothup_exit
.smoothup_noalt
	move.w	#2,(a0)
	move.w	#scrolla_crsr,(a1)
.smoothup_exit
	tst.l	d1			;<-
	beq.b	.smoothup_quit
	neg.w	(a0)
.smoothup_quit
	moveq	#0,d0
	rts


vesc:
	and.w	#IEQUALIFIER_RSHIFT!IEQUALIFIER_LSHIFT,d4
	bne.b	vquit
	bsr	mclose
	moveq	#0,d0
	rts
vctrl_c:
vquit:
	bsr.w	mquit
	moveq	#0,d0
	rts
vhelp:
	bsr.w	printhelp
	moveq	#0,d0
	rts
vwordwrap:
	eor.w	#1,wordwrap
	bsr.w	updatecheckmarks
	bsr.w	countlines
	tst.l	d0
	bne.b	.exit
	bsr.w	textredraw
	moveq	#0,d0
.exit
	rts
vnummer:
vl:

	jsr	requestline

	tst.l	d0
	bmi.b	.error
	beq.b	.exit

	lea	linepos(pc),a0
	move.l	d1,(a0)
	bsr.w	textredraw
.exit
	moveq	#0,d0
.error
	rts

vprosenten:
vg:
	jsr	requestpercent

	tst.l	d0
	bmi.b	.error
	beq.b	.exit

	move.l	lines(pc),d0
	sub.l	cheight(pc),d0
	mulu.w	d1,d0
	add.l	#50,d0			;round
	divu.w	#100,d0
	and.l	#$0000ffff,d0
	lea	linepos(pc),a0
	move.l	d0,(a0)

	bsr.w	textredraw
.exit
	moveq	#0,d0
.error
	rts

vsearchnocase:
	clr.w	search_case
	bsr.w	updatecheckmarks
	bra.w	search
vsearchcase:
	move.w	#1,search_case
	bsr.w	updatecheckmarks
	bra.w	search
vsearchnext:
	bra.w	searchnext
vedittext:
	lea.l	filelen(pc),a0
	tst.l	(a0)
	beq.B	.nofile
	base	dos
	lea	.editor(pc),a0
	move.l	a0,d1
	lea	temp,a2
	move.l	a2,d2
	move.l	#tempsize,d3
	moveq	#0,d4
	call	GetVar
	tst.l	d0
	beq.b	.error
	cmp.l	#-1,d0
	beq.b	.error
	add.l	d0,a2
	move.b	#" ",(a2)+
	move.b	#'"',(a2)+

	move.l	a2,d1
	lea	filereqdir(pc),a1
.copydir
	move.b	(a1)+,(a2)+
	bne.B	.copydir
	lea	filename(pc),a0		;d1 already ok
	move.l	a0,d2
	lea	tempend-1,a0
	move.l	a0,d3
	sub.l	d1,d3			;size
	call	AddPart
	tst.l	d0
	beq.b	.error
	lea	temp,a0
	move.l	a0,d1
.findnull
	tst.b	(a0)+
	bne.b	.findnull
	move.b	#'"',-1(a0)
	clr.b	(a0)

	lea	.tags(pc),a0
	move.l	a0,d2
	call	SystemTagList
.error
	moveq	#0,d0
	rts
.nofile
	lea	nofileloaded(pc),a1
	lea	oktext(pc),a2
	suba.l	a4,a4
	bsr.w	requester
	moveq	#0,d0
	rts
.editor	dc.b	"editor",0
	even
.tags	
	dc.l	SYS_Asynch,TRUE		;
	dc.l	SYS_UserShell,TRUE
	dc.l	TAG_DONE
*******************************************************************************
refreshwindow:
	base	gt
	move.l	windowptr(pc),a0
	call	GT_BeginRefresh
	move.l	windowptr(pc),a0
	moveq	#TRUE,d0
	call	GT_EndRefresh
	moveq	#0,d0
	rts
*******************************************************************************
handlegadgets:
	cmp.l	scrollgadget(pc),a2
	beq.B	.scrollgadget
	cmp.l	quitgadget(pc),a2
	beq.B	.quitgadget
	moveq	#0,d0
	rts
.scrollgadget
	bsr.w	fixlinepos		;scroll or redraw full page?
	move.l	cheight(pc),d2
	subq.l	#3,d2			;???
	mulu.w	fonty+2(pc),d2
	lea	linepos(pc),a0
	move.l	d3,d0
	sub.l	(a0),d0
	muls.w	fonty+2(pc),d0
	moveq	#1,d1
	cmp.l	d2,d0
	bls.w	scrollup_noupdate	;unsigned
	neg.l	d2
	cmp.l	d2,d0
	bls.s	.notprevline		;unsigned
	neg.l	d0
	bra.w	scrolldown_noupdate
.notprevline
	move.l	d3,(a0)			;code
	bsr.w	changenum
	bsr.w	cls
	move.l	linepos(pc),d0
	bsr.w	gotoline
	bsr.w	printpage
	moveq	#0,d0
	rts
.quitgadget
	bsr	mquit
	moveq	#0,d0
	rts
*******************************************************************************
handlebuttons:
	cmp.w	#SELECTDOWN,d3
	bne.s	.nodown
	lea	scrolla(pc),a0
	move.w	#scrolla_mouse,(a0)
.nodown
	cmp.w	#SELECTUP,d3
	bne.s	.noup
	lea	scrolla(pc),a0		;stop scrolling
	clr.w	(a0)
.noup
	moveq	#0,d0
	rts


*******************************************************************************
********************** S U B R U T I N E R ************************************
*******************************************************************************

**** prefs

;call after new settings has been loaded
prefs2curr:
	move.w	setcreateicons(pc),createicons
	move.w	setwordwrap(pc),wordwrap
	move.w	setalwaysremansi(pc),alwaysremansi
	move.w	setalwaysremcr(pc),alwaysremcr
	move.w	setnofontfake(pc),nofontfake
	move.w	setlegalprint(pc),legalprint

	move.w	setsysfont(pc),sysfont
	move.l	setdisplayid(pc),displayid

	pushm	d0-a6
	lea	palette,a1
	bsr	changepalette
	popm	d0-a6
	rts

;call before new settins will be loaded
curr2prefs:
	move.w	createicons(pc),setcreateicons
	move.w	wordwrap(pc),setwordwrap
	move.w	nofontfake(pc),setnofontfake
	move.w	legalprint(pc),setlegalprint
	move.w	alwaysremcr(pc),setalwaysremcr
	move.w	alwaysremansi(pc),setalwaysremansi

	move.w	sysfont(pc),setsysfont
	move.l	displayid(pc),setdisplayid

	pushm	d0-a6
	lea	palette,a1
	bsr	getpalette
	popm	d0-a6
	rts

**** buffer

nextbuff:
	move.l	bnum(pc),d0
	cmp.l	bmax(pc),d0
	bpl.b	.exit
;	addq.l	#1-1,d0			;bnum: 1-...
	bsr	multi_change
.exit
	moveq	#0,d0
	rts
prevbuff:
	move.l	bnum(pc),d0
	cmp.l	#1,d0
	beq.b	.exit
	subq.l	#1+1,d0			;bnum begins at 1, not 0
	bsr	multi_change
.exit
	moveq	#0,d0
	rts

;level 1

;ut:
* d0	NULL om fel.
multi_init:
	pushm	a0
	lea	bufflist(pc),a0
	NEWLIST	a0
	bsr	multi_addbuff
	lea	thisbuff(pc),a0
	move.l	d0,(a0)
	pop	a0
	rts
;in:
* a0	buffert
multi_curr2buff:
	pushm	d0/a0-a2
	move.l	fileaddr(pc),buff_fileaddr(a0)
	move.l	filelen(pc),buff_filelen(a0)
	move.l	filememlen(pc),buff_filememlen(a0)
	move.l	linepos(pc),buff_linepos(a0)
	move.l	lines(pc),buff_lines(a0)
	move.l	linelen(pc),buff_linelen(a0)
	move.l	linelensize(pc),buff_linelensize(a0)

	bsr	curr2prefs

	moveq	#32-1,d0
	lea	filename(pc),a1
	lea	buff_filename(a0),a2
.copyfilename
	move.b	(a1)+,(a2)+
	dbf	d0,.copyfilename

	move.w	#setnamelen-1,d0
	lea	setname,a1
	lea	buff_setname(a0),a2
.copysetname
	move.b	(a1)+,(a2)+
	dbf	d0,.copysetname

	move.w	#settings_len-1,d0
	lea	settings(pc),a1
	lea	buff_settings(a0),a2
.copysettings
	move.b	(a1)+,(a2)+
	dbf	d0,.copysettings

	move.w	#fkeys_lines_size-1,d0
	lea	fkeys_lines(pc),a1
	lea	buff_fkeys(a0),a2
.copyfkeys
	move.b	(a1)+,(a2)+
	dbf	d0,.copyfkeys

	popm	d0/a0-a2
	rts

;in:
* a0	buffert
multi_buff2curr:
	pushm	d0/a0-a2
	move.l	buff_fileaddr(a0),fileaddr
	move.l	buff_filelen(a0),filelen
	move.l	buff_filememlen(a0),filememlen
	move.l	buff_linepos(a0),linepos
	move.l	buff_lines(a0),lines
	move.l	buff_linelen(a0),linelen
	move.l	buff_linelensize(a0),linelensize

	moveq	#32-1,d0
	lea	buff_filename(a0),a1
	lea	filename(pc),a2
.copyfilename
	move.b	(a1)+,(a2)+
	dbf	d0,.copyfilename

	move.w	#setnamelen-1,d0
	lea	buff_setname(a0),a1
	lea	setname,a2
.copysetname
	move.b	(a1)+,(a2)+
	dbf	d0,.copysetname

	move.w	#settings_len-1,d0
	lea	buff_settings(a0),a1
	lea	settings(pc),a2
.copysettings
	move.b	(a1)+,(a2)+
	dbf	d0,.copysettings

	move.w	#fkeys_lines_size-1,d0
	lea	buff_fkeys(a0),a1
	lea	fkeys_lines(pc),a2
.copyfkeys
	move.b	(a1)+,(a2)+
	dbf	d0,.copyfkeys

	bsr	prefs2curr

	popm	d0/a0-a2
	rts

multi_clearcurr:
	clr.l	fileaddr
	clr.l	filelen
	clr.l	filememlen
	clr.l	linepos
	move.l	#1,lines
	move.l	#notnull,linelen
	clr.l	linelensize
	clr.b	filename
	rts

;in:
* a0	buffert
multi_freebuff:
	pushm	d0-d1/a0-a2/a6
	move.l	a0,a2
	base	exec


	move.l	commessage,d0
	move.l	d0,a1
	beq.b	.nomessage
	move.l	comport,MN_REPLYPORT(a1)
	move.w	#cm_SIZE,MN_LENGTH(a1)
	move.w	#cmc_quit,cm_command(a1)

	move.l	buff_commessageport(a2),d0
	beq.b	.nomessage
	move.l	d0,a0
	move.l	commessage,a1
	call	PutMsg				;tell other textread to quit

	move.l	comport,a0
	call	WaitPort
.getallcommsgs
	move.l	comport,a0
	call	GetMsg
	tst.l	d0
	bne.s	.getallcommsgs
.nomessage
	move.l	buff_fileaddr(a2),a1
	move.l	buff_filememlen(a2),d0
	beq.B	.nofileinmem
	call	FreeMem
.nofileinmem
	move.l	buff_linelen(a2),a1
	move.l	buff_linelensize(a2),d0
	beq.b	.ingetlineminne
	call	FreeMem
.ingetlineminne
	clr.l	buff_fileaddr(a2)
	clr.l	buff_filelen(a2)
	clr.l	buff_filememlen(a2)
	clr.l	buff_linepos(a2)
	move.l	#1,buff_lines(a2)
	move.l	#notnull,buff_linelen(a2)
	clr.l	buff_linelensize(a2)
	clr.b	buff_filename(a2)
	popm	d0-d1/a0-a2/a6
	rts
multi_freeall:
	lea	bufflist(pc),a0
	IFEMPTY	a0,.exit
	move.l	MLH_HEAD(a0),a0
.loop
	tst.l	(a0)
	beq.b	.exit
	bsr	multi_freebuff
	move.l	LN_SUCC(a0),a0
	bra.b	.loop
.exit
	rts

;ut:
* d0	NULL eller buffert.
multi_addbuff:
	pushm	d1-d2/a0-a2/a6
	base	exec
	move.l	#buff_len,d0
	move.l	#MEMF_CLEAR!MEMF_ANY,d1
	call	AllocMem
	move.l	d0,d2
	beq.b	.error
	move.l	d0,a1
	lea	buff_number(a1),a0
	move.l	a0,LN_NAME(a1)
	lea	bufflist(pc),a0
	call	AddTail
	move.l	d2,a0
	clr.l	buff_fileaddr(a0)
	clr.l	buff_filelen(a0)
	clr.l	buff_filememlen(a0)
	clr.l	buff_linepos(a0)
	move.l	#1,buff_lines(a0)
	move.l	#notnull,buff_linelen(a0)
	clr.l	buff_linelensize(a0)
	clr.b	buff_filename(a0)

;default: same settings and -name

	move.w	#setnamelen-1,d0
	lea	setname,a1
	lea	buff_setname(a0),a2
.copysetname
	move.b	(a1)+,(a2)+
	dbf	d0,.copysetname

	move.w	#settings_len-1,d0
	lea	settings(pc),a1
	lea	buff_settings(a0),a2
.copysettings
	move.b	(a1)+,(a2)+
	dbf	d0,.copysettings

	move.l	a0,d0
	popm	d1-d2/a0-a2/a6
	rts
.error
	popm	d1-d2/a0-a2/a6
	moveq	#0,d0
	rts

;in:
* a0	buffert att ta bort
multi_rembuff:
	pushm	d0-d2/a0-a1/a6
	base	exec
	move.l	a0,d2
	move.l	a0,a1
	call	Remove
	move.l	d2,a1
	move.l	#buff_len,d0
	call	FreeMem
	popm	d0-d2/a0-a1/a6
	rts

multi_remall:
	pushm	d0-d1/a0-a1/a6
	base	exec
.loop
	lea	bufflist(pc),a0
	call	RemTail
	tst.l	d0
	beq.b	.exit
	move.l	d0,a1
	move.l	#buff_len,d0
	call	FreeMem
	bra.b	.loop
.exit
	popm	d0-d1/a0-a1/a6
	rts
;in:
* a0	buffert
;ut:
* d0	NULL om ok.
multi_usebuff:
	pushm	d1-a6

	move.w	sysfont(pc),d1
	move.l	displayid(pc),d2

	bsr	multi_buff2curr
; d1,d2..
	bsr	changeenv

	popm	d1-a6
	rts


;level 2 (thisbuff must be valid for all but multi_quit)

;ut:
* d0	NULL eller felkod
multi_new_nochange:
	pushm	a0
	move.l	thisbuff(pc),a0
	bsr	multi_curr2buff
	bsr	multi_addbuff
	lea	thisbuff(pc),a0
	move.l	d0,(a0)
	beq.b	.error
	moveq	#0,d0
	popm	a0
	rts
.error
	moveq	#err_nomem,d0
	popm	a0
	rts
;ut:
* d0	NULL eller felkod
multi_new:
	pushm	a0
	move.l	thisbuff(pc),a0
	bsr	multi_curr2buff
	bsr	multi_addbuff
	lea	thisbuff(pc),a0
	move.l	d0,(a0)
	beq.b	.error
	move.l	d0,a0
	bsr	multi_usebuff
	popm	a0
	rts
.error
	moveq	#err_nomem,d0
	popm	a0
	rts

;ut:
* d0	NULL eller felkod
multi_close:
	pushm	a0/a1
	lea	bufflist(pc),a0
	move.l	MLH_HEAD(a0),d0
	cmp.l	MLH_TAILPRED(a0),d0
	beq.b	.exit			;don't close last one!
	move.l	thisbuff(pc),a0
	move.l	LN_SUCC(a0),a1
	tst.l	(a1)
	bne.b	.ok
	move.l	LN_PRED(a0),a1
.ok
	bsr	multi_curr2buff
	bsr	multi_freebuff
	bsr	multi_rembuff
	move.l	a1,a0
	lea	thisbuff(pc),a1
	move.l	a0,(a1)
	bsr	multi_usebuff
.exit
	popm	a0/a1
	rts

;in:
* d0	buffer #
;ut:
* d0	NULL eller felkod
multi_change:
	move.l	thisbuff(pc),a0
	bsr	multi_curr2buff
	lea	bufflist(pc),a0
.stepdown
	TSTNODE	a0,a0
	beq.b	.error
	dbf	d0,.stepdown
	lea	thisbuff(pc),a1
	move.l	a0,(a1)
	bsr	multi_usebuff
	rts
.error
	moveq	#0,d0
	rts

multi_quit:
	pushm	d0/a0
	move.l	thisbuff(pc),a0
	move.l	a0,d0
	beq.b	.nocurr
	bsr	multi_curr2buff
.nocurr
	bsr	multi_freeall
	bsr	multi_remall
	bsr	multi_clearcurr
	clr.l	thisbuff
	popm	d0/a0
	rts

;FIXIT! Countlines may have to be done on some buffers!
multi_prefs2all:
	pushm	d0/a0

	lea	bufflist(pc),a0
	IFEMPTY	a0,.exit
	move.l	MLH_HEAD(a0),a0
.loop
	tst.l	(a0)
	beq.b	.exit

	bsr.b	.copyit

	move.l	LN_SUCC(a0),a0
	bra.b	.loop
.exit
	popm	d0/a0
	rts

.copyit
	pushm	d0/a1-a2
	move.w	#prefs2all_len-1,d0
	lea	prefs2all_start(pc),a1
	lea	buff_settings+prefs2all_offset(a0),a2
.copysettings
	move.b	(a1)+,(a2)+
	dbf	d0,.copysettings
	popm	d0/a1-a2
	rts

* laddar in filerna i de resterande bufferterna
;in:
* a5	pek till nod i bufflist
;ut:
* d0	0 om ok
multi_loadrest:
	pushm	d1-a6
	bsr	changevideomode
	tst.l	d0
	bne.b	.exit
	lea	tabbar,a0
	bsr	fixatabbar
	bra.b	.changed

.loop
	tst.l	(a5)
	beq.b	.listisempty

	move.l	thisbuff(pc),a0
	bsr	multi_curr2buff
	move.l	a5,a0
	lea	thisbuff(pc),a1
	move.l	a0,(a1)
	bsr	multi_usebuff
	tst.l	d0
	bne.b	.exit
.changed
	lea	filename(pc),a1
	tst.b	(a1)
	beq.b	.openreq
	move.l	#tempsize,d0
	lea	filereqdir(pc),a0
	lea	filename(pc),a1
	lea	temp,a2
	bsr	pathnfiletobuff
	move.l	#temp,d0
	bsr	openfile
	tst.l	d0
	beq.b	.nextbuffer
.openreq
	pushm	d1-a6
	bsr	mopen
	popm	d1-a6
.nextbuffer
	move.l	LN_SUCC(a5),a5
	bra.b	.loop
.listisempty
	moveq	#0,d0
.exit
	lea	pipeused(pc),a0
	tst.b	(a0)
	bne.b	.nopipe

	pushm	d0
	base	dos
	lea	pipename(pc),a0
	move.l	a0,d1
	call	DeleteFile
	popm	d0
.nopipe

	popm	d1-a6
	rts
thisbuff:
	dc.l	0
bufflist:
	ds.b	MLH_SIZE		;minimal list header

**** window locking

;	*** nestla inte! ***
lockwindow:
	movem.l	d0-d1/a0-a2/a6,-(sp)
	base	intui
	lea	lockwinreq,a2
	move.l	a2,a0
	call	InitRequester
	move.l	a2,a0
	move.l	windowptr(pc),d0
	beq.b	.nowin
	move.l	d0,a1
	call	Request
	lea	lockwinreqsuccess(pc),a0
	move.l	d0,(a0)
	cmp.w	#39,LIB_VERSION(a6)
	blo.s	.no39
	move.l	windowptr(pc),a0
	lea	.tags(pc),a1
	call	SetWindowPointerA
.no39
.nowin
	movem.l	(sp)+,d0-d1/a0-a2/a6
	rts
.tags
	dc.l	WA_BusyPointer,TRUE
	dc.l	TAG_DONE
unlockwindow:
	movem.l	d0-d1/a0-a1/a6,-(sp)
	tst.l	lockwinreqsuccess
	beq.s	.nolock
	lea	lockwinreqsuccess(pc),a0
	clr.l	(a0)
	base	intui
	lea	lockwinreq,a0
	move.l	windowptr(pc),d0
	beq.b	.nowin
	move.l	d0,a1
	call	EndRequest
	cmp.w	#39,LIB_VERSION(a6)
	blo.s	.no39
	move.l	windowptr(pc),a0
	lea	.tags(pc),a1
	call	SetWindowPointerA
.no39
.nolock
.nowin
	movem.l	(sp)+,d0-d1/a0-a1/a6
	rts
.tags
	dc.l	WA_BusyPointer,FALSE
	dc.l	TAG_DONE
lockwinreqsuccess:
	dc.l	0

**** argument parsing

getargs:
	lea	boolcli(pc),a0
	tst.l	(a0)
	bne.w	ga_cli
ga_workbench:
	move.l	argset(pc),a0		;default prefs
	bsr.w	loadsettings
	bsr	prefs2curr

	move.l	wbmessage(pc),a0
	move.l	sm_NumArgs(a0),d1
	move.l	sm_ArgList(a0),a2

;in:
* d1	NumArgs
* a2	ArgList
parsewbarg:
	moveq	#TRUE,d2
	bsr.b	readargsfromicon

	add.w	#wa_SIZEOF,a2		;first project
	subq.l	#1,d1
;in:
* d1	NumArgs
* a2	ArgList
parsewbarg_proj:
	subq.w	#1,d1
	bmi.B	.noproject
.projectloop
	move.l	wa_Name(a2),a0
	tst.b	(a0)
	beq.b	.nofile
	moveq	#FALSE,d2
	bsr.b	readargsfromicon
	bra.b	.nodir
.nofile
	bsr.b	.wasdiricon
.nodir
	tst.l	d1			;last file?
	beq.b	.noproject
	tst.l	d0			;was it prefs?
	bne.b	.nextproject
	bsr	multi_new_nochange
	tst.l	d0
	bne.b	.noproject
.nextproject
	add.w	#wa_SIZEOF,a2		;next project
	dbf	d1,.projectloop
.noproject
	move.l	thisbuff(pc),a0
	bsr	multi_curr2buff		;save last buffer

	moveq	#0,d0
	rts
.wasdiricon
	pushm	d1-d3/a0-a3
	base	dos
	move.l	wa_Lock(a2),d1
	lea	filereqdir(pc),a0
	move.l	a0,d2
	move.l	#256,d3
	call	NameFromLock
	clr.b	filename
	moveq	#FALSE,d0		;no prefs file
	popm	d1-d3/a0-a3
	rts

;in:
* a2	pek till WBArg   (eller: dc.l lock : dc.l name)
* d2	TRUE=denna fil ska _inte_ laddas in (rör inte filename, filereqpath)
;ut:
* d0	TRUE om filen var en prefsfil
readargsfromicon:
	pushm	d1-a6

	moveq	#FALSE,d5

	bsr.w	.loadicon
	tst.l	d0
	beq.w	.filewithouticon
	base	icon
	move.l	d0,a0
	move.l	do_ToolTypes(a0),a2

	move.l	a2,a0
	lea	.filetype(pc),a1
	call	FindToolType
	tst.l	d0
	beq.b	.nofiletype
	move.l	d0,a0
	lea	.prefs(pc),a1
	call	MatchToolValue
	tst.l	d0
	beq.b	.notprefs

	moveq	#TRUE,d5
	lea	argprojectname,a0
	bsr.w	loadsettings		;load prefs file first
	bsr	prefs2curr
	bra.b	.skipload
.notprefs
.nofiletype
	move.l	a2,a0
	lea	.settings(pc),a1
	call	FindToolType
	tst.l	d0
	beq.b	.nosettings
	move.l	d0,a0

	bsr.w	loadsettings		;load prefs file first
	bsr	prefs2curr
.nosettings
;om man kommer hit var filen i fråga inte nån prefsfil, dvs denna ska vi
;ladda in
	tst.l	d2
	bne.b	.skipload
	move.l	#argprojectname,d2
	moveq	#0,d0			;wb has always full path!
	bsr	splitfilepath
.skipload

	move.l	a2,a0
	lea	.new(pc),a1
	call	FindToolType
	tst.l	d0
	beq.b	.nonew
	lea	argnew(pc),a0
	move.l	#1,(a0)
.nonew
	move.l	a2,a0
	lea	.tabs(pc),a1
	call	FindToolType
	tst.l	d0
	beq.b	.notabs
	move.l	d0,d1
	lea	.tabsize(pc),a0
	move.l	a0,d2
	base	dos
	call	StrToLong
	cmp.l	#-1,d0
	beq.b	.notabs
	lea	tabsize(pc),a0
	lea	.tabsize(pc),a1
	move.l	(a1),(a0)
.notabs
	base	icon
	move.l	a2,a0
	lea	.unit(pc),a1
	call	FindToolType
	tst.l	d0
	beq.b	.nounit
	move.l	d0,d1
	lea	.unittmp(pc),a0
	move.l	a0,d2
	base	dos
	call	StrToLong
	cmp.l	#-1,d0
	beq.b	.nounit
	lea	unit(pc),a0
	lea	.unittmp(pc),a1
	move.l	a1,(a0)
.nounit
.freediskobj
	base	icon
	move.l	wbdiskobj(pc),d0
	beq.b	.noicon
	move.l	d0,a0
	call	FreeDiskObject
	lea	wbdiskobj(pc),a0
	clr.l	(a0)
.noicon
	move.l	d5,d0
	popm	d1-a6
	rts
.filewithouticon
	tst.l	d2
	bne.b	.freediskobj
	move.l	#argprojectname,d2
	moveq	#0,d0			;wb has always full path!
	bsr	splitfilepath
	bra.b	.freediskobj

.tabsize	dc.l	0
.unittmp		dc.l	0
;in:
* a2	pek till WBArg   (eller: dc.l lock : dc.l name)
;ut:
* d0	pek till diskobj eller 0 om fel
.loadicon
	pushm	d1-d3/a0
	base	dos
	move.l	(a2),d1
	move.l	#argprojectname,d2
	move.l	#256,d3
	call	NameFromLock
	tst.l	d0
	beq.b	.li_error
	move.l	d2,d1
	move.l	4(a2),d2
	move.l	#256,d3
	call	AddPart
	tst.l	d0
	beq.b	.li_error
	base	icon
	lea	argprojectname,a0
	call	GetDiskObject
	lea	wbdiskobj(pc),a0
	move.l	d0,(a0)
.li_error
	popm	d1-d3/a0
	rts
.filetype	dc.b	"FILETYPE",0
.prefs		dc.b	"PREFS",0
.tabs		dc.b	"TABS",0
.new		dc.b	"NEW",0
.unit		dc.b	"UNIT",0
.settings	dc.b	"SETTINGS",0
	even
ga_cli:
	base	dos
	lea	ga_template(pc),a0
	move.l	a0,d1
	lea	args(pc),a0
	move.l	a0,d2
	moveq	#0,d3
	call	ReadArgs
	move.l	d0,rastruct
	bne.b	.ok
.show
	lea	ga_usage(pc),a0
	move.l	a0,d1
	call	PutStr
	moveq	#err_exitnow,d0
	rts
.ok
	move.l	argset(pc),a0
	bsr.w	loadsettings		;load prefs file first
	bsr	prefs2curr

	move.l	argunit(pc),a0
	lea	unit(pc),a1
	move.l	(a0),(a1)

	move.l	argtab(pc),a0
	lea	tabsize(pc),a1
	move.l	(a0),(a1)

	base	dos
	call	Input
	move.l	d0,d4
	beq.w	.noinputfile
	move.l	d4,d1
	call	IsInteractive
	tst.l	d0			;Interactive (= no file redirection)?
	bne.w	.noinputfile		;Yes, skip

	move.l	d4,d1
	move.l	#temp,d2
	move.l	#tempsize,d3
	call	NameFromFH
	tst.l	d0
	beq.b	.pipe

;Standard redirection: Filename & size known!

	moveq	#0,d0
	move.l	#temp,d2
	bsr	splitfilepath
	bra.w	parsefilenamearray\.exit

;Pipe: Unknown name, unknown size :-(

.pipe
	base	util
	cmp.w	#39,LIB_VERSION(a6)
	blo.s	.nov39
	call	GetUniqueID
	bra.b	.uniqueidok
.nov39
	move.l	thistask(pc),d0
.uniqueidok
	bsr	numdec
	move.l	d0,a0
	lea	pipenamenum(pc),a1
.copynum	
	move.b	(a0)+,(a1)+
	bne.b	.copynum

	base	exec
	move.l	#1024,d0
	moveq	#MEMF_ANY,d1
	call	AllocVec
	move.l	d0,d6
	beq.w	.nomem
	base	dos
	lea	pipename(pc),a0
	move.l	a0,d1
	move.l	#MODE_NEWFILE,d2
	call	Open
	move.l	d0,d5
	beq.b	.nooutputfile

	move.l	d4,d1
	move.l	d6,d2
	move.l	#1024,d3
	call	Read
	move.l	d0,d3
	beq.b	.error			;EOF on first read => run (for example)
	addq.l	#1,d0
	beq.b	.error

.copyfile
	move.l	d5,d1
	move.l	d6,d2
	call	Write
	addq.l	#1,d0
	beq.b	.error

	move.l	d4,d1
	move.l	d6,d2
	move.l	#1024,d3
	call	Read
	move.l	d0,d3
	beq.b	.eof
	addq.l	#1,d0
	bne.b	.copyfile

.eof

	lea	argnames(pc),a1
	lea	pipeargnames(pc),a0
	move.l	a0,(a1)
.error
	lea	pipeused(pc),a0		;mark file to be deleted
	clr.b	(a0)

	base	dos
	move.l	d5,d1
	call	Close

.nooutputfile
	base	exec
	move.l	d6,a1
	call	FreeVec
.nomem


.noinputfile


; gå igenom alla filnamn som angavs, skapa en ny buffer för var och en
; av dom (utom den första).

	move.l	argnames(pc),a2
	moveq	#0,d0			;currentdir is ok

;in:
* d0	currentdir (string) eller 0
* a2	array med pek till filnamn/dirs

parsefilenamearray:
	move.l	d0,.currentdir
	cmp.l	#0,a2
	beq.b	.exit
.copynextname
	move.l	(a2)+,d2
	beq.b	.exit

	move.l	.currentdir(pc),d0
	bsr	splitfilepath

	tst.l	(a2)
	beq.b	.exit
	bsr	multi_new_nochange
	tst.l	d0
	beq.b	.copynextname
.exit
	move.l	thisbuff(pc),a0
	bsr	multi_curr2buff		;save last buffer
	moveq	#0,d0
	rts
.currentdir
	dc.l	0
ga_template:
	dc.b	"FILES/M,TABS/K/N,UNIT/K/N,SETTINGS/K,NEW/S",0
;FIXIT! Localize it!
ga_usage:
	dc.b	27,"[1mTextread "
	releasestr
	dc.b	27,"[22m ©1992-1995 Martin Blom.",10
	dc.b	"Usage: Textread [[FILES] <files|dirs>] [TABS <n>] [UNIT <n>]",10
	dc.b    "                [SETTINGS <name>] [NEW]",10,0
	even
;FIXIT! kommentarer => engelska
args:
argnames:	dc.l	null		;pek till lista med filnamn
argtab:		dc.l	tabsize		;pek till lw med tab
argunit:	dc.l	null		;pek till lw med unit
argset:		dc.l	defsetname	;pek till settingsnamn
argnew:		dc.l	0		;0 eller inte 0
* ^^^ fylls i av readargs
argdir:		dc.l	null		;pek till dirnamn

boolcli:	dc.l	1
wbmessage:	dc.l	0
wbdiskobj:	dc.l	0
rastruct:	dc.l	0

pipeargnames:	dc.l	pipename,0
pipeused:	dc.b	1		;0=pipe, 1=no pipe
pipename:	dc.b	"T:TextreadPipe"
pipenamenum:	dc.b	"0000000000",0

**** file names

;in:
* d0	currentdir (lock) eller 0
* d2	filnamn med path
;ut:
* filename & filereqdir ifyllt med inga relativa sökvägar kvar
splitfilepath:
	pushm	d0-d3/a0-a1/a6
	base	dos
	move.l	d2,.filename
	clr.l	.currentdir
	move.l	d0,d1
	beq.b	.nocurrdir
	call	CurrentDir
	move.l	d0,.currentdir
.nocurrdir
	move.l	.filename(pc),d1
	moveq	#ACCESS_READ,d2
	call	Lock				;för att expandera pathen
	move.l	d0,.lock
	bne.b	.lockedfile
	move.l	.filename(pc),d1
	bra.b	.splitit			;ta det namnet som skrevs!
.lockedfile
	clr.l	fib+fib_DirEntryType
	move.l	.lock,d1
	move.l	#fib,d2
	call	Examine				;skit i resultatet

	move.l	.lock(pc),d1
	move.l	#temp,d2
	move.l	#tempsize,d3
	call	NameFromLock
	move.l	.lock(pc),d1
	call	UnLock
	tst.l	fib+fib_DirEntryType
	bmi.b	.wasfile
	clr.b	filename		
	move.l	.filename(pc),a0
	bra.b	.copydir
.wasfile
	move.l	#temp,d1
.splitit
	move.l	d1,d2		;->
	call	FilePart
	move.l	d0,a0
	lea	filename(pc),a1
	moveq	#31,d0
.copynameloop
	move.b	(a0)+,(a1)+
	dbeq	d0,.copynameloop

	move.l	d2,d1		;<-
	call	PathPart
	move.l	d0,a0
	clr.b	(a0)		;klipp bort filnamnet
	move.l	d2,a0		;<-
.copydir
	lea	filereqdir(pc),a1
	move.w	#255,d0
.copydirloop
	move.b	(a0)+,(a1)+
	dbeq	d0,.copydirloop
.exit
	move.l	.currentdir,d1
	beq.b	.nocurrdir2
	call	CurrentDir	;återställ!
.nocurrdir2
	popm	d0-d3/a0-a1/a6
	rts
.lock	dc.l	0
.currentdir
	dc.l	0
.filename
	dc.l	0
;in:
* d0	buffertlängd
* a0	path
* a1	filnamn
* a2	buffert
pathnfiletobuff:
	base	dos
	move.l	a2,d1
	move.l	a1,d2
	move.l	d0,d3
	subq.w	#1,d0
.copydir
	move.b	(a0)+,(a2)+
	dbf	d0,.copydir
	call	AddPart
	tst.l	d0
	beq.b	.exit
	moveq	#0,d0
	rts
.exit
	moveq	#-1,d0
	rts

**** buffer movements

;in:
* d0	gå till rad (0-?)
;ut:
* d0	rader att printa-1
* a0	adress till rad
gotoline:
	movem.l	d1-d2/a1,-(sp)
	move.l	.thisline(pc),d1
	move.l	d1,d2
	lea	.thisline(pc),a0
	move.l	d0,(a0)
	move.l	fileaddr(pc),a0
	move.l	linelen(pc),a1
	sub.l	d0,d1
	bmi.b	.minus
	cmp.l	d0,d1
	bhs.b	.beginning

	add.l	d2,a1			;<-- curr
	move.l	.thisaddr(pc),a0
	move.l	d1,d0
	subq.l	#1,d0
	bmi.b	.ok
	moveq	#0,d1
.loop1
	move.b	-(a1),d1
	addq.w	#1,d1
	sub.w	d1,a0
	dbf	d0,.loop1
	bra.b	.ok
.minus
	neg.l	d1
	cmp.l	d0,d1
	bhs.b	.beginning
	add.l	d2,a1			;curr -->
	move.l	.thisaddr(pc),a0
	move.l	d1,d0
.beginning
	subq.l	#1,d0
	bmi.b	.ok
	moveq	#0,d1
.loop
	move.b	(a1)+,d1
	addq.w	#1,d1
	add.w	d1,a0
	dbf	d0,.loop
.ok
	lea	.thisaddr(pc),a1
	move.l	a0,(a1)
	move.l	lines(pc),d0
	sub.l	.thisline(pc),d0
	cmp.l	cheight(pc),d0
	ble.b	.ok2
	move.l	cheight(pc),d0
.ok2
	movem.l	(sp)+,d1-d2/a1
	rts
.thisaddr	dc.l	0
.thisline	dc.l	1


;in:
* a0	adress
;ut:
* d0	rad som a0 var på
getline:
	pushm	d1/a0-a2
	move.l	fileaddr(pc),a1
	move.l	linelen(pc),a2
	moveq	#0,d0
.loop
	cmp.l	a0,a1
	bhi.b	.ok		;om passerat
	move.b	(a2)+,d1
	addq.w	#1,d1
	add.w	d1,a1
	addq.l	#1,d0
	bra.b	.loop
.ok
	popm	d1/a0-a2
	rts

gotop:
	lea	linepos(pc),a0
	tst.l	(a0)
	beq.b	.exit
	clr.l	(a0)
	bra.w	textredraw
.exit
	rts
gobottom:
	lea	linepos(pc),a0
	move.l	lines(pc),d0
	sub.l	cheight(pc),d0
	bpl.b	.ok
	moveq	#0,d0
.ok
	cmp.l	(a0),d0
	beq.b	.exit
	move.l	d0,(a0)
	bra.w	textredraw
.exit
	rts
prevpage:
	lea	linepos(pc),a0
	move.l	(a0),d0
	bne.b	.ok1		;redan 0 ?
	moveq	#0,d0
	rts
.ok1
	sub.l	cheight(pc),d0
	addq.l	#1,d0
	bpl.b	.ok
	moveq	#0,d0
.ok
	move.l	d0,(a0)
	bra.w	textredraw

nextpage:
	lea	linepos(pc),a0
	move.l	(a0),d0
	move.l	d0,d7
	add.l	cheight(pc),d0
	subq.l	#1,d0
	move.l	d0,(a0)
	bsr.w	fixlinepos
	cmp.l	linepos(pc),d7
	bne.b	.ok
	moveq	#0,d0
	rts
.ok
	bra.w	textredraw


prevpagescroll:
	move.l	fonty(pc),d0
	move.l	cheight(pc),d1
	subq.l	#1,d1
	bra.w	scrolldown

nextpagescroll:
	move.l	fonty(pc),d0
	move.l	cheight(pc),d1
	subq.l	#1,d1
	bra.w	scrollup

prevpage3:
	lea	linepos(pc),a0
	move.l	(a0),d0
	bne.b	.ok1		;redan 0 ?
	moveq	#0,d0
	rts
.ok1
	move.l	cheight(pc),d1
	mulu.w	#3,d1
	sub.l	d1,d0
	bpl.b	.ok
	moveq	#0,d0
.ok
	move.l	d0,(a0)
	bra.w	textredraw
nextpage3:
	lea	linepos(pc),a0
	move.l	(a0),d0
	move.l	d0,d7
	move.l	cheight(pc),d1
	mulu.w	#3,d1
	add.l	d1,d0
	move.l	d0,(a0)
	bsr.w	fixlinepos
	cmp.l	linepos(pc),d7
	bne.b	.ok
	moveq	#0,d0
	rts
.ok
	bra.w	textredraw

;in:
* d0.w	speed (-4, -3, -2, -1, 0, 1, 2, 3 ,4  0=inget)
scrollsmooth:
	ext.l	d0
	tst.l	d0
	beq.s	.exit
	bpl.s	.plus
	neg.l	d0
	subq.l	#1,d0
	moveq	#1,d1
	lsl.l	d0,d1
	move.l	d1,d0
	moveq	#1,d1
	bra.w	scrolldown
.plus
	subq.l	#1,d0
	moveq	#1,d1
	lsl.l	d0,d1
	move.l	d1,d0
	moveq	#1,d1
	bra	scrollup
.exit
	moveq	#0,d0
	rts

scrollup1:
	move.l	fonty(pc),d0
	moveq	#1,d1
scrollup:
	bsr.b	scrollup_noupdate
	bsr.w	changescroll
	moveq	#0,d0
	rts
;in:
* d0	(Pixel)Lines/Frame (1,2,3,...,fonty,fonty*2,fonty*3,...)
* d1	Repeat times
scrollup_noupdate:
	move.l	d0,.speed
	move.w	d1,.repeat

	base	exec
	move.l	intsignal(pc),d1
	moveq	#0,d0
	bset	d1,d0
	move.l	thistask(pc),a1
	call	Signal			;gör en ny signal direkt!

.nextpass
	move.l	.speed,d0
	add.l	fonty(pc),d0
	subq.l	#1,d0
	divu.w	fonty+2(pc),d0
	and.l	#$ffff,d0
	lea	linepos(pc),a0
	move.l	(a0),-(sp)
	add.l	d0,(a0)
	bsr.w	fixlinepos
	move.l	(sp)+,d0
	sub.l	linepos(pc),d0
	neg.l	d0
	beq	.exit

	move.l	fonty(pc),d1
	cmp.l	.speed(pc),d1
	bhi.b	.softscroll

	mulu.w	fonty+2(pc),d0
	move.l	d0,.speed
	move.w	#1,.cnt
	bra.b	.scroll_loop

.softscroll
	move.l	fonty(pc),d1
	divu.w	.speed+2(pc),d1
	move.w	d1,.cnt

	move.l	.speed(pc),-(sp)			;prescroll
	swap.w	d1
	move.w	d1,.speed+2
	beq.b	.noprescroll
	bsr     .scrollup_and_clear
.noprescroll
	move.l	(sp)+,.speed

.scroll_loop
	base	exec
	moveq	#0,d0
	move.l	intsignal(pc),d1
	bset	d1,d0
	call	Wait

	bsr	.scrollup_and_clear

	lea	.cnt(pc),a0
	subq.w	#1,(a0)
	bne.s	.scroll_loop

	move.l	.speed(pc),d5
	add.l	fonty(pc),d5
	subq.l	#1,d5
	divu.w	fonty+2(pc),d5
	and.l	#$ffff,d5

.printbottom
	move.l	linepos(pc),d0
	add.l	cheight(pc),d0
	sub.l	d5,d0
	move.l	linelen(pc),a0
	moveq	#0,d3
	move.b	(a0,d0.l),d3
	bsr.w	gotoline
	cmp.b	#$ff,d3
	beq.b	.skipline
	moveq	#0,d1
	move.l	cheight(pc),d2
	sub.l	d5,d2
	bsr.w	print
.skipline
	subq.l	#1,d5
	bne.b	.printbottom

	lea	.repeat(pc),a0
	subq.w	#1,(a0)
	bne.w	.nextpass
.exit
	bsr.w	changenum
	moveq	#0,d0
	rts
.speed	dc.l	0
.repeat	dc.w	0
.cnt	dc.w	0

.scrollup_and_clear
	base	gfx

	tst.w	legalprint
	bne.w	.system

	call	OwnBlitter
	call	WaitBlit
	lea	$dff000,a5

	move.l	#$09f00000,bltcon0(a5)
	move.l	#-1,bltafwm(a5)
	move.l	plane0(pc),d0
	move.l	.speed(pc),d1
	mulu.w	bwidth+2(pc),d1
	move.l	d0,bltdpt(a5)
	add.l	d1,d0
	move.l	d0,bltapt(a5)
	move.w	#0,bltamod(a5)
	move.w	#0,bltdmod(a5)
	move.l	bwidth(pc),d0
	lsr.l	#1,d0
	move.l	cheightp(pc),d1
	sub.l	.speed(pc),d1
	btst	#GFXB_BIG_BLITS,gb_ChipRevBits0(a6)
	bne.b	.bigblit1
	lsl.l	#6,d1
	or.l	d0,d1
	move.w	d1,bltsize(a5)
	bra.b	.blitted1
.bigblit1
	move.w	d1,bltsizv(a5)
	move.w	d0,bltsizh(a5)
.blitted1
	call	WaitBlit
	move.l	#$01000000,bltcon0(a5)
	move.l	#-1,bltafwm(a5)
	move.l	cheightp(pc),d0
	sub.l	.speed(pc),d0
	mulu.w	bwidth+2(pc),d0
	add.l	plane0(pc),d0
	move.l	d0,bltdpt(a5)
	move.w	#0,bltdmod(a5)

	move.l	bwidth(pc),d0
	lsr.l	#1,d0
	move.l	.speed(pc),d1
	btst	#GFXB_BIG_BLITS,gb_ChipRevBits0(a6)
	bne.b	.bigblit2
	lsl.l	#6,d1
	or.l	d0,d1
	move.w	d1,bltsize(a5)
	bra.b	.blitted2
.bigblit2
	move.w	d1,bltsizv(a5)
	move.w	d0,bltsizh(a5)
.blitted2
	call	DisownBlitter
	call	WaitBlit
	rts
.system
	move.l	screenptr(pc),a3
	lea	sc_RastPort(a3),a3
	move.l	rp_BitMap(a3),a0

	move.l	.speed(pc),d0
	move.l	a0,a1
	suba.l	a2,a2
	move.l	d0,d1
	add.l	barheight(pc),d1
	moveq	#0,d2
	move.l	d1,d3
	sub.l	d0,d3
	move.l	cwidthp(pc),d4
	move.l	cheightp(pc),d5
	sub.l	d0,d5
	move.w	#$c0,d6
	moveq	#1,d7
	moveq	#0,d0
	call	BltBitMap

	
	move.l	a3,a1
	moveq	#0,d0
	call	SetAPen
	move.l	a3,a1
	moveq	#0,d0
	move.l	cheightp(pc),d1
	add.l	barheight(pc),d1
	sub.l	.speed(pc),d1
	move.l	cwidthp(pc),d2
	move.l	d1,d3
	add.l	.speed(pc),d3
	subq.l	#1,d3
	call	RectFill
	rts

scrolldown1:
	move.l	fonty(pc),d0
	moveq	#1,d1
scrolldown:
	bsr.b	scrolldown_noupdate
	bsr.w	changescroll
	moveq	#0,d0
	rts
;in:
* d0	(Pixel)Lines/Frame (1,2,4,8,16,24,32,40,...)
* d1	Repeat times
scrolldown_noupdate:
	move.l	d0,.speed
	move.w	d1,.repeat

	base	exec
	move.l	intsignal(pc),d1
	moveq	#0,d0
	bset	d1,d0
	move.l	thistask(pc),a1
	call	Signal			;gör en ny signal direkt!

.nextpass
	move.l	.speed,d0
	add.l	fonty(pc),d0
	subq.l	#1,d0
	divu.w	fonty+2(pc),d0
	and.l	#$ffff,d0
	lea	linepos(pc),a0
	sub.l	d0,(a0)
	bpl.s	.notbeg
	move.l	(a0),d1
	clr.l	(a0)
	add.l	d1,d0
	beq.w	.exit
.notbeg
	move.l	fonty(pc),d1
	cmp.l	.speed(pc),d1
	bhi.b	.softscroll

	mulu.w	fonty+2(pc),d0
	move.l	d0,.speed
	move.w	#1,.cnt
	bra.b	.scroll_loop

.softscroll
	move.l	fonty(pc),d1
	divu.w	.speed+2(pc),d1
	move.w	d1,.cnt

	move.l	.speed(pc),-(sp)			;prescroll
	swap.w	d1
	move.w	d1,.speed+2
	beq.b	.noprescroll
	bsr     .scrolldown_and_clear
.noprescroll
	move.l	(sp)+,.speed

.scroll_loop
	base	exec
	moveq	#0,d0
	move.l	intsignal(pc),d1
	bset	d1,d0
	call	Wait

	bsr	.scrolldown_and_clear

	lea	.cnt(pc),a0
	subq.w	#1,(a0)
	bne.s	.scroll_loop

	move.l	.speed(pc),d5
	add.l	fonty(pc),d5
	subq.l	#1,d5
	divu.w	fonty+2(pc),d5
	subq.l	#1,d5
	and.l	#$ffff,d5
.printtop
	move.l	linepos(pc),d0
	add.l	d5,d0
	move.l	linelen(pc),a0
	moveq	#0,d3
	move.b	(a0,d0.l),d3
	bsr.w	gotoline
	cmp.b	#$ff,d3
	beq.w	.skipline
	moveq	#0,d1
	move.l	d5,d2
	bsr.w	print
.skipline
	dbf	d5,.printtop

	lea	.repeat(pc),a0
	subq.w	#1,(a0)
	bne.w	.nextpass
.exit
	bsr.w	changenum
	moveq	#0,d0
	rts
.speed	dc.l	0
.repeat	dc.w	0
.cnt	dc.w	0

.scrolldown_and_clear
	base	gfx

	tst.w	legalprint
	bne.w	.system

	call	OwnBlitter
	call	WaitBlit
	lea	$dff000,a5

	move.l	#$09f00002,bltcon0(a5)
	move.l	#-1,bltafwm(a5)
	move.l	cheightp(pc),d0
	move.l	.speed(pc),d1
	mulu.w	bwidth+2(pc),d0
	mulu.w	bwidth+2(pc),d1
	add.l	plane0(pc),d0
	subq.l	#2,d0
	move.l	d0,bltdpt(a5)
	sub.l	d1,d0
	move.l	d0,bltapt(a5)
	move.w	#0,bltamod(a5)
	move.w	#0,bltdmod(a5)
	move.l	bwidth(pc),d0
	lsr.l	#1,d0
	move.l	cheightp(pc),d1
	sub.l	.speed(pc),d1
	btst	#GFXB_BIG_BLITS,gb_ChipRevBits0(a6)
	bne.b	.bigblit1
	lsl.l	#6,d1
	or.l	d0,d1
	move.w	d1,bltsize(a5)
	bra.b	.blitted1
.bigblit1
	move.w	d1,bltsizv(a5)
	move.w	d0,bltsizh(a5)
.blitted1
	call	WaitBlit
	move.l	#$01000000,bltcon0(a5)
	move.l	#-1,bltafwm(a5)
	move.l	plane0(pc),bltdpt(a5)
	move.w	#0,bltdmod(a5)

	move.l	bwidth(pc),d0
	lsr.l	#1,d0
	move.l	.speed(pc),d1
	btst	#GFXB_BIG_BLITS,gb_ChipRevBits0(a6)
	bne.b	.bigblit2
	lsl.l	#6,d1
	or.l	d0,d1
	move.w	d1,bltsize(a5)
	bra.b	.blitted2
.bigblit2
	move.w	d1,bltsizv(a5)
	move.w	d0,bltsizh(a5)
.blitted2
	call	DisownBlitter
	call	WaitBlit
	rts
.system
	move.l	screenptr(pc),a3
	lea	sc_RastPort(a3),a3
	move.l	rp_BitMap(a3),a0

	move.l	.speed(pc),d0
	move.l	a0,a1
	suba.l	a2,a2
	move.l	barheight(pc),d1
	moveq	#0,d2
	move.l	d1,d3
	add.l	d0,d3
	move.l	cwidthp(pc),d4
	move.l	cheightp(pc),d5
	sub.l	d0,d5
	move.w	#$c0,d6
	moveq	#1,d7
	moveq	#0,d0
	call	BltBitMap

	move.l	a3,a1
	moveq	#0,d0
	call	SetAPen
	move.l	a3,a1
	moveq	#0,d0
	move.l	barheight(pc),d1
	move.l	cwidthp(pc),d2
	move.l	d1,d3
	add.l	.speed(pc),d3
	subq.l	#1,d3
	call	RectFill

	rts

**** text maintaining

textredraw:
	bsr.W	fixlinepos
	bsr.w	changescroll
	bsr.w	changenum
	bsr.w	cls
	move.l	linepos(pc),d0
	bsr.w	gotoline
	move.l	a0,d1			;INTE d0 !! (Bara tst.l	a0)
	beq.b	.notext
	bsr.w	printpage
	bra.b	.exit
.notext					;Inte? Skriv ut © då!
	moveq	#0,d1
	moveq	#0,d2
	move.w	#.hailtextend-.hailtext-1,d3
	lea	.hailtext(pc),a0
	bsr.w	print
.exit
	moveq	#0,d0
	rts
;FIXIT! Localize it!
.hailtext
 dc.b "Textread v"
 releasestr
 dc.b " - Copyright ©1992-1995 Martin Blom.",10
 dc.b "Shareware! Don't forget to send 40 SEK, 20 DM or US$ 10 to:",10,10
 dc.b "  Martin Blom",10
 dc.b "  Alsättersgatan 15A.24",10
 dc.b "S-582 51 Linköping",10
 dc.b "  Sweden",10,10
 dc.b "And yes, I prefer SEK. Send comments, bugreports and support letters to",10
 dc.b "the address above or why not email me:",10,10
 dc.b "lcs@lysator.liu.se or d93marbl@und.ida.liu.se"
.hailtextend
	even

fixlinepos:
	pushm	d1/a0
	move.l	lines(pc),d1
	sub.l	cheight(pc),d1
	bmi.b	.minus
	lea	linepos(pc),a0
	cmp.l	(a0),d1
	bhs.b	.ok
	move.l	d1,(a0)
	bra.b	.ok
.minus
	lea.l	linepos(pc),a0
	clr.l	(a0)
.ok
	popm	d1/a0
	rts

printhelp:
	base	intui
	move.l	windowptr(pc),a0
	move.l	#IDCMP_GADGETDOWN!IDCMP_GADGETUP!IDCMP_MOUSEBUTTONS!IDCMP_VANILLAKEY!IDCMP_RAWKEY,d0
	call	ModifyIDCMP

	move.l	windowptr(pc),a0
	or.l	#WFLG_RMBTRAP,wd_Flags(a0)

	bsr.w	cls
	moveq	#0,d1
	moveq	#0,d2
	move.w	#.helptextend-.helptext-1,d3
	lea	.helptext(pc),a0
	bsr.w	print

	move.l	windowptr(pc),a0
	move.l	wd_UserPort(a0),a2
	move.l	a2,a0
	base	exec
	call	WaitPort
.next
	move.l	a2,a0
	call	GetMsg
	tst.l	d0
	beq.b	.null
	move.l	d0,a1
	call	ReplyMsg
	bra.b	.next
.null
	base	intui
	move.l	windowptr(pc),a0
	move.l	#IDCMP_INACTIVEWINDOW!IDCMP_MOUSEBUTTONS!IDCMP_MENUVERIFY!IDCMP_MOUSEBUTTONS!ARROWIDCMP!SCROLLERIDCMP!IDCMP_REFRESHWINDOW!IDCMP_MENUPICK!IDCMP_VANILLAKEY!IDCMP_RAWKEY,d0
	call	ModifyIDCMP

	move.l	windowptr(pc),a0
	and.l	#~WFLG_RMBTRAP,wd_Flags(a0)
	bsr.w	textredraw
	moveq	#0,d0
	rts
;FIXIT! Localize it!
.helptext
 dc.b 'Keys used in Textread',10
 dc.b '¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯',10
 dc.b ' Help, H, ? ....................... Show this helppage.',10
 dc.b ' Q, Shift Esc, Ctrl C ............. Quit Textread.',10
 dc.b ' Esc .............................. Close current buffer/Quit.',10
 dc.b ' Down, Return, Enter .............. Scroll text down.',10
 dc.b ' Left, Shift Up, BackSpace, PgUp .. Show previous page.',10
 dc.b ' Right, Shift Down, Space, PgDn ... Show next page.',10
 dc.b ' Alt Up/Down ...................... Move 3 pages up/down.',10
 dc.b ' Ctrl Up, T, <, Home .............. Go to first page.',10
 dc.b ' Ctrl Down, B, >, End ............. Go to last page.',10
 dc.b ' L/# / G/% ........................ Go to line/percent.',10
 dc.b ' (Shift) F1 - (Shift) F10 ......... Go to (Set) bookmark',10
 dc.b ' S, "." ........................... Search for string... (no case)',10
 dc.b ' Shift S, "/" ..................... Search for string... (case)',10
 dc.b ' N/P .............................. Find next/previous.',10
 dc.b ' W ................................ Wordwrap on/off.',10
 dc.b ' Tab .............................. Select tab size...',10
 dc.b ' Shift-E .......................... Edit text using default editor.',10
 dc.b ' 1-0 , +/-, Alt Left/Right ........ Change buffer.',10
.helptextend
	even

printpage:
	pushm	d0-d7/a1-a6
	tst.w	legalprint
	bne.b	.system
	bsr.b	printpage_fast
	bra.b	.exit
.system
	bsr.w	printpage_system
.exit
	popm	d0-d7/a1-a6
	rts
;in:
* a0	text
* d0	antal rader
;ut:
* a0	text efter sidslut
printpage_fast:
	move.l	radpos(pc),a5
	lea	font,a3
	move.l	linelen(pc),a6
	add.l	linepos(pc),a6
	move.l	bwidth(pc),d4
	move.l	d0,d5			;antal rader
	subq.l	#1,d5
	bmi.b	.pagefinished
	moveq	#" ",d2
	moveq	#TAB,d6
	moveq	#LF,d7
.nextline
	move.l	(a5)+,a1
	moveq	#0,d1
	moveq	#0,d3
	move.b	(a6)+,d3
	cmp.w	#$ff,d3			;255 tecken/rad inte ok, utan ev eol.
	beq.b	.linefinished
.nextchar
	moveq	#0,d0
	move.b	(a0)+,d0
	cmp.b	d2,d0			;space
	beq.b	.space
	cmp.b	d6,d0			;tab
	beq.b	.tab
	cmp.b	d7,d0			;lf
	beq.b	.linefinished
	lsl.w	#3,d0
	move.l	a1,a2
	lea	(a3,d0.w),a4	;12
	move.b	(a4)+,(a1)+	;öka med ett för varje tecken
 	add.w	d4,a2
	move.b	(a4)+,(a2)
	add.w	d4,a2
	move.b	(a4)+,(a2)
	add.w	d4,a2
	move.b	(a4)+,(a2)
	add.w	d4,a2
	move.b	(a4)+,(a2)
	add.w	d4,a2
	move.b	(a4)+,(a2)
	add.w	d4,a2
	move.b	(a4)+,(a2)
	move.b	(a4)+,(a2,d4.w)	;2c snabbare än add d4,a2 först,6 extra
	addq.w	#1,d1
.charfinished
	dbf	d3,.nextchar
.linefinished
	dbf	d5,.nextline
.pagefinished
	rts
.space
	addq.l	#1,a1
	addq.w	#1,d1
	dbf	d3,.nextchar
	dbf	d5,.nextline
	rts
.tab
	lea	tabbar,a2
	move.b	(a2,d1.w),d0		;word rensat
	sub.w	d1,d0
	add.w	d0,a1
	move.b	(a2,d1.w),d1
	bra.b	.charfinished

;in:
* a0	text
* d0	antal rader
;ut:
* a0	text efter sidslut
printpage_system:
	move.l	linelen(pc),a6
	add.l	linepos(pc),a6
	move.w	d0,d5
	subq.w	#1,d5
	beq.b	.pagefinished

	moveq	#0,d1
	moveq	#0,d2

.nextline
	moveq	#0,d3
	move.b	(a6)+,d3
	cmp.w	#$ff,d3			;255 tecken/rad inte ok, utan ev eol.
	beq.b	.linefinished
	bsr	print
.linefinished
	moveq	#0,d1
	addq.w	#1,d2
	dbf	d5,.nextline
.pagefinished
	rts

printline:
	pushm	d0-d7/a1-a6
	tst.w	legalprint
	bne.b	.system
	bsr.b	printline_fast
	bra.b	.exit
.system
	bsr.w	printline_system
.exit
	popm	d0-d7/a1-a6
	rts
print:
	pushm	d0-d7/a1-a6
	tst.w	legalprint
	bne.b	.system
	bsr.b	print_fast
	bra.b	.exit
.system
	bsr.b	print_system
.exit
	popm	d0-d7/a1-a6
	rts
;in:
* d1.w	x-pos (tecken)
* d2.w	y-pos (print: tecken ; printline: pixels)
* d3.w	total längd-1 (-1 skriver nästan allt)
* a0	text
;ut:
* a0	text uppdaterat
printline_fast:				;INGA LF!!
	move.l	plane0(pc),a1
	mulu.w	bwidth+2(pc),d2
	add.l	d2,a1
	bra.b	printit

print_fast:
	move.w	d2,d4
	add.w	d4,d4
	add.w	d4,d4
	move.l	radpos(pc),a1
	move.l	(a1,d4.w),a1

printit:
	lea	tabbar,a5
	lea	font,a3
	move.l	bwidth(pc),d4
.nextchar
	moveq	#0,d0
	move.b	(a0)+,d0
	cmp.b	#TAB,d0
	bne.B	.notab
	move.b	(a5,d1.w),d1
	bra.b	.charfinished
.notab
	cmp.b	#LF,d0
	beq.b	.printlf
	lsl.w	#3,d0
	lea	(a1,d1.w),a2
	lea	(a3,d0.w),a4
	moveq	#8-1,d5				;fonty?
.printchar
	move.b	(a4)+,(a2)
	add.w	d4,a2
	dbf	d5,.printchar
	addq.w	#1,d1
.charfinished
	dbf	d3,.nextchar
	rts
.printlf
	moveq	#0,d1
	addq.w	#1,d2
	dbf	d3,print_fast
	rts

;in:
* d1.w	x-pos (tecken)
* d2.w	y-pos (print: tecken ; printline: pixels)
* d3.w	total längd-1 (-1 skriver nästan allt)
* a0	text
;ut:
* a0	text uppdaterat
print_system:
	mulu.w	fonty+2(pc),d2
printline_system:			;INGA LF!!
	mulu.w	fontx+2(pc),d1
	base	gfx
	move.l	a0,a3
	move.l	windowptr(pc),a2
	move.l	wd_RPort(a2),a2

	add.l	barheight(pc),d2
	add.w	rp_TxBaseline(a2),d2

	pushm	a0/d1
	move.l	a2,a1
	moveq	#1,d0
	call	SetAPen
	move.l	a2,a1
	moveq	#0,d0
	call	SetBPen
	popm	a0/d1
.nextline
	pushm	a0/d1
	move.l	a2,a1
	move.w	d1,d0
	move.w	d2,d1
	call	Move
	popm	a0/d1

	lea	temp,a1
	moveq	#0,d4
	lea	tabbar,a5
.nextchar
	moveq	#0,d0
	move.b	(a0)+,d0
	cmp.b	#TAB,d0
	bne.b	.notab

	move.b	(a5,d1.w),d0
	sub.b	d1,d0
	add.b	d0,d1
	add.b	d0,d4
	subq.b	#1,d0
.filltab
	move.b	#" ",(a1)+
	dbf	d0,.filltab
	bra.b	.charfinished
.notab
	cmp.b	#LF,d0
	beq.b	.printlf
	move.b	d0,(a1)+
	addq.w	#1,d4
	addq.w	#1,d1
.charfinished
	dbf	d3,.nextchar
	pushm	a0/d1
	lea	temp,a0
	move.l	a2,a1
	move.w	d4,d0
	call	Text
	popm	a0/d1
	rts
.printlf
	pushm	a0/d1
	lea	temp,a0
	move.l	a2,a1
	move.w	d4,d0
	call	Text
	popm	a0/d1

	moveq	#0,d1
	add.w	fonty+2(pc),d2
	dbf	d3,.nextline
	rts

;in:
* d0	FALSE om Open, TRUE om Save
;ut:
* d0	pek till filnamn med sökväg, eller NULL om fel/cancel
askfile:
	lea	filereqsave(pc),a0
	clr.l	(a0)
	lea	filereqtitle(pc),a1
	lea	openfiletext(pc),a2
	tst.l	d0
	beq.b	.wasopen
	addq.l	#TRUE,(a0)
	lea	savefiletext(pc),a2
.wasopen
	move.l	a2,(a1)

	base	asl
	move.l	filereqptr(pc),a0
	lea	filereqtags(pc),a1
	call	AslRequest
	tst.l	d0
	beq.s	.error

	move.l	filereqptr(pc),a0
	move.l	fr_Drawer(a0),a1
	move.l	fr_Pattern(a0),a0
	lea	filereqdir(pc),a2
	lea	filereqpat(pc),a3
	move.w	#255,d0
.copydir
	move.b	(a1)+,(a2)+
	dbeq	d0,.copydir
	moveq	#63,d0
.copypat
	move.b	(a0)+,(a3)+
	dbeq	d0,.copypat

	move.l	filereqptr(pc),a0
	move.l	fr_File(a0),a1
	move.l	fr_Drawer(a0),a0

	lea	temp,a2
	move.w	#tempsize,d0
	bsr.w	pathnfiletobuff
	tst.l	d0
	bmi.b	.error
.exit
	move.l	#temp,d0
	rts
.error
	moveq	#0,d0
	rts

;in:
* d0	pek till filnamn med sökväg
;ut:
* d0	NULL om ok
openfile:
	pushm	d1-d7/a0-a6
	move.l	d0,-(sp)

	Message	loadingtext

	move.l	(sp),a0
	bsr.w	loadfile
	move.l	d0,d4			;->
	move.l	a0,a2
	move.l	d1,d3

	base	exec
	move.l	fileaddr(pc),a1
	move.l	filememlen(pc),d0
	beq.B	.nofileinmem
	call	FreeMem
.nofileinmem
	lea	fileaddr(pc),a0
	move.l	a2,(a0)

	lea	filememlen(pc),a0
	move.l	d3,(a0)

	lea	filelen(pc),a0
	move.l	d2,(a0)
	clr.l	linepos
	lea	fkeys_lines(pc),a0
	moveq	#9,d0
.clrpos
	move.l	#-1,(a0)+
	dbf	d0,.clrpos

	move.l	(sp)+,d2
	move.l	d2,d1

	base	dos
	call	FilePart
	move.l	d0,a0
	lea	filename(pc),a1
	moveq	#31,d0
.copynameloop
	move.b	(a0)+,(a1)+
	dbeq	d0,.copynameloop

	move.l	d2,d1
	call	PathPart
	move.l	d0,a0
	clr.b	(a0)		;klipp bort filnamnet
	move.l	d2,a0
	lea	filereqdir(pc),a1
	move.w	#255,d0
.copydirloop
	move.b	(a0)+,(a1)+
	dbeq	d0,.copydirloop

	Message	screentitle

	tst.l	d4			;<-
	bne.b	.exit

	tst.w	alwaysremansi
	beq.b	.dontremansi
	bsr.w	RemoveANSI
.dontremansi
	tst.w	alwaysremcr
	beq.b	.dontremcr
	bsr.w	RemoveCR
.dontremcr
	bsr.w	countlines
	tst.l	d0
	bne.b	.error
	bsr.w	textredraw

.exit
	moveq	#0,d0
.error
	popm	d1-d7/a0-a6
	rts

;in:
* a0	filename
;ut:
* d0	0 om ok, -1 om fel
* d1	filememlen	(allokerat minne)
* d2	filelen		(filstorlek)
* a0	fileaddr
loadfile:
	clr.l	.addr
	clr.l	.memlen
	clr.l	.len

	base	xpk
	move.l	a6,d0
	beq.b	.pp			;ingen xpk, anv pp

	move.l	a0,.xpkfilename
	lea	.xpktags(pc),a0
	call	XpkUnpack
	tst.l	d0
	bne.w	.error
	bra.w	.fileloadedok
.pp
	base	pp			;a0 klar
	move.l	a6,d0
	beq.b	.dos			;ingen pp, anv dos
	lea	.addr(pc),a1
	lea	.len(pc),a2
	suba.l	a3,a3
	moveq	#DECR_POINTER,d0
	moveq	#MEMF_PUBLIC,d1
	call	ppLoadData
	tst.l	d0
	bne.W	.error
	bra.w	.fileloadedok
.dos
	base	dos
	move.l	a0,d1
	move.l	#MODE_OLDFILE,d2
	call	Open
	move.l	d0,.file
	bne.s	.dosnoerr1
	bsr.w	.dosclean
	lea	errnofile(pc),a1
	bra.w	.error
.dosnoerr1
	moveq	#DOS_FILEHANDLE,d1
	lea	tag_done(pc),a0
	move.l	a0,d2
	call	AllocDosObject
	move.l	d0,.finfo
	bne.s	.dosnoerr2
	bsr.w	.dosclean
	lea	errnomem(pc),a1
	bra.w	.error
.dosnoerr2
	move.l	.file(pc),d1
	move.l	.finfo(pc),d2
	call	ExamineFH
	tst.l	d0
	bne.s	.dosnoerr3
	bsr.s	.dosclean
	lea	errnofile(pc),a1
	bra.w	.error
.dosnoerr3
	move.l	.finfo(pc),a0
	move.l	fib_Size(a0),d0
	lea	.len(pc),a0
	move.l	d0,(a0)
	moveq	#MEMF_ANY,d1
	base	exec
	call	AllocMem
	lea	.addr(pc),a0
	move.l	d0,(a0)
	bne.s	.dosnoerr4
	bsr.s	.dosclean
	lea	errnomem(pc),a1
	bra.w	.error
.dosnoerr4
	base	dos
	move.l	.file(pc),d1
	move.l	.addr(pc),d2
	move.l	.len(pc),d3
	call	Read
	cmp.l	d0,d3
	beq.s	.dosnoerr5
	bsr.s	.dosclean
	base	exec
	move.l	.addr(pc),a1
	move.l	.len(pc),d0
	beq.B	.dosnofilemem
	call	FreeMem
.dosnofilemem
	lea	errnofile(pc),a1
	bra.w	.error
.dosnoerr5
	bsr.s	.dosclean
	bra.s	.fileloadedok
.dosclean
	base	dos
	move.l	.file(pc),d1
	beq.s	.doserr_nofile
	clr.l	.file
	call	Close
.doserr_nofile
	move.l	.finfo(pc),d2
	beq.s	.doserr_nofinfo
	clr.l	.finfo
	moveq	#DOS_FILEHANDLE,d1
	call	FreeDosObject
	clr.l	.finfo
.doserr_nofinfo
	rts
.fileloadedok
	move.l	.addr(pc),a0
	move.l	.memlen(pc),d1
	bne.b	.memlenok
	move.l	.len(pc),d1		;pp & dos har bara 2 tal, xpk har 3
.memlenok
	move.l	.len(pc),d2

	moveq	#0,d0
	rts

.addr	dc.l	0
.len	dc.l	0
.memlen	dc.l	0
.file	dc.l	0	;dosloader
.finfo	dc.l	0	;dosloader

.xpktags
	dc.l	XPK_InName
.xpkfilename
	dc.l	0
	dc.l	XPK_GetError,XPKerrbuf
	dc.l	XPK_GetOutBuf,.addr
	dc.l	XPK_GetOutLen,.len
	dc.l	XPK_GetOutBufLen,.memlen
	dc.l	XPK_PassThru,-1
	dc.l	TAG_DONE
	even
;in:
* .errbuf Textsträng med felmeddelande
* eller
* d0	pp error code
* eller
* a1	felmeddelande (dos)
.error
	base	xpk
	move.l	a6,d1
	beq.b	.pperr
	lea	XPKerrbuf,a1
	bra.b	.showerr
.pperr
	base	pp
	move.l	a6,d1
	beq.b	.doserr
	call	ppErrorMessage
	move.l	d0,a1
	bra.w	.showerr
.doserr
.showerr
	lea	oktext(pc),a2
	suba.l	a4,a4
	bsr.w	requester

	base	exec
	move.l	.addr(pc),a1
	move.l	.len(pc),d0
	beq.B	.noerrorfileinmem
	call	FreeMem
.noerrorfileinmem
	moveq	#-1,d0
	moveq	#0,d1
	move	#0,d2
	suba.l	a0,a0
	rts

;in:
* d0	pek till filnamn
;ut:
* d0	NULL om ok
savefile:
	pushm	d0
	Message	savingtext
	base	dos
	popm	d5					;-=>
	move.l	d5,d1
	move.l	#MODE_NEWFILE,d2
	call	Open
	move.l	d0,d7					;->
	beq.w	.exit
	move.l	d7,d1					;<-
	move.l	fileaddr(pc),d2
	move.l	filelen(pc),d3
	call	Write
	moveq	#0,d6					;=>
	cmp.l	d0,d3
	beq.b	.writeok

	lea	writeerr(pc),a1
	lea	oktext(pc),a2
	suba.l	a4,a4
	bsr	requester

	moveq	#1,d6					;=>
.writeok
	base	dos
	move.l	d7,d1					;<-
	call	Close

	move.l	d5,d1					;<=-
	base	dos
	call	FilePart
	move.l	d0,a0
	lea	filename(pc),a1
	moveq	#31,d0
.copynameloop
	move.b	(a0)+,(a1)+
	dbeq	d0,.copynameloop

	move.l	d5,d1
	call	PathPart
	move.l	d0,a0
	clr.b	(a0)		;klipp bort filnamnet
	move.l	d5,a0
	lea	filereqdir(pc),a1			;<=-
	move.w	#255,d0
.copydirloop
	move.b	(a0)+,(a1)+
	dbeq	d0,.copydirloop

.exit
	Message	screentitle
	move.l	d6,d0					;<=
	rts


;calls countlines if neccessary (sp?)
recountlines:
	move.l	clcwidth(pc),d0
	cmp.l	cwidth(pc),d0
	bne.b	countlines
	rts

;ut:
* d0	0 om ok, eller felkod
* linelen: pek till minnesblock med radlängder
* linelensize: minnesblockets storlek
countlines:
	Message	counting

;	Onödigt. Bättre att man får en rad med längd 255=eol markering.
;	lea	filelen(pc),a0
;	tst.l	(a0)
;	beq.w	.exit

	base	exec		;fria gammalt minne
	move.l	linelen(pc),a1
	move.l	linelensize(pc),d0
	beq.b	.ingetlineminne
	call	FreeMem
.ingetlineminne
	lea	linelen(pc),a0
	lea	notnull(pc),a1			;pek till 255
	move.l	a1,(a0)
	lea	linelensize(pc),a0
	clr.l	(a0)

;	moveq	#LF,d1
;	moveq	#0,d0
;	move.l	fileaddr(pc),a0
;	move.l	filelen(pc),d0
;	beq.s	.nofile
;.cntlf
;	subq.l	#1,d0
;	beq.s	.done
;	cmp.b	(a0)+,d1
;	bne.s	.cntlf
;	addq.l	#1,d2
;	bra.s	.cntlf
.done
.nofile

	move.l	#1024,d2		;1024 rader till att börja med
;	move.l	filelen(pc),d2
;	move.l	cwidth(pc),d0
;	divu	d0,d2
;	bvc.s	.under64k
;	move.w	#$ffff,d2
;.under64k

	cmp.w	#512,d2
	bhs.s	.sizeok
	move.w	#512,d2
.sizeok
	and.l	#$ffff,d2		;bort med resten!
	bsr.w	alloclinemem
	tst.l	d0
	beq.w	.error_nomem

	move.l	fileaddr(pc),a0
	move.l	a0,a1
	add.l	filelen(pc),a1

	lea	tabbar,a2
	move.l	linelen(pc),a4
	move.l	linelensize(pc),a5
	add.l	a4,a5
	move.l	cwidth(pc),d1
	subq.l	#1,d1
	moveq	#1,d2		;alltid 1 rad!
	moveq	#LF,d3
	moveq	#TAB,d4
	moveq	#" ",d5
	tst.w	wordwrap
	bne.B	.ww
.nextline
	moveq	#0,d0
	moveq	#-1,d7
.huntlf
	cmp.l	a0,a1		;sista tecknet
	beq.b	.klar
	move.b	(a0)+,d6
	addq.l	#1,d7
	cmp.b	d4,d6		;TAB?
	bne.B	.notab
	move.b	(a2,d0.w),d0
	bra.b	.testok
.notab
	addq.l	#1,d0		;nej,add 1
	cmp.b	d3,d6		;LF?
	beq.B	.lineok
.testok
	cmp.l	d1,d0		;nej fortsätt om ok
	bls.b	.huntlf

	move.b	(a0),d6
	cmp.b	d3,d6		;LF
	beq.b	.add1
	cmp.b	d4,d6		;TAB?
	beq.b	.add1
	cmp.b	d5,d6		;space
	bne.b	.lineok
.add1
	addq.l	#1,a0
	addq.l	#1,d7
.lineok
	addq.l	#1,d2		;ja, ny rad
	move.b	d7,(a4)+
	cmp.l	a4,a5
	bne.b	.nextline
	move.l	d2,-(sp)
	move.l	linelensize(pc),d2
	add.l	#1024,d2		;öka med 1024 i taget
	bsr.w	alloclinemem
	move.l	(sp)+,d2
	tst.l	d0
	beq.w	.error_nomem
	move.l	d0,a4
	move.l	linelen(pc),a5
	add.l	linelensize(pc),a5
	bra.b	.nextline
.klar
	move.b	d7,(a4)+		;sista radlängden...
	lea	lines(pc),a0
	move.l	d2,(a0)
	bra.b	.exit
.ww
..nextline
	moveq	#0,d0
	suba.l	a3,a3
	moveq	#-1,d7
	suba.l	a6,a6
..huntlf
	cmp.l	a0,a1		;sista tecknet
	beq.b	.klar

	move.b	(a0)+,d6
	addq.l	#1,d7
	cmp.b	d4,d6		;TAB?
	bne.b	..notab
	move.l	a0,a3		;spara tab
	move.l	d7,a6
	move.b	(a2,d0.w),d0
	bra.b	..testok
..notab
	cmp.b	d5,d6
	bne.b	..nospace
	move.l	a0,a3		;spara space
	move.l	d7,a6
..nospace
	addq.l	#1,d0		;nej,add 1
	cmp.b	d3,d6		;LF?
	beq.B	..lineok
..testok
	cmp.w	d1,d0		;nej fortsätt om ok
	bls.b	..huntlf

	move.b	(a0),d6
	cmp.b	d3,d6		;LF
	beq.b	..add1
	cmp.b	d4,d6		;TAB?
	beq.b	..add1
	cmp.b	d5,d6		;space
	beq.b	..add1
	move.w	a6,d6		;antingen är första tecknet " " eller TAB eller
	beq.b	..lineok	;bara ett ord.
	move.l	a3,a0
	move.l	a6,d7
	bra.b	..lineok
..add1
	addq.l	#1,a0
	addq.l	#1,d7
..lineok
	addq.l	#1,d2		;ja, ny rad
	move.b	d7,(a4)+
	cmp.l	a4,a5
	bne.b	..nextline
	move.l	d2,-(sp)
	move.l	linelensize(pc),d2
	add.l	#1024,d2	;öka med 1024 i taget
	bsr.w	alloclinemem
	move.l	(sp)+,d2
	tst.l	d0
	beq.b	.error_nomem
	move.l	d0,a4
	move.l	linelen(pc),a5
	add.l	linelensize(pc),a5
	bra.b	..nextline

.exit
	moveq	#0,d0			;Eftersom radlängden kanske ändrats,
	bsr.w	gotoline		;är det bäst att se till att vi inte
					;är nånstant i mitten på en rad.
	bsr	updatemenu
	lea	clcwidth(pc),a0
	move.l	cwidth(pc),(a0)
	moveq	#0,d0
	rts
.error_nomem
	bsr.b	.exit
	lea	clcwidth(pc),a0
	clr.l	(a0)
	moveq	#err_nomem,d0
	rts

alloclinemem:	;d2 size
	movem.l	d1-a6,-(sp)
	base	exec
	move.l	d2,d0
	move.l	#MEMF_PUBLIC!MEMF_CLEAR,d1
	call	AllocMem
	move.l	d0,d3			;->
	move.l	d0,a2			;->
	beq.B	.error
	move.l	linelensize(pc),d0
	beq.b	.ingetminne
	move.l	d0,d1
	subq.l	#1,d1
	move.l	linelen(pc),a0
	move.l	d3,a1			;<-
.loop
	move.b	(a0)+,(a1)+
	dbf	d1,.loop
	move.l	a1,a2			;->
	move.l	linelen(pc),a1		;d0 ok
	call	FreeMem
.ingetminne
	lea	linelen(pc),a0
	move.l	d3,(a0)			;<-
	lea	linelensize(pc),a0
	move.l	d2,(a0)
	move.l	a2,d0	;addr att fortsätta	;<-
.exit
	movem.l	(sp)+,d1-a6
	rts
.error
	moveq	#0,d0
	bra.b	.exit

**** menus

;in:
* a0	pek till menystrukt
* a1	pek till statusword
* d1	förstörs       ¯¯¯¯
;ut:
* d0	0 om ingen förändring, annars 1
checkmark:
	moveq	#0,d1
	move.w	mi_Flags(a0),d0
	and.w	#CHECKED,d0
	beq.b	.notchecked
	moveq	#1,d1
.notchecked
	moveq	#0,d0
	cmp.w	(a1),d1
	beq.b	.exit
	moveq	#1,d0
.exit
	move.w	d1,(a1)
	movem.l	d0-a6,-(sp)
	bsr.w	changenum		;uppdatera statusbokstäverna därnere!
	movem.l	(sp)+,d0-a6
	rts

updatemenu:
;update thisbuffnum
	move.l	thisbuff(pc),a1
	lea	bufflist(pc),a0
	lea	bnum(pc),a2
	clr.l	(a2)
	lea	bmax(pc),a3
	clr.l	(a3)
.stepdown
	TSTNODE	a0,a0
	beq.b	.fel
	addq.l	#1,(a2)
	addq.l	#1,(a3)
	cmp.l	a0,a1
	bne.b	.stepdown
.stepdown2
	TSTNODE	a0,a0
	beq.b	.klar
	addq.l	#1,(a3)
	bra.b	.stepdown2
.klar
.fel
	base	intui
	lea	bufflist(pc),a0
	move.l	MLH_HEAD(a0),d0
	cmp.l	MLH_TAILPRED(a0),d0
	bne.b	.notlast
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!10<<5!0,d0	;reveal... item
	call	OffMenu
;	move.l	windowptr(pc),a0
;	move.w	#$1f<<11!11<<5!0,d0	;close item
;	call	OffMenu
	bra.b	.closeok
.notlast
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!10<<5!0,d0	;reveal... item
	call	OnMenu
;	move.l	windowptr(pc),a0
;	move.w	#$1f<<11!11<<5!0,d0	;close item
;	call	OnMenu
.closeok
	lea.l	legalprint(pc),a0
	tst.w	(a0)
	bne.b	.legal
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!4<<5!1,d0	;nofake item
	call	OnMenu
	bra.b	.legalchecked
.legal
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!4<<5!1,d0	;nofake item
	call	OffMenu
.legalchecked
	lea.l	foreignbitmap(pc),a0
	tst.w	(a0)
	bne.b	.foreign
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!5<<5!1,d0	;legalprint item
	call	OnMenu
	bra.b	.foreignchecked
.foreign
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!5<<5!1,d0	;nofake item
	call	OffMenu
.foreignchecked

	move.l	diskfontbase(pc),d0
	bne.b	.diskfontchecked
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!10<<5!1,d0	;nofake item
	call	OffMenu
.diskfontchecked

	lea.l	filelen(pc),a0
	tst.l	(a0)
	beq.b	.notextinmem
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!$3f<<5!2,d0	;commands menu
	call	OnMenu
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!7<<5!0,d0	;print item
	call	OnMenu
	pea	menutext(pc)
	bra.b	.fixmenu
.notextinmem
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!$3f<<5!2,d0	;commands menu
	call	OffMenu
	move.l	windowptr(pc),a0
	move.w	#$1f<<11!7<<5!0,d0	;print item
	call	OffMenu
	pea	menuemptytext(pc)
.fixmenu
	base	exec
	lea	screentitle,a3
	move.l	(sp)+,a0
	lea	.args(pc),a1
	move.l	lines(pc),16(a1)
	lea	.rawdofmtfunc(pc),a2
	call	RawDoFmt

	Message	screentitle
	rts

.rawdofmtfunc
	move.b	d0,(a3)+
	rts

.releasestr
	releasestr
	dc.b	0
	even

.args
	dc.l	.releasestr
bnum:	dc.l	0	;buffer #
bmax:	dc.l	0
	dc.l	filename
	dc.l	0	;lines

updatecheckmarks:
	base	intui
	move.l	windowptr(pc),a0
	call	ClearMenuStrip

	move.l	winmenuptr(pc),a0
.nextmenu
	move.l	a0,d1	;tst!
	beq.b	.exit
	move.l	mu_FirstItem(a0),a1
.nextitem
	move.l	a1,d1	;tst!
	beq.B	.noitem
	move.l	mi_SubItem(a1),a2
.nextsub
	move.l	a2,d1	;tst!
	beq.B	.nosub

	GTMENUITEM_USERDATA	a2,a3
	move.w	mi_Flags(a2),d1
	move.w	d1,d3
	and.w	#CHECKIT,d1
	beq.b	.nosubflag
	or.w	#CHECKED,d3	;sätt
	tst.w	-(a3)		;wordet före är status
	bne.B	.subok		;om satt
	and.w	#~CHECKED,d3	;rensa
.subok
	move.w	d3,mi_Flags(a2)
.nosubflag
	move.l	mi_NextItem(a2),a2
	bra.b	.nextsub
.nosub
	GTMENUITEM_USERDATA	a1,a3
	move.w	mi_Flags(a1),d1
	move.w	d1,d3
	and.w	#CHECKIT,d1
	beq.b	.noitemflag
	or.w	#CHECKED,d3	;sätt
	tst.w	-(a3)		;wordet före är status
	bne.B	.itemok		;om satt
	and.w	#~CHECKED,d3	;rensa
.itemok
	move.w	d3,mi_Flags(a1)
.noitemflag
	move.l	mi_NextItem(a1),a1
	bra.b	.nextitem
.noitem
	move.l	mu_NextMenu(a0),a0
	bra.b	.nextmenu
.exit
	move.l	windowptr(pc),a0
	move.l	winmenuptr(pc),a1
	call	ResetMenuStrip
	bsr.w	changenum	;uppdatera IWSC därnere!
	moveq	#0,d0
	rts

**** find

search_case:	dc.w	0	;1=case sensitive
search_wild:	dc.w	0	;1=wildcards on
search_dir:	dc.w	0	;1=backwards
search_then:	dc.w	0	;1=restart

searchlen:	dc.w	0

search:
	tst.l	filelen
	beq.s	.exit
	jsr	requestsearch
	cmp.w	#1,d0
	bne.s	.exit

	moveq	#-1,d0
	lea	searchstring,a0
.calclen
	addq.w	#1,d0
	tst.b	(a0)+
	bne.b	.calclen
	lea	searchlen(pc),a0
	move.w	d0,(a0)
	tst.w	search_dir
	bne.w	searchprev
	bra.w	searchnext
.exit
	moveq	#0,d0
	rts

;in:
* a0	addr to first char found
* a2	addr to last char found +1 (if a0=a2, mark the whole line)
searchfound:
	move.l	a2,a1
	pushm	a0
	bsr	searchmarkfound
	popm	a0
	bsr.w	getline
	subq.l	#1,d0
	lea	linepos(pc),a0
	move.l	d0,(a0)
	bsr.w	textredraw

	bsr	setwritemask

	move.l	a2,a1
	moveq	#RP_COMPLEMENT,d0
	call	SetDrMd

	move.l	foundlinestart(pc),d1
	sub.l	linepos(pc),d1
	bmi.b	.quit
	cmp.l	cheight(pc),d1
	bhs.b	.quit
	mulu.w	fonty+2(pc),d1
	add.l	barheight(pc),d1
	move.w	d1,d3
	add.w	fonty+2(pc),d3
	subq.w	#1,d3
	move.w	foundcolstart(pc),d0
	mulu.w	fontx+2(pc),d0
	move.w	foundcolend(pc),d2
	mulu.w	fontx+2(pc),d2
	move.l	a2,a1
	call	RectFill
.quit
	move.l	a2,a1
	moveq	#1,d0
	call	SetAPen
	move.l	a2,a1
	moveq	#0,d0
	call	SetBPen
	move.l	a2,a1
	moveq	#RP_JAM2,d0
	call	SetDrMd
	moveq	#0,d0
	rts

searchnotfound:
	base	intui
	move.l	screenptr(pc),a0
	call	DisplayBeep
	bra.w	textredraw
searchnext:
	tst.l	filelen
	beq.s	.exit
	tst.b	searchstring
	beq.s	searchnotfound
	Message	searching
	tst.w	search_wild
	beq.s	.nowild
	bsr	searchwildnext
	bra.s	.exit
.nowild
	bsr	searchnowildnext
.exit
	Message	screentitle
	moveq	#0,d0
	rts
searchnowildnext:
	move.l	linepos(pc),d0
	addq.l	#1,d0
	bsr.w	gotoline
	tst.w	search_case
	beq.W	.case
	move.l	fileaddr(pc),d0
	add.l	filelen(pc),d0
	sub.l	a0,d0
	beq.w	searchnotfound
	lea	searchstring,a1
	move.b	(a1),d1
.fastsearch
	cmp.b	(a0)+,d1
	beq.b	.firstfound
.fastnextchar
	subq.l	#1,d0				;dbf bara word!!
	bne.b	.fastsearch
	bra.W	searchnotfound
.firstfound
	move.l	a0,a2
	subq.l	#1,a2
	move.l	a1,a3
	move.w	searchlen(pc),d2
	subq.w	#1,d2
	bmi.b	.found
.allsearch
	cmpm.b	(a2)+,(a3)+
	bne.b	.fastnextchar
	dbf	d2,.allsearch
.found
	subq.l	#1,a0
	bra.w	searchfound
.case
	move.l	a0,a3
	lea	searchstring,a2
	move.b	(a2),d0
	base	util
	call	ToUpper
	move.l	d0,d2
	call	ToLower
	move.l	d0,d1
	move.l	a3,a0
	move.l	fileaddr(pc),d0
	add.l	filelen(pc),d0
	sub.l	a0,d0
	beq.w	searchnotfound
	move.l	a2,a1
.cfastsearch
	cmp.b	(a0),d1
	beq.b	.cfirstfound
	cmp.b	(a0),d2
	beq.b	.cfirstfound
.cfastnextchar
	addq.l	#1,a0
	subq.l	#1,d0
	bne.b	.cfastsearch
	bra.w	searchnotfound
.cfirstfound
	move.l	a0,a2
	move.l	a1,a3
	move.l	d0,d3
	move.l	d1,d4
	moveq	#0,d0
	move.w	searchlen(pc),d0
	call	Strnicmp
	tst.l	d0
	beq.b	.cfound
	move.l	a2,a0
	move.l	a3,a1
	move.l	d3,d0
	move.l	d4,d1
	bra.b	.cfastnextchar
.cfound
	move.l	a2,a0
	add.w	searchlen(pc),a2
	bra.w	searchfound

;in:
* a0	start of text
* a1	end of text
searchmarkfound:
	pushm	d0-d1/a0-a3
	move.l	a0,a2
	move.l	a1,a3
	bsr	getline
	subq.l	#1,d0
	move.l	d0,foundlinestart
	bsr	gotoline

	moveq	#0,d1
	sub.l	a0,a2
	move.l	a2,d0
	beq.b	.4
	subq.l	#1,d0
	lea	tabbar,a1
.1
	cmp.b	#TAB,(a0)+
	bne.b	.2
	move.b	(a1,d1.w),d1
	bra.b	.3
.2
	addq.l	#1,d1
.3
	dbf	d0,.1
.4
	move.w	d1,foundcolstart

	move.l	a3,a0
	bsr	getline
	subq.l	#1,d0
	move.l	d0,foundlineend
	bsr	gotoline
	sub.l	a0,a3
	move.l	a3,d0
	bne.b	.notwholeline
	move.l	linelen(pc),a0
	add.l	foundlineend(pc),a0
	move.b	(a0),d0
	move.w	d0,foundcolend
	bra.b	.exit
.notwholeline
	subq.l	#1,d0
	moveq	#0,d1
	lea	tabbar,a1
.11
	cmp.b	#TAB,(a0)+
	bne.b	.12
	move.b	(a1,d1.w),d1
	bra.b	.13
.12
	addq.l	#1,d1
.13
	dbf	d0,.11
	move.w	d1,foundcolend
.exit
	popm	d0-d1/a0-a3
	rts

foundlinestart:	dc.l	0
foundlineend:	dc.l	0
foundcolstart:	dc.w	0
foundcolend:	dc.w 	0

searchprev:
	tst.l	filelen
	beq.s	.exit
	tst.b	searchstring
	beq.w	searchnotfound
	Message	searching
	tst.w	search_wild
	beq.s	.nowild
	bsr	searchwildprev
	bra.s	.exit
.nowild
	bsr	searchnowildprev
.exit
	Message	screentitle
	moveq	#0,d0
	rts

searchnowildprev:
	move.l	linepos(pc),d0
	bsr.w	gotoline
	tst.w	search_case
	beq.B	.case
	move.l	fileaddr(pc),d0
	sub.l	a0,d0
	neg.l	d0
	beq.w	searchnotfound
	lea	searchstring,a1
	move.b	(a1),d1
.fastsearch
	cmp.b	-(a0),d1
	beq.b	.firstfound
.fastnextchar
	subq.l	#1,d0
	bne.b	.fastsearch
	dbf	d0,.fastsearch
	bra.w	searchnotfound
.firstfound
	move.l	a0,a2
	move.l	a1,a3
	move.w	searchlen(pc),d2
	subq.w	#1,d2
	bmi.b	.found
.allsearch
	cmpm.b	(a2)+,(a3)+
	bne.b	.fastnextchar
	dbf	d2,.allsearch
.found
	bra.w	searchfound
.case
	move.l	a0,a3
	lea	searchstring,a2
	move.b	(a2),d0
	base	util
	call	ToUpper
	move.l	d0,d2
	call	ToLower
	move.l	d0,d1
	move.l	a3,a0
	move.l	fileaddr(pc),d0
	sub.l	a0,d0
	neg.l	d0
	beq.w	searchnotfound
	move.l	a2,a1
	subq.l	#1,a0
.cfastsearch
	cmp.b	(a0),d1
	beq.b	.cfirstfound
	cmp.b	(a0),d2
	beq.b	.cfirstfound
.cfastnextchar
	subq.l	#1,a0
	subq.l	#1,d0
	bne.b	.cfastsearch
	bra.w	searchnotfound
.cfirstfound
	move.l	a0,a2
	move.l	a1,a3
	move.l	d0,d3
	move.l	d1,d4
	moveq	#0,d0
	move.w	searchlen(pc),d0
	call	Strnicmp
	tst.l	d0
	beq.b	.cfound
	move.l	a2,a0
	move.l	a3,a1
	move.l	d3,d0
	move.l	d4,d1
	bra.b	.cfastnextchar
.cfound
	move.l	a2,a0
	add.w	searchlen(pc),a2
	bra.w	searchfound

searchwildnext:
	move.l	linepos(pc),d0
	addq.l	#1,d0
	bsr	gotoline

	move.l	fileaddr(pc),d0
	add.l	filelen(pc),d0
	sub.l	a0,d0
	beq.w	searchnotfound

	lea	searchstring,a0
	lea	temp,a1
	move.b	#"#",(a1)+
	move.b	#"?",(a1)+
.copystr
	move.b	(a0)+,(a1)+
	bne.s	.copystr
	move.b	#"#",-1(a1)
	move.b	#"?",(a1)+
	clr.b	(a1)

	tst.w	search_case
	bne.s	.casesens1
	lea	temp,a2				;V37 bugfix
	base	util
.toupper
	move.b	(a2),d0
	beq.s	.upperready
	call	ToUpper
	move.b	d0,(a2)+
	bra.s	.toupper
.upperready
.casesens1
	base	dos
	move.l	#temp,d1
	move.l	#temp+84,d2
	move.l	#84*2+2,d3
	tst.w	search_case
	bne.s	.casesens2
	call	ParsePatternNoCase
	bra.s	.parsed
.casesens2
	call	ParsePattern
.parsed
;	tst.l	d0
;	beq	searchnowildnext		;fanns inget wildcard!
	cmp.l	#-1,d0
	beq	searchnotfound			;fel

	move.l	linepos(pc),d7
	moveq	#0,d6
.loop
	addq.l	#1,d7
	cmp.l	lines(pc),d7
	bhs	searchnotfound

	move.l	d7,d0
	bsr	gotoline
	move.l	a0,a2


	move.l	linelen(pc),a1
	add.l	d7,a1
	moveq	#0,d0
	move.b	(a1),d0
	move.l	d0,d6
	beq	.loop
	subq.w	#1,d0
	lea	temp+256,a1
.copysearchstring
	move.b	(a0)+,(a1)+
	dbf	d0,.copysearchstring
	clr.b	(a1)
	move.l	#temp+84,d1
	move.l	#temp+256,d2
	tst.w	search_case
	bne.s	.casesens3
	call	MatchPatternNoCase
	bra.s	.tested
.casesens3
	call	MatchPattern
.tested
	tst.l	d0
	beq.s	.loop

	move.l	a2,a0
	bra.w	searchfound

searchwildprev:
	move.l	linepos(pc),d0
	bsr.w	gotoline
	move.l	fileaddr(pc),d0
	sub.l	a0,d0
	neg.l	d0
	beq.w	searchnotfound

	lea	searchstring,a0
	lea	temp,a1
	move.b	#"#",(a1)+
	move.b	#"?",(a1)+
.copystr
	move.b	(a0)+,(a1)+
	bne.s	.copystr
	move.b	#"#",-1(a1)
	move.b	#"?",(a1)+
	clr.b	(a1)

	tst.w	search_case
	bne.s	.casesens1
	lea	temp,a2				;V37 bugfix
	base	util
.toupper
	move.b	(a2),d0
	beq.s	.upperready
	call	ToUpper
	move.b	d0,(a2)+
	bra.s	.toupper
.upperready
.casesens1
	base	dos
	move.l	#temp,d1
	move.l	#temp+84,d2
	move.l	#84*2+2,d3
	tst.w	search_case
	bne.s	.casesens2
	call	ParsePatternNoCase
	bra.s	.parsed
.casesens2
	call	ParsePattern
.parsed
;	tst.l	d0
;	beq	searchnowildnext		;fanns inget wildcard!
	cmp.l	#-1,d0
	beq	searchnotfound			;fel

	move.l	linepos(pc),d7
	moveq	#0,d6
.loop
	subq.l	#1,d7
	bmi	searchnotfound

	move.l	d7,d0
	bsr	gotoline
	move.l	a0,a2

	move.l	linelen(pc),a1
	add.l	d7,a1
	moveq	#0,d0
	move.b	(a1),d0
	move.l	d0,d6
	beq	.loop
	subq.w	#1,d0
	lea	temp+256,a1
.copysearchstring
	move.b	(a0)+,(a1)+
	dbf	d0,.copysearchstring
	clr.b	(a1)
	move.l	#temp+84,d1
	move.l	#temp+256,d2
	tst.w	search_case
	bne.s	.casesens3
	call	MatchPatternNoCase
	bra.s	.tested
.casesens3
	call	MatchPattern
.tested
	tst.l	d0
	beq.s	.loop

	move.l	a2,a0
	bra.w	searchfound

**** screen/environment functions

iconify:
	base	icon
	lea	.defappiconname(pc),a0
	call	GetDiskObject
	lea	.diskobject(pc),a0
	move.l	d0,(a0)

	base	exec
	call	CreateMsgPort
	move.l	d0,d7
	beq.W	.nomsgport
	base	wb
	move.l	.diskobject(pc),d0
	move.l	d0,a3
	bne.b	.usedefappicon
	lea	AppIconDO,a3	;diskobj
.usedefappicon
	moveq	#0,d0		;id
	moveq	#0,d1		;userdata
	lea	programname(pc),a0
	move.l	d7,a1		;msgport
	suba.l	a2,a2		;lock
	suba.l	a4,a4
	call	AddAppIconA
	move.l	d0,d6
	beq.W	.noicon

	bsr.w	closeenvironment

.waitloop
	base	exec

	move.l	comport,a0
	move.b	MP_SIGBIT(a0),d1
	moveq	#0,d0
	bset	d1,d0
	lea	.comsignal(pc),a0
	move.l	d0,(a0)

	move.l	d7,a0
	move.b	MP_SIGBIT(a0),d1
	moveq	#0,d0
	bset	d1,d0
	lea	.appsignal(pc),a0
	move.l	d0,(a0)

	move.l	.comsignal(pc),d0
	or.l	.appsignal(pc),d0

	call	Wait

	move.l	d0,d1
	and.l	.appsignal,d1
	bne.b	.nextmsg

	move.l	d0,d1
	and.l	.comsignal,d1
	beq.b	.waitloop
	bsr	.doubleclicked
	bsr	handleCOM
	moveq	#1,d0
	rts
.nextmsg
	base	exec
	move.l	d7,a0
	call	GetMsg
	tst.l	d0
	beq.B	.waitloop
	move.l	d0,a1
	move.l	am_NumArgs(a1),d2
	call	ReplyMsg
	tst.l	d2
	beq.B	.doubleclicked
	base	intui
	suba.l	a0,a0
	call	DisplayBeep
	bra.B	.nextmsg
.doubleclicked
	base	wb
	move.l	d6,a0
	call	RemoveAppIcon
	bsr.b	.cleanup
	bsr.w	changevideomode
	tst.l	d0
	bne.b	.exit
	moveq	#1,d0			;inga fler meddelanden!
.exit
	rts
.noicon
	lea	noworkbench(pc),a1
	lea	oktext(pc),a2
	suba.l	a4,a4
	bsr.w	requester
	bsr.b	.cleanup
	moveq	#1,d0			;inga fler meddelanden!
	rts
.cleanup
	base	exec
.getallmsg
	move.l	d7,a0
	call	GetMsg
	tst.l	d0
	beq.B	.nomoremsg
	move.l	d0,a1
	call	ReplyMsg
	bra.B	.getallmsg
.nomoremsg
	move.l	d7,a0
	call	DeleteMsgPort
.nomsgport
	base	icon
	move.l	.diskobject(pc),d0
	beq.b	.nodefappicon
	move.l	d0,a0
	call	FreeDiskObject
.nodefappicon
	rts
.comsignal	dc.l	0
.appsignal	dc.l	0
.diskobject	dc.l	0
.defappiconname
	dc.b	"ENV:Textread/def_app",0
	even
;in:
* d1	gammalt sysfont
* d2	gammalt screenmode
;ut:
* d0	NULL eller felkod
changeenv:
	pushm	d1-a6
	cmp.w	sysfont(pc),d1
	bne.b	.changescreen
	cmp.l	displayid(pc),d2
	beq.b	.dontchangescreen
.changescreen
	bsr.w	changevideomode
	tst.l	d0
	bne.b	.error
;	lea	tabbar,a0
;	bsr.w	fixatabbar
;	bra.b	.exit
.dontchangescreen
	lea	tabbar,a0
	bsr.w	fixatabbar

	lea	thisfont(pc),a0
	bsr.w	changefont
	tst.l	d0
	beq.b	.fontok
	lea	topaz8font(pc),a0
	bsr.w	changefont
	tst.l	d0
	bne.b	.error
.fontok
	moveq	#FALSE,d0
	bsr.w	fixafonten
	bsr	recountlines
	bsr	updatemenu
	bsr.w	updatecheckmarks

	moveq	#0,d0
	bsr	gotoline
	bsr.w	textredraw
.exit
	moveq	#0,d0
.error
	popm	d1-a6
	rts

changevideomode:
	movem.l	d1-a6,-(sp)
	lea	scrnsysfont+2(pc),a0
	move.w	sysfont(pc),(a0)
	bsr.w	closeenvironment
	bsr.w	openenvironment
	tst.l	d0
	beq.b	.ok
	lea	displayid(pc),a0
	move.l	#DEFAULT_MONITOR_ID!HIRES_KEY,(a0);ok.. std hires, sysfont=0
	lea	scrnsysfont(pc),a0
	clr.l	(a0)
	lea	sysfont(pc),a0
	clr.w	(a0)
	bsr.w	closeenvironment
	bsr.w	openenvironment
	tst.l	d0
	bne.b	.failure		;skit i det då!
.ok
	moveq	#TRUE,d0
	bsr	fixafonten
	bsr	updatemenu
	bsr.w	updatecheckmarks
	base	intui
	move.l	screenptr(pc),a0
	call	ScreenToFront
	bsr	recountlines
	bsr.w	textredraw		;Uppdatera skärm och gadgetar
	moveq	#0,d0
.failure
	movem.l	(sp)+,d1-a6
	rts

savefont:	;a0=8 bytes buffer,a1=31 bytes namnbuffer
	move.l	windowptr(pc),a3
	move.l	wd_RPort(a3),a3
	move.l	rp_Font(a3),a3
	move.l	LN_NAME(a3),a2
	move.l	a1,(a0)+
	move.w	#30,d0
.copyname
	move.b	(a2)+,(a1)+
	dbeq	d0,.copyname
	move.l	tf_YSize(a3),(a0)+	;YSize, Style & Flags
	rts
changefont:	;byter font till TextAttr i a0
	move.l	a0,a2
	base	gfx
	move.l	myfont(pc),d0
	beq.b	.nofont
	move.l	d0,a1
	call	CloseFont
	lea	myfont(pc),a1
	clr.l	(a1)
.nofont
	tst.l	diskfontbase
	bne.b	.diskfontok
	base	exec
	lea	diskfontname(pc),a1
	moveq	#37,d0
	call	OpenLibrary		;öppna diskfont
	lea	diskfontbase(pc),a0	;Det är ju inte nödvändigt med
	move.l	d0,(a0)			;diskfont.library... alltså:
	bne.B	.diskfontok
	base	gfx
	move.l	a2,a0
	call	OpenFont
	bra.b	.fontopened
.diskfontok
	base	diskfont
	move.l	a2,a0
	call	OpenDiskFont
.fontopened
	lea	myfont(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	base	gfx
	move.l	windowptr(pc),a1
	move.l	wd_RPort(a1),a1
	move.l	d0,a0
	call	SetFont
	lea	thisfont(pc),a0
	lea	thisfontname(pc),a1
	move.l	a1,(a0)+
	move.l	4(a2),(a0)+
	move.w	#30,d0
	move.l	(a2),a0
.copyname
	move.b	(a0)+,(a1)+
	dbeq	d0,.copyname

	move.l	myfont(pc),a0
	moveq	#0,d0
	move.w	tf_XSize(a0),d0
	lea	fontx(pc),a1
	move.l	(a1),d1					;-> d1
	move.l	d0,(a1)
	move.w	tf_YSize(a0),d0
	lea	fonty(pc),a1
	move.l	d0,(a1)

	move.l	pwidth(pc),d0
	divu.w	fontx+2(pc),d0
	and.l	#$ffff,d0
	lea	cwidth(pc),a0
	move.l	d0,(a0)
	mulu.w	fontx+2(pc),d0
	lea	cwidthp(pc),a0
	move.l	d0,(a0)

	move.l	pheight(pc),d0
	sub.l	barheight(pc),d0
	sub.l	fonty(pc),d0
	subq.l	#4,d0					;plats för bevelbox
	divu.w	fonty+2(pc),d0
	and.l	#$ffff,d0
	lea	cheight(pc),a0
	move.l	d0,(a0)
	mulu.w	fonty+2(pc),d0
	lea	cheightp(pc),a0
	move.l	d0,(a0)

;	cmp.l	fontx(pc),d1				;<- d1
;	beq.b	.samex
;	bsr	countlines				;width was changed!
;.samex
	base	exec
	move.l	radpos(pc),d0
	beq.b	.noradpos
	move.l	d0,a1
	call	FreeVec
.noradpos
	move.l	cheight(pc),d0
	lsl.l	#2,d0
	moveq	#MEMF_ANY,d1
	call	AllocVec
	lea	radpos(pc),a0
	move.l	d0,(a0)
	beq.b	.error
	move.l	d0,a0
	move.l	plane0(pc),d1
	move.l	bwidth(pc),d2
	mulu.w	fonty+2(pc),d2
	move.l	cheight(pc),d0
	subq.w	#1,d0
.loop
	move.l	d1,(a0)+
	add.l	d2,d1
	dbf	d0,.loop

	moveq	#0,d0
	rts
.error
	moveq	#err_nofont,d0
	rts

;in:
* a0	tabbar (256 bytes buffert)
fixatabbar:
	move.l	tabsize(pc),d1
	move.w	d1,d4
	subq	#1,d1
	move.w	#255,d2		;max radlängd?
	moveq	#0,d3
.loop1
	move.w	d1,d0
	add.b	d4,d3
	bne.B	.loop2
	moveq	#-1,d3		;om 256, ta 255 istället...
.loop2
	move.b	d3,(a0)+
	dbf	d0,.loop2
	sub.w	d4,d2
	cmp.w	d1,d2
	bpl.B	.loop1
	rts

;in:
* d0	TRUE: force update ; FALSE: Only update if different font
fixafonten:		;fixar font-data. Om inte fonten är 8*8 öppnas
			;topaz.font 8 ROM-fonten.
	tst.l	d0
	beq.b	.dontforceupdate
	lea	oldfontsize(pc),a0
	clr.w	(a0)
.dontforceupdate
	tst.w	legalprint
	beq.w	.fast

	lea	.minmax(pc),a0
	bsr	getfontlimits

	move.l	windowptr(pc),a2
	move.l	wd_RPort(a2),a2

	move.w	rp_TxHeight(a2),d1
	move.w	rp_TxWidth(a2),d1

	move.w	.minmax+2(pc),d0
	cmp.w	rp_TxHeight(a2),d0
	bhi.b	.change			;<min?
	move.w	.minmax+6(pc),d0
	cmp.w	rp_TxHeight(a2),d0
	blo.b	.change			;>max?

	move.w	.minmax(pc),d0
	cmp.w	rp_TxWidth(a2),d0
	bhi.b	.change			;<min?
	move.w	.minmax+4(pc),d0
	cmp.w	rp_TxWidth(a2),d0
	blo.b	.change			;>max?
	bra.w	.exit
.change
	lea	topaz8font(pc),a0
	moveq	#0,d0
	bsr.w	changefont
	tst.l	d0
	bne.w	.error
	bra.w	.exit
.minmax
	dc.w	8,8,8,8
.fast
.again
	lea	temp,a5
	move.w	#255,d0
.fixfonttable
	move.b	d0,(a5,d0.w)
	dbf	d0,.fixfonttable
	
	base	gfx
	move.l	windowptr(pc),a2
	move.l	wd_RPort(a2),a2
	move.l	plane0(pc),a3
	lea	font,a4

	move.l	barheight(pc),d4
	add.w	rp_TxBaseline(a2),d4

	cmp.w	#8,rp_TxHeight(a2)
	bne.w	.no88
	cmp.w	#8,rp_TxWidth(a2)
	bne.w	.no88

	bsr	issamefont		;if same font, no need to rebuild it
	tst.l	d0
	bne	.exit

	move.l	a2,a1			;initera rätta färger!
	moveq	#1,d0
	call	SetAPen
	move.l	a2,a1
	moveq	#0,d0
	call	SetBPen

	move.l	a2,a1
	moveq	#0,d0
	move.l	d4,d1
	call	Move

	lea	(a5),a0
	tst.w	nofontfake
	bne.b	.notfaked00
	move.l	a2,a1
	moveq	#0,d0
	call	SetAPen
	move.l	a2,a1
	moveq	#1,d0
	call	SetBPen
	lea	$40(a5),a0		;inv 40 = 00
.notfaked00
	move.l	a2,a1
	moveq	#32,d0
	call	Text
	move.l	a2,a1
	moveq	#1,d0
	call	SetAPen
	move.l	a2,a1
	moveq	#0,d0
	call	SetBPen
	move.l	a2,a1
	lea	$20(a5),a0
	moveq	#32,d0
	call	Text
	bsr.w	.copytext
	move.l	a2,a1
	moveq	#0,d0
	move.l	d4,d1
	call	Move
	move.l	a2,a1
	lea	$40(a5),a0
	moveq	#64,d0
	call	Text
	bsr.W	.copytext

	move.l	a2,a1
	moveq	#0,d0
	move.l	d4,d1
	call	Move

	lea	$80(a5),a0
	tst.w	nofontfake
	bne.b	.notfaked80
	move.l	a2,a1
	moveq	#0,d0
	call	SetAPen
	move.l	a2,a1
	moveq	#1,d0
	call	SetBPen
	lea	$c0(a5),a0		;inv c0 = 80
.notfaked80
	move.l	a2,a1
	moveq	#32,d0
	call	Text
	move.l	a2,a1
	moveq	#1,d0
	call	SetAPen
	move.l	a2,a1
	moveq	#0,d0
	call	SetBPen
	move.l	a2,a1
	lea	$a0(a5),a0
	moveq	#32,d0
	call	Text
	bsr.B	.copytext

	move.l	a2,a1
	moveq	#0,d0
	move.l	d4,d1
	call	Move
	move.l	a2,a1
	moveq	#0,d0
	move.l	d4,d1
	call	Move
	move.l	a2,a1
	lea	$c0(a5),a0
	moveq	#64,d0
	call	Text
	bsr.B	.copytext
.exit
	bsr	issamefont
	tst.l	d0
	bne.b	.1

	bsr.w	freegadgets
	bsr.w	creategadgets
	tst.l	d0
	bne.b	.error
.1
	moveq	#0,d0
.error
	lea	oldfontsize(pc),a1
	move.w	thisfontsize(pc),(a1)
	lea	thisfontname(pc),a0
	lea	oldfontname,a1
.copyfontname
	move.b	(a0)+,(a1)+
	bne.b	.copyfontname
	rts
.no88
	lea	topaz8font(pc),a0
	moveq	#0,d0
	bsr.w	changefont
	tst.l	d0
	bne.w	.error
	bra.w	.again

.copytext
	moveq	#63,d0
	moveq	#0,d2
	move.l	bwidth(pc),d3
.nextchar
	move.l	a3,a0
	add.l	d2,a0
	addq.l	#1,d2
	moveq	#7,d1
.copychar
	move.b	(a0),(a4)+
	clr.b	(a0)
	add.l	d3,a0
	dbf	d1,.copychar
	dbf	d0,.nextchar
	rts

;ut:
* d0	TRUE if the same font, else FALSE.
issamefont:
	move.w	oldfontsize(pc),d0
	beq.b	.change
	cmp.w	thisfontsize(pc),d0
	bne.w	.change

	lea	thisfontname(pc),a0
	lea	oldfontname,a1
.nextchar
	move.b	(a0)+,d0
	beq.b	.exit
	cmp.b	(a1)+,d0
	beq.b	.nextchar
.change
	moveq	#FALSE,d0
	dc.w	$0c40
.exit
	moveq	#TRUE,d0
	rts

topaz8font:
	dc.l	.name
	dc.w	8
	dc.b	0
	dc.b	FPF_ROMFONT!FPF_DESIGNED
.name
	dc.b	"topaz.font",0
	even

openlibs:
	lea	_SysBase(pc),a0
	move.l	4.w,(a0)

	base	exec
	lea	intuiname(pc),a1
	move.l	a1,a2
	moveq	#37,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna intuition
	lea	intuibase(pc),a0
	move.l	d0,(a0)
	beq.w	.error

	lea	gfxname(pc),a1
	move.l	a1,a2
	moveq	#37,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna gfx
	lea	gfxbase(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	lea	dosname(pc),a1
	move.l	a1,a2
	moveq	#37,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna dos
	lea	dosbase(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	lea	gadtoolsname(pc),a1
	move.l	a1,a2
	moveq	#37,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna gadtools
	lea	gtbase(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	lea	aslname(pc),a1
	move.l	a1,a2
	moveq	#37,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna asl
	lea	aslbase(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	lea	wbname(pc),a1
	move.l	a1,a2
	moveq	#37,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna workbench
	lea	wbbase(pc),a0
	move.l	d0,(a0)
	beq.B	.error
	lea	iconname(pc),a1
	move.l	a1,a2
	moveq	#37,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna icon
	lea	iconbase(pc),a0
	move.l	d0,(a0)
	beq.B	.error
	lea	utilname(pc),a1
	move.l	a1,a2
	moveq	#37,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna utility
	lea	utilbase(pc),a0
	move.l	d0,(a0)
	beq.B	.error

	lea	xpkname(pc),a1
	move.l	a1,a2
	moveq	#0,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna xpk
	lea	xpkbase(pc),a0
	move.l	d0,(a0)
	bne.b	.filelibsopened
               
	lea	ppname(pc),a1
	move.l	a1,a2
	moveq	#35,d0
	move.l	d0,d2
	call	OpenLibrary		;öppna powerpacker, om inte xpk
	lea	ppbase(pc),a0
	move.l	d0,(a0)
;	beq.B	.error			;tjahapps. inget funkade. So what?
.filelibsopened

	moveq	#0,d0
	rts

* a2	library som inte kunde öppnas
* d2	version
.error
	lea	.args(pc),a0
	move.l	a2,(a0)+
	move.l	d2,(a0)
	base	intui
	move.l	a6,d0
	beq.b	.errnointui
	suba.l	a0,a0
	lea	.strukt(pc),a1
	suba.l	a2,a2
	lea	.args(pc),a3
	call	EasyRequestArgs
	bra.b	.exit
.errnointui
	ALERT	AN_Unknown!AG_OpenLib!AO_Intuition
.exit
	moveq	#err_exitnow,d0
	rts
.strukt
	dc.l	.struktend-*
	dc.l	0
	dc.l	0
	dc.l	errnolibtext
	dc.l	oktext
.struktend
.args	dc.l	0,0

openenvironment:
	movem.l	d1-a6,-(sp)
	base	intui
	lea	screenerrorcode(pc),a0
	clr.l	(a0)
	moveq	#err_noscreen,d7
	suba.l	a0,a0
	lea	screentags(pc),a1
	call	OpenScreenTagList
	lea	screenptr(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	screenerrorcode(pc),d1
	bne.w	.error

	lea	defpalette,a1
	bsr	getpalette

	lea	palette(pc),a1
	bsr	changepalette

	base	intui
	move.l	screenptr(pc),a0

	moveq	#0,d0
	move.w	sc_Width(a0),d0
	lea	windowwidth(pc),a1
	move.l	d0,(a1)
	lea	pwidth(pc),a1
	move.l	d0,(a1)			;pwidth

	moveq	#0,d0
	move.b	sc_BarHeight(a0),d0
	addq.l	#1,d0
	lea	barheight(pc),a1
	move.l	d0,(a1)			;barheight

	lea	sc_RastPort(a0),a1
	move.l	rp_BitMap(a1),a1
	moveq	#0,d0
	move.w	bm_BytesPerRow(a1),d0
	lea	bwidth(pc),a2
	move.l	d0,(a2)			;bwidth

	mulu.w	barheight+2(pc),d0
	add.l	bm_Planes(a1),d0
	lea	plane0(pc),a1
	move.l	d0,(a1)

	moveq	#0,d0
	move.w	sc_Height(a0),d0
	lea	windowheight(pc),a1
	move.l	d0,(a1)
	lea	pheight(pc),a1
	move.l	d0,(a1)			;pheight

	lea	pwidth(pc),a0		;för bred skärm?
	cmp.l	#254*8,(a0)
	bhi.w	.error
	lea	pheight(pc),a0		;för hög skärm?
	cmp.l	#254*8,(a0)
	bhi.w	.error

	move.l	screenptr(pc),a0
	lea	sc_RastPort(a0),a1
	move.l	rp_BitMap(a1),a1
	base	gfx
	cmp.w	#39,LIB_VERSION(a6)
	blo.s	.noforeignbitmap

	lea	foreignbitmap(pc),a2
	clr.w	(a2)
	move.l	screenptr(pc),a0
	lea	sc_RastPort(a0),a0
	move.l	rp_BitMap(a0),a0
	moveq	#BMA_FLAGS,d1
	call	GetBitMapAttr
	and.l	#BMF_STANDARD,d0
	bne.b	.noforeignbitmap
	addq.w	#1,(a2)
	lea	legalprint(pc),a0
	move.w	#TRUE,(a0)		;system rendering ON
	lea	nofontfake(pc),a0
	move.w	#TRUE,(a0)		;always on when legalprint=TRUE
.noforeignbitmap

	base	intui
	lea	windowonscreen(pc),a0
	move.l	screenptr(pc),(a0)

	moveq	#err_nowin,d7
	suba.l	a0,a0
	lea	windowtags(pc),a1
	call	OpenWindowTagList	;öppna fönster 1
	lea	windowptr(pc),a0
	move.l	d0,(a0)
	beq.w	.error

	lea	deffont,a0
	lea	deffontname,a1
	bsr.w	savefont

	lea	thisfont(pc),a0
	bsr.w	changefont
	tst.l	d0
	beq.b	.fontok
	lea	topaz8font(pc),a0
	bsr.w	changefont
	move.l	d0,d7
	bne.w	.error
.fontok

	base	exec
	suba.l	a1,a1
	call	FindTask
	lea	thistask(pc),a0
	move.l	d0,(a0)
	move.l	d0,a0
	lea	defreqscreen(pc),a1
	move.l	pr_WindowPtr(a0),(a1)
	move.l	windowptr(pc),pr_WindowPtr(a0)	;requestrar på skärmen!

	base	gt
	move.l	screenptr(pc),a0
	lea	tag_done(pc),a1
	call	GetVisualInfoA		;skaffa visuell information
	lea	screenvi(pc),a0
	move.l	d0,(a0)

	lea	winmenuerrorcode(pc),a0
	clr.l	(a0)

	moveq	#err_nomenu,d7
	base	gt
	lea	winmenu(pc),a0
	lea	winmenutags(pc),a1
	call	CreateMenusA
	lea	winmenuptr(pc),a0	;skapa menyer
	move.l	d0,(a0)
	beq.W	.error
	move.l	winmenuerrorcode(pc),d1
	bne.W	.error

;	move.l	winmenuprt(pc),a0	;skapa menyer
	move.l	d0,a0			;skapa menyer
	move.l	screenvi(pc),a1
	lea	.menutags(pc),a2
	call	LayoutMenusA
	tst.l	d0
	beq.W	.error

	base	intui
	move.l	windowptr(pc),a0
	move.l	winmenuptr(pc),a1
	call	SetMenuStrip

	moveq	#err_nogadget,d7
	bsr.w	creategadgets
	tst.l	d0
	bne.w	.error

	moveq	#err_nofilereq,d7
	lea	.filereqwindow(pc),a0
	move.l	windowptr(pc),(a0)
	lea	filereqhgt(pc),a0
	move.l	pheight(pc),d0
	mulu	#7,d0
	lsr.l	#3,d0
	move.l	d0,(a0)
	base	asl
	moveq	#ASL_FileRequest,d0
	lea	.filereqtags(pc),a0
	call	AllocAslRequest	
	lea	filereqptr(pc),a0
	move.l	d0,(a0)
	beq.w	.error

	lea	setname,a0
	tst.b	(a0)
	bne.b	.nosetname
	lea	defsetname(pc),a0
.nosetname
	move.l	a0,a2
	lea	temp,a1
	move.w	#tempsize-1,d0
.copysettingsname
	move.b	(a0)+,(a1)+
	dbeq	d0,.copysettingsname

	base	dos
	move.l	#temp,d1
	call	PathPart
	move.l	d0,a0
	clr.b	(a0)
	lea	.setfreqpathpart(pc),a0
	move.l	#temp,(a0)

	move.l	a2,d1				;setname/defsetname!
	call	FilePart
	lea	.setfreqfilepart(pc),a0
	move.l	d0,(a0)
	move.l	d0,a0

	moveq	#err_nofilereq,d7
	lea	.setfreqwindow(pc),a0
	move.l	windowptr(pc),(a0)
	lea	setfreqhgt(pc),a0
	move.l	pheight(pc),d0
	mulu	#7,d0
	lsr.l	#3,d0
	move.l	d0,(a0)
	base	asl
	moveq	#ASL_FileRequest,d0
	lea	.setfreqtags(pc),a0
	call	AllocAslRequest	
	lea	setfreqptr(pc),a0
	move.l	d0,(a0)
	beq.s	.error

;	jsr     SetupScreen		;GTBX
	moveq	#0,d7
.error
	move.l	d7,d0
	movem.l	(sp)+,d1-a6
	rts
.menutags
	dc.l	GTMN_NewLookMenus,TRUE
	dc.l	TAG_DONE

.setfreqtags
	dc.l	ASLFR_Window
.setfreqwindow
	dc.l	0
	dc.l	ASLFR_DoPatterns,TRUE
	dc.l	ASLFR_SleepWindow,TRUE
	dc.l	ASLFR_InitialFile
.setfreqfilepart
	dc.l	0
	dc.l	ASLFR_InitialDrawer
.setfreqpathpart
	dc.l	0
	dc.l	ASLFR_InitialPattern,.setfreqpat
	dc.l	TAG_DONE
.setfreqpat
	dc.b	"#?.prefs",0
	even
.filereqtags:
	dc.l	ASLFR_Window
.filereqwindow:
	dc.l	0
	dc.l	ASLFR_DoPatterns,TRUE
	dc.l	ASLFR_SleepWindow,TRUE
	dc.l	TAG_DONE

setfreqtags:
	dc.l	ASLFR_InitialHeight
setfreqhgt:
	dc.l	100
	dc.l	ASLFR_DoSaveMode
setfreqsave:
	dc.l	FALSE
	dc.l	ASLFR_TitleText
setfreqtitle:
	dc.l	0
	dc.l	TAG_DONE
	even

filereqtags:
	dc.l	ASLFR_InitialFile,filename
	dc.l	ASLFR_InitialDrawer,filereqdir
	dc.l	ASLFR_InitialPattern,filereqpat
	dc.l	ASLFR_InitialHeight
filereqhgt:
	dc.l	100
	dc.l	ASLFR_DoSaveMode
filereqsave:
	dc.l	FALSE
	dc.l	ASLFR_TitleText
filereqtitle:
	dc.l	0
	dc.l	TAG_DONE

closeenvironment:
	movem.l	d1-a6,-(sp)
;	jsr	CloseDownScreen		;GTBX
	base	asl
	move.l	setfreqptr(pc),a0
	clr.l	setfreqptr
	call	FreeAslRequest
.nosetfreq
	move.l	filereqptr(pc),a0
	clr.l	filereqptr
	call	FreeAslRequest
.nofilereq
	base	intui
	move.l	windowptr(pc),d0
	move.l	d0,a0
	beq.B	.nowindow_menu
	call	ClearMenuStrip
.nowindow_menu
	move.l	thistask(pc),d0
	beq.b	.nodefreq
	move.l	d0,a0
	move.l	defreqscreen(pc),pr_WindowPtr(a0)
.nodefreq
	base	exec
	call	Forbid
	base	gt
.nextmsg
	move.l	windowptr(pc),d0
	move.l	d0,a0
	beq.B	.nowindow

	move.l	wd_UserPort(a0),a0
	call	GT_GetIMsg
	tst.l	d0
	beq.b	.nullmsg
	move.l	d0,a1
	call	GT_ReplyIMsg
	bra.b	.nextmsg
.nullmsg
	base	intui
	move.l	windowptr(pc),a0
	call	CloseWindow
	lea	windowptr(pc),a0
	clr.l	(a0)
.nowindow
	base	exec
	call	Permit

	base	gt
	move.l	winmenuptr(pc),d0
	move.l	d0,a0
	beq.B	.nowinmenu
	call	FreeMenus
	lea	winmenuptr(pc),a0
	clr.l	(a0)
.nowinmenu
	bsr.w	freegadgets
	move.l	gtbase(pc),a6
	move.l	screenvi(pc),d0
	move.l	d0,a0
	beq.B	.noscreenvi
	call	FreeVisualInfo
	lea	screenvi(pc),a0
	clr.l	(a0)
.noscreenvi
	move.l	intuibase(pc),a6
	move.l	screenptr(pc),d0
	move.l	d0,a0
	beq.B	.noscreen
	call	CloseScreen
	lea	screenptr(pc),a0
	clr.l	(a0)
.noscreen
	base	gfx
	move.l	myfont(pc),d0
	beq.b	.nofont
	move.l	d0,a1
	call	CloseFont
	lea	myfont(pc),a0
	clr.l	(a0)
.nofont
	base	exec
	move.l	radpos(pc),d0
	beq.b	.noradpos
	move.l	d0,a1
	call	FreeVec
	lea	radpos(pc),a0
	clr.l	(a0)
.noradpos
	movem.l	(sp)+,d1-a6
	moveq	#0,d0
	rts
closelibs:
	base	exec
	move.l	xpkbase(pc),d0
	beq.B	.noxpkbase
	move.l	d0,a1
	call	CloseLibrary
	lea	xpkbase(pc),a0
	clr.l	(a0)
.noxpkbase
	move.l	ppbase(pc),d0
	beq.B	.noppbase
	move.l	d0,a1
	call	CloseLibrary
	lea	ppbase(pc),a0
	clr.l	(a0)
.noppbase
	move.l	utilbase(pc),d0
	beq.B	.noutilbase
	move.l	d0,a1
	call	CloseLibrary
	lea	utilbase(pc),a0
	clr.l	(a0)
.noutilbase
	move.l	iconbase(pc),d0
	beq.B	.noiconbase
	move.l	d0,a1
	call	CloseLibrary
	lea	iconbase(pc),a0
	clr.l	(a0)
.noiconbase
	move.l	diskfontbase(pc),d0
	beq.B	.nodiskbase
	move.l	d0,a1
	call	CloseLibrary
	lea	diskfontbase(pc),a0
	clr.l	(a0)
.nodiskbase
	move.l	wbbase(pc),d0
	beq.B	.nowbbase
	move.l	d0,a1
	call	CloseLibrary
	lea	wbbase(pc),a0
	clr.l	(a0)
.nowbbase
	move.l	aslbase(pc),d0
	beq.B	.noaslbase
	move.l	d0,a1
	call	CloseLibrary
	lea	aslbase(pc),a0
	clr.l	(a0)
.noaslbase
	move.l	gtbase(pc),d0
	beq.B	.nogtbase
	move.l	d0,a1
	call	CloseLibrary
	lea	gtbase(pc),a0
	clr.l	(a0)
.nogtbase
	move.l	intuibase(pc),d0
	beq.B	.nointuibase
	move.l	d0,a1
	call	CloseLibrary
	lea	intuibase(pc),a0
	clr.l	(a0)
.nointuibase
	move.l	dosbase(pc),d0
	beq.B	.nodosbase
	move.l	d0,a1
	call	CloseLibrary
	lea	dosbase(pc),a0
	clr.l	(a0)
.nodosbase
	move.l	gfxbase(pc),d0
	beq.B	.nogfxbase
	move.l	d0,a1
	call	CloseLibrary
	lea	gfxbase(pc),a0
	clr.l	(a0)
.nogfxbase
	moveq	#0,d0
	rts

creategadgets:
	base	gt

	lea	top(pc),a2
	move.l	linepos(pc),(a2)
	lea	total(pc),a2
	move.l	lines(pc),(a2)
	lea	synliga(pc),a2
	move.l	cheight(pc),(a2)

	lea	gadgetptr(pc),a0
	call	CreateContext
	move.l	d0,d7

	move.l	fonty(pc),d2
	addq.l	#3,d2

	move.l	fontx(pc),d3
	lsl.l	#1,d3				;'Q'-knappen

	move.l	fontx(pc),d4
	mulu.w	#25+2,d4
;	addq.l	#4,d4				;Statustext

	lea	newgadget(pc),a1
	move.l	d3,d0
	add.l	d4,d0
	move.w	d0,gng_LeftEdge(a1)
	move.l	pwidth(pc),d1
	sub.w	d0,d1
	ble.w	.error
	move.w	d1,gng_Width(a1)

	move.l	windowheight(pc),d0
	sub.l	d2,d0
	ble.w	.error
	move.w	d0,gng_TopEdge(a1)
	move.w	d2,gng_Height(a1)
	clr.l	gng_GadgetText(a1)
	lea	thisfont(pc),a0
	move.l	a0,gng_TextAttr(a1)
	move.w	#1,gng_GadgetID(a1)
	clr.l	gng_Flags(a1)
	move.l	screenvi(pc),gng_VisualInfo(a1)
	clr.l	gng_UserData(a1)
	move.l	d7,a0
	lea	scrollertags(pc),a2
	move.l	#SCROLLER_KIND,d0
	call	CreateGadgetA
	lea	scrollgadget(pc),a0
	move.l	d0,(a0)
	beq.W	.error

	lea	newgadget(pc),a1
	move.w	#0,gng_LeftEdge(a1)
	move.l	d3,d0
	subq.l	#1,d0
	move.w	d0,gng_Width(a1)
	lea	.q(pc),a0
	move.l	a0,gng_GadgetText(a1)
	move.w	#2,gng_GadgetID(a1)
	move.l	scrollgadget(pc),a0
	lea	tag_done(pc),a2
	move.l	#BUTTON_KIND,d0
	call	CreateGadgetA
	lea	quitgadget(pc),a0
	move.l	d0,(a0)
	beq.B	.error

	lea	.drawtags(pc),a1
	move.l	screenvi(pc),4(a1)
	move.l	d3,d0
	move.l	windowheight(pc),d1
	sub.l	d2,d1
	move.l	d2,d3
	move.l	d4,d2
	subq.l	#1,d2
	ble.b	.error
	move.l	screenptr(pc),a0
	lea	sc_RastPort(a0),a0
	call	DrawBevelBoxA

	base	intui
	move.l	windowptr(pc),a0
	move.l	gadgetptr(pc),a1
	suba.l	a2,a2
	moveq	#0,d0
	moveq	#-1,d1
	call	AddGList
	move.l	windowptr(pc),a1
	move.l	gadgetptr(pc),a0
	suba.l	a2,a2
	moveq	#-1,d0
	call	RefreshGList
	base	gt
	move.l	windowptr(pc),a0
	suba.l	a1,a1
	call	GT_RefreshWindow
	moveq	#0,d0
	rts
.error
	moveq	#-1,d0
	rts
.q
	dc.b	"Q",0
.drawtags
	dc.l	GT_VisualInfo,0
	dc.l	GTBB_Recessed,TRUE
	dc.l	TAG_DONE

changenum:
	moveq	#4,d0
	lea	linetext(pc),a0
	lea	temp,a1
.loop
	move.b	(a0)+,(a1)+
	dbf	d0,.loop
	move.l	linepos(pc),d0
	bsr.w	numdec
.copylinenum
	move.b	(a0)+,(a1)+
	bne.b	.copylinenum
	move.b	#" ",-1(a1)

	move.l	linepos(pc),d0
	mulu.w	#100,d0
	move.l	lines(pc),d1
	sub.l	cheight(pc),d1
	beq.b	.nodiv
	divu.w	d1,d0			;hmm...
.nodiv
	and.l	#$0000ffff,d0
	bsr.w	numdec
	addq.l	#7,a0
	move.b	(a0)+,(a1)+
	move.b	(a0)+,(a1)+
	move.b	(a0)+,(a1)+
	move.b	#"%",(a1)+
	move.b	#" ",(a1)+

	moveq	#"W",d0			;små statusbokstäver
	tst.w	wordwrap
	bne.b	.wwyes
	moveq	#" ",d0
.wwyes
	move.b	d0,(a1)+
	moveq	#"A",d0
	tst.w	alwaysremansi
	bne.b	.rayes
	moveq	#" ",d0
.rayes
	move.b	d0,(a1)+
	moveq	#"C",d0
	tst.w	alwaysremcr
	bne.b	.rcyes
	moveq	#" ",d0
.rcyes
	move.b	d0,(a1)+
	moveq	#"F",d0
	tst.w	nofontfake
	bne.b	.nfyes
	moveq	#" ",d0
.nfyes
	move.b	d0,(a1)+

	move.l	windowheight(pc),d2
	sub.l	barheight(pc),d2
	sub.l	fonty(pc),d2
	subq.l	#1,d2

	move.l	a1,d3
	lea	temp,a0
	sub.l	a0,d3
	subq.l	#1,d3			;längd ok!
	moveq	#3,d1
	bsr.w	printline
	rts

changescroll:
	base	gt
	lea	changetop(pc),a0
	move.l	linepos(pc),(a0)
	lea	changetotal(pc),a0
	move.l	lines(pc),(a0)
	move.l	scrollgadget(pc),a0
	move.l	windowptr(pc),a1
	suba.l	a2,a2
	lea	changescrolltags(pc),a3
	call	GT_SetGadgetAttrsA
	rts
changescrolltags:
	dc.l	GTSC_Top
changetop:
	dc.l	0
	dc.l	GTSC_Total
changetotal:
	dc.l	0
	dc.l	TAG_DONE

freegadgets:
	bsr	unsetwritemask

	lea.l	gadgetptr(pc),a2


	base	gfx
	move.l	windowptr(pc),d0
	beq.b	.nowin
	move.l	d0,a1
	move.l	wd_RPort(a1),a1
	moveq	#0,d0
	call	SetRast

	base	intui
	move.l	windowptr(pc),a0
	move.l	(a2),d0
	beq.b	.nogadget
	move.l	d0,a1
	moveq	#-1,d0
	call	RemoveGList
.nowin
	base	gt
	move.l	(a2),d0
	beq.b	.nogadget
	move.l	d0,a0
	call	FreeGadgets
.nogadget
	clr.l	(a2)
	moveq	#0,d0
	rts
scrollertags:
	dc.l	GTSC_Top
top:
	dc.l	0
	dc.l	GTSC_Total
total:
	dc.l	0
	dc.l	GTSC_Visible
synliga:
	dc.l	0
	dc.l	GTSC_Arrows,16
	dc.l	TAG_DONE
newgadget:
	blk.b	gng_SIZEOF,0
gadgetptr:	dc.l	0
scrollgadget:	dc.l	0
quitgadget:	dc.l	0

;in:
* a1	pointer to ULONG:R0,G0,B0,R1,B1,G1,... table to be filled
getpalette:
	move.l	screenptr(pc),d0
	beq.b	.exit
	move.l	d0,a2
	lea	sc_ViewPort(a2),a2
	move.l	vp_ColorMap(a2),a2
	base	gfx
	cmp.w	#39,LIB_VERSION(a6)
	blo.b	.v36
	moveq	#0,d0
	moveq	#4,d1
	move.l	a2,a0
	call	GetRGB32
	bra.b	.exit
.v36
	moveq	#0,d2
	move.l	a1,a3
.nextcol
	move.l	d2,d0
	move.l	a2,a0
	call	GetRGB4
	move.w	d0,d1
	bsr.b	.4to32
	move.l	d1,8(a3)

	lsr.w	#4,d0
	move.w	d0,d1
	lsr.w	#4,d0
	bsr.b	.4to32
	move.l	d1,4(a3)

	lsr.w	#4,d0
	move.w	d0,d1
	bsr.b	.4to32
	move.l	d1,(a3)

	add.l	#4*3,a3
	addq.l	#1,d2
	cmp.w	#4,d2
	bne.b	.nextcol
.exit
	moveq	#0,d0
	rts
;in:
* d1	4bit
;ut:
* d1	32bit
.4to32
	pushm	d0/d2
	move.l	d1,d0
	and.l	#15,d0
	moveq	#0,d1
	moveq	#8-1,d2
.4to32_loop
	lsl.l	#4,d1
	or.b	d0,d1
	dbf	d2,.4to32_loop
	popm	d0/d2
	rts
;in:
* a1	pointer to ULONG:R0,G0,B0,R1,B1,G1,... table
changepalette:
	move.l	screenptr(pc),d0
	beq.b	.exit
	move.l	d0,a2
	lea	sc_ViewPort(a2),a2

	base	gfx
	cmp.w	#39,LIB_VERSION(a6)
	blo.b	.v36
	lea	colortable,a0
	move.l	#4<<16!0,(a0)+
	moveq	#3*4-1,d0
.copylongs
	move.l	(a1)+,(a0)+
	dbf	d0,.copylongs
	clr.l	(a0)
	move.l	a2,a0
	lea	colortable,a1
	call	LoadRGB32
	bra.b	.exit
.v36
	lea	colortable,a0
	moveq	#4-1,d0
.make4table
	moveq	#0,d2
	move.l	(a1)+,d1
	rol.l	#4,d1
	and.w	#15,d1
	or.w	d1,d2
	lsl.w	#4,d2
	move.l	(a1)+,d1
	rol.l	#4,d1
	and.w	#15,d1
	or.w	d1,d2
	lsl.w	#4,d2
	move.l	(a1)+,d1
	rol.l	#4,d1
	and.w	#15,d1
	or.w	d1,d2
	move.w	d2,(a0)+
	dbf	d0,.make4table
	moveq	#4,d0
	move.l	a2,a0
	lea	colortable,a1
	call	LoadRGB4	
.exit
	moveq	#0,d0
	rts


**** diverse

setwritemask:
	base	gfx
	move.l	windowptr(pc),d0
	beq.b	.nowin
	move.l	d0,a2	
	move.l	wd_RPort(a2),a2

	cmp.w	#39,LIB_VERSION(a6)
	bhs.b	.v39
	move.b	#%00000001,rp_Mask(a2)
	bra.b	.writemaskset
.v39
	move.l	a2,a0
	moveq	#1,d0
	jsr	-990(a6)			;SetMaxPen (V39)
.writemaskset
.nowin
	rts
unsetwritemask:
	base	gfx
	move.l	windowptr(pc),d0
	beq.b	.nowin
	move.l	d0,a2
	move.l	wd_RPort(a2),a2

	cmp.w	#39,LIB_VERSION(a6)
	bhs.b	.v39
	move.b	#%11111111,rp_Mask(a2)
	bra.b	.writemaskset
.v39
	move.l	a2,a0
	moveq	#3,d0
	jsr	-990(a6)			;SetMaxPen (V39)
.writemaskset
.nowin
	rts

;in:
* a1	meddelande
* a2	gadget
* a4	NULL eller pek på argumentlista
requester:
	movem.l	d1/a0-a4/a6,-(sp)
	bsr	lockwindow
	base	intui
	lea	.strukttext(pc),a0
	move.l	a1,(a0)
	lea	.struktgadget(pc),a0
	move.l	a2,(a0)
	move.l	windowptr(pc),a0
	lea	.strukt(pc),a1
	suba.l	a2,a2
	move.l	a4,a3
	call	EasyRequestArgs
	bsr	unlockwindow
	movem.l	(sp)+,d1/a0-a4/a6
	rts
.strukt
	dc.l	.struktend-*
	dc.l	0
	dc.l	0
.strukttext
	dc.l	0
.struktgadget
	dc.l	0
.struktend

;in:
* a2	Window
* a3	Gadget
PressButton:
	pushm	a2/a6
	BASE	intui
	move.l	a2,a0
	move.l	a3,a1
	call	RemoveGadget
	or.w	#GFLG_SELECTED,gg_Flags(a3)
	or.w	#GACT_TOGGLESELECT,gg_Activation(a3)
	move.l	a2,a0
	move.l	a3,a1
	moveq	#-1,d0
	call	AddGadget
	move.l	a3,a0
	move.l	a2,a1
	suba.l	a2,a2
	call	RefreshGadgets
	BASE	dos
	moveq	#5,d1
	call	Delay
	popm	a2/a6
	rts
;in:
* d1	string
* d2	pointer to longword
strtoulong:		;exactly like dos.library/StrToLong, but unsigned
	pushm	d2-d4
	move.l	d1,a0
	move.l	d2,a1
	clr.l	(a1)
	moveq	#0,d3
	moveq	#0,d4
.findfirst
	move.b	(a0)+,d4
	cmp.b	#" ",d4
	beq.b	.findfirst
	cmp.b	#TAB,d4
	beq.b	.findfirst
	sub.b	#"0",d4
	bmi.b	.error
	cmp.b	#9,d4
	bhi.b	.error
.next
	move.l	d3,d2
	asl.l	#3,d3
	add.l	d2,d3
	add.l	d2,d3
	add.l	d4,d3
	move.b	(a0)+,d4
	sub.b	#"0",d4
	bmi.b	.klart
	cmp.b	#9,d4
	bls.b	.next
.klart
	move.l	d3,(a1)
.error
	sub.l	d1,a0
	move.l	a0,d0
	subq.l	#2,d0
	popm	d2-d4
	rts

;in:
* d0	number
;ut:
* a0	dec string (10 bytes: "       777",0)
* d0	dec string ("777",0)
numdec:
	movem.l	d1-d2/a1-a2/a6,-(sp)
	base	util
	lea	numdec_buffert,a2
	move.l	a2,a0
	lea	.siffror(pc),a1
	moveq	#9,d1
.init
	move.b	#" ",(a0)+
	dbf	d1,.init
	clr.b	(a0)
	moveq	#9,d2
.loop
	moveq	#10,d1
	call	UDivMod32
	move.b	(a1,d1.w),-(a0)		;remainder
	tst.l	d0
	beq.b	.klar
	dbf	d2,.loop
.klar
	move.l	a0,d0
	move.l	a2,a0
	movem.l	(sp)+,d1-d2/a1-a2/a6
	rts
.siffror
	dc.b	'0123456789'

cls:
	tst.w	legalprint
	bne.b	.system

	base	gfx

	call	OwnBlitter
	call	WaitBlit
	lea	$dff000,a5
	move.l	#$01000000,bltcon0(a5)
	move.l	#-1,bltafwm(a5)
	move.l	plane0(pc),bltdpt(a5)
	move.w	#0,bltdmod(a5)

	move.l	bwidth(pc),d0
	lsr.l	#1,d0
	move.l	cheightp(pc),d1
	btst	#GFXB_BIG_BLITS,gb_ChipRevBits0(a6)
	bne.b	.bigblit
	lsl.l	#6,d1
	or.l	d0,d1
	move.w	d1,bltsize(a5)
	bra.b	.blitted
.bigblit
	move.w	d1,bltsizv(a5)
	move.w	d0,bltsizh(a5)
.blitted
	call	DisownBlitter
	call	WaitBlit
	rts

.system
	bsr	setwritemask
	base	gfx
	move.l	screenptr(pc),a3
	lea	sc_RastPort(a3),a3
	move.l	a3,a1
	moveq	#0,d0
	call	SetAPen
	move.l	a3,a1
	moveq	#0,d0
	move.l	barheight(pc),d1
	move.l	cwidthp(pc),d2
	move.l	cheightp(pc),d3
	add.l	d1,d3
	subq.l	#1,d3
	call	RectFill
	call	WaitBlit
	rts


;slavefunc
;	lea	.keepgoing(pc),a0
;	clr.w	(a0)
;	base	exec
;	call	CreateMsgPort
;	lea	slaveport(pc),a0
;	move.l	d0,(a0)
;	beq.s	.noport
;.loop
;	move.l	slaveport(pc),a0
;	call	WaitPort
;.getallmsgs
;	base	exec
;	move.l	slaveport(pc),a0
;	call	GetMsg
;	tst.l	d0
;	beq.s	.outofmsgs
;	move.l	d0,a1
;	move.w	slavecmd(a1),d2
;	call	ReplyMsg
;
;	cmp.w	#-1,d2
;	bne.s	.noquit
;	lea	.keepgoing(pc),a0
;	addq.w	#1,(a0)
;	bra.s	.getallmsgs
;.noquit
;
;.outofmsgs
;	tst.w	.keepgoing
;	beq.s	.loop
;
;	move.l	slaveport(pc),a0
;	call	DeleteMsgPort
;.noport
;	rts				;avsluta processen!
;.keepgoing
;	dc.w	0
;
;slavesendmsg
;	base	exec
;	move.l	slaveport(pc),a0
;	lea	slavemessage(pc),a1
;	move.l	comport(pc),MN_REPLYPORT(a1)
;	move.w	#slavemessagelen,MN_LENGTH(a1)
;	call	PutMsg
;	move.l	comport(pc),a0
;	call	WaitPort
;.getallcommsgs
;	move.l	comport(pc),a0
;	call	GetMsg
;	tst.l	d0
;	bne.s	.getallcommsgs
;	rts
;slaveport
;	dc.l	0
;slavemessage
;	ds.b	MP_SIZE		;fult!!
;slavecmd		=*-slavemessage
;	dc.w	0	;command
;	dc.w	0	;data
;slavemessagelen	=*-slavemessage


**********************************************

;;
; Strings:

abouttext:	dc.b	"Textread release %s, version %s.",10
		dc.b	"Copyright ©1992-1995 Martin Blom.",10
		dc.b	10
		dc.b	"Current textfile: %s (%-ld bytes).",10
		dc.b	"Total free memory: %-ld bytes.",10
		dc.b	"Largest block: %-ld bytes.",10
		dc.b	10
		dc.b	"This program is shareware.",0

smodetext:	dc.b	"Please chose a screen mode",0
smodegadget:	dc.b	"PAL|PAL laced|NTSC|NTSC laced|Productivity|Cancel",0

savetext	dc.b	"Are you sure you want",10
		dc.b	"to save the file %s?",0
savegadget	dc.b	"Save|Cancel",0

errnomem	dc.b	"Out of memory.",0
nofileloaded	dc.b	"No text loaded.",0
noworkbench	dc.b	"Workbench isn't open/loaded.",0
colerrortext:	dc.b	"Unable to run 'Colors'.",0
errnofile	dc.b	"Unable to open file.",0
writeerr	dc.b	"Unable to save file.",0
savesetmessage:	dc.b	"Unable to save settings",0
errnolibtext	dc.b	"Unable to open",10
		dc.b	"%s V%ld+.",0

openfiletext	dc.b	"Open file",0
savefiletext	dc.b	"Save file",0
loadsettitle:	dc.b	"Load settings",0
savesettitle:	dc.b	"Save settings",0

loadingtext	dc.b	"Loading...",0
savingtext	dc.b	"Saving...",0
searching:	dc.b	"Searching...",0
counting	dc.b	"Counting lines...",0
remansitext	dc.b	"Removing ANSI codes...",0
remcrtext	dc.b	"Removing CR (ascii #13) characters...",0

menutext	dc.b	"Textread %s - Buffer %lu/%lu: %.31s (%lu lines)",0
menuemptytext	dc.b	"Textread %s - Buffer %lu/%lu (empty)",0

screenname:	dc.b	"Textread Screen",0
intname:	dc.b	"Textread VBLANK server",0
oktext:		dc.b	"Ok",0
linetext	dc.b	"Line:",0

	even

oldfontsize:	dc.w	0
foreignbitmap:	dc.w	0

fileaddr:	dc.l	0
filelen:	dc.l	0
filememlen:	dc.l	0
linepos:	dc.l	0
lines:		dc.l	1
linelen:	dc.l	notnull
linelensize:	dc.l	0

barheight:	dc.l	0	;linjer att hoppa innan menyraden är slut
bwidth:		dc.l	0	;vidd i bytes

pwidth:		dc.l	0	;vidd i pixels
cwidth:		dc.l	0	;kolumner (tecken)
cwidthp:	dc.l	0	;kolumner (pixels)

pheight:	dc.l	0	;höjd i pixels
cheight:	dc.l	0	;rader (tecken)
cheightp:	dc.l	0	;rader (pixels)

plane0:		dc.l	0
radpos:		dc.l	0

null:		dc.l	0
notnull:	dc.l	-1

thistask:	dc.l	0
;slave:		dc.l	0
;printerslave:	dc.l	0

aslname:	dc.b	"asl.library",0
diskfontname:	DISKFONTNAME
dosname:	DOSNAME
gadtoolsname:	dc.b	"gadtools.library",0
gfxname:	GRAPHICSNAME
iconname:	ICONNAME
intuiname:	INTNAME
ppname:		PPNAME
utilname:	UTILITYNAME
wbname:		WORKBENCHNAME
xpkname:	XPKNAME

aslbase:	dc.l	0
_SysBase:	dc.l	0
diskfontbase:	dc.l	0
dosbase:	dc.l	0
_GfxBase:
gfxbase:	dc.l	0
_GadToolsBase:
gtbase:		dc.l	0
iconbase:	dc.l	0
_IntuitionBase:
intuibase:	dc.l	0
ppbase:		dc.l	0
_UtilityBase:
utilbase:	dc.l	0
wbbase:		dc.l	0
xpkbase:	dc.l	0



unit:		dc.l	0


filereqptr:	dc.l	0
setfreqptr:	dc.l	0
setreqptr:	dc.l	0

screenptr:	dc.l	0
screenvi:	dc.l	0
windowptr:	dc.l	0
winmenuptr:	dc.l	0
myfont:		dc.l	0
fontx:		dc.l	0
fonty:		dc.l	0

defreqscreen:	dc.l	0
status:		dc.b	0
	even
tag_done:	dc.l	TAG_DONE

screentags:
	dc.l	SA_Depth,2
	dc.l	SA_Title,screenname
	dc.l	SA_DisplayID
displayid:
	dc.l	HIRES_KEY
	dc.l	SA_Overscan,OSCAN_TEXT
	dc.l	SA_AutoScroll,TRUE
	dc.l	SA_Pens,notnull
	dc.l	SA_Behind,TRUE
	dc.l	SA_ErrorCode,screenerrorcode
	dc.l	SA_SysFont
scrnsysfont:
	dc.l	TRUE
	dc.l	SA_MinimizeISG,TRUE
	dc.l	TAG_DONE
screenerrorcode:
	dc.l	0

windowtags:
	dc.l	WA_RptQueue,1
	dc.l	WA_Left,0
	dc.l	WA_Top,0
	dc.l	WA_Width
windowwidth:
	dc.l	640
	dc.l	WA_Height
windowheight:
	dc.l	200
	dc.l	WA_Title,programname
	dc.l	WA_CustomScreen
windowonscreen:
	dc.l	0
	dc.l	WA_Backdrop,TRUE
	dc.l	WA_Borderless,TRUE
	dc.l	WA_ScreenTitle,screentitle
	dc.l	WA_SmartRefresh,TRUE
	dc.l	WA_Activate,TRUE
	dc.l	WA_IDCMP,IDCMP_INACTIVEWINDOW!IDCMP_MOUSEBUTTONS!IDCMP_MENUVERIFY!IDCMP_MOUSEBUTTONS!ARROWIDCMP!SCROLLERIDCMP!IDCMP_REFRESHWINDOW!IDCMP_MENUPICK!IDCMP_VANILLAKEY!IDCMP_RAWKEY
	;se även rutinen "printhelp!!"

	dc.l	WA_NewLookMenus,TRUE
	dc.l	TAG_DONE

winmenutags:
	dc.l	GTMN_FullMenu,NULL
	dc.l	GTMN_SecondaryError,winmenuerrorcode
	dc.l	TAG_DONE
winmenuerrorcode:
	dc.l	0


programname:	dc.b	"Textread",0	;MUST NOT CHANGE! (used as name for messageport)
filename:	ds.b	32
	even

;fixa winmenu om man inte kör V39+
fixwinmenu:
	base	exec
	cmp.w	#39,LIB_VERSION(a6)
	bhs.b	.finished
	lea	winmenu(pc),a0
.nextnewmenu
	cmp.b	#NM_END,gnm_Type(a0)
	beq.b	.finished
	move.w	gnm_Flags(a0),d0
	and.w	#NM_COMMANDSTRING,d0
	beq.b	.ok
	and.w	#~NM_COMMANDSTRING,gnm_Flags(a0)
	clr.l	gnm_CommKey(a0)
.ok
	add.w	#gnm_SIZEOF,a0
	bra.b	.nextnewmenu
.finished
	rts

winmenu:
* Project
	dc.b	NM_TITLE,0
	dc.l	.project
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.new
	dc.l	.newc
	dc.w	0
	dc.l	0
	dc.l	mnew

	dc.b	NM_ITEM,0
	dc.l	.open
	dc.l	.openc
	dc.w	0
	dc.l	0
	dc.l	mopen

	dc.b	NM_ITEM,0
	dc.l	.opennew
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mopennew

* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.save
	dc.l	.savec
	dc.w	0
	dc.l	0
	dc.l	msave

	dc.b	NM_ITEM,0
	dc.l	.saveas
	dc.l	.saveasc
	dc.w	0
	dc.l	0
	dc.l	msaveas
* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.print
	dc.l	.printc
	dc.w	0
	dc.l	0
	dc.l	mprint

	dc.b	NM_ITEM,0
	dc.l	.printas
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mprintas
* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.reveal
	dc.l	.revealc
	dc.w	0
	dc.l	0
	dc.l	mreveal

	dc.b	NM_ITEM,0
	dc.l	.close
	dc.l	.closec
	dc.w	NM_COMMANDSTRING
	dc.l	0
	dc.l	mclose

* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.iconify
	dc.l	.iconifyc
	dc.w	0
	dc.l	0
	dc.l	miconify

	dc.b	NM_ITEM,0
	dc.l	.workbench
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_SUB,0
	dc.l	.openwb
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mopenwb

	dc.b	NM_SUB,0
	dc.l	.closewb
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mclosewb

* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.about
	dc.l	.aboutc
	dc.w	0
	dc.l	0
	dc.l	mabout

* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.quit
	dc.l	.quitc
	dc.w	0
	dc.l	0
	dc.l	mquit

* Settings
	dc.b	NM_TITLE,0
	dc.l	.settings
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.wordwrap
	dc.l	.wordwrapc
	dc.w	CHECKIT!MENUTOGGLE!CHECKED!NM_COMMANDSTRING
	dc.l	0
	dc.l	mwordwrap

	dc.b	NM_ITEM,0
	dc.l	.sysfont
	dc.l	NULL
	dc.w	CHECKIT!MENUTOGGLE!CHECKED
	dc.l	0
	dc.l	msysfont

	dc.b	NM_ITEM,0
	dc.l	.alwaysremansi
	dc.l	NULL
	dc.w	CHECKIT!MENUTOGGLE!CHECKED
	dc.l	0
	dc.l	malwaysremansi

	dc.b	NM_ITEM,0
	dc.l	.alwaysremcr
	dc.l	NULL
	dc.w	CHECKIT!MENUTOGGLE!CHECKED
	dc.l	0
	dc.l	malwaysremcr

	dc.b	NM_ITEM,0
	dc.l	.nofontfake
	dc.l	NULL
	dc.w	CHECKIT!MENUTOGGLE!CHECKED
	dc.l	0
	dc.l	mnofontfake

	dc.b	NM_ITEM,0
	dc.l	.legalprint
	dc.l	NULL
	dc.w	CHECKIT!MENUTOGGLE!CHECKED
	dc.l	0
	dc.l	mlegalprint

* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.tabsize
	dc.l	.tabsizec
	dc.w	NM_COMMANDSTRING
	dc.l	0
	dc.l	mtabsize

	dc.b	NM_ITEM,0
	dc.l	.screen
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mscreen

	dc.b	NM_ITEM,0
	dc.l	.colour
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_SUB,0
	dc.l	.screencol
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mscreencol

	dc.b	NM_SUB,0
	dc.l	.defcol
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mdefcol

	dc.b	NM_ITEM,0
	dc.l	.font
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_SUB,0
	dc.l	.textfont
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mtextfont

	dc.b	NM_SUB,0
	dc.l	.deffont
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mdeffont

* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.loadset
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mloadset

	dc.b	NM_ITEM,0
	dc.l	.saveset
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	msaveset

	dc.b	NM_ITEM,0
	dc.l	.savesetas
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	msavesetas

* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.createicons
	dc.l	NULL
	dc.w	CHECKIT!MENUTOGGLE!CHECKED
	dc.l	0
	dc.l	mcreateicons

	dc.b	NM_ITEM,0
	dc.l	.prefs2all
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	mprefs2all

* Commands
	dc.b	NM_TITLE,0
	dc.l	.commands
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.searchfor
	dc.l	.searchforc
	dc.w	0
	dc.l	0
	dc.l	msearch

	dc.b	NM_ITEM,0
	dc.l	.searchsnext
	dc.l	.searchsnextc
	dc.w	NM_COMMANDSTRING
	dc.l	0
	dc.l	msearchnext

	dc.b	NM_ITEM,0
	dc.l	.searchsprev
	dc.l	.searchsprevc
	dc.w	NM_COMMANDSTRING
	dc.l	0
	dc.l	msearchprev

* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.gotoline
	dc.l	.gotolinec
	dc.w	NM_COMMANDSTRING
	dc.l	0
	dc.l	vl

	dc.b	NM_ITEM,0
	dc.l	.gotopercent
	dc.l	.gotopercentc
	dc.w	NM_COMMANDSTRING
	dc.l	0
	dc.l	vg
* -----
	dc.b	NM_ITEM,0
	dc.l	NM_BARLABEL
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

	dc.b	NM_ITEM,0
	dc.l	.edittext
	dc.l	.edittextc
	dc.w	NM_COMMANDSTRING
	dc.l	0
	dc.l	medittext

	dc.b	NM_ITEM,0
	dc.l	.remansi
	dc.l	.remansic
	dc.w	NM_COMMANDSTRING
	dc.l	0
	dc.l	mremansi

	dc.b	NM_ITEM,0
	dc.l	.remcr
	dc.l	.remcrc
	dc.w	NM_COMMANDSTRING
	dc.l	0
	dc.l	mremcr
* END
	dc.b	NM_END,0
	dc.l	0
	dc.l	NULL
	dc.w	0
	dc.l	0
	dc.l	NULL

.project	dc.b	"Project",0
.new		dc.b		"New",0
.open		dc.b		"Open...",0
.opennew	dc.b		"Open new...",0
.save		dc.b		"Save...",0
.saveas		dc.b		"Save As...",0
.print		dc.b		"Print",0
.printas	dc.b		"Print as...",0
.iconify	dc.b		"Iconify",0
.workbench	dc.b		"Workbench",0
.openwb		dc.b			"Open",0
.closewb	dc.b			"Close",0
.reveal		dc.b		"Reveal...",0
.close		dc.b		"Close",0
.about		dc.b		"About...",0
.quit		dc.b		"Quit Textread",0
.settings	dc.b	"Settings",0
.wordwrap	dc.b		"Wrap words?",0
.sysfont	dc.b		"Use Sysfont?",0
.alwaysremansi	dc.b		"Auto-remove ANSIs?",0
.alwaysremcr	dc.b		"Auto-remove CRs?",0
.nofontfake	dc.b		"Don't 'fake' font?",0
.legalprint	dc.b		"System rendering?",0
.tabsize	dc.b		"Tab size...",0
.screen	 	dc.b		"Screen mode...",0
.colour		dc.b		"Screen colour",0
.screencol	dc.b			"Select screen colour...",0
.defcol		dc.b			"Default colour",0
.font		dc.b		"Text font",0
.textfont	dc.b			"Select text font...",0
.deffont	dc.b			"Default font",0
.loadset	dc.b		"Load settings...",0
.saveset	dc.b		"Save settings",0
.savesetas	dc.b		"Save settings as...",0
.createicons	dc.b		"Create icons?",0
.prefs2all	dc.b		"Copy to all buffers",0
.commands	dc.b	"Commands",0
.searchfor	dc.b		"Find...",0
.searchsnext	dc.b		"Find next",0
.searchsprev	dc.b		"Find previous",0
.gotoline	dc.b		"Go to line",0
.gotopercent	dc.b		"Go to percent",0
.edittext	dc.b		"Edit text",0
.remansi	dc.b		"Remove ANSIs",0
.remcr		dc.b		"Remove CRs",0

.newc		dc.b	"N",0
.openc		dc.b	"O",0
.savec		dc.b	"S",0
.saveasc	dc.b	"A",0
.printc		dc.b	"P",0
.revealc	dc.b	"R",0
.closec		dc.b	"Esc",0
.iconifyc	dc.b	"I",0
.aboutc		dc.b	"?",0
.quitc		dc.b	"Q",0

.wordwrapc	dc.b	"W",0
.tabsizec	dc.b	"Tab",0

.searchforc	dc.b	"F",0
.searchsnextc	dc.b	"N",0
.searchsprevc	dc.b	"P",0
.gotolinec	dc.b	"#/L",0
.gotopercentc	dc.b	"%/G",0
.edittextc	dc.b	"Shift E",0
.remansic	dc.b	"Shift A",0
.remcrc		dc.b	"Shift C",0
	even

***
;;
settings:

filereqdir:		dc.b	"SYS:",0
			blk.b	256-(*-filereqdir),0
filereqpat:		dc.b	"~(#?.info)",0
			blk.b	64-(*-filereqpat),0
clcwidth:		dc.l	0		;countlines' cwidth

prefs2all_start:
prefs2all_offset	=prefs2all_start-settings

thisfont:		dc.l	thisfontname
thisfontsize:		dc.w	8
			dc.b	0,FPF_ROMFONT!FPF_DESIGNED
thisfontname:		dc.b	"topaz.font",0
			blk.b	31-(*-thisfontname),0

		even

tabsize:		dc.l	8

* --- Checkmarks ---
setcreateicons:		dc.w	1
setwordwrap:		dc.w	1
setsysfont:		dc.w	0
setalwaysremansi:	dc.w	0
setalwaysremcr: 	dc.w	0
setnofontfake:		dc.w	1
setlegalprint:		dc.w	1


setdisplayid:		dc.l	HIRES_KEY

palette:
			dc.l	$AAAAAAAA,$AAAAAAAA,$AAAAAAAA
			dc.l	$00000000,$00000000,$00000000
			dc.l	$FFFFFFFF,$FFFFFFFF,$FFFFFFFF
			dc.l	$66666666,$88888888,$BBBBBBBB
prefs2all_end:
prefs2all_len		=prefs2all_end-prefs2all_start

settings_len		=*-settings

pol:		dc.l	1
pom:		dc.l	1
por:		dc.l	2
pow:		dc.l	TRUE
pob:		dc.l	TRUE
poi:		dc.l	FALSE
pou:		dc.l	TRUE
mptab:		dc.l	8
mppage:		dc.w	0
mpww:		dc.b	0
mpinfo:		dc.b	1		;header on/off
mpheader:	dc.b	5,1,4,3		;#0=fet #1=kursiv #2=understr
					;1=filnamn 2=titel 3=sidnummer 4=datum
mptitle:	blk.b	32,0		;30 tecken + 1 nolla + jämt!

defsetname:	dc.b	"ENVARC:Textread/Textread.prefs",0
	even


**** muti-buffer structure

	RSRESET
	rs.b		LN_SIZE		;node
buff_commessageport:	rs.l	1
buff_fileaddr:		rs.l	1
buff_filelen:		rs.l	1
buff_filememlen:	rs.l	1
buff_linepos:		rs.l	1
buff_lines:		rs.l	1
buff_linelen:		rs.l	1
buff_linelensize:	rs.l	1
buff_number		rs.b	4	; 'xx: ', requestreveal uses it!
buff_filename:		rs.b	32
buff_setname:		rs.b	setnamelen
buff_settings:		rs.b	settings_len
buff_fkeys:		rs.b	fkeys_lines_size
buff_len:		rs.b	0

	section Icons,data_c

AppIconData1:
;Plane 0
	dc.w	$0000,$0000,$0000,$0400,$0000,$0000,$0000,$0C00
	dc.w	$0000,$0000,$0000,$0C00,$007F,$FFFF,$FFF0,$0C00
	dc.w	$006A,$AFFE,$AAB0,$0C00,$0040,$0198,$0010,$0C00
	dc.w	$0040,$07FF,$8010,$0C00,$0040,$7801,$F810,$0C00
	dc.w	$0441,$8000,$7E11,$0C00,$0E46,$0001,$8791,$0C00
	dc.w	$1F46,$0006,$0791,$0C00,$0458,$0018,$01E7,$CC00
	dc.w	$0458,$0060,$01E3,$8C00,$0458,$0000,$01E1,$0C00
	dc.w	$0046,$0000,$0790,$0C00,$0046,$0000,$0790,$0C00
	dc.w	$0041,$8000,$1E10,$0C00,$006A,$F801,$FAB0,$0C00
	dc.w	$007F,$FFFF,$FFF0,$0C00,$0000,$0000,$0000,$0C00
	dc.w	$0000,$0000,$0000,$0C00,$7FFF,$FFFF,$FFFF,$FC00
;Plane 1
	dc.w	$FFFF,$FFFF,$FFFF,$F800,$D555,$5555,$5555,$5000
	dc.w	$D555,$5555,$5555,$5000,$D500,$0000,$0005,$5000
	dc.w	$D515,$5001,$5545,$5000,$D53F,$FE67,$FFE5,$5000
	dc.w	$D53F,$F800,$7FE5,$5000,$D53F,$87FE,$07E5,$5000
	dc.w	$D13E,$7FFF,$E1E4,$5000,$D139,$FFFF,$F864,$5000
	dc.w	$C039,$FFFF,$F864,$5000,$D127,$FFFF,$FE00,$1000
	dc.w	$D127,$FF9F,$FE04,$5000,$D127,$FFFF,$FE04,$5000
	dc.w	$D539,$FFFF,$F865,$5000,$D539,$FFFF,$F865,$5000
	dc.w	$D53E,$7FFF,$E165,$5000,$D515,$07FE,$0545,$5000
	dc.w	$D500,$0000,$0005,$5000,$D555,$5555,$5555,$5000
	dc.w	$D555,$5555,$5555,$5000,$8000,$0000,$0000,$0000

AppIcon1:
	dc.w	0,0
	dc.w	54,22,2
	dc.l	AppIconData1
	dc.b	3,0
	dc.l	NULL

AppIconData2:
;Plane 0
	dc.w	$FFFF,$FFFF,$FFFF,$F800,$C000,$0000,$0000,$0000
	dc.w	$C000,$0000,$0000,$0000,$C07F,$FFFF,$FFF0,$0000
	dc.w	$C06A,$AFFE,$AAB0,$0000,$C040,$0018,$0010,$0000
	dc.w	$C45D,$D5DF,$8011,$0000,$CE49,$1481,$F011,$0000
	dc.w	$DF49,$8880,$0091,$0000,$C449,$1480,$1897,$C000
	dc.w	$C449,$D480,$9493,$8000,$CE40,$001D,$5431,$0000
	dc.w	$DF58,$0191,$D4B1,$0000,$C458,$0159,$5877,$C000
	dc.w	$C446,$0191,$4793,$8000,$C446,$015C,$0791,$0000
	dc.w	$C041,$8140,$1E10,$0000,$C06A,$F801,$FAB0,$0000
	dc.w	$C07F,$FFFF,$FFF0,$0000,$C000,$0000,$0000,$0000
	dc.w	$C000,$0000,$0000,$0000,$8000,$0000,$0000,$0000
;Plane 1
	dc.w	$0000,$0000,$0000,$0400,$2AAA,$AAAA,$AAAA,$AC00
	dc.w	$2AAA,$AAAA,$AAAA,$AC00,$2A80,$0000,$000A,$AC00
	dc.w	$2A95,$5001,$554A,$AC00,$2ABF,$FFE7,$FFEA,$AC00
	dc.w	$2AA2,$2A20,$7FEA,$AC00,$20B6,$EB7E,$0FEA,$AC00
	dc.w	$20B6,$777F,$FF6A,$AC00,$2AB6,$EB7F,$E768,$2C00
	dc.w	$2AB6,$2B7F,$6B68,$2C00,$20BF,$FFE2,$ABCA,$AC00
	dc.w	$20A7,$FE6E,$2B4A,$AC00,$2AA7,$FEA6,$A788,$2C00
	dc.w	$2AB9,$FE6E,$B868,$2C00,$2AB9,$FEA3,$F86A,$AC00
	dc.w	$2ABE,$7EBF,$E1EA,$AC00,$2A95,$07FE,$054A,$AC00
	dc.w	$2A80,$0000,$000A,$AC00,$2AAA,$AAAA,$AAAA,$AC00
	dc.w	$2AAA,$AAAA,$AAAA,$AC00,$7FFF,$FFFF,$FFFF,$FC00

AppIcon2:
	dc.w	0,0
	dc.w	54,22,2
	dc.l	AppIconData2
	dc.b	3,0
	dc.l	NULL

AppIconDO:
	dc.w	NULL			; Magic Number
	dc.w	NULL			; Version
					; Embedded Gadget Structure
	dc.l	NULL			; Next Gadget Pointer
	dc.w	0,0,54,23		; Left,Top,Width,Height
	dc.w	GFLG_GADGHIMAGE		; Flags
	dc.w	NULL			; Activation Flags
	dc.w	NULL			; Gadget Type
	dc.l	AppIcon1		; Render Image
	dc.l	AppIcon2		; Select Image
	dc.l	NULL			; Gadget Text
	dc.l	NULL			; Mutual Exclude
	dc.l	NULL			; Special Info
	dc.w	NULL			; Gadget ID
	dc.l	NULL			; User Data

	dc.b	NULL,0	;WBTOOL t.ex.	  Icon Type
	dc.l	NULL			; Default Tool
	dc.l	NULL			; Tool Type Array
	dc.l	NO_ICON_POSITION	; Current X
	dc.l	NO_ICON_POSITION	; Current Y
	dc.l	NULL			; Drawer Structure
	dc.l	NULL			; Tool Window
	dc.l	NULL			; Stack size

Textread_prefsI1Data:
;Plane 0
	dc.w	$0000,$0000,$0004,$0000,$0000,$0000,$0001,$0000
	dc.w	$0000,$07FF,$8000,$4000,$1DD5,$D800,$6000,$1000
	dc.w	$0914,$A0FC,$1000,$0800,$0988,$C102,$0800,$0C00
	dc.w	$0914,$C082,$0800,$0C00,$09D4,$C082,$0800,$0C00
	dc.w	$0000,$2104,$0800,$0C00,$19C9,$9E18,$1000,$0C00
	dc.w	$1515,$4060,$2000,$0C00,$199D,$4080,$C000,$0C00
	dc.w	$1515,$4103,$0000,$0C00,$15D5,$821C,$0000,$0C00
	dc.w	$0000,$0109,$99DD,$CC00,$0000,$00F1,$5511,$0C00
	dc.w	$0000,$0109,$9999,$CC00,$0000,$0109,$1510,$4C00
	dc.w	$4000,$00F1,$15D1,$CC00,$1000,$0000,$0000,$0C00
	dc.w	$0400,$0000,$0000,$0C00,$01FF,$FFFF,$FFFF,$FC00
;Plane 1
	dc.w	$FFFF,$FFFF,$FFF8,$0000,$D555,$5555,$5556,$0000
	dc.w	$D555,$5000,$5555,$8000,$C000,$07FF,$9555,$6000
	dc.w	$C000,$1F03,$E555,$5000,$C000,$3E55,$F555,$5000
	dc.w	$C000,$3F55,$F555,$5000,$C000,$3F55,$F555,$5000
	dc.w	$D555,$5E53,$F555,$5000,$C000,$0147,$E555,$5000
	dc.w	$C000,$151F,$D555,$5000,$C000,$157F,$1555,$5000
	dc.w	$C000,$14FC,$5555,$5000,$C000,$15E1,$5555,$5000
	dc.w	$D555,$54F4,$0000,$1000,$D555,$5504,$0000,$1000
	dc.w	$D555,$54F4,$0000,$1000,$D555,$54F4,$0000,$1000
	dc.w	$3555,$5504,$0000,$1000,$0D55,$5555,$5555,$5000
	dc.w	$0355,$5555,$5555,$5000,$0000,$0000,$0000,$0000

Textread_prefsI1:
	dc.w	0,0
	dc.w	54,22,2
	dc.l	Textread_prefsI1Data
	dc.b	3,0
	dc.l	NULL

Textread_prefsTools:
	dc.l	.text
	dc.l	NULL
.text
	dc.b	"FILETYPE=PREFS",0
	even

Textread_prefs:
	dc.w	WB_DISKMAGIC
	dc.w	WB_DISKVERSION

	dc.l	NULL
	dc.w	0,0,54,23
	dc.w	GFLG_GADGIMAGE!GADGBACKFILL
	dc.w	GACT_RELVERIFY!GADGIMMEDIATE
	dc.w	BOOLGADGET
	dc.l	Textread_prefsI1
	dc.l	NULL
	dc.l	NULL
	dc.l	NULL
	dc.l	NULL
	dc.w	0
	dc.l	NULL

 	dc.b	WBPROJECT,0
	dc.l	.deftool
	dc.l	Textread_prefsTools
	dc.l	NO_ICON_POSITION
	dc.l	NO_ICON_POSITION
	dc.l	NULL
	dc.l	NULL
	dc.l	4096
.deftool
	dc.b	"Textread",0
	even

	section	bssdata,BSS

fib		ds.b	fib_SIZEOF	;fileinfoblock, lw-aligned (splitfilepath:)
com_array:	ds.b	coma_SIZE
int:		ds.b	IS_SIZE
lockwinreq:	ds.b	rq_SIZEOF	;(lockwindow)
XPKerrbuf:	ds.b	XPKERRMSGSIZE
colortable:	ds.l	1+4*3+1		;(changepalette)
searchstring:	ds.b	80
screentitle:	ds.b	90
savedfont:	ds.b	ta_SIZEOF
savedfontname:	ds.b	31
numdec_buffert	ds.b	11

defpalette:	ds.l	4*3
setname:	ds.b	setnamelen
tabbar:		ds.b	256
printtabbar:	ds.b	256
argprojectname:	ds.b	256
temp:		ds.b	512
tempend:
tempsize	=*-temp

oldfontname:	ds.b	32

font:		ds.b	256*8


deffont:	ds.b	ta_SIZEOF
deffontname:	ds.b	31
prefs_id_test:	ds.b	prefs_id_len+1

	section windows,code



HRWv	MACRO		;character,function
	dc.w	\1
	dc.l	\2
	ENDM

HRWr	MACRO		;keycode,qual (or),function
	dc.w	\1,\2
	dc.l	\3
	ENDM

HRWg	MACRO
	dc.w	\1
	dc.l	\2
	ENDM

HWRv_SIZEOF	EQU	2+4
HWRr_SIZEOF	EQU	2+2+4
HWRg_SIZEOF	EQU	2+4

	RSRESET
hrw_window	rs.l	1
hrw_render	rs.l	1
hrw_vkeys	rs.l	1
hrw_rkeys	rs.l	1
hrw_gadgets	rs.l	1





;ut:
* d0	-1	avsluta Textread
* d0	0	allt ok, leta inte
* d0	1	allt ok, leta
requestsearch:

	move.l	screenptr,a1
	bsr	OpenfindreqWindow
	tst.l	d0
	bne.w	.error
	jsr	lockwindow

	move.w	search_wild,.wild+2
	move.w	search_case,.casesens+2
	move.w	search_dir,.dir+2

	BASE	gt
	move.l	findreq(pc),a4
	suba.l	a2,a2
	move.l	findreqGadgets+GD_sw_searchfor*4(pc),a0
	move.l	a4,a1
	lea	.searchfor_tags(pc),a3
	call	GT_SetGadgetAttrsA
	move.l	findreqGadgets+GD_sw_casesens*4(pc),a0
	move.l	a4,a1
	lea	.casesens_tags(pc),a3
	call	GT_SetGadgetAttrsA
	move.l	findreqGadgets+GD_sw_wildcards*4(pc),a0
	move.l	a4,a1
	lea	.wildcards_tags(pc),a3
	call	GT_SetGadgetAttrsA
	move.l	findreqGadgets+GD_sw_fwdbwd*4(pc),a0
	move.l	a4,a1
	lea	.direction_tags(pc),a3
	call	GT_SetGadgetAttrsA

	BASE	intui
	move.l	findreqGadgets+GD_sw_searchfor*4(pc),a0
	move.l	findreq(pc),a1
	suba.l	a2,a2
	call	ActivateGadget

	lea	.strukt(pc),a0
	move.l	findreq(pc),(a0)
	bsr	handlerequestwindow
	move.l	d0,-(sp)
	jsr	unlockwindow
	bsr	ClosefindreqWindow
	move.l	(sp)+,d0
	rts
.error
	moveq	#0,d0
	rts
.strukt
	dc.l	0
	dc.l	.render
	dc.l	.strukt_v
	dc.l	.strukt_r
	dc.l	.strukt_g

.strukt_v
	HRWv	"s",.fn_search
	HRWv	$0d,.fn_search
	HRWv	"u",.fn_use
	HRWv	"c",.fn_cancel
	HRWv	$1b,.fn_cancel
	HRWv	"f",.fn_find
	HRWv	"w",.fn_wild
	HRWv	"a",.fn_case
	HRWv	"d",.fn_dir
	HRWv	-1,-1
.strukt_r
	HRWr	-1,-1,-1
.strukt_g

	HRWg	GD_sw_searchfor,.fn_findgad
	HRWg	GD_sw_wildcards,.fn_wildgad
	HRWg	GD_sw_casesens,.fn_casegad
	HRWg	GD_sw_fwdbwd,.fn_dirgad
	HRWg	GD_sw_search,.fn_searchgad
	HRWg	GD_sw_use,.fn_usegad
	HRWg	GD_sw_cancel,.fn_cancelgad
	HRWg	-1,-1

.render
;	bsr	searchwindowRender
	rts

.fn_search
	move.l	hrw_window(a5),a2
	move.l	findreqGadgets+GD_sw_search*4(pc),a3
	jsr	PressButton
.fn_searchgad
	bsr.w	.update
	move.w	#1+1,d0
	rts
.fn_use
	move.l	hrw_window(a5),a2
	move.l	findreqGadgets+GD_sw_use*4(pc),a3
	jsr	PressButton
.fn_usegad
	bsr.w	.update
	move.w	#0+1,d0
	rts
.fn_cancel
	move.l	hrw_window(a5),a2
	move.l	findreqGadgets+GD_sw_cancel*4(pc),a3
	jsr	PressButton
.fn_cancelgad
	move.w	#0+1,d0
	rts
.fn_find
	BASE	intui
	move.l	findreqGadgets+GD_sw_searchfor*4(pc),a0
	move.l	findreq(pc),a1
	suba.l	a2,a2
	call	ActivateGadget
	moveq	#0,d0
	rts
.fn_findgad
	and.w	#IEQUALIFIERB_LSHIFT!IEQUALIFIERB_RSHIFT,d4
	beq.w	.fn_search
	moveq	#0,d0
	rts
.fn_wild
	bchg	#0,.wild+3
	move.l	findreqGadgets+GD_sw_wildcards*4(pc),a0
	move.l	findreq(pc),a1
	suba.l	a2,a2
	lea	.wildcards_tags(pc),a3
	call	GT_SetGadgetAttrsA
	moveq	#0,d0
	rts
.fn_wildgad
	clr.l	.wild
	move.w	gg_Flags(a2),d0
	and.w	#GFLG_SELECTED,d0
	beq.s	.fn_wildgad_notselected
	addq.l	#1,.wild
.fn_wildgad_notselected
	moveq	#0,d0
	rts
.fn_case
	bchg	#0,.casesens+3
	move.l	findreqGadgets+GD_sw_casesens*4(pc),a0
	move.l	findreq(pc),a1
	suba.l	a2,a2
	lea	.casesens_tags(pc),a3
	call	GT_SetGadgetAttrsA
	moveq	#0,d0
	rts
.fn_casegad
	clr.l	.casesens
	move.w	gg_Flags(a2),d0
	and.w	#GFLG_SELECTED,d0
	beq.s	.fn_casegad_notselected
	addq.l	#1,.casesens
.fn_casegad_notselected
	moveq	#0,d0
	rts
.fn_dir
	bchg	#0,.dir+3
	move.l	findreqGadgets+GD_sw_fwdbwd*4(pc),a0
	move.l	findreq(pc),a1
	suba.l	a2,a2
	lea	.direction_tags(pc),a3
	call	GT_SetGadgetAttrsA
	moveq	#0,d0
	rts
.fn_dirgad
	move.l	d3,.dir
	moveq	#0,d0
	rts

.update
	move.w	.wild+2,search_wild
	move.w	.casesens+2,search_case
	move.w	.dir+2,search_dir
	move.l	findreqGadgets+GD_sw_searchfor*4(pc),a0
	move.l	gg_SpecialInfo(a0),a0
	move.l	si_Buffer(a0),a0
	moveq	#79,d0
	lea	searchstring,a1
.fn_use_copy
	move.b	(a0)+,(a1)+
	dbeq	d0,.fn_use_copy
	rts
	
.searchfor_tags
	dc.l	GTST_String,searchstring
	dc.l	TAG_DONE
.casesens_tags
	dc.l	GTCB_Checked
.casesens
	dc.l	0			;1=japps
	dc.l	TAG_DONE
.wildcards_tags
	dc.l	GTCB_Checked
.wild
	dc.l	0			;1=wildcards on
	dc.l	TAG_DONE
.direction_tags
	dc.l	GTCY_Active
.dir
	dc.l	0			;1=backwards
	dc.l	TAG_DONE

;ut:
* d0	-1	avsluta Textread
* d0	0	allt ok, byt inte
* d0	1	allt ok, byt
requesttabsize:
	move.l	screenptr,a1
	bsr	OpentabsizereqWindow
	tst.l	d0
	bne.b	.error
	jsr	lockwindow

	move.l	tabsize,.level
	bsr	.uppdateraslidern


	lea	.strukt(pc),a0
	move.l	tabsizereq(pc),(a0)
	bsr	handlerequestwindow
	move.l	d0,-(sp)
	jsr	unlockwindow
	bsr	ClosetabsizereqWindow
	move.l	(sp)+,d0
	rts
.error
	moveq	#0,d0
	rts

.strukt
	dc.l	0
	dc.l	.render
	dc.l	.strukt_v
	dc.l	.strukt_r
	dc.l	.strukt_g

.strukt_v
	HRWv	"u",.fn_use
	HRWv	$0d,.fn_use
	HRWv	"c",.fn_cancel
	HRWv	$1b,.fn_cancel
	HRWv	"t",.fn_inc
	HRWv	"T",.fn_dec
	HRWv	-1,-1
.strukt_r
	HRWr	IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,$4f,.fn_first
	HRWr	0,$4f,.fn_dec
	HRWr	IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,$4e,.fn_last
	HRWr	0,$4e,.fn_inc
	HRWr	0,$4c,.fn_center
	HRWr	-1,-1,-1
.strukt_g
	HRWg	GD_tw_use,.fn_usegad
	HRWg	GD_tw_cancel,.fn_cancelgad
	HRWg	GD_tw_tabsize,.fn_slider
	HRWg	-1,-1

.render
;	bsr	tabsizereqRender
	rts

.fn_use
	move.l	hrw_window(a5),a2
	move.l	tabsizereqGadgets+GD_tw_use*4(pc),a3
	jsr	PressButton
.fn_usegad
	move.l	.level,tabsize
	moveq	#1+1,d0
	rts
.fn_cancel
	move.l	hrw_window(a5),a2
	move.l	tabsizereqGadgets+GD_tw_cancel*4(pc),a3
	jsr	PressButton
.fn_cancelgad
	moveq	#0+1,d0
	rts
.fn_dec
	cmp.l	#1,.level
	beq.b	.fn_dec_first
	subq.l	#1,.level
.fn_dec_first
	bra.b	.uppdateraslidern
.fn_inc
	cmp.l	#15,.level
	beq.b	.fn_inc_last
	addq.l	#1,.level
.fn_inc_last
	bra.b	.uppdateraslidern
.fn_first
	move.l	#1,.level
	bra.b	.uppdateraslidern
.fn_last
	move.l	#15,.level
	bra.b	.uppdateraslidern
.fn_center
	move.l	#8,.level
	bra.w	.uppdateraslidern
.fn_slider
	move.l	d3,.level
	moveq	#0,d0
	rts
.uppdateraslidern
	BASE	gt
	move.l	tabsizereqGadgets+GD_tw_tabsize*4(pc),a0
	move.l	tabsizereq(pc),a1
	suba.l	a2,a2
	lea	.upp_tabsize_level(pc),a3
	call	GT_SetGadgetAttrsA
	moveq	#0,d0
	rts

.upp_tabsize_level
	dc.l	GTSL_Level
.level	dc.l	0
	dc.l	TAG_DONE


;ut:
* d0	-1	avsluta Textread
* d0	0	allt ok, byt inte
* d0	1	allt ok, byt
requestreveal:
	lea	bufflist,a1		;update buffer numbers ('xx: filename')
	moveq	#1,d1
.stepdown
	TSTNODE	a1,a1
	beq.b	.listok
	move.l	d1,d0
	addq.l	#1,d1
	jsr	numdec
	lea	buff_number(a1),a2
	move.b	8(a0),(a2)+
	move.b	9(a0),(a2)+
	move.b	#":",(a2)+
	move.b	#" ",(a2)+
	bra.b	.stepdown
.listok

	move.l	screenptr,a1
	bsr	OpenrevealreqWindow
	tst.l	d0
	bne.b	.error
	jsr	lockwindow

	BASE	gt
	move.l	revealreqGadgets+GD_rw_list*4(pc),a0
	move.l	revealreq(pc),a1
	suba.l	a2,a2
	lea	.initlist_tags(pc),a3
	call	GT_SetGadgetAttrsA

	move.l	bnum,.selected
	subq.l	#1,.selected
	bsr	.uppdateralistan

	lea	.strukt(pc),a0
	move.l	revealreq(pc),(a0)
	bsr	handlerequestwindow
	move.l	d0,-(sp)
	jsr	unlockwindow
	bsr	CloserevealreqWindow
	move.l	(sp)+,d0
	rts
.error
	moveq	#0,d0
	rts
.initlist_tags
	dc.l	GTLV_Labels,bufflist
	dc.l	TAG_DONE
.strukt
	dc.l	0
	dc.l	.render
	dc.l	.strukt_v
	dc.l	.strukt_r
	dc.l	.strukt_g

.strukt_v
	HRWv	"u",.fn_use
	HRWv	$0d,.fn_use
	HRWv	"c",.fn_cancel
	HRWv	$1b,.fn_cancel
	HRWv	"b",.fn_next
	HRWv	"B",.fn_prev
	HRWv	-1,-1
.strukt_r
	HRWr	IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,$4c,.fn_top
	HRWr	0,$4c,.fn_prev
	HRWr	IEQUALIFIER_LSHIFT!IEQUALIFIER_RSHIFT,$4d,.fn_end
	HRWr	0,$4d,.fn_next
	HRWr	-1,-1,-1
.strukt_g

	HRWg	GD_rw_list,.fn_list
	HRWg	GD_rw_use,.fn_usegad
	HRWg	GD_rw_cancel,.fn_cancelgad
	HRWg	-1,-1

.render
	rts

.fn_use
	move.l	hrw_window(a5),a2
	move.l	revealreqGadgets+GD_rw_use*4(pc),a3
	jsr	PressButton
.fn_usegad
	move.l	.selected,bnum
	addq.l	#1,bnum
	move.w	#1+1,d0
	rts
.fn_cancel
	move.l	hrw_window(a5),a2
	move.l	revealreqGadgets+GD_rw_cancel*4(pc),a3
	jsr	PressButton
.fn_cancelgad
	move.w	#0+1,d0
	rts
.fn_next
	move.l	bmax,d0
	subq.l	#1,d0
	cmp.l	.selected,d0
	beq.b	.fn_next_last
	addq.l	#1,.selected
.fn_next_last
	bra.w	.uppdateralistan
.fn_prev
	tst.l	.selected
	beq.b	.fn_prev_first
	subq.l	#1,.selected
.fn_prev_first
	bra.w	.uppdateralistan
.fn_top
	clr.l	.selected
	bra.w	.uppdateralistan
.fn_end
	move.l	bmax,.selected
	subq.l	#1,.selected
	bra.w	.uppdateralistan
.fn_list
	move.l	.fn_list_time(pc),d0
	move.l	.fn_list_time+4(pc),d1
	move.l	d5,.fn_list_time
	move.l	d6,.fn_list_time+4

	move.l	.selected(pc),d4
	move.l	d3,.selected
	cmp.l	.selected(pc),d4
	bne	.fn_list_exit
	move.l	d5,d2
	move.l	d6,d3
	BASE	intui
	call	DoubleClick
	tst.l	d0
	bne.w	.fn_use
.fn_list_exit
	moveq	#0,d0
	rts
.fn_list_time
	dc.l	0,0

.uppdateralistan
	BASE	gt
	cmp.w	#39,LIB_VERSION(a6)
	bhs.b	.uppdateralistan_selected
*** Kludge för <V39
	move.l	.top(pc),d0
	cmp.l	.selected(pc),d0
	bmi.b	.uppdateralistan_notB4
	move.l	.selected(pc),.top
	bra.b	.uppdateralistan_doit
.uppdateralistan_notB4
	addq.l	#6-1,d0				;?????????? WHERE?? FIXIT!
	cmp.l	.selected(pc),d0
	bpl.b	.uppdateralistan_selected
	move.l	.selected(pc),d0
	subq.l	#6-1,d0
	move.l	d0,.top
.uppdateralistan_doit
	move.l	revealreqGadgets+GD_rw_list*4(pc),a0
	move.l	revealreq(pc),a1
	suba.l	a2,a2
	lea	.top_tags(pc),a3
	call	GT_SetGadgetAttrsA
.uppdateralistan_selected
	move.l	.selected,.visible
	move.l	revealreqGadgets+GD_rw_list*4(pc),a0
	move.l	revealreq(pc),a1
	suba.l	a2,a2
	lea	.selected_tags(pc),a3
	call	GT_SetGadgetAttrsA

	moveq	#0,d0
	rts

.selected_tags
	dc.l	GTLV_Selected
.selected
	dc.l	0
	dc.l	GTLV_MakeVisible
.visible
	dc.l	0
	dc.l	TAG_DONE

.top_tags
	dc.l	GTLV_Top
.top
	dc.l	0
	dc.l	TAG_DONE

requestprintopts:
	jsr	lockwindow
	bsr	printoptions
	move.l	d0,-(sp)
	jsr	unlockwindow
	move.l	(sp)+,d0
	rts



;ut:
* d0	-1	avsluta Textread
* d0	0	allt ok, byt inte rad
* d0	1	allt ok, byt rad
* d1	tal
requestpercent:
.again
	move.l	linepos,d0
	mulu.w	#100,d0
	move.l	lines,d1
	sub.l	cheight,d1
	beq.b	.nodiv
	divu.w	d1,d0
.nodiv
	and.l	#$0000ffff,d0
	lea	.number(pc),a0
	move.l	d0,(a0)
	lea	.text1(pc),a1
	lea	.text2(pc),a2

	bsr	requestinteger

	tst.l	d0
	bmi.b	.exit
	beq.b	.exit

	move.l	.number(pc),d1

	cmp.l	#100,d1
	bhi.b	.again

.exit
	rts

.text1	dc.b	"Go to %",0
.text2	dc.b	"Min: 0 -- Max: 100"
.number	dc.l	0

;ut:
* d0	-1	avsluta Textread
* d0	0	allt ok, byt inte rad
* d0	1	allt ok, byt rad
* d1	tal
requestline:
.again
	move.l	lines,d0
	sub.l	cheight,d0
	bpl.b	.ok
	moveq	#0,d0
.ok
	lea	.max(pc),a0
	move.l	d0,(a0)
	jsr	numdec
	move.l	d0,a0
	lea	.text2n(pc),a1
.loop
	move.b	(a0)+,(a1)+
	bne.b	.loop

	lea	.linepos(pc),a0
	move.l	linepos,(a0)
	lea	.text1(pc),a1
	lea	.text2(pc),a2

	bsr	requestinteger
	tst.l	d0
	bmi.b	.exit
	beq.b	.exit
	move.l	.linepos(pc),d1
	cmp.l	.max(pc),d1
	bhi.b	.again
.exit
	rts
.max		dc.l	0
.linepos	dc.l	0
.text1	dc.b	"Go to line",0
.text2	dc.b	"Min: 0 -- Max: "
.text2n	dc.b	"xxxxxxxx",0
	even
;in:
* a0	default number
* a1	text1
* a2	text2
;ut:
* d0	-1	avsluta Textread
* d0	0	allt ok, byt inte rad
* d0	1	allt ok, byt rad
requestinteger:
	lea	.init_number(pc),a3
	move.l	(a0),(a3)
	lea	.number(pc),a3
	move.l	a0,(a3)
	lea	.init_text1(pc),a3
	move.l	a1,(a3)
	lea	.init_text2(pc),a3
	move.l	a2,(a3)

	move.l	screenptr,a1
	bsr	OpenintegerreqWindow
	tst.l	d0
	bne.b	.error
	jsr	lockwindow

	BASE	gt
	move.l	integerreqGadgets+GD_iw_integer*4(pc),a0
	move.l	integerreq(pc),a1
	suba.l	a2,a2
	lea	.init_number_tags(pc),a3
	call	GT_SetGadgetAttrsA

	move.l	integerreqGadgets+GD_iw_text1*4(pc),a0
	move.l	integerreq(pc),a1
	suba.l	a2,a2
	lea	.init_text1_tags(pc),a3
	call	GT_SetGadgetAttrsA

	move.l	integerreqGadgets+GD_iw_text2*4(pc),a0
	move.l	integerreq(pc),a1
	suba.l	a2,a2
	lea	.init_text2_tags(pc),a3
	call	GT_SetGadgetAttrsA

	BASE	intui
	move.l	integerreqGadgets+GD_iw_integer*4(pc),a0
	move.l	integerreq(pc),a1
	suba.l	a2,a2
	call	ActivateGadget

	lea	.strukt(pc),a0
	move.l	integerreq(pc),(a0)
	bsr	handlerequestwindow
	move.l	d0,-(sp)
	jsr	unlockwindow
	bsr	CloseintegerreqWindow
	move.l	(sp)+,d0
	rts
.error
	moveq	#0,d0
	rts
.number
	dc.l	0
.strukt
	dc.l	0
	dc.l	.render
	dc.l	.strukt_v
	dc.l	.strukt_r
	dc.l	.strukt_g

.strukt_v
	HRWv	"u",.fn_use
	HRWv	$0d,.fn_use
	HRWv	"c",.fn_cancel
	HRWv	$1b,.fn_cancel
	HRWv	-1,-1
.strukt_r
	HRWr	-1,-1,-1
.strukt_g
	HRWg	GD_iw_use,.fn_usegad
	HRWg	GD_iw_cancel,.fn_cancelgad
	HRWg	GD_iw_integer,.fn_integer
	HRWg	-1,-1

.render
;	bsr	integerreqRender
	rts

.fn_use
	move.l	hrw_window(a5),a2
	move.l	integerreqGadgets+GD_iw_use*4(pc),a3
	jsr	PressButton
.fn_usegad
	move.l	.number(pc),a1
	move.l	integerreqGadgets+GD_iw_integer*4(pc),a0
	move.l	gg_SpecialInfo(a0),a0
	move.l	si_LongInt(a0),(a1)
	moveq	#1+1,d0
	rts
.fn_cancel
	move.l	hrw_window(a5),a2
	move.l	integerreqGadgets+GD_iw_cancel*4(pc),a3
	jsr	PressButton
.fn_cancelgad
	moveq	#0+1,d0
	rts
.fn_integer
	and.w	#IEQUALIFIERB_LSHIFT!IEQUALIFIERB_RSHIFT,d4
	beq.w	.fn_use
	moveq	#0,d0
	rts

.init_number_tags
	dc.l	GTIN_Number
.init_number
	dc.l	0
	dc.l	TAG_DONE

.init_text1_tags
	dc.l	GTTX_Text
.init_text1
	dc.l	0
	dc.l	TAG_DONE

.init_text2_tags
	dc.l	GTTX_Text
.init_text2
	dc.l	0
	dc.l	TAG_DONE

;in:
* a0	struktur
handlerequestwindow
	move.l	a0,a5
.handlerequesterwindow_loop

	clr.w	.status
	base	exec
	move.l	hrw_window(a5),a0
	move.l	wd_UserPort(a0),a0
	move.b	MP_SIGBIT(a0),d1
	moveq	#0,d0
	bset	d1,d0
	lea	.usersignal(pc),a0
	move.l	d0,(a0)

	bset	#SIGBREAKB_CTRL_C,d0

	call	Wait
	lea	.signal(pc),a0
	move.l	d0,(a0)
	move.l	d0,d1
	and.l	.usersignal(pc),d1	;signalerade userport?
	beq.W	.nouserport
					;joo..
.handleIDCMP
	BASE	gt
	move.l	hrw_window(a5),a0
	move.l	wd_UserPort(a0),a0	;userport
	call	GT_GetIMsg
	tst.l	d0
	beq.w	.nomoremsgs
	move.l	d0,a0

	move.l	im_Class(a0),d2

	move.l	im_IAddress(a0),a2
	moveq	#0,d3
	move.w	im_Code(a0),d3
	move.w	im_Qualifier(a0),d4
	move.l	im_Seconds(a0),d5
	move.l	im_Micros(a0),d6

	move.l	a0,a1
	call	GT_ReplyIMsg

	cmp.l	#IDCMP_VANILLAKEY,d2
	bne.w	.dontcheckVkeys
	move.l	hrw_vkeys(a5),a0
.Vkeys_loop
	move.w	(a0),d0
	cmp.w	#-1,d0
	beq.b	.handleIDCMP
	cmp.w	d3,d0
	bne.b	.Vkeys_nope
	move.l	2(a0),a0
	jsr	(a0)
	bra.w	.fn_return
.Vkeys_nope
	add.w	#HWRv_SIZEOF,a0
	bra.b	.Vkeys_loop

.dontcheckVkeys
	cmp.l	#IDCMP_RAWKEY,d2
	bne.w	.dontcheckRkeys

	move.l	hrw_rkeys(a5),a0
.Rkeys_loop
	move.w	(a0),d0
	beq.b	.Rkeys_testcode
	cmp.w	#-1,d0
	beq.b	.handleIDCMP
	and.w	d4,d0
	beq.b	.Rkeys_nope
.Rkeys_testcode
	cmp.w	2(a0),d3
	bne.b	.Rkeys_nope
	move.l	4(a0),a0
	jsr	(a0)
	bra.b	.fn_return
.Rkeys_nope
	add.w	#HWRr_SIZEOF,a0
	bra.b	.Rkeys_loop

.dontcheckRkeys
	cmp.l	#IDCMP_GADGETUP,d2
	bne.w	.dontpressgadgets
	move.w	gg_GadgetID(a2),d1

	move.l	hrw_gadgets(a5),a0
.gad_loop
	move.w	(a0),d0
	cmp.w	#-1,d0
	beq.w	.handleIDCMP
	cmp.w	d1,d0
	bne.b	.gad_nope
	move.l	2(a0),a0
	jsr	(a0)
	bra.b	.fn_return
.gad_nope
	add.w	#HWRg_SIZEOF,a0
	bra.b	.gad_loop

.dontpressgadgets
	cmp.l	#IDCMP_REFRESHWINDOW,d2
	bne.s	.dontrefreshwindow
	BASE	gt
	move.l	hrw_window(a5),a0
	call	GT_BeginRefresh

	move.l	hrw_render(a5),a0
	jsr	(a0)

	BASE	gt
	move.l	hrw_window(a5),a0
	moveq	#TRUE,d0
	call	GT_EndRefresh
.dontrefreshwindow
	bra.w	.handleIDCMP
.fn_return
	move.w	d0,.status
.nomoremsgs
	tst.w	.status
	bne.s	.exit

.nouserport
	move.l	.signal(pc),d1
	and.l	#SIGBREAKF_CTRL_C,d1
	beq.B	.nobreak
	lea	status,a0
	bset	#s_exit,(a0)		;om ctrl_c från cli, sluta direkt!
	moveq	#err_exitnow,d0
	rts
.nobreak
	bra.w	.handlerequesterwindow_loop
.exit
	moveq	#0,d0
	move.w	.status(pc),d0
	subq.l	#1,d0
	rts
.signal		dc.l	0
.usersignal	dc.l	0
.status		dc.w	0





printfile:
	tst.l	filelen
	beq.w	.exit

	BASE	intui
	move.l	windowptr,a0
	move.l	#-1,a1
	lea	.printing(pc),a2
	call	SetWindowTitles

	BASE	exec
	call	CreateMsgPort
	lea	.port(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,a0
;	ifgt	IOSTD_SIZE,iopcr_SIZEOF
	moveq	#IOSTD_SIZE,d0
;	else
;	moveq	#iopcr_SIZEOF,d0
;	endif
	call	CreateIORequest		;union IOStdReq och IOPrtCmdReq
	lea	mpio(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,a1
	move.l	unit,d0			;skrivarenhet
	lea	.printer(pc),a0
	moveq	#0,d1
	call	OpenDevice
	lea	.deverr(pc),a0
	move.l	d0,(a0)
	bne.w	.error

;	call	Forbid			;ingen multitasking nu
	lea	tabsize,a0
	move.l	(a0),-(sp)
	move.l	mptab,(a0)
	lea	printtabbar,a0
	jsr	fixatabbar
	lea	tabsize,a0
	move.l	(sp)+,(a0)
;	base	exec
;	call	Permit

	move.l	mpio(pc),a0
	move.l	IO_DEVICE(a0),a0
	lea	pd_Preferences(a0),a0
	move.w	pf_PrintLeftMargin(a0),d0
;	subq.w	#1,d0
	move.w	pf_PrintRightMargin(a0),d1
	sub.w	d0,d1
	lea	.prwidth(pc),a1
	move.w	d1,(a1)
	beq.w	.error			;inga tecken/rad alls?!
	move.w	pf_PaperLength(a0),d0
	lea	.prheight(pc),a1
	move.w	d0,(a1)
	lea	mppage,a0
	clr.w	(a0)

	base	exec
	move.l	mpio(pc),a1
	lea	temp,a0
	move.l	a0,IO_DATA(a1)
	move.w	#PRD_QUERY,IO_COMMAND(a1)
	call	DoIO
	tst.l	d0
	bne.w	.error
	move.l	mpio(pc),a1
	cmp.l	#1,IO_ACTUAL(a1)
	bne.b	.notpar
	move.b	temp,d0
	btst	#0,d0
	bne.w	.offline	;offline
	btst	#1,d0
	bne.w	.nopaper	;paper out
.notpar
	lea	.prheight(pc),a1
	move.b	mpinfo,d0
	beq.b	.ingetuppe
	subq.w	#2,(a1)
.ingetuppe
	move.w	#aRIN,d0
	bsr.w	mprintcommand
	bne.w	.error
	move.l	fileaddr,a2
	move.l	filelen,a3
	add.l	a2,a3
.nextpage
	lea	mppage,a0
	addq.w	#1,(a0)

	move.l	a2,a4
	move.l	a3,a5
	move.b	mpinfo,d0
	beq.b	.tileprinted
	lea	mpheader,a2
	move.b	(a2)+,d0
	bsr.w	mprintstyle
	tst.l	d0
	bne.w	.error

	bsr.w	mpgettitfoot
	BASE	exec
	move.l	mpio(pc),a1
	move.l	#-1,IO_LENGTH(a1)
	move.l	a0,IO_DATA(a1)
	move.w	#CMD_WRITE,IO_COMMAND(a1)
	call	DoIO
	tst.l	d0
	bne.w	.error
	move.w	#aNEL,d0
	bsr.w	mprintcommand
	bne.w	.error
	move.w	#aNEL,d0
	bsr.w	mprintcommand
	bne.w	.error
.tileprinted
	move.w	#aSGR0,d0
	bsr.w	mprintcommand
	bne.w	.error
	move.l	a4,a2
	move.l	a5,a3
	move.w	.prheight(pc),d7
.nextline
	suba.l	a1,a1			;tala om att början på rad!

	lea	temp,a0
	move.w	.prwidth(pc),d1
	moveq	#0,d2
.nextchar
	moveq	#0,d0
	move.b	(a2)+,d0
	move.b	d0,(a0)+
	cmp.l	a2,a3
	beq.w	.textslut
	cmp.b	#LF,d0
	beq.b	.printline
	cmp.b	#12,d0
	bne.b	.not12
	moveq	#0,d7			;sid-slut
	bra.b	.printline
.not12
	cmp.b	#" ",d0
	bne.b	.notSPACE
	move.l	a0,a1
	move.l	a2,a5
.notSPACE
	cmp.b	#TAB,d0
	bne.b	.notTAB
	move.l	a0,a1
	move.l	a2,a5
	lea	printtabbar,a4
	moveq	#0,d0
	move.b	(a4,d2.w),d0
	sub.w	d2,d0
	move.b	(a4,d2.w),d2
	subq.w	#1,d0
	subq.l	#1,a0			;bort med tab!
.fillspace
	move.b	#" ",(a0)+
	dbf	d0,.fillspace
	bra.b	.charfinished
.notTAB
	cmp.b	#" ",d0
	blo.b	.nonascii
	cmp.b	#$80,d0
	blo.b	.ascii
	cmp.b	#$a0,d0
	bhs.b	.ascii
.nonascii
	move.b	#".",-1(a0)
.ascii
	addq.w	#1,d2
.charfinished
	cmp.w	d1,d2
	blo.B	.nextchar
	sub.w	d1,d2			;om man har gått över gränsen(=tab)
	sub.w	d2,a0			;klipp av !
	move.b	mpww,d0
	beq.b	.noww
	move.l	a1,d0
	beq.b	.noww
	move.l	a1,a0
	subq.l	#1,a0			;ta bort spacet oxå!
	move.l	a5,a2
.noww
	move.b	#10,(a0)+
.printline
	clr.b	(a0)
	move.l	mpio(pc),a1
	move.l	#-1,IO_LENGTH(a1)
	lea	temp,a0
	move.l	a0,IO_DATA(a1)
	move.w	#CMD_WRITE,IO_COMMAND(a1)
	call	DoIO
	tst.l	d0
	bne.B	.error
	dbf	d7,.nextline		;<-
	bra.w	.nextpage
.textslut
	clr.b	(a0)
	move.l	mpio(pc),a1
	move.l	#-1,IO_LENGTH(a1)
	lea	temp,a0
	move.l	a0,IO_DATA(a1)
	move.w	#CMD_WRITE,IO_COMMAND(a1)
	call	DoIO
	move.l	mpio(pc),a1
	move.l	#-1,IO_LENGTH(a1)
	lea	.cls,a0
	move.l	a0,IO_DATA(a1)
	move.w	#CMD_WRITE,IO_COMMAND(a1)
	call	DoIO
	move.l	mpio(pc),a1
	move.w	#CMD_FLUSH,IO_COMMAND(a1)
	call	DoIO
	move.l	mpio(pc),a1
	call	AbortIO
	move.l	mpio(pc),a1
	call	WaitIO
.error
	BASE	exec
	move.l	mpio(pc),d0
	beq.B	.noio
	move.l	.deverr(pc),d0
	bne.b	.wasdeverr
	move.l	mpio(pc),a1
	call	CloseDevice
	lea	.deverr(pc),a0
	clr.l	(a0)
.wasdeverr
	move.l	mpio(pc),a0
	call	DeleteIORequest
	lea	mpio(pc),a0
	clr.l	(a0)
.noio
	move.l	.port(pc),d0
	beq.B	.noport
	move.l	d0,a0
	call	DeleteMsgPort
	lea	.port(pc),a0
	clr.l	(a0)
.noport
	BASE	intui
	move.l	windowptr,a0
	move.l	#-1,a1
	lea	screentitle,a2
	call	SetWindowTitles
.exit
	moveq	#0,d0
	rts
.offline
	bsr.b	.error
	lea	.offlinemessage(pc),a1
	lea	oktext,a2
	suba.l	a4,a4
	jsr	requester
	moveq	#0,d0
	rts
.nopaper
	bsr.b	.error
	lea	.nopapermessage(pc),a1
	lea	oktext,a2
	suba.l	a4,a4
	jsr	requester
	moveq	#0,d0
	rts
.offlinemessage
	dc.b	"Printer is not on-line.",0
.nopapermessage
	dc.b	"Printer is out of paper.",0
	even

.prwidth	dc.w	0
.prheight	dc.w	0
.port		dc.l	0
.deverr		dc.l	0
.printer	dc.b	"printer.device",0
.printing	dc.b	"Printing file...",0
.cls		dc.b	12,0
	even
mpio:		dc.l	0

mprintstyle:
	move.w	d0,d2
	move.w	#aSGR0,d0
	bsr.B	mprintcommand
	bne.B	.error
	btst	#0,d2
	beq.B	.nobold
	move.w	#aSGR1,d0		;fet
	bsr.B	mprintcommand
	bne.B	.error
.nobold
	btst	#1,d2
	beq.B	.noital
	move.w	#aSGR3,d0		;kursiv
	bsr.B	mprintcommand
	bne.B	.error
.noital
	btst	#2,d2
	beq.B	.nounder
	move.w	#aSGR4,d0		;understryk
	bsr.B	mprintcommand
	bne.B	.error
.nounder
	moveq	#0,d0
.error
	rts
;in:
* d0	kommando
;ut:
* Z-flaggan 0 om fel (d0<>0)
mprintcommand:
	BASE	exec
	move.l	mpio(pc),a1
	move.w	d0,io_PrtCommand(a1)
	clr.l	io_Parm0(a1)		;0-3
	move.w	#PRD_PRTCOMMAND,IO_COMMAND(a1)
	call	DoIO
	tst.l	d0
	rts
;in:
* a2	pek till tre bytar
;ut:
* a0	sträng att skriva ut,0
mpgettitfoot:
	lea	temp+256,a0
	moveq	#79,d0
.fill
	move.b	#" ",(a0)+
	dbf	d0,.fill
	clr.b	(a0)

	move.b	(a2)+,d0
	bsr.B	.retright
	tst.l	d0
	beq.b	.next1
	lea	temp+256,a1
.copy1
	move.b	(a0)+,(a1)+
	bne.b	.copy1
	move.b	#" ",-(a1)
.next1
	move.b	(a2)+,d0
	bsr.B	.retright
	tst.l	d0
	beq.b	.next2
	lsr.l	#1,d0
	lea	temp+256+40,a1
	sub.l	d0,a1
.copy2
	move.b	(a0)+,(a1)+
	bne.b	.copy2
	move.b	#" ",-(a1)
.next2
	move.b	(a2)+,d0
	bsr.B	.retright
	lea	temp+256+80,a1
	tst.l	d0
	beq.b	.next3
	sub.l	d0,a1
.copy3
	move.b	(a0)+,(a1)+
	bne.b	.copy3
.next3
	lea	temp+256,a0
	rts
.retright
	cmp.b	#1,d0
	bne.B	.notfilename
	lea	filename,a0
	moveq	#30,d0			;längd
	rts
.notfilename
	cmp.b	#2,d0
	bne.B	.nottitle
	lea	mptitle,a0
	moveq	#30,d0			;längd
	rts
.nottitle
	cmp.b	#3,d0
	bne.B	.notdate
	lea	.datetime(pc),a0
	move.b	#FORMAT_INT,dat_Format(a0)
	lea	temp,a1
	move.l	a1,dat_StrDate(a0)
	BASE	dos
	move.l	a0,d1
	call	DateStamp
	move.l	d0,d1
	call	DateToStr
	tst.l	d0
	beq.b	.rrexit
	moveq	#-1,d0
	lea	temp,a0
.findnull
	addq.l	#1,d0
	tst.b	(a0)+
	bne.b	.findnull
	lea	temp,a0
	rts
.notdate
	cmp.b	#4,d0
	bne.B	.notsidnum
	moveq	#0,d0
	move.w	mppage,d0
	jsr	numdec
	lea	temp,a1
	move.l	d0,a0
	moveq	#3,d0
	move.b	#"-",(a1)+
	move.b	#" ",(a1)+
.copyside
	addq.w	#1,d0
	move.b	(a0)+,(a1)+
	bne.b	.copyside
	move.b	#" ",-1(a1)
	move.b	#"-",(a1)+
	clr.b	(a1)
	lea	temp,a0
	rts
.notsidnum
.rrexit
	moveq	#0,d0
	rts	
.datetime
	blk.b	dat_SIZEOF
printoptions:
	lea	pol,a0
	lea	.l2(pc),a1
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+

	lea	.leftactive(pc),a0
	move.l	pol,(a0)
	lea	.midactive(pc),a0
	move.l	pom,(a0)
	lea	.rightactive(pc),a0
	move.l	por,(a0)
	lea	.wwon(pc),a0
	move.l	pow,(a0)
	lea	.tabsize(pc),a0
	move.l	mptab,(a0)
	lea	.boldon(pc),a0
	move.l	pob,(a0)
	lea	.italon(pc),a0
	move.l	poi,(a0)
	lea	.underon(pc),a0
	move.l	pou,(a0)

	move.l	cwidthp,d0
	sub.l	#428,d0
	lsr.l	#1,d0
	lea	.pwinx(pc),a0
	move.l	d0,(a0)
	move.l	pheight,d0
	sub.l	#142,d0
	lsr.l	#1,d0
	lea	.pwiny(pc),a0
	move.l	d0,(a0)

	BASE	intui
	lea	.pwindowonscreen(pc),a0
	move.l	screenptr,(a0)
	suba.l	a0,a0
	lea	.pwindowtags(pc),a1
	call	OpenWindowTagList
	lea	.pwindowptr(pc),a0
	move.l	d0,(a0)
	beq.w	.error

	BASE	gt
	lea	.pgadgetptr(pc),a0
	call	CreateContext
	move.l	d0,d7

	lea	.pnewgadget(pc),a3
	move.w	#20,gng_LeftEdge(a3)
	move.w	#30,gng_TopEdge(a3)
	clr.w	gng_Width(a3)
	clr.w	gng_Height(a3)
	clr.l	gng_GadgetText(a3)
	lea	topaz8font,a0
	move.l	a0,gng_TextAttr(a3)
	move.l	#PLACETEXT_RIGHT,gng_Flags(a3)
	move.l	screenvi,gng_VisualInfo(a3)
	clr.w	gng_GadgetID(a3)		;start noll
	clr.l	gng_UserData(a3)

	move.l	d7,a0
	move.l	a3,a1
	lea	.lefttags(pc),a2
	move.l	#MX_KIND,d0
	call	CreateGadgetA
	lea	.leftgadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	add.w	#150,gng_LeftEdge(a3)

	move.l	d7,a0
	move.l	a3,a1
	lea	.midtags(pc),a2
	move.l	#MX_KIND,d0
	call	CreateGadgetA
	lea	.midgadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	add.w	#150,gng_LeftEdge(a3)

	move.l	d7,a0
	move.l	a3,a1
	lea	.righttags(pc),a2
	move.l	#MX_KIND,d0
	call	CreateGadgetA
	lea	.rightgadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	move.w	#105,gng_TopEdge(a3)
	move.w	#170,gng_LeftEdge(a3)
	lea	.ww(pc),a0
	move.l	a0,gng_GadgetText(a3)

	move.l	d7,a0
	move.l	a3,a1
	lea	.wwtags(pc),a2
	move.l	#CHECKBOX_KIND,d0
	call	CreateGadgetA
	lea	.wwgadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	move.w	#125,gng_TopEdge(a3)
	move.w	#20,gng_LeftEdge(a3)
	move.w	#12,gng_Height(a3)
	move.w	#60,gng_Width(a3)
	move.l	#PLACETEXT_IN,gng_Flags(a3)
	lea	.print(pc),a0
	move.l	a0,gng_GadgetText(a3)
	
	move.l	d7,a0
	move.l	a3,a1
	lea	tag_done,a2
	move.l	#BUTTON_KIND,d0
	call	CreateGadgetA
	lea	.printgadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	move.w	#185,gng_LeftEdge(a3)
	lea	.use(pc),a0
	move.l	a0,gng_GadgetText(a3)
	
	move.l	d7,a0
	move.l	a3,a1
	lea	tag_done,a2
	move.l	#BUTTON_KIND,d0
	call	CreateGadgetA
	lea	.usegadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	move.w	#348,gng_LeftEdge(a3)
	lea	.cancel(pc),a0
	move.l	a0,gng_GadgetText(a3)
	
	move.l	d7,a0
	move.l	a3,a1
	lea	tag_done,a2
	move.l	#BUTTON_KIND,d0
	call	CreateGadgetA
	lea	.cancelgadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	move.w	#90,gng_TopEdge(a3)
	move.w	#68,gng_LeftEdge(a3)
	move.w	#261,gng_Width(a3)
	move.l	#PLACETEXT_LEFT,gng_Flags(a3)
	lea	.title(pc),a0
	move.l	a0,gng_GadgetText(a3)

	move.l	d7,a0
	move.l	a3,a1
	lea	.titletags(pc),a2
	move.l	#STRING_KIND,d0
	call	CreateGadgetA
	lea	.titlegadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	move.w	#105,gng_TopEdge(a3)
	move.w	#92,gng_LeftEdge(a3)
	move.w	#37,gng_Width(a3)
	lea	.tab(pc),a0
	move.l	a0,gng_GadgetText(a3)

	move.l	d7,a0
	move.l	a3,a1
	lea	.tabtags(pc),a2
	move.l	#INTEGER_KIND,d0
	call	CreateGadgetA
	lea	.tabgadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	move.w	#75,gng_TopEdge(a3)
	addq.w	#1,gng_GadgetID(a3)
	move.w	#20,gng_LeftEdge(a3)
	move.l	#PLACETEXT_RIGHT,gng_Flags(a3)
	lea	.bold(pc),a0
	move.l	a0,gng_GadgetText(a3)

	move.l	d7,a0
	move.l	a3,a1
	lea	.boldtags(pc),a2
	move.l	#CHECKBOX_KIND,d0
	call	CreateGadgetA
	lea	.boldgadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	add.w	#150,gng_LeftEdge(a3)
	lea	.under(pc),a0
	move.l	a0,gng_GadgetText(a3)

	move.l	d7,a0
	move.l	a3,a1
	lea	.undertags(pc),a2
	move.l	#CHECKBOX_KIND,d0
	call	CreateGadgetA
	lea	.undergadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	addq.w	#1,gng_GadgetID(a3)
	add.w	#150,gng_LeftEdge(a3)
	lea	.ital(pc),a0
	move.l	a0,gng_GadgetText(a3)

	move.l	d7,a0
	move.l	a3,a1
	lea	.italtags(pc),a2
	move.l	#CHECKBOX_KIND,d0
	call	CreateGadgetA
	lea	.italgadget(pc),a0
	move.l	d0,(a0)
	beq.w	.error
	move.l	d0,d7

	lea.l	filelen,a0
	tst.l	(a0)
	bne.b	.textinmem
	move.l	.printgadget(pc),a0
	or.w	#GFLG_DISABLED,gg_Flags(a0)
.textinmem

	move.l	.titlegadget(pc),a0
	move.l	.pwindowptr(pc),a1
	suba.l	a2,a2
	BASE	intui
	cmp.l	#2,pol
	beq.s	.aa
	call	OffGadget
	bra.S	.ba
.aa
	call	OnGadget
.ba

	BASE	intui
	move.l	.pwindowptr(pc),a0
	move.l	wd_RPort(a0),a0
	moveq	#0,d0
	moveq	#0,d1
	lea	.windowtext(pc),a1
	call	PrintIText

	move.l	.pwindowptr(pc),a0
	move.l	.pgadgetptr(pc),a1
	suba.l	a2,a2
	moveq	#0,d0
	moveq	#-1,d1
	call	AddGList

	move.l	.pwindowptr(pc),a1
	move.l	.pgadgetptr(pc),a0
	suba.l	a2,a2
	moveq	#-1,d0
	call	RefreshGList
	BASE	gt
	move.l	.pwindowptr(pc),a0
	suba.l	a1,a1
	call	GT_RefreshWindow
.wait
	BASE	exec
	move.l	.pwindowptr(pc),a0
	move.l	wd_UserPort(a0),a0
	call	WaitPort
.nextmsg
	BASE	gt
	move.l	.pwindowptr(pc),a0
	move.l	wd_UserPort(a0),a0
	call	GT_GetIMsg
	tst.l	d0
	beq.b	.wait

	moveq	#0,d3
	moveq	#0,d4
	move.l	d0,a0
	move.l	im_IAddress(a0),a2
	move.l	im_Class(a0),d2
	move.w	im_Code(a0),d3
	move.w	im_Qualifier(a0),d4
	move.l	d0,a1
	call	GT_ReplyIMsg
	cmp.l	#IDCMP_GADGETUP,d2
	bne.w	.notup
	cmp.w	#3,gg_GadgetID(a2)
	bne.b	.not3
	lea	pow+3,a0
	bchg	#0,(a0)			;TRUE/FALSE
.not3
	cmp.w	#4,gg_GadgetID(a2)
	bne.b	.not4
	bsr.w	.error			;stäng fönster
	bsr.w	.fixset
	moveq	#1,d0
	rts
.not4
	cmp.w	#5,gg_GadgetID(a2)
	bne.b	.not5
	bsr.w	.error			;stäng fönster
	bra.w	.fixset
.not5
	cmp.w	#6,gg_GadgetID(a2)
	bne.b	.not6
	bsr.w	.error			;stäng fönster
	bra.w	.resset
.not6
	cmp.w	#9,gg_GadgetID(a2)
	bne.b	.not9
	lea	pob+3,a0
	bchg	#0,(a0)			;TRUE/FALSE
.not9
	cmp.w	#10,gg_GadgetID(a2)
	bne.b	.not10
	lea	pou+3,a0
	bchg	#0,(a0)			;TRUE/FALSE
.not10
	cmp.w	#11,gg_GadgetID(a2)
	bne.b	.not11
	lea	poi+3,a0
	bchg	#0,(a0)			;TRUE/FALSE
.not11
	bra.w	.nextmsg
.notup
	cmp.l	#IDCMP_GADGETDOWN,d2
	bne.b	.notdown
	tst.w	gg_GadgetID(a2)
	bne.b	.not0
	lea	pol,a0
	move.l	d3,(a0)

	move.l	.titlegadget(pc),a0
	move.l	.pwindowptr(pc),a1
	suba.l	a2,a2
	BASE	intui
	cmp.l	#2,d3
	beq.s	.a
	call	OffGadget
	bra.S	.b
.a
	call	OnGadget
.b
	bra.w	.nextmsg
.not0
	cmp.w	#1,gg_GadgetID(a2)
	bne.b	.not1
	lea	pom,a0
	move.l	d3,(a0)
	bra.w	.nextmsg
.not1
	cmp.w	#2,gg_GadgetID(a2)
	bne.b	.not2
	lea	por,a0
	move.l	d3,(a0)
.not2
	bra.w	.nextmsg
.notdown
	cmp.l	#IDCMP_REFRESHWINDOW,d2
	bne.w	.nextmsg
	BASE	gt
	move.l	.pwindowptr(pc),a0
	call	GT_BeginRefresh
	move.l	.pwindowptr(pc),a0
	moveq	#TRUE,d0
	call	GT_EndRefresh
	bra.w	.nextmsg
.error
	BASE	exec
	call	Forbid
	BASE	gt
.nextmsg1
.nextmsg2
	move.l	.pwindowptr(pc),a0
	move.l	wd_UserPort(a0),a0
	call	GT_GetIMsg
	tst.l	d0
	beq.b	.nullmsg2
	move.l	d0,a1
	call	GT_ReplyIMsg
	bra.b	.nextmsg2
.nullmsg2
	BASE	exec
	call	Permit

	BASE	intui
	move.l	.pwindowptr(pc),d0
	beq.b	.nowindow
	move.l	d0,a0
	call	CloseWindow
.nowindow
	lea	.pwindowptr(pc),a0
	clr.l	(a0)

	BASE	gt
	lea	.pgadgetptr(pc),a2
	move.l	(a2),a0
	call	FreeGadgets
	clr.l	(a2)

	moveq	#0,d0
	rts
.fixset
	moveq	#0,d0
	lea	mpheader+1,a0
	move.b	pol+3,(a0)+
	beq.b	.olle1
	addq	#1,d0
.olle1
	move.b	pom+3,(a0)+
	beq.b	.olle2
	addq.b	#2,-1(a0)
	addq	#1,d0
.olle2
	move.b	por+3,(a0)+
	beq.b	.olle3
	addq.b	#2,-1(a0)
	addq	#1,d0
.olle3
	lea	mpinfo,a0
	move.b	#1,(a0)
	tst.w	d0
	bne.b	.olle4
	clr.b	(a0)
.olle4
	lea	mpww,a0
	move.b	pow+3,(a0)
	lea	pob,a0
	move.l	(a0)+,d1
	move.l	(a0)+,d0
	lsl.w	#1,d0
	or.w	d0,d1
	move.l	(a0)+,d0
	lsl.w	#2,d0
	or.w	d0,d1
	lea	mpheader,a0
	move.b	d1,(a0)
	move.l	.titlegadget(pc),a0
	move.l	gg_SpecialInfo(a0),a0
	move.l	si_Buffer(a0),a0
	lea	mptitle,a1
	moveq	#29+1,d0		;nollan oxå!
.copytitle
	move.b	(a0)+,(a1)+
	dbeq	d0,.copytitle
	move.l	.tabgadget(pc),a0
	move.l	gg_SpecialInfo(a0),a0
	move.l	si_LongInt(a0),d0
	lea	mptab,a0
	cmp.l	#16,d0
	bls.b	.tsok
	moveq	#16,d0
.tsok
	move.l	d0,(a0)
	moveq	#0,d0
	rts
.resset
	lea	.l2(pc),a0
	lea	pol,a1
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	moveq	#0,d0
	rts
.pnewgadget	blk.b	gng_SIZEOF,0
.pwindowptr	dc.l	0
.pwindowtags
	dc.l	WA_Left
.pwinx	dc.l	50
	dc.l	WA_Top
.pwiny	dc.l	25
	dc.l	WA_Width,428
	dc.l	WA_Height,142
	dc.l	WA_CustomScreen
.pwindowonscreen
	dc.l	0
	dc.l	WA_SmartRefresh,TRUE
	dc.l	WA_Activate,TRUE
	dc.l	WA_IDCMP,IDCMP_REFRESHWINDOW!MXIDCMP!BUTTONIDCMP!CHECKBOXIDCMP!INTEGERIDCMP!STRINGIDCMP
	dc.l	TAG_DONE

.lefttags
	dc.l	GTMX_Labels,.leftlabels
	dc.l	GTMX_Spacing,2
	dc.l	GTMX_Active
.leftactive
	dc.l	0
	dc.l	TAG_DONE
.leftlabels
	dc.l	.none
	dc.l	.filename
	dc.l	.title
	dc.l	0
.midtags
	dc.l	GTMX_Labels,.midlabels
	dc.l	GTMX_Spacing,2
	dc.l	GTMX_Active
.midactive
	dc.l	0
	dc.l	TAG_DONE
.midlabels
	dc.l	.none
	dc.l	.date
	dc.l	.pagenum
	dc.l	0
.righttags
	dc.l	GTMX_Labels,.rightlabels
	dc.l	GTMX_Spacing,2
	dc.l	GTMX_Active
.rightactive
	dc.l	0
	dc.l	TAG_DONE
.rightlabels
	dc.l	.none
	dc.l	.date
	dc.l	.pagenum
	dc.l	0
.wwtags
	dc.l	GTCB_Checked
.wwon
	dc.l	0
	dc.l	TAG_DONE
.titletags
	dc.l	GTST_String,mptitle
	dc.l	GTST_MaxChars,30
	dc.l	GA_TabCycle,FALSE
	dc.l	TAG_DONE
.tabtags
	dc.l	GTIN_Number
.tabsize
	dc.l	8
	dc.l	GTIN_MaxChars,2
	dc.l	GA_TabCycle,TRUE
	dc.l	TAG_DONE

.boldtags
	dc.l	GTCB_Checked
.boldon
	dc.l	0
	dc.l	TAG_DONE
.italtags
	dc.l	GTCB_Checked
.italon
	dc.l	0
	dc.l	TAG_DONE
.undertags
	dc.l	GTCB_Checked
.underon
	dc.l	0
	dc.l	TAG_DONE

.pgadgetptr	dc.l	0
.leftgadget	dc.l	0
.midgadget	dc.l	0
.rightgadget	dc.l	0
.wwgadget	dc.l	0
.printgadget	dc.l	0
.usegadget	dc.l	0
.cancelgadget	dc.l	0
.titlegadget	dc.l	0
.tabgadget	dc.l	0
.boldgadget	dc.l	0
.italgadget	dc.l	0
.undergadget	dc.l	0

.none	dc.b	"Nothing",0
.filename dc.b	"Filename",0
.title	dc.b	"Title",0
.date	dc.b	"Date",0
.pagenum dc.b	"Page #",0
.ww	dc.b	"Wrap words",0
.print	dc.b	"Print",0
.use	dc.b	"Use",0
.cancel	dc.b	"Cancel",0
.tab	dc.b	"Tab size",0
.bold	dc.b	"Bold",0
.ital	dc.b	"Italic",0
.under	dc.b	"Underlined",0
	even

.l2	dc.l	0
.m2	dc.l	0
.r2	dc.l	0
.w2	dc.l	0
.b2	dc.l	TRUE
.i2	dc.l	FALSE
.u2	dc.l	TRUE

.windowtext
	dc.b	2,0,RP_JAM2,0
	dc.w	162,5
	dc.l	NULL
	dc.l	.text1
	dc.l	.it2
.it2
	dc.b	1,0,RP_JAM2,0
	dc.w	20,20
	dc.l	NULL
	dc.l	.text2
	dc.l	.it3
.it3
	dc.b	1,0,RP_JAM2,0
	dc.w	170,20
	dc.l	NULL
	dc.l	.text3
	dc.l	.it4
.it4
	dc.b	1,0,RP_JAM2,0
	dc.w	320,20
	dc.l	NULL
	dc.l	.text4
	dc.l	.it5
.it5
	dc.b	1,0,RP_JAM2,0
	dc.w	20,65
	dc.l	NULL
	dc.l	.text5
	dc.l	NULL

.text1	dc.b	"Print options",0
.text2	dc.b	"Left field",0
.text3	dc.b	"Middle field",0
.text4	dc.b	"Right field",0
.text5	dc.b	"Header style",0

	even

	incdir	""
	include	Projekt:Textread/CASE/Textread.s
