1 {
2  ***************************************************************************
3  *                                                                         *
4  *   This source is free software; you can redistribute it and/or modify   *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This code is distributed in the hope that it will be useful, but      *
10  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
12  *   General Public License for more details.                              *
13  *                                                                         *
14  *   A copy of the GNU General Public License is available on the World    *
15  *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
16  *   obtain it by writing to the Free Software Foundation,                 *
17  *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
18  *                                                                         *
19  ***************************************************************************
20 
21   Abstract:
22     SynEdit (actually TSynSearchOptions) dependent parts separated from
23     InputHistory unit. The idea is to reduce dependencies.
24 }
25 unit InputhistoryWithSearchOpt;
26 
27 {$mode objfpc}{$H+}
28 
29 interface
30 
31 uses
32   // LazUtils
33   Laz2_XMLCfg,
34   // SynEdit
35   SynEditTypes,
36   // IDE
37   InputHistory;
38 
39 type
40 
41   { TInputHistoriesWithSearchOpt }
42 
43   TInputHistoriesWithSearchOpt = class(TInputHistories)
44   private const
45     PathToOptions0: array[Boolean] of string = ('Options', 'Options');    // old config file
46     PathToOptions1: array[Boolean] of string = ('Options', 'SelOptions'); // new config file
47     XMLConfigVersion = 1;
48   public const
49     SaveOptionsGeneral = [ssoMatchCase, ssoWholeWord, ssoRegExpr, ssoRegExprMultiLine,
50       ssoPrompt, ssoBackwards];
51     SaveOptionsSelSpecific = [ssoEntireScope, ssoSelectedOnly];
52     SaveOptions = SaveOptionsGeneral + SaveOptionsSelSpecific;
53   private
54     FFindOptions: array[Boolean] of TSynSearchOptions; // array[SelAvail] - selection available
GetFindOptionsnull55     function GetFindOptions(const ASelAvail: Boolean): TSynSearchOptions;
56     procedure SetFindOptions(const ASelAvail: Boolean;
57       const AFindOptions: TSynSearchOptions);
58   protected
59     procedure LoadSearchOptions(XMLConfig: TXMLConfig; const Path: string); override;
60     procedure SaveSearchOptions(XMLConfig: TXMLConfig; const Path: string); override;
61   public
62     constructor Create;
63     destructor Destroy; override;
64     property FindOptions[const ASelAvail: Boolean]: TSynSearchOptions read GetFindOptions write SetFindOptions;
65   end;
66 
67 const
68   LazFindSearchOptionsDefault: array[Boolean] of TSynSearchOptions = ([], [ssoEntireScope, ssoSelectedOnly]);
69   LazFindSearchOptionNames: array[TSynSearchOption] of string = (
70     'MatchCase',
71     'WholeWord',
72     'Backwards',
73     'EntireScope',
74     'SelectedOnly',
75     'Replace',
76     'ReplaceAll',
77     'Prompt',
78     'SearchInReplacement',
79     'RegExpr',
80     'RegExprMultiLine',
81     'ssoFindContinue'
82     );
83 
84 var
85   InputHistoriesSO: TInputHistoriesWithSearchOpt = nil;
86 
87 implementation
88 
89 { TInputHistoriesWithSearchOpt }
90 
91 constructor TInputHistoriesWithSearchOpt.Create;
92 begin
93   inherited Create;
94   FFindOptions:=LazFindSearchOptionsDefault;
95 end;
96 
97 destructor TInputHistoriesWithSearchOpt.Destroy;
98 begin
99   inherited Destroy;
100 end;
101 
GetFindOptionsnull102 function TInputHistoriesWithSearchOpt.GetFindOptions(
103   const ASelAvail: Boolean): TSynSearchOptions;
104 begin
105   Result := FFindOptions[ASelAvail]*SaveOptions;
106 end;
107 
108 procedure TInputHistoriesWithSearchOpt.LoadSearchOptions(XMLConfig: TXMLConfig; const Path: string);
109 var
110   FindOption: TSynSearchOption;
111   PathToOptions: array[Boolean] of string;
112   I: Boolean;
113 begin
114   if XMLConfig.GetValue(Path+'Find/Version/Value',0)<=0 then
115     PathToOptions := PathToOptions0
116   else
117     PathToOptions := PathToOptions1;
118 
119   for I := Low(FFindOptions) to High(FFindOptions) do
120   begin
121     FFindOptions[I]:=[];
122     for FindOption:=Low(FFindOptions[I]) to High(FFindOptions[I]) do
123     begin
124       if XMLConfig.GetValue(Path+'Find/'+PathToOptions[I]+'/'+LazFindSearchOptionNames[FindOption],
125                             FindOption in LazFindSearchOptionsDefault[I])
126       then
127         Include(FFindOptions[I],FindOption);
128     end;
129   end;
130 end;
131 
132 procedure TInputHistoriesWithSearchOpt.SaveSearchOptions(XMLConfig: TXMLConfig; const Path: string);
133 var
134   FindOption: TSynSearchOption;
135   I: Boolean;
136 begin
137   for I := Low(FFindOptions) to High(FFindOptions) do
138   begin
139     XMLConfig.SetValue(Path+'Find/Version/Value', XMLConfigVersion);
140 
141     for FindOption:=Low(FFindOptions[I]) to High(FFindOptions[I]) do
142     begin
143       XMLConfig.SetDeleteValue(
144         Path+'Find/'+PathToOptions1[I]+'/'+LazFindSearchOptionNames[FindOption],
145         FindOption in FindOptions[I],
146         FindOption in LazFindSearchOptionsDefault[I]);
147     end;
148   end;
149 end;
150 
151 procedure TInputHistoriesWithSearchOpt.SetFindOptions(const ASelAvail: Boolean;
152   const AFindOptions: TSynSearchOptions);
153 begin
154   FFindOptions[ASelAvail] := AFindOptions*SaveOptions;
155   FFindOptions[not ASelAvail] := AFindOptions*SaveOptionsGeneral // change only general options
156     + FFindOptions[not ASelAvail]*SaveOptionsSelSpecific;
157 end;
158 
159 end.
160 
161