set $breakin FALSE

; Startup file for UE V 3.10/Lancaster on VT100 or VT200 terminals
; .rc file V 2.2.5

; **************************** End of Comments ******************************   

write-message "[MicroEMACS/UNIX Initialising (Version 3.10)]"

; ********************************* Macros *********************************

; Macro to toggle function key window

; set function list variable for help facility to detect.
set %fon "yes"

store-procedure toggle-fn-window
	!if %rcfkeys
		!goto rcfoff
	!endif

;	toggle function key window on
	save-window
	1 next-window
	!if &not &sequal $cbufname "Function Keys"
		1 split-current-window
		1 select-buffer "Function Keys"
		!force 5 resize-window
		1 goto-line
	!endif
	set %rcfkeys TRUE
	!force restore-window
	!if &sequal $cbufname "Function Keys"
		next-window
	!endif
	write-message "[Function key window ON]"
	!return

	;Toggle the function key window off
*rcfoff
	save-window
	1 next-window
	!if &sequal "Function Keys" $cbufname
		delete-window
	!endif
	!force restore-window
	write-message "[Function key window OFF]"
	set %rcfkeys FALSE
!endm

; Bind function key window to M-?

macro-to-key toggle-fn-window M-?

; ******************************** Help Macro ********************************

; set up global variables for use in Macro
	set %l$helpon FALSE

; This is called whenever the help window is selected, and re-binds the
; cursor keys to their help-mode operations

store-procedure enter-help-window
	set $discmd FALSE
	set %l$tmpbrk $breakin
	set $breakin FALSE

	!if &sequal "[HELP FILE]" $cbufname
		write-message "[Entering HELP]"
		8 resize-window
		1 redraw-display
		macro-to-key toggle-help ^M 	; return to quit
		macro-to-key help-page-up FNP 	; scroll up a page
		macro-to-key help-page-down FNN ; scroll down a page
		set %l$tmpexbhook $exbhook
		set $exbhook [exit-help-window]
	!endif

	set $discmd TRUE
	set $breakin %l$tmpbrk
!endm

store-procedure exit-help-window
	set $discmd FALSE
	set %l$tmpbrk $breakin
	set $breakin FALSE

	1 redraw-display
	bind-to-key newline	^M  	; redefine return key
	bind-to-key next-line 	FNN 	; reset down arrow key
	bind-to-key previous-line FNP 	; reset up arrow key

	set $exbhook %l$tmpexb

	set $discmd TRUE
	set $breakin %l$tmpbrk
!endm

; Macro to toggle help file on/off. Uses variable %l$helpon.

store-procedure toggle-help
; set breakin on and command display off.
	set $discmd FALSE
	set %l$tmpbrk $breakin
	set $breakin FALSE

; if window on move to old buffer.
	!if &sequal %l$helpon TRUE
		select-buffer %l$oldbuff
		delete-other-windows
		!if %rcfkeys
			execute-procedure toggle-fn-window
			execute-procedure toggle-fn-window
		!endif
		set %l$helpon FALSE
		set $bufhook %l$tmphook

; if window off, put up help info.

	!else
		set %l$oldbuff $cbufname
		delete-other-windows
		help
		goto-line 1
		set %l$tmphook $bufhook
		set $bufhook [enter-help-window]
		set %l$helpon TRUE
		enter-help-window
	!endif

	set $discmd TRUE
	set $breakin %l$tmpbrk
!endm

macro-to-key toggle-help M-H

;	scroll up a page in help
store-procedure help-page-up
	beginning-of-line
	search-reverse "=>"
	1 redraw-display
!endm

;	scroll down a page in help
store-procedure help-page-down
	beginning-of-line
	2 forward-character
	search-forward "=>"
	1 redraw-display
!endm


; Toggle insert/overstrike modes.
store-procedure toggle-insert-mode
	set $cmode &bxor $cmode 32
!endm

; Start-of-next-line function.
store-procedure start-of-next-line
	set %l$tmpbrk $breakin
	set $breakin FALSE

	next-line
	beginning-of-line

	set $breakin %l$tmpbrk
!endm

;	; set up auto CMODE

store-procedure auto-c-mode
	set %rctmp &sin $cfname "."
	!if &equ %rctmp 0
		!return
	!endif
	set %rctmp &mid $cfname &add %rctmp 1 5
	!if &or &seq %rctmp "c" &seq %rctmp "h"
		add-mode "cmode"
		-4 handle-tab
	!endif
	!if &seq %rctmp "sc"
		add-mode "cmode"
		-4 handle-tab
	!endif
	!if &seq %rctmp "mss"
		add-mode "wrap"
	!endif

