zsh-users / zsh-syntax-highlighting

Fish shell like syntax highlighting for Zsh.
github.com/zsh-users/zsh-syntax-highlighting
BSD 3-Clause "New" or "Revised" License
20k stars 1.33k forks source link

Disable the highlighting just for one function, and re-enable it after the function has run #946

Closed fretzo closed 7 months ago

fretzo commented 7 months ago

Hi,

i would like to disable the plugin just for one function in my .zshrc, and re-enable it once the function is run.

It is similar to this issue, but not sure how to get highlighting re-enabled.

I have a snippet function, where i would like to print the snippets without highlighting, and get the highlights back after i have run the snippet command.

Here is the function:

# fzf-snippets, single-line (different version in bash)
,fs() {
    selected="$(cat ~/.local/share/snippets/mysnippets | grep '^[[:blank:]]*[^[:blank:]#]' | sed '/^$/d' | fzf -e -i )"
    # print to shell: remove tags, leading and trailing spaces, also no newline
        print -z $(echo "$selected" | sed -e s/\;\;\.\*\$// | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -d '\n')
    # add to history
        print -s $(echo "$selected" | sed -e s/\;\;\.\*\$// | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -d '\n')
}

Thanks alot !

danielshahaf commented 7 months ago

i would like to disable the plugin just for one function in my .zshrc, and re-enable it once the function is run.

What are the values of $ZSH_VERSION and $ZSH_HIGHLIGHT_REVISION?

In general, two approaches: (1) temporarily disable z-sy-h's hook, to prevent $region_highlight from getting populated in the first place; (2) add your own hook that empties $region_highlight [as in the issue you referenced] after z-sy-h has computed highlighting, so that no highlighting is effected.

Note that z-sy-h specifically recomputes the highlighting upon accept-line, so any disabling may have to take that into account.

It is similar to this issue, but not sure how to get highlighting re-enabled.

That would depend on how it was disabled.

I have a snippet function, where i would like to print the snippets without highlighting, and get the highlights back after i have run the snippet command.

Hmm. Empty the highlighters array and register a precmd hook that restores the old value of the array and deregisters itself? I'm sure there's a simpler solution, though.

Here is the function:


# fzf-snippets, single-line (different version in bash)
,fs() {
  selected="$(cat ~/.local/share/snippets/mysnippets | grep '^[[:blank:]]*[^[:blank:]#]' | sed '/^$/d' | fzf -e -i )"

OT: The sed(1) use is a no-op, because the negated character class ensures all lines are non-empty.

print to shell: remove tags, leading and trailing spaces, also no newline

    print -z $(echo "$selected" | sed -e s/\;\;\.\*\$// | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -d '\n')

add to history

    print -s $(echo "$selected" | sed -e s/\;\;\.\*\$// | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -d '\n')

OT: You probably want to use print -r -- here.

Also, I wonder whether it would work to s/print -z/eval/.

}

fretzo commented 7 months ago

Hi,

and thank you for all your tips and information.

I realized that the issue i was having was that text that was between double quotes was not being shown, so i disabled highlighting for double-quoted text like this:

# Declare the variable
typeset -A ZSH_HIGHLIGHT_STYLES
# To disable highlighting of globbing expressions
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='none'

I don't know if this is the best solution, but it looks good for now.

I made the changes to the function that you recommended, thanks alot !!!

Here is my zsh version: zsh 5.9 (x86_64-pc-linux-gnu) and zsh-syntax-highlighting version: 0.8.1-dev

Sorry if i made you go down a rabbit hole for nothing.

danielshahaf commented 7 months ago

You're welcome. The recommended solution is to change the terminal emulator's RGB values for [whatever the default highlighting of double-quoted-argument is] and for the default background, otherwise, any application that uses that color value (fg=yellow, IIRC?) for whatever reason would print unreadable text.

fretzo commented 6 months ago

I just noticed i had the same issue with another program, will look into fixing this. Thanks again !!!