簡體   English   中英

在VIM中切換語法高亮的隱藏屬性

[英]Toggling the concealed attribute for a syntax highlight in VIM

我目前有一個解析日志文件的語法文件,非常類似於以下[這是針對syslog]:

syn match   syslogText  /.*$/
syn match   syslogFacility  /.\{-1,}:/  nextgroup=syslogText skipwhite
syn match   syslogHost  /\S\+/  nextgroup=syslogFacility,syslogText skipwhite
syn match   syslogDate  /^.\{-}\d\d:\d\d:\d\d/  nextgroup=syslogHost skipwhite

我想,使用地圖,切換是否隱藏給定組(例如,syslogHost)。 有沒有辦法做類似下面的偽代碼:

map <leader>ch :syn match syslogHost conceal!

從而切換syslogHost的定義:

syn match   syslogHost  /\S\+/  nextgroup=syslogFacility,syslogText skipwhite

syn match   syslogHost  /\S\+/  nextgroup=syslogFacility,syslogText skipwhite conceal

謝謝。

認為這樣做的唯一方法是使用一個函數:

map <leader>ch :call ToggleGroupConceal('sysLogHost')<CR>

function! ToggleGroupConceal(group)
    " Get the existing syntax definition
    redir => syntax_def
    exe 'silent syn list' a:group
    redir END
    " Split into multiple lines
    let lines = split(syntax_def, "\n")
    " Clear the existing syntax definitions
    exe 'syn clear' a:group
    for line in lines
            " Only parse the lines that mention the desired group
            " (so don't try to parse the "--- Syntax items ---" line)
        if line =~ a:group
                    " Get the required bits (the syntax type and the full definition)
            let matcher = a:group . '\s\+xxx\s\+\(\k\+\)\s\+\(.*\)'
            let type = substitute(line, matcher, '\1', '')
            let definition = substitute(line, matcher, '\2', '')
                    " Either add or remove 'conceal' from the definition
            if definition =~ 'conceal'
                let definition = substitute(definition, ' conceal\>', '', '')
                exe 'syn' type a:group definition
            else
                exe 'syn' type a:group definition 'conceal'
            endif
        endif
    endfor
endfunction

這是對@ DrAl原始版本的修改,適用於匹配和區域類型的語法組。

" function to toggle conceal attribute for a syntax group
function! ToggleGroupConceal(group)
    " Get the existing syntax definition for specified group
    redir => syntax_def
    exe 'silent syn list' a:group
    redir END
    " Split into multiple lines
    let lines = split(syntax_def, "\n")
    " Clear the existing syntax definitions
    exe 'syn clear' a:group
    for line in lines
        " Only parse lines that contain the desired group's definition
        " (skip "--- Syntax items ---" line and 'links to' lines)
        if line =~ a:group
            "echo 'line = ' . line
            " need to handle match and region types separately since type
            " isn't specified for regions in the syn list output like it is
            " for matches.
            let type = substitute(line, '\v' . a:group . '\s+xxx\s+(\k+)\s+(.*)', '\1', '')
            if type == 'match'
                " get args as everything after 'xxx match'
                let args = substitute(line, '\v' . a:group . '\s+xxx\s+(\k+)\s+(.*)', '\2', '')
            else
                " get args as everything after 'xxx'
                let args = substitute(line, '\v' . a:group . '\s+xxx\s+(.*)', '\1', '')
                if args =~ 'start' && args =~ 'end'
                    let type = 'region'
                endif
            endif
            "echo '    type = ' . type
            "echo '    args = ' . args
            " Either add or remove 'conceal' from the definition
            if args =~ 'conceal'
                exe 'syn' type a:group substitute(args, ' conceal', '', '')
            else
                exe 'syn' type a:group args 'conceal'
            endif
        endif
    endfor
endfunction

您還可以更改文件的語法。 我有一些我的json文件的隱藏規則,我沒有為我的javascript文件(例如,我隱藏引號和尾隨逗號,使文件更容易閱讀。)所以,當我想看到整個文件,我只是使用set syntax=javascript 您可以更進一步,只需為文件類型定義兩組規則(例如,syslog.vim和syslog2.vim),其中1從2繼承,然后應用隱藏規則,然后您可以綁定一個鍵以在它們之間切換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM