主題
why is syntax?
why use syntax by different file?
預期:
理解並照著實作
答案:
實作方法
|44.1| Basic syntax commands
|44.2| Keywords
|44.3| Matches
|44.4| Regions
|44.5| Nested items
|44.6| Following groups
|44.7| Other arguments
|44.8| Clusters
|44.9| Including another syntax file
|44.10| Synchronizing
|44.11| Installing a syntax file
|44.12| Portable syntax file layout
-----------------------------------
44.1 基本syntax commands
1.其存放位置,$VIMRUNTIME/syntax for a language
2.基本command
取消syntax
syntax clear
檢查目前syntax
:syntax
查看其item in a specific syntax group use
:syntax list {group-name}
44.2 keywords
最基本的syntax使用,當有多個group同時存在不同command時.以keyword為主
:syntax keyword {group} {keyword}...
( :syntax keyword xStatement if then else endi)
特別注意的是其keyword必須是在"iskeyword"
像是The x language uses the '-' character in keywords.
:setlocal iskeyword+=-
:syntax keyword xStatement when-not
縮寫:
:syntax keyword xStatement n[ext]
44.3 Matches
當你想要去定義的字串很複雜時,(像是函式名命 xxx+"()")
syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>[^()]*)("me=e-2
syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>\s*("me=e-1
*/bar* */\bar* */pattern*(see usr_27.txt)
44.4 Regions
其字串是被某一符號所包含(",{,'...)時,我們可以把它視為一個regions
:syntax region xString start=/"/ skip=/\\"/ end=/"/
何時使用region取代match?
主要是match只有當一個pattern,which must match as a whole.
而region,是從start開始的pattern,end 結束pattern(若你只需考慮start而不需要end patter時,你就只需用match)
44.5 nested ltems
即指的是,已在一個定義中,再去裡面找它的東西
如
#Test is a int variable.
:syntax keyword xTodo int contained
:syntax match xComment /#.*/ contains=xTodo
hi xComment guifg=#....
hi xTodo guifg=#...
#Test is a int variable.
也可以包含自己
如: :syntax region xBlock start=/{/ end=/}/ contains=xBlock
see :help syn-contains
44.6 following groups
如下,若想要去把"if"、(condition)、then分別其標示,則
if (condition) then
:syntax match xIf /if/ nextgroup=xIfCondition skipwhite
:syntax match xIfCondition /([^)]*)/ contained nextgroup=xThen skipwhite
:syntax match xThen /then/ contained
其中,
nextgroup是指其xIfCondition"可能"會出現(即也可能沒有)
skipwhite則是"可能"會有spaces and tab在group id中間出現.
44.7 other arguments
MATCHGROUP
To highlight the text enclosed in parentheses () with the group xInside
:syntax region xInside start=/(/ end=/)/
(test the matchgroup)
you want to highlight the parentheses differently. You can do this with a lot of convoluted region statements, or you can use the "matchgroup" argument.
:syntax region xInside matchgroup=xParen start=/(/ end=/)/
(test the matchgroup)
這樣子,是告訴vim將region的起始及終點,標示為不同
"一個副作用是被包含的項目不會在區域的開頭或結尾匹配"
TRANSPARENT(轉成parent)
If the "transparent" argument is given, this item will not be highlighted
itself, but will take the highlighting of the item it is contained in.
不會自己hightlight,會依所包含的為group 的標示來highlight
syn region cCondNest start=/(/ end=/)/ contained transparent
syn region cWhile matchgroup=cWhile start=/while\s*(/ end=/)/
\ contains=cCondNest
syn region cFor matchgroup=cFor start=/for\s*(/ end=/)/
\ contains=cCondNest
需matchgroup by self is ?
the side effect of using a matchgroup is
that contained items are not found in the match with the start item then.
This avoids that the cCondNest group matches the ( just after the "while" or
"for". If this would happen, it would span the whole text until the matching
) and the region would continue after it. Now cCondNest only matches after
the match with the start pattern, thus after the first (.
沒有留言:
張貼留言