Skip to content

Commit 9826174

Browse files
Last 20 search strings(including filenames) are saved/loaded to/from config automatically.
1 parent c990d96 commit 9826174

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

NppNavigateTo/Forms/FrmNavigateTo.cs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,69 @@ public FrmNavigateTo(IScintillaGateway editor, INotepadPPGateway notepad)
101101
lastKeyPressTimer.Tick += OnSearchTimerTick;
102102
nonFuzzyFilterFunc = null;
103103
InitializeComponent();
104+
LoadSearchHistory();
104105
ReloadFileList();
105106
this.notepad.ReloadMenuItems();
106107
FormStyle.ApplyStyle(this, true, notepad.IsDarkModeEnabled());
107108
FilterDataGrid("");
108109
}
109110

111+
private void LoadSearchHistory()
112+
{
113+
try
114+
{
115+
string hist = FrmSettings.Settings.GetSetting(Settings.searchHistory);
116+
if (!string.IsNullOrEmpty(hist))
117+
{
118+
// items are stored as a pipe-separated list
119+
var items = hist.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
120+
// insert in order so most recent is first
121+
foreach (var it in items)
122+
{
123+
if (!searchComboBox.Items.Contains(it))
124+
searchComboBox.Items.Add(it);
125+
}
126+
}
127+
}
128+
catch(Exception ex)
129+
{
130+
MessageBox.Show(ex.ToString(),
131+
@"Could not load search history",
132+
MessageBoxButtons.OK,
133+
MessageBoxIcon.Error);
134+
}
135+
}
136+
137+
public void SaveSearchHistory()
138+
{
139+
try
140+
{
141+
// store up to 20 entries to keep INI small
142+
int max = 20;
143+
var list = new List<string>();
144+
foreach (var o in searchComboBox.Items)
145+
{
146+
if (o == null) continue;
147+
string s = o.ToString();
148+
if (string.IsNullOrWhiteSpace(s)) continue;
149+
if (!list.Contains(s)) list.Add(s);
150+
if (list.Count >= max) break;
151+
}
152+
// serialize as pipe-separated
153+
string serial = string.Join("|", list);
154+
FrmSettings.Settings.SetSetting(Settings.searchHistory, serial);
155+
FrmSettings.Settings.SetIntSetting(Settings.searchHistoryLength, serial.Length);
156+
}
157+
catch(Exception ex)
158+
{
159+
MessageBox.Show(ex.ToString(),
160+
@"Could not save search history",
161+
MessageBoxButtons.OK,
162+
MessageBoxIcon.Error);
163+
}
164+
}
165+
166+
110167
/// <summary>
111168
/// reload the list of files that are currently open in Notepad++
112169
/// </summary>
@@ -710,11 +767,19 @@ private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
710767

711768
private void SwitchToFile(string path, bool isTab = true)
712769
{
713-
notepad.SwitchToFile(path, isTab);
714-
HideWindow(!FrmSettings.Settings.GetBoolSetting(Settings.keepDlgOpen));
715770
string fileName = Path.GetFileName(notepad.GetCurrentFilePath());
716771
searchComboBox.Items.Remove(fileName);
717772
searchComboBox.Items.Insert(0, fileName);
773+
string currentSearchString = searchComboBox.Text;
774+
// save also search string
775+
if (!String.IsNullOrEmpty(currentSearchString))
776+
{
777+
searchComboBox.Items.Remove(currentSearchString);
778+
searchComboBox.Items.Insert(0, currentSearchString);
779+
}
780+
781+
notepad.SwitchToFile(path, isTab);
782+
HideWindow(!FrmSettings.Settings.GetBoolSetting(Settings.keepDlgOpen));
718783
}
719784

720785
private void HideWindow(bool disabled = true)

NppNavigateTo/NppNavigateTo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static void OnNotification(ScNotification notification)
4747
NavigateTo.Plugin.Namespace.Main.NavigateToDlg();
4848
break;
4949
case (uint)NppMsg.NPPN_BEFORESHUTDOWN:
50+
NavigateTo.Plugin.Namespace.Main.frmNavigateTo.SaveSearchHistory();
5051
NavigateTo.Plugin.Namespace.Main.isShuttingDown = true;
5152
break;
5253
case (uint)NppMsg.NPPN_FILEOPENED:

NppNavigateTo/Settings.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ internal class Settings
3636
public static string secondsBetweenDirectoryScans = "secondsBetweenDirectoryScans";
3737
public static string maxResultsHighlightingEnabled = "maxResultsHighlightingEnabled";
3838
public static string searchDelayMs = "searchDelayMs";
39+
public static string searchHistory = "searchHistory";
40+
public static string searchHistoryLength = "searchHistoryLength";
3941

4042
static string iniFilePath;
4143
static string lpAppName = "NavigateTo";
@@ -155,6 +157,20 @@ public void LoadConfigValues()
155157
Win32.GetPrivateProfileInt(lpAppName, maxResultsHighlightingEnabled, 5000, iniFilePath));
156158
LoadIntSetting(searchDelayMs,
157159
Win32.GetPrivateProfileInt(lpAppName, searchDelayMs, 300, iniFilePath));
160+
try
161+
{
162+
LoadIntSetting(searchHistoryLength,
163+
Win32.GetPrivateProfileInt(lpAppName, searchHistoryLength, 5000, iniFilePath));
164+
int size = GetIntSetting(searchHistoryLength) + 1;
165+
StringBuilder shStringBuilder = new StringBuilder(size);
166+
Win32.GetPrivateProfileString(lpAppName, searchHistory, "", shStringBuilder, size, iniFilePath);
167+
LoadSetting(searchHistory, shStringBuilder.ToString());
168+
}
169+
catch(Exception ex)
170+
{
171+
SetIntSetting(searchHistoryLength, 5000);
172+
SetSetting(searchHistory, "");
173+
}
158174
// TODO: add customizable font size; changing font is tricky, so am not doing yet
159175
//LoadIntSetting(fontSize,
160176
// Win32.GetPrivateProfileInt(lpAppName, fontSize, 8, iniFilePath));

0 commit comments

Comments
 (0)