init.zsh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #
  2. # Sets terminal window and tab titles.
  3. #
  4. # Authors:
  5. # Sorin Ionescu <sorin.ionescu@gmail.com>
  6. # Olaf Conradi <olaf@conradi.org>
  7. #
  8. # Return if requirements are not found.
  9. if [[ "$TERM" == (dumb|linux|*bsd*|eterm*) ]]; then
  10. return 1
  11. fi
  12. # Sets the terminal window title.
  13. function set-window-title {
  14. local title_format{,ted}
  15. zstyle -s ':prezto:module:terminal:window-title' format 'title_format' || title_format="%s"
  16. zformat -f title_formatted "$title_format" "s:$argv"
  17. printf '\e]2;%s\a' "${(V%)title_formatted}"
  18. }
  19. # Sets the terminal tab title.
  20. function set-tab-title {
  21. local title_format{,ted}
  22. zstyle -s ':prezto:module:terminal:tab-title' format 'title_format' || title_format="%s"
  23. zformat -f title_formatted "$title_format" "s:$argv"
  24. printf '\e]1;%s\a' "${(V%)title_formatted}"
  25. }
  26. # Sets the terminal multiplexer tab title.
  27. function set-multiplexer-title {
  28. local title_format{,ted}
  29. zstyle -s ':prezto:module:terminal:multiplexer-title' format 'title_format' || title_format="%s"
  30. zformat -f title_formatted "$title_format" "s:$argv"
  31. printf '\ek%s\e\\' "${(V%)title_formatted}"
  32. }
  33. # Sets the tab and window titles with a given command.
  34. function _terminal-set-titles-with-command {
  35. emulate -L zsh
  36. setopt EXTENDED_GLOB
  37. # Get the command name that is under job control.
  38. if [[ "${2[(w)1]}" == (fg|%*)(\;|) ]]; then
  39. # Get the job name, and, if missing, set it to the default %+.
  40. local job_name="${${2[(wr)%*(\;|)]}:-%+}"
  41. # Make a local copy for use in the subshell.
  42. local -A jobtexts_from_parent_shell
  43. jobtexts_from_parent_shell=(${(kv)jobtexts})
  44. jobs "$job_name" 2> /dev/null > >(
  45. read index discarded
  46. # The index is already surrounded by brackets: [1].
  47. _terminal-set-titles-with-command "${(e):-\$jobtexts_from_parent_shell$index}"
  48. )
  49. else
  50. # Set the command name, or in the case of sudo or ssh, the next command.
  51. local cmd="${${2[(wr)^(*=*|sudo|ssh|-*)]}:t}"
  52. local truncated_cmd="${cmd/(#m)?(#c15,)/${MATCH[1,12]}...}"
  53. unset MATCH
  54. if [[ "$TERM" == screen* ]]; then
  55. set-multiplexer-title "$truncated_cmd"
  56. fi
  57. set-tab-title "$truncated_cmd"
  58. set-window-title "$cmd"
  59. fi
  60. }
  61. # Sets the tab and window titles with a given path.
  62. function _terminal-set-titles-with-path {
  63. emulate -L zsh
  64. setopt EXTENDED_GLOB
  65. local absolute_path="${${1:a}:-$PWD}"
  66. local abbreviated_path="${absolute_path/#$HOME/~}"
  67. local truncated_path="${abbreviated_path/(#m)?(#c15,)/...${MATCH[-12,-1]}}"
  68. unset MATCH
  69. if [[ "$TERM" == screen* ]]; then
  70. set-multiplexer-title "$truncated_path"
  71. fi
  72. set-tab-title "$truncated_path"
  73. set-window-title "$abbreviated_path"
  74. }
  75. # Do not override precmd/preexec; append to the hook array.
  76. autoload -Uz add-zsh-hook
  77. # Set up the Apple Terminal.
  78. if [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]] \
  79. && ( ! [[ -n "$STY" || -n "$TMUX" || -n "$DVTM" ]] )
  80. then
  81. # Sets the Terminal.app current working directory before the prompt is
  82. # displayed.
  83. function _terminal-set-terminal-app-proxy-icon {
  84. printf '\e]7;%s\a' "file://${HOST}${PWD// /%20}"
  85. }
  86. add-zsh-hook precmd _terminal-set-terminal-app-proxy-icon
  87. # Unsets the Terminal.app current working directory when a terminal
  88. # multiplexer or remote connection is started since it can no longer be
  89. # updated, and it becomes confusing when the directory displayed in the title
  90. # bar is no longer synchronized with real current working directory.
  91. function _terminal-unset-terminal-app-proxy-icon {
  92. if [[ "${2[(w)1]:t}" == (screen|tmux|dvtm|ssh|mosh) ]]; then
  93. print '\e]7;\a'
  94. fi
  95. }
  96. add-zsh-hook preexec _terminal-unset-terminal-app-proxy-icon
  97. # Do not set the tab and window titles in Terminal.app since it sets the tab
  98. # title to the currently running process by default and the current working
  99. # directory is set separately.
  100. return
  101. fi
  102. # Set up non-Apple terminals.
  103. if zstyle -t ':prezto:module:terminal' auto-title 'always' \
  104. || (zstyle -t ':prezto:module:terminal' auto-title \
  105. && ( ! [[ -n "$STY" || -n "$TMUX" ]] ))
  106. then
  107. # Sets titles before the prompt is displayed.
  108. add-zsh-hook precmd _terminal-set-titles-with-path
  109. # Sets titles before command execution.
  110. add-zsh-hook preexec _terminal-set-titles-with-command
  111. fi