Toggling the concealed attribute for a syntax highlight in VIM

拜拜、爱过 提交于 2020-01-04 04:03:54

问题


I currently have a syntax file that parses a log file, very similar to the following [this is for 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

I'd like to, using a map, toggle whether a given group (let's say, syslogHost) is concealed or not. Is there any way to do something like the following pseudocode:

map <leader>ch :syn match syslogHost conceal!

and thereby toggle the definition of syslogHost between:

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

and

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

Thanks.


回答1:


I think the only way to do this is with a function:

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



回答2:


Here's a modification to @DrAl's original one, that works with both match- and region-type syntax groups.

" 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



回答3:


You can also change the syntax of the file. I have a few conceal rules for my json files that I don't for my javascript files (e.g., I hide quote marks and trailing commas to make the files easier to read.) so when I want to see the whole file I just use set syntax=javascript. You could probably go one further and just define two sets of rules for file types (e.g., syslog.vim and syslog2.vim) where 1 inherits all from 2 and then applies conceal rules, then you could bind a key to toggle between them.



来源:https://stackoverflow.com/questions/3853631/toggling-the-concealed-attribute-for-a-syntax-highlight-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!