├── plugin └── SyntaxRange.vim ├── README ├── autoload └── SyntaxRange.vim └── doc └── SyntaxRange.txt /plugin/SyntaxRange.vim: -------------------------------------------------------------------------------- 1 | " SyntaxRange.vim: Define a different filetype syntax on regions of a buffer. 2 | " 3 | " DEPENDENCIES: 4 | " - Requires Vim 7.0 or higher. 5 | " - SyntaxRange.vim autoload script 6 | " 7 | " Copyright: (C) 2012 Ingo Karkat 8 | " The VIM LICENSE applies to this script; see ':help copyright'. 9 | " 10 | " Maintainer: Ingo Karkat 11 | " 12 | " REVISION DATE REMARKS 13 | " 1.00.002 13-Aug-2012 Add syntax completion to :SyntaxInclude. 14 | " 001 05-Jul-2012 file creation 15 | 16 | " Avoid installing twice or when in unsupported Vim version. 17 | if exists('g:loaded_SyntaxRange') || (v:version < 700) 18 | finish 19 | endif 20 | let g:loaded_SyntaxRange = 1 21 | 22 | command! -bar -range SyntaxIgnore call SyntaxRange#SyntaxIgnore(, ) 23 | if v:version < 703 24 | command! -bar -range -nargs=1 SyntaxInclude call SyntaxRange#SyntaxInclude(, , ) 25 | else 26 | command! -bar -range -nargs=1 -complete=syntax SyntaxInclude call SyntaxRange#SyntaxInclude(, , ) 27 | endif 28 | 29 | " vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax : 30 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is a mirror of http://www.vim.org/scripts/script.php?script_id=4168 2 | 3 | DESCRIPTION 4 | This plugin provides commands and functions to set up regions in the current 5 | buffer that either use a syntax different from the buffer's 'filetype', or 6 | completely ignore the syntax. 7 | 8 | SEE ALSO 9 | - If you also want different buffer options (like indent settings, etc.) for 10 | each syntax region, the OnSyntaxChange.vim plugin (vimscript #4085) allows 11 | you to dynamically change the buffer options as you move through the buffer. 12 | 13 | RELATED WORKS 14 | - If the highlighting doesn't work properly, you could alternatively edit the 15 | range(s) in a separate scratch buffer. Plugins like NrrwRgn (vimscript #3075) 16 | provide commands to set these up, with automatic syncing back to the 17 | original buffer. 18 | 19 | SOURCE 20 | The code to include a different syntax in a region is based on 21 | http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file 22 | 23 | USAGE 24 | For quick, ad-hoc manipulation of the syntax withing a range of lines, the 25 | following commands are provided: 26 | 27 | :[range]SyntaxIgnore Ignore the buffer's filetype syntax for the current 28 | line / lines in [range]. (Top-level keywords will 29 | still be highlighted.) 30 | This can be a useful fix when some text fragments 31 | confuse the syntax highlighting. (For example, when 32 | buffer syntax set to an inlined here-document is 33 | negatively affected by the foreign code surrounding 34 | the here-document.) 35 | 36 | :[range]SyntaxInclude {filetype} 37 | Use the {filetype} syntax for the current line / lines 38 | in [range]. 39 | 40 | Line numbers in [range] are fixed; i.e. they do not 41 | adapt to inserted / deleted lines. But when in a 42 | range, the last line ($) is interpreted as "end of 43 | file". 44 | 45 | For finer control and use in custom mappings or syntax tweaks, the following 46 | functions can be used. You'll find the details directly in the 47 | .vim/autoload/SyntaxRange.vim implementation file. 48 | 49 | SyntaxRange#Include( startPattern, endPattern, filetype, ... ) 50 | Use the {filetype} syntax for the region defined by 51 | {startPattern} and {endPattern}. 52 | SyntaxRange#IncludeEx( regionDefinition, filetype ) 53 | Use the {filetype} syntax for the region defined by 54 | {regionDefinition}. 55 | 56 | EXAMPLE 57 | To highlight the text between the markers 58 | @begin=c@ 59 | int i = 42; 60 | @end=c@ 61 | with C syntax, and make the markers themselves fade into the background: 62 | :call SyntaxRange#Include('@begin=c@', '@end=c@', 'c', 'NonText') 63 | 64 | To highlight inline patches inside emails: 65 | :call SyntaxRange#IncludeEx('start="^changeset\|^Index: \|^diff \|^--- .*\%( ----\)\@ 13 | " 14 | " REVISION DATE REMARKS 15 | " 1.02.003 30-Mar-2015 Handle :.SyntaxInclude and :.SyntaxIgnore on 16 | " folded lines correctly. Use 17 | " ingo#range#NetStart/End(). 18 | " Set main_syntax to the buffer's syntax during 19 | " :syntax include of the subordinate syntax 20 | " script. Some scripts may make special 21 | " arrangements when included. Suggested by OOO. 22 | " 1.01.002 23-Apr-2013 Avoid "E108: No such variable: b:current_syntax" 23 | " when the (misbehaving) included syntax doesn't 24 | " set it. Reported by o2genum at 25 | " http://stackoverflow.com/a/16162412/813602. 26 | " 1.00.001 05-Jul-2012 file creation 27 | let s:save_cpo = &cpo 28 | set cpo&vim 29 | 30 | function! SyntaxRange#Include( startPattern, endPattern, filetype, ... ) 31 | "****************************************************************************** 32 | "* PURPOSE: 33 | " Define a syntax region from a:startPattern to a:endPattern that includes the 34 | " syntax for a:filetype. For the common case, this automatically ensures that 35 | " a contained match does not extend beyond a:endPattern (though contained 36 | " syntax items with |:syn-extend| break that), and that the patterns are also 37 | " matched inside all existing (also contained) syntax items. 38 | "* ASSUMPTIONS / PRECONDITIONS: 39 | " None. 40 | "* EFFECTS / POSTCONDITIONS: 41 | " Defines a syntax region synInclude{filetype} for the current buffer. 42 | "* INPUTS: 43 | " a:startPattern Regular expression that specifies the beginning of the 44 | " region |:syn-start|. 45 | " a:endPattern Regular expression that specifies the end of the region 46 | " |:syn-end|. 47 | " a:filetype The filetype syntax to use in the region. 48 | " a:matchGroup Optional highlight group for the a:startPattern and 49 | " a:endPattern matches themselves |:syn-matchgroup|. 50 | "* RETURN VALUES: 51 | " None. 52 | "****************************************************************************** 53 | call SyntaxRange#IncludeEx( 54 | \ printf('%s keepend start="%s" end="%s" containedin=ALL', 55 | \ (a:0 ? 'matchgroup=' . a:1 : ''), 56 | \ a:startPattern, 57 | \ a:endPattern 58 | \ ), 59 | \ a:filetype 60 | \) 61 | endfunction 62 | function! SyntaxRange#IncludeEx( regionDefinition, filetype ) 63 | "****************************************************************************** 64 | "* PURPOSE: 65 | " Define a syntax region from a:regionDefinition that includes the syntax for 66 | " a:filetype. Use this extended function when you have multiple start- or end 67 | " patterns, skip patterns, want to specify match offsets, control the 68 | " containment, etc. 69 | "* ASSUMPTIONS / PRECONDITIONS: 70 | " None. 71 | "* EFFECTS / POSTCONDITIONS: 72 | " Defines a syntax region synInclude{filetype} for the current buffer. 73 | "* INPUTS: 74 | " a:regionDefinition |:syn-region| definition with at least |:syn-start| and 75 | " |:syn-end|. 76 | " a:filetype The filetype syntax to use in the region. 77 | "* RETURN VALUES: 78 | " None. 79 | "****************************************************************************** 80 | let l:syntaxGroup = 'synInclude' . toupper(a:filetype[0]) . tolower(a:filetype[1:]) 81 | 82 | if exists('b:current_syntax') 83 | let l:current_syntax = b:current_syntax 84 | " Remove current syntax definition, as some syntax files (e.g. cpp.vim) 85 | " do nothing if b:current_syntax is defined. 86 | unlet b:current_syntax 87 | endif 88 | 89 | if ! exists('g:main_syntax') && ! empty(&l:syntax) 90 | let g:main_syntax = &l:syntax 91 | let l:hasSetMainSyntax = 1 92 | endif 93 | 94 | execute printf('syntax include @%s syntax/%s.vim', l:syntaxGroup, a:filetype) 95 | 96 | if exists('l:hasSetMainSyntax') 97 | unlet! g:main_syntax 98 | endif 99 | 100 | if exists('l:current_syntax') 101 | let b:current_syntax = l:current_syntax 102 | else 103 | unlet! b:current_syntax 104 | endif 105 | 106 | execute printf('syntax region %s %s contains=@%s', 107 | \ l:syntaxGroup, 108 | \ a:regionDefinition, 109 | \ l:syntaxGroup 110 | \) 111 | endfunction 112 | 113 | 114 | function! SyntaxRange#SyntaxIgnore( startLnum, endLnum ) 115 | let [l:startLnum, l:endLnum] = [ingo#range#NetStart(a:startLnum), ingo#range#NetEnd(a:endLnum)] 116 | if l:startLnum == l:endLnum 117 | execute printf('syntax match synIgnoreLine /\%%%dl/', l:startLnum) 118 | elseif l:startLnum < l:endLnum && l:endLnum == line('$') 119 | execute printf('syntax match synIgnoreLine /\%%>%dl/', (l:startLnum - 1)) 120 | else 121 | execute printf('syntax match synIgnoreLine /\%%>%dl\%%<%dl/', (l:startLnum - 1), (l:endLnum + 1)) 122 | endif 123 | endfunction 124 | 125 | function! SyntaxRange#SyntaxInclude( startLnum, endLnum, filetype ) 126 | let [l:startLnum, l:endLnum] = [ingo#range#NetStart(a:startLnum), ingo#range#NetEnd(a:endLnum)] 127 | call SyntaxRange#Include( 128 | \ printf('\%%%dl', l:startLnum), 129 | \ (l:startLnum < l:endLnum && l:endLnum == line('$') ? 130 | \ '\%$' : 131 | \ printf('\%%%dl', (l:endLnum + 1)) 132 | \ ), 133 | \ a:filetype 134 | \) 135 | endfunction 136 | 137 | let &cpo = s:save_cpo 138 | unlet s:save_cpo 139 | " vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax : 140 | -------------------------------------------------------------------------------- /doc/SyntaxRange.txt: -------------------------------------------------------------------------------- 1 | *SyntaxRange.txt* Define a different filetype syntax on regions of a buffer. 2 | 3 | SYNTAX RANGE by Ingo Karkat 4 | *SyntaxRange.vim* 5 | description |SyntaxRange-description| 6 | usage |SyntaxRange-usage| 7 | installation |SyntaxRange-installation| 8 | integration |SyntaxRange-integration| 9 | limitations |SyntaxRange-limitations| 10 | known problems |SyntaxRange-known-problems| 11 | todo |SyntaxRange-todo| 12 | history |SyntaxRange-history| 13 | 14 | ============================================================================== 15 | DESCRIPTION *SyntaxRange-description* 16 | 17 | This plugin provides commands and functions to set up regions in the current 18 | buffer that either use a syntax different from the buffer's 'filetype', or 19 | completely ignore the syntax. 20 | 21 | SEE ALSO * 22 | 23 | - If you also want different buffer options (like indent settings, etc.) for 24 | each syntax region, the OnSyntaxChange.vim plugin (vimscript #4085) allows 25 | you to dynamically change the buffer options as you move through the buffer. 26 | 27 | RELATED WORKS * 28 | 29 | - If the highlighting doesn't work properly, you could alternatively edit the 30 | range(s) in a separate scratch buffer. Plugins like NrrwRgn (vimscript #3075) 31 | provide commands to set these up, with automatic syncing back to the 32 | original buffer. 33 | 34 | SOURCE * 35 | 36 | The code to include a different syntax in a region is based on 37 | http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file 38 | 39 | ============================================================================== 40 | USAGE *SyntaxRange-usage* 41 | 42 | For quick, ad-hoc manipulation of the syntax withing a range of lines, the 43 | following commands are provided: 44 | *:SyntaxIgnore* 45 | :[range]SyntaxIgnore Ignore the buffer's filetype syntax for the current 46 | line / lines in [range]. (Top-level keywords will 47 | still be highlighted.) 48 | This can be a useful fix when some text fragments 49 | confuse the syntax highlighting. (For example, when 50 | buffer syntax set to an inlined here-document is 51 | negatively affected by the foreign code surrounding 52 | the here-document.) 53 | *:SyntaxInclude* 54 | :[range]SyntaxInclude {filetype} 55 | Use the {filetype} syntax for the current line / lines 56 | in [range]. 57 | 58 | Line numbers in [range] are fixed; i.e. they do not 59 | adapt to inserted / deleted lines. But when in a 60 | range, the last line ($) is interpreted as "end of 61 | file". 62 | 63 | 64 | For finer control and use in custom mappings or syntax tweaks, the following 65 | functions can be used. You'll find the details directly in the 66 | .vim/autoload/SyntaxRange.vim implementation file. 67 | 68 | SyntaxRange#Include( startPattern, endPattern, filetype, ... ) 69 | Use the {filetype} syntax for the region defined by 70 | {startPattern} and {endPattern}. 71 | SyntaxRange#IncludeEx( regionDefinition, filetype ) 72 | Use the {filetype} syntax for the region defined by 73 | {regionDefinition}. 74 | 75 | EXAMPLE *SyntaxRange-example* 76 | 77 | To highlight the text between the markers 78 | @begin=c@ ~ 79 | int i = 42; ~ 80 | @end=c@ ~ 81 | with C syntax, and make the markers themselves fade into the background: > 82 | :call SyntaxRange#Include('@begin=c@', '@end=c@', 'c', 'NonText') 83 | 84 | 85 | To highlight inline patches inside emails: > 86 | :call SyntaxRange#IncludeEx('start="^changeset\|^Index: \|^diff \|^--- .*\%( ----\)\@ 97 | vim SyntaxRange*.vmb.gz 98 | :so % 99 | To uninstall, use the |:RmVimball| command. 100 | 101 | DEPENDENCIES *SyntaxRange-dependencies* 102 | 103 | - Requires Vim 7.0 or higher. 104 | - Requires the |ingo-library.vim| plugin (vimscript #4433), version 1.022 or 105 | higher. 106 | 107 | ============================================================================== 108 | INTEGRATION *SyntaxRange-integration* 109 | 110 | To automatically include a syntax in a certain {filetype}, you can put the 111 | command into a script in > 112 | ~/.vim/after/syntax/{filetype}/SyntaxInclude.vim 113 | If you want to include a syntax in several (or even all) syntaxes, you can put 114 | this into your |vimrc|: > 115 | :autocmd Syntax * call SyntaxRange#Include(...) 116 | < 117 | ============================================================================== 118 | LIMITATIONS *SyntaxRange-limitations* 119 | 120 | - The original filetype's syntax may interfere with the syntax range, and vice 121 | versa. To define the range with high priority, the commands inject it with 122 | "containedin=ALL". 123 | 124 | KNOWN PROBLEMS *SyntaxRange-known-problems* 125 | 126 | TODO *SyntaxRange-todo* 127 | 128 | IDEAS *SyntaxRange-ideas* 129 | 130 | ============================================================================== 131 | HISTORY *SyntaxRange-history* 132 | 133 | 1.02 23-Apr-2015 134 | - Set main_syntax to the buffer's syntax during :syntax include of the 135 | subordinate syntax script. Some scripts may make special arrangements when 136 | included. Suggested by OOO. 137 | - Handle :.SyntaxInclude and :.SyntaxIgnore on folded lines correctly. Use 138 | ingo#range#NetStart/End(). 139 | - Add dependency to ingo-library (vimscript #4433). *** You need to separately 140 | install ingo-library (vimscript #4433) version 1.022 (or higher)! *** 141 | 142 | 1.01 21-Nov-2013 143 | Avoid "E108: No such variable: b:current_syntax" when the (misbehaving) 144 | included syntax doesn't set it. Reported by o2genum at 145 | http://stackoverflow.com/a/16162412/813602. 146 | 147 | 1.00 13-Aug-2012 148 | First published version. 149 | 150 | 0.01 05-Jul-2012 151 | Started development. 152 | 153 | ============================================================================== 154 | Copyright: (C) 2012-2015 Ingo Karkat 155 | The VIM LICENSE applies to this plugin; see |copyright|. 156 | 157 | Maintainer: Ingo Karkat 158 | ============================================================================== 159 | vim:tw=78:ts=8:ft=help:norl: 160 | --------------------------------------------------------------------------------