;	EMACS.RC / .emacsrc
;
;   Notes for this fork, from Earl Roosa:
;
;	Startup file for MicroEMACS 3.9 and uEmacs/PK 4.0
;	This file is executed every time the editor is entered.
;	-- IF IT'S IN THE EMACS PATH!!  (see below)
;
;	i named this version of microemacs 'ue' rather than 'em' for
; 	two reasons:  to avoid overwriting and confusing it with the PK
;   program, and to keep my fat fingers from writing 'rm' and blowing 
; 	my file away.  
;
;   i've been editing my code with my own hack of microemacs 3.8g,
;	included in a Decus tape in 1988.  getting it working on the platforms
; 	i needed taught me a lot about C, and even though i didn't work out
; 	all the bugs, i added a lot of features, and probably some more bugs.
;	it compiled on ms-dos, windoze, winnt, vax-vms, alpha-vms, unix,
;	and many generations of linux.  it would barf whenever there was a major
;	migration or system upgrade, and i'd pick over the broken code or shut off
;	an optimization to get it working again.  a couple of linuxes ago, the
;	macro language broke beyond my capacity to repair it.  
;
;	but i had read that Linus himself was a fan, so surely there must be
;	a REALLY STABLE version hanging out somewhere?
;
;	3.9, or 4.0, or 4.0.15/PK, whatever, this is a really stable version,
;	as far as i can tell.  i broke it dozens of times with stupid code,
;   and it always was dirt-simple to fix.  but the github package is missing
;	some stuff i really want:
;
;	1 - a README that explains things better
;	2 - keyboard input that handles CSI better, i.e.,
;       a full set of gnome-terminal function keys
;	3 - automatic backup and autosave
;	4 - a good view of really wide files, i.e., horizontal scrolling
;	5 - some trivial cosmetics that make my hacking happier
;
;	so, i haven't smashed the stack or overflowed memory for a couple of
;	days.  i think there are still some keyboard combinations that are
;	best avoided, but they're chords, kinda out of the way.  i don't
;	think i broke anything in the macro language.  i did put a huge
;	hack in 'describe-bindings', which could probably be handled less
; 	invasively, but the info it dumps is invaluable, at least to me.
;   try 'list-internals' if you want to see what's going on. 
;   i think horizontal scrolling works, but it's not thoroughly tested
;   with utf-8.  independent of each other, you can turn on automatic
;   backup to save your existing file, and autosave at an arbitrary 
;   number of typed characters, or after an arbitrary number of seconds;
;   see 'autosave', below.  btw, if there are changes to your file, it
;   will be saved after 'autosave' seconds, whether you're typing or not.
;
; Emacs path elements on Linux/Unix:
;
; .emacsrc			- this initialization file
; emacs.hlp			- some help, sorta obsolete; these 2 files will
;                       be sought in the following paths:
; ~/    these aren't listed in epath.h,
; ./        they're hard-coded in bind.c
; /usr/local/bin/
; /usr/local/lib/
; /usr/lib/
; /usr/local/
; /usr/global/lib/	- ue continues looking for those 2 files here
;
;   my favorite installation has ue, .emacsrc, and emacs.hlp in
;   /usr/local/bin.  if you don't see initializations you're looking
;   for, try <ESC>= (bound to 'list-internals').  in the huge list of
;   variables, keystrokes, and functions, are the filespecs of the
;   .emacsrc and emacs.hlp, if they have been found.
;
;	Modified by Petri Kutvonen, last edited September 1991.
;
;	and again by earl roosa (earlroosa@gmail.com), 20260214_07_15:01:53
;
; 	i hope someone else likes obsolete stuff enough to keep playing with ue.
;
;
set $discmd "FALSE"

;	First, try to resolve if we are on a PC ... yes, this is a kludge

!if &seq $sres "NORMAL"
	set %system "OTHER"
!else
	set %system "PC"
!endif

!if &seq %system "PC"

;	PC specific initialization

write-message "(Setting up)"

;	Comment out or change this line if you want more than 25 lines,
;	other possible $sres values include EGA and VGA

set $sres "CGA"

;	Uncomment next line if your old screen "snows"

;set $flicker "TRUE"

;	If your screen "snows" you'll not like scrolling

!if &seq $flicker "TRUE"
	set $scroll "FALSE"
!endif

