Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

ODEdit.c 97.6 KB
Newer Older
rswindell's avatar
rswindell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* OpenDoors Online Software Programming Toolkit
 * (C) Copyright 1991 - 1999 by Brian Pirie.
 *
 * Oct-2001 door32.sys/socket modifications by Rob Swindell (www.synchro.net)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
Deucе's avatar
Deucе committed
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
rswindell's avatar
rswindell committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 *
 *
 *        File: ODEdit.c
 *
 * Description: Implementation of the OpenDoors multi-line editor, which
 *              allows the user to edit strings which may span many lines.
 *              Provides standard text editor features, such as word wrap.
 *
 *   Revisions: Date          Ver   Who  Change
 *              ---------------------------------------------------------------
 *              Dec 07, 1995  6.00  BP   Created.
 *              Dec 12, 1995  6.00  BP   Added entry, exit and kernel macros.
 *              Dec 30, 1995  6.00  BP   Added ODCALL for calling convention.
 *              Jan 04, 1996  6.00  BP   Use od_get_input().
 *              Jan 12, 1996  6.00  BP   Claim exclusive use of arrow keys.
 *              Jan 31, 1996  6.00  BP   Added timeout for od_get_input().
 *              Feb 08, 1996  6.00  BP   Finished implementation details.
 *              Feb 13, 1996  6.00  BP   Added od_get_input() flags parameter.
 *              Feb 16, 1996  6.00  BP   New trans. size estimation heuristics.
 *              Feb 19, 1996  6.00  BP   Changed version number to 6.00.
 *              Feb 24, 1996  6.00  BP   Fixed garbage on [Enter] after w-wrap.
 *              Mar 03, 1996  6.10  BP   Begin version 6.10.
 *              Mar 13, 1996  6.10  BP   Restore cursor position after menu.
 *              Mar 19, 1996  6.10  BP   MSVC15 source-level compatibility.
 *              Jun 08, 1996  6.10  BP   Added cast in call to alloc function.
 *              Oct 19, 2001  6.20  RS   Eliminated MSVC 6.0 warning.
45
 *              Aug 10, 2003  6.23  SH   *nix support
rswindell's avatar
rswindell committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
 */

#define BUILDING_OPENDOORS

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "OpenDoor.h"
#include "ODTypes.h"
#include "ODGen.h"
#include "ODCore.h"
#include "ODKrnl.h"
#include "ODStat.h"
#include "ODCom.h"
#include "ODScrn.h"


/* ========================================================================= */
/* Misc. definitions.                                                        */
/* ========================================================================= */

/* Macros used by this module. */
#define IS_EOL_CHAR(ch)       ((ch) == '\n' || (ch) == '\r' || (ch) == '\0')

/* Configurable constants. */
#define LINE_ARRAY_GROW_SIZE     20
#define BUFFER_GROW_SIZE         4096
#define ANSI_SCROLL_DISTANCE     7
#define PRE_DRAIN_TIME           10000
#define MAX_TAB_STOP_SIZE        8
#define DEFAULT_TAB_STOP_SIZE    8
#define DEFAULT_LINE_BREAK       "\n"

/* Other manifest constants. */
#define REDRAW_NO_BOUNDARY       0xffff

/* Default editor options. */
static tODEditOptions ODEditOptionsDefault =
{
   1, 1, 80, 23,
   FORMAT_PARAGRAPH_BREAKS,
   NULL,
   NULL,
   EFLAG_NORMAL,
};



/* ========================================================================= */
/* Multiline editor instance structure.                                      */
/* ========================================================================= */

typedef struct
{
   char *pszEditBuffer;
   UINT unBufferSize;
   tODEditOptions *pUserOptions;
   UINT unCurrentLine;
   UINT unCurrentColumn;
   UINT unLineScrolledToTop;
   UINT unAreaWidth;
   UINT unAreaHeight;
   char **papchStartOfLine;
   UINT unLineArraySize;
   UINT unLinesInBuffer;
   BOOL bInsertMode;
   UINT unTabStopSize;
   UINT unScrollDistance;
   char *pszLineBreak;
   char *pszParagraphBreak;
   BOOL bWordWrapLongLines;
   void *pRememberBuffer;
} tEditInstance;



/* ========================================================================= */
/* Editor function prototypes.                                               */
/* ========================================================================= */

/* High level implementation. */
static BOOL ODEditSetupInstance(tEditInstance *pEditInstance,
   char *pszBufferToEdit, UINT unBufferSize, tODEditOptions *pUserOptions);
static void ODEditRedrawArea(tEditInstance *pEditInstance);
static void ODEditDrawAreaLine(tEditInstance *pEditInstance,
   UINT unAreaLineToDraw);
static INT ODEditMainLoop(tEditInstance *pEditInstance);
static void ODEditGotoPreviousLine(tEditInstance *pEditInstance);
static void ODEditGotoNextLine(tEditInstance *pEditInstance);
static BOOL ODEditScrollArea(tEditInstance *pEditInstance, INT nDistance);
static BOOL ODEditRecommendFullRedraw(tEditInstance *pEditInstance,
   UINT unEstPartialRedrawBytes, BOOL bDefault);
static UINT ODEditEstDrawBytes(tEditInstance *pEditInstance,
   UINT unStartRedrawLine, UINT unStartRedrawColumn, UINT unFinishRedrawLine,
   UINT unFinishRedrawColumn);
static UINT ODEditGetCurrentLineInArea(tEditInstance *pEditInstance);
static void ODEditUpdateCursorPos(tEditInstance *pEditInstance);
static void ODEditUpdateCursorIfMoved(tEditInstance *pEditInstance);
static tODResult ODEditEnterText(tEditInstance *pEditInstance,
   char *pszEntered, BOOL bInsertMode);
static void ODEditSetBreakSequence(tEditInstance *pEditInstance,
   char chFirstEOLChar, char chSecondEOLChar);
static BOOL ODEditCursorLeft(tEditInstance *pEditInstance);
static void ODEditDeleteCurrentChar(tEditInstance *pEditInstance);
static void ODEditDeleteCurrentLine(tEditInstance *pEditInstance);
static BOOL ODEditPastEndOfCurLine(tEditInstance *pEditInstance);
static size_t ODEditRememberBufferSize(tEditInstance *pEditInstance);
static void ODEditRememberArea(tEditInstance *pEditInstance,
   void *pRememberedArea);
static void ODEditRedrawChanged(tEditInstance *pEditInstance,
   void *pRememberedArea, UINT unUpperBoundary, UINT unLowerBoundary);
static BOOL ODEditDetermineChanged(tEditInstance *pEditInstance,
   void *pRememberedArea, UINT unUpperBoundary, UINT unLowerBoundary, 
   UINT *punStartRedrawLine, UINT *punStartRedrawColumn,
   UINT *punFinishRedrawLine, UINT *punFinishRedrawColumn);
