fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerFix a wrong path to fish_indent on OpenBSDab39fab68

master
1.9 KiB49 linesraw
1function fish_clipboard_copy
2    set -l cmdline
3    if isatty stdin
4        # Copy the current selection, or the entire commandline if that is empty.
5        # Don't use `string collect -N` here - `commandline` adds a newline.
6        set cmdline (commandline --current-selection | fish_indent --only-indent | string collect)
7        test -n "$cmdline"; or set cmdline (commandline | fish_indent --only-indent | string collect)
8    else
9        # Read from stdin
10        while read -lz line
11            set -a cmdline $line
12        end
13    end
14
15    if type -q pbcopy
16        printf '%s' $cmdline | pbcopy
17    else if set -q WAYLAND_DISPLAY; and type -q wl-copy
18        printf '%s' $cmdline | wl-copy &
19        disown
20    else if set -q DISPLAY; and type -q xsel
21        printf '%s' $cmdline | xsel --clipboard
22    else if set -q DISPLAY; and type -q xclip
23        printf '%s' $cmdline | xclip -selection clipboard
24    else if type -q clip.exe
25        printf '%s' $cmdline | clip.exe
26    end
27
28    # Copy with OSC 52; useful if we are running in an SSH session or in
29    # a container.
30    if type -q base64 && [ "$TERM" != dumb ]
31        if not isatty stdout
32            echo "fish_clipboard_copy: stdout is not a terminal" >&2
33            return 1
34        end
35        set -l encoded (printf %s $cmdline | base64 | string join '')
36        printf '\e]52;c;%s\a' "$encoded"
37        # tmux requires user configuration to interpret OSC 52 on stdout.
38        # Luckily we can still make this work for the common case by bypassing
39        # tmux and writing to its underlying terminal.
40        if set -q TMUX
41            set -l tmux_tty (tmux display-message -p '#{client_tty}')
42            or return 1
43            # The terminal might not be writable if we switched user.
44            if test -w $tmux_tty
45                printf '\e]52;c;%s\a' "$encoded" >$tmux_tty
46            end
47        end
48    end
49end