From afdb4d82508c9baf4d75ad1185557df44f7163da Mon Sep 17 00:00:00 2001 From: Takeshi Banse Date: Tue, 14 Jun 2011 12:58:01 +0900 Subject: [PATCH 1/4] pass ${arg} explicit Signed-off-by: Takeshi Banse --- highlighters/main/main-highlighter.zsh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 3c8e25b..e0d1439 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -85,10 +85,10 @@ _zsh_highlight_main_highlighter() *': function') style=$ZSH_HIGHLIGHT_STYLES[function];; *': command') style=$ZSH_HIGHLIGHT_STYLES[command];; *': hashed') style=$ZSH_HIGHLIGHT_STYLES[hashed-command];; - *) if _zsh_highlight_main_highlighter_check_assign; then + *) if _zsh_highlight_main_highlighter_check_assign "$arg"; then style=$ZSH_HIGHLIGHT_STYLES[assign] new_expression=true - elif _zsh_highlight_main_highlighter_check_path; then + elif _zsh_highlight_main_highlighter_check_path "$arg"; then style=$ZSH_HIGHLIGHT_STYLES[path] elif [[ $arg[0,1] = $histchars[0,1] ]]; then style=$ZSH_HIGHLIGHT_STYLES[history-expansion] @@ -109,7 +109,7 @@ _zsh_highlight_main_highlighter() ;; '`'*'`') style=$ZSH_HIGHLIGHT_STYLES[back-quoted-argument];; *"*"*) $highlight_glob && style=$ZSH_HIGHLIGHT_STYLES[globbing] || style=$ZSH_HIGHLIGHT_STYLES[default];; - *) if _zsh_highlight_main_highlighter_check_path; then + *) if _zsh_highlight_main_highlighter_check_path "$arg"; then style=$ZSH_HIGHLIGHT_STYLES[path] elif [[ $arg[0,1] = $histchars[0,1] ]]; then style=$ZSH_HIGHLIGHT_STYLES[history-expansion] @@ -128,6 +128,7 @@ _zsh_highlight_main_highlighter() # Check if the argument is variable assignment _zsh_highlight_main_highlighter_check_assign() { + local arg="$1" setopt localoptions extended_glob [[ ${(Q)arg} == [[:alpha:]_]([[:alnum:]_])#=* ]] } @@ -135,6 +136,7 @@ _zsh_highlight_main_highlighter_check_assign() # Check if the argument is a path. _zsh_highlight_main_highlighter_check_path() { + local arg="$1" [[ -z ${(Q)arg} ]] && return 1 [[ -e ${(Q)arg} ]] && return 0 [[ ! -e ${(Q)arg:h} ]] && return 1 From c1517a01e1e6d8a684f4e32e3abaed8d2aa1f62b Mon Sep 17 00:00:00 2001 From: Takeshi Banse Date: Tue, 14 Jun 2011 13:00:45 +0900 Subject: [PATCH 2/4] add some tests Signed-off-by: Takeshi Banse --- highlighters/main/test-data/assign.zsh | 35 +++++++++++++++++++ highlighters/main/test-data/path-space- .zsh | 36 ++++++++++++++++++++ highlighters/main/test-data/path.zsh | 36 ++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 highlighters/main/test-data/assign.zsh create mode 100644 highlighters/main/test-data/path-space- .zsh create mode 100644 highlighters/main/test-data/path.zsh diff --git a/highlighters/main/test-data/assign.zsh b/highlighters/main/test-data/assign.zsh new file mode 100644 index 0000000..0401bf4 --- /dev/null +++ b/highlighters/main/test-data/assign.zsh @@ -0,0 +1,35 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2010-2011 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 +# ------------------------------------------------------------------------------------------------- + +BUFFER='A=1' + +expected_region_highlight=( + "1 3 $ZSH_HIGHLIGHT_STYLES[assign]" # A=1 +) diff --git a/highlighters/main/test-data/path-space- .zsh b/highlighters/main/test-data/path-space- .zsh new file mode 100644 index 0000000..91c3833 --- /dev/null +++ b/highlighters/main/test-data/path-space- .zsh @@ -0,0 +1,36 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2010-2011 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 +# ------------------------------------------------------------------------------------------------- + +BUFFER='ls highlighters/main/test-data/path-space-\ .zsh' + +expected_region_highlight=( + "1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls + "4 48 $ZSH_HIGHLIGHT_STYLES[path]" # highlighters/main/test-data/path-space-\ .zsh +) diff --git a/highlighters/main/test-data/path.zsh b/highlighters/main/test-data/path.zsh new file mode 100644 index 0000000..52e3af1 --- /dev/null +++ b/highlighters/main/test-data/path.zsh @@ -0,0 +1,36 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2010-2011 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 +# ------------------------------------------------------------------------------------------------- + +BUFFER='ls highlighters/main/test-data/path.zsh' + +expected_region_highlight=( + "1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls + "4 39 $ZSH_HIGHLIGHT_STYLES[path]" # highlighters/main/test-data/path.zsh +) From d794de728109c868b7496700b0afa8d4f6bf9a85 Mon Sep 17 00:00:00 2001 From: Takeshi Banse Date: Tue, 14 Jun 2011 13:15:06 +0900 Subject: [PATCH 3/4] add tests for handling tildes Signed-off-by: Takeshi Banse --- .../main/test-data/path-tilde-home.zsh | 36 ++++++++++++++++++ .../main/test-data/path-tilde-named.zsh | 38 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 highlighters/main/test-data/path-tilde-home.zsh create mode 100644 highlighters/main/test-data/path-tilde-named.zsh diff --git a/highlighters/main/test-data/path-tilde-home.zsh b/highlighters/main/test-data/path-tilde-home.zsh new file mode 100644 index 0000000..14f8e18 --- /dev/null +++ b/highlighters/main/test-data/path-tilde-home.zsh @@ -0,0 +1,36 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2010-2011 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 +# ------------------------------------------------------------------------------------------------- + +BUFFER='ls ~' + +expected_region_highlight=( + "1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls + "4 4 $ZSH_HIGHLIGHT_STYLES[path]" # ~ +) diff --git a/highlighters/main/test-data/path-tilde-named.zsh b/highlighters/main/test-data/path-tilde-named.zsh new file mode 100644 index 0000000..38b2bb5 --- /dev/null +++ b/highlighters/main/test-data/path-tilde-named.zsh @@ -0,0 +1,38 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2010-2011 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 +# ------------------------------------------------------------------------------------------------- + +hash -d D=highlighters/main/test-data + +BUFFER='ls ~D/path-tilde-named.zsh' + +expected_region_highlight=( + "1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls + "4 23 $ZSH_HIGHLIGHT_STYLES[path]" # ~D/path-tilde-named.zsh +) From 5b3d18ef36e73df14eb3eb2ec1f2686af90ce885 Mon Sep 17 00:00:00 2001 From: Takeshi Banse Date: Tue, 14 Jun 2011 13:15:33 +0900 Subject: [PATCH 4/4] handle tilde Signed-off-by: Takeshi Banse --- highlighters/main/main-highlighter.zsh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index e0d1439..385ec9a 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -139,11 +139,22 @@ _zsh_highlight_main_highlighter_check_path() local arg="$1" [[ -z ${(Q)arg} ]] && return 1 [[ -e ${(Q)arg} ]] && return 0 + [[ "${arg[1]-}" == \~ ]] && { _zsh_highlight_main_highlighter_check_path_tilde "${arg}" ; return $? } [[ ! -e ${(Q)arg:h} ]] && return 1 [[ ${BUFFER[1]} != "-" && ${#BUFFER} == $end_pos && -n $(print ${(Q)arg}*(N)) ]] && return 0 return 1 } +_zsh_highlight_main_highlighter_check_path_tilde() { + local head="${1[2,-1]%%/*}" + local tail="${1[((2+1+$#head)),-1]}" + local name= + eval "name=~$head/" 2>/dev/null || return 1 + [[ -z "$name" ]] && return 1 + [[ "$name" == "~$head/" ]] && return 1 + _zsh_highlight_main_highlighter_check_path "${name}${tail}"; return $? +} + # Highlight special chars inside double-quoted strings _zsh_highlight_main_highlighter_highlight_string() {