File: UniCodeConsole.pas

package info (click to toggle)
mysql-gui-tools 5.0r12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 105,540 kB
  • ctags: 50,897
  • sloc: sql: 348,439; pascal: 285,780; cpp: 94,578; ansic: 90,768; objc: 33,761; sh: 25,629; xml: 10,924; yacc: 10,755; java: 9,986; php: 2,806; python: 2,068; makefile: 1,945; perl: 3
file content (883 lines) | stat: -rw-r--r-- 26,448 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
unit UniCodeConsole;

// UniCodeEditor, a Unicode Source Code Editor for Delphi.
//
// UniCodeEditor is released under the MIT license:
// Copyright (c) 1999-2005 Mike Zinner, Mike Lischke (support@soft-gems.net, www.soft-gems.net).
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// You are asked to give the author(s) the due credit. This means that you acknowledge the work of the author(s)
// in the product documentation, about box, help or wherever a prominent place is.

interface

uses UniCodeEditor, Unicode, Classes, Contnrs, Types,
  Windows, Messages, SysUtils, Controls, Math;

type
  TExecuteCommandProcedure = procedure(Cmd: WideString; Key: Word; Shift: TShiftState) of object;
  TIsMultilineCommandFunction = function(Cmd: WideString; Key: Word; Shift: TShiftState): Boolean of object;

  TCommandHistoryObject = class
    constructor Create(Cmd: WideString);
  private
    FCmd: WideString;
  published
    property Cmd: WideString read FCmd write FCmd;
  end;

  TCustomUniCodeConsole = class(TCustomUniCodeEdit)
  private
    CommandHistory: TObjectList;
    CommandHistoryPos: integer;

    StoredCaretPos: TPoint;

    CurrentCommandStartLineNr: Integer;
    CurrentCommandEndLineNr: Integer;

    FOnExecuteCommand: TExecuteCommandProcedure;
    FOnIsMultilineCommand: TIsMultilineCommandFunction;

    FConsolePrompt: WideString;
    FConsolePromptLen: Integer;

    FConsoleDelimiter: WideString;

    FReadScriptInput: Boolean;

    FAsyncExecution,
    FExecuting: Boolean;

    procedure SetConsolePrompt(ConsolePrompt: WideString);
    procedure SetConsoleCommandSelStart(value: integer);

    procedure SetConsoleCommand(Cmd: WideString);
    function GetConsoleCommand: WideString;

    procedure SetConsoleCommandExcludeDelimiter(Cmd: WideString);
    function GetConsoleCommandExcludeDelimiter: WideString;

    procedure SetConsoleHistory(History: AnsiString);
    function GetConsoleHistory: AnsiString;
  protected
    procedure LineChanged(Sender: TObject; Line: TUCELine);
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); override;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); override;

    function GetConsolePromptMultiline: WideString;

    property ConsolePromptLen: Integer read FConsolePromptLen;
    property ConsolePrompt: WideString read FConsolePrompt write SetConsolePrompt;

    property ConsoleDelimiter: WideString read FConsoleDelimiter write FConsoleDelimiter;

    property OnExecuteCommand: TExecuteCommandProcedure read FOnExecuteCommand write FOnExecuteCommand;
    property OnIsMultilineCommand: TIsMultilineCommandFunction read FOnIsMultilineCommand write FOnIsMultilineCommand;

    property ReadScriptInput: Boolean read FReadScriptInput write FReadScriptInput;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure AddOutput(const Text: WideString);
    procedure PrepareNextConsoleCommand;
    procedure PrepareNextConsoleCommandMultiline;

    procedure ExecuteCommand;

    property ConsoleCommand: WideString read GetConsoleCommand write SetConsoleCommand;
    property ConsoleCommandExcludeDelim: WideString read GetConsoleCommandExcludeDelimiter write
      SetConsoleCommandExcludeDelimiter;
    property ConsoleCommandSelStart: Integer write SetConsoleCommandSelStart;

    property ConsoleHistory: AnsiString read GetConsoleHistory write SetConsoleHistory;

    property AsyncExecution: Boolean read FAsyncExecution write FAsyncExecution;
    property Executing: Boolean read FExecuting write FExecuting;
  end;

  TUniCodeConsole = class(TCustomUniCodeConsole)
  published
    property Align;
    property Anchors;
    property Constraints;
    property BevelEdges;
    property BevelInner;
    property BevelOuter;
    property BevelKind;
    property BevelWidth;
    property BorderWidth;
    property BorderStyle;
    property BookMarkOptions;
    property CharWidth;
    property Color;
    property Ctl3D;
    property Enabled;
    property ExtraLineSpacing;
    property Font;
    property GutterColor;
    property GutterWidth;
    property Height;
    property HighLighter;
    property IndentSize;
    property InsertCaret;
    property Keystrokes;
    property LineNumberFont;
    property MarginColor;
    property MaxUndo;
    property Name;
    property Options;
    property OverwriteCaret;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property RightMargin;
    property ShowHint;
    property ScrollBars;
    property ScrollHintColor;
    property SelectedColor;
    property TabOrder;
    property TabSize;
    property TabStop default True;
    property Tag;
    property Visible;
    property Width;

    property OnBookmarkChange;
    property OnCaretChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnEnter;
    property OnFocusChanged;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnPaint;
    property OnProcessCommand;
    property OnProcessUserCommand;
    property OnReplaceText;
    property OnSettingChange;
    property OnStartDrag;

    property ConsolePromptLen;
    property ConsolePrompt;

    property ConsoleDelimiter;

    property OnExecuteCommand;
    property OnIsMultilineCommand;

    property ReadScriptInput;
  end;

