Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colors for files, command_prefix + more minor modifications #152

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1a8e14f
Add command_prefix style
jimmijj Sep 23, 2014
85d1268
Changed ${#BUFFER} to ${#LBUFFER} in function _zsh_highlight_main_hig…
jimmijj Sep 23, 2014
28a5c6e
Add highlighting for files based on $LS_COLORS environment variable.
jimmijj Sep 23, 2014
f342c5e
Added missing separators: '&|', '|&' and '&!'
jimmijj Sep 24, 2014
4a3b2cd
Add TODO file
jimmijj Sep 24, 2014
50695ed
TODO update
jimmijj Sep 24, 2014
0382215
Merge remote-tracking branch 'upstream/master'
jimmijj Sep 26, 2014
d69a368
Add description of highlighting style for files to highlighter/main/R…
jimmijj Sep 26, 2014
7e334e7
Add ZSH_HIGHLIGHT_STYLES[file] style to change color for all files at…
jimmijj Sep 26, 2014
6035534
Created small image at misc/screenshot.png and added it to README.md.
jimmijj Sep 26, 2014
294c353
Add screenshot.png to git
jimmijj Sep 26, 2014
5d85b53
Small correction in README.md
jimmijj Sep 26, 2014
5ea0c05
TODO mostly done, so deleted
jimmijj Sep 26, 2014
a21f6ad
Changed screenshot link in README.md to relative path
jimmijj Sep 27, 2014
df2041e
Make a few variables local
jimmijj Sep 29, 2014
bd2e00a
Excluded several widgets from overriding following http://www.zsh.org…
jimmijj Sep 30, 2014
4a82aab
Check for TOKENS_COMMANDSEPARATOR even if $new_expression=false to hi…
jimmijj Oct 3, 2014
6d9a340
Add condition that path_approx should be highlighted only if $#arg > …
jimmijj Oct 3, 2014
df99f5f
Bug fix: when editing multi-line complex command highlighting was shi…
jimmijj Oct 3, 2014
f728546
Defined predicate_switcher function in order to be able to use main h…
jimmijj Oct 4, 2014
25b83ca
Prevent matching of command prefix if path is written explicitly. Thi…
jimmijj Oct 4, 2014
b85b225
Added style for redirection operators.
jimmijj Jan 17, 2015
b0f1340
Added information about redirection operators to README file.
jimmijj Jan 17, 2015
1626558
Reviewed remapped widgets - excluded unnecessary stuff.
jimmijj Aug 24, 2015
ece762e
User's definition of highlighting from "zle_highlight" array should t…
jimmijj Sep 3, 2015
3a6ba00
1. Removed two additional widgets from the rebinding list: push-input…
jimmijj Sep 4, 2015
103aecc
Updated README.
jimmijj Sep 7, 2015
ad75e32
Added more styles used in zle_highlight. Updated main README.md and h…
jimmijj Sep 7, 2015
69fe584
Added *NOT MAINTENED* warning to the README.
jimmijj Jan 11, 2016
d12bfae
README
jimmijj Jan 11, 2016
e710970
README: Clarify which projects are deprecated and which aren't.
danielshahaf Jun 1, 2016
4aa696a
Merge pull request #1 from danielshahaf/for-jimmijj/clarify-deprecati…
jimmijj Jun 6, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 9 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
zsh-syntax-highlighting
=======================
Fork of zsh-syntax-highlighting
===============================

**[Fish shell](http://www.fishshell.com) like syntax highlighting for [Zsh](http://www.zsh.org).**
**THIS FORK IS NO LONGER MAINTAINED.
YOU MAY WANT TO TEST ITS SUCCESSOR: [chromatic-zsh](https://github.com/jimmijj/chromatic-zsh)
YOU MAY WANT TO TEST THE PARENT PROJECT: [zsh-syntax-highlighting](https://github.com/zsh-users/zsh-syntax-highlighting)**

*Requirements: zsh 4.3.17+.*
Enhanced highlighting of the [Zsh](http://www.zsh.org) command-line, including syntax, paths, files, selected regions, searched patterns and much more.

![](misc/screenshot.png)


How to install
--------------

### Using packages

* Arch Linux: [community/zsh-syntax-highlighting](https://www.archlinux.org/packages/zsh-syntax-highlighting) / [AUR/zsh-syntax-highlighting-git](https://aur.archlinux.org/packages/zsh-syntax-highlighting-git)
* Gentoo: [mv overlay](http://gpo.zugaina.org/app-shells/zsh-syntax-highlighting)

### In your ~/.zshrc

* Download the script or clone this repository:

git clone git://github.com/zsh-users/zsh-syntax-highlighting.git
git clone git://github.com/jimmijj/zsh-syntax-highlighting.git

* Source the script **at the end** of `~/.zshrc`:

Expand All @@ -28,23 +25,6 @@ How to install

source ~/.zshrc


### With oh-my-zsh

* Download the script or clone this repository in [oh-my-zsh](http://github.com/robbyrussell/oh-my-zsh) plugins directory:

cd ~/.oh-my-zsh/custom/plugins
git clone git://github.com/zsh-users/zsh-syntax-highlighting.git

* Activate the plugin in `~/.zshrc` (in **last** position):

plugins=( [plugins...] zsh-syntax-highlighting)

* Source `~/.zshrc` to take changes into account:

source ~/.zshrc


How to tweak
------------

Expand Down
32 changes: 32 additions & 0 deletions highlighters/main/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This is the ***main*** highlighter, that highlights:
* Options
* Arguments
* Paths
* Files
* Strings

How to activate it
Expand All @@ -28,8 +29,10 @@ This highlighter defines the following styles:
* `builtin` - shell builtin commands
* `function` - functions
* `command` - commands
* `command_prefix` - command prefixes
* `precommand` - precommands (i.e. exec, builtin, ...)
* `commandseparator` - command separation tokens
* `redirection` - redirection operators
* `hashed-command` - hashed commands
* `path` - paths
* `path_prefix` - path prefixes
Expand All @@ -44,6 +47,10 @@ This highlighter defines the following styles:
* `dollar-double-quoted-argument` - dollar double quoted arguments
* `back-double-quoted-argument` - back double quoted arguments
* `assign` - variable assignments
* `isearch` - matched string in history-incremental-search* widgets
* `region` - selected region (between the point and the mark)
* `special` - unprintable characters
* `suffix` - suffix characters used in the completion system
* `default` - parts of the buffer that do not match anything

To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, for example in `~/.zshrc`:
Expand All @@ -58,3 +65,28 @@ To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, for
ZSH_HIGHLIGHT_STYLES[globbing]='none'

The syntax for declaring styles is [documented here](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#SEC135).


By default files are colored in the same fashion as `ls` command, namely by comparing file attributes and extension with the content of LS_COLORS environment variable. To override this behaviour change the value of ZSH_HIGHLIGHT_STYLES[file] in ~/.zshrc:

# To have all files in one color irrespectively of attributes and extensions
ZSH_HIGHLIGHT_STYLES[file]='fg=green'

# To disable higlighting for all files
ZSH_HIGHLIGHT_STYLES[file]='none'

# To use LS_COLORS do not set this style at all
# ZSH_HIGHLIGHT_STYLES[file]

It is also possible to change the color for one single file attribute/extenstion. To achieve this modify ZSH_HIGHLIGHT_FILES in ~/.zshrc:

# To set color for executables
ZSH_HIGHLIGHT_FILES[ex]='fg=119'

# To set color for files with sticky bit
ZSH_HIGHLIGHT_FILES[st]='fg=7,bg=4'

# To set color for files with pdf extenstion
ZSH_HIGHLIGHT_FILES[*.pdf]='fg=34'

Note that LS_COLORS uses ANSI color codes (not names as 'green') and so does ZSH_HIGHLIGHT_FILES by default, but ZSH_HIGHLIGHT_FILES[*.pdf]='fg=green' is possible too. However if you set color code by hand you must guarantee that your terminal is capable to display that color properly. In above examples 256 color palette is used. In case of doubt it is better not to set ZSH_HIGHLIGHT_STYLES[file] and change LS_COLORS via ~/.dircolors file. If ~/.dircolors files doesn't exist one can generate it by `dircolor` command.
165 changes: 158 additions & 7 deletions highlighters/main/main-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@
: ${ZSH_HIGHLIGHT_STYLES[builtin]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[function]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[command]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[command_prefix]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline}
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
: ${ZSH_HIGHLIGHT_STYLES[redirection]:=fg=magenta}
: ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
: ${ZSH_HIGHLIGHT_STYLES[path_prefix]:=underline}
: ${ZSH_HIGHLIGHT_STYLES[path_approx]:=fg=yellow,underline}
: ${ZSH_HIGHLIGHT_STYLES[file]:=}
: ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
: ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue}
: ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none}
Expand All @@ -53,26 +56,59 @@
: ${ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]:=fg=cyan}
: ${ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]:=fg=cyan}
: ${ZSH_HIGHLIGHT_STYLES[assign]:=none}
: ${ZSH_HIGHLIGHT_STYLES[isearch]:=fg=yellow,bg=red,bold}
: ${ZSH_HIGHLIGHT_STYLES[region]:=bg=blue}
: ${ZSH_HIGHLIGHT_STYLES[special]:=none}
: ${ZSH_HIGHLIGHT_STYLES[suffix]:=none}

# Whether the highlighter should be called or not.
_zsh_highlight_main_highlighter_predicate()
{
_zsh_highlight_buffer_modified
}

## In case we need to highlight in other circumstances then default from highlighter_predicate lets define a switcher
_zsh_highlight_main_highlighter_predicate_switcher()
{
case $1 in
'b') # buffer
_zsh_highlight_main_highlighter_predicate()
{
_zsh_highlight_buffer_modified
};;
'c') # cursor
_zsh_highlight_main_highlighter_predicate()
{
_zsh_highlight_cursor_moved
};;
'bc') bccounter=0 # buffer and cursor
_zsh_highlight_main_highlighter_predicate()
{
bccounter=$((bccounter+1))
(( $bccounter > 1 )) && _zsh_highlight_main_highlighter_predicate_switcher b
_zsh_highlight_cursor_moved || _zsh_highlight_buffer_modified
};;
*);;
esac
}

# Main syntax highlighting function.
_zsh_highlight_main_highlighter()
{
emulate -L zsh
setopt localoptions extendedglob bareglobqual
local start_pos=0 end_pos highlight_glob=true new_expression=true arg style sudo=false sudo_arg=false
local start_pos=0 end_pos highlight_glob=true new_expression=true arg style lsstyle start_file_pos end_file_pos sudo=false sudo_arg=false
typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
typeset -a ZSH_HIGHLIGHT_TOKENS_REDIRECTION
typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
typeset -a ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS
region_highlight=()

ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR=(
'|' '||' ';' '&' '&&'
'|' '||' ';' '&' '&&' '&|' '|&' '&!'
)
ZSH_HIGHLIGHT_TOKENS_REDIRECTION=(
'<' '<>' '>' '>|' '>!' '>>' '>>|' '>>!' '<<' '<<-' '<<<' '<&' '>&' '<& -' '>& -' '<& p' '>& p' '&>' '>&|' '>&!' '&>|' '&>!' '>>&' '&>>' '>>&|' '>>&!' '&>>|' '&>>!'
)
ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS=(
'builtin' 'command' 'exec' 'nocorrect' 'noglob'
Expand All @@ -82,12 +118,19 @@ _zsh_highlight_main_highlighter()
$ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR $ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
)

splitbuf1=(${(z)BUFFER})
splitbuf2=(${(z)BUFFER//$'\n'/ \$\'\\\\n\' }) # ugly hack, but I have no other idea
local argnum=0
for arg in ${(z)BUFFER}; do
local substr_color=0
argnum=$((argnum+1))
if [[ $splitbuf1[$argnum] != $splitbuf2[$argnum] ]] && new_expression=true && continue

local substr_color=0 isfile=false
local style_override=""
[[ $start_pos -eq 0 && $arg = 'noglob' ]] && highlight_glob=false
((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]##[[:space:]]#}}))
((end_pos=$start_pos+${#arg}))

# Parse the sudo command line
if $sudo; then
case "$arg" in
Expand Down Expand Up @@ -126,13 +169,20 @@ _zsh_highlight_main_highlighter()
*) if _zsh_highlight_main_highlighter_check_assign; then
style=$ZSH_HIGHLIGHT_STYLES[assign]
new_expression=true
elif _zsh_highlight_main_highlighter_check_command; then
style=$ZSH_HIGHLIGHT_STYLES[command_prefix]
elif _zsh_highlight_main_highlighter_check_path; then
style=$ZSH_HIGHLIGHT_STYLES[path]
elif [[ $arg[0,1] == $histchars[0,1] || $arg[0,1] == $histchars[2,2] ]]; then
style=$ZSH_HIGHLIGHT_STYLES[history-expansion]
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
style=$ZSH_HIGHLIGHT_STYLES[commandseparator]
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_REDIRECTION:#"$arg"} ]]; then
style=$ZSH_HIGHLIGHT_STYLES[redirection]
else
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
fi
_zsh_highlight_main_highlighter_check_file && isfile=true
;;
esac
fi
Expand All @@ -154,17 +204,26 @@ _zsh_highlight_main_highlighter()
style=$ZSH_HIGHLIGHT_STYLES[history-expansion]
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
style=$ZSH_HIGHLIGHT_STYLES[commandseparator]
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_REDIRECTION:#"$arg"} ]]; then
style=$ZSH_HIGHLIGHT_STYLES[redirection]
else
style=$ZSH_HIGHLIGHT_STYLES[default]
fi
_zsh_highlight_main_highlighter_check_file && isfile=true
;;
esac
fi
# if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
[[ -n $style_override ]] && style=$ZSH_HIGHLIGHT_STYLES[$style_override]
if [[ $isfile == true ]]; then
start_file_pos=$((start_pos+${#arg}-${#arg:t}))
end_file_pos=$end_pos
end_pos=$((end_pos-${#arg:t}))
region_highlight+=("$start_file_pos $end_file_pos $lsstyle")
fi
[[ $substr_color = 0 ]] && region_highlight+=("$start_pos $end_pos $style")
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && new_expression=true
start_pos=$end_pos
[[ $isfile == true ]] && start_pos=$end_file_pos || start_pos=$end_pos
done
}

Expand All @@ -188,14 +247,14 @@ _zsh_highlight_main_highlighter_check_path()
[[ -e "$cdpath_dir/$expanded_path" ]] && return 0
done
[[ ! -e ${expanded_path:h} ]] && return 1
if [[ ${BUFFER[1]} != "-" && ${#BUFFER} == $end_pos ]]; then
if [[ ${BUFFER[1]} != "-" && ${#LBUFFER} == $end_pos ]]; then
local -a tmp
# got a path prefix?
tmp=( ${expanded_path}*(N) )
(( $#tmp > 0 )) && style_override=path_prefix && return 0
(( $#tmp > 0 )) && style_override=path_prefix && _zsh_highlight_main_highlighter_predicate_switcher bc && return 0
# or maybe an approximate path?
tmp=( (#a1)${expanded_path}*(N) )
(( $#tmp > 0 )) && style_override=path_approx && return 0
(( $#arg > 3 && $#tmp > 0 )) && style_override=path_approx && return 0
fi
return 1
}
Expand Down Expand Up @@ -238,3 +297,95 @@ _zsh_highlight_main_highlighter_highlight_string()
region_highlight+=("$j $k $style")
done
}

## Check if command with given prefix exists
_zsh_highlight_main_highlighter_check_command()
{
setopt localoptions nonomatch
local -a prefixed_command
[[ $arg != $arg:t ]] && return 1 # don't match anything if explicit path is present
for p in $path; do prefixed_command+=( $p/${arg}*(N) ); done
[[ ${BUFFER[1]} != "-" && ${#LBUFFER} == $end_pos && $#prefixed_command > 0 ]] && return 0 || return 1
}

## Fill table with colors and file types from $LS_COLORS
_zsh_highlight_files_highlighter_fill_table_of_types()
{
local group type code ncolors=$(echotc Co)
local -a attrib

for group in ${(s.:.)LS_COLORS}; do
type=${group%=*}
code=${group#*=}
attrib=()
takeattrib ${(s.;.)code}
ZSH_HIGHLIGHT_FILES+=($type ${(j.,.)attrib})
done
}

## Take attributes from unfolded $LS_COLORS code
takeattrib()
{
while [ "$#" -gt 0 ]; do
[[ $1 == 38 && $2 == 5 ]] && {attrib+=("fg=$3"); shift 3; continue}
[[ $1 == 48 && $2 == 5 ]] && {attrib+=("bg=$3"); shift 3; continue}
case $1 in
00|0) attrib+=("none"); shift;;
01|1) attrib+=("bold" ); shift;;
02|2) attrib+=("faint"); shift;;
03|3) attrib+=("italic"); shift;;
04|4) attrib+=("underscore"); shift;;
05|5) attrib+=("blink"); shift;;
07|7) attrib+=("standout"); shift;;
08|8) attrib+=("concealed"); shift;;
3[0-7]) attrib+=("fg=$(($1-30))"); shift;;
4[0-7]) attrib+=("bg=$(($1-40))"); shift;;
9[0-7]) [[ $ncolors == 256 ]] && attrib+=("fg=$(($1-82))") || attrib+=("fg=$(($1-90))" "bold"); shift;;
10[0-7]) [[ $ncolors == 256 ]] && attrib+=("bg=$(($1-92))") || attrib+=("bg=$(($1-100))" "bold"); shift;;
*) shift;;
esac
done
}

## Check if the argument is a file, if yes change the style accordingly
_zsh_highlight_main_highlighter_check_file()
{
setopt localoptions nonomatch
local expanded_arg matched_file

expanded_arg=${(Q)~arg}
[[ -z $expanded_arg ]] && return 1
[[ -d $expanded_arg ]] && return 1
[[ ${BUFFER[1]} != "-" && ${#LBUFFER} == $end_pos ]] && matched_file=(${expanded_arg}*(Noa^/[1]))
[[ -e $expanded_arg || -e $matched_file ]] && lsstyle=none || return 1
[[ -e $matched_file ]] && _zsh_highlight_main_highlighter_predicate_switcher bc

[[ ! -z $ZSH_HIGHLIGHT_STYLES[file] ]] && lsstyle=$ZSH_HIGHLIGHT_STYLES[file] && return 0

# [[ rs ]]
# [[ -d $expanded_arg || -d $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[di] && return 0
[[ -h $expanded_arg || -h $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[ln] && return 0
# [[ mh ]]
[[ -p $expanded_arg || -p $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[pi] && return 0
[[ -S $expanded_arg || -S $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[so] && return 0
# [[ do ]]
[[ -b $expanded_arg || -b $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[bd] && return 0
[[ -c $expanded_arg || -c $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[cd] && return 0
# [[ or ]]
# [[ mi ]]
[[ -u $expanded_arg || -u $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[su] && return 0
[[ -g $expanded_arg || -g $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[sg] && return 0
# [[ ca ]]
# [[ tw ]]
# [[ ow ]]
[[ -k $expanded_arg || -k $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[st] && return 0
[[ -x $expanded_arg || -x $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[ex] && return 0

[[ -e $expanded_arg ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[*.$expanded_arg:e] && return 0
[[ -n $matched_file:e ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[*.$matched_file:e] && return 0

return 0
}

## Fill table only once, at the initialization process
_zsh_highlight_files_highlighter_fill_table_of_types
Binary file added misc/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading