Friday, August 3, 2012

vim: vim session vs. content based file type detection, and vim ft execution sequence...

experts:

2 issues about the file type auto detection script here
(sorry for the long tedious email, but the issue drive me crazy...)

I have created a small script : .vim/ftdetect/myft.vim

--------to start the story:
my intention is to inspect each txt file that vim is going to open,
check a couple of lines and see if it "looks like" an asciidoc or some
other my own defined file type.
it works good if I just open files in the normal way:
vim myfile.txt

--------the issue:
It doesn't work when I open a vim "session":
vim -S mysession

--------the reason is,
I have a line in my script to auto check the filesize,
let s:fsize = getfsize(expand('%%:p'))
if it's too large (>2M), set ft to asciidoc2 (a heavy syntax file) will
make the file operation (jump, modify,etc) quite slow, so in that case



--------the question1:
so question 1: I think the getfsize doesn't work with vim session?
any workaround?

--------question 2:
the sequence that vim run the scripts looks still not what I expected.

in my case, I want to enable/disable global var MyAsciidocDebug in
.vimrc , but it looks initially MyAsciidocDebug is 0 in line 15, but
then it got set to 1 (by .vimrc) after execution autocmd in line 37.
that's why when I open a txt file I got:

MyAsciidocDebug is 0 <---
filesize is 1947
"temp1.txt" 50L, 1947C
g:MyAsciidocDebug is 1 <--- got set by .vimrc ?
asciidoc mark not found
g:MyAsciidocDebug is 1
asciidoc mark ^= xxx found in 10lines, not a huge file,ft set to
asciidoc2...

:scriptname I got this:

1: /usr/share/vim/vimrc
2: /usr/share/vim/vim73/syntax/syntax.vim
3: /usr/share/vim/vim73/syntax/synload.vim
4: /usr/share/vim/vim73/syntax/syncolor.vim
5: /usr/share/vim/vim73/filetype.vim
6: /home/ping/.vim/ftdetect/csv.vim
7: /home/ping/.vim/ftdetect/mkd.vim
8: /home/ping/.vim/ftdetect/myft.vim <---step 1)
9: /home/ping/.vim/ftdetect/taskpaper.vim
10: /etc/vim/ftdetect/asciidoc_filetype.vim
11: /home/ping/.vimrc <---step 2)


but, per the debug output, it looks vim execute file 8(.vim/ftdetect),
then the autocmd make the execution change to file 11(.vimrc), then jump
back to file 8(vimrc) again?


I get a feeling that file type auto detection is of too complex a
machanism to learn, I'm thinking maybe vim mode line is a better/easy
method (but you have to modify the file)...



--------my problematic script:
8 if !exists("MyAsciidocDebug")
9 let MyAsciidocDebug = 0
10 endif
11
12 " ping: add file check
13 let s:largefilesize = 2000000
14 let s:fsize = getfsize(expand('%%:p')) "get current file size
15 echom "MyAsciidocDebug is" MyAsciidocDebug
16 if MyAsciidocDebug | echom "filesize is" s:fsize | endif
17 echom "filesize is" s:fsize
18 "
19 au BufRead *.asciidoc,README,TODO,CHANGELOG,NOTES
setfiletype=asciidoc2
20 au BufRead *.txt call s:FTasciidoc()
22


26 function! s:FTasciidoc()
27
28 " ping: add another check, set asciidoc if there is a "= ABC"
preceded with a blank line
29 let n = 1 "starting from 1st line
30 while n < 10 "checking 1st 10 lines
31 let line = getline(n) "get a line
32 let n = n + 1
33 if line =~ '^=\+\s\S\+' "if exact one space bet = and
text(make it strict)
34 if getline(n-2) =~ '^\s*$' "and an empty line exist
right before this line
35 if (s:fsize <= s:largefilesize)
36 set filetype=asciidoc2 "set simplest ft
37 echom "g:MyAsciidocDebug is" g:MyAsciidocDebug
38 if g:MyAsciidocDebug | echom "asciidoc mark ^= xxx
found in 10lines, not a huge file,ft set to asciidoc2..." | endif
39 else
40 set filetype=asciidoc
41 echom "g:MyAsciidocDebug is" g:MyAsciidocDebug
42 if g:MyAsciidocDebug | echom "asciidoc mark ^= xxx
found in 10lines, a huge file(over 2M!),ft set to asciidoc" | endif
43 endif
44 return
45 else
46 echom "g:MyAsciidocDebug is" g:MyAsciidocDebug
47 if g:MyAsciidocDebug | echom "asciidoc mark = ^xxx found,
but no empty line ahead, ft not set to asciidoc" | endif
48 endif
49 else
50 echom "g:MyAsciidocDebug is" g:MyAsciidocDebug
51 if g:MyAsciidocDebug | echom "asciidoc mark not found" | endif
52 endif
53 endwhile

...
...

--
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

No comments: