Permalink
Browse files

Add "Action on Selection" menu.

Rename "Web Template" to "Custom Action".
Add Google, Bing, and Wikipedia search URL.
  • Loading branch information...
zufuliu committed Oct 30, 2018
1 parent 82b5aad commit e87ff65252e87abfde4b3efd923f39750ac9eeb7
Showing with 95 additions and 71 deletions.
  1. BIN doc/Notepad2.ini
  2. +70 −0 src/Edit.c
  3. +1 −0 src/Edit.h
  4. +7 −62 src/Notepad2.c
  5. +11 −6 src/Notepad2.rc
  6. +6 −3 src/resource.h
BIN +212 Bytes (110%) doc/Notepad2.ini
Binary file not shown.
@@ -70,6 +70,7 @@ extern const NP2ENCODING mEncoding[];
extern LPMRULIST mruFind;
extern LPMRULIST mruReplace;
extern WCHAR szCurFile[MAX_PATH + 40];
//=============================================================================
//
@@ -5923,6 +5924,75 @@ BOOL EditSortDlg(HWND hwnd, int *piSortFlags) {
return iResult == IDOK;
}
void EditSelectionAction(HWND hwnd, int action) {
static const LPCWSTR kActionKeys[] = {
L"GoogleSearchUrl",
L"BingSearchUrl",
L"WikiSearchUrl",
L"CustomAction1",
L"CustomAction2",
};
WCHAR szCmdTemplate[256];
LPCWSTR actionKey = kActionKeys[action - CMD_ONLINE_SEARCH_GOOGLE];
const BOOL bCmdEnabled = IniGetString(INI_SECTION_NAME_FLAGS, actionKey, L"", szCmdTemplate, COUNTOF(szCmdTemplate));
if (!bCmdEnabled) {
return;
}
DWORD cchSelection = (int)SendMessage(hwnd, SCI_GETSELECTIONEND, 0, 0)
- (int)SendMessage(hwnd, SCI_GETSELECTIONSTART, 0, 0);
if (cchSelection > 0 && cchSelection < 512 && SendMessage(hwnd, SCI_GETSELTEXT, 0, 0) < 512) {
char mszSelection[512] = {0};
SendMessage(hwnd, SCI_GETSELTEXT, 0, (LPARAM)mszSelection);
mszSelection[cchSelection] = 0; // zero terminate
// Check lpszSelection and truncate bad WCHARs
char *lpsz = strpbrk(mszSelection, "\r\n\t");
if (lpsz) {
*lpsz = '\0';
}
if (StrNotEmptyA(mszSelection)) {
WCHAR wszSelection[512];
const UINT cpEdit = (UINT)SendMessage(hwnd, SCI_GETCODEPAGE, 0, 0);
MultiByteToWideChar(cpEdit, 0, mszSelection, -1, wszSelection, COUNTOF(wszSelection));
LPWSTR lpszCommand = NP2HeapAlloc(sizeof(WCHAR) * (512 + COUNTOF(szCmdTemplate) + MAX_PATH + 32));
const size_t cbCommand = NP2HeapSize(lpszCommand);
wsprintf(lpszCommand, szCmdTemplate, wszSelection);
ExpandEnvironmentStringsEx(lpszCommand, (DWORD)(cbCommand / sizeof(WCHAR)));
LPWSTR lpszArgs = NP2HeapAlloc(cbCommand);
ExtractFirstArgument(lpszCommand, lpszCommand, lpszArgs);
WCHAR wchDirectory[MAX_PATH] = L"";
if (StrNotEmpty(szCurFile)) {
lstrcpy(wchDirectory, szCurFile);
PathRemoveFileSpec(wchDirectory);
}
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = /*SEE_MASK_NOZONECHECKS*/0x00800000;
sei.hwnd = NULL;
sei.lpVerb = NULL;
sei.lpFile = lpszCommand;
sei.lpParameters = lpszArgs;
sei.lpDirectory = wchDirectory;
sei.nShow = SW_SHOWNORMAL;
ShellExecuteEx(&sei);
NP2HeapFree(lpszCommand);
NP2HeapFree(lpszArgs);
}
}
}
//=============================================================================
//
// FileVars_Init()
@@ -137,6 +137,7 @@ void EditInsertUnicodeControlCharacter(HWND hwnd, int menu);
void EditShowUnicodeControlCharacter(HWND hwnd, BOOL bShow);
BOOL EditSortDlg(HWND hwnd, int *piSortFlags);
BOOL EditAlignDlg(HWND hwnd, int *piAlignMode);
void EditSelectionAction(HWND hwnd, int action);
// in Print.cpp
BOOL EditPrint(HWND hwnd, LPCWSTR pszDocTitle, LPCWSTR pszPageFormat);
@@ -4573,68 +4573,13 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) {
}
break;
case CMD_WEBACTION1:
case CMD_WEBACTION2: {
WCHAR szCmdTemplate[256];
LPWSTR lpszTemplateName = (LOWORD(wParam) == CMD_WEBACTION1) ? L"WebTemplate1" : L"WebTemplate2";
const BOOL bCmdEnabled = IniGetString(INI_SECTION_NAME_FLAGS, lpszTemplateName, L"", szCmdTemplate, COUNTOF(szCmdTemplate));
if (bCmdEnabled) {
DWORD cchSelection = (int)SendMessage(hwndEdit, SCI_GETSELECTIONEND, 0, 0)
- (int)SendMessage(hwndEdit, SCI_GETSELECTIONSTART, 0, 0);
if (cchSelection > 0 && cchSelection < 512 && SendMessage(hwndEdit, SCI_GETSELTEXT, 0, 0) < 512) {
char mszSelection[512] = {0};
SendMessage(hwndEdit, SCI_GETSELTEXT, 0, (LPARAM)mszSelection);
mszSelection[cchSelection] = 0; // zero terminate
// Check lpszSelection and truncate bad WCHARs
char *lpsz = strpbrk(mszSelection, "\r\n\t");
if (lpsz) {
*lpsz = '\0';
}
if (StrNotEmptyA(mszSelection)) {
WCHAR wszSelection[512];
const UINT cpEdit = (UINT)SendMessage(hwndEdit, SCI_GETCODEPAGE, 0, 0);
MultiByteToWideChar(cpEdit, 0, mszSelection, -1, wszSelection, COUNTOF(wszSelection));
LPWSTR lpszCommand = NP2HeapAlloc(sizeof(WCHAR) * (512 + COUNTOF(szCmdTemplate) + MAX_PATH + 32));
const size_t cbCommand = NP2HeapSize(lpszCommand);
wsprintf(lpszCommand, szCmdTemplate, wszSelection);
ExpandEnvironmentStringsEx(lpszCommand, (DWORD)(cbCommand / sizeof(WCHAR)));
LPWSTR lpszArgs = NP2HeapAlloc(cbCommand);
ExtractFirstArgument(lpszCommand, lpszCommand, lpszArgs);
WCHAR wchDirectory[MAX_PATH] = L"";
if (StrNotEmpty(szCurFile)) {
lstrcpy(wchDirectory, szCurFile);
PathRemoveFileSpec(wchDirectory);
}
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = /*SEE_MASK_NOZONECHECKS*/0x00800000;
sei.hwnd = NULL;
sei.lpVerb = NULL;
sei.lpFile = lpszCommand;
sei.lpParameters = lpszArgs;
sei.lpDirectory = wchDirectory;
sei.nShow = SW_SHOWNORMAL;
ShellExecuteEx(&sei);
NP2HeapFree(lpszCommand);
NP2HeapFree(lpszArgs);
}
}
}
}
break;
case CMD_ONLINE_SEARCH_GOOGLE:
case CMD_ONLINE_SEARCH_BING:
case CMD_ONLINE_SEARCH_WIKI:
case CMD_CUSTOM_ACTION1:
case CMD_CUSTOM_ACTION2:
EditSelectionAction(hwndEdit, LOWORD(wParam));
break;
case CMD_FINDNEXTSEL:
case CMD_FINDPREVSEL:
@@ -100,9 +100,6 @@ BEGIN
MENUITEM "Execute &Document", IDM_FILE_LAUNCH
MENUITEM "&Open With...", IDM_FILE_OPENWITH
MENUITEM "&Command...\tCtrl+R", IDM_FILE_RUN
MENUITEM SEPARATOR
MENUITEM "Web Template &1\tCtrl+Shift+1", CMD_WEBACTION1
MENUITEM "Web Template &2\tCtrl+Shift+2", CMD_WEBACTION2
END
MENUITEM SEPARATOR
POPUP "&Encoding"
@@ -170,7 +167,7 @@ BEGIN
MENUITEM "&Window Position\tCtrl+Shift+K", CMD_COPYWINPOS
END
MENUITEM SEPARATOR
POPUP "&Block"
POPUP "&Selection"
BEGIN
MENUITEM "&Duplicate\tAlt+D", IDM_EDIT_SELECTIONDUPLICATE
MENUITEM SEPARATOR
@@ -311,6 +308,14 @@ BEGIN
MENUITEM SEPARATOR
MENUITEM "Update Timestamps\tShift+F5", CMD_TIMESTAMPS
END
POPUP "&Action on Selection"
BEGIN
MENUITEM "Search with Google", CMD_ONLINE_SEARCH_GOOGLE
MENUITEM "Search with Bing", CMD_ONLINE_SEARCH_BING
MENUITEM "Search on Wikipedia", CMD_ONLINE_SEARCH_WIKI
MENUITEM "Custom Action &1\tCtrl+Shift+1", CMD_CUSTOM_ACTION1
MENUITEM "Custom Action &2\tCtrl+Shift+2", CMD_CUSTOM_ACTION2
END
MENUITEM SEPARATOR
POPUP "&Find and Replace"
BEGIN
@@ -657,10 +662,10 @@ BEGIN
"0", IDM_VIEW_WORDWRAPSYMBOLS, VIRTKEY, SHIFT, CONTROL, NOINVERT
"1", IDM_VIEW_FOLD_LEVEL1, VIRTKEY, ALT, NOINVERT
"1", CMD_STRINGIFYS, VIRTKEY, CONTROL, NOINVERT
"1", CMD_WEBACTION1, VIRTKEY, SHIFT, CONTROL, NOINVERT
"1", CMD_CUSTOM_ACTION1, VIRTKEY, SHIFT, CONTROL, NOINVERT
"2", CMD_STRINGIFYD, VIRTKEY, CONTROL, NOINVERT
"2", IDM_VIEW_FOLD_LEVEL2, VIRTKEY, ALT, NOINVERT
"2", CMD_WEBACTION2, VIRTKEY, SHIFT, CONTROL, NOINVERT
"2", CMD_CUSTOM_ACTION2, VIRTKEY, SHIFT, CONTROL, NOINVERT
"3", CMD_STRINGIFYT2, VIRTKEY, CONTROL, NOINVERT
"3", IDM_VIEW_FOLD_LEVEL3, VIRTKEY, ALT, NOINVERT
"4", CMD_EMBRACKETR, VIRTKEY, CONTROL, NOINVERT
@@ -168,9 +168,12 @@
#define CMD_RELOADOEM 20008 // Ctrl+Shift+O
#define CMD_RELOADASCIIASUTF8 20009 // Shift+F8
#define CMD_RELOADNOFILEVARS 20010 // Alt+F8
#define CMD_TIMESTAMPS 20014 // Shift+F5
#define CMD_WEBACTION1 20015 // Ctrl+Shift+1
#define CMD_WEBACTION2 20016 // Ctrl+Shift+2
#define CMD_TIMESTAMPS 20011 // Shift+F5
#define CMD_ONLINE_SEARCH_GOOGLE 20012
#define CMD_ONLINE_SEARCH_BING 20013
#define CMD_ONLINE_SEARCH_WIKI 20014
#define CMD_CUSTOM_ACTION1 20015 // Ctrl+Shift+1
#define CMD_CUSTOM_ACTION2 20016 // Ctrl+Shift+2
#define CMD_FINDNEXTSEL 20017 // Ctrl+F3
#define CMD_FINDPREVSEL 20018 // Ctrl+Shift+F3
#define CMD_INCLINELIMIT 20019 // Alt++

0 comments on commit e87ff65

Please sign in to comment.