2016-05-24 19:17:03 +08:00
|
|
|
function fish_clipboard_paste
|
2017-03-09 23:52:38 +08:00
|
|
|
set -l data
|
2016-11-28 13:27:22 +08:00
|
|
|
if type -q pbpaste
|
2017-03-09 23:52:38 +08:00
|
|
|
set data (pbpaste)
|
2016-05-24 19:17:03 +08:00
|
|
|
else if type -q xsel
|
2017-03-09 23:52:38 +08:00
|
|
|
# Return if `xsel` failed.
|
2018-04-26 05:10:58 +08:00
|
|
|
# That way we don't print the redundant (and overly verbose for this) commandline error.
|
2016-12-11 04:26:51 +08:00
|
|
|
# Also require non-empty contents to not clear the buffer.
|
2018-04-26 05:10:58 +08:00
|
|
|
if not set data (xsel --clipboard 2>/dev/null)
|
2017-03-09 23:52:38 +08:00
|
|
|
return 1
|
2016-11-28 17:09:58 +08:00
|
|
|
end
|
2018-06-03 04:53:02 +08:00
|
|
|
else if type -q xclip
|
|
|
|
if not set data (xclip -selection clipboard -o 2>/dev/null)
|
|
|
|
return 1
|
|
|
|
end
|
2018-12-31 05:51:24 +08:00
|
|
|
else if type -q wl-paste
|
|
|
|
set data (wl-paste)
|
2016-05-24 19:17:03 +08:00
|
|
|
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).
|
|
|
|
if __fish_commandline_is_singlequoted
|
2019-01-20 18:12:27 +08:00
|
|
|
if status test-feature string-replace-fewer-backslashes
|
|
|
|
set data (string replace -ra "(['\\\])" '\\\\$1' -- $data)
|
|
|
|
else
|
|
|
|
set data (string replace -ra "(['\\\])" '\\\\\\\$1' -- $data)
|
|
|
|
end
|
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.
|
|
|
|
set data (string trim -l -- $data)
|
|
|
|
end
|
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
|