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

    Help routines for the IDE

    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 FPHelp;

{$ifdef cpullvm}
{$modeswitch nestedprocvars}
{$endif}

interface

uses
  Drivers,
  FVConsts,
  WHelp,WHlpView,WHTML,
  WEditor,WCEdit,
  WViews,WHTMLScn,
  FPViews;

type
    PIDEStatusLine = ^TIDEStatusLine;
    TIDEStatusLine = object(TAdvancedStatusLine)
      function  Hint(AHelpCtx: Word): String; virtual;
      procedure HandleEvent(var Event: TEvent); virtual;
    end;

    PFPHTMLFileLinkScanner = ^TFPHTMLFileLinkScanner;
    TFPHTMLFileLinkScanner = object(THTMLFileLinkScanner)
       function    CheckURL(const URL: string): boolean; virtual;
       function    CheckText(const Text: string): boolean; virtual;
       procedure   ProcessDoc(Doc: PHTMLLinkScanFile); virtual;
    end;

procedure Help(FileID, Context: THelpCtx; Modal: boolean);
procedure HelpIndex(Keyword: string);
procedure HelpTopicSearch(Editor: PEditor);
procedure HelpTopic(const S: string);
procedure CloseHelpWindows;
procedure HelpDebugInfos;

procedure InitHelpSystem;
procedure DoneHelpSystem;

procedure InitHelpFiles;
procedure DoneHelpFiles;
procedure CheckHelpSystem;

procedure PushStatus(S: string);
procedure SetStatus(S: string);
procedure ClearStatus;
procedure PopStatus;

const
      HelpWindow     : PFPHelpWindow = nil;
      HelpInited     : boolean = false;

implementation

uses Objects,Views,App,MsgBox,
     WUtils,WOAHelp,WHTMLHlp,WNGHelp,WOS2Help,WVPHelp,WWinHelp,
     FPConst,FPVars,FPUtils;

const
    MaxStatusLevel = 10;

var StatusStack : array[0..MaxStatusLevel] of string[MaxViewWidth];

const
      StatusStackPtr  : integer = 0;

