home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Text / bbeditinsertcolumn Folder / InsertColumn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-08  |  2.5 KB  |  101 lines  |  [TEXT/KAHL]

  1. #include <SetupA4.h>
  2. #include "ExternalInterface.h"
  3.  
  4. // Insert Column by Matthew Xavier Mora
  5. // 3-05-93 
  6. // Do with it what you will. But be gentle :-)
  7. // Since you have the source you can correct the bugs yourselves.
  8.  
  9. pascal void main(ExternalCallbackBlock *callbacks, WindowPtr w)
  10. {
  11.     long         line_count;
  12.     long         line;
  13.     long         line_start;
  14.     long         line_end;
  15.     long         selStart,selEnd,firstChar,columnStart;
  16.     Handle        text;
  17.     char        tabChar = 0x09;
  18.     char         cr         = 0x0D;
  19.     GrafPtr        save_port;
  20.     long         oldLength,actualLength;
  21.     Handle         newtext;
  22.     EventRecord theEvent;
  23.     long         selLine;
  24.     
  25.     RememberA0();
  26.     SetUpA4();
  27.     
  28.     if (w == nil)
  29.         goto done;
  30.     GetPort(&save_port);
  31.     // Hold down the cmd key for help
  32.     EventAvail(everyEvent,&theEvent);
  33.     if ( theEvent.modifiers & cmdKey ) {
  34.         Alert(128,nil);
  35.         SetPort(save_port);
  36.         goto done;
  37.     }
  38.     
  39.  
  40.     line_count = callbacks->GetLastLine();
  41.     text = callbacks->GetWindowContents(w);
  42.     callbacks->GetSelection(&selStart,&selEnd,&firstChar);
  43.     line_start = callbacks->GetLineStart(selStart);
  44.     columnStart = selStart - line_start;
  45.     selStart = columnStart;
  46.     callbacks->SetSelection(selStart,selStart,firstChar);
  47.  
  48.     
  49.     oldLength = GetHandleSize(text);
  50.     
  51.     newtext = callbacks->Allocate(oldLength+line_count,true);
  52.     
  53.     if (newtext == nil) {
  54.         callbacks->StartProgress("\pInserting Column (slow)…", line_count , FALSE);
  55.         for (line = 0; line < line_count; line++)
  56.         {    
  57.             if (callbacks->DoProgress(line)) line = line_count;
  58.             callbacks->Insert(&tabChar, 1L);
  59.             line_end = callbacks->GetLineEnd(selStart);
  60.             selStart = (++line_end) + columnStart;
  61.             callbacks->SetSelection(selStart,selStart,firstChar);
  62.         }
  63.     } else {
  64.     register char *textptr;                        //ptr to old text
  65.     register char *newtextptr;                    //ptr to new text
  66.     register long charcount = 1;                //character count of where we are on a line
  67.     char *endChar;                                //ending char
  68.     
  69.     HLock(newtext);                                //lock 'em just in case
  70.     HLock(text);
  71.  
  72.     newtextptr = *newtext;
  73.     textptr = *text;
  74.     endChar = textptr + oldLength;
  75.     callbacks->StartProgress("\pInserting Column (fast)…", (long)endChar , FALSE);
  76.  
  77.     if (columnStart == 0)                         //kludge for first line
  78.         *(newtextptr++) = tabChar;                //It didn't insert a tab if the col started at zero
  79.         
  80.     for (; textptr < endChar;textptr++) {
  81.         if (*textptr == cr){
  82.                 charcount = 0;
  83.                 callbacks->DoProgress((long)textptr);
  84.             }    
  85.         *(newtextptr++) = *textptr;
  86.         if (charcount++ == columnStart)
  87.                 *(newtextptr++) = tabChar;
  88.         
  89.     }
  90.  
  91.         HUnlock(newtext);
  92.         HUnlock(text);
  93.         callbacks->SetWindowContents(w,newtext);
  94.         callbacks->SetSelection(0,0,0);
  95.         callbacks->DoneProgress();
  96.         
  97.     }
  98. done:
  99.     RestoreA4();
  100. }
  101.