;	Turn off safe save within elm

	set %cftmp &lef $cfname 13
	!if &seq %cftmp "/temp/tmp/snd"
		set $ssave FALSE
		!if &not &equ %nowrap 1
			add-mode "wrap"
		!endif
	!endif

!endm

; Bind auto-c-mode to read file hook

set $readhook [auto-c-mode]

; Macro 36. Delete the whole of the current line.

store-procedure delete-current-line

	set %l$tmpbrk $breakin
	set $breakin FALSE

	beginning-of-line
	set-mark
	execute-procedure start-of-next-line
	kill-region
	clear-message-line

	set $breakin %l$tmpbrk
!endm

; Macro to delete to beginning of line.
35 store-macro
	set %l$tmpbrk $breakin
	set $breakin FALSE

	set-mark
	beginning-of-line
	kill-region
	clear-message-line

	set $breakin %l$tmpbrk
!endm

; Disable displaying of commands

	set $discmd FALSE


; *** Bind macro functions to unused ^B key combinations:

	macro-to-key	delete-current-line	^B^K ; Del current line
	macro-to-key	toggle-insert-mode	^BO ; Ins/Overstrike Mode
	macro-to-key	start-of-next-line	^B^N ; Move to start of next line

; *** Two changes for compatability with ADM terminals

	bind-to-key	kill-to-end-of-line	^BK
	bind-to-key	clear-and-redraw	^BL

; ***	Additional functions and bindings required for release of 3.10

store-procedure toggle-backup
!if %backupon
	set $backup FALSE
	write-message "[Automatic backup facility disabled]"
	set %backupon FALSE
!else
	set $backup TRUE
	write-message "[Automatic backup facility enabled]"
	set %backupon TRUE
!endif
!endm
macro-to-key toggle-backup ^B^B
store-procedure toggle-ssave
!if %ssaveon
	set $ssave FALSE
	write-message "[ssave facility disabled]"
	set %ssaveon FALSE
!else
	set $ssave TRUE
	write-message "[ssave facility enabled]"
	set %ssaveon TRUE
!endif
!endm
macro-to-key toggle-ssave ^B^U
store-procedure toggle-wrap-mode
set $cmode &bxor $cmode 1
!endm
macro-to-key toggle-wrap-mode ^BW
store-procedure toggle-exact-mode
set $cmode &bxor $cmode 8
!endm
macro-to-key toggle-exact-mode ^BE

; *** Run special set-up for terminal, if present

	set %rctmp &find &cat uesetup_ &env TERM

	!if &seq %rctmp ""
		!if &band $tcflags 64
			set %rctmp &find uesetup_vt
		!else
			set %rctmp &find uesetup_def
		!endif
	!endif

	!if &not &seq %rctmp ""
		execute-file %rctmp
	!else
		write-message "*** Terminal-specific setup file not found ***"
	!endif 

; ************************ End of key bindings ******************************



;	bring up the function key window

	1 split-current-window
	select-buffer "Function Keys"
	5 resize-window
	
	; First look for a specific entry for this terminal

	set %rctmp &find &cat uekeys_ &env TERM

	!if &not &env UEKEYS
		set %rctmp &find uekeys_def
	!endif

	; If not, use one of the defaults - vt100 or general.
	
	!if &seq %rctmp ""
		!if &band $tcflags 64
			set %rctmp &find uekeys_vt
		!else
			set %rctmp &find uekeys_def
		!endif
	!endif

	!if &not &seq %rctmp ""
		read-file %rctmp
	!else
		insert-string "*** Function key labels file not found ***"
	!endif 

	beginning-of-file
	unmark-buffer
	next-window
	set %rcfkeys TRUE

; Set various defaults

	set $backup FALSE	; No backups
	set %ssaveon TRUE	; Ssave is on to begin with
	set $hjump 40		; Horizontal scroll in 40 col jumps	

	!if &band $tcflags 32	
		set $sscroll -4 ; Smooth scrolling if hardware available
	!endif

; Run user's RC file, if present

	!if &not &seq &find ~/.uerc ""
		write-message "[Executing user startup file]"
		execute-file ~/.uerc
	!endif

	set $discmd TRUE
	set $breakin TRUE
	write-message &cat "[MicroEMACS/UNIX (Lancaster version) ready]"
