home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / Level 1 Extensions 29Sep94 / TextEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  86.3 KB  |  2,889 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. /* find the union of two selection ranges */
  664. void                            UnionSelection(SelRec One, SelRec Two, SelRec Three,
  665.                                         SelRec* Start, SelRec* End)
  666.     {
  667.         SortSelection(&One,&Three);
  668.         SortSelection(&One,&Two);
  669.         SortSelection(&Two,&Three);
  670.         *Start = One;
  671.         *End = Three;
  672.     }
  673.  
  674.  
  675. /* find the difference (union - intersection) of two selection ranges.  This is */
  676. /* used to avoid redrawing the entire selection range all the time */
  677. void                            DiffSelection(SelRec OneStart, SelRec OneEnd, SelRec TwoStart,
  678.                                         SelRec TwoEnd, SelRec* OutStart, SelRec* OutEnd)
  679.     {
  680.         SortSelection(&OneStart,&OneEnd);
  681.         SortSelection(&TwoStart,&TwoEnd);
  682.         if (GreaterThan(&OneStart,&TwoStart))
  683.             {
  684.                 *OutStart = TwoStart;
  685.                 *OutEnd = OneStart;
  686.                 if (GreaterThan(&OneEnd,&TwoEnd))
  687.                     {
  688.                         *OutEnd = OneEnd;
  689.                         return;
  690.                     }
  691.                 if (GreaterThan(&TwoEnd,&OneEnd))
  692.                     {
  693.                         *OutEnd = TwoEnd;
  694.                         return;
  695.                     }
  696.                 return;
  697.             }
  698.         if (GreaterThan(&TwoStart,&OneStart))
  699.             {
  700.                 *OutStart = OneStart;
  701.                 *OutEnd = TwoStart;
  702.                 if (GreaterThan(&OneEnd,&TwoEnd))
  703.                     {
  704.                         *OutEnd = OneEnd;
  705.                         return;
  706.                     }
  707.                 if (GreaterThan(&TwoEnd,&OneEnd))
  708.                     {
  709.                         *OutEnd = TwoEnd;
  710.                         return;
  711.                     }
  712.                 return;
  713.             }
  714.         if (GreaterThan(&OneEnd,&TwoEnd))
  715.             {
  716.                 *OutStart = TwoEnd;
  717.                 *OutEnd = OneEnd;
  718.                 return;
  719.             }
  720.         if (GreaterThan(&TwoEnd,&OneEnd))
  721.             {
  722.                 *OutStart = OneEnd;
  723.                 *OutEnd = TwoEnd;
  724.                 return;
  725.             }
  726.         *OutStart = OneStart;
  727.         *OutEnd = OneStart;
  728.     }
  729.  
  730.  
  731. /* if the first selection point is after the second then reverse their order */
  732. void                            SortSelection(SelRec* One, SelRec* Two)
  733.     {
  734.         SelRec            Temp;
  735.  
  736.         if (GreaterThan(One,Two))
  737.             {
  738.                 Temp = *One;
  739.                 *One = *Two;
  740.                 *Two = Temp;
  741.             }
  742.     }
  743.  
  744.  
  745. /* returns True if the first selection point is after the second one */
  746. MyBoolean                    GreaterThan(SelRec* One, SelRec* Two)
  747.     {
  748.         if (One->Line == Two->Line)
  749.             {
  750.                 if (One->Column > Two->Column)
  751.                     {
  752.                         return True;
  753.                     }
  754.             }
  755.          else
  756.             {
  757.                 if (One->Line > Two->Line)
  758.                     {
  759.                         return True;
  760.                     }
  761.             }
  762.         return False;
  763.     }
  764.  
  765.  
  766. /* extend the selection using the current mouse-click state (single, double, triple) */
  767. void                            ExtendSelection(TextEditRec* Edit, SelRec* Start, SelRec* End)
  768.     {
  769.         char*                Buffer;
  770.         long                LineLength;
  771.  
  772.         CheckPtrExistence(Edit);
  773.         switch (Edit->ClickPhase)
  774.             {
  775.                 case eNoClick:
  776.                     break;
  777.                 case eSingleClick:
  778.                     break;
  779.                 case eDoubleClick:
  780.                     if ((Start != NIL) && (Start->Line >= 0)
  781.                         && (Start->Line < GetTextEditNumLines(Edit)))
  782.                         {
  783.                             Buffer = GetTextEditLine(Edit,Start->Line);
  784.                             if (Buffer != NIL)
  785.                                 {
  786.                                     LineLength = PtrSize(Buffer);
  787.                                     if (AlphaNum(Buffer[Start->Column]))
  788.                                         {
  789.                                             while ((Start->Column > 0)
  790.                                                 && AlphaNum(Buffer[Start->Column - 1]))
  791.                                                 {
  792.                                                     Start->Column -= 1;
  793.                                                 }
  794.                                         }
  795.                                     ReleasePtr(Buffer);
  796.                                 }
  797.                         }
  798.                     if ((End != NIL) && (End->Line >= 0)
  799.                         && (End->Line < GetTextEditNumLines(Edit)))
  800.                         {
  801.                             Buffer = GetTextEditLine(Edit,End->Line);
  802.                             if (Buffer != NIL)
  803.                                 {
  804.                                     LineLength = PtrSize(Buffer);
  805.                                     while ((End->Column < LineLength)
  806.                                         && AlphaNum(Buffer[End->Column]))
  807.                                         {
  808.                                             End->Column += 1;
  809.                                         }
  810.                                     ReleasePtr(Buffer);
  811.                                 }
  812.                         }
  813.                     break;
  814.                 case eTripleClick:
  815.                     if (Start != NIL)
  816.                         {
  817.                             Start->Column = 0;
  818.                         }
  819.                     if ((End != NIL) && (End->Column != 0))
  820.                         {
  821.                             End->Column = 0;
  822.                             End->Line += 1;
  823.                         }
  824.                     break;
  825.             }
  826.     }
  827.  
  828.  
  829. /* find out if the character is an alphanumeric character.  this is used by */
  830. /* ExtendSelection for figuring out where double-click extends should stop. */
  831. MyBoolean                    AlphaNum(char It)
  832.     {
  833.         return ((It >= 'a') && (It <= 'z')) || ((It >= 'A') && (It <= 'Z'))
  834.             || ((It >= '0') && (It <= '9')) || (It == '_');
  835.     }
  836.  
  837.  
  838. /* append a line of text to the end of the text edit.  This can be used if the */
  839. /* text edit box is being used as an interaction (terminal) window */
  840. /* if NIL is passed in for Data, a blank line will be appended */
  841. MyBoolean                    TextEditAppendLineInteraction(TextEditRec* Edit, char* Data)
  842.     {
  843.         CheckPtrExistence(Edit);
  844.         TextEditPurgeUndoRecord(Edit); /* this routine modifies data, so remove undo info */
  845.         SetTextEditInsertionPoint(Edit,GetTextViewNumLines(Edit->View) - 1,
  846.             GetTextViewLineLength(Edit->View,GetTextViewNumLines(Edit->View) - 1));
  847.         if (!TextViewBreakLine(Edit->View,GetTextEditSelectStartLine(Edit),
  848.             GetTextEditSelectStartChar(Edit)))
  849.             {
  850.                 return False;
  851.             }
  852.         SetTextEditInsertionPoint(Edit,GetTextEditSelectStartLine(Edit) + 1,0);
  853.         if (Data == NIL)
  854.             {
  855.                 return True;
  856.             }
  857.         return SetTextViewLine(Edit->View,GetTextEditSelectStartLine(Edit),Data);
  858.     }
  859.  
  860.  
  861. /* dump the data contained in the text edit to the current position in the */
  862. /* specified file.  returns True if all the data was written successfully */
  863. MyBoolean                    TestEditWriteDataToFile(TextEditRec* Edit,
  864.                                         struct FileType* FileRefNum, char* EOLN)
  865.     {
  866.         CheckPtrExistence(Edit);
  867.         return TextViewWriteDataToFile(Edit->View,FileRefNum,EOLN);
  868.     }
  869.  
  870.  
  871. /* cut the selected data to the clipboard.  if this fails, some of the data */
  872. /* may have been deleted */
  873. MyBoolean                    TextEditDoMenuCut(TextEditRec* Edit)
  874.     {
  875.         TextStorageRec*            Removed;
  876.         TextStorageRec*            UndoInfo;
  877.         char*                                RawBlock;
  878.         MyBoolean                        Flag;
  879.  
  880.         CheckPtrExistence(Edit);
  881.         Removed = TextViewGetSelection(Edit->View);
  882.         if (Removed == NIL)
  883.             {
  884.              FailurePoint1:
  885.                 return False;
  886.             }
  887.         UndoInfo = TextViewGetSelection(Edit->View);
  888.         if (UndoInfo == NIL)
  889.             {
  890.              FailurePoint2:
  891.                 DisposeTextStorage(Removed);
  892.                 goto FailurePoint1;
  893.             }
  894.         RawBlock = TextStorageMakeRawBuffer(Removed,SYSTEMLINEFEED);
  895.         if (RawBlock == NIL)
  896.             {
  897.              FailurePoint3:
  898.                 DisposeTextStorage(UndoInfo);
  899.                 goto FailurePoint2;
  900.             }
  901.         Flag = SetScrapToThis(RawBlock);
  902.         ReleasePtr(RawBlock);
  903.         if (!Flag)
  904.             {
  905.                 goto FailurePoint3;
  906.             }
  907.         DisposeTextStorage(Removed);
  908.  
  909.         if (!TextViewDeleteSelection(Edit->View))
  910.             {
  911.                 goto FailurePoint3;
  912.             }
  913.         TextEditBlockRemovedUndoSave(Edit,UndoInfo,GetTextEditSelectStartLine(Edit),
  914.             GetTextEditSelectStartChar(Edit));
  915.         TextEditRecalcVerticalScroll(Edit);
  916.         TextEditRecalcHorizontalScroll(Edit);
  917.         TextEditShowSelection(Edit);
  918.         return True;
  919.     }
  920.  
  921.  
  922. /* copy the selected data to the clipboard */
  923. MyBoolean                    TextEditDoMenuCopy(TextEditRec* Edit)
  924.     {
  925.         TextStorageRec*    Removed;
  926.         char*                        RawBlock;
  927.         MyBoolean                Flag;
  928.  
  929.         CheckPtrExistence(Edit);
  930.         Removed = TextViewGetSelection(Edit->View);
  931.         if (Removed == NIL)
  932.             {
  933.                 return False;
  934.             }
  935.         RawBlock = TextStorageMakeRawBuffer(Removed,SYSTEMLINEFEED);
  936.         DisposeTextStorage(Removed);
  937.         if (RawBlock == NIL)
  938.             {
  939.                 return False;
  940.             }
  941.         Flag = SetScrapToThis(RawBlock);
  942.         ReleasePtr(RawBlock);
  943.         if (!Flag)
  944.             {
  945.                 return False;
  946.             }
  947.         return True;
  948.     }
  949.  
  950.  
  951. /* paste the clipboard in, replacing the current selection if there is one */
  952. /* if this fails, some of the data may have been inserted */
  953. MyBoolean                    TextEditDoMenuPaste(TextEditRec* Edit)
  954.     {
  955.         char*                        RawBlock;
  956.         TextStorageRec*    ToBeInserted;
  957.  
  958.         CheckPtrExistence(Edit);
  959.         RawBlock = GetCopyOfScrap();
  960.         if (RawBlock == NIL)
  961.             {
  962.              FailurePoint1:
  963.                 return False;
  964.             }
  965.         ToBeInserted = TextStorageFromRawBuffer(RawBlock,SYSTEMLINEFEED);
  966.         ReleasePtr(RawBlock);
  967.         if (ToBeInserted == NIL)
  968.             {
  969.                 goto FailurePoint1;
  970.             }
  971.         if (!TextEditDoMenuClear(Edit)) /* erase old selection & save it in Undo record */
  972.             {
  973.              FailurePoint2:
  974.                 DisposeTextStorage(ToBeInserted);
  975.                 goto FailurePoint1;
  976.             }
  977.         Edit->Undo.ReplacingValidFlag = True;
  978.         Edit->Undo.ReplacingStartLine = GetTextEditSelectStartLine(Edit);
  979.         Edit->Undo.ReplacingStartChar = GetTextEditSelectStartChar(Edit);
  980.         if (!TextViewInsertBlock(Edit->View,ToBeInserted))
  981.             {
  982.                 goto FailurePoint2;
  983.             }
  984.         Edit->Undo.ReplacingEndLine = GetTextEditSelectEndLine(Edit);
  985.         Edit->Undo.ReplacingEndCharPlusOne = GetTextEditSelectEndCharPlusOne(Edit);
  986.         DisposeTextStorage(ToBeInserted);
  987.         TextEditRecalcVerticalScroll(Edit);
  988.         TextEditRecalcHorizontalScroll(Edit);
  989.         TextEditShowSelection(Edit);
  990.         return True;
  991.     }
  992.  
  993.  
  994. /* select the entire data area of the text edit */
  995. void                            TextEditDoMenuSelectAll(TextEditRec* Edit)
  996.     {
  997.         CheckPtrExistence(Edit);
  998.         SetTextViewSelection(Edit->View,0,0,GetTextViewNumLines(Edit->View) - 1,
  999.             GetTextViewLineLength(Edit->View,GetTextViewNumLines(Edit->View) - 1));
  1000.     }
  1001.  
  1002.  
  1003. /* delete the selected area.  This is the same as pressing the delete key */
  1004. /* when there is a valid selection.  if this fails, some of the data may */
  1005. /* have been deleted */
  1006. MyBoolean                    TextEditDoMenuClear(TextEditRec* Edit)
  1007.     {
  1008.         TextStorageRec*            Deleted;
  1009.  
  1010.         /* this routine is called from several other routines which need to delete */
  1011.         /* a selection before inserting.  Therefore, we can do the delete-save part of */
  1012.         /* the 'undo' info construction in this routine. */
  1013.         CheckPtrExistence(Edit);
  1014.         Deleted = TextViewGetSelection(Edit->View);
  1015.         if (Deleted == NIL)
  1016.             {
  1017.              FailurePoint1:
  1018.                 return False;
  1019.             }
  1020.         if (!TextViewDeleteSelection(Edit->View))
  1021.             {
  1022.                 DisposeTextStorage(Deleted);
  1023.                 goto FailurePoint1;
  1024.             }
  1025.         TextEditBlockRemovedUndoSave(Edit,Deleted,GetTextEditSelectStartLine(Edit),
  1026.             GetTextEditSelectStartChar(Edit));
  1027.         TextEditRecalcVerticalScroll(Edit);
  1028.         TextEditRecalcHorizontalScroll(Edit);
  1029.         TextEditShowSelection(Edit);
  1030.         return True;
  1031.     }
  1032.  
  1033.  
  1034. /* shift the selection toward the left margin by deleting one tab (or spaces) */
  1035. /* from the beginning of the line.  It will not remove non-whitespace characters */
  1036. /* if this fails, some of the lines may have been shifted */
  1037. MyBoolean                    TextEditShiftSelectionLeftOneTab(TextEditRec* Edit)
  1038.     {
  1039.         long                    Scan;
  1040.         long                    Limit;
  1041.         long                    SpacesPerTab;
  1042.  
  1043.         CheckPtrExistence(Edit);
  1044.         Limit = GetTextViewSelectEndLine(Edit->View);
  1045.         if (TextEditIsThereValidSelection(Edit)
  1046.             && (GetTextViewSelectEndCharPlusOne(Edit->View) == 0))
  1047.             {
  1048.                 Limit -= 1;
  1049.             }
  1050.         SpacesPerTab = GetTextViewSpacesPerTab(Edit->View);
  1051.         for (Scan = GetTextViewSelectStartLine(Edit->View); Scan <= Limit; Scan += 1)
  1052.             {
  1053.                 char*                    LineCopy;
  1054.                 char*                    Line;
  1055.                 long                    CharScan;
  1056.                 long                    ColumnCount;
  1057.  
  1058.                 Line = GetTextViewLine(Edit->View,Scan);
  1059.                 if (Line == NIL)
  1060.                     {
  1061.                         return False;
  1062.                     }
  1063.                 CharScan = 0;
  1064.                 ColumnCount = 0;
  1065.                 while ((CharScan < PtrSize(Line)) && (ColumnCount < SpacesPerTab))
  1066.                     {
  1067.                         if (Line[CharScan] == ' ')
  1068.                             {
  1069.                                 CharScan += 1;
  1070.                                 ColumnCount += 1;
  1071.                             }
  1072.                         else if (Line[CharScan] == 9)
  1073.                             {
  1074.                                 CharScan += 1;
  1075.                                 ColumnCount = SpacesPerTab; /* cause loop termination */
  1076.                             }
  1077.                         else
  1078.                             {
  1079.                                 ColumnCount = SpacesPerTab; /* cause loop termination */
  1080.                             }
  1081.                     }
  1082.                 LineCopy = RemoveBlockFromBlockCopy(Line,0,CharScan);
  1083.                 ReleasePtr(Line);
  1084.                 if (LineCopy == NIL)
  1085.                     {
  1086.                         return False;
  1087.                     }
  1088.                 SetTextEditLine(Edit,Scan,LineCopy);
  1089.                 ReleasePtr(LineCopy);
  1090.             }
  1091.         if (TextEditIsThereValidSelection(Edit)
  1092.             && (GetTextViewSelectEndCharPlusOne(Edit->View) == 0))
  1093.             {
  1094.                 SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1095.                     0,GetTextViewSelectEndLine(Edit->View),0);
  1096.             }
  1097.          else
  1098.             {
  1099.                 SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1100.                     0,GetTextViewSelectEndLine(Edit->View) + 1,0);
  1101.             }
  1102.         TextEditRecalcHorizontalScroll(Edit);
  1103.         return True;
  1104.     }
  1105.  
  1106.  
  1107. /* shift selection toward the right margin by inserting a tab at the */
  1108. /* beginning of each line.  if this fails, some of the lines may have been shifted. */
  1109. MyBoolean                    TextEditShiftSelectionRightOneTab(TextEditRec* Edit)
  1110.     {
  1111.         long                    Scan;
  1112.         long                    Limit;
  1113.         char                    MyTab[1] = {9};
  1114.  
  1115.         CheckPtrExistence(Edit);
  1116.         Limit = GetTextViewSelectEndLine(Edit->View);
  1117.         if (TextEditIsThereValidSelection(Edit)
  1118.             && (GetTextViewSelectEndCharPlusOne(Edit->View) == 0))
  1119.             {
  1120.                 Limit -= 1;
  1121.             }
  1122.         for (Scan = GetTextViewSelectStartLine(Edit->View); Scan <= Limit; Scan += 1)
  1123.             {
  1124.                 char*                    Line;
  1125.  
  1126.                 Line = GetTextViewLine(Edit->View,Scan);
  1127.                 if (Line == NIL)
  1128.                     {
  1129.                         return False;
  1130.                     }
  1131.                 if (PtrSize(Line) != 0)
  1132.                     {
  1133.                         char*                    LineCopy;
  1134.  
  1135.                         /* we only shift if the line has stuff on it. */
  1136.                         LineCopy = InsertBlockIntoBlockCopy(Line,MyTab,0,1);
  1137.                         if (LineCopy == NIL)
  1138.                             {
  1139.                                 ReleasePtr(Line);
  1140.                                 return False;
  1141.                             }
  1142.                         SetTextEditLine(Edit,Scan,LineCopy);
  1143.                         ReleasePtr(LineCopy);
  1144.                     }
  1145.                 ReleasePtr(Line);
  1146.             }
  1147.         if (TextEditIsThereValidSelection(Edit)
  1148.             && (GetTextViewSelectEndCharPlusOne(Edit->View) == 0))
  1149.             {
  1150.                 SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1151.                     0,GetTextViewSelectEndLine(Edit->View),0);
  1152.             }
  1153.          else
  1154.             {
  1155.                 SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1156.                     0,GetTextViewSelectEndLine(Edit->View) + 1,0);
  1157.             }
  1158.         TextEditRecalcHorizontalScroll(Edit);
  1159.         return True;
  1160.     }
  1161.  
  1162.  
  1163. /* convert all tab characters in the text box to the appropriate number of spaces */
  1164. /* if this fails, some of the lines may have been converted. */
  1165. MyBoolean                    TextEditConvertTabsToSpaces(TextEditRec* Edit)
  1166.     {
  1167.         long                        Scan;
  1168.         long                        Limit;
  1169.         long                        SelectionStartColumn;
  1170.         long                        SelectionEndColumn;
  1171.  
  1172.         CheckPtrExistence(Edit);
  1173.  
  1174.         /* before we do it, we should remember the start and end columns of the insertion */
  1175.         /* point so that we can do this so that the selection screen does not change. */
  1176.         SelectionStartColumn = TextViewCalculateColumnFromCharIndex(Edit->View,
  1177.             GetTextViewSelectStartLine(Edit->View),GetTextViewSelectStartChar(Edit->View));
  1178.         SelectionEndColumn = TextViewCalculateColumnFromCharIndex(Edit->View,
  1179.             GetTextViewSelectEndLine(Edit->View),GetTextViewSelectEndCharPlusOne(Edit->View));
  1180.  
  1181.         /* do the conversion */
  1182.         Limit = GetTextViewNumLines(Edit->View);
  1183.         for (Scan = 0; Scan < Limit; Scan += 1)
  1184.             {
  1185.                 char*                        Line;
  1186.                 MyBoolean                Flag;
  1187.  
  1188.                 Line = GetTextViewSpaceFromTabLine(Edit->View,Scan);
  1189.                 if (Line == NIL)
  1190.                     {
  1191.                         return False;
  1192.                     }
  1193.                 Flag = SetTextEditLine(Edit,Scan,Line);
  1194.                 ReleasePtr(Line);
  1195.                 if (!Flag)
  1196.                     {
  1197.                         return False;
  1198.                     }
  1199.             }
  1200.         TextEditRecalcHorizontalScroll(Edit);
  1201.  
  1202.         /* restore apparent selection */
  1203.         SetTextViewSelection(Edit->View,GetTextViewSelectStartLine(Edit->View),
  1204.             SelectionStartColumn,GetTextViewSelectEndLine(Edit->View),SelectionEndColumn);
  1205.  
  1206.         /* exit successfully */
  1207.         return True;
  1208.     }
  1209.  
  1210.  
  1211. /* show the current selection in the edit window */
  1212. void                            TextEditShowSelection(TextEditRec* Edit)
  1213.     {
  1214.         long            CheckValue;
  1215.  
  1216.         CheckPtrExistence(Edit);
  1217.         /* figure out how much space to leave at bottom and top edges */
  1218.         CheckValue = 4;
  1219.         while ((CheckValue > 0)
  1220.             && (TextViewNumVisibleLines(Edit->View) < CheckValue * 2 + 4))
  1221.             {
  1222.                 CheckValue -= 1;
  1223.             }
  1224.         /* vertical adjustment */
  1225.         if (((GetTextEditSelectStartLine(Edit) >= GetTextEditTopLine(Edit) + CheckValue)
  1226.                 && (GetTextEditSelectStartLine(Edit) < GetTextEditTopLine(Edit)
  1227.                 + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1)))
  1228.             || ((GetTextEditSelectStartLine(Edit) < CheckValue)
  1229.                 && (GetTextEditSelectStartLine(Edit) >= GetTextEditTopLine(Edit))))
  1230.             {
  1231.                 /* beginning of selection is in the box, so try to center the end */
  1232.                 if (GetTextEditSelectEndLine(Edit) < GetTextEditTopLine(Edit) + CheckValue)
  1233.                     {
  1234.                         /* selection is to far up */
  1235.                         SetTextEditTopLine(Edit,GetTextEditSelectEndLine(Edit) - CheckValue);
  1236.                     }
  1237.                 if (GetTextEditSelectEndLine(Edit) >= GetTextEditTopLine(Edit)
  1238.                     + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1))
  1239.                     {
  1240.                         /* selection is too far down */
  1241.                         SetTextEditTopLine(Edit,GetTextEditSelectEndLine(Edit)
  1242.                             - (TextViewNumVisibleLines(Edit->View) - CheckValue - 1) + 1);
  1243.                     }
  1244.             }
  1245.          else
  1246.             {
  1247.                 /* center the beginning in the box */
  1248.                 if (GetTextEditSelectStartLine(Edit) < GetTextEditTopLine(Edit) + CheckValue)
  1249.                     {
  1250.                         /* selection is to far up */
  1251.                         SetTextEditTopLine(Edit,GetTextEditSelectStartLine(Edit) - CheckValue);
  1252.                     }
  1253.                 if (GetTextEditSelectStartLine(Edit) >= GetTextEditTopLine(Edit)
  1254.                     + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1))
  1255.                     {
  1256.                         /* selection is too far down */
  1257.                         SetTextEditTopLine(Edit,GetTextEditSelectStartLine(Edit)
  1258.                             - (TextViewNumVisibleLines(Edit->View) - CheckValue - 1) + 1);
  1259.                     }
  1260.             }
  1261.         /* figure out how much space on left and right edges to leave */
  1262.         CheckValue = 32;
  1263.         while ((CheckValue > 0)
  1264.             && (GetTextViewWidth(Edit->View) < CheckValue * 4 + 16))
  1265.             {
  1266.                 CheckValue -= 1;
  1267.             }
  1268.         /* horizontal adjustment */
  1269.         if (!TextEditIsThereValidSelection(Edit))
  1270.             {
  1271.                 /* only adjust left-to-right if it's an insertion point */
  1272.                 if (TextViewScreenXFromCharIndex(Edit->View,
  1273.                     GetTextEditSelectStartLine(Edit),
  1274.                     GetTextEditSelectStartChar(Edit))
  1275.                     < GetTextEditPixelIndent(Edit) + CheckValue)
  1276.                     {
  1277.                         SetTextEditPixelIndent(Edit,
  1278.                             TextViewScreenXFromCharIndex(Edit->View,
  1279.                             GetTextEditSelectStartLine(Edit),
  1280.                             GetTextEditSelectStartChar(Edit)) - (2 * CheckValue));
  1281.                     }
  1282.                 if (TextViewScreenXFromCharIndex(Edit->View,
  1283.                     GetTextEditSelectStartLine(Edit),
  1284.                     GetTextEditSelectStartChar(Edit))
  1285.                     > GetTextEditPixelIndent(Edit)
  1286.                     + GetTextViewWidth(Edit->View) - CheckValue)
  1287.                     {
  1288.                         SetTextEditPixelIndent(Edit,
  1289.                             TextViewScreenXFromCharIndex(Edit->View,
  1290.                             GetTextEditSelectStartLine(Edit),
  1291.                             GetTextEditSelectStartChar(Edit))
  1292.                             - GetTextViewWidth(Edit->View) + (2 * CheckValue));
  1293.                     }
  1294.             }
  1295.     }
  1296.  
  1297.  
  1298. /* show the starting edge of the selection. */
  1299. void                            TextEditShowSelectionStartEdge(TextEditRec* Edit)
  1300.     {
  1301.         long            CheckValue;
  1302.  
  1303.         CheckPtrExistence(Edit);
  1304.         /* figure out how much space to leave at bottom and top edges */
  1305.         CheckValue = 4;
  1306.         while ((CheckValue > 0)
  1307.             && (TextViewNumVisibleLines(Edit->View) < CheckValue * 2 + 4))
  1308.             {
  1309.                 CheckValue -= 1;
  1310.             }
  1311.         /* vertical adjustment */
  1312.         /* center the beginning in the box */
  1313.         if (GetTextEditSelectStartLine(Edit) < GetTextEditTopLine(Edit) + CheckValue)
  1314.             {
  1315.                 /* selection is to far up */
  1316.                 SetTextEditTopLine(Edit,GetTextEditSelectStartLine(Edit) - CheckValue);
  1317.             }
  1318.         if (GetTextEditSelectStartLine(Edit) >= GetTextEditTopLine(Edit)
  1319.             + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1))
  1320.             {
  1321.                 /* selection is too far down */
  1322.                 SetTextEditTopLine(Edit,GetTextEditSelectStartLine(Edit)
  1323.                     - (TextViewNumVisibleLines(Edit->View) - CheckValue - 1) + 1);
  1324.             }
  1325.         /* figure out how much space on left and right edges to leave */
  1326.         CheckValue = 32;
  1327.         while ((CheckValue > 0)
  1328.             && (GetTextViewWidth(Edit->View) < CheckValue * 4 + 16))
  1329.             {
  1330.                 CheckValue -= 1;
  1331.             }
  1332.         /* horizontal adjustment */
  1333.         if (TextViewScreenXFromCharIndex(Edit->View,
  1334.             GetTextEditSelectStartLine(Edit),
  1335.             GetTextEditSelectStartChar(Edit))
  1336.             < GetTextEditPixelIndent(Edit) + CheckValue)
  1337.             {
  1338.                 SetTextEditPixelIndent(Edit,
  1339.                     TextViewScreenXFromCharIndex(Edit->View,
  1340.                     GetTextEditSelectStartLine(Edit),
  1341.                     GetTextEditSelectStartChar(Edit)) - (2 * CheckValue));
  1342.             }
  1343.         if (TextViewScreenXFromCharIndex(Edit->View,
  1344.             GetTextEditSelectStartLine(Edit),
  1345.             GetTextEditSelectStartChar(Edit))
  1346.             > GetTextEditPixelIndent(Edit)
  1347.             + GetTextViewWidth(Edit->View) - CheckValue)
  1348.             {
  1349.                 SetTextEditPixelIndent(Edit,
  1350.                     TextViewScreenXFromCharIndex(Edit->View,
  1351.                     GetTextEditSelectStartLine(Edit),
  1352.                     GetTextEditSelectStartChar(Edit))
  1353.                     - GetTextViewWidth(Edit->View) + (2 * CheckValue));
  1354.             }
  1355.     }
  1356.  
  1357.  
  1358. /* show the ending edge of the selection. */
  1359. void                            TextEditShowSelectionEndEdge(TextEditRec* Edit)
  1360.     {
  1361.         long            CheckValue;
  1362.  
  1363.         CheckPtrExistence(Edit);
  1364.         /* figure out how much space to leave at bottom and top edges */
  1365.         CheckValue = 4;
  1366.         while ((CheckValue > 0)
  1367.             && (TextViewNumVisibleLines(Edit->View) < CheckValue * 2 + 4))
  1368.             {
  1369.                 CheckValue -= 1;
  1370.             }
  1371.         /* vertical adjustment */
  1372.         /* try to center the end */
  1373.         if (GetTextEditSelectEndLine(Edit) < GetTextEditTopLine(Edit) + CheckValue)
  1374.             {
  1375.                 /* selection is to far up */
  1376.                 SetTextEditTopLine(Edit,GetTextEditSelectEndLine(Edit) - CheckValue);
  1377.             }
  1378.         if (GetTextEditSelectEndLine(Edit) >= GetTextEditTopLine(Edit)
  1379.             + (TextViewNumVisibleLines(Edit->View) - CheckValue - 1))
  1380.             {
  1381.                 /* selection is too far down */
  1382.                 SetTextEditTopLine(Edit,GetTextEditSelectEndLine(Edit)
  1383.                     - (TextViewNumVisibleLines(Edit->View) - CheckValue - 1) + 1);
  1384.             }
  1385.         /* figure out how much space on left and right edges to leave */
  1386.         CheckValue = 32;
  1387.         while ((CheckValue > 0)
  1388.             && (GetTextViewWidth(Edit->View) < CheckValue * 4 + 16))
  1389.             {
  1390.                 CheckValue -= 1;
  1391.             }
  1392.         /* horizontal adjustment */
  1393.         if (TextViewScreenXFromCharIndex(Edit->View,
  1394.             GetTextEditSelectEndLine(Edit),
  1395.             GetTextEditSelectEndCharPlusOne(Edit))
  1396.             < GetTextEditPixelIndent(Edit) + CheckValue)
  1397.             {
  1398.                 SetTextEditPixelIndent(Edit,
  1399.                     TextViewScreenXFromCharIndex(Edit->View,
  1400.                     GetTextEditSelectEndLine(Edit),
  1401.                     GetTextEditSelectEndCharPlusOne(Edit)) - (2 * CheckValue));
  1402.             }
  1403.         if (TextViewScreenXFromCharIndex(Edit->View,
  1404.             GetTextEditSelectEndLine(Edit),
  1405.             GetTextEditSelectEndCharPlusOne(Edit))
  1406.             > GetTextEditPixelIndent(Edit)
  1407.             + GetTextViewWidth(Edit->View) - CheckValue)
  1408.             {
  1409.                 SetTextEditPixelIndent(Edit,
  1410.                     TextViewScreenXFromCharIndex(Edit->View,
  1411.                     GetTextEditSelectEndLine(Edit),
  1412.                     GetTextEditSelectEndCharPlusOne(Edit))
  1413.                     - GetTextViewWidth(Edit->View) + (2 * CheckValue));
  1414.             }
  1415.     }
  1416.  
  1417.  
  1418. /* handle a keypress for inserting or deleting into the text box */
  1419. void                            TextEditDoKeyPressed(TextEditRec* Edit, char TheKey,
  1420.                                         ModifierFlags Modifiers)
  1421.     {
  1422.         MyBoolean                Extension;
  1423.         MyBoolean                DoShowSelection = True;
  1424.  
  1425.         CheckPtrExistence(Edit);
  1426.         Extension = ((Modifiers & eShiftKey) != 0);
  1427.         switch (((unsigned char)TheKey) & 0xff)
  1428.             {
  1429.                 case 0: case 1: case 2: case 4: case 5: case 6: case 7:
  1430.                 case 10: case 11: case 12: case 14: case 15:
  1431.                 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
  1432.                 case 24: case 25: case 26: case 27:
  1433.                     break;
  1434.                 case 3:  /* show selection */
  1435.                     /* TextEditShowSelection(Edit); */
  1436.                     break;
  1437.                 case 13:  /* carriage return */
  1438.                     if (TextViewIsThereValidSelection(Edit->View))
  1439.                         {
  1440.                             /* delete any existing section & restore insertion point */
  1441.                             TextEditDoMenuClear(Edit);
  1442.                         }
  1443.                     TextViewBreakLine(Edit->View,GetTextEditSelectStartLine(Edit),
  1444.                         GetTextEditSelectStartChar(Edit));
  1445.                     SetTextEditInsertionPoint(Edit,
  1446.                         GetTextEditSelectStartLine(Edit) + 1,0);
  1447.                     TextEditKeyPressedUndoSave(Edit); /* record the new line in the undo info */
  1448.                     if (Edit->AutoIndent)
  1449.                         {
  1450.                             char*                PreviousLine;
  1451.                             long                CharScan;
  1452.                             long                Limit;
  1453.  
  1454.                             PreviousLine = GetTextEditLine(Edit,GetTextEditSelectStartLine(Edit) - 1);
  1455.                             if (PreviousLine != NIL)
  1456.                                 {
  1457.                                     Limit = PtrSize(PreviousLine);
  1458.                                     CharScan = 0;
  1459.                                     while ((CharScan < Limit) && ((PreviousLine[CharScan] == 9)
  1460.                                         || (PreviousLine[CharScan] == 32))) /* space or tab */
  1461.                                         {
  1462.                                             /* yo, this is recursive and could cause problems.  It would */
  1463.                                             /* be a BAD idea to call this if the char was a carriage return */
  1464.                                             TextEditDoKeyPressed(Edit,PreviousLine[CharScan],eNoModifiers);
  1465.                                             CharScan += 1;
  1466.                                         }
  1467.                                     ReleasePtr(PreviousLine);
  1468.                                 }
  1469.                         }
  1470.                     TextEditRecalcVerticalScroll(Edit);
  1471.                     TextEditRecalcHorizontalScroll(Edit);
  1472.                     break;
  1473.                 case 8:  /* backspace key */
  1474.                 case 127:  /* delete key */
  1475.                     if (TextViewIsThereValidSelection(Edit->View))
  1476.                         {
  1477.                             /* delete any existing section & restore insertion point */
  1478.                             TextEditDoMenuClear(Edit);
  1479.                         }
  1480.                      else
  1481.                         {
  1482.                             if (GetTextEditSelectStartChar(Edit) == 0)
  1483.                                 {
  1484.                                     long                SelStartLine;
  1485.  
  1486.                                     /* delete carriage return */
  1487.                                     SelStartLine = GetTextEditSelectStartLine(Edit);
  1488.                                     if (SelStartLine > 0)
  1489.                                         {
  1490.                                             long                FirstLineLength;
  1491.  
  1492.                                             FirstLineLength = GetTextViewLineLength(Edit->View,
  1493.                                                 SelStartLine - 1);
  1494.                                             TextViewFoldLines(Edit->View,SelStartLine - 1);
  1495.                                             SetTextEditInsertionPoint(Edit,SelStartLine - 1,FirstLineLength);
  1496.                                             TextEditRememberUndoDeletedCR(Edit);
  1497.                                             TextEditRecalcVerticalScroll(Edit);
  1498.                                             TextEditRecalcHorizontalScroll(Edit);
  1499.                                         }
  1500.                                     /* else, can't delete past start */
  1501.                                 }
  1502.                              else
  1503.                                 {
  1504.                                     char*                    LineTemp;
  1505.                                     char*                    LineCopy;
  1506.  
  1507.                                     LineTemp = GetTextEditLine(Edit,GetTextEditSelectStartLine(Edit));
  1508.                                     if (LineTemp != NIL)
  1509.                                         {
  1510.                                             char                    WhatWeDeleted;
  1511.  
  1512.                                             WhatWeDeleted = LineTemp[GetTextEditSelectStartChar(Edit) - 1];
  1513.                                             LineCopy = RemoveBlockFromBlockCopy(LineTemp,
  1514.                                                 GetTextEditSelectStartChar(Edit) - 1,1);
  1515.                                             if (LineCopy != NIL)
  1516.                                                 {
  1517.                                                     SetTextEditInsertionPoint(Edit,
  1518.                                                         GetTextEditSelectStartLine(Edit),
  1519.                                                         GetTextEditSelectStartChar(Edit) - 1);
  1520.                                                     /* this is one of the few places where we can use */
  1521.                                                     /* SetTextViewLine instead of SetTextEditLine.  We have */
  1522.                                                     /* to since SetTextEditLine deletes the Undo information. */
  1523.                                                     SetTextViewLine(Edit->View,
  1524.                                                         GetTextEditSelectStartLine(Edit),LineCopy);
  1525.                                                     TextEditRememberUndoDeletedChar(Edit,WhatWeDeleted);
  1526.                                                     ReleasePtr(LineCopy);
  1527.                                                 }
  1528.                                             ReleasePtr(LineTemp);
  1529.                                         }
  1530.                                     TextEditRecalcHorizontalScroll(Edit);
  1531.                                 }
  1532.                         }
  1533.                     break;
  1534.                 case eLeftArrow:
  1535.                     if ((Modifiers & eCommandKey) != 0)
  1536.                         {
  1537.                             if (!Extension)
  1538.                                 {
  1539.                                     SetTextEditInsertionPoint(Edit,
  1540.                                         GetTextEditSelectStartLine(Edit),0);
  1541.                                 }
  1542.                              else
  1543.                                 {
  1544.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),0,
  1545.                                         GetTextEditSelectEndLine(Edit),
  1546.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1547.                                     DoShowSelection = False;
  1548.                                     TextEditShowSelectionStartEdge(Edit);
  1549.                                 }
  1550.                         }
  1551.                     else if ((Modifiers & eOptionKey) != 0)
  1552.                         {
  1553.                             long                Line;
  1554.                             long                Index;
  1555.                             char*                LineTemp;
  1556.  
  1557.                             Line = GetTextEditSelectStartLine(Edit);
  1558.                             Index = GetTextEditSelectStartChar(Edit);
  1559.                             LineTemp = GetTextEditLine(Edit,Line);
  1560.                             if (LineTemp != NIL)
  1561.                                 {
  1562.                                     if (Index > 0)
  1563.                                         {
  1564.                                             while ((Index > 0) && !AlphaNum(LineTemp[Index - 1]))
  1565.                                                 {
  1566.                                                     /* skipping white space between cursor & previous word */
  1567.                                                     Index -= 1;
  1568.                                                 }
  1569.                                             while ((Index > 0) && AlphaNum(LineTemp[Index - 1]))
  1570.                                                 {
  1571.                                                     /* skipping over the word itself */
  1572.                                                     Index -= 1;
  1573.                                                 }
  1574.                                         }
  1575.                                      else
  1576.                                         {
  1577.                                             if (Line > 0)
  1578.                                                 {
  1579.                                                     Line -= 1;
  1580.                                                     Index = GetTextViewLineLength(Edit->View,Line);
  1581.                                                 }
  1582.                                         }
  1583.                                     ReleasePtr(LineTemp);
  1584.                                 }
  1585.                             if (!Extension)
  1586.                                 {
  1587.                                     SetTextEditInsertionPoint(Edit,Line,Index);
  1588.                                 }
  1589.                              else
  1590.                                 {
  1591.                                     SetTextEditSelection(Edit,Line,Index,
  1592.                                         GetTextEditSelectEndLine(Edit),
  1593.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1594.                                     DoShowSelection = False;
  1595.                                     TextEditShowSelectionStartEdge(Edit);
  1596.                                 }
  1597.                         }
  1598.                     else
  1599.                         {
  1600.                             if (TextEditIsThereValidSelection(Edit) && !Extension)
  1601.                                 {
  1602.                                     SetTextEditInsertionPoint(Edit,
  1603.                                         GetTextEditSelectStartLine(Edit),GetTextEditSelectStartChar(Edit));
  1604.                                 }
  1605.                              else
  1606.                                 {
  1607.                                     if (GetTextEditSelectStartChar(Edit) == 0)
  1608.                                         {
  1609.                                             if (GetTextEditSelectStartLine(Edit) > 0)
  1610.                                                 {
  1611.                                                     if (!Extension)
  1612.                                                         {
  1613.                                                             SetTextEditInsertionPoint(Edit,
  1614.                                                                 GetTextEditSelectStartLine(Edit) - 1,
  1615.                                                                 GetTextViewLineLength(Edit->View,
  1616.                                                                 GetTextEditSelectStartLine(Edit) - 1));
  1617.                                                         }
  1618.                                                      else
  1619.                                                         {
  1620.                                                             SetTextEditSelection(Edit,
  1621.                                                                 GetTextEditSelectStartLine(Edit) - 1,
  1622.                                                                 GetTextViewLineLength(Edit->View,
  1623.                                                                     GetTextEditSelectStartLine(Edit) - 1),
  1624.                                                                 GetTextEditSelectEndLine(Edit),
  1625.                                                                 GetTextEditSelectEndCharPlusOne(Edit));
  1626.                                                             DoShowSelection = False;
  1627.                                                             TextEditShowSelectionStartEdge(Edit);
  1628.                                                         }
  1629.                                                 }
  1630.                                         }
  1631.                                      else
  1632.                                         {
  1633.                                             if (!Extension)
  1634.                                                 {
  1635.                                                     SetTextEditInsertionPoint(Edit,
  1636.                                                         GetTextEditSelectStartLine(Edit),
  1637.                                                         GetTextEditSelectStartChar(Edit) - 1);
  1638.                                                 }
  1639.                                              else
  1640.                                                 {
  1641.                                                     SetTextEditSelection(Edit,
  1642.                                                         GetTextEditSelectStartLine(Edit),
  1643.                                                         GetTextEditSelectStartChar(Edit) - 1,
  1644.                                                         GetTextEditSelectEndLine(Edit),
  1645.                                                         GetTextEditSelectEndCharPlusOne(Edit));
  1646.                                                     DoShowSelection = False;
  1647.                                                     TextEditShowSelectionStartEdge(Edit);
  1648.                                                 }
  1649.                                         }
  1650.                                 }
  1651.                         }
  1652.                     break;
  1653.                 case eRightArrow:
  1654.                     if ((Modifiers & eCommandKey) != 0)
  1655.                         {
  1656.                             if (!Extension)
  1657.                                 {
  1658.                                     SetTextEditInsertionPoint(Edit,GetTextEditSelectEndLine(Edit),
  1659.                                         GetTextViewLineLength(Edit->View,GetTextEditSelectEndLine(Edit)));
  1660.                                 }
  1661.                              else
  1662.                                 {
  1663.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1664.                                         GetTextEditSelectStartChar(Edit),GetTextEditSelectEndLine(Edit),
  1665.                                         GetTextViewLineLength(Edit->View,GetTextEditSelectEndLine(Edit)));
  1666.                                     DoShowSelection = False;
  1667.                                     TextEditShowSelectionEndEdge(Edit);
  1668.                                 }
  1669.                         }
  1670.                     else if ((Modifiers & eOptionKey) != 0)
  1671.                         {
  1672.                             long                Line;
  1673.                             long                Index;
  1674.                             long                Length;
  1675.                             char*                LineTemp;
  1676.  
  1677.                             Line = GetTextEditSelectEndLine(Edit);
  1678.                             Index = GetTextEditSelectEndCharPlusOne(Edit);
  1679.                             Length = GetTextViewLineLength(Edit->View,Line);
  1680.                             LineTemp = GetTextEditLine(Edit,Line);
  1681.                             if (LineTemp != NIL)
  1682.                                 {
  1683.                                     if (Index < Length)
  1684.                                         {
  1685.                                             while ((Index < Length) && !AlphaNum(LineTemp[Index]))
  1686.                                                 {
  1687.                                                     /* skipping white space between cursor & next word */
  1688.                                                     Index += 1;
  1689.                                                 }
  1690.                                             while ((Index < Length) && AlphaNum(LineTemp[Index]))
  1691.                                                 {
  1692.                                                     /* skipping over the word itself */
  1693.                                                     Index += 1;
  1694.                                                 }
  1695.                                         }
  1696.                                      else
  1697.                                         {
  1698.                                             if (Line < GetTextViewNumLines(Edit->View) - 1)
  1699.                                                 {
  1700.                                                     Line += 1;
  1701.                                                     Index = 0;
  1702.                                                 }
  1703.                                         }
  1704.                                     ReleasePtr(LineTemp);
  1705.                                 }
  1706.                             if (!Extension)
  1707.                                 {
  1708.                                     SetTextEditInsertionPoint(Edit,Line,Index);
  1709.                                 }
  1710.                              else
  1711.                                 {
  1712.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1713.                                         GetTextEditSelectStartChar(Edit),Line,Index);
  1714.                                     DoShowSelection = False;
  1715.                                     TextEditShowSelectionEndEdge(Edit);
  1716.                                 }
  1717.                         }
  1718.                     else
  1719.                         {
  1720.                             if (TextEditIsThereValidSelection(Edit) && !Extension)
  1721.                                 {
  1722.                                     SetTextEditInsertionPoint(Edit,GetTextEditSelectEndLine(Edit),
  1723.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1724.                                 }
  1725.                              else
  1726.                                 {
  1727.                                     if (GetTextEditSelectEndCharPlusOne(Edit) == GetTextViewLineLength(
  1728.                                         Edit->View,GetTextEditSelectEndLine(Edit)))
  1729.                                         {
  1730.                                             if (GetTextEditSelectEndLine(Edit) <
  1731.                                                 GetTextViewNumLines(Edit->View) - 1)
  1732.                                                 {
  1733.                                                     if (!Extension)
  1734.                                                         {
  1735.                                                             SetTextEditInsertionPoint(Edit,
  1736.                                                                 GetTextEditSelectEndLine(Edit) + 1,0);
  1737.                                                         }
  1738.                                                      else
  1739.                                                         {
  1740.                                                             SetTextEditSelection(Edit,
  1741.                                                                 GetTextEditSelectStartLine(Edit),
  1742.                                                                 GetTextEditSelectStartChar(Edit),
  1743.                                                                 GetTextEditSelectEndLine(Edit) + 1,0);
  1744.                                                             DoShowSelection = False;
  1745.                                                             TextEditShowSelectionEndEdge(Edit);
  1746.                                                         }
  1747.                                                 }
  1748.                                         }
  1749.                                      else
  1750.                                         {
  1751.                                             if (!Extension)
  1752.                                                 {
  1753.                                                     SetTextEditInsertionPoint(Edit,GetTextEditSelectEndLine(Edit),
  1754.                                                         GetTextEditSelectEndCharPlusOne(Edit) + 1);
  1755.                                                 }
  1756.                                              else
  1757.                                                 {
  1758.                                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1759.                                                         GetTextEditSelectStartChar(Edit),
  1760.                                                         GetTextEditSelectEndLine(Edit),
  1761.                                                         GetTextEditSelectEndCharPlusOne(Edit) + 1);
  1762.                                                     DoShowSelection = False;
  1763.                                                     TextEditShowSelectionEndEdge(Edit);
  1764.                                                 }
  1765.                                         }
  1766.                                 }
  1767.                         }
  1768.                     break;
  1769.                 case eUpArrow:
  1770.                     if ((Modifiers & eCommandKey) != 0)
  1771.                         {
  1772.                             if (!Extension)
  1773.                                 {
  1774.                                     SetTextEditInsertionPoint(Edit,0,0);
  1775.                                 }
  1776.                              else
  1777.                                 {
  1778.                                     SetTextEditSelection(Edit,0,0,GetTextEditSelectEndLine(Edit),
  1779.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1780.                                     DoShowSelection = False;
  1781.                                     TextEditShowSelectionStartEdge(Edit);
  1782.                                 }
  1783.                         }
  1784.                     else if (((Modifiers & eOptionKey) != 0) && !Extension)
  1785.                         {
  1786.                             long                        NewPosition;
  1787.                             long                        NewPoint;
  1788.  
  1789.                             NewPosition = GetTextEditSelectStartLine(Edit)
  1790.                                 - TextViewNumVisibleLines(Edit->View) + 5;
  1791.                             if (NewPosition < 0)
  1792.                                 {
  1793.                                     NewPosition = 0;
  1794.                                 }
  1795.                             NewPoint = TextViewCharIndexFromScreenX(Edit->View,
  1796.                                 NewPosition, /* previous line */
  1797.                                 TextViewScreenXFromCharIndex(Edit->View,
  1798.                                 GetTextEditSelectStartLine(Edit), /* this line */
  1799.                                 GetTextEditSelectStartChar(Edit)));
  1800.                             SetTextEditInsertionPoint(Edit,NewPosition,NewPoint);
  1801.                         }
  1802.                     else
  1803.                         {
  1804.                             long                        NewLineIndex;
  1805.                             long                        NewPoint;
  1806.  
  1807.                             NewLineIndex = GetTextEditSelectStartLine(Edit) - 1;
  1808.                             if (NewLineIndex < 0)
  1809.                                 {
  1810.                                     NewLineIndex = 0;
  1811.                                 }
  1812.                             /* snap it to the closest point on the next line */
  1813.                             NewPoint = TextViewCharIndexFromScreenX(Edit->View,
  1814.                                 NewLineIndex, /* previous line */
  1815.                                 TextViewScreenXFromCharIndex(Edit->View,
  1816.                                 GetTextEditSelectStartLine(Edit), /* this line */
  1817.                                 GetTextEditSelectStartChar(Edit)));
  1818.                             if (!Extension)
  1819.                                 {
  1820.                                     SetTextEditInsertionPoint(Edit,NewLineIndex,NewPoint);
  1821.                                 }
  1822.                              else
  1823.                                 {
  1824.                                     SetTextEditSelection(Edit,NewLineIndex,NewPoint,
  1825.                                         GetTextEditSelectEndLine(Edit),
  1826.                                         GetTextEditSelectEndCharPlusOne(Edit));
  1827.                                     DoShowSelection = False;
  1828.                                     TextEditShowSelectionStartEdge(Edit);
  1829.                                 }
  1830.                         }
  1831.                     break;
  1832.                 case eDownArrow:
  1833.                     if ((Modifiers & eCommandKey) != 0)
  1834.                         {
  1835.                             if (!Extension)
  1836.                                 {
  1837.                                     SetTextEditInsertionPoint(Edit,GetTextViewNumLines(
  1838.                                         Edit->View) - 1,GetTextViewLineLength(Edit->View,
  1839.                                         GetTextViewNumLines(Edit->View) - 1));
  1840.                                 }
  1841.                              else
  1842.                                 {
  1843.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1844.                                         GetTextEditSelectStartChar(Edit),GetTextViewNumLines(
  1845.                                         Edit->View) - 1,GetTextViewLineLength(Edit->View,
  1846.                                         GetTextViewNumLines(Edit->View) - 1));
  1847.                                     DoShowSelection = False;
  1848.                                     TextEditShowSelectionEndEdge(Edit);
  1849.                                 }
  1850.                         }
  1851.                     else if (((Modifiers & eOptionKey) != 0) && !Extension)
  1852.                         {
  1853.                             long                        NewPosition;
  1854.                             long                        NewPoint;
  1855.  
  1856.                             NewPosition = GetTextEditSelectEndLine(Edit)
  1857.                                 + TextViewNumVisibleLines(Edit->View) - 5;
  1858.                             if (NewPosition > GetTextViewNumLines(Edit->View) - 1)
  1859.                                 {
  1860.                                     NewPosition = GetTextViewNumLines(Edit->View) - 1;
  1861.                                 }
  1862.                             NewPoint = TextViewCharIndexFromScreenX(Edit->View,
  1863.                                 NewPosition, /* next line */
  1864.                                 TextViewScreenXFromCharIndex(Edit->View,
  1865.                                 GetTextEditSelectEndLine(Edit), /* this line */
  1866.                                 GetTextEditSelectEndCharPlusOne(Edit)));
  1867.                             SetTextEditInsertionPoint(Edit,NewPosition,NewPoint);
  1868.                         }
  1869.                     else
  1870.                         {
  1871.                             long                        NewLineIndex;
  1872.                             long                        NewPoint;
  1873.  
  1874.                             NewLineIndex = GetTextEditSelectEndLine(Edit) + 1;
  1875.                             if (NewLineIndex > GetTextViewNumLines(Edit->View) - 1)
  1876.                                 {
  1877.                                     NewLineIndex = GetTextViewNumLines(Edit->View) - 1;
  1878.                                 }
  1879.                             /* snap it to the closest point on the next line */
  1880.                             NewPoint = TextViewCharIndexFromScreenX(Edit->View,
  1881.                                 NewLineIndex, /* next line */
  1882.                                 TextViewScreenXFromCharIndex(Edit->View,
  1883.                                 GetTextEditSelectEndLine(Edit), /* this line */
  1884.                                 GetTextEditSelectEndCharPlusOne(Edit)));
  1885.                             if (!Extension)
  1886.                                 {
  1887.                                     SetTextEditInsertionPoint(Edit,NewLineIndex,NewPoint);
  1888.                                 }
  1889.                              else
  1890.                                 {
  1891.                                     SetTextEditSelection(Edit,GetTextEditSelectStartLine(Edit),
  1892.                                         GetTextEditSelectStartChar(Edit),NewLineIndex,NewPoint);
  1893.                                     DoShowSelection = False;
  1894.                                     TextEditShowSelectionEndEdge(Edit);
  1895.                                 }
  1896.                         }
  1897.                     break;
  1898.                 case 9:  /* tab key */
  1899.                 default:  /* any other character */
  1900.                     if ((Modifiers & eCommandKey) == 0)
  1901.                         {
  1902.                             char*                    LineTemp;
  1903.                             char                    Buffer[1];
  1904.                             char*                    LineCopy;
  1905.  
  1906.                             CheckPtrExistence(Edit);
  1907.                             if (TextViewIsThereValidSelection(Edit->View))
  1908.                                 {
  1909.                                     /* delete any existing section & restore insertion point */
  1910.                                     TextEditDoMenuClear(Edit);
  1911.                                 }
  1912.                             LineTemp = GetTextEditLine(Edit,GetTextEditSelectStartLine(Edit));
  1913.                             if (LineTemp != NIL)
  1914.                                 {
  1915.                                     Buffer[0] = TheKey;
  1916.                                     LineCopy = InsertBlockIntoBlockCopy(LineTemp,Buffer,
  1917.                                         GetTextEditSelectStartChar(Edit),1);
  1918.                                     if (LineCopy != NIL)
  1919.                                         {
  1920.                                             /* this is one of the few places where we can use */
  1921.                                             /* SetTextViewLine instead of SetTextEditLine.  We have */
  1922.                                             /* to since SetTextEditLine deletes the Undo information. */
  1923.                                             SetTextViewLine(Edit->View,
  1924.                                                 GetTextEditSelectStartLine(Edit),LineCopy);
  1925.                                             ReleasePtr(LineCopy);
  1926.                                         }
  1927.                                     SetTextEditInsertionPoint(Edit,GetTextEditSelectStartLine(Edit),
  1928.                                         GetTextEditSelectStartChar(Edit) + 1);
  1929.                                     ReleasePtr(LineTemp);
  1930.                                     TextEditKeyPressedUndoSave(Edit);
  1931.                                 }
  1932.                         }
  1933.                     break;
  1934.             }
  1935.         if (DoShowSelection)
  1936.             {
  1937.                 TextEditShowSelection(Edit); /* make sure insertion point is on screen */
  1938.             }
  1939.     }
  1940.  
  1941.  
  1942. /* vertical scroll callback routine */
  1943. static void            TEVerticalScrollHook(long Parameter, ScrollType How,
  1944.                                     TextEditRec* TempScrollEdit)
  1945.     {
  1946.         CheckPtrExistence(TempScrollEdit);
  1947.         switch (How)
  1948.             {
  1949.                 case eScrollToPosition:
  1950.                     SetTextEditTopLine(TempScrollEdit,Parameter);
  1951.                     break;
  1952.                 case eScrollPageMinus:
  1953.                     SetTextEditTopLine(TempScrollEdit,GetTextEditTopLine(TempScrollEdit)
  1954.                         - (TextViewNumVisibleLines(TempScrollEdit->View) - 3));
  1955.                     break;
  1956.                 case eScrollPagePlus:
  1957.                     SetTextEditTopLine(TempScrollEdit,GetTextEditTopLine(TempScrollEdit)
  1958.                         + (TextViewNumVisibleLines(TempScrollEdit->View) - 3));
  1959.                     break;
  1960.                 case eScrollLineMinus:
  1961.                     SetTextEditTopLine(TempScrollEdit,GetTextEditTopLine(TempScrollEdit) - 1);
  1962.                     break;
  1963.                 case eScrollLinePlus:
  1964.                     SetTextEditTopLine(TempScrollEdit,GetTextEditTopLine(TempScrollEdit) + 1);
  1965.                     break;
  1966.                 default:
  1967.                     EXECUTE(PRERR(AllowResume,"TEVerticalScrollHook:  Unknown scroll opcode"));
  1968.             }
  1969.     }
  1970.  
  1971.  
  1972. /* horizontal scroll callback routine */
  1973. static void            TEHorizontalScrollHook(long Parameter, ScrollType How,
  1974.                                     TextEditRec* TempScrollEdit)
  1975.     {
  1976.         CheckPtrExistence(TempScrollEdit);
  1977.         switch (How)
  1978.             {
  1979.                 case eScrollToPosition:
  1980.                     SetTextEditPixelIndent(TempScrollEdit,Parameter);
  1981.                     break;
  1982.                 case eScrollPageMinus:
  1983.                     SetTextEditPixelIndent(TempScrollEdit,GetTextEditPixelIndent(TempScrollEdit)
  1984.                         - (GetTextViewWidth(TempScrollEdit->View) - 32));
  1985.                     break;
  1986.                 case eScrollPagePlus:
  1987.                     SetTextEditPixelIndent(TempScrollEdit,GetTextEditPixelIndent(TempScrollEdit)
  1988.                         + (GetTextViewWidth(TempScrollEdit->View) - 32));
  1989.                     break;
  1990.                 case eScrollLineMinus:
  1991.                     SetTextEditPixelIndent(TempScrollEdit,
  1992.                         GetTextEditPixelIndent(TempScrollEdit) - 32);
  1993.                     break;
  1994.                 case eScrollLinePlus:
  1995.                     SetTextEditPixelIndent(TempScrollEdit,
  1996.                         GetTextEditPixelIndent(TempScrollEdit) + 32);
  1997.                     break;
  1998.                 default:
  1999.                     EXECUTE(PRERR(AllowResume,"TEHorizontalScrollHook:  Unknown scroll opcode"));
  2000.             }
  2001.     }
  2002.  
  2003.  
  2004. /* handle a mouse-down in the text box */
  2005. void                            TextEditDoMouseDown(TextEditRec* Edit, OrdType OrigX, OrdType OrigY,
  2006.                                         ModifierFlags Modifiers)
  2007.     {
  2008.         SelRec                BaseSelectionStart; /* where the mouse first hits, and if shift */
  2009.         SelRec                BaseSelectionEnd; /* is down, then it's the previous range */
  2010.         SelRec                CurrentMousePoint;
  2011.         SelRec                TempFirst;
  2012.         SelRec                TempLast;
  2013.         SelRec                OldSelectionStart;
  2014.         SelRec                OldSelectionEnd;
  2015.         OrdType                WhereX;
  2016.         OrdType                WhereY;
  2017.         OrdType                MouseLocationX;
  2018.         OrdType                MouseLocationY;
  2019.  
  2020.         CheckPtrExistence(Edit);
  2021.         WhereX = OrigX;
  2022.         WhereY = OrigY;
  2023.         if (Edit->VerticalScroll != NIL)
  2024.             {
  2025.                 if (ScrollHitTest(Edit->VerticalScroll,WhereX,WhereY))
  2026.                     {
  2027.                         ScrollHitProc(Edit->VerticalScroll,Modifiers,WhereX,
  2028.                             WhereY,Edit,(void (*)(long,ScrollType,void*))&TEVerticalScrollHook);
  2029.                         return;
  2030.                     }
  2031.             }
  2032.         if (Edit->HorizontalScroll != NIL)
  2033.             {
  2034.                 if (ScrollHitTest(Edit->HorizontalScroll,WhereX,WhereY))
  2035.                     {
  2036.                         ScrollHitProc(Edit->HorizontalScroll,Modifiers,WhereX,
  2037.                             WhereY,Edit,(void (*)(long,ScrollType,void*))&TEHorizontalScrollHook);
  2038.                         return;
  2039.                     }
  2040.             }
  2041.         WhereX -= GetTextViewXLoc(Edit->View);
  2042.         WhereY -= GetTextViewYLoc(Edit->View);
  2043.  
  2044.         if ((TimerDifference(ReadTimer(),Edit->LastClickTime) < GetDoubleClickInterval())
  2045.             && ((WhereX - Edit->LastClickX <= 3) && (WhereX - Edit->LastClickX >= -3))
  2046.             && ((WhereY - Edit->LastClickY <= 3) && (WhereY - Edit->LastClickY >= -3)))
  2047.             {
  2048.                 switch (Edit->ClickPhase)
  2049.                     {
  2050.                         case eNoClick:
  2051.                             Edit->ClickPhase = eSingleClick;
  2052.                             break;
  2053.                         case eSingleClick:
  2054.                             Edit->ClickPhase = eDoubleClick;
  2055.                             break;
  2056.                         case eDoubleClick:
  2057.                             Edit->ClickPhase = eTripleClick;
  2058.                             break;
  2059.                         case eTripleClick:
  2060.                             /* no change */
  2061.                             break;
  2062.                     }
  2063.             }
  2064.          else
  2065.             {
  2066.                 Edit->ClickPhase = eSingleClick;
  2067.             }
  2068.         Edit->LastClickTime = ReadTimer();
  2069.         Edit->LastClickX = WhereX;
  2070.         Edit->LastClickY = WhereY;
  2071.  
  2072.         CurrentMousePoint.Line = WhereY / GetTextViewLineHeight(Edit->View)
  2073.             + GetTextViewTopLine(Edit->View);
  2074.         CurrentMousePoint.Column = TextViewCharIndexFromScreenX(Edit->View,
  2075.             CurrentMousePoint.Line,WhereX + GetTextViewPixelIndent(Edit->View));
  2076.         if ((Modifiers & eShiftKey) != 0)
  2077.             {
  2078.                 BaseSelectionStart.Line = GetTextEditSelectStartLine(Edit);
  2079.                 BaseSelectionStart.Column = GetTextEditSelectStartChar(Edit);
  2080.                 BaseSelectionEnd.Line = GetTextEditSelectEndLine(Edit);
  2081.                 BaseSelectionEnd.Column = GetTextEditSelectEndCharPlusOne(Edit);
  2082.             }
  2083.          else
  2084.             {
  2085.                 if (TextEditIsThereValidSelection(Edit))
  2086.                     {
  2087.                         SelRec                Start,End;
  2088.  
  2089.                         Start.Line = GetTextEditSelectStartLine(Edit);
  2090.                         Start.Column = GetTextEditSelectStartChar(Edit);
  2091.                         End.Line = GetTextEditSelectEndLine(Edit);
  2092.                         End.Column = GetTextEditSelectEndCharPlusOne(Edit);
  2093.                         SetTextEditInsertionPoint(Edit,Start.Line,Start.Column);
  2094.                         TextViewRedrawRange(Edit->View,Start.Line,End.Line);
  2095.                     }
  2096.                 BaseSelectionStart = CurrentMousePoint;
  2097.                 BaseSelectionEnd = CurrentMousePoint;
  2098.                 ExtendSelection(Edit,&BaseSelectionStart,&BaseSelectionEnd);
  2099.                 SetTextEditSelection(Edit,BaseSelectionStart.Line,BaseSelectionStart.Column,
  2100.                     BaseSelectionEnd.Line,BaseSelectionEnd.Column);
  2101.             }
  2102.  
  2103.         while (eMouseUp != GetAnEvent(&MouseLocationX,&MouseLocationY,NIL,NIL,NIL,NIL))
  2104.             {
  2105.                 WhereX = MouseLocationX - GetTextViewXLoc(Edit->View);
  2106.                 WhereY = MouseLocationY - GetTextViewYLoc(Edit->View);
  2107.                 if (WhereX < 0)
  2108.                     {
  2109.                         SetTextEditPixelIndent(Edit,GetTextEditPixelIndent(Edit) - 24);
  2110.                     }
  2111.                 if (WhereX > GetTextViewWidth(Edit->View))
  2112.                     {
  2113.                         SetTextEditPixelIndent(Edit,GetTextEditPixelIndent(Edit) + 24);
  2114.                     }
  2115.                 if (WhereY < 0)
  2116.                     {
  2117.                         SetTextEditTopLine(Edit,GetTextEditTopLine(Edit) - 1);
  2118.                     }
  2119.                 if (WhereY > GetTextViewHeight(Edit->View))
  2120.                     {
  2121.                         SetTextEditTopLine(Edit,GetTextEditTopLine(Edit) + 1);
  2122.                     }
  2123.  
  2124.                 if (WhereX < 0)
  2125.                     {
  2126.                         WhereX = 0;
  2127.                     }
  2128.                 if (WhereX > GetTextViewWidth(Edit->View) - 1)
  2129.                     {
  2130.                         WhereX = GetTextViewWidth(Edit->View) - 1;
  2131.                     }
  2132.                 if (WhereY < 0)
  2133.                     {
  2134.                         WhereY = 0;
  2135.                     }
  2136.                 if (WhereY > GetTextViewHeight(Edit->View) - 1)
  2137.                     {
  2138.                         WhereY = GetTextViewHeight(Edit->View) - 1;
  2139.                     }
  2140.                 CurrentMousePoint.Line = WhereY / GetTextViewLineHeight(Edit->View)
  2141.                     + GetTextViewTopLine(Edit->View);
  2142.                 CurrentMousePoint.Column = TextViewCharIndexFromScreenX(Edit->View,
  2143.                     CurrentMousePoint.Line,WhereX + GetTextViewPixelIndent(Edit->View));
  2144.                 /* calculate what the extent of the current mouse selection should be */
  2145.                 if (GreaterThan(&BaseSelectionStart,&CurrentMousePoint)
  2146.                     || ((BaseSelectionStart.Line == CurrentMousePoint.Line)
  2147.                     && (BaseSelectionStart.Column == CurrentMousePoint.Column)))
  2148.                     {
  2149.                         ExtendSelection(Edit,&CurrentMousePoint,NIL);
  2150.                     }
  2151.                  else
  2152.                     {
  2153.                         ExtendSelection(Edit,NIL,&CurrentMousePoint);
  2154.                     }
  2155.                 /* calculating the total selection */
  2156.                 UnionSelection(BaseSelectionStart,BaseSelectionEnd,CurrentMousePoint,
  2157.                     &TempFirst,&TempLast);
  2158.                 /* redrawing what has changed */
  2159.                 OldSelectionStart.Line = GetTextEditSelectStartLine(Edit);
  2160.                 OldSelectionStart.Column = GetTextEditSelectStartChar(Edit);
  2161.                 OldSelectionEnd.Line = GetTextEditSelectEndLine(Edit);
  2162.                 OldSelectionEnd.Column = GetTextEditSelectEndCharPlusOne(Edit);
  2163.                 SetTextEditSelection(Edit,TempFirst.Line,TempFirst.Column,
  2164.                     TempLast.Line,TempLast.Column);
  2165.             }
  2166.     }
  2167.  
  2168.  
  2169. /* this would be called from TextEditDoMenuClear, which would then call */
  2170. /* TextEditBlockRemoved to install the stuff that had been removed */
  2171. static void                TextEditPurgeUndoRecord(TextEditRec* Edit)
  2172.     {
  2173.         CheckPtrExistence(Edit);
  2174.         if (Edit->Undo.CanUndoFlag)
  2175.             {
  2176.                 /* valid record needs purging */
  2177.                 if (Edit->Undo.DeletedValidFlag)
  2178.                     {
  2179.                         /* delete the text thing */
  2180.                         DisposeTextStorage(Edit->Undo.DeletedStuff);
  2181.                     }
  2182.                 Edit->Undo.CanUndoFlag = False;
  2183.             }
  2184.     }
  2185.  
  2186.  
  2187. /* something tricky to watch out for:  If you delete stuff, then insert stuff */
  2188. /* above it, the position that it was deleted from will be invalid.  You then */
  2189. /* have to exactly remove the stuff you inserted above BEFORE deleting when */
  2190. /* you actually perform the 'undo' operation */
  2191. static void                TextEditBlockRemovedUndoSave(TextEditRec* Edit,
  2192.                                         TextStorageRec* Stuff, long WhereLine, long WhereChar)
  2193.     {
  2194.         TextEditPurgeUndoRecord(Edit);
  2195.         Edit->Undo.CanUndoFlag = True;
  2196.         Edit->Undo.DeletedValidFlag = True;
  2197.         Edit->Undo.DeletedStuff = Stuff;
  2198.         Edit->Undo.DeletedLine = WhereLine;
  2199.         Edit->Undo.DeletedChar = WhereChar;
  2200.         Edit->Undo.ReplacingValidFlag = False; /* haven't started replacing yet */
  2201.     }
  2202.  
  2203.  
  2204. /* this routine is called when a key is pressed to update the information */
  2205. /* for the undo record.  If the insertion point is in a place indicating that */
  2206. /* the user did NOT move to another location, then it will be added to the record. */
  2207. /* otherwise the undo record will be purged and a new one started. */
  2208. static void                TextEditKeyPressedUndoSave(TextEditRec* Edit)
  2209.     {
  2210.         CheckPtrExistence(Edit);
  2211.         ERROR(TextEditIsThereValidSelection(Edit),PRERR(AllowResume,
  2212.             "TextEditKeyPressedUndoSave:  Why is there a valid selection?"));
  2213.         if (!Edit->Undo.CanUndoFlag)
  2214.             {
  2215.                 Edit->Undo.CanUndoFlag = True;
  2216.                 Edit->Undo.DeletedValidFlag = False;
  2217.                 Edit->Undo.ReplacingValidFlag = False;
  2218.             }
  2219.         if (Edit->Undo.ReplacingValidFlag)
  2220.             {
  2221.                 /* was the character added in the logical next position? */
  2222.                 if ((Edit->Undo.ReplacingEndLine == GetTextEditSelectEndLine(Edit))
  2223.                     && (Edit->Undo.ReplacingEndCharPlusOne + 1
  2224.                     == GetTextEditSelectEndCharPlusOne(Edit)))
  2225.                     {
  2226.                         /* yup, right after the last char */
  2227.                         Edit->Undo.ReplacingEndCharPlusOne += 1; /* increment & all's cool */
  2228.                     }
  2229.                  else
  2230.                     {
  2231.                         /* there's still hope.  Did they make a new line? */
  2232.                         if ((Edit->Undo.ReplacingEndLine + 1 == GetTextEditSelectEndLine(Edit))
  2233.                             && (GetTextEditSelectEndCharPlusOne(Edit) == 0)
  2234.                             && (Edit->Undo.ReplacingEndCharPlusOne ==
  2235.                             GetTextViewLineLength(Edit->View,Edit->Undo.ReplacingEndLine)))
  2236.                             {
  2237.                                 /* stated in English:  If we're on the line right after the last */
  2238.                                 /* line in the added range, and we're at the beginning, and */
  2239.                                 /* the last char added before this was at the end of the previous */
  2240.                                 /* line, then the user probably hit return. */
  2241.                                 Edit->Undo.ReplacingEndCharPlusOne = 0;
  2242.                                 Edit->Undo.ReplacingEndLine += 1;
  2243.                             }
  2244.                          else
  2245.                             {
  2246.                                 /* otherwise we need to purge and reset. */
  2247.                                 TextEditPurgeUndoRecord(Edit);
  2248.                                 Edit->Undo.CanUndoFlag = True;
  2249.                                 Edit->Undo.DeletedValidFlag = False;
  2250.                              MakeNewRangePoint: /* we might just drop in some day */
  2251.                                 Edit->Undo.ReplacingValidFlag = True; /* only this happens */
  2252.                                 if (GetTextEditSelectStartChar(Edit) == 0)
  2253.                                     {
  2254.                                         ERROR(GetTextEditSelectStartLine(Edit) == 0,PRERR(AllowResume,
  2255.                                             "TextEditKeyPressedUndoSave:  Char "
  2256.                                             "inserted, but insertion point "
  2257.                                             "is still in the home position (0,0)."));
  2258.                                         Edit->Undo.ReplacingStartLine
  2259.                                             = GetTextEditSelectStartLine(Edit) - 1;
  2260.                                         Edit->Undo.ReplacingStartChar
  2261.                                             = GetTextViewLineLength(Edit->View,
  2262.                                             Edit->Undo.ReplacingStartLine);
  2263.                                     }
  2264.                                  else
  2265.                                     {
  2266.                                         Edit->Undo.ReplacingStartChar
  2267.                                             = GetTextEditSelectStartChar(Edit) - 1;
  2268.                                         Edit->Undo.ReplacingStartLine = GetTextEditSelectStartLine(Edit);
  2269.                                     }
  2270.                                 Edit->Undo.ReplacingEndLine = GetTextEditSelectEndLine(Edit);
  2271.                                 Edit->Undo.ReplacingEndCharPlusOne
  2272.                                     = GetTextEditSelectEndCharPlusOne(Edit);
  2273.                             }
  2274.                     }
  2275.             }
  2276.          else
  2277.             {
  2278.                 /* else, there isn't even a range defined.  So define one. */
  2279.                 goto MakeNewRangePoint;
  2280.             }
  2281.     }
  2282.  
  2283.  
  2284. /* undo the last operation that changed the contained data.  not all operations */
  2285. /* can be undone.  Who knows what the state of things will be if this fails. */
  2286. MyBoolean                    TextEditDoMenuUndo(TextEditRec* Edit)
  2287.     {
  2288.         TextStorageRec*    OldAddedStuff;
  2289.         MyBoolean                OldStuffValid;
  2290.         long                        OldAddedLine;
  2291.         long                        OldAddedChar;
  2292.         MyBoolean                StuffWasInserted;
  2293.  
  2294.         CheckPtrExistence(Edit);
  2295.         if (Edit->Undo.CanUndoFlag)
  2296.             {
  2297.                 OldStuffValid = Edit->Undo.ReplacingValidFlag;
  2298.                 if (OldStuffValid)
  2299.                     {
  2300.                         SetTextEditSelection(Edit,Edit->Undo.ReplacingStartLine,
  2301.                             Edit->Undo.ReplacingStartChar,Edit->Undo.ReplacingEndLine,
  2302.                             Edit->Undo.ReplacingEndCharPlusOne);
  2303.                         OldAddedStuff = TextViewGetSelection(Edit->View);
  2304.                         if (OldAddedStuff == NIL)
  2305.                             {
  2306.                                 return False; /* abort */
  2307.                             }
  2308.                         if (!TextViewDeleteSelection(Edit->View))
  2309.                             {
  2310.                                 TextEditPurgeUndoRecord(Edit); /* avoid inconsistencies */
  2311.                                 return False;
  2312.                             }
  2313.                         /* save the place where we'd put this stuff back */
  2314.                         OldAddedLine = GetTextEditSelectStartLine(Edit);
  2315.                         OldAddedChar = GetTextEditSelectStartChar(Edit);
  2316.                     }
  2317.                 /* now the coordinates for the old deleted stuff are valid again */
  2318.                 StuffWasInserted = Edit->Undo.DeletedValidFlag;
  2319.                 if (StuffWasInserted)
  2320.                     {
  2321.                         SetTextEditInsertionPoint(Edit,Edit->Undo.DeletedLine,
  2322.                             Edit->Undo.DeletedChar);
  2323.                         if (!TextViewInsertBlock(Edit->View,Edit->Undo.DeletedStuff))
  2324.                             {
  2325.                                 TextEditPurgeUndoRecord(Edit); /* avoid inconsistencies */
  2326.                                 return False;
  2327.                             }
  2328.                         /* this might have failed & nothing (or part) was inserted.  This */
  2329.                         /* will end up selecting what was inserted, but the rest is lost. */
  2330.                         SetTextEditSelection(Edit,Edit->Undo.DeletedLine,
  2331.                             Edit->Undo.DeletedChar,GetTextEditSelectEndLine(Edit),
  2332.                             GetTextEditSelectEndCharPlusOne(Edit));
  2333.                         DisposeTextStorage(Edit->Undo.DeletedStuff);
  2334.                     }
  2335.                 /* now we have to pull a switcheroo */
  2336.                 /* first, the deleted stuff becomes what we removed above */
  2337.                 Edit->Undo.DeletedValidFlag = OldStuffValid;
  2338.                 if (OldStuffValid)
  2339.                     {
  2340.                         Edit->Undo.DeletedLine = OldAddedLine;
  2341.                         Edit->Undo.DeletedChar = OldAddedChar;
  2342.                         Edit->Undo.DeletedStuff = OldAddedStuff;
  2343.                     }
  2344.                 /* now, update the inserted stuff */
  2345.                 Edit->Undo.ReplacingValidFlag = StuffWasInserted;
  2346.                 if (StuffWasInserted)
  2347.                     {
  2348.                         Edit->Undo.ReplacingStartLine = GetTextEditSelectStartLine(Edit);
  2349.                         Edit->Undo.ReplacingStartChar = GetTextEditSelectStartChar(Edit);
  2350.                         Edit->Undo.ReplacingEndLine = GetTextEditSelectEndLine(Edit);
  2351.                         Edit->Undo.ReplacingEndCharPlusOne
  2352.                             = GetTextEditSelectEndCharPlusOne(Edit);
  2353.                     }
  2354.             }
  2355.         return True;
  2356.     }
  2357.  
  2358.  
  2359. /* this routine is called when the user has pressed delete and deleted */
  2360. /* a line break.  If this deletion is a logical extention of the existing */
  2361. /* deleted range, AND there is no insertion, then adjust the deleted range. */
  2362. /* if there is an insertion AND the deletion is of the last character of */
  2363. /* the insertion, then adjust the insertion.  If it is neither, purge the */
  2364. /* undo information and start a new deletion record. */
  2365. static void                TextEditRememberUndoDeletedCR(TextEditRec* Edit)
  2366.     {
  2367.         CheckPtrExistence(Edit);
  2368.         ERROR(TextEditIsThereValidSelection(Edit),PRERR(AllowResume,
  2369.             "TextEditRememberUndoDeletedCR:  Why is there a valid selection?"));
  2370.         if (!Edit->Undo.CanUndoFlag)
  2371.             {
  2372.                 Edit->Undo.CanUndoFlag = True;
  2373.                 Edit->Undo.DeletedValidFlag = False;
  2374.                 Edit->Undo.ReplacingValidFlag = False;
  2375.             }
  2376.         if (Edit->Undo.ReplacingValidFlag)
  2377.             {
  2378.                 /* valid replacement, but is this deletion part of it? */
  2379.                 /* if it is, then the current insertion point will be on the line above */
  2380.                 /* the last insertion line; the last insertion char will be 0. */
  2381.                 if ((Edit->Undo.ReplacingEndLine - 1 == GetTextEditSelectStartLine(Edit))
  2382.                     && (Edit->Undo.ReplacingEndCharPlusOne == 0))
  2383.                     {
  2384.                         /* yup, just roll back the insertion range */
  2385.                         /* but we have to make sure we aren't deleting past the beginning */
  2386.                         /* of the insertion range. */
  2387.                         Edit->Undo.ReplacingEndLine -= 1;
  2388.                         Edit->Undo.ReplacingEndCharPlusOne = GetTextEditSelectStartChar(Edit);
  2389.                         if ((Edit->Undo.ReplacingEndLine < Edit->Undo.ReplacingStartLine)
  2390.                             || ((Edit->Undo.ReplacingEndLine == Edit->Undo.ReplacingStartLine)
  2391.                             && (Edit->Undo.ReplacingEndCharPlusOne
  2392.                             < Edit->Undo.ReplacingStartChar)))
  2393.                             {
  2394.                                 /* we just deleted a character not in the range.  Start a new */
  2395.                                 /* deletion */
  2396.                                 goto DeletingSomewhereElsePoint;
  2397.                             }
  2398.                     }
  2399.                  else
  2400.                     {
  2401.                         TextStorageRec*                NewDeleter;
  2402.  
  2403.                         /* otherwise, there was an insertion, but this isn't part of it, which */
  2404.                         /* means the user moved the cursor before deleting.  Thus we start a */
  2405.                         /* new deletion */
  2406.                      DeletingSomewhereElsePoint:
  2407.                         TextEditPurgeUndoRecord(Edit);
  2408.                         Edit->Undo.CanUndoFlag = True;
  2409.                         Edit->Undo.DeletedValidFlag = True;
  2410.                         Edit->Undo.ReplacingValidFlag = False;
  2411.                         NewDeleter = NewTextStorage();
  2412.                         if (NewDeleter == NIL)
  2413.                             {
  2414.                                 /* woah, major error -- user just won't be able to undo! */
  2415.                                 TextEditPurgeUndoRecord(Edit);
  2416.                                 return;
  2417.                             }
  2418.                         Edit->Undo.DeletedStuff = NewDeleter;
  2419.                         /* deleter should contain a new line */
  2420.                         TextStorageInsertLine(NewDeleter,0);
  2421.                         Edit->Undo.DeletedLine = GetTextEditSelectStartLine(Edit);
  2422.                         Edit->Undo.DeletedChar = GetTextEditSelectStartChar(Edit);
  2423.                     }
  2424.             }
  2425.          else
  2426.             {
  2427.                 /* there is no insertion, but is there a deletion? */
  2428.                 if (Edit->Undo.DeletedValidFlag)
  2429.                     {
  2430.                         /* there's a deletion, but was this part of it? */
  2431.                         /* to be part of it, the cursor has to be on the line before */
  2432.                         /* the current deletion line and the deletion char has to be 0 */
  2433.                         if ((Edit->Undo.DeletedLine - 1 == GetTextEditSelectStartLine(Edit))
  2434.                             && (Edit->Undo.DeletedChar == 0))
  2435.                             {
  2436.                                 /* yup.  Just insert a blank line at the beginning of the */
  2437.                                 /* deletion thing and adjust the point.  If a memory out error */
  2438.                                 /* occurs, the data will be mangled -- too bad for the user */
  2439.                                 TextStorageInsertLine(Edit->Undo.DeletedStuff,0);
  2440.                                 Edit->Undo.DeletedLine -= 1;
  2441.                                 Edit->Undo.DeletedChar = GetTextEditSelectStartChar(Edit);
  2442.                             }
  2443.                          else
  2444.                             {
  2445.                                 /* nope, we are deleting somewhere else */
  2446.                                 goto DeletingSomewhereElsePoint;
  2447.                             }
  2448.                     }
  2449.                  else
  2450.                     {
  2451.                         /* no insertion and no deletion?  Well, let's just start one */
  2452.                         /* on our own. */
  2453.                         goto DeletingSomewhereElsePoint;
  2454.                     }
  2455.             }
  2456.     }
  2457.  
  2458.  
  2459. /* this routine is called when the user has pressed delete and deleted */
  2460. /* a normal character.  If this deletion is a logical extention of the existing */
  2461. /* deleted range, AND there is no insertion, then adjust the deleted range. */
  2462. /* if there is an insertion AND the deletion is of the last character of */
  2463. /* the insertion, then adjust the insertion.  If it is neither, purge the */
  2464. /* undo information and start a new deletion record. */
  2465. static void                TextEditRememberUndoDeletedChar(TextEditRec* Edit, char What)
  2466.     {
  2467.         CheckPtrExistence(Edit);
  2468.         ERROR(TextEditIsThereValidSelection(Edit),PRERR(AllowResume,
  2469.             "TextEditRememberUndoDeletedChar:  Why is there a valid selection?"));
  2470.         if (!Edit->Undo.CanUndoFlag)
  2471.             {
  2472.                 Edit->Undo.CanUndoFlag = True;
  2473.                 Edit->Undo.DeletedValidFlag = False;
  2474.                 Edit->Undo.ReplacingValidFlag = False;
  2475.             }
  2476.         if (Edit->Undo.ReplacingValidFlag)
  2477.             {
  2478.                 /* valid replacement, but is this deletion part of it? */
  2479.                 /* if it is, then the current insertion point will be on the same line */
  2480.                 /* as the last insertion line, and the insertion char will be the last */
  2481.                 /* insertion char - 1 */
  2482.                 if ((Edit->Undo.ReplacingEndLine == GetTextEditSelectStartLine(Edit))
  2483.                     && (Edit->Undo.ReplacingEndCharPlusOne - 1
  2484.                     == GetTextEditSelectStartChar(Edit)))
  2485.                     {
  2486.                         /* yup, just roll back the insertion range */
  2487.                         Edit->Undo.ReplacingEndCharPlusOne -= 1;
  2488.                         if ((Edit->Undo.ReplacingEndLine < Edit->Undo.ReplacingStartLine)
  2489.                             || ((Edit->Undo.ReplacingEndLine == Edit->Undo.ReplacingStartLine)
  2490.                             && (Edit->Undo.ReplacingEndCharPlusOne
  2491.                             < Edit->Undo.ReplacingStartChar)))
  2492.                             {
  2493.                                 /* we just deleted a character not in the range.  Start a new */
  2494.                                 /* deletion */
  2495.                                 goto DeletingSomewhereElsePoint;
  2496.                             }
  2497.                     }
  2498.                  else
  2499.                     {
  2500.                         TextStorageRec*        NewDeleter;
  2501.                         char*                            Line;
  2502.                         char*                            LineCopy;
  2503.                         char                            Buffer[1];
  2504.  
  2505.                         /* otherwise, there was an insertion, but this isn't part of it, which */
  2506.                         /* means the user moved the cursor before deleting.  Thus we start a */
  2507.                         /* new deletion */
  2508.                      DeletingSomewhereElsePoint:
  2509.                         TextEditPurgeUndoRecord(Edit);
  2510.                         Edit->Undo.CanUndoFlag = True;
  2511.                         Edit->Undo.DeletedValidFlag = True;
  2512.                         Edit->Undo.ReplacingValidFlag = False;
  2513.                         NewDeleter = NewTextStorage();
  2514.                         if (NewDeleter == NIL)
  2515.                             {
  2516.                                 /* woah, major error -- user just won't be able to undo! */
  2517.                                 TextEditPurgeUndoRecord(Edit);
  2518.                                 return;
  2519.                             }
  2520.                         Edit->Undo.DeletedStuff = NewDeleter;
  2521.                         /* deleter should contain the character */
  2522.                         Line = TextStorageGetLineCopy(Edit->Undo.DeletedStuff,0);
  2523.                         if (Line != NIL)
  2524.                             {
  2525.                                 Buffer[0] = What;
  2526.                                 LineCopy = InsertBlockIntoBlockCopy(Line,Buffer,0,1);
  2527.                                 if (LineCopy != NIL)
  2528.                                     {
  2529.                                         TextStorageChangeLine(Edit->Undo.DeletedStuff,0,LineCopy);
  2530.                                         ReleasePtr(LineCopy);
  2531.                                     }
  2532.                                 ReleasePtr(Line);
  2533.                             }
  2534.                         Edit->Undo.DeletedLine = GetTextEditSelectStartLine(Edit);
  2535.                         Edit->Undo.DeletedChar = GetTextEditSelectStartChar(Edit);
  2536.                     }
  2537.             }
  2538.          else
  2539.             {
  2540.                 /* there is no insertion, but is there a deletion? */
  2541.                 if (Edit->Undo.DeletedValidFlag)
  2542.                     {
  2543.                         /* there's a deletion, but was this part of it? */
  2544.                         /* to be part of it, the cursor has to be on the same line as */
  2545.                         /* the current deletion line and the deletion char has to be */
  2546.                         /* the cursor char + 1 */
  2547.                         if ((Edit->Undo.DeletedLine == GetTextEditSelectStartLine(Edit))
  2548.                             && (Edit->Undo.DeletedChar - 1 == GetTextEditSelectStartChar(Edit)))
  2549.                             {
  2550.                                 char*                    Line;
  2551.                                 char*                    LineCopy;
  2552.                                 char                    Buffer[1];
  2553.  
  2554.                                 /* yup.  Just insert the character at the beginning of the */
  2555.                                 /* deletion thing and adjust the point. */
  2556.                                 Line = TextStorageGetLineCopy(Edit->Undo.DeletedStuff,0);
  2557.                                 if (Line != NIL)
  2558.                                     {
  2559.                                         Buffer[0] = What;
  2560.                                         LineCopy = InsertBlockIntoBlockCopy(Line,Buffer,0,1);
  2561.                                         if (LineCopy != NIL)
  2562.                                             {
  2563.                                                 TextStorageChangeLine(Edit->Undo.DeletedStuff,0,LineCopy);
  2564.                                                 ReleasePtr(LineCopy);
  2565.                                             }
  2566.                                         ReleasePtr(Line);
  2567.                                     }
  2568.                                 Edit->Undo.DeletedChar -= 1;
  2569.                             }
  2570.                          else
  2571.                             {
  2572.                                 /* nope, we are deleting somewhere else */
  2573.                                 goto DeletingSomewhereElsePoint;
  2574.                             }
  2575.                     }
  2576.                  else
  2577.                     {
  2578.                         /* no insertion and no deletion?  Well, let's just start one */
  2579.                         /* on our own. */
  2580.                         goto DeletingSomewhereElsePoint;
  2581.                     }
  2582.             }
  2583.     }
  2584.  
  2585.  
  2586. #define OpenParen '('
  2587. #define CloseParen ')'
  2588. #define OpenBrace '{'
  2589. #define CloseBrace '}'
  2590. #define OpenBracket '['
  2591. #define CloseBracket ']'
  2592. #define MaxStackSize (1024)
  2593.  
  2594.  
  2595. /* extend the current selection to show balanced parentheses, or beep if */
  2596. /* the parentheses are not balanced */
  2597. void                            TextEditBalanceParens(TextEditRec* Edit)
  2598.     {
  2599.         char                    Form;
  2600.         char                    Stack[MaxStackSize];
  2601.         long                    StackIndex;
  2602.         long                    BackLine;
  2603.         long                    BackChar;
  2604.         long                    ForwardLine;
  2605.         long                    ForwardChar;
  2606.         char*                    Line;
  2607.  
  2608.         CheckPtrExistence(Edit);
  2609.         BackLine = GetTextEditSelectStartLine(Edit);
  2610.         BackChar = GetTextEditSelectStartChar(Edit);
  2611.         ForwardLine = GetTextEditSelectEndLine(Edit);
  2612.         ForwardChar = GetTextEditSelectEndCharPlusOne(Edit);
  2613.         if ((BackLine == ForwardLine) && (BackChar == ForwardChar))
  2614.             {
  2615.                 /* just an insertion point.  In this case, if it's like this: */
  2616.                 /* (...)|  or like this:  |(...), then the group immediately */
  2617.                 /* next to the insertion point should be selected */
  2618.                 Line = GetTextEditLine(Edit,BackLine);
  2619.                 if (Line == NIL)
  2620.                     {
  2621.                         return;
  2622.                     }
  2623.                 if (BackChar > 0)
  2624.                     {
  2625.                         if ((Line[BackChar - 1] == CloseParen)
  2626.                             || (Line[BackChar - 1] == CloseBrace)
  2627.                             || (Line[BackChar - 1] == CloseBracket))
  2628.                             {
  2629.                                 /* move insertion point left */
  2630.                                 BackChar -= 1;
  2631.                                 ForwardChar -= 1;
  2632.                                 goto InitialSetupSkipOutPoint;
  2633.                             }
  2634.                     }
  2635.                 if (BackChar < PtrSize(Line)/*no -1*/)
  2636.                     {
  2637.                         /* notice we don't use BackChar + 1 here, because the insertion point */
  2638.                         /* is BETWEEN two characters (the x-1 and the x character) */
  2639.                         if ((Line[BackChar] == OpenParen)
  2640.                             || (Line[BackChar] == OpenBrace)
  2641.                             || (Line[BackChar] == OpenBracket))
  2642.                             {
  2643.                                 /* move insertion point right */
  2644.                                 BackChar += 1;
  2645.                                 ForwardChar += 1;
  2646.                                 goto InitialSetupSkipOutPoint;
  2647.                                 /* BackChar and ForwardChar could be equal to PtrSize(Line) */
  2648.                                 /* after this. */
  2649.                             }
  2650.                     }
  2651.                 /* jump here when the insertion point has been adjusted */
  2652.              InitialSetupSkipOutPoint:
  2653.                 ReleasePtr(Line);
  2654.             }
  2655.         StackIndex = 0;
  2656.         while (BackLine >= 0)
  2657.             {
  2658.                 Line = GetTextEditLine(Edit,BackLine);
  2659.                 if (Line == NIL)
  2660.                     {
  2661.                         return;
  2662.                     }
  2663.                 while (BackChar > 0)
  2664.                     {
  2665.                         char                    C;
  2666.  
  2667.                         BackChar -= 1;
  2668.                         PRNGCHK(Line,&(Line[BackChar]),sizeof(char));
  2669.                         C = Line[BackChar];
  2670.                         if ((C == CloseParen) || (C == CloseBrace) || (C == CloseBracket))
  2671.                             {
  2672.                                 /* we ran into the trailing end of a grouping, so we increment */
  2673.                                 /* the count and look for the beginning end. */
  2674.                                 Stack[StackIndex] = C;
  2675.                                 StackIndex += 1;
  2676.                                 if (StackIndex >= MaxStackSize)
  2677.                                     {
  2678.                                         /* expression is too complex to be analyzed */
  2679.                                         ErrorBeep();
  2680.                                         ReleasePtr(Line);
  2681.                                         return;
  2682.                                     }
  2683.                             }
  2684.                         else if ((C == OpenParen) || (C == OpenBrace) || (C == OpenBracket))
  2685.                             {
  2686.                                 /* here we found a beginning end of some sort.  If it's the */
  2687.                                 /* beginning of a group we aren't in, then check to see that */
  2688.                                 /* it matches */
  2689.                                 if (StackIndex == 0)
  2690.                                     {
  2691.                                         /* there are no other blocks we had to go through so this */
  2692.                                         /* begin must enclose us */
  2693.                                         Form = C;
  2694.                                         ReleasePtr(Line);
  2695.                                         goto ForwardScanEntryPoint;
  2696.                                     }
  2697.                                 StackIndex -= 1;
  2698.                                 if (((C == OpenParen) && (Stack[StackIndex] == CloseParen))
  2699.                                     || ((C == OpenBrace) && (Stack[StackIndex] == CloseBrace))
  2700.                                     || ((C == OpenBracket) && (Stack[StackIndex] == CloseBracket)))
  2701.                                     {
  2702.                                         /* good */
  2703.                                     }
  2704.                                  else
  2705.                                     {
  2706.                                         /* bad */
  2707.                                         ReleasePtr(Line);
  2708.                                         ErrorBeep();
  2709.                                         return;
  2710.                                     }
  2711.                             }
  2712.                     }
  2713.                 BackLine -= 1;
  2714.                 ReleasePtr(Line);
  2715.                 if (BackLine >= 0)
  2716.                     {
  2717.                         Line = GetTextEditLine(Edit,BackLine);
  2718.                         BackChar = PtrSize(Line);
  2719.                         ReleasePtr(Line);
  2720.                     }
  2721.             }
  2722.         ErrorBeep();
  2723.         return;
  2724.      ForwardScanEntryPoint:
  2725.         StackIndex = 0;
  2726.         while (ForwardLine < GetTextEditNumLines(Edit))
  2727.             {
  2728.                 Line = GetTextEditLine(Edit,ForwardLine);
  2729.                 if (Line == NIL)
  2730.                     {
  2731.                         return;
  2732.                     }
  2733.                 while (ForwardChar < PtrSize(Line))
  2734.                     {
  2735.                         char                    C;
  2736.  
  2737.                         PRNGCHK(Line,&(Line[ForwardChar]),sizeof(char));
  2738.                         C = Line[ForwardChar];
  2739.                         ForwardChar += 1;
  2740.                         if ((C == OpenParen) || (C == OpenBrace) || (C == OpenBracket))
  2741.                             {
  2742.                                 /* we ran into the leading end of a grouping, so we increment */
  2743.                                 /* the count and look for the end end. */
  2744.                                 Stack[StackIndex] = C;
  2745.                                 StackIndex += 1;
  2746.                                 if (StackIndex >= MaxStackSize)
  2747.                                     {
  2748.                                         /* expression is too complex to be analyzed */
  2749.                                         ErrorBeep();
  2750.                                         ReleasePtr(Line);
  2751.                                         return;
  2752.                                     }
  2753.                             }
  2754.                         else if ((C == CloseParen) || (C == CloseBrace) || (C == CloseBracket))
  2755.                             {
  2756.                                 /* here we found an end of some sort.  If it's the */
  2757.                                 /* end of a group we aren't in, then check to see that */
  2758.                                 /* it matches */
  2759.                                 if (StackIndex == 0)
  2760.                                     {
  2761.                                         /* there are no other blocks we had to go through so this */
  2762.                                         /* end must enclose us */
  2763.                                         ReleasePtr(Line);
  2764.                                         if (((Form == OpenParen) && (C == CloseParen))
  2765.                                             || ((Form == OpenBrace) && (C == CloseBrace))
  2766.                                             || ((Form == OpenBracket) && (C == CloseBracket)))
  2767.                                             {
  2768.                                                 SetTextEditSelection(Edit,BackLine,BackChar,
  2769.                                                     ForwardLine,ForwardChar);
  2770.                                                 TextEditShowSelection(Edit);
  2771.                                                 return;
  2772.                                             }
  2773.                                          else
  2774.                                             {
  2775.                                                 ErrorBeep();
  2776.                                                 return;
  2777.                                             }
  2778.                                     }
  2779.                                 StackIndex -= 1;
  2780.                                 if (((C == CloseParen) && (Stack[StackIndex] == OpenParen))
  2781.                                     || ((C == CloseBrace) && (Stack[StackIndex] == OpenBrace))
  2782.                                     || ((C == CloseBracket) && (Stack[StackIndex] == OpenBracket)))
  2783.                                     {
  2784.                                         /* good */
  2785.                                     }
  2786.                                  else
  2787.                                     {
  2788.                                         /* bad */
  2789.                                         ReleasePtr(Line);
  2790.                                         ErrorBeep();
  2791.                                         return;
  2792.                                     }
  2793.                             }
  2794.                     }
  2795.                 ForwardLine += 1;
  2796.                 ReleasePtr(Line);
  2797.                 ForwardChar = 0;
  2798.             }
  2799.         ErrorBeep();
  2800.         return;
  2801.     }
  2802.  
  2803.  
  2804. /* find the specified search string starting at the current selection. */
  2805. MyBoolean                    TextEditFindAgain(TextEditRec* Edit, char* SearchString)
  2806.     {
  2807.         long                        LineScan;
  2808.         long                        KeyLength;
  2809.         long                        LineLimit;
  2810.         long                        ColumnScan;
  2811.         long                        ElapsedLineCount;
  2812.  
  2813.         CheckPtrExistence(Edit);
  2814.         CheckPtrExistence(SearchString);
  2815.         KeyLength = PtrSize(SearchString);
  2816.         LineLimit = GetTextEditNumLines(Edit);
  2817.         LineScan = GetTextEditSelectStartLine(Edit);
  2818.         ElapsedLineCount = 0;
  2819.         if (TextEditIsThereValidSelection(Edit))
  2820.             {
  2821.                 /* if there is a selection, assume it's from a previous search.  we need */
  2822.                 /* to have + 1 so we don't find what we found again. */
  2823.                 ColumnScan = GetTextEditSelectStartChar(Edit) + 1;
  2824.             }
  2825.          else
  2826.             {
  2827.                 /* if no selection, start search at current position.  this lets us find */
  2828.                 /* patterns at the very beginning of the file. */
  2829.                 ColumnScan = GetTextEditSelectStartChar(Edit);
  2830.             }
  2831.         while (LineScan < LineLimit)
  2832.             {
  2833.                 char*                        TestLine;
  2834.                 long                        ColumnLimit;
  2835.  
  2836.                 TestLine = GetTextEditLine(Edit,LineScan);
  2837.                 if (TestLine == NIL)
  2838.                     {
  2839.                         return False;
  2840.                     }
  2841.                 ColumnLimit = PtrSize(TestLine) - KeyLength;
  2842.                 while (ColumnScan <= ColumnLimit)
  2843.                     {
  2844.                         if (MemEquNoCase(&(SearchString[0]),&(TestLine[ColumnScan]),KeyLength))
  2845.                             {
  2846.                                 /* found it! */
  2847.                                 SetTextEditSelection(Edit,LineScan,ColumnScan,
  2848.                                     LineScan,ColumnScan + KeyLength);
  2849.                                 TextEditShowSelection(Edit);
  2850.                                 ReleasePtr(TestLine);
  2851.                                 return True;
  2852.                             }
  2853.                         ColumnScan += 1;
  2854.                     }
  2855.                 ReleasePtr(TestLine);
  2856.                 LineScan += 1;
  2857.                 ElapsedLineCount += 1;
  2858.                 ColumnScan = 0;
  2859.                 if (RelinquishCPUJudiciouslyCheckCancel())
  2860.                     {
  2861.                         /* cancelling */
  2862.                         return False;
  2863.                     }
  2864.             }
  2865.         ErrorBeep(); /* selection not found */
  2866.         return False;
  2867.     }
  2868.  
  2869.  
  2870. /* see if the specified location is in the text edit box */
  2871. MyBoolean                    TextEditHitTest(TextEditRec* Edit, OrdType X, OrdType Y)
  2872.     {
  2873.         CheckPtrExistence(Edit);
  2874.         return (X >= Edit->X) && (Y >= Edit->Y) && (X < Edit->X + Edit->TotalWidth)
  2875.             && (Y < Edit->Y + Edit->TotalHeight);
  2876.     }
  2877.  
  2878.  
  2879. /* see if the specified location is in the text edit area of the box (not the */
  2880. /* scrollbars).  This is used for deciding whether the mouse should be an Ibeam */
  2881. /* or an arrow. */
  2882. MyBoolean                    TextEditIBeamTest(TextEditRec* Edit, OrdType X, OrdType Y)
  2883.     {
  2884.         CheckPtrExistence(Edit);
  2885.         return (X >= Edit->X) && (Y >= Edit->Y)
  2886.             && (X < Edit->X + GetTextViewWidth(Edit->View))
  2887.             && (Y < Edit->Y + GetTextViewHeight(Edit->View));
  2888.     }
  2889.