Skip to content

Commit

Permalink
f12 searches selected text
Browse files Browse the repository at this point in the history
  • Loading branch information
vivainio committed Jan 26, 2024
1 parent 4544072 commit 10ab70d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions RoughGrep/FullPreviewForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion RoughGrep/FullPreviewForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ namespace RoughGrep
{
public partial class FullPreviewForm : Form
{
public FullPreviewForm()
public FullPreviewForm(MainFormUi ui)
{
InitializeComponent();
_mainUi = ui;
this.KeyPreview = true;

scintilla = SciUtil.CreateScintilla();
Controls.Add(scintilla);
scintilla.Dock = DockStyle.Fill;
Expand All @@ -31,5 +34,17 @@ public FullPreviewForm()
}

public Scintilla scintilla;
private readonly MainFormUi _mainUi;

private void FullPreviewForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F12 && _mainUi != null)
{
var selected = SciUtil.GetSelectionOrWordOnPosition(scintilla);
_mainUi.searchTextBox.Text = selected;
_mainUi.form.BringToFront();
Logic.StartSearch(_mainUi);
}
}
}
}
13 changes: 11 additions & 2 deletions RoughGrep/MainFormBehind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ private FullPreviewForm Previewer()
{
if (previewForm == null)
{
previewForm = new FullPreviewForm();
previewForm = new FullPreviewForm(Ui);
FormsUtil.FindVisiblePlaceForNewForm(Ui.form, previewForm);
}
return previewForm;
}

private readonly Lazy<FullPreviewForm> Notepad = new Lazy<FullPreviewForm>(() =>
{
var np = new FullPreviewForm();
var np = new FullPreviewForm(null);
np.Text = "RoughGrep Notes";
return np;
});
Expand Down Expand Up @@ -248,6 +248,15 @@ internal void HandleKeyDownOnResults(KeyEventArgs e, int line)
Ui.searchControl.searchTextBox.Focus();
break;
}
case Keys.F12:
{
var selected = SciUtil.GetSelectionOrWordOnPosition(Ui.resultBox);
Ui.searchTextBox.Text = selected;
Ui.form.BringToFront();
Logic.StartSearch(Ui);
break;
}

default:

{
Expand Down
1 change: 0 additions & 1 deletion RoughGrep/MainFormUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class MainFormUi
public MainForm form;
public SearchControl searchControl;
public Button btnAbort;
public Scintilla sci;
public TableLayoutPanel tableLayout;
public ToolStripStatusLabel statusLabel;
public ToolStripComboBox rgArgsComboBox;
Expand Down
12 changes: 12 additions & 0 deletions RoughGrep/SciUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public static void TouchAfterTextLoad(Scintilla scintilla)
scintilla.ScrollWidthTracking = true;
}

public static string GetSelectionOrWordOnPosition(Scintilla scintilla)
{
var selection = scintilla.SelectedText;
if (!string.IsNullOrEmpty(selection))
return selection;
var pos = scintilla.CurrentPosition;
int start = scintilla.WordStartPosition(pos, true);
int end = scintilla.WordEndPosition(pos, true);
var selected = scintilla.GetTextRange(start, end - start);
return selected;

}
public static void SetAllText(Scintilla scintilla, string text)
{
scintilla.ReadOnly = false;
Expand Down

0 comments on commit 10ab70d

Please sign in to comment.