#
#	C-EDRC
#
#	configuration file to create C-language programs with DME
#
#	(c) 1992  by Bernd Noll
#

addpath dinclude:
addpath autodoc:
addpath dice:doc
addpath dice:man
addpath man3:cclib

map sac-, (last left ifelse !c=59 (right (;)) (right) -return)
map sac-m (split down split down c-tab)

set getname (escimm (set name `))
set gettype (escimm (set type `))
#setmacro getname 0 (escimm (set name `))
#setmacro gettype 0 (escimm (set type `))
#set blockbeginner (while for if switch case FOREVER)

setmacro freeline 0 (last ifelse l adjustline cr)
setmacro splitlastword 2 (eval (rx (" x='$$arg1'; y=words(x); 'set $arg2' word(x,y); 'set $arg1 ('delword(x,y,1)')';")))


#C-language support
menuoff

menuadd Declare (Char)                  ((char  ;)  left)
menuadd Declare (Short)                 ((short ;)  left)
menuadd Declare (Int)                   ((int   ;)  left)
menuadd Declare (Long)                  ((long  ;)  left)
menuadd Declare (uns.Char)              ((unsigned char  ;) left)
menuadd Declare (uns.Short)             ((unsigned short ;) left)
menuadd Declare (uns.Int)               ((unsigned int   ;) left)
menuadd Declare (uns.Long)              ((unsigned long  ;) left)

menubar Declare
menuadd Declare (Struct-Type)           ($getname (struct \$name {) spdw cr (} /* struct \$name */) adjustline  up tab)

menuadd States (Switch-Constr.  a-s)    ( a-s)
menuadd States (Case-Label      a-c)    ( a-c $getname (\$name) right cr)
menuadd States (If-Statement    a-i)    ( a-i)
menuadd States (Else-Part       a-e)    ( a-e)
menuadd States (ElsIf-Part     sa-e)    (sa-e)
menuadd States (For-Loop        a-f)    ( a-f)
menuadd States (While-Loop      a-w)    ( a-w)
menuadd States (} /*  */           )    ( pushdot (} /*  */) popdot cr col +5)
menuadd States ({\\n} /*  */       )    ( freeline ({) cr (} /*  */) adjustline col +5)
menubar States
menuadd States (referred to)            (insert (->))
menuadd States (<line-end>)             (sac-,)

menuon

setmacro c-case   0 (freeline (case :) adjustline last left)
setmacro c-elif   0 (freeline (} else if () {) adjustline last rundzu prev)
setmacro c-else   0 (freeline (} else {) adjustline last)
setmacro c-for	  0 (freeline (for (;;) {) cr (} /* for */) adjustline up last findstr (;;) prev)
setmacro c-if	  0 (freeline (if () {) cr (} /* if */) adjustline rundzu prev)
setmacro c-switch 0 (freeline (switch () {) cr (} /* switch */) adjustline rundzu prev)
setmacro c-while  0 (freeline (while () {) cr (} /* while */) adjustline rundzu prev)
setmacro c-struct 0 ((struct  {) cr (} /* struct */) up up col -7)
setmacro c-proc   2 (freeline first (\$arg1) cr (\$arg2 ()) cr ({) cr (} /* \$arg2 */) adjustline last cr goto -3 last left)
#setmacro c-proc   0 (freeline first (\$type) cr (\$name()) cr ({) cr (} /* \$name */) adjustline last cr goto -3 last left)

# with that macro it is possible to surround a block with
# one of the 2-line c-constructs
# the only things, that are bugs/not supported is that after that call
# the ranges of the block are not set right any more and
# that the block is not tabbed and that we had to insert 1 line, that cannot be deleted
#setmacro surroundblock 1 (ifelse !cb (\$arg1) (while cb down pushdot up while cb up eval (\$arg1) pushmark unblock down block block swapdot down bmove popdot up popmark))
setmacro surroundblock 2 (ifelse !cb (\$arg1) (while cb up down eval (\$arg2) up insline eval (\$arg1) down bmove up))

map  s-nk* ((/*  */) col -3)

map  a-a ((&& ))
map sa-a ((& ))
map sA-a ((void* ))

map  a-c (c-case)
map sa-c ((const) cr tab)
map  A-c ((char  ; ) left )

map  a-d ((/ ))

map  a-e (c-else)
map sa-e (c-elif)
map  A-e ((extern ))