{$ifdef useresstrings}
resourcestring
{$else}
const
{$endif}
      dialog_help = 'Help';

      msg_modalhelpnotimplemented = 'Sorry, modal help not yet implemented.';

      { Help messages }
      msg_indexingfile = 'Indexing file %s';
      msg_loadinghelpfiles = 'Loading help files...';
      msg_loadinghelpfile = 'Loading help file...';
      msg_buildinghelpindex = 'Building Help Index...';
      msg_locatingtopic = 'Locating topic...';
      msg_failedtoloadhelpfile = 'Failed to load help file %s';

      { Menu hints }
      hint_systemmenu        = 'System menu';
      hint_update            = 'Refresh and redraw display';
      hint_about             = 'Show version and copyright information';
      hint_filemenu          = 'File management commands (Open, New, Save, etc.)';
      hint_filenew           = 'Create a new file in a new edit window';
      hint_filenewfromtemplate='Create a new file using a code template';
      hint_fileopen          = 'Locate and open a file in an edit window';
      hint_filesave          = 'Save the file in the active edit window';
      hint_filesaveas        = 'Save the current file under a different name, directory or drive';
      hint_filesaveall       = 'Save all modified files';
      hint_print             = 'Print current file';
      hint_printersetup      = 'Setup printer output device';
      hint_changedir         = 'Choose a new default directory';
      hint_dosshell          = 'Temporarily exit to shell';
      hint_exit              = 'Exit the IDE';
      hint_openrecentfile    = 'Open ';
      hint_editmenu          = 'Clipboard editing commands';
      hint_editundo          = 'Undo the previous editor operation';
      hint_editredo          = 'Redo the previously undone editor operation';
      hint_editcut           = 'Remove the selected text and put it in the clipboard';
      hint_editcopy          = 'Copy the selected text in the clipboard';
      hint_editpaste         = 'Insert selected text from the clipboard at the cursor position';
      {$ifdef HASAMIGA}
      {$ifdef AROS}
      hint_editcopywin       = 'Copy the selected text in AROS clipboard';
      hint_editpastewin      = 'Insert selected text from AROS clipboard at the cursor position';
      {$else}
      hint_editcopywin       = 'Copy the selected text to the system clipboard';
      hint_editpastewin      = 'Insert selected text from the system clipboard at the cursor position';
      {$endif}
      {$else}
      hint_editcopywin       = 'Copy the selected text in windows clipboard';
      hint_editpastewin      = 'Insert selected text from windows clipboard at the cursor position';
      {$endif}
      hint_editclear         = 'Delete the selected text';
      hint_editselectall     = 'Select the whole text';
      hint_editunselect      = 'Unselect everything';
      hint_showclipboard     = 'Open then clipboard window';
      hint_searchmenu        = 'Text and symbols search commands';
      hint_searchfind        = 'Search for text';
      hint_searchreplace     = 'Search for text and replace it with new text';
      hint_replaceagain      = 'Repeat the last Replace command (%s with %s)';
      hint_searchagain       = 'Repeat the last Search  command (%s)';
      hint_gotoline          = 'Move the cursor to a specified line number';
      hint_objects           = 'Open a browser displaying all objects in the program';
      hint_modules           = 'Open a browser displaying all modules of the program';
      hint_globals           = 'Open a browser displaying all global symbols in the program';
      hint_symbol            = 'Open a browser a current word (not yet scope sensitive)';
      hint_runmenu           = 'Execution and parameters';
      hint_run               = 'Run the current program';
      hint_rundir            = 'Set directory that will be used as current working directory at execution';
      hint_runparameters     = 'Set command-line parameters passed to program at execution';
      hint_resetprogram      = 'Reset Program';
      hint_rununtilcursor    = 'Go on until Cursor position';
      hint_rununtilreturn    = 'Go on until end of current function';
      hint_userscreen        = 'Switch to the full-screen user output';
      hint_compilemenu       = 'Compile, build & make';
      hint_compile           = 'Compile the current source file';
      hint_make              = 'Rebuild source file and all other files that have been modified';
      hint_build             = 'Rebuild program and all available source files';
      hint_target            = 'Select target platform to compile for';
      hint_primaryfile       = 'Define the file that is the focus of Make and Build';
      hint_clearprimaryfile  = 'Clear the file previously set to Primary';
      hint_information       = 'Show compiler messages and program information';
      hint_showmessages      = 'Show compiler messages window';
      hint_debugmenu         = 'Debug Program';
      hint_togglebreakpoint  = 'Toggles Breakpoint';
      hint_createnewbreakpoint = 'Create a new breakpoint';
      hint_editbreakpoint    = 'Edit focused breakpoint';
      hint_deletebreakpoint  = 'Delete focused breakpoint';
      hint_opengdbwindow     = 'Open direct window to GDB';
      hint_addwatch          = 'Add a new expression to watch';
      hint_watches           = 'Open the Watches Window';
      hint_callstack         = 'Show call stack';
      hint_editbreakpoints   = 'Edit breakpoints';
      hint_toolsmenu         = 'User installed tools';
      hint_calculator        = 'Show calculator';
      hint_grep              = 'Run grep';
      hint_gotosource        = 'Edit source';
      hint_registers         = 'Open the Registers Window';
      hint_fpuregisters      = 'Open the FPU Registers Window';
      hint_vectorregisters   = 'Open the Vector Registers Window';
      hint_messageswindow    = 'Open the message window';
      hint_gotonextmsg       = 'Jumps to the next message in the Message Window';
      hint_gotoprevmsg       = 'Jumps to the previous message in the Message Window';
      hint_usertool          = 'User installed tool';
      hint_asciitable        = 'Show ASCII table';
      hint_optionsmenu       = 'Setting for compiler, editor, mouse, etc.';
      hint_switchesmode      = 'Select settings for normal, debug or release version';
      hint_compiler          = 'Set default compiler directives and conditional defines';
      hint_memorysizes       = 'Set default stack and heap sizes for generated programs';
      hint_linkeroptions     = 'Set linker options';
      hint_debugoptions      = 'Set debug information options';
      hint_remotedialog      = 'Set remote protocol parameters';
      hint_transferremote    = 'Transfer executable to remote target';
      hint_directories       = 'Set paths for units, include, object and generated files';
      hint_browser           = 'Specify global browser settings';
      hint_reloadmodifiedfile= 'Reload file modified on disk';
      hint_tools             = 'Create or change tools';
      hint_environmentmenu   = 'Specify environment settins';
      hint_preferences       = 'Specify preferences settings';
      hint_editoroptions     = 'Specify default editor settings';
      hint_codecomplete      = 'Specify CodeComplete keywords';
      hint_codetemplates     = 'Specify CodeTemplates';
      hint_mouseoptions      = 'Specify mouse settings';
      hint_desktopoptions    = 'Specify desktop settings';
      hint_startup           = 'Permanently change default startup options';
      hint_colors            = 'Customize IDE colors for windows, menus, editors, etc.';
      hint_openini           = 'Load a previously saved options file';
      hint_saveini           = 'Save all the changes made in the options menu';
      hint_saveasini         = 'Save all the changes made under a different name';
      hint_windowmenu        = 'Windows management commands';
      hint_tile              = 'Arrange windows on desktop by tiling';
      hint_cascade           = 'Arrange windows on desktop by cascading';
      hint_closeall          = 'Close all windows on the desktop';
      hint_resize            = 'Change the size/postion of the active window';
      hint_zoom              = 'Enlarge or restore the size of the active window';
      hint_next              = 'Make the next window active';
      hint_prev              = 'Make the previous window active';
      hint_hide              = 'Hide the current window';
      hint_closewindow       = 'Close the active window';
      hint_windowlist        = 'Show a list of all open windows';
      hint_userscreenwindow  = 'Show contents of user screen in a window';
      hint_helpmenu          = 'Get online help';
      hint_helpcontents      = 'Show table of contents for Online Help';
      hint_helpindex         = 'Show index for Online Help';
      hint_helptopicsearch   = 'Display help on the word at cursor';
      hint_helpprevtopic     = 'Redisplay the last-viewed Online Help screen';
      hint_helphowtouse      = 'How to use Online Help';
      hint_helpfiles         = 'Install or remove installed help files';
      hint_openatcursor      = 'Attempt to open the file indicated by the word at cursor';
      hint_browseatcursor    = 'Attempt to browse the symbol at cursor';
      hint_editoroptionscur  = 'Specify editor settings';
      hint_rawgdbwindow      = 'Raw GDB communication window';
      hint_disassemblywindow = 'Show mixed Assembly/Source window';
      hint_allbreakpoints    = 'All current breakpoints';

procedure TIDEStatusLine.HandleEvent(var Event: TEvent);
begin
  case Event.What of
    evBroadcast :
      case Event.Command of
        cmUpdate : Update;
      end;
  end;
  inherited HandleEvent(Event);
end;

function TIDEStatusLine.Hint(AHelpCtx: Word): String;
var S: string;
begin
  case AHelpCtx of
    hcNoContext     : S:='';

    hcDragging      : S:='';

    hcSourceWindow  : S:='';
    hcHelpWindow    : S:='';
    hcCalcWindow    : S:='';
    hcInfoWindow    : S:='';
    hcClipboardWindow:S:='';
    hcBrowserWindow : S:='';
    hcMessagesWindow: S:='';
    hcCompilerMessagesWindow: S:='';
    hcASCIITableWindow: S:='';
    hcGDBWindow     : S:=hint_rawgdbwindow;
    hcDisassemblyWindow     : S:=hint_disassemblywindow;
    hcBreakpointListWindow : S:=hint_allbreakpoints;

    hcSystemMenu    : S:=hint_systemmenu;
    hcUpdate        : S:=hint_update;
    hcAbout         : S:=hint_about;

    hcFileMenu      : S:=hint_filemenu;
    hcNew           : S:=hint_filenew;
    hcNewFromTemplate:S:=hint_filenewfromtemplate;
    hcOpen          : S:=hint_fileopen;
    hcSave          : S:=hint_filesave;
    hcSaveAs        : S:=hint_filesaveas;
    hcSaveAll       : S:=hint_filesaveall;
    hcPrint         : S:=hint_print;
    hcPrinterSetup    : S:=hint_printersetup;
    hcChangeDir     : S:=hint_changedir;
    hcDOSShell      : S:=hint_dosshell;
    hcQuit          : S:=hint_exit;
    hcRecentFileBase..hcRecentFileBase+10
                    : S:=hint_openrecentfile+RecentFiles[AHelpCtx-hcRecentFileBase].FileName;

    hcEditMenu      : S:=hint_editmenu;
    hcUndo          : S:=hint_editundo;
    hcRedo          : S:=hint_editredo;
    hcCut           : S:=hint_editcut;
    hcCopy          : S:=hint_editcopy;
    hcPaste         : S:=hint_editpaste;
    hcSelectAll     : S:=hint_editselectall;
    hcUnselect      : S:=hint_editunselect;
    hcCopyWin       : S:=hint_editcopywin;
    hcPasteWin      : S:=hint_editpastewin;
    hcClear         : S:=hint_editclear;
    hcShowClipboard : S:=hint_showclipboard;

    hcSearchMenu    : S:=hint_searchmenu;
    hcFind          : S:=hint_searchfind;
    hcReplace       : S:=hint_searchreplace;
    hcSearchAgain   : begin
                        if (FindFlags and ffDoReplace)<>0 then
                          s:=formatstrstr2(hint_replaceagain,findstr,WEditor.replacestr)
                        else
                          s:=formatstrstr(hint_searchagain,findstr);
                      end;
    hcGotoLine      : S:=hint_gotoline;
    hcObjects       : S:=hint_objects;
    hcModules       : S:=hint_modules;
    hcGlobals       : S:=hint_globals;
    hcSymbol        : S:=hint_symbol;
    hcRunMenu       : S:=hint_runmenu;
    hcRun           : S:=hint_run;
    hcRunDir        : S:=hint_rundir;
    hcParameters    : S:=hint_runparameters;
    hcResetDebugger : S:=hint_resetprogram;
    hcContToCursor  : S:=hint_rununtilcursor;
    hcUntilReturn   : S:=hint_rununtilreturn;
    hcUserScreen    : S:=hint_userscreen;

    hcCompileMenu   : S:=hint_compilemenu;
    hcCompile       : S:=hint_compile;
    hcMake          : S:=hint_make;
    hcBuild         : S:=hint_build;
    hcTarget        : S:=hint_target;
    hcPrimaryFile   : S:=hint_primaryfile;
    hcClearPrimary  : S:=hint_clearprimaryfile;
    hcCompilerMessages:S:=hint_showmessages;

    hcDebugMenu     : S:=hint_debugmenu;
    hcToggleBreakpoint : S:=hint_togglebreakpoint;
    hcNewBreakpoint    : S:=hint_createnewbreakpoint;
    hcEditBreakpoint   : S:=hint_editbreakpoint;
    hcDeleteBreakpoint : S:=hint_deletebreakpoint;
    hcOpenGDBWindow : S:=hint_opengdbwindow;
    hcAddWatch      : S:=hint_addwatch;
    hcWatchesWindow : S:=hint_watches;
    hcStackWindow   : S:=hint_callstack;
    hcBreakpointList : S:=hint_editbreakpoints;
    hcToolsMenu     : S:=hint_toolsmenu;
    hcCalculator    : S:=hint_calculator;
    hcGrep          : S:=hint_grep;
    hcMsgGotoSource : S:=hint_gotosource;
    hcRegistersWindow : S:=hint_registers;
    hcFPURegisters    : S:=hint_FPURegisters;
    hcVectorRegisters : S:=hint_VectorRegisters;

    hcToolsMessages : S:=hint_messageswindow;
    hcToolsMsgNext  : S:=hint_gotonextmsg;
    hcToolsMsgPrev  : S:=hint_gotoprevmsg;

    hcToolsBase..
    hcToolsBase+MaxToolCount
                    : S:=hint_usertool;
    hcASCIITable    : S:=hint_asciitable;

    hcOptionsMenu   : S:=hint_optionsmenu;
    hcSwitchesMode  : S:=hint_switchesmode;
    hcCompiler,
    hcCompilerNoAltX  : S:=hint_compiler;
    hcMemorySizes   : S:=hint_memorysizes;
    hcLinker        : S:=hint_linkeroptions;
    hcDebugger      : S:=hint_debugoptions;
    hcDirectories   : S:=hint_directories;
    hcBrowser,
    hcBrowserOptions: S:=hint_browser;
    hcTools         : S:=hint_tools;
    hcRemoteDialog  : S:=hint_remotedialog;
    hcTransferRemote: S:=hint_transferremote;
    hcDoReload      : S:=hint_reloadmodifiedfile;

    hcEnvironmentMenu:S:=hint_environmentmenu;
    hcPreferences   : S:=hint_preferences;
    hcEditor        : S:=hint_editoroptions;
    hcCodeCompleteOptions:S:=hint_codecomplete;
    hcCodeTemplateOptions:S:=hint_codetemplates;
    hcMouse         : S:=hint_mouseoptions;
    hcDesktopOptions: S:=hint_desktopoptions;
    hcStartup       : S:=hint_startup;
    hcColors        : S:=hint_colors;
    hcOpenINI       : S:=hint_openini;
    hcSaveINI       : S:=hint_saveini;
    hcSaveAsINI     : S:=hint_saveasini;

    hcWindowMenu    : S:=hint_windowmenu;
    hcTile          : S:=hint_tile;
    hcCascade       : S:=hint_cascade;
    hcCloseAll      : S:=hint_closeall;
    hcResize        : S:=hint_resize;
    hcZoom          : S:=hint_zoom;
    hcNext          : S:=hint_next;
    hcPrev          : S:=hint_prev;
    hcHide          : S:=hint_hide;
    hcClose         : S:=hint_closewindow;
    hcWindowList    : S:=hint_windowlist;
    hcUserScreenWindow:S:=hint_userscreenwindow;

    hcHelpMenu      : S:=hint_helpmenu;
    hcHelpContents  : S:=hint_helpcontents;
    hcHelpIndex     : S:=hint_helpindex;
    hcHelpTopicSearch:S:=hint_helptopicsearch;
    hcHelpPrevTopic : S:=hint_helpprevtopic;
    hcHelpUsingHelp : S:=hint_helphowtouse;
    hcHelpFiles     : S:=hint_helpfiles;

    hcOpenAtCursor  : S:=hint_openatcursor;
    hcBrowseAtCursor: S:=hint_browseatcursor;
    hcEditorOptions : S:=hint_editoroptionscur;
  else S:='???';
  end;
  Hint:=S;
