home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / Level 1 Extensions 04Jan95 / TextEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-28  |  87.4 KB  |  2,927 lines  |  [TEXT/KAHL]

  1. /* TextEdit.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Debug.h"
  21. #include "Audit.h"
  22. #include "Definitions.h"
  23.  
  24. #include "TextEdit.h"
  25. #include "Memory.h"
  26. #include "Scroll.h"
  27. #include "TextView.h"
  28. #include "Scrap.h"
  29. #include "TextStorage.h"
  30. #include "DataMunging.h"
  31. #include "EventLoop.h"
  32.  
  33.  
  34. #define BorderWidth (3)
  35.  
  36. /* click phases for selecting things */
  37. typedef enum
  38.     {
  39.         eNoClick EXECUTE(= -19741),
  40.         eSingleClick,
  41.         eDoubleClick,
  42.         eTripleClick
  43.     } ClickStates;
  44.  
  45.  
  46. typedef struct
  47.     {
  48.         /* this flag indicates if the undo record is valid */
  49.         MyBoolean                        CanUndoFlag;
  50.         /* this info remembers what was deleted, if any */
  51.         MyBoolean                        DeletedValidFlag;
  52.         long                                DeletedLine; /* where it came from */
  53.         long                                DeletedChar;
  54.         TextStorageRec*            DeletedStuff;
  55.         /* this info remembers what was/is replacing the deleted stuff, if any */
  56.         MyBoolean                        ReplacingValidFlag;
  57.         long                                ReplacingStartLine;
  58.         long                                ReplacingStartChar;
  59.         long                                ReplacingEndLine;
  60.         long                                ReplacingEndCharPlusOne;
  61.     } UndoRec;
  62.  
  63.  
  64. /* prototypes for undo routines */
  65. static void                TextEditBlockRemovedUndoSave(TextEditRec* Edit,
  66.                                         TextStorageRec* Stuff, long WhereLine, long WhereChar);
  67. static void                TextEditPurgeUndoRecord(TextEditRec* Edit);
  68. static void                TextEditKeyPressedUndoSave(TextEditRec* Edit);
  69. static void                TextEditRememberUndoDeletedCR(TextEditRec* Edit);
  70. static void                TextEditRememberUndoDeletedChar(TextEditRec* Edit, char What);
  71.  
  72.  
  73. struct TextEditRec
  74.     {
  75.         WinType*                        Window;
  76.         OrdType                            X;
  77.         OrdType                            Y;
  78.         OrdType                            TextAreaWidth;
  79.         OrdType                            TextAreaHeight;
  80.         OrdType                            TotalWidth;
  81.         OrdType                            TotalHeight;
  82.         ScrollRec*                    HorizontalScroll;
  83.         ScrollRec*                    VerticalScroll;
  84.         TextViewRec*                View;
  85.         MyBoolean                        AutoIndent;
  86.         ClickStates                    ClickPhase;
  87.         OrdType                            LastClickX;
  88.         OrdType                            LastClickY;
  89.         double                            LastClickTime;
  90.         UndoRec                            Undo;
  91.     };
  92.  
  93.  
  94. /* create a new, empty text edit */
  95. TextEditRec*            NewTextEdit(WinType* Window, TEScrollType ScrollStuff,
  96.                                         FontType FontID, FontSizeType FontSize, OrdType X, OrdType Y,
  97.                                         OrdType Width, OrdType Height)
  98.     {
  99.         TextEditRec*        Edit;
  100.  
  101.         Edit = (TextEditRec*)AllocPtrCanFail(sizeof(TextEditRec),"TextEditRec");
  102.         if (Edit == NIL)
  103.             {
  104.              MemOut1:
  105.                 return NIL;
  106.             }
  107.         Edit->Window = Window;
  108.         Edit->X = X;
  109.         Edit->Y = Y;
  110.         Edit->TotalWidth = Width;
  111.         Edit->TotalHeight = Height;
  112.         if ((ScrollStuff & eTEVScrollBar) != 0)
  113.             {
  114.                 Width = Width - 16 + 1;
  115.             }
  116.         if ((ScrollStuff & eTEHScrollBar) != 0)
  117.             {
  118.                 Height = Height - 16 + 1;
  119.             }
  120.         if ((ScrollStuff & eTEVScrollBar) != 0)
  121.             {
  122.                 Edit->VerticalScroll = NewScrollBar(Window,eVScrollBar,
  123.                     X + Width - 1,Y,Height);
  124.                 if (Edit->VerticalScroll == NIL)
  125.                     {
  126.                      MemOut2:
  127.                         ReleasePtr((char*)Edit);
  128.                         goto MemOut1;
  129.                     }
  130.             }
  131.          else
  132.             {
  133.                 Edit->VerticalScroll = NIL;
  134.             }
  135.         if ((ScrollStuff & eTEHScrollBar) != 0)
  136.             {
  137.                 Edit->HorizontalScroll = NewScrollBar(Window,eHScrollBar,
  138.                     X,Y + Height - 1,Width);
  139.                 if (Edit->HorizontalScroll == NIL)
  140.                     {
  141.                      MemOut3:
  142.                         if (Edit->VerticalScroll != NIL)
  143.                             {
  144.                                 DisposeScrollBar(Edit->VerticalScroll);
  145.                             }
  146.                         goto MemOut2;
  147.                     }
  148.             }
  149.          else
  150.             {
  151.                 Edit->HorizontalScroll = NIL;
  152.             }
  153.         Edit->TextAreaWidth = Width;
  154.         Edit->TextAreaHeight = Height;
  155.         Edit->View = NewTextView(Window,X + BorderWidth,Y + BorderWidth,
  156.             Width - (BorderWidth * 2), Height - (BorderWidth * 2),8,FontID,FontSize);
  157.         if (Edit->View == NIL)
  158.             {
  159.              MemOut4:
  160.                 if (Edit->HorizontalScroll != NIL)
  161.                     {
  162.                         DisposeScrollBar(Edit->HorizontalScroll);
  163.                     }
  164.                 goto MemOut3;
  165.             }
  166.         Edit->ClickPhase = eNoClick;
  167.         Edit->LastClickTime = ReadTimer();
  168.         Edit->AutoIndent = False;
  169.         Edit->Undo.CanUndoFlag = False;
  170.         TextEditRecalcVerticalScroll(Edit);
  171.         TextEditRecalcHorizontalScroll(Edit);
  172.         return Edit;
  173.     }
  174.  
  175.  
  176. /* dispose text edit and all text it contains */
  177. void                            DisposeTextEdit(TextEditRec* Edit)
  178.     {
  179.         TextEditPurgeUndoRecord(Edit);
  180.         DisposeTextView(Edit->View);
  181.         if (Edit->VerticalScroll != NIL)
  182.             {
  183.                 DisposeScrollBar(Edit->VerticalScroll);
  184.             }
  185.         if (Edit->HorizontalScroll != NIL)
  186.             {
  187.                 DisposeScrollBar(Edit->HorizontalScroll);
  188.             }
  189.         ReleasePtr((char*)Edit);
  190.     }
  191.  
  192.  
  193. /* get location of text edit */
  194. OrdType                        GetTextEditXLoc(TextEditRec* Edit)
  195.     {
  196.         CheckPtrExistence(Edit);
  197.         return Edit->X;
  198.     }
  199.  
  200.  
  201. /* get location of text edit */
  202. OrdType                        GetTextEditYLoc(TextEditRec* Edit)
  203.     {
  204.         CheckPtrExistence(Edit);
  205.         return Edit->Y;
  206.     }
  207.  
  208.  
  209. /* get location of text edit */
  210. OrdType                        GetTextEditWidth(TextEditRec* Edit)
  211.     {
  212.         CheckPtrExistence(Edit);
  213.         return Edit->TotalWidth;
  214.     }
  215.  
  216.  
  217. /* get location of text edit */
  218. OrdType                        GetTextEditHeight(TextEditRec* Edit)
  219.     {
  220.         CheckPtrExistence(Edit);
  221.         return Edit->TotalHeight;
  222.     }
  223.  
  224.  
  225. /* get font stuff for text edit */
  226. FontType                    GetTextEditFont(TextEditRec* Edit)
  227.     {
  228.         CheckPtrExistence(Edit);
  229.         return GetTextViewFont(Edit->View);
  230.     }
  231.  
  232.  
  233. /* get font stuff for text edit */
  234. FontSizeType            GetTextEditPointSize(TextEditRec* Edit)
  235.     {
  236.         CheckPtrExistence(Edit);
  237.         return GetTextViewPointSize(Edit->View);
  238.     }
  239.  
  240.  
  241. /* get the number of spaces per tab character */
  242. long                            GetTextEditSpacesPerTab(TextEditRec* Edit)
  243.     {
  244.         CheckPtrExistence(Edit);
  245.         return GetTextViewSpacesPerTab(Edit->View);
  246.     }
  247.  
  248.  
  249. /* get the index of the top line in the window */
  250. long                            GetTextEditTopLine(TextEditRec* Edit)
  251.     {
  252.         CheckPtrExistence(Edit);
  253.         return GetTextViewTopLine(Edit->View);
  254.     }
  255.  
  256.  
  257. /* get the pixel index of the leftmost text of the text box */
  258. OrdType                        GetTextEditPixelIndent(TextEditRec* Edit)
  259.     {
  260.         CheckPtrExistence(Edit);
  261.         return GetTextViewPixelIndent(Edit->View);
  262.     }
  263.  
  264.  
  265. /* returns True if the selection is non-empty, or false if it's an insertion point */
  266. MyBoolean                    TextEditIsThereValidSelection(TextEditRec* Edit)
  267.     {
  268.         CheckPtrExistence(Edit);
  269.         return TextViewIsThereValidSelection(Edit->View);
  270.     }
  271.  
  272.  
  273. /* get the line number of the start of the selection */
  274. long                            GetTextEditSelectStartLine(TextEditRec* Edit)
  275.     {
  276.         CheckPtrExistence(Edit);
  277.         return GetTextViewSelectStartLine(Edit->View);
  278.     }
  279.  
  280.  
  281. /* get the line number of the end of the selection */
  282. long                            GetTextEditSelectEndLine(TextEditRec* Edit)
  283.     {
  284.         CheckPtrExistence(Edit);
  285.         return GetTextViewSelectEndLine(Edit->View);
  286.     }
  287.  
  288.  
  289. /* get the character index of the start of the selection */
  290. long                            GetTextEditSelectStartChar(TextEditRec* Edit)
  291.     {
  292.         CheckPtrExistence(Edit);
  293.         return GetTextViewSelectStartChar(Edit->View);
  294.     }
  295.  
  296.  
  297. /* get the character index of the character immediately after the end of the */
  298. /* selection.  (if this == start char and startline == endline, then there is no */
  299. /* space between them and therefore there is no selection */
  300. long                            GetTextEditSelectEndCharPlusOne(TextEditRec* Edit)
  301.     {
  302.         CheckPtrExistence(Edit);
  303.         return GetTextViewSelectEndCharPlusOne(Edit->View);
  304.     }
  305.  
  306.  
  307. /* find out if selection & scrollbar display is enabled */
  308. MyBoolean                    TextEditIsShowSelectionEnabled(TextEditRec* Edit)
  309.     {
  310.         CheckPtrExistence(Edit);
  311.         return TextViewIsShowSelectionEnabled(Edit->View);
  312.     }
  313.  
  314.  
  315. /* find out if the data has been modified since the last call to TextEditHasBeenSaved */
  316. MyBoolean                    TextEditDoesItNeedToBeSaved(TextEditRec* Edit)
  317.     {
  318.         CheckPtrExistence(Edit);
  319.         return TextViewDoesItNeedToBeSaved(Edit->View);
  320.     }
  321.  
  322.  
  323. /* get the total number of lines contained in the edit */
  324. long                            GetTextEditNumLines(TextEditRec* Edit)
  325.     {
  326.         CheckPtrExistence(Edit);
  327.         return GetTextViewNumLines(Edit->View);
  328.     }
  329.  
  330.  
  331. /* find out if auto-indent upon newline is enabled */
  332. MyBoolean                    TextEditIsAutoIndentEnabled(TextEditRec* Edit)
  333.     {
  334.         CheckPtrExistence(Edit);
  335.         return Edit->AutoIndent;
  336.     }
  337.  
  338.  
  339. /* find out if it is possible to undo the last operation (for enabling menu item) */
  340. MyBoolean                    TextEditCanWeUndo(TextEditRec* Edit)
  341.     {
  342.         CheckPtrExistence(Edit);
  343.         return Edit->Undo.CanUndoFlag;
  344.     }
  345.  
  346.  
  347. /* change the screen location of the text edit box */
  348. void                            SetTextEditPosition(TextEditRec* Edit, OrdType X, OrdType Y,
  349.                                         OrdType Width, OrdType Height)
  350.     {
  351.         CheckPtrExistence(Edit);
  352.         Edit->X = X;
  353.         Edit->Y = Y;
  354.         Edit->TotalWidth = Width;
  355.         Edit->TotalHeight = Height;
  356.         if (Edit->VerticalScroll != NIL)
  357.             {
  358.                 Width = Width - 16 + 1;
  359.             }
  360.         if (Edit->HorizontalScroll != NIL)
  361.             {
  362.                 Height = Height - 16 + 1;
  363.             }
  364.         if (Edit->VerticalScroll != NIL)
  365.             {
  366.                 SetScrollLocation(Edit->VerticalScroll,X + Width - 1,Y,Height);
  367.             }
  368.         if (Edit->HorizontalScroll != NIL)
  369.             {
  370.                 SetScrollLocation(Edit->HorizontalScroll,X,Y + Height - 1,Width);
  371.             }
  372.         Edit->TextAreaWidth = Width;
  373.         Edit->TextAreaHeight = Height;
  374.         SetTextViewPosition(Edit->View,X + BorderWidth,Y + BorderWidth,
  375.             Width - (BorderWidth * 2),Height - (BorderWidth * 2));
  376.         TextEditRecalcVerticalScroll(Edit);
  377.         TextEditRecalcHorizontalScroll(Edit);
  378.         TextEditFullRedraw(Edit);
  379.     }
  380.  
  381.  
  382. /* change the font being used to display the text */
  383. void                            SetTextEditFontStuff(TextEditRec* Edit, FontType Font,
  384.                                         FontSizeType Size)
  385.     {
  386.         CheckPtrExistence(Edit);
  387.         SetTextViewFontStuff(Edit->View,Font,Size);
  388.         TextEditRecalcVerticalScroll(Edit); /* line height may change */
  389.     }
  390.  
  391.  
  392. /* set the number of spaces displayed for a tab */
  393. void                            SetTextEditTabSize(TextEditRec* Edit, long SpacesPerTab)
  394.     {
  395.         CheckPtrExistence(Edit);
  396.         SetTextViewTabSize(Edit->View,SpacesPerTab);
  397.         TextEditRecalcHorizontalScroll(Edit); /* line length may change */
  398.     }
  399.  
  400.  
  401. /* change the top line being displayed in the exit box */
  402. void                            SetTextEditTopLine(TextEditRec* Edit, long NewTopLine)
  403.     {
  404.         CheckPtrExistence(Edit);
  405.         SetTextViewTopLine(Edit->View,NewTopLine);
  406.         TextEditRecalcVerticalScroll(Edit);
  407.     }
  408.  
  409.  
  410. /* change the pixel index of the left edge of the text box */
  411. void                            SetTextEditPixelIndent(TextEditRec* Edit, OrdType NewPixelIndent)
  412.     {
  413.         CheckPtrExistence(Edit);
  414.         SetTextViewPixelIndent(Edit->View,NewPixelIndent);
  415.         TextEditRecalcHorizontalScroll(Edit);
  416.     }
  417.  
  418.  
  419. /* set the selection to a specified range */
  420. void                            SetTextEditSelection(TextEditRec* Edit, long StartLine,
  421.                                         long StartChar, long EndLine, long EndCharPlusOne)
  422.     {
  423.         CheckPtrExistence(Edit);
  424.         SetTextViewSelection(Edit->View,StartLine,StartChar,EndLine,EndCharPlusOne);
  425.     }
  426.  
  427.  
  428. /* set the selection to an insertion point at the specified position */
  429. void                            SetTextEditInsertionPoint(TextEditRec* Edit, long Line, long Char)
  430.     {
  431.         CheckPtrExistence(Edit);
  432.         SetTextViewInsertionPoint(Edit->View,Line,Char);
  433.     }
  434.  
  435.  
  436. /* enable display of selection and scrollbars */
  437. void                            EnableTextEditSelection(TextEditRec* Edit)
  438.     {
  439.         CheckPtrExistence(Edit);
  440.         EnableTextViewSelection(Edit->View);
  441.         if (Edit->HorizontalScroll != NIL)
  442.             {
  443.                 EnableScrollBar(Edit->HorizontalScroll);
  444.             }
  445.         if (Edit->VerticalScroll != NIL)
  446.             {
  447.                 EnableScrollBar(Edit->VerticalScroll);
  448.             }
  449.         TextEditRedrawFrame(Edit);
  450.     }
  451.  
  452.  
  453. /* disable display of selection and scrollbars */
  454. void                            DisableTextEditSelection(TextEditRec* Edit)
  455.     {
  456.         CheckPtrExistence(Edit);
  457.         DisableTextViewSelection(Edit->View);
  458.         if (Edit->HorizontalScroll != NIL)
  459.             {
  460.                 DisableScrollBar(Edit->HorizontalScroll);
  461.             }
  462.         if (Edit->VerticalScroll != NIL)
  463.             {
  464.                 DisableScrollBar(Edit->VerticalScroll);
  465.             }
  466.         TextEditRedrawFrame(Edit);
  467.     }
  468.  
  469.  
  470. /* indicate that any data in the text edit has been saved.  After this call, */
  471. /* TextEditDoesItNeedToBeSaved will return False.  It will start returning true */
  472. /* if any subsequent changes are made. */
  473. void                            TextEditHasBeenSaved(TextEditRec* Edit)
  474.     {
  475.         CheckPtrExistence(Edit);
  476.         TextViewHasBeenSaved(Edit->View);
  477.     }
  478.  
  479.  
  480. /* enable or disable auto-indent on carriage return */
  481. void                            SetTextEditAutoIndent(TextEditRec* Edit, MyBoolean AutoIndentFlag)
  482.     {
  483.         CheckPtrExistence(Edit);
  484.         Edit->AutoIndent = AutoIndentFlag;
  485.     }
  486.  
  487.  
  488. /* recalculate the position index of the vertical scrollbar */
  489. void                            TextEditRecalcVerticalScroll(TextEditRec* Edit)
  490.     {
  491.         CheckPtrExistence(Edit);
  492.         if (Edit->VerticalScroll != NIL)
  493.             {
  494.                 SetMaxScrollIndex(Edit->VerticalScroll,GetTextEditNumLines(Edit)
  495.                     - TextViewNumVisibleLines(Edit->View) + 2/*why 2?*/);
  496.                 SetScrollIndex(Edit->VerticalScroll,GetTextEditTopLine(Edit));
  497.                 RedrawScrollBar(Edit->VerticalScroll);
  498.             }
  499.     }
  500.  
  501.  
  502. /* recalculate the position index of the horizontal scrollbar */
  503. void                            TextEditRecalcHorizontalScroll(TextEditRec* Edit)
  504.     {
  505.         CheckPtrExistence(Edit);
  506.         if (Edit->HorizontalScroll != NIL)
  507.             {
  508.                 SetMaxScrollIndex(Edit->HorizontalScroll,
  509.                     TextViewGetVirtualWindowWidth(Edit->View) - GetTextViewWidth(Edit->View));
  510.                 SetScrollIndex(Edit->HorizontalScroll,GetTextEditPixelIndent(Edit));
  511.                 RedrawScrollBar(Edit->HorizontalScroll);
  512.             }
  513.     }
  514.  
  515.  
  516. /* redraw the entire text edit box */
  517. void                            TextEditFullRedraw(TextEditRec* Edit)
  518.     {
  519.         CheckPtrExistence(Edit);
  520.         if (Edit->VerticalScroll != NIL)
  521.             {
  522.                 RedrawScrollBar(Edit->VerticalScroll);
  523.             }
  524.         if (Edit->HorizontalScroll != NIL)
  525.             {
  526.                 RedrawScrollBar(Edit->HorizontalScroll);
  527.             }
  528.         TextViewFullRedraw(Edit->View);
  529.         TextEditRedrawFrame(Edit);
  530.     }
  531.  
  532.  
  533. /* redraw the outline frame of the text edit box */
  534. void                            TextEditRedrawFrame(TextEditRec* Edit)
  535.     {
  536.         Patterns                PatternToUse;
  537.  
  538.         CheckPtrExistence(Edit);
  539.         if (TextEditIsShowSelectionEnabled(Edit))
  540.             {
  541.                 PatternToUse = eBlack;
  542.             }
  543.          else
  544.             {
  545.                 PatternToUse = eMediumGrey;
  546.             }
  547.         SetClipRect(Edit->Window,Edit->X,Edit->Y,
  548.             Edit->TextAreaWidth,Edit->TextAreaHeight);
  549.         DrawBoxFrame(Edit->Window,PatternToUse,Edit->X,Edit->Y,
  550.             Edit->TextAreaWidth,Edit->TextAreaHeight);
  551.         DrawBoxFrame(Edit->Window,eWhite,Edit->X + 1,Edit->Y + 1,
  552.             Edit->TextAreaWidth - 2,Edit->TextAreaHeight - 2);
  553.         DrawBoxFrame(Edit->Window,eWhite,Edit->X + 2,Edit->Y + 2,
  554.             Edit->TextAreaWidth - 4,Edit->TextAreaHeight - 4);
  555.     }
  556.  
  557.  
  558. /* update cursor.  This should be called during idle events.  It keeps track of */
  559. /* when the cursor was last blinked and blinks the cursor again if necessary. */
  560. void                            TextEditUpdateCursor(TextEditRec* Edit)
  561.     {
  562.         CheckPtrExistence(Edit);
  563.         TextViewUpdateCursor(Edit->View);
  564.     }
  565.  
  566.  
  567. /* get the specified line of text from the exit */
  568. char*                            GetTextEditLine(TextEditRec* Edit, long LineIndex)
  569.     {
  570.         CheckPtrExistence(Edit);
  571.         return GetTextViewLine(Edit->View,LineIndex);
  572.     }
  573.  
  574.  
  575. /* get a line of text, but first convert all tabs in the line into the */
  576. /* proper number of spaces. */
  577. char*                            GetTextEditSpaceFromTabLine(TextEditRec* Edit, long LineIndex)
  578.     {
  579.         CheckPtrExistence(Edit);
  580.         return GetTextViewSpaceFromTabLine(Edit->View,LineIndex);
  581.     }
  582.  
  583.  
  584. /* put a new line in the text box.  This overwrites data already on that line */
  585. MyBoolean                    SetTextEditLine(TextEditRec* Edit, long LineIndex, char* LineToCopy)
  586.     {
  587.         CheckPtrExistence(Edit);
  588.         TextEditPurgeUndoRecord(Edit); /* this routine modifies data, so remove undo info */
  589.         return SetTextViewLine(Edit->View,LineIndex,LineToCopy);
  590.     }
  591.  
  592.  
  593. /* use the LineFeed string to create a single block of text containing all */
  594. /* of the lines packed into it */
  595. char*                            TextEditGetRawData(TextEditRec* Edit, char* LineFeed)
  596.     {
  597.         CheckPtrExistence(Edit);
  598.         return TextViewGetRawData(Edit->View,LineFeed);
  599.     }
  600.  
  601.  
  602. /* put new data into the text edit.  The RawData is a block with all text lines */
  603. /* packed into it separated by the LineFeed string. */
  604. MyBoolean                    TextEditNewRawData(TextEditRec* Edit, char* RawData, char* LineFeed)
  605.     {
  606.         MyBoolean                ReturnValue;
  607.  
  608.         CheckPtrExistence(Edit);
  609.         TextEditPurgeUndoRecord(Edit); /* this routine modifies data, so remove undo info */
  610.         ReturnValue = TextViewNewRawData(Edit->View,LineFeed,RawData);
  611.         TextEditRecalcVerticalScroll(Edit);
  612.         TextEditRecalcHorizontalScroll(Edit);
  613.         return ReturnValue;
  614.     }
  615.  
  616.  
  617. /* get a text block containing the selected data */
  618. char*                            TextEditGetSelection(TextEditRec* Edit)
  619.     {
  620.         TextStorageRec*            Storage;
  621.         char*                                Raw;
  622.  
  623.         CheckPtrExistence(Edit);
  624.         Storage = TextViewGetSelection(Edit->View);
  625.         if (Storage == NIL)
  626.             {
  627.                 return NIL;
  628.             }
  629.         /* selection uses system's line feed */
  630.         Raw = TextStorageMakeRawBuffer(Storage,SYSTEMLINEFEED);
  631.         DisposeTextStorage(Storage);
  632.         if (Raw == NIL)
  633.             {
  634.                 return NIL;
  635.             }
  636.         return Raw;
  637.     }
  638.  
  639.  
  640. /* replace the current selection (if any) with the specified raw data block. */
  641. /* if this fails, some of the data may have been inserted */
  642. MyBoolean                    TextEditInsertRawData(TextEditRec* Edit, char* RawData, char* LineFeed)
  643.     {
  644.         TextStorageRec*    Block;
  645.         MyBoolean                ReturnValue;
  646.  
  647.         CheckPtrExistence(Edit);
  648.         CheckPtrExistence(RawData);
  649.         TextEditPurgeUndoRecord(Edit); /* this routine modifies data, so remove undo info */
  650.         Block = TextStorageFromRawBuffer(RawData,LineFeed);
  651.         if (Block == NIL)
  652.             {
  653.                 return False;
  654.             }
  655.         ReturnValue = TextViewInsertBlock(Edit->View,Block);
  656.         DisposeTextStorage(Block);
  657.         TextEditRecalcVerticalScroll(Edit);
  658.         TextEditRecalcHorizontalScroll(Edit);
  659.         return ReturnValue;
  660.     }
  661.  
  662.  
  663. /* replace the current selection (if any) with the specified raw data block. */
  664. /* if this fails, some of the data may have been inserted.  this version sets up */
  665. /* the undo information. */
  666. MyBoolean                    TextEditInsertRawDataWithUndo(TextEditRec* Edit, char* RawData,
  667.                                         char* LineFeed)
  668.     {
  669.         TextStorageRec*    Block;
  670.  
  671.         CheckPtrExistence(Edit);
  672.         CheckPtrExistence(RawData);
  673.         Block = TextStorageFromRawBuffer(RawData,LineFeed);
  674.         if (Block == NIL)
  675.             {
  676.              FailurePoint1:
  677.                 return False;
  678.             }
  679.         if (!TextEditDoMenuClear(Edit)) /* erase old selection & save it in Undo record */
  680.             {
  681.              FailurePoint2:
  682.                 DisposeTextStorage(Block);
  683.                 goto FailurePoint1;
  684.             }
  685.         Edit->Undo.ReplacingValidFlag = True;
  686.         Edit->Undo.ReplacingStartLine = GetTextEditSelectStartLine(Edit);
  687.         Edit->Undo.ReplacingStartChar = GetTextEditSelectStartChar(Edit);
  688.         if (!TextViewInsertBlock(Edit->View,Block))
  689.             {
  690.                 goto FailurePoint2;
  691.             }
  692.         Edit->Undo.ReplacingEndLine = GetTextEditSelectEndLine(Edit);
  693.         Edit->Undo.ReplacingEndCharPlusOne = GetTextEditSelectEndCharPlusOne(Edit);
  694.         DisposeTextStorage(Block);
  695.         TextEditRecalcVerticalScroll(Edit);
  696.         TextEditRecalcHorizontalScroll(Edit);
  697.         return True;
  698.     }
  699.  
  700.  
  701. /* find the union of two selection ranges */
  702. void                            UnionSelection(SelRec One, SelRec Two, SelRec Three,
  703.                                         SelRec* Start, SelRec* End)
  704.     {
  705.         SortSelection(&One,&Three);
  706.         SortSelection(&One,&Two);
  707.         SortSelection(&Two,&Three);
  708.         *Start = One;
  709.         *End = Three;
  710.     }
  711.  
  712.  
  713. /* find the difference (union - intersection) of two selection ranges.  This is */
  714. /* used to avoid redrawing the entire selection range all the time */
  715. void                            DiffSelection(SelRec OneStart, SelRec OneEnd, SelRec TwoStart,
  716.                                         SelRec TwoEnd, SelRec* OutStart, SelRec* OutEnd)
  717.     {
  718.         SortSelection(&OneStart,&OneEnd);
  719.         SortSelection(&TwoStart,&TwoEnd);
  720.         if (GreaterThan(&OneStart,&TwoStart))
  721.             {
  722.                 *OutStart = TwoStart;
  723.                 *OutEnd = OneStart;
  724.                 if (GreaterThan(&OneEnd,&TwoEnd))
  725.                     {
  726.                         *OutEnd = OneEnd;
  727.                         return;
  728.                     }
  729.                 if (GreaterThan(&TwoEnd,&OneEnd))
  730.                     {
  731.                         *OutEnd = TwoEnd;
  732.                         return;
  733.                     }
  734.                 return;
  735.             }
  736.         if (GreaterThan(&TwoStart,&OneStart))
  737.             {
  738.                 *OutStart = OneStart;
  739.                 *OutEnd = TwoStart;
  740.                 if (GreaterThan(&OneEnd,&TwoEnd))
  741.                     {
  742.                         *OutEnd = OneEnd;
  743.                         return;
  744.                     }
  745.                 if (GreaterThan(&TwoEnd,&OneEnd))
  746.                     {
  747.                         *OutEnd = TwoEnd;
  748.                         return;
  749.                     }
  750.                 return;
  751.             }
  752.         if (GreaterThan(&OneEnd,&TwoEnd))
  753.             {
  754.                 *OutStart = TwoEnd;
  755.                 *OutEnd = OneEnd;
  756.                 return;
  757.             }
  758.         if (GreaterThan(&TwoEnd,&OneEnd))
  759.             {
  760.                 *OutStart = OneEnd;
  761.                 *OutEnd = TwoEnd;
  762.                 return;
  763.             }
  764.         *OutStart = OneStart;
  765.         *OutEnd = OneStart;
  766.     }
  767.  
  768.  
  769. /* if the first selection point is after the second then reverse their order */
  770. void                            SortSelection(SelRec* One, SelRec* Two)
  771.     {
  772.         SelRec            Temp;
  773.  
  774.         if (GreaterThan(One,Two))
  775.             {
  776.                 Temp = *One;
  777.                 *One = *Two;
  778.                 *Two = Temp;
  779.             }
  780.     }
  781.  
  782.  
  783. /* returns True if the first selection point is after the second one */
  784. MyBoolean                    GreaterThan(SelRec* One, SelRec* Two)
  785.     {
  786.         if (One->Line == Two->Line)
  787.             {
  788.                 if (One->Column > Two->Column)
  789.                     {
  790.                         return True;
  791.                     }
  792.             }
  793.          else
  794.             {
  795.                 if (One->Line > Two->Line)
  796.                     {
  797.                         return True;
  798.                     }
  799.             }
  800.         return False;
  801.     }
  802.  
  803.  
  804. /* extend the selection using the current mouse-click state (single, double, triple) */
  805. void                            ExtendSelection(TextEditRec* Edit, SelRec* Start, SelRec* End)
  806.     {
  807.         char*                Buffer;
  808.         long                LineLength;
  809.  
  810.         CheckPtrExistence(Edit);
  811.         switch (Edit->ClickPhase)
  812.             {
  813.                 case eNoClick:
  814.                     break;
  815.                 case eSingleClick:
  816.                     break;
  817.                 case eDoubleClick:
  818.                     if ((Start != NIL) && (Start->Line >= 0)
  819.                         && (Start->Line < GetTextEditNumLines(Edit)))
  820.                         {
  821.                             Buffer = GetTextEditLine(Edit,Start->Line);
  822.                             if (Buffer != NIL)
  823.                                 {
  824.                                     LineLength = PtrSize(Buffer);
  825.                                     if (AlphaNum(Buffer[Start->Column]))
  826.                                         {
  827.                                             while ((Start->Column > 0)
  828.                                                 && AlphaNum(Buffer[Start->Column - 1]))
  829.                                                 {
  830.                                                     Start->Column -= 1;
  831.                                                 }
  832.                                         }
  833.                                     ReleasePtr(Buffer);
  834.                                 }
  835.                         }
  836.                     if ((End != NIL) && (End->Line >= 0)
  837.                         && (End->Line < GetTextEditNumLines(Edit)))
  838.                         {
  839.                             Buffer = GetTextEditLine(Edit,End->Line);
  840.                             if (Buffer != NIL)
  841.                                 {
  842.                                     LineLength = PtrSize(Buffer);
  843.                                     while ((End->Column < LineLength)
  844.                                         && AlphaNum(Buffer[End->Column]))
  845.                                         {
  846.                                             End->Column += 1;
  847.                                         }
  848.                                     ReleasePtr(Buffer);
  849.                                 }
  850.                         }
  851.                     break;
  852.                 case eTripleClick:
  853.                     if (Start != NIL)
  854.                         {
  855.                             Start->Column = 0;
  856.                         }
  857.                     if ((End != NIL) && (End->Column != 0))
  858.                         {
  859.                             End->Column = 0;
  860.                             End->Line += 1;
  861.                         }
  862.                     break;
  863.             }
  864.     }
  865.  
  866.  
  867. /* find out if the character is an alphanumeric character.  this is used by */
  868. /* ExtendSelection for figuring out where double-click extends should stop. */
  869. MyBoolean                    AlphaNum(char It)
  870.     {
  871.         return ((It >= 'a') && (It <= 'z')) || ((It >= 'A') && (It <= 'Z'))
  872.             || ((It >= '0') && (It <= '9')) || (It == '_');
  873.     }
  874.  
  875.  
  876. /* append a line of text to the end of the text edit.  This can be used if the */
  877. /* text edit box is being used as an interaction (terminal) window */
  878. /* if NIL is passed in for Data, a blank line will be appended */
  879. MyBoolean                    TextEditAppendLineInteraction(TextEditRec* Edit, char* Data)
  880.     {
  881.         CheckPtrExistence(Edit);
  882.         TextEditPurgeUndoRecord(Edit); /* this routine modifies data, so remove undo info */
  883.         SetTextEditInsertionPoint(Edit,GetTextViewNumLines(Edit->View) - 1,
  884.             GetTextViewLineLength(Edit->View,GetTextViewNumLines(Edit->View) - 1));
  885.         if (!TextViewBreakLine(Edit->View,GetTextEditSelectStartLine(Edit),
  886.             GetTextEditSelectStartChar(Edit)))
  887.             {
  888.                 return False;
  889.             }
  890.         SetTextEditInsertionPoint(Edit,GetTextEditSelectStartLine(Edit) + 1,0);
  891.         if (Data == NIL)
  892.             {
  893.                 return True;
  894.             }
  895.         return SetTextViewLine(Edit->View,GetTextEditSelectStartLine(Edit),Data);
  896.     }
  897.  
  898.  
  899. /* dump the data contained in the text edit to the current position in the */
  900. /* specified file.  returns True if all the data was written successfully */
  901. MyBoolean                    TestEditWriteDataToFile(TextEditRec* Edit,
  902.                                         struct FileType* FileRefNum, char* EOLN)
  903.     {
  904.         CheckPtrExistence(Edit);
  905.         return TextViewWriteDataToFile(Edit->View,FileRefNum,EOLN);
  906.     }
  907.  
  908.  
  909. /* cut the selected data to the clipboard.  if this fails, some of the data */
  910. /* may have been deleted */
  911. MyBoolean                    TextEditDoMenuCut(TextEditRec* Edit)
  912.     {
  913.         TextStorageRec*            Removed;
  914.         TextStorageRec*            UndoInfo;
  915.         char*                                RawBlock;
  916.         MyBoolean                        Flag;
  917.  
  918.         CheckPtrExistence(Edit);
  919.         Removed = TextViewGetSelection(Edit->View);
  920.         if (Removed == NIL)
  921.             {
  922.              FailurePoint1:
  923.                 return False;
  924.             }
  925.         UndoInfo = TextViewGetSelection(Edit->View);
  926.         if (UndoInfo == NIL)
  927.             {
  928.              FailurePoint2:
  929.                 DisposeTextStorage(Removed);
  930.                 goto FailurePoint1;
  931.             }
  932.         RawBlock = TextStorageMakeRawBuffer(Removed,SYSTEMLINEFEED);
  933.         if (RawBlock == NIL)
  934.             {
  935.              FailurePoint3:
  936.                 DisposeTextStorage(UndoInfo);
  937.                 goto FailurePoint2;
  938.             }
  939.         Flag = SetScrapToThis(RawBlock);
  940.         ReleasePtr(RawBlock);
  941.         if (!Flag)
  942.             {
  943.                 goto FailurePoint3;
  944.             }
  945.         DisposeTextStorage(Removed);
  946.  
  947.         if (!TextViewDeleteSelection(Edit->View))
  948.             {
  949.                 goto FailurePoint3;
  950.             }
  951.         TextEditBlockRemovedUndoSave(Edit,UndoInfo,GetTextEditSelectStartLine(Edit),
  952.             GetTextEditSelectStartChar(Edit));
  953.         TextEditRecalcVerticalScroll(Edit);
  954.         TextEditRecalcHorizontalScroll(Edit);
  955.         TextEditShowSelection(Edit);
  956.         return True;
  957.     }
  958.  
  959.  
  960. /* copy the selected data to the clipboard */
  961. MyBoolean                    TextEditDoMenuCopy(TextEditRec* Edit)
  962.     {
  963.         TextStorageRec*    Removed;
  964.         char*                        RawBlock;
  965.         MyBoolean                Flag;
  966.  
  967.         CheckPtrExistence(Edit);
  968.         Removed = TextViewGetSelection(Edit->View);
  969.         if (Removed == NIL)
  970.             {
  971.                 return False;
  972.             }
  973.         RawBlock = TextStorageMakeRawBuffer(Removed,SYSTEMLINEFEED);
  974.         DisposeTextStorage(Removed);
  975.         if (RawBlock == NIL)
  976.             {
  977.                 return False;
  978.             }
  979.         Flag = SetScrapToThis(RawBlock);
  980.         ReleasePtr(RawBlock);
  981.         if (!Flag)
  982.             {
  983.                 return False;
  984.             }
  985.         return True;
  986.     }
  987.  
  988.  
  989. /* paste the clipboard in, replacing the current selection if there is one */
  990. /* if this fails, some of the data may have been inserted */
  991. MyBoolean                    TextEditDoMenuPaste(TextEditRec* Edit)
  992.     {
  993.         char*                        RawBlock;
  994.         TextStorageRec*    ToBeInserted;
  995.  
  996.         CheckPtrExistence(Edit);
  997.         RawBlock = GetCopyOfScrap();
  998.         if (RawBlock == NIL)
  999.             {
  1000.              FailurePoint1:
  1001.                 return False;
  1002.             }
  1003.         ToBeInserted = TextStorageFromRawBuffer(RawBlock,SYSTEMLINEFEED);
  1004.         ReleasePtr(RawBlock);
  1005.         if (ToBeInserted == NIL)
  1006.             {
  1007.                 goto FailurePoint1;
  1008.             }
  1009.         if (!TextEditDoMenuClear(Edit)) /* erase old selection & save it in Undo record */
  1010.             {
  1011.              FailurePoint2:
  1012.                 DisposeTextStorage(ToBeInserted);
  1013.                 goto FailurePoint1;
  1014.             }
  1015.         Edit->Undo.ReplacingValidFlag = True;
  1016.         Edit->Undo.ReplacingStartLine = GetTextEditSelectStartLine(Edit);
  1017.         Edit->Undo.ReplacingStartChar = GetTextEditSelectStartChar(Edit);
  1018.         if (!TextViewInsertBlock(Edit->View,ToBeInserted))
  1019.             {
  1020.                 goto FailurePoint2;
  1021.             }
  1022.         Edit->Undo.ReplacingEndLine = GetTextEditSelectEndLine(Edit);
  1023.         Edit->Undo.ReplacingEndCharPlusOne = GetTextEditSelectEndCharPlusOne(Edit);
  1024.         DisposeTextStorage(ToBeInserted);
  1025.         TextEditRecalcVerticalScroll(Edit);
  1026.         TextEditRecalcHorizontalScroll(Edit);
  1027.         TextEditShowSelection(Edit);
  1028.         return True;
  1029.     }
  1030.  
  1031.  
  1032. /* select the entire data area of the text edit */
  1033. void                            TextEditDoMenuSelectAll(TextEditRec* Edit)
  1034.     {
  1035.         CheckPtrExistence(Edit);
  1036.         SetTextViewSelection(Edit->View,0,0,GetTextViewNumLines(Edit->View) - 1,
  1037.             GetTextViewLineLength(Edit->View,GetTextViewNumLines(Edit->View) - 1));
  1038.     }
  1039.  
  1040.  
  1041. /* delete the selected area.  This is the same as pressing the delete key */
  1042. /* when there is a valid selection.  if this fails, some of the data may */
  1043. /* have been deleted */
  1044. MyBoolean                    TextEditDoMenuClear(TextEditRec* Edit)
  1045.     {
  1046.         TextStorageRec*            Deleted;
  1047.  
  1048.         /* this routine is called from several other routines which need to delete */
  1049.         /* a selection before inserting.  Therefore, we can do the delete-save part of */
  1050.         /* the 'undo' info construction in this routine. */
  1051.         CheckPtrExistence(Edit);
  1052.         Deleted = TextViewGetSelection(Edit->View);
  1053.         if (Deleted == NIL)
  1054.             {
  1055.              FailurePoint1:
  1056.                 return False;
  1057.             }
  1058.         if (!TextViewDeleteSelection(Edit->View))
  1059.             {
  1060.                 DisposeTextStorage(Deleted);
  1061.                 goto FailurePoint1;
  1062.             }
  1063.         TextEditBlockRemovedUndoSave(Edit,Deleted,GetTextEditSelectStartLine(Edit),
  1064.             GetTextEditSelectStartChar(Edit));
  1065.         TextEditRecalcVerticalScroll(Edit);
  1066.         TextEditRecalcHorizontalScroll(Edit);
  1067.         TextEditShowSelection(Edit);
  1068.         return True;
  1069.     }
  1070.  
  1071.  
  1072. /* shift the selection toward the left margin by deleting one tab (or spaces) */
  1073. /* from the beginning of the line.  It will not remove non-whitespace characters */
  1074. /* if this fails, some of the lines may have been shifted */
  1075. MyBoolean                    TextEditShiftSelectionLeftOneTab(TextEditRec* Edit)
  1076.     {
  1077.         long                    Scan;
  1078.         long                    Limit;
  1079.         long                    SpacesPerTab;
  1080.  
  1081.         CheckPtrExistence(Edit);
  1082.         Limit = GetTextViewSelectEndLine(Edit->View);
  1083.         if (TextEditIsThereValidSelection(Edit)
  1084.             && (GetTextViewSelectEndCharPlusOne(Edit->View) == 0))
  1085.             {
  1086.                 Limit -= 1;
  1087.             }
  1088.         SpacesPerTab = GetTextViewSpacesPerTab(Edit->View);
  1089.         for (Scan = GetTextViewSelectStartLine(Edit->View); Scan <= Limit; Scan += 1)
  1090.             {
  1091.                 char*                    LineCopy;
  1092.                 char*                    Line;
  1093.                 long                    CharScan;
  1094.                 long                    ColumnCount;
  1095.  
  1096.                 Line = GetTextViewLine(Edit->View,Scan);
  1097.                 if (Line == NIL)
  1098.                     {
  1099.                         return False;
  1100.                     }
  1101.                 CharScan = 0;
  1102.                 ColumnCount = 0;
  1103.                 while ((CharScan < PtrSize(Line)) && (ColumnCount < SpacesPerTab))
  1104.                     {
  1105.                         if (Line[CharScan] == ' ')
  1106.                             {
  1107.                                 CharScan += 1;
  1108.                                 ColumnCount += 1;
  1109.                             }
  1110.                         else if (Line[CharScan] == 9)
  1111.                             {
  1112.                                 CharScan += 1;
  1113.                                 ColumnCount = SpacesPerTab; /* cause loop termination */
  1114.                             }
  1115.                         else
  1116.                             {
  1117.                                 ColumnCount = SpacesPerTab; /* cause loop termination */
  1118.                             }
  1119.                     }
  1120.                 LineCopy = RemoveBlockFromBlockCopy(Line,0,CharScan);
  1121.                 ReleasePtr(Line);
  1122.                 if (LineCopy == NIL)
  1123.                     {
  1124.                         return False;
  1125.                     }
  1126.                 SetTextEditLine(Edit,Scan,LineCopy);
  1127.                 ReleasePtr(LineCopy);
  1128.             }
  1129.         if (TextEditIsThereValidSelection(Edit)
  1130.             && (GetTextViewSelectEndCharPlusOne(Edit->View) == 0))
  1131.             {
  1132.                 SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1133.                     0,GetTextViewSelectEndLine(Edit->View),0);
  1134.             }
  1135.          else
  1136.             {
  1137.                 SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1138.                     0,GetTextViewSelectEndLine(Edit->View) + 1,0);
  1139.             }
  1140.         TextEditRecalcHorizontalScroll(Edit);
  1141.         return True;
  1142.     }
  1143.  
  1144.  
  1145. /* shift selection toward the right margin by inserting a tab at the */
  1146. /* beginning of each line.  if this fails, some of the lines may have been shifted. */
  1147. MyBoolean                    TextEditShiftSelectionRightOneTab(TextEditRec* Edit)
  1148.     {
  1149.         long                    Scan;
  1150.         long                    Limit;
  1151.         char                    MyTab[1] = {9};
  1152.  
  1153.         CheckPtrExistence(Edit);
  1154.         Limit = GetTextViewSelectEndLine(Edit->View);
  1155.         if (TextEditIsThereValidSelection(Edit)
  1156.             && (GetTextViewSelectEndCharPlusOne(Edit->View) == 0))
  1157.             {
  1158.                 Limit -= 1;
  1159.             }
  1160.         for (Scan = GetTextViewSelectStartLine(Edit->View); Scan <= Limit; Scan += 1)
  1161.             {
  1162.                 char*                    Line;
  1163.  
  1164.                 Line = GetTextViewLine(Edit->View,Scan);
  1165.                 if (Line == NIL)
  1166.                     {
  1167.                         return False;
  1168.                     }
  1169.                 if (PtrSize(Line) != 0)
  1170.                     {
  1171.                         char*                    LineCopy;
  1172.  
  1173.                         /* we only shift if the line has stuff on it. */
  1174.                         LineCopy = InsertBlockIntoBlockCopy(Line,MyTab,0,1);
  1175.                         if (LineCopy == NIL)
  1176.                             {
  1177.                                 ReleasePtr(Line);
  1178.                                 return False;
  1179.                             }
  1180.                         SetTextEditLine(Edit,Scan,LineCopy);
  1181.                         ReleasePtr(LineCopy);
  1182.                     }
  1183.                 ReleasePtr(Line);
  1184.             }
  1185.         if (TextEditIsThereValidSelection(Edit)
  1186.             && (GetTextViewSelectEndCharPlusOne(Edit->View) == 0))
  1187.             {
  1188.                 SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1189.                     0,GetTextViewSelectEndLine(Edit->View),0);
  1190.             }
  1191.          else
  1192.             {
  1193.                 SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1194.                     0,GetTextViewSelectEndLine(Edit->View) + 1,0);
  1195.             }
  1196.         TextEditRecalcHorizontalScroll(Edit);
  1197.         return True;
  1198.     }
  1199.  
  1200.  
  1201. /* convert all tab characters in the text box to the appropriate number of spaces */
  1202. /* if this fails, some of the lines may have been converted. */
  1203. MyBoolean                    TextEditConvertTabsToSpaces(TextEditRec* Edit)
  1204.     {
  1205.         long                        Scan;
  1206.         long                        Limit;
  1207.         long                        SelectionStartColumn;
  1208.         long                        SelectionEndColumn;
  1209.  
  1210.         CheckPtrExistence(Edit);
  1211.  
  1212.         /* before we do it, we should remember the start and end columns of the insertion */
  1213.         /* point so that we can do this so that the selection screen does not change. */
  1214.         SelectionStartColumn = TextViewCalculateColumnFromCharIndex(Edit->View,
  1215.             GetTextViewSelectStartLine(Edit->View),GetTextViewSelectStartChar(Edit->View));
  1216.         SelectionEndColumn = TextViewCalculateColumnFromCharIndex(Edit->View,
  1217.             GetTextViewSelectEndLine(Edit->View),GetTextViewSelectEndCharPlusOne(Edit->View));
  1218.  
  1219.         /* do the conversion */
  1220.         Limit = GetTextViewNumLines(Edit->View);
  1221.         for (Scan = 0; Scan < Limit; Scan += 1)
  1222.             {
  1223.                 char*                        Line;
  1224.                 MyBoolean                Flag;
  1225.  
  1226.                 Line = GetTextViewSpaceFromTabLine(Edit->View,Scan);
  1227.                 if (Line == NIL)
  1228.                     {
  1229.                         return False;
  1230.                     }
  1231.                 Flag = SetTextEditLine(Edit,Scan,Line);
  1232.                 ReleasePtr(Line);
  1233.                 if (!Flag)
  1234.                     {
  1235.                         return False;
  1236.                     }
  1237.             }
  1238.         TextEditRecalcHorizontalScroll(Edit);
  1239.  
  1240.         /* restore apparent selection */
  1241.         SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1242.             SelectionStartColumn,GetTextViewSelectEndLine(Edit->View),SelectionEndColumn);
  1243.  
  1244.         /* exit successfully */
  1245.         return True;
  1246.     }
  1247.  
  1248.  
  1249. /* show the current selection in the edit window */
  1250. void                            TextEditShowSelection(TextEditRec* Edit)
  1251.     {
  1252.         long            CheckValue;
  1253.  
  1254.         CheckPtrExistence(Edit);
  1255.         /* figure out how much space to leave at bottom and top edges */
  1256.         CheckValue = 4;
  1257.         while ((CheckValue > 0)
  1258.             && (TextViewNumVisibleLines(Edit->View) < CheckValue * 2 + 4))
  1259.             {
  1260.                 CheckValue -= 1;
  1261.             }
  1262.         /* vertical adjustment */
  1263.         if (((GetTextEditSelectStartLine(Edit) >= GetTextEditTopLine(Edit) + CheckValue)
  1264.                 && (GetTextEditSelectStartLine(Edit) < GetTextEditTopLine(Edit)
  1265.                 + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1)))
  1266.             || ((GetTextEditSelectStartLine(Edit) < CheckValue)
  1267.                 && (GetTextEditSelectStartLine(Edit) >= GetTextEditTopLine(Edit))))
  1268.             {
  1269.                 /* beginning of selection is in the box, so try to center the end */
  1270.                 if (GetTextEditSelectEndLine(Edit) < GetTextEditTopLine(Edit) + CheckValue)
  1271.                     {
  1272.                         /* selection is to far up */
  1273.                         SetTextEditTopLine(Edit,GetTextEditSelectEndLine(Edit) - CheckValue);
  1274.                     }
  1275.                 if (GetTextEditSelectEndLine(Edit) >= GetTextEditTopLine(Edit)
  1276.                     + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1))
  1277.                     {
  1278.                         /* selection is too far down */
  1279.                         SetTextEditTopLine(Edit,GetTextEditSelectEndLine(Edit)
  1280.                             - (TextViewNumVisibleLines(Edit->View) - CheckValue - 1) + 1);
  1281.                     }
  1282.             }
  1283.          else
  1284.             {
  1285.                 /* center the beginning in the box */
  1286.                 if (GetTextEditSelectStartLine(Edit) < GetTextEditTopLine(Edit) + CheckValue)
  1287.                     {
  1288.                         /* selection is to far up */
  1289.                         SetTextEditTopLine(Edit,GetTextEditSelectStartLine(Edit) - CheckValue);
  1290.                     }
  1291.                 if (GetTextEditSelectStartLine(Edit) >= GetTextEditTopLine(Edit)
  1292.                     + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1))
  1293.                     {
  1294.                         /* selection is too far down */
  1295.                         SetTextEditTopLine(Edit,GetTextEditSelectStartLine(Edit)
  1296.                             - (TextViewNumVisibleLines(Edit->View) - CheckValue - 1) + 1);
  1297.                     }
  1298.             }
  1299.         /* figure out how much space on left and right edges to leave */
  1300.         CheckValue = 32;
  1301.         while ((CheckValue > 0)
  1302.             && (GetTextViewWidth(Edit->View) < CheckValue * 4 + 16))
  1303.             {
  1304.                 CheckValue -= 1;
  1305.             }
  1306.         /* horizontal adjustment */
  1307.         if (!TextEditIsThereValidSelection(Edit))
  1308.             {
  1309.                 /* only adjust left-to-right if it's an insertion point */
  1310.                 if (TextViewScreenXFromCharIndex(Edit->View,
  1311.                     GetTextEditSelectStartLine(Edit),
  1312.                     GetTextEditSelectStartChar(Edit))
  1313.                     < GetTextEditPixelIndent(Edit) + CheckValue)
  1314.                     {
  1315.                         SetTextEditPixelIndent(Edit,
  1316.                             TextViewScreenXFromCharIndex(Edit->View,
  1317.                             GetTextEditSelectStartLine(Edit),
  1318.                             GetTextEditSelectStartChar(Edit)) - (2 * CheckValue));
  1319.                     }
  1320.                 if (TextViewScreenXFromCharIndex(Edit->View,
  1321.                     GetTextEditSelectStartLine(Edit),
  1322.                     GetTextEditSelectStartChar(Edit))
  1323.                     > GetTextEditPixelIndent(Edit)
  1324.                     + GetTextViewWidth(Edit->View) - CheckValue)
  1325.                     {
  1326.                         SetTextEditPixelIndent(Edit,
  1327.                             TextViewScreenXFromCharIndex(Edit->View,
  1328.                             GetTextEditSelectStartLine(Edit),
  1329.                             GetTextEditSelectStartChar(Edit))
  1330.                             - GetTextViewWidth(Edit->View) + (2 * CheckValue));
  1331.                     }
  1332.             }
  1333.     }
  1334.  
  1335.  
  1336. /* show the starting edge of the selection. */
  1337. void                            TextEditShowSelectionStartEdge(TextEditRec* Edit)
  1338.     {
  1339.         long            CheckValue;
  1340.  
  1341.         CheckPtrExistence(Edit);
  1342.         /* figure out how much space to leave at bottom and top edges */
  1343.         CheckValue = 4;
  1344.         while ((CheckValue > 0)
  1345.             && (TextViewNumVisibleLines(Edit->View) < CheckValue * 2 + 4))
  1346.             {
  1347.                 CheckValue -= 1;
  1348.             }
  1349.         /* vertical adjustment */
  1350.         /* center the beginning in the box */
  1351.         if (GetTextEditSelectStartLine(Edit) < GetTextEditTopLine(Edit) + CheckValue)
  1352.             {
  1353.                 /* selection is to far up */
  1354.                 SetTextEditTopLine(Edit,GetTextEditSelectStartLine(Edit) - CheckValue);
  1355.             }
  1356.         if (GetTextEditSelectStartLine(Edit) >= GetTextEditTopLine(Edit)
  1357.             + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1))
  1358.             {
  1359.                 /* selection is too far down */
  1360.                 SetTextEditTopLine(Edit,GetTextEditSelectStartLine(Edit)
  1361.                     - (TextViewNumVisibleLines(Edit->View) - CheckValue - 1) + 1);
  1362.             }
  1363.         /* figure out how much space on left and right edges to leave */
  1364.         CheckValue = 32;
  1365.         while ((CheckValue > 0)
  1366.             && (GetTextViewWidth(Edit->View) < CheckValue * 4 + 16))
  1367.             {
  1368.                 CheckValue -= 1;
  1369.             }
  1370.         /* horizontal adjustment */
  1371.         if (TextViewScreenXFromCharIndex(Edit->View,
  1372.             GetTextEditSelectStartLine(Edit),
  1373.             GetTextEditSelectStartChar(Edit))
  1374.             < GetTextEditPixelIndent(Edit) + CheckValue)
  1375.             {
  1376.                 SetTextEditPixelIndent(Edit,
  1377.                     TextViewScreenXFromCharIndex(Edit->View,
  1378.                     GetTextEditSelectStartLine(Edit),
  1379.                     GetTextEditSelectStartChar(Edit)) - (2 * CheckValue));
  1380.             }
  1381.         if (TextViewScreenXFromCharIndex(Edit->View,
  1382.             GetTextEditSelectStartLine(Edit),
  1383.             GetTextEditSelectStartChar(Edit))
  1384.             > GetTextEditPixelIndent(Edit)
  1385.             + GetTextViewWidth(Edit->View) - CheckValue)
  1386.             {
  1387.                 SetTextEditPixelIndent(Edit,
  1388.                     TextViewScreenXFromCharIndex(Edit->View,
  1389.                     GetTextEditSelectStartLine(Edit),
  1390.                     GetTextEditSelectStartChar(Edit))
  1391.                     - GetTextViewWidth(Edit->View) + (2 * CheckValue));
  1392.             }
  1393.     }
  1394.  
  1395.  
  1396. /* show the ending edge of the selection. */
  1397. void                            TextEditShowSelectionEndEdge(TextEditRec* Edit)
  1398.     {
  1399.         long            CheckValue;
  1400.  
  1401.         CheckPtrExistence(Edit);
  1402.         /* figure out how much space to leave at bottom and top edges */
  1403.         CheckValue = 4;
  1404.         while ((CheckValue > 0)
  1405.             && (TextViewNumVisibleLines(Edit->View) < CheckValue * 2 + 4))
  1406.             {
  1407.                 CheckValue -= 1;
  1408.             }
  1409.         /* vertical adjustment */
  1410.         /* try to center the end */
  1411.         if (GetTextEditSelectEndLine(Edit) < GetTextEditTopLine(Edit) + CheckValue)
  1412.             {
  1413.                 /* selection is to far up */
  1414.                 SetTextEditTopLine(Edit,GetTextEditSelectEndLine(Edit) - CheckValue);
  1415.             }
  1416.         if (GetTextEditSelectEndLine(Edit) >= GetTextEditTopLine(Edit)
  1417.             + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1))
  1418.             {
  1419.                 /* selection is too far down */
  1420.                 SetTextEditTopLine(Edit,GetTextEditSelectEndLine(Edit)
  1421.                     - (TextViewNumVisibleLines(Edit->View) - CheckValue - 1) + 1);
  1422.             }
  1423.         /* figure out how much space on left and right edges to leave */
  1424.         CheckValue = 32;
  1425.         while ((CheckValue > 0)
  1426.             && (GetTextViewWidth(Edit->View) < CheckValue * 4 + 16))
  1427.             {
  1428.                 CheckValue -= 1;
  1429.             }
  1430.         /* horizontal adjustment */
  1431.         if (TextViewScreenXFromCharIndex(Edit->View,
  1432.             GetTextEditSelectEndLine(Edit),
  1433.             GetTextEditSelectEndCharPlusOne(Edit))
  1434.             < GetTextEditPixelIndent(Edit) + CheckValue)
  1435.             {
  1436.                 SetTextEditPixelIndent(Edit,
  1437.                     TextViewScreenXFromCharIndex(Edit->View,
  1438.                     GetTextEditSelectEndLine(Edit),
  1439.                     GetTextEditSelectEndCharPlusOne(Edit)) - (2 * CheckValue));
  1440.             }
  1441.         if (TextViewScreenXFromCharIndex(Edit->View,
  1442.             GetTextEditSelectEndLine(Edit),
  1443.             GetTextEditSelectEndCharPlusOne(Edit))
  1444.             > GetTextEditPixelIndent(Edit)
  1445.             + GetTextViewWidth(Edit->View) - CheckValue)
  1446.             {
  1447.                 SetTextEditPixelIndent(Edit,
  1448.                     TextViewScreenXFromCharIndex(Edit->View,
  1449.                     GetTextEditSelectEndLine(Edit),
  1450.                     GetTextEditSelectEndCharPlusOne(Edit))
  1451.                     - GetTextViewWidth(Edit->View) + (2 * CheckValue));
  1452.             }
  1453.     }
  1454.  
  1455.  
  1456. /* handle a keypress for inserting or deleting into the text box */
  1457. void                            TextEditDoKeyPressed(TextEditRec* Edit, char TheKey,
  1458.                                         ModifierFlags Modifiers)
  1459.     {
  1460.         MyBoolean                Extension;
  1461.         MyBoolean                DoShowSelection = True;
  1462.  
  1463.         CheckPtrExistence(Edit);
  1464.         Extension = ((Modifiers & eShiftKey) != 0);
  1465.         switch (((unsigned char)TheKey) & 0xff)
  1466.             {
  1467.                 case 0: case 1: case 2: case 4: case 5: case 6: case 7:
  1468.                 case 10: case 11: case 12: case 14: case 15:
  1469.                 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
  1470.                 case 24: case 25: case 26: case 27:
  1471.                     break;
  1472.                 case 3:  /* show selection */
  1473.                     /* TextEditShowSelection(Edit); */
  1474.                     break;
  1475.                 case 13:  /* carriage return */
  1476.                     if (TextViewIsThereValidSelection(Edit->View))
  1477.                         {
  1478.                             /* delete any existing section & restore insertion point */
  1479.                             TextEditDoMenuClear(Edit);
  1480.                         }
  1481.                     TextViewBreakLine(Edit->View,GetTextEditSelectStartLine(Edit),
  1482.                         GetTextEditSelectStartChar(Edit));
  1483.                     SetTextEditInsertionPoint(Edit,
  1484.                         GetTextEditSelectStartLine(Edit) + 1,0);
  1485.                     TextEditKeyPressedUndoSave(Edit); /* record the new line in the undo info */
  1486.                     if (Edit->AutoIndent)
  1487.                         {
  1488.                             char*                PreviousLine;
  1489.                             long                CharScan;
  1490.                             long                Limit;
  1491.  
  1492.                             PreviousLine = GetTextEditLine(Edit,GetTextEditSelectStartLine(Edit) - 1);
  1493.                             if (PreviousLine != NIL)
  1494.                                 {
  1495.                                     Limit = PtrSize(PreviousLine);
  1496.                                     CharScan = 0;
  1497.                                     while ((CharScan < Limit) && ((PreviousLine[CharScan] == 9)
  1498.                                         || (PreviousLine[CharScan] == 32))) /* space or tab */
  1499.                                         {
  1500.                                             /* yo, this is recursive and could cause problems.  It would */
  1501.                                             /* be a BAD idea to call this if the char was a carriage return */
  1502.                                             TextEditDoKeyPressed(Edit,PreviousLine[CharScan],eNoModifiers);
  1503.                                             CharScan += 1;
  1504.                                         }
  1505.                                     ReleasePtr(PreviousLine);
  1506.                                 }
  1507.                         }
  1508.                     TextEditRecalcVerticalScroll(Edit);
  1509.                     TextEditRecalcHorizontalScroll(Edit);
  1510.                     break;
  1511.                 case 8:  /* backspace key */
  1512.                 case 127:  /* delete key */
  1513.                     if (TextViewIsThereValidSelection(Edit->View))
  1514.                         {
  1515.                             /* delete any existing section & restore insertion point */
  1516.                             TextEditDoMenuClear(Edit);
  1517.                         }
  1518.                      else
  1519.                         {
  1520.                             if (GetTextEditSelectStartChar(Edit) == 0)
  1521.                                 {
  1522.                                     long                SelStartLine;
  1523.  
  1524.                                     /* delete carriage return */
  1525.                                     SelStartLine = GetTextEditSelectStartLine(Edit);
  1526.                                     if (SelStartLine > 0)
  1527.                                         {
  1528.                                             long                FirstLineLength;
  1529.  
  1530.                                             FirstLineLength = GetTextViewLineLength(Edit->View,
  1531.                                                 SelStartLine - 1);
  1532.                                             TextViewFoldLines(Edit->View,SelStartLine - 1);
  1533.                                             SetTextEditInsertionPoint(Edit,SelStartLine - 1,FirstLineLength);
  1534.                                             TextEditRememberUndoDeletedCR(Edit);
  1535.                                             TextEditRecalcVerticalScroll(Edit);
  1536.                                             TextEditRecalcHorizontalScroll(Edit);
  1537.                                         }
  1538.                                     /* else, can't delete past start */
  1539.                                 }
  1540.                              else
  1541.                                 {
  1542.                                     char*                    LineTemp;
  1543.                                     char*                    LineCopy;
  1544.  
  1545.                                     LineTemp = GetTextEditLine(Edit,GetTextEditSelectStartLine(Edit));
  1546.                                     if (LineTemp != NIL)
  1547.                                         {
  1548.                                             char                    WhatWeDeleted;
  1549.  
  1550.                                             WhatWeDeleted = LineTemp[GetTextEditSelectStartChar(Edit) - 1];
  1551.                                             LineCopy = RemoveBlockFromBlockCopy(LineTemp,
  1552.                                                 GetTextEditSelectStartChar(Edit) - 1,1);
  1553.                                             if (LineCopy != NIL)
  1554.                                                 {
  1555.                                                     SetTextEditInsertionPoint(Edit,
  1556.                                                         GetTextEditSelectStartLine(Edit),
  1557.                                                         GetTextEditSelectStartChar(Edit) - 1);
  1558.                                                     /* this is one of the few places where we can use */
  1559.                                                     /* SetTextViewLine instead of SetTextEditLine.  We have */
  1560.                                                     /* to since SetTextEditLine deletes the Undo information. */
  1561.                                                     SetTextViewLine(Edit->View,
  1562.                                                         GetTextEditSelectStartLine(Edit),LineCopy);
  1563.                                                     TextEditRememberUndoDeletedChar(Edit,WhatWeDeleted);
  1564.                                                     ReleasePtr(LineCopy);
  1565.                                                 }
  1566.                                             ReleasePtr(LineTemp);
  1567.                                         }
  1568.                                     TextEditRecalcHorizontalScroll(Edit);
  1569.                                 }
  1570.                         }
  1571.                     break;
  1572.                 case eLeftArrow:
  1573.                     if ((Modifiers & eCommandKey) != 0)
  1574.                         {
  1575.                             if (!Extension)
  1576.                                 {
  1577.                                     SetTextEditInsertionPoint(Edit,
  1578.                                         GetTextEditSelectStartLine(Edit),0);
  1579.                                 }
  1580.                              else
  1581.                                 {
  1582.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),0,
  1583.                                         GetTextEditSelectEndLine(Edit),
  1584.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1585.                                     DoShowSelection = False;
  1586.                                     TextEditShowSelectionStartEdge(Edit);
  1587.                                 }
  1588.                         }
  1589.                     else if ((Modifiers & eOptionKey) != 0)
  1590.                         {
  1591.                             long                Line;
  1592.                             long                Index;
  1593.                             char*                LineTemp;
  1594.  
  1595.                             Line = GetTextEditSelectStartLine(Edit);
  1596.                             Index = GetTextEditSelectStartChar(Edit);
  1597.                             LineTemp = GetTextEditLine(Edit,Line);
  1598.                             if (LineTemp != NIL)
  1599.                                 {
  1600.                                     if (Index > 0)
  1601.                                         {
  1602.                                             while ((Index > 0) && !AlphaNum(LineTemp[Index - 1]))
  1603.                                                 {
  1604.                                                     /* skipping white space between cursor & previous word */
  1605.                                                     Index -= 1;
  1606.                                                 }
  1607.                                             while ((Index > 0) && AlphaNum(LineTemp[Index - 1]))
  1608.                                                 {
  1609.                                                     /* skipping over the word itself */
  1610.                                                     Index -= 1;
  1611.                                                 }
  1612.                                         }
  1613.                                      else
  1614.                                         {
  1615.                                             if (Line > 0)
  1616.                                                 {
  1617.                                                     Line -= 1;
  1618.                                                     Index = GetTextViewLineLength(Edit->View,Line);
  1619.                                                 }
  1620.                                         }
  1621.                                     ReleasePtr(LineTemp);
  1622.                                 }
  1623.                             if (!Extension)
  1624.                                 {
  1625.                                     SetTextEditInsertionPoint(Edit,Line,Index);
  1626.                                 }
  1627.                              else
  1628.                                 {
  1629.                                     SetTextEditSelection(Edit,Line,Index,
  1630.                                         GetTextEditSelectEndLine(Edit),
  1631.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1632.                                     DoShowSelection = False;
  1633.                                     TextEditShowSelectionStartEdge(Edit);
  1634.                                 }
  1635.                         }
  1636.                     else
  1637.                         {
  1638.                             if (TextEditIsThereValidSelection(Edit) && !Extension)
  1639.                                 {
  1640.                                     SetTextEditInsertionPoint(Edit,
  1641.                                         GetTextEditSelectStartLine(Edit),GetTextEditSelectStartChar(Edit));
  1642.                                 }
  1643.                              else
  1644.                                 {
  1645.                                     if (GetTextEditSelectStartChar(Edit) == 0)
  1646.                                         {
  1647.                                             if (GetTextEditSelectStartLine(Edit) > 0)
  1648.                                                 {
  1649.                                                     if (!Extension)
  1650.                                                         {
  1651.                                                             SetTextEditInsertionPoint(Edit,
  1652.                                                                 GetTextEditSelectStartLine(Edit) - 1,
  1653.                                                                 GetTextViewLineLength(Edit->View,
  1654.                                                                 GetTextEditSelectStartLine(Edit) - 1));
  1655.                                                         }
  1656.                                                      else
  1657.                                                         {
  1658.                                                             SetTextEditSelection(Edit,
  1659.                                                                 GetTextEditSelectStartLine(Edit) - 1,
  1660.                                                                 GetTextViewLineLength(Edit->View,
  1661.                                                                     GetTextEditSelectStartLine(Edit) - 1),
  1662.                                                                 GetTextEditSelectEndLine(Edit),
  1663.                                                                 GetTextEditSelectEndCharPlusOne(Edit));
  1664.                                                             DoShowSelection = False;
  1665.                                                             TextEditShowSelectionStartEdge(Edit);
  1666.                                                         }
  1667.                                                 }
  1668.                                         }
  1669.                                      else
  1670.                                         {
  1671.                                             if (!Extension)
  1672.                                                 {
  1673.                                                     SetTextEditInsertionPoint(Edit,
  1674.                                                         GetTextEditSelectStartLine(Edit),
  1675.                                                         GetTextEditSelectStartChar(Edit) - 1);
  1676.                                                 }
  1677.                                              else
  1678.                                                 {
  1679.                                                     SetTextEditSelection(Edit,
  1680.                                                         GetTextEditSelectStartLine(Edit),
  1681.                                                         GetTextEditSelectStartChar(Edit) - 1,
  1682.                                                         GetTextEditSelectEndLine(Edit),
  1683.                                                         GetTextEditSelectEndCharPlusOne(Edit));
  1684.                                                     DoShowSelection = False;
  1685.                                                     TextEditShowSelectionStartEdge(Edit);
  1686.                                                 }
  1687.                                         }
  1688.                                 }
  1689.                         }
  1690.                     break;
  1691.                 case eRightArrow:
  1692.                     if ((Modifiers & eCommandKey) != 0)
  1693.                         {
  1694.                             if (!Extension)
  1695.                                 {
  1696.                                     SetTextEditInsertionPoint(Edit,GetTextEditSelectEndLine(Edit),
  1697.                                         GetTextViewLineLength(Edit->View,GetTextEditSelectEndLine(Edit)));
  1698.                                 }
  1699.                              else
  1700.                                 {
  1701.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1702.                                         GetTextEditSelectStartChar(Edit),GetTextEditSelectEndLine(Edit),
  1703.                                         GetTextViewLineLength(Edit->View,GetTextEditSelectEndLine(Edit)));
  1704.                                     DoShowSelection = False;
  1705.                                     TextEditShowSelectionEndEdge(Edit);
  1706.                                 }
  1707.                         }
  1708.                     else if ((Modifiers & eOptionKey) != 0)
  1709.                         {
  1710.                             long                Line;
  1711.                             long                Index;
  1712.                             long                Length;
  1713.                             char*                LineTemp;
  1714.  
  1715.                             Line = GetTextEditSelectEndLine(Edit);
  1716.                             Index = GetTextEditSelectEndCharPlusOne(Edit);
  1717.                             Length = GetTextViewLineLength(Edit->View,Line);
  1718.                             LineTemp = GetTextEditLine(Edit,Line);
  1719.                             if (LineTemp != NIL)
  1720.                                 {
  1721.                                     if (Index < Length)
  1722.                                         {
  1723.                                             while ((Index < Length) && !AlphaNum(LineTemp[Index]))
  1724.                                                 {
  1725.                                                     /* skipping white space between cursor & next word */
  1726.                                                     Index += 1;
  1727.                                                 }
  1728.                                             while ((Index < Length) && AlphaNum(LineTemp[Index]))
  1729.                                                 {
  1730.                                                     /* skipping over the word itself */
  1731.                                                     Index += 1;
  1732.                                                 }
  1733.                                         }
  1734.                                      else
  1735.                                         {
  1736.                                             if (Line < GetTextViewNumLines(Edit->View) - 1)
  1737.                                                 {
  1738.                                                     Line += 1;
  1739.                                                     Index = 0;
  1740.                                                 }
  1741.                                         }
  1742.                                     ReleasePtr(LineTemp);
  1743.                                 }
  1744.                             if (!Extension)
  1745.                                 {
  1746.                                     SetTextEditInsertionPoint(Edit,Line,Index);
  1747.                                 }
  1748.                              else
  1749.                                 {
  1750.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1751.                                         GetTextEditSelectStartChar(Edit),Line,Index);
  1752.                                     DoShowSelection = False;
  1753.                                     TextEditShowSelectionEndEdge(Edit);
  1754.                                 }
  1755.                         }
  1756.                     else
  1757.                         {
  1758.                             if (TextEditIsThereValidSelection(Edit) && !Extension)
  1759.                                 {
  1760.                                     SetTextEditInsertionPoint(Edit,GetTextEditSelectEndLine(Edit),
  1761.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1762.                                 }
  1763.                              else
  1764.                                 {
  1765.                                     if (GetTextEditSelectEndCharPlusOne(Edit) == GetTextViewLineLength(
  1766.                                         Edit->View,GetTextEditSelectEndLine(Edit)))
  1767.                                         {
  1768.                                             if (GetTextEditSelectEndLine(Edit) <
  1769.                                                 GetTextViewNumLines(Edit->View) - 1)
  1770.                                                 {
  1771.                                                     if (!Extension)
  1772.                                                         {
  1773.                                                             SetTextEditInsertionPoint(Edit,
  1774.                                                                 GetTextEditSelectEndLine(Edit) + 1,0);
  1775.                                                         }
  1776.                                                      else
  1777.                                                         {
  1778.                                                             SetTextEditSelection(Edit,
  1779.                                                                 GetTextEditSelectStartLine(Edit),
  1780.                                                                 GetTextEditSelectStartChar(Edit),
  1781.                                                                 GetTextEditSelectEndLine(Edit) + 1,0);
  1782.                                                             DoShowSelection = False;
  1783.                                                             TextEditShowSelectionEndEdge(Edit);
  1784.                                                         }
  1785.                                                 }
  1786.                                         }
  1787.                                      else
  1788.                                         {
  1789.                                             if (!Extension)
  1790.                                                 {
  1791.                                                     SetTextEditInsertionPoint(Edit,GetTextEditSelectEndLine(Edit),
  1792.                                                         GetTextEditSelectEndCharPlusOne(Edit) + 1);
  1793.                                                 }
  1794.                                              else
  1795.                                                 {
  1796.                                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1797.                                                         GetTextEditSelectStartChar(Edit),
  1798.                                                         GetTextEditSelectEndLine(Edit),
  1799.                                                         GetTextEditSelectEndCharPlusOne(Edit) + 1);
  1800.                                                     DoShowSelection = False;
  1801.                                                     TextEditShowSelectionEndEdge(Edit);
  1802.                                                 }
  1803.                                         }
  1804.                                 }
  1805.                         }
  1806.                     break;
  1807.                 case eUpArrow:
  1808.                     if ((Modifiers & eCommandKey) != 0)
  1809.                         {
  1810.                             if (!Extension)
  1811.                                 {
  1812.                                     SetTextEditInsertionPoint(Edit,0,0);
  1813.                                 }
  1814.                              else
  1815.                                 {
  1816.                                     SetTextEditSelection(Edit,0,0,GetTextEditSelectEndLine(Edit),
  1817.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1818.                                     DoShowSelection = False;
  1819.                                     TextEditShowSelectionStartEdge(Edit);
  1820.                                 }
  1821.                         }
  1822.                     else if (((Modifiers & eOptionKey) != 0) && !Extension)
  1823.                         {
  1824.                             long                        NewPosition;
  1825.                             long                        NewPoint;
  1826.  
  1827.                             NewPosition = GetTextEditSelectStartLine(Edit)
  1828.                                 - TextViewNumVisibleLines(Edit->View) + 5;
  1829.                             if (NewPosition < 0)
  1830.                                 {
  1831.                                     NewPosition = 0;
  1832.                                 }
  1833.                             NewPoint = TextViewCharIndexFromScreenX(Edit->View,
  1834.                                 NewPosition, /* previous line */
  1835.                                 TextViewScreenXFromCharIndex(Edit->View,
  1836.                                 GetTextEditSelectStartLine(Edit), /* this line */
  1837.                                 GetTextEditSelectStartChar(Edit)));
  1838.                             SetTextEditInsertionPoint(Edit,NewPosition,NewPoint);
  1839.                         }
  1840.                     else
  1841.                         {
  1842.                             long                        NewLineIndex;
  1843.                             long                        NewPoint;
  1844.  
  1845.                             NewLineIndex = GetTextEditSelectStartLine(Edit) - 1;
  1846.                             if (NewLineIndex < 0)
  1847.                                 {
  1848.                                     NewLineIndex = 0;
  1849.                                 }
  1850.                             /* snap it to the closest point on the next line */
  1851.                             NewPoint = TextViewCharIndexFromScreenX(Edit->View,
  1852.                                 NewLineIndex, /* previous line */
  1853.                                 TextViewScreenXFromCharIndex(Edit->View,
  1854.                                 GetTextEditSelectStartLine(Edit), /* this line */
  1855.                                 GetTextEditSelectStartChar(Edit)));
  1856.                             if (!Extension)
  1857.                                 {
  1858.                                     SetTextEditInsertionPoint(Edit,NewLineIndex,NewPoint);
  1859.                                 }
  1860.                              else
  1861.                                 {
  1862.                                     SetTextEditSelection(Edit,NewLineIndex,NewPoint,
  1863.                                         GetTextEditSelectEndLine(Edit),
  1864.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1865.                                     DoShowSelection = False;
  1866.                                     TextEditShowSelectionStartEdge(Edit);
  1867.                                 }
  1868.                         }
  1869.                     break;
  1870.                 case eDownArrow:
  1871.                     if ((Modifiers & eCommandKey) != 0)
  1872.                         {
  1873.                             if (!Extension)
  1874.                                 {
  1875.                                     SetTextEditInsertionPoint(Edit,GetTextViewNumLines(
  1876.                                         Edit->View) - 1,GetTextViewLineLength(Edit->View,
  1877.                                         GetTextViewNumLines(Edit->View) - 1));
  1878.                                 }
  1879.                              else
  1880.                                 {
  1881.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1882.                                         GetTextEditSelectStartChar(Edit),GetTextViewNumLines(
  1883.                                         Edit->View) - 1,GetTextViewLineLength(Edit->View,
  1884.                                         GetTextViewNumLines(Edit->View) - 1));
  1885.                                     DoShowSelection = False;
  1886.                                     TextEditShowSelectionEndEdge(Edit);
  1887.                                 }
  1888.                         }
  1889.                     else if (((Modifiers & eOptionKey) != 0) && !Extension)
  1890.                         {
  1891.                             long                        NewPosition;
  1892.                             long                        NewPoint;
  1893.  
  1894.                             NewPosition = GetTextEditSelectEndLine(Edit)
  1895.                                 + TextViewNumVisibleLines(Edit->View) - 5;
  1896.                             if (NewPosition > GetTextViewNumLines(Edit->View) - 1)
  1897.                                 {
  1898.                                     NewPosition = GetTextViewNumLines(Edit->View) - 1;
  1899.                                 }
  1900.                             NewPoint = TextViewCharIndexFromScreenX(Edit->View,
  1901.                                 NewPosition, /* next line */
  1902.                                 TextViewScreenXFromCharIndex(Edit->View,
  1903.                                 GetTextEditSelectEndLine(Edit), /* this line */
  1904.                                 GetTextEditSelectEndCharPlusOne(Edit)));
  1905.                             SetTextEditInsertionPoint(Edit,NewPosition,NewPoint);
  1906.                         }
  1907.                     else
  1908.                         {
  1909.                             long                        NewLineIndex;
  1910.                             long                        NewPoint;
  1911.  
  1912.                             NewLineIndex = GetTextEditSelectEndLine(Edit) + 1;
  1913.                             if (NewLineIndex > GetTextViewNumLines(Edit->View) - 1)
  1914.                                 {
  1915.                                     NewLineIndex = GetTextViewNumLines(Edit->View) - 1;
  1916.                                 }
  1917.                             /* snap it to the closest point on the next line */
  1918.                             NewPoint = TextViewCharIndexFromScreenX(Edit->View,
  1919.                                 NewLineIndex, /* next line */
  1920.                                 TextViewScreenXFromCharIndex(Edit->View,
  1921.                                 GetTextEditSelectEndLine(Edit), /* this line */
  1922.                                 GetTextEditSelectEndCharPlusOne(Edit)));
  1923.                             if (!Extension)
  1924.                                 {
  1925.                                     SetTextEditInsertionPoint(Edit,NewLineIndex,NewPoint);
  1926.                                 }
  1927.                              else
  1928.                                 {
  1929.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1930.                                         GetTextEditSelectStartChar(Edit),NewLineIndex,NewPoint);
  1931.                                     DoShowSelection = False;
  1932.                                     TextEditShowSelectionEndEdge(Edit);
  1933.                                 }
  1934.                         }
  1935.                     break;
  1936.                 case 9:  /* tab key */
  1937.                 default:  /* any other character */
  1938.                     if ((Modifiers & eCommandKey) == 0)
  1939.                         {
  1940.                             char*                    LineTemp;
  1941.                             char                    Buffer[1];
  1942.                             char*                    LineCopy;
  1943.  
  1944.                             CheckPtrExistence(Edit);
  1945.                             if (TextViewIsThereValidSelection(Edit->View))
  1946.                                 {
  1947.                                     /* delete any existing section & restore insertion point */
  1948.                                     TextEditDoMenuClear(Edit);
  1949.                                 }
  1950.                             LineTemp = GetTextEditLine(Edit,GetTextEditSelectStartLine(Edit));
  1951.                             if (LineTemp != NIL)
  1952.                                 {
  1953.                                     Buffer[0] = TheKey;
  1954.                                     LineCopy = InsertBlockIntoBlockCopy(LineTemp,Buffer,
  1955.                                         GetTextEditSelectStartChar(Edit),1);
  1956.                                     if (LineCopy != NIL)
  1957.                                         {
  1958.                                             /* this is one of the few places where we can use */
  1959.                                             /* SetTextViewLine instead of SetTextEditLine.  We have */
  1960.                                             /* to since SetTextEditLine deletes the Undo information. */
  1961.                                             SetTextViewLine(Edit->View,
  1962.                                                 GetTextEditSelectStartLine(Edit),LineCopy);
  1963.                                             ReleasePtr(LineCopy);
  1964.                                         }
  1965.                                     SetTextEditInsertionPoint(Edit,GetTextEditSelectStartLine(Edit),
  1966.                                         GetTextEditSelectStartChar(Edit) + 1);
  1967.                                     ReleasePtr(LineTemp);
  1968.                                     TextEditKeyPressedUndoSave(Edit);
  1969.                                 }
  1970.                         }
  1971.                     break;
  1972.             }
  1973.         if (DoShowSelection)
  1974.             {
  1975.                 TextEditShowSelection(Edit); /* make sure insertion point is on screen */
  1976.             }
  1977.     }
  1978.  
  1979.  
  1980. /* vertical scroll callback routine */
  1981. static void            TEVerticalScrollHook(long Parameter, ScrollType How,
  1982.                                     TextEditRec* TempScrollEdit)
  1983.     {
  1984.         CheckPtrExistence(TempScrollEdit);
  1985.         switch (How)
  1986.             {
  1987.                 case eScrollToPosition:
  1988.                     SetTextEditTopLine(TempScrollEdit,Parameter);
  1989.                     break;
  1990.                 case eScrollPageMinus:
  1991.                     SetTextEditTopLine(TempScrollEdit,GetTextEditTopLine(TempScrollEdit)
  1992.                         - (TextViewNumVisibleLines(TempScrollEdit->View) - 3));
  1993.                     break;
  1994.                 case eScrollPagePlus:
  1995.                     SetTextEditTopLine(TempScrollEdit,GetTextEditTopLine(TempScrollEdit)
  1996.                         + (TextViewNumVisibleLines(TempScrollEdit->View) - 3));
  1997.                     break;
  1998.                 case eScrollLineMinus:
  1999.                     SetTextEditTopLine(TempScrollEdit,GetTextEditTopLine(TempScrollEdit) - 1);
  2000.                     break;
  2001.                 case eScrollLinePlus:
  2002.                     SetTextEditTopLine(TempScrollEdit,GetTextEditTopLine(TempScrollEdit) + 1);
  2003.                     break;
  2004.                 default:
  2005.                     EXECUTE(PRERR(AllowResume,"TEVerticalScrollHook:  Unknown scroll opcode"));
  2006.             }
  2007.     }
  2008.  
  2009.  
  2010. /* horizontal scroll callback routine */
  2011. static void            TEHorizontalScrollHook(long Parameter, ScrollType How,
  2012.                                     TextEditRec* TempScrollEdit)
  2013.     {
  2014.         CheckPtrExistence(TempScrollEdit);
  2015.         switch (How)
  2016.             {
  2017.                 case eScrollToPosition:
  2018.                     SetTextEditPixelIndent(TempScrollEdit,Parameter);
  2019.                     break;
  2020.                 case eScrollPageMinus:
  2021.                     SetTextEditPixelIndent(TempScrollEdit,GetTextEditPixelIndent(TempScrollEdit)
  2022.                         - (GetTextViewWidth(TempScrollEdit->View) - 32));
  2023.                     break;
  2024.                 case eScrollPagePlus:
  2025.                     SetTextEditPixelIndent(TempScrollEdit,GetTextEditPixelIndent(TempScrollEdit)
  2026.                         + (GetTextViewWidth(TempScrollEdit->View) - 32));
  2027.                     break;
  2028.                 case eScrollLineMinus:
  2029.                     SetTextEditPixelIndent(TempScrollEdit,
  2030.                         GetTextEditPixelIndent(TempScrollEdit) - 32);
  2031.                     break;
  2032.                 case eScrollLinePlus:
  2033.                     SetTextEditPixelIndent(TempScrollEdit,
  2034.                         GetTextEditPixelIndent(TempScrollEdit) + 32);
  2035.                     break;
  2036.                 default:
  2037.                     EXECUTE(PRERR(AllowResume,"TEHorizontalScrollHook:  Unknown scroll opcode"));
  2038.             }
  2039.     }
  2040.  
  2041.  
  2042. /* handle a mouse-down in the text box */
  2043. void                            TextEditDoMouseDown(TextEditRec* Edit, OrdType OrigX, OrdType OrigY,
  2044.                                         ModifierFlags Modifiers)
  2045.     {
  2046.         SelRec                BaseSelectionStart; /* where the mouse first hits, and if shift */
  2047.         SelRec                BaseSelectionEnd; /* is down, then it's the previous range */
  2048.         SelRec                CurrentMousePoint;
  2049.         SelRec                TempFirst;
  2050.         SelRec                TempLast;
  2051.         SelRec                OldSelectionStart;
  2052.         SelRec                OldSelectionEnd;
  2053.         OrdType                WhereX;
  2054.         OrdType                WhereY;
  2055.         OrdType                MouseLocationX;
  2056.         OrdType                MouseLocationY;
  2057.  
  2058.         CheckPtrExistence(Edit);
  2059.         WhereX = OrigX;
  2060.         WhereY = OrigY;
  2061.         if (Edit->VerticalScroll != NIL)
  2062.             {
  2063.                 if (ScrollHitTest(Edit->VerticalScroll,WhereX,WhereY))
  2064.                     {
  2065.                         ScrollHitProc(Edit->VerticalScroll,Modifiers,WhereX,
  2066.                             WhereY,Edit,(void (*)(long,ScrollType,void*))&TEVerticalScrollHook);
  2067.                         return;
  2068.                     }
  2069.             }
  2070.         if (Edit->HorizontalScroll != NIL)
  2071.             {
  2072.                 if (ScrollHitTest(Edit->HorizontalScroll,WhereX,WhereY))
  2073.                     {
  2074.                         ScrollHitProc(Edit->HorizontalScroll,Modifiers,WhereX,
  2075.                             WhereY,Edit,(void (*)(long,ScrollType,void*))&TEHorizontalScrollHook);
  2076.                         return;
  2077.                     }
  2078.             }
  2079.         WhereX -= GetTextViewXLoc(Edit->View);
  2080.         WhereY -= GetTextViewYLoc(Edit->View);
  2081.  
  2082.         if ((TimerDifference(ReadTimer(),Edit->LastClickTime) < GetDoubleClickInterval())
  2083.             && ((WhereX - Edit->LastClickX <= 3) && (WhereX - Edit->LastClickX >= -3))
  2084.             && ((WhereY - Edit->LastClickY <= 3) && (WhereY - Edit->LastClickY >= -3)))
  2085.             {
  2086.                 switch (Edit->ClickPhase)
  2087.                     {
  2088.                         case eNoClick:
  2089.                             Edit->ClickPhase = eSingleClick;
  2090.                             break;
  2091.                         case eSingleClick:
  2092.                             Edit->ClickPhase = eDoubleClick;
  2093.                             break;
  2094.                         case eDoubleClick:
  2095.                             Edit->ClickPhase = eTripleClick;
  2096.                             break;
  2097.                         case eTripleClick:
  2098.                             /* no change */
  2099.                             break;
  2100.                     }
  2101.             }
  2102.          else
  2103.             {
  2104.                 Edit->ClickPhase = eSingleClick;
  2105.             }
  2106.         Edit->LastClickTime = ReadTimer();
  2107.         Edit->LastClickX = WhereX;
  2108.         Edit->LastClickY = WhereY;
  2109.  
  2110.         CurrentMousePoint.Line = WhereY / GetTextViewLineHeight(Edit->View)
  2111.             + GetTextViewTopLine(Edit->View);
  2112.         CurrentMousePoint.Column = TextViewCharIndexFromScreenX(Edit->View,
  2113.             CurrentMousePoint.Line,WhereX + GetTextViewPixelIndent(Edit->View));
  2114.         if ((Modifiers & eShiftKey) != 0)
  2115.             {
  2116.                 BaseSelectionStart.Line = GetTextEditSelectStartLine(Edit);
  2117.                 BaseSelectionStart.Column = GetTextEditSelectStartChar(Edit);
  2118.                 BaseSelectionEnd.Line = GetTextEditSelectEndLine(Edit);
  2119.                 BaseSelectionEnd.Column = GetTextEditSelectEndCharPlusOne(Edit);
  2120.             }
  2121.          else
  2122.             {
  2123.                 if (TextEditIsThereValidSelection(Edit))
  2124.                     {
  2125.                         SelRec                Start,End;
  2126.  
  2127.                         Start.Line = GetTextEditSelectStartLine(Edit);
  2128.                         Start.Column = GetTextEditSelectStartChar(Edit);
  2129.                         End.Line = GetTextEditSelectEndLine(Edit);
  2130.                         End.Column = GetTextEditSelectEndCharPlusOne(Edit);
  2131.                         SetTextEditInsertionPoint(Edit,Start.Line,Start.Column);
  2132.                         TextViewRedrawRange(Edit->View,Start.Line,End.Line);
  2133.                     }
  2134.                 BaseSelectionStart = CurrentMousePoint;
  2135.                 BaseSelectionEnd = CurrentMousePoint;
  2136.                 ExtendSelection(Edit,&BaseSelectionStart,&BaseSelectionEnd);
  2137.                 SetTextEditSelection(Edit,BaseSelectionStart.Line,BaseSelectionStart.Column,
  2138.                     BaseSelectionEnd.Line,BaseSelectionEnd.Column);
  2139.             }
  2140.  
  2141.         while (eMouseUp != GetAnEvent(&MouseLocationX,&MouseLocationY,NIL,NIL,NIL,NIL))
  2142.             {
  2143.                 WhereX = MouseLocationX - GetTextViewXLoc(Edit->View);
  2144.                 WhereY = MouseLocationY - GetTextViewYLoc(Edit->View);
  2145.                 if (WhereX < 0)
  2146.                     {
  2147.                         SetTextEditPixelIndent(Edit,GetTextEditPixelIndent(Edit) - 24);
  2148.                     }
  2149.                 if (WhereX > GetTextViewWidth(Edit->View))
  2150.                     {
  2151.                         SetTextEditPixelIndent(Edit,GetTextEditPixelIndent(Edit) + 24);
  2152.                     }
  2153.                 if (WhereY < 0)
  2154.                     {
  2155.                         SetTextEditTopLine(Edit,GetTextEditTopLine(Edit) - 1);
  2156.                     }
  2157.                 if (WhereY > GetTextViewHeight(Edit->View))
  2158.                     {
  2159.                         SetTextEditTopLine(Edit,GetTextEditTopLine(Edit) + 1);
  2160.                     }
  2161.  
  2162.                 if (WhereX < 0)
  2163.                     {
  2164.                         WhereX = 0;
  2165.                     }
  2166.                 if (WhereX > GetTextViewWidth(Edit->View) - 1)
  2167.                     {
  2168.                         WhereX = GetTextViewWidth(Edit->View) - 1;
  2169.                     }
  2170.                 if (WhereY < 0)
  2171.                     {
  2172.                         WhereY = 0;
  2173.                     }
  2174.                 if (WhereY > GetTextViewHeight(Edit->View) - 1)
  2175.                     {
  2176.                         WhereY = GetTextViewHeight(Edit->View) - 1;
  2177.                     }
  2178.                 CurrentMousePoint.Line = WhereY / GetTextViewLineHeight(Edit->View)
  2179.                     + GetTextViewTopLine(Edit->View);
  2180.                 CurrentMousePoint.Column = TextViewCharIndexFromScreenX(Edit->View,
  2181.                     CurrentMousePoint.Line,WhereX + GetTextViewPixelIndent(Edit->View));
  2182.                 /* calculate what the extent of the current mouse selection should be */
  2183.                 if (GreaterThan(&BaseSelectionStart,&CurrentMousePoint)
  2184.                     || ((BaseSelectionStart.Line == CurrentMousePoint.Line)
  2185.                     && (BaseSelectionStart.Column == CurrentMousePoint.Column)))
  2186.                     {
  2187.                         ExtendSelection(Edit,&CurrentMousePoint,NIL);
  2188.                     }
  2189.                  else
  2190.                     {
  2191.                         ExtendSelection(Edit,NIL,&CurrentMousePoint);
  2192.                     }
  2193.                 /* calculating the total selection */
  2194.                 UnionSelection(BaseSelectionStart,BaseSelectionEnd,CurrentMousePoint,
  2195.                     &TempFirst,&TempLast);
  2196.                 /* redrawing what has changed */
  2197.                 OldSelectionStart.Line = GetTextEditSelectStartLine(Edit);
  2198.                 OldSelectionStart.Column = GetTextEditSelectStartChar(Edit);
  2199.                 OldSelectionEnd.Line = GetTextEditSelectEndLine(Edit);
  2200.                 OldSelectionEnd.Column = GetTextEditSelectEndCharPlusOne(Edit);
  2201.                 SetTextEditSelection(Edit,TempFirst.Line,TempFirst.Column,
  2202.                     TempLast.Line,TempLast.Column);
  2203.             }
  2204.     }
  2205.  
  2206.  
  2207. /* this would be called from TextEditDoMenuClear, which would then call */
  2208. /* TextEditBlockRemoved to install the stuff that had been removed */
  2209. static void                TextEditPurgeUndoRecord(TextEditRec* Edit)
  2210.     {
  2211.         CheckPtrExistence(Edit);
  2212.         if (Edit->Undo.CanUndoFlag)
  2213.             {
  2214.                 /* valid record needs purging */
  2215.                 if (Edit->Undo.DeletedValidFlag)
  2216.                     {
  2217.                         /* delete the text thing */
  2218.                         DisposeTextStorage(Edit->Undo.DeletedStuff);
  2219.                     }
  2220.                 Edit->Undo.CanUndoFlag = False;
  2221.             }
  2222.     }
  2223.  
  2224.  
  2225. /* something tricky to watch out for:  If you delete stuff, then insert stuff */
  2226. /* above it, the position that it was deleted from will be invalid.  You then */
  2227. /* have to exactly remove the stuff you inserted above BEFORE deleting when */
  2228. /* you actually perform the 'undo' operation */
  2229. static void                TextEditBlockRemovedUndoSave(TextEditRec* Edit,
  2230.                                         TextStorageRec* Stuff, long WhereLine, long WhereChar)
  2231.     {
  2232.         TextEditPurgeUndoRecord(Edit);
  2233.         Edit->Undo.CanUndoFlag = True;
  2234.         Edit->Undo.DeletedValidFlag = True;
  2235.         Edit->Undo.DeletedStuff = Stuff;
  2236.         Edit->Undo.DeletedLine = WhereLine;
  2237.         Edit->Undo.DeletedChar = WhereChar;
  2238.         Edit->Undo.ReplacingValidFlag = False; /* haven't started replacing yet */
  2239.     }
  2240.  
  2241.  
  2242. /* this routine is called when a key is pressed to update the information */
  2243. /* for the undo record.  If the insertion point is in a place indicating that */
  2244. /* the user did NOT move to another location, then it will be added to the record. */
  2245. /* otherwise the undo record will be purged and a new one started. */
  2246. static void                TextEditKeyPressedUndoSave(TextEditRec* Edit)
  2247.     {
  2248.         CheckPtrExistence(Edit);
  2249.         ERROR(TextEditIsThereValidSelection(Edit),PRERR(AllowResume,
  2250.             "TextEditKeyPressedUndoSave:  Why is there a valid selection?"));
  2251.         if (!Edit->Undo.CanUndoFlag)
  2252.             {
  2253.                 Edit->Undo.CanUndoFlag = True;
  2254.                 Edit->Undo.DeletedValidFlag = False;
  2255.                 Edit->Undo.ReplacingValidFlag = False;
  2256.             }
  2257.         if (Edit->Undo.ReplacingValidFlag)
  2258.             {
  2259.                 /* was the character added in the logical next position? */
  2260.                 if ((Edit->Undo.ReplacingEndLine == GetTextEditSelectEndLine(Edit))
  2261.                     && (Edit->Undo.ReplacingEndCharPlusOne + 1
  2262.                     == GetTextEditSelectEndCharPlusOne(Edit)))
  2263.                     {
  2264.                         /* yup, right after the last char */
  2265.                         Edit->Undo.ReplacingEndCharPlusOne += 1; /* increment & all's cool */
  2266.                     }
  2267.                  else
  2268.                     {
  2269.                         /* there's still hope.  Did they make a new line? */
  2270.                         if ((Edit->Undo.ReplacingEndLine + 1 == GetTextEditSelectEndLine(Edit))
  2271.                             && (GetTextEditSelectEndCharPlusOne(Edit) == 0)
  2272.                             && (Edit->Undo.ReplacingEndCharPlusOne ==
  2273.                             GetTextViewLineLength(Edit->View,Edit->Undo.ReplacingEndLine)))
  2274.                             {
  2275.                                 /* stated in English:  If we're on the line right after the last */
  2276.                                 /* line in the added range, and we're at the beginning, and */
  2277.                                 /* the last char added before this was at the end of the previous */
  2278.                                 /* line, then the user probably hit return. */
  2279.                                 Edit->Undo.ReplacingEndCharPlusOne = 0;
  2280.                                 Edit->Undo.ReplacingEndLine += 1;
  2281.                             }
  2282.                          else
  2283.                             {
  2284.                                 /* otherwise we need to purge and reset. */
  2285.                                 TextEditPurgeUndoRecord(Edit);
  2286.                                 Edit->Undo.CanUndoFlag = True;
  2287.                                 Edit->Undo.DeletedValidFlag = False;
  2288.                              MakeNewRangePoint: /* we might just drop in some day */
  2289.                                 Edit->Undo.ReplacingValidFlag = True; /* only this happens */
  2290.                                 if (GetTextEditSelectStartChar(Edit) == 0)
  2291.                                     {
  2292.                                         ERROR(GetTextEditSelectStartLine(Edit) == 0,PRERR(AllowResume,
  2293.                                             "TextEditKeyPressedUndoSave:  Char "
  2294.                                             "inserted, but insertion point "
  2295.                                             "is still in the home position (0,0)."));
  2296.                                         Edit->Undo.ReplacingStartLine
  2297.                                             = GetTextEditSelectStartLine(Edit) - 1;
  2298.                                         Edit->Undo.ReplacingStartChar
  2299.                                             = GetTextViewLineLength(Edit->View,
  2300.                                             Edit->Undo.ReplacingStartLine);
  2301.                                     }
  2302.                                  else
  2303.                                     {
  2304.                                         Edit->Undo.ReplacingStartChar
  2305.                                             = GetTextEditSelectStartChar(Edit) - 1;
  2306.                                         Edit->Undo.ReplacingStartLine = GetTextEditSelectStartLine(Edit);
  2307.                                     }
  2308.                                 Edit->Undo.ReplacingEndLine = GetTextEditSelectEndLine(Edit);
  2309.                                 Edit->Undo.ReplacingEndCharPlusOne
  2310.                                     = GetTextEditSelectEndCharPlusOne(Edit);
  2311.                             }
  2312.                     }
  2313.             }
  2314.          else
  2315.             {
  2316.                 /* else, there isn't even a range defined.  So define one. */
  2317.                 goto MakeNewRangePoint;
  2318.             }
  2319.     }
  2320.  
  2321.  
  2322. /* undo the last operation that changed the contained data.  not all operations */
  2323. /* can be undone.  Who knows what the state of things will be if this fails. */
  2324. MyBoolean                    TextEditDoMenuUndo(TextEditRec* Edit)
  2325.     {
  2326.         TextStorageRec*    OldAddedStuff;
  2327.         MyBoolean                OldStuffValid;
  2328.         long                        OldAddedLine;
  2329.         long                        OldAddedChar;
  2330.         MyBoolean                StuffWasInserted;
  2331.  
  2332.         CheckPtrExistence(Edit);
  2333.         if (Edit->Undo.CanUndoFlag)
  2334.             {
  2335.                 OldStuffValid = Edit->Undo.ReplacingValidFlag;
  2336.                 if (OldStuffValid)
  2337.                     {
  2338.                         SetTextEditSelection(Edit,Edit->Undo.ReplacingStartLine,
  2339.                             Edit->Undo.ReplacingStartChar,Edit->Undo.ReplacingEndLine,
  2340.                             Edit->Undo.ReplacingEndCharPlusOne);
  2341.                         OldAddedStuff = TextViewGetSelection(Edit->View);
  2342.                         if (OldAddedStuff == NIL)
  2343.                             {
  2344.                                 return False; /* abort */
  2345.                             }
  2346.                         if (!TextViewDeleteSelection(Edit->View))
  2347.                             {
  2348.                                 TextEditPurgeUndoRecord(Edit); /* avoid inconsistencies */
  2349.                                 return False;
  2350.                             }
  2351.                         /* save the place where we'd put this stuff back */
  2352.                         OldAddedLine = GetTextEditSelectStartLine(Edit);
  2353.                         OldAddedChar = GetTextEditSelectStartChar(Edit);
  2354.                     }
  2355.                 /* now the coordinates for the old deleted stuff are valid again */
  2356.                 StuffWasInserted = Edit->Undo.DeletedValidFlag;
  2357.                 if (StuffWasInserted)
  2358.                     {
  2359.                         SetTextEditInsertionPoint(Edit,Edit->Undo.DeletedLine,
  2360.                             Edit->Undo.DeletedChar);
  2361.                         if (!TextViewInsertBlock(Edit->View,Edit->Undo.DeletedStuff))
  2362.                             {
  2363.                                 TextEditPurgeUndoRecord(Edit); /* avoid inconsistencies */
  2364.                                 return False;
  2365.                             }
  2366.                         /* this might have failed & nothing (or part) was inserted.  This */
  2367.                         /* will end up selecting what was inserted, but the rest is lost. */
  2368.                         SetTextEditSelection(Edit,Edit->Undo.DeletedLine,
  2369.                             Edit->Undo.DeletedChar,GetTextEditSelectEndLine(Edit),
  2370.                             GetTextEditSelectEndCharPlusOne(Edit));
  2371.                         DisposeTextStorage(Edit->Undo.DeletedStuff);
  2372.                     }
  2373.                 /* now we have to pull a switcheroo */
  2374.                 /* first, the deleted stuff becomes what we removed above */
  2375.                 Edit->Undo.DeletedValidFlag = OldStuffValid;
  2376.                 if (OldStuffValid)
  2377.                     {
  2378.                         Edit->Undo.DeletedLine = OldAddedLine;
  2379.                         Edit->Undo.DeletedChar = OldAddedChar;
  2380.                         Edit->Undo.DeletedStuff = OldAddedStuff;
  2381.                     }
  2382.                 /* now, update the inserted stuff */
  2383.                 Edit->Undo.ReplacingValidFlag = StuffWasInserted;
  2384.                 if (StuffWasInserted)
  2385.                     {
  2386.                         Edit->Undo.ReplacingStartLine = GetTextEditSelectStartLine(Edit);
  2387.                         Edit->Undo.ReplacingStartChar = GetTextEditSelectStartChar(Edit);
  2388.                         Edit->Undo.ReplacingEndLine = GetTextEditSelectEndLine(Edit);
  2389.                         Edit->Undo.ReplacingEndCharPlusOne
  2390.                             = GetTextEditSelectEndCharPlusOne(Edit);
  2391.                     }
  2392.             }
  2393.         return True;
  2394.     }
  2395.  
  2396.  
  2397. /* this routine is called when the user has pressed delete and deleted */
  2398. /* a line break.  If this deletion is a logical extention of the existing */
  2399. /* deleted range, AND there is no insertion, then adjust the deleted range. */
  2400. /* if there is an insertion AND the deletion is of the last character of */
  2401. /* the insertion, then adjust the insertion.  If it is neither, purge the */
  2402. /* undo information and start a new deletion record. */
  2403. static void                TextEditRememberUndoDeletedCR(TextEditRec* Edit)
  2404.     {
  2405.         CheckPtrExistence(Edit);
  2406.         ERROR(TextEditIsThereValidSelection(Edit),PRERR(AllowResume,
  2407.             "TextEditRememberUndoDeletedCR:  Why is there a valid selection?"));
  2408.         if (!Edit->Undo.CanUndoFlag)
  2409.             {
  2410.                 Edit->Undo.CanUndoFlag = True;
  2411.                 Edit->Undo.DeletedValidFlag = False;
  2412.                 Edit->Undo.ReplacingValidFlag = False;
  2413.             }
  2414.         if (Edit->Undo.ReplacingValidFlag)
  2415.             {
  2416.                 /* valid replacement, but is this deletion part of it? */
  2417.                 /* if it is, then the current insertion point will be on the line above */
  2418.                 /* the last insertion line; the last insertion char will be 0. */
  2419.                 if ((Edit->Undo.ReplacingEndLine - 1 == GetTextEditSelectStartLine(Edit))
  2420.                     && (Edit->Undo.ReplacingEndCharPlusOne == 0))
  2421.                     {
  2422.                         /* yup, just roll back the insertion range */
  2423.                         /* but we have to make sure we aren't deleting past the beginning */
  2424.                         /* of the insertion range. */
  2425.                         Edit->Undo.ReplacingEndLine -= 1;
  2426.                         Edit->Undo.ReplacingEndCharPlusOne = GetTextEditSelectStartChar(Edit);
  2427.                         if ((Edit->Undo.ReplacingEndLine < Edit->Undo.ReplacingStartLine)
  2428.                             || ((Edit->Undo.ReplacingEndLine == Edit->Undo.ReplacingStartLine)
  2429.                             && (Edit->Undo.ReplacingEndCharPlusOne
  2430.                             < Edit->Undo.ReplacingStartChar)))
  2431.                             {
  2432.                                 /* we just deleted a character not in the range.  Start a new */
  2433.                                 /* deletion */
  2434.                                 goto DeletingSomewhereElsePoint;
  2435.                             }
  2436.                     }
  2437.                  else
  2438.                     {
  2439.                         TextStorageRec*                NewDeleter;
  2440.  
  2441.                         /* otherwise, there was an insertion, but this isn't part of it, which */
  2442.                         /* means the user moved the cursor before deleting.  Thus we start a */
  2443.                         /* new deletion */
  2444.                      DeletingSomewhereElsePoint:
  2445.                         TextEditPurgeUndoRecord(Edit);
  2446.                         Edit->Undo.CanUndoFlag = True;
  2447.                         Edit->Undo.DeletedValidFlag = True;
  2448.                         Edit->Undo.ReplacingValidFlag = False;
  2449.                         NewDeleter = NewTextStorage();
  2450.                         if (NewDeleter == NIL)
  2451.                             {
  2452.                                 /* woah, major error -- user just won't be able to undo! */
  2453.                                 TextEditPurgeUndoRecord(Edit);
  2454.                                 return;
  2455.                             }
  2456.                         Edit->Undo.DeletedStuff = NewDeleter;
  2457.                         /* deleter should contain a new line */
  2458.                         TextStorageInsertLine(NewDeleter,0);
  2459.                         Edit->Undo.DeletedLine = GetTextEditSelectStartLine(Edit);
  2460.                         Edit->Undo.DeletedChar = GetTextEditSelectStartChar(Edit);
  2461.                     }
  2462.             }
  2463.          else
  2464.             {
  2465.                 /* there is no insertion, but is there a deletion? */
  2466.                 if (Edit->Undo.DeletedValidFlag)
  2467.                     {
  2468.                         /* there's a deletion, but was this part of it? */
  2469.                         /* to be part of it, the cursor has to be on the line before */
  2470.                         /* the current deletion line and the deletion char has to be 0 */
  2471.                         if ((Edit->Undo.DeletedLine - 1 == GetTextEditSelectStartLine(Edit))
  2472.                             && (Edit->Undo.DeletedChar == 0))
  2473.                             {
  2474.                                 /* yup.  Just insert a blank line at the beginning of the */
  2475.                                 /* deletion thing and adjust the point.  If a memory out error */
  2476.                                 /* occurs, the data will be mangled -- too bad for the user */
  2477.                                 TextStorageInsertLine(Edit->Undo.DeletedStuff,0);
  2478.                                 Edit->Undo.DeletedLine -= 1;
  2479.                                 Edit->Undo.DeletedChar = GetTextEditSelectStartChar(Edit);
  2480.                             }
  2481.                          else
  2482.                             {
  2483.                                 /* nope, we are deleting somewhere else */
  2484.                                 goto DeletingSomewhereElsePoint;
  2485.                             }
  2486.                     }
  2487.                  else
  2488.                     {
  2489.                         /* no insertion and no deletion?  Well, let's just start one */
  2490.                         /* on our own. */
  2491.                         goto DeletingSomewhereElsePoint;
  2492.                     }
  2493.             }
  2494.     }
  2495.  
  2496.  
  2497. /* this routine is called when the user has pressed delete and deleted */
  2498. /* a normal character.  If this deletion is a logical extention of the existing */
  2499. /* deleted range, AND there is no insertion, then adjust the deleted range. */
  2500. /* if there is an insertion AND the deletion is of the last character of */
  2501. /* the insertion, then adjust the insertion.  If it is neither, purge the */
  2502. /* undo information and start a new deletion record. */
  2503. static void                TextEditRememberUndoDeletedChar(TextEditRec* Edit, char What)
  2504.     {
  2505.         CheckPtrExistence(Edit);
  2506.         ERROR(TextEditIsThereValidSelection(Edit),PRERR(AllowResume,
  2507.             "TextEditRememberUndoDeletedChar:  Why is there a valid selection?"));
  2508.         if (!Edit->Undo.CanUndoFlag)
  2509.             {
  2510.                 Edit->Undo.CanUndoFlag = True;
  2511.                 Edit->Undo.DeletedValidFlag = False;
  2512.                 Edit->Undo.ReplacingValidFlag = False;
  2513.             }
  2514.         if (Edit->Undo.ReplacingValidFlag)
  2515.             {
  2516.                 /* valid replacement, but is this deletion part of it? */
  2517.                 /* if it is, then the current insertion point will be on the same line */
  2518.                 /* as the last insertion line, and the insertion char will be the last */
  2519.                 /* insertion char - 1 */
  2520.                 if ((Edit->Undo.ReplacingEndLine == GetTextEditSelectStartLine(Edit))
  2521.                     && (Edit->Undo.ReplacingEndCharPlusOne - 1
  2522.                     == GetTextEditSelectStartChar(Edit)))
  2523.                     {
  2524.                         /* yup, just roll back the insertion range */
  2525.                         Edit->Undo.ReplacingEndCharPlusOne -= 1;
  2526.                         if ((Edit->Undo.ReplacingEndLine < Edit->Undo.ReplacingStartLine)
  2527.                             || ((Edit->Undo.ReplacingEndLine == Edit->Undo.ReplacingStartLine)
  2528.                             && (Edit->Undo.ReplacingEndCharPlusOne
  2529.                             < Edit->Undo.ReplacingStartChar)))
  2530.                             {
  2531.                                 /* we just deleted a character not in the range.  Start a new */
  2532.                                 /* deletion */
  2533.                                 goto DeletingSomewhereElsePoint;
  2534.                             }
  2535.                     }
  2536.                  else
  2537.                     {
  2538.                         TextStorageRec*        NewDeleter;
  2539.                         char*                            Line;
  2540.                         char*                            LineCopy;
  2541.                         char                            Buffer[1];
  2542.  
  2543.                         /* otherwise, there was an insertion, but this isn't part of it, which */
  2544.                         /* means the user moved the cursor before deleting.  Thus we start a */
  2545.                         /* new deletion */
  2546.                      DeletingSomewhereElsePoint:
  2547.                         TextEditPurgeUndoRecord(Edit);
  2548.                         Edit->Undo.CanUndoFlag = True;
  2549.                         Edit->Undo.DeletedValidFlag = True;
  2550.                         Edit->Undo.ReplacingValidFlag = False;
  2551.                         NewDeleter = NewTextStorage();
  2552.                         if (NewDeleter == NIL)
  2553.                             {
  2554.                                 /* woah, major error -- user just won't be able to undo! */
  2555.                                 TextEditPurgeUndoRecord(Edit);
  2556.                                 return;
  2557.                             }
  2558.                         Edit->Undo.DeletedStuff = NewDeleter;
  2559.                         /* deleter should contain the character */
  2560.                         Line = TextStorageGetLineCopy(Edit->Undo.DeletedStuff,0);
  2561.                         if (Line != NIL)
  2562.                             {
  2563.                                 Buffer[0] = What;
  2564.                                 LineCopy = InsertBlockIntoBlockCopy(Line,Buffer,0,1);
  2565.                                 if (LineCopy != NIL)
  2566.                                     {
  2567.                                         TextStorageChangeLine(Edit->Undo.DeletedStuff,0,LineCopy);
  2568.                                         ReleasePtr(LineCopy);
  2569.                                     }
  2570.                                 ReleasePtr(Line);
  2571.                             }
  2572.                         Edit->Undo.DeletedLine = GetTextEditSelectStartLine(Edit);
  2573.                         Edit->Undo.DeletedChar = GetTextEditSelectStartChar(Edit);
  2574.                     }
  2575.             }
  2576.          else
  2577.             {
  2578.                 /* there is no insertion, but is there a deletion? */
  2579.                 if (Edit->Undo.DeletedValidFlag)
  2580.                     {
  2581.                         /* there's a deletion, but was this part of it? */
  2582.                         /* to be part of it, the cursor has to be on the same line as */
  2583.                         /* the current deletion line and the deletion char has to be */
  2584.                         /* the cursor char + 1 */
  2585.                         if ((Edit->Undo.DeletedLine == GetTextEditSelectStartLine(Edit))
  2586.                             && (Edit->Undo.DeletedChar - 1 == GetTextEditSelectStartChar(Edit)))
  2587.                             {
  2588.                                 char*                    Line;
  2589.                                 char*                    LineCopy;
  2590.                                 char                    Buffer[1];
  2591.  
  2592.                                 /* yup.  Just insert the character at the beginning of the */
  2593.                                 /* deletion thing and adjust the point. */
  2594.                                 Line = TextStorageGetLineCopy(Edit->Undo.DeletedStuff,0);
  2595.                                 if (Line != NIL)
  2596.                                     {
  2597.                                         Buffer[0] = What;
  2598.                                         LineCopy = InsertBlockIntoBlockCopy(Line,Buffer,0,1);
  2599.                                         if (LineCopy != NIL)
  2600.                                             {
  2601.                                                 TextStorageChangeLine(Edit->Undo.DeletedStuff,0,LineCopy);
  2602.                                                 ReleasePtr(LineCopy);
  2603.                                             }
  2604.                                         ReleasePtr(Line);
  2605.                                     }
  2606.                                 Edit->Undo.DeletedChar -= 1;
  2607.                             }
  2608.                          else
  2609.                             {
  2610.                                 /* nope, we are deleting somewhere else */
  2611.                                 goto DeletingSomewhereElsePoint;
  2612.                             }
  2613.                     }
  2614.                  else
  2615.                     {
  2616.                         /* no insertion and no deletion?  Well, let's just start one */
  2617.                         /* on our own. */
  2618.                         goto DeletingSomewhereElsePoint;
  2619.                     }
  2620.             }
  2621.     }
  2622.  
  2623.  
  2624. #define OpenParen '('
  2625. #define CloseParen ')'
  2626. #define OpenBrace '{'
  2627. #define CloseBrace '}'
  2628. #define OpenBracket '['
  2629. #define CloseBracket ']'
  2630. #define MaxStackSize (1024)
  2631.  
  2632.  
  2633. /* extend the current selection to show balanced parentheses, or beep if */
  2634. /* the parentheses are not balanced */
  2635. void                            TextEditBalanceParens(TextEditRec* Edit)
  2636.     {
  2637.         char                    Form;
  2638.         char                    Stack[MaxStackSize];
  2639.         long                    StackIndex;
  2640.         long                    BackLine;
  2641.         long                    BackChar;
  2642.         long                    ForwardLine;
  2643.         long                    ForwardChar;
  2644.         char*                    Line;
  2645.  
  2646.         CheckPtrExistence(Edit);
  2647.         BackLine = GetTextEditSelectStartLine(Edit);
  2648.         BackChar = GetTextEditSelectStartChar(Edit);
  2649.         ForwardLine = GetTextEditSelectEndLine(Edit);
  2650.         ForwardChar = GetTextEditSelectEndCharPlusOne(Edit);
  2651.         if ((BackLine == ForwardLine) && (BackChar == ForwardChar))
  2652.             {
  2653.                 /* just an insertion point.  In this case, if it's like this: */
  2654.                 /* (...)|  or like this:  |(...), then the group immediately */
  2655.                 /* next to the insertion point should be selected */
  2656.                 Line = GetTextEditLine(Edit,BackLine);
  2657.                 if (Line == NIL)
  2658.                     {
  2659.                         return;
  2660.                     }
  2661.                 if (BackChar > 0)
  2662.                     {
  2663.                         if ((Line[BackChar - 1] == CloseParen)
  2664.                             || (Line[BackChar - 1] == CloseBrace)
  2665.                             || (Line[BackChar - 1] == CloseBracket))
  2666.                             {
  2667.                                 /* move insertion point left */
  2668.                                 BackChar -= 1;
  2669.                                 ForwardChar -= 1;
  2670.                                 goto InitialSetupSkipOutPoint;
  2671.                             }
  2672.                     }
  2673.                 if (BackChar < PtrSize(Line)/*no -1*/)
  2674.                     {
  2675.                         /* notice we don't use BackChar + 1 here, because the insertion point */
  2676.                         /* is BETWEEN two characters (the x-1 and the x character) */
  2677.                         if ((Line[BackChar] == OpenParen)
  2678.                             || (Line[BackChar] == OpenBrace)
  2679.                             || (Line[BackChar] == OpenBracket))
  2680.                             {
  2681.                                 /* move insertion point right */
  2682.                                 BackChar += 1;
  2683.                                 ForwardChar += 1;
  2684.                                 goto InitialSetupSkipOutPoint;
  2685.                                 /* BackChar and ForwardChar could be equal to PtrSize(Line) */
  2686.                                 /* after this. */
  2687.                             }
  2688.                     }
  2689.                 /* jump here when the insertion point has been adjusted */
  2690.              InitialSetupSkipOutPoint:
  2691.                 ReleasePtr(Line);
  2692.             }
  2693.         StackIndex = 0;
  2694.         while (BackLine >= 0)
  2695.             {
  2696.                 Line = GetTextEditLine(Edit,BackLine);
  2697.                 if (Line == NIL)
  2698.                     {
  2699.                         return;
  2700.                     }
  2701.                 while (BackChar > 0)
  2702.                     {
  2703.                         char                    C;
  2704.  
  2705.                         BackChar -= 1;
  2706.                         PRNGCHK(Line,&(Line[BackChar]),sizeof(char));
  2707.                         C = Line[BackChar];
  2708.                         if ((C == CloseParen) || (C == CloseBrace) || (C == CloseBracket))
  2709.                             {
  2710.                                 /* we ran into the trailing end of a grouping, so we increment */
  2711.                                 /* the count and look for the beginning end. */
  2712.                                 Stack[StackIndex] = C;
  2713.                                 StackIndex += 1;
  2714.                                 if (StackIndex >= MaxStackSize)
  2715.                                     {
  2716.                                         /* expression is too complex to be analyzed */
  2717.                                         ErrorBeep();
  2718.                                         ReleasePtr(Line);
  2719.                                         return;
  2720.                                     }
  2721.                             }
  2722.                         else if ((C == OpenParen) || (C == OpenBrace) || (C == OpenBracket))
  2723.                             {
  2724.                                 /* here we found a beginning end of some sort.  If it's the */
  2725.                                 /* beginning of a group we aren't in, then check to see that */
  2726.                                 /* it matches */
  2727.                                 if (StackIndex == 0)
  2728.                                     {
  2729.                                         /* there are no other blocks we had to go through so this */
  2730.                                         /* begin must enclose us */
  2731.                                         Form = C;
  2732.                                         ReleasePtr(Line);
  2733.                                         goto ForwardScanEntryPoint;
  2734.                                     }
  2735.                                 StackIndex -= 1;
  2736.                                 if (((C == OpenParen) && (Stack[StackIndex] == CloseParen))
  2737.                                     || ((C == OpenBrace) && (Stack[StackIndex] == CloseBrace))
  2738.                                     || ((C == OpenBracket) && (Stack[StackIndex] == CloseBracket)))
  2739.                                     {
  2740.                                         /* good */
  2741.                                     }
  2742.                                  else
  2743.                                     {
  2744.                                         /* bad */
  2745.                                         ReleasePtr(Line);
  2746.                                         ErrorBeep();
  2747.                                         return;
  2748.                                     }
  2749.                             }
  2750.                     }
  2751.                 BackLine -= 1;
  2752.                 ReleasePtr(Line);
  2753.                 if (BackLine >= 0)
  2754.                     {
  2755.                         Line = GetTextEditLine(Edit,BackLine);
  2756.                         BackChar = PtrSize(Line);
  2757.                         ReleasePtr(Line);
  2758.                     }
  2759.             }
  2760.         ErrorBeep();
  2761.         return;
  2762.      ForwardScanEntryPoint:
  2763.         StackIndex = 0;
  2764.         while (ForwardLine < GetTextEditNumLines(Edit))
  2765.             {
  2766.                 Line = GetTextEditLine(Edit,ForwardLine);
  2767.                 if (Line == NIL)
  2768.                     {
  2769.                         return;
  2770.                     }
  2771.                 while (ForwardChar < PtrSize(Line))
  2772.                     {
  2773.                         char                    C;
  2774.  
  2775.                         PRNGCHK(Line,&(Line[ForwardChar]),sizeof(char));
  2776.                         C = Line[ForwardChar];
  2777.                         ForwardChar += 1;
  2778.                         if ((C == OpenParen) || (C == OpenBrace) || (C == OpenBracket))
  2779.                             {
  2780.                                 /* we ran into the leading end of a grouping, so we increment */
  2781.                                 /* the count and look for the end end. */
  2782.                                 Stack[StackIndex] = C;
  2783.                                 StackIndex += 1;
  2784.                                 if (StackIndex >= MaxStackSize)
  2785.                                     {
  2786.                                         /* expression is too complex to be analyzed */
  2787.                                         ErrorBeep();
  2788.                                         ReleasePtr(Line);
  2789.                                         return;
  2790.                                     }
  2791.                             }
  2792.                         else if ((C == CloseParen) || (C == CloseBrace) || (C == CloseBracket))
  2793.                             {
  2794.                                 /* here we found an end of some sort.  If it's the */
  2795.                                 /* end of a group we aren't in, then check to see that */
  2796.                                 /* it matches */
  2797.                                 if (StackIndex == 0)
  2798.                                     {
  2799.                                         /* there are no other blocks we had to go through so this */
  2800.                                         /* end must enclose us */
  2801.                                         ReleasePtr(Line);
  2802.                                         if (((Form == OpenParen) && (C == CloseParen))
  2803.                                             || ((Form == OpenBrace) && (C == CloseBrace))
  2804.                                             || ((Form == OpenBracket) && (C == CloseBracket)))
  2805.                                             {
  2806.                                                 SetTextEditSelection(Edit,BackLine,BackChar,
  2807.                                                     ForwardLine,ForwardChar);
  2808.                                                 TextEditShowSelection(Edit);
  2809.                                                 return;
  2810.                                             }
  2811.                                          else
  2812.                                             {
  2813.                                                 ErrorBeep();
  2814.                                                 return;
  2815.                                             }
  2816.                                     }
  2817.                                 StackIndex -= 1;
  2818.                                 if (((C == CloseParen) && (Stack[StackIndex] == OpenParen))
  2819.                                     || ((C == CloseBrace) && (Stack[StackIndex] == OpenBrace))
  2820.                                     || ((C == CloseBracket) && (Stack[StackIndex] == OpenBracket)))
  2821.                                     {
  2822.                                         /* good */
  2823.                                     }
  2824.                                  else
  2825.                                     {
  2826.                                         /* bad */
  2827.                                         ReleasePtr(Line);
  2828.                                         ErrorBeep();
  2829.                                         return;
  2830.                                     }
  2831.                             }
  2832.                     }
  2833.                 ForwardLine += 1;
  2834.                 ReleasePtr(Line);
  2835.                 ForwardChar = 0;
  2836.             }
  2837.         ErrorBeep();
  2838.         return;
  2839.     }
  2840.  
  2841.  
  2842. /* find the specified search string starting at the current selection. */
  2843. MyBoolean                    TextEditFindAgain(TextEditRec* Edit, char* SearchString)
  2844.     {
  2845.         long                        LineScan;
  2846.         long                        KeyLength;
  2847.         long                        LineLimit;
  2848.         long                        ColumnScan;
  2849.         long                        ElapsedLineCount;
  2850.  
  2851.         CheckPtrExistence(Edit);
  2852.         CheckPtrExistence(SearchString);
  2853.         KeyLength = PtrSize(SearchString);
  2854.         LineLimit = GetTextEditNumLines(Edit);
  2855.         LineScan = GetTextEditSelectStartLine(Edit);
  2856.         ElapsedLineCount = 0;
  2857.         if (TextEditIsThereValidSelection(Edit))
  2858.             {
  2859.                 /* if there is a selection, assume it's from a previous search.  we need */
  2860.                 /* to have + 1 so we don't find what we found again. */
  2861.                 ColumnScan = GetTextEditSelectStartChar(Edit) + 1;
  2862.             }
  2863.          else
  2864.             {
  2865.                 /* if no selection, start search at current position.  this lets us find */
  2866.                 /* patterns at the very beginning of the file. */
  2867.                 ColumnScan = GetTextEditSelectStartChar(Edit);
  2868.             }
  2869.         while (LineScan < LineLimit)
  2870.             {
  2871.                 char*                        TestLine;
  2872.                 long                        ColumnLimit;
  2873.  
  2874.                 TestLine = GetTextEditLine(Edit,LineScan);
  2875.                 if (TestLine == NIL)
  2876.                     {
  2877.                         return False;
  2878.                     }
  2879.                 ColumnLimit = PtrSize(TestLine) - KeyLength;
  2880.                 while (ColumnScan <= ColumnLimit)
  2881.                     {
  2882.                         if (MemEquNoCase(&(SearchString[0]),&(TestLine[ColumnScan]),KeyLength))
  2883.                             {
  2884.                                 /* found it! */
  2885.                                 SetTextEditSelection(Edit,LineScan,ColumnScan,
  2886.                                     LineScan,ColumnScan + KeyLength);
  2887.                                 TextEditShowSelection(Edit);
  2888.                                 ReleasePtr(TestLine);
  2889.                                 return True;
  2890.                             }
  2891.                         ColumnScan += 1;
  2892.                     }
  2893.                 ReleasePtr(TestLine);
  2894.                 LineScan += 1;
  2895.                 ElapsedLineCount += 1;
  2896.                 ColumnScan = 0;
  2897.                 if (RelinquishCPUJudiciouslyCheckCancel())
  2898.                     {
  2899.                         /* cancelling */
  2900.                         return False;
  2901.                     }
  2902.             }
  2903.         ErrorBeep(); /* selection not found */
  2904.         return False;
  2905.     }
  2906.  
  2907.  
  2908. /* see if the specified location is in the text edit box */
  2909. MyBoolean                    TextEditHitTest(TextEditRec* Edit, OrdType X, OrdType Y)
  2910.     {
  2911.         CheckPtrExistence(Edit);
  2912.         return (X >= Edit->X) && (Y >= Edit->Y) && (X < Edit->X + Edit->TotalWidth)
  2913.             && (Y < Edit->Y + Edit->TotalHeight);
  2914.     }
  2915.  
  2916.  
  2917. /* see if the specified location is in the text edit area of the box (not the */
  2918. /* scrollbars).  This is used for deciding whether the mouse should be an Ibeam */
  2919. /* or an arrow. */
  2920. MyBoolean                    TextEditIBeamTest(TextEditRec* Edit, OrdType X, OrdType Y)
  2921.     {
  2922.         CheckPtrExistence(Edit);
  2923.         return (X >= Edit->X) && (Y >= Edit->Y)
  2924.             && (X < Edit->X + GetTextViewWidth(Edit->View))
  2925.             && (Y < Edit->Y + GetTextViewHeight(Edit->View));
  2926.     }
  2927.