diff --git a/highlighters/main/README.md b/highlighters/main/README.md index 5bdc017..fff62de 100644 --- a/highlighters/main/README.md +++ b/highlighters/main/README.md @@ -45,6 +45,7 @@ This highlighter defines the following styles: * `dollar-quoted-argument` - dollar quoted arguments (`` $'foo' ``) * `dollar-double-quoted-argument` - dollar double quoted arguments ($foo inside "") * `back-double-quoted-argument` - back double quoted arguments (\x inside "") +* `back-dollar-quoted-argument` - back dollar quoted arguments (\x inside $'') * `assign` - variable assignments * `default` - parts of the buffer that do not match anything diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index b576c46..0284a3b 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -54,6 +54,7 @@ : ${ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]:=fg=yellow} : ${ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]:=fg=cyan} : ${ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]:=fg=cyan} +: ${ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]:=fg=cyan} : ${ZSH_HIGHLIGHT_STYLES[assign]:=none} # Whether the highlighter should be called or not. @@ -103,7 +104,7 @@ _zsh_highlight_main_highlighter() for arg in ${(z)buf}; do # substr_color is set to 1 to disable adding an entry to region_highlight - # for this iteration. Currently, that is done for "" strings, + # for this iteration. Currently, that is done for "" and $'' strings, # which add the entry early so escape sequences within the string override # the string's color. integer substr_color=0 @@ -204,6 +205,9 @@ _zsh_highlight_main_highlighter() substr_color=1 ;; \$\'*) style=$ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument] + _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style + _zsh_highlight_main_highlighter_highlight_dollar_string + substr_color=1 ;; '`'*) style=$ZSH_HIGHLIGHT_STYLES[back-quoted-argument];; *[*?]*) $highlight_glob && style=$ZSH_HIGHLIGHT_STYLES[globbing] || style=$ZSH_HIGHLIGHT_STYLES[default];; @@ -265,8 +269,6 @@ _zsh_highlight_main_highlighter_highlight_string() { setopt localoptions noksharrays local i j k style - local AA - integer c # Starting quote is at 1, so start parsing at offset 2 in the string. for (( i = 2 ; i < end_pos - start_pos ; i += 1 )) ; do (( j = i + start_pos - 1 )) @@ -282,6 +284,33 @@ _zsh_highlight_main_highlighter_highlight_string() fi ;; "\\") style=$ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument] + if [[ \\\`\"\$ == *$arg[$i+1]* ]]; then + (( k += 1 )) # Color following char too. + (( i += 1 )) # Skip parsing the escaped char. + else + continue + fi + ;; + *) continue ;; + + esac + _zsh_highlight_main_add_region_highlight $j $k $style + done +} + +# Highlight special chars inside dollar-quoted strings +_zsh_highlight_main_highlighter_highlight_dollar_string() +{ + setopt localoptions noksharrays + local i j k style + local AA + integer c + # Starting dollar-quote is at 1:2, so start parsing at offset 3 in the string. + for (( i = 3 ; i < end_pos - start_pos ; i += 1 )) ; do + (( j = i + start_pos - 1 )) + (( k = j + 1 )) + case "$arg[$i]" in + "\\") style=$ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument] for (( c = i + 1 ; c < end_pos - start_pos ; c += 1 )); do [[ "$arg[$c]" != ([0-9,xX,a-f,A-F]) ]] && break done diff --git a/highlighters/main/test-data/dollar-quoted2.zsh b/highlighters/main/test-data/dollar-quoted2.zsh new file mode 100644 index 0000000..21df3c8 --- /dev/null +++ b/highlighters/main/test-data/dollar-quoted2.zsh @@ -0,0 +1,38 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2015 zsh-syntax-highlighting contributors +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this list of conditions +# and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors +# may be used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ------------------------------------------------------------------------------------------------- +# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- +# vim: ft=zsh sw=2 ts=2 et +# ------------------------------------------------------------------------------------------------- + +ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]=$unused_highlight +BUFFER=": \$'foo\xbar'" + +expected_region_highlight=( + "3 7 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # $'foo + "8 11 $ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]" # \xba + "12 13 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # r' +) diff --git a/highlighters/main/test-data/double-quoted.zsh b/highlighters/main/test-data/double-quoted.zsh index 0847dca..35178a2 100644 --- a/highlighters/main/test-data/double-quoted.zsh +++ b/highlighters/main/test-data/double-quoted.zsh @@ -29,7 +29,7 @@ # ------------------------------------------------------------------------------------------------- BUFFER=': "foo$bar:\`:\":\$:' -BUFFER+=\\\':\" +BUFFER+=\\\\:\" expected_region_highlight=( "3 6 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # "foo @@ -41,6 +41,6 @@ expected_region_highlight=( "17 17 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # : "18 19 $ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]" # \" "20 20 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # : - "21 22 $ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]" # \' + "21 22 $ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]" # \\ "23 24 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # :" ) diff --git a/highlighters/main/test-data/double-quoted3.zsh b/highlighters/main/test-data/double-quoted3.zsh index 4a91c85..2d861cb 100644 --- a/highlighters/main/test-data/double-quoted3.zsh +++ b/highlighters/main/test-data/double-quoted3.zsh @@ -29,10 +29,12 @@ # ------------------------------------------------------------------------------------------------- BUFFER=': "$" "$42foo"' +BUFFER+=\ \"\\\'\\x\" expected_region_highlight=( "3 5 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # "$" "7 7 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # " "8 10 $ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]" # $42 "11 14 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # foo" + "16 21 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # "\'\x" - \' and \x are not escape sequences )