static void ODEditRedrawSubArea(tEditInstance *pEditInstance,
   UINT unStartRedrawLine, UINT unStartRedrawColumn, UINT unFinishRedrawLine,
   UINT unFinishRedrawColumn);
static void ODEditGetActualCurPos(tEditInstance *pEditInstance,
   UINT *punRow, UINT *punColumn);
static BOOL ODEditIsEOLForMode(tEditInstance *pEditInstance, char chToTest);

/* Low level buffer manipulation functions. */
static BOOL ODEditBufferFormatAndIndex(tEditInstance *pEditInstance);
static UINT ODEditBufferGetLineLength(tEditInstance *pEditInstance,
   UINT unBufferLine);
static UINT ODEditBufferGetTotalLines(tEditInstance *pEditInstance);
static char *ODEditBufferGetCharacter(tEditInstance *pEditInstance,
   UINT unBufferLine, UINT unBufferColumn);
static tODResult ODEditBufferMakeSpace(tEditInstance *pEditInstance,
   UINT unLine, UINT unColumn, UINT unNumChars);
static tODResult ODEditTryToGrow(tEditInstance *pEditInstance,
   UINT unSizeNeeded);



/* ========================================================================= */
/* High level editor implementation.                                         */
/* ========================================================================= */

/* ----------------------------------------------------------------------------
 * od_multiline_edit()
 *
 * Multiline editor function, allows the user to enter or change text that
 * spans multiple lines.
 *
 * Parameters: pszBufferToEdit  - Pointer to '\0'-terminated buffer of text
 *                                to edit.
 *
 *             unBufferSize     - Size of the buffer, in characters.
 *
 *             pEditOptions     - Pointer to a tODEditOptions structure, or
 *                                NULL to use default settings.
 *
 *     Return: An od_multiline_edit()-specific result code.
 */
ODAPIDEF INT ODCALL od_multiline_edit(char *pszBufferToEdit, UINT unBufferSize,
   tODEditOptions *pEditOptions)
{
   tEditInstance EditInstance;
   INT nToReturn;

   /* Log function entry if running in trace mode */
   TRACE(TRACE_API, "od_node_open()");

   /* Initialize OpenDoors if not already done. */
   if(!bODInitialized) od_init();

   OD_API_ENTRY();

   /* Validate parameters. */
   if(pszBufferToEdit == NULL || unBufferSize == 0)
   {
      od_control.od_error = ERR_PARAMETER;
      OD_API_EXIT();
      return(OD_MULTIEDIT_ERROR);
   }

   /* Check the user's terminal supports the required capabilities. */
   if(!(od_control.user_ansi || od_control.user_avatar))
   {
      od_control.od_error = ERR_NOGRAPHICS;
      OD_API_EXIT();
      return(OD_MULTIEDIT_ERROR);
   }

   /* Initialize editor instance information structure. */
   if(!ODEditSetupInstance(&EditInstance, pszBufferToEdit, unBufferSize,
      pEditOptions))
   {
      OD_API_EXIT();
      return(OD_MULTIEDIT_ERROR);
   }

   /* Attempt to build the buffer line index and ensure that the buffer */
   /* conforms to the format specified by the client application.       */
   if(!ODEditBufferFormatAndIndex(&EditInstance))
   {
      od_control.od_error = ERR_MEMORY;
      OD_API_EXIT();
      return(OD_MULTIEDIT_ERROR);
   }

   /* Claim exclusive use of arrow keys. */
   ODStatStartArrowUse();

   /* Ensure that all information in the outbound communications buffer  */
   /* has been sent before starting. This way, we can safely purge the   */
   /* outbound buffer at any time without loosing anything that was sent */
   /* before od_multiline_edit() was called.                             */
   ODWaitDrain(PRE_DRAIN_TIME);

   /* Draw the initial edit area. */
   ODEditRedrawArea(&EditInstance);

   /* Run the main editor loop. */
   nToReturn = ODEditMainLoop(&EditInstance);

   /* Release exclusive use of arrow keys. */
   ODStatEndArrowUse();

   /* Set final information which will be available in the user options */
   /* structure for the client application to access.                   */
   EditInstance.pUserOptions->pszFinalBuffer = EditInstance.pszEditBuffer;
   EditInstance.pUserOptions->unFinalBufferSize = unBufferSize;

   OD_API_EXIT();
   return(nToReturn);
}


/* ----------------------------------------------------------------------------
 * ODEditSetupInstance()                               *** PRIVATE FUNCTION ***
 *
 * Initializes editor instance information structure.
 *
 * Parameters: pEditInstance   - Editor instance information structure.
 *
 *             pszBufferToEdit - Buffer pointer provided by client.
 *
 *             unBufferSize    - Initial buffer size, as specified by the
 *                               client.
 *
 *             pUserOptions    - Editor options specified by the client.
 *
 *     Return: TRUE on success, FALSE on failure. In the case of failure,
 *             od_control.od_error is set appropriately.
 */
