From 948f9b44419066d4e414c06d839abf63c5da60d2 Mon Sep 17 00:00:00 2001 From: Demetri Obenour Date: Mon, 13 Jan 2014 21:27:23 -0500 Subject: [PATCH 1/6] Terminal window and tab titles are correctly escaped. Also, files ending in ~ are gitignored --- .gitignore | 1 + lib/termsupport.zsh | 57 +++++++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 51a5ee6c3..e043d2706 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ custom/* *.swp !custom/example.zshcache cache/ +*~ diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index 80319e1a8..1c8865a9c 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -3,33 +3,56 @@ #Fully support screen, iterm, and probably most modern xterm and rxvt #Limited support for Apple Terminal (Terminal can't set window or tab separately) function title { - if [[ "$DISABLE_AUTO_TITLE" == "true" ]] || [[ "$EMACS" == *term* ]]; then - return - fi - if [[ "$TERM" == screen* ]]; then - print -Pn "\ek$1:q\e\\" #set screen hardstatus, usually truncated at 20 chars - elif [[ "$TERM" == xterm* ]] || [[ $TERM == rxvt* ]] || [[ $TERM == ansi ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then - print -Pn "\e]2;$2:q\a" #set window name - print -Pn "\e]1;$1:q\a" #set icon (=tab) name (will override window name on broken terminal) - fi + if [[ "$DISABLE_AUTO_TITLE" == "true" ]] || [[ "$EMACS" == *term* ]]; then + return + fi + if [[ "$TERM" == screen* ]]; then + print -Pn "\ek$1:q\e\\" #set screen hardstatus, usually truncated at 20 chars + elif [[ "$TERM" == xterm* ]] || [[ $TERM == rxvt* ]] || [[ $TERM == ansi ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then + # print -r "$2:q" 1>&2 + # print -r "$1:q" 1>&2 + print -Pn "\e]2;$2:q\a" #set window name + print -Pn "\e]1;$1:q\a" #set icon (=tab) name (will override window name on broken terminal) + fi } ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD -ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~" +ZSH_THEME_TERM_TITLE_IDLE="%a@%m: %~" #Appears when you have the prompt function omz_termsupport_precmd { - title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE + title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE } #Appears at the beginning of (and during) of command execution function omz_termsupport_preexec { - emulate -L zsh - setopt extended_glob - local CMD=${1[(wr)^(*=*|sudo|ssh|rake|-*)]} #cmd name only, or if this is sudo or ssh, the next cmd - local LINE="${2:gs/$/\\$}" - LINE="${LINE:gs/%/%%}" - title "$CMD" "%100>...>$LINE%<<" + emulate -L zsh + setopt extended_glob + local CMD=${1[(wr)^(*=*|sudo|ssh|rake|-*)]} #cmd name only, or if this is sudo or ssh, the next cmd + CMD=${CMD:gs/\\/\\\\} # escapses \ -- must be first + local LINE=${2:gs/\\/\\\\} # escapes \ -- must be first + + if [[ -o PROMPT_BANG ]]; then + # We must escape ! so that it is not interpreted as a history event number + LINE=${LINE:gs/\!/\!\!} + CMD=${CMD:gs/\!/\!\!} + fi + + if [[ -o PROMPT_SUBST ]]; then + # We must escape $ and `, as both initiate command substitution and + # the former also initiates arithmetic and parameter expansion + CMD=${CMD:gs/$/\\$} + CMD=${CMD:gs/\`/\\\`} + LINE=${LINE:gs/$/\\$} + LINE=${LINE:gs/\`/\\\`} + fi + + if [[ -o PROMPT_PERCENT ]]; then + # We must escape % so that it is not interpreted as starting an escape sequence + CMD=${CMD:gs/%/%%} + LINE=${LINE:gs/%/%%} + fi + title "${CMD}" "%100>...>${LINE}%<<" } autoload -U add-zsh-hook From a1f831bed4db8f24aec6f3429d723065aa6a450a Mon Sep 17 00:00:00 2001 From: Demetri Obenour Date: Mon, 13 Jan 2014 21:34:51 -0500 Subject: [PATCH 2/6] Accidentally changed all tabs to spaces and %a to %n in terminal idle title --- lib/termsupport.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index 1c8865a9c..d949edb00 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -17,7 +17,7 @@ function title { } ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD -ZSH_THEME_TERM_TITLE_IDLE="%a@%m: %~" +ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~" #Appears when you have the prompt function omz_termsupport_precmd { From b4e64e8c68dae2508bacfd5d6c18c9e90713974a Mon Sep 17 00:00:00 2001 From: Demetri Obenour Date: Mon, 13 Jan 2014 21:35:55 -0500 Subject: [PATCH 3/6] Accidentally changed all tabs to spaces and %a to %n in terminal idle title --- lib/termsupport.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index d949edb00..beaa543ef 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -52,7 +52,7 @@ function omz_termsupport_preexec { CMD=${CMD:gs/%/%%} LINE=${LINE:gs/%/%%} fi - title "${CMD}" "%100>...>${LINE}%<<" + title "$CMD" "%100>...>$LINE%<<" } autoload -U add-zsh-hook From a47bfac033a840208e5f2a28c8f30666bd0969df Mon Sep 17 00:00:00 2001 From: Demetri Obenour Date: Mon, 13 Jan 2014 21:44:21 -0500 Subject: [PATCH 4/6] Removed commented-out debug prints --- lib/termsupport.zsh | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index beaa543ef..1c53f8f02 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -9,8 +9,6 @@ function title { if [[ "$TERM" == screen* ]]; then print -Pn "\ek$1:q\e\\" #set screen hardstatus, usually truncated at 20 chars elif [[ "$TERM" == xterm* ]] || [[ $TERM == rxvt* ]] || [[ $TERM == ansi ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then - # print -r "$2:q" 1>&2 - # print -r "$1:q" 1>&2 print -Pn "\e]2;$2:q\a" #set window name print -Pn "\e]1;$1:q\a" #set icon (=tab) name (will override window name on broken terminal) fi @@ -39,20 +37,20 @@ function omz_termsupport_preexec { fi if [[ -o PROMPT_SUBST ]]; then - # We must escape $ and `, as both initiate command substitution and - # the former also initiates arithmetic and parameter expansion - CMD=${CMD:gs/$/\\$} - CMD=${CMD:gs/\`/\\\`} - LINE=${LINE:gs/$/\\$} - LINE=${LINE:gs/\`/\\\`} - fi + # We must escape $ and `, as both initiate command substitution and + # the former also initiates arithmetic and parameter expansion + CMD=${CMD:gs/$/\\$} + CMD=${CMD:gs/\`/\\\`} + LINE=${LINE:gs/$/\\$} + LINE=${LINE:gs/\`/\\\`} + fi - if [[ -o PROMPT_PERCENT ]]; then - # We must escape % so that it is not interpreted as starting an escape sequence - CMD=${CMD:gs/%/%%} - LINE=${LINE:gs/%/%%} - fi - title "$CMD" "%100>...>$LINE%<<" + if [[ -o PROMPT_PERCENT ]]; then + # We must escape % so that it is not interpreted as starting an escape sequence + CMD=${CMD:gs/%/%%} + LINE=${LINE:gs/%/%%} + fi + title "$CMD" "%100>...>$LINE%<<" } autoload -U add-zsh-hook From 1e6079ca665a5a4fcc856f2a53d7634c8ef69a1c Mon Sep 17 00:00:00 2001 From: Demetri Obenour Date: Mon, 13 Jan 2014 21:44:42 -0500 Subject: [PATCH 5/6] Replaced spaces with tabs and deleted trailing whitespace --- lib/termsupport.zsh | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index 1c53f8f02..6f2b6a17e 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -37,22 +37,22 @@ function omz_termsupport_preexec { fi if [[ -o PROMPT_SUBST ]]; then - # We must escape $ and `, as both initiate command substitution and - # the former also initiates arithmetic and parameter expansion - CMD=${CMD:gs/$/\\$} - CMD=${CMD:gs/\`/\\\`} - LINE=${LINE:gs/$/\\$} - LINE=${LINE:gs/\`/\\\`} - fi + # We must escape $ and `, as both initiate command substitution and + # the former also initiates arithmetic and parameter expansion + CMD=${CMD:gs/$/\\$} + CMD=${CMD:gs/\`/\\\`} + LINE=${LINE:gs/$/\\$} + LINE=${LINE:gs/\`/\\\`} + fi - if [[ -o PROMPT_PERCENT ]]; then - # We must escape % so that it is not interpreted as starting an escape sequence - CMD=${CMD:gs/%/%%} - LINE=${LINE:gs/%/%%} - fi - title "$CMD" "%100>...>$LINE%<<" + if [[ -o PROMPT_PERCENT ]]; then + # We must escape % so that it is not interpreted as starting an escape sequence + CMD=${CMD:gs/%/%%} + LINE=${LINE:gs/%/%%} + fi + title "$CMD" "%100>...>$LINE%<<" } autoload -U add-zsh-hook -add-zsh-hook precmd omz_termsupport_precmd +add-zsh-hook precmd omz_termsupport_precmd add-zsh-hook preexec omz_termsupport_preexec From e77c31edc75f7a403ee4348b53f69d9e6882c939 Mon Sep 17 00:00:00 2001 From: Demetri Obenour Date: Mon, 13 Jan 2014 22:20:45 -0500 Subject: [PATCH 6/6] Deleted unnecessary space --- lib/termsupport.zsh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index 6f2b6a17e..7d1ff24b0 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -27,8 +27,7 @@ function omz_termsupport_preexec { emulate -L zsh setopt extended_glob local CMD=${1[(wr)^(*=*|sudo|ssh|rake|-*)]} #cmd name only, or if this is sudo or ssh, the next cmd - CMD=${CMD:gs/\\/\\\\} # escapses \ -- must be first - local LINE=${2:gs/\\/\\\\} # escapes \ -- must be first + local LINE=${2} if [[ -o PROMPT_BANG ]]; then # We must escape ! so that it is not interpreted as a history event number @@ -37,10 +36,14 @@ function omz_termsupport_preexec { fi if [[ -o PROMPT_SUBST ]]; then - # We must escape $ and `, as both initiate command substitution and - # the former also initiates arithmetic and parameter expansion + # We must escape '\', '$', and '`' + # The backslash is the escape character, and so must be escaped first. + # Both '$' and '`' initiate command substitution, and + # the former initiates arithmetic and parameter expansion + CMD=${CMD:gs/\\/\\\\} # escapses \ -- must be first CMD=${CMD:gs/$/\\$} CMD=${CMD:gs/\`/\\\`} + local LINE=${LINE:gs/\\/\\\\} # escapes \ -- must be first LINE=${LINE:gs/$/\\$} LINE=${LINE:gs/\`/\\\`} fi @@ -54,5 +57,5 @@ function omz_termsupport_preexec { } autoload -U add-zsh-hook -add-zsh-hook precmd omz_termsupport_precmd +add-zsh-hook precmd omz_termsupport_precmd add-zsh-hook preexec omz_termsupport_preexec