From 1089e3a40886155c14bd9540e3de5f29a63776a2 Mon Sep 17 00:00:00 2001 From: Matthew Martin Date: Tue, 24 Nov 2015 22:28:30 -0600 Subject: [PATCH] themes: Initial support --- README.md | 2 + docs/highlighters.md | 30 ++++++++--- .../brackets/brackets-highlighter.zsh | 9 ---- highlighters/cursor/cursor-highlighter.zsh | 3 -- highlighters/line/line-highlighter.zsh | 3 -- highlighters/main/main-highlighter.zsh | 33 ------------ highlighters/root/root-highlighter.zsh | 3 -- themes/default | 50 +++++++++++++++++++ themes/error-only | 2 + zsh-syntax-highlighting.zsh | 23 ++++++++- 10 files changed, 99 insertions(+), 59 deletions(-) create mode 100644 themes/default create mode 100644 themes/error-only diff --git a/README.md b/README.md index 08af2b6..3c1f9da 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +**This branch will be rebased from time to time.** + zsh-syntax-highlighting [![Build Status][build-status-image]][build-status-travis] ======================= diff --git a/docs/highlighters.md b/docs/highlighters.md index f6eba7f..0adc2da 100644 --- a/docs/highlighters.md +++ b/docs/highlighters.md @@ -44,6 +44,18 @@ page][zshzle-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 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. 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 - style for that key in the highlighter script outside of any function with - `: ${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} + be highlighted and the `ZSH_HIGHLIGHT_STYLES` key to use. The key should + be prefixed with your highlighter name and a colon _zsh_highlight_highlighter_acme_paint() { # 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; 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`: ```zsh diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index fc71f2a..4056b25 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -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. _zsh_highlight_highlighter_brackets_predicate() { diff --git a/highlighters/cursor/cursor-highlighter.zsh b/highlighters/cursor/cursor-highlighter.zsh index 81633a3..bdbdbf0 100644 --- a/highlighters/cursor/cursor-highlighter.zsh +++ b/highlighters/cursor/cursor-highlighter.zsh @@ -28,9 +28,6 @@ # ------------------------------------------------------------------------------------------------- -# Define default styles. -: ${ZSH_HIGHLIGHT_STYLES[cursor]:=standout} - # Whether the cursor highlighter should be called or not. _zsh_highlight_highlighter_cursor_predicate() { diff --git a/highlighters/line/line-highlighter.zsh b/highlighters/line/line-highlighter.zsh index f922dc9..8eec427 100644 --- a/highlighters/line/line-highlighter.zsh +++ b/highlighters/line/line-highlighter.zsh @@ -28,9 +28,6 @@ # ------------------------------------------------------------------------------------------------- -# Define default styles. -: ${ZSH_HIGHLIGHT_STYLES[line]:=} - # Whether the root highlighter should be called or not. _zsh_highlight_highlighter_line_predicate() { diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 91be6ca..f24fc76 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -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. _zsh_highlight_highlighter_main_predicate() { diff --git a/highlighters/root/root-highlighter.zsh b/highlighters/root/root-highlighter.zsh index 3718c44..a2745c9 100644 --- a/highlighters/root/root-highlighter.zsh +++ b/highlighters/root/root-highlighter.zsh @@ -28,9 +28,6 @@ # ------------------------------------------------------------------------------------------------- -# Define default styles. -: ${ZSH_HIGHLIGHT_STYLES[root]:=standout} - # Whether the root highlighter should be called or not. _zsh_highlight_highlighter_root_predicate() { diff --git a/themes/default b/themes/default new file mode 100644 index 0000000..8ec90b7 --- /dev/null +++ b/themes/default @@ -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 diff --git a/themes/error-only b/themes/error-only new file mode 100644 index 0000000..6132f26 --- /dev/null +++ b/themes/error-only @@ -0,0 +1,2 @@ +ZSH_HIGHLIGHT_STYLES[bracket-error]=fg=red,bold +ZSH_HIGHLIGHT_STYLES[unknown-token]=fg=red,bold diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index df8a1aa..110541f 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -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 # ------------------------------------------------------------------------------------------------- @@ -410,7 +428,7 @@ _zsh_highlight_bind_widgets || { } # 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.' return 1 } @@ -432,6 +450,9 @@ zmodload zsh/parameter 2>/dev/null || true # Initialize the array of active highlighters if needed. [[ $#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 eval "$zsh_highlight__aliases" builtin unset zsh_highlight__aliases