static BOOL ODEditSetupInstance(tEditInstance *pEditInstance,
   char *pszBufferToEdit, UINT unBufferSize, tODEditOptions *pUserOptions)
{
   ASSERT(pEditInstance != NULL);
   ASSERT(pszBufferToEdit != NULL);

   /* Setup editor instance structure. */
   pEditInstance->pszEditBuffer = pszBufferToEdit;
   pEditInstance->unBufferSize = unBufferSize;
   if(pUserOptions == NULL)
   {
      /* Edit options is just the defaults. */
      pEditInstance->pUserOptions = &ODEditOptionsDefault;
   }
   else
   {
      /* Edit options are supplied by the user. */
      pEditInstance->pUserOptions = pUserOptions;

      /* Initialize any edit options that the user did not setup. */
      /* Check that edit area has been initialized. */
      if(pUserOptions->nAreaLeft == 0)
      {
         pUserOptions->nAreaLeft = ODEditOptionsDefault.nAreaLeft;
      }
      if(pUserOptions->nAreaRight == 0)
      {
         pUserOptions->nAreaRight = ODEditOptionsDefault.nAreaRight;
      }
        if(pUserOptions->nAreaTop == 0)
      {
         pUserOptions->nAreaTop = ODEditOptionsDefault.nAreaTop;
      }
      if(pUserOptions->nAreaBottom == 0)
      {
         pUserOptions->nAreaBottom = ODEditOptionsDefault.nAreaBottom;
      }
   }
   pEditInstance->unCurrentLine = 0;
   pEditInstance->unCurrentColumn = 0;
   pEditInstance->unLineScrolledToTop = 0;
   pEditInstance->papchStartOfLine = NULL;
   pEditInstance->unLineArraySize = 0;
   pEditInstance->unLinesInBuffer = 0;
   pEditInstance->unAreaWidth = (UINT)pEditInstance->pUserOptions->
      nAreaRight - (UINT)pEditInstance->pUserOptions->nAreaLeft + 1;
   pEditInstance->unAreaHeight = (UINT)pEditInstance->pUserOptions->
      nAreaBottom - (UINT)pEditInstance->pUserOptions->nAreaTop + 1;
   pEditInstance->bInsertMode = TRUE;
   pEditInstance->unTabStopSize = DEFAULT_TAB_STOP_SIZE;

   /* Setup line break and paragraph break sequences, if they can be  */
   /* determined at this point. If they can't be determined, set them */
   /* to NULL.                                                        */
   switch(pEditInstance->pUserOptions->TextFormat)
   {
      case FORMAT_FTSC_MESSAGE:
         /* FTSC compliant messages use \r as a paragraph break, and do */
         /* not have any line break characters.                         */
         pEditInstance->pszLineBreak = "";
         pEditInstance->pszParagraphBreak = "\r";
         break;

      case FORMAT_PARAGRAPH_BREAKS:
         /* Paragraph break mode only inserts CR/LF sequences at the end  */
         /* of a paragrah, and word-wraps the text that forms a paragrah. */
         pEditInstance->pszLineBreak = "";
         pEditInstance->pszParagraphBreak = NULL;
         break;

      case FORMAT_LINE_BREAKS:
      case FORMAT_NO_WORDWRAP:
         /* Line break mode and no word wrap mode both terminate every    */
         /* line of the file with a CR/LF sequence, and have no paragrah  */
         /* terminator. In line break mode, word wrap is enabled, whereas */
         /* it is not in FORMAT_NO_WORDWRAP mode.                         */
         pEditInstance->pszLineBreak = NULL;
         pEditInstance->pszParagraphBreak = "";
         break;

      default:
         /* An invalid text format was specified. */
         od_control.od_error = ERR_PARAMETER;
         return(FALSE);
   }

   /* Determine whether long lines sould be word wrapped or character */
   /* wrapped.                                                        */
   pEditInstance->bWordWrapLongLines = (pEditInstance->pUserOptions->TextFormat
      != FORMAT_NO_WORDWRAP);

   /* Attempt to allocate abuffer for remembered data. */
   pEditInstance->pRememberBuffer =
      malloc(ODEditRememberBufferSize(pEditInstance));
   
   if(pEditInstance->pRememberBuffer == NULL)
   {
      od_control.od_error = ERR_MEMORY;
      return(FALSE);
   }

   /* If AVATAR mode or local mode is active, then scroll up or down one */
   /* line at a time.                                                    */
   if(od_control.user_avatar || od_control.baud == 0)
   {
      pEditInstance->unScrollDistance = 1;
   }
   /* In ANSI mode with a remote connection, scroll multiple lines at a */
   /* time. This is the minimum of the default scroll distance, and the */
   /* current height of the edit area - 1.                              */
   else
   {
      pEditInstance->unScrollDistance = MIN(ANSI_SCROLL_DISTANCE,
         pEditInstance->pUserOptions->nAreaBottom
         - pEditInstance->pUserOptions->nAreaTop);
   }

   /* Return with success. */
   return(TRUE);
}


/* ----------------------------------------------------------------------------
 * ODEditRedrawArea()                                  *** PRIVATE FUNCTION ***
 *
 * Redraws the area of the screen used by the editor.
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *     Return: void
 */
static void ODEditRedrawArea(tEditInstance *pEditInstance)
{
   UINT unAreaLine;

   ASSERT(pEditInstance != NULL);

   ODScrnEnableCaret(FALSE);

   /* First, remove anything that is still in the outbound communications */
   /* buffer, since whatever it was, it will no longer be visible after   */
   /* the screen redraw anyhow.                                           */
   if(od_control.baud != 0) ODComClearOutbound(hSerialPort);

   /* Loop, drawing every line in the edit area. */
   for(unAreaLine = 0; unAreaLine < pEditInstance->unAreaHeight; ++unAreaLine)
   {
      ODEditDrawAreaLine(pEditInstance, unAreaLine);
   }

   ODScrnEnableCaret(TRUE);
}


/* ----------------------------------------------------------------------------
 * ODEditDrawAreaLine()                                *** PRIVATE FUNCTION ***
 *
 * Redraws the specified line in the area of the screen used by the editor.
 *
 * Parameters: pEditInstance    - Editor instance information structure.
 *
 *             unAreaLineToDraw - 0-based line number in the edit area to be
 *                                redrawn.
 *
 *     Return: void
 */
static void ODEditDrawAreaLine(tEditInstance *pEditInstance,
   UINT unAreaLineToDraw)
{
   UINT unBufferLine;
   UINT unLineLength;

   ASSERT(pEditInstance != NULL);
   ASSERT(unAreaLineToDraw >= 0);
   ASSERT(unAreaLineToDraw < pEditInstance->unAreaHeight);

   /* Determine the buffer line that is displayed on this screen line. */   
   unBufferLine = unAreaLineToDraw + pEditInstance->unLineScrolledToTop;

   /* Position the cursor to the beginning of this line. */
   od_set_cursor((UINT)pEditInstance->pUserOptions->nAreaTop
      + unAreaLineToDraw, (UINT)pEditInstance->pUserOptions->nAreaLeft);

   /* If this line is not beyond the end of the buffer. */
   if(unBufferLine < pEditInstance->unLinesInBuffer)
   {
      /* Determine the length of this buffer line. */
      unLineLength = ODEditBufferGetLineLength(pEditInstance, unBufferLine);

      od_disp(ODEditBufferGetCharacter(pEditInstance, unBufferLine, 0),
         unLineLength, TRUE);
   }
487 488 489 490
   else
   {
      unLineLength = 0;
   }
rswindell's avatar
rswindell committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472

   /* If right edge of edit area aligns with the right edge of the screen. */
   if(pEditInstance->pUserOptions->nAreaRight == OD_SCREEN_WIDTH)
   {
      /* Clear the remainder of this line on the screen. */
      od_clr_line();
   }
   else
   {
      /* Place spaces after the end of the current line, up to right edge of */
      /* the edit area.                                                      */
      od_repeat(' ', (BYTE)(pEditInstance->unAreaWidth - unLineLength));
   }
}


/* ----------------------------------------------------------------------------
 * ODEditMainLoop()                                    *** PRIVATE FUNCTION ***
 *
 * Implements the main editor loop, which repeatedly waits for input from the
 * user, until the user chooses to exit.
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *     Return: Value to be returned by od_multiline_edit.
 */
