Tuesday, October 27, 2015

Switching syntax highlighting

Hello all!
I'm trying to write a function for switching between the current buffer
syntax highlighting and whitespace, but I'm really new to vim scripting
and such and am having a hard time.
I switch to whitespace syntax highlighting and back quite frequently.
However, it's not as simple as switching back to the buffer's filetype
syntax because there are times when I've changed it to something else.
For instance, data in .txt files that I visualize with different syntax
highlighting formats depending on the situation.

What I've tried is creating a buffer variable on buffer creation and
then updating it, this is what I have so far:

au BufEnter * let b:current_syntax=&syntax
fu! SwitchHLwhitespace()
if &syntax == "whitespace"
let &syntax=b:current_syntax
else
let tmp=&syntax
set syntax=whitespace
let b:current_syntax=tmp
endif
endfunction

This works pretty well until I open a second buffer, either with split,
newtab or whatever.
Now onto the questions...
1) If I don't use the tmp variable, somewhere inside the "set syntax"
routine the buffer var b:current_syntax disappears. I'm not entirely
sure why this happens, is it normal? For instance, right after opening a
file I can do "echo b:current_syntax" and get the correct output, then I
call my function and then once again the echo command and now it fails
with 'Undefined variable'. Why is this?
2) When opening a second buffer (lets name the A and B), if I call this
on A and switch it to whitespace, then B and switch it as well, then
back to A I can no longer go back, the buffer var has changed to
"whitespace" and no longer contains the stored syntax highlighting.

Now, I'm pretty sure I'm missing something important here... given that
I'm pretty new to vim scripting and such. I was under the impression
that b: variables were local to buffers, so I thought I could create one
per opened buffer and this would work, does it not behave like this?

Thank you all for your help!!

-- Sycc

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment