summaryrefslogtreecommitdiff
path: root/fpcsrc/ide/fpcodcmp.pas
blob: 0a2b6ff471f11b7d5cc2e0bf559e4b9f024553a4 (plain)
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
{
    This file is part of the Free Pascal Integrated Development Environment
    Copyright (c) 1998 by Berczi Gabor

    Code Complete routines

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}


unit FPCodCmp; { CodeComplete }

interface

uses Objects,Drivers,Dialogs,
     WEditor,WUtils,WViews;

type
     PCodeCompleteWordList = ^TCodeCompleteWordList;
     TCodeCompleteWordList = object(TTextCollection)
     end;

    PCodeCompleteDialog = ^TCodeCompleteDialog;
    TCodeCompleteDialog = object(TCenterDialog)
      constructor Init;
      function    Execute: Word; virtual;
      procedure   HandleEvent(var Event: TEvent); virtual;
    private
      CodeCompleteLB : PAdvancedListBox;
      RB : PRadioButtons;
      CB : PCheckBoxes;
      MinInputL,InputL : PEditorInputLine;
      procedure Add;
      procedure Edit;
      procedure Delete;
    end;

function FPCompleteCodeWord(const WordS: string; var Text: string): boolean;

procedure InitCodeComplete;
function  LoadCodeComplete(var S: TStream): boolean;
procedure AddStandardUnitsToCodeComplete;
procedure AddAvailableUnitsToCodeComplete(OnlyStandard : boolean);
function  StoreCodeComplete(var S: TStream): boolean;
procedure DoneCodeComplete;

const CodeCompleteWords : PCodeCompleteWordList = nil;
type
      TCodeCompleteCase = (ccc_unchanged, ccc_lower, ccc_upper, ccc_mixed);
const
     CodeCompleteCase : TCodeCompleteCase = ccc_unchanged;
     UnitsCodeCompleteWords : PCodeCompleteWordList = nil;

procedure RegisterCodeComplete;

implementation

uses App,Views,MsgBox,Validate,
     FVConsts,
     systems, BrowCol,
     FPSwitch, FPCompil,
     FPVars, FPSymbol,
     FPConst,FPViews;

{$ifndef NOOBJREG}
const
  RCodeCompleteWordList: TStreamRec = (
     ObjType: 14401;
     VmtLink: Ofs(TypeOf(TCodeCompleteWordList)^);
     Load:    @TCodeCompleteWordList.Load;
     Store:   @TCodeCompleteWordList.Store
  );
{$endif}

{$ifdef useresstrings}
resourcestring
{$else}
const
{$endif}
      { CodeComplete dialog }
      dialog_codecomplete     = 'CodeComplete';
      label_codecomplete_keywords = '~K~eywords';

      dialog_codecomplete_add = 'Add new keyword';
      label_codecomplete_add_keyword = 'Keyword';

      dialog_codecomplete_edit = 'Edit keyword';
      label_codecomplete_edit_keyword = 'Keyword';

      msg_codecomplete_alreadyinlist = '"%s" is already in the list';

      { standard button texts }
      button_OK          = 'O~K~';
      button_Cancel      = 'Cancel';
      button_New         = '~N~ew';
      button_Edit        = '~E~dit';
      button_Delete      = '~D~elete';

function FPCompleteCodeWord(const WordS: string; var Text: string): boolean;
var OK: boolean;
    CIndex, Index, i : sw_integer;
    St, UpWordS : string;
