ODEdit.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  1. /* OpenDoors Online Software Programming Toolkit
  2. * (C) Copyright 1991 - 1999 by Brian Pirie.
  3. *
  4. * Oct-2001 door32.sys/socket modifications by Rob Swindell (www.synchro.net)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library 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. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. *
  21. * File: ODEdit.c
  22. *
  23. * Description: Implementation of the OpenDoors multi-line editor, which
  24. * allows the user to edit strings which may span many lines.
  25. * Provides standard text editor features, such as word wrap.
  26. *
  27. * Revisions: Date Ver Who Change
  28. * ---------------------------------------------------------------
  29. * Dec 07, 1995 6.00 BP Created.
  30. * Dec 12, 1995 6.00 BP Added entry, exit and kernel macros.
  31. * Dec 30, 1995 6.00 BP Added ODCALL for calling convention.
  32. * Jan 04, 1996 6.00 BP Use od_get_input().
  33. * Jan 12, 1996 6.00 BP Claim exclusive use of arrow keys.
  34. * Jan 31, 1996 6.00 BP Added timeout for od_get_input().
  35. * Feb 08, 1996 6.00 BP Finished implementation details.
  36. * Feb 13, 1996 6.00 BP Added od_get_input() flags parameter.
  37. * Feb 16, 1996 6.00 BP New trans. size estimation heuristics.
  38. * Feb 19, 1996 6.00 BP Changed version number to 6.00.
  39. * Feb 24, 1996 6.00 BP Fixed garbage on [Enter] after w-wrap.
  40. * Mar 03, 1996 6.10 BP Begin version 6.10.
  41. * Mar 13, 1996 6.10 BP Restore cursor position after menu.
  42. * Mar 19, 1996 6.10 BP MSVC15 source-level compatibility.
  43. * Jun 08, 1996 6.10 BP Added cast in call to alloc function.
  44. * Oct 19, 2001 6.20 RS Eliminated MSVC 6.0 warning.
  45. * Aug 10, 2003 6.23 SH *nix support
  46. */
  47. #define BUILDING_OPENDOORS
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include "OpenDoor.h"
  52. #include "ODTypes.h"
  53. #include "ODGen.h"
  54. #include "ODCore.h"
  55. #include "ODKrnl.h"
  56. #include "ODStat.h"
  57. #include "ODCom.h"
  58. #include "ODScrn.h"
  59. /* ========================================================================= */
  60. /* Misc. definitions. */
  61. /* ========================================================================= */
  62. /* Macros used by this module. */
  63. #define IS_EOL_CHAR(ch) ((ch) == '\n' || (ch) == '\r' || (ch) == '\0')
  64. /* Configurable constants. */
  65. #define LINE_ARRAY_GROW_SIZE 20
  66. #define BUFFER_GROW_SIZE 4096
  67. #define ANSI_SCROLL_DISTANCE 7
  68. #define PRE_DRAIN_TIME 10000
  69. #define MAX_TAB_STOP_SIZE 8
  70. #define DEFAULT_TAB_STOP_SIZE 8
  71. #define DEFAULT_LINE_BREAK "\n"
  72. /* Other manifest constants. */
  73. #define REDRAW_NO_BOUNDARY 0xffff
  74. /* Default editor options. */
  75. static tODEditOptions ODEditOptionsDefault =
  76. {
  77. 1, 1, 80, 23,
  78. FORMAT_PARAGRAPH_BREAKS,
  79. NULL,
  80. NULL,
  81. EFLAG_NORMAL,
  82. };
  83. /* ========================================================================= */
  84. /* Multiline editor instance structure. */
  85. /* ========================================================================= */
  86. typedef struct
  87. {
  88. char *pszEditBuffer;
  89. UINT unBufferSize;
  90. tODEditOptions *pUserOptions;
  91. UINT unCurrentLine;
  92. UINT unCurrentColumn;
  93. UINT unLineScrolledToTop;
  94. UINT unAreaWidth;
  95. UINT unAreaHeight;
  96. char **papchStartOfLine;
  97. UINT unLineArraySize;
  98. UINT unLinesInBuffer;
  99. BOOL bInsertMode;
  100. UINT unTabStopSize;
  101. UINT unScrollDistance;
  102. char *pszLineBreak;
  103. char *pszParagraphBreak;
  104. BOOL bWordWrapLongLines;
  105. void *pRememberBuffer;
  106. } tEditInstance;
  107. /* ========================================================================= */
  108. /* Editor function prototypes. */
  109. /* ========================================================================= */
  110. /* High level implementation. */
  111. static BOOL ODEditSetupInstance(tEditInstance *pEditInstance,
  112. char *pszBufferToEdit, UINT unBufferSize, tODEditOptions *pUserOptions);
  113. static void ODEditRedrawArea(tEditInstance *pEditInstance);
  114. static void ODEditDrawAreaLine(tEditInstance *pEditInstance,
  115. UINT unAreaLineToDraw);
  116. static INT ODEditMainLoop(tEditInstance *pEditInstance);
  117. static void ODEditGotoPreviousLine(tEditInstance *pEditInstance);
  118. static void ODEditGotoNextLine(tEditInstance *pEditInstance);
  119. static BOOL ODEditScrollArea(tEditInstance *pEditInstance, INT nDistance);
  120. static BOOL ODEditRecommendFullRedraw(tEditInstance *pEditInstance,
  121. UINT unEstPartialRedrawBytes, BOOL bDefault);
  122. static UINT ODEditEstDrawBytes(tEditInstance *pEditInstance,
  123. UINT unStartRedrawLine, UINT unStartRedrawColumn, UINT unFinishRedrawLine,
  124. UINT unFinishRedrawColumn);
  125. static UINT ODEditGetCurrentLineInArea(tEditInstance *pEditInstance);
  126. static void ODEditUpdateCursorPos(tEditInstance *pEditInstance);
  127. static void ODEditUpdateCursorIfMoved(tEditInstance *pEditInstance);
  128. static tODResult ODEditEnterText(tEditInstance *pEditInstance,
  129. char *pszEntered, BOOL bInsertMode);
  130. static void ODEditSetBreakSequence(tEditInstance *pEditInstance,
  131. char chFirstEOLChar, char chSecondEOLChar);
  132. static BOOL ODEditCursorLeft(tEditInstance *pEditInstance);
  133. static void ODEditDeleteCurrentChar(tEditInstance *pEditInstance);
  134. static void ODEditDeleteCurrentLine(tEditInstance *pEditInstance);
  135. static BOOL ODEditPastEndOfCurLine(tEditInstance *pEditInstance);
  136. static size_t ODEditRememberBufferSize(tEditInstance *pEditInstance);
  137. static void ODEditRememberArea(tEditInstance *pEditInstance,
  138. void *pRememberedArea);
  139. static void ODEditRedrawChanged(tEditInstance *pEditInstance,
  140. void *pRememberedArea, UINT unUpperBoundary, UINT unLowerBoundary);
  141. static BOOL ODEditDetermineChanged(tEditInstance *pEditInstance,
  142. void *pRememberedArea, UINT unUpperBoundary, UINT unLowerBoundary,
  143. UINT *punStartRedrawLine, UINT *punStartRedrawColumn,
  144. UINT *punFinishRedrawLine, UINT *punFinishRedrawColumn);
  145. static void ODEditRedrawSubArea(tEditInstance *pEditInstance,
  146. UINT unStartRedrawLine, UINT unStartRedrawColumn, UINT unFinishRedrawLine,
  147. UINT unFinishRedrawColumn);
  148. static void ODEditGetActualCurPos(tEditInstance *pEditInstance,
  149. UINT *punRow, UINT *punColumn);
  150. static BOOL ODEditIsEOLForMode(tEditInstance *pEditInstance, char chToTest);
  151. /* Low level buffer manipulation functions. */
  152. static BOOL ODEditBufferFormatAndIndex(tEditInstance *pEditInstance);
  153. static UINT ODEditBufferGetLineLength(tEditInstance *pEditInstance,
  154. UINT unBufferLine);
  155. static UINT ODEditBufferGetTotalLines(tEditInstance *pEditInstance);
  156. static char *ODEditBufferGetCharacter(tEditInstance *pEditInstance,
  157. UINT unBufferLine, UINT unBufferColumn);
  158. static tODResult ODEditBufferMakeSpace(tEditInstance *pEditInstance,
  159. UINT unLine, UINT unColumn, UINT unNumChars);
  160. static tODResult ODEditTryToGrow(tEditInstance *pEditInstance,
  161. UINT unSizeNeeded);
  162. /* ========================================================================= */
  163. /* High level editor implementation. */
  164. /* ========================================================================= */
  165. /* ----------------------------------------------------------------------------
  166. * od_multiline_edit()
  167. *
  168. * Multiline editor function, allows the user to enter or change text that
  169. * spans multiple lines.
  170. *
  171. * Parameters: pszBufferToEdit - Pointer to '\0'-terminated buffer of text
  172. * to edit.
  173. *
  174. * unBufferSize - Size of the buffer, in characters.
  175. *
  176. * pEditOptions - Pointer to a tODEditOptions structure, or
  177. * NULL to use default settings.
  178. *
  179. * Return: An od_multiline_edit()-specific result code.
  180. */
  181. ODAPIDEF INT ODCALL od_multiline_edit(char *pszBufferToEdit, UINT unBufferSize,
  182. tODEditOptions *pEditOptions)
  183. {
  184. tEditInstance EditInstance;
  185. INT nToReturn;
  186. /* Log function entry if running in trace mode */
  187. TRACE(TRACE_API, "od_node_open()");
  188. /* Initialize OpenDoors if not already done. */
  189. if(!bODInitialized) od_init();
  190. OD_API_ENTRY();
  191. /* Validate parameters. */
  192. if(pszBufferToEdit == NULL || unBufferSize == 0)
  193. {
  194. od_control.od_error = ERR_PARAMETER;
  195. OD_API_EXIT();
  196. return(OD_MULTIEDIT_ERROR);
  197. }
  198. /* Check the user's terminal supports the required capabilities. */
  199. if(!(od_control.user_ansi || od_control.user_avatar))
  200. {
  201. od_control.od_error = ERR_NOGRAPHICS;
  202. OD_API_EXIT();
  203. return(OD_MULTIEDIT_ERROR);
  204. }
  205. /* Initialize editor instance information structure. */
  206. if(!ODEditSetupInstance(&EditInstance, pszBufferToEdit, unBufferSize,
  207. pEditOptions))
  208. {
  209. OD_API_EXIT();
  210. return(OD_MULTIEDIT_ERROR);
  211. }
  212. /* Attempt to build the buffer line index and ensure that the buffer */
  213. /* conforms to the format specified by the client application. */
  214. if(!ODEditBufferFormatAndIndex(&EditInstance))
  215. {
  216. od_control.od_error = ERR_MEMORY;
  217. OD_API_EXIT();
  218. return(OD_MULTIEDIT_ERROR);
  219. }
  220. /* Claim exclusive use of arrow keys. */
  221. ODStatStartArrowUse();
  222. /* Ensure that all information in the outbound communications buffer */
  223. /* has been sent before starting. This way, we can safely purge the */
  224. /* outbound buffer at any time without loosing anything that was sent */
  225. /* before od_multiline_edit() was called. */
  226. ODWaitDrain(PRE_DRAIN_TIME);
  227. /* Draw the initial edit area. */
  228. ODEditRedrawArea(&EditInstance);
  229. /* Run the main editor loop. */
  230. nToReturn = ODEditMainLoop(&EditInstance);
  231. /* Release exclusive use of arrow keys. */
  232. ODStatEndArrowUse();
  233. /* Set final information which will be available in the user options */
  234. /* structure for the client application to access. */
  235. EditInstance.pUserOptions->pszFinalBuffer = EditInstance.pszEditBuffer;
  236. EditInstance.pUserOptions->unFinalBufferSize = unBufferSize;
  237. OD_API_EXIT();
  238. return(nToReturn);
  239. }
  240. /* ----------------------------------------------------------------------------
  241. * ODEditSetupInstance() *** PRIVATE FUNCTION ***
  242. *
  243. * Initializes editor instance information structure.
  244. *
  245. * Parameters: pEditInstance - Editor instance information structure.
  246. *
  247. * pszBufferToEdit - Buffer pointer provided by client.
  248. *
  249. * unBufferSize - Initial buffer size, as specified by the
  250. * client.
  251. *
  252. * pUserOptions - Editor options specified by the client.
  253. *
  254. * Return: TRUE on success, FALSE on failure. In the case of failure,
  255. * od_control.od_error is set appropriately.
  256. */
  257. static BOOL ODEditSetupInstance(tEditInstance *pEditInstance,
  258. char *pszBufferToEdit, UINT unBufferSize, tODEditOptions *pUserOptions)
  259. {
  260. ASSERT(pEditInstance != NULL);
  261. ASSERT(pszBufferToEdit != NULL);
  262. /* Setup editor instance structure. */
  263. pEditInstance->pszEditBuffer = pszBufferToEdit;
  264. pEditInstance->unBufferSize = unBufferSize;
  265. if(pUserOptions == NULL)
  266. {
  267. /* Edit options is just the defaults. */
  268. pEditInstance->pUserOptions = &ODEditOptionsDefault;
  269. }
  270. else
  271. {
  272. /* Edit options are supplied by the user. */
  273. pEditInstance->pUserOptions = pUserOptions;
  274. /* Initialize any edit options that the user did not setup. */
  275. /* Check that edit area has been initialized. */
  276. if(pUserOptions->nAreaLeft == 0)
  277. {
  278. pUserOptions->nAreaLeft = ODEditOptionsDefault.nAreaLeft;
  279. }
  280. if(pUserOptions->nAreaRight == 0)
  281. {
  282. pUserOptions->nAreaRight = ODEditOptionsDefault.nAreaRight;
  283. }
  284. if(pUserOptions->nAreaTop == 0)
  285. {
  286. pUserOptions->nAreaTop = ODEditOptionsDefault.nAreaTop;
  287. }
  288. if(pUserOptions->nAreaBottom == 0)
  289. {
  290. pUserOptions->nAreaBottom = ODEditOptionsDefault.nAreaBottom;
  291. }
  292. }
  293. pEditInstance->unCurrentLine = 0;
  294. pEditInstance->unCurrentColumn = 0;
  295. pEditInstance->unLineScrolledToTop = 0;
  296. pEditInstance->papchStartOfLine = NULL;
  297. pEditInstance->unLineArraySize = 0;
  298. pEditInstance->unLinesInBuffer = 0;
  299. pEditInstance->unAreaWidth = (UINT)pEditInstance->pUserOptions->
  300. nAreaRight - (UINT)pEditInstance->pUserOptions->nAreaLeft + 1;
  301. pEditInstance->unAreaHeight = (UINT)pEditInstance->pUserOptions->
  302. nAreaBottom - (UINT)pEditInstance->pUserOptions->nAreaTop + 1;
  303. pEditInstance->bInsertMode = TRUE;
  304. pEditInstance->unTabStopSize = DEFAULT_TAB_STOP_SIZE;
  305. /* Setup line break and paragraph break sequences, if they can be */
  306. /* determined at this point. If they can't be determined, set them */
  307. /* to NULL. */
  308. switch(pEditInstance->pUserOptions->TextFormat)
  309. {
  310. case FORMAT_FTSC_MESSAGE:
  311. /* FTSC compliant messages use \r as a paragraph break, and do */
  312. /* not have any line break characters. */
  313. pEditInstance->pszLineBreak = "";
  314. pEditInstance->pszParagraphBreak = "\r";
  315. break;
  316. case FORMAT_PARAGRAPH_BREAKS:
  317. /* Paragraph break mode only inserts CR/LF sequences at the end */
  318. /* of a paragrah, and word-wraps the text that forms a paragrah. */
  319. pEditInstance->pszLineBreak = "";
  320. pEditInstance->pszParagraphBreak = NULL;
  321. break;
  322. case FORMAT_LINE_BREAKS:
  323. case FORMAT_NO_WORDWRAP:
  324. /* Line break mode and no word wrap mode both terminate every */
  325. /* line of the file with a CR/LF sequence, and have no paragrah */
  326. /* terminator. In line break mode, word wrap is enabled, whereas */
  327. /* it is not in FORMAT_NO_WORDWRAP mode. */
  328. pEditInstance->pszLineBreak = NULL;
  329. pEditInstance->pszParagraphBreak = "";
  330. break;
  331. default:
  332. /* An invalid text format was specified. */
  333. od_control.od_error = ERR_PARAMETER;
  334. return(FALSE);
  335. }
  336. /* Determine whether long lines sould be word wrapped or character */
  337. /* wrapped. */
  338. pEditInstance->bWordWrapLongLines = (pEditInstance->pUserOptions->TextFormat
  339. != FORMAT_NO_WORDWRAP);
  340. /* Attempt to allocate abuffer for remembered data. */
  341. pEditInstance->pRememberBuffer =
  342. malloc(ODEditRememberBufferSize(pEditInstance));
  343. if(pEditInstance->pRememberBuffer == NULL)
  344. {
  345. od_control.od_error = ERR_MEMORY;
  346. return(FALSE);
  347. }
  348. /* If AVATAR mode or local mode is active, then scroll up or down one */
  349. /* line at a time. */
  350. if(od_control.user_avatar || od_control.baud == 0)
  351. {
  352. pEditInstance->unScrollDistance = 1;
  353. }
  354. /* In ANSI mode with a remote connection, scroll multiple lines at a */
  355. /* time. This is the minimum of the default scroll distance, and the */
  356. /* current height of the edit area - 1. */
  357. else
  358. {
  359. pEditInstance->unScrollDistance = MIN(ANSI_SCROLL_DISTANCE,
  360. pEditInstance->pUserOptions->nAreaBottom
  361. - pEditInstance->pUserOptions->nAreaTop);
  362. }
  363. /* Return with success. */
  364. return(TRUE);
  365. }
  366. /* ----------------------------------------------------------------------------
  367. * ODEditRedrawArea() *** PRIVATE FUNCTION ***
  368. *
  369. * Redraws the area of the screen used by the editor.
  370. *
  371. * Parameters: pEditInstance - Editor instance information structure.
  372. *
  373. * Return: void
  374. */
  375. static void ODEditRedrawArea(tEditInstance *pEditInstance)
  376. {
  377. UINT unAreaLine;
  378. ASSERT(pEditInstance != NULL);
  379. ODScrnEnableCaret(FALSE);
  380. /* First, remove anything that is still in the outbound communications */
  381. /* buffer, since whatever it was, it will no longer be visible after */
  382. /* the screen redraw anyhow. */
  383. if(od_control.baud != 0) ODComClearOutbound(hSerialPort);
  384. /* Loop, drawing every line in the edit area. */
  385. for(unAreaLine = 0; unAreaLine < pEditInstance->unAreaHeight; ++unAreaLine)
  386. {
  387. ODEditDrawAreaLine(pEditInstance, unAreaLine);
  388. }
  389. ODScrnEnableCaret(TRUE);
  390. }
  391. /* ----------------------------------------------------------------------------
  392. * ODEditDrawAreaLine() *** PRIVATE FUNCTION ***
  393. *
  394. * Redraws the specified line in the area of the screen used by the editor.
  395. *
  396. * Parameters: pEditInstance - Editor instance information structure.
  397. *
  398. * unAreaLineToDraw - 0-based line number in the edit area to be
  399. * redrawn.
  400. *
  401. * Return: void
  402. */
  403. static void ODEditDrawAreaLine(tEditInstance *pEditInstance,
  404. UINT unAreaLineToDraw)
  405. {
  406. UINT unBufferLine;
  407. UINT unLineLength;
  408. ASSERT(pEditInstance != NULL);
  409. ASSERT(unAreaLineToDraw >= 0);
  410. ASSERT(unAreaLineToDraw < pEditInstance->unAreaHeight);
  411. /* Determine the buffer line that is displayed on this screen line. */
  412. unBufferLine = unAreaLineToDraw + pEditInstance->unLineScrolledToTop;
  413. /* Position the cursor to the beginning of this line. */
  414. od_set_cursor((UINT)pEditInstance->pUserOptions->nAreaTop
  415. + unAreaLineToDraw, (UINT)pEditInstance->pUserOptions->nAreaLeft);
  416. /* If this line is not beyond the end of the buffer. */
  417. if(unBufferLine < pEditInstance->unLinesInBuffer)
  418. {
  419. /* Determine the length of this buffer line. */
  420. unLineLength = ODEditBufferGetLineLength(pEditInstance, unBufferLine);
  421. od_disp(ODEditBufferGetCharacter(pEditInstance, unBufferLine, 0),
  422. unLineLength, TRUE);
  423. }
  424. else
  425. {
  426. unLineLength = 0;
  427. }
  428. /* If right edge of edit area aligns with the right edge of the screen. */
  429. if(pEditInstance->pUserOptions->nAreaRight == OD_SCREEN_WIDTH)
  430. {
  431. /* Clear the remainder of this line on the screen. */
  432. od_clr_line();
  433. }
  434. else
  435. {
  436. /* Place spaces after the end of the current line, up to right edge of */
  437. /* the edit area. */
  438. od_repeat(' ', (BYTE)(pEditInstance->unAreaWidth - unLineLength));
  439. }
  440. }
  441. /* ----------------------------------------------------------------------------
  442. * ODEditMainLoop() *** PRIVATE FUNCTION ***
  443. *
  444. * Implements the main editor loop, which repeatedly waits for input from the
  445. * user, until the user chooses to exit.
  446. *
  447. * Parameters: pEditInstance - Editor instance information structure.
  448. *
  449. * Return: Value to be returned by od_multiline_edit.
  450. */
  451. static INT ODEditMainLoop(tEditInstance *pEditInstance)
  452. {
  453. tODInputEvent InputEvent;
  454. ASSERT(pEditInstance != NULL);
  455. /* Set initial cursor position. */
  456. ODEditUpdateCursorPos(pEditInstance);
  457. /* Loop, obtaining keystrokes until the user chooses to exit. */
  458. for(;;)
  459. {
  460. od_get_input(&InputEvent, OD_NO_TIMEOUT, GETIN_NORMAL);
  461. if(InputEvent.EventType == EVENT_EXTENDED_KEY)
  462. {
  463. switch(InputEvent.chKeyPress)
  464. {
  465. case OD_KEY_UP:
  466. /* If we aren't at the start of the file, then move to the */
  467. /* previous line. */
  468. if(pEditInstance->unCurrentLine > 0)
  469. {
  470. ODEditGotoPreviousLine(pEditInstance);
  471. ODEditUpdateCursorPos(pEditInstance);
  472. }
  473. break;
  474. case OD_KEY_DOWN:
  475. /* If we aren't at the end of the file, then move to the */
  476. /* next line. */
  477. if(pEditInstance->unCurrentLine
  478. < ODEditBufferGetTotalLines(pEditInstance) - 1)
  479. {
  480. ODEditGotoNextLine(pEditInstance);
  481. ODEditUpdateCursorPos(pEditInstance);
  482. }
  483. break;
  484. case OD_KEY_LEFT:
  485. /* Attempt to move the cursor left. */
  486. if(ODEditCursorLeft(pEditInstance))
  487. {
  488. /* If it was possible to move the cursor, then update the */
  489. /* cursor position on the screen. */
  490. ODEditUpdateCursorPos(pEditInstance);
  491. }
  492. break;
  493. case OD_KEY_RIGHT:
  494. /* In word wrap mode, we allow the cursor to move up to the */
  495. /* end of this line, and then wrap around to the next line. */
  496. if(pEditInstance->bWordWrapLongLines)
  497. {
  498. if(pEditInstance->unCurrentColumn < ODEditBufferGetLineLength
  499. (pEditInstance, pEditInstance->unCurrentLine))
  500. {
  501. ++pEditInstance->unCurrentColumn;
  502. ODEditUpdateCursorPos(pEditInstance);
  503. }
  504. else if(pEditInstance->unCurrentLine
  505. < ODEditBufferGetTotalLines(pEditInstance) - 1)
  506. {
  507. ODEditGotoNextLine(pEditInstance);
  508. pEditInstance->unCurrentColumn = 0;
  509. ODEditUpdateCursorPos(pEditInstance);
  510. }
  511. }
  512. /* In character wrap mode, we allow the cursor to move up to */
  513. /* the right edge of the edit area. */
  514. else
  515. {
  516. if(pEditInstance->unCurrentColumn
  517. < pEditInstance->unAreaWidth - 1)
  518. {
  519. ++pEditInstance->unCurrentColumn;
  520. ODEditUpdateCursorPos(pEditInstance);
  521. }
  522. }
  523. break;
  524. case OD_KEY_HOME:
  525. pEditInstance->unCurrentColumn = 0;
  526. ODEditUpdateCursorPos(pEditInstance);
  527. break;
  528. case OD_KEY_END:
  529. pEditInstance->unCurrentColumn = ODEditBufferGetLineLength(
  530. pEditInstance, pEditInstance->unCurrentLine);
  531. ODEditUpdateCursorPos(pEditInstance);
  532. break;
  533. case OD_KEY_PGUP:
  534. if(pEditInstance->unLineScrolledToTop > 0)
  535. {
  536. UINT unDistance = MIN(pEditInstance->unAreaHeight - 1,
  537. pEditInstance->unLineScrolledToTop);
  538. ODEditScrollArea(pEditInstance, -((INT)unDistance));
  539. pEditInstance->unCurrentLine -= unDistance;
  540. ODEditUpdateCursorPos(pEditInstance);
  541. }
  542. else if(pEditInstance->unCurrentLine != 0)
  543. {
  544. pEditInstance->unCurrentLine = 0;
  545. ODEditUpdateCursorPos(pEditInstance);
  546. }
  547. break;
  548. case OD_KEY_PGDN:
  549. if(pEditInstance->unLineScrolledToTop <
  550. pEditInstance->unLinesInBuffer - 1)
  551. {
  552. UINT unDistance = MIN(pEditInstance->unAreaHeight - 1,
  553. pEditInstance->unLinesInBuffer
  554. - pEditInstance->unLineScrolledToTop - 1);
  555. ODEditScrollArea(pEditInstance, (INT)unDistance);
  556. pEditInstance->unCurrentLine = MIN(
  557. pEditInstance->unCurrentLine + unDistance,
  558. pEditInstance->unLinesInBuffer - 1);
  559. ODEditUpdateCursorPos(pEditInstance);
  560. }
  561. break;
  562. case OD_KEY_INSERT:
  563. /* If the insert key is pressed, then toggle insert mode. */
  564. pEditInstance->bInsertMode = !pEditInstance->bInsertMode;
  565. break;
  566. case OD_KEY_DELETE:
  567. /* Delete the character at the current position. */
  568. /* If we are currently past the end of this line. */
  569. if(ODEditPastEndOfCurLine(pEditInstance))
  570. {
  571. /* Add spaces to this line to fill it up to the current */
  572. /* cursor position. */
  573. switch(ODEditBufferMakeSpace(pEditInstance,
  574. pEditInstance->unCurrentLine,
  575. pEditInstance->unCurrentColumn, 0))
  576. {
  577. case kODRCUnrecoverableFailure:
  578. /* If we encountered an unrecoverable failure, then */
  579. /* exit from the editor with a memory allocation */
  580. /* error. */
  581. od_control.od_error = ERR_MEMORY;
  582. return(OD_MULTIEDIT_ERROR);
  583. case kODRCSuccess:
  584. /* On success, delete the current character. */
  585. ODEditDeleteCurrentChar(pEditInstance);
  586. break;
  587. default:
  588. /* On any other failure, just beep and continue. */
  589. od_putch('\a');
  590. }
  591. }
  592. else
  593. {
  594. /* If we aren't pas the end of the line, then just do a */
  595. /* simple delete character operation. */
  596. ODEditDeleteCurrentChar(pEditInstance);
  597. }
  598. break;
  599. }
  600. }
  601. else if(InputEvent.EventType == EVENT_CHARACTER)
  602. {
  603. if(InputEvent.chKeyPress == 25)
  604. {
  605. /* Control-Y (delete line) has been pressed. */
  606. ODEditDeleteCurrentLine(pEditInstance);
  607. }
  608. else if(InputEvent.chKeyPress == 26
  609. || InputEvent.chKeyPress == 27)
  610. {
  611. /* Escape or control-Z has been pressed. */
  612. /* If a menu callback function has been provided by */
  613. /* the client, then call it. */
  614. if(pEditInstance->pUserOptions->pfMenuCallback != NULL)
  615. {
  616. /* Call the menu callback function. */
  617. switch((*pEditInstance->pUserOptions->pfMenuCallback)(NULL))
  618. {
  619. case EDIT_MENU_EXIT_EDITOR:
  620. return(OD_MULTIEDIT_SUCCESS);
  621. case EDIT_MENU_DO_NOTHING:
  622. /* Continue in the editor without doing anything. */
  623. break;
  624. default:
  625. ASSERT(FALSE);
  626. }
  627. /* If we are continuing, then restore initial cursor pos. */
  628. ODEditUpdateCursorPos(pEditInstance);
  629. }
  630. else
  631. {
  632. /* If a menu key callback function has not been provided, */
  633. /* then we exit the editor unconditionally. */
  634. return(OD_MULTIEDIT_SUCCESS);
  635. }
  636. }
  637. else if(InputEvent.chKeyPress == '\b')
  638. {
  639. /* Backspace key has been pressed. */
  640. /* If the cursor is past the end of the line, then we just move */
  641. /* the cursor left, without deleting any characters. */
  642. BOOL bDelete = !ODEditPastEndOfCurLine(pEditInstance);
  643. /* Backup the cursor one space. */
  644. if(ODEditCursorLeft(pEditInstance))
  645. {
  646. /* If there was space to move the cursor back to, then */
  647. /* proceed and remove the character at the current position. */
  648. if(bDelete)
  649. {
  650. ODEditDeleteCurrentChar(pEditInstance);
  651. }
  652. else
  653. {
  654. /* In this case, we must still show the new cursor */
  655. /* position. */
  656. ODEditUpdateCursorPos(pEditInstance);
  657. }
  658. }
  659. }
  660. else if(InputEvent.chKeyPress == '\t')
  661. {
  662. char szTextToAdd[MAX_TAB_STOP_SIZE + 1];
  663. UINT unTargetColumn;
  664. UINT unTargetDistance;
  665. /* A tab key has been entered. */
  666. /* Determine the column that this will move the cursor to. */
  667. ASSERT(pEditInstance->unTabStopSize <= MAX_TAB_STOP_SIZE);
  668. unTargetColumn = ((pEditInstance->unCurrentColumn / pEditInstance->
  669. unTabStopSize) + 1) * pEditInstance->unTabStopSize;
  670. /* In insert mode, then insert spaces into the buffer. */
  671. if(pEditInstance->bInsertMode)
  672. {
  673. /* Determine the number of columns that we need to advance in */
  674. /* order to reach this target column. */
  675. unTargetDistance = unTargetColumn -
  676. pEditInstance->unCurrentColumn;
  677. ASSERT(unTargetDistance <= MAX_TAB_STOP_SIZE);
  678. /* Translate this to a string with the appropriate number of */
  679. /* spaces. */
  680. memset(szTextToAdd, ' ', unTargetDistance);
  681. szTextToAdd[unTargetDistance] = '\0';
  682. /* Add this to the buffer. */
  683. if(ODEditEnterText(pEditInstance, szTextToAdd, TRUE) ==
  684. kODRCUnrecoverableFailure)
  685. {
  686. od_control.od_error = ERR_MEMORY;
  687. return(OD_MULTIEDIT_ERROR);
  688. }
  689. }
  690. /* In overwrite mode, then just advance the cursor position. */
  691. else
  692. {
  693. /* Determine the column where the cursor should be wrapped. */
  694. UINT unWrapColumn = pEditInstance->bWordWrapLongLines ?
  695. ODEditBufferGetLineLength(pEditInstance,
  696. pEditInstance->unCurrentLine)
  697. : pEditInstance->unAreaWidth;
  698. if(unTargetColumn < unWrapColumn)
  699. {
  700. pEditInstance->unCurrentColumn = unTargetColumn;
  701. ODEditUpdateCursorPos(pEditInstance);
  702. }
  703. else if(pEditInstance->unCurrentLine
  704. < ODEditBufferGetTotalLines(pEditInstance) - 1)
  705. {
  706. ODEditGotoNextLine(pEditInstance);
  707. pEditInstance->unCurrentColumn = 0;
  708. ODEditUpdateCursorPos(pEditInstance);
  709. }
  710. }
  711. }
  712. else if(InputEvent.chKeyPress == '\r')
  713. {
  714. char *pszTextToAdd;
  715. /* The enter key has been pressed. In insert mode, or at the end */
  716. /* of the buffer in overwrite mode, this adds a new line. */
  717. if(pEditInstance->bInsertMode || pEditInstance->unCurrentLine
  718. >= ODEditBufferGetTotalLines(pEditInstance) - 1)
  719. {
  720. if(!pEditInstance->bInsertMode)
  721. {
  722. /* If we are not in insert mode, begin by positioning the */
  723. /* cursor at the end of this line. */
  724. pEditInstance->unCurrentColumn = ODEditBufferGetLineLength(
  725. pEditInstance, pEditInstance->unCurrentLine);
  726. }
  727. /* Determine the line/paragraph break sequence to use. */
  728. if(pEditInstance->pszLineBreak != NULL
  729. && strlen(pEditInstance->pszLineBreak) > 0)
  730. {
  731. pszTextToAdd = pEditInstance->pszLineBreak;
  732. }
  733. else if(pEditInstance->pszParagraphBreak != NULL
  734. && strlen(pEditInstance->pszParagraphBreak) > 0)
  735. {
  736. pszTextToAdd = pEditInstance->pszParagraphBreak;
  737. }
  738. else
  739. {
  740. pszTextToAdd = DEFAULT_LINE_BREAK;
  741. }
  742. /* Insert the sequence into the buffer. */
  743. if(ODEditEnterText(pEditInstance, pszTextToAdd, TRUE) ==
  744. kODRCUnrecoverableFailure)
  745. {
  746. od_control.od_error = ERR_MEMORY;
  747. return(OD_MULTIEDIT_ERROR);
  748. }
  749. }
  750. else
  751. {
  752. /* Pressing the enter key in overwrite mode just moves the */
  753. /* cursor to the beginning of the next line. In other words, */
  754. /* it is equivalent to pressing Down arrow followed by home. */
  755. ODEditGotoNextLine(pEditInstance);
  756. pEditInstance->unCurrentColumn = 0;
  757. ODEditUpdateCursorPos(pEditInstance);
  758. }
  759. }
  760. else if(InputEvent.chKeyPress >= 32)
  761. {
  762. char szTextToAdd[2];
  763. szTextToAdd[0] = InputEvent.chKeyPress;
  764. szTextToAdd[1] = '\0';
  765. /* A valid buffer character has been entered. */
  766. if(ODEditEnterText(pEditInstance, szTextToAdd,
  767. (BOOL)(pEditInstance->bInsertMode
  768. || ODEditPastEndOfCurLine(pEditInstance)))
  769. == kODRCUnrecoverableFailure)
  770. {
  771. od_control.od_error = ERR_MEMORY;
  772. return(OD_MULTIEDIT_ERROR);
  773. }
  774. }
  775. }
  776. }
  777. }
  778. /* ----------------------------------------------------------------------------
  779. * ODEditGotoPreviousLine() *** PRIVATE FUNCTION ***
  780. *
  781. * Moves the current cursor position to the previous line, scrolling the screen
  782. * if necessary to keep the cursor visible.
  783. *
  784. * Parameters: pEditInstance - Editor instance information structure.
  785. *
  786. * Return: void
  787. */
  788. static void ODEditGotoPreviousLine(tEditInstance *pEditInstance)
  789. {
  790. ASSERT(pEditInstance != NULL);
  791. /* If we are already at the first line, then return without doing */
  792. /* anything. */
  793. if(pEditInstance->unCurrentLine == 0) return;
  794. /* If cursor is at top of edit area, then scroll area */
  795. /* first. */
  796. if(ODEditGetCurrentLineInArea(pEditInstance) == 0)
  797. {
  798. ODEditScrollArea(pEditInstance,
  799. -(INT)(MIN(pEditInstance->unScrollDistance,
  800. pEditInstance->unCurrentLine)));
  801. }
  802. /* Move cursor to previous line. */
  803. --pEditInstance->unCurrentLine;
  804. }
  805. /* ----------------------------------------------------------------------------
  806. * ODEditGotoNextLine() *** PRIVATE FUNCTION ***
  807. *
  808. * Advances the current cursor position to the next line, scrolling the screen
  809. * if necessary to keep the cursor visible.
  810. *
  811. * Parameters: pEditInstance - Editor instance information structure.
  812. *
  813. * Return: void
  814. */
  815. static void ODEditGotoNextLine(tEditInstance *pEditInstance)
  816. {
  817. ASSERT(pEditInstance != NULL);
  818. /* If we are already at the end of the file, then return without */
  819. /* doing anything. */
  820. if(pEditInstance->unCurrentLine
  821. >= ODEditBufferGetTotalLines(pEditInstance) - 1)
  822. {
  823. return;
  824. }
  825. /* If cursor is at the bottom of the edit area, then scroll area first. */
  826. if(ODEditGetCurrentLineInArea(pEditInstance)
  827. == pEditInstance->unAreaHeight - 1)
  828. {
  829. ODEditScrollArea(pEditInstance,
  830. (INT)MIN(pEditInstance->unScrollDistance,
  831. ODEditBufferGetTotalLines(pEditInstance)
  832. - pEditInstance->unCurrentLine));
  833. }
  834. /* Move cursor to next line. */
  835. ++pEditInstance->unCurrentLine;
  836. }
  837. /* ----------------------------------------------------------------------------
  838. * ODEditScrollArea() *** PRIVATE FUNCTION ***
  839. *
  840. * Scrolls the edit area up or down the specified distance, redrawing newly
  841. * "exposed" lines.
  842. *
  843. * Parameters: pEditInstance - Editor instance information structure.
  844. *
  845. * nDistance - Number of lines to scroll, where a positive
  846. * value moves the text upwards, and a negative
  847. * value moves the text down.
  848. *
  849. * Return: FALSE if a full redraw has been performed, TRUE if an efficient
  850. * scroll command has been used.
  851. */
  852. static BOOL ODEditScrollArea(tEditInstance *pEditInstance, INT nDistance)
  853. {
  854. BOOL bUseScrollCommand = FALSE;
  855. UINT unAreaLine;
  856. UINT unBufferLine;
  857. UINT unFirstAreaLineToDraw;
  858. UINT unLastAreaLineToDraw;
  859. UINT unPositiveDistance;
  860. ASSERT(pEditInstance);
  861. /* If scroll distance is zero, then we don't need to do anything at all. */
  862. if(nDistance == 0)
  863. {
  864. return(TRUE);
  865. }
  866. /* Otherwise, obtain the absolute value of the distance as an unsigned */
  867. /* integer. */
  868. else if(nDistance < 0)
  869. {
  870. unPositiveDistance = (UINT)-nDistance;
  871. }
  872. else
  873. {
  874. unPositiveDistance = (UINT)nDistance;
  875. }
  876. /* In AVATAR mode, if more than one of the currently visible lines will */
  877. /* still be visible after scrolling, then we will consider using the */
  878. /* scroll operation. */
  879. if(od_control.user_avatar
  880. && ((INT)pEditInstance->unAreaHeight) - ((INT)unPositiveDistance) > 1)
  881. {
  882. /* Even under this situation, we only want to use the scroll operation */
  883. /* if the amount of data still in the outbound buffer + our estimate */
  884. /* of the amount of data that will be sent to perform the scroll */
  885. /* operation is less than our estimate of the amount of data that */
  886. /* would be sent by a complete screen redraw. */
  887. UINT unEstimatedScrollData = ((pEditInstance->unAreaWidth + 4) *
  888. unPositiveDistance) + 7;
  889. if(!ODEditRecommendFullRedraw(pEditInstance, unEstimatedScrollData,
  890. TRUE))
  891. {
  892. bUseScrollCommand = TRUE;
  893. }
  894. }
  895. /* In local mode, we can also use the scroll command for efficiency. */
  896. if(od_control.baud == 0)
  897. {
  898. bUseScrollCommand = TRUE;
  899. }
  900. /* Area scroll is achieved by one of two means. We either use the scroll */
  901. /* command, and then draw just the newly visible lines, or we redraw the */
  902. /* entire edit area, after removing any data from the outbound */
  903. /* communications buffer. */
  904. if(bUseScrollCommand)
  905. {
  906. /* Use the od_scroll() function to scroll the screen contents. */
  907. od_scroll(pEditInstance->pUserOptions->nAreaLeft,
  908. pEditInstance->pUserOptions->nAreaTop,
  909. pEditInstance->pUserOptions->nAreaRight,
  910. pEditInstance->pUserOptions->nAreaBottom,
  911. nDistance, SCROLL_NO_CLEAR);
  912. /* Fill the newly visible lines. First, the portion of the area that */
  913. /* requires redrawing is determined, and then a loop redraws the lines */
  914. /* that must be drawn. */
  915. /* If we are moving text upwards, exposing new lines at the bottom of */
  916. /* the area: */
  917. if(nDistance > 0)
  918. {
  919. ASSERT(pEditInstance->unLineScrolledToTop + unPositiveDistance
  920. < pEditInstance->unLinesInBuffer);
  921. pEditInstance->unLineScrolledToTop += unPositiveDistance;
  922. unFirstAreaLineToDraw = pEditInstance->unAreaHeight
  923. - (UINT)unPositiveDistance;
  924. unLastAreaLineToDraw = pEditInstance->unAreaHeight - 1;
  925. }
  926. /* Otherwise, we have moved text downwards, exposing new lines at the */
  927. /* top of the edit area. */
  928. else
  929. {
  930. ASSERT(pEditInstance->unLineScrolledToTop >= unPositiveDistance);
  931. pEditInstance->unLineScrolledToTop -= unPositiveDistance;
  932. unFirstAreaLineToDraw = 0;
  933. unLastAreaLineToDraw = unPositiveDistance - 1;
  934. }
  935. ODScrnEnableCaret(FALSE);
  936. /* Now, redraw the new lines. */
  937. unBufferLine = unFirstAreaLineToDraw
  938. + pEditInstance->unLineScrolledToTop;
  939. for(unAreaLine = unFirstAreaLineToDraw; unAreaLine <=
  940. unLastAreaLineToDraw; ++unAreaLine, ++unBufferLine)
  941. {
  942. /* Draw the entire line. */
  943. ODEditDrawAreaLine(pEditInstance, unAreaLine);
  944. }
  945. ODScrnEnableCaret(TRUE);
  946. }
  947. /* Just redraw the entire edit area. */
  948. else
  949. {
  950. /* Adjust the line number that is scrolled to the top of the screen. */
  951. if(nDistance > 0)
  952. {
  953. pEditInstance->unLineScrolledToTop += unPositiveDistance;
  954. }
  955. else
  956. {
  957. pEditInstance->unLineScrolledToTop -= unPositiveDistance;
  958. }
  959. /* Perform redraw, first purging outbound buffer. */
  960. ODEditRedrawArea(pEditInstance);
  961. }
  962. return(bUseScrollCommand);
  963. }
  964. /* ----------------------------------------------------------------------------
  965. * ODEditRecommendFullRedraw() *** PRIVATE FUNCTION ***
  966. *
  967. * Determines whether it would be more efficient to add the specified number
  968. * of bytes to the outbound buffer as part of an incremental redraw, or if
  969. * it would be more efficient to just purge the outbound buffer and do a
  970. * complete redraw of the edit area.
  971. *
  972. * Parameters: pEditInstance - Editor instance information structure.
  973. *
  974. * unEstPartialRedrawBytes - Estimate of the number of bytes that
  975. * would be transmitted if an incremental
  976. * redraw is performed.
  977. *
  978. * bDefault - The default action (TRUE for full
  979. * redraw, FALSE for incremental) if the
  980. * number of bytes in the outbound buffer
  981. * cannot be determined.
  982. *
  983. * Return: TRUE if a full redraw is recommended, FALSE if an the
  984. * incremental redraw is recommended.
  985. */
  986. static BOOL ODEditRecommendFullRedraw(tEditInstance *pEditInstance,
  987. UINT unEstPartialRedrawBytes, BOOL bDefault)
  988. {
  989. int nOutboundBufferBytes;
  990. UINT unEstFullRedrawBytes;
  991. /* In local mode, just return the default action. */
  992. if(od_control.baud == 0)
  993. {
  994. return(bDefault);
  995. }
  996. /* Attempt to obtain the number of bytes in the communications outbound */
  997. /* buffer. Unfortunately, this information may not be available. For */
  998. /* example, FOSSIL drivers will only report whether or not there is */
  999. /* still data in the outbound buffer, but not a count of the number of */
  1000. /* bytes in the buffer. Under such a situation, ODComOutbound() returns */
  1001. /* SIZE_NON_ZERO if there is data in the buffer, and 0 if there is no */
  1002. /* data in the buffer. This is not a problem under OpenDoor's internal */
  1003. /* serial I/O code, nor is it a problem under Win32's communications */
  1004. /* facilities. */
  1005. ODComOutbound(hSerialPort, &nOutboundBufferBytes);
  1006. if(nOutboundBufferBytes == SIZE_NON_ZERO)
  1007. {
  1008. /* We know that there is data in the outbound buffer, but we don't */
  1009. /* know how much, and so we cannot make a recommendation. Instead, */
  1010. /* the default course of action will be taken. */
  1011. return(bDefault);
  1012. }
  1013. /* Estimate the # of bytes required for a full redraw of the edit area. */
  1014. unEstFullRedrawBytes = ODEditEstDrawBytes(pEditInstance, 0,
  1015. 0, pEditInstance->unAreaHeight - 1, pEditInstance->unAreaWidth);
  1016. /* Recommend a full redraw if the number of bytes for an incremental */
  1017. /* redraw plus the number of bytes already in the outbound buffer */
  1018. /* exceed the number of bytes required for a full redraw. */
  1019. if(unEstPartialRedrawBytes + (UINT)nOutboundBufferBytes
  1020. > unEstFullRedrawBytes)
  1021. {
  1022. return(TRUE);
  1023. }
  1024. else
  1025. {
  1026. return(FALSE);
  1027. }
  1028. }
  1029. /* ----------------------------------------------------------------------------
  1030. * ODEditEstDrawBytes() *** PRIVATE FUNCTION ***
  1031. *
  1032. * Estimates the number of bytes which will be transmitted in order to redraw
  1033. * the specified portion of the edit area.
  1034. *
  1035. * Parameters: pEditInstance - Editor instance information structure.
  1036. *
  1037. * unStartRedrawLine - Line of first character to draw.
  1038. *
  1039. * unStartRedrawColumn - Column of first character to draw.
  1040. *
  1041. * unFinishRedrawLine - Line of last character to draw.
  1042. *
  1043. * unFinishRedrawColumn - Column after last character to draw.
  1044. *
  1045. * Return: A rough estimate of the number of bytes required for the redraw.
  1046. */
  1047. static UINT ODEditEstDrawBytes(tEditInstance *pEditInstance,
  1048. UINT unStartRedrawLine, UINT unStartRedrawColumn, UINT unFinishRedrawLine,
  1049. UINT unFinishRedrawColumn)
  1050. {
  1051. UINT unAreaLine;
  1052. UINT unBufferLine;
  1053. UINT unLineLength;
  1054. UINT unByteTally = 0;
  1055. /* If we are only drawing text on a single line, then estimate is just */
  1056. /* the distance between the start and finish redraw column. This number */
  1057. /* is precise only if the cursor is already at the location where */
  1058. /* output is to begin, and the final cursor position is the location */
  1059. /* where output finishes. This is in fact the situation when the user */
  1060. /* is entering new text in the middle of the line - the most common */
  1061. /* situation that will be encountered. */
  1062. if(unStartRedrawLine == unFinishRedrawLine)
  1063. {
  1064. return(unFinishRedrawColumn - unStartRedrawColumn);
  1065. }
  1066. /* If we are drawing text on multiple lines, then inspect the contents */
  1067. /* of those lines to estimate the number of bytes to be transmitted. */
  1068. for(unAreaLine = unStartRedrawLine,
  1069. unBufferLine = pEditInstance->unLineScrolledToTop + unStartRedrawLine;
  1070. unAreaLine <= unFinishRedrawLine;
  1071. ++unAreaLine, ++unBufferLine)
  1072. {
  1073. /* Determine the length of this line. */
  1074. if(unBufferLine < pEditInstance->unLinesInBuffer)
  1075. {
  1076. unLineLength = ODEditBufferGetLineLength(pEditInstance, unBufferLine);
  1077. if(unAreaLine == unStartRedrawLine)
  1078. {
  1079. unLineLength -= unStartRedrawColumn;
  1080. }
  1081. }
  1082. else
  1083. {
  1084. unLineLength = 0;
  1085. }
  1086. /* Add the number of characters on this line, along with the number of */
  1087. /* bytes required to reposition the cursor and to clear the unused */
  1088. /* portion of this line to the tally. This assumes that the edit area */
  1089. /* spans the entire screen. */
  1090. unByteTally += unLineLength + 7;
  1091. }
  1092. return(unByteTally);
  1093. }
  1094. /* ----------------------------------------------------------------------------
  1095. * ODEditGetCurrentLineInArea() *** PRIVATE FUNCTION ***
  1096. *
  1097. * Determines which line of the edit area the cursor is currently located in.
  1098. *
  1099. * Parameters: pEditInstance - Editor instance information structure.
  1100. *
  1101. * Return: 0-based index of the distance from the top of the edit area
  1102. * (as specified in the user options structure) where the
  1103. * cursor is currently located.
  1104. */
  1105. static UINT ODEditGetCurrentLineInArea(tEditInstance *pEditInstance)
  1106. {
  1107. ASSERT(pEditInstance != NULL);
  1108. return(pEditInstance->unCurrentLine - pEditInstance->unLineScrolledToTop);
  1109. }
  1110. /* ----------------------------------------------------------------------------
  1111. * ODEditUpdateCursorPos() *** PRIVATE FUNCTION ***
  1112. *
  1113. * Unconditionally updates the position of the cursor on the screen to
  1114. * reflect its actual position. Compare with ODEditUpdateCursorIfMoved().
  1115. *
  1116. * Parameters: pEditInstance - Editor instance information structure.
  1117. *
  1118. * Return: void
  1119. */
  1120. static void ODEditUpdateCursorPos(tEditInstance *pEditInstance)
  1121. {
  1122. ASSERT(pEditInstance != NULL);
  1123. /* Reposition the cursor on the screen. */
  1124. od_set_cursor(ODEditGetCurrentLineInArea(pEditInstance)
  1125. + pEditInstance->pUserOptions->nAreaTop,
  1126. pEditInstance->unCurrentColumn + pEditInstance->pUserOptions->nAreaLeft);
  1127. }
  1128. /* ----------------------------------------------------------------------------
  1129. * ODEditUpdateCursorIfMoved() *** PRIVATE FUNCTION ***
  1130. *
  1131. * Updates the position of the cursor on the screen to reflec its actual
  1132. * position only if we belive it isn't already there. Compare with
  1133. * ODEditUpdateCursorPos().
  1134. *
  1135. * Parameters: pEditInstance - Editor instance information structure.
  1136. *
  1137. * Return: void
  1138. */
  1139. static void ODEditUpdateCursorIfMoved(tEditInstance *pEditInstance)
  1140. {
  1141. UINT unActualRow;
  1142. UINT unActualColumn;
  1143. ODEditGetActualCurPos(pEditInstance, &unActualRow, &unActualColumn);
  1144. if(!(unActualRow == ODEditGetCurrentLineInArea(pEditInstance)
  1145. + pEditInstance->pUserOptions->nAreaTop
  1146. && unActualColumn == pEditInstance->unCurrentColumn
  1147. + pEditInstance->pUserOptions->nAreaLeft))
  1148. {
  1149. ODEditUpdateCursorPos(pEditInstance);
  1150. }
  1151. }
  1152. /* ----------------------------------------------------------------------------
  1153. * ODEditEnterText() *** PRIVATE FUNCTION ***
  1154. *
  1155. * Inserts new text at the current cursor position, updating the cursor
  1156. * position accordingly.
  1157. *
  1158. * Parameters: pEditInstance - Editor instance information structure.
  1159. *
  1160. * pszEntered - The character(s) to be inserted.
  1161. *
  1162. * Return: kODRCSuccess on success, kODRCSafeFailure if we were unable to
  1163. * obtain enough buffer space before making any changes, or
  1164. * kODRCUnrecoverableFailure if the buffer has been changed, but
  1165. * there was insufficient memory to re-index the buffer.
  1166. */
  1167. static tODResult ODEditEnterText(tEditInstance *pEditInstance,
  1168. char *pszEntered, BOOL bInsertMode)
  1169. {
  1170. UINT unNumCharsToAdd;
  1171. char *pch;
  1172. tODResult Result;
  1173. ASSERT(pEditInstance != NULL);
  1174. ASSERT(pszEntered != NULL);
  1175. /* Remember initial edit area contents, to permit selective redraw. */
  1176. ODEditRememberArea(pEditInstance, pEditInstance->pRememberBuffer);
  1177. /* Determine the number of characters that are to be added to the buffer. */
  1178. unNumCharsToAdd = strlen(pszEntered);
  1179. /* Make room in the buffer for the new characters, if needed. */
  1180. if(bInsertMode)
  1181. {
  1182. Result = ODEditBufferMakeSpace(pEditInstance,
  1183. pEditInstance->unCurrentLine, pEditInstance->unCurrentColumn,
  1184. unNumCharsToAdd);
  1185. if(Result != kODRCSuccess)
  1186. {
  1187. /* Beep on failure. */
  1188. od_putch('\a');
  1189. return(Result);
  1190. }
  1191. }
  1192. /* Copy the new characters to the buffer. */
  1193. pch = ODEditBufferGetCharacter(pEditInstance,
  1194. pEditInstance->unCurrentLine, pEditInstance->unCurrentColumn);
  1195. memcpy(pch, pszEntered, unNumCharsToAdd);
  1196. /* Move the cursor position to the end of the newly added text. The */
  1197. /* cursor position may be temporarily assigned to a position that is */
  1198. /* past the end of the edit area or past the end of the buffer. */
  1199. for(pch = pszEntered; *pch != '\0'; ++pch)
  1200. {
  1201. if(IS_EOL_CHAR(*pch))
  1202. {
  1203. /* A carriage return character advances the cursor to the */
  1204. /* leftmost column on the next line. */
  1205. pEditInstance->unCurrentColumn = 0;
  1206. pEditInstance->unCurrentLine++;
  1207. /* If the next character is a different EOL character, and is */
  1208. /* not the end of the string, then skip that character. */
  1209. if(IS_EOL_CHAR(pch[1]) && pch[1] != '\0' && pch[1] != *pch)
  1210. {
  1211. ++pch;
  1212. }
  1213. }
  1214. else
  1215. {
  1216. /* All other characters move the cursor ahead one column. */
  1217. pEditInstance->unCurrentColumn++;
  1218. }
  1219. }
  1220. /* Reindex and reformat the buffer based on its new contents. */
  1221. if(!ODEditBufferFormatAndIndex(pEditInstance))
  1222. {
  1223. return(kODRCUnrecoverableFailure);
  1224. }
  1225. /* Check whether the cursor is now positioned past the end of the edit */
  1226. /* area, requiring the edit area to be scrolled up. */
  1227. if(ODEditGetCurrentLineInArea(pEditInstance)
  1228. >= pEditInstance->unAreaHeight)
  1229. {
  1230. /* We need to scroll the area accordingly. */
  1231. /* Distance to scroll is maximum of the current single-step scroll */
  1232. /* distance, and the distance that the cursor is positioned below */
  1233. /* the bottom of the current edit area. */
  1234. UINT unScrollDistance = MAX(pEditInstance->unScrollDistance,
  1235. ODEditGetCurrentLineInArea(pEditInstance) -
  1236. pEditInstance->unAreaHeight + 1);
  1237. /* Perform actual scroll operation. */
  1238. if(ODEditScrollArea(pEditInstance, (INT)unScrollDistance))
  1239. {
  1240. /* Entire area wasn't redrawn by operation, so some redrawing */
  1241. /* may still be required, within the area of text that was */
  1242. /* visible before the scroll operation. */
  1243. ODEditRedrawChanged(pEditInstance, pEditInstance->pRememberBuffer,
  1244. 0, pEditInstance->unAreaHeight - unScrollDistance);
  1245. }
  1246. }
  1247. /* Perform necessary redrawing and reposition the cursor if needed. */
  1248. ODEditRedrawChanged(pEditInstance, pEditInstance->pRememberBuffer,
  1249. REDRAW_NO_BOUNDARY, REDRAW_NO_BOUNDARY);
  1250. /* Return with success. */
  1251. return(kODRCSuccess);
  1252. }
  1253. /* ----------------------------------------------------------------------------
  1254. * ODEditSetBreakSequence() *** PRIVATE FUNCTION ***
  1255. *
  1256. * If the default line or paragraph break sequence has not yet been set, then
  1257. * this function sets it based on the break sequence that was encountered in
  1258. * the buffer supplied by the client application.
  1259. *
  1260. * Parameters: pEditInstance - Editor instance information structure.
  1261. *
  1262. * chFirstEOLChar - First character in the encountered sequence.
  1263. *
  1264. * chSecondEOLChar - Second character in the encountered sequence.
  1265. *
  1266. * Return: void
  1267. */
  1268. static void ODEditSetBreakSequence(tEditInstance *pEditInstance,
  1269. char chFirstEOLChar, char chSecondEOLChar)
  1270. {
  1271. char *pszSequence;
  1272. ASSERT(pEditInstance != NULL);
  1273. if(pEditInstance->pszParagraphBreak != NULL
  1274. && pEditInstance->pszLineBreak != NULL)
  1275. {
  1276. /* In this situation, both the default line break sequence and default */
  1277. /* paragraph break sequence have been set, so there is nothing for us */
  1278. /* to do. */
  1279. return;
  1280. }
  1281. /* Obtain a pointer to the encountered sequence. We want to use a static */
  1282. /* string constant for this, so that the string will continue to exist, */
  1283. /* in unchanged form. */
  1284. if(chFirstEOLChar == '\r' && chSecondEOLChar == '\0')
  1285. {
  1286. pszSequence = "\r";
  1287. }
  1288. else if(chFirstEOLChar == '\n' && chSecondEOLChar == '\0')
  1289. {
  1290. pszSequence = "\n";
  1291. }
  1292. else if(chFirstEOLChar == '\n' && chSecondEOLChar == '\r')
  1293. {
  1294. pszSequence = "\n\r";
  1295. }
  1296. else if(chFirstEOLChar == '\r' && chSecondEOLChar == '\n')
  1297. {
  1298. pszSequence = "\r\n";
  1299. }
  1300. else
  1301. {
  1302. /* This should never happen: an invalid end of line sequence was */
  1303. /* passed in. */
  1304. pszSequence = NULL;
  1305. ASSERT(FALSE);
  1306. }
  1307. /* Set the as yet undetermined line/paragraph terminators. */
  1308. if(pEditInstance->pszParagraphBreak == NULL)
  1309. {
  1310. pEditInstance->pszParagraphBreak = pszSequence;
  1311. }
  1312. if(pEditInstance->pszLineBreak == NULL)
  1313. {
  1314. pEditInstance->pszLineBreak = pszSequence;
  1315. }
  1316. }
  1317. /* ----------------------------------------------------------------------------
  1318. * ODEditCursorLeft() *** PRIVATE FUNCTION ***
  1319. *
  1320. * Attempts to move the cursor left. Called when the user presses the left
  1321. * arrow key, and is also called as part of the process of handling the
  1322. * backspace key.
  1323. *
  1324. * Parameters: pEditInstance - Editor instance information structure.
  1325. *
  1326. * Return: TRUE on success, or FALSE if the cursor cannot be moved left any
  1327. * further.
  1328. */
  1329. static BOOL ODEditCursorLeft(tEditInstance *pEditInstance)
  1330. {
  1331. ASSERT(pEditInstance != NULL);
  1332. /* In word wrap mode, pressing the left key when the cursor */
  1333. /* is past the end of the line jumps the cursor to the end */
  1334. /* of the line. */
  1335. if(pEditInstance->bWordWrapLongLines &&
  1336. pEditInstance->unCurrentColumn > ODEditBufferGetLineLength
  1337. (pEditInstance, pEditInstance->unCurrentLine))
  1338. {
  1339. pEditInstance->unCurrentColumn = ODEditBufferGetLineLength
  1340. (pEditInstance, pEditInstance->unCurrentLine);
  1341. return(TRUE);
  1342. }
  1343. /* If we are not already at the leftmost column. */
  1344. else if(pEditInstance->unCurrentColumn > 0)
  1345. {
  1346. /* Move left one column. */
  1347. --pEditInstance->unCurrentColumn;
  1348. return(TRUE);
  1349. }
  1350. else if(pEditInstance->bWordWrapLongLines)
  1351. {
  1352. /* In word wrap mode, this will move us up to the end of */
  1353. /* the previous line. */
  1354. if(pEditInstance->unCurrentLine > 0)
  1355. {
  1356. ODEditGotoPreviousLine(pEditInstance);
  1357. pEditInstance->unCurrentColumn = ODEditBufferGetLineLength(
  1358. pEditInstance, pEditInstance->unCurrentLine);
  1359. return(TRUE);
  1360. }
  1361. }
  1362. /* It wasn't possible to move the cursor. */
  1363. return(FALSE);
  1364. }
  1365. /* ----------------------------------------------------------------------------
  1366. * ODEditDeleteCurrentChar() *** PRIVATE FUNCTION ***
  1367. *
  1368. * Deletes the character at the current cursor position, performing necessary
  1369. * redraw. Pressing the delete key causes just this function to be called.
  1370. * Pressing the backspace key causes ODEditCursorLeft() to be called to first
  1371. * move the cursor left before calling ODEditDeleteCurrentChar().
  1372. *
  1373. * Parameters: pEditInstance - Editor instance information structure.
  1374. *
  1375. * Return: void
  1376. */
  1377. static void ODEditDeleteCurrentChar(tEditInstance *pEditInstance)
  1378. {
  1379. char *pch;
  1380. ASSERT(pEditInstance != NULL);
  1381. /* Remember initial edit area contents, to permit selective redraw. */
  1382. ODEditRememberArea(pEditInstance, pEditInstance->pRememberBuffer);
  1383. /* Backup the entire buffer contents by one character. */
  1384. pch = ODEditBufferGetCharacter(pEditInstance,
  1385. pEditInstance->unCurrentLine, pEditInstance->unCurrentColumn);
  1386. memmove(pch, pch + 1, strlen(pch + 1) + 1);
  1387. /* Reindex and reformat the buffer based on its new contents. */
  1388. ODEditBufferFormatAndIndex(pEditInstance);
  1389. /* Perform necessary redrawing and reposition the cursor if needed. */
  1390. ODEditRedrawChanged(pEditInstance, pEditInstance->pRememberBuffer,
  1391. REDRAW_NO_BOUNDARY, REDRAW_NO_BOUNDARY);
  1392. }
  1393. /* ----------------------------------------------------------------------------
  1394. * ODEditDeleteCurrentLine() *** PRIVATE FUNCTION ***
  1395. *
  1396. * Removes the entire current line from the buffer.
  1397. *
  1398. * Parameters: pEditInstance - Editor instance information structure.
  1399. *
  1400. * Return: void
  1401. */
  1402. static void ODEditDeleteCurrentLine(tEditInstance *pEditInstance)
  1403. {
  1404. char *pszStartOfThisLine;
  1405. char *pszStartOfNextLine;
  1406. ASSERT(pEditInstance != NULL);
  1407. /* Remember initial edit area contents, to permit selective redraw. */
  1408. ODEditRememberArea(pEditInstance, pEditInstance->pRememberBuffer);
  1409. /* Determine start of this line. */
  1410. pszStartOfThisLine = ODEditBufferGetCharacter(pEditInstance,
  1411. pEditInstance->unCurrentLine, 0);
  1412. if(pEditInstance->unLinesInBuffer == pEditInstance->unCurrentLine + 1)
  1413. {
  1414. /* If this is the last line of the buffer, then we just remove */
  1415. /* everything from this line, without actually removing the line. */
  1416. *pszStartOfThisLine = '\0';
  1417. }
  1418. else
  1419. {
  1420. /* If this is not the last line of the buffer, then remove this */
  1421. /* entire line, so that the next line will become the current */
  1422. /* line. */
  1423. pszStartOfNextLine = ODEditBufferGetCharacter(pEditInstance,
  1424. pEditInstance->unCurrentLine + 1, 0);
  1425. memmove(pszStartOfThisLine, pszStartOfNextLine,
  1426. strlen(pszStartOfNextLine) + 1);
  1427. }
  1428. /* Reset the cursor position to the beginning of the current line. */
  1429. pEditInstance->unCurrentColumn = 0;
  1430. /* Reindex and reformat the buffer based on its new contents. */
  1431. ODEditBufferFormatAndIndex(pEditInstance);
  1432. /* Perform necessary redrawing and reposition the cursor if needed. */
  1433. ODEditRedrawChanged(pEditInstance, pEditInstance->pRememberBuffer,
  1434. REDRAW_NO_BOUNDARY, REDRAW_NO_BOUNDARY);
  1435. }
  1436. /* ----------------------------------------------------------------------------
  1437. * ODEditPastEndOfCurLine() *** PRIVATE FUNCTION ***
  1438. *
  1439. * Determines whether the cursor is currently past the end of the current line.
  1440. * The end of the line is considered to be the first column after the last
  1441. * character on the line. So, on a blank line, a cursor is considered to be
  1442. * past the end if it is in or past the second column (column 0).
  1443. *
  1444. * Parameters: pEditInstance - Editor instance information structure.
  1445. *
  1446. * Return: TRUE if the cursor is past the end of the current line,
  1447. * FALSE if it is not.
  1448. */
  1449. static BOOL ODEditPastEndOfCurLine(tEditInstance *pEditInstance)
  1450. {
  1451. ASSERT(pEditInstance != NULL);
  1452. return(pEditInstance->unCurrentColumn >
  1453. ODEditBufferGetLineLength(pEditInstance, pEditInstance->unCurrentLine));
  1454. }
  1455. /* ----------------------------------------------------------------------------
  1456. * ODEditRememberBufferSize() *** PRIVATE FUNCTION ***
  1457. *
  1458. * Determines the buffer size required by ODEditRememberArea().
  1459. *
  1460. * Parameters: pEditInstance - Editor instance information structure.
  1461. *
  1462. * Return: the required buffer size, in size_t units (bytes).
  1463. */
  1464. static size_t ODEditRememberBufferSize(tEditInstance *pEditInstance)
  1465. {
  1466. ASSERT(pEditInstance != NULL);
  1467. return((pEditInstance->unAreaWidth + 1)
  1468. * pEditInstance->unAreaHeight);
  1469. }
  1470. /* ----------------------------------------------------------------------------
  1471. * ODEditRememberArea() *** PRIVATE FUNCTION ***
  1472. *
  1473. * Stores a copy of the text currently displayed in the edit area in the
  1474. * provided buffer, so that it can later be reused to redraw only the portion
  1475. * of the edit area which has been changed by an operation.
  1476. *
  1477. * Parameters: pEditInstance - Editor instance information structure.
  1478. *
  1479. * pRememberedArea - Pointer to a buffer, which is at least
  1480. * the number of bytes specified by
  1481. * ODEditRememberBufferSize() in size.
  1482. *
  1483. * Return: void
  1484. */
  1485. static void ODEditRememberArea(tEditInstance *pEditInstance,
  1486. void *pRememberedArea)
  1487. {
  1488. UINT unDataLineOffset = 0;
  1489. UINT unDataLineSize;
  1490. UINT unAreaLine;
  1491. UINT unBufferLine;
  1492. UINT unLineLength;
  1493. char *pchStartOfLine;
  1494. char *pchDataLocation;
  1495. ASSERT(pEditInstance != NULL);
  1496. ASSERT(pRememberedArea != NULL);
  1497. /* Determine the length of a single line in the remember buffer. */
  1498. unDataLineSize = pEditInstance->unAreaWidth + 1;
  1499. pchDataLocation = (char *)pRememberedArea + unDataLineOffset;
  1500. for(unBufferLine = pEditInstance->unLineScrolledToTop, unAreaLine = 0;
  1501. unAreaLine < pEditInstance->unAreaHeight;
  1502. ++unAreaLine, ++unBufferLine)
  1503. {
  1504. /* If this line is not beyond the end of the buffer. */
  1505. if(unBufferLine < pEditInstance->unLinesInBuffer)
  1506. {
  1507. /* Determine the length of this buffer line. */
  1508. unLineLength = ODEditBufferGetLineLength(pEditInstance, unBufferLine);
  1509. /* Determine the start location of this buffer line. */
  1510. pchStartOfLine = ODEditBufferGetCharacter(pEditInstance, unBufferLine,
  1511. 0);
  1512. }
  1513. else
  1514. {
  1515. /* If this line is beyond the end of the buffer, then it is empty. */
  1516. unLineLength = 0;
  1517. pchStartOfLine = "";
  1518. }
  1519. /* Copy the contents of this line to the data buffer. */
  1520. memcpy(pchDataLocation, pchStartOfLine, unLineLength);
  1521. pchDataLocation[unLineLength] = '\0';
  1522. /* Update the location where data is being stored in the buffer. */
  1523. pchDataLocation += unDataLineSize;
  1524. }
  1525. }
  1526. /* ----------------------------------------------------------------------------
  1527. * ODEditRedrawChanged() *** PRIVATE FUNCTION ***
  1528. *
  1529. * Redraws the portion of the edit area which has been changed by an operation,
  1530. * based on the original edit area contents as stored in a buffer by the
  1531. * ODEditRememberArea() function.
  1532. *
  1533. * Parameters: pEditInstance - Editor instance information structure.
  1534. *
  1535. * pRememberedArea - Pointer to a buffer that was filled by a
  1536. * previous call to ODEditRememberArea().
  1537. *
  1538. * unUpperBoundary - The first line in the edit area to consider
  1539. * redrawing, or REDRAW_NO_BOUNDARY to specify
  1540. * the top of the edit area.
  1541. *
  1542. * unLowerBoundary - The last line in the edit area to consider
  1543. * redrawing, or REDRAW_NO_BOUNDARY to specify
  1544. * the bottom of the edit area.
  1545. *
  1546. * Return: void
  1547. */
  1548. static void ODEditRedrawChanged(tEditInstance *pEditInstance,
  1549. void *pRememberedArea, UINT unUpperBoundary, UINT unLowerBoundary)
  1550. {
  1551. UINT unStartRedrawLine;
  1552. UINT unStartRedrawColumn;
  1553. UINT unFinishRedrawLine;
  1554. UINT unFinishRedrawColumn;
  1555. UINT unEstPartialRedrawBytes;
  1556. ASSERT(pEditInstance != NULL);
  1557. ASSERT(pRememberedArea != NULL);
  1558. /* Determine what portion of the edit area, within the specified upper */
  1559. /* and lower boundaries, has been changed. */
  1560. if(!ODEditDetermineChanged(pEditInstance, pRememberedArea, unUpperBoundary,
  1561. unLowerBoundary, &unStartRedrawLine, &unStartRedrawColumn,
  1562. &unFinishRedrawLine, &unFinishRedrawColumn))
  1563. {
  1564. /* Nothing has changed in the edit area. */
  1565. ODEditUpdateCursorIfMoved(pEditInstance);
  1566. return;
  1567. }
  1568. /* Now that we have determined the portion of the edit area that would */
  1569. /* be redraw by a partial (incremental) redraw, we compare the amount */
  1570. /* of data involved + the amount of data still in the outbound buffer */
  1571. /* with the amount of data involved in a full redraw. If the amount of */
  1572. /* data in the outbound buffer cannot be determined, we default to */
  1573. /* using an incremental redraw. */
  1574. unEstPartialRedrawBytes = ODEditEstDrawBytes(pEditInstance,
  1575. unStartRedrawLine, unStartRedrawColumn, unFinishRedrawLine,
  1576. unFinishRedrawColumn);
  1577. if(ODEditRecommendFullRedraw(pEditInstance, unEstPartialRedrawBytes,
  1578. FALSE))
  1579. {
  1580. /* Purge the outbound buffer and do a full redraw. */
  1581. ODEditRedrawArea(pEditInstance);
  1582. /* Move the cursor back to its appropriate position. */
  1583. ODEditUpdateCursorPos(pEditInstance);
  1584. }
  1585. else
  1586. {
  1587. /* Perform a partial (incremental) redraw. */
  1588. /* Now, redraw the portion of the edit area that has been determined to */
  1589. /* require redrawing. */
  1590. ODEditRedrawSubArea(pEditInstance, unStartRedrawLine, unStartRedrawColumn,
  1591. unFinishRedrawLine, unFinishRedrawColumn);
  1592. /* Now, move the cursor back to its appropriate position, if it isn't */
  1593. /* already there. */
  1594. ODEditUpdateCursorIfMoved(pEditInstance);
  1595. }
  1596. }
  1597. /* ----------------------------------------------------------------------------
  1598. * ODEditDetermineChanged() *** PRIVATE FUNCTION ***
  1599. *
  1600. * Determines what portion of the edit area, within the specifiede upper and
  1601. * lower boundary, has been changed. Area is specified as row and column
  1602. * position of the first changed characer between boundaries, and the row
  1603. * and column position past the last changed character between the boundaries.
  1604. *
  1605. * Parameters: pEditInstance - Editor instance information structure.
  1606. *
  1607. * pRememberedArea - Pointer to a buffer that was filled by a
  1608. * previous call to ODEditRememberArea().
  1609. *
  1610. * unUpperBoundary - The first line in the edit area to
  1611. * consider redrawing, or
  1612. * REDRAW_NO_BOUNDARY to specify the top of
  1613. * the edit area.
  1614. *
  1615. * unLowerBoundary - The last line in the edit area to
  1616. * consider redrawing, or
  1617. * REDRAW_NO_BOUNDARY to specify the bottom
  1618. * of the edit area.
  1619. *
  1620. * punStartRedrawLine - Output: Line of first changed character.
  1621. *
  1622. * punStartRedrawColumn - Output: Column of first changed char.
  1623. *
  1624. * punFinishRedrawLine - Output: Line of last changed character.
  1625. *
  1626. * punFinishRedrawColumn - Output: Column after last changed char.
  1627. *
  1628. * Return: TRUE if some text in the edit area has been changed, FALSE
  1629. * if there is no change.
  1630. */
  1631. static BOOL ODEditDetermineChanged(tEditInstance *pEditInstance,
  1632. void *pRememberedArea, UINT unUpperBoundary, UINT unLowerBoundary,
  1633. UINT *punStartRedrawLine, UINT *punStartRedrawColumn,
  1634. UINT *punFinishRedrawLine, UINT *punFinishRedrawColumn)
  1635. {
  1636. BOOL bFoundStart = FALSE;
  1637. BOOL bFoundFinish = FALSE;
  1638. UINT unDataLineOffset = 0;
  1639. UINT unDataLineSize;
  1640. UINT unAreaLine;
  1641. UINT unLineLength;
  1642. UINT unColumn;
  1643. UINT unBufferLine;
  1644. char *pchCurrent;
  1645. char *pchRemembered;
  1646. /* Determine the length of a single line in the remember buffer. */
  1647. unDataLineSize = pEditInstance->unAreaWidth + 1;
  1648. /* If caller specified no upper boundary, then reset upper boundary */
  1649. /* to 0. */
  1650. if(unUpperBoundary == REDRAW_NO_BOUNDARY) unUpperBoundary = 0;
  1651. /* Likewise, iff caller specified no lower boundary, then reset the */
  1652. /* lower boundary to the bottom of the edit area. */
  1653. if(unLowerBoundary == REDRAW_NO_BOUNDARY)
  1654. {
  1655. unLowerBoundary = pEditInstance->unAreaHeight;
  1656. }
  1657. /* Loop through the area within boundaries, determining which */
  1658. /* portion of the edit area has changed. */
  1659. for(unBufferLine = pEditInstance->unLineScrolledToTop + unUpperBoundary,
  1660. unAreaLine = unUpperBoundary; unAreaLine < unLowerBoundary;
  1661. ++unAreaLine, ++unBufferLine)
  1662. {
  1663. /* Determine location of corresponding line in remembered data. */
  1664. pchRemembered = (char *)pRememberedArea + unDataLineOffset
  1665. + unDataLineSize * unAreaLine;
  1666. /* If this line is not beyond the end of the buffer. */
  1667. if(unBufferLine < pEditInstance->unLinesInBuffer)
  1668. {
  1669. /* Determine the start location of this buffer line. */
  1670. pchCurrent = ODEditBufferGetCharacter(pEditInstance, unBufferLine, 0);
  1671. /* Determine the length of this buffer line. */
  1672. unLineLength = ODEditBufferGetLineLength(pEditInstance, unBufferLine);
  1673. }
  1674. else
  1675. {
  1676. pchCurrent = "";
  1677. unLineLength = 0;
  1678. }
  1679. /* Start at the first column on this line. */
  1680. unColumn = 0;
  1681. /* Look for any characters that differ. */
  1682. for(;; ++unColumn, ++pchCurrent, ++pchRemembered)
  1683. {
  1684. /* Determine if we are at the end of the remembered line. */
  1685. BOOL bEndOfRemembered = (*pchRemembered == '\0');
  1686. /* Determine if we are at the end of the current buffer line. */
  1687. BOOL bEndOfCurrent = (unColumn == unLineLength);
  1688. /* If we are at the end of either of the buffers (but not both), */
  1689. /* or if these two characters differ, then we have found the */
  1690. /* start of the area that must be redrawn. */
  1691. if(!(bEndOfRemembered && bEndOfCurrent))
  1692. {
  1693. if(bEndOfRemembered || bEndOfCurrent
  1694. || *pchCurrent != *pchRemembered)
  1695. {
  1696. if(bFoundStart)
  1697. {
  1698. bFoundFinish = FALSE;
  1699. }
  1700. else
  1701. {
  1702. /* We have found a character that differs. */
  1703. bFoundStart = TRUE;
  1704. *punStartRedrawLine = unAreaLine;
  1705. *punStartRedrawColumn = unColumn;
  1706. }
  1707. }
  1708. }
  1709. /* If we have found the first changed text in the buffer, then we */
  1710. /* are now looking for the last changed text in the buffer. */
  1711. if(bFoundStart && !bFoundFinish)
  1712. {
  1713. if(*pchCurrent == *pchRemembered)
  1714. {
  1715. bFoundFinish = TRUE;
  1716. *punFinishRedrawLine = unAreaLine;
  1717. *punFinishRedrawColumn = unColumn;
  1718. }
  1719. else if(bEndOfRemembered)
  1720. {
  1721. bFoundFinish = TRUE;
  1722. *punFinishRedrawLine = unAreaLine;
  1723. *punFinishRedrawColumn = unLineLength;
  1724. }
  1725. else if(bEndOfCurrent)
  1726. {
  1727. bFoundFinish = TRUE;
  1728. *punFinishRedrawLine = unAreaLine;
  1729. *punFinishRedrawColumn = unColumn + strlen(pchRemembered);
  1730. }
  1731. }
  1732. /* If we are at the end of either buffers. */
  1733. if(bEndOfRemembered || bEndOfCurrent)
  1734. {
  1735. /* Now, proceed to processing the next line in the edit area. */
  1736. break;
  1737. }
  1738. }
  1739. }
  1740. /* If we haven't found any text in the edit area that has changed. */
  1741. if(!bFoundStart)
  1742. {
  1743. /* Then return indicating no change. */
  1744. return(FALSE);
  1745. }
  1746. /* If we haven't found an end to the portion of the area that has */
  1747. /* changed, then we must redraw up to the end of the edit area. */
  1748. if(!bFoundFinish)
  1749. {
  1750. *punFinishRedrawLine = unLowerBoundary;
  1751. *punFinishRedrawColumn = unColumn;
  1752. }
  1753. /* Return indicating that ther has been some change. */
  1754. return(TRUE);
  1755. }
  1756. /* ----------------------------------------------------------------------------
  1757. * ODEditRedrawSubArea() *** PRIVATE FUNCTION ***
  1758. *
  1759. * Redraws the portion of the edit area within the specified range. Redrawing
  1760. * is performed from the location of the start redraw row and column, up to
  1761. * but not includin gthe finish redraw row and column.
  1762. *
  1763. * Parameters: pEditInstance - Editor instance information structure.
  1764. *
  1765. * unStartRedrawLine - Line of first character to draw.
  1766. *
  1767. * unStartRedrawColumn - Column of first character to draw.
  1768. *
  1769. * unFinishRedrawLine - Line of last character to draw.
  1770. *
  1771. * unFinishRedrawColumn - Column after last character to draw.
  1772. *
  1773. * Return: void
  1774. */
  1775. static void ODEditRedrawSubArea(tEditInstance *pEditInstance,
  1776. UINT unStartRedrawLine, UINT unStartRedrawColumn, UINT unFinishRedrawLine,
  1777. UINT unFinishRedrawColumn)
  1778. {
  1779. UINT unAreaLine;
  1780. UINT unLineLength;
  1781. UINT unBufferLine;
  1782. char *pchCurrent;
  1783. UINT unStartColumn;
  1784. UINT unFinishColumn;
  1785. UINT unScrnStartColumn;
  1786. UINT unTextLength;
  1787. /* Now, perform actual redraw in area that requires redrawing. */
  1788. for(unBufferLine = pEditInstance->unLineScrolledToTop + unStartRedrawLine,
  1789. unAreaLine = unStartRedrawLine; unAreaLine <= unFinishRedrawLine;
  1790. ++unBufferLine, ++unAreaLine)
  1791. {
  1792. BOOL bFirstLine = (unAreaLine == unStartRedrawLine);
  1793. BOOL bLastLine = (unAreaLine == unFinishRedrawLine);
  1794. UINT unScrnRow = (UINT)pEditInstance->pUserOptions->nAreaTop
  1795. + unAreaLine;
  1796. /* If this line is not beyond the end of the buffer. */
  1797. if(unBufferLine < pEditInstance->unLinesInBuffer)
  1798. {
  1799. pchCurrent = ODEditBufferGetCharacter(pEditInstance, unBufferLine, 0);
  1800. unTextLength = unLineLength =
  1801. ODEditBufferGetLineLength(pEditInstance, unBufferLine);
  1802. }
  1803. else
  1804. {
  1805. pchCurrent = "";
  1806. unTextLength = unLineLength = 0;
  1807. }
  1808. /* Move to the position on the first line to begin redraw. */
  1809. if(bFirstLine)
  1810. {
  1811. UINT unActualRow;
  1812. UINT unActualColumn;
  1813. ODEditGetActualCurPos(pEditInstance, &unActualRow, &unActualColumn);
  1814. unStartColumn = unStartRedrawColumn;
  1815. unScrnStartColumn = (UINT)pEditInstance->pUserOptions->nAreaLeft
  1816. + unStartColumn;
  1817. if(unScrnRow != unActualRow || unScrnStartColumn != unActualColumn)
  1818. {
  1819. od_set_cursor(unScrnRow, unScrnStartColumn);
  1820. }
  1821. pchCurrent += unStartRedrawColumn;
  1822. unTextLength -= unStartRedrawColumn;
  1823. }
  1824. else
  1825. {
  1826. unStartColumn = 0;
  1827. unScrnStartColumn = (UINT)pEditInstance->pUserOptions->nAreaLeft;
  1828. od_set_cursor(unScrnRow, unScrnStartColumn);
  1829. }
  1830. /* If this is the last line to redraw, then adjust accordingly. */
  1831. if(bLastLine)
  1832. {
  1833. if(unFinishRedrawColumn < unLineLength)
  1834. {
  1835. unTextLength -= unLineLength - unFinishRedrawColumn;
  1836. }
  1837. unFinishColumn = unFinishRedrawColumn;
  1838. }
  1839. else
  1840. {
  1841. unFinishColumn = pEditInstance->unAreaWidth;
  1842. }
  1843. /* Output changed text. */
  1844. if(unStartColumn < unLineLength)
  1845. {
  1846. od_disp(pchCurrent, unTextLength, TRUE);
  1847. unStartColumn += unTextLength;
  1848. }
  1849. /* If we need to clear the rest of the line. */
  1850. if(unFinishColumn == pEditInstance->unAreaWidth)
  1851. {
  1852. /* If right edge of edit area aligns with the right edge of the */
  1853. /* screen. */
  1854. if(pEditInstance->pUserOptions->nAreaRight == OD_SCREEN_WIDTH)
  1855. {
  1856. /* Clear the remainder of this line on the screen. */
  1857. od_clr_line();
  1858. }
  1859. else
  1860. {
  1861. /* Place spaces after the end of the current line, up to right */
  1862. /* edge of the edit area. */
  1863. od_repeat(' ', (BYTE)(pEditInstance->unAreaWidth - unLineLength));
  1864. }
  1865. }
  1866. else if(unStartColumn < unFinishColumn)
  1867. {
  1868. od_repeat(' ', (BYTE)(unFinishColumn - unStartColumn));
  1869. }
  1870. }
  1871. }
  1872. /* ----------------------------------------------------------------------------
  1873. * ODEditGetActualCurPos() *** PRIVATE FUNCTION ***
  1874. *
  1875. * Estimates the actual position of the cursor on the screen.
  1876. *
  1877. * Parameters: pEditInstance - Editor instance information structure.
  1878. *
  1879. * punRow - Pointer to location where cursor row number
  1880. * should be stored.
  1881. *
  1882. * punColumn - Pointer to location where cursor column number
  1883. * should be stored.
  1884. *
  1885. * Return: void
  1886. */
  1887. static void ODEditGetActualCurPos(tEditInstance *pEditInstance,
  1888. UINT *punRow, UINT *punColumn)
  1889. {
  1890. tODScrnTextInfo TextInfo;
  1891. ASSERT(pEditInstance != NULL);
  1892. ASSERT(punRow != NULL);
  1893. ASSERT(punColumn != NULL);
  1894. UNUSED(pEditInstance);
  1895. /* Obtain current cursor position information from the ODScrn module. */
  1896. ODScrnGetTextInfo(&TextInfo);
  1897. *punRow = (UINT)TextInfo.cury;
  1898. *punColumn = (UINT)TextInfo.curx;
  1899. }
  1900. /* ----------------------------------------------------------------------------
  1901. * ODEditIsEOLForMode() *** PRIVATE FUNCTION ***
  1902. *
  1903. * Determines whether the specified character should be treated as an EOL
  1904. * character for the current mode.
  1905. *
  1906. * Parameters: pEditInstance - Editor instance information structure.
  1907. *
  1908. * Return: TRUE if this is an EOL character, FALSE otherwise.
  1909. */
  1910. static BOOL ODEditIsEOLForMode(tEditInstance *pEditInstance, char chToTest)
  1911. {
  1912. switch(pEditInstance->pUserOptions->TextFormat)
  1913. {
  1914. case FORMAT_FTSC_MESSAGE:
  1915. return(chToTest == '\r' || chToTest == '\0');
  1916. default:
  1917. return(IS_EOL_CHAR(chToTest));
  1918. }
  1919. }
  1920. /* ========================================================================= */
  1921. /* Low level buffer manipulation functions. */
  1922. /* ========================================================================= */
  1923. /* ----------------------------------------------------------------------------
  1924. * ODEditBufferFormatAndIndex() *** PRIVATE FUNCTION ***
  1925. *
  1926. * Regenerates the count of lines in the buffer, and the array of pointers to
  1927. * the start of each line.
  1928. *
  1929. * Parameters: pEditInstance - Editor instance information structure.
  1930. *
  1931. * Return: TRUE on success, or FALSE if there is not enough memory
  1932. * available to complete this operation.
  1933. */
  1934. static BOOL ODEditBufferFormatAndIndex(tEditInstance *pEditInstance)
  1935. {
  1936. char *pch;
  1937. char *pchLastSpace;
  1938. UINT unProcessingColumn;
  1939. UINT unProcessingLine;
  1940. BOOL bAtEndOfBuffer = FALSE;
  1941. BOOL bLineEndedByBreak;
  1942. BOOL bFTSCMode =
  1943. (pEditInstance->pUserOptions->TextFormat == FORMAT_FTSC_MESSAGE);
  1944. ASSERT(pEditInstance != NULL);
  1945. /* Reset current line count. */
  1946. unProcessingLine = 0;
  1947. /* Begin at the beginning of the buffer to edit. */
  1948. pch = pEditInstance->pszEditBuffer;
  1949. ASSERT(pch != NULL);
  1950. /* Loop for each line in the buffer. */
  1951. while(!bAtEndOfBuffer)
  1952. {
  1953. /* In FTSC mode, skip a line if it begins with a ^A ("kludge lines"). */
  1954. if(bFTSCMode)
  1955. {
  1956. /* Loop while the current line begins with a ^A. */
  1957. while(*pch == 0x01)
  1958. {
  1959. /* Loop until the end of the line is found. */
  1960. while(!ODEditIsEOLForMode(pEditInstance, *pch)) ++pch;
  1961. if(*pch == '\0')
  1962. {
  1963. /* If the line was ended by a null character, then note that */
  1964. /* the end of the buffer has been reached. */
  1965. bAtEndOfBuffer = TRUE;
  1966. }
  1967. else
  1968. {
  1969. /* If the line was not ended by a null character, then skip */
  1970. /* the end of line character. */
  1971. ++pch;
  1972. }
  1973. }
  1974. continue;
  1975. }
  1976. /* Add the address of the start of this line to the line array. */
  1977. /* If the line array is full, then attempt to grow it. */
  1978. ASSERT(unProcessingLine <= pEditInstance->unLineArraySize);
  1979. if(unProcessingLine == pEditInstance->unLineArraySize)
  1980. {
  1981. /* Determine the size to grow the array to. */
  1982. UINT unNewArraySize = pEditInstance->unLineArraySize
  1983. + LINE_ARRAY_GROW_SIZE;
  1984. /* Attempt to reallocate this memory block. */
  1985. char **papchNewLineArray = (char **)realloc(
  1986. pEditInstance->papchStartOfLine, unNewArraySize * sizeof(char *));
  1987. /* If reallocation failed, then return with failure. */
  1988. if(papchNewLineArray == NULL)
  1989. {
  1990. return(FALSE);
  1991. }
  1992. /* Otherwise, update the editor instance information with the new */
  1993. /* array address and array size information. */
  1994. pEditInstance->papchStartOfLine = papchNewLineArray;
  1995. pEditInstance->unLineArraySize = unNewArraySize;
  1996. }
  1997. /* Add the address of the start of this line to the array. */
  1998. pEditInstance->papchStartOfLine[unProcessingLine] = pch;
  1999. /* Reset word wrap information. */
  2000. pchLastSpace = NULL;
  2001. /* Now, find the end of this line. */
  2002. bLineEndedByBreak = TRUE;
  2003. unProcessingColumn = 0;
  2004. while(!ODEditIsEOLForMode(pEditInstance, *pch))
  2005. {
  2006. /* If this character is a space, then record the location of the */
  2007. /* last space characters. */
  2008. if(*pch == ' ') pchLastSpace = pch;
  2009. /* Check for characters which must be filtered from the buffer */
  2010. /* in FTSC message mode. */
  2011. if(bFTSCMode)
  2012. {
  2013. if(*pch == 0x0a || ((unsigned char)*pch) == 0x8d)
  2014. {
  2015. /* If this character must be removed, then move rest of */
  2016. /* buffer up by one character, and proceed to next loop */
  2017. /* iteration. */
  2018. memmove(pch, pch + 1, strlen(pch + 1) + 1);
  2019. continue;
  2020. }
  2021. }
  2022. /* Increment count of characters on this line. */
  2023. ++unProcessingColumn;
  2024. /* Check whether we have reached the maximum number of characters */
  2025. /* that will fit on this line. */
  2026. if(unProcessingColumn >= pEditInstance->unAreaWidth - 1)
  2027. {
  2028. if(pEditInstance->bWordWrapLongLines)
  2029. {
  2030. /* If we are to word wrap long lines, then back up to the */
  2031. /* beginning of the last word, if we have encountered any */
  2032. /* space characters. */
  2033. if(pchLastSpace != NULL && pchLastSpace < pch)
  2034. {
  2035. /* Update current column number accordingly. */
  2036. unProcessingColumn -= (UINT)(pch - pchLastSpace);
  2037. /* Back up to position to perform word wrap at. */
  2038. pch = pchLastSpace;
  2039. }
  2040. }
  2041. /* If we are wrapping text where the cursor is located, then we */
  2042. /* will have to reposition the cursor accordingly. */
  2043. if(unProcessingLine == pEditInstance->unCurrentLine
  2044. && unProcessingColumn < pEditInstance->unCurrentColumn)
  2045. {
  2046. /* Move the cursor to the next line. */
  2047. pEditInstance->unCurrentLine++;
  2048. /* Adjust the cursor column number to the position where the */
  2049. /* corresponding wrapped text will appear. */
  2050. pEditInstance->unCurrentColumn -= unProcessingColumn;
  2051. }
  2052. /* Note that line was not ended by en explicit line break. */
  2053. bLineEndedByBreak = FALSE;
  2054. break;
  2055. }
  2056. /* Move to the next character in the buffer. */
  2057. ++pch;
  2058. }
  2059. /* If we the line was terminated by a '\0', then note that the end of */
  2060. /* the buffer has been reached. */
  2061. if(*pch == '\0')
  2062. {
  2063. bAtEndOfBuffer = TRUE;
  2064. }
  2065. /* If the line was not terminated by a '\0', then find the first */
  2066. /* character of the next line. */
  2067. else
  2068. {
  2069. char chFirstEOLChar = *pch;
  2070. char chSecondEOLChar = '\0';
  2071. /* Move to the next character in the buffer. */
  2072. ++pch;
  2073. /* If this character is a different EOL sequence character from the */
  2074. /* already encountered EOL character, then skip past it too. */
  2075. if(ODEditIsEOLForMode(pEditInstance, chFirstEOLChar) && *pch != '\0'
  2076. && ODEditIsEOLForMode(pEditInstance, *pch)
  2077. && *pch != chFirstEOLChar)
  2078. {
  2079. chSecondEOLChar = *pch;
  2080. ++pch;
  2081. }
  2082. /* If we don't already know what form of line/paragraph break to */
  2083. /* use (just a CR, just a LF, a CR/LF sequence or a LF/CR */
  2084. /* sequence), then use this line termination as an example of */
  2085. /* what should be used. */
  2086. if(bLineEndedByBreak)
  2087. {
  2088. ODEditSetBreakSequence(pEditInstance, chFirstEOLChar,
  2089. chSecondEOLChar);
  2090. }
  2091. }
  2092. /* Increment the count of the current line number. */
  2093. unProcessingLine++;
  2094. }
  2095. /* Update count of number of lines in the buffer, based on the number */
  2096. /* that we have found. */
  2097. pEditInstance->unLinesInBuffer = unProcessingLine;
  2098. /* Return with success. */
  2099. return(TRUE);
  2100. }
  2101. /* ----------------------------------------------------------------------------
  2102. * ODEditBufferGetLineLength() *** PRIVATE FUNCTION ***
  2103. *
  2104. * Determines the length of the specified line in the buffer, not including
  2105. * any line terminator characters.
  2106. *
  2107. * Parameters: pEditInstance - Editor instance information structure.
  2108. *
  2109. * unBufferLine - 0-based index of the line in question.
  2110. *
  2111. * Return: The number of characters on this line.
  2112. */
  2113. static UINT ODEditBufferGetLineLength(tEditInstance *pEditInstance,
  2114. UINT unBufferLine)
  2115. {
  2116. char *pch;
  2117. char *pchStartOfLine;
  2118. UINT unCharsBeforeEOL;
  2119. ASSERT(pEditInstance != NULL);
  2120. ASSERT(unBufferLine < pEditInstance->unLinesInBuffer);
  2121. ASSERT(pEditInstance->unLinesInBuffer <= pEditInstance->unLineArraySize);
  2122. /* Get the address of the start of this line in the buffer. */
  2123. pchStartOfLine
  2124. = ODEditBufferGetCharacter(pEditInstance, unBufferLine, 0);
  2125. /* Count the number of characters before the next end of line character. */
  2126. for(pch = pchStartOfLine, unCharsBeforeEOL = 0;
  2127. !ODEditIsEOLForMode(pEditInstance, *pch);
  2128. ++unCharsBeforeEOL, ++pch);
  2129. /* If this is the last line in the buffer, then the number of characers */
  2130. /* before the next end of line character is the length of this line. */
  2131. if(unBufferLine >= pEditInstance->unLinesInBuffer - 1)
  2132. {
  2133. return(unCharsBeforeEOL);
  2134. }
  2135. /* If this is not the last line in the buffer, then the length of this */
  2136. /* line is the minimum of the number of characters before the next end */
  2137. /* of line character and the number of characters before the next line, */
  2138. /* according to the line index information. This is because all lines */
  2139. /* do not necessarily end with an end-of-line character. */
  2140. else
  2141. {
  2142. return(MIN(unCharsBeforeEOL, (UINT)(ODEditBufferGetCharacter(
  2143. pEditInstance, unBufferLine + 1, 0) - pchStartOfLine)));
  2144. }
  2145. }
  2146. /* ----------------------------------------------------------------------------
  2147. * ODEditBufferGetTotalLines() *** PRIVATE FUNCTION ***
  2148. *
  2149. * Determines the number of lines in the current edit buffer.
  2150. *
  2151. * Parameters: pEditInstance - Editor instance information structure.
  2152. *
  2153. * Return: The total number of lines in the buffer.
  2154. */
  2155. static UINT ODEditBufferGetTotalLines(tEditInstance *pEditInstance)
  2156. {
  2157. ASSERT(pEditInstance != NULL);
  2158. ASSERT(pEditInstance->unLinesInBuffer <= pEditInstance->unLineArraySize);
  2159. /* Return the total number of lines in the buffer. */
  2160. return(pEditInstance->unLinesInBuffer);
  2161. }
  2162. /* ----------------------------------------------------------------------------
  2163. * ODEditBufferGetCharacter() *** PRIVATE FUNCTION ***
  2164. *
  2165. * Obtains the character at the specified position in the buffer.
  2166. *
  2167. * Parameters: pEditInstance - Editor instance information structure.
  2168. *
  2169. * unBufferLine - 0-based index of the line in question.
  2170. *
  2171. * unBufferColumn - The position in the line of the required
  2172. * character.
  2173. *
  2174. * Return: A pointer to the character at the specified position in the
  2175. * specified line. The caller can assume that any remaining
  2176. * character(s) in the line follow this character, but should
  2177. * not assume that the pointer can be used to access following
  2178. * lines in the buffer.
  2179. */
  2180. static char *ODEditBufferGetCharacter(tEditInstance *pEditInstance,
  2181. UINT unBufferLine, UINT unBufferColumn)
  2182. {
  2183. ASSERT(pEditInstance != NULL);
  2184. ASSERT(unBufferLine < pEditInstance->unLinesInBuffer);
  2185. ASSERT(pEditInstance->unLinesInBuffer <= pEditInstance->unLineArraySize);
  2186. ASSERT(unBufferColumn <= ODEditBufferGetLineLength(pEditInstance, unBufferLine));
  2187. /* The position of this character is the position of this line, plus */
  2188. /* the number of characters into the line. */
  2189. return(pEditInstance->papchStartOfLine[unBufferLine] + unBufferColumn);
  2190. }
  2191. /* ----------------------------------------------------------------------------
  2192. * ODEditBufferMakeSpace() *** PRIVATE FUNCTION ***
  2193. *
  2194. * Moves the remaining buffer contents by the specified distance to make room
  2195. * for new text. The new space is filled by space (' ') characters. Does not
  2196. * necessarily reindex the buffer before returning, and so this should be done
  2197. * by the caller after the new buffer space has been filled.
  2198. *
  2199. * Parameters: pEditInstance - Editor instance information structure.
  2200. *
  2201. * unLine - Line number to make more room on.
  2202. *
  2203. * unColumn - Column number to insert the space.
  2204. *
  2205. * unNumChars - Number of characters to make room for.
  2206. *
  2207. * Return: kODRCSuccess on success, kODRCSafeFailure if we were unable to
  2208. * obtain enough buffer space before making any changes, or
  2209. * kODRCUnrecoverableFailure if the buffer has been changed, but
  2210. * there was insufficient memory to re-index the buffer.
  2211. */
  2212. static tODResult ODEditBufferMakeSpace(tEditInstance *pEditInstance,
  2213. UINT unLine, UINT unColumn, UINT unNumChars)
  2214. {
  2215. UINT unLineLength;
  2216. UINT unBufferUsed;
  2217. UINT unBufferUnused;
  2218. UINT unRemainingBufferBytes;
  2219. UINT unCount;
  2220. char *pchBufferPos;
  2221. tODResult Result;
  2222. ASSERT(pEditInstance != NULL);
  2223. ASSERT(unLine < pEditInstance->unLinesInBuffer);
  2224. /* Determine the current length of the specified line. */
  2225. unLineLength = ODEditBufferGetLineLength(pEditInstance, unLine);
  2226. /* If a column past the current end of this line was specified, then */
  2227. /* adjust column and number of characters in order to extend the line */
  2228. /* up to the specified column as well as adding space beginning at that */
  2229. /* column. */
  2230. if(unColumn > unLineLength)
  2231. {
  2232. UINT unExtendLineBy = unColumn - unLineLength;
  2233. unColumn -= unExtendLineBy;
  2234. unNumChars += unExtendLineBy;
  2235. }
  2236. /* Now, determine whether the buffer is large enough for the additional */
  2237. /* space that will be added. */
  2238. unBufferUsed = strlen(pEditInstance->pszEditBuffer) + 1;
  2239. unBufferUnused = pEditInstance->unBufferSize - unBufferUsed;
  2240. if(unBufferUnused < unNumChars)
  2241. {
  2242. /* There is not currently sufficient room in the buffer for the new */
  2243. /* characters, then attempt to grow the buffer to make more room. */
  2244. Result = ODEditTryToGrow(pEditInstance, unBufferUsed + unNumChars);
  2245. if(Result != kODRCSuccess)
  2246. {
  2247. /* On failure, return the result code that indicates the nature */
  2248. /* of the failure. */
  2249. return(Result);
  2250. }
  2251. }
  2252. /* Now, shift the buffer contents from this location forward by */
  2253. /* unNumChars characters. */
  2254. pchBufferPos = ODEditBufferGetCharacter(pEditInstance, unLine, unColumn);
  2255. unRemainingBufferBytes = strlen(pchBufferPos) + 1;
  2256. memmove(pchBufferPos + unNumChars, pchBufferPos, unRemainingBufferBytes);
  2257. /* Next, we fill the new buffer area with space characters. */
  2258. for(unCount = 0; unCount < unNumChars; ++unCount)
  2259. {
  2260. *pchBufferPos++ = ' ';
  2261. }
  2262. /* Return with success. */
  2263. return(kODRCSuccess);
  2264. }
  2265. /* ----------------------------------------------------------------------------
  2266. * ODEditTryToGrow() *** PRIVATE FUNCTION ***
  2267. *
  2268. * Attempts to reallocate the buffer to a larger size. This function is called
  2269. * if it is found that the current buffer isn't large enough to complete some
  2270. * operation. If the client application has setup the editor to permit buffer
  2271. * reallocation, then this function will call the realloc callback function
  2272. * supplied by the client application. If buffer growing is not possible, then
  2273. * this function automatically fails.
  2274. *
  2275. * Parameters: pEditInstance - Editor instance information structure.
  2276. *
  2277. * unSizeNeeded - The minimum buffer size that is needed.
  2278. *
  2279. * Return: kODRCSuccess on success, kODRCSafeFailure if we were unable to
  2280. * obtain enough buffer space before making any changes, or
  2281. * kODRCUnrecoverableFailure if the buffer has been changed, but
  2282. * there was insufficient memory to re-index the buffer.
  2283. */
  2284. static tODResult ODEditTryToGrow(tEditInstance *pEditInstance,
  2285. UINT unSizeNeeded)
  2286. {
  2287. BOOL bFullReIndexRequired = FALSE;
  2288. ASSERT(pEditInstance != NULL);
  2289. ASSERT(unSizeNeeded > pEditInstance->unBufferSize);
  2290. if(pEditInstance->pUserOptions->pfBufferRealloc != NULL)
  2291. {
  2292. /* If the buffer is growable, then attempt to grow it using the */
  2293. /* realloc function provided by the client application. */
  2294. UINT unNewBufferSize = MAX(pEditInstance->unBufferSize
  2295. + BUFFER_GROW_SIZE, unSizeNeeded);
  2296. char *pszNewBuffer = (char *)((*pEditInstance->pUserOptions->
  2297. pfBufferRealloc)(pEditInstance->pszEditBuffer, unNewBufferSize));
  2298. /* If we were unable to grow the buffer, then fail now. At this */
  2299. /* point, nothing has changed, and so the buffer information */
  2300. /* is still intact and valid. */
  2301. if(pszNewBuffer == NULL)
  2302. {
  2303. return(kODRCSafeFailure);
  2304. }
  2305. /* Otherwise, determine whether the entire buffer will now have to */
  2306. /* be reindexed. This is necessary if the reallocated buffer is at */
  2307. /* a new location than the original was. */
  2308. if(pszNewBuffer != pEditInstance->pszEditBuffer)
  2309. {
  2310. bFullReIndexRequired = TRUE;
  2311. }
  2312. /* Now, store the new buffer pointer and buffer size information. */
  2313. pEditInstance->pszEditBuffer = pszNewBuffer;
  2314. pEditInstance->unBufferSize = unNewBufferSize;
  2315. }
  2316. else
  2317. {
  2318. /* If the buffer is not growable, then fail right away. */
  2319. return(kODRCSafeFailure);
  2320. }
  2321. /* If a full reindex is required due to buffer reallocation, then do so. */
  2322. if(bFullReIndexRequired)
  2323. {
  2324. if(!ODEditBufferFormatAndIndex(pEditInstance))
  2325. {
  2326. /* If this fails, then return with failure. */
  2327. return(kODRCUnrecoverableFailure);
  2328. }
  2329. bFullReIndexRequired = FALSE;
  2330. }
  2331. /* If we get to this point, we suceeded in growing the buffer to the */
  2332. /* required size, so return with success. */
  2333. return(kODRCSuccess);
  2334. }