store-procedure	init-help

	clear-message-line
	set %oldscreen $scrname		;remember the screen we were on
	set %oldmode $modeflag		;remember the original $modeflag
	set $discmd FALSE
	find-screen "EmacsHLP"		;switch to the help screen
	set $modeflag FALSE
	set %done FALSE			;not done with help
	find-file EhelpTxt		;read in the help file!
	sethscreen
	set %helpscreen "Main Menu"	;start at the main menu
	gethscreen
!endm

store-procedure gethscreen	;switch the current help screen

*gethbegin

	;build the window ID line
	1 next-window
	beginning-of-file
	set-mark
	end-of-file
	kill-region
	!if &gre &len $version 5
		insert-string "MicroEMACS version "
	!else
		insert-string "	MicroEMACS version "
	!endif
	insert-string $version
	insert-string "		Help System	"
	insert-string %helpscreen
	beginning-of-line
	unmark-buffer

	;find and display the named helpscreen
	2 next-window
	beginning-of-file
	set %tmp &cat "~n=>" %helpscreen
	!force search-forward %tmp
	!if &seq $status FALSE
		set %helpscreen "Main Menu"
		print "          [Attempt to fetch illegal screen]"
		!goto gethbegin
	!endif
	2 next-line
	beginning-of-line
	1 redraw-display
	update-screen
	set %topline $curline
	set %bottomline &add $curline 20

	firstsel	;position us at the first selection

!endm

store-procedure sethscreen	;set up the windows on the help screen

	find-screen "EmacsHLP"
	delete-other-windows
	beginning-of-file
	split-current-window
	0 resize-window
	add-mode "red"
	add-mode "WHITE"
	select-buffer "Window ID"
	next-window
	split-current-window
	19 resize-window
	add-mode "white"
	add-mode "BLACK"
	next-window
	add-mode "red"
	add-mode "WHITE"
	beginning-of-file

!endm

store-procedure firstsel	;position us at the first selection

	9 set-mark		;remember where we started from
	set %endscreen &add $curline 20
	search-forward "< >"	;look for a selection
	!if &gre $curline %endscreen

		;no selections on this screen!
		9 goto-mark
		set %selflag FALSE
	!else
		2 backward-character
		set %selflag TRUE
		8 set-mark
	!endif

!endm

store-procedure setsel		;highlight the current selection

	!if &seq %selflag TRUE
		delete-next-character
		insert-string "*"
		backward-character
		unmark-buffer
	!endif
!endm

store-procedure clearsel		;clear the current selection

	!if &seq %selflag TRUE
		delete-next-character
		insert-string " "
		backward-character
		unmark-buffer
	!endif
!endm

store-procedure nextcmd

	!if &seq $pending FALSE
		update-screen
	!endif
	set %cmd &gtcmd
	set %cmd &bind %cmd
	clear-message-line

!endm

store-procedure execloop	;execute a user screen

	setsel		;mark the current selection
	nextcmd		;get the command
	clearsel	;clear the selection
	execcmd		;execute the command

!endm

store-procedure execcmd		;execute the current command

	!if &seq %selflag TRUE

		!if &seq %cmd "forward-character"
			forward-selection
			!return
		!endif

		!if &seq %cmd "backward-character"
			backward-selection
			!return
		!endif

		!if &seq %cmd "next-line"
			next-selection
			!return
		!endif

		!if &seq %cmd "previous-line"
			previous-selection
			!return
		!endif

		!if &seq %cmd "newline"
			select-this
			!return
		!endif
	!endif

	!if &seq %cmd "next-page"
		next-hscreen
		!return
	!endif

	!if &seq %cmd "newline"
		next-hscreen
		!return
	!endif

	!if &seq %cmd "previous-page"
		previous-hscreen
		!return
	!endif

	!if &seq %cmd "beginning-of-file"
		set %helpscreen "Main Menu"
		gethscreen
		!return
	!endif

	!if &seq %cmd "exit-emacs"
		exit-help
		!return
	!endif

	print "              [Not a legal command at this time]"

!endm

store-procedure forward-selection

	search-forward "< >"
	!if &gre $curline %bottomline
		set $curline %topline
		search-forward "< >"
	!endif
	2 backward-character
	8 set-mark

!endm

store-procedure backward-selection

	search-reverse "< >"
	!if &less $curline %topline
		set $curline %bottomline
		search-reverse "< >"
	!endif
	1 forward-character
	8 set-mark

!endm

store-procedure next-selection

	set %target $curcol
	forward-selection
	!while &not &equ %target $curcol
		forward-selection
	!endwhile

!endm

store-procedure previous-selection

	set %target $curcol
	backward-selection
	!while &not &equ %target $curcol
		backward-selection
	!endwhile

!endm

store-procedure select-this	;select the currently highlighted selection

	next-word		;jump to the beginning of the selection
	7 set-mark		;mark it so we can come back!
	set-mark
	set %tmpline $curline	;remember what line we are on
	search-forward "< >"	;find the beginning of the next selection
	!if &not &equ %tmpline $curline

		;we are no longer on the same line, select the whole line
		7 goto-mark
		end-of-line
	!else

		;select up to this selection
		4 backward-character
	!endif

	;get the selection
	copy-region
	set %selection &trim $kill
;	print &cat &cat "[selecting :" %selection ":]"
	set %helpscreen %selection
	gethscreen
!endm

store-procedure next-screen

	search-reverse "NEXT["
	set-mark
	search-forward "]"
	backward-character
	copy-region
	set %helpscreen $kill
	gethscreen

!endm

store-procedure next-hscreen

	search-reverse "NEXT["
	5 forward-character
	set-mark
	search-forward "]"
	backward-character
	copy-region
	set %helpscreen $kill
	gethscreen

!endm

store-procedure previous-hscreen

	search-reverse "PREV["
	5 forward-character
	set-mark
	search-forward "]"
	backward-character
	copy-region
	set %helpscreen $kill
	gethscreen

!endm

store-procedure exit-help

	find-screen %oldscreen
	set $modeflag %oldmode
	set $discmd TRUE
	set %done TRUE

!endm

;	Help main program

init-help
!while &not %done
	execloop
!endwhile