begin
  if ShowOnlyUnique then
    UpWordS:=UpCaseStr(WordS);
  OK:=Assigned(CodeCompleteWords);
  if OK then
  begin
    Text:=CodeCompleteWords^.Lookup(WordS,CIndex);
    OK:=(CIndex<>-1) and (length(Text)<>length(WordS));
    Index:=-1;
    if OK and ShowOnlyUnique and (CIndex<CodeCompleteWords^.Count-1) then
      begin
        St:=PString(CodeCompleteWords^.At(CIndex+1))^;
        if (UpCaseStr(Copy(St,1,length(WordS)))=UpWordS) then
          begin
            {if UpCase(st[Length(UpWordS)+1])<>Upcase(Text[Length(UpWordS)+1]) then}
              begin
                Text:='';
                FPCompleteCodeWord:=false;
                exit;
            (*  end
            else
              { only give the common part }
              begin
                i:=Length(UpWordS)+1;
                while (i<=length(st)) and (i<=length(text)) and (UpCase(st[i])=Upcase(Text[i])) do
                  inc(i);
                SetLength(Text,i-1);    *)
              end;
          end;
      end;
  end;
  if (ShowOnlyUnique or not OK) and Assigned(UnitsCodeCompleteWords) then
  begin
    Text:=UnitsCodeCompleteWords^.Lookup(WordS,Index);
    OK:=(Index<>-1) and (length(Text)<>length(WordS));
    if ShowOnlyUnique and (Index<UnitsCodeCompleteWords^.Count-1) then
      begin
        St:=PString(UnitsCodeCompleteWords^.At(Index+1))^;
        if UpCaseStr(Copy(St,1,length(WordS)))=UpWordS then
          begin
            {if UpCase(st[Length(UpWordS)+1])<>Upcase(Text[Length(UpWordS)+1]) then}
              begin
                Text:='';
                FPCompleteCodeWord:=false;
                exit;
            (*  end
            else
              { only give the common part }
              begin
                i:=Length(UpWordS)+1;
                while (i<=length(st)) and (i<=length(text)) and (UpCase(st[i])=Upcase(Text[i])) do
                  inc(i);
                SetLength(Text,i-1); *)
              end;
          end;
      end;
  end;

  if ShowOnlyUnique and (Index<>-1) and (CIndex<>-1) then
    begin
      {St:=PString(CodeCompleteWords^.At(CIndex+1))^;
       Was wrong, CIndex+1 could be above count => collection.error
       generated RTE 213
      if UpCase(st[Length(UpWordS)+1])<>Upcase(Text[Length(UpWordS)+1]) then}
        begin
          Text:='';
          FPCompleteCodeWord:=false;
          exit;
      (*  end
      else
        { only give the common part }
        begin
          i:=Length(UpWordS)+1;
          while (i<=length(st)) and (i<=length(text)) and (UpCase(st[i])=Upcase(Text[i])) do
            inc(i);
          SetLength(Text,i-1); *)
        end;
    end;
  if OK=false then Text:=''
  else case CodeCompleteCase of
    ccc_upper : Text:=UpcaseStr(Text);
    ccc_lower : Text:=LowcaseStr(Text);
    ccc_mixed : Text:=UpCase(Text[1])+LowCaseStr(Copy(Text,2,High(Text)));
  end;
  FPCompleteCodeWord:=OK;
end;

procedure InitCodeComplete;
var I:integer;
    S: string;
begin
  if Assigned(CodeCompleteWords) then
    Dispose(CodeCompleteWords, Done);
  New(CodeCompleteWords, Init(10,10));
  for I:=0 to GetReservedWordCount-1 do
    begin
      S:=LowCaseStr(GetReservedWord(I));
      if length(S)>=CodeCompleteMinLen then
        CodeCompleteWords^.Insert(NewStr(S));
    end;
  {
    there should be also a user front-end for customizing CodeComplete !
     any volunteers to implement? ;) - Gabor
  }
end;


procedure AddAvailableUnitsToCodeComplete(OnlyStandard : boolean);

