]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/safe-paste/safe-paste.plugin.zsh
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / safe-paste / safe-paste.plugin.zsh
1 # A good summary of the zsh 5.1 Bracketed Paste Mode changes is at:
2 # https://archive.zhimingwang.org/blog/2015-09-21-zsh-51-and-bracketed-paste.html
3
4 # zsh 5.1 (September 2015) introduced built-in support for Bracketed Paste Mode
5 # https://github.com/zsh-users/zsh/blob/68405f31a043bdd5bf338eb06688ed3e1f740937/README#L38-L45
6 #
7 # zsh 5.1 breaks url-quote-magic and other widgets replacing self-insert
8 # zsh-users' bracketed-paste-magic resolves these issues:
9 # https://github.com/zsh-users/zsh/blob/f702e17b14d75aa21bff014168fa9048124db286/Functions/Zle/bracketed-paste-magic#L9-L12
10
11 # Load bracketed-paste-magic if zsh version is >= 5.1
12 if [[ ${ZSH_VERSION:0:3} -ge 5.1 ]]; then
13   set zle_bracketed_paste  # Explicitly restore this zsh default
14   autoload -Uz bracketed-paste-magic
15   zle -N bracketed-paste bracketed-paste-magic
16   return  ### The rest of this file is NOT executed on zsh version >= 5.1 ###
17 fi
18
19 ######################################################################
20 #    The rest of this file is ONLY executed if zsh version < 5.1
21 ######################################################################
22
23 # Code from Mikael Magnusson: https://www.zsh.org/mla/users/2011/msg00367.html
24 #
25 # Requires xterm, urxvt, iTerm2 or any other terminal that supports
26 # Bracketed Paste Mode as documented:
27 # https://www.xfree86.org/current/ctlseqs.html#Bracketed%20Paste%20Mode
28 #
29 # For tmux, use:   bind ] paste-buffer -p
30 #
31 # Additional technical details: https://cirw.in/blog/bracketed-paste
32
33 # Create a new keymap to use while pasting
34 bindkey -N bracketed-paste
35 # Make everything in this new keymap enqueue characters for pasting
36 bindkey -RM bracketed-paste '\x00-\xFF' bracketed-paste-enqueue
37 # These are the codes sent around the pasted text in bracketed paste mode
38 bindkey -M main            '^[[200~' _bracketed_paste_begin
39 bindkey -M bracketed-paste '^[[201~' _bracketed_paste_end
40 # Insert newlines rather than carriage returns when pasting newlines
41 bindkey -M bracketed-paste -s '^M' '^J'
42
43 zle -N _bracketed_paste_begin
44 zle -N _bracketed_paste_end
45 zle -N bracketed-paste-enqueue _bracketed_paste_enqueue
46
47 # Attempt to not clobber zle_line_{init,finish}
48 # Use https://github.com/willghatch/zsh-hooks if available
49 if typeset -f hooks-add-hook > /dev/null; then
50   hooks-add-hook zle_line_init_hook   _bracketed_paste_zle_init
51   hooks-add-hook zle_line_finish_hook _bracketed_paste_zle_finish
52 else
53   zle -N zle-line-init   _bracketed_paste_zle_init
54   zle -N zle-line-finish _bracketed_paste_zle_finish
55 fi
56
57 # Switch the active keymap to paste mode
58 _bracketed_paste_begin() {
59   # Save the bindkey command to restore the active ("main") keymap
60   # Tokenise the restorative bindkey command into an array
61   _bracketed_paste_restore_keymap=( ${(z)"$(bindkey -lL main)"} )
62   bindkey -A bracketed-paste main
63 }
64
65 # Go back to our normal keymap, and insert all the pasted text in the
66 # command line. This has the nice effect of making the whole paste be
67 # a single undo/redo event.
68 _bracketed_paste_end() {
69   # Only execute the restore command if it starts with 'bindkey'
70   # Allow for option KSH_ARRAYS being set (indexing starts at 0)
71   if [ ${_bracketed_paste_restore_keymap[@]:0:1} = 'bindkey' ]; then
72     $_bracketed_paste_restore_keymap
73   fi
74   LBUFFER+=$_bracketed_paste_content
75   unset _bracketed_paste_content _bracketed_paste_restore_keymap
76 }
77
78 # Append a pasted character to the content which is later inserted as a whole
79 _bracketed_paste_enqueue() {
80   _bracketed_paste_content+=$KEYS
81 }
82
83 # Run at zle-line-init
84 _bracketed_paste_zle_init() {
85   _bracketed_paste_content=''
86   # Tell terminal to send escape codes around pastes
87   if [[ $TERM =~ '^(rxvt-unicode|xterm(-256color)?|screen(-256color)?)$' ]]; then
88     printf '\e[?2004h'
89   fi
90 }
91
92 # Run at zle-line-finish
93 _bracketed_paste_zle_finish() {
94   # Turn off bracketed paste when we leave ZLE, so pasting in other programs
95   # doesn't get the ^[[200~ codes around the pasted text
96   if [[ $TERM =~ '^(rxvt-unicode|xterm(-256color)?|screen(-256color)?)$' ]]; then
97     printf '\e[?2004l'
98   fi
99 }
100