home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / bracket.cpp < prev    next >
C/C++ Source or Header  |  2001-10-29  |  5KB  |  196 lines

  1. /*
  2. ** Module   :BRACKET.CPP
  3. ** Abstract :Bracket matching routine
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Mon  15/03/1998     Created
  8. */
  9.  
  10. #include <buffer.h>
  11. #include <version.h>
  12.  
  13. #define UNDO    1
  14.  
  15. #ifndef max
  16. #define max(a,b) (((a) > (b)) ? (a) : (b))
  17. #define min(a,b) (((a) < (b)) ? (a) : (b))
  18. #endif
  19.  
  20.  
  21. //-----------------------------------------
  22. // Bracket matching v 0.0.1a
  23. //-----------------------------------------
  24.  
  25. struct brackets_pair
  26. {
  27.     char cLB;
  28.     char cRB;
  29. };
  30.  
  31. static brackets_pair brackets[]=
  32. {
  33.     {'(',')'},
  34.     {'{','}'},
  35.     {'[',']'},
  36.     {'<','>'}
  37. };
  38.  
  39. #define BM_FORWARD  0
  40. #define BM_BACKWARD 1
  41. #define BM_NONE     2
  42.  
  43. void Buffer::match_bracket(Rect& rect)
  44. {
  45.     int i;
  46.     int direction = BM_NONE;
  47.     int pair  = 0;
  48.     int level = 0;
  49.     int an_const = 0;
  50.  
  51.     char bracket = (char)abs_line()->char_at(abs_col());
  52.  
  53.     for(i = 0; i < (sizeof(brackets)/sizeof(brackets[0])); i++)
  54.     {
  55.         if(bracket == brackets[i].cLB)
  56.         {
  57.             // Bracket found, direction forward
  58.             direction = BM_FORWARD;
  59.             pair = i;
  60.             break;
  61.         }
  62.  
  63.         if(bracket == brackets[i].cRB)
  64.         {
  65.             // Bracket found, direction backward
  66.             direction = BM_BACKWARD;
  67.             pair = i;
  68.             break;
  69.         }
  70.     }
  71.  
  72.     if(abs_col() &&
  73.        abs_line()->char_at(abs_col() - 1) == '\'' &&
  74.        abs_line()->char_at(abs_col() + 1) == '\'')
  75.     {
  76.         an_const = 1;
  77.     }
  78.  
  79.     if(direction == BM_NONE)
  80.         return;
  81.  
  82.     if(direction == BM_FORWARD)
  83.     {
  84.         //Direction: forward
  85.         //quickly looking for bracket, then check other parameters
  86.         int _row = abs_row();
  87.         int _col = abs_col()+1;
  88.  
  89.         while(_row < Count())
  90.         {
  91.             PLine ln = line(_row);
  92.  
  93.             for(; _col < ln->len(); _col++)
  94.             {
  95.                 if(ln->char_at(_col) != brackets[pair].cLB &&
  96.                    ln->char_at(_col) != brackets[pair].cRB)
  97.                 {
  98.                     continue;
  99.                 }
  100.  
  101.                 if(an_const && (!_col ||
  102.                                 ln->char_at(_col - 1) != '\'' ||
  103.                                 ln->char_at(_col + 1) != '\''))
  104.                 {
  105.                     continue;
  106.                 }
  107.  
  108.                 if(!an_const &&
  109.                    ln->char_at(_col - 1) == '\'' &&
  110.                    ln->char_at(_col + 1) == '\'')
  111.                 {
  112.                     continue;
  113.                 }
  114.  
  115.                 if(ln->char_at(_col) == brackets[pair].cLB)
  116.                 {
  117.                     level++;
  118.                     continue;
  119.                 }
  120.  
  121.                 if(ln->char_at(_col) == brackets[pair].cRB)
  122.                 {
  123.                      if(level)
  124.                      {
  125.                          level--;
  126.                          continue;
  127.                      }
  128.  
  129.                      if(_row != abs_row())
  130.                          goto_line(rect, _row);
  131.                      goto_col(rect, _col);
  132.                      return;
  133.                 }
  134.             }
  135.             _row++;
  136.             _col = 0;
  137.         }
  138.     }
  139.     else
  140.     {
  141.         int _row = abs_row();
  142.         int _col = abs_col()-1;
  143.  
  144.         while(_row >= 0)
  145.         {
  146.             PLine ln = line(_row);
  147.  
  148.             for(; _col >= 0; _col--)
  149.             {
  150.                 if(ln->char_at(_col) != brackets[pair].cLB &&
  151.                    ln->char_at(_col) != brackets[pair].cRB)
  152.                 {
  153.                     continue;
  154.                 }
  155.  
  156.                 if(an_const && (!_col ||
  157.                                 ln->char_at(_col - 1) != '\'' ||
  158.                                 ln->char_at(_col + 1) != '\''))
  159.                 {
  160.                     continue;
  161.                 }
  162.  
  163.                 if(!an_const &&
  164.                    ln->char_at(_col - 1) == '\'' &&
  165.                    ln->char_at(_col + 1) == '\'')
  166.                 {
  167.                     continue;
  168.                 }
  169.  
  170.                 if(ln->char_at(_col) == brackets[pair].cRB)
  171.                 {
  172.                     level++;
  173.                     continue;
  174.                 }
  175.  
  176.                 if(ln->char_at(_col) == brackets[pair].cLB)
  177.                 {
  178.                     if(level)
  179.                     {
  180.                         level--;
  181.                         continue;
  182.                     }
  183.  
  184.                     if(_row != abs_row())
  185.                         goto_line(rect, _row);
  186.                     goto_col(rect, _col);
  187.                     return;
  188.                 }
  189.             }
  190.             _row--;
  191.             _col = line(_row) ? line(_row)->len() : 0;
  192.         }
  193.     }
  194. }
  195.  
  196.