var
  I : sw_integer;
  Overflow: boolean;
  Level : longint;
  UpStandardUnits : string;

  procedure InsertInS(P: PSymbol);

    procedure InsertItemsInS(P: PSymbolCollection);
    var I: Sw_integer;
    begin
      for I:=0 to P^.Count-1 do
        InsertInS(P^.At(I));
    end;
  Var
    st : string;
    CIndex : sw_integer;
  begin
    Inc(level);
    if UnitsCodeCompleteWords^.Count=MaxCollectionSize then
       begin Overflow:=true; Exit; end;
    st:=P^.GetName;
    if Length(st)>=CodeCompleteMinLen then
      if not ((level=1) and OnlyStandard and (st=UpCaseStr(CodeCompleteUnitName))) then
        begin
          st:=Lowcasestr(st);
          UnitsCodeCompleteWords^.LookUp(st,CIndex);
          if CIndex<>-1 then
          UnitsCodeCompleteWords^.Insert(NewStr(st));
        end;
    { this is wrong because it inserted args or locals of proc
      in the globals list !! PM}
    if (P^.Items<>nil) and (level=1) and
        ((not OnlyStandard or (Pos(P^.GetName+',',UpStandardUnits)>0) or
        { don't exclude system unit ... }
        (Pos('SYS',P^.GetName)>0))) then
      InsertItemsInS(P^.Items);
    Dec(level);
  end;

begin
  if OnlyStandard then
    UpStandardunits:=UpCaseStr(StandardUnits)+',';
  if IsSymbolInfoAvailable then
    begin
      if Assigned(UnitsCodeCompleteWords) then
        begin
          Dispose(UnitsCodeCompleteWords,done);
          UnitsCodeCompleteWords:=nil;
        end;

      New(UnitsCodeCompleteWords, Init(10,10));
      level:=0;
      Overflow:=false;
      BrowCol.Modules^.ForEach(@InsertInS);
      { if Overflow then
        WarningBox(msg_toomanysymbolscantdisplayall,nil); }
    end;
end;

procedure AddStandardUnitsToCodeComplete;
var
  HiddenSource : PSourceWindow;
  R : TRect;
  StoreBrowserSwitchesConfig : string;
begin
  Desktop^.GetExtent(R);
  New(HiddenSource,init(R,'*'));
  HiddenSource^.NoNameCount:=0;
  HiddenSource^.UpdateTitle;
  HiddenSource^.Hide;
  CompilingHiddenFile:=HiddenSource;
  { compile a dummy file to get symbol info }
  with HiddenSource^.Editor^ do
    begin
      FileName:=CodeCompleteUnitName+'.pp';
      Addline('unit '+CodeCompleteUnitName+';');
      Addline('interface');
      if StandardUnits<>'' then
        begin
          AddLine('uses');
          Addline(StandardUnits);
          Addline('  ;');
        end;
      Addline('implementation');
      Addline('end.');
      SetModified(true);
      // SaveFile;
    end;
  StoreBrowserSwitchesConfig:=BrowserSwitches^.GetCurrSelParam;
  BrowserSwitches^.ReadItemsCfg('+');
  DoCompile(cCompile);
  BrowserSwitches^.SetCurrSelParam(StoreBrowserSwitchesConfig);
  AddAvailableUnitsToCodeComplete(true);
  { Now add the interface declarations to the Code Complete list }
  CompilingHiddenFile:=nil;
  Dispose(HiddenSource,Done);
end;

function LoadCodeComplete(var S: TStream): boolean;
var C: PCodeCompleteWordList;
    OK: boolean;
    NewCodeCompleteMinLen : byte;
    NewUseStandardUnitsInCodeComplete,
    NewUseAllUnitsInCodeComplete,
    NewShowOnlyUnique : boolean;
    NewCodeCompleteCase : TCodeCompleteCase;
    StPtr : PString;
begin
  New(C, Load(S));
  OK:=Assigned(C) and (S.Status=stOk);
  if OK then
    begin
      if Assigned(CodeCompleteWords) then Dispose(CodeCompleteWords, Done);
      CodeCompleteWords:=C;
      S.Read(NewCodeCompleteCase,Sizeof(TCodeCompleteCase));
      OK:=(S.Status=stOk);
      if OK then
        CodeCompleteCase:=NewCodeCompleteCase;
      { Old version of Code complete, also OK PM }
      if not OK or (S.getPos=S.getSize) then
        begin
          LoadCodeComplete:=OK;
          exit;
        end;

      if S.Status=stOK then
        S.Read(NewUseStandardUnitsInCodeComplete,Sizeof(UseStandardUnitsInCodeComplete));
      if S.Status=stOK then
        UseStandardUnitsInCodeComplete:=NewUseStandardUnitsInCodeComplete;
      if S.Status=stOK then
        S.Read(NewUseAllUnitsInCodeComplete,Sizeof(UseAllUnitsInCodeComplete));
      if S.Status=stOK then
        UseAllUnitsInCodeComplete:=NewUseAllUnitsInCodeComplete;
      if S.Status=stOK then
        S.Read(NewShowOnlyUnique,Sizeof(ShowOnlyUnique));
      if S.Status=stOK then
        ShowOnlyUnique:=NewShowOnlyUnique;
      if S.Status=stOK then
        S.Read(NewCodeCompleteMinLen,Sizeof(CodeCompleteMinLen));
      if S.Status=stOK then
        CodeCompleteMinLen:=NewCodeCompleteMinLen;
      if S.Status=stOK then
        StPtr:=S.ReadStr
      else
        StPtr:=nil;
      if (S.Status=stOK) then
        StandardUnits:=GetStr(StPtr);
      if assigned(StPtr) then
        FreeMem(StPtr,Length(StandardUnits)+1);
      OK:=S.Status=stOK;
    end
  else
    if Assigned(C) then
      Dispose(C, Done);
  LoadCodeComplete:=OK;
end;

function StoreCodeComplete(var S: TStream): boolean;
var OK: boolean;
begin
  OK:=Assigned(CodeCompleteWords);
  if OK then
  begin
    CodeCompleteWords^.Store(S);
    S.Write(CodeCompleteCase,Sizeof(TCodeCompleteCase));
    { New fields added }
    S.Write(UseStandardUnitsInCodeComplete,Sizeof(UseStandardUnitsInCodeComplete));
    S.Write(UseAllUnitsInCodeComplete,Sizeof(UseAllUnitsInCodeComplete));
    S.Write(ShowOnlyUnique,Sizeof(ShowOnlyUnique));
    S.Write(CodeCompleteMinLen,Sizeof(CodeCompleteMinLen));
    S.WriteStr(@StandardUnits);
    OK:=OK and (S.Status=stOK);
  end;
  StoreCodeComplete:=OK;
end;

procedure DoneCodeComplete;
begin
  if Assigned(CodeCompleteWords) then
    begin
      Dispose(CodeCompleteWords, Done);
      CodeCompleteWords:=nil;
    end;
  if Assigned(UnitsCodeCompleteWords) then
    begin
      Dispose(UnitsCodeCompleteWords,done);
      UnitsCodeCompleteWords:=nil;
    end;
end;

constructor TCodeCompleteDialog.Init;
var R,R2,R3: TRect;
    Items: PSItem;
    SB: PScrollBar;
begin
  R.Assign(0,0,50,22);
  inherited Init(R,dialog_codecomplete);
  HelpCtx:=hcCodeCompleteOptions;

  { name list dialog }
  GetExtent(R); R.Grow(-3,-2); Inc(R.A.Y); R3.Copy(R); Dec(R.B.X,12);
  Dec(R.B.Y,7);
  R2.Copy(R); R2.Move(1,0); R2.A.X:=R2.B.X-1;
  New(SB, Init(R2)); Insert(SB);
  New(CodeCompleteLB, Init(R,1,SB));
  Insert(CodeCompleteLB);
  R2.Copy(R); R2.Move(0,-1); R2.B.Y:=R2.A.Y+1; Dec(R2.A.X);
  Insert(New(PLabel, Init(R2, label_codecomplete_keywords, CodeCompleteLB)));

  { Case choice }
  R.Copy(R3); Dec(R.B.Y,2); R.A.Y:=R.B.Y-4; Inc(R.A.X); R.B.X:=R.A.X+15;
  Items:=NewSItem('Unc~h~anged',
           NewSItem('~L~ower',
           NewSItem('~U~pper',
           NewSItem('~M~ixed',nil))));
  RB:=New(PRadioButtons,Init(R,Items));
  RB^.SetData(ord(CodeCompleteCase));
  R2.Copy(R); R2.Move(0,-1); R2.B.Y:=R2.A.Y+1; Dec(R2.A.X);
  Insert(New(PLabel, Init(R2, 'Case handling', RB)));
  Insert(RB);

  { Mininum length inputline }
  R.Copy(R3); R.A.Y:=R.B.Y-7;R.B.Y:=R.A.Y+1; Dec(R.B.X); R.A.X:=R.B.X -5;
  New(MinInputL, Init(R,5));
  MinInputL^.SetValidator(New(PRangeValidator, Init(1,255)));
  Insert(MinInputL);
  R2.Copy(R); R2.A.X:=20;Dec(R2.B.X,5);
  Insert(New(PLabel, Init(R2, 'Min. length', MinInputL)));

  { Standard/all units booleans }
  Items:=nil;
  Items:=NewSItem('Add standard units', Items);
  Items:=NewSItem('Add all units', Items);
  Items:=NewSItem('Show only unique', Items);
  R.Copy(R3); R.A.Y:=R.B.Y-5;R.B.Y:=R.A.Y+3; Inc(R.A.X,18); Dec(R.B.X);
  New(CB, Init(R, Items));
  Insert(CB);
  R2.Copy(R); R2.Move(0,-1); R2.B.Y:=R2.A.Y+1; Dec(R2.A.X);
  Insert(New(PLabel, Init(R2, 'Unit handling', CB)));
  R2.Copy(R); R2.Move(0,-1); R2.B.Y:=R2.A.Y+1;
  If ShowOnlyUnique then
    CB^.Press(0);
  If UseAllUnitsInCodeComplete then
    CB^.Press(1);
  If UseStandardUnitsInCodeComplete then
    CB^.Press(2);

  { Standard unit name boolean }
  R.Copy(R3); R.A.Y:=R.B.Y-1; Inc(R.A.X); Dec(R.B.X);
  New(InputL,Init(R,255));
  Insert(InputL);
  InputL^.SetValidator(New(PFilterValidator,Init(NumberChars+AlphaChars+[','])));
  R2.Copy(R); R2.Move(0,-1); R2.B.Y:=R2.A.Y+1; Dec(R2.A.X);R2.B.X:=R2.A.X+25;
  Insert(New(PLabel, Init(R2, '~S~tandard unit list', InputL)));

  R.Copy(R3); R.A.X:=R.B.X-10; R.B.Y:=R.A.Y+2;
  Insert(New(PButton, Init(R, button_OK, cmOK, bfNormal)));
  R.Move(0,2);
  Insert(New(PButton, Init(R, button_Edit, cmEditItem, bfDefault)));
  R.Move(0,2);
  Insert(New(PButton, Init(R, button_New, cmAddItem, bfNormal)));
  R.Move(0,2);
  Insert(New(PButton, Init(R, button_Delete, cmDeleteItem, bfNormal)));
  R.Move(0,2);
  Insert(New(PButton, Init(R, button_Cancel, cmCancel, bfNormal)));
  SelectNext(false);
end;

procedure TCodeCompleteDialog.HandleEvent(var Event: TEvent);
var DontClear: boolean;
begin
  case Event.What of
    evKeyDown :
      begin
        DontClear:=false;
        case Event.KeyCode of
          kbIns  :
            Message(@Self,evCommand,cmAddItem,nil);
          kbDel  :
            Message(@Self,evCommand,cmDeleteItem,nil);
        else DontClear:=true;
        end;
        if DontClear=false then ClearEvent(Event);
      end;
    evBroadcast :
      case Event.Command of
        cmListItemSelected :
          if Event.InfoPtr=pointer(CodeCompleteLB) then
            Message(@Self,evCommand,cmEditItem,nil);
      end;
    evCommand :
      begin
        DontClear:=false;
        case Event.Command of
          cmAddItem    : Add;
          cmDeleteItem : Delete;
          cmEditItem   : Edit;
        else DontClear:=true;
        end;
        if DontClear=false then ClearEvent(Event);
      end;
  end;
  inherited HandleEvent(Event);
end;

function TCodeCompleteDialog.Execute: Word;
var R: word;
    C: PCodeCompleteWordList;
    NewVal, I: integer;
    NewValStr : string;
begin
  New(C, Init(10,20));
  if Assigned(CodeCompleteWords) then
  for I:=0 to CodeCompleteWords^.Count-1 do
    C^.Insert(NewStr(GetStr(CodeCompleteWords^.At(I))));
  CodeCompleteLB^.NewList(C);
  InputL^.SetData(StandardUnits);
  NewValStr:=IntToStr(CodeCompleteMinLen);
  MinInputL^.SetData(NewValStr);
  R:=inherited Execute;
  if R=cmOK then
    begin
      if Assigned(CodeCompleteWords) then Dispose(CodeCompleteWords, Done);
      CodeCompleteWords:=C;
      CodeCompleteCase:=TCodeCompleteCase(RB^.Value);
      MinInputL^.GetData(NewValStr);
      NewVal:=StrToInt(NewValStr);
      if (NewVal>0) and (NewVal<>CodeCompleteMinLen) then
        begin
          CodeCompleteMinLen:=NewVal;
          InitCodeComplete;
        end;
      ShowOnlyUnique:=CB^.Mark(0);
      UseAllUnitsInCodeComplete:=CB^.Mark(1);
      UseStandardUnitsInCodeComplete:=CB^.Mark(2);
      if UseStandardUnitsInCodeComplete and (not UseAllUnitsInCodeComplete or not assigned(UnitsCodeCompleteWords)) and
         ((StandardUnits<>GetStr(InputL^.Data)) or not assigned(UnitsCodeCompleteWords)) then
        begin
          InputL^.GetData(StandardUnits);
          AddStandardUnitsToCodeComplete;
        end
      else
        InputL^.GetData(StandardUnits);
    end
  else
    Dispose(C, Done);
  Execute:=R;
end;

procedure TCodeCompleteDialog.Add;
var IC: boolean;
    S: string;
    P: PString;
    Cmd: word;
    CanExit: boolean;
    I: sw_integer;
begin
  IC:=CodeCompleteLB^.Range=0;
  if IC=false then
    S:=GetStr(CodeCompleteLB^.List^.At(CodeCompleteLB^.Focused))
  else
    S:='';

  repeat
    Cmd:=InputBox(dialog_codecomplete_add,label_codecomplete_add_keyword,S,255);
    CanExit:=Cmd<>cmOK;
    if CanExit=false then
      begin
        CanExit:=PCodeCompleteWordList(CodeCompleteLB^.List)^.Search(@S,I)=false;
        if CanExit=false then
        begin
          ClearFormatParams; AddFormatParamStr(S);
          ErrorBox(msg_codecomplete_alreadyinlist,@FormatParams);
        end;
      end;
  until CanExit;

  if Cmd=cmOK then
    begin
      P:=NewStr(S);
      with CodeCompleteLB^ do
      begin
        List^.Insert(P);
        SetRange(List^.Count);
        SetFocusedItem(P);
      end;
      ReDraw;
    end;
end;

procedure TCodeCompleteDialog.Edit;
var S: string;
    I,T: sw_integer;
    Cmd: word;
    CanExit: boolean;
    P: PString;
begin
  if CodeCompleteLB^.Range=0 then Exit;
  I:=CodeCompleteLB^.Focused;
  S:=GetStr(CodeCompleteLB^.List^.At(I));
  repeat
    Cmd:=InputBox(dialog_codecomplete_edit,label_codecomplete_edit_keyword,S,255);
    CanExit:=Cmd<>cmOK;
    if CanExit=false then
      begin
        CanExit:=PCodeCompleteWordList(CodeCompleteLB^.List)^.Search(@S,T)=false;
        CanExit:=CanExit or (T=I);
        if CanExit=false then
        begin
          ClearFormatParams; AddFormatParamStr(S);
          ErrorBox(msg_codecomplete_alreadyinlist,@FormatParams);
        end;
      end;
  until CanExit;

  if Cmd=cmOK then
    begin
      P:=NewStr(S);
      with CodeCompleteLB^ do
      begin
        List^.AtFree(I);
        List^.Insert(P);
        SetFocusedItem(P);
      end;
      ReDraw;
    end;
end;

procedure TCodeCompleteDialog.Delete;
begin
  if CodeCompleteLB^.Range=0 then Exit;
  CodeCompleteLB^.List^.AtFree(CodeCompleteLB^.Focused);
  CodeCompleteLB^.SetRange(CodeCompleteLB^.List^.Count);
  ReDraw;
end;

procedure RegisterCodeComplete;
begin
{$ifndef NOOBJREG}
  RegisterType(RCodeCompleteWordList);
{$endif}
end;

END.