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   MultiPaste text - Lazarus addon
22 
23   Author:        Silvio Clecio  (https://github.com/silvioprog)
24   Inspired by:   Delphi Multi Paste
25   Last Modified: Fri Sep 30 15:22:18 EDT 2016
26 
27   Abstract:
28 
29   The MultiPaste dialog enables you to modify and paste into the
30   Source Editor the text you copied to the clipboard. The MultiPaste feature
31   helps you to work with SQL, HTML, JSON, formatted text, and any other text.
32 
33 }
34 
35 unit MultiPasteDlg;
36 
37 {$mode objfpc}{$H+}
38 
39 interface
40 
41 uses
42   SysUtils, Classes,
43   // LCL
44   StdCtrls, ButtonPanel, Forms, Clipbrd,
45   // SynEdit
46   SynEdit,
47   // IdeIntf
48   IDEHelpIntf,
49   // IDE
50   InputHistory, IDEProcs, LazarusIDEStrConsts, EnvironmentOpts;
51 
52 const
53   hlFormatPasteTxtBefore = 'FormatPasteTxtBefore';
54   hlFormatPasteTxtAfter = 'FormatPasteTxtAfter';
55 
56 type
57 
58   { TMultiPasteDialog }
59 
60   TMultiPasteDialog = class(TForm)
61     BottomButtonPanel: TButtonPanel;
62     PreviewSynEdit: TSynEdit;
63     TrimClipbrdContentsCheckBox: TCheckBox;
64     EscQuotesCheckBox: TCheckBox;
65     EscQuotesStyleComboBox: TComboBox;
66     TxtAfterLinesComboBox: TComboBox;
67     TxtBeforeLinesComboBox: TComboBox;
68     TxtBeforeLinesLabel: TLabel;
69     TxtAfterLinesLabel: TLabel;
70     PasteOptsGroupBox: TGroupBox;
71     PreviewGroupBox: TGroupBox;
72     procedure FormCreate(Sender: TObject);
73     procedure FormDestroy(Sender: TObject);
74   private
75     FContent: TStringList;
76   protected
77     procedure DoWatch(Sender: TObject); virtual;
78     procedure DoUpdatePreview; virtual;
DoFormatContentnull79     function DoFormatContent(const AContent: string): string; virtual;
80     procedure DoEscQuotesCheckBoxChange(Sender: TObject); virtual;
81     procedure DoHelpButtonClick(Sender: TObject); virtual;
82   public
83     property Content: TStringList read FContent;
84   end;
85 
86 implementation
87 
88 {$R *.lfm}
89 
90 procedure TMultiPasteDialog.FormCreate(Sender: TObject);
91 var
92   List: THistoryList;
93 begin
94   FContent := TStringList.Create;
95   {$IF FPC_FULLVERSION >= 30101}
96   FContent.SkipLastLineBreak := True;
97   {$ENDIF}
98 
99   OnShow := @DoWatch;
100   OnActivate := @DoWatch;
101   TxtBeforeLinesComboBox.OnChange := @DoWatch;
102   TxtAfterLinesComboBox.OnChange := @DoWatch;
103   EscQuotesCheckBox.OnChange := @DoEscQuotesCheckBoxChange;
104   EscQuotesStyleComboBox.OnChange := @DoWatch;
105   TrimClipbrdContentsCheckBox.OnChange := @DoWatch;
106   BottomButtonPanel.HelpButton.OnClick := @DoHelpButtonClick;
107 
108   List:=InputHistories.HistoryLists.GetList(hlFormatPasteTxtBefore,true,rltCaseSensitive);
109   List.AppendEntry('Add(''');
110   TxtBeforeLinesComboBox.Items.Assign(List);
111   TxtBeforeLinesComboBox.Text:=List[0];
112 
113   List:=InputHistories.HistoryLists.GetList(hlFormatPasteTxtAfter,true,rltCaseSensitive);
114   List.AppendEntry(''');');
115   TxtAfterLinesComboBox.Items.Assign(List);
116   TxtAfterLinesComboBox.Text:=List[0];
117 
118   Caption := lismpMultiPaste;
119   PasteOptsGroupBox.Caption := lismpPasteOptions;
120   TxtBeforeLinesLabel.Caption := lismpTextBeforeEachLine;
121   TxtAfterLinesLabel.Caption := lismpTextAfterEachLine;
122   EscQuotesCheckBox.Caption := lismpEscapeQuotes;
123   EscQuotesStyleComboBox.Items[0] := lismpPascalStyle;
124   EscQuotesStyleComboBox.Items[1] := lismpCStyle;
125   TrimClipbrdContentsCheckBox.Caption := lismpTrimClipboardContents;
126   PreviewGroupBox.Caption := lismpPreview;
127   TxtBeforeLinesComboBox.DropDownCount := EnvironmentOptions.DropDownCount;
128   TxtAfterLinesComboBox.DropDownCount := EnvironmentOptions.DropDownCount;
129   EscQuotesStyleComboBox.DropDownCount := EnvironmentOptions.DropDownCount;
130 end;
131 
132 procedure TMultiPasteDialog.FormDestroy(Sender: TObject);
133 begin
134   TxtBeforeLinesComboBox.AddHistoryItem(TxtBeforeLinesComboBox.Text,20,true,false);
135   InputHistories.HistoryLists.GetList(hlFormatPasteTxtBefore,true,rltCaseSensitive)
136     .Assign(TxtBeforeLinesComboBox.Items);
137 
138   TxtAfterLinesComboBox.AddHistoryItem(TxtAfterLinesComboBox.Text,20,true,false);
139   InputHistories.HistoryLists.GetList(hlFormatPasteTxtAfter,true,rltCaseSensitive)
140     .Assign(TxtAfterLinesComboBox.Items);
141 
142   FreeAndNil(FContent);
143 end;
144 
145 procedure TMultiPasteDialog.DoWatch(Sender: TObject);
146 begin
147   DoUpdatePreview;
148 end;
149 
150 procedure TMultiPasteDialog.DoUpdatePreview;
151 begin
152   if Clipboard.HasFormat(CF_TEXT) then
153     PreviewSynEdit.Lines.Text := DoFormatContent(Clipboard.AsText)
154   else
155     PreviewSynEdit.Clear;
156 end;
157 
DoFormatContentnull158 function TMultiPasteDialog.DoFormatContent(const AContent: string): string;
159 var
160   I: Integer;
161   S: string;
162 begin
163   FContent.Text := AContent;
164   for I := 0 to Pred(FContent.Count) do
165   begin
166     S := FContent[I];
167     if TrimClipbrdContentsCheckBox.Checked then
168       S := Trim(S);
169     if EscQuotesCheckBox.Checked then
170       case EscQuotesStyleComboBox.ItemIndex of
171         0: S := StringReplace(S, '''', '''''', [rfReplaceAll]);
172         1: S := StringReplace(S, '"', '\"', [rfReplaceAll]);
173       end;
174     FContent[I] := Concat(TxtBeforeLinesComboBox.Text, S, TxtAfterLinesComboBox.Text);
175   end;
176   Result := FContent.Text;
177 end;
178 
179 procedure TMultiPasteDialog.DoEscQuotesCheckBoxChange(Sender: TObject);
180 begin
181   DoWatch(Sender);
182   EscQuotesStyleComboBox.Enabled := EscQuotesCheckBox.Checked;
183 end;
184 
185 procedure TMultiPasteDialog.DoHelpButtonClick(Sender: TObject);
186 begin
187   LazarusHelp.ShowHelpForIDEControl(Self);
188 end;
189 
190 end.
191 
192