Ziggy
Ziggy

Reputation: 22365

Vim: How to compose search with another command?

I just installed sweet vim plugin that provides context colouring for javascript. I've fiddled with the colours so that they are just right, and am pretty pleased. However, there are times when I don't want the context colouring:

  1. in insert mode, since the colouring doesn't work well while I'm editing
  2. when I'm search, since the colouring seems to override the search hl

I resolved the first problem with:

autocmd InsertEnter *.js :JSContextColorToggle
autocmd InsertLeave *.js :JSContextColorToggle

However, the second problem is trickier. At first I thought I could just map /, my search key, so that it would first toggle the context colouring and then perform the search. I haven't been able to figure out how to write that mapping, though. How would I store "the original meaning of /" for use in my map?

Thanks,

p.s. check out this sweet context colouring (with solarized). enter image description here

Upvotes: 1

Views: 134

Answers (2)

bigfish
bigfish

Reputation: 5731

Hi I'm the author of the plugin. I fixed the conflict with hlsearch so it should just work now (try pulling latest version from git, I've not updated vim.org yet..)

As for the insert mode behaviour, there is a difference in behaviour between vim 7.3 and 7.4. 7.4 has the 'TextChanged' and 'TextChangedI' events, which fire when text is changed in normal , and insert mode respectively. This triggers the highlighting to update. However the TextChangedI event only fires on leaving insert mode. So if this is the behaviour you want, you can get it by upgrading to 7.4. In 7.3 I had to hook into the cursormoved event, which checks the 'b:changedtick' variable, which vim updates whenever changes occur... I'm not sure if this can occur during insert mode, but I think it might, which would explain odd behaviour if you're using 7.3.

I'm still trying to figure out what the best behaviour should be in insert mode...its tricky because the code syntax my be invalid during editing, and when the code cannot be parsed the plugin cannot work (and you will see regular syntax highlighting appear.. this may be a good thing as one value of syntax highlighting is visual syntax checking!). Another option would be to assume it is the same level as at the point where editing started, and offset the following text by however many characters are added/deleted during editing. Yet another option would be to have syntax highlighting in the area being edited (current line?) .

Upvotes: 3

Kent
Kent

Reputation: 195039

/ will enter command-mode.

if you only want to toggle highlighting with /, I think you need map <expr>.

something like

nnoremap <expr> / YourFunction()

in YourFunction(), you first do turn off the syntax hi, then return a /.

However you have to think about when to restore the hi. you can create another command mode mapping, map <cr> to first turn on the js hi, then return <cr>.

or just create an autocmd, when entering Normal mode, turn on the highlighting.

I didn't test above idea, hope it helps.

Upvotes: 2

Related Questions