home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / editor / me / misc.m < prev    next >
Text File  |  1994-01-31  |  10KB  |  365 lines

  1. /*=========================================================================*/
  2. /*                                                                         */
  3. /*  MISC - assorted macros for the ME Text Editor.                         */
  4. /*         Some of these macros are not earth-shattering, but are included */
  5. /*         for demonstration purposes.                                     */
  6. /*                                                                         */
  7. /*  Written by Marc Adler  Magma Systems                                   */
  8. /*             (Creators of ME and New York Word)                          */
  9. /*                                                                         */
  10. /* Functions :                                                             */
  11. /*   copy_ch_above - copies the char above the cursor & inserts it at the  */
  12. /*                   cursor position                                       */
  13. /*   copy_wd_above - copies the word above the cursor & inserts it at the  */
  14. /*                   cursor position                                       */
  15. /*                                                                         */
  16. /*                                                                         */
  17. /*                                                                         */
  18. /*=========================================================================*/
  19.  
  20. #include mekeys.h
  21.  
  22. int     CBRACE;   /* C Braces */
  23.  
  24. init()
  25. {
  26.   assign_key("copy_ch_above", CTRL_C);
  27.   assign_key("copy_wd_above", CTRL_W);
  28.   assign_key("downpara", 14);
  29.   assign_key("uppara",   16);
  30. }
  31.  
  32. copy_ch_above()
  33. {
  34.   int c;
  35.  
  36.   if (up())             /* if we're not at the first line */
  37.   {                     /* go up and get the char above the cursor position */
  38.     if (!is_eol())
  39.       c = currchar();
  40.     else
  41.       c = ' ';
  42.     down();
  43.     if (c == 0)         /* check for a NULL character */
  44.       c = ' ';
  45.     insert(chr(c));     /* insert the string equivalent of the character */
  46.   }
  47. }
  48.  
  49. copy_wd_above()
  50. {
  51.   int col1, col2;
  52.   string str;
  53.  
  54.   save_position();
  55.   str = "";
  56.  
  57.   if (up() && !is_eol())                        /* move the cursor up */
  58.   {
  59.     col1 = currcol();                           /* save the starting column */
  60.     while (!is_eol() && currchar() == ' ')      /* move to 1st char of word */
  61.       right(); 
  62.     while (!is_eol() && currchar() != ' ')      /* move to end of word */
  63.       right();
  64.     col2 = currcol();                           /* get current column */
  65.     if (col2 > col1)                            /* extract the word */
  66.       str = substr(currline(), col1, col2 - col1);
  67.   }
  68.  
  69.   restore_position();
  70.   insert(str);
  71. }
  72.  
  73.  
  74. /* These macros move up and down one paragraph. C programmers who want */
  75. /* to move up and down one function can alter these macros. */
  76.  
  77. downpara()
  78. {
  79.   while (!is_line_blank(currline()) && currlinenum() < lastlinenum())
  80.     down();
  81.   while (is_line_blank(currline())  && currlinenum() < lastlinenum())
  82.     down();
  83. }
  84.  
  85. uppara()
  86. {
  87.   if (!is_line_blank(currline()))
  88.     up();      /* don't get stuck at first line of para */
  89.   while (is_line_blank(currline())  && currlinenum() > 1)
  90.     up();
  91.   while (!is_line_blank(currline()) && currlinenum() > 1)
  92.     up();
  93.   if (currlinenum() > 1)
  94.     down();
  95. }
  96.  
  97. is_line_blank(str)
  98.   string str;
  99. {
  100.   int i;
  101.   int len;
  102.  
  103.   len = strlen(str);
  104.  
  105.   for (i = 1;  i <= len;  i = i + 1)
  106.     if (substr(str, i, 1) != " ")
  107.       return 0;
  108.   return 1;
  109. }
  110.  
  111.  
  112. /* This macro shifts a marked block of text left or right. You can do */
  113. /* the same thing my marking the area to indent, using the 'indent'   */
  114. /* block command, and using the arrow keys. */
  115.  
  116. shift()
  117. {
  118.   int line1, line2;
  119.   int n, j;
  120.  
  121.   /* Prompt the user for the indent width, and check for a bad number. */
  122.   n = atoi(get_tty_str("How many spaces (default 2) ?"));
  123.   if (n == 0)  n = 2;
  124.  
  125.   line1 = marked_line();    /* get the marked range */
  126.   line2 = currlinenum();
  127.   goline(line1);            /* set the cursor to the start of the area */
  128.   gobol();
  129.  
  130.   while (currlinenum() <= line2)
  131.   {
  132.     if (n >= 0)         /* shift rightwards - insert n spaces */
  133.     {
  134.       gobol();
  135.       insert(repstr(" ", n));
  136.     }
  137.     else                /* a negative amount means to shift leftwards */
  138.     {
  139.       j = -n;
  140.       while (j > 0)     /* delete 'n' characters */
  141.       {
  142.         delchar();
  143.         j = j - 1;
  144.       }
  145.     }
  146.     down();             /* move down to the next line */
  147.   }
  148.  
  149.   clear_mark();         /* remove any existing marks */
  150. }
  151.  
  152.  
  153.  
  154. /**************************************************************************/
  155. /* The following macros were written by Blake McBride of Miami, Fla. They */
  156. /* were written for version 1.2, so they could probably be trimmed down   */
  157. /* by using the new library functions.                                    */
  158. /* Any macro contributions will be added to the library - send 'em in!!!  */
  159. /**************************************************************************/
  160.  
  161. /*************************************************************************
  162.   If you use any of Blake's macros, you might want to place these key
  163.   assignments (and the initialization of CBRACE) in the init() function.
  164.  
  165.   assign_key("scroll_up",   CTRL_F1);
  166.   assign_key("scroll_down", CTRL_F2);
  167.   assign_key("lbrace", '{');
  168.   assign_key("rbrace", '}');
  169.   CBRACE = 1; 
  170.   assign_key("delete_white_space", ALT_D);
  171.   assign_key("line_to_top", ALT_T);
  172. *************************************************************************/
  173.  
  174. /* lbrace */
  175. /* If CBRACE is on, insert a brace and start the next line past the indent */
  176. lbrace()
  177. {      
  178.   /* If the CBRACE variable is not set, then insert a brace literally */
  179.   if (!CBRACE)  {
  180.     insert("{");
  181.     return;
  182.   }
  183.  
  184.   /* Insert a brace, and append a new line */
  185.   insert("{\n");
  186.   up();
  187.   /* Find the first non-blank character on the line */
  188.   for (col = 0; currchar() == ' ';  col++)
  189.     right();
  190.   /* Move to the next line and indent properly */
  191.   gobol();             
  192.   down();
  193.   insert(repstr(" ", col));
  194.   insert("\t");   /* tab past the indentation */
  195. }
  196.  
  197. /* rbrace */
  198. /* If CBRACE is on, then insert the right brace at the appropriate column */
  199. rbrace()
  200. {
  201.   int  col, i, nb, cc, back;
  202.  
  203.   if (!CBRACE)  {
  204.     insert("}");
  205.     return;
  206.   }
  207.  
  208.   gobol();
  209.   save_position();        
  210.   back = nb = 0;
  211.  
  212.   while (nb || rsearch("{|}"))
  213.   {
  214.     /* If we have a brace, adjust the level */
  215.     if ((cc = currchar()) == '}')
  216.       nb++;
  217.     else if (cc == '{')
  218.       if (--nb <= 0)  break;
  219.  
  220.     /* Move back a character (6000 chars from the starting pt maximum) */
  221.     if (!left())
  222.     { /* Can't find the match - just insert } at the cursor */
  223.       restore_position();
  224.       insert("}");
  225.       return;
  226.     }
  227.   }
  228.  
  229.   /* Move to the first non-blank on the left-brace line. Col is the
  230.      column number of the non-blank */
  231.   col = currcol();
  232.   restore_position();
  233.  
  234.   /* Go to the line we want to insert the right brace in. Move to the
  235.      column which the left brace occured in & insert the matching '}'  */
  236.   insert(repstr(" ", col-1));
  237.   insert("}\n");
  238.  
  239.   /* Auto-indent next line */
  240.   insert(repstr(" ", col-1));
  241. }
  242.   
  243.   
  244. /* These two functions set and clear CBRACE mode */
  245. cbrace_on()
  246. {
  247.   CBRACE = 1;
  248. }
  249.  
  250. cbrace_off()
  251. {
  252.   CBRACE = 0;
  253. }
  254.  
  255.  
  256.  
  257. /* Scroll the screen down 1 line */
  258. scroll_down()
  259. {    
  260.   int     flag;
  261.   
  262.   if (get_window_size() != get_window_line())  {
  263.     flag = 1;
  264.     save_position();
  265.   }
  266.   else
  267.     flag = 0;
  268.   home();
  269.   up();
  270.   if (flag)
  271.     restore_position();
  272.   else
  273.     bottom();
  274. }
  275.  
  276. /* Scroll the screen up one line */
  277. scroll_up()
  278. {
  279.   if (get_window_line() > 1)  {
  280.     flag = 1;
  281.     save_position();
  282.   }
  283.   else
  284.     flag = 0;
  285.   bottom();
  286.   down();
  287.   if (flag)
  288.     restore_position();
  289.   else
  290.     home();
  291. }
  292.  
  293. /* ALT T */
  294. /* Reposition the current window so the current line is at the top */
  295. line_to_top()
  296. {
  297.   n = get_window_line();
  298.   bottom();
  299.   while (n--)
  300.     if (!down())     break;
  301.   home();
  302. }
  303.  
  304. /* Returns the line number relative to the top of the window */
  305. get_window_line()
  306. {          
  307.   save_position();
  308.   last = currlinenum();
  309.   home();
  310.   last -= currlinenum() - 1;
  311.   restore_position();           
  312.   return last;
  313. }
  314.  
  315. /* ALT D */
  316. /* Delete all white space surrounding the cursor */
  317. delete_white_space()
  318. {
  319.   while (currchar() == ' ')     /* trim right */
  320.     delchar();
  321.   while (!is_bol())  {          /* trim left  */
  322.     left();
  323.     if (currchar() != ' ')  {
  324.       right();
  325.       break;
  326.     }
  327.     delchar();
  328.   }
  329. }
  330.  
  331. /* string_size */
  332. /* The string_size function assumes that the cursor in somewhere within */
  333. /* a C string. It tells the user how long the string is.                */
  334. string_size()
  335. {
  336.   int  i, c;
  337.   
  338.   save_position();
  339.  
  340.   /* Move left until we find the starting quote */
  341.   left();
  342.   while (currchar() != '"'  &&  !is_bol())
  343.     left();
  344.  
  345.   if (currchar() != '"')  {      /* no starting quote */
  346.     restore_position();
  347.     bell();
  348.     return;
  349.   }
  350.  
  351.   /* Move to the closing quote */
  352.   right();
  353.   for (i=0 ; (c=currchar())  &&  c != '"' ; ++i)
  354.     right();
  355.   restore_position();
  356.   if (c != '"')  {      /* unmatched quotes */
  357.     bell();
  358.     return;
  359.   }
  360.  
  361.   /* Tell the user the length of the string and wait for a key press */
  362.   message(sprintf("String size = %d", i));
  363.   get_tty_char();
  364. }
  365.