static INT ODEditMainLoop(tEditInstance *pEditInstance)
{
   tODInputEvent InputEvent;

   ASSERT(pEditInstance != NULL);

   /* Set initial cursor position. */
   ODEditUpdateCursorPos(pEditInstance);

   /* Loop, obtaining keystrokes until the user chooses to exit. */
   for(;;)
   {
      od_get_input(&InputEvent, OD_NO_TIMEOUT, GETIN_NORMAL);
      if(InputEvent.EventType == EVENT_EXTENDED_KEY)
      {
         switch(InputEvent.chKeyPress)
         {
            case OD_KEY_UP:
               /* If we aren't at the start of the file, then move to the */
               /* previous line.                                          */
               if(pEditInstance->unCurrentLine > 0)
               {
                  ODEditGotoPreviousLine(pEditInstance);
                  ODEditUpdateCursorPos(pEditInstance);
               }
               break;

            case OD_KEY_DOWN:
               /* If we aren't at the end of the file, then move to the */
               /* next line.                                            */
               if(pEditInstance->unCurrentLine
                  < ODEditBufferGetTotalLines(pEditInstance) - 1)
               {
                  ODEditGotoNextLine(pEditInstance);
                  ODEditUpdateCursorPos(pEditInstance);
               }
               break;

            case OD_KEY_LEFT:
               /* Attempt to move the cursor left. */
               if(ODEditCursorLeft(pEditInstance))
               {
                  /* If it was possible to move the cursor, then update the */
                  /* cursor position on the screen.                         */
                  ODEditUpdateCursorPos(pEditInstance);
               }
               break;

            case OD_KEY_RIGHT:
               /* In word wrap mode, we allow the cursor to move up to the */
               /* end of this line, and then wrap around to the next line. */
               if(pEditInstance->bWordWrapLongLines)
               {
                  if(pEditInstance->unCurrentColumn < ODEditBufferGetLineLength
                     (pEditInstance, pEditInstance->unCurrentLine))
                  {
                     ++pEditInstance->unCurrentColumn;
                     ODEditUpdateCursorPos(pEditInstance);
                  }
                  else if(pEditInstance->unCurrentLine
                     < ODEditBufferGetTotalLines(pEditInstance) - 1)
                  {
                     ODEditGotoNextLine(pEditInstance);
                     pEditInstance->unCurrentColumn = 0;
                     ODEditUpdateCursorPos(pEditInstance);
                  }
               }

               /* In character wrap mode, we allow the cursor to move up to */
               /* the right edge of the edit area.                          */
               else
               {
                  if(pEditInstance->unCurrentColumn
                     < pEditInstance->unAreaWidth - 1)
                  {
                     ++pEditInstance->unCurrentColumn;
                     ODEditUpdateCursorPos(pEditInstance);
                  }
               }
               break;

            case OD_KEY_HOME:
               pEditInstance->unCurrentColumn = 0;
               ODEditUpdateCursorPos(pEditInstance);
               break;

            case OD_KEY_END:
               pEditInstance->unCurrentColumn = ODEditBufferGetLineLength(
                  pEditInstance, pEditInstance->unCurrentLine);
               ODEditUpdateCursorPos(pEditInstance);
               break;

            case OD_KEY_PGUP:
               if(pEditInstance->unLineScrolledToTop > 0)
               {
                  UINT unDistance = MIN(pEditInstance->unAreaHeight - 1,
                     pEditInstance->unLineScrolledToTop);
                  ODEditScrollArea(pEditInstance, -((INT)unDistance));
                  pEditInstance->unCurrentLine -= unDistance;
                  ODEditUpdateCursorPos(pEditInstance);
               }
               else if(pEditInstance->unCurrentLine != 0)
               {
                  pEditInstance->unCurrentLine = 0;
                  ODEditUpdateCursorPos(pEditInstance);
               }
               break;

            case OD_KEY_PGDN:
               if(pEditInstance->unLineScrolledToTop <
                  pEditInstance->unLinesInBuffer - 1)
               {
                  UINT unDistance = MIN(pEditInstance->unAreaHeight - 1,
                     pEditInstance->unLinesInBuffer
                     - pEditInstance->unLineScrolledToTop - 1);
                  ODEditScrollArea(pEditInstance, (INT)unDistance);
                  pEditInstance->unCurrentLine = MIN(
                     pEditInstance->unCurrentLine + unDistance,
                     pEditInstance->unLinesInBuffer - 1);
                  ODEditUpdateCursorPos(pEditInstance);
               }
            break;

            case OD_KEY_INSERT:
               /* If the insert key is pressed, then toggle insert mode. */
               pEditInstance->bInsertMode = !pEditInstance->bInsertMode;
               break;

            case OD_KEY_DELETE:
               /* Delete the character at the current position. */
               
               /* If we are currently past the end of this line. */
               if(ODEditPastEndOfCurLine(pEditInstance))
               {
                  /* Add spaces to this line to fill it up to the current */
                  /* cursor position.                                     */
                  switch(ODEditBufferMakeSpace(pEditInstance,
                     pEditInstance->unCurrentLine,
                     pEditInstance->unCurrentColumn, 0))
                  {
                     case kODRCUnrecoverableFailure:
                        /* If we encountered an unrecoverable failure, then */
                        /* exit from the editor with a memory allocation    */
                        /* error.                                           */
                        od_control.od_error = ERR_MEMORY;
                        return(OD_MULTIEDIT_ERROR);

                     case kODRCSuccess:
                        /* On success, delete the current character. */
                        ODEditDeleteCurrentChar(pEditInstance);
                        break;

                     default:
                        /* On any other failure, just beep and continue. */
                        od_putch('\a');
                  }
               }
               else
               {
                  /* If we aren't pas the end of the line, then just do a */
                  /* simple delete character operation.                   */
                  ODEditDeleteCurrentChar(pEditInstance);
               }
               break;
         }
      }
      else if(InputEvent.EventType == EVENT_CHARACTER)
      {
         if(InputEvent.chKeyPress == 25)
         {
            /* Control-Y (delete line) has been pressed. */
            ODEditDeleteCurrentLine(pEditInstance);
         }
         else if(InputEvent.chKeyPress == 26
            || InputEvent.chKeyPress == 27)
         {
            /* Escape or control-Z has been pressed. */

            /* If a menu callback function has been provided by */
            /* the client, then call it.                        */
            if(pEditInstance->pUserOptions->pfMenuCallback != NULL)
            {
               /* Call the menu callback function. */
               switch((*pEditInstance->pUserOptions->pfMenuCallback)(NULL))
               {
                  case EDIT_MENU_EXIT_EDITOR:
                     return(OD_MULTIEDIT_SUCCESS);

                  case EDIT_MENU_DO_NOTHING:
                     /* Continue in the editor without doing anything. */
                     break;

                  default:
                     ASSERT(FALSE);
               }

               /* If we are continuing, then restore initial cursor pos. */
               ODEditUpdateCursorPos(pEditInstance);
            }
            else
            {
               /* If a menu key callback function has not been provided, */
               /* then we exit the editor unconditionally.               */
               return(OD_MULTIEDIT_SUCCESS);
            }
         }
         else if(InputEvent.chKeyPress == '\b')
         {
            /* Backspace key has been pressed. */

            /* If the cursor is past the end of the line, then we just move */
            /* the cursor left, without deleting any characters. */
            BOOL bDelete = !ODEditPastEndOfCurLine(pEditInstance);

            /* Backup the cursor one space. */
            if(ODEditCursorLeft(pEditInstance))
            {
               /* If there was space to move the cursor back to, then       */
               /* proceed and remove the character at the current position. */
               if(bDelete)
               {
                  ODEditDeleteCurrentChar(pEditInstance);
               }
               else
               {
                  /* In this case, we must still show the new cursor */
                  /* position.                                       */
                  ODEditUpdateCursorPos(pEditInstance);
               }
            }
         }
         else if(InputEvent.chKeyPress == '\t')
         {
            char szTextToAdd[MAX_TAB_STOP_SIZE + 1];
            UINT unTargetColumn;
            UINT unTargetDistance;

            /* A tab key has been entered. */

            /* Determine the column that this will move the cursor to. */
            ASSERT(pEditInstance->unTabStopSize <= MAX_TAB_STOP_SIZE);
            unTargetColumn = ((pEditInstance->unCurrentColumn / pEditInstance->
               unTabStopSize) + 1) * pEditInstance->unTabStopSize;

            /* In insert mode, then insert spaces into the buffer. */
            if(pEditInstance->bInsertMode)
            {
               /* Determine the number of columns that we need to advance in */
               /* order to reach this target column.                         */
               unTargetDistance = unTargetColumn -
                  pEditInstance->unCurrentColumn;
               ASSERT(unTargetDistance <= MAX_TAB_STOP_SIZE);

               /* Translate this to a string with the appropriate number of */
               /* spaces.                                                   */
               memset(szTextToAdd, ' ', unTargetDistance);
               szTextToAdd[unTargetDistance] = '\0';

               /* Add this to the buffer. */
               if(ODEditEnterText(pEditInstance, szTextToAdd, TRUE) ==
                  kODRCUnrecoverableFailure)
               {
                  od_control.od_error = ERR_MEMORY;
                  return(OD_MULTIEDIT_ERROR);
               }
            }

            /* In overwrite mode, then just advance the cursor position. */
            else
            {
               /* Determine the column where the cursor should be wrapped. */
               UINT unWrapColumn = pEditInstance->bWordWrapLongLines ?
                  ODEditBufferGetLineLength(pEditInstance,
                  pEditInstance->unCurrentLine)
                  : pEditInstance->unAreaWidth;

               if(unTargetColumn < unWrapColumn)
               {
                  pEditInstance->unCurrentColumn = unTargetColumn;
                  ODEditUpdateCursorPos(pEditInstance);
               }
               else if(pEditInstance->unCurrentLine
                  < ODEditBufferGetTotalLines(pEditInstance) - 1)
               {
                  ODEditGotoNextLine(pEditInstance);
                  pEditInstance->unCurrentColumn = 0;
                  ODEditUpdateCursorPos(pEditInstance);
               }
            }
         }
         else if(InputEvent.chKeyPress == '\r')
         {
            char *pszTextToAdd;

            /* The enter key has been pressed. In insert mode, or at the end */
            /* of the buffer in overwrite mode, this adds a new line.        */
            if(pEditInstance->bInsertMode || pEditInstance->unCurrentLine
                  >= ODEditBufferGetTotalLines(pEditInstance) - 1)
            {
               if(!pEditInstance->bInsertMode)
               {
                  /* If we are not in insert mode, begin by positioning the */
                  /* cursor at the end of this line.                        */
                  pEditInstance->unCurrentColumn = ODEditBufferGetLineLength(
                     pEditInstance, pEditInstance->unCurrentLine);
               }

               /* Determine the line/paragraph break sequence to use. */
               if(pEditInstance->pszLineBreak != NULL
                  && strlen(pEditInstance->pszLineBreak) > 0)
               {
                  pszTextToAdd = pEditInstance->pszLineBreak;
               }
               else if(pEditInstance->pszParagraphBreak != NULL
                  && strlen(pEditInstance->pszParagraphBreak) > 0)
               {
                  pszTextToAdd = pEditInstance->pszParagraphBreak;
               }
               else
               {
                  pszTextToAdd = DEFAULT_LINE_BREAK;
               }

               /* Insert the sequence into the buffer. */
               if(ODEditEnterText(pEditInstance, pszTextToAdd, TRUE) ==
                  kODRCUnrecoverableFailure)
               {
                  od_control.od_error = ERR_MEMORY;
                  return(OD_MULTIEDIT_ERROR);
               }
            }
            else
            {
               /* Pressing the enter key in overwrite mode just moves the   */
               /* cursor to the beginning of the next line. In other words, */
               /* it is equivalent to pressing Down arrow followed by home. */
               ODEditGotoNextLine(pEditInstance);
               pEditInstance->unCurrentColumn = 0;
               ODEditUpdateCursorPos(pEditInstance);
            }
         }
         else if(InputEvent.chKeyPress >= 32)
         {
            char szTextToAdd[2];
            szTextToAdd[0] = InputEvent.chKeyPress;
            szTextToAdd[1] = '\0';

            /* A valid buffer character has been entered. */
            if(ODEditEnterText(pEditInstance, szTextToAdd,
               (BOOL)(pEditInstance->bInsertMode
               || ODEditPastEndOfCurLine(pEditInstance)))
               == kODRCUnrecoverableFailure)
            {
               od_control.od_error = ERR_MEMORY;
               return(OD_MULTIEDIT_ERROR);
            }
         }
      }
   }
}


