Namespacing z-sy-h code ended

This commit is contained in:
Sebastian Gniazdowski 2016-10-09 09:52:50 +02:00
parent 0110eb24d9
commit ef2fd4cdb5
2 changed files with 40 additions and 42 deletions

View file

@ -118,8 +118,8 @@ _hsmw_main() {
POSTDISPLAY="$preamble$text" POSTDISPLAY="$preamble$text"
region_highlight=( ) region_highlight=( )
_hsmw_highlight_init -hsmw-highlight-init
_hsmw_highlight_process "$text" -hsmw-highlight-process "$text"
region_highlight=( "${region_highlight[@]//(#b)([[:digit:]]##)/$(( ${match[1]} + offset ))}" ) region_highlight=( "${region_highlight[@]//(#b)([[:digit:]]##)/$(( ${match[1]} + offset ))}" )
region_highlight+=( "$(( offset + ${#txt_before} )) $(( offset + ${#txt_before} + ${#entry} + 1 )) underline" ) region_highlight+=( "$(( offset + ${#txt_before} )) $(( offset + ${#txt_before} + ${#entry} + 1 )) underline" )

View file

@ -114,7 +114,7 @@ typeset -gA HSMW_HIGHLIGHT_STYLES
# Check whether the first argument is a redirection operator token. # Check whether the first argument is a redirection operator token.
# Report result via the exit code. # Report result via the exit code.
_zsh_highlight_main__is_redirection() { -hsmw-highlight-is-redirection() {
# A redirection operator token: # A redirection operator token:
# - starts with an optional single-digit number; # - starts with an optional single-digit number;
# - then, has a '<' or '>' character; # - then, has a '<' or '>' character;
@ -127,7 +127,7 @@ _zsh_highlight_main__is_redirection() {
# Takes a single argument. # Takes a single argument.
# #
# The result will be stored in REPLY. # The result will be stored in REPLY.
_zsh_highlight_main__resolve_alias() { -hsmw-highlight-resolve-alias() {
if zmodload -e zsh/parameter; then if zmodload -e zsh/parameter; then
REPLY=${aliases[$arg]} REPLY=${aliases[$arg]}
else else
@ -140,7 +140,7 @@ _zsh_highlight_main__resolve_alias() {
# #
# $1: character expected to be at the top of $braces_stack # $1: character expected to be at the top of $braces_stack
# $2: assignment to execute it if matches # $2: assignment to execute it if matches
_zsh_highlight_main__stack_pop() { -hsmw-highlight-stack-pop() {
if [[ $braces_stack[1] == $1 ]]; then if [[ $braces_stack[1] == $1 ]]; then
braces_stack=${braces_stack:1} braces_stack=${braces_stack:1}
eval "$2" eval "$2"
@ -150,7 +150,7 @@ _zsh_highlight_main__stack_pop() {
} }
# Main syntax highlighting function. # Main syntax highlighting function.
_hsmw_highlight_process() -hsmw-highlight-process()
{ {
## Before we even 'emulate -L', we must test a few options that would reset. ## Before we even 'emulate -L', we must test a few options that would reset.
if [[ -o interactive_comments ]]; then if [[ -o interactive_comments ]]; then
@ -177,9 +177,9 @@ _hsmw_highlight_process()
## Variable declarations and initializations ## Variable declarations and initializations
local start_pos=0 end_pos highlight_glob=true arg style local start_pos=0 end_pos highlight_glob=true arg style
local in_array_assignment=false # true between 'a=(' and the matching ')' local in_array_assignment=false # true between 'a=(' and the matching ')'
typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR typeset -a __HSMW_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS typeset -a __HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS
typeset -a ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW typeset -a __HSMW_HIGHLIGHT_TOKENS_CONTROL_FLOW
local -a options_to_set # used in callees local -a options_to_set # used in callees
local buf="$1" local buf="$1"
integer len="${#buf}" integer len="${#buf}"
@ -192,21 +192,21 @@ _hsmw_highlight_process()
fi fi
unset path_dirs_was_set unset path_dirs_was_set
ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR=( __HSMW_HIGHLIGHT_TOKENS_COMMANDSEPARATOR=(
'|' '||' ';' '&' '&&' '|' '||' ';' '&' '&&'
'|&' '|&'
'&!' '&|' '&!' '&|'
# ### 'case' syntax, but followed by a pattern, not by a command # ### 'case' syntax, but followed by a pattern, not by a command
# ';;' ';&' ';|' # ';;' ';&' ';|'
) )
ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS=( __HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS=(
'builtin' 'command' 'exec' 'nocorrect' 'noglob' 'builtin' 'command' 'exec' 'nocorrect' 'noglob'
'pkexec' # immune to #121 because it's usually not passed --option flags 'pkexec' # immune to #121 because it's usually not passed --option flags
) )
# Tokens that, at (naively-determined) "command position", are followed by # Tokens that, at (naively-determined) "command position", are followed by
# a de jure command position. All of these are reserved words. # a de jure command position. All of these are reserved words.
ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW=( __HSMW_HIGHLIGHT_TOKENS_CONTROL_FLOW=(
$'\x7b' # block $'\x7b' # block
$'\x28' # subshell $'\x28' # subshell
'()' # anonymous function '()' # anonymous function
@ -366,7 +366,7 @@ _hsmw_highlight_process()
fi fi
# Analyse the current word. # Analyse the current word.
if _zsh_highlight_main__is_redirection $arg ; then if -hsmw-highlight-is-redirection $arg ; then
# A '<' or '>', possibly followed by a digit # A '<' or '>', possibly followed by a digit
in_redirection=2 in_redirection=2
fi fi
@ -403,7 +403,7 @@ _hsmw_highlight_process()
style=reserved-word # de facto a reserved word, although not de jure style=reserved-word # de facto a reserved word, although not de jure
next_word=':start:' next_word=':start:'
elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then if [[ -n ${(M)__HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then
style=precommand style=precommand
elif [[ "$arg" = "sudo" ]]; then elif [[ "$arg" = "sudo" ]]; then
style=precommand style=precommand
@ -411,7 +411,7 @@ _hsmw_highlight_process()
next_word+=':sudo_opt:' next_word+=':sudo_opt:'
next_word+=':start:' next_word+=':start:'
else else
_zsh_highlight_main_highlighter_expand_path $arg -hsmw-highlight-expand-path $arg
local expanded_arg="$REPLY" local expanded_arg="$REPLY"
-hsmw-highlight-main-type ${expanded_arg} -hsmw-highlight-main-type ${expanded_arg}
local res="$REPLY" local res="$REPLY"
@ -439,7 +439,7 @@ _hsmw_highlight_process()
braces_stack='Y'"$braces_stack" braces_stack='Y'"$braces_stack"
elif [[ $arg == $'\x7d' ]]; then elif [[ $arg == $'\x7d' ]]; then
# We're at command word, so no need to check $right_brace_is_recognised_everywhere # We're at command word, so no need to check $right_brace_is_recognised_everywhere
_zsh_highlight_main__stack_pop 'Y' style=reserved-word -hsmw-highlight-stack-pop 'Y' style=reserved-word
if [[ $style == reserved-word ]]; then if [[ $style == reserved-word ]]; then
next_word+=':always:' next_word+=':always:'
fi fi
@ -464,9 +464,9 @@ _hsmw_highlight_process()
style=unknown-token style=unknown-token
else else
style=alias style=alias
_zsh_highlight_main__resolve_alias $arg -hsmw-highlight-resolve-alias $arg
local alias_target="$REPLY" local alias_target="$REPLY"
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$alias_target"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg) [[ -n ${(M)__HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$alias_target"} && -z ${(M)__HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]] && __HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg)
fi fi
} }
;; ;;
@ -474,7 +474,7 @@ _hsmw_highlight_process()
function) style=function;; function) style=function;;
command) style=command;; command) style=command;;
hashed) style=hashed-command;; hashed) style=hashed-command;;
none) if _zsh_highlight_main_highlighter_check_assign; then none) if -hsmw-highlight-check-assign; then
style=assign style=assign
if [[ $arg[-1] == '(' ]]; then if [[ $arg[-1] == '(' ]]; then
in_array_assignment=true in_array_assignment=true
@ -487,7 +487,7 @@ _hsmw_highlight_process()
style=history-expansion style=history-expansion
elif [[ $arg[0,1] == $histchars[2,2] ]]; then elif [[ $arg[0,1] == $histchars[2,2] ]]; then
style=history-expansion style=history-expansion
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then elif [[ -n ${(M)__HSMW_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
if [[ $this_word == *':regular:'* ]]; then if [[ $this_word == *':regular:'* ]]; then
# This highlights empty commands (semicolon follows nothing) as an error. # This highlights empty commands (semicolon follows nothing) as an error.
# Zsh accepts them, though. # Zsh accepts them, though.
@ -522,7 +522,7 @@ _hsmw_highlight_process()
style=reserved-word style=reserved-word
braces_stack='R'"$braces_stack" braces_stack='R'"$braces_stack"
else else
if _zsh_highlight_main_highlighter_check_path; then if -hsmw-highlight-check-path; then
style=$REPLY style=$REPLY
else else
style=unknown-token style=unknown-token
@ -545,7 +545,7 @@ _hsmw_highlight_process()
in_array_assignment=false in_array_assignment=false
next_word+=':start:' next_word+=':start:'
else else
_zsh_highlight_main__stack_pop 'R' style=reserved-word -hsmw-highlight-stack-pop 'R' style=reserved-word
fi;; fi;;
$'\x28\x29') # possibly a function definition $'\x28\x29') # possibly a function definition
if (( multi_func_def )) || false # TODO: or if the previous word was a command word if (( multi_func_def )) || false # TODO: or if the previous word was a command word
@ -561,7 +561,7 @@ _hsmw_highlight_process()
# Additionally, `tt(})' is recognized in any position if neither the # Additionally, `tt(})' is recognized in any position if neither the
# tt(IGNORE_BRACES) option nor the tt(IGNORE_CLOSE_BRACES) option is set.""" # tt(IGNORE_BRACES) option nor the tt(IGNORE_CLOSE_BRACES) option is set."""
if $right_brace_is_recognised_everywhere; then if $right_brace_is_recognised_everywhere; then
_zsh_highlight_main__stack_pop 'Y' style=reserved-word -hsmw-highlight-stack-pop 'Y' style=reserved-word
if [[ $style == reserved-word ]]; then if [[ $style == reserved-word ]]; then
next_word+=':always:' next_word+=':always:'
fi fi
@ -574,12 +574,12 @@ _hsmw_highlight_process()
"'"*) style=single-quoted-argument;; "'"*) style=single-quoted-argument;;
'"'*) style=double-quoted-argument '"'*) style=double-quoted-argument
-hsmw-add-highlight $start_pos $end_pos $style -hsmw-add-highlight $start_pos $end_pos $style
_zsh_highlight_main_highlighter_highlight_string -hsmw-highlight-string
already_added=1 already_added=1
;; ;;
\$\'*) style=dollar-quoted-argument \$\'*) style=dollar-quoted-argument
-hsmw-add-highlight $start_pos $end_pos $style -hsmw-add-highlight $start_pos $end_pos $style
_zsh_highlight_main_highlighter_highlight_dollar_string -hsmw-highlight-dollar-string
already_added=1 already_added=1
;; ;;
'`'*) style=back-quoted-argument;; '`'*) style=back-quoted-argument;;
@ -590,7 +590,7 @@ _hsmw_highlight_process()
# was handled by the $'\x7d' case above # was handled by the $'\x7d' case above
elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then
style=history-expansion style=history-expansion
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then elif [[ -n ${(M)__HSMW_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
if [[ $this_word == *':regular:'* ]]; then if [[ $this_word == *':regular:'* ]]; then
style=commandseparator style=commandseparator
else else
@ -599,7 +599,7 @@ _hsmw_highlight_process()
elif (( in_redirection == 2 )); then elif (( in_redirection == 2 )); then
style=redirection style=redirection
else else
if _zsh_highlight_main_highlighter_check_path; then if -hsmw-highlight-check-path; then
style=$REPLY style=$REPLY
else else
style=default style=default
@ -610,9 +610,9 @@ _hsmw_highlight_process()
fi fi
if ! (( already_added )); then if ! (( already_added )); then
-hsmw-add-highlight $start_pos $end_pos $style -hsmw-add-highlight $start_pos $end_pos $style
[[ $style == path || $style == path_prefix ]] && _zsh_highlight_main_highlighter_highlight_path_separators [[ $style == path || $style == path_prefix ]] && -hsmw-highlight-path-separators
fi fi
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then if [[ -n ${(M)__HSMW_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
if [[ $arg == ';' ]] && $in_array_assignment; then if [[ $arg == ';' ]] && $in_array_assignment; then
# literal newline inside an array assignment # literal newline inside an array assignment
next_word=':regular:' next_word=':regular:'
@ -621,8 +621,8 @@ _hsmw_highlight_process()
highlight_glob=true highlight_glob=true
fi fi
elif elif
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]] || [[ -n ${(M)__HSMW_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]] ||
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && $this_word == *':start:'* ]]; then [[ -n ${(M)__HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && $this_word == *':start:'* ]]; then
next_word=':start:' next_word=':start:'
elif [[ $arg == "repeat" && $this_word == *':start:'* ]]; then elif [[ $arg == "repeat" && $this_word == *':start:'* ]]; then
# skip the repeat-count word # skip the repeat-count word
@ -647,14 +647,14 @@ _hsmw_highlight_process()
} }
# Check if $arg is variable assignment # Check if $arg is variable assignment
_zsh_highlight_main_highlighter_check_assign() -hsmw-highlight-check-assign()
{ {
setopt localoptions extended_glob setopt localoptions extended_glob
[[ $arg == [[:alpha:]_][[:alnum:]_]#(|\[*\])(|[+])=* ]] || [[ $arg == [[:alpha:]_][[:alnum:]_]#(|\[*\])(|[+])=* ]] ||
[[ $arg == [0-9]##(|[+])=* ]] [[ $arg == [0-9]##(|[+])=* ]]
} }
_zsh_highlight_main_highlighter_highlight_path_separators() -hsmw-highlight-path-separators()
{ {
local pos style_pathsep local pos style_pathsep
style_pathsep=${style}_pathseparator style_pathsep=${style}_pathseparator
@ -669,9 +669,9 @@ _zsh_highlight_main_highlighter_highlight_path_separators()
# Check if $arg is a path. # Check if $arg is a path.
# If yes, return 0 and in $REPLY the style to use. # If yes, return 0 and in $REPLY the style to use.
# Else, return non-zero (and the contents of $REPLY is undefined). # Else, return non-zero (and the contents of $REPLY is undefined).
_zsh_highlight_main_highlighter_check_path() -hsmw-highlight-check-path()
{ {
_zsh_highlight_main_highlighter_expand_path $arg; -hsmw-highlight-expand-path $arg;
local expanded_path="$REPLY" local expanded_path="$REPLY"
REPLY=path REPLY=path
@ -702,7 +702,7 @@ _zsh_highlight_main_highlighter_check_path()
} }
# Highlight special chars inside double-quoted strings # Highlight special chars inside double-quoted strings
_zsh_highlight_main_highlighter_highlight_string() -hsmw-highlight-string()
{ {
setopt localoptions noksharrays setopt localoptions noksharrays
local -a match mbegin mend local -a match mbegin mend
@ -741,7 +741,7 @@ _zsh_highlight_main_highlighter_highlight_string()
} }
# Highlight special chars inside dollar-quoted strings # Highlight special chars inside dollar-quoted strings
_zsh_highlight_main_highlighter_highlight_dollar_string() -hsmw-highlight-dollar-string()
{ {
setopt localoptions noksharrays setopt localoptions noksharrays
local -a match mbegin mend local -a match mbegin mend
@ -785,12 +785,10 @@ _zsh_highlight_main_highlighter_highlight_dollar_string()
# Called with a single positional argument. # Called with a single positional argument.
# Perform filename expansion (tilde expansion) on the argument and set $REPLY to the expanded value. # Perform filename expansion (tilde expansion) on the argument and set $REPLY to the expanded value.
#
#_hsmw_highlight_init
# Does not perform filename generation (globbing). # Does not perform filename generation (globbing).
_zsh_highlight_main_highlighter_expand_path() -hsmw-highlight-expand-path()
{ {
(( $# == 1 )) || print -r -- >&2 "zsh-syntax-highlighting: BUG: _zsh_highlight_main_highlighter_expand_path: called without argument" (( $# == 1 )) || print -r -- >&2 "hsmw-highlight: BUG: -hsmw-highlight-expand-path: called without argument"
# The $~1 syntax normally performs filename generation, but not when it's on the right-hand side of ${x:=y}. # The $~1 syntax normally performs filename generation, but not when it's on the right-hand side of ${x:=y}.
setopt localoptions nonomatch setopt localoptions nonomatch
@ -802,7 +800,7 @@ _zsh_highlight_main_highlighter_expand_path()
# Main highlighter initialization # Main highlighter initialization
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
_hsmw_highlight_init() { -hsmw-highlight-init() {
__hsmw_highlight_main__command_type_cache=() __hsmw_highlight_main__command_type_cache=()
} }