File: fish_clipboard_paste.fish

package info (click to toggle)
fish 3.1.2-3%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 38,672 kB
  • sloc: ansic: 80,259; cpp: 47,069; javascript: 17,087; sh: 6,163; python: 3,429; makefile: 669; perl: 367; objc: 78; xml: 18
file content (42 lines) | stat: -rw-r--r-- 1,462 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function fish_clipboard_paste
    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)
    end

    # Issue 6254: Handle zero-length clipboard content
    if not string match -qr . -- "$data"
        return 1
    end

    # Also split on \r to turn it into a newline,
    # otherwise the output looks really confusing.
    set data (string split \r -- $data)

    # 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
        if status test-feature regex-easyesc
            set data (string replace -ra "(['\\\])" '\\\\$1' -- $data)
        else
            set data (string replace -ra "(['\\\])" '\\\\\\\$1' -- $data)
        end
    end
    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[1] (string trim -l -- $data[1])
    end
    if test -n "$data"
        commandline -i -- $data
    end
end