/* ----------------------------------------------------------------------------
 * ODEditGotoPreviousLine()                            *** PRIVATE FUNCTION ***
 *
 * Moves the current cursor position to the previous line, scrolling the screen
 * if necessary to keep the cursor visible.
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *     Return: void
 */
static void ODEditGotoPreviousLine(tEditInstance *pEditInstance)
{
   ASSERT(pEditInstance != NULL);

   /* If we are already at the first line, then return without doing */
   /* anything.                                                      */
   if(pEditInstance->unCurrentLine == 0) return;

   /* If cursor is at top of edit area, then scroll area */
   /* first.                                             */
   if(ODEditGetCurrentLineInArea(pEditInstance) == 0)
   {
      ODEditScrollArea(pEditInstance,
         -(INT)(MIN(pEditInstance->unScrollDistance,
         pEditInstance->unCurrentLine)));
   }

   /* Move cursor to previous line. */
   --pEditInstance->unCurrentLine;
}


/* ----------------------------------------------------------------------------
 * ODEditGotoNextLine()                                *** PRIVATE FUNCTION ***
 *
 * Advances the current cursor position to the next line, scrolling the screen
 * if necessary to keep the cursor visible.
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *     Return: void
 */
static void ODEditGotoNextLine(tEditInstance *pEditInstance)
{
   ASSERT(pEditInstance != NULL);

   /* If we are already at the end of the file, then return without */
   /* doing anything.                                               */
   if(pEditInstance->unCurrentLine
      >= ODEditBufferGetTotalLines(pEditInstance) - 1)
   {
      return;
   }

   /* If cursor is at the bottom of the edit area, then scroll area first. */
   if(ODEditGetCurrentLineInArea(pEditInstance)
      == pEditInstance->unAreaHeight - 1)
   {
      ODEditScrollArea(pEditInstance,
         (INT)MIN(pEditInstance->unScrollDistance,
         ODEditBufferGetTotalLines(pEditInstance)
         - pEditInstance->unCurrentLine));
   }

   /* Move cursor to next line. */
   ++pEditInstance->unCurrentLine;
}