//----------------------------------------------------------------------------------------------------------------------

implementation

uses
  UCEEditorKeyCommands;
  
//----------------------------------------------------------------------------------------------------------------------

constructor TCommandHistoryObject.Create(Cmd: WideString);

begin
  inherited Create;

  FCmd := Cmd;
end;

//----------------------------------------------------------------------------------------------------------------------

constructor TCustomUniCodeConsole.Create(AOwner: TComponent);

begin
  inherited;

  CommandHistory := TObjectList.Create;

  FOnExecuteCommand := nil;
  FOnIsMultilineCommand := nil;

  ConsolePrompt := '> ';

  FConsoleDelimiter := ';';
  Content.OnChangeLine := LineChanged;

  FAsyncExecution := False;
  FExecuting := False;
end;

//----------------------------------------------------------------------------------------------------------------------

destructor TCustomUniCodeConsole.Destroy;

begin
  CommandHistory.Free;

  inherited;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.PrepareNextConsoleCommand;

begin
  Content.AddLine(ConsolePrompt);
  CaretY := Content.Count;
  CaretX := ConsolePromptLen;
  Invalidate;

  CurrentCommandStartLineNr := Content.Count - 1;
  CurrentCommandEndLineNr := CurrentCommandStartLineNr;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.LineChanged(Sender: TObject; Line: TUCELine);

begin
  if Assigned(Line) then
    RefreshLine(Line.Index);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.ExecuteCommand;

var Key: Word;

begin
  Key := VK_Return;

  KeyDown(Key, []);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.KeyDown(var Key: Word; Shift: TShiftState);

var
  Cmd: WideString;
  CmdTrimmed: WideString;
  CopiedText: WideString;