;	ER:
;	gnome-terminal f1-f12
;	FNP FNQ FNR FNS FNU FNW FNX FNY FN` FNa       FNd
;
;	up  down left right home end del
;	FNq FNr  FNt  FNs   FNx  FNv FNc
;   --------------------------------
;
;
;	Function keys (unshifted)
;	f1  f2  f3  f4  f5  f6  f7  f8  f9  f10  f11  f12
;	FN; FN< FN= FN> FN? FN@ FNA FNB FNC FND

;	Function keys (shifted)
;	F1  F2  F3  F4  F5  F6  F7  F8  F9  F10
;	FNT FNU FNV FNW FNX FNY FNZ FN[ FN\ FN]
;
;	Other special keys (unshifted)
;	Home End  Ins  Del  PgUp PgDn  
;	FNG  FNO  FNR  FNS  FNI  FNQ
;
;       Some common Alt-keys
;	Alt-X Alt-Z Alt-C Alt-F Alt-O
;	FN-   FN,   FN.   FN!   FN^X

bind-to-key exit-emacs			FN-
bind-to-key quick-exit			FN,
bind-to-key i-shell				FN.
bind-to-key find-file			FN!
bind-to-key view-file			FN/
bind-to-key next-window			FN^X

;	Set screen colors

;	You can define a DOS environment variable EMACS_BW (any value)
;	if you don't like colors, e.g. if you have a LCD screen

!if &seq &env "EMACS_BW" ""
	add-global-mode "blue"
	add-global-mode "HIGH"
!endif

!endif

;	Help facility
;
40 	store-macro
	set $discmd "FALSE"
	!if &not &seq $cbufname "emacs.hlp"
		write-message "(Loading Help)"
		!force help
		!force 8 resize-window
		!if &seq %system "PC"
			!if &seq &env "EMACS_BW" ""
				add-mode "red"
			!endif
			bind-to-key execute-macro-38 FNI
			bind-to-key execute-macro-37 FNQ
		!else
			bind-to-key execute-macro-38 FN5
			bind-to-key execute-macro-37 FN6
		!endif
		beginning-of-line
		2 forward-character
		1 redraw-display
		save-window
		!if &seq %system "PC"
			set %hlpupdn "<PgUp> / <PgDn>"
			set %hlphelp "<F1>"
		!else
			set %hlpupdn "<Prev Scrn> / <Next Scrn>"
			set %hlphelp "<Help>"
		!endif
		execute-macro-39
	!else
		set %hlpcode &lef $line 2 
		!if &seq %hlpcode ".."
			set %hlptopic &mid $line 4 99
			end-of-line
			!force search-forward %hlptopic
			beginning-of-line
			2 forward-character
			1 redraw-display
			execute-macro-39
		!else
			!if &seq %system "PC"
				bind-to-key previous-page FNI
				bind-to-key next-page FNQ
			!else
				bind-to-key previous-page FN5
				bind-to-key next-page FN6
			!endif
			!force restore-window
			!force delete-window
			clear-message-line
		!endif
	!endif
	set $discmd "TRUE"
!endm

;bind-to-key execute-macro-40 M-?
!if &seq %system "PC"
	bind-to-key execute-macro-40 FN;
!else
	bind-to-key execute-macro-40 FNh
!endif

;	Help on Help

39	store-macro
	!if &seq &rig $line 5 "INDEX"
		write-message &cat "Select topic from list and press " %hlphelp
	!else
		write-message &cat "Use " &cat %hlpupdn &cat " to scan help file -- " &cat %hlphelp " to toggle help window"
	!endif
!endm

;	Previous help page

38 	store-macro
	!if &seq $cbufname "emacs.hlp"
		beginning-of-line
		!force search-reverse "=>"
		2 forward-character
		1 redraw-display
		execute-macro-39
	!else
		previous-page
	!endif
!endm

;	Next help page

37 	store-macro
	!if &seq $cbufname "emacs.hlp"
		beginning-of-line
		2 forward-character
		!force search-forward "=>"
		1 redraw-display
		execute-macro-39
	!else
		next-page
	!endif
!endm

;	Set up auto CMODE

36	store-macro
	!if &seq &mid $cfname 1 7 "/tmp/Re"
		add-mode "wrap"
		!return
	!endif
	!if &gre &sin $cfname "/.ed" 0
		add-mode "wrap"
		!return
	!endif
	!if &gre &sin $cfname "/.let" 0
		add-mode "wrap"
		!return
	!endif
	!if &gre &sin $cfname "/.art" 0
		add-mode "wrap"
		!return
	!endif
	!if &gre &sin $cfname "/nn." 0
		add-mode "wrap"
		!return
	!endif
	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"
	!endif
	!if &or &seq %rctmp "txt" &or &seq %rctmp "doc" &or &seq %rctmp "tmp" &seq %rctmp "tex"
		add-mode "wrap"
	!endif

!endm
	
bind-to-key execute-macro-36	M-FNR

;	Setup for ASCII {|}[\] to ISO Latin-1 translation

21	store-macro
	insert-string ""
!endm
22	store-macro
	insert-string ""
!endm
23	store-macro
	insert-string ""
!endm
24	store-macro
	insert-string ""
!endm
25	store-macro
	insert-string ""
!endm
26	store-macro
	insert-string ""
!endm

27	store-macro
	bind-to-key execute-macro-21 {
	bind-to-key execute-macro-22 |
	bind-to-key execute-macro-23 }
	bind-to-key execute-macro-24 [
	bind-to-key execute-macro-25 \
	bind-to-key execute-macro-26 ]
	write-message "ISO Latin-1 "
!endm

28	store-macro
	unbind-key {
	unbind-key |
	unbind-key }
	unbind-key [
	unbind-key \
	unbind-key ]
	write-message "ASCII {|}[\]"
!endm

bind-to-key execute-macro-27 ^X[
bind-to-key execute-macro-28 ^X]

9	store-macro
	set-mark
	detab-line
	exchange-point-and-mark
	set %t &mod $curcol $tab
	!if &equ %t 0
		!if &gre $curcol 0
            set %t $tab
		!endif
	!endif
	%t delete-previous-character
!endm

; column-cut, or kill-rectangle in gnu-emacs.
; This is a 'smart' cut routine, in that the 'rectangle' you think you're
; cutting winds up in a buffer called 'cut'.  Both the cut buffer and the
; lines that have been modified will be padded with spaces if necessary, to
; create a rectangular area.  These trailing spaces are not removed
; automatically.  Be careful with this, i.e., save before using... this
; does a lot of text manipulation uninterrupted.  If you get impatient, you
; may lose work.
; N.B.:  DE-TAB YOUR FILE before using this!  figuring out the columns
; with 'real' tabs, 8-space tabs, 4-space tabs, is not on my bucket list.
; also, i have not tested this with high-bit (utf-8) characters. 
11 store-macro
    !if &equ $tab 0
        write-message "Hard tabs enabled . . . set $tab."
        !return
    !endif
    !if $over
        set %ov 1
        delete-mode "OVER"
    !else
        set %ov 0
    !endif
    delete-other-windows
    set %b  $cbufname
    set %l1 $curline
    set %c1 $curcol
    exchange-point-and-mark
    set %l2 $curline
    set %c2 $curcol
    !if &gre %l1 %l2
        set %l  %l1
        set %l1 %l2
        set %l2 %l
    !else
        exchange-point-and-mark
    !endif
    !if &gre %c1 %c2
        set %c  %c1
        set %c1 %c2
        set %c2 %c
    !endif
    0 set-mark
    !force search-forward "	"
    !if $status
        !if &gre $curline %l2
            exchange-point-and-mark
            0 set-mark
        !else
            exchange-point-and-mark
            set-mark
            write-message "Hard tabs embedded:  strip-tabs (M-Tab)."
            !return
        !endif
    !endif
    delete-buffer "cut"
*rect
    end-of-line
    set %c &sub %c2 $curcol
    !if &gre %c 0
        %c insert-string " "
    !else
        set %c &neg %c
        %c backward-character
    !endif
    set %c &sub %c2 %c1
    %c delete-previous-character
    next-line
    select-buffer "cut"
    yank
    next-line
    unmark-buffer
    select-buffer %b
    !if &gre $curline %l2
        exchange-point-and-mark
        !if %ov
            add-mode "OVER"
        !endif
        !return
    !endif
    !goto rect
!endm
    bind-to-key execute-macro-11    ^X^K
;
; column-paste, or yank-rectangle: 
; The upper left corner of the cut buffer is inserted at the point, using
; the same logic as macro 11 used to make the cut.  Again, spaces may be
; added to maintain the rectangular alignment of the cut buffer. 
12 store-macro
    0 set-mark
    set %b $cbufname
    select-buffer "cut"
    beginning-of-file
    set $debug 0
*paist
    kill-to-end-of-line
    yank
	beginning-of-line
	next-line
    unmark-buffer
    set %agin $lwidth
    select-buffer %b
    set %c $curcol
    yank
    !if $over
        set %d &sub $curcol %c
        set %r &len $curtext
        set %r &sub %r $curcol
        !if &gre %r %d
            set %r %d
        !endif
        !if &gre %r 0
            %r delete-next-character
        !endif
    !endif
    !if &gre %agin 0
        &sub $curcol %c backward-character
        next-line
        set %c &sub %c $curcol
        !if &gre %c 0
            %c insert-string " "
        !endif
        select-buffer "cut"
        !goto paist
    !else
        select-buffer %b
        exchange-point-and-mark
    !endif
!endm
    bind-to-key execute-macro-12    ^X^Y
;
; just a stupid ascii table, 32-126, 161-254. for the exercise.
;
13 store-macro
;	set $debug 1
    select-buffer ascii
	set %a 32
	execute-macro-14
	set %a 48
	execute-macro-14
	set %a 64
	execute-macro-14
	set %a 80
	execute-macro-14
	set %a 96
	execute-macro-14
	set %a 112
	execute-macro-14
;	set %a 128
;	execute-macro-14
;	set %a 144
;	execute-macro-14
	set %a 160
	execute-macro-14
	set %a 176
	execute-macro-14
	set %a 192
	execute-macro-14
	set %a 208
	execute-macro-14
	set %a 224
	execute-macro-14
	set %a 240
	execute-macro-14
    previous-page
    unmark-buffer
    add-mode view
!endm

14 store-macro
	set %i 0
	!while &les %i 16
		set %h &add %a %i
		!if &gre %h 99
			insert-string " "
		!else
			insert-string "  "
		!endif
		insert-string %h
		set %i &add %i 1
	!endwhile
	newline
	set %i 0
	!while &les %i 16
		set %c &add %a %i
		insert-string "   "
		!if &equ %c 255
    		insert-string " "
		!else
	    !if &equ %c 127
    		insert-string " "
	    !else
        !if &equ %c 160
    		insert-string " "
	    !else
		    insert-string &chr %c
	    !endif
	    !endif
	    !endif
		set %i &add %i 1
	!endwhile
	newline
	newline

!endm
	bind-to-key execute-macro-13 	M-@
;
15 store-macro
    insert-string "/* "
    end-of-line
    insert-string " */"
!endm
    bind-to-key execute-macro-15    M-;
;
16 store-macro
    write-message $lastkey
    !if &equ $lastkey 20
        insert-string $now
    !else
        insert-string $instime
    !endif
!endm
    bind-to-key execute-macro-16  ^XT
    bind-to-key execute-macro-16  ^X^T

;	Make cut-paste easier in window systems
; well, ok.  not sure how this helps, and it makes
; it harder when i jump into a c-like file that ue
; doesn't recognize.  so put it back if you like.
;bind-to-key	newline	^J

;	uEmacs/PK specific initialization

!if &seq $progname "uEmacs/PK"

;	Don't scroll on a Sun

	!if &or &seq $TERM "sun" &seq $TERM "sun-cmd"
		set $scroll "FALSE"
	!endif

;	Execute local initialization files

	!if &seq %system "PC"
		!force execute-file "EM.RC"
	!else
		!force execute-file &cat $HOME "/.emrc"
		!force execute-file ".emrc"
	!endif
!endif

!if &gre &sin $LANG "UTF-8" 0
	add-global-mode "utf-8"
!endif

!if &gre &sin $LANG "utf8" 0
	add-global-mode "utf-8"
!endif

add-global-mode "utf-8"
set $discmd "TRUE"

; these are gnome-terminal bindings, which owe a lot to vt220.  most of them didn't
; work in the 'PK' package i found on github, which is ok, the core emacs stuff is
; all a hacker needs to get started.  but i wanted to be comfortable, so...
; CSI, in particular, was not handled gracefully when a key didn't fit the loop.
; i found it really needs a long if-list, not clever code nor compact, but it
; provides the flexibility to eat all the one- and two-character prefixes, and
; the sometimes-trailing '~', without stack-smashing.  there are plenty of corner
; cases left for anyone obsessive enough to care; no SS3, for example. 
;
bind-to-key help				    FNP
bind-to-key set-mark			    FNQ
bind-to-key next-buffer			    FNR
bind-to-key next-window			    FNS
bind-to-key delete-next-character	FNU
bind-to-key kill-to-end-of-line		FNW
bind-to-key find-file			    FNX
bind-to-key list-buffers		    FNY
bind-to-key exit-emacs			    FNa
bind-to-key beginning-of-line		FNH
bind-to-key end-of-line			    FNF
bind-to-key yank				    FNd
bind-to-key incremental-search		    ^S
bind-to-key reverse-incremental-search	^R
bind-to-key describe-bindings	        ^X*
bind-to-key list-internals          	M-=
bind-to-key set-trmchr			        ^X.
bind-to-key set-nfword			        ^X/
bind-to-key forward-character			FNs
bind-to-key backward-character			FNt
bind-to-key next-line					FNr
bind-to-key previous-line				FNq
bind-to-key next-page					FNf
bind-to-key previous-page				FNe
bind-to-key kill-region					FNc
bind-to-key end-of-line					FNv
bind-to-key beginning-of-line			FNx
bind-to-key backtab						FNZ
bind-to-key list-internals				FN`
bind-to-key execute-file				^X&

