2019-10-28 01:34:58 +08:00
|
|
|
function fish_clipboard_paste
|
2019-11-05 03:47:58 +08: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 20:11:56 +08:00
|
|
|
else if type -q powershell.exe
|
|
|
|
set data (powershell.exe Get-Clipboard | string trim -r -c \r)
|
2019-11-05 03:47:58 +08:00
|
|
|
end
|
2019-10-28 01:31:35 +08:00
|
|
|
|
|
|
|
# Issue 6254: Handle zero-length clipboard content
|
|
|
|
if not string match -qr . -- "$data"
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2017-04-23 19:40:40 +08: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 23:52:38 +08: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 15:40:07 +08:00
|
|
|
set -l quote_state (__fish_tokenizer_state -- (commandline -ct | string collect))
|
2021-02-08 15:12:43 +08:00
|
|
|
if contains -- $quote_state single single-escaped
|
2019-08-14 13:28:17 +08:00
|
|
|
if status test-feature regex-easyesc
|
2019-01-20 18:12:27 +08:00
|
|
|
set data (string replace -ra "(['\\\])" '\\\\$1' -- $data)
|
|
|
|
else
|
|
|
|
set data (string replace -ra "(['\\\])" '\\\\\\\$1' -- $data)
|
|
|
|
end
|
2021-02-08 15:12:43 +08: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 23:52:38 +08:00
|
|
|
end
|
2019-03-13 19:15:45 +08: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 17:19:21 +08:00
|
|
|
set data[1] (string trim -l -- $data[1])
|
2019-03-13 19:15:45 +08:00
|
|
|
end
|
2021-01-06 05:40:09 +08:00
|
|
|
|
2017-03-09 23:52:38 +08:00
|
|
|
if test -n "$data"
|
2017-04-23 19:38:32 +08:00
|
|
|
commandline -i -- $data
|
2017-03-09 23:52:38 +08:00
|
|
|
end
|
2016-05-24 19:17:03 +08:00
|
|
|
end
|