Skip to content

Commit

Permalink
Add context menu entry for appending into existing file
Browse files Browse the repository at this point in the history
  • Loading branch information
eltos committed Feb 1, 2024
1 parent 7765397 commit 83146fe
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 48 deletions.
2 changes: 1 addition & 1 deletion PasteIntoFile/Dialog.Designer.cs

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

26 changes: 22 additions & 4 deletions PasteIntoFile/Properties/Resources.Designer.cs

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

16 changes: 10 additions & 6 deletions PasteIntoFile/Properties/Resources.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,6 @@ Diese Einstellungen werden ab dem nächsten Login aktiv.</value>
<data name="str_preview_svg" xml:space="preserve">
<value>Skalierbare Vektorgrafik Vorschau</value>
</data>
<data name="str_contextentry_replaceintofile" xml:space="preserve">
<value>Ersetzen mit Inhalt der Zwischenablage</value>
<comment>Context menu entry for replace-pasting
</comment>
</data>
<data name="str_portable" xml:space="preserve">
<value>Portable</value>
</data>
Expand All @@ -288,7 +283,7 @@ Die Umschalttaste gedrückt halten, um diese Einstellung temporär zu invertiere
Allows to keep application window always on top (in foreground of other windows), even if not focussed.
</comment>
</data>
<data name="str_append" xml:space="preserve">
<data name="str_append_to_file" xml:space="preserve">
<value>Zur Datei hinzufügen</value>
</data>
<data name="str_append_not_supported" xml:space="preserve">
Expand All @@ -298,4 +293,13 @@ Allows to keep application window always on top (in foreground of other windows)
<value>Falls die Datei bereits existiert wird der Inhalt zur Datei hinzugefügt.
(Nur für unterstützte Dateitypen)</value>
</data>
<data name="str_replace" xml:space="preserve">
<value>Ersetzen</value>
</data>
<data name="str_contextentry_paste_into_existing_file" xml:space="preserve">
<value>Einfügen in diese Datei</value>
</data>
<data name="str_append" xml:space="preserve">
<value>Anfügen</value>
</data>
</root>
8 changes: 3 additions & 5 deletions PasteIntoFile/Properties/Resources.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,10 @@
<data name="str_open" xml:space="preserve">
<value>Ouvrir</value>
</data>
<data name="str_contextentry_replaceintofile" xml:space="preserve">
<value>Remplacer par le contenu du presse-papiers</value>
<comment>Context menu entry for replace-pasting
</comment>
</data>
<data name="str_always_on_top" xml:space="preserve">
<value>Toujours au premier plan</value>
</data>
<data name="str_replace" xml:space="preserve">
<value>Remplacer</value>
</data>
</root>
8 changes: 3 additions & 5 deletions PasteIntoFile/Properties/Resources.it.resx
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,6 @@ Queste impostazioni avranno effetto sul prossimo accesso.</value>
<data name="str_preview_svg" xml:space="preserve">
<value>Anteprima grafica vettoriale scalabile</value>
</data>
<data name="str_contextentry_replaceintofile" xml:space="preserve">
<value>Sostituisci con il contenuto degli appunti</value>
<comment>Context menu entry for replace-pasting
</comment>
</data>
<data name="str_portable" xml:space="preserve">
<value>Portatile</value>
</data>
Expand All @@ -283,4 +278,7 @@ Tieni premuto Maiusc per invertire temporaneamente questa impostazione.</value>
<data name="str_wizard_autosave_may_open_new_explorer" xml:space="preserve">
<value>Il salvataggio automatico può aprire esplora file (ad esempio quando si incolla in una sottocartella)</value>
</data>
<data name="str_replace" xml:space="preserve">
<value>Sostituisci</value>
</data>
</root>
5 changes: 0 additions & 5 deletions PasteIntoFile/Properties/Resources.ko.resx
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,6 @@ Tooltip</comment>
<data name="str_preview_svg" xml:space="preserve">
<value>SVG 파일 미리 보기</value>
</data>
<data name="str_contextentry_replaceintofile" xml:space="preserve">
<value>클립보드 내용으로 교체</value>
<comment>Context menu entry for replace-pasting
</comment>
</data>
<data name="str_portable" xml:space="preserve">
<value>무설치</value>
</data>
Expand Down
16 changes: 10 additions & 6 deletions PasteIntoFile/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,6 @@ These setting will take effect on the next login.</value>
<data name="str_preview_svg" xml:space="preserve">
<value>Scalable Vector Graphic preview</value>
</data>
<data name="str_contextentry_replaceintofile" xml:space="preserve">
<value>Replace with clipboard content</value>
<comment>Context menu entry for replace-pasting
</comment>
</data>
<data name="str_portable" xml:space="preserve">
<value>Portable</value>
</data>
Expand All @@ -297,7 +292,7 @@ Hold SHIFT to temporarly invert this setting.</value>
Allows to keep application window always on top (in foreground of other windows), even if not focussed.
</comment>
</data>
<data name="str_append" xml:space="preserve">
<data name="str_append_to_file" xml:space="preserve">
<value>Append into file</value>
</data>
<data name="str_append_tooltip" xml:space="preserve">
Expand All @@ -307,4 +302,13 @@ Allows to keep application window always on top (in foreground of other windows)
<data name="str_append_not_supported" xml:space="preserve">
<value>Appending is not supported for extension "{0}".</value>
</data>
<data name="str_replace" xml:space="preserve">
<value>Replace</value>
</data>
<data name="str_contextentry_paste_into_existing_file" xml:space="preserve">
<value>Paste into this file</value>
</data>
<data name="str_append" xml:space="preserve">
<value>Append</value>
</data>
</root>
63 changes: 48 additions & 15 deletions PasteIntoFile/RegistryUtil.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
Expand All @@ -7,6 +8,26 @@
namespace PasteIntoFile {
public class RegistryUtil {

public class Command {
public delegate bool HasDialog();

public readonly string Title;
public readonly string Args;
public readonly HasDialog Dialog;

public Command(string title, string args, HasDialog dialog) {
Title = title;
Args = args;
Dialog = dialog;
}

public void Register(RegistryKey key) {
key.SetValue("MUIVerb", Title + (Dialog() ? "" : ""));
var cmdkey = key.CreateSubKey("command");
cmdkey.SetValue("", "\"" + Application.ExecutablePath + "\" " + Args);
}
}

// Please note that registry keys are also created by installer and removed upon uninstall
// Always keep the "Installer/PasteIntoFile.wxs" up to date with the keys used below!
public static readonly ContextMenuEntry ContextMenuPaste = new ContextMenuEntry(
Expand All @@ -16,8 +37,9 @@ public class RegistryUtil {
"*", "PasteIntoFile", Resources.str_contextentry_copyfromfile,
"copy \"%V\"", () => false);
public static readonly ContextMenuEntry ContextMenuReplace = new ContextMenuEntry(
"*", "PasteIntoFile_replace", Resources.str_contextentry_replaceintofile,
"paste --directory=\"%w\" --filename=\"%V\" --autosave=true --overwrite=true", () => false);
"*", "PasteIntoFile_replace", Resources.str_contextentry_paste_into_existing_file,
new Command(Resources.str_replace, "paste --overwrite=true --directory=\"%w\" --filename=\"%V\" --autosave=true", () => false),
new Command(Resources.str_append, "paste --append --directory=\"%w\" --filename=\"%V\" --autosave=true", () => false));

public static readonly ContextMenuEntry[] AllContextMenu = { ContextMenuPaste, ContextMenuCopy, ContextMenuReplace };

Expand All @@ -29,11 +51,9 @@ public class ContextMenuEntry {

private readonly string _type;
private readonly string _key;
private readonly Command _command;
private readonly string _title;
private readonly string _args;
private readonly HasDialog _hasDialog;

public delegate bool HasDialog();
private readonly Command[] _subCommands;

/// <summary>
///
Expand All @@ -43,23 +63,30 @@ public class ContextMenuEntry {
/// <param name="title">Title to show in context menu entry</param>
/// <param name="args">Arguments to pass to binary</param>
/// <param name="hasDialog"></param>
public ContextMenuEntry(string type, string key, string title, string args, HasDialog hasDialog) {
public ContextMenuEntry(string type, string key, string title, string args, Command.HasDialog hasDialog)
: this(type, key, new Command(title, args, hasDialog)) { }
public ContextMenuEntry(string type, string key, Command command) {
_type = type;
_key = key;
_command = command;
}
public ContextMenuEntry(string type, string key, string title, params Command[] entries) {
_type = type;
_key = key;
_title = title;
_args = args;
_hasDialog = hasDialog;
_subCommands = entries;
}

private static RegistryKey RootKey => Registry.CurrentUser.CreateSubKey(@"Software\Classes");

/// <summary>
/// Opens a number of class sub keys according to the type.
/// </summary>
/// <returns>List of registry keys</returns>
private IEnumerable<RegistryKey> OpenClassKeys() {
var classes = Registry.CurrentUser.CreateSubKey(@"Software\Classes");
if (_type == "Directory")
return new[] { classes.CreateSubKey(@"Directory\shell"), classes.CreateSubKey(@"Directory\Background\shell") };
return new[] { classes.CreateSubKey(_type + @"\shell") };
return new[] { RootKey.CreateSubKey(@"Directory\shell"), RootKey.CreateSubKey(@"Directory\Background\shell") };
return new[] { RootKey.CreateSubKey(_type + @"\shell") };
}

/// <summary>
Expand Down Expand Up @@ -89,10 +116,16 @@ public void UnRegister() {
public void Register() {
foreach (var classKey in OpenClassKeys()) {
var key = classKey.CreateSubKey(_key);
key.SetValue("", _title + (_hasDialog() ? "" : ""));
key.SetValue("Icon", "\"" + Application.ExecutablePath + "\",0");
key = key.CreateSubKey("command");
key.SetValue("", "\"" + Application.ExecutablePath + "\" " + _args);
if (_title != null) key.SetValue("MUIVerb", _title);
_command?.Register(key);
if (_subCommands?.Length > 0) {
key.SetValue("ExtendedSubCommandsKey", key.Name.Substring(RootKey.Name.Length + 1));
var subkey = key.CreateSubKey("shell");
for (var i = 0; i < _subCommands.Length; i++) {
_subCommands[i].Register(subkey.CreateSubKey("cmd" + i));
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion PasteIntoFile/Wizard.Designer.cs

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

0 comments on commit 83146fe

Please sign in to comment.