From a7a7f8b42280ccc93df07127d6ae379a0b755cbc Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Mon, 9 May 2016 04:06:44 +0000 Subject: [PATCH 01/66] Support linewise region. Fixes #267. --- zsh-syntax-highlighting.zsh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index f07851d..b806fae 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -115,7 +115,22 @@ _zsh_highlight() # Re-apply zle_highlight settings # region - (( REGION_ACTIVE )) && _zsh_highlight_apply_zle_highlight region standout "$MARK" "$CURSOR" + if (( REGION_ACTIVE == 1 )); then + _zsh_highlight_apply_zle_highlight region standout "$MARK" "$CURSOR" + elif (( REGION_ACTIVE == 2 )); then + () { + local needle=$'\n' + integer min max + if (( MARK > CURSOR )) ; then + min=$CURSOR max=$MARK + else + min=$MARK max=$CURSOR + fi + (( min = ${${BUFFER[1,$min]}[(I)$needle]} )) + (( max += ${${BUFFER:($max-1)}[(i)$needle]} - 1 )) + _zsh_highlight_apply_zle_highlight region standout "$min" "$max" + } + fi # yank / paste (zsh-5.1.1 and newer) (( $+YANK_ACTIVE )) && (( YANK_ACTIVE )) && _zsh_highlight_apply_zle_highlight paste standout "$YANK_START" "$YANK_END" From b9112aec798a41035d044d4f93d7cfa50c9ca580 Mon Sep 17 00:00:00 2001 From: m0viefreak Date: Sat, 11 Jun 2016 16:38:03 +0200 Subject: [PATCH 02/66] driver: Widget binding: Use ${(k)widgets} instead of $(zle -la) Avoids a fork. --- zsh-syntax-highlighting.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 87967b6..e49e5a8 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -236,7 +236,7 @@ _zsh_highlight_bind_widgets() # Override ZLE widgets to make them invoke _zsh_highlight. local cur_widget - for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|orig-*|run-help|which-command|beep|set-local-history|yank)}; do + for cur_widget in ${${(k)widgets}:#(.*|orig-*|run-help|which-command|beep|set-local-history|yank)}; do case $widgets[$cur_widget] in # Already rebound event: do nothing. From ee07588cfd9bb07a5176bc6eb74e25da6db39129 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 1 Jul 2016 01:59:37 +0000 Subject: [PATCH 03/66] tests: Add a regression test for issue #267, concerning highlighting a vi linewise region. --- .../main/test-data/vi-linewise-mode.zsh | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 highlighters/main/test-data/vi-linewise-mode.zsh diff --git a/highlighters/main/test-data/vi-linewise-mode.zsh b/highlighters/main/test-data/vi-linewise-mode.zsh new file mode 100644 index 0000000..4b77766 --- /dev/null +++ b/highlighters/main/test-data/vi-linewise-mode.zsh @@ -0,0 +1,38 @@ +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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 +# ------------------------------------------------------------------------------------------------- + +# See issue #267 for the magic numbers +BUFFER=$'foo foo\nbar bar' +REGION_ACTIVE=2 +CURSOR=4 +MARK=12 + +expected_region_highlight=( + "1 3 standout" # foo +) From 3e59ab41b6b85a7b3113f3f3e7a97befd3e16491 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 12 Jul 2016 06:50:49 +0000 Subject: [PATCH 04/66] driver: Change a variable name to avoid squatting the highlighters' namespace. Part of issue #329. --- zsh-syntax-highlighting.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 610add0..25b9453 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -81,7 +81,7 @@ _zsh_highlight() local highlighter; for highlighter in $ZSH_HIGHLIGHT_HIGHLIGHTERS; do # eval cache place for current highlighter and prepare it - cache_place="_zsh_highlight_${highlighter}_highlighter_cache" + cache_place="_zsh_highlight_highlighter_${highlighter}_cache" typeset -ga ${cache_place} # If highlighter needs to be invoked From ba16cf2fb2f2071499a2a19c264be942d2a3a5b6 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 12 Jul 2016 07:03:18 +0000 Subject: [PATCH 05/66] docs: s/myhighlighter/acme/g Makes the text easier to read ("_zsh_highlight_myhighlighter_highlighter" is a mouthful). --- docs/highlighters.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/highlighters.md b/docs/highlighters.md index 00738ba..d09da3e 100644 --- a/docs/highlighters.md +++ b/docs/highlighters.md @@ -49,21 +49,21 @@ highlighter's documentation for details and examples. How to implement a new highlighter ---------------------------------- -To create your own `myhighlighter` highlighter: +To create your own `acme` highlighter: * Create your script at - `highlighters/${myhighlighter}/${myhighlighter}-highlighter.zsh`. + `highlighters/${acme}/${acme}-highlighter.zsh`. -* Implement the `_zsh_highlight_myhighlighter_highlighter_predicate` function. +* Implement the `_zsh_highlight_acme_highlighter_predicate` function. This function must return 0 when the highlighter needs to be called and non-zero otherwise, for example: - _zsh_highlight_myhighlighter_highlighter_predicate() { + _zsh_highlight_acme_highlighter_predicate() { # Call this highlighter in SVN working copies [[ -d .svn ]] } -* Implement the `_zsh_highlight_myhighlighter_highlighter` function. +* Implement the `_zsh_highlight_acme_highlighter` function. This function does the actual syntax highlighting, by calling `_zsh_highlight_add_highlight` with the start and end of the region to be highlighted and the `ZSH_HIGHLIGHT_STYLES` key to use. Define the default @@ -71,15 +71,15 @@ To create your own `myhighlighter` highlighter: `: ${ZSH_HIGHLIGHT_STYLES[key]:=value}`, being sure to prefix the key with your highlighter name and a colon. For example: - : ${ZSH_HIGHLIGHT_STYLES[myhighlighter:aurora]:=fg=green} + : ${ZSH_HIGHLIGHT_STYLES[acme:aurora]:=fg=green} - _zsh_highlight_myhighlighter_highlighter() { + _zsh_highlight_acme_highlighter() { # Colorize the whole buffer with the 'aurora' style - _zsh_highlight_add_highlight 0 $#BUFFER myhighlighter:aurora + _zsh_highlight_add_highlight 0 $#BUFFER acme:aurora } * Activate your highlighter in `~/.zshrc`: - ZSH_HIGHLIGHT_HIGHLIGHTERS+=(myhighlighter) + ZSH_HIGHLIGHT_HIGHLIGHTERS+=(acme) * [Write tests](../tests/README.md). From 80148f6c840299f0980f4359ec5307ca63837dff Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 12 Jul 2016 07:05:37 +0000 Subject: [PATCH 06/66] docs: State highlighters' designated namespace. --- docs/highlighters.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/highlighters.md b/docs/highlighters.md index d09da3e..1df2388 100644 --- a/docs/highlighters.md +++ b/docs/highlighters.md @@ -78,6 +78,8 @@ To create your own `acme` highlighter: _zsh_highlight_add_highlight 0 $#BUFFER acme:aurora } +* Name your own functions and global variables `_zsh_highlight_acme_*`. + * Activate your highlighter in `~/.zshrc`: ZSH_HIGHLIGHT_HIGHLIGHTERS+=(acme) From fffe13a8fb85c3c05c20293ed58f5e79c1b848ce Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Mon, 18 Jul 2016 04:27:54 +0000 Subject: [PATCH 07/66] docs: Minor tweak. Suggested-by: Matthew Martin --- docs/highlighters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/highlighters.md b/docs/highlighters.md index 1df2388..2659645 100644 --- a/docs/highlighters.md +++ b/docs/highlighters.md @@ -52,7 +52,7 @@ How to implement a new highlighter To create your own `acme` highlighter: * Create your script at - `highlighters/${acme}/${acme}-highlighter.zsh`. + `highlighters/acme/acme-highlighter.zsh`. * Implement the `_zsh_highlight_acme_highlighter_predicate` function. This function must return 0 when the highlighter needs to be called and From 95f7206a93738f8bc4fc7dc98dd0c8fcafba3976 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 20 Jul 2016 02:00:28 +0000 Subject: [PATCH 08/66] tests: Add an XFail test for issue #342. --- .../main/test-data/path-broken-symlink.zsh | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 highlighters/main/test-data/path-broken-symlink.zsh diff --git a/highlighters/main/test-data/path-broken-symlink.zsh b/highlighters/main/test-data/path-broken-symlink.zsh new file mode 100644 index 0000000..79a790e --- /dev/null +++ b/highlighters/main/test-data/path-broken-symlink.zsh @@ -0,0 +1,36 @@ +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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 +# ------------------------------------------------------------------------------------------------- + +ln -s /nonexistent broken-symlink +BUFFER=': broken-symlink' +CURSOR=5 # to make path_prefix ineligible + +expected_region_highlight=( + "3 16 path 'issue #342'" # broken-symlink +) From 53083da8215e3a32a3d515ce06439a2609cd209b Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 20 Jul 2016 02:00:56 +0000 Subject: [PATCH 09/66] 'main': Highlight a broken symlink as a file. Fixes #342. --- highlighters/main/main-highlighter.zsh | 1 + highlighters/main/test-data/path-broken-symlink.zsh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 540e05b..e1ff409 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -582,6 +582,7 @@ _zsh_highlight_main_highlighter_check_path() REPLY=path [[ -z $expanded_path ]] && return 1 + [[ -L $expanded_path ]] && return 0 [[ -e $expanded_path ]] && return 0 # Search the path in CDPATH diff --git a/highlighters/main/test-data/path-broken-symlink.zsh b/highlighters/main/test-data/path-broken-symlink.zsh index 79a790e..84c7d59 100644 --- a/highlighters/main/test-data/path-broken-symlink.zsh +++ b/highlighters/main/test-data/path-broken-symlink.zsh @@ -32,5 +32,5 @@ BUFFER=': broken-symlink' CURSOR=5 # to make path_prefix ineligible expected_region_highlight=( - "3 16 path 'issue #342'" # broken-symlink + "3 16 path" # broken-symlink ) From 3409a2e4d27386e3404e331553194c3e0f0163ac Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 21 Jul 2016 04:01:50 +0000 Subject: [PATCH 10/66] *: s/echo/print/ Just in case one of the interpolated variables contains a backslash. --- highlighters/main/main-highlighter.zsh | 4 ++-- zsh-syntax-highlighting.zsh | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 540e05b..abe4d38 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -693,7 +693,7 @@ _zsh_highlight_main_highlighter_highlight_dollar_string() # Does not perform filename generation (globbing). _zsh_highlight_main_highlighter_expand_path() { - (( $# == 1 )) || echo "zsh-syntax-highlighting: BUG: _zsh_highlight_main_highlighter_expand_path: called without argument" >&2 + (( $# == 1 )) || print -r -- >&2 "zsh-syntax-highlighting: BUG: _zsh_highlight_main_highlighter_expand_path: called without argument" # The $~1 syntax normally performs filename generation, but not when it's on the right-hand side of ${x:=y}. setopt localoptions nonomatch @@ -714,7 +714,7 @@ if add-zsh-hook precmd _zsh_highlight_main__precmd_hook 2>/dev/null; then # Initialize command type cache typeset -gA _zsh_highlight_main__command_type_cache else - echo 'zsh-syntax-highlighting: Failed to load add-zsh-hook. Some speed optimizations will not be used.' >&2 + print -r -- >&2 'zsh-syntax-highlighting: Failed to load add-zsh-hook. Some speed optimizations will not be used.' # Make sure the cache is unset unset _zsh_highlight_main__command_type_cache fi diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 25b9453..e401956 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -245,7 +245,7 @@ _zsh_highlight_bind_widgets() # Load ZSH module zsh/zleparameter, needed to override user defined widgets. zmodload zsh/zleparameter 2>/dev/null || { - echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2 + print -r -- >&2 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' return 1 } @@ -278,7 +278,7 @@ _zsh_highlight_bind_widgets() zle -N $cur_widget _zsh_highlight_widget_$cur_widget;; # Default: unhandled case. - *) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;; + *) print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" ;; esac done } @@ -293,7 +293,7 @@ _zsh_highlight_load_highlighters() # Check the directory exists. [[ -d "$1" ]] || { - echo "zsh-syntax-highlighting: highlighters directory '$1' not found." >&2 + print -r -- >&2 "zsh-syntax-highlighting: highlighters directory '$1' not found." return 1 } @@ -305,7 +305,7 @@ _zsh_highlight_load_highlighters() . "$highlighter_dir/${highlighter}-highlighter.zsh" type "_zsh_highlight_${highlighter}_highlighter" &> /dev/null && type "_zsh_highlight_${highlighter}_highlighter_predicate" &> /dev/null || { - echo "zsh-syntax-highlighting: '${highlighter}' highlighter should define both required functions '_zsh_highlight_${highlighter}_highlighter' and '_zsh_highlight_${highlighter}_highlighter_predicate' in '${highlighter_dir}/${highlighter}-highlighter.zsh'." >&2 + print -r -- >&2 "zsh-syntax-highlighting: '${highlighter}' highlighter should define both required functions '_zsh_highlight_${highlighter}_highlighter' and '_zsh_highlight_${highlighter}_highlighter_predicate' in '${highlighter_dir}/${highlighter}-highlighter.zsh'." } } done @@ -318,13 +318,13 @@ _zsh_highlight_load_highlighters() # Try binding widgets. _zsh_highlight_bind_widgets || { - echo 'zsh-syntax-highlighting: failed binding ZLE widgets, exiting.' >&2 + print -r -- >&2 'zsh-syntax-highlighting: failed binding ZLE widgets, exiting.' return 1 } # Resolve highlighters directory location. _zsh_highlight_load_highlighters "${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:-${${0:A}:h}/highlighters}" || { - echo 'zsh-syntax-highlighting: failed loading highlighters, exiting.' >&2 + print -r -- >&@ 'zsh-syntax-highlighting: failed loading highlighters, exiting.' return 1 } @@ -336,7 +336,7 @@ _zsh_highlight_preexec_hook() } autoload -U add-zsh-hook add-zsh-hook preexec _zsh_highlight_preexec_hook 2>/dev/null || { - echo 'zsh-syntax-highlighting: failed loading add-zsh-hook.' >&2 + print -r -- >&2 'zsh-syntax-highlighting: failed loading add-zsh-hook.' } # Load zsh/parameter module if available From 6b69389bd9ea20f09af8550c2045cc76a9053e68 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 22 Jul 2016 15:52:43 +0000 Subject: [PATCH 11/66] 'main': New test, related to issue #328. The test passes so I'm adding it directly to master. --- .../main/test-data/path-dollared-word2.zsh | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 highlighters/main/test-data/path-dollared-word2.zsh diff --git a/highlighters/main/test-data/path-dollared-word2.zsh b/highlighters/main/test-data/path-dollared-word2.zsh new file mode 100644 index 0000000..3485b45 --- /dev/null +++ b/highlighters/main/test-data/path-dollared-word2.zsh @@ -0,0 +1,36 @@ +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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 +# ------------------------------------------------------------------------------------------------- + +lambda="''" +touch \$lambda +BUFFER=': \$lambda' + +expected_region_highlight=( + "3 8 path" # \$lambda +) From 0d41933c61d16d8c2d68df01f41ead98b0330127 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 07:50:38 +0000 Subject: [PATCH 12/66] 'main': Add test for issue #343, concerning the 'command' precommand. --- highlighters/main/test-data/precommand2.zsh | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 highlighters/main/test-data/precommand2.zsh diff --git a/highlighters/main/test-data/precommand2.zsh b/highlighters/main/test-data/precommand2.zsh new file mode 100644 index 0000000..3d8f332 --- /dev/null +++ b/highlighters/main/test-data/precommand2.zsh @@ -0,0 +1,36 @@ +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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='command -v ls' + +expected_region_highlight=( + "1 7 precommand" # command + "9 10 single-hyphen-option 'issue #343'" # -v + "12 13 command 'issue #343'" # ls +) From fa57633d81526451248919210ae4aad731a76a0e Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 08:13:51 +0000 Subject: [PATCH 13/66] 'main': Enable test for issue #238. --- highlighters/main/test-data/exec-redirection1.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/main/test-data/exec-redirection1.zsh b/highlighters/main/test-data/exec-redirection1.zsh index 784ec47..caec2f1 100644 --- a/highlighters/main/test-data/exec-redirection1.zsh +++ b/highlighters/main/test-data/exec-redirection1.zsh @@ -31,7 +31,7 @@ BUFFER='exec {foo}>&/tmp ls' expected_region_highlight=( "1 4 precommand" # exec - # TODO: "6 10 redirection 'issue #238'" # {foo} + "6 10 redirection 'issue #238'" # {foo} "11 12 redirection" # >& "13 16 path" # /tmp "18 19 command 'issue #238'" # ls From 8013dc3b8db6726fd2cc4bccacaaab9fb31055ca Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 23:15:32 +0000 Subject: [PATCH 14/66] dev tools: Add a script that generates a test-data file. --- tests/README.md | 19 +++++++++++ tests/generate.zsh | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 tests/generate.zsh diff --git a/tests/README.md b/tests/README.md index d55945f..b7e3450 100644 --- a/tests/README.md +++ b/tests/README.md @@ -39,6 +39,25 @@ which is automatically cleaned up after the test exits. For example: "1 21 command" # bar/testing-issue-228 ) + +Writing new tests +----------------- + +An experimental tool is available to generate test files: + + zsh -f tests/generate.zsh 'ls -x' \ + | sed s/YYYY/$(date +%Y)/ \ + > highlighters/main/test-data/foo.zsh + git add -N $_ + +This generates a test file based on the current highlighting of the given `$BUFFER` +(in this case, `ls -x`). + +_This tool is experimental._ Its interface may change. In particular it may +grow ways to set `$PREBUFFER` and/or `$ZSH_HIGHLIGHT_HIGHLIGHTERS` or to +inject free-form code into the generated file. + + Highlighting test ----------------- diff --git a/tests/generate.zsh b/tests/generate.zsh new file mode 100755 index 0000000..3fc0ebc --- /dev/null +++ b/tests/generate.zsh @@ -0,0 +1,83 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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 +# ------------------------------------------------------------------------------------------------- + +emulate -LR zsh + +# Argument parsing. +if (( $# != 1 )) || [[ $1 == -* ]]; then + print -r -- >&2 "$0: usage: $0 BUFFER" + print -r -- >&2 "" + print -r -- >&2 "This tool generates a test file, suitable for highlighters/*/test-data/." + exit 1 +fi +buffer=$1 + +# Load the main script. +. ${0:A:h:h}/zsh-syntax-highlighting.zsh + +# Overwrite _zsh_highlight_add_highlight so we get the key itself instead of the style +_zsh_highlight_add_highlight() +{ + region_highlight+=("$1 $2 $3") +} + +# Copyright block +<$0 sed -n -e '1,/^$/p' | sed -e 's/2[0-9][0-9][0-9]/YYYY/' +echo "" + +# Buffer +print -n 'BUFFER=' +print -r -- ${(qq)buffer} +echo "" + +# Expectations +print 'expected_region_highlight=(' +() { + local i + local PREBUFFER + local BUFFER + + PREBUFFER="" + BUFFER="$buffer" + region_highlight=() + _zsh_highlight + + for ((i=1; i<=${#region_highlight}; i++)); do + local -a highlight_zone; highlight_zone=( ${(z)region_highlight[$i]} ) + integer start=$highlight_zone[1] end=$highlight_zone[2] + if (( start < end )) # region_highlight ranges are half-open + then + (( --end )) # convert to closed range, like expected_region_highlight + (( ++start, ++end )) # region_highlight is 0-indexed; expected_region_highlight is 1-indexed + fi + printf " %s # %s\n" ${(qq):-"$start $end $highlight_zone[3]"} $BUFFER[start,end] + done +} +print ')' From add6825898e101ccab12d3ffd93091fc2284e637 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 29 Jul 2016 19:02:40 +0000 Subject: [PATCH 15/66] dev tools: Extend tests/generate.zsh. --- tests/README.md | 12 ++++-------- tests/generate.zsh | 10 +++++++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/README.md b/tests/README.md index b7e3450..5526612 100644 --- a/tests/README.md +++ b/tests/README.md @@ -45,17 +45,13 @@ Writing new tests An experimental tool is available to generate test files: - zsh -f tests/generate.zsh 'ls -x' \ - | sed s/YYYY/$(date +%Y)/ \ - > highlighters/main/test-data/foo.zsh - git add -N $_ + zsh -f tests/generate.zsh 'ls -x' acme newfile -This generates a test file based on the current highlighting of the given `$BUFFER` -(in this case, `ls -x`). +This generates a `highlighters/acme/test-data/newfile.zsh` test file based on +the current highlighting of the given `$BUFFER` (in this case, `ls -x`). _This tool is experimental._ Its interface may change. In particular it may -grow ways to set `$PREBUFFER` and/or `$ZSH_HIGHLIGHT_HIGHLIGHTERS` or to -inject free-form code into the generated file. +grow ways to set `$PREBUFFER` to inject free-form code into the generated file. Highlighting test diff --git a/tests/generate.zsh b/tests/generate.zsh index 3fc0ebc..5bd0b60 100755 --- a/tests/generate.zsh +++ b/tests/generate.zsh @@ -31,13 +31,17 @@ emulate -LR zsh # Argument parsing. -if (( $# != 1 )) || [[ $1 == -* ]]; then - print -r -- >&2 "$0: usage: $0 BUFFER" +if (( $# != 3 )) || [[ $1 == -* ]]; then + print -r -- >&2 "$0: usage: $0 BUFFER HIGHLIGHTER BASENAME" print -r -- >&2 "" - print -r -- >&2 "This tool generates a test file, suitable for highlighters/*/test-data/." + print -r -- >&2 "Generate highlighters/HIGHILGHTER/test-data/BASENAME.zsh based on the" + print -r -- >&2 "current highlighting of BUFFER." exit 1 fi buffer=$1 +ZSH_HIGHLIGHT_HIGHLIGHTERS=( $2 ) +exec >${0:A:h:h}/highlighters/$2/test-data/$3.zsh +git add -N ${0:A:h:h}/highlighters/$2/test-data/$3.zsh # Load the main script. . ${0:A:h:h}/zsh-syntax-highlighting.zsh From 9e569bb0fe04211d3ddb04ff73a88b03e390cf35 Mon Sep 17 00:00:00 2001 From: m0viefreak Date: Tue, 29 Mar 2016 21:50:46 +0200 Subject: [PATCH 16/66] driver: Widget binding: Support binding incomplete/nonexistent widgets --- zsh-syntax-highlighting.zsh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index e401956..84fe126 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -250,8 +250,10 @@ _zsh_highlight_bind_widgets() } # Override ZLE widgets to make them invoke _zsh_highlight. + local -U widgets_to_bind + widgets_to_bind=(${${(k)widgets}:#(.*|orig-*|run-help|which-command|beep|set-local-history|yank)}) local cur_widget - for cur_widget in ${${(k)widgets}:#(.*|orig-*|run-help|which-command|beep|set-local-history|yank)}; do + for cur_widget in $widgets_to_bind; do case $widgets[$cur_widget] in # Already rebound event: do nothing. @@ -277,8 +279,15 @@ _zsh_highlight_bind_widgets() builtin) eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget .${(q)cur_widget} -- \"\$@\" }" zle -N $cur_widget _zsh_highlight_widget_$cur_widget;; + # Incomplete or nonexistent widget: Bind to z-sy-h directly. + *) + if [[ $cur_widget == zle-* ]] && [[ -z $widgets[$cur_widget] ]]; then + _zsh_highlight_widget_${cur_widget}() { :; _zsh_highlight } + zle -N $cur_widget _zsh_highlight_widget_$cur_widget + else # Default: unhandled case. - *) print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" ;; + print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" + fi esac done } From 5bae6219008b2a8671c0de60476f50f6e0ff34ce Mon Sep 17 00:00:00 2001 From: m0viefreak Date: Tue, 29 Mar 2016 21:56:57 +0200 Subject: [PATCH 17/66] driver: Always bind zle-line-finish and use it instead of accept-* Special handling for cursor imprint or partial path highlighting is needed in more cases than accept-*. For example when accepting a line from isearch, no accept-* widget is invoked. The proper way is to use zle-line-finish. Trumps #259. Fixes #284. --- highlighters/cursor/cursor-highlighter.zsh | 7 +++---- highlighters/main/main-highlighter.zsh | 7 +++---- highlighters/main/test-data/path_prefix2.zsh | 2 +- zsh-syntax-highlighting.zsh | 6 ++++++ 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/highlighters/cursor/cursor-highlighter.zsh b/highlighters/cursor/cursor-highlighter.zsh index aa70f55..13479b9 100644 --- a/highlighters/cursor/cursor-highlighter.zsh +++ b/highlighters/cursor/cursor-highlighter.zsh @@ -34,15 +34,14 @@ # Whether the cursor highlighter should be called or not. _zsh_highlight_cursor_highlighter_predicate() { - # accept-* may trigger removal of cursor highlighting - [[ $WIDGET == accept-* ]] || - _zsh_highlight_cursor_moved + # remove cursor highlighting when the line is finished + [[ $WIDGET == zle-line-finish ]] || _zsh_highlight_cursor_moved } # Cursor highlighting function. _zsh_highlight_cursor_highlighter() { - [[ $WIDGET == accept-* ]] && return + [[ $WIDGET == zle-line-finish ]] && return _zsh_highlight_add_highlight $CURSOR $(( $CURSOR + 1 )) cursor } diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 7e62b90..701d0cc 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -62,9 +62,8 @@ # Whether the highlighter should be called or not. _zsh_highlight_main_highlighter_predicate() { - # accept-* may trigger removal of path_prefix highlighting - [[ $WIDGET == accept-* ]] || - _zsh_highlight_buffer_modified + # may need to remove path_prefix highlighting when the line ends + [[ $WIDGET == zle-line-finish ]] || _zsh_highlight_buffer_modified } # Helper to deal with tokens crossing line boundaries. @@ -596,7 +595,7 @@ _zsh_highlight_main_highlighter_check_path() # If this word ends the buffer, check if it's the prefix of a valid path. if [[ ${BUFFER[1]} != "-" && ${#BUFFER} == $end_pos ]] && - [[ $WIDGET != accept-* ]]; then + [[ $WIDGET != zle-line-finish ]]; then local -a tmp tmp=( ${expanded_path}*(N) ) (( $#tmp > 0 )) && REPLY=path_prefix && return 0 diff --git a/highlighters/main/test-data/path_prefix2.zsh b/highlighters/main/test-data/path_prefix2.zsh index ffe50cd..501928a 100644 --- a/highlighters/main/test-data/path_prefix2.zsh +++ b/highlighters/main/test-data/path_prefix2.zsh @@ -31,7 +31,7 @@ # Related to path_prefix.zsh BUFFER='ls /bin/s' -WIDGET=accept-line +WIDGET=zle-line-finish expected_region_highlight=( "4 9 default" # /bin/s diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 84fe126..80f5a4e 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -252,6 +252,12 @@ _zsh_highlight_bind_widgets() # Override ZLE widgets to make them invoke _zsh_highlight. local -U widgets_to_bind widgets_to_bind=(${${(k)widgets}:#(.*|orig-*|run-help|which-command|beep|set-local-history|yank)}) + + # Always wrap special zle-line-finish widget. This is needed to decide if the + # current line ends and special highlighting logic needs to be applied. + # E.g. remove cursor imprint, don't highlight partial paths, ... + widgets_to_bind+=(zle-line-finish) + local cur_widget for cur_widget in $widgets_to_bind; do case $widgets[$cur_widget] in From a8fe22d422515a46a4c2e968ff37762c577059bc Mon Sep 17 00:00:00 2001 From: m0viefreak Date: Tue, 29 Mar 2016 22:13:46 +0200 Subject: [PATCH 18/66] driver: Don't highlight in isearch zsh version 5.2 and lower don't support ISEARCHMATCH_ACTIVE and we are unable to re-apply zle_highlight on top. Therefore it is impossible to see the underlined matched area. Since that information is more important, completely disable highlighting in isearch in that case. To do that, we need to make sure we are actually called when something changes in isearch. Trumps #257. The FAQ entry presupposes #245 will be fixed (in time for the release) too. --- README.md | 18 ++++++++++++++++++ zsh-syntax-highlighting.zsh | 11 +++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index ac8beaf..7f0fe28 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,24 @@ custom widgets have been created (i.e., after all `zle -N` calls and after running `compinit`). Widgets created later will work, but will not update the syntax highlighting. +### Why does syntax highlighting not work while searching history? + +_This problem is fixed in zsh 5.3 and newer._ + +Highlighting the command line during an incremental history search +(with the `history-incremental-search-backward` widget, which is +bound by default to Ctrl+R in zsh's emacs keymap) requires zsh 5.3 +or newer. + +Under zsh 5.2 and older, the zsh-default underlining of the matched portion +of the buffer remains available, but zsh-syntax-highlighting's additional +highlighting is unavailable. (Those versions of zsh do not provide enough +information to allow computing the highlighting correctly.) + +See [issue #288][i288] for details. + +[i288]: https://github.com/zsh-users/zsh-syntax-highlighting/pull/288 + ### How are new releases announced? There is currently no "push" announcements channel. However, the following diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 80f5a4e..de3fea9 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -58,6 +58,13 @@ _zsh_highlight() # Store the previous command return code to restore it whatever happens. local ret=$? + # Remove all highlighting in isearch, so that only the underlining done by zsh itself remains. + # For details see FAQ entry 'Why does syntax highlighting not work while searching history?'. + if [[ $WIDGET == zle-isearch-update ]]; then + region_highlight=() + return $ret + fi + setopt localoptions warncreateglobal setopt localoptions noksharrays local REPLY # don't leak $REPLY into global scope @@ -258,6 +265,10 @@ _zsh_highlight_bind_widgets() # E.g. remove cursor imprint, don't highlight partial paths, ... widgets_to_bind+=(zle-line-finish) + # Always wrap special zle-isearch-update widget to be notified of updates in isearch. + # This is needed because we need to disable highlighting in that case. + widgets_to_bind+=(zle-isearch-update) + local cur_widget for cur_widget in $widgets_to_bind; do case $widgets[$cur_widget] in From 4ad311ec0a6837b7c83bcfd1a1bf12cceb03f363 Mon Sep 17 00:00:00 2001 From: m0viefreak Date: Sat, 30 Jul 2016 20:08:12 +0000 Subject: [PATCH 19/66] =?UTF-8?q?driver:=20Enable=20highlighting=20during?= =?UTF-8?q?=20isearch=20under=20zsh=E2=89=A55.3.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch causes a behaviour difference in the [i257] scenario: - Before this change, the zle_highlight[isearch] is applied and z-sy-h's highlighting isn't. - With this change, both zle_highlight[isearch] and z-sy-h's highlighting are applied, so «echo foo» renders the first word in green underline (fg=green from ZSH_HIGHLIGHT_STYLES[builtin], underline from zle_highlight[isearch]). This patch causes the presuppositional FAQ entry added in a8fe22d422515a46a4c2e968ff37762c577059bc to be correct. This is part of #261, of which #288 was a spin-off. [i257] https://github.com/zsh-users/zsh-syntax-highlighting/pull/257#issuecomment-168394665 --- zsh-syntax-highlighting.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index de3fea9..7879216 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -60,7 +60,7 @@ _zsh_highlight() # Remove all highlighting in isearch, so that only the underlining done by zsh itself remains. # For details see FAQ entry 'Why does syntax highlighting not work while searching history?'. - if [[ $WIDGET == zle-isearch-update ]]; then + if [[ $WIDGET == zle-isearch-update ]] && ! (( $+ISEARCHMATCH_ACTIVE )); then region_highlight=() return $ret fi From e2f863c151c395023dd2b0f3a6f8d3bd1a6d8470 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 11 Aug 2016 19:37:37 +0000 Subject: [PATCH 20/66] minor: Fix typo in development usage message. --- tests/generate.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/generate.zsh b/tests/generate.zsh index 5bd0b60..3eee487 100755 --- a/tests/generate.zsh +++ b/tests/generate.zsh @@ -34,7 +34,7 @@ emulate -LR zsh if (( $# != 3 )) || [[ $1 == -* ]]; then print -r -- >&2 "$0: usage: $0 BUFFER HIGHLIGHTER BASENAME" print -r -- >&2 "" - print -r -- >&2 "Generate highlighters/HIGHILGHTER/test-data/BASENAME.zsh based on the" + print -r -- >&2 "Generate highlighters/HIGHLIGHTER/test-data/BASENAME.zsh based on the" print -r -- >&2 "current highlighting of BUFFER." exit 1 fi From d711563fe1bf8fa6810bc34ac92a2fd3150290ed Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Aug 2016 09:17:59 +0000 Subject: [PATCH 21/66] driver: Make it reentrant. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes an infinite recursion in zsh without zle-line-pre-redraw [≤5.2] in the following situation: % source zsh-syntax-highlighting.zsh % eval "my-self-insert() { zle -M 'foobar'; ${(q)widgets[self-insert]#*:} \"\$@\" }" % zle -N self-insert my-self-insert % source zsh-syntax-highlighting.zsh Fixes #305. --- zsh-syntax-highlighting.zsh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 7879216..27da031 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -249,6 +249,7 @@ _zsh_highlight_call_widget() _zsh_highlight_bind_widgets() { setopt localoptions noksharrays + local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once # Load ZSH module zsh/zleparameter, needed to override user defined widgets. zmodload zsh/zleparameter 2>/dev/null || { @@ -258,7 +259,7 @@ _zsh_highlight_bind_widgets() # Override ZLE widgets to make them invoke _zsh_highlight. local -U widgets_to_bind - widgets_to_bind=(${${(k)widgets}:#(.*|orig-*|run-help|which-command|beep|set-local-history|yank)}) + widgets_to_bind=(${${(k)widgets}:#(.*|run-help|which-command|beep|set-local-history|yank)}) # Always wrap special zle-line-finish widget. This is needed to decide if the # current line ends and special highlighting logic needs to be applied. @@ -283,13 +284,13 @@ _zsh_highlight_bind_widgets() # NO_function_argzero, regardless of the option's setting here. # User defined widget: override and rebind old one with prefix "orig-". - user:*) zle -N orig-$cur_widget ${widgets[$cur_widget]#*:} - eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget orig-${(q)cur_widget} -- \"\$@\" }" + user:*) zle -N $prefix-$cur_widget ${widgets[$cur_widget]#*:} + eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }" zle -N $cur_widget _zsh_highlight_widget_$cur_widget;; # Completion widget: override and rebind old one with prefix "orig-". - completion:*) zle -C orig-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]} - eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget orig-${(q)cur_widget} -- \"\$@\" }" + completion:*) zle -C $prefix-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]} + eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }" zle -N $cur_widget _zsh_highlight_widget_$cur_widget;; # Builtin widget: override and make it call the builtin ".widget". From 295d62ec888d0d43579abad4e4245aede8f2cc85 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Aug 2016 09:43:54 +0000 Subject: [PATCH 22/66] driver: Followup to last: make the value more unique. Part of issue #305. --- zsh-syntax-highlighting.zsh | 1 + 1 file changed, 1 insertion(+) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 27da031..5e13253 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -249,6 +249,7 @@ _zsh_highlight_call_widget() _zsh_highlight_bind_widgets() { setopt localoptions noksharrays + typeset -F SECONDS local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once # Load ZSH module zsh/zleparameter, needed to override user defined widgets. From d1c773faa7e86b76657cd39bbb1dcb5db8f5c017 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 13:00:55 +0000 Subject: [PATCH 23/66] noop: Fix indentation. --- highlighters/main/main-highlighter.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 701d0cc..7d652eb 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -348,9 +348,9 @@ _zsh_highlight_main_highlighter() next_word+=':sudo_opt:' next_word+=':start:' fi - fi + fi - if [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word + if [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then style=precommand elif [[ "$arg" = "sudo" ]]; then From 6e2ef574c89fa7e0f0e3ddc8934020be722e1d59 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 13:25:43 +0000 Subject: [PATCH 24/66] noop: Restructure code for clarity. The structure now mirrors the stall construct at the top of the loop. --- highlighters/main/main-highlighter.zsh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 7d652eb..47e43e7 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -546,7 +546,12 @@ _zsh_highlight_main_highlighter() this_word=':start::regular:' fi start_pos=$end_pos - (( in_redirection == 0 )) && this_word=$next_word + if (( in_redirection == 0 )); then + # This is the default/common codepath. + this_word=$next_word + else + # Stall $this_word. + fi done } From 179b8e753fa572f2d42003954b04c23cdc63d646 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 13:27:36 +0000 Subject: [PATCH 25/66] 'main': Test for redirection earlier. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a prerequisite for the next commit. The incumbent code was wrong: the test of $in_redirection in the first hunk would never have seen «(( in_redirection == 2 ))». That had no visible effect since options to sudo don't look like redirection operators. --- highlighters/main/main-highlighter.zsh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 47e43e7..ade7907 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -331,6 +331,11 @@ _zsh_highlight_main_highlighter() continue fi + if _zsh_highlight_main__is_redirection $arg ; then + # A '<' or '>', possibly followed by a digit + in_redirection=2 + fi + # Parse the sudo command line if (( ! in_redirection )); then if [[ $this_word == *':sudo_opt:'* ]]; then @@ -432,10 +437,8 @@ _zsh_highlight_main_highlighter() else style=unknown-token fi - elif _zsh_highlight_main__is_redirection $arg; then - # A '<' or '>', possibly followed by a digit + elif (( in_redirection == 2 )); then style=redirection - (( in_redirection=2 )) elif [[ $arg[1,2] == '((' ]]; then # Arithmetic evaluation. # @@ -509,9 +512,8 @@ _zsh_highlight_main_highlighter() else style=unknown-token fi - elif _zsh_highlight_main__is_redirection $arg; then + elif (( in_redirection == 2 )); then style=redirection - (( in_redirection=2 )) else if _zsh_highlight_main_highlighter_check_path; then style=$REPLY From 757d047f09cda75a74ce62e198504893335f9ad5 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 13:31:05 +0000 Subject: [PATCH 26/66] 'main': The word after 'sudo' is only a non-command word if it is an option. --- highlighters/main/main-highlighter.zsh | 3 +++ highlighters/main/test-data/sudo-command.zsh | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index ade7907..02d7225 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -338,6 +338,9 @@ _zsh_highlight_main_highlighter() # Parse the sudo command line if (( ! in_redirection )); then + if [[ $this_word == *':sudo_opt:'* ]] && [[ $arg != -* ]]; then + this_word=${this_word//:sudo_opt:/} + fi if [[ $this_word == *':sudo_opt:'* ]]; then case "$arg" in # Flag that requires an argument diff --git a/highlighters/main/test-data/sudo-command.zsh b/highlighters/main/test-data/sudo-command.zsh index 4f8909e..3183a23 100644 --- a/highlighters/main/test-data/sudo-command.zsh +++ b/highlighters/main/test-data/sudo-command.zsh @@ -31,7 +31,7 @@ # * -i (no argument) # * -C3 (pasted argument) # * -u otheruser (non-pasted argument) -BUFFER='sudo -C3 -u otheruser -i ls /; sudo ; sudo -u ;' +BUFFER='sudo -C3 -u otheruser -i ls /; sudo ; sudo -u ; sudo notacommand' expected_region_highlight=( "1 4 precommand" # sudo @@ -43,4 +43,5 @@ expected_region_highlight=( "29 29 path" # / "37 37 unknown-token" # ;, error because empty command "47 47 unknown-token" # ;, error because incomplete command + "54 64 unknown-token" # notacommand - doesn't falls back to "not a command word" codepath ) From 2c002f9f89bd3b127577ee1247b89b1a02af6234 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 13:40:31 +0000 Subject: [PATCH 27/66] noop: Add comments. --- highlighters/main/main-highlighter.zsh | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 02d7225..a810eff 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -258,6 +258,7 @@ _zsh_highlight_main_highlighter() local proc_buf="$buf" for arg in ${interactive_comments-${(z)buf}} \ ${interactive_comments+${(zZ+c+)buf}}; do + # Initialize $next_word. if (( in_redirection )); then (( --in_redirection )) fi @@ -267,6 +268,14 @@ _zsh_highlight_main_highlighter() else # Stall $next_word. fi + + # Initialize per-"simple command" [zshmisc(1)] variables: + # + # $already_added (see next paragraph) + # $style how to highlight $arg + # $in_array_assignment boolean flag for "between '(' and ')' of array assignment" + # $highlight_glob boolean flag for "'noglob' is in effect" + # # $already_added is set to 1 to disable adding an entry to region_highlight # for this iteration. Currently, that is done for "" and $'' strings, # which add the entry early so escape sequences within the string override @@ -280,7 +289,7 @@ _zsh_highlight_main_highlighter() fi fi - # advance $start_pos, skipping over whitespace in $buf. + # Compute the new $start_pos and $end_pos, skipping over whitespace in $buf. if [[ $arg == ';' ]] ; then # We're looking for either a semicolon or a newline, whichever comes # first. Both of these are rendered as a ";" (SEPER) by the ${(z)..} @@ -300,8 +309,7 @@ _zsh_highlight_main_highlighter() ((end_pos=$start_pos+${#arg})) fi - # Above `if` computes new start_pos and end_pos. - # Here we compute new proc_buf. We advance it + # Compute the new $proc_buf. We advance it # (chop off characters from the beginning) # beyond what end_pos points to, by skipping # as many characters as end_pos was advanced. @@ -320,6 +328,9 @@ _zsh_highlight_main_highlighter() # Why [,-1] is slower than [,length] isn't clear. proc_buf="${proc_buf[offset + $#arg + 1,len]}" + # Handle the INTERACTIVE_COMMENTS option. + # + # We use the (Z+c+) flag so the entire comment is presented as one token in $arg. if [[ -n ${interactive_comments+'set'} && $arg[1] == $histchars[3] ]]; then if [[ $this_word == *(':regular:'|':start:')* ]]; then style=comment @@ -331,16 +342,21 @@ _zsh_highlight_main_highlighter() continue fi + # Analyse the current word. if _zsh_highlight_main__is_redirection $arg ; then # A '<' or '>', possibly followed by a digit in_redirection=2 fi - # Parse the sudo command line + # Special-case the first word after 'sudo'. if (( ! in_redirection )); then if [[ $this_word == *':sudo_opt:'* ]] && [[ $arg != -* ]]; then this_word=${this_word//:sudo_opt:/} fi + fi + + # Parse the sudo command line + if (( ! in_redirection )); then if [[ $this_word == *':sudo_opt:'* ]]; then case "$arg" in # Flag that requires an argument @@ -358,6 +374,7 @@ _zsh_highlight_main_highlighter() fi fi + # The Great Fork: is this a command word? Is this a non-command word? if [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then style=precommand From b7bb4f86575496918602ace125a9718fedae7b68 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 17:08:47 +0000 Subject: [PATCH 28/66] README: Rephrase a question non-negatively^W neutrally. --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 7f0fe28..f5521c6 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,7 @@ custom widgets have been created (i.e., after all `zle -N` calls and after running `compinit`). Widgets created later will work, but will not update the syntax highlighting. -### Why does syntax highlighting not work while searching history? - -_This problem is fixed in zsh 5.3 and newer._ +### Does syntax highlighting work during incremental history search? Highlighting the command line during an incremental history search (with the `history-incremental-search-backward` widget, which is From d1e0defceb2330b18d794ad5cc24c87984cbdb95 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 17:09:04 +0000 Subject: [PATCH 29/66] README: Add a reference to upstream's documentation. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f5521c6..9e045a8 100644 --- a/README.md +++ b/README.md @@ -39,13 +39,14 @@ Highlighting the command line during an incremental history search bound by default to Ctrl+R in zsh's emacs keymap) requires zsh 5.3 or newer. -Under zsh 5.2 and older, the zsh-default underlining of the matched portion +Under zsh 5.2 and older, the zsh-default [underlining][zshzle-Character-Highlighting] of the matched portion of the buffer remains available, but zsh-syntax-highlighting's additional highlighting is unavailable. (Those versions of zsh do not provide enough information to allow computing the highlighting correctly.) See [issue #288][i288] for details. +[zshzle-Character-Highlighting]: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting [i288]: https://github.com/zsh-users/zsh-syntax-highlighting/pull/288 ### How are new releases announced? From b2ba91f12f7e88170adc549e978c400c9c7d6a3e Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 17:09:42 +0000 Subject: [PATCH 30/66] noop: Rewrap. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9e045a8..4558463 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,10 @@ Highlighting the command line during an incremental history search bound by default to Ctrl+R in zsh's emacs keymap) requires zsh 5.3 or newer. -Under zsh 5.2 and older, the zsh-default [underlining][zshzle-Character-Highlighting] of the matched portion -of the buffer remains available, but zsh-syntax-highlighting's additional -highlighting is unavailable. (Those versions of zsh do not provide enough -information to allow computing the highlighting correctly.) +Under zsh 5.2 and older, the zsh-default [underlining][zshzle-Character-Highlighting] +of the matched portion of the buffer remains available, but zsh-syntax-highlighting's +additional highlighting is unavailable. (Those versions of zsh do not provide +enough information to allow computing the highlighting correctly.) See [issue #288][i288] for details. From f3242cbd6aba306d4423991ac738fd430c4723ec Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 17:22:39 +0000 Subject: [PATCH 31/66] driver: Followup to d711563fe1bf8fa6810bc34ac92a2fd3150290ed: actually make the driver reentrant. Re-fixes #305. --- zsh-syntax-highlighting.zsh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 5e13253..98e640e 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -286,17 +286,17 @@ _zsh_highlight_bind_widgets() # User defined widget: override and rebind old one with prefix "orig-". user:*) zle -N $prefix-$cur_widget ${widgets[$cur_widget]#*:} - eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }" - zle -N $cur_widget _zsh_highlight_widget_$cur_widget;; + eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }" + zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;; # Completion widget: override and rebind old one with prefix "orig-". completion:*) zle -C $prefix-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]} - eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }" - zle -N $cur_widget _zsh_highlight_widget_$cur_widget;; + eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }" + zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;; # Builtin widget: override and make it call the builtin ".widget". - builtin) eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget .${(q)cur_widget} -- \"\$@\" }" - zle -N $cur_widget _zsh_highlight_widget_$cur_widget;; + builtin) eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget .${(q)cur_widget} -- \"\$@\" }" + zle -N $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;; # Incomplete or nonexistent widget: Bind to z-sy-h directly. *) From f91a7b885e7ddbf3c27d48c300cb0de83ae5cab6 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 17:49:18 +0000 Subject: [PATCH 32/66] driver: Followup to 80148f6c840299f0980f4359ec5307ca63837dff: don't squat on the highlighters' namespace. --- zsh-syntax-highlighting.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 98e640e..66a42f8 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -88,7 +88,7 @@ _zsh_highlight() local highlighter; for highlighter in $ZSH_HIGHLIGHT_HIGHLIGHTERS; do # eval cache place for current highlighter and prepare it - cache_place="_zsh_highlight_highlighter_${highlighter}_cache" + cache_place="_zsh_highlight__highlighter_${highlighter}_cache" typeset -ga ${cache_place} # If highlighter needs to be invoked From a3d5dfcbdae9cea58aa703efe79192927a34e713 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 12 Jul 2016 07:15:04 +0000 Subject: [PATCH 33/66] driver: Rename highlighter entry points This updates the docs and the driver, in a manner backwards compatible with existing highlighters. (None of the highlighters are touched by this change, yet tests continue to pass.) Part of issue #329. --- docs/highlighters.md | 18 ++++++++++++++---- zsh-syntax-highlighting.zsh | 29 +++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/docs/highlighters.md b/docs/highlighters.md index 2659645..c0f79bc 100644 --- a/docs/highlighters.md +++ b/docs/highlighters.md @@ -54,16 +54,16 @@ To create your own `acme` highlighter: * Create your script at `highlighters/acme/acme-highlighter.zsh`. -* Implement the `_zsh_highlight_acme_highlighter_predicate` function. +* Implement the `_zsh_highlight_highlighter_acme_predicate` function. This function must return 0 when the highlighter needs to be called and non-zero otherwise, for example: - _zsh_highlight_acme_highlighter_predicate() { + _zsh_highlight_highlighter_acme_predicate() { # Call this highlighter in SVN working copies [[ -d .svn ]] } -* Implement the `_zsh_highlight_acme_highlighter` function. +* Implement the `_zsh_highlight_highlighter_acme_paint` function. This function does the actual syntax highlighting, by calling `_zsh_highlight_add_highlight` with the start and end of the region to be highlighted and the `ZSH_HIGHLIGHT_STYLES` key to use. Define the default @@ -73,13 +73,23 @@ To create your own `acme` highlighter: : ${ZSH_HIGHLIGHT_STYLES[acme:aurora]:=fg=green} - _zsh_highlight_acme_highlighter() { + _zsh_highlight_highlighter_acme_paint() { # Colorize the whole buffer with the 'aurora' style _zsh_highlight_add_highlight 0 $#BUFFER acme:aurora } * Name your own functions and global variables `_zsh_highlight_acme_*`. + - In zsh-syntax-highlighting 0.4.0 and earlier, the entrypoints + `_zsh_highlight_highlighter_acme_predicate` and + `_zsh_highlight_highlighter_acme_paint` + were named + `_zsh_highlight_acme_highlighter_predicate` and + `_zsh_highlight_highlighter_acme_paint` respectively. + + These names are still supported for backwards compatibility; + however, support for them will be removed in a a future major or minor release (v0.x.0 or v1.0.0). + * Activate your highlighter in `~/.zshrc`: ZSH_HIGHLIGHT_HIGHLIGHTERS+=(acme) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index e401956..9462c2a 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -85,7 +85,7 @@ _zsh_highlight() typeset -ga ${cache_place} # If highlighter needs to be invoked - if "_zsh_highlight_${highlighter}_highlighter_predicate"; then + if "_zsh_highlight_highlighter_${highlighter}_predicate"; then # save a copy, and cleanup region_highlight region_highlight_copy=("${region_highlight[@]}") @@ -93,7 +93,7 @@ _zsh_highlight() # Execute highlighter and save result { - "_zsh_highlight_${highlighter}_highlighter" + "_zsh_highlight_highlighter_${highlighter}_paint" } always { eval "${cache_place}=(\"\${region_highlight[@]}\")" } @@ -301,13 +301,26 @@ _zsh_highlight_load_highlighters() local highlighter highlighter_dir for highlighter_dir ($1/*/); do highlighter="${highlighter_dir:t}" - [[ -f "$highlighter_dir/${highlighter}-highlighter.zsh" ]] && { + [[ -f "$highlighter_dir/${highlighter}-highlighter.zsh" ]] && . "$highlighter_dir/${highlighter}-highlighter.zsh" - type "_zsh_highlight_${highlighter}_highlighter" &> /dev/null && - type "_zsh_highlight_${highlighter}_highlighter_predicate" &> /dev/null || { - print -r -- >&2 "zsh-syntax-highlighting: '${highlighter}' highlighter should define both required functions '_zsh_highlight_${highlighter}_highlighter' and '_zsh_highlight_${highlighter}_highlighter_predicate' in '${highlighter_dir}/${highlighter}-highlighter.zsh'." - } - } + if type "_zsh_highlight_highlighter_${highlighter}_paint" &> /dev/null && + type "_zsh_highlight_highlighter_${highlighter}_predicate" &> /dev/null; + then + # New (0.5.0) function names + elif type "_zsh_highlight_${highlighter}_highlighter" &> /dev/null && + type "_zsh_highlight_${highlighter}_highlighter_predicate" &> /dev/null; + then + # Old (0.4.x) function names + if false; then + # TODO: only show this warning for plugin authors/maintainers, not for end users + print -r -- >&2 "zsh-syntax-highlighting: warning: ${(qq)highlighter} highlighter uses deprecated entry point names; please ask its maintainer to update it: https://github.com/zsh-users/zsh-syntax-highlighting/issues/329" + fi + # Make it work. + eval "_zsh_highlight_highlighter_${(q)highlighter}_paint() { _zsh_highlight_${(q)highlighter}_highlighter \"\$@\" }" + eval "_zsh_highlight_highlighter_${(q)highlighter}_predicate() { _zsh_highlight_${(q)highlighter}_highlighter_predicate \"\$@\" }" + else + print -r -- >&2 "zsh-syntax-highlighting: '${highlighter}' highlighter should define both required functions '_zsh_highlight_highlighter_${highlighter}_paint' and '_zsh_highlight_highlighter_${highlighter}_predicate' in '${highlighter_dir}/${highlighter}-highlighter.zsh'." + fi done } From c793e0dceab1571ff02078ed764d294326922a4f Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 21 Jul 2016 03:33:28 +0000 Subject: [PATCH 34/66] highlighters: Rename entry points. This tracks the API change made in the previous commit, as suggested in the (#if 0'd) deprecation warning. --- highlighters/brackets/brackets-highlighter.zsh | 4 ++-- highlighters/cursor/cursor-highlighter.zsh | 4 ++-- highlighters/line/line-highlighter.zsh | 4 ++-- highlighters/main/main-highlighter.zsh | 6 +++--- highlighters/pattern/pattern-highlighter.zsh | 4 ++-- highlighters/root/root-highlighter.zsh | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index ce52309..2a5f396 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -38,13 +38,13 @@ : ${ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket]:=standout} # Whether the brackets highlighter should be called or not. -_zsh_highlight_brackets_highlighter_predicate() +_zsh_highlight_highlighter_brackets_predicate() { _zsh_highlight_cursor_moved || _zsh_highlight_buffer_modified } # Brackets highlighting function. -_zsh_highlight_brackets_highlighter() +_zsh_highlight_highlighter_brackets_paint() { local char style local -i bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]} buflen=${#BUFFER} level=0 matchingpos pos diff --git a/highlighters/cursor/cursor-highlighter.zsh b/highlighters/cursor/cursor-highlighter.zsh index aa70f55..6806aa2 100644 --- a/highlighters/cursor/cursor-highlighter.zsh +++ b/highlighters/cursor/cursor-highlighter.zsh @@ -32,7 +32,7 @@ : ${ZSH_HIGHLIGHT_STYLES[cursor]:=standout} # Whether the cursor highlighter should be called or not. -_zsh_highlight_cursor_highlighter_predicate() +_zsh_highlight_highlighter_cursor_predicate() { # accept-* may trigger removal of cursor highlighting [[ $WIDGET == accept-* ]] || @@ -40,7 +40,7 @@ _zsh_highlight_cursor_highlighter_predicate() } # Cursor highlighting function. -_zsh_highlight_cursor_highlighter() +_zsh_highlight_highlighter_cursor_paint() { [[ $WIDGET == accept-* ]] && return diff --git a/highlighters/line/line-highlighter.zsh b/highlighters/line/line-highlighter.zsh index 2da55ea..f922dc9 100644 --- a/highlighters/line/line-highlighter.zsh +++ b/highlighters/line/line-highlighter.zsh @@ -32,13 +32,13 @@ : ${ZSH_HIGHLIGHT_STYLES[line]:=} # Whether the root highlighter should be called or not. -_zsh_highlight_line_highlighter_predicate() +_zsh_highlight_highlighter_line_predicate() { _zsh_highlight_buffer_modified } # root highlighting function. -_zsh_highlight_line_highlighter() +_zsh_highlight_highlighter_line_paint() { _zsh_highlight_add_highlight 0 $#BUFFER line } diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index abe4d38..f790a2a 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -60,7 +60,7 @@ : ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold} # Whether the highlighter should be called or not. -_zsh_highlight_main_highlighter_predicate() +_zsh_highlight_highlighter_main_predicate() { # accept-* may trigger removal of path_prefix highlighting [[ $WIDGET == accept-* ]] || @@ -150,7 +150,7 @@ _zsh_highlight_main__resolve_alias() { } # Main syntax highlighting function. -_zsh_highlight_main_highlighter() +_zsh_highlight_highlighter_main_paint() { ## Before we even 'emulate -L', we must test a few options that would reset. if [[ -o interactive_comments ]]; then @@ -166,7 +166,7 @@ _zsh_highlight_main_highlighter() # At the PS3 prompt and in vared, highlight nothing. # - # (We can't check this in _zsh_highlight_main_highlighter_predicate because + # (We can't check this in _zsh_highlight_highlighter_main_predicate because # if the predicate returns false, the previous value of region_highlight # would be reused.) if [[ $CONTEXT == (select|vared) ]]; then diff --git a/highlighters/pattern/pattern-highlighter.zsh b/highlighters/pattern/pattern-highlighter.zsh index 4e2eabd..054eff7 100644 --- a/highlighters/pattern/pattern-highlighter.zsh +++ b/highlighters/pattern/pattern-highlighter.zsh @@ -32,13 +32,13 @@ typeset -gA ZSH_HIGHLIGHT_PATTERNS # Whether the pattern highlighter should be called or not. -_zsh_highlight_pattern_highlighter_predicate() +_zsh_highlight_highlighter_pattern_predicate() { _zsh_highlight_buffer_modified } # Pattern syntax highlighting function. -_zsh_highlight_pattern_highlighter() +_zsh_highlight_highlighter_pattern_paint() { setopt localoptions extendedglob local pattern diff --git a/highlighters/root/root-highlighter.zsh b/highlighters/root/root-highlighter.zsh index ede9769..3718c44 100644 --- a/highlighters/root/root-highlighter.zsh +++ b/highlighters/root/root-highlighter.zsh @@ -32,13 +32,13 @@ : ${ZSH_HIGHLIGHT_STYLES[root]:=standout} # Whether the root highlighter should be called or not. -_zsh_highlight_root_highlighter_predicate() +_zsh_highlight_highlighter_root_predicate() { _zsh_highlight_buffer_modified } # root highlighting function. -_zsh_highlight_root_highlighter() +_zsh_highlight_highlighter_root_paint() { if (( EUID == 0 )) { _zsh_highlight_add_highlight 0 $#BUFFER root } } From 11c908196700c00c4662295f44747a5826615c4b Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 19:01:25 +0000 Subject: [PATCH 35/66] *: error messages: Fix quoting. --- zsh-syntax-highlighting.zsh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index dad3800..9de8d61 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -305,7 +305,7 @@ _zsh_highlight_bind_widgets() zle -N $cur_widget _zsh_highlight_widget_$cur_widget else # Default: unhandled case. - print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" + print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget ${(qq)cur_widget}" fi esac done @@ -321,7 +321,7 @@ _zsh_highlight_load_highlighters() # Check the directory exists. [[ -d "$1" ]] || { - print -r -- >&2 "zsh-syntax-highlighting: highlighters directory '$1' not found." + print -r -- >&2 "zsh-syntax-highlighting: highlighters directory ${(qq)1} not found." return 1 } @@ -347,7 +347,7 @@ _zsh_highlight_load_highlighters() eval "_zsh_highlight_highlighter_${(q)highlighter}_paint() { _zsh_highlight_${(q)highlighter}_highlighter \"\$@\" }" eval "_zsh_highlight_highlighter_${(q)highlighter}_predicate() { _zsh_highlight_${(q)highlighter}_highlighter_predicate \"\$@\" }" else - print -r -- >&2 "zsh-syntax-highlighting: '${highlighter}' highlighter should define both required functions '_zsh_highlight_highlighter_${highlighter}_paint' and '_zsh_highlight_highlighter_${highlighter}_predicate' in '${highlighter_dir}/${highlighter}-highlighter.zsh'." + print -r -- >&2 "zsh-syntax-highlighting: ${(qq)highlighter} highlighter should define both required functions '_zsh_highlight_highlighter_${highlighter}_paint' and '_zsh_highlight_highlighter_${highlighter}_predicate' in ${(qq):-"$highlighter_dir/${highlighter}-highlighter.zsh"}." fi done } From 51614ca2c994486dc0a41ec0737a97f0af16a505 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 30 Aug 2016 02:56:23 +0000 Subject: [PATCH 36/66] 'main': Avoid triggering a zsh bug related to hashed commands. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This manifested in completion of the form «./foo» where there happened to be a program called 'foo' in $PATH. Fixes #354. Closes #355. --- highlighters/main/main-highlighter.zsh | 9 ++++++++- zsh-syntax-highlighting.zsh | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index b120bf2..f6194cf 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -113,7 +113,14 @@ _zsh_highlight_main__type() { REPLY=builtin elif (( $+commands[(e)$1] )); then REPLY=command - elif ! builtin type -w -- $1 >/dev/null 2>&1; then + # zsh 5.2 and older have a bug whereby running 'type -w ./sudo' implicitly + # runs 'hash ./sudo=/usr/local/bin/./sudo' (assuming /usr/local/bin/sudo + # exists and is in $PATH). Avoid triggering the bug, at the expense of + # falling through to the $() below, incurring a fork. (Issue #354.) + # + # The second disjunct mimics the isrelative() C call from the zsh bug. + elif { is-at-least 5.3 || [[ $1 != */* ]] } && + ! builtin type -w -- $1 >/dev/null 2>&1; then REPLY=none fi fi diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 9de8d61..c20751b 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -383,5 +383,7 @@ add-zsh-hook preexec _zsh_highlight_preexec_hook 2>/dev/null || { # Load zsh/parameter module if available zmodload zsh/parameter 2>/dev/null || true +autoload -U is-at-least + # Initialize the array of active highlighters if needed. [[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && ZSH_HIGHLIGHT_HIGHLIGHTERS=(main) || true From da60234fb236ccd2b199ffa8a157014dacf4e591 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Wed, 31 Aug 2016 14:08:16 +0000 Subject: [PATCH 37/66] driver: Declare global variables This caused warnings with `setopt warn_create_global`. --- zsh-syntax-highlighting.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index c20751b..f9d82de 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -32,8 +32,8 @@ 0=${(%):-%N} if true; then # $0 is reliable - ZSH_HIGHLIGHT_VERSION=$(<"${0:A:h}"/.version) - ZSH_HIGHLIGHT_REVISION=$(<"${0:A:h}"/.revision-hash) + typeset -g ZSH_HIGHLIGHT_VERSION=$(<"${0:A:h}"/.version) + typeset -g ZSH_HIGHLIGHT_REVISION=$(<"${0:A:h}"/.revision-hash) if [[ $ZSH_HIGHLIGHT_REVISION == \$Format:* ]]; then # When running from a source tree without 'make install', $ZSH_HIGHLIGHT_REVISION # would be set to '$Format:%H$' literally. That's an invalid value, and obtaining From 4c4baede519a20805482161d175188ddf34975dc Mon Sep 17 00:00:00 2001 From: m0viefreak Date: Thu, 8 Sep 2016 14:56:24 +0200 Subject: [PATCH 38/66] 'brackets': Don't highlight corresponding bracket on accept-line --- .../brackets/brackets-highlighter.zsh | 14 ++++--- .../cursor-matchingbracket-line-finish.zsh | 37 +++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 highlighters/brackets/test-data/cursor-matchingbracket-line-finish.zsh diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 2a5f396..1bdd1f9 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -40,7 +40,7 @@ # Whether the brackets highlighter should be called or not. _zsh_highlight_highlighter_brackets_predicate() { - _zsh_highlight_cursor_moved || _zsh_highlight_buffer_modified + [[ $WIDGET == zle-line-finish ]] || _zsh_highlight_cursor_moved || _zsh_highlight_buffer_modified } # Brackets highlighting function. @@ -85,11 +85,13 @@ _zsh_highlight_highlighter_brackets_paint() _zsh_highlight_add_highlight $((pos - 1)) $pos $style done - # If cursor is on a bracket, then highlight corresponding bracket, if any - pos=$((CURSOR + 1)) - if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then - local -i otherpos=$matching[$pos] - _zsh_highlight_add_highlight $((otherpos - 1)) $otherpos cursor-matchingbracket + # If cursor is on a bracket, then highlight corresponding bracket, if any. + if [[ $WIDGET != zle-line-finish ]]; then + pos=$((CURSOR + 1)) + if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then + local -i otherpos=$matching[$pos] + _zsh_highlight_add_highlight $((otherpos - 1)) $otherpos cursor-matchingbracket + fi fi } diff --git a/highlighters/brackets/test-data/cursor-matchingbracket-line-finish.zsh b/highlighters/brackets/test-data/cursor-matchingbracket-line-finish.zsh new file mode 100644 index 0000000..23b317b --- /dev/null +++ b/highlighters/brackets/test-data/cursor-matchingbracket-line-finish.zsh @@ -0,0 +1,37 @@ +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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 +# ------------------------------------------------------------------------------------------------- + +WIDGET=zle-line-finish + +BUFFER=': $foo[bar]' +CURSOR=6 # cursor is zero-based + +expected_region_highlight=( + "11 11 NONE" +) From fdaeec45146b5fe6b2c2b01da6f97681d89f8094 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 8 Sep 2016 19:09:29 +0000 Subject: [PATCH 39/66] 'main': Followup to 51614ca2c994: Run cheaper conditions first. This was suggested on #355. --- highlighters/main/main-highlighter.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index f6194cf..6df671e 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -119,7 +119,7 @@ _zsh_highlight_main__type() { # falling through to the $() below, incurring a fork. (Issue #354.) # # The second disjunct mimics the isrelative() C call from the zsh bug. - elif { is-at-least 5.3 || [[ $1 != */* ]] } && + elif { [[ $1 != */* ]] || is-at-least 5.3 } && ! builtin type -w -- $1 >/dev/null 2>&1; then REPLY=none fi From a8a6384356af7f3ad718acaccbc77128d166eacf Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 22 Sep 2016 03:27:32 +0000 Subject: [PATCH 40/66] 'main': Add tests for the IGNORE_BRACES option. Also adds an XFail test for balanced braces for issue #344. --- highlighters/main/test-data/braces1.zsh | 42 +++++++++++++++++++++++++ highlighters/main/test-data/braces2.zsh | 42 +++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 highlighters/main/test-data/braces1.zsh create mode 100644 highlighters/main/test-data/braces2.zsh diff --git a/highlighters/main/test-data/braces1.zsh b/highlighters/main/test-data/braces1.zsh new file mode 100644 index 0000000..858ade5 --- /dev/null +++ b/highlighters/main/test-data/braces1.zsh @@ -0,0 +1,42 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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=$'() { echo }\n}' +# no special setopts + +expected_region_highlight=( + '1 2 reserved-word' # () + '4 4 reserved-word' # { + '6 9 builtin' # echo + '11 11 reserved-word' # } + '12 12 commandseparator' # \n + '13 13 unknown-token "issue #344 (balanced parentheses/braces)"' # } +) diff --git a/highlighters/main/test-data/braces2.zsh b/highlighters/main/test-data/braces2.zsh new file mode 100644 index 0000000..89ba15a --- /dev/null +++ b/highlighters/main/test-data/braces2.zsh @@ -0,0 +1,42 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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=$'() { echo }\n}' +setopt ignorebraces + +expected_region_highlight=( + '1 2 reserved-word' # () + '4 4 reserved-word' # { + '6 9 builtin' # echo + '11 11 default "ignore_braces; fixed by next commit"' # } + '12 12 commandseparator' # \n + '13 13 reserved-word' # } +) From 02807f1826a512672764a8658de3b8a67f074c44 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 22 Sep 2016 03:38:34 +0000 Subject: [PATCH 41/66] 'main': Support the IGNORE_BRACES option. This is related to a future "unbalanced { ( ) }" check for issue #344. --- highlighters/main/main-highlighter.zsh | 20 +++++++++++++++++++- highlighters/main/test-data/braces2.zsh | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 6df671e..a010b22 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -162,6 +162,11 @@ _zsh_highlight_highlighter_main_paint() if [[ -o interactive_comments ]]; then local interactive_comments= # set to empty fi + if [[ -o ignore_braces ]] || [[ -o ignore_close_braces ]]; then + local right_brace_is_recognised_everywhere=false + else + local right_brace_is_recognised_everywhere=true + fi if [[ -o path_dirs ]]; then integer path_dirs_was_set=1 else @@ -513,7 +518,18 @@ _zsh_highlight_highlighter_main_paint() else style=reserved-word fi;; - $'\x7d') style=reserved-word;; # block + $'\x7d') # right brace + # + # Parsing rule: # { + # + # Additionally, `tt(})' is recognized in any position if neither the + # tt(IGNORE_BRACES) option nor the tt(IGNORE_CLOSE_BRACES) option is set.""" + if $right_brace_is_recognised_everywhere; then + style=reserved-word + else + # Fall through to the catchall case at the end. + fi + ;| '--'*) style=double-hyphen-option;; '-'*) style=single-hyphen-option;; "'"*) style=single-quoted-argument;; @@ -531,6 +547,8 @@ _zsh_highlight_highlighter_main_paint() [*?]*|*[^\\][*?]*) $highlight_glob && style=globbing || style=default;; *) if false; then + elif [[ $arg = $'\x7d' ]] && $right_brace_is_recognised_everywhere; then + # was handled by the $'\x7d' case above elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then style=history-expansion elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then diff --git a/highlighters/main/test-data/braces2.zsh b/highlighters/main/test-data/braces2.zsh index 89ba15a..adee00b 100644 --- a/highlighters/main/test-data/braces2.zsh +++ b/highlighters/main/test-data/braces2.zsh @@ -36,7 +36,7 @@ expected_region_highlight=( '1 2 reserved-word' # () '4 4 reserved-word' # { '6 9 builtin' # echo - '11 11 default "ignore_braces; fixed by next commit"' # } + '11 11 default' # } '12 12 commandseparator' # \n '13 13 reserved-word' # } ) From 0a9b347483ae653e95ed7ccb147a0db3644b6384 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 22 Sep 2016 04:10:28 +0000 Subject: [PATCH 42/66] driver: Warn just once when a highlighter is missing. The heretofore code warned once per keypress. --- zsh-syntax-highlighting.zsh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index f9d82de..ec01306 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -92,7 +92,11 @@ _zsh_highlight() typeset -ga ${cache_place} # If highlighter needs to be invoked - if "_zsh_highlight_highlighter_${highlighter}_predicate"; then + if ! type "_zsh_highlight_highlighter_${highlighter}_predicate" >&/dev/null; then + echo "zsh-syntax-highlighting: warning: disabling the ${(qq)highlighter} highlighter as it has not been loaded" >&2 + # TODO: use ${(b)} rather than ${(q)} if supported + ZSH_HIGHLIGHT_HIGHLIGHTERS=( ${ZSH_HIGHLIGHT_HIGHLIGHTERS:#${highlighter}} ) + elif "_zsh_highlight_highlighter_${highlighter}_predicate"; then # save a copy, and cleanup region_highlight region_highlight_copy=("${region_highlight[@]}") From 51b9d79c3bb61d3233a67b440f6ef7899d04b8c9 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 22 Sep 2016 04:52:32 +0000 Subject: [PATCH 43/66] 'main': Highlight mismatched parentheses and braces. --- highlighters/main/main-highlighter.zsh | 35 ++++++++++++++-- highlighters/main/test-data/braces1.zsh | 2 +- .../main/test-data/brackets-mismatch1.zsh | 40 +++++++++++++++++++ .../main/test-data/brackets-mismatch2.zsh | 40 +++++++++++++++++++ 4 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 highlighters/main/test-data/brackets-mismatch1.zsh create mode 100644 highlighters/main/test-data/brackets-mismatch2.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index a010b22..99ebe6a 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -155,6 +155,20 @@ _zsh_highlight_main__resolve_alias() { fi } +# Check that the top of $braces_stack has the expected value. If it does, set +# the style according to $2; otherwise, set style=unknown-token. +# +# $1: character expected to be at the top of $braces_stack +# $2: assignment to execute it if matches +_zsh_highlight_main__stack_pop() { + if [[ $braces_stack[1] == $1 ]]; then + braces_stack=${braces_stack:1} + eval "$2" + else + style=unknown-token + fi +} + # Main syntax highlighting function. _zsh_highlight_highlighter_main_paint() { @@ -194,6 +208,8 @@ _zsh_highlight_highlighter_main_paint() local buf="$PREBUFFER$BUFFER" integer len="${#buf}" + local braces_stack # "R" for round, "Q" for square, "Y" for curly + if (( path_dirs_was_set )); then options_to_set+=( PATH_DIRS ) fi @@ -418,7 +434,15 @@ _zsh_highlight_highlighter_main_paint() fi } case $res in - reserved) style=reserved-word;; + reserved) # reserved word + style=reserved-word + if [[ $arg == $'\x7b' ]]; then + braces_stack='Y'"$braces_stack" + elif [[ $arg == $'\x7d' ]]; then + # We're at command word, so no need to check $right_brace_is_recognised_everywhere + _zsh_highlight_main__stack_pop 'Y' style=reserved-word + fi + ;; 'suffix alias') style=suffix-alias;; alias) () { integer insane_alias @@ -488,10 +512,13 @@ _zsh_highlight_highlighter_main_paint() _zsh_highlight_main_add_region_highlight $((end_pos - 2)) $end_pos $style already_added=1 fi - elif [[ $arg == '()' || $arg == $'\x28' ]]; then + elif [[ $arg == '()' ]]; then # anonymous function + style=reserved-word + elif [[ $arg == $'\x28' ]]; then # subshell style=reserved-word + braces_stack='R'"$braces_stack" else if _zsh_highlight_main_highlighter_check_path; then style=$REPLY @@ -516,7 +543,7 @@ _zsh_highlight_highlighter_main_paint() in_array_assignment=false next_word+=':start:' else - style=reserved-word + _zsh_highlight_main__stack_pop 'R' style=reserved-word fi;; $'\x7d') # right brace # @@ -525,7 +552,7 @@ _zsh_highlight_highlighter_main_paint() # Additionally, `tt(})' is recognized in any position if neither the # tt(IGNORE_BRACES) option nor the tt(IGNORE_CLOSE_BRACES) option is set.""" if $right_brace_is_recognised_everywhere; then - style=reserved-word + _zsh_highlight_main__stack_pop 'Y' style=reserved-word else # Fall through to the catchall case at the end. fi diff --git a/highlighters/main/test-data/braces1.zsh b/highlighters/main/test-data/braces1.zsh index 858ade5..4b8064b 100644 --- a/highlighters/main/test-data/braces1.zsh +++ b/highlighters/main/test-data/braces1.zsh @@ -38,5 +38,5 @@ expected_region_highlight=( '6 9 builtin' # echo '11 11 reserved-word' # } '12 12 commandseparator' # \n - '13 13 unknown-token "issue #344 (balanced parentheses/braces)"' # } + '13 13 unknown-token' # } ) diff --git a/highlighters/main/test-data/brackets-mismatch1.zsh b/highlighters/main/test-data/brackets-mismatch1.zsh new file mode 100644 index 0000000..b9fd02a --- /dev/null +++ b/highlighters/main/test-data/brackets-mismatch1.zsh @@ -0,0 +1,40 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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='() { echo foo )' + +expected_region_highlight=( + '1 2 reserved-word' # () + '4 4 reserved-word' # { + '6 9 builtin' # echo + '11 13 default' # foo + '15 15 unknown-token' # ) +) diff --git a/highlighters/main/test-data/brackets-mismatch2.zsh b/highlighters/main/test-data/brackets-mismatch2.zsh new file mode 100644 index 0000000..ae74a3b --- /dev/null +++ b/highlighters/main/test-data/brackets-mismatch2.zsh @@ -0,0 +1,40 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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='() ( echo foo }' + +expected_region_highlight=( + '1 2 reserved-word' # () + '4 4 reserved-word' # ( + '6 9 builtin' # echo + '11 13 default' # foo + '15 15 unknown-token' # } +) From 2fabf7ca64b77ddcbbcb1aba76c9d52ccfc7ef41 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 22 Sep 2016 05:58:08 +0000 Subject: [PATCH 44/66] 'main': More tests for mismstached parentheses and braces. --- .../main/test-data/brackets-mismatch3.zsh | 37 +++++++++++++++++++ .../main/test-data/brackets-mismatch4.zsh | 37 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 highlighters/main/test-data/brackets-mismatch3.zsh create mode 100644 highlighters/main/test-data/brackets-mismatch4.zsh diff --git a/highlighters/main/test-data/brackets-mismatch3.zsh b/highlighters/main/test-data/brackets-mismatch3.zsh new file mode 100644 index 0000000..497e435 --- /dev/null +++ b/highlighters/main/test-data/brackets-mismatch3.zsh @@ -0,0 +1,37 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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='echo )' + +expected_region_highlight=( + '1 4 builtin' # echo + '6 6 unknown-token' # ) +) diff --git a/highlighters/main/test-data/brackets-mismatch4.zsh b/highlighters/main/test-data/brackets-mismatch4.zsh new file mode 100644 index 0000000..d3a99b0 --- /dev/null +++ b/highlighters/main/test-data/brackets-mismatch4.zsh @@ -0,0 +1,37 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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='echo }' + +expected_region_highlight=( + '1 4 builtin' # echo + '6 6 unknown-token' # } +) From 6f91850a01e17c3215ca0873582b3b726efe6abd Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 22 Sep 2016 17:56:00 +0000 Subject: [PATCH 45/66] 'main': Highlight first command word in named functions defined in the sh syntax when MULTI_FUNC_DEF is set. Fixes a subset of issue #237. --- highlighters/main/main-highlighter.zsh | 12 +++++ .../main/test-data/function-named1.zsh | 45 +++++++++++++++++++ .../main/test-data/function-named2.zsh | 40 +++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 highlighters/main/test-data/function-named1.zsh create mode 100644 highlighters/main/test-data/function-named2.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 99ebe6a..50408a0 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -186,6 +186,11 @@ _zsh_highlight_highlighter_main_paint() else integer path_dirs_was_set=0 fi + if [[ -o multi_func_def ]]; then + integer multi_func_def=1 + else + integer multi_func_def=0 + fi emulate -L zsh setopt localoptions extendedglob bareglobqual @@ -545,6 +550,13 @@ _zsh_highlight_highlighter_main_paint() else _zsh_highlight_main__stack_pop 'R' style=reserved-word fi;; + $'\x28\x29') # possibly a function definition + if (( multi_func_def )) || false # TODO: or if the previous word was a command word + then + next_word+=':start:' + fi + style=reserved-word + ;; $'\x7d') # right brace # # Parsing rule: # { diff --git a/highlighters/main/test-data/function-named1.zsh b/highlighters/main/test-data/function-named1.zsh new file mode 100644 index 0000000..30a0e23 --- /dev/null +++ b/highlighters/main/test-data/function-named1.zsh @@ -0,0 +1,45 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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='f() pwd; f() { balanced braces }' + +expected_region_highlight=( + '1 1 TBD "issue #223"' # f + '2 3 reserved-word' # () + '5 7 builtin' # pwd + '8 8 commandseparator' # ; + '10 10 TBD "issue #223"' # f + '11 12 reserved-word' # () + '14 14 reserved-word' # { + '16 23 unknown-token' # balanced + '25 30 default' # braces + '32 32 reserved-word' # } +) diff --git a/highlighters/main/test-data/function-named2.zsh b/highlighters/main/test-data/function-named2.zsh new file mode 100644 index 0000000..4a29587 --- /dev/null +++ b/highlighters/main/test-data/function-named2.zsh @@ -0,0 +1,40 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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='f g h () pwd' + +expected_region_highlight=( + '1 1 TBD "issue #223"' # f + '3 3 TBD "issue #223"' # g + '5 5 TBD "issue #223"' # h + '7 8 reserved-word' # () + '10 12 builtin' # pwd +) From e15a09840ea537b3b41efaae940ad022662db75d Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 22 Sep 2016 21:22:57 +0000 Subject: [PATCH 46/66] dev tools: Automate a recurring step: Set year correctly for new tests. --- tests/generate.zsh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/generate.zsh b/tests/generate.zsh index 3eee487..4b21587 100755 --- a/tests/generate.zsh +++ b/tests/generate.zsh @@ -40,8 +40,10 @@ if (( $# != 3 )) || [[ $1 == -* ]]; then fi buffer=$1 ZSH_HIGHLIGHT_HIGHLIGHTERS=( $2 ) -exec >${0:A:h:h}/highlighters/$2/test-data/$3.zsh -git add -N ${0:A:h:h}/highlighters/$2/test-data/$3.zsh +fname=${0:A:h:h}/highlighters/$2/test-data/$3.zsh +exec 3>&1 +exec >$fname +git add -N $fname # Load the main script. . ${0:A:h:h}/zsh-syntax-highlighting.zsh @@ -85,3 +87,10 @@ print 'expected_region_highlight=(' done } print ')' + +exec >&3 +year="`LC_ALL=C date +%Y`" +if read -q "?Set copyright year to $year? "; then + <$fname >$fname.t sed s/YYYY/$year/ && + mv $fname.t $fname +fi From a4196eda5e6fea29bf3cc70a17bf3bd2dd2eea95 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 23 Sep 2016 15:55:06 +0000 Subject: [PATCH 47/66] 'main': Restore compatibility with zsh-4.3.14 and older (after e3182c18de8f). Fixes #368. --- highlighters/main/main-highlighter.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 50408a0..af0ee67 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -176,7 +176,7 @@ _zsh_highlight_highlighter_main_paint() if [[ -o interactive_comments ]]; then local interactive_comments= # set to empty fi - if [[ -o ignore_braces ]] || [[ -o ignore_close_braces ]]; then + if [[ -o ignore_braces ]] || eval '[[ -o ignore_close_braces ]] 2>/dev/null'; then local right_brace_is_recognised_everywhere=false else local right_brace_is_recognised_everywhere=true From 987c13348693db2e33184d688b6e732532147f8d Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 24 Sep 2016 17:06:21 +0000 Subject: [PATCH 48/66] dev tools: Tolerate invalid input. --- tests/generate.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/generate.zsh b/tests/generate.zsh index 4b21587..e0a06da 100755 --- a/tests/generate.zsh +++ b/tests/generate.zsh @@ -40,7 +40,7 @@ if (( $# != 3 )) || [[ $1 == -* ]]; then fi buffer=$1 ZSH_HIGHLIGHT_HIGHLIGHTERS=( $2 ) -fname=${0:A:h:h}/highlighters/$2/test-data/$3.zsh +fname=${0:A:h:h}/highlighters/$2/test-data/${3%.zsh}.zsh exec 3>&1 exec >$fname git add -N $fname From 5627fd2045c773a61d672151b489612539d4eab1 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 24 Sep 2016 17:07:01 +0000 Subject: [PATCH 49/66] dev tools: Stage the copyright block to make diffs smaller. --- tests/generate.zsh | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tests/generate.zsh b/tests/generate.zsh index e0a06da..f6c32d6 100755 --- a/tests/generate.zsh +++ b/tests/generate.zsh @@ -41,9 +41,6 @@ fi buffer=$1 ZSH_HIGHLIGHT_HIGHLIGHTERS=( $2 ) fname=${0:A:h:h}/highlighters/$2/test-data/${3%.zsh}.zsh -exec 3>&1 -exec >$fname -git add -N $fname # Load the main script. . ${0:A:h:h}/zsh-syntax-highlighting.zsh @@ -54,9 +51,17 @@ _zsh_highlight_add_highlight() region_highlight+=("$1 $2 $3") } + # Copyright block -<$0 sed -n -e '1,/^$/p' | sed -e 's/2[0-9][0-9][0-9]/YYYY/' +year="`LC_ALL=C date +%Y`" +if ! read -q "?Set copyright year to $year? "; then + year="YYYY" +fi +exec >$fname +<$0 sed -n -e '1,/^$/p' | sed -e "s/2[0-9][0-9][0-9]/${year}/" echo "" +# Assumes stdout is line-buffered +git add -- $fname # Buffer print -n 'BUFFER=' @@ -87,10 +92,3 @@ print 'expected_region_highlight=(' done } print ')' - -exec >&3 -year="`LC_ALL=C date +%Y`" -if read -q "?Set copyright year to $year? "; then - <$fname >$fname.t sed s/YYYY/$year/ && - mv $fname.t $fname -fi From 8bf423d16d463fa5fe6fe69cecd2297a8fdbeed7 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 24 Sep 2016 17:55:18 +0000 Subject: [PATCH 50/66] 'main': Don't find command positions within multiline array literals. Fixes #333. --- highlighters/main/main-highlighter.zsh | 9 ++++- .../test-data/multiline-array-assignment1.zsh | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 highlighters/main/test-data/multiline-array-assignment1.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index af0ee67..993a716 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -613,8 +613,13 @@ _zsh_highlight_highlighter_main_paint() [[ $style == path || $style == path_prefix ]] && _zsh_highlight_main_highlighter_highlight_path_separators fi if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then - next_word=':start:' - highlight_glob=true + if [[ $arg == ';' ]] && $in_array_assignment; then + # literal newline inside an array assignment + next_word=':regular:' + else + next_word=':start:' + highlight_glob=true + fi elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]] || [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && $this_word == *':start:'* ]]; then diff --git a/highlighters/main/test-data/multiline-array-assignment1.zsh b/highlighters/main/test-data/multiline-array-assignment1.zsh new file mode 100644 index 0000000..662e0f3 --- /dev/null +++ b/highlighters/main/test-data/multiline-array-assignment1.zsh @@ -0,0 +1,40 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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=$'foo=(\nbar) env' + +expected_region_highlight=( + '1 5 assign' # foo=( + '6 6 commandseparator' # \n + '7 9 default' # bar + '10 10 assign' # ) + '12 14 command' # env +) From e5782e4ddfb6dbf5d5b12cb7352b3d955260e9b3 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 07:56:28 +0000 Subject: [PATCH 51/66] 'main': Highlight 'always' blocks. Fixes #335. --- highlighters/main/main-highlighter.zsh | 13 +++++++- highlighters/main/test-data/always1.zsh | 41 +++++++++++++++++++++++ highlighters/main/test-data/always2.zsh | 43 +++++++++++++++++++++++++ highlighters/main/test-data/always3.zsh | 43 +++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 highlighters/main/test-data/always1.zsh create mode 100644 highlighters/main/test-data/always2.zsh create mode 100644 highlighters/main/test-data/always3.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 993a716..07049a9 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -260,6 +260,7 @@ _zsh_highlight_highlighter_main_paint() # words) but not in "-ufoo" (one word). # - :regular: "Not a command word", and command delimiters are permitted. # Mainly used to detect premature termination of commands. + # - :always: The word 'always' in the «{ foo } always { bar }» syntax. # # When the kind of a word is not yet known, $this_word / $next_word may contain # multiple states. For example, after "sudo -i", the next word may be either @@ -408,7 +409,11 @@ _zsh_highlight_highlighter_main_paint() fi # The Great Fork: is this a command word? Is this a non-command word? - if [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word + if [[ $this_word == *':always:'* && $arg == 'always' ]]; then + # try-always construct + style=reserved-word # de facto a reserved word, although not de jure + next_word=':start:' + elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then style=precommand elif [[ "$arg" = "sudo" ]]; then @@ -446,6 +451,9 @@ _zsh_highlight_highlighter_main_paint() elif [[ $arg == $'\x7d' ]]; then # We're at command word, so no need to check $right_brace_is_recognised_everywhere _zsh_highlight_main__stack_pop 'Y' style=reserved-word + if [[ $style == reserved-word ]]; then + next_word+=':always:' + fi fi ;; 'suffix alias') style=suffix-alias;; @@ -565,6 +573,9 @@ _zsh_highlight_highlighter_main_paint() # tt(IGNORE_BRACES) option nor the tt(IGNORE_CLOSE_BRACES) option is set.""" if $right_brace_is_recognised_everywhere; then _zsh_highlight_main__stack_pop 'Y' style=reserved-word + if [[ $style == reserved-word ]]; then + next_word+=':always:' + fi else # Fall through to the catchall case at the end. fi diff --git a/highlighters/main/test-data/always1.zsh b/highlighters/main/test-data/always1.zsh new file mode 100644 index 0000000..ae40a72 --- /dev/null +++ b/highlighters/main/test-data/always1.zsh @@ -0,0 +1,41 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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 } always { pwd }' + +expected_region_highlight=( + '1 1 reserved-word' # { + '3 4 command' # ls + '6 6 reserved-word' # } + '8 13 reserved-word' # always + '15 15 reserved-word' # { + '17 19 builtin' # pwd + '21 21 reserved-word' # } +) diff --git a/highlighters/main/test-data/always2.zsh b/highlighters/main/test-data/always2.zsh new file mode 100644 index 0000000..f5852c6 --- /dev/null +++ b/highlighters/main/test-data/always2.zsh @@ -0,0 +1,43 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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=$'{\nls\n} always { pwd }' + +expected_region_highlight=( + '1 1 reserved-word' # { + '2 2 unknown-token' # \n + '3 4 command' # ls + '5 5 commandseparator' # \n + '6 6 reserved-word' # } + '8 13 reserved-word' # always + '15 15 reserved-word' # { + '17 19 builtin' # pwd + '21 21 reserved-word' # } +) diff --git a/highlighters/main/test-data/always3.zsh b/highlighters/main/test-data/always3.zsh new file mode 100644 index 0000000..d41beeb --- /dev/null +++ b/highlighters/main/test-data/always3.zsh @@ -0,0 +1,43 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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 +# ------------------------------------------------------------------------------------------------- + +setopt ignorebraces +BUFFER='echo { foo } always { bar }' + +expected_region_highlight=( + '1 4 builtin' # echo + '6 6 default' # { + '8 10 default' # foo + '12 12 default' # } + '14 19 default' # always + '21 21 default' # { + '23 25 default' # bar + '27 27 default' # } +) From a053768627fd2b552a96c38505e3e30a8cb93792 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 08:08:45 +0000 Subject: [PATCH 52/66] dev tools: Remove a superfluous empty line. --- highlighters/main/test-data/braces1.zsh | 1 - highlighters/main/test-data/braces2.zsh | 1 - highlighters/main/test-data/brackets-mismatch1.zsh | 1 - highlighters/main/test-data/brackets-mismatch2.zsh | 1 - highlighters/main/test-data/brackets-mismatch3.zsh | 1 - highlighters/main/test-data/brackets-mismatch4.zsh | 1 - highlighters/main/test-data/function-named1.zsh | 1 - highlighters/main/test-data/function-named2.zsh | 1 - highlighters/main/test-data/multiline-array-assignment1.zsh | 1 - tests/generate.zsh | 1 - 10 files changed, 10 deletions(-) diff --git a/highlighters/main/test-data/braces1.zsh b/highlighters/main/test-data/braces1.zsh index 4b8064b..3c5b1f0 100644 --- a/highlighters/main/test-data/braces1.zsh +++ b/highlighters/main/test-data/braces1.zsh @@ -28,7 +28,6 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- - BUFFER=$'() { echo }\n}' # no special setopts diff --git a/highlighters/main/test-data/braces2.zsh b/highlighters/main/test-data/braces2.zsh index adee00b..a3ea7c9 100644 --- a/highlighters/main/test-data/braces2.zsh +++ b/highlighters/main/test-data/braces2.zsh @@ -28,7 +28,6 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- - BUFFER=$'() { echo }\n}' setopt ignorebraces diff --git a/highlighters/main/test-data/brackets-mismatch1.zsh b/highlighters/main/test-data/brackets-mismatch1.zsh index b9fd02a..5e6e80f 100644 --- a/highlighters/main/test-data/brackets-mismatch1.zsh +++ b/highlighters/main/test-data/brackets-mismatch1.zsh @@ -28,7 +28,6 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- - BUFFER='() { echo foo )' expected_region_highlight=( diff --git a/highlighters/main/test-data/brackets-mismatch2.zsh b/highlighters/main/test-data/brackets-mismatch2.zsh index ae74a3b..2d98ac2 100644 --- a/highlighters/main/test-data/brackets-mismatch2.zsh +++ b/highlighters/main/test-data/brackets-mismatch2.zsh @@ -28,7 +28,6 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- - BUFFER='() ( echo foo }' expected_region_highlight=( diff --git a/highlighters/main/test-data/brackets-mismatch3.zsh b/highlighters/main/test-data/brackets-mismatch3.zsh index 497e435..e33040a 100644 --- a/highlighters/main/test-data/brackets-mismatch3.zsh +++ b/highlighters/main/test-data/brackets-mismatch3.zsh @@ -28,7 +28,6 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- - BUFFER='echo )' expected_region_highlight=( diff --git a/highlighters/main/test-data/brackets-mismatch4.zsh b/highlighters/main/test-data/brackets-mismatch4.zsh index d3a99b0..a9b15dc 100644 --- a/highlighters/main/test-data/brackets-mismatch4.zsh +++ b/highlighters/main/test-data/brackets-mismatch4.zsh @@ -28,7 +28,6 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- - BUFFER='echo }' expected_region_highlight=( diff --git a/highlighters/main/test-data/function-named1.zsh b/highlighters/main/test-data/function-named1.zsh index 30a0e23..c962ddb 100644 --- a/highlighters/main/test-data/function-named1.zsh +++ b/highlighters/main/test-data/function-named1.zsh @@ -28,7 +28,6 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- - BUFFER='f() pwd; f() { balanced braces }' expected_region_highlight=( diff --git a/highlighters/main/test-data/function-named2.zsh b/highlighters/main/test-data/function-named2.zsh index 4a29587..e12fce6 100644 --- a/highlighters/main/test-data/function-named2.zsh +++ b/highlighters/main/test-data/function-named2.zsh @@ -28,7 +28,6 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- - BUFFER='f g h () pwd' expected_region_highlight=( diff --git a/highlighters/main/test-data/multiline-array-assignment1.zsh b/highlighters/main/test-data/multiline-array-assignment1.zsh index 662e0f3..3734c7a 100644 --- a/highlighters/main/test-data/multiline-array-assignment1.zsh +++ b/highlighters/main/test-data/multiline-array-assignment1.zsh @@ -28,7 +28,6 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- - BUFFER=$'foo=(\nbar) env' expected_region_highlight=( diff --git a/tests/generate.zsh b/tests/generate.zsh index f6c32d6..e3f8f9d 100755 --- a/tests/generate.zsh +++ b/tests/generate.zsh @@ -59,7 +59,6 @@ if ! read -q "?Set copyright year to $year? "; then fi exec >$fname <$0 sed -n -e '1,/^$/p' | sed -e "s/2[0-9][0-9][0-9]/${year}/" -echo "" # Assumes stdout is line-buffered git add -- $fname From c6b6513ac0d61602a3fec196d26579070454b31c Mon Sep 17 00:00:00 2001 From: Sebastian Gniazdowski Date: Sun, 25 Sep 2016 12:24:40 +0200 Subject: [PATCH 53/66] 'main' / *_check_path: Precompute $#BUFFER and use it for speed gain Running main highlighter on itself (both runs are on the optimized version): - clean (8 runs, last 3 noted): 1.7007670000 1.7330720000 1.7038810000 - optimized (8 runs, last 3 noted): 1.5007230000 1.5142960000 1.4973320000 Average difference: 0.208456 When parsing main-highlighter with itself, the *_check_path function is called 426 times. Note that there are 686 region_highlight resulting entries. --- highlighters/main/main-highlighter.zsh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 07049a9..e842128 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -212,6 +212,7 @@ _zsh_highlight_highlighter_main_paint() local -a options_to_set # used in callees local buf="$PREBUFFER$BUFFER" integer len="${#buf}" + integer pure_buf_len=$(( len - ${#PREBUFFER} )) # == $#BUFFER, used e.g. in *_check_path local braces_stack # "R" for round, "Q" for square, "Y" for curly @@ -701,7 +702,7 @@ _zsh_highlight_main_highlighter_check_path() [[ ! -d ${expanded_path:h} ]] && return 1 # If this word ends the buffer, check if it's the prefix of a valid path. - if [[ ${BUFFER[1]} != "-" && ${#BUFFER} == $end_pos ]] && + if [[ ${BUFFER[1]} != "-" && $pure_buf_len == $end_pos ]] && [[ $WIDGET != zle-line-finish ]]; then local -a tmp tmp=( ${expanded_path}*(N) ) From 2755438e80bbe5572db7ef8062163bcda6eb80fc Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 09:20:40 +0000 Subject: [PATCH 54/66] 'main': Yet another test for mismatched braces. --- .../main/test-data/brackets-mismatch5.zsh | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 highlighters/main/test-data/brackets-mismatch5.zsh diff --git a/highlighters/main/test-data/brackets-mismatch5.zsh b/highlighters/main/test-data/brackets-mismatch5.zsh new file mode 100644 index 0000000..21e8bb2 --- /dev/null +++ b/highlighters/main/test-data/brackets-mismatch5.zsh @@ -0,0 +1,37 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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='echo { }' + +expected_region_highlight=( + '1 4 builtin' # echo + '6 6 default' # { + '8 8 unknown-token' # } +) From b2733a64da9388d8f9254f205c664a9872006856 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 09:20:40 +0000 Subject: [PATCH 55/66] 'main': Highlight mismatched do/done. --- highlighters/main/main-highlighter.zsh | 33 ++++++++++----- .../main/test-data/brackets-mismatch6.zsh | 40 +++++++++++++++++++ 2 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 highlighters/main/test-data/brackets-mismatch6.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index e842128..e08bce1 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -214,7 +214,11 @@ _zsh_highlight_highlighter_main_paint() integer len="${#buf}" integer pure_buf_len=$(( len - ${#PREBUFFER} )) # == $#BUFFER, used e.g. in *_check_path - local braces_stack # "R" for round, "Q" for square, "Y" for curly + # "R" for round + # "Q" for square + # "Y" for curly + # "D" for do/done + local braces_stack if (( path_dirs_was_set )); then options_to_set+=( PATH_DIRS ) @@ -447,15 +451,24 @@ _zsh_highlight_highlighter_main_paint() case $res in reserved) # reserved word style=reserved-word - if [[ $arg == $'\x7b' ]]; then - braces_stack='Y'"$braces_stack" - elif [[ $arg == $'\x7d' ]]; then - # We're at command word, so no need to check $right_brace_is_recognised_everywhere - _zsh_highlight_main__stack_pop 'Y' style=reserved-word - if [[ $style == reserved-word ]]; then - next_word+=':always:' - fi - fi + case $arg in + ($'\x7b') + braces_stack='Y'"$braces_stack" + ;; + ($'\x7d') + # We're at command word, so no need to check $right_brace_is_recognised_everywhere + _zsh_highlight_main__stack_pop 'Y' style=reserved-word + if [[ $style == reserved-word ]]; then + next_word+=':always:' + fi + ;; + ('do') + braces_stack='D'"$braces_stack" + ;; + ('done') + _zsh_highlight_main__stack_pop 'D' style=reserved-word + ;; + esac ;; 'suffix alias') style=suffix-alias;; alias) () { diff --git a/highlighters/main/test-data/brackets-mismatch6.zsh b/highlighters/main/test-data/brackets-mismatch6.zsh new file mode 100644 index 0000000..9df8388 --- /dev/null +++ b/highlighters/main/test-data/brackets-mismatch6.zsh @@ -0,0 +1,40 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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='(repeat 1; do)' + +expected_region_highlight=( + '1 1 reserved-word' # ( + '2 7 reserved-word' # repeat + '9 9 default' # 1 + '10 10 commandseparator' # ; + '12 13 reserved-word' # do + '14 14 unknown-token' # ) +) From a2876fb57dfdd53930667736300bd00eb5bd9b89 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 11:38:43 +0000 Subject: [PATCH 56/66] dev tools: Add a TODO to introduce code reuse. --- tests/generate.zsh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/generate.zsh b/tests/generate.zsh index e3f8f9d..0f515d8 100755 --- a/tests/generate.zsh +++ b/tests/generate.zsh @@ -77,6 +77,7 @@ print 'expected_region_highlight=(' PREBUFFER="" BUFFER="$buffer" region_highlight=() + # TODO: use run_test() from tests/test-highlighting.zsh (to get a tempdir) _zsh_highlight for ((i=1; i<=${#region_highlight}; i++)); do From 2bb8f0703d8f3f976177493979f8e8efdf9aae58 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 09:20:40 +0000 Subject: [PATCH 57/66] 'main': Highlight mismatched foreach/end. Fixes #96. --- highlighters/main/main-highlighter.zsh | 7 +++ .../main/test-data/brackets-mismatch7.zsh | 44 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 highlighters/main/test-data/brackets-mismatch7.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index e08bce1..1bcac2a 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -218,6 +218,7 @@ _zsh_highlight_highlighter_main_paint() # "Q" for square # "Y" for curly # "D" for do/done + # "$" for 'end' (matches 'foreach' always; also used with cshjunkiequotes in repeat/while) local braces_stack if (( path_dirs_was_set )); then @@ -468,6 +469,12 @@ _zsh_highlight_highlighter_main_paint() ('done') _zsh_highlight_main__stack_pop 'D' style=reserved-word ;; + ('foreach') + braces_stack='$'"$braces_stack" + ;; + ('end') + _zsh_highlight_main__stack_pop '$' style=reserved-word + ;; esac ;; 'suffix alias') style=suffix-alias;; diff --git a/highlighters/main/test-data/brackets-mismatch7.zsh b/highlighters/main/test-data/brackets-mismatch7.zsh new file mode 100644 index 0000000..27d5920 --- /dev/null +++ b/highlighters/main/test-data/brackets-mismatch7.zsh @@ -0,0 +1,44 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2012 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='for n in *; do echo $n; end' + +expected_region_highlight=( + '1 3 reserved-word' # for + '5 5 default' # n + '7 8 default' # in + '10 10 globbing' # * + '11 11 commandseparator' # ; + '13 14 reserved-word' # do + '16 19 builtin' # echo + '21 22 default' # $n + '23 23 commandseparator' # ; + '25 27 unknown-token' # end +) From 4fc35362ee5a48b587184efd265d2df4b3dbbcaf Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 12:06:39 +0000 Subject: [PATCH 58/66] 'main': Permit subshells to end at command position. Fixes #344. --- highlighters/main/main-highlighter.zsh | 3 ++ .../main/test-data/brackets-mismatch8.zsh | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 highlighters/main/test-data/brackets-mismatch8.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 1bcac2a..92767a6 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -553,6 +553,9 @@ _zsh_highlight_highlighter_main_paint() # subshell style=reserved-word braces_stack='R'"$braces_stack" + elif [[ $arg == $'\x29' ]]; then + # end of subshell + _zsh_highlight_main__stack_pop 'R' style=reserved-word else if _zsh_highlight_main_highlighter_check_path; then style=$REPLY diff --git a/highlighters/main/test-data/brackets-mismatch8.zsh b/highlighters/main/test-data/brackets-mismatch8.zsh new file mode 100644 index 0000000..07a42e3 --- /dev/null +++ b/highlighters/main/test-data/brackets-mismatch8.zsh @@ -0,0 +1,38 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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 1 reserved-word' # ( + '2 3 command' # ls + '4 4 commandseparator' # & + '5 5 reserved-word' # ) +) From c3913e0d8eadfa85e9cb1b4138d4fee7c7b486ab Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 14:31:31 +0000 Subject: [PATCH 59/66] 'main': Highlight command substitutions inside double quotes. Part of issue #139. --- highlighters/main/main-highlighter.zsh | 2 ++ highlighters/main/test-data/dollar-paren.zsh | 38 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 highlighters/main/test-data/dollar-paren.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 92767a6..544550b 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -756,6 +756,8 @@ _zsh_highlight_main_highlighter_highlight_string() elif [[ ${arg:$i} =~ ^[{]([A-Za-z_][A-Za-z0-9_]*|[0-9]+)[}] ]] ; then (( k += $#MATCH )) # highlight the parameter name and braces (( i += $#MATCH )) # skip past it + elif [[ $arg[i+1] == $'\x28' ]]; then + # Highlight just the '$'. else continue fi diff --git a/highlighters/main/test-data/dollar-paren.zsh b/highlighters/main/test-data/dollar-paren.zsh new file mode 100644 index 0000000..354daa0 --- /dev/null +++ b/highlighters/main/test-data/dollar-paren.zsh @@ -0,0 +1,38 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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=': "$(:)"' + +expected_region_highlight=( + '1 1 builtin' # : + '3 3 double-quoted-argument' # " + '4 4 dollar-double-quoted-argument' # $ + '5 8 double-quoted-argument' # (:)" +) From 4afe670f7a1b9f38e417eaeeefdf2ea4021c6728 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 14:35:16 +0000 Subject: [PATCH 60/66] 'main': Highlight shell's PID ($$) inside double quotes. --- highlighters/main/main-highlighter.zsh | 4 ++ highlighters/main/test-data/dollar-dollar.zsh | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 highlighters/main/test-data/dollar-dollar.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 544550b..c08a6d9 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -756,6 +756,10 @@ _zsh_highlight_main_highlighter_highlight_string() elif [[ ${arg:$i} =~ ^[{]([A-Za-z_][A-Za-z0-9_]*|[0-9]+)[}] ]] ; then (( k += $#MATCH )) # highlight the parameter name and braces (( i += $#MATCH )) # skip past it + elif [[ $arg[i+1] == '$' ]]; then + # $$ - pid + (( k += 1 )) # highlight both dollar signs + (( i += 1 )) # don't consider the second one as introducing another parameter expansion elif [[ $arg[i+1] == $'\x28' ]]; then # Highlight just the '$'. else diff --git a/highlighters/main/test-data/dollar-dollar.zsh b/highlighters/main/test-data/dollar-dollar.zsh new file mode 100644 index 0000000..79d7f7d --- /dev/null +++ b/highlighters/main/test-data/dollar-dollar.zsh @@ -0,0 +1,39 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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=': "$$ $$foo"' + +expected_region_highlight=( + '1 1 builtin' # : + '3 3 double-quoted-argument' # " + '4 5 dollar-double-quoted-argument' # $$ + '7 8 dollar-double-quoted-argument' # $$ + '9 12 double-quoted-argument' # foo" +) From 44ef6e38e5a7f99defdbd3e65c5ffb79fd9eaf3e Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 14:51:02 +0000 Subject: [PATCH 61/66] 'main': Highlight several more special (non-alphabetic) parameters. --- highlighters/main/main-highlighter.zsh | 4 ++ highlighters/main/test-data/dollar-noise.zsh | 46 ++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 highlighters/main/test-data/dollar-noise.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index c08a6d9..7cdfa76 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -760,6 +760,10 @@ _zsh_highlight_main_highlighter_highlight_string() # $$ - pid (( k += 1 )) # highlight both dollar signs (( i += 1 )) # don't consider the second one as introducing another parameter expansion + elif [[ $arg[i+1] == [-#*@?] ]]; then + # $#, $*, $@, $?, $- - like $$ above + (( k += 1 )) # highlight both dollar signs + (( i += 1 )) # don't consider the second one as introducing another parameter expansion elif [[ $arg[i+1] == $'\x28' ]]; then # Highlight just the '$'. else diff --git a/highlighters/main/test-data/dollar-noise.zsh b/highlighters/main/test-data/dollar-noise.zsh new file mode 100644 index 0000000..e900b02 --- /dev/null +++ b/highlighters/main/test-data/dollar-noise.zsh @@ -0,0 +1,46 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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=': "$- $# $* $@ $?"' + +expected_region_highlight=( + '1 1 builtin' # : + '3 3 double-quoted-argument' # " + '4 5 dollar-double-quoted-argument' # $- + '6 6 double-quoted-argument' # + '7 8 dollar-double-quoted-argument' # $# + '9 9 double-quoted-argument' # + '10 11 dollar-double-quoted-argument' # $* + '12 12 double-quoted-argument' # + '13 14 dollar-double-quoted-argument' # $@ + '15 15 double-quoted-argument' # + '16 17 dollar-double-quoted-argument' # $? + '18 18 double-quoted-argument' # " +) From 15461e7d21c3b868e1aff89521fbd20732ec157f Mon Sep 17 00:00:00 2001 From: Sebastian Gniazdowski Date: Sun, 25 Sep 2016 17:26:37 +0200 Subject: [PATCH 62/66] 'main': Directly count spaces to skip, don't leverage proc_buf length Main highlighter run on itself, on the optimized version: - optimized (8 runs, 3 last noted): 1.1201650000 1.1074430000 1.1263810000 - unoptimized (8 runs, 3 last noted): 1.5746400000 1.5115250000 1.5155440000 Average difference: 0.415907 --- highlighters/main/main-highlighter.zsh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index e842128..f599a0b 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -251,6 +251,8 @@ _zsh_highlight_highlighter_main_paint() '!' # reserved word; unrelated to $histchars[1] ) + local -a match mbegin mend + # State machine # # The states are: @@ -339,7 +341,22 @@ _zsh_highlight_highlighter_main_paint() (( start_pos += offset )) (( end_pos = start_pos + $#arg )) else - integer offset=$(((len-start_pos)-${#${proc_buf##([[:space:]]|\\[[:space:]])#}})) + # The line was: + # + # integer offset=$(((len-start_pos)-${#${proc_buf##([[:space:]]|\\[[:space:]])#}})) + # + # - len-start_pos is length of current proc_buf; basically: initial length minus where + # we are, and proc_buf is chopped to the "where we are" (compare the "previous value + # of start_pos" below, and the len-(start_pos-offset) = len-start_pos+offset) + # - what's after main minus sign is: length of proc_buf without spaces at the beginning + # - so what the line actually did, was computing length of the spaces! + # - this can be done via (#b) flag, like below + if [[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\[[:space:]])##)* ]]; then + # The first, outer parenthesis + integer offset="${#match[1]}" + else + integer offset=0 + fi ((start_pos+=offset)) ((end_pos=$start_pos+${#arg})) fi From 79198759b3121b5f69beb81fbffe264a19420105 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 18:08:36 +0000 Subject: [PATCH 63/66] tests: Also test arguments to an anonymous function. --- highlighters/main/test-data/anonymous-function.zsh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/highlighters/main/test-data/anonymous-function.zsh b/highlighters/main/test-data/anonymous-function.zsh index a6fbcf0..5b8e0b6 100644 --- a/highlighters/main/test-data/anonymous-function.zsh +++ b/highlighters/main/test-data/anonymous-function.zsh @@ -27,7 +27,7 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- -BUFFER='() echo hello; () { echo world }' +BUFFER='() echo hello; () { echo world } "argument"' expected_region_highlight=( "1 2 reserved-word" # () @@ -37,4 +37,5 @@ expected_region_highlight=( "16 17 reserved-word" # () "19 19 reserved-word" # { "21 24 builtin" # echo + "34 43 double-quoted-argument" # "argument" ) From 2b303f01b696c4538e9a7fc862c6340400bc3273 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 25 Sep 2016 08:01:54 +0000 Subject: [PATCH 64/66] dev tools: Automagically handle newlines (\n) in $BUFFER. --- tests/generate.zsh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/generate.zsh b/tests/generate.zsh index 0f515d8..64a1ede 100755 --- a/tests/generate.zsh +++ b/tests/generate.zsh @@ -29,6 +29,7 @@ # ------------------------------------------------------------------------------------------------- emulate -LR zsh +setopt localoptions extendedglob # Argument parsing. if (( $# != 3 )) || [[ $1 == -* ]]; then @@ -64,7 +65,11 @@ git add -- $fname # Buffer print -n 'BUFFER=' -print -r -- ${(qq)buffer} +if [[ $buffer != (#s)[$'\t -~']#(#e) ]]; then + print -r -- ${(qqqq)buffer} +else + print -r -- ${(qq)buffer} +fi echo "" # Expectations @@ -88,7 +93,7 @@ print 'expected_region_highlight=(' (( --end )) # convert to closed range, like expected_region_highlight (( ++start, ++end )) # region_highlight is 0-indexed; expected_region_highlight is 1-indexed fi - printf " %s # %s\n" ${(qq):-"$start $end $highlight_zone[3]"} $BUFFER[start,end] + printf " %s # %s\n" ${(qq):-"$start $end $highlight_zone[3]"} ${${(qqqq)BUFFER[start,end]}[3,-2]} done } print ')' From b4537a972eedb920fc61cabb359afd3ec7ea4b34 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 17:24:58 +0000 Subject: [PATCH 65/66] 'main': Set fallback style for the 'arg0' style. The fallback style name uses '_' in anticipation of a future auto-fallback feature keyed on style names. 'arg0' was previously known as 'commandtypefromthefuture'. --- docs/highlighters/main.md | 23 +++++++++++++++++++++++ highlighters/main/main-highlighter.zsh | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/highlighters/main.md b/docs/highlighters/main.md index 4793fed..bf83820 100644 --- a/docs/highlighters/main.md +++ b/docs/highlighters/main.md @@ -44,6 +44,7 @@ This highlighter defines the following styles: * `assign` - parameter assignments * `redirection` - redirection operators (`<`, `>`, etc) * `comment` - comments, when `setopt INTERACTIVE_COMMENTS` is in effect (`echo # foo`) +* `arg0` - a command word other than one of those enumrated above (other than a command, precommand, alias, function, or shell builtin command). * `default` - everything else To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, @@ -65,4 +66,26 @@ The syntax for values is the same as the syntax of "types of highlighting" of the zsh builtin `$zle_highlight` array, which is documented in [the `zshzle(1)` manual page][zshzle-Character-Highlighting]. +### Useless trivia + +#### Forward compatibility. + +zsh-syntax-highlighting attempts to be forward-compatible with zsh. +Specifically, we attempt to facilitate highlighting _command word_ types that +had not yet been invented when this version of zsh-syntax-highlighting was +released. + +A _command word_ is something like a function name, external command name, et +cetera. (See +[Simple Commands & Pipelines in `zshmisc(1)`][zshmisc-Simple-Commands-And-Pipelines] +for a formal definition.) + +If a new _kind_ of command word is ever added to zsh — something conceptually +different than "function" and "alias" and "external command" — then command words +of that (new) kind will be highlighted by the style `arg0_$kind`, +where `$kind` is the output of `type -w` on the new kind of command word. If that +style is not defined, then the style `arg0` will be used instead. + +[zshmisc-Simple-Commands-And-Pipelines]: http://zsh.sourceforge.net/Doc/Release/Shell-Grammar.html#Simple-Commands-_0026-Pipelines + [zshzle-Character-Highlighting]: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index a810eff..095a8fd 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -58,6 +58,7 @@ : ${ZSH_HIGHLIGHT_STYLES[assign]:=none} : ${ZSH_HIGHLIGHT_STYLES[redirection]:=none} : ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold} +: ${ZSH_HIGHLIGHT_STYLES[arg0]:=fg=green} # Whether the highlighter should be called or not. _zsh_highlight_main_highlighter_predicate() @@ -488,7 +489,7 @@ _zsh_highlight_main_highlighter() fi fi ;; - *) _zsh_highlight_main_add_region_highlight $start_pos $end_pos commandtypefromthefuture-$res + *) _zsh_highlight_main_add_region_highlight $start_pos $end_pos arg0_$res arg0 already_added=1 ;; esac From bccc3dc26943949e5b2f9525e6827440507b03ac Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 20 Aug 2016 09:31:13 +0000 Subject: [PATCH 66/66] 'main': Enable fallback to the 'arg0' style. --- highlighters/main/main-highlighter.zsh | 33 +++++++++++++---- highlighters/main/test-data/inheritance.zsh | 40 +++++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 highlighters/main/test-data/inheritance.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 095a8fd..9c7fb12 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -32,17 +32,11 @@ : ${ZSH_HIGHLIGHT_STYLES[default]:=none} : ${ZSH_HIGHLIGHT_STYLES[unknown-token]:=fg=red,bold} : ${ZSH_HIGHLIGHT_STYLES[reserved-word]:=fg=yellow} -: ${ZSH_HIGHLIGHT_STYLES[alias]:=fg=green} : ${ZSH_HIGHLIGHT_STYLES[suffix-alias]:=fg=green,underline} -: ${ZSH_HIGHLIGHT_STYLES[builtin]:=fg=green} -: ${ZSH_HIGHLIGHT_STYLES[function]:=fg=green} -: ${ZSH_HIGHLIGHT_STYLES[command]:=fg=green} : ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline} : ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none} -: ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green} : ${ZSH_HIGHLIGHT_STYLES[path]:=underline} : ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=} -: ${ZSH_HIGHLIGHT_STYLES[path_prefix]:=underline} : ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=} : ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue} : ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue} @@ -72,6 +66,33 @@ _zsh_highlight_main_add_region_highlight() { integer start=$1 end=$2 shift 2 + if (( $+argv[2] )); then + # Caller specified inheritance explicitly. + else + # Automate inheritance. + typeset -A fallback_of; fallback_of=( + alias arg0 + suffix-alias arg0 + builtin arg0 + function arg0 + command arg0 + precommand arg0 + hashed-command arg0 + + path_prefix path + # The path separator fallback won't ever be used, due to the optimisation + # in _zsh_highlight_main_highlighter_highlight_path_separators(). + path_pathseparator path + path_prefix_pathseparator path_prefix + ) + local needle=$1 value + while [[ -n ${value::=$fallback_of[$needle]} ]]; do + unset "fallback_of[$needle]" # paranoia against infinite loops + argv+=($value) + needle=$value + done + fi + # The calculation was relative to $PREBUFFER$BUFFER, but region_highlight is # relative to $BUFFER. (( start -= $#PREBUFFER )) diff --git a/highlighters/main/test-data/inheritance.zsh b/highlighters/main/test-data/inheritance.zsh new file mode 100644 index 0000000..e851ca3 --- /dev/null +++ b/highlighters/main/test-data/inheritance.zsh @@ -0,0 +1,40 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2016 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_add_highlight() +{ + region_highlight+=("$1 $2 ${(j.,.)argv[3,-1]}") +} + +BUFFER='type' + +expected_region_highlight=( + '1 4 builtin,arg0' # type +)