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