; ctrl-rightarrow:
bind-to-key next-word					FN^c
; ctrl-leftarrow:
bind-to-key previous-word				FN^d
;
; ctrl-uparrow:
bind-to-key move-window-down			FN^b
; ctrl-leftarrow:
bind-to-key move-window-up				FN^a
;
; it was tricky to get input.c to pass these 2 sequences back, which is
; why i added the alternatives below.
bind-to-key next-word					^\
bind-to-key previous-word				^_

; like move-by-word, the old input didn't want to process esc-esc,
; so there's an alternative i used until i got it to work:
bind-to-key execute-command-line M-^[
bind-to-key execute-command-line M-`

; if you don't touch 'nfword', you get something like the gnu-emacs move-by-word
; behavior, ie, hang on the end of 'this' word before going to the next, instead
; of the more usual jump-to-start-of-word.  it's a toggle on/off.
; set-nfword

; i like to see the last line of the previous page at the top of the new page,
; on page-down.  make it 2 (like gnu-emacs) or 0, or whatever.
set $overlap 1

; these 2 tab vars aren't the same thing.  $tab tells display how far to space
; for a real tab, which might be 4 or 8 columns.
; $tabsize has 2 functions:  synch backtab to the nearest mod($tab) when erasing,
; and tell input (actually random.c) whether to insert 4 spaces, 8 spaces,
; or real tabs.  the names are kinda intuitively backward, but whatever.  
; move forward mod(4) spaces on ^I
set $tab 4
; if $tabsize is 0, real tabs go in the file.
set $tabsize 4
;
; put this anywhere to see the code at work, line-by-line
; set $debug 1
;
; $trmchr is my OCD at work.  i like to see the end of the line if it
; terminates in whitespace, and occasionally it matters to clean up
; dangling spaces and tabs.  editors with lots of color control can
; use a light gray shading or whatever.
; i like to use something less blatant than a hyphen, and utf-8
; has the high-bit character 183, a centered dot.
; otoh, some people find a dangling non-character annoying.  make it 32 (space)
; if you don't want to see it.
set $trmchr 183
;
; autosave:
; my major contribution (or malicious hack, depending on your point of
; view).  i can't stand losing work, and even the most carefully-groomed
; interface is subject to i/o interruptions, power-offs, and random bugs. 
; hence, 'autosave':  set ### seconds here, and ue will make a backup,
; suffixed '_v00##' sequentially, on the way in.  if all goes well, and
; you don't touch the keyboard for 'n' seconds, it will save your work
; without interruption.  of course, whole-file backups are disgracefully
; inefficient, but disk is cheap, labor is not. 
; 
;
set $autosave 300
;
; the autosave held over from 3.9.  type the requisite number of characters and ue will
; save without asking.  belt-and-suspenders -- either save when we type enough, or when
; time runs out. 
;
set $acount 240
set $asave 240
;
; kill this line if you want to stop at hyphens, underbars, and quotes:
set $isword _-'"
;
