1
0
mirror of synced 2025-01-10 21:41:53 +01:00

feat: Add capability to paste as string literal in files (#1998)

### Problem description
As suggested in #1904, ImHex could benefit from having the capability of
pasting text directly into the editor.
This also complements the "Copy as... ASCII string" capability already
implemented in the software.

### Implementation description
A new shortcut called `Paste all as string` (to resemble the naming of
the `ASCII string` copy option) now allows to paste plaintext directly.
This shortcut is mapped to the `CTRL + ALT + SHIFT + V` keybind.

Internally, a new flag called `asPlainText` has been added to the
`pasteBytes` function, to minimise code changes and streamline code
readability.
The buffer is a simple type cast of the clipboard, without any
modification applied, which is then handed to the provider's `write`
function.

### Screenshots
The new shortcut is visible in the menu, just below the other paste
options:

![image](https://github.com/user-attachments/assets/41a2a512-5c39-4984-bab6-114c58266351)

---------

Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
This commit is contained in:
BioTheWolff 2024-12-14 16:52:14 +01:00 committed by GitHub
parent d9a7f40eb4
commit 13e079d1b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 6 deletions

View File

@ -818,7 +818,9 @@
"hex.builtin.view.hex_editor.menu.edit.jump_to.curr_pattern": "Current Pattern",
"hex.builtin.view.hex_editor.menu.edit.open_in_new_provider": "Open selection view...",
"hex.builtin.view.hex_editor.menu.edit.paste": "Paste",
"hex.builtin.view.hex_editor.menu.edit.paste_as": "Paste...",
"hex.builtin.view.hex_editor.menu.edit.paste_all": "Paste all",
"hex.builtin.view.hex_editor.menu.edit.paste_all_string": "Paste all as string",
"hex.builtin.view.hex_editor.menu.edit.remove": "Remove...",
"hex.builtin.view.hex_editor.menu.edit.resize": "Resize...",
"hex.builtin.view.hex_editor.menu.edit.select_all": "Select all",

View File

@ -702,7 +702,7 @@ namespace hex::plugin::builtin {
ImGui::SetClipboardText(result.c_str());
}
static void pasteBytes(const Region &selection, bool selectionCheck) {
static void pasteBytes(const Region &selection, bool selectionCheck, bool asPlainText) {
auto provider = ImHexApi::Provider::get();
if (provider == nullptr)
return;
@ -711,7 +711,14 @@ namespace hex::plugin::builtin {
if (clipboard == nullptr)
return;
auto buffer = parseHexString(clipboard);
std::vector<u8> buffer;
if (asPlainText) {
// Directly reinterpret clipboard as an array of bytes
std::string cp = clipboard;
buffer = std::vector<u8>(cp.begin(), cp.end());
}
else
buffer = parseHexString(clipboard);
if (!selectionCheck) {
if (selection.getStartAddress() + buffer.size() >= provider->getActualSize())
@ -1184,15 +1191,27 @@ namespace hex::plugin::builtin {
/* Paste */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.paste" }, ICON_VS_OUTPUT, 1450, CurrentView + CTRLCMD + Keys::V,
[] {
pasteBytes(ImHexApi::HexEditor::getSelection().value_or( ImHexApi::HexEditor::ProviderRegion(Region { 0, 0 }, ImHexApi::Provider::get())), true);
pasteBytes(ImHexApi::HexEditor::getSelection().value_or( ImHexApi::HexEditor::ProviderRegion(Region { 0, 0 }, ImHexApi::Provider::get())), true, false);
},
ImHexApi::HexEditor::isSelectionValid,
this);
/* Paste All */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.paste_all" }, ICON_VS_CLIPPY, 1500, CurrentView + CTRLCMD + SHIFT + Keys::V,
/* Paste... */
ContentRegistry::Interface::addMenuItemSubMenu({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.paste_as" }, ICON_VS_CLIPPY, 1490, []{}, ImHexApi::HexEditor::isSelectionValid);
/* Paste... > Paste all */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.paste_as", "hex.builtin.view.hex_editor.menu.edit.paste_all" }, ICON_VS_CLIPPY, 1500, CurrentView + CTRLCMD + SHIFT + Keys::V,
[] {
pasteBytes(ImHexApi::HexEditor::getSelection().value_or( ImHexApi::HexEditor::ProviderRegion(Region { 0, 0 }, ImHexApi::Provider::get())), false);
pasteBytes(ImHexApi::HexEditor::getSelection().value_or( ImHexApi::HexEditor::ProviderRegion(Region { 0, 0 }, ImHexApi::Provider::get())), false, false);
},
ImHexApi::HexEditor::isSelectionValid,
this);
/* Paste... > Paste all as string */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.paste_as", "hex.builtin.view.hex_editor.menu.edit.paste_all_string" }, ICON_VS_SYMBOL_TEXT, 1510,
Shortcut::None,
[] {
pasteBytes(ImHexApi::HexEditor::getSelection().value_or( ImHexApi::HexEditor::ProviderRegion(Region { 0, 0 }, ImHexApi::Provider::get())), false, true);
},
ImHexApi::HexEditor::isSelectionValid,
this);