From b98c7469e5026c55efa48cdacd4923ca54e8b098 Mon Sep 17 00:00:00 2001 From: Sorin Ionescu Date: Mon, 26 Aug 2013 12:12:44 -0400 Subject: [PATCH] Rewrite terminal auto-title --- modules/helper/README.md | 2 ++ modules/helper/init.zsh | 5 +++ modules/terminal/README.md | 9 +++-- modules/terminal/init.zsh | 70 ++++++++++++++++++++++++-------------- runcoms/zpreztorc | 8 +++-- 5 files changed, 63 insertions(+), 31 deletions(-) diff --git a/modules/helper/README.md b/modules/helper/README.md index b010b00..7cafc2c 100644 --- a/modules/helper/README.md +++ b/modules/helper/README.md @@ -11,6 +11,8 @@ Functions - `is-autoloadable` checks if a file can be autoloaded by trying to load it in a subshell. - `is-callable` checks if a name is a command, function, or alias. + - `is-terminal-inside-multiplexer` checks if the currently running terminal is + inside a terminal multiplexer. - `is-true` checks a boolean variable for "true". - `coalesce` prints the first non-empty string in the arguments array. diff --git a/modules/helper/init.zsh b/modules/helper/init.zsh index 7e0f9d5..b7a89ae 100644 --- a/modules/helper/init.zsh +++ b/modules/helper/init.zsh @@ -21,6 +21,11 @@ function is-true { [[ -n "$1" && "$1" == (1|[Yy]([Ee][Ss]|)|[Tt]([Rr][Uu][Ee]|)|[Oo]([Nn]|)) ]] } +# Checks if the currently running terminal is inside a terminal multiplexer. +function is-terminal-inside-multiplexer { + [[ -n "$STY" || -n "$TMUX" || -n "$DVTM" ]] +} + # Prints the first non-empty string in the arguments array. function coalesce { for arg in $argv; do diff --git a/modules/terminal/README.md b/modules/terminal/README.md index f1256c6..c1158b1 100644 --- a/modules/terminal/README.md +++ b/modules/terminal/README.md @@ -8,10 +8,15 @@ Settings ### Auto-Title -To auto set the terminal window and tab titles with the current command or +To auto set the terminal emulator window and tab titles with the current command +or directory, add the following to *zpreztorc*: + + zstyle ':prezto:module:terminal:auto-title' emulator 'yes' + +To auto set the terminal multiplexer window title with the current command or directory, add the following to *zpreztorc*: - zstyle ':prezto:module:terminal' auto-title 'yes' + zstyle ':prezto:module:terminal:auto-title' multiplexer 'yes' Functions --------- diff --git a/modules/terminal/init.zsh b/modules/terminal/init.zsh index c5b9dd3..8c46eb0 100644 --- a/modules/terminal/init.zsh +++ b/modules/terminal/init.zsh @@ -6,6 +6,9 @@ # Sorin Ionescu # +# Load dependencies. +pmodload 'helper' + # Return if requirements are not found. if [[ "$TERM" == 'dumb' ]]; then return 1 @@ -20,21 +23,23 @@ function set-screen-window-title { # Sets the terminal window title. function set-terminal-window-title { - if [[ "$TERM" == ((x|a|ml|dt|E)term*|(u|)rxvt*) ]]; then + if [[ "$TERM" == ((x|a|ml|dt|E)term*|(u|)rxvt*|dvtm*) ]]; then printf "\e]2;%s\a" ${(V)argv} fi } # Sets the terminal tab title. function set-terminal-tab-title { - if [[ "$TERM" == ((x|a|ml|dt|E)term*|(u|)rxvt*) ]]; then + if [[ "$TERM" == ((x|a|ml|dt|E)term*|(u|)rxvt*|dvtm*) ]]; then printf "\e]1;%s\a" ${(V)argv} fi } # Sets the Terminal.app current working directory. function set-terminal-app-cwd { - printf '\e]7;%s\a' "file://$HOST${PWD// /%20}" + if [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]]; then + printf '\e]7;%s\a' "file://$HOST${${1:-$PWD}// /%20}" + fi } # Sets the tab and window titles with a given command. @@ -89,36 +94,49 @@ function set-titles-with-path { fi } -# Don't override precmd/preexec; append to hook array. +# Do not override precmd/preexec; append to the hook array. autoload -Uz add-zsh-hook -if [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]]; then - # Sets the Terminal.app current working directory. +# Set up the Apple Terminal. +if [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]] \ + && (( ${${OSTYPE#darwin}[1,2]} >= 11 )) \ + && ! is-terminal-inside-multiplexer +then + # Sets the Terminal.app current working directory before the prompt is + # displayed. add-zsh-hook precmd set-terminal-app-cwd + # Unsets the Terminal.app current working directory when a terminal + # multiplexer or remote connection is started since it can no longer be + # updated, and it becomes confusing when the directory displayed in the title + # bar is no longer synchronized with real current working directory. + function unset-terminal-app-cwd { + if [[ "${2[(w)1]:t}" == (screen|tmux|dvtm|ssh|mosh) ]]; then + set-terminal-app-cwd ' ' + fi + } + add-zsh-hook preexec unset-terminal-app-cwd + # Do not set the tab and window titles in Terminal.app since it sets the tab # title to the currently running process by default and the current working - # directory is set separately. - - # Do set the tab and window titles inside terminal multiplexers. - if [[ "$TERM" != screen* ]]; then - return - fi + # directory is set separately, but do set the tab and window titles inside + # terminal multiplexers inside Terminal.app. + return fi -# Sets the tab and window titles before the prompt is displayed. -function set-titles-precmd { - if zstyle -t ':prezto:module:terminal' auto-title; then - set-titles-with-path - fi -} -add-zsh-hook precmd set-titles-precmd +# Set up non-Apple terminals. +if ( ( ! is-terminal-inside-multiplexer || [[ -n "$DVTM" ]] ) \ + && zstyle -t ':prezto:module:terminal:auto-title' emulator ) \ + || ( is-terminal-inside-multiplexer \ + && zstyle -t ':prezto:module:terminal:auto-title' multiplexer ) +then + # Sets the tab and window titles before the prompt is displayed. + add-zsh-hook precmd set-titles-with-path -# Sets the tab and window titles before command execution. -function set-titles-preexec { - if zstyle -t ':prezto:module:terminal' auto-title; then - set-titles-with-command "$2" - fi -} -add-zsh-hook preexec set-titles-preexec + # Sets the tab and window titles before command execution. + function set-titles-with-command-preexec { + set-titles-with-command "$2" + } + add-zsh-hook preexec set-titles-with-command-preexec +fi diff --git a/runcoms/zpreztorc b/runcoms/zpreztorc index 8cdb163..0fde4bd 100644 --- a/runcoms/zpreztorc +++ b/runcoms/zpreztorc @@ -122,14 +122,16 @@ zstyle ':prezto:module:prompt' theme 'sorin' # 'builtin' 'bg=blue' \ # 'command' 'bg=blue' \ # 'function' 'bg=blue' -# # # Terminal # -# Auto set the tab and window titles. -zstyle ':prezto:module:terminal' auto-title 'yes' +# Auto set the terminal emulator tab and window titles. +zstyle ':prezto:module:terminal:auto-title' emulator 'yes' + +# Auto set the terminal multiplexer window title. +zstyle ':prezto:module:terminal:auto-title' multiplexer 'no' # # Tmux