From a3d5dfcbdae9cea58aa703efe79192927a34e713 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 12 Jul 2016 07:15:04 +0000 Subject: [PATCH 1/2] 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 2/2] 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 } }