Skip to content

Commit

Permalink
Modif for NPPM_SETUNTITLEDNAME
Browse files Browse the repository at this point in the history
  • Loading branch information
donho committed Sep 25, 2024
1 parent 7032885 commit 8f4d807
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion PowerEditor/src/Notepad_plus.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ friend class FileManager;
bool fileSaveAs(BufferID id = BUFFER_INVALID, bool isSaveCopy = false);
bool fileDelete(BufferID id = BUFFER_INVALID);
bool fileRename(BufferID id = BUFFER_INVALID);
bool fileRenameUntitled(BufferID id, const wchar_t* tabNewName);
bool fileRenameUntitledPluginAPI(BufferID id, const wchar_t* tabNewName);

bool switchToFile(BufferID buffer); //find buffer in active view then in other view.
//@}
Expand Down
2 changes: 1 addition & 1 deletion PowerEditor/src/NppBigSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3150,7 +3150,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
case NPPM_SETUNTITLEDNAME:
{
if (!wParam || !lParam) return FALSE;
return fileRenameUntitled(reinterpret_cast<BufferID>(wParam), reinterpret_cast<const wchar_t*>(lParam));
return fileRenameUntitledPluginAPI(reinterpret_cast<BufferID>(wParam), reinterpret_cast<const wchar_t*>(lParam));
}

case NPPM_GETBOOKMARKID:
Expand Down
68 changes: 35 additions & 33 deletions PowerEditor/src/NppIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1978,16 +1978,16 @@ bool Notepad_plus::fileRename(BufferID id)
// We are just going to rename the tab nothing else
// So just rename the tab and rename the backup file too if applicable

std::wstring staticName = _nativeLangSpeaker.getLocalizedStrFromID("tabrename-newname", L"New name");
wstring staticName = _nativeLangSpeaker.getLocalizedStrFromID("tabrename-newname", L"New name");

StringDlg strDlg;
std::wstring title = _nativeLangSpeaker.getLocalizedStrFromID("tabrename-title", L"Rename Current Tab");
wstring title = _nativeLangSpeaker.getLocalizedStrFromID("tabrename-title", L"Rename Current Tab");
strDlg.init(_pPublicInterface->getHinst(), _pPublicInterface->getHSelf(), title.c_str(), staticName.c_str(), buf->getFileName(), langNameLenMax - 1, filenameReservedChars.c_str(), true);

wchar_t *tabNewName = reinterpret_cast<wchar_t *>(strDlg.doDialog());
if (tabNewName)
{
std::wstring tabNewNameStr = tabNewName;
wstring tabNewNameStr = tabNewName;
trim(tabNewNameStr); // No leading and tailing space allowed

BufferID sameNamedBufferId = _pDocTab->findBufferByName(tabNewNameStr.c_str());
Expand Down Expand Up @@ -2024,11 +2024,11 @@ bool Notepad_plus::fileRename(BufferID id)
bool isSnapshotMode = NppParameters::getInstance().getNppGUI().isSnapshotMode();
if (isSnapshotMode)
{
std::wstring oldBackUpFileName = buf->getBackupFileName();
wstring oldBackUpFileName = buf->getBackupFileName();
if (oldBackUpFileName.empty())
return success;

std::wstring newBackUpFileName = oldBackUpFileName;
wstring newBackUpFileName = oldBackUpFileName;

size_t index = newBackUpFileName.find_last_of(oldFileNamePath) - oldFileNamePath.length() + 1;
newBackUpFileName.replace(index, oldFileNamePath.length(), tabNewNameStr);
Expand All @@ -2047,17 +2047,17 @@ bool Notepad_plus::fileRename(BufferID id)
return success;
}

bool Notepad_plus::fileRenameUntitled(BufferID id, const wchar_t* tabNewName)
bool Notepad_plus::fileRenameUntitledPluginAPI(BufferID id, const wchar_t* tabNewName)
{
BufferID bufferID = id;
if (id == BUFFER_INVALID)
{
bufferID = _pEditView->getCurrentBufferID();
}

Buffer* buf = MainFileManager.getBufferByID(bufferID);

bool isFileExisting = doesFileExist(buf->getFullPathName());
if (isFileExisting) return false;
if (!buf->isUntitled()) return false;

// We are just going to rename the tab nothing else
// So just rename the tab and rename the backup file too if applicable
Expand All @@ -2080,37 +2080,39 @@ bool Notepad_plus::fileRenameUntitled(BufferID id, const wchar_t* tabNewName)
sameNamedBufferId = _pNonDocTab->findBufferByName(tabNewNameStr.c_str());
}

if (sameNamedBufferId == BUFFER_INVALID)
{
SCNotification scnN{};
scnN.nmhdr.code = NPPN_FILEBEFORERENAME;
scnN.nmhdr.hwndFrom = _pPublicInterface->getHSelf();
scnN.nmhdr.idFrom = (uptr_t)bufferID;
_pluginsManager.notify(&scnN);
if (sameNamedBufferId != BUFFER_INVALID) return false;

buf->setFileName(tabNewNameStr.c_str());

scnN.nmhdr.code = NPPN_FILERENAMED;
_pluginsManager.notify(&scnN);
SCNotification scnN{};
scnN.nmhdr.code = NPPN_FILEBEFORERENAME;
scnN.nmhdr.hwndFrom = _pPublicInterface->getHSelf();
scnN.nmhdr.idFrom = (uptr_t)bufferID;
_pluginsManager.notify(&scnN);

bool isSnapshotMode = NppParameters::getInstance().getNppGUI().isSnapshotMode();
if (isSnapshotMode)
{
std::wstring oldBackUpFile = buf->getBackupFileName();
buf->setFileName(tabNewNameStr.c_str());

// Change the backup file name and let MainFileManager decide the new filename
buf->setBackupFileName(L"");
scnN.nmhdr.code = NPPN_FILERENAMED;
_pluginsManager.notify(&scnN);

// Create new backup
buf->setModifiedStatus(true);
bool bRes = MainFileManager.backupCurrentBuffer();
bool isSnapshotMode = NppParameters::getInstance().getNppGUI().isSnapshotMode();
if (isSnapshotMode)
{
wstring oldName = buf->getFullPathName();
wstring oldBackUpFileName = buf->getBackupFileName();
if (oldBackUpFileName.empty())
return false;

// Delete old backup
if (bRes)
{
::DeleteFile(oldBackUpFile.c_str());
}
}
wstring newBackUpFileName = oldBackUpFileName;

size_t index = newBackUpFileName.find_last_of(oldName) - oldName.length() + 1;
newBackUpFileName.replace(index, oldName.length(), tabNewNameStr);

if (doesFileExist(newBackUpFileName.c_str()))
::ReplaceFile(newBackUpFileName.c_str(), oldBackUpFileName.c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS | REPLACEFILE_IGNORE_ACL_ERRORS, 0, 0);
else
::MoveFileEx(oldBackUpFileName.c_str(), newBackUpFileName.c_str(), MOVEFILE_REPLACE_EXISTING);

buf->setBackupFileName(newBackUpFileName);
}

return true;
Expand Down

0 comments on commit 8f4d807

Please sign in to comment.