themes: Initial support

This commit is contained in:
Matthew Martin 2015-11-24 22:28:30 -06:00
parent 2bd709fc28
commit 1089e3a408
10 changed files with 99 additions and 59 deletions

View file

@ -1,3 +1,5 @@
**This branch will be rebased from time to time.**
zsh-syntax-highlighting [![Build Status][build-status-image]][build-status-travis] zsh-syntax-highlighting [![Build Status][build-status-image]][build-status-travis]
======================= =======================

View file

@ -44,6 +44,18 @@ page][zshzle-Character-Highlighting].
[zshzle-Character-Highlighting]: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting [zshzle-Character-Highlighting]: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting
Styles may be set directly or by themes. If no theme is specified in
`ZSH_HIGHLIGHT_THEME` the `default` theme will be loaded. Additional themes
may be layered on top (overriding previous theme's settings) by calling
`_zsh_highlight_load_theme`. `_zsh_highlight_load_theme` takes either an
absolute path to a theme file to load or a theme name. For a theme name the
base theme from the themes directory is loaded and then the extensions of the
theme that any active highlighter has are loaded. Names must not contain a `/`.
The `default` theme is a colorful theme that preserves the defaults the
highlighters originally had. The `error-only` theme is also available for
highlighting only syntax errors.
Some highlighters support additional configuration parameters; see each Some highlighters support additional configuration parameters; see each
highlighter's documentation for details and examples. highlighter's documentation for details and examples.
@ -70,13 +82,8 @@ To create your own `acme` highlighter:
* Implement the `_zsh_highlight_highlighter_acme_paint` function. * Implement the `_zsh_highlight_highlighter_acme_paint` function.
This function does the actual syntax highlighting, by calling This function does the actual syntax highlighting, by calling
`_zsh_highlight_add_highlight` with the start and end of the region to `_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 be highlighted and the `ZSH_HIGHLIGHT_STYLES` key to use. The key should
style for that key in the highlighter script outside of any function with be prefixed with your highlighter name and a colon
`: ${ZSH_HIGHLIGHT_STYLES[key]:=value}`, being sure to prefix
the key with your highlighter name and a colon. For example:
```zsh
: ${ZSH_HIGHLIGHT_STYLES[acme:aurora]:=fg=green}
_zsh_highlight_highlighter_acme_paint() { _zsh_highlight_highlighter_acme_paint() {
# Colorize the whole buffer with the 'aurora' style # Colorize the whole buffer with the 'aurora' style
@ -106,6 +113,15 @@ To create your own `acme` highlighter:
These names are still supported for backwards compatibility; 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). however, support for them will be removed in a a future major or minor release (v0.x.0 or v1.0.0).
* Optionally extended the built-in themes in
`highlighters/${myhighlighter}/themes/${themename}`.
Define the theme's style for that key with `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
* Activate your highlighter in `~/.zshrc`: * Activate your highlighter in `~/.zshrc`:
```zsh ```zsh

View file

@ -28,15 +28,6 @@
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
# Define default styles.
: ${ZSH_HIGHLIGHT_STYLES[bracket-error]:=fg=red,bold}
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-1]:=fg=blue,bold}
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-2]:=fg=green,bold}
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-3]:=fg=magenta,bold}
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-4]:=fg=yellow,bold}
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-5]:=fg=cyan,bold}
: ${ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket]:=standout}
# Whether the brackets highlighter should be called or not. # Whether the brackets highlighter should be called or not.
_zsh_highlight_highlighter_brackets_predicate() _zsh_highlight_highlighter_brackets_predicate()
{ {

View file

@ -28,9 +28,6 @@
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
# Define default styles.
: ${ZSH_HIGHLIGHT_STYLES[cursor]:=standout}
# Whether the cursor highlighter should be called or not. # Whether the cursor highlighter should be called or not.
_zsh_highlight_highlighter_cursor_predicate() _zsh_highlight_highlighter_cursor_predicate()
{ {

View file

@ -28,9 +28,6 @@
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
# Define default styles.
: ${ZSH_HIGHLIGHT_STYLES[line]:=}
# Whether the root highlighter should be called or not. # Whether the root highlighter should be called or not.
_zsh_highlight_highlighter_line_predicate() _zsh_highlight_highlighter_line_predicate()
{ {

View file

@ -28,39 +28,6 @@
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
# Define default styles.
: ${ZSH_HIGHLIGHT_STYLES[default]:=none}
: ${ZSH_HIGHLIGHT_STYLES[unknown-token]:=fg=red,bold}
: ${ZSH_HIGHLIGHT_STYLES[reserved-word]:=fg=yellow}
: ${ZSH_HIGHLIGHT_STYLES[suffix-alias]:=fg=green,underline}
: ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline}
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
: ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
: ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=}
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=}
: ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
: ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue}
: ${ZSH_HIGHLIGHT_STYLES[command-substitution]:=none}
: ${ZSH_HIGHLIGHT_STYLES[command-substitution-delimiter]:=fg=magenta}
: ${ZSH_HIGHLIGHT_STYLES[process-substitution]:=none}
: ${ZSH_HIGHLIGHT_STYLES[process-substitution-delimiter]:=fg=magenta}
: ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none}
: ${ZSH_HIGHLIGHT_STYLES[double-hyphen-option]:=none}
: ${ZSH_HIGHLIGHT_STYLES[back-quoted-argument]:=none}
: ${ZSH_HIGHLIGHT_STYLES[back-quoted-argument-delimiter]:=fg=magenta}
: ${ZSH_HIGHLIGHT_STYLES[single-quoted-argument]:=fg=yellow}
: ${ZSH_HIGHLIGHT_STYLES[double-quoted-argument]:=fg=yellow}
: ${ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]:=fg=yellow}
: ${ZSH_HIGHLIGHT_STYLES[rc-quote]:=fg=cyan}
: ${ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]:=fg=cyan}
: ${ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]:=fg=cyan}
: ${ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]:=fg=cyan}
: ${ZSH_HIGHLIGHT_STYLES[assign]:=none}
: ${ZSH_HIGHLIGHT_STYLES[redirection]:=none}
: ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold}
: ${ZSH_HIGHLIGHT_STYLES[named-fd]:=none}
: ${ZSH_HIGHLIGHT_STYLES[arg0]:=fg=green}
# Whether the highlighter should be called or not. # Whether the highlighter should be called or not.
_zsh_highlight_highlighter_main_predicate() _zsh_highlight_highlighter_main_predicate()
{ {

View file

@ -28,9 +28,6 @@
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
# Define default styles.
: ${ZSH_HIGHLIGHT_STYLES[root]:=standout}
# Whether the root highlighter should be called or not. # Whether the root highlighter should be called or not.
_zsh_highlight_highlighter_root_predicate() _zsh_highlight_highlighter_root_predicate()
{ {

50
themes/default Normal file
View file

@ -0,0 +1,50 @@
# brackets
ZSH_HIGHLIGHT_STYLES[bracket-error]=fg=red,bold
ZSH_HIGHLIGHT_STYLES[bracket-level-1]=fg=blue,bold
ZSH_HIGHLIGHT_STYLES[bracket-level-2]=fg=green,bold
ZSH_HIGHLIGHT_STYLES[bracket-level-3]=fg=magenta,bold
ZSH_HIGHLIGHT_STYLES[bracket-level-4]=fg=yellow,bold
ZSH_HIGHLIGHT_STYLES[bracket-level-5]=fg=cyan,bold
ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket]=standout
# cursor
ZSH_HIGHLIGHT_STYLES[cursor]=standout
# line
ZSH_HIGHLIGHT_STYLES[line]=
# main
ZSH_HIGHLIGHT_STYLES[default]=none
ZSH_HIGHLIGHT_STYLES[unknown-token]=fg=red,bold
ZSH_HIGHLIGHT_STYLES[reserved-word]=fg=yellow
ZSH_HIGHLIGHT_STYLES[suffix-alias]=fg=green,underline
ZSH_HIGHLIGHT_STYLES[precommand]=fg=green,underline
ZSH_HIGHLIGHT_STYLES[commandseparator]=none
ZSH_HIGHLIGHT_STYLES[path]=underline
ZSH_HIGHLIGHT_STYLES[path_pathseparator]=
ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]=
ZSH_HIGHLIGHT_STYLES[globbing]=fg=blue
ZSH_HIGHLIGHT_STYLES[history-expansion]=fg=blue
ZSH_HIGHLIGHT_STYLES[command-substitution]=none
ZSH_HIGHLIGHT_STYLES[command-substitution-delimiter]=fg=magenta
ZSH_HIGHLIGHT_STYLES[process-substitution]=none
ZSH_HIGHLIGHT_STYLES[process-substitution-delimiter]=fg=magenta
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]=none
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]=none
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]=none
ZSH_HIGHLIGHT_STYLES[back-quoted-argument-delimiter]=fg=magenta
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]=fg=yellow
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]=fg=yellow
ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]=fg=yellow
ZSH_HIGHLIGHT_STYLES[rc-quote]=fg=cyan
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]=fg=cyan
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]=fg=cyan
ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]=fg=cyan
ZSH_HIGHLIGHT_STYLES[assign]=none
ZSH_HIGHLIGHT_STYLES[redirection]=none
ZSH_HIGHLIGHT_STYLES[comment]=fg=black,bold
ZSH_HIGHLIGHT_STYLES[named-fd]=none
ZSH_HIGHLIGHT_STYLES[arg0]=fg=green
# root
ZSH_HIGHLIGHT_STYLES[root]=standout

