minor
[far2l.git] / incsrch / incsrch.c
blobba02f4d47b20fb90979c455dd5f5f135efd0f401
1 /*
2 FAR manager incremental search plugin, search as you type in editor.
3 Copyright (C) 1999-2019, Stanislav V. Mekhanoshin
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
18 #include "incsrch.h"
20 static const TCHAR sDirIncSearch[] =
21 #if defined(WINPORT_DIRECT)
22 _T("/Incremental Search");
23 #else
24 _T("\\Incremental Search");
25 #endif
27 static const TCHAR sHlfConfig[] = _T("Cfg");
28 static const TCHAR sHlfMenu[] = _T("Menu");
30 INT_PTR ModuleNumber;
31 FARAPIGETMSG apiGetMsg;
32 FARAPIMENU apiMenu;
33 FARAPITEXT apiText;
34 FARAPIEDITORCONTROL apiEditorControl;
35 #ifdef VIEWVER_SUPPORT
36 FARAPIVIEWERCONTROL apiViewerControl;
37 int iViewerStatusColor;
38 #endif
39 #if defined(WINPORT_DIRECT)
40 FARAPIDIALOGINIT apiDialogInit;
41 FARAPIDIALOGRUN apiDialogRun;
42 FARAPIDIALOGFREE apiDialogFree;
43 FARAPISENDDLGMESSAGE apiSendDlgMessage;
44 FARSTDSNPRINTF apiSnprintf;
45 #else
46 FARAPIDIALOG apiDialog;
47 FARAPICHARTABLE apiCharTable;
48 #endif
50 BOOL bEscape;
51 BOOL bTermEvent;
52 int nEvents;
53 #if !defined(WINPORT_DIRECT)
54 HANDLE hInputHandle;
55 #endif
56 BOOL bNotFound;
57 KbdCommand aEvents[PREVIEW_EVENTS];
58 INPUT_RECORD Event;
60 static TCHAR PluginRootKey[_MAX_PATH];
62 BOOL bThisUseSelection;
63 BOOL bThisAutoNext;
64 BOOL bStopOnFound;
65 BOOL bReverse = FALSE;
67 BOOL bCaseSensitive = FALSE;
68 BOOL bKeepSelection = TRUE;
69 BOOL bBeepOnMismatch = FALSE;
70 BOOL bRestartEOF = FALSE;
71 BOOL bUseSelection = TRUE;
72 BOOL bAutoNext = FALSE;
73 BOOL bBSunroll = TRUE;
75 TCHAR sStr[MAX_STR];
76 int nLen;
78 struct EditorInfo ei;
79 #if !defined(WINPORT_DIRECT)
80 struct EditorConvertText ect;
81 #endif
82 struct EditorSetPosition esp;
83 struct EditorGetString egs;
85 void RestoreConfig(void);
86 void SaveConfig(void);
88 #if !defined(WINPORT_DIRECT)
89 #ifndef SHAREDSYMBOL
90 #define SHAREDSYMBOL
91 #endif
92 #ifndef EXP_NAME
93 #define EXP_NAME(x) x
94 #endif
95 #endif
97 void __plugin WINAPI EXP_NAME(SetStartupInfo)(const struct PluginStartupInfo *pInfo)
99 ModuleNumber = pInfo->ModuleNumber;
100 apiMenu = pInfo->Menu;
101 apiGetMsg = pInfo->GetMsg;
102 #if defined(WINPORT_DIRECT)
103 apiDialogInit = pInfo->DialogInit;
104 apiDialogRun = pInfo->DialogRun;
105 apiDialogFree = pInfo->DialogFree;
106 apiSendDlgMessage = pInfo->SendDlgMessage;
107 apiSnprintf = pInfo->FSF->snprintf;
108 #else
109 apiDialog = pInfo->Dialog;
110 apiCharTable = pInfo->CharTable;
111 #endif
112 apiText = pInfo->Text;
113 apiEditorControl = pInfo->EditorControl;
114 _tstrcpy(PluginRootKey, pInfo->RootKey);
115 _tstrcpy(PluginRootKey + _tstrlen(PluginRootKey), sDirIncSearch);
116 #ifdef VIEWVER_SUPPORT
117 apiViewerControl = pInfo->ViewerControl;
118 iViewerStatusColor = pInfo->AdvControl(ModuleNumber, ACTL_GETCOLOR, (void *)COL_VIEWERSTATUS);
119 #endif
120 RestoreConfig();
123 int __plugin WINAPI EXP_NAME(Configure)(int ItemNumber)
125 struct FarDialogItem DialogItems[11];
126 static DialogTemplateItem DialogTemplate[10] = {
127 {DI_CHECKBOX, 5, 2, FALSE, DIFT_MSGNUM, MCaseSensitive },
128 {DI_CHECKBOX, 5, 3, FALSE, DIFT_MSGNUM, MRestartEOF },
129 {DI_CHECKBOX, 5, 4, FALSE, DIFT_MSGNUM, MKeepSelection },
130 {DI_CHECKBOX, 5, 5, FALSE, DIFT_MSGNUM, MBeepOnMismatch},
131 {DI_CHECKBOX, 5, 6, FALSE, DIFT_MSGNUM, MUseSelection },
132 {DI_CHECKBOX, 5, 7, FALSE, DIFT_MSGNUM, MAutoNext },
133 {DI_CHECKBOX, 5, 8, FALSE, DIFT_MSGNUM, MBSunroll },
134 {DI_TEXT, 5, 9, FALSE, DIF_BOXCOLOR | DIF_SEPARATOR, 0 },
135 {DI_BUTTON, 5, 10, FALSE, DIF_CENTERGROUP | DIFT_MSGNUM, MOk },
136 {DI_BUTTON, 5, 10, FALSE, DIF_CENTERGROUP | DIFT_MSGNUM, MCancel }
139 (void)ItemNumber;
141 DialogTemplate[0].Selected = (signed char)bCaseSensitive;
142 DialogTemplate[1].Selected = (signed char)bRestartEOF;
143 DialogTemplate[2].Selected = (signed char)bKeepSelection;
144 DialogTemplate[3].Selected = (signed char)bBeepOnMismatch;
145 DialogTemplate[4].Selected = (signed char)bUseSelection;
146 DialogTemplate[5].Selected = (signed char)bAutoNext;
147 DialogTemplate[6].Selected = (signed char)bBSunroll;
149 if (DialogFromTemplate(GetMsg(MIncSearch), DialogTemplate, DialogItems,
150 sizeof(DialogItems) / sizeof(DialogItems[0]), (TCHAR *)sHlfConfig, 1, 9)
151 != 9)
152 return FALSE;
154 bCaseSensitive = DialogItems[1].Selected;
155 bRestartEOF = DialogItems[2].Selected;
156 bKeepSelection = DialogItems[3].Selected;
157 bBeepOnMismatch = DialogItems[4].Selected;
158 bUseSelection = DialogItems[5].Selected;
159 bAutoNext = DialogItems[6].Selected;
160 bBSunroll = DialogItems[7].Selected;
162 SaveConfig();
163 return TRUE;
166 void __plugin WINAPI EXP_NAME(GetPluginInfo)(struct PluginInfo *Info)
168 static const TCHAR *sPtr;
170 sPtr = GetMsg(MIncSearch);
171 Info->StructSize = sizeof(*Info);
172 Info->Flags =
173 #ifdef VIEWVER_SUPPORT
174 PF_VIEWER |
175 #endif
176 PF_DISABLEPANELS | PF_EDITOR;
177 Info->DiskMenuStringsNumber = 0;
178 Info->PluginMenuStrings = &sPtr;
179 Info->PluginMenuStringsNumber = 1;
180 Info->PluginConfigStrings = &sPtr;
181 Info->PluginConfigStringsNumber = 1;
184 HANDLE __plugin WINAPI EXP_NAME(OpenPlugin)(int OpenFrom, INT_PTR Item)
186 struct FarMenuItem aMenuItems[7];
187 int nItems;
189 (void)OpenFrom;
190 (void)Item;
192 bThisUseSelection = bUseSelection;
193 bThisAutoNext = bAutoNext;
194 bStopOnFound = FALSE;
196 zeromem(aMenuItems, sizeof(aMenuItems));
197 #if defined(WINPORT_DIRECT)
198 aMenuItems[0].Text = GetMsg(MSearchForward);
199 aMenuItems[1].Text = GetMsg(MSearchBackward);
200 #else
201 _tstrcpy(aMenuItems[0].Text, GetMsg(MSearchForward));
202 _tstrcpy(aMenuItems[1].Text, GetMsg(MSearchBackward));
203 #endif
204 aMenuItems[2].Separator = TRUE;
205 if (OpenFrom == OPEN_EDITOR) {
206 #if defined(WINPORT_DIRECT)
207 aMenuItems[3].Text = GetMsg(MFindNext);
208 aMenuItems[4].Text = GetMsg(MFindPrevious);
209 #else
210 _tstrcpy(aMenuItems[3].Text, GetMsg(MFindNext));
211 _tstrcpy(aMenuItems[4].Text, GetMsg(MFindPrevious));
212 #endif
213 aMenuItems[5].Separator = TRUE;
214 nItems = sizeof(aMenuItems) / sizeof(aMenuItems[0]);
215 } else {
216 nItems = 4;
218 #if defined(WINPORT_DIRECT)
219 aMenuItems[nItems - 1].Text = GetMsg(MSetup);
220 #else
221 _tstrcpy(aMenuItems[nItems - 1].Text, GetMsg(MSetup));
222 #endif
223 aMenuItems[bReverse ? 1 : 0].Selected = TRUE;
224 Menu:
225 switch (apiMenu(ModuleNumber, -1, -1, 0, FMENU_WRAPMODE, GetMsg(MIncSearch), NULL, (TCHAR *)sHlfMenu,
226 NULL, NULL, aMenuItems, nItems)) {
227 case 0:
228 bReverse = FALSE;
229 break;
230 case 1:
231 bReverse = TRUE;
232 break;
233 case 3:
234 if (OpenFrom == OPEN_EDITOR) {
235 bReverse = FALSE;
236 goto Auto;
237 case 4:
238 bReverse = TRUE;
239 Auto:
240 bThisUseSelection = TRUE;
241 bThisAutoNext = TRUE;
242 bStopOnFound = TRUE;
243 break;
245 case 6:
246 EXP_NAME(Configure)(0);
247 goto Menu;
248 default:
249 return INVALID_HANDLE_VALUE;
251 if (OpenFrom == OPEN_EDITOR)
252 SearchLoopEditor();
253 #ifdef VIEWVER_SUPPORT
254 else
255 SearchLoopViewer();
256 #endif
257 return INVALID_HANDLE_VALUE;