kitty.bash 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #!/bin/bash
  2. if [[ "$-" != *i* ]] ; then builtin return; fi # check in interactive mode
  3. if [[ -z "$KITTY_SHELL_INTEGRATION" ]]; then builtin return; fi
  4. # Load the normal bash startup files
  5. if [[ -n "$KITTY_BASH_INJECT" ]]; then
  6. builtin declare kitty_bash_inject="$KITTY_BASH_INJECT"
  7. builtin declare ksi_val="$KITTY_SHELL_INTEGRATION"
  8. builtin unset KITTY_SHELL_INTEGRATION # ensure manual sourcing of this file in bashrc does not have any effect
  9. builtin unset KITTY_BASH_INJECT ENV
  10. if [[ -z "$HOME" ]]; then HOME=~; fi
  11. if [[ -z "$KITTY_BASH_ETC_LOCATION" ]]; then KITTY_BASH_ETC_LOCATION="/etc"; fi
  12. _ksi_sourceable() {
  13. [[ -f "$1" && -r "$1" ]] && builtin return 0; builtin return 1;
  14. }
  15. if [[ -n "$ksi_val" && "$ksi_val" != *no-sudo* && -n "$TERMINFO" && ! ( -r "/usr/share/terminfo/x/xterm-kitty" || -r "/usr/share/terminfo/78/xterm-kitty" ) ]]; then
  16. # this must be done before sourcing user bashrc otherwise aliasing of sudo does not work
  17. sudo() {
  18. # Ensure terminfo is available in sudo
  19. builtin local is_sudoedit="n"
  20. for arg; do
  21. if [[ "$arg" == "-e" || $arg == "--edit" ]]; then
  22. is_sudoedit="y"
  23. builtin break;
  24. fi
  25. [[ "$arg" != -* && "$arg" != *=* ]] && builtin break # command found
  26. done
  27. if [[ "$is_sudoedit" == "y" ]]; then
  28. builtin command sudo "$@";
  29. else
  30. builtin command sudo TERMINFO="$TERMINFO" "$@";
  31. fi
  32. }
  33. fi
  34. if [[ "$kitty_bash_inject" == *"posix"* ]]; then
  35. _ksi_sourceable "$KITTY_BASH_POSIX_ENV" && {
  36. builtin source "$KITTY_BASH_POSIX_ENV"
  37. builtin export ENV="$KITTY_BASH_POSIX_ENV"
  38. }
  39. else
  40. builtin set +o posix
  41. builtin shopt -u inherit_errexit 2>/dev/null # resetting posix does not clear this
  42. if [[ -n "$KITTY_BASH_UNEXPORT_HISTFILE" ]]; then
  43. builtin export -n HISTFILE
  44. builtin unset KITTY_BASH_UNEXPORT_HISTFILE
  45. fi
  46. # See run_startup_files() in shell.c in the Bash source code
  47. if builtin shopt -q login_shell; then
  48. if [[ "$kitty_bash_inject" != *"no-profile"* ]]; then
  49. _ksi_sourceable "$KITTY_BASH_ETC_LOCATION/profile" && builtin source "$KITTY_BASH_ETC_LOCATION/profile"
  50. for _ksi_i in "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.profile"; do
  51. _ksi_sourceable "$_ksi_i" && { builtin source "$_ksi_i"; break; }
  52. done
  53. fi
  54. else
  55. if [[ "$kitty_bash_inject" != *"no-rc"* ]]; then
  56. # Linux distros build bash with -DSYS_BASHRC. Unfortunately, there is
  57. # no way to to probe bash for it and different distros use different files
  58. # Arch, Debian, Ubuntu use /etc/bash.bashrc
  59. # Fedora uses /etc/bashrc sourced from ~/.bashrc instead of SYS_BASHRC
  60. # Void Linux uses /etc/bash/bashrc
  61. for _ksi_i in "$KITTY_BASH_ETC_LOCATION/bash.bashrc" "$KITTY_BASH_ETC_LOCATION/bash/bashrc" ; do
  62. _ksi_sourceable "$_ksi_i" && { builtin source "$_ksi_i"; break; }
  63. done
  64. if [[ -z "$KITTY_BASH_RCFILE" ]]; then KITTY_BASH_RCFILE="$HOME/.bashrc"; fi
  65. _ksi_sourceable "$KITTY_BASH_RCFILE" && builtin source "$KITTY_BASH_RCFILE"
  66. fi
  67. fi
  68. fi
  69. builtin unset KITTY_BASH_RCFILE KITTY_BASH_POSIX_ENV KITTY_BASH_ETC_LOCATION
  70. builtin unset -f _ksi_sourceable
  71. builtin export KITTY_SHELL_INTEGRATION="$ksi_val"
  72. builtin unset _ksi_i ksi_val kitty_bash_inject
  73. fi
  74. if [ "${BASH_VERSINFO:-0}" -lt 4 ]; then
  75. builtin unset KITTY_SHELL_INTEGRATION
  76. builtin printf "%s\n" "Bash version ${BASH_VERSION} too old, kitty shell integration disabled" > /dev/stderr
  77. builtin return
  78. fi
  79. if [[ "${_ksi_prompt[sourced]}" == "y" ]]; then
  80. # we have already run
  81. builtin unset KITTY_SHELL_INTEGRATION
  82. builtin return
  83. fi
  84. # this is defined outside _ksi_main to make it global without using declare -g
  85. # which is not available on older bash
  86. builtin declare -A _ksi_prompt
  87. _ksi_prompt=(
  88. [cursor]='y' [title]='y' [mark]='y' [complete]='y' [cwd]='y' [sudo]='y' [ps0]='' [ps0_suffix]='' [ps1]='' [ps1_suffix]='' [ps2]=''
  89. [hostname_prefix]='' [sourced]='y' [last_reported_cwd]=''
  90. )
  91. _ksi_main() {
  92. builtin local ifs="$IFS" i
  93. IFS=" "
  94. for i in ${KITTY_SHELL_INTEGRATION[@]}; do
  95. case "$i" in
  96. "no-cursor") _ksi_prompt[cursor]='n';;
  97. "no-title") _ksi_prompt[title]='n';;
  98. "no-prompt-mark") _ksi_prompt[mark]='n';;
  99. "no-complete") _ksi_prompt[complete]='n';;
  100. "no-cwd") _ksi_prompt[cwd]='n';;
  101. "no-sudo") _ksi_prompt[sudo]='n';;
  102. esac
  103. done
  104. IFS="$ifs"
  105. builtin unset KITTY_SHELL_INTEGRATION
  106. _ksi_debug_print() {
  107. # print a line to STDERR of parent kitty process
  108. builtin local b
  109. b=$(builtin command base64 <<< "${@}")
  110. builtin printf "\eP@kitty-print|%s\e\\" "${b//[[:space:]]}}"
  111. }
  112. _ksi_set_mark() {
  113. _ksi_prompt["${1}_mark"]="\[\e]133;k;${1}_kitty\a\]"
  114. }
  115. _ksi_set_mark start
  116. _ksi_set_mark end
  117. _ksi_set_mark start_secondary
  118. _ksi_set_mark end_secondary
  119. _ksi_set_mark start_suffix
  120. _ksi_set_mark end_suffix
  121. builtin unset -f _ksi_set_mark
  122. _ksi_prompt[secondary_prompt]="\n${_ksi_prompt[start_secondary_mark]}\[\e]133;A;k=s\a\]${_ksi_prompt[end_secondary_mark]}"
  123. _ksi_prompt_command() {
  124. # we first remove any previously added kitty code from the prompt variables and then add
  125. # it back, to ensure we have only a single instance
  126. if [[ -n "${_ksi_prompt[ps0]}" ]]; then
  127. PS0=${PS0//\\\[\\e\]133;k;start_kitty\\a\\\]*end_kitty\\a\\\]}
  128. PS0="${_ksi_prompt[ps0]}$PS0"
  129. fi
  130. if [[ -n "${_ksi_prompt[ps0_suffix]}" ]]; then
  131. PS0=${PS0//\\\[\\e\]133;k;start_suffix_kitty\\a\\\]*end_suffix_kitty\\a\\\]}
  132. PS0="${PS0}${_ksi_prompt[ps0_suffix]}"
  133. fi
  134. # restore PS1 to its pristine state without our additions
  135. if [[ -n "${_ksi_prompt[ps1]}" ]]; then
  136. PS1=${PS1//\\\[\\e\]133;k;start_kitty\\a\\\]*end_kitty\\a\\\]}
  137. PS1=${PS1//\\\[\\e\]133;k;start_secondary_kitty\\a\\\]*end_secondary_kitty\\a\\\]}
  138. fi
  139. if [[ -n "${_ksi_prompt[ps1_suffix]}" ]]; then
  140. PS1=${PS1//\\\[\\e\]133;k;start_suffix_kitty\\a\\\]*end_suffix_kitty\\a\\\]}
  141. fi
  142. if [[ -n "${_ksi_prompt[ps1]}" ]]; then
  143. if [[ "${_ksi_prompt[mark]}" == "y" && ( "${PS1}" == *"\n"* || "${PS1}" == *$'\n'* ) ]]; then
  144. builtin local oldval
  145. oldval=$(builtin shopt -p extglob)
  146. builtin shopt -s extglob
  147. # bash does not redraw the leading lines in a multiline prompt so
  148. # mark the last line as a secondary prompt. Otherwise on resize the
  149. # lines before the last line will be erased by kitty.
  150. # the first part removes everything from the last \n onwards
  151. # the second part appends a newline with the secondary marking
  152. # the third part appends everything after the last newline
  153. PS1=${PS1%@('\n'|$'\n')*}${_ksi_prompt[secondary_prompt]}${PS1##*@('\n'|$'\n')}
  154. builtin eval "$oldval"
  155. fi
  156. PS1="${_ksi_prompt[ps1]}$PS1"
  157. fi
  158. if [[ -n "${_ksi_prompt[ps1_suffix]}" ]]; then
  159. PS1="${PS1}${_ksi_prompt[ps1_suffix]}"
  160. fi
  161. if [[ -n "${_ksi_prompt[ps2]}" ]]; then
  162. PS2=${PS2//\\\[\\e\]133;k;start_kitty\\a\\\]*end_kitty\\a\\\]}
  163. PS2="${_ksi_prompt[ps2]}$PS2"
  164. fi
  165. if [[ "${_ksi_prompt[cwd]}" == "y" ]]; then
  166. # unfortunately bash provides no hooks to detect cwd changes
  167. # in particular this means cwd reporting will not happen for a
  168. # command like cd /test && cat. PS0 is evaluated before cd is run.
  169. if [[ "${_ksi_prompt[last_reported_cwd]}" != "$PWD" ]]; then
  170. _ksi_prompt[last_reported_cwd]="$PWD"
  171. builtin printf "\e]7;kitty-shell-cwd://%s%s\a" "$HOSTNAME" "$PWD"
  172. fi
  173. fi
  174. }
  175. if [[ "${_ksi_prompt[cursor]}" == "y" ]]; then
  176. _ksi_prompt[ps1_suffix]+="\[\e[5 q\]" # blinking bar cursor
  177. _ksi_prompt[ps0_suffix]+="\[\e[0 q\]" # blinking default cursor
  178. fi
  179. if [[ "${_ksi_prompt[title]}" == "y" ]]; then
  180. if [[ -z "$KITTY_PID" ]]; then
  181. if [[ -n "$SSH_TTY" || -n "$SSH2_TTY$KITTY_WINDOW_ID" ]]; then
  182. # connected to most SSH servers
  183. # or use ssh kitten to connected to some SSH servers that do not set SSH_TTY
  184. _ksi_prompt[hostname_prefix]="\h: "
  185. elif [[ -n "$(builtin command -v who)" && "$(builtin command who -m 2> /dev/null)" =~ "\([a-fA-F.:0-9]+\)$" ]]; then
  186. # the shell integration script is installed manually on the remote system
  187. # the environment variables are cleared after sudo
  188. # OpenSSH's sshd creates entries in utmp for every login so use those
  189. _ksi_prompt[hostname_prefix]="\h: "
  190. fi
  191. fi
  192. # see https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html#Controlling-the-Prompt
  193. # we use suffix here because some distros add title setting to their bashrc files by default
  194. _ksi_prompt[ps1_suffix]+="\[\e]2;${_ksi_prompt[hostname_prefix]}\w\a\]"
  195. if [[ "$HISTCONTROL" == *"ignoreboth"* ]] || [[ "$HISTCONTROL" == *"ignorespace"* ]]; then
  196. _ksi_debug_print "ignoreboth or ignorespace present in bash HISTCONTROL setting, showing running command in window title will not be robust"
  197. fi
  198. _ksi_get_current_command() {
  199. builtin local last_cmd
  200. last_cmd=$(HISTTIMEFORMAT= builtin history 1)
  201. last_cmd="${last_cmd#*[[:digit:]]*[[:space:]]}" # remove leading history number
  202. last_cmd="${last_cmd#"${last_cmd%%[![:space:]]*}"}" # remove remaining leading whitespace
  203. builtin printf "\e]2;%s%s\a" "${_ksi_prompt[hostname_prefix]@P}" "${last_cmd//[[:cntrl:]]}" # remove any control characters
  204. }
  205. _ksi_prompt[ps0_suffix]+='$(_ksi_get_current_command)'
  206. fi
  207. if [[ "${_ksi_prompt[mark]}" == "y" ]]; then
  208. _ksi_prompt[ps1]+="\[\e]133;A\a\]"
  209. _ksi_prompt[ps2]+="\[\e]133;A;k=s\a\]"
  210. _ksi_prompt[ps0]+="\[\e]133;C\a\]"
  211. fi
  212. builtin alias edit-in-kitty="kitten edit-in-kitty"
  213. if [[ "${_ksi_prompt[complete]}" == "y" ]]; then
  214. _ksi_completions() {
  215. builtin local src
  216. builtin local limit
  217. # Send all words up to the word the cursor is currently on
  218. builtin let limit=1+$COMP_CWORD
  219. src=$(builtin printf "%s\n" "${COMP_WORDS[@]:0:$limit}" | builtin command kitten __complete__ bash)
  220. if [[ $? == 0 ]]; then
  221. builtin eval "${src}"
  222. fi
  223. }
  224. builtin complete -F _ksi_completions kitty
  225. builtin complete -F _ksi_completions edit-in-kitty
  226. builtin complete -F _ksi_completions clone-in-kitty
  227. builtin complete -F _ksi_completions kitten
  228. fi
  229. # wrap our prompt additions in markers we can use to remove them using
  230. # bash's anemic pattern substitution
  231. if [[ -n "${_ksi_prompt[ps0]}" ]]; then
  232. _ksi_prompt[ps0]="${_ksi_prompt[start_mark]}${_ksi_prompt[ps0]}${_ksi_prompt[end_mark]}"
  233. fi
  234. if [[ -n "${_ksi_prompt[ps0_suffix]}" ]]; then
  235. _ksi_prompt[ps0_suffix]="${_ksi_prompt[start_suffix_mark]}${_ksi_prompt[ps0_suffix]}${_ksi_prompt[end_suffix_mark]}"
  236. fi
  237. if [[ -n "${_ksi_prompt[ps1]}" ]]; then
  238. _ksi_prompt[ps1]="${_ksi_prompt[start_mark]}${_ksi_prompt[ps1]}${_ksi_prompt[end_mark]}"
  239. fi
  240. if [[ -n "${_ksi_prompt[ps1_suffix]}" ]]; then
  241. _ksi_prompt[ps1_suffix]="${_ksi_prompt[start_suffix_mark]}${_ksi_prompt[ps1_suffix]}${_ksi_prompt[end_suffix_mark]}"
  242. fi
  243. if [[ -n "${_ksi_prompt[ps2]}" ]]; then
  244. _ksi_prompt[ps2]="${_ksi_prompt[start_mark]}${_ksi_prompt[ps2]}${_ksi_prompt[end_mark]}"
  245. fi
  246. # BASH aborts the entire script when doing unset with failglob set, somebody should report this upstream
  247. builtin local oldval
  248. oldval=$(builtin shopt -p failglob)
  249. builtin shopt -u failglob
  250. builtin unset _ksi_prompt[start_mark] _ksi_prompt[end_mark] _ksi_prompt[start_suffix_mark] _ksi_prompt[end_suffix_mark] _ksi_prompt[start_secondary_mark] _ksi_prompt[end_secondary_mark]
  251. builtin eval "$oldval"
  252. # install our prompt command, using an array if it is unset or already an array,
  253. # otherwise append a string. We check if _ksi_prompt_command exists as some shell
  254. # scripts stupidly export PROMPT_COMMAND making it inherited by all programs launched
  255. # from the shell
  256. builtin local pc
  257. pc='builtin declare -F _ksi_prompt_command > /dev/null 2> /dev/null && _ksi_prompt_command'
  258. if [[ -z "${PROMPT_COMMAND[*]}" ]]; then
  259. PROMPT_COMMAND=([0]="$pc")
  260. elif [[ $(builtin declare -p PROMPT_COMMAND 2> /dev/null) =~ 'declare -a PROMPT_COMMAND' ]]; then
  261. PROMPT_COMMAND+=("$pc")
  262. else
  263. builtin local oldval
  264. oldval=$(builtin shopt -p extglob)
  265. builtin shopt -s extglob
  266. PROMPT_COMMAND="${PROMPT_COMMAND%%+([[:space:]])}"
  267. PROMPT_COMMAND="${PROMPT_COMMAND%%+(;)}"
  268. builtin eval "$oldval"
  269. PROMPT_COMMAND+="; $pc"
  270. fi
  271. if [ -n "${KITTY_IS_CLONE_LAUNCH}" ]; then
  272. builtin local orig_conda_env="$CONDA_DEFAULT_ENV"
  273. builtin eval "${KITTY_IS_CLONE_LAUNCH}"
  274. builtin hash -r 2> /dev/null 1> /dev/null
  275. builtin local venv="${VIRTUAL_ENV}/bin/activate"
  276. builtin local sourced=""
  277. _ksi_s_is_ok() {
  278. [[ -z "$sourced" && "$KITTY_CLONE_SOURCE_STRATEGIES" == *",$1,"* ]] && builtin return 0
  279. builtin return 1
  280. }
  281. if _ksi_s_is_ok "venv" && [ -n "${VIRTUAL_ENV}" -a -r "$venv" ]; then
  282. sourced="y"
  283. builtin unset VIRTUAL_ENV
  284. builtin source "$venv"
  285. fi; if _ksi_s_is_ok "conda" && [ -n "${CONDA_DEFAULT_ENV}" ] && builtin command -v conda >/dev/null 2>/dev/null && [ "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]; then
  286. sourced="y"
  287. conda activate "${CONDA_DEFAULT_ENV}"
  288. fi; if _ksi_s_is_ok "env_var" && [[ -n "${KITTY_CLONE_SOURCE_CODE}" ]]; then
  289. sourced="y"
  290. builtin eval "${KITTY_CLONE_SOURCE_CODE}"
  291. fi; if _ksi_s_is_ok "path" && [[ -r "${KITTY_CLONE_SOURCE_PATH}" ]]; then
  292. sourced="y"
  293. builtin source "${KITTY_CLONE_SOURCE_PATH}"
  294. fi
  295. builtin unset -f _ksi_s_is_ok
  296. # Ensure PATH has no duplicate entries
  297. if [ -n "$PATH" ]; then
  298. builtin local old_PATH=$PATH:; PATH=
  299. while [ -n "$old_PATH" ]; do
  300. builtin local x
  301. x=${old_PATH%%:*}
  302. case $PATH: in
  303. *:"$x":*) ;;
  304. *) PATH=$PATH:$x;;
  305. esac
  306. old_PATH=${old_PATH#*:}
  307. done
  308. PATH=${PATH#:}
  309. fi
  310. fi
  311. builtin unset KITTY_IS_CLONE_LAUNCH KITTY_CLONE_SOURCE_STRATEGIES
  312. }
  313. _ksi_main
  314. builtin unset -f _ksi_main
  315. case :$SHELLOPTS: in
  316. *:posix:*) ;;
  317. *)
  318. _ksi_transmit_data() {
  319. builtin local data
  320. data="${1//[[:space:]]}"
  321. builtin local pos=0
  322. builtin local chunk_num=0
  323. while [ $pos -lt ${#data} ]; do
  324. builtin local chunk="${data:$pos:2048}"
  325. pos=$(($pos+2048))
  326. builtin printf '\eP@kitty-%s|%s:%s\e\\' "${2}" "${chunk_num}" "${chunk}"
  327. chunk_num=$(($chunk_num+1))
  328. done
  329. # save history so it is available in new shell
  330. [ "$3" = "save_history" ] && builtin history -a
  331. builtin printf '\eP@kitty-%s|\e\\' "${2}"
  332. }
  333. clone-in-kitty() {
  334. builtin local bv="${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}.${BASH_VERSINFO[2]}"
  335. builtin local data="shell=bash,pid=$$,bash_version=$bv,cwd=$(builtin printf "%s" "$PWD" | builtin command base64),envfmt=bash,env=$(builtin export | builtin command base64)"
  336. while :; do
  337. case "$1" in
  338. "") break;;
  339. -h|--help)
  340. builtin printf "%s\n\n%s\n" "Clone the current bash session into a new kitty window." "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#clone-shell"
  341. builtin return
  342. ;;
  343. *) data="$data,a=$(builtin printf "%s" "$1" | builtin command base64)";;
  344. esac
  345. shift
  346. done
  347. _ksi_transmit_data "$data" "clone" "save_history"
  348. }
  349. ;;
  350. esac