how to automagically run a "syn off" the moment I start "editing" a file.

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

how to automagically run a "syn off" the moment I start "editing" a file.

Rahul Nabar

I made a custom syntax highlighting file for one of our chemistry
coordinate file formats.

I wanted it to be on by default but issue a "syn off" whenever I start
editing the file (otherwise it is hard to keep the syntax highlighting
in a consistent, meaningful state during partial edits)

Is it possible to automatically run  a "syn off" the moment I start
"editing" a file. By editing I mean making any changes whatsoever.

This way whenever a user opens a file the highlighting is present. But
as soon as he edits it disappears and then finally if he wants he can
manually issue a "syn on" after he has again brought the file to a
meaningful parseable state.

Just wondering how to achieve this via a vim hack!
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Charles E Campbell Jr

Rahul wrote:

> I made a custom syntax highlighting file for one of our chemistry
> coordinate file formats.
>
> I wanted it to be on by default but issue a "syn off" whenever I start
> editing the file (otherwise it is hard to keep the syntax highlighting
> in a consistent, meaningful state during partial edits)
>
> Is it possible to automatically run  a "syn off" the moment I start
> "editing" a file. By editing I mean making any changes whatsoever.
>
> This way whenever a user opens a file the highlighting is present. But
> as soon as he edits it disappears and then finally if he wants he can
> manually issue a "syn on" after he has again brought the file to a
> meaningful parseable state.
>
> Just wondering how to achieve this via a vim hack!
>  
You could use InsertEnter and InsertLeave autocmds.  Won't affect normal
mode changes, though.

Regards,
C Campbell


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Rahul Nabar



On Mar 9, 5:43 pm, "Charles E. Campbell, Jr."
<[hidden email]> wrote:
>
> You could use InsertEnter and InsertLeave autocmds.  Won't affect normal
> mode changes, though.

Thanks Charles. That just might work.
You mean to say commands like "dd" etc. will not trigger it?

No way to trap *all* changes at all?

--
Rahul
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

scott-268

On Tuesday 10 March 2009 1:51 am, RPN wrote:

>
> On Mar 9, 5:43   pm, "Charles E. Campbell, Jr."
> <[hidden email]> wrote:
> >
> > You could use InsertEnter and InsertLeave autocmds.    Won't affect normal
> > mode changes, though.
>
> Thanks Charles. That just might work.
> You mean to say commands like "dd" etc. will not trigger it?
>
> No way to trap *all* changes at all?

there is the 'modified' flag, but you'd have to find a way to
trigger testing it that would make you happy

AFAIK there is no BufferModified autocommand

sc


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Christian Brabandt
In reply to this post by Charles E Campbell Jr

Hi Charles!

On Mon, 09 Mar 2009, Charles E. Campbell, Jr. wrote:

>
> Rahul wrote:
> > I made a custom syntax highlighting file for one of our chemistry
> > coordinate file formats.
> >
> > I wanted it to be on by default but issue a "syn off" whenever I start
> > editing the file (otherwise it is hard to keep the syntax highlighting
> > in a consistent, meaningful state during partial edits)
> >
> > Is it possible to automatically run  a "syn off" the moment I start
> > "editing" a file. By editing I mean making any changes whatsoever.
> >
> > This way whenever a user opens a file the highlighting is present. But
> > as soon as he edits it disappears and then finally if he wants he can
> > manually issue a "syn on" after he has again brought the file to a
> > meaningful parseable state.
> >
> > Just wondering how to achieve this via a vim hack!
> >  
> You could use InsertEnter and InsertLeave autocmds.  Won't affect normal
> mode changes, though.

Combine this with a CursorMoved autocmd testing for modification of
the buffer (e.g.
au CursorMoved * if (&modified) | syn off | else |syn on
)

regards,
Christian
--
:wq

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Rahul Nabar



On Mar 10, 4:13 am, Christian Brabandt <[hidden email]> wrote:

> Combine this with a CursorMoved autocmd testing for modification of
> the buffer (e.g.
> au CursorMoved * if (&modified) | syn off | else |syn on

I tried putting this in my vimrc. It does work in a limited way but
has a bizarre side effect.

Symptoms:

syn is on when I open file.
If I dd syn  turns off. [good]

BUT as soon as I open the file my cursor keys are unresponsive. They
stay unresponsive till I do a dd. Then the syn highlighting disappears
and my cursory keys start working again.

Any clues?

--
Rahul
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Charles Campbell

RPN wrote:

>
> On Mar 10, 4:13 am, Christian Brabandt <[hidden email]> wrote:
>
>  
>> Combine this with a CursorMoved autocmd testing for modification of
>> the buffer (e.g.
>> au CursorMoved * if (&modified) | syn off | else |syn on
>>    
>
> I tried putting this in my vimrc. It does work in a limited way but
> has a bizarre side effect.
>
> Symptoms:
>
> syn is on when I open file.
> If I dd syn  turns off. [good]
>
> BUT as soon as I open the file my cursor keys are unresponsive. They
> stay unresponsive till I do a dd. Then the syn highlighting disappears
> and my cursory keys start working again.
>
> Any clues?
>
>  
Try the following:

au InsertEnter * if exists("syntax_on")| syn off | endif
au InsertLeave * syn on
au CursorMoved * if &modified && exists("syntax_on") | syn off | endif
au BufWritePost * if !exists("syntax_on") | syn on | endif
 
As you can see, I added one event -- if you've written the buffer,
presumably its safe to syntax highlight.

Regards,
Chip Campbell


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Rahul Nabar

On Mar 10, 2:09 pm, Charles Campbell <[hidden email]>
wrote:

> As you can see, I added one event -- if you've written the buffer,
> presumably its safe to syntax highlight.

The presumption is correct. I hadn't thought of doing that but it very
accurately ties in with the workflow and results in a very desirable
outcome: "highlighting is on as soon as user decides his edits are
complete and does a save"


> au InsertEnter * if exists("syntax_on")| syn off | endif
> au InsertLeave * syn on
> au CursorMoved * if &modified && exists("syntax_on") | syn off | endif
> au BufWritePost * if !exists("syntax_on") | syn on | endif

Works great Charles! Thank you.  ....except has another bizarre
seemingly non-local side effect.

Symptoms:

Open a file.
Take insertion point to a line I want to edit.
Press "i" and enter insert mode.
Highlighting turns off [great, as desired]
Press escape to come out of insert mode.
Press any cursor key and the cursor suddenly drops down to almost the
very bottom of the file.

I have no idea why it chooses that particular non-local location to
drop to but it seems pretty consistent. I tried taking the cursor to
another arbitrary location in the file before I saved. But still the
same symptom. It drops to the original location the moment I do the
sequence "Esc + cursor key"

Any other ideas? Are we somehow storing the cursor location and not
resetting it or some such?

--
Rahul



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Charles Campbell

RPN wrote:

> Works great Charles! Thank you.  ....except has another bizarre
> seemingly non-local side effect.
>
> Symptoms:
>
> Open a file.
> Take insertion point to a line I want to edit.
> Press "i" and enter insert mode.
> Highlighting turns off [great, as desired]
> Press escape to come out of insert mode.
> Press any cursor key and the cursor suddenly drops down to almost the
> very bottom of the file.
>
> I have no idea why it chooses that particular non-local location to
> drop to but it seems pretty consistent. I tried taking the cursor to
> another arbitrary location in the file before I saved. But still the
> same symptom. It drops to the original location the moment I do the
> sequence "Esc + cursor key"
>
> Any other ideas? Are we somehow storing the cursor location and not
> resetting it or some such?
>  
I'm afraid that I don't see that problem.  What happens without the
syntax highlighting changes; does use of the cursor cause odd behavior?  
I'm wondering if your termcap is correct.

Regards,
Chip Campbell


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Rahul Nabar

On Mar 10, 3:41 pm, Charles Campbell <[hidden email]>
wrote:
>
> I'm afraid that I don't see that problem.  What happens without the
> syntax highlighting changes; does use of the cursor cause odd behavior?  

You are right Charles. The reason seems elsewhere but is still
mysterious. Let me explain:

I did more testing. Tried commenting out each of those 4 lines and it
boils down that this is the line that is correlated with the bizarre
behavior:

au InsertLeave * syn on

But then I tried "syn off" and  "syn on" manually and the bizarre
behavior still remains. So my "syn on" seems to be having this strange
side effect of putting the cursor at the bottom of the file. How can I
change this? Relevant two lines from my .vimrc are below:

au BufRead,BufNewFile *.nc.txt set filetype=ncfile
au! Syntax ncfile source ~/.vim/syntax/ncfile.vim

Essentially where does the cursor end up after parsing the file? That
is the key issue. Mine seems to fall to the end after each syn
highlighting.

A related issue:If I totally disable syn-highlighting then vim seems
to remember the last cursor point and return exactly there on
reopening the file too. Syn highlighting seems to have broken that
feature for me. I suspect somewhere I ought to save and reset cursor
position. Any tips are greatly appreciated!

A full reproduction of my syntax file (with liberal credits due to
other members here for helping me develop this fairly complicated syn
file!) might be key to solving this problem. Here it is (with some
editing to remove extraneous matter):
##########################################################
let b:current_syntax = "ncfile"

syn sync clear
syn sync fromstart

syn keyword Identifier  DynamicAtomPositions
[snip]

syn match   Delimiter   "{\|}"

syn region MaskReg start=/^ DynamicAtomAttributes =/ end=/;/ keepend
contains=Mask,L?
syn match Mask /^ DynamicAtomAttributes =.*\n/ nextgroup=L1 contained

syn region PosReg start=/^ DynamicAtomPositions =/ end=/;/ keepend
contains=Pos,L?
syn match Pos /^ DynamicAtomPositions =.*\n/ nextgroup=L1 contained

syn match L1 /.*\n/ nextgroup=L2 contained
syn match L2 /.*\n/ nextgroup=L3 contained
[snip]
syn match L29 /.*\n/ nextgroup=L30 contained
syn match L30 /.*\n/ nextgroup=L31 contained

syn keyword C C
[snip]

"adsorbate atoms
hi C      ctermfg=Black
[snip]

func! Update()
  let s = search('^ DynamicAtomSpecies =','w')
  let e = search(';')
  for l in range(s+1,e)
    let atom = matchstr(getline(l),'\w\+')
    exec 'hi link L'.(l-s).' '.atom
  endfor
endfun
call Update()
finish

######################

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Matt Wozniski-2

On Tue, Mar 10, 2009 at 6:32 PM, RPN wrote:
> But then I tried "syn off" and  "syn on" manually and the bizarre
> behavior still remains. So my "syn on" seems to be having this strange
> side effect of putting the cursor at the bottom of the file. How can I
> change this? Relevant two lines from my .vimrc are below:
>
> au BufRead,BufNewFile *.nc.txt set filetype=ncfile
> au! Syntax ncfile source ~/.vim/syntax/ncfile.vim

Setting filetype=ncfile will automatically load syntax/ncfile.vim -
you don't need the Syntax autocmd, at best it's just making the file
be read twice.

> Essentially where does the cursor end up after parsing the file? That
> is the key issue. Mine seems to fall to the end after each syn
> highlighting.
>
> A related issue:If I totally disable syn-highlighting then vim seems
> to remember the last cursor point and return exactly there on
> reopening the file too. Syn highlighting seems to have broken that
> feature for me. I suspect somewhere I ought to save and reset cursor
> position. Any tips are greatly appreciated!

It's exactly the same issue - your syntax highlighting script moves the cursor.

> func! Update()
>  let s = search('^ DynamicAtomSpecies =','w')
>  let e = search(';')

^ search() moves the cursor unless you give it the 'n' flag!

>  for l in range(s+1,e)
>    let atom = matchstr(getline(l),'\w\+')
>    exec 'hi link L'.(l-s).' '.atom
>  endfor
> endfun
> call Update()

~Matt

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Rahul Nabar

On Mar 11, 5:13 am, Matt Wozniski <[hidden email]> wrote:
> On Tue, Mar 10, 2009 at 6:32 PM, RPN wrote:

>  for l in range(s+1,e)
>    let atom = matchstr(getline(l),'\w\+')
>    exec 'hi link L'.(l-s).' '.atom
>  endfor
> endfun
> call Update()
>
> It's exactly the same issue - your syntax highlighting script moves the cursor.
>
> ^ search() moves the cursor unless you give it the 'n' flag!

Thanks for the info Matt. I think I have solved it now. I tried just
adding the 'n' flag to both my searches. But that screws up the
program logic. I think the way it was written it actually depends on
the cursor moving to where it does after the search is done.

Then I added two lines to save and restore the cursor position around
the body of the Update position. That seems to do the trick (unless
there is something that I still miss and haven't yet discovered!).

##########################
func! Update()
    let save_cursor = getpos(".")

    let s = search('^ DynamicAtomSpecies =','w')
    let e = search(';')

    [function cruft]

    call setpos('.', save_cursor)
endfun
#############################


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Rahul Nabar
In reply to this post by Charles Campbell



On Mar 10, 2:09 pm, Charles Campbell <[hidden email]>
wrote:
> Try the following:
>
> au InsertEnter * if exists("syntax_on")| syn off | endif
> au InsertLeave * syn on
> au CursorMoved * if &modified && exists("syntax_on") | syn off | endif
> au BufWritePost * if !exists("syntax_on") | syn on | endif
>
> As you can see, I added one event -- if you've written the buffer,
> presumably its safe to syntax highlight.


Chris, in hindsight the extra logic (although tempting) is a failure.
The reason is that the logic of lines 2 and 3 is mutually incompatible
in a subtle sense (assuming I understand vim correctly!). Just noticed
that while analyzing another side effect:

Symptoms:
I go into insert mode -> Highlighting disappears [good!]
I come out of insert mode with an Esc. ->highlighting re-appears.
[great]
I press any cursor key -> Highlighting disappears [crap; this is not
what we wanted!]

As I understand it, even if Line 2 ( au InsertLeave * syn on) succeeds
in turning HL on as soon as I leave insert mode the next line 3 (au
CursorMoved * if &modified && exists("syntax_on") | syn off | endif )
reacts to the cursor movement immediately after. And it "sees" the
buffer changes made during the Insert mode via the "modified" tag and
hence immediately turns HL off.

Hard to spot! Of course, the design depends on the fact the Inser mode
changes returned the file to a consistent state suitlble for syn-HL
again.

As far as I can see my best bet might be to comment out the "extra
logic" "au InsertLeave * syn on" totally. Thus vim takes care of
turning HL off but turning it on would be upto  the user manually (or
post a write). Not so satisfactory.  I had really liked the "au
InsertLeave * syn on" bit!

If you see any ways around do let me know!

--
Rahul
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how to automagically run a "syn off" the moment I start "editing" a file.

Cyril Slobin-4

On Wed, Mar 11, 2009 at 8:17 PM, RPN <[hidden email]> wrote:

> Chris, in hindsight the extra logic (although tempting) is a failure.
> The reason is that the logic of lines 2 and 3 is mutually incompatible
> in a subtle sense (assuming I understand vim correctly!).

I am using a similar trick in my .vimrc: instead of changing syntax on and off,
I change the color of the status line depending of modified state of the buffer.
I believe the same technique may be applied to the problem in question.
Quotation from my .vimrc:

" Bogus value, just not to leave it undefined
let was_modified = 2

autocmd BufEnter,BufReadPost,BufWritePost * call WhenModified(1)
autocmd CursorMoved,CursorMovedI * call WhenModified(0)

function! WhenModified(force)
  if a:force || &modified != g:was_modified
    execute 'setlocal statusline=%' . &modified . '*%t\ %L'
        \ . '\ %y%r[%{&tw}]%=\ %{Moon()}\ %{Lang()}\ %{Icon()}'
        \ . '\ %{&fenc}\ %m\ %-15(%{HexDec()}%)%-10(%l,%v%)%P'
    let g:was_modified = &modified
  endif
endfunction

Instead of my long "execute 'setlocal statusline...'" you can put any command
at your wish. A bit complex, but works for me.

--
http://slobin.pp.ru/ `When I use a word,' Humpty Dumpty said,
<[hidden email]> `it means just what I choose it to mean'

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---