/* ----------------------------------------------------------------------------
 * ODEditScrollArea()                                  *** PRIVATE FUNCTION ***
 *
 * Scrolls the edit area up or down the specified distance, redrawing newly
 * "exposed" lines.
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *             nDistance     - Number of lines to scroll, where a positive
 *                             value moves the text upwards, and a negative
 *                             value moves the text down.
 *
 *     Return: FALSE if a full redraw has been performed, TRUE if an efficient
 *             scroll command has been used.
 */
static BOOL ODEditScrollArea(tEditInstance *pEditInstance, INT nDistance)
{
   BOOL bUseScrollCommand = FALSE;
   UINT unAreaLine;
   UINT unBufferLine;
   UINT unFirstAreaLineToDraw;
   UINT unLastAreaLineToDraw;
   UINT unPositiveDistance;

   ASSERT(pEditInstance);

   /* If scroll distance is zero, then we don't need to do anything at all. */
   if(nDistance == 0)
   {
      return(TRUE);
   }
   /* Otherwise, obtain the absolute value of the distance as an unsigned */
   /* integer.                                                            */
   else if(nDistance < 0)
   {
      unPositiveDistance = (UINT)-nDistance;
   }
   else
   {
      unPositiveDistance = (UINT)nDistance;
   }

   /* In AVATAR mode, if more than one of the currently visible lines will */
   /* still be visible after scrolling, then we will consider using the    */
   /* scroll operation.                                                    */
   if(od_control.user_avatar
      && ((INT)pEditInstance->unAreaHeight) - ((INT)unPositiveDistance) > 1)
   {
      /* Even under this situation, we only want to use the scroll operation */
      /* if the amount of data still in the outbound buffer + our estimate   */
      /* of the amount of data that will be sent to perform the scroll       */
      /* operation is less than our estimate of the amount of data that      */
      /* would be sent by a complete screen redraw.                          */
      UINT unEstimatedScrollData = ((pEditInstance->unAreaWidth + 4) *
         unPositiveDistance) + 7;

      if(!ODEditRecommendFullRedraw(pEditInstance, unEstimatedScrollData,
         TRUE))
      {
         bUseScrollCommand = TRUE;
      }
   }

   /* In local mode, we can also use the scroll command for efficiency. */
   if(od_control.baud == 0)
   {
      bUseScrollCommand = TRUE;
   }

   /* Area scroll is achieved by one of two means. We either use the scroll */
   /* command, and then draw just the newly visible lines, or we redraw the */
   /* entire edit area, after removing any data from the outbound           */
   /* communications buffer.                                                */

   if(bUseScrollCommand)
   {
      /* Use the od_scroll() function to scroll the screen contents. */
      od_scroll(pEditInstance->pUserOptions->nAreaLeft,
         pEditInstance->pUserOptions->nAreaTop,
         pEditInstance->pUserOptions->nAreaRight,
         pEditInstance->pUserOptions->nAreaBottom,
         nDistance, SCROLL_NO_CLEAR);

      /* Fill the newly visible lines. First, the portion of the area that   */
      /* requires redrawing is determined, and then a loop redraws the lines */
      /* that must be drawn.                                                 */

      /* If we are moving text upwards, exposing new lines at the bottom of */
      /* the area:                                                          */
      if(nDistance > 0)
      {
         ASSERT(pEditInstance->unLineScrolledToTop + unPositiveDistance
            < pEditInstance->unLinesInBuffer);
         pEditInstance->unLineScrolledToTop += unPositiveDistance;
         unFirstAreaLineToDraw = pEditInstance->unAreaHeight
            - (UINT)unPositiveDistance;
         unLastAreaLineToDraw = pEditInstance->unAreaHeight - 1;
      }
      /* Otherwise, we have moved text downwards, exposing new lines at the */
      /* top of the edit area.                                              */
      else
      {
         ASSERT(pEditInstance->unLineScrolledToTop >= unPositiveDistance);
         pEditInstance->unLineScrolledToTop -= unPositiveDistance;
         unFirstAreaLineToDraw = 0;
         unLastAreaLineToDraw = unPositiveDistance - 1;
      }

      ODScrnEnableCaret(FALSE);

      /* Now, redraw the new lines. */
      unBufferLine = unFirstAreaLineToDraw
         + pEditInstance->unLineScrolledToTop;
      for(unAreaLine = unFirstAreaLineToDraw; unAreaLine <=
         unLastAreaLineToDraw; ++unAreaLine, ++unBufferLine)
      {
         /* Draw the entire line. */
         ODEditDrawAreaLine(pEditInstance, unAreaLine);
      }

      ODScrnEnableCaret(TRUE);
   }

   /* Just redraw the entire edit area. */
   else
   {
      /* Adjust the line number that is scrolled to the top of the screen. */
      if(nDistance > 0)
      {
         pEditInstance->unLineScrolledToTop += unPositiveDistance;
      }
      else
      {
         pEditInstance->unLineScrolledToTop -= unPositiveDistance;
      }

      /* Perform redraw, first purging outbound buffer. */
      ODEditRedrawArea(pEditInstance);
   }

   return(bUseScrollCommand);
}


