🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: vmsnet.tpu
Article #77

Re: Procedure to matchup (){}[] ???

#77
From: ht
Date: Wed, 11 Nov 1992 07:58
76 lines
2439 bytes
In article <1992Nov8.053543.383@wsuhub.uc.twsu.edu>, ldgiltne@wsuhub.uc.twsu.edu (Lawrence Giltner) writes:
> Before I spend a lot of time reinventing the wheel:
> 
> Does anybody have a procedure to pair up parenthesis, brackets, and
> braces.  I would like to put the cursor on one of ()[]{} and let the
> program figure out which direction it has to search and if found, then
> marks/selects all the text between them, including the ()[]{}.
I have one that finds the corresponding paren, brac. or whatever, but it does
not select the text between - mind you that is easy: just press select
before you find the matching thingymagig.

---------
procedure find_bracket
local
  cur_ch,
  this_ch,
  br_list,
  br_num,
  pat,
  level,
  here,
  there,
  direction;

  cur_ch := get_info(get_info(BUFFER, 'current'), 'character');
  br_list := "()[]{}<>+;!!??";
  br_num := INDEX(br_list, cur_ch);

  if br_num = 0
  then
    MESSAGE ("Not on a bracket");
  else
    br_num := ((br_num+1)/2)*2 -1;
    if br_num = INDEX(br_list, cur_ch)
    then
      direction:= FORWARD;
    else
      direction:= REVERSE;
    endif;

    pat := ANY(SUBSTR(br_list, br_num, 2));
    level := 1;
    here := mark(NONE);

    loop
      if direction = FORWARD
      then
        move_horizontal(1);
      else
        move_horizontal(-1);
      endif;
      there := search_quietly(pat, direction);
      exitif there = 0;      
      position(there);
      this_ch := get_info(get_info(BUFFER, 'current'), 'character');
      if this_ch = cur_ch
      then
          level := level + 1;
      else
          level := level - 1;
      endif;                               
      exitif level = 0;
    endloop;
    if there = 0 then
      position(here);
      message ("Matching bracket not found");
    endif;
  endif;
endprocedure

---
compile it and bind it to a key of your choice.
---
Henrik Tougaard             Postmaster, News-Reader and general dogsbody
Datani A/S, Software Consultants
Copenhagen, Denmark         Only my own opinions........
Technical Details
Message-ID: <1992Nov11.085850.108@datani.dk>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!dkuug!datani!ht
References: <1992Nov8.053543.383@wsuhub.uc.twsu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!dkuug!datani!ht
From: ht@datani.dk
Newsgroups: vmsnet.tpu
Subject: Re: Procedure to matchup (){}[]  ???
Message-ID: <1992Nov11.085850.108@datani.dk>
Date: 11 Nov 92 07:58:50 GMT
References: <1992Nov8.053543.383@wsuhub.uc.twsu.edu>
Organization: Datani A/S, Copenhagen, Denmark
Lines: 76