map  a-f (c-for)
map  A-f ((FALSE))

map  a-i (c-if)
map sa-i (surroundblock  (first (#if ) spdw (#endif /*  */) up col 5) ())
map  A-i ((int  ;) left)

map  a-k (firstnb `/* ' last right `*/')
map sa-k (first findstr (*) next left del del del last prev del del bs firstnb)

map  A-l ((long ;) left)
map sA-l ((unsigned long ;) left)

map  a-m ((% ))

map  a-n ((!))
map  A-n ((NULL ))
map sa-n ($getname)
map sA-n ($gettype)

map  a-p ($gettype $getname if !vlname (splitlastword type name) first (\$type) cr (\$name()) cr ({) cr (} /* \$name */) adjustline last cr goto -3 last left)
map  A-p ((Prototype ))

map sa-r ((return ) if c=32 ((();) col -2))
map  A-r ((register ))

map  a-s (c-switch)
map  A-s (c-struct)
map sA-s ((static ))

map  a-t ((typedef) -tab)
map  A-t ((TRUE))

map  A-v  ((void))

map  a-w (c-while)

map  a-z ((printf("",,0);) col -6)

map   help ctags
map s-help ref




#map -return (split down set crline \$lineno up firstnb while r (up firstnb) set crcol \$colno (if c=123 add crcol 4) goto \$crline first ifelse ca

# you should test for , { \ ) ( you do test for {
#map -return (split down set _crl \$lineno prevline firstnb set _crc \$colno ifelse ca (rx ("trace results; if ~(find('\$blockbeginner','\\\$currentword')=0) then 'add _crc 4')) (last left if c=123 (add _crc 4)) goto \$_crl first while x<\$_crc (( ))


set blockbeginner (if else while for switch case FOREVER)
set blockender	  (else case)
## some c-possibilities are not respected here:
## (it did not disturb me, as I do not use such style too much)
##	"if ((...) <cr> (...)) { <cr>" would cause 2 tabs
##	"} else { /* bla bla */ <cr>"  would cause no tab
##	"#if/#ifdef/#endif/..."-lines are not ignored
##	"if (...) <cr> state <cr> does not go back to old-insertion */
## (in comments the used formatting is disgusting, too)
setmacro checkblockers	 1 (unfail (last left if !ca (add $arg1 \$tabstop if c=123 abort if c=58 abort sub $arg1 \$tabstop) if c=59 abort firstnb if ca (rx ("if find('\$blockbeginner','\$currentword') ~= 0 then 'add $arg1 \$tabstop'"))))
setmacro checkunblockers 1 (firstnb ifelse c=125 (sub $arg1 \$tabstop) (firstnb if ca (rx ("if find('\$blockender','\$currentword') ~= 0 then 'sub $arg1 \$tabstop'"))))
setmacro adjustline	 0 (quiet (getlineno _crl prevline firstnb getcolno _crc checkblockers _crc goto \\\$_crl) firstnb checkunblockers _crc eval (while x<\\\$_crc space while x>\\\$_crc bs))

#	     __that part calls the right indention also for the currentline__ :-) only the last three commands are neccessary for return
map -return (set _crx -\$colno last math2 add _crx \$colno adjustline last col -\$_crx split down adjustline)
map c-tab (adjustline)




##########################################################
##
##  DICE-ERROR-SUPPORT
##
##  it is supposed that U call the compiler with "-E \$errsrc"
##  and create that file starting with "DJMP <cr>TPLT..." (see below)
##  before compiling ( to be able to correctly read the errors)
##  (else that has to be done in readerrs via something like :)
##  execute (echo > \$errfile "DJMP...") execute (type >> \$errfile \$errsrc)
##
set errsrc  errors
set errfile errors
set errnum  0
##
setmacro readerrs   0 (readjumps \$errfile g set errnum 0)
setmacro currenterr 0 (gotojump  \$errfile \$errnum g)
setmacro nexterr    0 (inc errnum currenterr)
setmacro preverr    0 (dec errnum currenterr)
setmacro firsterr   0 (set errnum 1      currenterr)
setmacro gotoerr    1 (set errnum \$arg1 currenterr)
setmacro clearerrs  0 (execute (echo > \$errfile "DJMP*NTPLT%**s%**s%**s%Xld%Ts%C99c") readerrs)
##
##########################################################