/* ----------------------------------------------------------------------------
 * ODEditRecommendFullRedraw()                         *** PRIVATE FUNCTION ***
 *
 * Determines whether it would be more efficient to add the specified number
 * of bytes to the outbound buffer as part of an incremental redraw, or if
 * it would be more efficient to just purge the outbound buffer and do a
 * complete redraw of the edit area.
 *
 * Parameters: pEditInstance           - Editor instance information structure.
 *
 *             unEstPartialRedrawBytes - Estimate of the number of bytes that
 *                                       would be transmitted if an incremental
 *                                       redraw is performed.
 *
 *             bDefault                - The default action (TRUE for full
 *                                       redraw, FALSE for incremental) if the
 *                                       number of bytes in the outbound buffer
 *                                       cannot be determined.
 *
 *     Return: TRUE if a full redraw is recommended, FALSE if an the
 *             incremental redraw is recommended.
 */
static BOOL ODEditRecommendFullRedraw(tEditInstance *pEditInstance,
   UINT unEstPartialRedrawBytes, BOOL bDefault)
{
   int nOutboundBufferBytes;
   UINT unEstFullRedrawBytes;

   /* In local mode, just return the default action. */
   if(od_control.baud == 0)
   {
      return(bDefault);
   }

   /* Attempt to obtain the number of bytes in the communications outbound */
   /* buffer. Unfortunately, this information may not be available. For    */
   /* example, FOSSIL drivers will only report whether or not there is     */
   /* still data in the outbound buffer, but not a count of the number of  */
   /* bytes in the buffer. Under such a situation, ODComOutbound() returns */
   /* SIZE_NON_ZERO if there is data in the buffer, and 0 if there is no   */
   /* data in the buffer. This is not a problem under OpenDoor's internal  */
   /* serial I/O code, nor is it a problem under Win32's communications    */
   /* facilities.                                                          */
   ODComOutbound(hSerialPort, &nOutboundBufferBytes);

   if(nOutboundBufferBytes == SIZE_NON_ZERO)
   {
      /* We know that there is data in the outbound buffer, but we don't */
      /* know how much, and so we cannot make a recommendation. Instead, */
      /* the default course of action will be taken.                     */
      return(bDefault);
   }

   /* Estimate the # of bytes required for a full redraw of the edit area. */
   unEstFullRedrawBytes = ODEditEstDrawBytes(pEditInstance, 0,
      0, pEditInstance->unAreaHeight - 1, pEditInstance->unAreaWidth);

   /* Recommend a full redraw if the number of bytes for an incremental */
   /* redraw plus the number of bytes already in the outbound buffer    */
   /* exceed the number of bytes required for a full redraw.            */
   if(unEstPartialRedrawBytes + (UINT)nOutboundBufferBytes
      > unEstFullRedrawBytes)
   {
      return(TRUE);
   }
   else
   {
      return(FALSE);
   }
}


/* ----------------------------------------------------------------------------
 * ODEditEstDrawBytes()                                *** PRIVATE FUNCTION ***
 *
 * Estimates the number of bytes which will be transmitted in order to redraw
 * the specified portion of the edit area.
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *             unStartRedrawLine    - Line of first character to draw.
 *
 *             unStartRedrawColumn  - Column of first character to draw.
 *
 *             unFinishRedrawLine   - Line of last character to draw.
 *
 *             unFinishRedrawColumn - Column after last character to draw.
 *
 *     Return: A rough estimate of the number of bytes required for the redraw.
 */
static UINT ODEditEstDrawBytes(tEditInstance *pEditInstance,
   UINT unStartRedrawLine, UINT unStartRedrawColumn, UINT unFinishRedrawLine,
   UINT unFinishRedrawColumn)
{
   UINT unAreaLine;
   UINT unBufferLine;
   UINT unLineLength;
   UINT unByteTally = 0;

   /* If we are only drawing text on a single line, then estimate is just  */
   /* the distance between the start and finish redraw column. This number */
   /* is precise only if the cursor is already at the location where       */
   /* output is to begin, and the final cursor position is the location    */
   /* where output finishes. This is in fact the situation when the user   */
   /* is entering new text in the middle of the line - the most common     */
   /* situation that will be encountered.                                  */
   if(unStartRedrawLine == unFinishRedrawLine)
   {
      return(unFinishRedrawColumn - unStartRedrawColumn);
   }

   /* If we are drawing text on multiple lines, then inspect the contents */
   /* of those lines to estimate the number of bytes to be transmitted.   */
   for(unAreaLine = unStartRedrawLine,
      unBufferLine = pEditInstance->unLineScrolledToTop + unStartRedrawLine;
      unAreaLine <= unFinishRedrawLine;
      ++unAreaLine, ++unBufferLine)
   {
      /* Determine the length of this line. */
      if(unBufferLine < pEditInstance->unLinesInBuffer)
      {
         unLineLength = ODEditBufferGetLineLength(pEditInstance, unBufferLine);
         if(unAreaLine == unStartRedrawLine)
         {
            unLineLength -= unStartRedrawColumn;
         }
      }
      else
      {
         unLineLength = 0;
      }

      /* Add the number of characters on this line, along with the number of */
      /* bytes required to reposition the cursor and to clear the unused     */
      /* portion of this line to the tally. This assumes that the edit area  */
      /* spans the entire screen.                                            */
      unByteTally += unLineLength + 7;
   }

   return(unByteTally);
}


/* ----------------------------------------------------------------------------
 * ODEditGetCurrentLineInArea()                        *** PRIVATE FUNCTION ***
 *
 * Determines which line of the edit area the cursor is currently located in.
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *     Return: 0-based index of the distance from the top of the edit area
 *             (as specified in the user options structure) where the
 *             cursor is currently located.
 */
static UINT ODEditGetCurrentLineInArea(tEditInstance *pEditInstance)
{
   ASSERT(pEditInstance != NULL);

   return(pEditInstance->unCurrentLine - pEditInstance->unLineScrolledToTop);
}


/* ----------------------------------------------------------------------------
 * ODEditUpdateCursorPos()                             *** PRIVATE FUNCTION ***
 *
 * Unconditionally updates the position of the cursor on the screen to 
 * reflect its actual position. Compare with ODEditUpdateCursorIfMoved().
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *     Return: void
 */
static void ODEditUpdateCursorPos(tEditInstance *pEditInstance)
{
   ASSERT(pEditInstance != NULL);

   /* Reposition the cursor on the screen. */
   od_set_cursor(ODEditGetCurrentLineInArea(pEditInstance)
      + pEditInstance->pUserOptions->nAreaTop,
      pEditInstance->unCurrentColumn + pEditInstance->pUserOptions->nAreaLeft);
}


/* ----------------------------------------------------------------------------
 * ODEditUpdateCursorIfMoved()                         *** PRIVATE FUNCTION ***
 *
 * Updates the position of the cursor on the screen to reflec its actual
 * position only if we belive it isn't already there. Compare with
 * ODEditUpdateCursorPos().
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *     Return: void
 */
