Highlight backslash escapes in "" and $'' strings correctly.
Remove highlighting of hex and octal escapes in "" strings (which don't parse those escapes), add it to $'' strings (which do), and correct the regexps. Also add support for unicode \u/\U escapes. Fixes zsh-users/zsh-syntax-highlighting#196. * danielsh/i196-back-dollar-quoted-v1: back-dollar-quoted-argument: Correct an off-by-one. back-dollar-quoted-argument: Correct octal escape syntax. back-dollar-quoted-argument: Highlight \uHHHH and \UHHHHHHHH escapes. back-dollar-quoted-argument: Don't consider \0xHH a hex escape sequence. back-dollar-quoted-argument: Don't consider comma a hex character. Highlight backslash escapes within $'' strings.
This commit is contained in:
		
						commit
						219184f046
					
				|  | @ -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 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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,12 +284,43 @@ _zsh_highlight_main_highlighter_highlight_string() | |||
|             fi | ||||
|             ;; | ||||
|       "\\") style=$ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument] | ||||
|             for (( c = i + 1 ; c < end_pos - start_pos ; c += 1 )); do | ||||
|               [[ "$arg[$c]" != ([0-9,xX,a-f,A-F]) ]] && break | ||||
|             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-9xXuUa-fA-F]) ]] && break | ||||
|             done | ||||
|             AA=$arg[$i+1,$c-1] | ||||
|             # Matching for HEX and OCT values like \0xA6, \xA6 or \012 | ||||
|             if [[ "$AA" =~ "^(0*(x|X)[0-9,a-f,A-F]{1,2})" || "$AA" =~ "^(0[0-7]{1,3})" ]];then | ||||
|             if [[    "$AA" =~ "^(x|X)[0-9a-fA-F]{1,2}" | ||||
|                   || "$AA" =~ "^[0-7]{1,3}" | ||||
|                   || "$AA" =~ "^u[0-9a-fA-F]{1,4}" | ||||
|                   || "$AA" =~ "^U[0-9a-fA-F]{1,8}" | ||||
|                ]]; then | ||||
|               (( k += $#MATCH )) | ||||
|               (( i += $#MATCH )) | ||||
|             else | ||||
|  |  | |||
|  | @ -0,0 +1,40 @@ | |||
| #!/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\udeadbeef'" | ||||
| 
 | ||||
| expected_region_highlight=( | ||||
|   "3 7 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # $'foo | ||||
|   "8 11 $ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]" # \xba | ||||
|   "12 12 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # r | ||||
|   "13 18 $ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]" # \dead | ||||
|   "19 23 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # beef' | ||||
| ) | ||||
|  | @ -0,0 +1,39 @@ | |||
| #!/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 | ||||
| # ------------------------------------------------------------------------------------------------- | ||||
| 
 | ||||
| # Similar to double-quoted2.zsh | ||||
| ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]=$unused_highlight | ||||
| # This test checks that the '1' gets highlighted correctly.  Do not append to the BUFFER. | ||||
| BUFFER=": \$'\xa1" | ||||
| 
 | ||||
| expected_region_highlight=( | ||||
|   "3 4 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # $' | ||||
|   "5 8 $ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]" # \xa1 | ||||
| ) | ||||
|  | @ -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]" # :" | ||||
| ) | ||||
|  |  | |||
|  | @ -28,6 +28,7 @@ | |||
| # vim: ft=zsh sw=2 ts=2 et | ||||
| # ------------------------------------------------------------------------------------------------- | ||||
| 
 | ||||
| # Similar to dollar-quoted3.zsh | ||||
| # This test checks that the 'r' gets highlighted correctly.  Do not append to the BUFFER. | ||||
| BUFFER=': "foo$bar' | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 | ||||
| ) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue