home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / editor / epmtools / epmmac / ckeys.e < prev    next >
Text File  |  1993-04-19  |  13KB  |  418 lines

  1. /*                    C keys                            */
  2. /*                                                      */
  3. /* The enter and space bar keys have been defined to do */
  4. /* specific C editing features.                         */
  5.  
  6. CONST
  7. compile if not defined(I_like_my_cases_under_my_switch)
  8.    I_like_my_cases_under_my_switch = 1
  9. compile endif
  10. compile if not defined(I_like_a_semicolon_supplied_after_default)
  11.    I_like_a_semicolon_supplied_after_default = 0
  12. compile endif
  13. compile if not defined(ADD_BREAK_AFTER_DEFAULT)
  14.    ADD_BREAK_AFTER_DEFAULT = 1
  15. compile endif
  16. compile if not defined(WANT_BRACE_BELOW_STATEMENT)
  17.    WANT_BRACE_BELOW_STATEMENT = 0
  18. compile endif
  19. compile if not defined(USE_ANSI_C_NOTATION)
  20.    USE_ANSI_C_NOTATION = 1  -- 1 means use shorter ANSI C notation on MAIN.
  21. compile endif
  22.  
  23. compile if INCLUDING_FILE <> 'EXTRA.E'  -- Following only gets defined in the base
  24. compile if EVERSION >= '4.12'
  25. ;  Keyset selection is now done once at file load time, not every time
  26. ;  the file is selected.  And because the DEFLOAD procedures don't have to be
  27. ;  kept together in the macros (ET will concatenate all the DEFLOADs the
  28. ;  same way it does DEFINITs), we can put the DEFLOAD here where it belongs,
  29. ;  with the rest of the keyset function.  (what a concept!)
  30. ;
  31. defload
  32.    universal load_ext
  33. compile if EPM
  34.    universal load_var
  35. compile endif
  36. ;                                               C++                       Data Base Manager
  37.    if load_ext='C' or load_ext='H' or load_ext='CPP' or load_ext='CXX' or load_ext='SQC' then
  38.       keys   C_keys
  39.  compile if C_TABS <> 0
  40.   compile if EPM
  41.       if not (load_var // 2) then  -- 1 would be on if tabs set from EA EPM.TABS
  42.   compile endif
  43.       'tabs' C_TABS
  44.   compile if EPM
  45.       endif
  46.   compile endif
  47.  compile endif
  48.  compile if C_MARGINS <> 0
  49.   compile if EPM
  50.       if not (load_var%2 - 2*(load_var%4)) then  -- 2 would be on if tabs set from EA EPM.MARGINS
  51.   compile endif
  52.       'ma'   C_MARGINS
  53.   compile if EPM
  54.       endif
  55.   compile endif
  56.  compile endif
  57.    endif
  58. compile endif
  59.  
  60. compile if WANT_CUA_MARKING & EPM
  61.  defkeys c_keys clear
  62. compile else
  63.  defkeys c_keys
  64. compile endif
  65.  
  66. compile if EVERSION >= 5
  67. def space=
  68. compile else
  69. def ' '=
  70. compile endif
  71.    universal expand_on
  72.    if expand_on then
  73.       if  not c_first_expansion() then
  74.          keyin ' '
  75.       endif
  76.    else
  77.       keyin ' '
  78.    endif
  79.  compile if EVERSION >= '5.20'
  80.    undoaction 1, junk                -- Create a new state
  81.  compile endif
  82.  
  83. compile if ASSIST_TRIGGER = 'ENTER'
  84. def enter=
  85.  compile if ENHANCED_ENTER_KEYS & ENTER_ACTION <> ''
  86.    universal enterkey
  87.  compile endif
  88. compile else
  89. def c_enter=
  90.  compile if ENHANCED_ENTER_KEYS & c_ENTER_ACTION <> ''
  91.    universal c_enterkey
  92.  compile endif
  93. compile endif
  94.    universal expand_on
  95.  
  96. compile if EVERSION >= 5
  97.    if expand_on then
  98. compile else
  99.    if expand_on & not command_state() then
  100. compile endif
  101. compile if EVERSION <= '4.12'
  102.       if c_second_expansion() then
  103.          call maybe_autosave()
  104.       else
  105. compile else
  106.       if not c_second_expansion() then
  107. compile endif
  108. compile if ASSIST_TRIGGER = 'ENTER'
  109.  compile if ENHANCED_ENTER_KEYS & ENTER_ACTION <> ''
  110.          call enter_common(enterkey)
  111.  compile else
  112.          call my_enter()
  113.  compile endif
  114. compile else  -- ASSIST_TRIGGER
  115.  compile if ENHANCED_ENTER_KEYS & c_ENTER_ACTION <> ''
  116.          call enter_common(c_enterkey)
  117.  compile else
  118.          call my_c_enter()
  119.  compile endif
  120. compile endif -- ASSIST_TRIGGER
  121.       endif
  122.    else
  123. compile if ASSIST_TRIGGER = 'ENTER'
  124.  compile if ENHANCED_ENTER_KEYS & ENTER_ACTION <> ''
  125.       call enter_common(enterkey)
  126.  compile else
  127.       call my_enter()
  128.  compile endif
  129. compile else  -- ASSIST_TRIGGER
  130.  compile if ENHANCED_ENTER_KEYS & c_ENTER_ACTION <> ''
  131.       call enter_common(c_enterkey)
  132.  compile else
  133.       call my_c_enter()
  134.  compile endif
  135. compile endif -- ASSIST_TRIGGER
  136.    endif
  137.  
  138. /* Taken out, interferes with some people's c_enter. */
  139. ;def c_enter=   /* I like Ctrl-Enter to finish the comment field also. */
  140. ;   getline line
  141. ;   if pos('/*',line) then
  142. ;      if not pos('*/',line) then
  143. ;         end_line;keyin' */'
  144. ;      endif
  145. ;   endif
  146. ;   down;begin_line
  147.  
  148. def c_x=       /* Force expansion if we don't have it turned on automatic */
  149.    if not c_first_expansion() then
  150.       call c_second_expansion()
  151.    endif
  152. compile endif  -- EXTRA
  153.  
  154. compile if not EXTRA_EX or INCLUDING_FILE = 'EXTRA.E'  -- Following gets defined in EXTRA.EX if it's being used
  155. defproc c_first_expansion
  156.    retc=1
  157. compile if EVERSION >= 5
  158.    if .line then
  159. compile else
  160.    if .line and (not command_state()) then
  161. compile endif
  162.       getline line
  163.       line=strip(line,'T')
  164.       w=line
  165.       wrd=upcase(w)
  166.       ws = substr(line, 1, max(verify(line, ' '\9)-1,0))
  167.       if wrd='FOR' then
  168. compile if WANT_BRACE_BELOW_STATEMENT
  169.          replaceline w' (; ; )'
  170.          insertline ws'{',.line+1
  171.          insertline ws'} /* endfor */',.line+2
  172. compile else
  173.          replaceline w' (; ; ) {'
  174.          insertline ws'} /* endfor */',.line+1
  175. compile endif -- WANT_BRACE_BELOW_STATEMENT
  176.          if not insert_state() then insert_toggle
  177. compile if EVERSION >= '5.50'
  178.              call fixup_cursor()
  179. compile endif
  180.          endif
  181.          .col=.col+2
  182.       elseif wrd='IF' then
  183. compile if WANT_BRACE_BELOW_STATEMENT
  184.          replaceline w' ()'
  185.          insertline ws'{',.line+1
  186.          insertline ws'}',.line+2
  187.          insertline ws'else',.line+3
  188.          insertline ws'{',.line+4
  189.          insertline ws'} /* endif */',.line+5
  190. compile else
  191.          replaceline w' () {'
  192.          insertline ws'} else {',.line+1
  193.          insertline ws'} /* endif */',.line+2
  194. compile endif -- WANT_BRACE_BELOW_STATEMENT
  195.          if not insert_state() then insert_toggle
  196. compile if EVERSION >= '5.50'
  197.          call fixup_cursor()
  198. compile endif
  199.          endif
  200.          .col=.col+2
  201.       elseif wrd='WHILE' then
  202. compile if WANT_BRACE_BELOW_STATEMENT
  203.          replaceline w' ()'
  204.          insertline ws'{',.line+1
  205.          insertline ws'} /* endwhile */',.line+2
  206. compile else
  207.          replaceline w' () {'
  208.          insertline ws'} /* endwhile */',.line+1
  209. compile endif -- WANT_BRACE_BELOW_STATEMENT
  210.          if not insert_state() then insert_toggle
  211. compile if EVERSION >= '5.50'
  212.              call fixup_cursor()
  213. compile endif
  214.          endif
  215.          .col=.col+2
  216.       elseif wrd='DO' then
  217. compile if WANT_BRACE_BELOW_STATEMENT
  218.          insertline ws'{',.line+1
  219.          down
  220. compile else
  221.          replaceline w' {'
  222. compile endif -- WANT_BRACE_BELOW_STATEMENT
  223.          insertline ws'} while (  ); /* enddo */',.line+1
  224.          call einsert_line()
  225.          .col=.col+SYNTAX_INDENT    /* indent for new line */
  226.       elseif wrd='SWITCH' then
  227. compile if WANT_BRACE_BELOW_STATEMENT
  228.          replaceline w' ()'
  229.          insertline ws'{',.line+1
  230.          insertline substr(wrd,1,length(wrd)-6)'} /* endswitch */',.line+2
  231. compile else
  232.          replaceline w' () {'
  233.          insertline substr(wrd,1,length(wrd)-6)'} /* endswitch */',.line+1
  234. compile endif -- WANT_BRACE_BELOW_STATEMENT
  235.          if not insert_state() then insert_toggle
  236. compile if EVERSION >= '5.50'
  237.              call fixup_cursor()
  238. compile endif
  239.          endif
  240.          .col=.col+2    /* move cursor between parentheses of switch ()*/
  241.       elseif wrd='MAIN' then
  242.          call enter_main_heading()
  243.       else
  244.          retc=0
  245.       endif
  246.    else
  247.       retc=0
  248.    endif
  249.    return retc
  250.  
  251. defproc c_second_expansion
  252.    retc=1
  253.    if .line then
  254.       getline line
  255.       parse value upcase(line) with '{' +0 a
  256.       brace = pos('{', line)
  257.       if .line < .last then
  258.          next_is_brace = textline(.line+1)='{'
  259.       else
  260.          next_is_brace = 0
  261.       endif
  262.       parse value line with wrd rest
  263.       i=verify(wrd,'({:;','M',1)-1
  264.       if i<=0 then i=length(wrd) endif
  265.       firstword=upcase(substr(wrd,1,i))
  266.       if firstword='FOR' then
  267.          /* do tabs to fields of C for statement */
  268.          cp=pos(';',line,.col)
  269.          if cp and cp>=.col then
  270.              .col=cp+2
  271.          else
  272.            cpn=pos(';',line,cp+1)
  273.            if cpn and (cpn>=.col) then
  274.              .col=cpn+2
  275.            else
  276.               if not brace and next_is_brace then down; endif
  277.              call einsert_line()
  278.              .col=.col+SYNTAX_INDENT
  279.            endif
  280.          endif
  281.       elseif firstword='CASE' or firstword='DEFAULT' then
  282.          call einsert_line()
  283.          if .line>2 then  /* take a look at the previous line */
  284.             getline prevline,.line-2
  285.             prevline=upcase(prevline)
  286.             parse value prevline with w .
  287.             if pos('(', w) then
  288.                parse value w with w '('
  289.             endif
  290.             if w='CASE' then  /* align case statements */
  291.                i=pos('C',prevline)
  292.                replaceline substr('',1,i-1)||wrd rest,.line-1
  293.                .col=i
  294.             elseif w<>'CASE' and w<>'SWITCH' and w<>'{' and prevline<>'' then  /* shift current line over */
  295.                i=verify(prevline,' ')
  296.                if i then .col=i endif
  297.                if i>SYNTAX_INDENT then i=i-SYNTAX_INDENT else i=1 endif
  298.                .col=i
  299.                replaceline substr('',1,i-1)||wrd rest,.line-1
  300.             endif
  301.             /* get rid of line containing just a ; */
  302.             if firstword='DEFAULT' and .line <.last then
  303.                getline line,.line+1
  304.                if line=';' then
  305.                   deleteline .line+1
  306.                endif
  307.             endif
  308.          endif
  309.          .col=.col+SYNTAX_INDENT
  310.       elseif firstword='BREAK' then
  311.          call einsert_line()
  312.          c=.col
  313.          if .col>SYNTAX_INDENT then
  314.             .col=.col-SYNTAX_INDENT
  315.          endif
  316.          keyin 'case :';left
  317.          insertline substr('',1,c-1)'break;',.line+1
  318.       elseif firstword='SWITCH' then
  319.          if not brace and next_is_brace then down; endif
  320.          call einsert_line()
  321.          c=.col
  322. compile if I_like_my_cases_under_my_switch
  323.          keyin 'case :';left
  324. compile else
  325.          keyin substr(' ',1,SYNTAX_INDENT)'case :';left
  326.          c=c+SYNTAX_INDENT
  327. compile endif
  328.          insertline substr(' ',1,c+SYNTAX_INDENT-1)'break;',.line+1
  329.          /* look at the next line to see if this is the first time */
  330.          /* the user typed enter on this switch statement */
  331.          if .line<=.last-2 then
  332.             getline line,.line+2
  333.             i=verify(line,' ')
  334.             if i then
  335.                if substr(line,i,1)='}' then
  336. compile if I_like_my_cases_under_my_switch
  337.                   if i>1 then
  338.                      i=i-1
  339.                      insertline substr(' ',1,i)'default:',.line+2
  340.                   else
  341.                      insertline 'default:',.line+2
  342.                   endif
  343. compile else
  344.                   i=i+SYNTAX_INDENT-1
  345.                   insertline substr(' ',1,i)'default:',.line+2
  346. compile endif
  347. compile if ADD_BREAK_AFTER_DEFAULT
  348.                   insertline substr(' ',1,i+SYNTAX_INDENT-1)'break;',.line+3
  349. compile elseif I_like_a_semicolon_supplied_after_default then
  350.                   insertline substr(' ',1,i+SYNTAX_INDENT)';',.line+3
  351. compile endif
  352.                endif
  353.             endif
  354.          endif
  355.       elseif a='{' or firstword='{' then  /* firstword or last word {?*/
  356. ;        if firstword='{' then
  357. ;           replaceline  wrd rest      -- This shifts the { to col 1.  Why???
  358. ;           call einsert_line();.col=SYNTAX_INDENT+1
  359. ;        else
  360.             call einsert_line()
  361.             .col=.col+SYNTAX_INDENT
  362. ;        endif
  363.       elseif firstword='MAIN' then
  364.          call enter_main_heading()
  365. compile if EPM
  366.       elseif firstword<>'' & wordpos(firstword, 'DO IF ELSE WHILE') then
  367. compile else
  368.       elseif pos(' 'firstword' ', ' DO IF ELSE WHILE ') then
  369. compile endif
  370.          if not brace and next_is_brace then down; endif
  371.          call einsert_line()
  372.          .col=.col+SYNTAX_INDENT
  373. ;        insert
  374. ;        .col=length(a)+2
  375.       elseif pos('/*',line) then
  376.          if not pos('*/',line) then
  377.             end_line;keyin' */'
  378.          endif
  379.          call einsert_line()
  380.       else
  381.          retc=0
  382.       endif
  383.    else
  384.       retc=0
  385.    endif
  386.    return retc
  387.  
  388. defproc enter_main_heading
  389. compile if not USE_ANSI_C_NOTATION     -- Use standard notation
  390.    temp=substr('',1,SYNTAX_INDENT)  /* indent spaces */
  391.    replaceline 'main(argc, argv, envp)'
  392.    insertline temp'int argc;',.line+1         /* double indent */
  393.    insertline temp'char *argv[];',.line+2
  394.    insertline temp'char *envp[];',.line+3
  395.    insertline '{',.line+4
  396.    insertline '',.line+5
  397.    mainline = .line
  398.    if .cursory<7 then
  399.       .cursory=7
  400.    endif
  401.    mainline+5
  402.    .col=SYNTAX_INDENT+1
  403.    insertline '}',.line+1
  404. compile else                           -- Use shorter ANSII notation
  405.    replaceline 'main(int argc, char *argv[], char *envp[])'
  406.    insertline '{',.line+1
  407.    insertline '',.line+2
  408.    .col=SYNTAX_INDENT+1
  409.    insertline '}',.line+3
  410.    mainline = .line
  411.    if .cursory<4 then
  412.       .cursory=4
  413.    endif
  414.    mainline+2
  415. compile endif
  416.  
  417. compile endif  -- EXTRA
  418.