2
themes/error-only Normal file
View file

@ -0,0 +1,2 @@
ZSH_HIGHLIGHT_STYLES[bracket-error]=fg=red,bold
ZSH_HIGHLIGHT_STYLES[unknown-token]=fg=red,bold

View file

@ -399,6 +399,24 @@ _zsh_highlight_load_highlighters()
} }
# Load theme
# $1 should be a theme defined in themes/ or an absolute path
_zsh_highlight_load_theme()
{
local theme=$1
shift 1
if [[ ${theme[1]} == / ]]; then
source $theme
elif [[ $theme == */* ]]; then
print -r -- >&2 "zsh-syntax-highlighting: failed on invalid theme name: ${(qq)theme}"
return 1
else
for theme in ${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:h}/themes/$theme(N) $ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR/$^ZSH_HIGHLIGHT_HIGHLIGHTERS/$theme(N); do
source $theme
done
fi
}
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
# Setup # Setup
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
@ -410,7 +428,7 @@ _zsh_highlight_bind_widgets || {
} }
# Resolve highlighters directory location. # Resolve highlighters directory location.
_zsh_highlight_load_highlighters "${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:-${${0:A}:h}/highlighters}" || { _zsh_highlight_load_highlighters "${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:=${${0:A}:h}/highlighters}" || {
print -r -- >&2 'zsh-syntax-highlighting: failed loading highlighters, exiting.' print -r -- >&2 'zsh-syntax-highlighting: failed loading highlighters, exiting.'
return 1 return 1
} }
@ -432,6 +450,9 @@ zmodload zsh/parameter 2>/dev/null || true
# Initialize the array of active highlighters if needed. # Initialize the array of active highlighters if needed.
[[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && ZSH_HIGHLIGHT_HIGHLIGHTERS=(main) [[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && ZSH_HIGHLIGHT_HIGHLIGHTERS=(main)
# Load the theme.
_zsh_highlight_load_theme "${ZSH_HIGHLIGHT_THEME-default}"
# Restore the aliases we unned # Restore the aliases we unned
eval "$zsh_highlight__aliases" eval "$zsh_highlight__aliases"
builtin unset zsh_highlight__aliases builtin unset zsh_highlight__aliases