From 8636636c0022b6d6458e5e4c58abad03aff8f65f Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Mon, 28 Sep 2015 20:56:16 +0000 Subject: [PATCH] WIP issue 202: non-executable command word: design / plan: - if the command word is... then highlight it as... ------------------------- ----------------------- executable file command executable dir path_prefix prefix of executable file path_prefix prefix of executable dir path_prefix (anything else) unknown-token - TODO: causes BUFFER="." to be highlighted as 'builtin' immediately; previously, would only be highlighted so after - TODO: consider issue 148 plans - [filed as issue 210] use (Y1) if supported --- highlighters/main/main-highlighter.zsh | 25 ++++++++++--- .../test-data/nonexecutable-command-word.zsh | 35 +++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 highlighters/main/test-data/nonexecutable-command-word.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 6fa2754e6..60dca5cc9 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -199,7 +199,7 @@ _zsh_highlight_main_highlighter() _zsh_highlight_main_add_region_highlight $start_pos $((start_pos + 2)) $style substr_color=1 else - if _zsh_highlight_main_highlighter_check_path; then + if _zsh_highlight_main_highlighter_check_path 1; then style=$ZSH_HIGHLIGHT_STYLES[path] else style=$ZSH_HIGHLIGHT_STYLES[unknown-token] @@ -238,7 +238,7 @@ _zsh_highlight_main_highlighter() elif [[ $arg[1] == '<' || $arg[1] == '>' ]]; then style=$ZSH_HIGHLIGHT_STYLES[redirection] else - if _zsh_highlight_main_highlighter_check_path; then + if _zsh_highlight_main_highlighter_check_path 0; then style=$ZSH_HIGHLIGHT_STYLES[path] else style=$ZSH_HIGHLIGHT_STYLES[default] @@ -264,13 +264,20 @@ _zsh_highlight_main_highlighter_check_assign() } # Check if $arg is a path. +# The sole positional argument is 1 if at command word position and 0 otherwise. _zsh_highlight_main_highlighter_check_path() { + integer at_command_word=$1 _zsh_highlight_main_highlighter_expand_path $arg; local expanded_path="$REPLY" [[ -z $expanded_path ]] && return 1 - [[ -e $expanded_path ]] && return 0 + if (( $at_command_word )); then + # $expanded_path may be an executable file, or a listable directory; either is good + [[ -x $expanded_path ]] && return 0 + else + [[ -e $expanded_path ]] && return 0 + fi # Search the path in CDPATH local cdpath_dir @@ -284,10 +291,18 @@ _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 ]]; then local -a tmp - tmp=( ${expanded_path}*(N) ) + if (( $at_command_word )); then + tmp=( ${expanded_path}*?(N-f:u+x,g+x,o+x:) ) + else + tmp=( ${expanded_path}*?(N) ) + fi (( $#tmp > 0 )) && style_override=path_prefix && return 0 # or maybe an approximate path? - tmp=( (#a1)${expanded_path}*(N) ) + if (( $at_command_word )); then + tmp=( (#a1)${expanded_path}*(N-f:u+x,g+x,o+x:) ) + else + tmp=( (#a1)${expanded_path}*(N) ) + fi (( $#tmp > 0 )) && style_override=path_approx && return 0 fi diff --git a/highlighters/main/test-data/nonexecutable-command-word.zsh b/highlighters/main/test-data/nonexecutable-command-word.zsh new file mode 100644 index 000000000..246fdff5c --- /dev/null +++ b/highlighters/main/test-data/nonexecutable-command-word.zsh @@ -0,0 +1,35 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2015 zsh-syntax-highlighting contributors +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this list of conditions +# and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors +# may be used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ------------------------------------------------------------------------------------------------- +# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- +# vim: ft=zsh sw=2 ts=2 et +# ------------------------------------------------------------------------------------------------- + +BUFFER="$0" # the name of _this_ file + +expected_region_highlight=( + "1 $#0 $ZSH_HIGHLIGHT_STYLES[unknown-token]" # highlighters/main/test-data/nonexecutable-command-word.zsh +)