(defun split-kernel ()
  "Split the output of the m2m2 translation of KSOS into separate files."
  (interactive)
  (setq old-fold-default default-case-fold-search)
  (setq default-case-fold-search nil)
  (setq old-fold case-fold-search)
  (setq case-fold-search nil)
  (setq main-buffer (buffer-name))
  (setq buf-list '())
  (init-buffer 'K32conf 'def t)
  (init-buffer 'K32except 'def t)
  (init-buffer 'K32baseTypes 'def t)
  (init-buffer 'K32types 'def t)
  (init-buffer 'Kmisc 'def t)
  (progn (setq macros "kernel_macros.h")
	 (setq buf-list (append buf-list (list 'macros)))
	 (put 'macros 'enclosed nil)
	 (put 'macros 'type 'h)
	 (switch-to-buffer macros))
  (init-buffer 'KSPnad 'def t)
  (init-buffer 'KSMnad 'def t)
  (init-buffer 'KERpcs 'def t)
  (init-buffer 'KPRdcl 'def t)
  (init-buffer 'Nucleus 'imp t)
  (init-buffer 'K32callp 'def t)
  (init-buffer 'Kernel 'prog nil)
  (init-buffer 'KIO 'def nil)
  (init-buffer 'IOform 'def t)
  (init-buffer 'IOUtil 'imp nil)
  (init-buffer 'IOSecure 'imp nil)
  (init-buffer 'IOD 'def nil)
  (init-buffer 'IODtrap 'imp nil)
  (init-buffer 'IODnew 'imp nil)
  (init-buffer 'IODint 'imp nil)
  (init-buffer 'IODtimer 'imp nil)
  (init-buffer 'IODbus 'imp nil)
  (init-buffer 'IODqueue 'imp nil)
  (init-buffer 'IOTqueue 'imp nil)
  (init-buffer 'IODdevs 'imp nil)
  (progn (setq IODconf "IODconf.h")
	 (setq buf-list (append buf-list (list 'IODconf)))
	 (put 'IODconf 'enclosed nil)
	 (put 'IODconf 'type 'h)
	 (switch-to-buffer IODconf))
  (init-buffer 'IODRK05 'mod nil)
  (init-buffer 'IODRWP 'mod nil)
  (init-buffer 'IODRWS04 'mod nil)
  (init-buffer 'IODTWE16 'mod nil)
  (init-buffer 'IODPR11 'mod nil)
  (init-buffer 'IODPC11 'mod nil)
  (init-buffer 'IODLP11 'mod nil)
  (init-buffer 'IODDL11 'mod nil)
  (init-buffer 'IODDH11 'mod nil)
  (init-buffer 'IODLHDH 'mod nil)
  (init-buffer 'IOTsys 'imp nil)
  (init-buffer 'IODsys 'imp nil)
  (init-buffer 'IOE 'imp nil)
  (init-buffer 'IOEform 'def t)
  (init-buffer 'IOF 'def nil)
  (init-buffer 'IOFform 'def t)
  (init-buffer 'IOFcacher 'imp nil)
  (init-buffer 'IOFindex 'imp nil)
  (init-buffer 'IOFspace 'imp nil)
  (init-buffer 'IOFsys 'imp nil)
  (init-buffer 'IOCommon 'imp nil)
  (init-buffer 'KSM 'imp t)
  (init-buffer 'KSMa 'imp nil)
  (init-buffer 'KSMb 'imp nil)
  (init-buffer 'KSMc 'imp nil)
  (init-buffer 'KSMd 'imp t)
  (init-buffer 'KPRa 'imp nil)
  (init-buffer 'clock 'imp nil)
  (init-buffer 'KPRb 'imp nil)
  (init-buffer 'KPRc 'imp nil)
  (init-buffer 'KPRd 'imp nil)
  (init-buffer 'psd 'imp nil)
  (init-buffer 'KSPa 'imp nil)
  (init-buffer 'KSParith 'imp nil)
  (init-buffer 'BUILD 'imp nil)
  (init-buffer 'KSPb 'imp nil)
  (init-buffer 'kcdp 'imp nil)
  (init-buffer 'K32calls 'def t)
  (init-buffer 'TIIacc 'imp nil)
  (init-buffer 'memPar 'imp nil)
  (setq default-case-fold-search old-fold-default)
  (switch-to-buffer main-buffer)
  (goto-char (point-min))
  (gobble-text "KERdclWhat" 4 Kernel)
  (gobble-text "openSegLimit" 4 K32conf)
  (gobble-text "XbPm" 4 K32except)
  (gobble-text "Misc" 3 K32types)
  (gobble-text "integer16" 3 K32baseTypes)
  (gobble-text "selector" 3 K32types)
  (gobble-text "cardinal32" 3 K32baseTypes)
  (gobble-text "allExecute" 3 K32types)
  (gobble-text "nullByte" 2 K32baseTypes)
  (gobble-text "nullSeid" 2 K32types)
  (gobble-text "nullBits32" 2 K32baseTypes)
  (gobble-text "nullTiiStruct" 3 K32types)
  (gobble-text "CPP" 0 K32types)
  (gobble-text "ASSERT" 4 macros)
  (gobble-text "filler2" 4 K32types)
  (gobble-text "nullUnitId" 3 KSPnad)
  (gobble-text "remapOutSize" 2 KSMnad)
  (gobble-text "down" 2 K32types)
  (gobble-text "TYPE" 3 KSMnad)
  (gobble-text "segSize" 3 K32types)
  (gobble-text "gstTii" 4 KSMnad)
  (gobble-text "statLoc" 4 K32types)
  (gobble-text "nullSegSize" 1 KSMnad)
  (gobble-text "nullSegSize" 2 K32types)
  (gobble-text "nullTiiStruct" 3 KSMnad)
  (gobble-text "nullVirtLoc" 5 K32types)
  (gobble-text "kernelReady" 5 Kmisc)
  (gobble-text "nullPostEntry" 4 KERpcs)
  (gobble-module "KSMald" 3 KSM)
  (gobble-text "pcsId" 6 KPRdcl)
  (gobble-text "utiming" 3 K32types)
  (gobble-text "kern_timing" 3 KPRdcl)
  (gobble-text "_etext:" 9 Nucleus)
  (gobble-text "KbootParam" 5 K32callp)
  (gobble-text "pBoot" 3 kcdp)
  (gobble-module "KSMinit" 3 KSM)
  (gobble-module "KLIB" 3 KSPa)
  (gobble-module "KSParith" 3 KSParith)
  (gobble-module "SMX" 3 KSPa)
  (gobble-module "BUILD" 3 BUILD)
  (gobble-module "KSPa" 3 KSPa)
  (gobble-text "timeOfDayClock " 3 KPRa)
  (gobble-text "secondSignal" 2 clock)
  (gobble-text "#endif" 3 Nucleus)
  (gobble-text "*****)" 3 KPRa)
  (gobble-text "PROCESS" 3 clock)
  (gobble-text "getOldPc;" 4 Nucleus)
  (gobble-module "clock" 5 clock)
  (gobble-text "DEVsetClock;" 4 KPRa)
  (gobble-text "dev_time" 7 Nucleus)
  (gobble-module "memAcc" 3 KPRa)
  (gobble-module "nucAdr" 3 Nucleus)
  (gobble-module "KPRa" 4 KPRa)
  (search-forward "END KPRd;")		; skip forward modules
  (beginning-of-line 3)
  (gobble-module "KSPb" 3 KSPb)
  (gobble-module "KSMa" 3 KSMa)
  (gobble-text "MAPuserToIndex;" 4 KPRb)
  (gobble-text "MAPgatAdr;" 4 Nucleus)
  (gobble-text "MAPinitMap:" 5 KPRb)
  (gobble-text "ppGet;" 4 Nucleus)
  (gobble-text "softwareTrap" 6 KPRb)
  (gobble-text "DEVgetPD;" 3 Nucleus)
  (gobble-module "KPRb" 3 KPRb)
  (gobble-text "relAddress" 3 KIO)
  (gobble-text "nullPointer16" 2 IOform)
  (gobble-text "nullIOopenPart" 3 KIO)
  (gobble-text "security" 6 IOform)
  (gobble-module "IOUtil" 3 IOUtil)
  (gobble-module "IOSecure" 3 IOSecure)
  (gobble-text "IODcontroller:" 3 IOD)
  (gobble-module "IODtrap" 3 IODtrap)
  (gobble-module "IODnew" 3 IODnew)
  (gobble-module "IODint" 3 IODint)
  (gobble-module "IODtimer" 3 IODtimer)
  (gobble-module "IODbus" 3 IODbus)
  (gobble-module "IODqueue" 3 IODqueue)
  (gobble-module "IOTqueue" 3 IOTqueue)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODRK05" 2 IODRK05)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODRWP" 2 IODRWP)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODRWS04" 2 IODRWS04)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODTWE16" 2 IODTWE16)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODPR11" 2 IODPR11)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODPC11" 2 IODPC11)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODLP11" 2 IODLP11)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODDL11" 2 IODDL11)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODDH11" 2 IODDH11)
  (gobble-text "#define" 1 IODdevs)
  (gobble-text "#define" 2 IODconf)
  (gobble-text "MODULE" 1 IODdevs)
  (gobble-module "IODLHDH" 2 IODLHDH)
  (gobble-module "IODdevs" 3 IODdevs)
  (gobble-module "IOTsys" 3 IOTsys)
  (gobble-module "IODsys" 2 IODsys)
  (gobble-module "IOD" 3 IOD)
  (gobble-text "IOEdefs" 3 IOE)
  (gobble-text "END DISK" 3 IOEform)
  (gobble-module "IOE" 3 IOE)
  (gobble-text "IOFdefs" 3 IOF)
  (gobble-text "END DISK" 2 IOFform)
  (gobble-text "Initial" 5 IOF)
  (gobble-text "initialFreeItem" 3 IOFform)
  (gobble-text "zero" 4 IOF)
  (gobble-module "IOFcacher" 3 IOFcacher)
  (gobble-module "IOFindex" 3 IOFindex)
  (gobble-module "IOFspace" 3 IOFspace)
  (gobble-module "IOFsys" 3 IOFsys)
  (gobble-module "IOF" 3 IOF)
  (gobble-text "nullP" 3 IOCommon)
  (gobble-text "nullOpenModes" 2 K32types)
  (gobble-module "IOCommon" 2 IOCommon)
  (gobble-module "KIO" 3 KIO)
  (gobble-module "KSMb" 3 KSMb)
  (gobble-text "clear;" 5 KPRc)
  (gobble-text "getPc;" 4 Nucleus)
  (gobble-module "KPRc" 3 KPRc)
  (gobble-module "SURH" 3 KSMc)
  (gobble-module "SMPC" 4 KSMd)
  (gobble-module "KSMc" 3 KSMc)
  (gobble-module "devDel" 3 KPRd)
  (gobble-module "nucCntx" 3 Nucleus)
  (gobble-text "END GATemt;" 4 KPRd)
  (gobble-text "GATjump;" 4 Nucleus)
  (gobble-module "gat" 4 KPRd)
  (gobble-module "psd" 3 psd)
  (gobble-module "KPRd" 3 KPRd)
  (gobble-text "setPriv" 6 kcdp)
  (gobble-module "TIIacc" 4 TIIacc)
  (gobble-module "memPar" 3 memPar)
  (gobble-text "modDelay" 3 kcdp)
  (gobble-text "Kunlock" 4 K32calls)
  (gobble-module "kcdp" 5 kcdp)
  (gobble-text "swap_extent " 3 Kernel)
  (gobble-text "segLink" 3 KSM)
  (gobble-text "upt" 0 Kernel)			; just a blank line
  (gobble-text "PCSrecSize" 3 KPRdcl)
  (append-to-buffer Kernel (point) (point-max))
  (progn (switch-to-buffer K32baseTypes)
	 (goto-char (point-max))
	 (search-backward "bits16")
	 (beginning-of-line)
	 (insert "TYPE\n")
	 (search-forward "null")
	 (beginning-of-line)
	 (insert "CONST\n"))
  (progn (switch-to-buffer K32types)
	 (goto-char (point-max))
	 (search-backward "nullSegSize")
	 (beginning-of-line)
	 (insert "CONST\n")
	 (fix-complex-constant "nullSegStatBlock")
	 (beginning-of-line)
	 (insert "\nVAR\n")
	 (beginning-of-line 3)
	 (insert "TYPE")
	 (search-backward "nullVirtLoc")
	 (beginning-of-line)
	 (insert "VAR\n")
	 (fix-complex-constant "nullVirtLoc")
	 (fix-complex-constant "nullSeid")
	 (fix-complex-constant "nullTiiStruct")
	 (beginning-of-line 2)
	 (insert "\tnullIpcReturn : ipcReturn;\n")
	 (search-forward "directions")
	 (beginning-of-line)
	 (insert "CONST\n")
	 (search-forward "BOOLEAN")
	 (beginning-of-line)
	 (insert "\nTYPE\n")
	 (fix-complex-constant "nullOpenModes")
	 (beginning-of-line)
	 (insert "\nVAR\n"))
  (progn (switch-to-buffer KSMnad)
	 (goto-char (point-max))
	 (search-backward "nullStEntry")
	 (beginning-of-line)
	 (insert "\nVAR\n")
	 (fix-complex-constant "nullStEntry"))
  (progn (switch-to-buffer KERpcs)
	 (goto-char (point-max))
	 (teco-fs "CONST " "VAR\n\t" -1)
	 (fix-complex-constant "nullPostEntry"))
  (progn (switch-to-buffer Nucleus)
	 (goto-char (point-min))
	 (fix-import-list "nucAdr" "baseDspace, firstAfterKernel,")
	 (kill-lines-containing "baseDspace" 6 5)	; entire EXPORT list
	 (kill-lines-containing "baseDspace" 2 2)	; entire VAR list
	 )
  (progn (replace-port-lists 'KIO)
	 (fix-complex-constant "nullIOopenPart")
	 (beginning-of-line)
	 (insert "VAR\n")
	 (beginning-of-line 2)
	 (insert "\tnullTransferBlock : transferBlock;\n")
	 (search-forward "BEGIN")
	 (beginning-of-line)
	 (delete-region (point) (progn (search-forward "END") (beginning-of-line) (point))))
  (progn (replace-port-lists 'IOUtil)
	 (include-macros))
  (progn (replace-port-lists 'IOSecure)
	 (include-macros))
  (progn (replace-port-lists 'IOD)
	 (search-forward "IODclass ")
	 (teco-fs "CONST" "VAR\n" -1)
	 (fix-complex-constant "IODclass")
	 (search-forward "BEGIN")
	 (beginning-of-line)
	 (delete-region (point) (progn (search-forward "END") (beginning-of-line) (point))))
  (progn (switch-to-buffer IODtrap)
	 (goto-char (point-min))
	 (search-forward "EXPORT")
	 (beginning-of-line)
	 (setq x (point))
	 (search-forward ";")
	 (beginning-of-line 2)
	 (delete-region x (point))
	 (insert-file-contents "IODtrap.head" nil))
  (replace-port-lists 'IODnew)
  (progn (replace-port-lists 'IODint)
	 (search-forward "END")
	 (insert ?;))
  (progn (replace-port-lists 'IODtimer)
	 (include-macros))
  (progn (replace-port-lists 'IODbus)
	 (include-macros))
  (progn (replace-port-lists 'IODqueue)
	 (include-macros))
  (progn (replace-port-lists 'IOTqueue)
	 (include-macros))
  (progn (switch-to-buffer IODdevs)
	 (include-macros)
	 (insert "#include \"IODconf.h\"\n")
	 (search-forward "IMPORT")
	 (beginning-of-line)
	 (set-mark (point))
	 (search-forward ";")
	 (beginning-of-line 2)
	 (delete-region (mark) (point))
	 (insert-file-contents "IODdevs.head" nil)
	 (add-include-line "RK05")
	 (add-include-line "RWP")
	 (add-include-line "RWS04")
	 (add-include-line "TWE16")
	 (add-include-line "PR11")
	 (add-include-line "PC11")
	 (add-include-line "LP11")
	 (add-include-line "DL11")
	 (add-include-line "DH11")
	 (add-include-line "LHDH"))
  (progn (switch-to-buffer IODconf)
	 (goto-char (point-min))
	 (replace-string "y" "n"))
  (replace-port-lists 'IOTsys)
  (progn (replace-port-lists 'IODsys)
	 (include-macros))
  (progn (replace-port-lists 'IOE)
	 (include-macros))
  (progn (replace-port-lists 'IOF)
	 (fix-complex-constant "initialIOFopen")
	 (teco-fs "CONST" "VAR" -1)
	 (fix-complex-constant "nullSubtype")
	 (kill-lines-containing "zero" 0 1)
	 (fix-complex-constant "nullP")
	 (beginning-of-line 3)
	 (setq x (point))
	 (search-forward "END IOF")
	 (beginning-of-line)
	 (delete-region x (point)))
  (progn (replace-port-lists 'IOFcacher)
	 (include-macros))
  (progn (replace-port-lists 'IOFindex)
	 (include-macros))
  (progn (replace-port-lists 'IOFspace)
	 (include-macros))
  (progn (replace-port-lists 'IOFsys)
	 (include-macros))
  (progn (replace-port-lists 'IOCommon)
	 (include-macros))
  (progn (fix-module-top 'KSMa)
	 (replace-port-lists 'KSMa))
  (progn (fix-module-top 'KSMb)
	 (replace-port-lists 'KSMb)
	 (include-macros))
  (progn (fix-module-top 'KSMc)
	 (replace-port-lists 'KSMc))
  (progn (fix-module-top 'KPRa)
	 (replace-port-lists 'KPRa)
	 (include-macros))
  (progn (fix-module-top 'clock)
	 (goto-char (point-min))
	 (search-forward "IMPORT")
	 (beginning-of-line)
	 (set-mark (point))
	 (search-forward ";")
	 (beginning-of-line 2)
	 (delete-region (mark) (point))
	 (insert-file-contents "clock.head" nil))
  (progn (fix-module-top 'KPRb)
	 (replace-port-lists 'KPRb)
	 (include-macros))
  (progn (fix-module-top 'KPRc)
	 (include-macros)
	 (replace-port-lists 'KPRc)
	 (kill-lines-containing "prim_pcs_lock" 0 3)
	 (search-forward "END KPRc")
	 (beginning-of-line)
	 (yank))
  (progn (fix-module-top 'KPRd)
	 (replace-port-lists 'KPRd)
	 (include-macros))
  (progn (fix-module-top 'psd)
	 (replace-port-lists 'psd)
	 (include-macros))
  (progn (fix-module-top 'KSPa)
	 (replace-port-lists 'KSPa))
  (replace-port-lists 'KSParith)
  (progn (fix-module-top 'BUILD)
	 (search-forward "IMPORT")
	 (beginning-of-line)
	 (set-mark (point))
	 (search-forward ";")
	 (beginning-of-line 2)
	 (delete-region (mark) (point))
	 (insert-file-contents "BUILD.head" nil))
  (replace-port-lists 'KSPb)
  (progn (fix-module-top 'kcdp)
	 (insert-file "kcdp.head")
	 (exchange-point-and-mark)
	 (search-forward "IMPORT")
	 (beginning-of-line -3)
	 (set-mark (point))
	 (search-forward ";" nil nil 3)
	 (beginning-of-line 4)
	 (delete-region (mark) (point)))
  (progn (fix-module-top 'TIIacc)
	 (replace-port-lists 'TIIacc)
	 (include-macros))
  (progn (fix-module-top 'memPar)
	 (replace-port-lists 'memPar))
  (progn (switch-to-buffer K32calls)
	 (goto-char (point-min))
	 (search-forward "=")
	 (beginning-of-line)
	 (insert "CONST\n"))
  (progn (switch-to-buffer Kernel)
	 (goto-line 2)
	 (insert-file-contents "Kernel.head" nil)
	 (teco-fs " {" " bits16{"))
  (while (not (equal buf-list nil))
    (flush-buffer (car buf-list))
    (setq buf-list (cdr buf-list))
    )
  (switch-to-buffer main-buffer)
  (setq case-fold-search old-fold)
  )

(defun init-buffer (name type enclosed)
  (interactive "SBuffer name: \nSType: \nSEnclosed? ")
  (set name (concat (symbol-name name) (if (equal type 'def) ".def" ".mod")))
  (setq buf-list (append buf-list (list name)))
  (put name 'enclosed enclosed)
  (put name 'type type)
  (switch-to-buffer (eval name))
  (goto-char (point-min))
  (cond ((equal type 'def)
	 (insert "DEFINITION "))
	((equal type 'imp)
	 (insert "IMPLEMENTATION "))
	)
  (if enclosed
      (progn
	(insert (format "MODULE %s;\n\n" (symbol-name name)))
	(if (file-exists-p (concat (symbol-name name) ".head"))
	    (insert-file-contents (concat (symbol-name name) ".head") nil))
	(goto-char (point-max))
	)
    )
  )

(defun gobble-text (string blanks buf)
  (interactive "sString: \nsBlank lines: \nbBuffer name: ")
  (setq x (point))
  (search-forward string)
  (beginning-of-line blanks)
  (append-to-buffer buf x (point))
  )

(defun gobble-module (name blanks buf)
  (interactive "sModule name: \nsBlank lines: \nbBuffer name: ")
  (gobble-text (concat "END " name ";") blanks buf)
  )

(defun fix-complex-constant (name)
  (interactive "sName of null constant: ")
  (search-forward name)
  (just-one-space)
  (forward-word -1)
  (kill-line 0)
  (insert ?\t)
  (teco-fs "=" ":")
  (just-one-space)
  (forward-word 1)
  (setq x (point))
  (search-forward ";")
  (kill-region x (1- (point)))
  )

(defun fix-module-top (name)
  (interactive "*S")
  (switch-to-buffer (eval name))
  (goto-char (point-min))
  (re-search-forward (concat "MODULE[ \t]*" (symbol-name name)))
  (beginning-of-line)
  (kill-line 2)
  (goto-char (point-min))
  (forward-word 1)	; skip "IMPLEMENTATION"
  (forward-char 1)	; skip space
  (yank)
  )

(defun kill-lines-containing (string before after)
  (interactive "*sString: \nnLines before: \nnLines after: \n")
  (search-forward string)
  (beginning-of-line (- 1 before))
  (kill-line (+ before after))
  )

(defun replace-port-lists (buf)
  "Replace IMPORT and EXPORT lists with contents of file buf.head."
  (interactive "*SSymbol of buffer: ")
  (switch-to-buffer (eval buf))
  (goto-char (point-min))
  (search-forward "IMPORT")
  (beginning-of-line)
  (setq x (point))
  (search-forward ";" nil nil 2)
  (beginning-of-line 2)
  (delete-region x (point))
  (insert-file (concat (symbol-name buf) ".head"))
  (exchange-point-and-mark)
  )

(defun fix-import-list (module symbols)
  (interactive "*sModule name: \nsSymbols to add: ")
  (re-search-forward (concat "MODULE[ \t]*" module))
  (search-forward "IMPORT")
  (re-search-forward "[,;]")
  (forward-word -1)
  (setq col (current-column))
  (search-backward "IMPORT")
  (forward-word 1)
  (if (equal (following-char) 10)
      (forward-char 1)
    (insert "\n")
    )
  (indent-to col 0)
  (insert (format "%s\n" symbols))
  )

(defun fix-export-list (module symbols)
  (interactive "*sModule name: \nsSymbols to add: ")
  (re-search-forward (concat "MODULE[ \t]*" module))
  (search-forward "EXPORT")
  (re-search-forward "[,;]")
  (forward-word -1)
  (setq col (current-column))
  (beginning-of-line)
  (indent-to col 0)
  (insert (format "%s\n" symbols))
  )

(defun include-macros ()
  (interactive "*")
  (goto-char (point-min))
  (beginning-of-line 2)
  (insert "\n#include \"kernel_macros.h\"")
  (if (not (equal (following-char) 10))
      (insert "\n"))
  )

(defun add-include-line (module)
  (interactive "*sModule name: ")
  (search-forward "IC_")
  (beginning-of-line 2)
  (insert "#include \"IOD" module ".mod\"")
  )

(defun flush-buffer (buf)
  (interactive "SSymbol of buffer to flush: ")
  (switch-to-buffer (eval buf))
  (goto-char (point-min))
  (forward-word 1)
  (just-one-space)
  (goto-char (1- (point-max)))
  (if (equal (following-char) 10)	; remove trailing blank lines
      (progn
	(while (equal (preceding-char) 10)
	  (goto-char (1- (point)))
	  )
	(delete-region (point) (1- (point-max))))
    )
  (goto-char (point-max))
  (if (get buf 'enclosed)
      (insert (format "\nEND %s.\n" (symbol-name buf)))
    (setq typ (get buf 'type))
    (if (or (equal typ 'def) (equal typ 'imp))
      (progn (search-backward "END")
	     (if (search-forward ";" nil t)
		 (replace-match "."))
	     ))
    )
  (write-file (eval buf))
  )

(defun teco-fs (from-string to-string &optional arg)
  "Perform the TECO \"fs\" (find and substitute) command.
Replaces one occurrence of FROM-STRING with TO-STRING -- the ARGth
occurrence, to be precise.  Negative ARG means search backward.

When calling from a program, nil means \"no arg\",
a number counts as a prefix arg."
  (interactive "*sFind: \nsFind \"%s\" and substitute with: \nP")
  (if (or (equal arg nil) (equal arg 0))
      (setq arg 1))
  (if (< arg 0)
      (search-backward from-string nil nil (- arg))
    (search-forward from-string nil nil arg)
    )
  (replace-match to-string t t)
  )
