wcedit.pas 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  1. {
  2. This file is part of the Free Pascal Integrated Development Environment
  3. Copyright (c) 1998-2000 by Berczi Gabor
  4. Code editor template objects
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$i globdir.inc}
  12. unit WCEdit;
  13. interface
  14. uses Objects,Drivers,Views,
  15. WUtils,WEditor;
  16. type
  17. PCodeEditor = ^TCodeEditor;
  18. PIndicator = ^TIndicator;
  19. TIndicator = object(TView)
  20. Location: TPoint;
  21. Modified : Boolean;
  22. CodeOwner : PCodeEditor;
  23. {$ifdef debug}
  24. StoreUndo : Boolean;
  25. SyntaxComplete : boolean;
  26. UseTabs : Boolean;
  27. {$endif debug}
  28. constructor Init(var Bounds: TRect);
  29. procedure Draw; virtual;
  30. function GetPalette: PPalette; virtual;
  31. procedure SetState(AState: Word; Enable: Boolean); virtual;
  32. procedure SetValue(ALocation: TPoint; AModified: Boolean);
  33. end;
  34. PLine = ^TLine;
  35. TLine = object(TCustomLine)
  36. public { internal use only! }
  37. Text : PString;
  38. DefaultEditorInfo : PEditorLineInfo;
  39. EditorInfos : PEditorLineInfoCollection;
  40. Flags : longint;
  41. Owner : PCustomCodeEditorCore;
  42. procedure AddEditorInfo(Index: sw_integer; AEditor: PCustomCodeEditor); virtual;
  43. procedure RemoveEditorInfo(AEditor: PCustomCodeEditor); virtual;
  44. public
  45. constructor Init(AOwner: PCustomCodeEditorCore; const AText: string; AFlags: longint);
  46. function GetText: string; virtual;
  47. procedure SetText(const AText: string); virtual;
  48. function GetEditorInfo(Editor: PCustomCodeEditor): PEditorLineInfo; virtual;
  49. function GetFlags: longint; virtual;
  50. procedure SetFlags(AFlags: longint); virtual;
  51. destructor Done; virtual;
  52. end;
  53. PCodeEditorCore = ^TCodeEditorCore;
  54. TCodeEditorCore = object(TCustomCodeEditorCore)
  55. protected
  56. Lines : PLineCollection;
  57. CanUndo : Boolean;
  58. StoreUndo : boolean;
  59. Modified : Boolean;
  60. ReadOnly : Boolean;
  61. TabSize : integer;
  62. IndentSize : integer;
  63. ModifiedTime : cardinal;
  64. public
  65. UndoList : PEditorActionCollection;
  66. RedoList : PEditorActionCollection;
  67. constructor Init;
  68. destructor Done; virtual;
  69. procedure ChangeLinesTo(ALines : PLineCollection); virtual;
  70. function GetModified: boolean; virtual;
  71. procedure SetModified(AModified: boolean); virtual;
  72. function GetModifyTime: cardinal; virtual;
  73. function GetTabSize: integer; virtual;
  74. procedure SetTabSize(ATabSize: integer); virtual;
  75. function GetIndentSize: integer; virtual;
  76. procedure SetIndentSize(AIndentSize: integer); virtual;
  77. function GetStoreUndo: boolean; virtual;
  78. procedure SetStoreUndo(AStore: boolean); virtual;
  79. function GetSyntaxCompleted: boolean; virtual;
  80. procedure SetSyntaxCompleted(SC : boolean); virtual;
  81. function GetLastSyntaxedLine: sw_integer; virtual;
  82. procedure SetLastSyntaxedLine(ALine: sw_integer); virtual;
  83. { Storage }
  84. protected
  85. { Text & info storage abstraction }
  86. procedure ISetLineFlagState(Binding: PEditorBinding; LineNo: sw_integer; Flag: longint; ASet: boolean); virtual;
  87. procedure IGetDisplayTextFormat(Binding: PEditorBinding; LineNo: sw_integer;var DT,DF:string); virtual;
  88. function IGetLineFormat(Binding: PEditorBinding; LineNo: sw_integer): string; virtual;
  89. procedure ISetLineFormat(Binding: PEditorBinding; LineNo: sw_integer;const S: string); virtual;
  90. public
  91. { Text & info storage abstraction }
  92. function GetLineCount: sw_integer; virtual;
  93. function GetLine(LineNo: sw_integer): PCustomLine; virtual;
  94. function GetLineText(LineNo: sw_integer): string; virtual;
  95. procedure SetDisplayText(I: sw_integer;const S: string); virtual;
  96. function GetDisplayText(I: sw_integer): string; virtual;
  97. procedure SetLineText(I: sw_integer;const S: string); virtual;
  98. procedure DeleteAllLines; virtual;
  99. procedure DeleteLine(I: sw_integer); virtual;
  100. function InsertLine(LineNo: sw_integer; const S: string): PCustomLine; virtual;
  101. procedure AddLine(const S: string); virtual;
  102. procedure GetContent(ALines: PUnsortedStringCollection); virtual;
  103. procedure SetContent(ALines: PUnsortedStringCollection); virtual;
  104. public
  105. { Undo info storage }
  106. procedure AddAction(AAction: byte; AStartPos, AEndPos: TPoint; AText: string;AFlags : longint); virtual;
  107. procedure AddGroupedAction(AAction : byte); virtual;
  108. procedure CloseGroupedAction(AAction : byte); virtual;
  109. function GetUndoActionCount: sw_integer; virtual;
  110. function GetRedoActionCount: sw_integer; virtual;
  111. private
  112. OnDiskLoadTime : cardinal;
  113. SystemLoadTime : cardinal;
  114. procedure LinesInsert(Idx: sw_integer; Line: PLine);
  115. end;
  116. TCodeEditor = object(TCustomCodeEditor)
  117. Core : PCodeEditorCore;
  118. Flags : longint;
  119. Indicator : PIndicator;
  120. HighlightRow: sw_integer;
  121. DebuggerRow: sw_integer;
  122. CodeCompleteFrag: PString;
  123. CodeCompleteWord: PString;
  124. ReadOnly : boolean;
  125. CompleteState: TCompleteState;
  126. ErrorMessage: PString;
  127. IndicatorDrawCalled : boolean;
  128. Folds : PFoldCollection;
  129. MaxFoldLevel: sw_integer;
  130. constructor Init(var Bounds: TRect; AHScrollBar, AVScrollBar:
  131. PScrollBar; AIndicator: PIndicator; ACore: PCodeEditorCore);
  132. public
  133. procedure DrawIndicator; virtual;
  134. public
  135. function GetFlags: longint; virtual;
  136. procedure SetFlags(AFlags: longint); virtual;
  137. function GetModified: boolean; virtual;
  138. procedure SetModified(AModified: boolean); virtual;
  139. function GetStoreUndo: boolean; virtual;
  140. procedure SetStoreUndo(AStore: boolean); virtual;
  141. procedure ClearUndoList;
  142. function GetSyntaxCompleted: boolean; virtual;
  143. procedure SetSyntaxCompleted(SC : boolean); virtual;
  144. function GetLastSyntaxedLine: sw_integer; virtual;
  145. procedure SetLastSyntaxedLine(ALine: sw_integer); virtual;
  146. function GetTabSize: integer; virtual;
  147. procedure SetTabSize(ATabSize: integer); virtual;
  148. function GetIndentSize: integer; virtual;
  149. procedure SetIndentSize(AIndentSize: integer); virtual;
  150. function IsReadOnly: boolean; virtual;
  151. public
  152. procedure UpdateIndicator; virtual;
  153. procedure ModifiedChanged; virtual;
  154. procedure PositionChanged; virtual;
  155. procedure LimitsChanged; virtual;
  156. function IsClipboard: Boolean; virtual;
  157. function LoadFromStream(Stream: PFastBufStream): boolean; virtual;
  158. function SaveToStream(Stream: PStream): boolean; virtual;
  159. function SaveAreaToStream(Stream: PStream; StartP,EndP: TPoint): boolean;virtual;
  160. destructor Done; virtual;
  161. public
  162. { ChangedLine : sw_integer;}
  163. { Text & info storage abstraction }
  164. function GetLineCount: sw_integer; virtual;
  165. function GetLine(LineNo: sw_integer): PCustomLine; virtual;
  166. function CharIdxToLinePos(Line,CharIdx: sw_integer): sw_integer; virtual;
  167. function LinePosToCharIdx(Line,X: sw_integer): sw_integer; virtual;
  168. function GetLineText(I: sw_integer): string; virtual;
  169. procedure SetDisplayText(I: sw_integer;const S: string); virtual;
  170. function GetDisplayText(I: sw_integer): string; virtual;
  171. procedure SetLineText(I: sw_integer;const S: string); virtual;
  172. procedure GetDisplayTextFormat(I: sw_integer;var DT,DF:string); virtual;
  173. function GetLineFormat(I: sw_integer): string; virtual;
  174. procedure SetLineFormat(I: sw_integer;const S: string); virtual;
  175. procedure DeleteAllLines; virtual;
  176. procedure DeleteLine(I: sw_integer); virtual;
  177. function InsertLine(LineNo: sw_integer; const S: string): PCustomLine; virtual;
  178. procedure AddLine(const S: string); virtual;
  179. function GetErrorMessage: string; virtual;
  180. procedure SetErrorMessage(const S: string); virtual;
  181. procedure GetContent(ALines: PUnsortedStringCollection); virtual;
  182. procedure SetContent(ALines: PUnsortedStringCollection); virtual;
  183. procedure Lock; virtual;
  184. procedure UnLock; virtual;
  185. public
  186. { CodeComplete support }
  187. function GetCodeCompleteWord: string; virtual;
  188. procedure SetCodeCompleteWord(const S: string); virtual;
  189. function GetCodeCompleteFrag: string; virtual;
  190. procedure SetCodeCompleteFrag(const S: string); virtual;
  191. function GetCompleteState: TCompleteState; virtual;
  192. procedure SetCompleteState(AState: TCompleteState); virtual;
  193. public
  194. { Syntax highlight }
  195. {a}function UpdateAttrs(FromLine: sw_integer; Attrs: byte): sw_integer; virtual;
  196. {a}function UpdateAttrsRange(FromLine, ToLine: sw_integer; Attrs: byte): sw_integer; virtual;
  197. public
  198. { Undo info storage }
  199. procedure AddAction(AAction: byte; AStartPos, AEndPos: TPoint; AText: string;AFlags : longint); virtual;
  200. procedure AddGroupedAction(AAction : byte); virtual;
  201. procedure CloseGroupedAction(AAction : byte); virtual;
  202. function GetUndoActionCount: sw_integer; virtual;
  203. function GetRedoActionCount: sw_integer; virtual;
  204. procedure JumpToLastCursorPos; virtual;
  205. procedure Undo; virtual;
  206. procedure Redo; virtual;
  207. { Fold support }
  208. function GetMaxFoldLevel: sw_integer; virtual;
  209. function GetFoldCount: sw_integer; virtual;
  210. function GetFold(Index: sw_integer): PFold; virtual;
  211. procedure RegisterFold(AFold: PFold); virtual;
  212. procedure UnRegisterFold(AFold: PFold); virtual;
  213. end;
  214. PFileEditor = ^TFileEditor;
  215. TFileEditor = object(TCodeEditor)
  216. FileName: string;
  217. constructor Init(var Bounds: TRect; AHScrollBar, AVScrollBar:
  218. PScrollBar; AIndicator: PIndicator; ACore: PCodeEditorCore; const AFileName: string);
  219. function Save: Boolean; virtual;
  220. function SaveAs: Boolean; virtual;
  221. function SaveAsk(Force: boolean): Boolean; virtual;
  222. function LoadFile: boolean; virtual;
  223. function ReloadFile: boolean; virtual;
  224. function SaveFile: boolean; virtual;
  225. function Valid(Command: Word): Boolean; virtual;
  226. procedure HandleEvent(var Event: TEvent); virtual;
  227. function ShouldSave: boolean; virtual;
  228. function IsChangedOnDisk : boolean;
  229. public
  230. procedure BindingsChanged; virtual;
  231. end;
  232. function DefUseSyntaxHighlight(Editor: PFileEditor): boolean;
  233. function DefUseTabsPattern(Editor: PFileEditor): boolean;
  234. const
  235. DefaultCodeEditorFlags : longint =
  236. efBackupFiles+efInsertMode+efAutoIndent+efPersistentBlocks+
  237. {efUseTabCharacters+}efBackSpaceUnindents+efSyntaxHighlight+
  238. efExpandAllTabs+efCodeComplete{+efFolds};
  239. DefaultTabSize : integer = 8;
  240. DefaultIndentSize : integer = 1;
  241. UseSyntaxHighlight : function(Editor: PFileEditor): boolean = {$ifdef fpc}@{$endif}DefUseSyntaxHighlight;
  242. UseTabsPattern : function(Editor: PFileEditor): boolean = {$ifdef fpc}@{$endif}DefUseTabsPattern;
  243. procedure RegisterWCEdit;
  244. implementation
  245. uses Dos,
  246. WConsts,
  247. FVConsts,
  248. App,WViews;
  249. {$ifndef NOOBJREG}
  250. const
  251. RIndicator: TStreamRec = (
  252. ObjType: 1100;
  253. VmtLink: Ofs(TypeOf(TIndicator)^);
  254. Load: @TIndicator.Load;
  255. Store: @TIndicator.Store
  256. );
  257. RCodeEditor: TStreamRec = (
  258. ObjType: 1101;
  259. VmtLink: Ofs(TypeOf(TCodeEditor)^);
  260. Load: @TCodeEditor.Load;
  261. Store: @TCodeEditor.Store
  262. );
  263. RFileEditor: TStreamRec = (
  264. ObjType: 1102;
  265. VmtLink: Ofs(TypeOf(TFileEditor)^);
  266. Load: @TFileEditor.Load;
  267. Store: @TFileEditor.Store
  268. );
  269. {$endif}
  270. constructor TLine.Init(AOwner: PCustomCodeEditorCore; const AText: string; AFlags: longint);
  271. begin
  272. inherited Init(AText,AFlags);
  273. // New(EditorInfos, Init(10,10));
  274. Owner:=AOwner;
  275. end;
  276. procedure TLine.AddEditorInfo(Index: sw_integer; AEditor: PCustomCodeEditor);
  277. begin
  278. if Index=0 then
  279. begin
  280. DefaultEditorInfo:=New(PEditorLineInfo, Init(AEditor));
  281. exit;
  282. end;
  283. if not assigned(EditorInfos) then
  284. begin
  285. New(EditorInfos, Init(10,10));
  286. EditorInfos^.AtInsert(0,DefaultEditorInfo);
  287. DefaultEditorInfo:=nil;
  288. end;
  289. EditorInfos^.AtInsert(Index,New(PEditorLineInfo, Init(AEditor)));
  290. end;
  291. procedure TLine.RemoveEditorInfo(AEditor: PCustomCodeEditor);
  292. var E: PEditorLineInfo;
  293. begin
  294. E:=GetEditorInfo(AEditor);
  295. if Assigned(EditorInfos) then
  296. EditorInfos^.Free(E);
  297. end;
  298. function TLine.GetText: string;
  299. begin
  300. GetText:=GetStr(Text);
  301. end;
  302. procedure TLine.SetText(const AText: string);
  303. begin
  304. SetStr(Text,AText);
  305. end;
  306. function TLine.GetEditorInfo(Editor: PCustomCodeEditor): PEditorLineInfo;
  307. function Match(P: PEditorLineInfo): boolean;
  308. begin
  309. Match:=P^.Editor=Editor;
  310. end;
  311. begin
  312. if not assigned(EditorInfos) then
  313. GetEditorInfo:=DefaultEditorInfo
  314. else
  315. GetEditorInfo:=EditorInfos^.FirstThat(@Match);
  316. end;
  317. function TLine.GetFlags: longint;
  318. begin
  319. GetFlags:=Flags;
  320. end;
  321. procedure TLine.SetFlags(AFlags: longint);
  322. begin
  323. Flags:=AFlags;
  324. if Assigned(Owner) then
  325. Owner^.ContentsChanged;
  326. end;
  327. destructor TLine.Done;
  328. begin
  329. if Assigned(Text) then
  330. DisposeStr(Text);
  331. Text:=nil;
  332. if Assigned(EditorInfos) then
  333. Dispose(EditorInfos, Done);
  334. EditorInfos:=nil;
  335. if Assigned(DefaultEditorInfo) then
  336. Dispose(DefaultEditorInfo, Done);
  337. DefaultEditorInfo:=nil;
  338. inherited Done;
  339. end;
  340. constructor TCodeEditorCore.Init;
  341. begin
  342. inherited Init;
  343. StoreUndo:=true;
  344. new(UndoList,init(500,1000));
  345. new(RedoList,init(500,1000));
  346. New(Lines, Init(500,1000));
  347. TabSize:=DefaultTabSize;
  348. IndentSize:=DefaultIndentSize;
  349. OnDiskLoadTime:=0;
  350. SystemLoadTime:=0;
  351. end;
  352. procedure TCodeEditorCore.ChangeLinesTo(ALines : PLineCollection);
  353. begin
  354. if assigned(lines) then
  355. Dispose(Lines,Done);
  356. Lines:=ALines;
  357. end;
  358. function TCodeEditorCore.GetLineCount: sw_integer;
  359. begin
  360. GetLineCount:=Lines^.Count;
  361. end;
  362. function TCodeEditorCore.GetLine(LineNo: sw_integer): PCustomLine;
  363. begin
  364. GetLine:=Lines^.At(LineNo);
  365. end;
  366. function TCodeEditorCore.GetModified: boolean;
  367. begin
  368. GetModified:=Modified;
  369. end;
  370. function TCodeEditorCore.GetModifyTime: cardinal;
  371. begin
  372. GetModifyTime:=ModifiedTime;
  373. end;
  374. procedure TCodeEditorCore.SetModified(AModified: boolean);
  375. begin
  376. if AModified<>Modified then
  377. begin
  378. Modified:=AModified;
  379. ModifiedChanged;
  380. end;
  381. ModifiedTime:=cardinal(Now);
  382. end;
  383. function TCodeEditorCore.GetStoreUndo: boolean;
  384. begin
  385. GetStoreUndo:=StoreUndo;
  386. end;
  387. procedure TCodeEditorCore.SetStoreUndo(AStore: boolean);
  388. begin
  389. if StoreUndo<>AStore then
  390. begin
  391. StoreUndo:=AStore;
  392. StoreUndoChanged;
  393. end;
  394. end;
  395. function TCodeEditorCore.GetSyntaxCompleted: boolean;
  396. begin
  397. {$ifdef TEST_PARTIAL_SYNTAX}
  398. GetSyntaxCompleted:=SyntaxComplete;
  399. {$else}
  400. GetSyntaxCompleted:=true;
  401. {$endif}
  402. end;
  403. procedure TCodeEditorCore.SetSyntaxCompleted(SC: boolean);
  404. begin
  405. {$ifdef TEST_PARTIAL_SYNTAX}
  406. if SC<>SyntaxComplete then
  407. begin
  408. SyntaxComplete:=SC;
  409. end;
  410. {$endif}
  411. end;
  412. function TCodeEditorCore.GetLastSyntaxedLine: sw_integer;
  413. begin
  414. GetLastSyntaxedLine:=LastSyntaxedLine;
  415. end;
  416. procedure TCodeEditorCore.SetLastSyntaxedLine(ALine: sw_integer);
  417. begin
  418. LastSyntaxedLine:=ALine;
  419. end;
  420. procedure TCodeEditorCore.ISetLineFlagState(Binding: PEditorBinding; LineNo: sw_integer; Flag: longint; ASet: boolean);
  421. var P: PCustomLine;
  422. begin
  423. if LineNo<GetLineCount then
  424. begin
  425. P:=GetLine(LineNo);
  426. if assigned(P) then
  427. P^.SetFlagState(Flag,ASet);
  428. end;
  429. end;
  430. procedure TCodeEditorCore.GetContent(ALines: PUnsortedStringCollection);
  431. procedure AddIt(P: PCustomLine);
  432. begin
  433. if Assigned(P) then
  434. ALines^.Insert(NewStr(P^.GetText));
  435. end;
  436. begin
  437. if Assigned(Lines) then
  438. Lines^.ForEach(@AddIt);
  439. end;
  440. procedure TCodeEditorCore.SetContent(ALines: PUnsortedStringCollection);
  441. procedure AddIt(P: PString);
  442. begin
  443. AddLine(GetStr(P));
  444. end;
  445. begin
  446. DeleteAllLines;
  447. if Assigned(ALines) then
  448. ALines^.ForEach(@AddIt);
  449. LimitsChanged;
  450. end;
  451. function TCodeEditorCore.GetTabSize: integer;
  452. begin
  453. GetTabSize:=TabSize;
  454. end;
  455. procedure TCodeEditorCore.SetTabSize(ATabSize: integer);
  456. begin
  457. if ATabSize<>TabSize then
  458. begin
  459. TabSize:=ATabSize;
  460. TabSizeChanged;
  461. end;
  462. end;
  463. function TCodeEditorCore.GetIndentSize: integer;
  464. begin
  465. GetIndentSize:=IndentSize;
  466. end;
  467. procedure TCodeEditorCore.SetIndentSize(AIndentSize: integer);
  468. begin
  469. if AIndentSize<>IndentSize then
  470. begin
  471. IndentSize:=AIndentSize;
  472. end;
  473. end;
  474. function TCodeEditorCore.GetLineText(LineNo: sw_integer): string;
  475. var
  476. L : PCustomLine;
  477. begin
  478. GetLineText:='';
  479. if LineNo<Lines^.Count then
  480. begin
  481. L:=Lines^.At(LineNo);
  482. GetLineText:=L^.GetText;
  483. end;
  484. end;
  485. procedure TCodeEditorCore.LinesInsert(Idx: sw_integer; Line: PLine);
  486. var I: sw_integer;
  487. procedure RegLine(P: PEditorBinding);
  488. begin
  489. Line^.AddEditorInfo(I,P^.Editor);
  490. Inc(I);
  491. end;
  492. begin
  493. if Idx=-1 then Idx:=Lines^.Count;
  494. I:=0;
  495. Bindings^.ForEach(@RegLine);
  496. Lines^.AtInsert(Idx,Line);
  497. end;
  498. procedure TCodeEditorCore.SetLineText(I: sw_integer;const S: string);
  499. var
  500. L : PCustomLine;
  501. AddCount : Sw_Integer;
  502. begin
  503. AddCount:=0;
  504. while (Lines^.Count<I+1) do
  505. begin
  506. LinesInsert(-1,New(PLine, Init(@Self,'',0)));
  507. Inc(AddCount);
  508. end;
  509. if AddCount>0 then
  510. LimitsChanged;
  511. L:=Lines^.At(I);
  512. L^.SetText(S);
  513. ContentsChanged;
  514. end;
  515. function TCodeEditorCore.GetDisplayText(I: sw_integer): string;
  516. begin
  517. GetDisplayText:=ExtractTabs(GetLineText(I),GetTabSize);
  518. end;
  519. procedure TCodeEditorCore.SetDisplayText(I: sw_integer;const S: string);
  520. begin
  521. { I disagree here
  522. I don't want the editor to change the position of the tabs
  523. in my makefiles !! PM
  524. if FlagSet(efUseTabCharacters) and (TabSize>0) then
  525. SetLineText(I,CompressUsingTabs(S,TabSize))
  526. else }
  527. { ... then you better make this optional - Gabor }
  528. SetLineText(I,S);
  529. end;
  530. procedure TCodeEditorCore.IGetDisplayTextFormat(Binding: PEditorBinding; LineNo: sw_integer;var DT,DF:string);
  531. var
  532. L : PCustomLine;
  533. P,PAdd : SW_Integer;
  534. begin
  535. DF:='';
  536. DT:='';
  537. if (0<=LineNo) and (LineNo<GetLineCount) then
  538. begin
  539. L:=GetLine(LineNo);
  540. if not assigned(L) then
  541. exit;
  542. DF:=IGetLineFormat(Binding,LineNo);
  543. DT:=L^.GetText;
  544. p:=0;
  545. while p<length(DT) do
  546. begin
  547. inc(p);
  548. if DT[p]=#9 then
  549. begin
  550. PAdd:=TabSize-((p-1) mod TabSize);
  551. if DF<>'' then
  552. DF:=copy(DF,1,P-1)+CharStr(DF[p],PAdd)+copy(DF,P+1,High(DF));
  553. DT:=copy(DT,1,P-1)+CharStr(' ',PAdd)+copy(DT,P+1,High(DF));
  554. inc(P,PAdd-1);
  555. end;
  556. end;
  557. end;
  558. end;
  559. function TCodeEditorCore.IGetLineFormat(Binding: PEditorBinding; LineNo: sw_integer): string;
  560. var P: PCustomLine;
  561. LI: PEditorLineInfo;
  562. S: string;
  563. begin
  564. if (0<=LineNo) and (LineNo<GetLineCount) then
  565. P:=GetLine(LineNo)
  566. else
  567. P:=nil;
  568. if P=nil then LI:=nil else
  569. LI:=P^.GetEditorInfo(Binding^.Editor);
  570. if LI=nil then S:='' else S:=LI^.GetFormat;
  571. IGetLineFormat:=S;
  572. end;
  573. procedure TCodeEditorCore.ISetLineFormat(Binding: PEditorBinding; LineNo: sw_integer;const S: string);
  574. var P: PCustomLine;
  575. LI: PEditorLineInfo;
  576. begin
  577. if (LineNo<GetLineCount) then
  578. begin
  579. P:=GetLine(LineNo);
  580. if P=nil then LI:=nil else LI:=P^.GetEditorInfo(Binding^.Editor);
  581. if Assigned(LI) then LI^.SetFormat(S);
  582. end;
  583. end;
  584. procedure TCodeEditorCore.DeleteAllLines;
  585. begin
  586. if Assigned(Lines) then
  587. Lines^.FreeAll;
  588. end;
  589. procedure TCodeEditorCore.DeleteLine(I: sw_integer);
  590. var
  591. CP : Tpoint;
  592. begin
  593. if I<Lines^.Count then
  594. begin
  595. if StoreUndo then
  596. begin
  597. CP.X:=0;CP.Y:=I;
  598. AddAction(eaDeleteLine,CP,CP,GetLineText(I),0);
  599. end;
  600. Lines^.AtFree(I);
  601. end;
  602. end;
  603. function TCodeEditorCore.InsertLine(LineNo: sw_integer; const S: string): PCustomLine;
  604. var L: PLine;
  605. begin
  606. L:=New(PLine, Init(@Self,S,0));
  607. LinesInsert(LineNo, L);
  608. InsertLine:=L;
  609. end;
  610. procedure TCodeEditorCore.AddLine(const S: string);
  611. begin
  612. LinesInsert(-1,New(PLine, Init(@Self,S,0)));
  613. end;
  614. procedure TCodeEditorCore.AddAction(AAction: byte; AStartPos, AEndPos: TPoint; AText: string;AFlags : longint);
  615. var
  616. ActionIntegrated : boolean;
  617. pa : PEditorAction;
  618. S : String;
  619. begin
  620. if (UndoList=nil) or (not StoreUndo) then Exit;
  621. ActionIntegrated:=false;
  622. if UndoList^.count>0 then
  623. begin
  624. pa:=UndoList^.At(UndoList^.count-1);
  625. if (pa^.action=AAction) and
  626. (pa^.EndPos.X=AStartPos.X) and
  627. (pa^.EndPos.Y=AStartPos.Y) and
  628. { do not group InsertLine and DeleteLine !! }
  629. ((AAction=eaMoveCursor) or
  630. (AAction=eaInsertText) or
  631. (AAction=eaOverwriteText) or
  632. (AAction=eaDeleteText)) and
  633. { do not group if a new grouped_action started }
  634. (not assigned(UndoList^.CurrentGroupedAction) or
  635. (UndoList^.CurrentGroupedAction^.ActionCount>0))
  636. then
  637. begin
  638. pa^.EndPos:=AEndPos;
  639. S:=GetStr(pa^.text);
  640. if S<>'' then
  641. DisposeStr(pa^.text);
  642. if (AAction=eaDeleteText) and
  643. (AStartPos.X>AEndPos.X) then
  644. pa^.text:=NewStr(AText+S)
  645. else
  646. pa^.text:=NewStr(S+AText);
  647. ActionIntegrated:=true;
  648. end;
  649. end;
  650. if not ActionIntegrated then
  651. begin
  652. UndoList^.Insert(New(PEditorAction,Init(AAction,AStartPos,AEndPos,AText,AFlags)));
  653. if assigned(UndoList^.CurrentGroupedAction) then
  654. Inc(UndoList^.CurrentGroupedAction^.actionCount);
  655. UpdateUndoRedo(cmUndo,AAction);
  656. end;
  657. if UndoList^.count>0 then
  658. begin
  659. UpdateUndoRedo(cmRedo,0);
  660. RedoList^.FreeAll;
  661. end;
  662. end;
  663. procedure TCodeEditorCore.AddGroupedAction(AAction : byte);
  664. begin
  665. if (UndoList=nil) or (not StoreUndo) then Exit;
  666. if Assigned(UndoList^.CurrentGroupedAction) then
  667. inc(UndoList^.GroupLevel)
  668. else
  669. begin
  670. UndoList^.CurrentGroupedAction:=New(PEditorAction,Init_group(AAction));
  671. UndoList^.GroupLevel:=1;
  672. end;
  673. end;
  674. procedure TCodeEditorCore.CloseGroupedAction(AAction : byte);
  675. begin
  676. if (UndoList=nil) or (not StoreUndo) then Exit;
  677. dec(UndoList^.GroupLevel);
  678. if UndoList^.GroupLevel=0 then
  679. begin
  680. UndoList^.CurrentGroupedAction^.TimeStamp:=now;
  681. UndoList^.Insert(UndoList^.CurrentGroupedAction);
  682. UndoList^.CurrentGroupedAction:=nil;
  683. UpdateUndoRedo(cmUndo,AAction);
  684. end;
  685. end;
  686. function TCodeEditorCore.GetUndoActionCount: sw_integer;
  687. begin
  688. GetUndoActionCount:=UndoList^.Count;
  689. end;
  690. function TCodeEditorCore.GetRedoActionCount: sw_integer;
  691. begin
  692. GetRedoActionCount:=RedoList^.Count;
  693. end;
  694. destructor TCodeEditorCore.Done;
  695. begin
  696. inherited Done;
  697. if Assigned(Lines) then Dispose(Lines, Done); Lines:=nil;
  698. if Assigned(RedoList) then Dispose(RedoList, Done); RedoList:=nil;
  699. if Assigned(UndoList) then Dispose(UndoList, Done); UndoList:=nil;
  700. end;
  701. constructor TIndicator.Init(var Bounds: TRect);
  702. begin
  703. inherited Init(Bounds);
  704. GrowMode := gfGrowLoY + gfGrowHiY;
  705. end;
  706. procedure TIndicator.Draw;
  707. var
  708. Color: Byte;
  709. Frame: Char;
  710. L: array[0..1] of PtrInt;
  711. S: String[15];
  712. B: TDrawBuffer;
  713. begin
  714. if assigned(CodeOwner) and
  715. (CodeOwner^.ELockFlag>0) then
  716. begin
  717. CodeOwner^.IndicatorDrawCalled:=true;
  718. exit;
  719. end;
  720. if (State and sfDragging = 0) and (State and sfActive <> 0) then
  721. begin
  722. Color := GetColor(1);
  723. Frame := #205;
  724. end
  725. else
  726. begin
  727. if (State and sfDragging)<>0 then
  728. Color := GetColor(2)
  729. else
  730. Color := GetColor(3);
  731. Frame := #196;
  732. end;
  733. MoveChar(B, Frame, Color, Size.X);
  734. if State and sfActive<>0 then
  735. begin
  736. if Modified then
  737. WordRec (B[0]).Lo := ord('*');
  738. {$ifdef debug}
  739. if StoreUndo then
  740. WordRec (B[1]).Lo := ord('S');
  741. if SyntaxComplete then
  742. WordRec(B[2]).lo := ord('C');
  743. if UseTabs then
  744. WordRec(B[3]).lo := ord('T');
  745. {$endif debug}
  746. L[0] := Location.Y + 1;
  747. L[1] := Location.X + 1;
  748. FormatStr(S, ' %d:%d ', L);
  749. MoveStr(B[8 - Pos(':', S)], S, Color);
  750. end;
  751. WriteBuf(0, 0, Size.X, 1, B);
  752. end;
  753. function TIndicator.GetPalette: PPalette;
  754. const
  755. P: string[Length(CIndicator)] = CIndicator;
  756. begin
  757. GetPalette := @P;
  758. end;
  759. procedure TIndicator.SetState(AState: Word; Enable: Boolean);
  760. begin
  761. inherited SetState(AState, Enable);
  762. if (AState = sfDragging) or (AState=sfActive) then
  763. DrawView;
  764. end;
  765. procedure TIndicator.SetValue(ALocation: TPoint; AModified: Boolean);
  766. begin
  767. if (Location.X<>ALocation.X) or
  768. (Location.Y<>ALocation.Y) or
  769. (Modified <> AModified) then
  770. begin
  771. Location := ALocation;
  772. Modified := AModified;
  773. DrawView;
  774. end;
  775. end;
  776. {constructor TIndicator.Load(var S: TStream);
  777. begin
  778. inherited Load(S);
  779. S.Read(Location,SizeOf(Location));
  780. S.Read(Modified,SizeOf(Modified));
  781. end;
  782. procedure TIndicator.Store(var S: TStream);
  783. begin
  784. inherited Store(S);
  785. S.Write(Location,SizeOf(Location));
  786. S.Write(Modified,SizeOf(Modified));
  787. end;}
  788. {*****************************************************************************
  789. TCodeEditor
  790. *****************************************************************************}
  791. constructor TCodeEditor.Init(var Bounds: TRect; AHScrollBar, AVScrollBar:
  792. PScrollBar; AIndicator: PIndicator; ACore: PCodeEditorCore);
  793. begin
  794. inherited Init(Bounds,AHScrollBar,AVScrollBar);
  795. New(Folds, Init(100,100));
  796. if ACore=nil then ACore:=New(PCodeEditorCore, Init);
  797. Core:=ACore;
  798. Core^.BindEditor(@Self);
  799. SetState(sfCursorVis,true);
  800. SetFlags(DefaultCodeEditorFlags);
  801. SetCurPtr(0,0);
  802. Indicator:=AIndicator;
  803. if assigned(Indicator) then
  804. Indicator^.CodeOwner:=@Self;
  805. UpdateIndicator;
  806. LimitsChanged;
  807. end;
  808. function TCodeEditor.GetFlags: longint;
  809. begin
  810. GetFlags:=Flags;
  811. end;
  812. procedure TCodeEditor.SetFlags(AFlags: longint);
  813. var OFlags: longint;
  814. begin
  815. if AFlags<>Flags then
  816. begin
  817. OFlags:=Flags;
  818. Flags:=AFlags;
  819. FlagsChanged(OFlags);
  820. end;
  821. end;
  822. function TCodeEditor.GetModified: boolean;
  823. begin
  824. GetModified:=Core^.GetModified;
  825. end;
  826. procedure TCodeEditor.SetModified(AModified: boolean);
  827. begin
  828. Core^.SetModified(AModified);
  829. end;
  830. function TCodeEditor.GetStoreUndo: boolean;
  831. begin
  832. GetStoreUndo:=Core^.GetStoreUndo;
  833. end;
  834. procedure TCodeEditor.SetStoreUndo(AStore: boolean);
  835. begin
  836. Core^.SetStoreUndo(AStore);
  837. end;
  838. procedure TCodeEditor.ClearUndoList;
  839. begin
  840. Core^.UndoList^.FreeAll;
  841. Core^.RedoList^.FreeAll;
  842. end;
  843. function TCodeEditor.GetSyntaxCompleted: boolean;
  844. begin
  845. GetSyntaxCompleted:=Core^.GetSyntaxCompleted;
  846. end;
  847. procedure TCodeEditor.SetSyntaxCompleted(SC : boolean);
  848. begin
  849. Core^.SetSyntaxCompleted(SC);
  850. UpdateIndicator;
  851. end;
  852. function TCodeEditor.GetLastSyntaxedLine: sw_integer;
  853. begin
  854. GetLastSyntaxedLine:=Core^.GetLastSyntaxedLine;
  855. end;
  856. procedure TCodeEditor.SetLastSyntaxedLine(ALine: sw_integer);
  857. begin
  858. Core^.SetLastSyntaxedLine(ALine);
  859. end;
  860. function TCodeEditor.GetTabSize: integer;
  861. begin
  862. GetTabSize:=Core^.GetTabSize;
  863. end;
  864. procedure TCodeEditor.SetTabSize(ATabSize: integer);
  865. begin
  866. Core^.SetTabSize(ATabSize);
  867. end;
  868. function TCodeEditor.GetIndentSize: integer;
  869. begin
  870. GetIndentSize:=Core^.GetIndentSize;
  871. end;
  872. procedure TCodeEditor.SetIndentSize(AIndentSize: integer);
  873. begin
  874. Core^.SetIndentSize(AIndentSize);
  875. end;
  876. function TCodeEditor.IsReadOnly: boolean;
  877. begin
  878. IsReadOnly:=ReadOnly or (Core^.ReadOnly);
  879. end;
  880. function TCodeEditor.IsClipboard: Boolean;
  881. begin
  882. IsClipboard:=Core^.IsClipboard;
  883. end;
  884. function TCodeEditor.GetErrorMessage: string;
  885. begin
  886. GetErrorMessage:=GetStr(ErrorMessage);
  887. end;
  888. procedure TCodeEditor.SetErrorMessage(const S: string);
  889. begin
  890. SetStr(ErrorMessage,S);
  891. DrawView;
  892. end;
  893. function TCodeEditor.GetLineCount: sw_integer;
  894. begin
  895. GetLineCount:=Core^.GetLineCount;
  896. end;
  897. function TCodeEditor.GetLine(LineNo: sw_integer): PCustomLine;
  898. begin
  899. GetLine:=Core^.GetLine(LineNo);
  900. end;
  901. function TCodeEditor.CharIdxToLinePos(Line,CharIdx: sw_integer): sw_integer;
  902. begin
  903. CharIdxToLinePos:=Core^.CharIdxToLinePos(Line,CharIdx);
  904. end;
  905. function TCodeEditor.LinePosToCharIdx(Line,X: sw_integer): sw_integer;
  906. begin
  907. LinePosToCharIdx:=Core^.LinePosToCharIdx(Line,X);
  908. end;
  909. function TCodeEditor.GetLineText(I: sw_integer): string;
  910. begin
  911. GetLineText:=Core^.GetLineText(I);
  912. end;
  913. procedure TCodeEditor.SetDisplayText(I: sw_integer;const S: string);
  914. begin
  915. Core^.SetDisplayText(I,S);
  916. end;
  917. function TCodeEditor.GetDisplayText(I: sw_integer): string;
  918. begin
  919. GetDisplayText:=Core^.GetDisplayText(I);
  920. end;
  921. procedure TCodeEditor.SetLineText(I: sw_integer;const S: string);
  922. begin
  923. Core^.SetLineText(I,S);
  924. end;
  925. procedure TCodeEditor.GetDisplayTextFormat(I: sw_integer;var DT,DF:string);
  926. begin
  927. Core^.GetDisplayTextFormat(@Self,I,DT,DF);
  928. end;
  929. function TCodeEditor.GetLineFormat(I: sw_integer): string;
  930. begin
  931. GetLineFormat:=Core^.GetLineFormat(@Self,I);
  932. end;
  933. procedure TCodeEditor.SetLineFormat(I: sw_integer;const S: string);
  934. begin
  935. Core^.SetLineFormat(@Self,I,S);
  936. end;
  937. procedure TCodeEditor.DeleteAllLines;
  938. begin
  939. Core^.DeleteAllLines;
  940. end;
  941. procedure TCodeEditor.DeleteLine(I: sw_integer);
  942. begin
  943. Core^.DeleteLine(I);
  944. end;
  945. function TCodeEditor.InsertLine(LineNo: sw_integer; const S: string): PCustomLine;
  946. begin
  947. InsertLine:=Core^.InsertLine(LineNo,S);
  948. end;
  949. procedure TCodeEditor.AddLine(const S: string);
  950. begin
  951. Core^.AddLine(S);
  952. end;
  953. function TCodeEditor.GetMaxFoldLevel: sw_integer;
  954. begin
  955. GetMaxFoldLevel:=MaxFoldLevel;
  956. end;
  957. procedure TCodeEditor.RegisterFold(AFold: PFold);
  958. var L: sw_integer;
  959. begin
  960. if Assigned(Folds) then
  961. begin
  962. Folds^.Insert(AFold);
  963. L:=AFold^.GetLevel+1;
  964. if L>MaxFoldLevel then MaxFoldLevel:=L;
  965. end;
  966. end;
  967. procedure TCodeEditor.UnRegisterFold(AFold: PFold);
  968. begin
  969. if Assigned(Folds) then
  970. begin
  971. Folds^.Delete(AFold);
  972. if Folds^.Count=0 then
  973. MaxFoldLevel:=0
  974. else
  975. MaxFoldLevel:=inherited GetMaxFoldLevel+1;
  976. end;
  977. end;
  978. function TCodeEditor.GetFoldCount: sw_integer;
  979. begin
  980. GetFoldCount:=Folds^.Count;
  981. end;
  982. function TCodeEditor.GetFold(Index: sw_integer): PFold;
  983. begin
  984. GetFold:=Folds^.At(Index);
  985. end;
  986. {function TCodeEditor.GetLineTextPos(Line,X: integer): integer;
  987. var
  988. S: string;
  989. rx,i : Sw_integer;
  990. begin
  991. S:=GetLineText(Line);
  992. i:=0; rx:=0;
  993. while (RX<X) and (i<Length(s)) do
  994. begin
  995. inc(i);
  996. inc(rx);
  997. if s[i]=#9 then
  998. inc(rx,TabSize-(rx mod tabsize));
  999. end;
  1000. if RX<X then Inc(I,X-RX);
  1001. GetLineTextPos:=i;
  1002. end;
  1003. function TCodeEditor.GetDisplayTextPos(Line,X: integer): integer;
  1004. var
  1005. S: string;
  1006. L: PCustomLine;
  1007. rx,i : Sw_integer;
  1008. begin
  1009. S:='';
  1010. if Line<Lines^.Count then
  1011. begin
  1012. L:=Lines^.At(Line);
  1013. if assigned(L^.Text) then
  1014. S:=L^.Text^;
  1015. end;
  1016. i:=0;
  1017. rx:=0;
  1018. while (i<X) and (i<Length(s)) do
  1019. begin
  1020. inc(i);
  1021. inc(rx);
  1022. if s[i]=#9 then
  1023. inc(rx,TabSize-(rx mod tabsize));
  1024. end;
  1025. GetDisplayTextPos:=rx;
  1026. end;}
  1027. procedure TCodeEditor.GetContent(ALines: PUnsortedStringCollection);
  1028. begin
  1029. Core^.GetContent(ALines);
  1030. end;
  1031. procedure TCodeEditor.SetContent(ALines: PUnsortedStringCollection);
  1032. begin
  1033. Lock;
  1034. TextStart; HideSelect;
  1035. Core^.SetContent(ALines);
  1036. LimitsChanged;
  1037. if IsFlagSet(efSyntaxHighlight) then
  1038. Core^.UpdateAttrsRange(0,Min(Delta.Y+Size.Y,GetLineCount-1),
  1039. attrAll
  1040. {$ifndef TEST_PARTIAL_SYNTAX}
  1041. +attrForceFull
  1042. {$endif TEST_PARTIAL_SYNTAX}
  1043. );
  1044. TextStart;
  1045. UnLock;
  1046. end;
  1047. function TCodeEditor.GetCodeCompleteFrag: string;
  1048. begin
  1049. GetCodeCompleteFrag:=GetStr(CodeCompleteFrag);
  1050. end;
  1051. procedure TCodeEditor.SetCodeCompleteFrag(const S: string);
  1052. begin
  1053. SetStr(CodeCompleteFrag,S);
  1054. end;
  1055. function TCodeEditor.GetCompleteState: TCompleteState;
  1056. begin
  1057. GetCompleteState:=CompleteState;
  1058. end;
  1059. procedure TCodeEditor.SetCompleteState(AState: TCompleteState);
  1060. begin
  1061. if AState<>CompleteState then
  1062. begin
  1063. CompleteState:=AState;
  1064. if CompleteState<>csOffering then
  1065. ClearCodeCompleteWord;
  1066. end;
  1067. end;
  1068. function TCodeEditor.GetCodeCompleteWord: string;
  1069. begin
  1070. GetCodeCompleteWord:=GetStr(CodeCompleteWord);
  1071. end;
  1072. procedure TCodeEditor.SetCodeCompleteWord(const S: string);
  1073. begin
  1074. if Assigned(CodeCompleteWord) then DisposeStr(CodeCompleteWord);
  1075. CodeCompleteWord:=NewStr(S);
  1076. inherited SetCodeCompleteWord(S);
  1077. end;
  1078. procedure TCodeEditor.DrawIndicator;
  1079. begin
  1080. if Assigned(Indicator) then
  1081. Indicator^.DrawView;
  1082. end;
  1083. procedure TCodeEditor.Lock;
  1084. begin
  1085. inherited Lock;
  1086. Core^.Lock(@Self);
  1087. end;
  1088. procedure TCodeEditor.UnLock;
  1089. begin
  1090. Core^.UnLock(@Self);
  1091. inherited UnLock;
  1092. If (ELockFlag=0) and IndicatorDrawCalled then
  1093. begin
  1094. DrawIndicator;
  1095. IndicatorDrawCalled:=false;
  1096. end;
  1097. end;
  1098. procedure TCodeEditor.UpdateIndicator;
  1099. begin
  1100. if Indicator<>nil then
  1101. begin
  1102. Indicator^.Location:=CurPos;
  1103. Indicator^.Modified:=GetModified;
  1104. {$ifdef debug}
  1105. Indicator^.StoreUndo:=GetStoreUndo;
  1106. {$ifdef TEST_PARTIAL_SYNTAX}
  1107. Indicator^.SyntaxComplete:=GetSyntaxCompleted and IsFlagSet(efSyntaxHighlight);
  1108. {$endif TEST_PARTIAL_SYNTAX}
  1109. Indicator^.UseTabs:=IsFlagSet(efUseTabCharacters);
  1110. {$endif debug}
  1111. if Elockflag>0 then
  1112. IndicatorDrawCalled:=true
  1113. else
  1114. Indicator^.DrawView;
  1115. end;
  1116. end;
  1117. procedure TCodeEditor.LimitsChanged;
  1118. begin
  1119. Core^.LimitsChanged;
  1120. end;
  1121. procedure TCodeEditor.ModifiedChanged;
  1122. begin
  1123. UpdateIndicator;
  1124. end;
  1125. procedure TCodeEditor.PositionChanged;
  1126. begin
  1127. UpdateIndicator;
  1128. end;
  1129. procedure TCodeEditor.JumpToLastCursorPos;
  1130. var
  1131. pa : PEditorAction;
  1132. begin
  1133. if (Core^.UndoList^.count>0) and (Core^.RedoList^.count=0) then
  1134. begin
  1135. { Or should we just call Undo ?? PM }
  1136. pa:=Core^.UndoList^.At(Core^.UndoList^.count-1);
  1137. if (pa^.action=eaMoveCursor) then
  1138. SetCurPtr(pa^.StartPos.X,pa^.StartPos.Y);
  1139. end;
  1140. end;
  1141. procedure TCodeEditor.Undo;
  1142. var
  1143. Temp,Idx,Last,Count : Longint;
  1144. StoredFlags : longint;
  1145. UndoTime : longint;
  1146. WasInserting,IsGrouped,HadefNoIndent : boolean;
  1147. MaxY,MinY : sw_integer;
  1148. Line : String;
  1149. procedure SetMinMax(y : sw_integer);
  1150. begin
  1151. if MinY=-1 then
  1152. MinY:=Y;
  1153. if Y<MinY then
  1154. MinY:=Y;
  1155. if MaxY=-1 then
  1156. MaxY:=Y;
  1157. if Y>MaxY then
  1158. MaxY:=Y;
  1159. end;
  1160. begin
  1161. Core^.SetStoreUndo(False);
  1162. Lock;
  1163. MinY:=-1;
  1164. MaxY:=-1;
  1165. if Core^.UndoList^.count > 0 then
  1166. begin
  1167. Last:=Core^.UndoList^.count-1;
  1168. if Core^.UndoList^.At(Last)^.Is_grouped_action then
  1169. begin
  1170. Count:=Core^.UndoList^.At(Last)^.ActionCount;
  1171. UndoTime:=Core^.UndoList^.At(Last)^.TimeStamp;
  1172. Dec(Last);
  1173. IsGrouped:=true;
  1174. end
  1175. else
  1176. begin
  1177. Count:=1;
  1178. IsGrouped:=false;
  1179. end;
  1180. for Idx:=Last downto Last-Count+1 do
  1181. with Core^.UndoList^.At(Idx)^ do
  1182. begin
  1183. if not IsGrouped then
  1184. UndoTime:=TimeStamp;
  1185. case action of
  1186. eaMoveCursor :
  1187. begin
  1188. { move cursor back to original position }
  1189. SetCurPtr(startpos.x,startpos.y);
  1190. end;
  1191. eaInsertText :
  1192. begin
  1193. SetCurPtr(StartPos.X,StartPos.Y);
  1194. if assigned(text) then
  1195. for Temp := 1 to length(Text^) do
  1196. DelChar;
  1197. SetMinMax(StartPos.Y);
  1198. end;
  1199. eaDeleteText :
  1200. begin
  1201. { reinsert deleted text }
  1202. SetCurPtr(EndPos.X,EndPos.Y);
  1203. WasInserting:=GetInsertMode;
  1204. SetInsertMode(true);
  1205. if assigned(text) then
  1206. for Temp := 1 to length(Text^) do
  1207. AddChar(Text^[Temp]);
  1208. SetInsertMode(WasInserting);
  1209. SetMinMax(EndPos.Y);
  1210. SetCurPtr(StartPos.X,StartPos.Y);
  1211. end;
  1212. eaOverwriteText :
  1213. begin
  1214. SetCurPtr(StartPos.X,StartPos.Y);
  1215. Line:=GetDisplayText(StartPos.Y);
  1216. WasInserting:=GetInsertMode;
  1217. SetInsertMode(false);
  1218. if assigned(text) then
  1219. for Temp := 1 to length(Text^) do
  1220. begin
  1221. AddChar(Text^[Temp]);
  1222. if StartPos.X+Temp>Length(Line) then
  1223. Text^[Temp]:=' '
  1224. else
  1225. Text^[Temp]:=Line[StartPos.X+Temp];
  1226. end;
  1227. SetInsertMode(WasInserting);
  1228. SetMinMax(EndPos.Y);
  1229. SetCurPtr(StartPos.X,StartPos.Y);
  1230. end;
  1231. eaInsertLine :
  1232. begin
  1233. SetCurPtr(EndPos.X,EndPos.Y);
  1234. Line:=Copy(GetDisplayText(StartPos.Y),1,StartPos.X);
  1235. If Length(Line)<StartPos.X then
  1236. Line:=Line+CharStr(' ',StartPos.X-length(Line))+GetStr(Text);
  1237. SetDisplayText(StartPos.Y,Line+Copy(GetDisplayText(EndPos.Y),EndPos.X+1,255));
  1238. SetMinMax(EndPos.Y);
  1239. SetCurPtr(0,EndPos.Y);
  1240. DeleteLine(EndPos.Y);
  1241. SetCurPtr(StartPos.X,StartPos.Y);
  1242. SetMinMax(StartPos.Y);
  1243. end;
  1244. eaDeleteLine :
  1245. begin
  1246. HadefNoIndent:=(GetFlags and efNoIndent)<>0;
  1247. WasInserting:=GetInsertMode;
  1248. SetInsertMode(true);
  1249. SetFlags(GetFlags or efNoIndent);
  1250. InsertLine(StartPos.Y,GetStr(Text));
  1251. SetInsertMode(WasInserting);
  1252. if not HadefNoIndent then
  1253. SetFlags(GetFlags and not efNoIndent);
  1254. {DelEnd; wrong for eaCut at least }
  1255. SetCurPtr(StartPos.X,StartPos.Y);
  1256. if StartPos.Y > EndPos.Y then
  1257. SetLineText(EndPos.Y,Copy(GetDisplayText(EndPos.Y),1,EndPos.X));
  1258. SetMinMax(StartPos.Y);
  1259. end;
  1260. eaSelectionChanged :
  1261. begin
  1262. { move cursor to end of last set selection }
  1263. end;
  1264. else
  1265. { what the 'ell's an undefined action doing round 'ere mate! }
  1266. ;
  1267. end; { once this lot is done paste into redo and modify to suit needs }
  1268. { move item to redo stack }
  1269. Core^.RedoList^.Insert(Core^.UndoList^.At(Idx));
  1270. UpdateUndoRedo(cmRedo,Core^.UndoList^.At(Idx)^.Action);
  1271. Core^.UndoList^.atDelete(Idx);
  1272. If Idx>0 then
  1273. UpdateUndoRedo(cmUndo,Core^.UndoList^.At(Idx-1)^.Action)
  1274. else
  1275. UpdateUndoRedo(cmUndo,0);
  1276. end;{Idx loop for grouped actions }
  1277. if IsGrouped then
  1278. begin
  1279. Idx:=Core^.UndoList^.Count-1;
  1280. Core^.RedoList^.Insert(Core^.UndoList^.At(Idx));
  1281. UpdateUndoRedo(cmRedo,Core^.UndoList^.At(Idx)^.Action);
  1282. Core^.UndoList^.atDelete(Idx);
  1283. If Idx>0 then
  1284. UpdateUndoRedo(cmUndo,Core^.UndoList^.At(Idx-1)^.Action)
  1285. else
  1286. UpdateUndoRedo(cmUndo,0);
  1287. end;
  1288. if Core^.UndoList^.count=0 then
  1289. SetCmdState(UndoCmd,false);
  1290. if (Core^.UndoList^.count=0) or
  1291. ((Core^.UndoList^.count=1) and
  1292. (Core^.UndoList^.At(0)^.Action=eaMoveCursor)) then
  1293. begin
  1294. SetCmdState(UndoCmd,false);
  1295. if (UndoTime>=Core^.SystemLoadTime) or (Core^.SystemLoadTime=0) then
  1296. SetModified(false);
  1297. end;
  1298. SetCmdState(RedoCmd,true);
  1299. Message(Application,evBroadcast,cmCommandSetChanged,nil);
  1300. if MinY<>-1 then
  1301. UpdateAttrsRange(MinY,MaxY,attrAll);
  1302. DrawView;
  1303. end;
  1304. Core^.SetStoreUndo(True);
  1305. Unlock;
  1306. end;
  1307. procedure TCodeEditor.Redo;
  1308. var
  1309. Temp,Idx,i,Last,Count : Longint;
  1310. StoredFlags : longint;
  1311. WasInserting,IsGrouped,ShouldInsertText : boolean;
  1312. Line : String;
  1313. MaxY,MinY : sw_integer;
  1314. procedure SetMinMax(y : sw_integer);
  1315. begin
  1316. if MinY=-1 then
  1317. MinY:=Y;
  1318. if Y<MinY then
  1319. MinY:=Y;
  1320. if MaxY=-1 then
  1321. MaxY:=Y;
  1322. if Y>MaxY then
  1323. MaxY:=Y;
  1324. end;
  1325. begin
  1326. Core^.SetStoreUndo(False);
  1327. Lock;
  1328. MinY:=-1;
  1329. MaxY:=-1;
  1330. if Core^.RedoList^.count <> 0 then
  1331. begin
  1332. Last:=Core^.RedoList^.count-1;
  1333. if Core^.RedoList^.At(Last)^.Is_grouped_action then
  1334. begin
  1335. Count:=Core^.RedoList^.At(Last)^.ActionCount;
  1336. Dec(Last);
  1337. IsGrouped:=true;
  1338. end
  1339. else
  1340. begin
  1341. Count:=1;
  1342. IsGrouped:=false;
  1343. end;
  1344. for Idx:=Last downto Last-Count+1 do
  1345. with Core^.RedoList^.At(Idx)^ do
  1346. begin
  1347. case action of
  1348. eaMoveCursor :
  1349. begin
  1350. { move cursor back to original position }
  1351. SetCurPtr(EndPos.X,EndPos.Y);
  1352. end;
  1353. eaInsertText :
  1354. begin
  1355. SetCurPtr(startpos.x,startpos.y);
  1356. InsertText(GetStr(Text));
  1357. SetMinMax(StartPos.Y);
  1358. end;
  1359. eaDeleteText :
  1360. begin
  1361. SetCurPtr(EndPos.X,EndPos.Y);
  1362. for Temp := 1 to length(GetStr(Text)) do
  1363. DelChar;
  1364. SetMinMax(EndPos.Y);
  1365. end;
  1366. eaOverwriteText :
  1367. begin
  1368. SetCurPtr(StartPos.X,StartPos.Y);
  1369. Line:=GetDisplayText(StartPos.Y);
  1370. WasInserting:=GetInsertMode;
  1371. SetInsertMode(false);
  1372. if assigned(text) then
  1373. for Temp := 1 to length(Text^) do
  1374. begin
  1375. AddChar(Text^[Temp]);
  1376. if StartPos.X+Temp>Length(Line) then
  1377. Text^[Temp]:=' '
  1378. else
  1379. Text^[Temp]:=Line[StartPos.X+Temp];
  1380. end;
  1381. SetInsertMode(WasInserting);
  1382. SetCurPtr(EndPos.X,EndPos.Y);
  1383. SetMinMax(StartPos.Y);
  1384. end;
  1385. eaInsertLine :
  1386. begin
  1387. SetCurPtr(StartPos.X,StartPos.Y);
  1388. StoredFlags:=GetFlags;
  1389. SetFlags(Flags);
  1390. InsertNewLine;
  1391. SetCurPtr(0,EndPos.Y);
  1392. Line:=GetStr(Text);
  1393. ShouldInsertText:=false;
  1394. for I:=1 to Length(Line) do
  1395. if Line[I]<>' ' then
  1396. ShouldInsertText:=true;
  1397. If ShouldInsertText then
  1398. InsertText(Line);
  1399. SetFlags(StoredFlags);
  1400. SetCurPtr(EndPos.X,EndPos.Y);
  1401. SetMinMax(StartPos.Y);
  1402. end;
  1403. eaDeleteLine :
  1404. begin
  1405. SetCurPtr(StartPos.X,StartPos.Y);
  1406. DeleteLine(StartPos.Y);
  1407. SetCurPtr(EndPos.X,EndPos.Y);
  1408. if EndPos.Y=StartPos.Y-1 then
  1409. SetDisplayText(EndPos.Y,RExpand(
  1410. copy(GetDisplayText(EndPos.Y),1,EndPos.X),EndPos.X)
  1411. +GetStr(Text));
  1412. SetCurPtr(EndPos.X,EndPos.Y);
  1413. SetMinMax(StartPos.Y);
  1414. SetMinMax(EndPos.Y);
  1415. end;
  1416. eaSelectionChanged :
  1417. begin
  1418. { move cursor to end of last set test selection }
  1419. end;
  1420. else
  1421. { what the 'ell's an undefined action doing round 'ere mate! }
  1422. ;
  1423. end; { once this lot is done paste back into undo and modify to suit needs }
  1424. { move item to undo stack }
  1425. Core^.UndoList^.Insert(Core^.RedoList^.At(Idx));
  1426. UpdateUndoRedo(cmUndo,Core^.RedoList^.At(Idx)^.Action);
  1427. If Idx>0 then
  1428. UpdateUndoRedo(cmRedo,Core^.RedoList^.At(Idx-1)^.Action)
  1429. else
  1430. UpdateUndoRedo(cmRedo,0);
  1431. Core^.RedoList^.atDelete(Idx);
  1432. end;{ Idx loop for grouped action }
  1433. If IsGrouped then
  1434. begin
  1435. Idx:=Core^.RedoList^.count-1;
  1436. Core^.UndoList^.Insert(Core^.RedoList^.At(Idx));
  1437. UpdateUndoRedo(cmUndo,Core^.RedoList^.At(Idx)^.Action);
  1438. If Idx>0 then
  1439. UpdateUndoRedo(cmRedo,Core^.RedoList^.At(Idx-1)^.Action)
  1440. else
  1441. UpdateUndoRedo(cmRedo,0);
  1442. Core^.RedoList^.atDelete(Idx);
  1443. end;
  1444. if Core^.RedoList^.count=0 then
  1445. SetCmdState(RedoCmd,false);
  1446. SetCmdState(UndoCmd,true);
  1447. Message(Application,evBroadcast,cmCommandSetChanged,nil);
  1448. if MinY<>-1 then
  1449. UpdateAttrsRange(MinY,MaxY,attrAll);
  1450. DrawView;
  1451. end;
  1452. Core^.SetStoreUndo(True);
  1453. Unlock;
  1454. end;
  1455. (*constructor TCodeEditor.Load(var S: TStream);
  1456. var TS: PSubStream;
  1457. TSize: longint;
  1458. begin
  1459. inherited Load(S);
  1460. New(UndoList,init(500,1000));
  1461. New(RedoList,init(500,1000));
  1462. New(Lines, Init(500,1000));
  1463. { we have always need at least 1 line }
  1464. LinesInsert(New(PLine, Init('',0)));
  1465. GetPeerViewPtr(S,Indicator);
  1466. S.Read(Flags,SizeOf(Flags));
  1467. S.Read(TabSize,SizeOf(TabSize));
  1468. if IsFlagSet(efStoreContent) then
  1469. begin
  1470. S.Read(TSize,SizeOf(TSize));
  1471. New(TS, Init(@S,S.GetPos,TSize));
  1472. {$ifdef TEST_PARTIAL_SYNTAX}
  1473. Core^.SearchBinding(Editor)^.SyntaxComplete:=false;
  1474. { Idle necessary }
  1475. EventMask:=EventMask or evIdle;
  1476. {$endif TEST_PARTIAL_SYNTAX}
  1477. LoadFromStream(TS);
  1478. Dispose(TS, Done);
  1479. end;
  1480. S.Read(SelStart,SizeOf(SelStart));
  1481. S.Read(SelEnd,SizeOf(SelEnd));
  1482. S.Read(Highlight,SizeOf(Highlight));
  1483. S.Read(CurPos,SizeOf(CurPos));
  1484. S.Read(StoreUndo,SizeOf(StoreUndo));
  1485. S.Read(IsReadOnly,SizeOf(IsReadOnly));
  1486. S.Read(NoSelect,SizeOf(NoSelect));
  1487. S.Read(HighlightRow,SizeOf(HighlightRow));
  1488. SetDebuggerRow(-1);
  1489. LimitsChanged;
  1490. SelectionChanged; HighlightChanged;
  1491. UpdateIndicator;
  1492. end;
  1493. procedure TCodeEditor.Store(var S: TStream);
  1494. var {NS: TNulStream;}
  1495. TSizePos,TSize,EndPos: longint;
  1496. begin
  1497. inherited Store(S);
  1498. PutPeerViewPtr(S,Indicator);
  1499. S.Write(Flags,SizeOf(Flags));
  1500. S.Write(TabSize,SizeOf(TabSize));
  1501. if IsFlagSet(efStoreContent) then
  1502. begin
  1503. { NS.Init;
  1504. SaveToStream(@NS);
  1505. TSize:=NS.GetSize;
  1506. NS.Done;
  1507. This is waste of time PM
  1508. use Seek instead !! }
  1509. { yep. and this won't work for serial streams. - Gabor }
  1510. TSize:=0;
  1511. TSizePos:=S.GetPos;
  1512. S.Write(TSize,SizeOf(TSize));
  1513. SaveToStream(@S);
  1514. EndPos:=S.GetPos;
  1515. TSize:=EndPos-TSizePos-SizeOf(TSize);
  1516. S.Seek(TSizePos);
  1517. S.Write(TSize,SizeOf(TSize));
  1518. S.Seek(EndPos);
  1519. end;
  1520. S.Write(SelStart,SizeOf(SelStart));
  1521. S.Write(SelEnd,SizeOf(SelEnd));
  1522. S.Write(Highlight,SizeOf(Highlight));
  1523. S.Write(CurPos,SizeOf(CurPos));
  1524. S.Write(StoreUndo,SizeOf(StoreUndo));
  1525. S.Write(IsReadOnly,SizeOf(IsReadOnly));
  1526. S.Write(NoSelect,SizeOf(NoSelect));
  1527. S.Write(HighlightRow,SizeOf(HighlightRow));
  1528. end;*)
  1529. function TCodeEditor.LoadFromStream(Stream: PFastBufStream): boolean;
  1530. var OK: boolean;
  1531. begin
  1532. OK:=Core^.LoadFromStream(@Self,Stream);
  1533. if IsFlagSet(efSyntaxHighlight) then
  1534. UpdateAttrsRange(0,Min(Delta.Y+Size.Y,GetLineCount-1),
  1535. attrAll
  1536. {$ifndef TEST_PARTIAL_SYNTAX}
  1537. +attrForceFull
  1538. {$endif TEST_PARTIAL_SYNTAX}
  1539. );
  1540. TextStart;
  1541. LoadFromStream:=OK;
  1542. end;
  1543. function TCodeEditor.SaveToStream(Stream: PStream): boolean;
  1544. begin
  1545. SaveToStream:=Core^.SaveToStream(@Self,Stream);
  1546. end;
  1547. function TCodeEditor.SaveAreaToStream(Stream: PStream; StartP,EndP: TPoint): boolean;
  1548. begin
  1549. SaveAreaToStream:=Core^.SaveAreaToStream(@Self,Stream,StartP,EndP);
  1550. end;
  1551. function TCodeEditor.UpdateAttrs(FromLine: sw_integer; Attrs: byte): sw_integer;
  1552. begin
  1553. UpdateAttrs:=Core^.UpdateAttrs(FromLine,Attrs);
  1554. end;
  1555. function TCodeEditor.UpdateAttrsRange(FromLine, ToLine: sw_integer; Attrs: byte): sw_integer;
  1556. begin
  1557. UpdateAttrsRange:=Core^.UpdateAttrsRange(FromLine,ToLine,Attrs);
  1558. end;
  1559. procedure TCodeEditor.AddAction(AAction: byte; AStartPos, AEndPos: TPoint; AText: string;AFlags : longint);
  1560. begin
  1561. Core^.AddAction(AAction,AStartPos,AEndPos,AText,AFlags);
  1562. end;
  1563. procedure TCodeEditor.AddGroupedAction(AAction : byte);
  1564. begin
  1565. Core^.AddGroupedAction(AAction);
  1566. end;
  1567. procedure TCodeEditor.CloseGroupedAction(AAction : byte);
  1568. begin
  1569. Core^.CloseGroupedAction(AAction);
  1570. end;
  1571. function TCodeEditor.GetUndoActionCount: sw_integer;
  1572. begin
  1573. GetUndoActionCount:=Core^.GetUndoActionCount;
  1574. end;
  1575. function TCodeEditor.GetRedoActionCount: sw_integer;
  1576. begin
  1577. GetRedoActionCount:=Core^.GetRedoActionCount;
  1578. end;
  1579. destructor TCodeEditor.Done;
  1580. begin
  1581. inherited Done;
  1582. if Assigned(Core) then
  1583. begin
  1584. Core^.UnBindEditor(@Self);
  1585. if Core^.CanDispose then
  1586. Dispose(Core, Done);
  1587. end;
  1588. Core:=nil;
  1589. if Assigned(CodeCompleteFrag) then
  1590. DisposeStr(CodeCompleteFrag);
  1591. if Assigned(CodeCompleteWord) then
  1592. DisposeStr(CodeCompleteWord);
  1593. if Assigned(ErrorMessage) then
  1594. DisposeStr(ErrorMessage);
  1595. if Assigned(Folds) then
  1596. Dispose(Folds, Done);
  1597. Folds:=nil;
  1598. end;
  1599. constructor TFileEditor.Init(var Bounds: TRect; AHScrollBar, AVScrollBar:
  1600. PScrollBar; AIndicator: PIndicator;ACore: PCodeEditorCore; const AFileName: string);
  1601. begin
  1602. inherited Init(Bounds,AHScrollBAr,AVScrollBAr,AIndicator,ACore);
  1603. FileName:=AFileName;
  1604. UpdateIndicator;
  1605. Message(@Self,evBroadcast,cmFileNameChanged,@Self);
  1606. end;
  1607. function TFileEditor.LoadFile: boolean;
  1608. var OK: boolean;
  1609. PA : Array[1..2] of pointer;
  1610. begin
  1611. OK:=LoadFromFile(FileName);
  1612. if GetModified and (Core^.GetBindingCount=1) then
  1613. begin
  1614. PA[1]:=@FileName;
  1615. Ptrint(PA[2]):=Core^.GetChangedLine;
  1616. EditorDialog(edChangedOnloading,@PA);
  1617. end;
  1618. Core^.OnDiskLoadTime:=Cardinal(GetFileTime(FileName));
  1619. Core^.SystemLoadTime:=Core^.OnDiskLoadTime;
  1620. LoadFile:=OK;
  1621. end;
  1622. function TFileEditor.IsChangedOnDisk : boolean;
  1623. begin
  1624. IsChangedOnDisk:=(Core^.OnDiskLoadTime<>Cardinal(GetFileTime(FileName))) and
  1625. (Core^.OnDiskLoadTime<>0);
  1626. end;
  1627. function TFileEditor.SaveFile: boolean;
  1628. var OK: boolean;
  1629. BAKName: string;
  1630. f: text;
  1631. SaveTime : cardinal;
  1632. begin
  1633. If IsChangedOnDisk then
  1634. begin
  1635. if EditorDialog(edFileOnDiskChanged, @FileName) <> cmYes then
  1636. begin
  1637. SaveFile:=false;
  1638. exit;
  1639. end;
  1640. end;
  1641. {$I-}
  1642. if IsFlagSet(efBackupFiles) and ExistsFile(FileName) then
  1643. begin
  1644. BAKName:=DirAndNameOf(FileName)+'.bak';
  1645. Assign(f,BAKName);
  1646. Erase(f);
  1647. EatIO;
  1648. Assign(f,FileName);
  1649. Rename(F,BAKName);
  1650. EatIO;
  1651. end;
  1652. {$I+}
  1653. SaveTime:=cardinal(now);
  1654. OK:=SaveToFile(FileName);
  1655. if OK then
  1656. SetModified(false)
  1657. { Restore the original }
  1658. else if IsFlagSet(efBackupFiles) and ExistsFile(BakName) then
  1659. begin
  1660. {$I-}
  1661. Assign(f,BakName);
  1662. Rename(F,FileName);
  1663. EatIO;
  1664. {$I+}
  1665. end;
  1666. { don't forget to update the OnDiskLoadTime value }
  1667. if OK then
  1668. begin
  1669. Core^.OnDiskLoadTime:=Cardinal(GetFileTime(FileName));
  1670. Core^.SystemLoadTime:=SaveTime;
  1671. end;
  1672. if not OK then
  1673. EditorDialog(edSaveError,@FileName);
  1674. SaveFile:=OK;
  1675. end;
  1676. function TFileEditor.ReloadFile: boolean;
  1677. var OK,WasModified: boolean;
  1678. BAKName: string;
  1679. f: text;
  1680. begin
  1681. If not IsChangedOnDisk then
  1682. begin
  1683. ReloadFile:=false;
  1684. exit;
  1685. end;
  1686. WasModified:=GetModified;
  1687. if not WasModified then
  1688. OK:=EditorDialog(edreloaddiskmodifiedfile, @FileName)=cmYes
  1689. else
  1690. OK:=EditorDialog(edreloaddiskandidemodifiedfile, @FileName)=cmYes;
  1691. if not OK then
  1692. begin
  1693. ReloadFile:=false;
  1694. exit;
  1695. end;
  1696. { avoid wrong message }
  1697. if WasModified then
  1698. SetModified(false);
  1699. OK:=LoadFile;
  1700. if OK then
  1701. begin
  1702. SetModified(false);
  1703. ClearUndoList;
  1704. { don't forget to update the OnDiskLoadTime value }
  1705. Core^.OnDiskLoadTime:=Cardinal(GetFileTime(FileName));
  1706. Core^.SystemLoadTime:=Core^.OnDiskLoadTime;
  1707. DrawView;
  1708. end
  1709. else
  1710. begin
  1711. if WasModified then
  1712. SetModified(true);
  1713. EditorDialog(edReadError,@FileName);
  1714. end;
  1715. ReloadFile:=OK;
  1716. end;
  1717. function TFileEditor.ShouldSave: boolean;
  1718. begin
  1719. ShouldSave:=GetModified{ or (FileName='')};
  1720. end;
  1721. function TFileEditor.Save: Boolean;
  1722. begin
  1723. if ShouldSave=false then begin Save:=true; Exit; end;
  1724. if FileName = '' then Save := SaveAs else Save := SaveFile;
  1725. end;
  1726. function TFileEditor.SaveAs: Boolean;
  1727. var
  1728. SavedName : String;
  1729. SavedDiskLoadTime : cardinal;
  1730. begin
  1731. SaveAs := False;
  1732. SavedName:=FileName;
  1733. SavedDiskLoadTime:=Core^.OnDiskLoadTime;
  1734. if EditorDialog(edSaveAs, @FileName) <> cmCancel then
  1735. begin
  1736. FileName:=FExpand(FileName);
  1737. Message(Owner, evBroadcast, cmUpdateTitle, @Self);
  1738. { if we rename the file the OnDiskLoadTime is wrong so we reset it }
  1739. Core^.OnDiskLoadTime:=0;
  1740. if SaveFile then
  1741. begin
  1742. SaveAs := true;
  1743. end
  1744. else
  1745. begin
  1746. FileName:=SavedName;
  1747. Core^.OnDiskLoadTime:=SavedDiskLoadTime;
  1748. Message(Owner, evBroadcast, cmUpdateTitle, @Self);
  1749. end;
  1750. if IsClipboard then FileName := '';
  1751. Message(Application,evBroadcast,cmFileNameChanged,@Self);
  1752. end;
  1753. end;
  1754. function TFileEditor.SaveAsk(Force: boolean): boolean;
  1755. var OK: boolean;
  1756. D: Sw_integer;
  1757. begin
  1758. if Force then
  1759. begin
  1760. if GetModified then
  1761. OK:=Save
  1762. else
  1763. OK:=true;
  1764. end
  1765. else
  1766. begin
  1767. OK:=(GetModified=false);
  1768. if (OK=false) and (Core^.GetBindingCount>1) then
  1769. OK:=true;
  1770. if OK=false then
  1771. begin
  1772. if FileName = '' then D := edSaveUntitled else D := edSaveModify;
  1773. case EditorDialog(D, @FileName) of
  1774. cmYes : OK := Save;
  1775. cmNo : begin
  1776. { the file should be still marked as modified! (FK) }
  1777. { SetModified(False); }
  1778. OK:=true;
  1779. end;
  1780. cmCancel : begin
  1781. OK := False;
  1782. Message(Application,evBroadcast,cmSaveCancelled,@Self);
  1783. end;
  1784. end;
  1785. end;
  1786. end;
  1787. SaveAsk:=OK;
  1788. end;
  1789. procedure TFileEditor.BindingsChanged;
  1790. begin
  1791. Message(Application,evBroadcast,cmUpdateTitle,@Self);
  1792. end;
  1793. procedure TFileEditor.HandleEvent(var Event: TEvent);
  1794. var SH,B: boolean;
  1795. begin
  1796. case Event.What of
  1797. evBroadcast :
  1798. case Event.Command of
  1799. cmFileNameChanged :
  1800. if (Event.InfoPtr=nil) or (Event.InfoPtr=@Self) then
  1801. begin
  1802. B:=IsFlagSet(efSyntaxHighlight);
  1803. SH:=UseSyntaxHighlight(@Self);
  1804. if SH<>B then
  1805. if SH then
  1806. SetFlags(Flags or efSyntaxHighlight)
  1807. else
  1808. SetFlags(Flags and not efSyntaxHighlight);
  1809. if UseTabsPattern(@Self) then
  1810. SetFlags(Flags or efUseTabCharacters);
  1811. end;
  1812. end;
  1813. end;
  1814. inherited HandleEvent(Event);
  1815. end;
  1816. function TFileEditor.Valid(Command: Word): Boolean;
  1817. var OK: boolean;
  1818. begin
  1819. OK:=inherited Valid(Command);
  1820. if OK and (Command=cmClose) then
  1821. if IsClipboard=false then
  1822. OK:=SaveAsk(false);
  1823. Valid:=OK;
  1824. end;
  1825. (* constructor TFileEditor.Load(var S: TStream);
  1826. var P: PString;
  1827. SSP,SEP,CP,DP: TPoint;
  1828. HR: TRect;
  1829. PA : Array[1..2] of pointer;
  1830. HoldUndo : boolean;
  1831. begin
  1832. inherited Load(S);
  1833. HoldUndo:=GetStoreUndo;
  1834. SetStoreUndo(False);
  1835. P:=S.ReadStr;
  1836. FileName:=GetStr(P);
  1837. if P<>nil then DisposeStr(P);
  1838. UpdateIndicator;
  1839. { Message(@Self,evBroadcast,cmFileNameChanged,@Self);}
  1840. SSP:=SelStart; SEP:=SelEnd;
  1841. CP:=CurPos;
  1842. HR:=Highlight;
  1843. DP:=Delta;
  1844. if FileName<>'' then
  1845. LoadFile;
  1846. { if GetModified then
  1847. begin
  1848. PA[1]:=@FileName;
  1849. longint(PA[2]):=ChangedLine;
  1850. EditorDialog(edChangedOnloading,@PA);
  1851. end;}
  1852. SetHighlight(HR.A,HR.B);
  1853. SetSelection(SSP,SEP);
  1854. SetCurPtr(CP.X,CP.Y);
  1855. ScrollTo(DP.X,DP.Y);
  1856. SetModified(false);
  1857. LimitsChanged;
  1858. SetStoreUndo(HoldUndo);
  1859. end;
  1860. procedure TFileEditor.Store(var S: TStream);
  1861. begin
  1862. inherited Store(S);
  1863. S.WriteStr(@FileName);
  1864. end;
  1865. *)
  1866. function DefUseSyntaxHighlight(Editor: PFileEditor): boolean;
  1867. begin
  1868. DefUseSyntaxHighlight:=Editor^.IsFlagSet(efSyntaxHighlight);
  1869. end;
  1870. function DefUseTabsPattern(Editor: PFileEditor): boolean;
  1871. begin
  1872. DefUseTabsPattern:=Editor^.IsFlagSet(efUseTabCharacters);
  1873. end;
  1874. procedure RegisterWCEdit;
  1875. begin
  1876. {$ifndef NOOBJREG}
  1877. RegisterType(RIndicator);
  1878. RegisterType(RCodeEditor);
  1879. RegisterType(RFileEditor);
  1880. {$endif}
  1881. end;
  1882. end.