begin
  // clear selection, but keep it for Ctrl+C
  if Not((Key = VK_Control) or (Key = VK_Shift) or
      ((Key = Ord('C')) and (Shift = [ssCtrl])) or
      ((Key = Ord('X')) and (Shift = [ssCtrl])) or
      ((Key = Ord('V')) and (Shift = [ssCtrl])) or
      ((Key = VK_Left) and (Shift = [ssShift])) or
      ((Key = VK_Right) and (Shift = [ssShift])) or
      ((Key = VK_HOME) and (Shift = [ssShift])) or
      ((Key = VK_END) and (Shift = [ssShift]))) then
    ClearSelection;

  if (Key = VK_Return) and (Shift<>[ssShift]) then
  begin
    Cmd := GetConsoleCommand;
    CmdTrimmed := Trim(Cmd);

    if (CompareText(CmdTrimmed, 'cls') = 0) then
    begin
      Content.Clear;
      ClientWidth := Width;

      PrepareNextConsoleCommand;
      Invalidate;
      Key := 0;
    end
    else
      if (CmdTrimmed = '') then
      begin
        PrepareNextConsoleCommand;
        Key := 0;
      end
      else
        if (CmdTrimmed <> '') then
        begin
          if (Copy(CmdTrimmed, Length(CmdTrimmed) -
            Length(FConsoleDelimiter) + 1, Length(FConsoleDelimiter)) = FConsoleDelimiter) then
          begin
            if (Assigned(OnExecuteCommand)) then
            begin
              if Not(FAsyncExecution) then
                FExecuting := True;

              try
                OnExecuteCommand(Cmd, Key, Shift)
              finally
                if Not(FAsyncExecution) then
                  FExecuting := False;
              end;
            end
            else
              PrepareNextConsoleCommand;
          end
          else
          begin
            if (Assigned(OnIsMultilineCommand)) then
            begin
              if (not (OnIsMultilineCommand(Cmd, Key, Shift))) then
              begin
                if (Assigned(OnExecuteCommand)) then
                begin
                  if Not(FAsyncExecution) then
                    FExecuting := True;

                  try
                    OnExecuteCommand(Cmd, Key, Shift)
                  finally
                    if Not(FAsyncExecution) then
                      FExecuting := False;
                  end;
                end
                else
                  PrepareNextConsoleCommand;
              end
              else
                PrepareNextConsoleCommandMultiline;
            end
            else
              PrepareNextConsoleCommandMultiline;
          end;

          //Add command to history
          if (cmd <> '') then
          begin
            if (CommandHistory.Count = 0) or
               ((CommandHistory.Count>0) and (
                Trim(TCommandHistoryObject(
                  CommandHistory[CommandHistory.Count-1]).cmd) <>
                Trim(cmd))) then
            begin
              CommandHistory.Add(TCommandHistoryObject.Create(cmd));
              CommandHistoryPos := CommandHistory.Count;
            end;
          end;

          Key := 0;
        end;
  end
  else
    if (Key = VK_Return) and (Shift=[ssShift]) then
    begin
      ConsoleCommand := ConsoleCommand + #13#10;
    end
    else
      if (Key = VK_UP) and (Shift = []) then
      begin
        if (CaretY <= CurrentCommandStartLineNr) then
          Key := 0;
      end
      else
        if (Key = VK_DOWN) and (Shift = []) then
        begin
          if (CaretY >= CurrentCommandEndLineNr) then
            Key := 0;
        end

        else
          if ((Key = VK_PRIOR) and (Shift = [])) or
            ((Key = VK_UP) and (Shift = [ssCtrl])) then
          begin
            if (CommandHistoryPos > 0) then
            begin
              dec(CommandHistoryPos);
              ConsoleCommand := TCommandHistoryObject(CommandHistory[CommandHistoryPos]).Cmd;
            end;

            Key := 0;
          end
          else
            if ((Key = VK_NEXT) and (Shift = [])) or
              ((Key = VK_DOWN) and (Shift = [ssCtrl])) then
            begin
              if (CommandHistoryPos < CommandHistory.Count - 1) then
              begin
                inc(CommandHistoryPos);
                ConsoleCommand := TCommandHistoryObject(CommandHistory[CommandHistoryPos]).Cmd;
              end
              else
              begin
                ConsoleCommand := '';
        {Lines[CaretY]:=ConsolePrompt;
        CaretX:=ConsolePromptLen;}
                CommandHistoryPos := CommandHistory.Count;
              end;

              Key := 0;
            end
            else
              if (Key = VK_HOME) then
              begin
                CaretX := ConsolePromptLen;

                Key := 0;
              end
              else
                if (Key = VK_PRIOR) then
                begin
                  Key := 0;
                end
                else
                  if (Key = VK_NEXT) then
                  begin
                    Key := 0;
                  end
                  else
                    if (Key = VK_BACK) or (Key = VK_LEFT) then
                    begin
                      if (CaretX < Integer(ConsolePromptLen + 1)) then
                        Key := 0;
                    end
                    else
                      if (Key = VK_RIGHT) then
                      begin
                        if (CaretX >= Length(Content[CaretY].Text)) then
                          Key := 0;
                      end
                      else
                        if (Key = VK_ESCAPE) then
                        begin
                          ConsoleCommand := '';

                          Key := 0;
                        end
                        else
                          if (Key = Ord('V')) and (Shift = [ssCtrl]) then
                          begin
                            CopiedText := TextFromClipboard;

                            // if there is one linebreak,
                            // subsitute the complete console command
                            if (Pos(#10, CopiedText) > 0) then
                            begin
                              ConsoleCommand := CopiedText;
                              Key := 0;
                            end;
                          end;

  if (Key <> 0) then
    inherited;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.AddOutput(const Text: WideString);

// Takes the given text and adds it to the console output. The text is scanned for certain control characters and the
// output is adjusted accordingly.

const
  ControlChars = [
    WideChar(#$00),  // Terminator
    WideChar(#$07),  // BELL
    WideChar(#$08),  // Back space
    WideChar(#$09),  // Horizontal tabulator
    WideChar(#$0A),  // Line feed (new line)
    WideChar(#$0D),  // Carriage return (back to start of line)
    WideChar(#$18),  // Cancel
    WideChar(#$1B)   // Escape
  ];

var
  Head, Tail: PWideChar;
  S: WideString;
  Index: Integer;
  Cmd, Prompt: WideString;

begin
  Index := Content.Count - 1;
  if Index < 0 then
  begin
    Content.AddLine('');
    Inc(Index);
  end;

  // if the console is not executing, remove the
  // current cmd and prompt to print the message
  if (Not(FExecuting)) then
  begin
    Cmd := ConsoleCommand;
    Prompt := ConsolePrompt;

    ConsoleCommand := '';
    ConsolePrompt := '';
  end;

  Head := PWideChar(Text);
  while Head^ <> #0 do
  begin
    Tail := Head;

    // Collect everything not containing any control character.
    while not (Tail^ in ControlChars) do
      Inc(Tail);
    if Head <> Tail then
    begin
      SetString(S, Head, Tail - Head);
      Content[Index].Text := Content[Index].Text + S;
    end;

    // Now handle control character.
    case Tail^ of
      WideChar(#$00):  // Terminator
        ; // Nothing to do.
      WideChar(#$07):  // BELL
        Inc(Tail); // Just swallow it.
      WideChar(#$08):  // Back space
        begin
          // Delete previous character if there is one.
          CommandProcessor(ecDeleteLastChar, Tail^, nil);
          Inc(Tail);
        end;
      WideChar(#$09):  // Horizontal tabulator
        begin
          // Add 8 space characters at current cursor position.
          InsertText('        ');
          Inc(Tail);
        end;
      WideChar(#$0A):  // Line feed (new line)
        begin
          Content.AddLine('');
          Inc(Index);
          CaretXY := Point(0, Index);
          Inc(Tail);
        end;
      WideChar(#$0D):  // Carriage return (back to start of line)
        begin
          CaretX := 0;
          Inc(Tail);
        end;
      WideChar(#$18),  // Cancel
      WideChar(#$1B):  // Escape
        begin
          // Clear line.
          Content[Index].Text := '';
          Inc(Tail);
        end;
    end;
    Head := Tail;
  end;

  // if the prompt and cmd have been removed, put them back again
  if (Not(FExecuting)) then
  begin
    ConsoleCommand := Cmd;
    ConsolePrompt := Prompt;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.PrepareNextConsoleCommandMultiline;

begin
  Content.AddLine(GetConsolePromptMultiline);
  CaretY := Content.Count;
  CaretX := ConsolePromptLen;
  Invalidate;

  Inc(CurrentCommandEndLineNr);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.SetConsoleCommand(Cmd: WideString);

var
  CmdLines: TWideStringList;
  i: integer;
begin
  if (Content.Count > 0) then
  begin
    //Clear current command
    for i := 0 to CurrentCommandEndLineNr - CurrentCommandStartLineNr do
      Content.DeleteLine(Content.Count - 1);
  end;

  CurrentCommandStartLineNr := Content.Count;

  //Add new command
  CmdLines := TWideStringList.Create;
  try
    CmdLines.Text := Cmd;

    if (CmdLines.Count > 0) then
    begin
      Content.AddLine(ConsolePrompt + CmdLines[0]);

      for i := 1 to CmdLines.Count - 1 do
        Content.AddLine(GetConsolePromptMultiline + CmdLines[i]);
    end
    else
      Content.AddLine(ConsolePrompt);
  finally
    CmdLines.Free;
  end;

  CurrentCommandEndLineNr := Content.Count - 1;

  SetCaretToEditorBottom;

  Invalidate;
end;

//----------------------------------------------------------------------------------------------------------------------

function TCustomUniCodeConsole.GetConsoleCommand: WideString;

var
  cmd: WideString;
  i: integer;

begin
  if (Content.Count > 0) then
  begin
    cmd := '';
    for i := CurrentCommandStartLineNr to CurrentCommandEndLineNr do
      if (CurrentCommandEndLineNr < Content.Count) then
        cmd := cmd +
          Copy(Content[i].Text, ConsolePromptLen + 1, Length(Content[i].Text)) + #13#10;

    Result := cmd;
  end
  else
    Result := '';
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.SetConsoleCommandExcludeDelimiter(Cmd: WideString);

begin
  Cmd := TrimRight(Cmd);

  if (Copy(Cmd, Length(Cmd) -
    Length(ConsoleDelimiter) + 1, Length(ConsoleDelimiter)) = ConsoleDelimiter) then
    ConsoleCommand := Cmd
  else
    ConsoleCommand := Cmd + ConsoleDelimiter;
end;

//----------------------------------------------------------------------------------------------------------------------

function TCustomUniCodeConsole.GetConsoleCommandExcludeDelimiter: WideString;

begin
  Result := TrimRight(ConsoleCommand);

  Result := Copy(Result, 1, Length(Result) -
    Length(ConsoleDelimiter));
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

begin
  StoredCaretPos.X := CaretX;
  StoredCaretPos.Y := CaretY;

  if (StoredCaretPos.Y > Content.Count) then
    StoredCaretPos.Y := Content.Count;

  if (StoredCaretPos.X < Integer(ConsolePromptLen)) then
    StoredCaretPos.X := ConsolePromptLen;

  inherited;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

begin
  if (SelStart<>SelEnd) and
    (Trim(SelectedText) <> '') then
    CopyToClipboard;

  if (CaretY < CurrentCommandStartLineNr) or
    (CaretY > CurrentCommandEndLineNr) then
  begin
    CaretY := StoredCaretPos.Y;
  end;

  if (CaretX < ConsolePromptLen) then
    CaretX := StoredCaretPos.X;

  //ClearSelection;

  inherited;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.SetConsolePrompt(ConsolePrompt: WideString);

var
  OldConsolePrompt: WideString;
  OldConsolePromptLen: Integer;
  i: integer;

begin
  OldConsolePrompt := FConsolePrompt;
  OldConsolePromptLen := ConsolePromptLen;

  FConsolePrompt := ConsolePrompt;
  FConsolePromptLen := Length(FConsolePrompt);

  if (Content.Count > 0) and (Not(FReadScriptInput)) then
    if (CurrentCommandStartLineNr < Content.Count) then
      if (Copy(Content[CurrentCommandStartLineNr].Text, 1, OldConsolePromptLen) =
        OldConsolePrompt) then
      begin
        for i := CurrentCommandStartLineNr to CurrentCommandEndLineNr do
        begin
          if (i = CurrentCommandStartLineNr) then
            Content[i].Text := ConsolePrompt +
              Copy(Content[i].Text, OldConsolePromptLen + 1, Length(Content[i].Text))
          else
            Content[i].Text := GetConsolePromptMultiline +
              Copy(Content[i].Text, OldConsolePromptLen + 1, Length(Content[i].Text));
        end;
      end;

  SetCaretToEditorBottom;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.SetConsoleCommandSelStart(value: integer);

var
  Loop: Integer;
  Count: Integer;

begin
  Loop := CurrentCommandStartLineNr;
  Count := 0;
  while ((Count + Length(Content[Loop].Text) - ConsolePromptLen + 2) < value)
    and (Loop < Content.Count) do
  begin
    Count := Count + Length(Content[Loop].Text) - ConsolePromptLen + 2;
    Inc(loop);
  end;
  CaretY := Loop;
  CaretX := ConsolePromptLen + Value - Count;
end;

function TCustomUniCodeConsole.GetConsolePromptMultiline: WideString;
begin
  if (ConsolePromptLen > 3) then
    Result := StringOfChar(' ', ConsolePromptLen - 3) + '>> '
  else
    Result := '>>';
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TCustomUniCodeConsole.SetConsoleHistory(History: AnsiString);

var
  Lines: TStringList;
  I: Integer;
  cmd: AnsiString;

begin
  CommandHistory.Clear;
  cmd := '';

  Lines := TStringList.Create;
  try
    Lines.Text := History;

    for I := 2 to Lines.Count-2 do
    begin
      if (Lines[I] = '  </history-entry>') and
        (cmd <> '') then
      begin
        CommandHistory.Add(TCommandHistoryObject.Create(
          UTF8ToWideString(
            StringReplace(
              StringReplace(
                StringReplace(cmd, '&gt;', '>', [rfReplaceAll]),
                '&lt;', '<', [rfReplaceAll]),
              '&amp;', '&', [rfReplaceAll])
          )));

        cmd := '';
      end
      else
        if (Lines[I] <> '  <history-entry>') then
          cmd := cmd + Lines[I] + #13#10;
    end;

    CommandHistoryPos := CommandHistory.Count;
  finally
    Lines.Free;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TCustomUniCodeConsole.GetConsoleHistory: AnsiString;

var
  I: Integer;
  Start: Integer;

begin
  Result := '<?xml version="1.0"?>'#13#10'<consolehistory>'#13#10;

  Start := Max(0, CommandHistory.Count - 1000);

  for I := Start to CommandHistory.Count-1 do
    Result := Result + '  <history-entry>'#13#10+
      Trim(
        StringReplace(
          StringReplace(
            StringReplace(
              WideStringToUTF8(
                TCommandHistoryObject(CommandHistory[I]).Cmd
              ), '&', '&amp;', [rfReplaceAll]),
            '<', '&lt;', [rfReplaceAll]),
          '>', '&gt;', [rfReplaceAll])
        )+#13#10+
      '  </history-entry>'#13#10;

  Result := Result + '</consolehistory>';
end;

//----------------------------------------------------------------------------------------------------------------------


end.