static void ODEditUpdateCursorIfMoved(tEditInstance *pEditInstance)
{
   UINT unActualRow;
   UINT unActualColumn;
   ODEditGetActualCurPos(pEditInstance, &unActualRow, &unActualColumn);

   if(!(unActualRow == ODEditGetCurrentLineInArea(pEditInstance)
         + pEditInstance->pUserOptions->nAreaTop
      && unActualColumn == pEditInstance->unCurrentColumn
         + pEditInstance->pUserOptions->nAreaLeft))
   {
      ODEditUpdateCursorPos(pEditInstance);
   }
}


/* ----------------------------------------------------------------------------
 * ODEditEnterText()                                   *** PRIVATE FUNCTION ***
 *
 * Inserts new text at the current cursor position, updating the cursor
 * position accordingly.
 *
 * Parameters: pEditInstance - Editor instance information structure.
 *
 *             pszEntered    - The character(s) to be inserted.
 *
 *     Return: kODRCSuccess on success, kODRCSafeFailure if we were unable to
 *             obtain enough buffer space before making any changes, or
 *             kODRCUnrecoverableFailure if the buffer has been changed, but
 *             there was insufficient memory to re-index the buffer.
 */
static tODResult ODEditEnterText(tEditInstance *pEditInstance,
   char *pszEntered, BOOL bInsertMode)
{
   UINT unNumCharsToAdd;
   char *pch;
   tODResult Result;

   ASSERT(pEditInstance != NULL);
   ASSERT(pszEntered != NULL);

   /* Remember initial edit area contents, to permit selective redraw. */
   ODEditRememberArea(pEditInstance, pEditInstance->pRememberBuffer);

   /* Determine the number of characters that are to be added to the buffer. */
   unNumCharsToAdd = strlen(pszEntered);

   /* Make room in the buffer for the new characters, if needed. */
   if(bInsertMode)
   {
      Result = ODEditBufferMakeSpace(pEditInstance,
         pEditInstance->unCurrentLine, pEditInstance->unCurrentColumn,
         unNumCharsToAdd);

      if(Result != kODRCSuccess)
      {
         /* Beep on failure. */
         od_putch('\a');
         return(Result);
      }
   }

   /* Copy the new characters to the buffer. */
   pch = ODEditBufferGetCharacter(pEditInstance,
      pEditInstance->unCurrentLine, pEditInstance->unCurrentColumn);
   memcpy(pch, pszEntered, unNumCharsToAdd);

   /* Move the cursor position to the end of the newly added text. The  */
   /* cursor position may be temporarily assigned to a position that is */
   /* past the end of the edit area or past the end of the buffer.      */
   for(pch = pszEntered; *pch != '\0'; ++pch)
   {
      if(IS_EOL_CHAR(*pch))
      {
         /* A carriage return character advances the cursor to the */
         /* leftmost column on the next line.                      */
         pEditInstance->unCurrentColumn = 0;
         pEditInstance->unCurrentLine++;

         /* If the next character is a different EOL character, and is */
         /* not the end of the string, then skip that character.       */
         if(IS_EOL_CHAR(pch[1]) && pch[1] != '\0' && pch[1] != *pch)
         {
            ++pch;
         }
      }
      else
      {
         /* All other characters move the cursor ahead one column. */
         pEditInstance->unCurrentColumn++;
      }
   }

   /* Reindex and reformat the buffer based on its new contents. */
   if(!ODEditBufferFormatAndIndex(pEditInstance))
   {
      return(kODRCUnrecoverableFailure);
   }

   /* Check whether the cursor is now positioned past the end of the edit */
   /* area, requiring the edit area to be scrolled up.                    */
   if(ODEditGetCurrentLineInArea(pEditInstance)
      >= pEditInstance->unAreaHeight)
   {
      /* We need to scroll the area accordingly. */

      /* Distance to scroll is maximum of the current single-step scroll */
      /* distance, and the distance that the cursor is positioned below  */
      /* the bottom of the current edit area.                            */
      UINT unScrollDistance = MAX(pEditInstance->unScrollDistance,
         ODEditGetCurrentLineInArea(pEditInstance) -
         pEditInstance->unAreaHeight + 1);

      /* Perform actual scroll operation. */
      if(ODEditScrollArea(pEditInstance, (INT)unScrollDistance))
      {
         /* Entire area wasn't redrawn by operation, so some redrawing */
         /* may still be required, within the area of text that was    */
         /* visible before the scroll operation.                       */
         ODEditRedrawChanged(pEditInstance, pEditInstance->pRememberBuffer,
            0, pEditInstance->unAreaHeight - unScrollDistance);
      }
   }

   /* Perform necessary redrawing and reposition the cursor if needed. */
   ODEditRedrawChanged(pEditInstance, pEditInstance->pRememberBuffer,
      REDRAW_NO_BOUNDARY, REDRAW_NO_BOUNDARY);

   /* Return with success. */
   return(kODRCSuccess);
}


/* ----------------------------------------------------------------------------
 * ODEditSetBreakSequence()                            *** PRIVATE FUNCTION ***
 *
 * If the default line or paragraph break sequence has not yet been set, then
 * this function sets it based on the break sequence that was encountered in
 * the buffer supplied by the client application.
 *
 * Parameters: pEditInstance   - Editor instance information structure.
 *
 *             chFirstEOLChar  - First character in the encountered sequence.
 *
 *             chSecondEOLChar - Second character in the encountered sequence.
 *
 *     Return: void
 */
static void ODEditSetBreakSequence(tEditInstance *pEditInstance,
   char chFirstEOLChar, char chSecondEOLChar)
{
   char *pszSequence;

   ASSERT(pEditInstance != NULL);

   if(pEditInstance->pszParagraphBreak != NULL
      && pEditInstance->pszLineBreak != NULL)
   {
      /* In this situation, both the default line break sequence and default */
      /* paragraph break sequence have been set, so there is nothing for us  */
      /* to do.                                                              */
      return;
   }

   /* Obtain a pointer to the encountered sequence. We want to use a static */
   /* string constant for this, so that the string will continue to exist,  */
   /* in unchanged form.                                                    */
   if(chFirstEOLChar == '\r' && chSecondEOLChar == '\0')
   {
      pszSequence = "\r";
   }
   else if(chFirstEOLChar == '\n' && chSecondEOLChar == '\0')
   {
      pszSequence = "\n";
   }
   else if(chFirstEOLChar == '\n' && chSecondEOLChar == '\r')
   {
      pszSequence = "\n\r";
   }
   else if(chFirstEOLChar == '\r' && chSecondEOLChar == '\n')
   {
      pszSequence = "\r\n";
   }
   else
   {
      /* This should never happen: an invalid end of line sequence was */
      /* passed in.                                                    */
1473
      pszSequence = NULL;
rswindell's avatar
rswindell committed
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627