2019-10-27 17:34:58 +00:00
|
|
|
function fish_clipboard_paste
|
2019-11-04 19:47:58 +00:00
|
|
|
set -l data
|
|
|
|
if type -q pbpaste
|
|
|
|
set data (pbpaste 2>/dev/null)
|
|
|
|
else if set -q WAYLAND_DISPLAY; and type -q wl-paste
|
|
|
|
set data (wl-paste 2>/dev/null)
|
|
|
|
else if type -q xsel
|
|
|
|
set data (xsel --clipboard 2>/dev/null)
|
|
|
|
else if type -q xclip
|
|
|
|
set data (xclip -selection clipboard -o 2>/dev/null)
|
2020-11-06 12:11:56 +00:00
|
|
|
else if type -q powershell.exe
|
|
|
|
set data (powershell.exe Get-Clipboard | string trim -r -c \r)
|
2019-11-04 19:47:58 +00:00
|
|
|
end
|
2019-10-27 17:31:35 +00:00
|
|
|
|
|
|
|
# Issue 6254: Handle zero-length clipboard content
|
|
|
|
if not string match -qr . -- "$data"
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2017-04-23 11:40:40 +00:00
|
|
|
# Also split on \r to turn it into a newline,
|
|
|
|
# otherwise the output looks really confusing.
|
|
|
|
set data (string split \r -- $data)
|
|
|
|
|
2017-03-09 15:52:38 +00:00
|
|
|
# If the current token has an unmatched single-quote,
|
|
|
|
# escape all single-quotes (and backslashes) in the paste,
|
|
|
|
# in order to turn it into a single literal token.
|
|
|
|
#
|
|
|
|
# This eases pasting non-code (e.g. markdown or git commitishes).
|
2021-03-06 07:40:07 +00:00
|
|
|
set -l quote_state (__fish_tokenizer_state -- (commandline -ct | string collect))
|
2021-02-08 07:12:43 +00:00
|
|
|
if contains -- $quote_state single single-escaped
|
2019-08-14 05:28:17 +00:00
|
|
|
if status test-feature regex-easyesc
|
2019-01-20 10:12:27 +00:00
|
|
|
set data (string replace -ra "(['\\\])" '\\\\$1' -- $data)
|
|
|
|
else
|
|
|
|
set data (string replace -ra "(['\\\])" '\\\\\\\$1' -- $data)
|
|
|
|
end
|
2021-02-08 07:12:43 +00:00
|
|
|
else if not contains -- $quote_state double double-escaped
|
|
|
|
and set -q data[2]
|
|
|
|
# Leading whitespace in subsequent lines is unneded, since fish
|
|
|
|
# already indents. Also gets rid of tabs (issue #5274).
|
|
|
|
set -l tmp
|
|
|
|
for line in $data
|
|
|
|
switch $quote_state
|
|
|
|
case normal
|
|
|
|
set -a tmp (string trim -l -- $line)
|
|
|
|
case single single-escaped double double-escaped escaped
|
|
|
|
set -a tmp $line
|
|
|
|
end
|
|
|
|
set quote_state (__fish_tokenizer_state -i $quote_state -- $line)
|
|
|
|
end
|
|
|
|
set data $data[1] $tmp[2..]
|
2017-03-09 15:52:38 +00:00
|
|
|
end
|
2019-03-13 11:15:45 +00:00
|
|
|
if not string length -q -- (commandline -c)
|
|
|
|
# If we're at the beginning of the first line, trim whitespace from the start,
|
|
|
|
# so we don't trigger ignoring history.
|
2019-05-11 09:19:21 +00:00
|
|
|
set data[1] (string trim -l -- $data[1])
|
2019-03-13 11:15:45 +00:00
|
|
|
end
|
2021-01-05 21:40:09 +00:00
|
|
|
|
2017-03-09 15:52:38 +00:00
|
|
|
if test -n "$data"
|
2017-04-23 11:38:32 +00:00
|
|
|
commandline -i -- $data
|
2017-03-09 15:52:38 +00:00
|
|
|
end
|
2016-05-24 11:17:03 +00:00
|
|
|
end
|