Skip to content

Commit

Permalink
Add ViClipboardMode Option
Browse files Browse the repository at this point in the history
This option allows the user to choose if they want the ViRegister to use
the system clipboard or the default local clipboard. By setting
ViClipboardMode to SystemClipboard, the user can easily copy and paste
text in the PowerShell prompt using Vi shortcuts in Command mode.
  • Loading branch information
WestRyanK authored and Ryan West committed Aug 2, 2023
1 parent e5537d8 commit e4bd694
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
19 changes: 19 additions & 0 deletions PSReadLine/Cmdlets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public enum ViMode
Command
}

public enum ViClipboardMode
{
ViRegister,
SystemClipboard
}

public enum HistorySaveStyle
{
SaveIncrementally,
Expand Down Expand Up @@ -342,6 +348,11 @@ public object ContinuationPromptColor
/// </summary>
public ScriptBlock ViModeChangeHandler { get; set; }

/// <summary>
/// Which source should be used when copying and pasting in vi edit mode?
/// </summary>
public ViClipboardMode ViClipboardMode { get; set; }

/// <summary>
/// The path to the saved history.
/// </summary>
Expand Down Expand Up @@ -789,6 +800,14 @@ public ViModeStyle ViModeIndicator
[Parameter]
public ScriptBlock ViModeChangeHandler { get; set; }

[Parameter]
public ViClipboardMode ViClipboardMode
{
get => _viClipboardMode.GetValueOrDefault();
set => _viClipboardMode = value;
}
internal ViClipboardMode? _viClipboardMode;

[Parameter]
public PredictionSource PredictionSource
{
Expand Down
4 changes: 4 additions & 0 deletions PSReadLine/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ private void SetOptionsInternal(SetPSReadLineOption options)
}
Options.ViModeChangeHandler = options.ViModeChangeHandler;
}
if (options._viClipboardMode.HasValue)
{
Options.ViClipboardMode = options.ViClipboardMode;
}
if (options.HistorySavePath != null)
{
Options.HistorySavePath = options.HistorySavePath;
Expand Down
3 changes: 3 additions & 0 deletions PSReadLine/PSReadLine.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ $d = [Microsoft.PowerShell.KeyHandler]::GetGroupingDescription($_.Group)
<ItemSelectionCondition><ScriptBlock>$null -ne $_.ViModeChangeHandler</ScriptBlock></ItemSelectionCondition>
<PropertyName>ViModeChangeHandler</PropertyName>
</ListItem>
<ListItem>
<PropertyName>ViClipboardMode</PropertyName>
</ListItem>
<ListItem>
<PropertyName>WordDelimiters</PropertyName>
</ListItem>
Expand Down
49 changes: 38 additions & 11 deletions PSReadLine/ViRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,32 @@ public partial class PSConsoleReadLine
internal sealed class ViRegister
{
private readonly PSConsoleReadLine _singleton;
private string _text;
private string _localText;
private string Text
{
get
{
if (_singleton.Options.ViClipboardMode == ViClipboardMode.ViRegister)
{
return _localText;
}
else
{
return Internal.Clipboard.GetText();
}
}
set
{
if (_singleton.Options.ViClipboardMode == ViClipboardMode.ViRegister)
{
_localText = value;
}
else
{
Internal.Clipboard.SetText(value);
}
}
}

/// <summary>
/// Initialize a new instance of the <see cref="ViRegister" /> class.
Expand All @@ -29,7 +54,7 @@ public ViRegister(PSConsoleReadLine singleton)
/// Returns whether this register is empty.
/// </summary>
public bool IsEmpty
=> String.IsNullOrEmpty(_text);
=> String.IsNullOrEmpty(Text);

/// <summary>
/// Returns whether this register contains
Expand All @@ -41,7 +66,7 @@ public bool IsEmpty
/// Gets the raw text contained in the register
/// </summary>
public string RawText
=> _text;
=> Text;

/// <summary>
/// Records the entire buffer in the register.
Expand All @@ -67,7 +92,7 @@ public void Record(StringBuilder buffer, int offset, int count)
System.Diagnostics.Debug.Assert(offset + count <= buffer.Length);

HasLinewiseText = false;
_text = buffer.ToString(offset, count);
Text = buffer.ToString(offset, count);
}

/// <summary>
Expand All @@ -77,7 +102,7 @@ public void Record(StringBuilder buffer, int offset, int count)
public void LinewiseRecord(string text)
{
HasLinewiseText = true;
_text = text;
Text = text;
}

public int PasteAfter(StringBuilder buffer, int position)
Expand All @@ -89,7 +114,7 @@ public int PasteAfter(StringBuilder buffer, int position)

if (HasLinewiseText)
{
var text = _text;
var text = Text;

if (text[0] != '\n')
{
Expand Down Expand Up @@ -127,8 +152,9 @@ public int PasteAfter(StringBuilder buffer, int position)
position += 1;
}

InsertAt(buffer, _text, position, position);
position += _text.Length - 1;
var text = Text;
InsertAt(buffer, text, position, position);
position += text.Length - 1;

return position;
}
Expand All @@ -145,7 +171,7 @@ public int PasteBefore(StringBuilder buffer, int position)

position = Math.Max(0, Math.Min(position, buffer.Length - 1));

var text = _text;
var text = Text;

if (text[0] == '\n')
{
Expand Down Expand Up @@ -184,8 +210,9 @@ public int PasteBefore(StringBuilder buffer, int position)
}
else
{
InsertAt(buffer, _text, position, position);
return position + _text.Length - 1;
var text = Text;
InsertAt(buffer, text, position, position);
return position + text.Length - 1;
}
}

Expand Down

0 comments on commit e4bd694

Please sign in to comment.