From 25ae1c01216c967335dfc5fd419848aaa8507b53 Mon Sep 17 00:00:00 2001 From: Matthew Martin Date: Tue, 14 Nov 2017 22:43:40 -0600 Subject: [PATCH] main: Add ' helper function --- docs/highlighters/main.md | 1 + highlighters/main/main-highlighter.zsh | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/highlighters/main.md b/docs/highlighters/main.md index bf83820..19912ab 100644 --- a/docs/highlighters/main.md +++ b/docs/highlighters/main.md @@ -38,6 +38,7 @@ This highlighter defines the following styles: * `single-quoted-argument` - single quoted arguments (`` 'foo' ``) * `double-quoted-argument` - double quoted arguments (`` "foo" ``) * `dollar-quoted-argument` - dollar quoted arguments (`` $'foo' ``) +* `rc-quote` - two single quotes inside single quotes when the `RC_QUOTES` option is set (`` 'foo''bar' ``) * `dollar-double-quoted-argument` - parameter expansion inside double quotes (`$foo` inside `""`) * `back-double-quoted-argument` - back double quoted arguments (`\x` inside `""`) * `back-dollar-quoted-argument` - back dollar quoted arguments (`\x` inside `$''`) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 5a26311..5afa146 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -46,6 +46,7 @@ : ${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} @@ -675,7 +676,9 @@ _zsh_highlight_highlighter_main_paint() ;| '--'*) style=double-hyphen-option;; '-'*) style=single-hyphen-option;; - "'"*) style=single-quoted-argument;; + "'"*) _zsh_highlight_main_highlighter_highlight_single_quote 1 + already_added=1 + ;; '"'*) _zsh_highlight_main_highlighter_highlight_double_quote already_added=1 ;; @@ -802,6 +805,26 @@ _zsh_highlight_main_highlighter_check_path() return 1 } +# Highlight single-quoted strings +_zsh_highlight_main_highlighter_highlight_single_quote() +{ + local arg1=$1 i q=\' + local -a highlights + i=$arg[(ib:arg1+1:)$q] + + if [[ $zsyh_user_options[rcquotes] == on ]]; then + while [[ $arg[i+1] == "'" ]]; do + highlights+=($(( start_pos + i - 1 )) $(( start_pos + i + 1 )) rc-quote) + (( i++ )) + i=$arg[(ib:i+1:)$q] + done + fi + + highlights=($(( start_pos + $1 - 1 )) $(( start_pos + i )) single-quoted-argument $highlights) + _zsh_highlight_main_add_many_region_highlights $highlights + REPLY=$i +} + # Highlight special chars inside double-quoted strings _zsh_highlight_main_highlighter_highlight_double_quote() {