home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / DEMO / RIM22 / MACROS / MISC.RM < prev    next >
Text File  |  1993-11-10  |  6KB  |  268 lines

  1. /*
  2. ** Macro module: misc.c - Misc. functions
  3. **
  4. ** Copyright (C) 1993 Brian L. Smith
  5. ** Copyright (C) 1993 RimStar Technology, Inc.
  6. ** All rights reserved internationally.
  7. ** Unlicensed use is a violation of applicable laws.
  8. **
  9. ** This source code is provided to licensed users of RimStar's products
  10. ** for the purpose of allowing the user to customize and/or enhance RimStar's
  11. ** products. The source code remains the property of the copyright holders
  12. ** with all rights reserved internationally.
  13. ** Any modifications to the source code are considered derivative works and
  14. ** all rights thereto are reserved to the copyright holders except
  15. ** that the purchaser may use the derivitive work in the same manner
  16. ** as permitted by the license governing the unmodified product.
  17. ** Distribution in any manner of any part of the original source code,
  18. ** whether in source or object form, is expressly prohibited without the
  19. ** express written permission of the copyright holders.
  20. **
  21. */
  22.  
  23. #include "macro.h"
  24.  
  25. void
  26. quote(void) {
  27.     PRAWKEY    pKey;
  28.  
  29.     /* Wait for a key */
  30.     while ( !(pKey = KbdBufferQueryKey()) )
  31.         ;
  32.     if ( pKey->ascii && !(pKey->key & KC_ALT) ) {
  33.          pKey = KbdBufferGetKey();    /* remove from keyboard buffer */
  34.          BufInsertChar(pKey->ascii);
  35.     }
  36. } /* end quote() */
  37.  
  38.  
  39. long
  40. __next_word(void) {
  41.  
  42.     return SrchFwd("([^ \t\f\n]+)|([ \t\f\n]*)[ \t\f\n]+\\c[^ \t\f\n]", -1);
  43.  
  44. } /* end __next_word() */
  45.  
  46.  
  47. long
  48. __prev_word(void) {
  49.     PosPrevChar();
  50.     while ( strchr(" \t\f", BufQueryChar()) )
  51.         if ( PosPrevChar() )
  52.             return 0;
  53.     return SrchBack("^|[ \t\f]\\c[^ \t\f\n]+", -1);
  54. } /* end __prev_word() */
  55.  
  56.  
  57. void
  58. next_word(void) {
  59.     char  *pathname;
  60.     char  ext[5];
  61.     char  *func;
  62.  
  63.     /* Determine file extension of current buffer */
  64.     pathname = BufQueryFilename();
  65.     if ( !pathname ) {
  66.         __next_word();
  67.         return;
  68.     }
  69.     /* Build function name from file extension */
  70.     splitpath(pathname, NULL, NULL, NULL, ext);
  71.     func = malloc(32);
  72.     ext[0] = '_';
  73.     strcpy(func, ext);
  74.     strlwr(func);
  75.     strcat(func, "_next_word");
  76.     if ( LibQueryFunction(func) )    /* is there an extension specific version */
  77.         ExecFunction(func);
  78.     else
  79.         __next_word();
  80.     free(func);
  81. } /* end next_word() */
  82.  
  83.  
  84. void
  85. prev_word(void) {
  86.     char  *pathname;
  87.     char  ext[5];
  88.     char  *func;
  89.  
  90.     /* Determine file extension of current buffer */
  91.     pathname = BufQueryFilename();
  92.     if ( !pathname ) {
  93.         __prev_word();
  94.         return;
  95.     }
  96.     /* Build function name from file extension */
  97.     splitpath(pathname, NULL, NULL, NULL, ext);
  98.     func = malloc(32);
  99.     ext[0] = '_';
  100.     strcpy(func, ext);
  101.     strlwr(func);
  102.     strcat(func, "_prev_word");
  103.     if ( LibQueryFunction(func) )    /* is there an extension specific version */
  104.         ExecFunction(func);
  105.     else
  106.         __prev_word();
  107.     free(func);
  108. } /* end prev_word() */
  109.  
  110.  
  111. int
  112. delete_next_word() {
  113.     long    startPos;
  114.     long    endPos;
  115.     long    startLine;
  116.     long    endLine;
  117.  
  118.     startPos  = BufQueryOffset();
  119.     startLine = BufQueryLine();
  120.     MarkPushPos();
  121.  
  122.     next_word();
  123.  
  124.     endPos  = BufQueryOffset();
  125.     endLine = BufQueryLine();
  126.  
  127.     if ( endPos != startPos ) {
  128.         MarkPopPos();
  129.         if ( endLine != startLine )
  130.             BufDeleteToEOL();
  131.         else
  132.             BufDeleteChar(endPos - startPos);
  133.         return 1;
  134.     }
  135.     MarkPopPos();
  136.     return 0;
  137. } /* end delete_next_word() */
  138.  
  139.  
  140. int
  141. delete_prev_word() {
  142.     long    startPos;
  143.     long    size;
  144.  
  145.     startPos = BufQueryOffset();
  146.     MarkPushPos();
  147.  
  148.     prev_word();
  149.  
  150.     if ( size = startPos - BufQueryOffset() ) {
  151.         BufDeleteChar(size);
  152.         MarkPopPos(0);    /* discard saved position */
  153.         return 1;
  154.     }
  155.     MarkPopPos();
  156.     return 0;
  157. } /* end delete_prev_word() */
  158.  
  159.  
  160. /*
  161. ** open_line - creates an empty line following the current line
  162. */
  163. void
  164. open_line(void) {
  165.     char    *pszFunc;
  166.  
  167.     MovEOL();
  168.  
  169.     pszFunc = KbdQueryFunction(KbdStringToKeys("Enter"), 1);
  170.     if ( !strcmp(pszFunc, "SelfInsert") || !strcmp(pszFunc, "open_line") )
  171.         BufInsertNewline();
  172.     else
  173.         ExecFunction(pszFunc);
  174. } /* end open_line() */
  175.  
  176.  
  177. void
  178. process_selection(char *(*func)() ) {
  179.     SELECTION    sel;
  180.     int            type;
  181.     long            atLine;
  182.     long            offset;
  183.     long            size;
  184.     char            *s1, *s2;
  185.  
  186.     MarkPushPos();
  187.  
  188.     if ( !MarkQuerySelType() )
  189.         MarkLine();
  190.  
  191.     type = MarkQuerySel(&sel, 1);
  192.  
  193.     PosAbs(atLine = sel.s_line, sel.s_column);
  194.  
  195.     while ( atLine <= sel.e_line ) {
  196.         MarkPushPos();
  197.         offset = BufQueryOffset();
  198.         if ( atLine == sel.e_line || type == SELECT_COLUMN )
  199.             PosAbs(0, sel.e_column);
  200.         else
  201.             PosEOL();
  202.         size = BufQueryOffset() - offset + 1;
  203.         MarkPopPos(1);
  204.         s1 = BufRead(size);
  205.         s2 = strdup(s1);
  206.         s2 = func(s2);
  207.         if ( strcmp(s1, s2) ) {
  208.             BufDeleteChar(size);
  209.             BufInsertString(s2);
  210.         }
  211.         free(s1);
  212.         free(s2);
  213.         if ( type == SELECT_COLUMN )
  214.             offset = sel.s_column;
  215.         else
  216.             offset = 1L;
  217.         PosAbs(++atLine, offset);
  218.     }
  219.     MarkPopPos(1);
  220. } /* end process_selection() */
  221.  
  222.  
  223. #undef toupper
  224. #undef tolower
  225.  
  226. void
  227. toupper() {
  228.     process_selection(strupr);
  229.     MarkRemoveSel();
  230. } /* end toupper() */
  231.  
  232.  
  233. void
  234. tolower() {
  235.     process_selection(strlwr);
  236.     MarkRemoveSel();
  237. } /* end tolower() */
  238.  
  239.  
  240. char *
  241. convert_to_space(char *s) {
  242.     long    i;
  243.  
  244.     for ( i = 0; s[i] && s[i] != '\n'; ++i )
  245.         if ( s[i] != ' ' && s[i] != '\t' )
  246.             s[i] = ' ';
  247.     return s;
  248. } /* end convert_to_space() */
  249.  
  250.  
  251. void
  252. tospace() {
  253.     SELECTION    sel;
  254.  
  255.     if ( !MarkQuerySel(&sel, 1) )
  256.         BufInsertChar(' ');
  257.     else {
  258.         process_selection(convert_to_space);
  259.         MarkRemoveSel();
  260.         MovAbs(sel.s_line, sel.s_column);
  261.     }
  262. } /* end tospace() */
  263.  
  264.  
  265. /*
  266. ** End macro: misc.rm
  267. */
  268.