end;

procedure TFPHTMLFileLinkScanner.ProcessDoc(Doc: PHTMLLinkScanFile);
begin
  PushStatus(FormatStrStr(msg_indexingfile,Doc^.GetDocumentURL));
  inherited ProcessDoc(Doc);
  PopStatus;
end;

function TFPHTMLFileLinkScanner.CheckURL(const URL: string): boolean;
var OK: boolean;
const HTTPPrefix = 'http:';
      FTPPrefix  = 'ftp:';
begin
  OK:=inherited CheckURL(URL);
  if OK then OK:=DirAndNameOf(URL)<>'';
  if OK then OK:=CompareText(copy(ExtOf(URL),1,4),'.HTM')=0;
  if OK then OK:=CompareText(copy(URL,1,length(HTTPPrefix)),HTTPPrefix)<>0;
  if OK then OK:=CompareText(copy(URL,1,length(FTPPrefix)),FTPPrefix)<>0;
  CheckURL:=OK;
end;

function TFPHTMLFileLinkScanner.CheckText(const Text: string): boolean;
var OK: boolean;
    i : sw_integer;
    S: string;
begin
  S:=Trim(Text);
  OK:=(S<>'') and (S[1]<>'[') and (S[1]<>',');
  { remove all Indexes }
  if s[1] in ['0'..'9'] then
    begin
      i:=1;
      while (i<length(s)) and (s[i] in ['0'..'9']) do
        inc(i);
      if (i<length(s)) and (s[i] in [' ',#9,'.']) then
        OK:=false;
    end;

  CheckText:=OK;
end;

procedure InitHelpSystem;

  procedure AddHelpFile(HelpFile,Param: string);
  begin
    {$IFDEF DEBUG}SetStatus(msg_LoadingHelpFile+' ('+SmartPath(HelpFile)+')');{$ENDIF}
    if HelpFacility^.AddFile(HelpFile,Param)=nil then
      ErrorBox(FormatStrStr(msg_failedtoloadhelpfile,HelpFile),nil);
    {$IFDEF DEBUG}SetStatus(msg_LoadingHelpFile);{$ENDIF}
  end;

var I,P: sw_integer;
    S: string;
    Param: string;
begin
  New(HelpFacility, Init);

  WOAHelp.RegisterHelpType;
  WNGHelp.RegisterHelpType;
  WOS2Help.RegisterHelpType;
  WWinHelp.RegisterHelpType;
  WVPHelp.RegisterHelpType;
  WHTMLHlp.RegisterHelpType; // Also registers chm and html index (.htx)

  PushStatus(msg_LoadingHelpFiles);
  for I:=0 to HelpFiles^.Count-1 do
    begin
      S:=HelpFiles^.At(I)^; Param:='';
      P:=Pos('|',S);
      if P>0 then
        begin Param:=copy(S,P+1,High(S)); S:=copy(S,1,P-1); end;
      AddHelpFile(S,Param);
    end;
  PopStatus;
end;

procedure CheckHelpSystem;
begin
  if HelpInited then Exit;
  InitHelpSystem;
  HelpInited:=true;
end;

procedure DoneHelpSystem;
begin
  if assigned(HelpFacility) then
    begin
      Dispose(HelpFacility, Done);
      HelpFacility:=nil;
    end;
  HelpInited:=false;
end;

procedure HelpCreateWindow;
var R: TRect;
begin
  CheckHelpSystem;
  if HelpWindow=nil then
  begin
     Desktop^.GetExtent(R); R.Grow(-15,-3); Dec(R.A.Y);
     New(HelpWindow, Init(R, dialog_help, 0, 0, SearchFreeWindowNo));
     if HelpWindow<>nil then
     begin
       HelpWindow^.Hide;
       Desktop^.Insert(HelpWindow);
     end;
  end;
end;

procedure Help(FileID, Context: THelpCtx; Modal: boolean);
begin
  if Modal then
     begin MessageBox(msg_modalhelpnotimplemented,nil,mfInformation+mfInsertInApp+mfOKButton); Exit; end;
  HelpCreateWindow;
  with HelpWindow^ do
  begin
    HelpWindow^.ShowTopic(FileID,Context);
    if GetState(sfVisible)=false then Show;
    MakeFirst;
  end;
  Message(Application,evCommand,cmUpdate,nil);
end;

procedure HelpTopicSearch(Editor: PEditor);
var S: string;
begin
  if Editor=nil then S:='' else
  S:=GetEditorCurWord(Editor,[]);
  HelpTopic(S);
end;

procedure HelpTopic(const S: string);
var FileID: word;
    Ctx   : THelpCtx;
var Found: boolean;
begin
  CheckHelpSystem;
  PushStatus(msg_LocatingTopic);
  Found:=HelpFacility^.TopicSearch(S,FileID,Ctx);
  PopStatus;
  if Found then
     Help(FileID,Ctx,false)
  else
     HelpIndex(S);
end;

procedure HelpIndex(Keyword: string);
begin
  HelpCreateWindow;
  with HelpWindow^ do
  begin
    PushStatus(msg_BuildingHelpIndex);
    HelpWindow^.ShowIndex;
    if Keyword<>'' then
       HelpWindow^.HelpView^.Lookup(Keyword);
    PopStatus;
    if GetState(sfVisible)=false then Show;
    MakeFirst;
  end;
  Message(Application,evCommand,cmUpdate,nil);
end;

procedure HelpDebugInfos;
begin
  HelpCreateWindow;
  HelpWindow^.ShowDebugInfos;
end;

procedure PushStatus(S: string);
begin
  if StatusLine=nil then
    Exit;
  If StatusStackPtr<=MaxStatusLevel then
    StatusStack[StatusStackPtr]:=PAdvancedStatusLine(StatusLine)^.GetStatusText
  else
    StatusStack[MaxStatusLevel]:=PAdvancedStatusLine(StatusLine)^.GetStatusText;
  SetStatus(S);
  Inc(StatusStackPtr);
end;

procedure PopStatus;
begin
  if StatusLine=nil then
    Exit;
  Dec(StatusStackPtr);
  If StatusStackPtr<=MaxStatusLevel then
    SetStatus(StatusStack[StatusStackPtr])
  else
    SetStatus(StatusStack[MaxStatusLevel]);
end;

procedure SetStatus(S: string);
begin
  if StatusLine=nil then
    Exit;
  PAdvancedStatusLine(StatusLine)^.SetStatusText(S);
end;

procedure ClearStatus;
begin
  PAdvancedStatusLine(StatusLine)^.ClearStatusText;
end;

function FPHTMLGetSectionColor(Section: THTMLSection; var Color: byte): boolean;
var OK: boolean;
    S: string;
begin
  Color:=0;
  OK:=(ord(Section) in [1..length(CHTMLSectionAttrs)]);
  if OK then
  begin
    S:=#0;
    S:=copy(CHTMLSectionAttrs,ord(Section),1);
    if Assigned(Application)=false then Color:=0 else
    Color:=Application^.GetColor(ord(S[1]));
    if (Color and $0f) = ((Color and $f0) shr 4) then { same color ? }
      OK:=false;
  end;
  FPHTMLGetSectionColor:=OK;
end;

function FPNGGetAttrColor(Attr: char; var Color: byte): boolean;
var OK: boolean;
begin
  OK:=false;
  case Attr of
    'A' : OK:=FPHTMLGetSectionColor(hsHeading1,Color);
    'B' : OK:=FPHTMLGetSectionColor(hsHeading2,Color);
    'b' : OK:=FPHTMLGetSectionColor(hsHeading5,Color);
    'U' : OK:=FPHTMLGetSectionColor(hsHeading3,Color);
    'N' : OK:=FPHTMLGetSectionColor(hsHeading4,Color);
  {$ifdef DEBUGMSG}
  else ErrorBox('Unknown attr encountered : "'+Attr+'"',nil);
  {$endif}
  end;
  FPNGGetAttrColor:=OK;
end;

function FPINFGetAttrColor(TextStyle, TextColor: byte; var Color: byte): boolean;
var OK: boolean;
begin
  OK:=false;
  case TextColor of
    1 : OK:=FPHTMLGetSectionColor(hsHeading1,Color);
    2 : OK:=FPHTMLGetSectionColor(hsHeading2,Color);
    3 : OK:=FPHTMLGetSectionColor(hsHeading3,Color);
  end;
  FPINFGetAttrColor:=OK;
end;

procedure InitHelpFiles;
begin
  HTMLGetSectionColor:={$ifdef FPC}@{$endif}FPHTMLGetSectionColor;
  NGGetAttrColor:={$ifdef FPC}@{$endif}FPNGGetAttrColor;
  INFGetAttrColor:={$ifdef FPC}@{$endif}FPINFGetAttrColor;
  New(HelpFiles, Init(10,10));
end;

procedure DoneHelpFiles;
begin
  if assigned(HelpFiles) then
    Dispose(HelpFiles, Done);
end;

procedure CloseHelpWindows;
procedure CloseIfHelpWindow(P: PView);
begin
  if P^.HelpCtx=hcHelpWindow then
    begin
      Message(P,evCommand,cmClose,nil);
      {Dispose(P, Done);  help windows are only hidden on close so we've
                          to destroy them manually
       but this was wrong as it was not correctly
       resetting the corresponding pointer in whelp unit PM }
    end;
end;
begin
  Desktop^.ForEach(TCallbackProcParam(@CloseIfHelpWindow));
end;

END.