Skip to content

Commit

Permalink
established Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
daddel80 committed Sep 21, 2023
1 parent 2c09cd0 commit 1b345ec
Show file tree
Hide file tree
Showing 71 changed files with 76,080 additions and 11 deletions.
14 changes: 12 additions & 2 deletions src/DockingFeature/StaticDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// This file is part of Notepad++ project
// Copyright (C)2022 Don HO <don.h@free.fr>
// This
// file
// is
// part
// of
// Notepad++
// project
// Copyright
// (C)2022
// Don
// HO
// <don.h@free.fr>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down
14 changes: 12 additions & 2 deletions src/DockingFeature/StaticDialog.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// This file is part of Notepad++ project
// Copyright (C)2022 Don HO <don.h@free.fr>
// This
// file
// is
// part
// of
// Notepad++
// project
// Copyright
// (C)2022
// Don
// HO
// <don.h@free.fr>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down
1 change: 1 addition & 0 deletions src/DockingFeature/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#define IDC_WHOLE_WORD_CHECKBOX 5200
#define IDC_MATCH_CASE_CHECKBOX 5201
#define IDC_WRAP_AROUND_CHECKBOX 5202
#define IDC_USE_VARIABLES_CHECKBOX 5203

#define IDC_SEARCH_MODE_GROUP 5300
#define IDC_NORMAL_RADIO 5301
Expand Down
74 changes: 74 additions & 0 deletions src/MultiReplacePanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <vector>
#include <windows.h>
#include <filesystem>
#include <lua.hpp>


#ifdef UNICODE
Expand Down Expand Up @@ -101,6 +102,7 @@ void MultiReplace::positionAndResizeControls(int windowWidth, int windowHeight)
ctrlMap[IDC_WHOLE_WORD_CHECKBOX] = { 20, 114, 163, 25, WC_BUTTON, L"Match whole word only", BS_AUTOCHECKBOX | WS_TABSTOP, NULL };
ctrlMap[IDC_MATCH_CASE_CHECKBOX] = { 20, 143, 100, 25, WC_BUTTON, L"Match case", BS_AUTOCHECKBOX | WS_TABSTOP, NULL };
ctrlMap[IDC_WRAP_AROUND_CHECKBOX] = { 20, 172, 155, 25, WC_BUTTON, L"Wrap around", BS_AUTOCHECKBOX | WS_TABSTOP, NULL };
ctrlMap[IDC_USE_VARIABLES_CHECKBOX] = { 20, 201, 120, 25, WC_BUTTON, L"Use Variables", BS_AUTOCHECKBOX | WS_TABSTOP, NULL };

ctrlMap[IDC_SEARCH_MODE_GROUP] = { 195, 90, 200, 116, WC_BUTTON, L"Search Mode", BS_GROUPBOX, NULL };
ctrlMap[IDC_NORMAL_RADIO] = { 205, 114, 100, 25, WC_BUTTON, L"Normal", BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP, NULL };
Expand Down Expand Up @@ -1507,6 +1509,15 @@ int MultiReplace::replaceString(const std::wstring& findText, const std::wstring
SearchResult searchResult = performSearchForward(findTextUtf8, searchFlags, false, 0);
while (searchResult.pos >= 0)
{
bool useVariablesEnabled = (IsDlgButtonChecked(_hSelf, IDC_USE_VARIABLES_CHECKBOX) == BST_CHECKED);
if (useVariablesEnabled) {
int CNT = replaceCount;
int APOS = static_cast<int>(searchResult.pos);
int LINE = (int)::SendMessage(_hScintilla, SCI_LINEFROMPOSITION, (WPARAM)searchResult.pos, 0);
int LPOS = (int)searchResult.pos - (int)::SendMessage(_hScintilla, SCI_POSITIONFROMLINE, (WPARAM)LINE, 0);
replaceTextUtf8 = resolveLuaSyntax(replaceTextUtf8, CNT, LINE, LPOS, APOS);
}

Sci_Position newPos;
if (regex) {
newPos = performRegexReplace(replaceTextUtf8, searchResult.pos, searchResult.length);
Expand Down Expand Up @@ -1598,6 +1609,69 @@ SelectionInfo MultiReplace::getSelectionInfo() {
return SelectionInfo{ selectedText, selectionStart, selectionLength };
}

std::string MultiReplace::resolveLuaSyntax(const std::string& inputString, int CNT, int LINE, int LPOS, int APOS)
{
std::wstring error_message;

lua_State* L = luaL_newstate(); // Create a new Lua environment
luaL_openlibs(L); // Load standard libraries

// Set variables
lua_pushnumber(L, CNT);
lua_setglobal(L, "CNT");
lua_pushnumber(L, LINE);
lua_setglobal(L, "LINE");
lua_pushnumber(L, LPOS);
lua_setglobal(L, "LPOS");
lua_pushnumber(L, APOS);
lua_setglobal(L, "APOS");

// Initialize 'result' to an empty string
lua_pushstring(L, "");
lua_setglobal(L, "result");

// Declare if statement function
luaL_dostring(L,
"function if_statement(cond, trueVal, falseVal)\n"
" if loadstring('return ' .. cond)() then\n" // Evaluate the condition
" result = trueVal\n"
" else\n"
" result = falseVal\n"
" end\n"
"end\n");

// Execute your expression
std::string luaCode = inputString; // Replace inputString with your actual Lua expression
if (luaL_dostring(L, luaCode.c_str()) != LUA_OK) {
const char* cstr = lua_tostring(L, -1);

int len = MultiByteToWideChar(CP_UTF8, 0, cstr, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len];
MultiByteToWideChar(CP_UTF8, 0, cstr, -1, wstr, len);

error_message += wstr; // Append to error_message
delete[] wstr;

showStatusMessage(error_message.c_str(), RGB(255, 0, 0));
lua_close(L);
return "";
}


// Retrieve the result
lua_getglobal(L, "result");
std::string result = lua_tostring(L, -1); // Assuming 'result' could be a string
lua_pop(L, 1); // Pop 'result'

MessageBoxA(NULL, ("After Retrieving Result: " + result).c_str(), "Debug", MB_OK);

// Close Lua environment
lua_close(L);

// Return the result
return result;
}

#pragma endregion


Expand Down
4 changes: 3 additions & 1 deletion src/MultiReplacePanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#ifndef MULTI_REPLACE_H
#define MULTI_REPLACE_H

#include "DockingFeature\DockingDlgInterface.h"
#include "DockingFeature\StaticDialog.h"
#include "DockingFeature\resource.h"
#include "PluginInterface.h"

Expand All @@ -29,6 +29,7 @@
#include <unordered_map>
#include <set>
#include <commctrl.h>
#include <lua.hpp>

extern NppData nppData;

Expand Down Expand Up @@ -300,6 +301,7 @@ class MultiReplace : public StaticDialog
Sci_Position performRegexReplace(const std::string& replaceTextUtf8, Sci_Position pos, Sci_Position length);
SelectionInfo getSelectionInfo();
std::string utf8ToCodepage(const std::string& utf8Str, int codepage);
std::string resolveLuaSyntax(const std::string& inputString, int CNT, int LINE, int LPOS, int APOS);

//Find
void handleFindNextButton();
Expand Down
Loading

0 comments on commit 1b345ec

Please sign in to comment.